@opengeni/worker-bundle 0.7.4 → 0.8.3

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 (42) hide show
  1. package/dist/chunk-BJUW5LCM.js +325 -0
  2. package/dist/chunk-BJUW5LCM.js.map +1 -0
  3. package/dist/host-export.d.ts +70 -0
  4. package/dist/host-export.js +7 -0
  5. package/dist/host-export.js.map +1 -0
  6. package/dist/index.d.ts +98 -6
  7. package/dist/index.js +2853 -834
  8. package/dist/index.js.map +1 -1
  9. package/dist/workflow-bundle.js +12358 -0
  10. package/package.json +18 -12
  11. package/src/activities/agent-turn.ts +2052 -554
  12. package/src/activities/capabilities.ts +9 -2
  13. package/src/activities/codex-capacity.ts +6 -11
  14. package/src/activities/codex-rotation.ts +75 -86
  15. package/src/activities/context-compaction.ts +104 -24
  16. package/src/activities/environment.ts +367 -92
  17. package/src/activities/expiring-material-renewal.ts +143 -0
  18. package/src/activities/goals.ts +4 -0
  19. package/src/activities/mcp-credentials.ts +38 -0
  20. package/src/activities/packs.ts +115 -2
  21. package/src/activities/parent-wake.ts +48 -38
  22. package/src/activities/run-credential-renewal.ts +70 -0
  23. package/src/activities/run-credentials.ts +132 -0
  24. package/src/activities/run-input.ts +23 -0
  25. package/src/activities/sandbox-lease.ts +1 -1
  26. package/src/activities/scheduled-tasks.ts +21 -0
  27. package/src/activities/session-state.ts +83 -12
  28. package/src/activities/streaming.ts +1 -0
  29. package/src/activities/toolspace-token-renewal.ts +57 -0
  30. package/src/activities/types.ts +44 -6
  31. package/src/activities/workspace-capture.ts +113 -93
  32. package/src/activities.ts +10 -2
  33. package/src/host-export-pump.ts +416 -0
  34. package/src/host-export.ts +22 -0
  35. package/src/http.ts +77 -28
  36. package/src/index.ts +360 -156
  37. package/src/sandbox-resume.ts +47 -1
  38. package/src/sandbox-routing.ts +1 -1
  39. package/src/worker-service-lifecycle.ts +104 -0
  40. package/src/workflows/activities.ts +13 -7
  41. package/src/workflows/session.ts +219 -23
  42. package/src/workflows.ts +1 -0
@@ -1,4 +1,4 @@
1
- // Workbench v2 — turn-end workspace capture (dossier §10.1).
1
+ // Workbench v2 — turn-end workspace capture.
2
2
  //
3
3
  // The universal spine that makes the session workspace paint instantly on cold /
4
4
  // offline reads: at TURN END, while the box is still guaranteed live and the
@@ -10,24 +10,25 @@
10
10
  // this when the machine is cold/offline; a warm box always wins (capture is a
11
11
  // labelled cache, never a replacement).
12
12
  //
13
- // SAFETY INVARIANTS (non-negotiable — see dossier §7.3 / §19):
13
+ // SAFETY INVARIANTS (non-negotiable — see):
14
14
  // • This module NEVER calls close()/terminate()/kill() on the session handle.
15
15
  // session.close() == terminate() on Modal and would kill the user's box.
16
16
  // Only the reaper terminates. We drop references, nothing more.
17
17
  // • The whole capture is time-capped (60s) and best-effort: ANY failure logs
18
18
  // "workspace capture failed — turn outcome unaffected" and returns; nothing
19
19
  // throws past captureWorkspaceRevision.
20
- // • The DB write is fenced on the lease epoch — a superseded lease writes zero
21
- // rows (insertWorkspaceCapture).
20
+ // • The DB write is fenced on the exact attempt, accepted control requests,
21
+ // and lease epoch — a cancelled attempt or superseded lease writes zero rows
22
+ // (insertWorkspaceCapture).
22
23
  // • F9 storage ordering: blobs + manifest are PUT and the row is committed
23
24
  // BEFORE any GC delete runs.
24
25
  //
25
26
  // It is deliberately an EXTERNAL module with a SINGLE call-site line in
