@opengeni/worker-bundle 0.7.0 → 0.7.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 (45) hide show
  1. package/dist/index.d.ts +182 -67
  2. package/dist/index.js +7127 -1654
  3. package/dist/index.js.map +1 -1
  4. package/package.json +19 -19
  5. package/src/activities/agent-turn.ts +4405 -1089
  6. package/src/activities/capabilities.ts +43 -8
  7. package/src/activities/codex-capacity.ts +223 -0
  8. package/src/activities/codex-rotation.ts +550 -54
  9. package/src/activities/common.ts +7 -1
  10. package/src/activities/context-compaction.ts +152 -118
  11. package/src/activities/documents.ts +32 -27
  12. package/src/activities/environment.ts +318 -75
  13. package/src/activities/file-upload-reaper.ts +79 -0
  14. package/src/activities/git-credential-renewal.ts +183 -0
  15. package/src/activities/goals.ts +142 -123
  16. package/src/activities/packs.ts +55 -5
  17. package/src/activities/parent-wake.ts +228 -41
  18. package/src/activities/recording.ts +154 -122
  19. package/src/activities/redaction.ts +3 -1
  20. package/src/activities/rig-verification.ts +441 -0
  21. package/src/activities/run-input.ts +110 -122
  22. package/src/activities/sandbox-lease.ts +186 -51
  23. package/src/activities/scheduled-tasks.ts +287 -171
  24. package/src/activities/session-state.ts +168 -157
  25. package/src/activities/streaming.ts +143 -19
  26. package/src/activities/turn-attempt-fenced.ts +14 -0
  27. package/src/activities/types.ts +102 -32
  28. package/src/activities/workflow-wake.ts +44 -0
  29. package/src/activities/workspace-capture.ts +964 -0
  30. package/src/activities.ts +75 -35
  31. package/src/concurrency.ts +11 -0
  32. package/src/http.ts +16 -6
  33. package/src/index.ts +322 -49
  34. package/src/observability-metrics.ts +441 -8
  35. package/src/op-journal.ts +46 -0
  36. package/src/sandbox-resume.ts +431 -201
  37. package/src/sandbox-routing.ts +209 -22
  38. package/src/workflow-wake-contract.ts +5 -0
  39. package/src/workflows/activities.ts +58 -7
  40. package/src/workflows/file-upload-reaper.ts +12 -0
  41. package/src/workflows/rig-verification.ts +28 -0
  42. package/src/workflows/scheduled-tasks.ts +12 -26
  43. package/src/workflows/session.ts +330 -173
  44. package/src/workflows/workflow-wake.ts +12 -0
  45. package/src/workflows.ts +9 -7
