@opengeni/worker-bundle 0.16.29 → 0.20.0

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 (188) hide show
  1. package/dist/activities/agent-run-admission.d.ts +12 -0
  2. package/dist/activities/agent-turn.d.ts +110 -19
  3. package/dist/activities/api-integrations.d.ts +21 -0
  4. package/dist/activities/browser-state-artifact-reaper.d.ts +29 -0
  5. package/dist/activities/capabilities.d.ts +6 -0
  6. package/dist/activities/codemode-dispatcher.d.ts +35 -0
  7. package/dist/activities/codemode-token-renewal.d.ts +23 -0
  8. package/dist/activities/codex-capacity.d.ts +6 -5
  9. package/dist/activities/codex-image-generation.d.ts +21 -0
  10. package/dist/activities/context-compaction.d.ts +1 -0
  11. package/dist/activities/documents.d.ts +3 -2
  12. package/dist/activities/environment.d.ts +17 -8
  13. package/dist/activities/expiring-material-renewal.d.ts +1 -1
  14. package/dist/activities/file-upload-reaper.d.ts +7 -2
  15. package/dist/activities/gateway-image-generation.d.ts +39 -0
  16. package/dist/activities/gateway-video-generation.d.ts +53 -0
  17. package/dist/activities/generated-images.d.ts +112 -0
  18. package/dist/activities/git-credential-renewal.d.ts +1 -1
  19. package/dist/activities/goals.d.ts +2 -2
  20. package/dist/activities/google-drive-changes.d.ts +99 -0
  21. package/dist/activities/image-generation-operation.d.ts +74 -0
  22. package/dist/activities/image-generation-references.d.ts +25 -0
  23. package/dist/activities/knowledge-source-sync-driver.d.ts +23 -0
  24. package/dist/activities/knowledge-source-sync.d.ts +75 -0
  25. package/dist/activities/packs.d.ts +21 -36
  26. package/dist/activities/parent-wake.d.ts +1 -1
  27. package/dist/activities/retained-screenshot-reaper.d.ts +2 -2
  28. package/dist/activities/retained-screenshots.d.ts +36 -0
  29. package/dist/activities/rig-verification.d.ts +82 -5
  30. package/dist/activities/run-credential-renewal.d.ts +1 -1
  31. package/dist/activities/run-input.d.ts +3 -1
  32. package/dist/activities/sandbox-images.d.ts +14 -0
  33. package/dist/activities/sandbox-lease.d.ts +47 -9
  34. package/dist/activities/scheduled-tasks.d.ts +2 -2
  35. package/dist/activities/session-state.d.ts +7 -5
  36. package/dist/activities/site-auth-maintenance.d.ts +27 -0
  37. package/dist/activities/streaming.d.ts +1 -1
  38. package/dist/activities/types.d.ts +87 -5
  39. package/dist/activities/video-generation-admission.d.ts +43 -0
  40. package/dist/activities/video-generation-credential.d.ts +24 -0
  41. package/dist/activities/video-generation-reconciliation.d.ts +13 -0
  42. package/dist/activities/video-media-validation.d.ts +34 -0
  43. package/dist/activities/video-output-retention.d.ts +36 -0
  44. package/dist/activities/video-reference-staging.d.ts +55 -0
  45. package/dist/activities/workflow-wake.d.ts +2 -2
  46. package/dist/activities/workspace-capture.d.ts +10 -1
  47. package/dist/activities/xai-auth.d.ts +23 -0
  48. package/dist/activities/xai-image-generation.d.ts +26 -0
  49. package/dist/activities/xai-video-generation.d.ts +18 -0
  50. package/dist/activities-control-XURE4VKN.js +7221 -0
  51. package/dist/activities-control-XURE4VKN.js.map +1 -0
  52. package/dist/activities-control.d.ts +297 -0
  53. package/dist/activities-turn-6EBXGGLS.js +16036 -0
  54. package/dist/activities-turn-6EBXGGLS.js.map +1 -0
  55. package/dist/activities-turn.d.ts +17 -0
  56. package/dist/activities.d.ts +138 -150
  57. package/dist/activity-services.d.ts +10 -0
  58. package/dist/artifact-materializer-entry.d.ts +6 -0
  59. package/dist/artifact-materializer-entry.js +2662 -0
  60. package/dist/artifact-materializer-entry.js.map +1 -0
  61. package/dist/artifact-outbox-entry.d.ts +2 -0
  62. package/dist/artifact-outbox-entry.js +681 -0
  63. package/dist/artifact-outbox-entry.js.map +1 -0
  64. package/dist/chunk-5GRJIP5H.js +3092 -0
  65. package/dist/chunk-5GRJIP5H.js.map +1 -0
  66. package/dist/chunk-GRFQTTSQ.js +448 -0
  67. package/dist/chunk-GRFQTTSQ.js.map +1 -0
  68. package/dist/chunk-TYNPCG4U.js +906 -0
  69. package/dist/chunk-TYNPCG4U.js.map +1 -0
  70. package/dist/concurrency.d.ts +108 -5
  71. package/dist/editable-artifact-hint-broker.d.ts +49 -0
  72. package/dist/editable-artifact-materializer-launcher.d.ts +49 -0
  73. package/dist/editable-artifact-materializer-service.d.ts +108 -0
  74. package/dist/editable-artifact-materializer-storage.d.ts +12 -0
  75. package/dist/editable-artifact-materializer-subprocess.d.ts +50 -0
  76. package/dist/editable-artifact-materializer.d.ts +262 -0
  77. package/dist/editable-artifact-outbox-dispatcher.d.ts +44 -0
  78. package/dist/editable-artifact-outbox-service.d.ts +70 -0
  79. package/dist/host-export.d.ts +1 -1
  80. package/dist/index.d.ts +189 -10
  81. package/dist/index.js +715 -17380
  82. package/dist/index.js.map +1 -1
  83. package/dist/memory-pressure-guard.d.ts +57 -0
  84. package/dist/model-checkpoint-memory-collector.d.ts +23 -0
  85. package/dist/model-context-contributions.d.ts +40 -0
  86. package/dist/observability-metrics.d.ts +53 -1
  87. package/dist/sandbox-reaper-contract.d.ts +40 -0
  88. package/dist/sandbox-reaper-timeout.d.ts +14 -0
  89. package/dist/sandbox-resume.d.ts +34 -7
  90. package/dist/sandbox-routing.d.ts +6 -0
  91. package/dist/worker-service-lifecycle.d.ts +6 -2
  92. package/dist/workflow-bundle.js +1094 -370
  93. package/dist/workflows/activities.d.ts +8 -2
  94. package/dist/workflows/document-index.d.ts +1 -1
  95. package/dist/workflows/knowledge-source-sync.d.ts +13 -0
  96. package/dist/workflows/rig-verification.d.ts +1 -1
  97. package/dist/workflows/sandbox-reaper.d.ts +8 -0
  98. package/dist/workflows/scheduled-tasks.d.ts +1 -1
  99. package/dist/workflows/session.d.ts +2 -2
  100. package/dist/workflows/site-auth-maintenance.d.ts +2 -0
  101. package/dist/workflows/video-generation.d.ts +10 -0
  102. package/dist/workflows/workflow-wake.d.ts +1 -1
  103. package/dist/workflows.d.ts +10 -7
  104. package/package.json +35 -15
  105. package/src/activities/agent-run-admission.ts +76 -0
  106. package/src/activities/agent-turn.ts +2600 -340
  107. package/src/activities/api-integrations.ts +138 -0
  108. package/src/activities/browser-state-artifact-reaper.ts +120 -0
  109. package/src/activities/capabilities.ts +10 -0
  110. package/src/activities/codemode-dispatcher.ts +335 -0
  111. package/src/activities/codemode-token-renewal.ts +64 -0
  112. package/src/activities/codex-capacity.ts +54 -7
  113. package/src/activities/codex-image-generation.ts +56 -0
  114. package/src/activities/context-compaction.ts +38 -3
  115. package/src/activities/documents.ts +14 -4
  116. package/src/activities/environment.ts +54 -45
  117. package/src/activities/expiring-material-renewal.ts +1 -1
  118. package/src/activities/file-upload-reaper.ts +34 -3
  119. package/src/activities/gateway-image-generation.ts +146 -0
  120. package/src/activities/gateway-video-generation.ts +298 -0
  121. package/src/activities/generated-images.ts +860 -0
  122. package/src/activities/goals.ts +8 -3
  123. package/src/activities/google-drive-changes.ts +454 -0
  124. package/src/activities/image-generation-operation.ts +333 -0
  125. package/src/activities/image-generation-references.ts +118 -0
  126. package/src/activities/knowledge-source-sync-driver.ts +27 -0
  127. package/src/activities/knowledge-source-sync.ts +2840 -0
  128. package/src/activities/mcp-credentials.ts +1 -0
  129. package/src/activities/packs.ts +75 -152
  130. package/src/activities/parent-wake.ts +13 -1
  131. package/src/activities/retained-screenshot-reaper.ts +2 -2
  132. package/src/activities/retained-screenshots.ts +453 -28
  133. package/src/activities/rig-verification.ts +554 -43
  134. package/src/activities/run-input.ts +22 -6
  135. package/src/activities/sandbox-images.ts +167 -0
  136. package/src/activities/sandbox-lease.ts +1065 -483
  137. package/src/activities/scheduled-tasks.ts +104 -74
  138. package/src/activities/session-state.ts +22 -4
  139. package/src/activities/site-auth-maintenance.ts +269 -0
  140. package/src/activities/streaming.ts +42 -3
  141. package/src/activities/types.ts +104 -5
  142. package/src/activities/video-generation-admission.ts +303 -0
  143. package/src/activities/video-generation-credential.ts +112 -0
  144. package/src/activities/video-generation-reconciliation.ts +771 -0
  145. package/src/activities/video-media-validation.ts +332 -0
  146. package/src/activities/video-output-retention.ts +314 -0
  147. package/src/activities/video-reference-staging.ts +350 -0
  148. package/src/activities/workflow-wake.ts +2 -2
  149. package/src/activities/workspace-capture.ts +44 -10
  150. package/src/activities/xai-auth.ts +122 -0
  151. package/src/activities/xai-image-generation.ts +86 -0
  152. package/src/activities/xai-video-generation.ts +69 -0
  153. package/src/activities-control.ts +58 -0
  154. package/src/activities-turn.ts +40 -0
  155. package/src/activities.ts +53 -124
  156. package/src/activity-services.ts +62 -0
  157. package/src/artifact-materializer-entry.ts +35 -0
  158. package/src/artifact-outbox-entry.ts +29 -0
  159. package/src/concurrency.ts +484 -6
  160. package/src/editable-artifact-hint-broker.ts +235 -0
  161. package/src/editable-artifact-materializer-launcher.ts +515 -0
  162. package/src/editable-artifact-materializer-service.ts +787 -0
  163. package/src/editable-artifact-materializer-storage.ts +413 -0
  164. package/src/editable-artifact-materializer-subprocess.ts +956 -0
  165. package/src/editable-artifact-materializer.ts +1289 -0
  166. package/src/editable-artifact-outbox-dispatcher.ts +244 -0
  167. package/src/editable-artifact-outbox-service.ts +438 -0
  168. package/src/index.ts +328 -72
  169. package/src/memory-pressure-guard.ts +489 -0
  170. package/src/model-checkpoint-memory-collector.ts +123 -0
  171. package/src/model-context-contributions.ts +159 -0
  172. package/src/observability-metrics.ts +297 -13
  173. package/src/sandbox-reaper-contract.ts +73 -0
  174. package/src/sandbox-reaper-timeout.ts +79 -0
  175. package/src/sandbox-resume.ts +294 -153
  176. package/src/sandbox-routing.ts +32 -24
  177. package/src/worker-service-lifecycle.ts +80 -14
  178. package/src/workflows/activities.ts +31 -0
  179. package/src/workflows/file-upload-reaper.ts +9 -1
  180. package/src/workflows/knowledge-source-sync.ts +83 -0
  181. package/src/workflows/sandbox-reaper.ts +183 -21
  182. package/src/workflows/scheduled-tasks.ts +42 -6
  183. package/src/workflows/session.ts +20 -6
  184. package/src/workflows/site-auth-maintenance.ts +14 -0
  185. package/src/workflows/video-generation.ts +36 -0
  186. package/src/workflows.ts +18 -1
  187. package/dist/activities/toolspace-token-renewal.d.ts +0 -23
  188. package/src/activities/toolspace-token-renewal.ts +0 -64
