@opengeni/worker-bundle 0.16.29 → 0.17.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. package/dist/activities/agent-turn.d.ts +85 -19
  2. package/dist/activities/api-integrations.d.ts +21 -0
  3. package/dist/activities/codemode-dispatcher.d.ts +35 -0
  4. package/dist/activities/codemode-token-renewal.d.ts +23 -0
  5. package/dist/activities/codex-capacity.d.ts +5 -5
  6. package/dist/activities/codex-image-generation.d.ts +21 -0
  7. package/dist/activities/context-compaction.d.ts +1 -0
  8. package/dist/activities/documents.d.ts +3 -2
  9. package/dist/activities/environment.d.ts +11 -8
  10. package/dist/activities/expiring-material-renewal.d.ts +1 -1
  11. package/dist/activities/file-upload-reaper.d.ts +7 -2
  12. package/dist/activities/gateway-image-generation.d.ts +39 -0
  13. package/dist/activities/gateway-video-generation.d.ts +53 -0
  14. package/dist/activities/generated-images.d.ts +112 -0
  15. package/dist/activities/git-credential-renewal.d.ts +1 -1
  16. package/dist/activities/goals.d.ts +2 -2
  17. package/dist/activities/google-drive-changes.d.ts +99 -0
  18. package/dist/activities/image-generation-operation.d.ts +74 -0
  19. package/dist/activities/image-generation-references.d.ts +25 -0
  20. package/dist/activities/knowledge-source-sync-driver.d.ts +23 -0
  21. package/dist/activities/knowledge-source-sync.d.ts +75 -0
  22. package/dist/activities/packs.d.ts +13 -26
  23. package/dist/activities/parent-wake.d.ts +1 -1
  24. package/dist/activities/retained-screenshot-reaper.d.ts +2 -2
  25. package/dist/activities/retained-screenshots.d.ts +36 -0
  26. package/dist/activities/rig-verification.d.ts +82 -5
  27. package/dist/activities/run-credential-renewal.d.ts +1 -1
  28. package/dist/activities/run-input.d.ts +3 -1
  29. package/dist/activities/sandbox-images.d.ts +14 -0
  30. package/dist/activities/sandbox-lease.d.ts +47 -9
  31. package/dist/activities/scheduled-tasks.d.ts +2 -2
  32. package/dist/activities/session-state.d.ts +4 -4
  33. package/dist/activities/streaming.d.ts +1 -1
  34. package/dist/activities/types.d.ts +71 -5
  35. package/dist/activities/video-generation-admission.d.ts +38 -0
  36. package/dist/activities/video-generation-credential.d.ts +7 -0
  37. package/dist/activities/video-generation-reconciliation.d.ts +13 -0
  38. package/dist/activities/video-media-validation.d.ts +34 -0
  39. package/dist/activities/video-output-retention.d.ts +36 -0
  40. package/dist/activities/video-reference-staging.d.ts +49 -0
  41. package/dist/activities/workflow-wake.d.ts +2 -2
  42. package/dist/activities/workspace-capture.d.ts +10 -1
  43. package/dist/activities-control-44KBWLSM.js +6885 -0
  44. package/dist/activities-control-44KBWLSM.js.map +1 -0
  45. package/dist/activities-control.d.ts +289 -0
  46. package/dist/activities-turn-K4CEDEGJ.js +14675 -0
  47. package/dist/activities-turn-K4CEDEGJ.js.map +1 -0
  48. package/dist/activities-turn.d.ts +17 -0
  49. package/dist/activities.d.ts +130 -150
  50. package/dist/activity-services.d.ts +10 -0
  51. package/dist/artifact-materializer-entry.d.ts +6 -0
  52. package/dist/artifact-materializer-entry.js +2662 -0
  53. package/dist/artifact-materializer-entry.js.map +1 -0
  54. package/dist/artifact-outbox-entry.d.ts +2 -0
  55. package/dist/artifact-outbox-entry.js +681 -0
  56. package/dist/artifact-outbox-entry.js.map +1 -0
  57. package/dist/chunk-E3RI3I33.js +828 -0
  58. package/dist/chunk-E3RI3I33.js.map +1 -0
  59. package/dist/chunk-GRFQTTSQ.js +448 -0
  60. package/dist/chunk-GRFQTTSQ.js.map +1 -0
  61. package/dist/chunk-KKCRGAGZ.js +3091 -0
  62. package/dist/chunk-KKCRGAGZ.js.map +1 -0
  63. package/dist/concurrency.d.ts +108 -5
  64. package/dist/editable-artifact-hint-broker.d.ts +49 -0
  65. package/dist/editable-artifact-materializer-launcher.d.ts +49 -0
  66. package/dist/editable-artifact-materializer-service.d.ts +108 -0
  67. package/dist/editable-artifact-materializer-storage.d.ts +12 -0
  68. package/dist/editable-artifact-materializer-subprocess.d.ts +50 -0
  69. package/dist/editable-artifact-materializer.d.ts +262 -0
  70. package/dist/editable-artifact-outbox-dispatcher.d.ts +44 -0
  71. package/dist/editable-artifact-outbox-service.d.ts +70 -0
  72. package/dist/host-export.d.ts +1 -1
  73. package/dist/index.d.ts +176 -10
  74. package/dist/index.js +662 -17374
  75. package/dist/index.js.map +1 -1
  76. package/dist/memory-pressure-guard.d.ts +57 -0
  77. package/dist/model-checkpoint-memory-collector.d.ts +22 -0
  78. package/dist/observability-metrics.d.ts +33 -0
  79. package/dist/sandbox-reaper-contract.d.ts +40 -0
  80. package/dist/sandbox-reaper-timeout.d.ts +14 -0
  81. package/dist/sandbox-resume.d.ts +34 -7
  82. package/dist/sandbox-routing.d.ts +2 -0
  83. package/dist/worker-service-lifecycle.d.ts +6 -2
  84. package/dist/workflow-bundle.js +1043 -363
  85. package/dist/workflows/activities.d.ts +7 -1
  86. package/dist/workflows/document-index.d.ts +1 -1
  87. package/dist/workflows/knowledge-source-sync.d.ts +13 -0
  88. package/dist/workflows/rig-verification.d.ts +1 -1
  89. package/dist/workflows/sandbox-reaper.d.ts +8 -0
  90. package/dist/workflows/scheduled-tasks.d.ts +1 -1
  91. package/dist/workflows/session.d.ts +2 -2
  92. package/dist/workflows/video-generation.d.ts +10 -0
  93. package/dist/workflows/workflow-wake.d.ts +1 -1
  94. package/dist/workflows.d.ts +9 -7
  95. package/package.json +34 -15
  96. package/src/activities/agent-turn.ts +1470 -246
  97. package/src/activities/api-integrations.ts +138 -0
  98. package/src/activities/codemode-dispatcher.ts +335 -0
  99. package/src/activities/codemode-token-renewal.ts +64 -0
  100. package/src/activities/codex-capacity.ts +6 -6
  101. package/src/activities/codex-image-generation.ts +56 -0
  102. package/src/activities/context-compaction.ts +38 -3
  103. package/src/activities/documents.ts +14 -4
  104. package/src/activities/environment.ts +45 -42
  105. package/src/activities/expiring-material-renewal.ts +1 -1
  106. package/src/activities/file-upload-reaper.ts +34 -3
  107. package/src/activities/gateway-image-generation.ts +146 -0
  108. package/src/activities/gateway-video-generation.ts +298 -0
  109. package/src/activities/generated-images.ts +860 -0
  110. package/src/activities/goals.ts +6 -2
  111. package/src/activities/google-drive-changes.ts +454 -0
  112. package/src/activities/image-generation-operation.ts +333 -0
  113. package/src/activities/image-generation-references.ts +118 -0
  114. package/src/activities/knowledge-source-sync-driver.ts +27 -0
  115. package/src/activities/knowledge-source-sync.ts +2840 -0
  116. package/src/activities/mcp-credentials.ts +1 -0
  117. package/src/activities/packs.ts +55 -55
  118. package/src/activities/retained-screenshot-reaper.ts +2 -2
  119. package/src/activities/retained-screenshots.ts +453 -28
  120. package/src/activities/rig-verification.ts +554 -43
  121. package/src/activities/run-input.ts +22 -6
  122. package/src/activities/sandbox-images.ts +167 -0
  123. package/src/activities/sandbox-lease.ts +1065 -483
  124. package/src/activities/scheduled-tasks.ts +77 -4
  125. package/src/activities/session-state.ts +4 -4
  126. package/src/activities/streaming.ts +42 -3
  127. package/src/activities/types.ts +87 -5
  128. package/src/activities/video-generation-admission.ts +270 -0
  129. package/src/activities/video-generation-credential.ts +34 -0
  130. package/src/activities/video-generation-reconciliation.ts +578 -0
  131. package/src/activities/video-media-validation.ts +326 -0
  132. package/src/activities/video-output-retention.ts +314 -0
  133. package/src/activities/video-reference-staging.ts +263 -0
  134. package/src/activities/workflow-wake.ts +2 -2
  135. package/src/activities/workspace-capture.ts +44 -10
  136. package/src/activities-control.ts +54 -0
  137. package/src/activities-turn.ts +40 -0
  138. package/src/activities.ts +48 -124
  139. package/src/activity-services.ts +62 -0
  140. package/src/artifact-materializer-entry.ts +35 -0
  141. package/src/artifact-outbox-entry.ts +29 -0
  142. package/src/concurrency.ts +484 -6
  143. package/src/editable-artifact-hint-broker.ts +235 -0
  144. package/src/editable-artifact-materializer-launcher.ts +515 -0
  145. package/src/editable-artifact-materializer-service.ts +787 -0
  146. package/src/editable-artifact-materializer-storage.ts +413 -0
  147. package/src/editable-artifact-materializer-subprocess.ts +956 -0
  148. package/src/editable-artifact-materializer.ts +1289 -0
  149. package/src/editable-artifact-outbox-dispatcher.ts +244 -0
  150. package/src/editable-artifact-outbox-service.ts +438 -0
  151. package/src/index.ts +277 -72
  152. package/src/memory-pressure-guard.ts +489 -0
  153. package/src/model-checkpoint-memory-collector.ts +116 -0
  154. package/src/observability-metrics.ts +199 -4
  155. package/src/sandbox-reaper-contract.ts +73 -0
  156. package/src/sandbox-reaper-timeout.ts +79 -0
  157. package/src/sandbox-resume.ts +294 -153
  158. package/src/sandbox-routing.ts +19 -24
  159. package/src/worker-service-lifecycle.ts +80 -14
  160. package/src/workflows/activities.ts +30 -0
  161. package/src/workflows/file-upload-reaper.ts +3 -1
  162. package/src/workflows/knowledge-source-sync.ts +83 -0
  163. package/src/workflows/sandbox-reaper.ts +183 -21
  164. package/src/workflows/scheduled-tasks.ts +42 -6
  165. package/src/workflows/video-generation.ts +36 -0
  166. package/src/workflows.ts +17 -1
  167. package/dist/activities/toolspace-token-renewal.d.ts +0 -23
  168. package/src/activities/toolspace-token-renewal.ts +0 -64