@@ -0,0 +1,441 @@
1
+ import type { AccessGrant, Rig, RigChange, RigVersion } from "@opengeni/contracts";
2
+ import {
3
+ recordRigAuditEvent,
4
+ classifyRigVerificationOutcome,
5
+ promoteSetupAppendChange,
6
+ } from "@opengeni/core";
7
+ import {
8
+ beginRigChangeVerificationAttempt,
9
+ getRig,
10
+ getRigChange,
11
+ getRigVersionById,
12
+ sanitizeEventPayload,
13
+ sanitizeEventString,
14
+ sanitizeMemoryText,
15
+ updateRigChangeStatus,
16
+ type Database,
17
+ } from "@opengeni/db";
18
+ import {
19
+ establishSandboxSessionFromEnvelope,
20
+ runRigSetupHook,
21
+ sandboxCommandExitCode,
22
+ sandboxCommandOutput,
23
+ type EstablishedSandboxSession,
24
+ } from "@opengeni/runtime";
25
+ import type { ActivityServices } from "./types";
26
+ import { settingsWithRigImage } from "./packs";
27
+
28
+ export type RigVerificationWorkflowInput =
29
+ | { workspaceId: string; changeId: string; versionId?: never }
30
+ | { workspaceId: string; versionId: string; changeId?: never };
31
+
32
+ type CommandResult = {
33
+ exitCode: number | null;
34
+ output: string;
35
+ };
36
+
37
+ type CommandSession = {
38
+ exec?: (args: Record<string, unknown>) => Promise<unknown>;
39
+ execCommand?: (args: Record<string, unknown>) => Promise<unknown>;
40
+ };
41
+
42
+ const OUTPUT_TAIL_LIMIT = 64 * 1024;
43
+
44
+ function tail(value: string, limit = OUTPUT_TAIL_LIMIT): string {
45
+ return value.length > limit ? value.slice(-limit) : value;
46
+ }
47
+
48
+ function scrubVerificationOutput(value: string): string {
49
+ return sanitizeMemoryText(sanitizeEventString(value)).text;
50
+ }
51
+
52
+ function scrubVerificationPayload<T>(value: T): T {
53
+ return sanitizeEventPayload(value);
54
+ }
55
+
56
+ function systemGrant(rig: Rig): AccessGrant {
57
+ return {
58
+ accountId: rig.accountId,
59
+ workspaceId: rig.workspaceId,
60
+ subjectId: "system:rig-verification",
61
+ permissions: ["rigs:use", "rigs:manage"],
62
+ };
63
+ }
64
+
65
+ async function terminateThrowaway(established: EstablishedSandboxSession | null): Promise<void> {
66
+ if (!established) {
67
+ return;
68
+ }
69
+ const client = established.client as { delete?: (state: unknown) => Promise<unknown> };
70
+ if (typeof client.delete === "function" && established.sessionState !== undefined) {
71
+ await client.delete(established.sessionState).catch(() => undefined);
72
+ return;
73
+ }
74
+ const session = established.session as {
75
+ terminate?: () => Promise<unknown>;
76
+ kill?: () => Promise<unknown>;
77
+ close?: () => Promise<unknown>;
78
+ closed?: boolean;
79
+ };
80
+ if (session.terminate) {
81
+ await session.terminate().catch(() => undefined);
82
+ } else if (session.kill) {
83
+ await session.kill().catch(() => undefined);
84
+ } else if (session.close && !session.closed) {
85
+ await session.close().catch(() => undefined);
86
+ }
87
+ }
88
+
89
+ async function runCommand(
90
+ session: CommandSession,
91
+ command: string,
92
+ timeoutMs: number,
93
+ ): Promise<CommandResult> {
94
+ const args = {
95
+ cmd: command,
96
+ workdir: "/workspace",
97
+ runAs: "root",
98
+ yieldTimeMs: timeoutMs,
99
+ maxOutputTokens: 40_000,
100
+ };
101
+ const result = session.exec
102
+ ? await session.exec(args)
103
+ : session.execCommand
104
+ ? await session.execCommand(args)
105
+ : (() => {
106
+ throw new Error("Sandbox session does not support command execution");
107
+ })();
108
+ return {
109
+ exitCode: sandboxCommandExitCode(result),
110
+ output: scrubVerificationOutput(tail(sandboxCommandOutput(result))),
111
+ };
112
+ }
113
+
114
+ function setupAppendCommand(change: RigChange): string | null {
115
+ if (change.kind !== "setup_append") {
116
+ return null;
117
+ }
118
+ const command = (change.payload as { command?: unknown }).command;
119
+ return typeof command === "string" ? command : null;
120
+ }
121
+
122
+ function candidateVersionForChange(baseVersion: RigVersion, change: RigChange): RigVersion {
123
+ if (change.kind !== "definition_edit") {
124
+ return baseVersion;
125
+ }
126
+ const payload = change.payload as {
127
+ image?: unknown;
128
+ setupScript?: unknown;
129
+ checks?: unknown;
130
+ credentialHooks?: unknown;
131
+ defaultVariableSetIds?: unknown;
132
+ changelog?: unknown;
133
+ };
134
+ return {
135
+ ...baseVersion,
136
+ image: payload.image === undefined ? baseVersion.image : (payload.image as string | null),
137
+ setupScript:
138
+ payload.setupScript === undefined
139
+ ? baseVersion.setupScript
140
+ : (payload.setupScript as string | null),
141
+ checks: Array.isArray(payload.checks)
142
+ ? (payload.checks as RigVersion["checks"])
143
+ : baseVersion.checks,
144
+ credentialHooks: Array.isArray(payload.credentialHooks)
145
+ ? (payload.credentialHooks as string[])
146
+ : baseVersion.credentialHooks,
147
+ defaultVariableSetIds: Array.isArray(payload.defaultVariableSetIds)
148
+ ? (payload.defaultVariableSetIds as string[])
149
+ : baseVersion.defaultVariableSetIds,
150
+ changelog: typeof payload.changelog === "string" ? payload.changelog : baseVersion.changelog,
151
+ };
152
+ }
153
+
154
+ async function loadChangeTarget(
155
+ db: Database,
156
+ workspaceId: string,
157
+ changeId: string,
158
+ ): Promise<{ rig: Rig; baseVersion: RigVersion; change: RigChange }> {
159
+ const change = await getRigChange(db, workspaceId, changeId);
160
+ if (!change) {
161
+ throw new Error(`Rig change not found: ${changeId}`);
162
+ }
163
+ const rig = await getRig(db, workspaceId, change.rigId);
164
+ if (!rig) {
165
+ throw new Error(`Rig not found for change: ${change.rigId}`);
166
+ }
167
+ if (!change.baseVersionId) {
168
+ throw new Error(`Rig change ${change.id} has no base version`);
169
+ }
170
+ const baseVersion = await getRigVersionById(db, workspaceId, change.baseVersionId);
171
+ if (!baseVersion || baseVersion.rigId !== rig.id) {
172
+ throw new Error(`Base rig version not found: ${change.baseVersionId}`);
173
+ }
174
+ return { rig, baseVersion, change };
175
+ }
176
+
177
+ async function loadVersionTarget(
178
+ db: Database,
179
+ workspaceId: string,
180
+ versionId: string,
181
+ ): Promise<{ rig: Rig; version: RigVersion }> {
182
+ const version = await getRigVersionById(db, workspaceId, versionId);
183
+ if (!version) {
184
+ throw new Error(`Rig version not found: ${versionId}`);
185
+ }
186
+ const rig = await getRig(db, workspaceId, version.rigId);
187
+ if (!rig) {
188
+ throw new Error(`Rig not found for version: ${version.rigId}`);
189
+ }
190
+ return { rig, version };
191
+ }
192
+
193
+ export function createRigVerificationActivities(services: () => Promise<ActivityServices>) {
194
+ return {
195
+ verifyRigChange: async (input: { workspaceId: string; changeId: string }) => {
196
+ const { settings, db } = await services();
197
+ const { rig, baseVersion, change } = await loadChangeTarget(
198
+ db,
199
+ input.workspaceId,
200
+ input.changeId,
201
+ );
202
+ const grant = systemGrant(rig);
203
+ const startedAt = new Date().toISOString();
204
+ await beginRigChangeVerificationAttempt(db, input.workspaceId, change.id, {
205
+ startedAt,
206
+ allowAlreadyVerifying: true,
207
+ });
208
+ await recordRigAuditEvent(db, {
209
+ grant,
210
+ action: "rig.verification.started",
211
+ rigId: rig.id,
212
+ metadata: { changeId: change.id },
213
+ });
214
+
215
+ let established: EstablishedSandboxSession | null = null;
216
+ const verification: Record<string, unknown> = { startedAt, checkResults: [] };
217
+ try {
218
+ const candidateVersion = candidateVersionForChange(baseVersion, change);
219
+ const runSettings = settingsWithRigImage(settings, candidateVersion.image);
220
+ established = await establishSandboxSessionFromEnvelope(runSettings, null, {
221
+ sessionId: `rig-verification-${change.id}`,
222
+ recovery: "create-or-restore",
223
+ environment: {},
224
+ });
225
+ if ((candidateVersion.setupScript ?? "").trim()) {
226
+ await runRigSetupHook(established.session as never, {
227
+ environment: {},
228
+ runAs: "root",
229
+ rigSetup: {
230
+ rigId: rig.id,
231
+ rigName: rig.name,
232
+ versionId: candidateVersion.id,
233
+ script: candidateVersion.setupScript ?? "",
234
+ timeoutMs: settings.rigSetupTimeoutMs,
235
+ },
236
+ });
237
+ verification.setupResult = { exitCode: 0, output: "" };
238
+ }
239
+ const command = setupAppendCommand(change);
240
+ if (command) {
241
+ const commandResult = await runCommand(
242
+ established.session as CommandSession,
243
+ command,
244
+ settings.rigSetupTimeoutMs,
245
+ );
246
+ verification.commandResult = commandResult;
247
+ if (commandResult.exitCode !== 0) {
248
+ verification.finishedAt = new Date().toISOString();
249
+ verification.passed = false;
250
+ const updated = await updateRigChangeStatus(db, input.workspaceId, change.id, {
251
+ status: "rejected",
252
+ verification: scrubVerificationPayload(verification),
253
+ });
254
+ await recordRigAuditEvent(db, {
255
+ grant,
256
+ action: "rig.verification.failed",
257
+ rigId: rig.id,
258
+ metadata: { changeId: change.id, status: "rejected" },
259
+ });
260
+ await recordRigAuditEvent(db, {
261
+ grant,
262
+ action: "rig.change.rejected",
263
+ rigId: rig.id,
264
+ metadata: { changeId: change.id },
265
+ });
266
+ return updated;
267
+ }
268
+ }
269
+ const checkResults = [];
270
+ for (const check of candidateVersion.checks) {
271
+ const result = await runCommand(
272
+ established.session as CommandSession,
273
+ check.command,
274
+ settings.rigSetupTimeoutMs,
275
+ );
276
+ checkResults.push({
277
+ name: check.name,
278
+ command: scrubVerificationOutput(check.command),
279
+ ...result,
280
+ });
281
+ }
282
+ verification.checkResults = checkResults;
283
+ const passed = checkResults.every((result) => result.exitCode === 0);
284
+ verification.finishedAt = new Date().toISOString();
285
+ verification.passed = passed;
286
+ const classified = classifyRigVerificationOutcome({ kind: change.kind, passed });
287
+ if (classified.action === "auto_promote") {
288
+ // Keep the change `verifying` (NOT `proposed`) across the write→promote
289
+ // gap: promoteSetupAppendChange accepts `verifying`, and leaving it
290
+ // `verifying` keeps beginRigChangeVerificationAttempt blocking a
291
+ // concurrent /verify — resetting to `proposed` would reopen that race
292
+ // (a second run could reject a change whose first verification passed).
293
+ await updateRigChangeStatus(db, input.workspaceId, change.id, {
294
+ status: "verifying",
295
+ verification: scrubVerificationPayload(verification),
296
+ });
297
+ const { change: merged } = await promoteSetupAppendChange({ db }, grant, rig, {
298
+ ...change,
299
+ verification,
300
+ });
301
+ await recordRigAuditEvent(db, {
302
+ grant,
303
+ action: "rig.verification.passed",
304
+ rigId: rig.id,
305
+ metadata: { changeId: change.id },
306
+ });
307
+ return merged;
308
+ }
309
+ const updated = await updateRigChangeStatus(db, input.workspaceId, change.id, {
310
+ status: classified.status,
311
+ verification: scrubVerificationPayload(verification),
312
+ });
313
+ await recordRigAuditEvent(db, {
314
+ grant,
315
+ action: passed ? "rig.verification.passed" : "rig.verification.failed",
316
+ rigId: rig.id,
317
+ metadata: { changeId: change.id, status: classified.status },
318
+ });
319
+ if (!passed) {
320
+ await recordRigAuditEvent(db, {
321
+ grant,
322
+ action: "rig.change.rejected",
323
+ rigId: rig.id,
324
+ metadata: { changeId: change.id },
325
+ });
326
+ }
327
+ return updated;
328
+ } catch (error) {
329
+ verification.finishedAt = new Date().toISOString();
330
+ verification.passed = false;
331
+ verification.error = scrubVerificationOutput(
332
+ error instanceof Error ? error.message : String(error),
333
+ );
334
+ const updated = await updateRigChangeStatus(db, input.workspaceId, change.id, {
335
+ status: "failed",
336
+ verification: scrubVerificationPayload(verification),
337
+ });
338
+ await recordRigAuditEvent(db, {
339
+ grant,
340
+ action: "rig.verification.failed",
341
+ rigId: rig.id,
342
+ metadata: { changeId: change.id, status: "failed" },
343
+ });
344
+ await recordRigAuditEvent(db, {
345
+ grant,
346
+ action: "rig.change.failed",
347
+ rigId: rig.id,
348
+ metadata: { changeId: change.id },
349
+ });
350
+ return updated;
351
+ } finally {
352
+ await terminateThrowaway(established);
353
+ }
354
+ },
355
+
356
+ verifyRigVersion: async (input: { workspaceId: string; versionId: string }) => {
357
+ const { settings, db } = await services();
358
+ const { rig, version } = await loadVersionTarget(db, input.workspaceId, input.versionId);
359
+ const grant = systemGrant(rig);
360
+ const startedAt = new Date().toISOString();
361
+ await recordRigAuditEvent(db, {
362
+ grant,
363
+ action: "rig.verification.started",
364
+ rigId: rig.id,
365
+ metadata: { versionId: version.id },
366
+ });
367
+ let established: EstablishedSandboxSession | null = null;
368
+ try {
369
+ const runSettings = settingsWithRigImage(settings, version.image);
370
+ established = await establishSandboxSessionFromEnvelope(runSettings, null, {
371
+ sessionId: `rig-version-verification-${version.id}`,
372
+ recovery: "create-or-restore",
373
+ environment: {},
374
+ });
375
+ if ((version.setupScript ?? "").trim()) {
376
+ await runRigSetupHook(established.session as never, {
377
+ environment: {},
378
+ runAs: "root",
379
+ rigSetup: {
380
+ rigId: rig.id,
381
+ rigName: rig.name,
382
+ versionId: version.id,
383
+ script: version.setupScript ?? "",
384
+ timeoutMs: settings.rigSetupTimeoutMs,
385
+ },
386
+ });
387
+ }
388
+ const checkResults = [];
389
+ for (const check of version.checks) {
390
+ checkResults.push({
391
+ name: check.name,
392
+ command: scrubVerificationOutput(check.command),
393
+ ...(await runCommand(
394
+ established.session as CommandSession,
395
+ check.command,
396
+ settings.rigSetupTimeoutMs,
397
+ )),
398
+ });
399
+ }
400
+ const passed = checkResults.every((result) => result.exitCode === 0);
401
+ await recordRigAuditEvent(db, {
402
+ grant,
403
+ action: passed ? "rig.verification.passed" : "rig.verification.failed",
404
+ rigId: rig.id,
405
+ metadata: scrubVerificationPayload({
406
+ versionId: version.id,
407
+ startedAt,
408
+ finishedAt: new Date().toISOString(),
409
+ passed,
410
+ checkResults,
411
+ }),
412
+ });
413
+ return { versionId: version.id, passed, checkResults };
414
+ } catch (error) {
415
+ // Infra failure (sandbox establish / setup / check exec threw) — record
416
+ // rig.verification.failed so activeVersionHealth reflects the failed
417
+ // re-run instead of staying stale, symmetric to verifyRigChange. Then
418
+ // rethrow so the Temporal activity still surfaces the failure.
419
+ const detail = tail(
420
+ scrubVerificationOutput(error instanceof Error ? error.message : String(error)),
421
+ 4096,
422
+ );
423
+ await recordRigAuditEvent(db, {
424
+ grant,
425
+ action: "rig.verification.failed",
426
+ rigId: rig.id,
427
+ metadata: scrubVerificationPayload({
428
+ versionId: version.id,
429
+ startedAt,
430
+ finishedAt: new Date().toISOString(),
431
+ passed: false,
432
+ error: detail,
433
+ }),
434
+ });
435
+ throw error;
436
+ } finally {
437
+ await terminateThrowaway(established);
438
+ }
439
+ },
440
+ };
441
+ }