26
- // agent-turn.ts's finally (dossier "Accepted warts" #2 — do not grow the turn
27
+ // agent-turn.ts's finally (the accepted design tradeoff — do not grow the turn
27
28
  // activity). The emitted `workspace.revision.captured` event is ANNOUNCE-ONLY
28
29
  // (metadata, never content) and must never gain a rendered timeline item.
29
30
 
30
- import { createHash, randomUUID } from "node:crypto";
31
+ import { createHash } from "node:crypto";
31
32
  import type { Settings } from "@opengeni/config";
32
33
  import type { Database } from "@opengeni/db";
33
34
  import {
@@ -42,7 +43,7 @@ import type { Observability } from "@opengeni/observability";
42
43
  // The un-agent-loop leaf. Constructing the Channel-A service over the un-proxied
43
44
  // setupBoxSession here (NOT the routing veneer) guarantees a mid-turn
44
45
  // sandbox_swap can never re-route capture execs onto a user's machine — the same
45
- // reason the snapshot uses setupBoxSession (dossier §7.3).
46
+ // reason the snapshot uses setupBoxSession.
46
47
  import {
47
48
  SandboxChannelAService,
48
49
  type ChannelASession,
@@ -60,7 +61,7 @@ import type {
60
61
  WorkspaceCaptureStats,
61
62
  } from "@opengeni/contracts";
62
63
 
63
- // ─── Guards & constants (dossier §10.1 — pathological-only; configurable) ─────
64
+ // ─── Guards & constants for pathological cases; configurable ─────
64
65
  export const CAPTURE_TIMEOUT_MS = 60_000;
65
66
  export const PER_FILE_CONTENT_GUARD_BYTES = 5 * 1024 * 1024; // after-image; over → tooLarge marker
66
67
  export const PER_FILE_DIFF_GUARD_BYTES = 10 * 1024 * 1024; // per-file diff; over → truncated marker
@@ -171,7 +172,7 @@ function classifyCaptureEntryError(error: unknown): BoxExitingError | null {
171
172
  return null;
172
173
  }
173
174
  const TREE_MAX_ENTRIES = 20_000; // whole-tree node cap (truncate beyond)
174
- const TREE_MAX_DIRS = 600; // BFS round-trip cap (bounds turn-end latency)
175
+ const TREE_MAX_DEPTH = 600; // pathological nesting cap; still one remote round-trip
175
176
 
176
177
  // The capture row and announcement are committed together by @opengeni/db. This
177
178
  // callback performs only best-effort live fanout of those already-durable events.
@@ -189,8 +190,15 @@ export type CaptureWorkspaceRevisionInput = {
189
190
  accountId: string;
190
191
  workspaceId: string;
191
192
  sessionId: string;
192
- turnId: string | null;
193
+ turnId: string;
194
+ attemptId: string;
193
195
  observability: Observability;
196
+ /**
197
+ * The owning turn activity's cancellation signal. Pause/Steer must preempt
198
+ * review-cache housekeeping immediately; correctness still comes from the
199
+ * activity plus the capture's transaction-level attempt/control/lease fences.
200
+ */
201
+ signal?: AbortSignal;
194
202
  /** Test-only: override keep-latest-N GC threshold (default 10). */
195
203
  keepLatest?: number;
196
204
  };
@@ -207,6 +215,13 @@ export async function captureWorkspaceRevision(
207
215
  input: CaptureWorkspaceRevisionInput,
208
216
  ): Promise<void> {
209
217
  const { observability } = input;
218
+ if (input.signal?.aborted) {
219
+ observability.incrementCounter({
220
+ name: "opengeni_workspace_capture_total",
221
+ labels: { result: "cancelled" },
222
+ });
223
+ return;
224
+ }
210
225
  if (!input.settings.workspaceCaptureEnabled) {
211
226
  return; // flag off → capture skipped; reads fall back to live/wake (status quo)
212
227
  }
@@ -224,6 +239,20 @@ export async function captureWorkspaceRevision(
224
239
 
225
240
  const startedAt = Date.now();
226
241
  const controller = new AbortController();
242
+ let rejectOwnerCancellation: ((reason?: unknown) => void) | undefined;
243
+ const ownerCancelled = input.signal
244
+ ? new Promise<never>((_resolve, reject) => {
245
+ rejectOwnerCancellation = reject;
246
+ })
247
+ : new Promise<never>(() => undefined);
248
+ const cancelFromOwner = (): void => {
249
+ controller.abort(input.signal?.reason);
250
+ rejectOwnerCancellation?.(
251
+ input.signal?.reason ?? new Error("workspace capture cancelled by its owning activity"),
252
+ );
253
+ };
254
+ input.signal?.addEventListener("abort", cancelFromOwner, { once: true });
255
+ if (input.signal?.aborted) cancelFromOwner();
227
256
  observability.incrementGauge({
228
257
  name: "opengeni_workspace_captures_inflight",
229
258
  help: "Current workspace-capture operations still resident in this worker process.",
@@ -244,6 +273,7 @@ export async function captureWorkspaceRevision(
244
273
  try {
245
274
  await Promise.race([
246
275
  capture,
276
+ ownerCancelled,
247
277
  new Promise<never>((_, reject) => {
248
278
  timer = setTimeout(() => {
249
279
  controller.abort();
@@ -252,6 +282,13 @@ export async function captureWorkspaceRevision(
252
282
  }),
253
283
  ]);
254
284
  } catch (error) {
285
+ if (input.signal?.aborted) {
286
+ observability.incrementCounter({
287
+ name: "opengeni_workspace_capture_total",
288
+ labels: { result: "cancelled" },
289
+ });
290
+ return;
291
+ }
255
292
  // The one and only place a capture failure surfaces — as a log line, never
256
293
  // an exception past this boundary (the turn already completed).
257
294
  observability.warn("workspace capture failed — turn outcome unaffected", {
@@ -266,6 +303,7 @@ export async function captureWorkspaceRevision(
266
303
  });
267
304
  } finally {
268
305
  if (timer) clearTimeout(timer);
306
+ input.signal?.removeEventListener("abort", cancelFromOwner);
269
307
  controller.abort();
270
308
  }
271
309
  }
@@ -294,6 +332,11 @@ async function runCapture(
294
332
  // ── 1. per-repo status + diff, union the touched set ──────────────────────
295
333
  const discovery = await svc.detectReposDetailed();
296
334
  throwIfCaptureAborted(signal);
335
+ if (process.env.OPENGENI_TEST_SCENARIO === "sandbox") {
336
+ console.log(
337
+ `[sandbox-e2e] capture discovery complete=${discovery.complete} repos=${JSON.stringify(discovery.repos)} degraded=${discovery.degradedReason ?? "none"}`,
338
+ );
339
+ }
297
340
  if (!discovery.complete) {
298
341
  const capturedAt = new Date();
299
342
  const reason = captureDegradedReason(discovery.degradedReason);
@@ -307,6 +350,7 @@ async function runCapture(
307
350
  workspaceId: input.workspaceId,
308
351
  sessionId: input.sessionId,
309
352
  turnId: input.turnId,
353
+ attemptId: input.attemptId,
310
354
  sandboxGroupId: input.sandboxGroupId,
311
355
  expectedEpoch: input.leaseEpoch,
312
356
  revision,
@@ -368,6 +412,7 @@ async function runCapture(
368
412
  diff = await svc.gitDiff({
369
413
  path: root,
370
414
  staged: false,
415
+ includeUntracked: true,
371
416
  fromRef: "HEAD",
372
417
  pathspec: [],
373
418
  contextLines: 3,
@@ -425,8 +470,8 @@ async function runCapture(
425
470
  // dirty tree on every read-only turn. Instead we skip when the change surface
426
471
  // is byte-identical to the previous revision — "no new revision when nothing
427
472
  // changed" holds even with a persistently dirty tree, and content-addressed
428
- // blobs already dedupe storage. (Deviation from the dossier's literal "clean"
429
- // wording — same intent, strictly better; recorded in PROGRESS.)
473
+ // blobs already dedupe storage. This preserves the intended empty-turn behavior
474
+ // while correctly handling a persistently dirty tree.
430
475
  // ── 3. after-images of touched files (size-gated), content-addressed ───────
431
476
  const files: WorkspaceCaptureFile[] = [];
432
477
  const blobKeys = new Set<string>();
@@ -525,7 +570,7 @@ async function runCapture(
525
570
  });
526
571
  }
527
572
 
528
- // ── 4. empty-turn gate (dossier §10.1 / B3) ───────────────────────────────
573
+ // ── 4. empty-turn gate (B3) ───────────────────────────────
529
574
  // Skip when the change surface is byte-identical to the previous revision.
530
575
  // Fires BEFORE the tree BFS + storage PUTs (the expensive parts) so a no-op
531
576
  // read-only turn on a persistently dirty tree costs only the (small) status/
@@ -539,14 +584,14 @@ async function runCapture(
539
584
  return;
540
585
  }
541
586
 
542
- // ── 5. tree index (bounded BFS; residue dirs collapsed, not descended) ─────
587
+ // ── 5. tree index (one bounded listing; residue dirs pruned at source) ─────
543
588
  const tree = await buildTreeIndex(svc, startedAt, signal);
544
589
  throwIfCaptureAborted(signal);
545
590
 
546
591
  // ── 6. PUT blobs + tree + manifest (F9: all writes BEFORE any delete) ──────
547
592
  // Key manifest/tree by the turn (one capture per turn) so the key is known
548
593
  // before the revision is committed; content blobs are content-addressed.
549
- const turnKey = input.turnId ?? randomUUID();
594
+ const turnKey = input.turnId;
550
595
  const treeKey = `workspace-captures/${input.workspaceId}/${input.sessionId}/trees/${turnKey}.json`;
551
596
  const manifestKey = `workspace-captures/${input.workspaceId}/${input.sessionId}/manifests/${turnKey}.json`;
552
597
 
@@ -708,6 +753,7 @@ async function runCapture(
708
753
  workspaceId: input.workspaceId,
709
754
  sessionId: input.sessionId,
710
755
  turnId: input.turnId,
756
+ attemptId: input.attemptId,
711
757
  sandboxGroupId: input.sandboxGroupId,
712
758
  expectedEpoch: input.leaseEpoch,
713
759
  revision,
@@ -718,6 +764,11 @@ async function runCapture(
718
764
  stats,
719
765
  capturedAt,
720
766
  });
767
+ if (process.env.OPENGENI_TEST_SCENARIO === "sandbox") {
768
+ console.log(
769
+ `[sandbox-e2e] capture commit inserted=${Boolean(inserted)} revision=${revision} epoch=${input.leaseEpoch} group=${input.sandboxGroupId}`,
770
+ );
771
+ }
721
772
  throwIfCaptureAborted(signal);
722
773
  if (!inserted) {
723
774
  // Lease superseded/released between capture and commit. Best-effort clean up
@@ -871,94 +922,63 @@ async function safeDelete(
871
922
  }
872
923
 
873
924
  /**
874
- * Build the workspace tree index via bounded BFS over the PUBLIC fsList (depth 1
875
- * per directory). Residue dirs are emitted as collapsed nodes but never queued
876
- * for descent — a single deep fsList would let node_modules exhaust the entry
877
- * budget and truncate real files, so we prune at the source. Bounded by
878
- * TREE_MAX_ENTRIES, TREE_MAX_DIRS round-trips, and the outer capture deadline.
925
+ * Build the workspace tree index in ONE remote filesystem round-trip. Channel-A
926
+ * prunes residue directories inside `find` while still emitting their collapsed
927
+ * node, so node_modules cannot consume the authored-file budget. The former
928
+ * serial per-directory BFS took 55.8 seconds for a 4,522-entry production tree
929
+ * and blocked Steer cancellation. This path is bounded by entry count, nesting
930
+ * depth, the outer capture deadline, and the owning activity's cancellation.
879
931
  */
880
932
  async function buildTreeIndex(
881
933
  svc: SandboxChannelAService,
882
934
  startedAt: number,
883
935
  signal: AbortSignal,
884
936
  ): Promise<{ root: FsTreeNode; entryCount: number; truncated: boolean }> {
885
- const root: FsTreeNode = {
886
- name: "",
887
- path: "",
888
- type: "dir",
889
- sizeBytes: null,
890
- mtimeMs: null,
891
- mode: null,
892
- children: [],
893
- truncated: false,
894
- };
895
- const byPath = new Map<string, FsTreeNode>([["", root]]);
896
- const queue: string[] = [""];
897
- let entryCount = 0;
898
- let dirCalls = 0;
899
- let truncated = false;
900
-
901
- while (queue.length > 0) {
937
+ throwIfCaptureAborted(signal);
938
+ if (Date.now() - startedAt > CAPTURE_TIMEOUT_MS - 5_000) {
939
+ throw new Error("workspace capture tree index reached its deadline before listing");
940
+ }
941
+ let listing: Awaited<ReturnType<typeof svc.fsListPruned>>;
942
+ try {
943
+ listing = await svc.fsListPruned(
944
+ {
945
+ path: "",
946
+ depth: TREE_MAX_DEPTH,
947
+ maxEntries: TREE_MAX_ENTRIES,
948
+ includeHidden: true,
949
+ },
950
+ RESIDUE_DIRS,
951
+ );
902
952
  throwIfCaptureAborted(signal);
903
- if (dirCalls >= TREE_MAX_DIRS || entryCount >= TREE_MAX_ENTRIES) {
904
- truncated = true;
905
- break;
953
+ } catch (error) {
954
+ if (isBoxExitingError(error)) {
955
+ throw new BoxExitingError(error instanceof Error ? error.message : String(error));
906
956
  }
907
- if (Date.now() - startedAt > CAPTURE_TIMEOUT_MS - 5_000) {
908
- truncated = true;
909
- break;
910
- } // leave headroom for PUTs
911
- const dir = queue.shift()!;
912
- dirCalls += 1;
913
- // Per-dir resilience: a directory can vanish or fail to list mid-walk (fs
914
- // churn on a desktop box, a permission edge). Skip the one directory and keep
915
- // walking the rest — a single unreadable dir must never abort the tree index.
916
- // A box death still aborts the whole capture (BoxExitingError).
917
- let listing: Awaited<ReturnType<typeof svc.fsList>>;
918
- try {
919
- listing = await svc.fsList({ path: dir, depth: 1, maxEntries: 2_000, includeHidden: true });
920
- throwIfCaptureAborted(signal);
921
- } catch (error) {
922
- if (isBoxExitingError(error)) {
923
- throw new BoxExitingError(error instanceof Error ? error.message : String(error));
924
- }
925
- truncated = true; // this subtree is incomplete; continue with the rest
957
+ throw error;
958
+ }
959
+
960
+ let entryCount = 0;
961
+ let depthTruncated = false;
962
+ const stack = [...(listing.root.children ?? [])];
963
+ while (stack.length > 0) {
964
+ const node = stack.pop()!;
965
+ entryCount += 1;
966
+ if (node.type !== "dir") continue;
967
+ if (RESIDUE_DIR_SET.has(node.name)) {
968
+ node.truncated = true;
969
+ node.children = [];
926
970
  continue;
927
971
  }
928
- if (listing.truncated) truncated = true;
929
- const parent = byPath.get(dir) ?? root;
930
- const children = collectImmediateChildren(listing.root);
931
- for (const child of children) {
932
- if (entryCount >= TREE_MAX_ENTRIES) {
933
- truncated = true;
934
- break;
935
- }
936
- const node: FsTreeNode = {
937
- name: child.name,
938
- path: child.path,
939
- type: child.type,
940
- sizeBytes: child.sizeBytes,
941
- mtimeMs: child.mtimeMs,
942
- mode: child.mode,
943
- truncated: false,
944
- ...(child.type === "dir" ? { children: [] as FsTreeNode[] } : {}),
945
- };
946
- (parent.children ??= []).push(node);
947
- byPath.set(node.path, node);
948
- entryCount += 1;
949
- if (node.type === "dir") {
950
- if (RESIDUE_DIR_SET.has(node.name)) {
951
- node.truncated = true; // collapsed residue dir — contents on the machine
952
- } else {
953
- queue.push(node.path);
954
- }
955
- }
972
+ if (node.path.split("/").length >= TREE_MAX_DEPTH) {
973
+ node.truncated = true;
974
+ depthTruncated = true;
975
+ continue;
956
976
  }
977
+ stack.push(...(node.children ?? []));
957
978
  }
958
- return { root, entryCount, truncated };
959
- }
960
-
961
- /** fsList returns a subtree; at depth 1 the immediate children hang off root. */
962
- function collectImmediateChildren(node: FsTreeNode): FsTreeNode[] {
963
- return node.children ?? [];
979
+ return {
980
+ root: listing.root,
981
+ entryCount,
982
+ truncated: listing.truncated || depthTruncated,
983
+ };
964
984
  }
package/src/activities.ts CHANGED
@@ -25,6 +25,8 @@ export type {
25
25
  ActivityDependencies,
26
26
  DispatchScheduledTaskRunInput,
27
27
  DispatchScheduledTaskRunResult,
28
+ ExpireSessionHumanInputInput,
29
+ ExpireSessionHumanInputResult,
28
30
  IndexDocumentInput,
29
31
  MaybeContinueGoalInput,
30
32
  MaybeContinueGoalResult,
@@ -34,8 +36,10 @@ export type {
34
36
  ReconcileCodexCapacityWaitResult,
35
37
  RecoverDispatchInput,
36
38
  RecoverDispatchResult,
39
+ PersistSessionAttemptQuiescenceInput,
37
40
  RunAgentTurnInput,
38
41
  RunAgentTurnResult,
42
+ SessionAttemptQuiescenceProof,
39
43
  } from "./activities/types";
40
44
 
41
45
  function createActivityServices(
@@ -73,7 +77,7 @@ function createActivityServices(
73
77
  // broker binding (the same `createNatsEventBus(natsUrl)`) here AND on
74
78
  // the mounted API, so the two SEPARATE processes share one broker and
75
79
  // derive the IDENTICAL `sessionSubject` — the only way live fanout
76
- // (worker emit → API SSE) works cross-process (SPIKE-1 F5/F6, proven).
80
+ // (worker emit → API SSE) works cross-process (schema-isolation contract F5/F6, proven).
77
81
  // NEVER default to an in-memory bus: it fans out intra-process only and
78
82
  // would silently break live SSE. unset → today's NATS default,
79
83
  // byte-for-byte. The bus is live-fanout ONLY — the durable Postgres
@@ -97,13 +101,14 @@ function createActivityServices(
97
101
  documentServices: dependencies.documentServices ?? createDocumentServices(settings),
98
102
  observability,
99
103
  wakeSessionWorkflow: dependencies.wakeSessionWorkflow ?? null,
104
+ signalSessionAttemptQuiesced: dependencies.signalSessionAttemptQuiesced ?? null,
100
105
  signalCodexCapacityWorkflow: dependencies.signalCodexCapacityWorkflow ?? null,
101
106
  // §7.5 P3 — host-entitlements port. No constructed default: standalone
102
107
  // has no host meter, so unset → null → `ensureRunAllowed` reads the
103
108
  // local ledger exactly as today (mirrors `wakeSessionWorkflow`'s
104
109
  // null-degrades-gracefully shape, not a `createX(settings)` default).
105
110
  entitlements: dependencies.entitlements ?? null,
106
- // §7.6 P4a — host connection-credential provider. No constructed
111
+ // §7.6 connection-credential provider — host connection-credential provider. No constructed
107
112
  // default: standalone owns its own GitHub App + encryption key, so unset
108
113
  // → null → the per-run credential mint self-mints from `settings`
109
114
  // (createGitHubAppInstallationToken + environmentsEncryptionKeyBytes)
@@ -154,8 +159,11 @@ export const runAgentTurn = defaultTurnActivities.runAgentTurn;
154
159
  export const indexDocument = defaultControlActivities.indexDocument;
155
160
  export const failSessionAttempt = defaultControlActivities.failSessionAttempt;
156
161
  export const settleSessionInterruptions = defaultControlActivities.settleSessionInterruptions;
162
+ export const persistSessionAttemptQuiescence =
163
+ defaultControlActivities.persistSessionAttemptQuiescence;
157
164
  export const recoverDispatch = defaultControlActivities.recoverDispatch;
158
165
  export const peekSessionWork = defaultControlActivities.peekSessionWork;
166
+ export const expireSessionHumanInput = defaultControlActivities.expireSessionHumanInput;
159
167
  export const markSessionIdle = defaultControlActivities.markSessionIdle;
160
168
  export const dispatchScheduledTaskRun = defaultControlActivities.dispatchScheduledTaskRun;
161
169
  export const enqueueGoalRetryWake = defaultControlActivities.enqueueGoalRetryWake;