@@ -0,0 +1,263 @@
1
+ import { GENERATED_VIDEO_MAX_BYTES, type GenerateVideoToolInput } from "@opengeni/contracts";
2
+ import type { SealedVideoReference, SealedVideoReferenceRole } from "@opengeni/core";
3
+ import type { ObjectStorage } from "@opengeni/storage";
4
+ import {
5
+ copyVersionedObjectToVerifiedTemp,
6
+ validateImageReference,
7
+ validateVideoReference,
8
+ } from "./video-media-validation";
9
+
10
+ const MAX_IMAGE_REFERENCE_BYTES = 30 * 1024 * 1024;
11
+ const MAX_VIDEO_REFERENCE_BYTES = 200 * 1024 * 1024;
12
+ const UPLOAD_TIMEOUT_SECONDS = 15 * 60;
13
+
14
+ export type SandboxCommandRunner = (input: {
15
+ cmd: string;
16
+ workdir: string;
17
+ maxOutputTokens: number;
18
+ }) => Promise<{ stdout: string; stderr: string; exitCode: number }>;
19
+
20
+ export type InspectedSandboxVideoReference = Readonly<{
21
+ ordinal: number;
22
+ role: SealedVideoReferenceRole;
23
+ path: string;
24
+ contentType: string;
25
+ sizeBytes: number;
26
+ sha256: string;
27
+ }>;
28
+
29
+ export function videoReferencePaths(
30
+ request: GenerateVideoToolInput,
31
+ ): Array<{ role: SealedVideoReferenceRole; path: string }> {
32
+ const source = request.source;
33
+ if (!source || source.mode === "text") return [];
34
+ if (source.mode === "first_frame") return [{ role: "first_frame", path: source.imagePath }];
35
+ if (source.mode === "first_and_last_frames") {
36
+ return [
37
+ { role: "first_frame", path: source.firstFramePath },
38
+ { role: "last_frame", path: source.lastFramePath },
39
+ ];
40
+ }
41
+ if (source.mode === "image_reference") {
42
+ return [{ role: "image_reference", path: source.imagePath }];
43
+ }
44
+ return [{ role: "video_reference", path: source.videoPath }];
45
+ }
46
+
47
+ export async function inspectSandboxVideoReferences(input: {
48
+ request: GenerateVideoToolInput;
49
+ runCommand: SandboxCommandRunner;
50
+ }): Promise<InspectedSandboxVideoReference[]> {
51
+ const paths = videoReferencePaths(input.request);
52
+ const inspected: InspectedSandboxVideoReference[] = [];
53
+ for (const [ordinal, reference] of paths.entries()) {
54
+ const result = await input.runCommand({
55
+ cmd: inspectCommand(reference.path),
56
+ workdir: "/workspace",
57
+ maxOutputTokens: 4_096,
58
+ });
59
+ if (result.exitCode !== 0) throw new Error("Video reference is not a stable workspace file");
60
+ const [sizeText, sha256, prefixBase64] = result.stdout.trim().split("\t");
61
+ const sizeBytes = Number(sizeText);
62
+ if (
63
+ !Number.isSafeInteger(sizeBytes) ||
64
+ sizeBytes <= 0 ||
65
+ !/^[0-9a-f]{64}$/u.test(sha256 ?? "")
66
+ ) {
67
+ throw new Error("Video reference inspection returned invalid metadata");
68
+ }
69
+ const prefix = Buffer.from(prefixBase64 ?? "", "base64");
70
+ const contentType = sniffContentType(prefix);
71
+ const maxBytes =
72
+ reference.role === "video_reference" ? MAX_VIDEO_REFERENCE_BYTES : MAX_IMAGE_REFERENCE_BYTES;
73
+ if (sizeBytes > maxBytes) throw new Error("Video reference exceeds the supported size");
74
+ if (
75
+ (reference.role === "video_reference" && contentType !== "video/mp4") ||
76
+ (reference.role !== "video_reference" && !contentType.startsWith("image/"))
77
+ ) {
78
+ throw new Error("Video reference media type does not match its semantic role");
79
+ }
80
+ inspected.push({
81
+ ordinal,
82
+ role: reference.role,
83
+ path: reference.path,
84
+ contentType,
85
+ sizeBytes,
86
+ sha256: sha256!,
87
+ });
88
+ }
89
+ return inspected;
90
+ }
91
+
92
+ export async function uploadAndVerifyVideoReferences(input: {
93
+ storage: ObjectStorage;
94
+ references: readonly InspectedSandboxVideoReference[];
95
+ stagingKeys: readonly string[];
96
+ runCommand: SandboxCommandRunner;
97
+ tempRoot: string;
98
+ ffprobePath: string;
99
+ uploadTtlSeconds: number;
100
+ signal?: AbortSignal;
101
+ }): Promise<SealedVideoReference[]> {
102
+ if (input.references.length !== input.stagingKeys.length) {
103
+ throw new Error("Video reference staging identities do not match");
104
+ }
105
+ const sealed: SealedVideoReference[] = [];
106
+ for (const [index, reference] of input.references.entries()) {
107
+ throwIfAborted(input.signal);
108
+ const key = input.stagingKeys[index]!;
109
+ const existing = input.storage.headObject ? await input.storage.headObject(key) : null;
110
+ if (!existing) {
111
+ const upload = await input.storage.createPutUrl({
112
+ key,
113
+ contentType: reference.contentType,
114
+ sha256: reference.sha256,
115
+ expiresInSeconds: Math.min(input.uploadTtlSeconds, UPLOAD_TIMEOUT_SECONDS),
116
+ });
117
+ const result = await input.runCommand({
118
+ cmd: uploadCommand(reference, upload),
119
+ workdir: "/workspace",
120
+ maxOutputTokens: 4_096,
121
+ });
122
+ if (result.exitCode !== 0) throw new Error("Video reference staging upload failed");
123
+ }
124
+ const verified = await copyVersionedObjectToVerifiedTemp({
125
+ storage: input.storage,
126
+ key,
127
+ expectedSizeBytes: reference.sizeBytes,
128
+ expectedSha256: reference.sha256,
129
+ expectedContentType: reference.contentType,
130
+ maxBytes:
131
+ reference.role === "video_reference"
132
+ ? MAX_VIDEO_REFERENCE_BYTES
133
+ : MAX_IMAGE_REFERENCE_BYTES,
134
+ tempRoot: input.tempRoot,
135
+ ...(input.signal ? { signal: input.signal } : {}),
136
+ });
137
+ try {
138
+ if (reference.role === "video_reference") {
139
+ await validateVideoReference({ path: verified.path, ffprobePath: input.ffprobePath });
140
+ } else {
141
+ const image = await validateImageReference(verified.path);
142
+ if (image.contentType !== reference.contentType) {
143
+ throw new Error("Video image reference MIME type changed during validation");
144
+ }
145
+ }
146
+ } finally {
147
+ await verified.cleanup();
148
+ }
149
+ sealed.push({
150
+ role: reference.role,
151
+ contentSha256: reference.sha256,
152
+ contentType: reference.contentType,
153
+ byteSize: reference.sizeBytes,
154
+ });
155
+ }
156
+ return sealed;
157
+ }
158
+
159
+ export function videoReferenceStagingKey(input: {
160
+ workspaceId: string;
161
+ operationId: string;
162
+ ordinal: number;
163
+ sha256: string;
164
+ }): string {
165
+ if (!/^[0-9a-f]{64}$/u.test(input.sha256)) throw new Error("Video reference digest is invalid");
166
+ return `video-generation/staging/${input.workspaceId}/${input.operationId}/${input.ordinal}/${input.sha256}`;
167
+ }
168
+
169
+ function inspectCommand(path: string): string {
170
+ return [
171
+ "set -euo pipefail",
172
+ `requested=${shellQuote(path)}`,
173
+ 'case "$requested" in /workspace/*) ;; *) exit 41 ;; esac',
174
+ 'case "$requested" in *"/../"*|*"/./"*|*/..|*/.) exit 42 ;; esac',
175
+ 'resolved=$(realpath -e -- "$requested")',
176
+ '[ "$resolved" = "$requested" ] || exit 43',
177
+ 'exec 3<"$resolved"',
178
+ 'fd_path=$(realpath -e -- "/proc/$$/fd/3" 2>/dev/null || realpath -e -- "/dev/fd/3")',
179
+ 'case "$fd_path" in /workspace/*) ;; *) exit 44 ;; esac',
180
+ '[ -f "/proc/$$/fd/3" ] 2>/dev/null || [ -f "/dev/fd/3" ] || exit 45',
181
+ 'fd=/proc/$$/fd/3; [ -e "$fd" ] || fd=/dev/fd/3',
182
+ 'before=$(stat -Lc "%d:%i:%s:%Y" -- "$fd" 2>/dev/null || stat -f "%d:%i:%z:%m" "$fd")',
183
+ 'size=$(stat -Lc "%s" -- "$fd" 2>/dev/null || stat -f "%z" "$fd")',
184
+ "sha=$(sha256sum -- \"$fd\" 2>/dev/null | awk '{print $1}' || shasum -a 256 \"$fd\" | awk '{print $1}')",
185
+ 'exec 3<&-; exec 3<"$resolved"',
186
+ 'fd=/proc/$$/fd/3; [ -e "$fd" ] || fd=/dev/fd/3',
187
+ 'reopened=$(stat -Lc "%d:%i:%s:%Y" -- "$fd" 2>/dev/null || stat -f "%d:%i:%z:%m" "$fd")',
188
+ '[ "$before" = "$reopened" ] || exit 46',
189
+ 'prefix=$(dd if="$fd" bs=32 count=1 2>/dev/null | base64 | tr -d "\\r\\n")',
190
+ 'after=$(stat -Lc "%d:%i:%s:%Y" -- "$fd" 2>/dev/null || stat -f "%d:%i:%z:%m" "$fd")',
191
+ '[ "$before" = "$after" ] || exit 46',
192
+ 'printf "%s\\t%s\\t%s\\n" "$size" "$sha" "$prefix"',
193
+ ].join("\n");
194
+ }
195
+
196
+ function uploadCommand(
197
+ reference: InspectedSandboxVideoReference,
198
+ upload: Awaited<ReturnType<ObjectStorage["createPutUrl"]>>,
199
+ ): string {
200
+ const headers = Object.entries(upload.requiredHeaders)
201
+ .sort(([left], [right]) => left.localeCompare(right))
202
+ .map(([name, value]) => `--header ${shellQuote(`${name}: ${value}`)}`)
203
+ .join(" ");
204
+ return [
205
+ "set -euo pipefail",
206
+ `requested=${shellQuote(reference.path)}`,
207
+ 'resolved=$(realpath -e -- "$requested")',
208
+ '[ "$resolved" = "$requested" ] || exit 51',
209
+ 'exec 3<"$resolved"',
210
+ 'fd_path=$(realpath -e -- "/proc/$$/fd/3" 2>/dev/null || realpath -e -- "/dev/fd/3")',
211
+ 'case "$fd_path" in /workspace/*) ;; *) exit 52 ;; esac',
212
+ 'fd=/proc/$$/fd/3; [ -e "$fd" ] || fd=/dev/fd/3',
213
+ 'before=$(stat -Lc "%d:%i:%s:%Y" -- "$fd" 2>/dev/null || stat -f "%d:%i:%z:%m" "$fd")',
214
+ 'size=$(stat -Lc "%s" -- "$fd" 2>/dev/null || stat -f "%z" "$fd")',
215
+ `[ "$size" = ${shellQuote(String(reference.sizeBytes))} ] || exit 53`,
216
+ "sha=$(sha256sum -- \"$fd\" 2>/dev/null | awk '{print $1}' || shasum -a 256 \"$fd\" | awk '{print $1}')",
217
+ `[ "$sha" = ${shellQuote(reference.sha256)} ] || exit 54`,
218
+ 'exec 3<&-; exec 3<"$resolved"',
219
+ 'fd=/proc/$$/fd/3; [ -e "$fd" ] || fd=/dev/fd/3',
220
+ 'reopened=$(stat -Lc "%d:%i:%s:%Y" -- "$fd" 2>/dev/null || stat -f "%d:%i:%z:%m" "$fd")',
221
+ '[ "$before" = "$reopened" ] || exit 55',
222
+ `curl --silent --show-error --fail --connect-timeout 20 --max-time ${UPLOAD_TIMEOUT_SECONDS} --request PUT ${headers} --upload-file "$fd" --output /dev/null ${shellQuote(upload.url)}`,
223
+ 'after=$(stat -Lc "%d:%i:%s:%Y" -- "$fd" 2>/dev/null || stat -f "%d:%i:%z:%m" "$fd")',
224
+ '[ "$before" = "$after" ] || exit 56',
225
+ ].join("\n");
226
+ }
227
+
228
+ function sniffContentType(prefix: Uint8Array): string {
229
+ if (
230
+ prefix.byteLength >= 8 &&
231
+ Buffer.from(prefix.subarray(0, 8)).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))
232
+ ) {
233
+ return "image/png";
234
+ }
235
+ if (prefix.byteLength >= 3 && prefix[0] === 0xff && prefix[1] === 0xd8 && prefix[2] === 0xff) {
236
+ return "image/jpeg";
237
+ }
238
+ if (
239
+ prefix.byteLength >= 12 &&
240
+ Buffer.from(prefix.subarray(0, 4)).toString("ascii") === "RIFF" &&
241
+ Buffer.from(prefix.subarray(8, 12)).toString("ascii") === "WEBP"
242
+ ) {
243
+ return "image/webp";
244
+ }
245
+ if (prefix.byteLength >= 12 && Buffer.from(prefix.subarray(4, 8)).toString("ascii") === "ftyp") {
246
+ return "video/mp4";
247
+ }
248
+ throw new Error("Video reference has an unsupported media signature");
249
+ }
250
+
251
+ function shellQuote(value: string): string {
252
+ return `'${value.replaceAll("'", `'"'"'`)}'`;
253
+ }
254
+
255
+ function throwIfAborted(signal?: AbortSignal): void {
256
+ if (signal?.aborted) throw signal.reason ?? new Error("Video reference staging was cancelled");
257
+ }
258
+
259
+ export const VIDEO_REFERENCE_LIMITS = Object.freeze({
260
+ imageBytes: MAX_IMAGE_REFERENCE_BYTES,
261
+ videoBytes: MAX_VIDEO_REFERENCE_BYTES,
262
+ outputBytes: GENERATED_VIDEO_MAX_BYTES,
263
+ });
@@ -1,4 +1,4 @@
1
- import type { ActivityServices } from "./types";
1
+ import type { ControlActivityServices } from "./types";
2
2
  import { reconcilePendingSessionWorkflowWakes } from "./parent-wake";