@@ -0,0 +1,350 @@
1
+ import {
2
+ GENERATED_VIDEO_MAX_BYTES,
3
+ type GenerateVideoToolInput,
4
+ type VideoGenerationRejectedCode,
5
+ } from "@opengeni/contracts";
6
+ import type { SealedVideoReference, SealedVideoReferenceRole } from "@opengeni/core";
7
+ import type { ObjectStorage } from "@opengeni/storage";
8
+ import {
9
+ copyVersionedObjectToVerifiedTemp,
10
+ validateImageReference,
11
+ validateVideoReference,
12
+ } from "./video-media-validation";
13
+
14
+ const MAX_IMAGE_REFERENCE_BYTES = 30 * 1024 * 1024;
15
+ const MAX_VIDEO_REFERENCE_BYTES = 200 * 1024 * 1024;
16
+ const UPLOAD_TIMEOUT_SECONDS = 15 * 60;
17
+
18
+ export type SandboxCommandRunner = (input: {
19
+ cmd: string;
20
+ workdir: string;
21
+ maxOutputTokens: number;
22
+ }) => Promise<{ stdout: string; stderr: string; exitCode: number }>;
23
+
24
+ export type InspectedSandboxVideoReference = Readonly<{
25
+ ordinal: number;
26
+ role: SealedVideoReferenceRole;
27
+ path: string;
28
+ contentType: string;
29
+ sizeBytes: number;
30
+ sha256: string;
31
+ }>;
32
+
33
+ export type VideoReferenceInputErrorCode = VideoGenerationRejectedCode;
34
+
35
+ /** A deterministic, pre-admission tool-input error. No video operation exists yet. */
36
+ export class VideoReferenceInputError extends Error {
37
+ readonly code: VideoReferenceInputErrorCode;
38
+
39
+ constructor(code: VideoReferenceInputErrorCode, message: string) {
40
+ super(message);
41
+ this.name = "VideoReferenceInputError";
42
+ this.code = code;
43
+ }
44
+ }
45
+
46
+ export function videoReferencePaths(
47
+ request: GenerateVideoToolInput,
48
+ ): Array<{ role: SealedVideoReferenceRole; path: string }> {
49
+ const source = request.source;
50
+ if (!source || source.mode === "text") return [];
51
+ if (source.mode === "first_frame") return [{ role: "first_frame", path: source.imagePath }];
52
+ if (source.mode === "first_and_last_frames") {
53
+ return [
54
+ { role: "first_frame", path: source.firstFramePath },
55
+ { role: "last_frame", path: source.lastFramePath },
56
+ ];
57
+ }
58
+ if (source.mode === "image_reference") {
59
+ return [{ role: "image_reference", path: source.imagePath }];
60
+ }
61
+ return [{ role: "video_reference", path: source.videoPath }];
62
+ }
63
+
64
+ export async function inspectSandboxVideoReferences(input: {
65
+ request: GenerateVideoToolInput;
66
+ runCommand: SandboxCommandRunner;
67
+ }): Promise<InspectedSandboxVideoReference[]> {
68
+ const paths = videoReferencePaths(input.request);
69
+ const inspected: InspectedSandboxVideoReference[] = [];
70
+ for (const [ordinal, reference] of paths.entries()) {
71
+ const result = await input.runCommand({
72
+ cmd: inspectCommand(reference.path),
73
+ workdir: "/workspace",
74
+ maxOutputTokens: 4_096,
75
+ });
76
+ if (result.exitCode !== 0) {
77
+ throw new VideoReferenceInputError(
78
+ "reference_not_stable",
79
+ "The video reference must be an existing regular file at the exact /workspace path returned by the preceding tool.",
80
+ );
81
+ }
82
+ const [sizeText, sha256, prefixBase64] = result.stdout.trim().split("\t");
83
+ const sizeBytes = Number(sizeText);
84
+ if (
85
+ !Number.isSafeInteger(sizeBytes) ||
86
+ sizeBytes <= 0 ||
87
+ !/^[0-9a-f]{64}$/u.test(sha256 ?? "")
88
+ ) {
89
+ throw new VideoReferenceInputError(
90
+ "reference_not_stable",
91
+ "The video reference changed or could not be read safely. Use the exact current /workspace path and try again.",
92
+ );
93
+ }
94
+ const prefix = Buffer.from(prefixBase64 ?? "", "base64");
95
+ const contentType = sniffContentType(prefix);
96
+ const maxBytes =
97
+ reference.role === "video_reference" ? MAX_VIDEO_REFERENCE_BYTES : MAX_IMAGE_REFERENCE_BYTES;
98
+ if (sizeBytes > maxBytes) {
99
+ throw new VideoReferenceInputError(
100
+ "reference_too_large",
101
+ "The video reference exceeds the supported size for this source mode.",
102
+ );
103
+ }
104
+ if (
105
+ (reference.role === "video_reference" && contentType !== "video/mp4") ||
106
+ (reference.role !== "video_reference" && !contentType.startsWith("image/"))
107
+ ) {
108
+ throw new VideoReferenceInputError(
109
+ "reference_media_type_mismatch",
110
+ "The video reference media type does not match the selected source mode.",
111
+ );
112
+ }
113
+ inspected.push({
114
+ ordinal,
115
+ role: reference.role,
116
+ path: reference.path,
117
+ contentType,
118
+ sizeBytes,
119
+ sha256: sha256!,
120
+ });
121
+ }
122
+ return inspected;
123
+ }
124
+
125
+ export async function uploadAndVerifyVideoReferences(input: {
126
+ storage: ObjectStorage;
127
+ references: readonly InspectedSandboxVideoReference[];
128
+ stagingKeys: readonly string[];
129
+ runCommand: SandboxCommandRunner;
130
+ tempRoot: string;
131
+ ffprobePath: string;
132
+ uploadTtlSeconds: number;
133
+ signal?: AbortSignal;
134
+ }): Promise<SealedVideoReference[]> {
135
+ if (input.references.length !== input.stagingKeys.length) {
136
+ throw new Error("Video reference staging identities do not match");
137
+ }
138
+ const sealed: SealedVideoReference[] = [];
139
+ for (const [index, reference] of input.references.entries()) {
140
+ throwIfAborted(input.signal);
141
+ const key = input.stagingKeys[index]!;
142
+ const existing = input.storage.headObject ? await input.storage.headObject(key) : null;
143
+ if (!existing) {
144
+ const upload = await input.storage.createPutUrl({
145
+ key,
146
+ contentType: reference.contentType,
147
+ sha256: reference.sha256,
148
+ expiresInSeconds: Math.min(input.uploadTtlSeconds, UPLOAD_TIMEOUT_SECONDS),
149
+ });
150
+ const result = await input.runCommand({
151
+ cmd: uploadCommand(reference, upload),
152
+ workdir: "/workspace",
153
+ maxOutputTokens: 4_096,
154
+ });
155
+ if (result.exitCode !== 0) throw new Error("Video reference staging upload failed");
156
+ }
157
+ const verified = await copyVersionedObjectToVerifiedTemp({
158
+ storage: input.storage,
159
+ key,
160
+ expectedSizeBytes: reference.sizeBytes,
161
+ expectedSha256: reference.sha256,
162
+ expectedContentType: reference.contentType,
163
+ maxBytes:
164
+ reference.role === "video_reference"
165
+ ? MAX_VIDEO_REFERENCE_BYTES
166
+ : MAX_IMAGE_REFERENCE_BYTES,
167
+ tempRoot: input.tempRoot,
168
+ ...(input.signal ? { signal: input.signal } : {}),
169
+ });
170
+ try {
171
+ if (reference.role === "video_reference") {
172
+ await validateVideoReference({
173
+ path: verified.path,
174
+ ffprobePath: input.ffprobePath,
175
+ });
176
+ } else {
177
+ const image = await validateImageReference(verified.path);
178
+ if (image.contentType !== reference.contentType) {
179
+ throw new Error("Video image reference MIME type changed during validation");
180
+ }
181
+ }
182
+ } finally {
183
+ await verified.cleanup();
184
+ }
185
+ sealed.push({
186
+ role: reference.role,
187
+ contentSha256: reference.sha256,
188
+ contentType: reference.contentType,
189
+ byteSize: reference.sizeBytes,
190
+ });
191
+ }
192
+ return sealed;
193
+ }
194
+
195
+ export function videoReferenceStagingKey(input: {
196
+ workspaceId: string;
197
+ operationId: string;
198
+ ordinal: number;
199
+ sha256: string;
200
+ }): string {
201
+ if (!/^[0-9a-f]{64}$/u.test(input.sha256)) throw new Error("Video reference digest is invalid");
202
+ return `video-generation/staging/${input.workspaceId}/${input.operationId}/${input.ordinal}/${input.sha256}`;
203
+ }
204
+
205
+ function inspectCommand(path: string): string {
206
+ return [
207
+ "set -euo pipefail",
208
+ ...stableWorkspaceFilePrelude(path, { nonCanonical: 43, invalidFile: 45 }),
209
+ 'sha=$(video_reference_sha256 "$fd")',
210
+ 'exec 3<&-; exec 3<"$resolved"',
211
+ 'fd=/proc/$$/fd/3; [ -e "$fd" ] || fd=/dev/fd/3',
212
+ 'reopened=$(video_reference_stat_identity "$fd")',
213
+ '[ "$before" = "$reopened" ] || exit 46',
214
+ '[ "$(video_reference_stat_content_identity "$resolved")" = "$(video_reference_stat_content_identity "$fd")" ] || exit 46',
215
+ '[ "$(realpath -- "$requested")" = "$expected" ] || exit 46',
216
+ 'prefix=$(dd if="$fd" bs=32 count=1 2>/dev/null | base64 | tr -d "\\r\\n")',
217
+ 'after=$(video_reference_stat_identity "$fd")',
218
+ '[ "$before" = "$after" ] || exit 46',
219
+ 'printf "%s\\t%s\\t%s\\n" "$size" "$sha" "$prefix"',
220
+ ].join("\n");
221
+ }
222
+
223
+ function uploadCommand(
224
+ reference: InspectedSandboxVideoReference,
225
+ upload: Awaited<ReturnType<ObjectStorage["createPutUrl"]>>,
226
+ ): string {
227
+ const headers = Object.entries(upload.requiredHeaders)
228
+ .sort(([left], [right]) => left.localeCompare(right))
229
+ .map(([name, value]) => `--header ${shellQuote(`${name}: ${value}`)}`)
230
+ .join(" ");
231
+ return [
232
+ "set -euo pipefail",
233
+ ...stableWorkspaceFilePrelude(reference.path, {
234
+ nonCanonical: 51,
235
+ invalidFile: 52,
236
+ }),
237
+ `[ "$size" = ${shellQuote(String(reference.sizeBytes))} ] || exit 53`,
238
+ 'sha=$(video_reference_sha256 "$fd")',
239
+ `[ "$sha" = ${shellQuote(reference.sha256)} ] || exit 54`,
240
+ 'exec 3<&-; exec 3<"$resolved"',
241
+ 'fd=/proc/$$/fd/3; [ -e "$fd" ] || fd=/dev/fd/3',
242
+ 'reopened=$(video_reference_stat_identity "$fd")',
243
+ '[ "$before" = "$reopened" ] || exit 55',
244
+ '[ "$(video_reference_stat_content_identity "$resolved")" = "$(video_reference_stat_content_identity "$fd")" ] || exit 55',
245
+ '[ "$(realpath -- "$requested")" = "$expected" ] || exit 55',
246
+ `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)}`,
247
+ 'after=$(video_reference_stat_identity "$fd")',
248
+ '[ "$before" = "$after" ] || exit 56',
249
+ ].join("\n");
250
+ }
251
+
252
+ function stableWorkspaceFilePrelude(
253
+ path: string,
254
+ exitCodes: { nonCanonical: number; invalidFile: number },
255
+ ): string[] {
256
+ const requested = workspaceRelativeReferencePath(path);
257
+ return [
258
+ `requested=${shellQuote(requested)}`,
259
+ "root=$(pwd -P)",
260
+ 'expected="$root/${requested#./}"',
261
+ 'resolved=$(realpath -- "$requested")',
262
+ `[ "$resolved" = "$expected" ] || exit ${exitCodes.nonCanonical}`,
263
+ `[ -f "$resolved" ] || exit ${exitCodes.invalidFile}`,
264
+ 'exec 3<"$resolved"',
265
+ 'fd=/proc/$$/fd/3; [ -e "$fd" ] || fd=/dev/fd/3',
266
+ `[ -f "$fd" ] || exit ${exitCodes.invalidFile}`,
267
+ "video_reference_platform=$(uname -s)",
268
+ 'if [ "$video_reference_platform" = Darwin ]; then',
269
+ ' video_reference_stat_identity() { stat -f "%d:%i:%z:%m" "$1"; }',
270
+ ' video_reference_stat_content_identity() { stat -f "%i:%z:%m" "$1"; }',
271
+ ' video_reference_stat_size() { stat -f "%z" "$1"; }',
272
+ "else",
273
+ ' video_reference_stat_identity() { stat -Lc "%d:%i:%s:%Y" -- "$1"; }',
274
+ ' video_reference_stat_content_identity() { stat -Lc "%d:%i:%s:%Y" -- "$1"; }',
275
+ ' video_reference_stat_size() { stat -Lc "%s" -- "$1"; }',
276
+ "fi",
277
+ "video_reference_sha256() {",
278
+ " if command -v sha256sum >/dev/null 2>&1; then",
279
+ " sha256sum \"$1\" | awk '{print $1}'",
280
+ " else",
281
+ " shasum -a 256 \"$1\" | awk '{print $1}'",
282
+ " fi",
283
+ "}",
284
+ 'if [ "$video_reference_platform" != Darwin ]; then',
285
+ ' fd_path=$(realpath -- "$fd")',
286
+ ` [ "$fd_path" = "$expected" ] || exit ${exitCodes.invalidFile}`,
287
+ "fi",
288
+ `[ "$(video_reference_stat_content_identity "$resolved")" = "$(video_reference_stat_content_identity "$fd")" ] || exit ${exitCodes.invalidFile}`,
289
+ `[ "$(realpath -- "$requested")" = "$expected" ] || exit ${exitCodes.invalidFile}`,
290
+ 'before=$(video_reference_stat_identity "$fd")',
291
+ 'size=$(video_reference_stat_size "$fd")',
292
+ ];
293
+ }
294
+
295
+ function workspaceRelativeReferencePath(path: string): string {
296
+ if (!path.startsWith("/workspace/") || /[\0\r\n]/u.test(path)) {
297
+ throw new VideoReferenceInputError(
298
+ "invalid_reference_path",
299
+ "Video references must use an exact file path under /workspace.",
300
+ );
301
+ }
302
+ const segments = path.slice("/workspace/".length).split("/");
303
+ if (segments.some((segment) => segment.length === 0 || segment === "." || segment === "..")) {
304
+ throw new VideoReferenceInputError(
305
+ "invalid_reference_path",
306
+ "Video references must use a canonical file path under /workspace.",
307
+ );
308
+ }
309
+ return `./${segments.join("/")}`;
310
+ }
311
+
312
+ function sniffContentType(prefix: Uint8Array): string {
313
+ if (
314
+ prefix.byteLength >= 8 &&
315
+ Buffer.from(prefix.subarray(0, 8)).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))
316
+ ) {
317
+ return "image/png";
318
+ }
319
+ if (prefix.byteLength >= 3 && prefix[0] === 0xff && prefix[1] === 0xd8 && prefix[2] === 0xff) {
320
+ return "image/jpeg";
321
+ }
322
+ if (
323
+ prefix.byteLength >= 12 &&
324
+ Buffer.from(prefix.subarray(0, 4)).toString("ascii") === "RIFF" &&
325
+ Buffer.from(prefix.subarray(8, 12)).toString("ascii") === "WEBP"
326
+ ) {
327
+ return "image/webp";
328
+ }
329
+ if (prefix.byteLength >= 12 && Buffer.from(prefix.subarray(4, 8)).toString("ascii") === "ftyp") {
330
+ return "video/mp4";
331
+ }
332
+ throw new VideoReferenceInputError(
333
+ "unsupported_reference_media",
334
+ "The video reference is not a supported PNG, JPEG, WebP, or MP4 file.",
335
+ );
336
+ }
337
+
338
+ function shellQuote(value: string): string {
339
+ return `'${value.replaceAll("'", `'"'"'`)}'`;
340
+ }
341
+
342
+ function throwIfAborted(signal?: AbortSignal): void {
343
+ if (signal?.aborted) throw signal.reason ?? new Error("Video reference staging was cancelled");
344
+ }
345
+
346
+ export const VIDEO_REFERENCE_LIMITS = Object.freeze({
347
+ imageBytes: MAX_IMAGE_REFERENCE_BYTES,
348
+ videoBytes: MAX_VIDEO_REFERENCE_BYTES,
349
+ outputBytes: GENERATED_VIDEO_MAX_BYTES,
350
+ });
@@ -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,122 @@
1
+ import { environmentsEncryptionKeyBytes, type Settings } from "@opengeni/config";
2
+ import {
3
+ materializeXaiCredentialForRun,
4
+ refreshXaiSubscriptionCredentialSerialized,
5
+ type Database,
6
+ } from "@opengeni/db";
7
+ import type { XaiProviderAccountAuthoritySnapshotV1 } from "@opengeni/contracts";
8
+ import {
9
+ refreshXaiToken,
10
+ XAI_CLIENT_VERSION,
11
+ XAI_SUBSCRIPTION_MODEL_ID_PREFIX,
12
+ XaiSubscriptionReloginRequired,
13
+ xaiAccessTokenExpiry,
14
+ type XaiFetch,
15
+ type XaiSubscriptionRequestContext,
16
+ type XaiSubscriptionTokenSnapshot,
17
+ } from "@opengeni/xai-subscription";
18
+
19
+ export type XaiTurnRequestAuthorization = {
20
+ context: XaiSubscriptionRequestContext;
21
+ credentialId: string;
22
+ };
23
+
24
+ export async function buildXaiTurnRequestAuthorization(input: {
25
+ db: Database;
26
+ settings: Settings;
27
+ accountId: string;
28
+ workspaceId: string;
29
+ subjectId: string;
30
+ sessionId: string;
31
+ turnId: string;
32
+ credentialId: string;
33
+ authoritySnapshot: XaiProviderAccountAuthoritySnapshotV1;
34
+ fetch?: XaiFetch;
35
+ hostedSearch?: XaiSubscriptionRequestContext["hostedSearch"];
36
+ onFinalContextUsage?: XaiSubscriptionRequestContext["onFinalContextUsage"];
37
+ nextRequestId?: XaiSubscriptionRequestContext["nextRequestId"];
38
+ }): Promise<XaiTurnRequestAuthorization> {
39
+ const encryptionKey = environmentsEncryptionKeyBytes(input.settings);
40
+ if (!encryptionKey) {
41
+ throw new Error("SuperGrok subscriptions require OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
42
+ }
43
+ let credential = await materializeXaiCredentialForRun(input.db, {
44
+ workspaceId: input.workspaceId,
45
+ subjectId: input.subjectId,
46
+ credentialId: input.credentialId,
47
+ authoritySnapshot: input.authoritySnapshot,
48
+ encryptionKey,
49
+ });
50
+
51
+ const tokenSnapshot = (): XaiSubscriptionTokenSnapshot => {
52
+ const accessToken = credential.secret.accessToken;
53
+ const userId = credential.providerAccountId;
54
+ if (!accessToken || !userId) {
55
+ throw new XaiSubscriptionReloginRequired(
56
+ "The SuperGrok connection is missing a verified token identity. Reconnect the account.",
57
+ );
58
+ }
59
+ return { accessToken, userId };
60
+ };
61
+
62
+ const refresh = async (): Promise<XaiSubscriptionTokenSnapshot> => {
63
+ const observedAccessToken = credential.secret.accessToken;
64
+ const observedRefreshToken = credential.secret.refreshToken;
65
+ if (!observedRefreshToken) {
66
+ throw new XaiSubscriptionReloginRequired(
67
+ "The SuperGrok connection cannot be refreshed. Reconnect the account.",
68
+ );
69
+ }
70
+ const result = await refreshXaiSubscriptionCredentialSerialized(input.db, {
71
+ accountId: input.accountId,
72
+ workspaceId: input.workspaceId,
73
+ subjectId: input.subjectId,
74
+ credentialId: input.credentialId,
75
+ authoritySnapshot: input.authoritySnapshot,
76
+ encryptionKey,
77
+ observedAccessToken,
78
+ observedRefreshToken,
79
+ refresh: async (current) => {
80
+ const refreshToken = current.secret.refreshToken;
81
+ if (!refreshToken) {
82
+ throw new XaiSubscriptionReloginRequired(
83
+ "The SuperGrok connection cannot be refreshed. Reconnect the account.",
84
+ );
85
+ }
86
+ const tokens = await refreshXaiToken(refreshToken, {
87
+ ...(input.fetch ? { fetch: input.fetch } : {}),
88
+ });
89
+ return {
90
+ secret: {
91
+ version: 1,
92
+ accessToken: tokens.accessToken,
93
+ refreshToken: tokens.refreshToken,
94
+ },
95
+ expiresAt:
96
+ xaiAccessTokenExpiry(tokens.accessToken) ??
97
+ new Date(Date.now() + tokens.expiresInSeconds * 1_000),
98
+ };
99
+ },
100
+ });
101
+ credential = result.credential;
102
+ return tokenSnapshot();
103
+ };
104
+
105
+ return {
106
+ credentialId: input.credentialId,
107
+ context: {
108
+ clientVersion: XAI_CLIENT_VERSION,
109
+ sessionId: input.sessionId,
110
+ turnId: input.turnId,
111
+ getToken: async () => tokenSnapshot(),
112
+ refresh,
113
+ resolveModel: (model) =>
114
+ model.startsWith(XAI_SUBSCRIPTION_MODEL_ID_PREFIX)
115
+ ? model.slice(XAI_SUBSCRIPTION_MODEL_ID_PREFIX.length)
116
+ : model,
117
+ ...(input.hostedSearch ? { hostedSearch: input.hostedSearch } : {}),
118
+ ...(input.onFinalContextUsage ? { onFinalContextUsage: input.onFinalContextUsage } : {}),
119
+ ...(input.nextRequestId ? { nextRequestId: input.nextRequestId } : {}),
120
+ },
121
+ };
122
+ }
@@ -0,0 +1,86 @@
1
+ import {
2
+ generateXaiSubscriptionImage,
3
+ XAI_IMAGE_MODEL,
4
+ XAI_SUBSCRIPTION_PROVIDER_ID,
5
+ type XaiSubscriptionRequestContext,
6
+ } from "@opengeni/xai-subscription";
7
+ import type { Database } from "@opengeni/db";
8
+ import type { ObjectStorage } from "@opengeni/storage";
9
+ import type { GeneratedImageReceipt } from "./generated-images";
10
+ import {
11
+ executeImageGenerationOperation,
12
+ imageProviderBindingHash,
13
+ } from "./image-generation-operation";
14
+ import type { ResolvedImageGenerationReference } from "./image-generation-references";
15
+
16
+ export type XaiImageGenerationPorts = {
17
+ execute: typeof executeImageGenerationOperation;
18
+ generate: typeof generateXaiSubscriptionImage;
19
+ };
20
+
21
+ const xaiImageGenerationPorts: XaiImageGenerationPorts = {
22
+ execute: executeImageGenerationOperation,
23
+ generate: generateXaiSubscriptionImage,
24
+ };
25
+
26
+ /** Execute xAI's standalone subscription image path under the durable paid-operation fence. */
27
+ export async function executeXaiSubscriptionImageGeneration(
28
+ input: {
29
+ db: Database;
30
+ objectStorage: ObjectStorage | null;
31
+ accountId: string;
32
+ workspaceId: string;
33
+ sessionId: string;
34
+ turnId: string;
35
+ attemptId: string;
36
+ toolCallId: string;
37
+ prompt: string;
38
+ references?: readonly ResolvedImageGenerationReference[];
39
+ credentialId: string;
40
+ xaiContext: Pick<XaiSubscriptionRequestContext, "getToken" | "refresh">;
41
+ abortSignal?: AbortSignal;
42
+ },
43
+ ports: XaiImageGenerationPorts = xaiImageGenerationPorts,
44
+ ): Promise<GeneratedImageReceipt> {
45
+ const providerBindingHash = imageProviderBindingHash(
46
+ XAI_SUBSCRIPTION_PROVIDER_ID,
47
+ input.credentialId,
48
+ );
49
+ return await ports.execute({
50
+ db: input.db,
51
+ objectStorage: input.objectStorage,
52
+ accountId: input.accountId,
53
+ workspaceId: input.workspaceId,
54
+ sessionId: input.sessionId,
55
+ turnId: input.turnId,
56
+ attemptId: input.attemptId,
57
+ toolCallId: input.toolCallId,
58
+ prompt: input.prompt,
59
+ providerId: XAI_SUBSCRIPTION_PROVIDER_ID,
60
+ providerBindingHash,
61
+ modelId: XAI_IMAGE_MODEL,
62
+ generate: async () => {
63
+ const generated = await ports.generate({
64
+ prompt: input.prompt,
65
+ sessionId: input.sessionId,
66
+ ...(input.references?.length
67
+ ? {
68
+ references: input.references.map((reference) => ({
69
+ mediaType: reference.mediaType,
70
+ bytes: reference.bytes,
71
+ })),
72
+ }
73
+ : {}),
74
+ getToken: input.xaiContext.getToken,
75
+ refresh: input.xaiContext.refresh,
76
+ ...(input.abortSignal ? { abortSignal: input.abortSignal } : {}),
77
+ });
78
+ return {
79
+ toolCallId: input.toolCallId,
80
+ providerItemId: null,
81
+ bytes: generated.bytes,
82
+ declaredMediaType: generated.declaredMediaType,
83
+ };
84
+ },
85
+ });
86
+ }