3
3
 
4
4
  const BATCH_SIZE = 1_000;
@@ -16,7 +16,7 @@ export type DispatchSessionWorkflowWakesResult = {
16
16
  * acknowledgement path used by immediate delivery. This is a repair path, not
17
17
  * an eligibility scan: producers have already decided what must be delivered.
18
18
  */
19
- export function createWorkflowWakeActivities(services: () => Promise<ActivityServices>) {
19
+ export function createWorkflowWakeActivities(services: () => Promise<ControlActivityServices>) {
20
20
  return {
21
21
  async dispatchSessionWorkflowWakes(): Promise<DispatchSessionWorkflowWakesResult> {
22
22
  const service = await services();
@@ -540,6 +540,7 @@ async function runCapture(
540
540
  repos.push({
541
541
  root,
542
542
  head: status.head,
543
+ headOid: status.headOid ?? null,
543
544
  detached: status.detached,
544
545
  upstream: status.upstream,
545
546
  ahead: status.ahead,
@@ -1036,24 +1037,57 @@ function statusCodeOf(f: GitFileStatus): GitFileStatusCode {
1036
1037
 
1037
1038
  /**
1038
1039
  * sha256 over the CHANGE SURFACE only — per-file (path, status, hash, deleted,
1039
- * tooLarge) and per-repo diff summary (path, status, additions, deletions).
1040
+ * tooLarge), exact HEAD identity, the working diff summary, and exact committed
1041
+ * branch changes.
1040
1042
  * Deliberately excludes the tree index and file mtimes (which drift without a
1041
1043
  * real change) so two turns that leave the workspace in the same state produce
1042
1044
  * the same fingerprint (the empty-turn gate). Order-independent (sorted).
1043
1045
  */
1044
- function changeFingerprint(repos: WorkspaceCaptureRepo[], files: WorkspaceCaptureFile[]): string {
1046
+ export function changeFingerprint(
1047
+ repos: WorkspaceCaptureRepo[],
1048
+ files: WorkspaceCaptureFile[],
1049
+ ): string {
1045
1050
  const fileParts = files
1046
1051
  .map((f) => `${f.path}|${f.status}|${f.hash ?? ""}|${f.deleted ? 1 : 0}|${f.tooLarge ? 1 : 0}`)
1047
1052
  .sort();
1053
+ const branchDiffPart = (diff: WorkspaceCaptureRepo["diff"][number]): string =>
1054
+ JSON.stringify([
1055
+ diff.path,
1056
+ diff.oldPath,
1057
+ diff.status,
1058
+ diff.isBinary,
1059
+ diff.isImage,
1060
+ diff.additions,
1061
+ diff.deletions,
1062
+ diff.truncated,
1063
+ diff.hunks.map((hunk) => [
1064
+ hunk.oldStart,
1065
+ hunk.oldLines,
1066
+ hunk.newStart,
1067
+ hunk.newLines,
1068
+ hunk.header,
1069
+ hunk.lines.map((line) => [line.type, line.oldNo, line.newNo, line.text]),
1070
+ ]),
1071
+ ]);
1048
1072
  const repoParts = repos
1049
- .map(
1050
- (r) =>
1051
- `${r.root}#${r.head ?? ""}#` +
1052
- r.diff
1053
- .map((d) => `${d.path}:${d.status}:${d.additions}:${d.deletions}:${d.truncated ? 1 : 0}`)
1054
- .sort()
1055
- .join(","),
1056
- )
1073
+ .map((repo) => {
1074
+ const workingDiff = repo.diff
1075
+ .map(
1076
+ (diff) =>
1077
+ `${diff.path}:${diff.status}:${diff.additions}:${diff.deletions}:${diff.truncated ? 1 : 0}`,
1078
+ )
1079
+ .sort();
1080
+ const branchDiff =
1081
+ repo.branchDiff === undefined ? null : repo.branchDiff.map(branchDiffPart).sort();
1082
+ return JSON.stringify([
1083
+ repo.root,
1084
+ repo.head,
1085
+ repo.headOid ?? null,
1086
+ repo.ahead,
1087
+ workingDiff,
1088
+ branchDiff,
1089
+ ]);
1090
+ })
1057
1091
  .sort();
1058
1092
  return sha256(utf8(JSON.stringify({ files: fileParts, repos: repoParts })));
1059
1093
  }
@@ -0,0 +1,54 @@
1
+ import { createSharedActivityServices } from "./activity-services";
2
+ import { createCodexCapacityActivities } from "./activities/codex-capacity";
3
+ import { createDocumentActivities } from "./activities/documents";
4
+ import { createFileUploadReaperActivities } from "./activities/file-upload-reaper";
5
+ import { createGoalActivities } from "./activities/goals";
6
+ import { createKnowledgeSourceSyncActivities } from "./activities/knowledge-source-sync";
7
+ import { createRetainedScreenshotMaintenanceActivities } from "./activities/retained-screenshot-reaper";
8
+ import { createRigVerificationActivities } from "./activities/rig-verification";
9
+ import { createSandboxLeaseActivities } from "./activities/sandbox-lease";
10
+ import { createScheduledTaskActivities } from "./activities/scheduled-tasks";
11
+ import { createSessionStateActivities } from "./activities/session-state";
12
+ import type { ActivityDependencies, ControlActivityServices } from "./activities/types";
13
+ import { createWorkflowWakeActivities } from "./activities/workflow-wake";
14
+
15
+ function createControlActivityServices(
16
+ dependencies: ActivityDependencies,
17
+ ): () => Promise<ControlActivityServices> {
18
+ const shared = createSharedActivityServices(dependencies);
19
+ return shared;
20
+ }
21
+
22
+ export function createControlActivitiesFromServices(
23
+ services: () => Promise<ControlActivityServices>,
24
+ resolveDocumentServices?: () => Promise<import("@opengeni/documents").DocumentServices>,
25
+ ) {
26
+ return {
27
+ ...createDocumentActivities(services, resolveDocumentServices),
28
+ ...createKnowledgeSourceSyncActivities(services, resolveDocumentServices),
29
+ ...createSessionStateActivities(services),
30
+ ...createScheduledTaskActivities(services),
31
+ ...createGoalActivities(services),
32
+ ...createCodexCapacityActivities(services),
33
+ ...createRigVerificationActivities(services),
34
+ ...createFileUploadReaperActivities(services),
35
+ ...createRetainedScreenshotMaintenanceActivities(services),
36
+ ...createWorkflowWakeActivities(services),
37
+ ...createSandboxLeaseActivities(services),
38
+ };
39
+ }
40
+
41
+ export function createControlActivities(dependencies: ActivityDependencies = {}) {
42
+ const services = createControlActivityServices(dependencies);
43
+ let documentServicesPromise: Promise<import("@opengeni/documents").DocumentServices> | null =
44
+ null;
45
+ const resolveDocumentServices = async () => {
46
+ documentServicesPromise ??= dependencies.documentServices
47
+ ? Promise.resolve(dependencies.documentServices)
48
+ : import("@opengeni/documents").then(async ({ createDocumentServices }) =>
49
+ createDocumentServices((await services()).settings),
50
+ );
51
+ return await documentServicesPromise;
52
+ };
53
+ return createControlActivitiesFromServices(services, resolveDocumentServices);
54
+ }
@@ -0,0 +1,40 @@
1
+ import { createProductionAgentRuntime, summarizeForCompaction } from "@opengeni/runtime";
2
+ import { createSharedActivityServices } from "./activity-services";
3
+ import { createRunAgentTurnActivity } from "./activities/agent-turn";
4
+ import { createVideoGenerationActivities } from "./activities/video-generation-reconciliation";
5
+ import type { ActivityDependencies, TurnActivityServices } from "./activities/types";
6
+ import { runtimeMetricsHooksForObservability } from "./observability-metrics";
7
+
8
+ function createTurnActivityServices(
9
+ dependencies: ActivityDependencies,
10
+ ): () => Promise<TurnActivityServices> {
11
+ const shared = createSharedActivityServices(dependencies);
12
+ let servicesPromise: Promise<TurnActivityServices> | null = null;
13
+ return async () => {
14
+ servicesPromise ??= (async () => {
15
+ const services = await shared();
16
+ return {
17
+ ...services,
18
+ runtime:
19
+ dependencies.runtime ??
20
+ createProductionAgentRuntime({
21
+ metrics: runtimeMetricsHooksForObservability(services.observability),
22
+ }),
23
+ summarizeContextForCompaction:
24
+ dependencies.summarizeContextForCompaction ?? summarizeForCompaction,
25
+ };
26
+ })();
27
+ return await servicesPromise;
28
+ };
29
+ }
30
+
31
+ export function createTurnActivities(dependencies: ActivityDependencies = {}) {
32
+ return createTurnActivitiesFromServices(createTurnActivityServices(dependencies));
33
+ }
34
+
35
+ export function createTurnActivitiesFromServices(services: () => Promise<TurnActivityServices>) {
36
+ return {
37
+ runAgentTurn: createRunAgentTurnActivity(services),
38
+ ...createVideoGenerationActivities(services),
39
+ };
40
+ }
package/src/activities.ts CHANGED
@@ -1,29 +1,24 @@
1
- import { dbSearchPath, getSettings, resolveNatsControlPlaneAuth } from "@opengeni/config";
2
- import { createDb } from "@opengeni/db";
1
+ /**
2
+ * Compatibility/test activity barrel.
3
+ *
4
+ * Production workers do not import this module: the control and turn process
5
+ * loaders resolve their role-specific activity graph independently. Keeping
6
+ * this superset preserves the historical workflow type surface and direct test
7
+ * harness without charging every production process for both dependency trees.
8
+ */
3
9
  import { createDocumentServices } from "@opengeni/documents";
4
- import { createNatsEventBus } from "@opengeni/events";
5
- import { createObservability } from "@opengeni/observability";
6
- import { createProductionAgentRuntime } from "@opengeni/runtime";
7
- import { createObjectStorage } from "@opengeni/storage";
8
- import { createRunAgentTurnActivity } from "./activities/agent-turn";
9
- import { createCodexCapacityActivities } from "./activities/codex-capacity";
10
- import { createDocumentActivities } from "./activities/documents";
11
- import { createFileUploadReaperActivities } from "./activities/file-upload-reaper";
12
- import { createGoalActivities } from "./activities/goals";
13
- import { createSandboxLeaseActivities } from "./activities/sandbox-lease";
14
- import { createScheduledTaskActivities } from "./activities/scheduled-tasks";
15
- import { createSessionStateActivities } from "./activities/session-state";
16
- import { createWorkflowWakeActivities } from "./activities/workflow-wake";
17
- import { createRigVerificationActivities } from "./activities/rig-verification";
18
- import { createRetainedScreenshotMaintenanceActivities } from "./activities/retained-screenshot-reaper";
10
+ import { createProductionAgentRuntime, summarizeForCompaction } from "@opengeni/runtime";
11
+ import { createControlActivities, createControlActivitiesFromServices } from "./activities-control";
12
+ import { createTurnActivities, createTurnActivitiesFromServices } from "./activities-turn";
13
+ import { createSharedActivityServices } from "./activity-services";
19
14
  import type { ActivityDependencies, ActivityServices } from "./activities/types";
20
- import {
21
- observabilityEventLogger,
22
- runtimeMetricsHooksForObservability,
23
- } from "./observability-metrics";
15
+ import { runtimeMetricsHooksForObservability } from "./observability-metrics";
24
16
 
25
17
  export type {
26
18
  ActivityDependencies,
19
+ ActivityServices,
20
+ ControlActivityDependencies,
21
+ ControlActivityServices,
27
22
  DispatchScheduledTaskRunInput,
28
23
  DispatchScheduledTaskRunResult,
29
24
  ExpireSessionHumanInputInput,
@@ -42,125 +37,46 @@ export type {
42
37
  PersistSessionAttemptQuiescenceInput,
43
38
  ReconcileSessionAttemptQuiescenceInput,
44
39
  ReconcileSessionAttemptQuiescenceResult,
40
+ RunKnowledgeSourceSyncBatchInput,
41
+ RunKnowledgeSourceSyncBatchResult,
45
42
  RunAgentTurnInput,
46
43
  RunAgentTurnResult,
44
+ VideoGenerationReconcileResult,
45
+ VideoGenerationTerminalStatus,
47
46
  SessionAttemptQuiescenceProof,
47
+ SharedActivityServices,
48
+ TurnActivityDependencies,
49
+ TurnActivityServices,
48
50
  } from "./activities/types";
51
+ export { createControlActivities, createTurnActivities };
49
52
 
50
- function createActivityServices(
51
- dependencies: ActivityDependencies,
52
- ): () => Promise<ActivityServices> {
53
+ /** Direct full-graph harness for tests; production always chooses one role. */
54
+ export function createActivityTestHarness(dependencies: ActivityDependencies = {}) {
55
+ const shared = createSharedActivityServices(dependencies);
53
56
  let servicesPromise: Promise<ActivityServices> | null = null;
54
-
55
- async function services(): Promise<ActivityServices> {
57
+ const services = async (): Promise<ActivityServices> => {
56
58
  servicesPromise ??= (async () => {
57
- const settings = dependencies.settings ?? getSettings();
58
- const observability =
59
- dependencies.observability ?? createObservability(settings, { component: "worker" });
60
- // Step I: when not injected, build the standalone handle — searchPath
61
- // undefined for standalone (public), scoped to the dedicated schema +
62
- // host RLS strategy when embedded config is set. An embedded host injects
63
- // `dependencies.db` directly and this branch is skipped.
64
- const searchPath = dbSearchPath(settings);
65
- const dbClient = dependencies.db
66
- ? null
67
- : createDb(settings.databaseUrl, {
68
- ...(searchPath ? { searchPath } : {}),
69
- rlsStrategy: settings.rlsStrategy,
70
- });
71
- // The PRIVILEGED control-plane NATS login (M-AUTH): the worker resolves the
72
- // SAME static account user the API uses to request `agent.*.rpc`. Null in
73
- // local dev → anonymous connect (the bus default).
74
- const controlPlaneAuth = resolveNatsControlPlaneAuth(settings);
59
+ const common = await shared();
75
60
  return {
76
- settings,
77
- db: dependencies.db ?? dbClient!.db,
78
- // §7 Step G — EventBus binding contract. `bus` is the INJECTED
79
- // live-fanout port; the ONE production impl is `createNatsEventBus`,
80
- // the default on BOTH processes (this worker edge + the API edge in
81
- // `apps/api/src/index.ts`). A host that embeds OpenGeni injects ONE
82
- // broker binding (the same `createNatsEventBus(natsUrl)`) here AND on
83
- // the mounted API, so the two SEPARATE processes share one broker and
84
- // derive the IDENTICAL `sessionSubject` — the only way live fanout
85
- // (worker emit → API SSE) works cross-process (schema-isolation contract F5/F6, proven).
86
- // NEVER default to an in-memory bus: it fans out intra-process only and
87
- // would silently break live SSE. unset → today's NATS default,
88
- // byte-for-byte. The bus is live-fanout ONLY — the durable Postgres
89
- // `session_events` log is source-of-truth (the API backfills missed
90
- // events by sequence). See `.agents/skills/opengeni/references/eventbus-binding-contract.md`.
91
- bus:
92
- dependencies.bus ??
93
- (await createNatsEventBus(
94
- settings.natsUrl,
95
- controlPlaneAuth
96
- ? { user: controlPlaneAuth.user, pass: controlPlaneAuth.password }
97
- : undefined,
98
- { logger: observabilityEventLogger(observability) },
99
- )),
61
+ ...common,
100
62
  runtime:
101
63
  dependencies.runtime ??
102
64
  createProductionAgentRuntime({
103
- metrics: runtimeMetricsHooksForObservability(observability),
65
+ metrics: runtimeMetricsHooksForObservability(common.observability),
104
66
  }),
105
- objectStorage: dependencies.objectStorage ?? createObjectStorage(settings),
106
- documentServices: dependencies.documentServices ?? createDocumentServices(settings),
107
- observability,
108
- wakeSessionWorkflow: dependencies.wakeSessionWorkflow ?? null,
109
- signalSessionAttemptQuiesced: dependencies.signalSessionAttemptQuiesced ?? null,
110
- inspectSessionAttemptActivity: dependencies.inspectSessionAttemptActivity ?? null,
111
- signalCodexCapacityWorkflow: dependencies.signalCodexCapacityWorkflow ?? null,
112
- // §7.5 P3 — host-entitlements port. No constructed default: standalone
113
- // has no host meter, so unset → null → `ensureRunAllowed` reads the
114
- // local ledger exactly as today (mirrors `wakeSessionWorkflow`'s
115
- // null-degrades-gracefully shape, not a `createX(settings)` default).
116
- entitlements: dependencies.entitlements ?? null,
117
- // §7.6 connection-credential provider — host connection-credential provider. No constructed
118
- // default: standalone owns its own GitHub App + encryption key, so unset
119
- // → null → the per-run credential mint self-mints from `settings`
120
- // (createGitHubAppInstallationToken + environmentsEncryptionKeyBytes)
121
- // exactly as today. Same null-degrades shape as `entitlements`.
122
- connectionCredentials: dependencies.connectionCredentials ?? null,
67
+ summarizeContextForCompaction:
68
+ dependencies.summarizeContextForCompaction ?? summarizeForCompaction,
69
+ documentServices: dependencies.documentServices ?? createDocumentServices(common.settings),
123
70
  };
124
71
  })();
125
- return servicesPromise;
126
- }
127
-
128
- return services;
129
- }
130
-
131
- function controlActivities(services: () => Promise<ActivityServices>) {
132
- return {
133
- ...createDocumentActivities(services),
134
- ...createSessionStateActivities(services),
135
- ...createScheduledTaskActivities(services),
136
- ...createGoalActivities(services),
137
- ...createCodexCapacityActivities(services),
138
- ...createRigVerificationActivities(services),
139
- ...createFileUploadReaperActivities(services),
140
- ...createRetainedScreenshotMaintenanceActivities(services),
141
- ...createWorkflowWakeActivities(services),
142
- // P1.3: the SOLE liveness/GC/cost-stop driver. Only reapSandboxLeases — no
143
- // *ForViewer activities, no ownerHeartbeat, no resolveOwnerTaskQueue.
144
- ...createSandboxLeaseActivities(services),
72
+ return await servicesPromise;
145
73
  };
146
- }
147
-
148
- export function createControlActivities(dependencies: ActivityDependencies = {}) {
149
- return controlActivities(createActivityServices(dependencies));
150
- }
151
-
152
- export function createTurnActivities(dependencies: ActivityDependencies = {}) {
153
- return {
154
- runAgentTurn: createRunAgentTurnActivity(createActivityServices(dependencies)),
155
- };
156
- }
157
-
158
- /** Direct activity harness for tests; production workers always choose one role. */
159
- export function createActivityTestHarness(dependencies: ActivityDependencies = {}) {
160
- const services = createActivityServices(dependencies);
161
74
  return {
162
- runAgentTurn: createRunAgentTurnActivity(services),
163
- ...controlActivities(services),
75
+ ...createTurnActivitiesFromServices(services),
76
+ ...createControlActivitiesFromServices(
77
+ services,
78
+ async () => (await services()).documentServices,
79
+ ),
164
80
  };
165
81
  }
166
82
 
@@ -168,6 +84,8 @@ const defaultControlActivities = createControlActivities();
168
84
  const defaultTurnActivities = createTurnActivities();
169
85
 
170
86
  export const runAgentTurn = defaultTurnActivities.runAgentTurn;
87
+ export const reconcileVideoGenerationOperation =
88
+ defaultTurnActivities.reconcileVideoGenerationOperation;
171
89
  export const indexDocument = defaultControlActivities.indexDocument;
172
90
  export const failSessionAttempt = defaultControlActivities.failSessionAttempt;
173
91
  export const settleSessionInterruptions = defaultControlActivities.settleSessionInterruptions;
@@ -181,12 +99,18 @@ export const peekSessionWork = defaultControlActivities.peekSessionWork;
181
99
  export const expireSessionHumanInput = defaultControlActivities.expireSessionHumanInput;
182
100
  export const markSessionIdle = defaultControlActivities.markSessionIdle;
183
101
  export const dispatchScheduledTaskRun = defaultControlActivities.dispatchScheduledTaskRun;
102
+ export const runKnowledgeSourceSyncBatch = defaultControlActivities.runKnowledgeSourceSyncBatch;
184
103
  export const enqueueGoalRetryWake = defaultControlActivities.enqueueGoalRetryWake;
185
104
  export const maybeContinueGoal = defaultControlActivities.maybeContinueGoal;
186
105
  export const getCodexCapacityWait = defaultControlActivities.getCodexCapacityWait;
187
106
  export const reconcileCodexCapacityWait = defaultControlActivities.reconcileCodexCapacityWait;
107
+ export const prepareSandboxLeaseSweep = defaultControlActivities.prepareSandboxLeaseSweep;
108
+ export const drainSandboxLease = defaultControlActivities.drainSandboxLease;
109
+ export const maintainSandboxLeaseSweep = defaultControlActivities.maintainSandboxLeaseSweep;
188
110
  export const reapSandboxLeases = defaultControlActivities.reapSandboxLeases;
189
111
  export const reapExpiredFileUploads = defaultControlActivities.reapExpiredFileUploads;
112
+ export const recoverVideoGenerationWorkflows =
113
+ defaultControlActivities.recoverVideoGenerationWorkflows;
190
114
  export const maintainRetainedScreenshots = defaultControlActivities.maintainRetainedScreenshots;
191
115
  export const dispatchSessionWorkflowWakes = defaultControlActivities.dispatchSessionWorkflowWakes;
192
116
  export const verifyRigChange = defaultControlActivities.verifyRigChange;