@bastani/atomic 0.9.88-alpha.1 → 0.9.88

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 (55) hide show
  1. package/CHANGELOG.md +287 -0
  2. package/dist/builtin/intercom/CHANGELOG.md +57 -0
  3. package/dist/builtin/intercom/index.bundle.mjs +62 -38
  4. package/dist/builtin/intercom/package.json +1 -1
  5. package/dist/builtin/intercom/session-target.ts +15 -3
  6. package/dist/builtin/mcp/package.json +1 -1
  7. package/dist/builtin/subagents/CHANGELOG.md +50 -0
  8. package/dist/builtin/subagents/package.json +1 -1
  9. package/dist/builtin/web-access/CHANGELOG.md +10 -0
  10. package/dist/builtin/web-access/package.json +1 -1
  11. package/dist/builtin/workflows/CHANGELOG.md +117 -0
  12. package/dist/builtin/workflows/README.md +2 -2
  13. package/dist/builtin/workflows/builtin/{chunk-r9xfx5d0.js → chunk-4h4nh4f1.js} +1 -1
  14. package/dist/builtin/workflows/builtin/{chunk-cryf7s3r.js → chunk-jm7redy5.js} +648 -91
  15. package/dist/builtin/workflows/builtin/{chunk-c7bc4ppx.js → chunk-t661k7hd.js} +1 -1
  16. package/dist/builtin/workflows/builtin/goal.js +2 -2
  17. package/dist/builtin/workflows/builtin/index.js +3 -3
  18. package/dist/builtin/workflows/builtin/ralph.js +2 -2
  19. package/dist/builtin/workflows/package.json +1 -1
  20. package/dist/builtin/workflows/src/durable/fork-errors.d.ts +0 -1
  21. package/dist/builtin/workflows/src/durable/journal/internal.d.ts +1 -0
  22. package/dist/builtin/workflows/src/extension/index.bundle.mjs +2515 -1091
  23. package/dist/builtin/workflows/src/extension/local-worker-entry.mjs +1092 -98
  24. package/dist/builtin/workflows/src/index.js +1077 -91
  25. package/dist/builtin/workflows/src/local-worker-entry.mjs +1092 -98
  26. package/dist/core/storage/session/session-resume.d.ts +8 -1
  27. package/dist/core/storage/session/session-resume.d.ts.map +1 -1
  28. package/dist/core/storage/session/session-resume.js +22 -3
  29. package/dist/core/storage/session/session-resume.js.map +1 -1
  30. package/dist/core/worktree-snapshot/restore.d.ts +2 -0
  31. package/dist/core/worktree-snapshot/restore.d.ts.map +1 -1
  32. package/dist/core/worktree-snapshot/restore.js +41 -5
  33. package/dist/core/worktree-snapshot/restore.js.map +1 -1
  34. package/dist/modes/interactive/components/jump-to-latest.d.ts +0 -5
  35. package/dist/modes/interactive/components/jump-to-latest.d.ts.map +1 -1
  36. package/dist/modes/interactive/components/jump-to-latest.js +0 -5
  37. package/dist/modes/interactive/components/jump-to-latest.js.map +1 -1
  38. package/dist/modes/interactive/interactive-mode-base.d.ts +1 -0
  39. package/dist/modes/interactive/interactive-mode-base.d.ts.map +1 -1
  40. package/dist/modes/interactive/interactive-mode-base.js +13 -2
  41. package/dist/modes/interactive/interactive-mode-base.js.map +1 -1
  42. package/dist/modes/interactive/interactive-render-chat.d.ts +0 -4
  43. package/dist/modes/interactive/interactive-render-chat.d.ts.map +1 -1
  44. package/dist/modes/interactive/interactive-render-chat.js +19 -45
  45. package/dist/modes/interactive/interactive-render-chat.js.map +1 -1
  46. package/dist/modes/interactive/interactive-startup-chat-container.d.ts +9 -1
  47. package/dist/modes/interactive/interactive-startup-chat-container.d.ts.map +1 -1
  48. package/dist/modes/interactive/interactive-startup-chat-container.js +38 -0
  49. package/dist/modes/interactive/interactive-startup-chat-container.js.map +1 -1
  50. package/docs/keybindings.md +1 -1
  51. package/docs/storage.md +1 -1
  52. package/docs/workflows/api-reference.md +3 -2
  53. package/docs/workflows/operations.md +11 -9
  54. package/npm-shrinkwrap.json +49 -49
  55. package/package.json +6 -6
@@ -754,66 +754,6 @@ function getDurableBackendProcessOwner() {
754
754
  return created;
755
755
  }
756
756
 
757
- // dist/builtin/workflows/src/shared/session-scoped-singleton.ts
758
- import { sessionScopedExtensionState } from "@bastani/atomic";
759
- var SHARED_STATES_KEY = Symbol.for("atomic-workflows/session-scoped-singleton-pre-adoption@1");
760
- function processBag() {
761
- return globalThis;
762
- }
763
- function recordLocal(key, local) {
764
- const bag = processBag();
765
- let states = bag[SHARED_STATES_KEY];
766
- if (states === undefined || states.version !== 1) {
767
- states = { version: 1, byKey: new Map };
768
- bag[SHARED_STATES_KEY] = states;
769
- }
770
- const existing = states.byKey.get(key);
771
- if (existing !== undefined)
772
- return existing;
773
- const created = { local, instance: undefined, localClaimed: false };
774
- states.byKey.set(key, created);
775
- return created;
776
- }
777
- function createSessionScopedSingleton(key, createLocal) {
778
- const local = createLocal();
779
- const shared = recordLocal(key, local);
780
- let instance = local;
781
- const live = () => shared.instance ?? instance;
782
- const current = () => live();
783
- const adoptWithResult = (scope, options) => {
784
- const current = live();
785
- let targetCreated = false;
786
- const target = sessionScopedExtensionState(scope, key, () => {
787
- targetCreated = true;
788
- if (shared.localClaimed)
789
- return (options?.create ?? createLocal)();
790
- shared.localClaimed = true;
791
- return shared.local;
792
- });
793
- const preservedCurrent = !targetCreated && target !== current && options?.preserveCurrentWhenTargetExists?.(current, target) === true;
794
- shared.instance = preservedCurrent ? current : target;
795
- instance = shared.instance;
796
- return { instance: shared.instance, preservedCurrent };
797
- };
798
- const adopt = (scope, options) => adoptWithResult(scope, options).instance;
799
- const facade = new Proxy(local, {
800
- get(_target, prop) {
801
- const value = Reflect.get(live(), prop, live());
802
- if (typeof value === "function") {
803
- return value.bind(live());
804
- }
805
- return value;
806
- },
807
- set(_target, prop, value) {
808
- return Reflect.set(live(), prop, value, live());
809
- },
810
- has(_target, prop) {
811
- return Reflect.has(live(), prop);
812
- }
813
- });
814
- return { facade, adopt, adoptWithResult, current };
815
- }
816
-
817
757
  // dist/builtin/workflows/src/shared/graph-store-snapshot.ts
818
758
  import { sliceHead as sliceHead2 } from "@bastani/atomic";
819
759
 
@@ -822,6 +762,96 @@ function flattenTruncatedString(value) {
822
762
  return value.split("").join("");
823
763
  }
824
764
 
765
+ // dist/builtin/workflows/src/shared/inspection-payload.ts
766
+ function boxedKind(value) {
767
+ try {
768
+ const prototype = Object.getPrototypeOf(value);
769
+ if (prototype === Object.prototype || prototype === null || Array.isArray(value))
770
+ return;
771
+ } catch {
772
+ return;
773
+ }
774
+ try {
775
+ Number.prototype.valueOf.call(value);
776
+ return "number";
777
+ } catch {}
778
+ try {
779
+ String.prototype.valueOf.call(value);
780
+ return "string";
781
+ } catch {}
782
+ try {
783
+ Boolean.prototype.valueOf.call(value);
784
+ return "boolean";
785
+ } catch {}
786
+ try {
787
+ BigInt.prototype.valueOf.call(value);
788
+ return "bigint";
789
+ } catch {}
790
+ return;
791
+ }
792
+ function cloneValue(source, key, ancestors) {
793
+ let value = source;
794
+ if (value !== null && (typeof value === "object" || typeof value === "function") || typeof value === "bigint") {
795
+ const convert = value.toJSON;
796
+ if (typeof convert === "function")
797
+ value = convert.call(value, key);
798
+ }
799
+ if (value !== null && typeof value === "object") {
800
+ const kind = boxedKind(value);
801
+ if (kind === "number")
802
+ value = Number(value);
803
+ else if (kind === "string")
804
+ value = String(value);
805
+ else if (kind === "boolean")
806
+ value = Boolean.prototype.valueOf.call(value);
807
+ else if (kind === "bigint")
808
+ throw new TypeError("BigInt cannot be serialized");
809
+ }
810
+ if (value === null)
811
+ return null;
812
+ const json = JSON;
813
+ if (typeof value === "object" && json.isRawJSON?.(value) === true) {
814
+ return JSON.parse(value.rawJSON);
815
+ }
816
+ switch (typeof value) {
817
+ case "string":
818
+ case "boolean":
819
+ return value;
820
+ case "number":
821
+ return Number.isFinite(value) ? Object.is(value, -0) ? 0 : value : null;
822
+ case "bigint":
823
+ throw new TypeError("BigInt cannot be serialized");
824
+ case "undefined":
825
+ case "function":
826
+ case "symbol":
827
+ return;
828
+ }
829
+ if (ancestors.has(value))
830
+ throw new TypeError("Circular snapshot value cannot be serialized");
831
+ ancestors.add(value);
832
+ try {
833
+ if (Array.isArray(value)) {
834
+ return Array.from({ length: value.length }, (_, index) => cloneValue(value[index], String(index), ancestors) ?? null);
835
+ }
836
+ const result = {};
837
+ for (const name of Object.keys(value)) {
838
+ const item = cloneValue(value[name], name, ancestors);
839
+ if (item === undefined)
840
+ continue;
841
+ if (name === "__proto__")
842
+ Object.defineProperty(result, name, { value: item, enumerable: true, configurable: true, writable: true });
843
+ else
844
+ result[name] = item;
845
+ }
846
+ return result;
847
+ } finally {
848
+ ancestors.delete(value);
849
+ }
850
+ }
851
+ function cloneInspectionPayload(value) {
852
+ return cloneValue(value, "", new Set);
853
+ }
854
+
825
855
  // dist/builtin/workflows/src/shared/run-stage-index.ts
826
856
  var indexes = new WeakMap;
827
857
  function asksForInput(stage) {
@@ -993,7 +1023,7 @@ function cloneObjectLike(state, value) {
993
1023
  items.push(null);
994
1024
  continue;
995
1025
  }
996
- items.push(cloneValue(state, item));
1026
+ items.push(cloneValue2(state, item));
997
1027
  }
998
1028
  return items;
999
1029
  }
@@ -1004,7 +1034,7 @@ function cloneObjectLike(state, value) {
1004
1034
  const child = safeProperty(value, key);
1005
1035
  if (isOmittedJsonValue(child))
1006
1036
  continue;
1007
- cloned[key] = cloneValue(state, child);
1037
+ cloned[key] = cloneValue2(state, child);
1008
1038
  }
1009
1039
  if (scan.stoppedByLimit())
1010
1040
  addTruncationMarker(cloned);
@@ -1014,7 +1044,7 @@ function cloneObjectLike(state, value) {
1014
1044
  state.seen.delete(value);
1015
1045
  }
1016
1046
  }
1017
- function cloneValue(state, value) {
1047
+ function cloneValue2(state, value) {
1018
1048
  const resolved = resolveJsonValue(value);
1019
1049
  if (resolved === null) {
1020
1050
  state.remaining -= NULL_COST;
@@ -1040,7 +1070,7 @@ function cloneValue(state, value) {
1040
1070
  }
1041
1071
  }
1042
1072
  function boundedToolPayload(value, limit = TOOL_PAYLOAD_VALUE_LIMIT) {
1043
- return cloneValue({ remaining: Math.max(1, limit), depth: 0, keysRemaining: TOOL_PAYLOAD_MAX_KEYS, seen: new Set }, value);
1073
+ return cloneValue2({ remaining: Math.max(1, limit), depth: 0, keysRemaining: TOOL_PAYLOAD_MAX_KEYS, seen: new Set }, value);
1044
1074
  }
1045
1075
  function boundedToolPayloadRecord(value, limit = TOOL_PAYLOAD_VALUE_LIMIT) {
1046
1076
  const record = {};
@@ -1058,7 +1088,7 @@ function boundedToolPayloadRecord(value, limit = TOOL_PAYLOAD_VALUE_LIMIT) {
1058
1088
  const child = safeProperty(value, key);
1059
1089
  if (isOmittedJsonValue(child))
1060
1090
  continue;
1061
- record[key] = cloneValue(state, child);
1091
+ record[key] = cloneValue2(state, child);
1062
1092
  }
1063
1093
  if (scan.stoppedByLimit())
1064
1094
  addTruncationMarker(record);
@@ -1196,7 +1226,11 @@ function compactToolNode(node) {
1196
1226
  ...node.source !== undefined ? { source: boundedToolText(node.source) } : {}
1197
1227
  };
1198
1228
  }
1199
- function compactRun(run, stages = run.stages.map(compactStage), toolNodes = run.toolNodes.map(compactToolNode)) {
1229
+ function compactRun(run, stages = run.stages.map(compactStage), toolNodes = run.toolNodes.map(compactToolNode), full = false) {
1230
+ if (full) {
1231
+ const copy = cloneInspectionPayload({ ...run, stages: [], toolNodes: [] });
1232
+ return { ...copy, stages, toolNodes };
1233
+ }
1200
1234
  const {
1201
1235
  inputs: _inputs,
1202
1236
  result: sourceResult,
@@ -1231,8 +1265,8 @@ function sameSourceShape(value, shape) {
1231
1265
  function isSettledStage(stage) {
1232
1266
  return stage.status === "completed" || stage.status === "failed" || stage.status === "skipped";
1233
1267
  }
1234
- function projectStage(stage) {
1235
- const compact = compactStage(stage);
1268
+ function projectStage(stage, full = false) {
1269
+ const compact = full ? cloneInspectionPayload(stage) : compactStage(stage);
1236
1270
  deepFreezeGraphValue(compact);
1237
1271
  return {
1238
1272
  shape: sourceShape(stage),
@@ -1247,15 +1281,19 @@ function projectStage(stage) {
1247
1281
  function stageUnchanged(stage, projected) {
1248
1282
  return sameSourceShape(stage, projected.shape) && stage.parentIds.length === projected.parentCount && stage.toolEvents.length === projected.toolEventCount && stage.toolEvents.at(-1) === projected.lastToolEvent && stage.notices?.length === projected.noticeCount;
1249
1283
  }
1250
- function projectToolNode(node) {
1251
- const compact = compactToolNode(node);
1284
+ function projectToolNode(node, full = false) {
1285
+ const compact = full ? cloneInspectionPayload(node) : compactToolNode(node);
1252
1286
  deepFreezeGraphValue(compact);
1253
1287
  return { shape: sourceShape(node), parentCount: node.parentIds.length, compact };
1254
1288
  }
1255
1289
  function toolNodeUnchanged(node, projected) {
1256
1290
  return sameSourceShape(node, projected.shape) && node.parentIds.length === projected.parentCount;
1257
1291
  }
1258
- function freezeCompactRun(run) {
1292
+ function freezeCompactRun(run, full = false) {
1293
+ if (full) {
1294
+ Object.freeze(run.stages);
1295
+ Object.freeze(run.toolNodes);
1296
+ }
1259
1297
  for (const [key, value] of Object.entries(run)) {
1260
1298
  if (key === "stages" || key === "toolNodes")
1261
1299
  markPublishedSnapshotArray(value);
@@ -1265,17 +1303,23 @@ function freezeCompactRun(run) {
1265
1303
  Object.freeze(run);
1266
1304
  }
1267
1305
  var snapshotLineages = new WeakMap;
1306
+ function graphSnapshotLineage(snapshot) {
1307
+ return snapshotLineages.get(snapshot);
1308
+ }
1309
+
1268
1310
  class GraphStoreSnapshotBuilder {
1311
+ full;
1312
+ constructor(options = {}) {
1313
+ this.full = options.fullPayloads ?? false;
1314
+ }
1269
1315
  projected = new Map;
1316
+ recheckSettledStages = false;
1270
1317
  changedRuns = new Set;
1271
1318
  changedStages = new Map;
1272
1319
  changedToolNodes = new Map;
1273
1320
  markChanged(change) {
1274
1321
  if (change === undefined) {
1275
- this.projected.clear();
1276
- this.changedRuns.clear();
1277
- this.changedStages.clear();
1278
- this.changedToolNodes.clear();
1322
+ this.recheckSettledStages = true;
1279
1323
  return;
1280
1324
  }
1281
1325
  if (change.stageId === undefined && change.toolNodeId === undefined) {
@@ -1287,6 +1331,22 @@ class GraphStoreSnapshotBuilder {
1287
1331
  if (change.toolNodeId !== undefined)
1288
1332
  addScoped(this.changedToolNodes, change.runId, change.toolNodeId);
1289
1333
  }
1334
+ hasRun(run) {
1335
+ return this.projected.get(run.id)?.source === run;
1336
+ }
1337
+ removeRun(runId) {
1338
+ this.projected.delete(runId);
1339
+ this.changedRuns.delete(runId);
1340
+ this.changedStages.delete(runId);
1341
+ this.changedToolNodes.delete(runId);
1342
+ }
1343
+ clear() {
1344
+ this.projected.clear();
1345
+ this.recheckSettledStages = false;
1346
+ this.changedRuns.clear();
1347
+ this.changedStages.clear();
1348
+ this.changedToolNodes.clear();
1349
+ }
1290
1350
  build(runs, notices, version) {
1291
1351
  const previous = this.projected;
1292
1352
  const next = new Map;
@@ -1297,16 +1357,16 @@ class GraphStoreSnapshotBuilder {
1297
1357
  compactRuns.push(projected.compact);
1298
1358
  }
1299
1359
  this.projected = next;
1360
+ this.recheckSettledStages = false;
1300
1361
  this.changedRuns.clear();
1301
1362
  this.changedStages.clear();
1302
1363
  this.changedToolNodes.clear();
1303
1364
  const compactNotices = notices.map((notice) => ({ ...notice }));
1304
1365
  deepFreezeGraphValue(compactNotices);
1305
- const snapshot = Object.freeze({
1306
- runs: Object.freeze(compactRuns),
1307
- notices: compactNotices,
1308
- version
1309
- });
1366
+ return this.publish(compactRuns, compactNotices, version);
1367
+ }
1368
+ publish(runs, notices, version) {
1369
+ const snapshot = Object.freeze({ runs: Object.freeze(runs), notices, version });
1310
1370
  snapshotLineages.set(snapshot, this);
1311
1371
  return snapshot;
1312
1372
  }
@@ -1357,7 +1417,8 @@ class GraphStoreSnapshotBuilder {
1357
1417
  changed.add(position);
1358
1418
  }
1359
1419
  }
1360
- for (const position of projected.unsettled)
1420
+ const positions = this.recheckSettledStages ? sources.keys() : projected.unsettled;
1421
+ for (const position of positions)
1361
1422
  if (!changed.has(position) && !stageUnchanged(sources[position], projected.stages[position]))
1362
1423
  changed.add(position);
1363
1424
  if (changed.size === 0 && run.stages.length === sources.length)
@@ -1366,7 +1427,7 @@ class GraphStoreSnapshotBuilder {
1366
1427
  const appendedFrom = sources.length;
1367
1428
  const compactStages = previous.stages.slice();
1368
1429
  for (const position of changed) {
1369
- const stage = projectStage(sources[position]);
1430
+ const stage = projectStage(sources[position], this.full);
1370
1431
  projected.stages[position] = stage;
1371
1432
  compactStages[position] = stage.compact;
1372
1433
  if (stage.settled)
@@ -1376,7 +1437,7 @@ class GraphStoreSnapshotBuilder {
1376
1437
  }
1377
1438
  for (let position = sources.length;position < run.stages.length; position++) {
1378
1439
  const source = run.stages[position];
1379
- const stage = projectStage(source);
1440
+ const stage = projectStage(source, this.full);
1380
1441
  sources.push(source);
1381
1442
  projected.stages.push(stage);
1382
1443
  compactStages.push(stage.compact);
@@ -1388,7 +1449,7 @@ class GraphStoreSnapshotBuilder {
1388
1449
  projected.stagePositions.set(source.id, position);
1389
1450
  }
1390
1451
  const compact = { ...previous, stages: compactStages };
1391
- freezeCompactRun(compact);
1452
+ freezeCompactRun(compact, this.full);
1392
1453
  indexRunFrom(compact, previous, changed, appendedFrom);
1393
1454
  projected.compact = compact;
1394
1455
  return projected;
@@ -1408,7 +1469,7 @@ class GraphStoreSnapshotBuilder {
1408
1469
  for (const stage of run.stages) {
1409
1470
  let projected = previousStages.get(stage);
1410
1471
  if (projected === undefined || changedStages?.has(stage.id) === true || !stageUnchanged(stage, projected))
1411
- projected = projectStage(stage);
1472
+ projected = projectStage(stage, this.full);
1412
1473
  previousStages.set(stage, projected);
1413
1474
  const position = stages.length;
1414
1475
  stageSources.push(stage);
@@ -1427,12 +1488,12 @@ class GraphStoreSnapshotBuilder {
1427
1488
  for (const node of run.toolNodes) {
1428
1489
  let projected = toolNodes.get(node) ?? previous?.toolNodes.get(node);
1429
1490
  if (projected === undefined || changedToolNodes?.has(node.id) === true || !toolNodeUnchanged(node, projected))
1430
- projected = projectToolNode(node);
1491
+ projected = projectToolNode(node, this.full);
1431
1492
  toolNodes.set(node, projected);
1432
1493
  compactToolNodes.push(projected.compact);
1433
1494
  }
1434
- const compact = compactRun(run, compactStages, compactToolNodes);
1435
- freezeCompactRun(compact);
1495
+ const compact = compactRun(run, compactStages, compactToolNodes, this.full);
1496
+ freezeCompactRun(compact, this.full);
1436
1497
  return {
1437
1498
  source: run,
1438
1499
  shape: sourceShape(run),
@@ -1467,15 +1528,436 @@ function deepFreezeGraphValue(value) {
1467
1528
  }
1468
1529
  }
1469
1530
 
1470
- // dist/builtin/workflows/src/shared/workflow-observation-runtime.ts
1471
- import { randomUUID } from "node:crypto";
1472
-
1473
1531
  // dist/builtin/workflows/src/shared/expanded-workflow-graph.ts
1532
+ class MissingWorkflowToolNodesError extends Error {
1533
+ runId;
1534
+ constructor(runId) {
1535
+ super(`atomic-workflows: run ${runId} has no tool node collection`);
1536
+ this.name = "MissingWorkflowToolNodesError";
1537
+ this.runId = runId;
1538
+ }
1539
+ }
1540
+ function toolNodesFor(run) {
1541
+ if (run.toolNodes === undefined)
1542
+ throw new MissingWorkflowToolNodesError(run.id);
1543
+ return run.toolNodes;
1544
+ }
1545
+ function virtualNodeId(runId, nodeId, isRootRun) {
1546
+ return isRootRun ? nodeId : `${runId}:${nodeId}`;
1547
+ }
1548
+ function localItems(run) {
1549
+ const stages = run.stages.map((stage, sourceIndex) => ({ kind: "stage", stage, sourceIndex }));
1550
+ const tools = toolNodesFor(run).map((tool, sourceIndex) => ({ kind: "tool", tool, sourceIndex }));
1551
+ return [...stages, ...tools].sort((left, right) => {
1552
+ const leftOrder = left.kind === "stage" ? left.stage.executionOrder : left.tool.executionOrder;
1553
+ const rightOrder = right.kind === "stage" ? right.stage.executionOrder : right.tool.executionOrder;
1554
+ if (leftOrder !== undefined || rightOrder !== undefined)
1555
+ return (leftOrder ?? Number.MAX_SAFE_INTEGER) - (rightOrder ?? Number.MAX_SAFE_INTEGER);
1556
+ if (left.kind !== right.kind)
1557
+ return left.kind === "stage" ? -1 : 1;
1558
+ return left.sourceIndex - right.sourceIndex;
1559
+ });
1560
+ }
1561
+ function itemId(item) {
1562
+ return item.kind === "stage" ? item.stage.id : item.tool.id;
1563
+ }
1564
+ function itemParents(item) {
1565
+ return item.kind === "stage" ? item.stage.parentIds : item.tool.parentIds;
1566
+ }
1567
+ function localTerminalIds(items) {
1568
+ const parents = new Set(items.flatMap((item) => [...itemParents(item)]));
1569
+ const terminals = items.map(itemId).filter((id) => !parents.has(id));
1570
+ return terminals.length > 0 ? terminals : items.map(itemId);
1571
+ }
1572
+ function projectedToolStatus(tool) {
1573
+ if (tool.status === "cached")
1574
+ return "completed";
1575
+ if (tool.status === "cancelled")
1576
+ return "skipped";
1577
+ return tool.status;
1578
+ }
1474
1579
  var leafExpansions = new WeakMap;
1580
+ function isLeafRun(run) {
1581
+ return run.stages.every((stage) => authoritativeWorkflowChildRunId(stage) === undefined);
1582
+ }
1583
+ function expandWorkflowGraph(snapshot, rootRunId) {
1584
+ return createWorkflowGraphExpander(snapshot)(rootRunId);
1585
+ }
1475
1586
  var snapshotExpansions = new WeakMap;
1587
+ function isSettledStage2(stage) {
1588
+ return stage.status === "completed" || stage.status === "failed" || stage.status === "skipped";
1589
+ }
1476
1590
  var settledStageCounts = new WeakMap;
1477
1591
  var expandedStageLists = new WeakSet;
1478
1592
  var chainExpansions = new WeakMap;
1593
+ function executionOrderKey(stage) {
1594
+ return stage.executionOrder ?? Number.MAX_SAFE_INTEGER;
1595
+ }
1596
+ function chainStage(stage) {
1597
+ return stage.nodeKind !== "tool" && authoritativeWorkflowChildRunId(stage) === undefined;
1598
+ }
1599
+ function isChainRun(run) {
1600
+ if (toolNodesFor(run).length > 0)
1601
+ return false;
1602
+ let order = Number.NEGATIVE_INFINITY;
1603
+ for (const stage of run.stages) {
1604
+ const next = executionOrderKey(stage);
1605
+ if (next < order || !chainStage(stage))
1606
+ return false;
1607
+ order = next;
1608
+ }
1609
+ return true;
1610
+ }
1611
+ function expandChainFrom(run, previous) {
1612
+ const before = previous.run.stages;
1613
+ if (run.id !== previous.run.id || run.name !== previous.run.name || toolNodesFor(run).length > 0)
1614
+ return;
1615
+ if (run.stages.length < before.length)
1616
+ return;
1617
+ const replaced = [];
1618
+ for (let index = 0;index < before.length; index++) {
1619
+ const stage = run.stages[index];
1620
+ const earlier = before[index];
1621
+ if (stage === earlier)
1622
+ continue;
1623
+ if (stage.id !== earlier.id || stage.executionOrder !== earlier.executionOrder || !sameIds(stage.parentIds, earlier.parentIds) || !chainStage(stage))
1624
+ return;
1625
+ replaced.push(index);
1626
+ }
1627
+ let order = before.length === 0 ? Number.NEGATIVE_INFINITY : executionOrderKey(before[before.length - 1]);
1628
+ for (let index = before.length;index < run.stages.length; index++) {
1629
+ const stage = run.stages[index];
1630
+ const next = executionOrderKey(stage);
1631
+ if (next < order || !chainStage(stage))
1632
+ return;
1633
+ order = next;
1634
+ }
1635
+ const graph = previous.graph;
1636
+ if (replaced.length === 0 && run.stages.length === before.length)
1637
+ return graph;
1638
+ const stages = graph.stages.slice();
1639
+ const nodes = graph.nodes.slice();
1640
+ for (const index of replaced) {
1641
+ const earlier = graph.stages[index];
1642
+ const stage = {
1643
+ ...run.stages[index],
1644
+ id: earlier.id,
1645
+ parentIds: earlier.parentIds,
1646
+ workflowGraphTarget: earlier.workflowGraphTarget
1647
+ };
1648
+ stages[index] = stage;
1649
+ nodes[index] = { kind: "stage", stage };
1650
+ }
1651
+ let settled = settledStageCounts.get(graph.stages);
1652
+ if (settled !== undefined)
1653
+ for (const index of replaced)
1654
+ settled += Number(isSettledStage2(stages[index])) - Number(isSettledStage2(graph.stages[index]));
1655
+ let targets = graph.targets;
1656
+ if (run.stages.length > before.length) {
1657
+ const extended = new Map(graph.targets);
1658
+ for (let index = before.length;index < run.stages.length; index++) {
1659
+ const source = run.stages[index];
1660
+ const target = { runId: run.id, stageId: source.id, runName: run.name, depth: 0 };
1661
+ extended.set(source.id, target);
1662
+ const stage = {
1663
+ ...source,
1664
+ id: source.id,
1665
+ parentIds: Object.freeze([...source.parentIds]),
1666
+ workflowGraphTarget: target
1667
+ };
1668
+ stages.push(stage);
1669
+ nodes.push({ kind: "stage", stage });
1670
+ if (settled !== undefined)
1671
+ settled += Number(isSettledStage2(stage));
1672
+ }
1673
+ targets = extended;
1674
+ }
1675
+ expandedStageLists.add(stages);
1676
+ if (settled !== undefined)
1677
+ settledStageCounts.set(stages, settled);
1678
+ return { stages, renderStages: stages, tools: graph.tools, nodes, targets };
1679
+ }
1680
+ function rememberChainExpansion(memo, runById, run, graph, patched) {
1681
+ if (patched || isChainRun(run))
1682
+ memo.set(run.id, { run, graph });
1683
+ else
1684
+ memo.delete(run.id);
1685
+ for (const runId of memo.keys())
1686
+ if (!runById.has(runId))
1687
+ memo.delete(runId);
1688
+ }
1689
+ function createWorkflowGraphExpander(snapshot) {
1690
+ let runById;
1691
+ let expansions;
1692
+ let chains;
1693
+ if (Object.isFrozen(snapshot)) {
1694
+ expansions = snapshotExpansions.get(snapshot);
1695
+ if (expansions === undefined) {
1696
+ expansions = new Map;
1697
+ snapshotExpansions.set(snapshot, expansions);
1698
+ }
1699
+ const lineage = graphSnapshotLineage(snapshot);
1700
+ if (lineage !== undefined) {
1701
+ chains = chainExpansions.get(lineage);
1702
+ if (chains === undefined) {
1703
+ chains = new Map;
1704
+ chainExpansions.set(lineage, chains);
1705
+ }
1706
+ }
1707
+ }
1708
+ return (rootRunId) => {
1709
+ const known = expansions?.get(rootRunId);
1710
+ if (known !== undefined)
1711
+ return known;
1712
+ runById ??= new Map(snapshot.runs.map((run) => [run.id, run]));
1713
+ const root = runById.get(rootRunId);
1714
+ const previous = root === undefined ? undefined : chains?.get(rootRunId);
1715
+ const patched = root === undefined || previous === undefined ? undefined : expandChainFrom(root, previous);
1716
+ const graph = patched ?? expandIndexedWorkflowGraph(runById, rootRunId);
1717
+ expansions?.set(rootRunId, graph);
1718
+ if (chains !== undefined && root !== undefined)
1719
+ rememberChainExpansion(chains, runById, root, graph, patched !== undefined);
1720
+ return graph;
1721
+ };
1722
+ }
1723
+ function expandIndexedWorkflowGraph(runById, rootRunId) {
1724
+ const root = runById.get(rootRunId);
1725
+ if (!root)
1726
+ return { stages: [], renderStages: [], tools: [], nodes: [], targets: new Map };
1727
+ const targets = new Map;
1728
+ const rootOwnerByRunId = new Map;
1729
+ const rootOwnerFor = (runId) => {
1730
+ if (rootOwnerByRunId.has(runId))
1731
+ return rootOwnerByRunId.get(runId);
1732
+ const owner = reciprocalWorkflowRootRunId(runById, runId);
1733
+ rootOwnerByRunId.set(runId, owner);
1734
+ return owner;
1735
+ };
1736
+ const visiting = new Set;
1737
+ const expandedChildren = new Set;
1738
+ const expandRun = (run, depth, incomingParentIds) => {
1739
+ if (visiting.has(run.id))
1740
+ return { stages: [], tools: [], nodes: [], terminalIds: [] };
1741
+ const isRootRun = run.id === rootRunId;
1742
+ const memoizable = !isRootRun && Object.isFrozen(run) && isLeafRun(run);
1743
+ if (memoizable) {
1744
+ const known = leafExpansions.get(run);
1745
+ if (known !== undefined && known.depth === depth && sameIds(known.incomingParentIds, incomingParentIds)) {
1746
+ for (const [id, target] of known.targets)
1747
+ targets.set(id, target);
1748
+ return known.result;
1749
+ }
1750
+ }
1751
+ const runTargets = [];
1752
+ visiting.add(run.id);
1753
+ const items = localItems(run);
1754
+ const stages = [];
1755
+ const tools = [];
1756
+ const nodes = [];
1757
+ const stageById = new Map(run.stages.map((stage) => [stage.id, stage]));
1758
+ const boundaryExpansions = new Map;
1759
+ const validChildRunFor = (stage) => {
1760
+ const childRunId = authoritativeWorkflowChildRunId(stage);
1761
+ if (childRunId === undefined)
1762
+ return;
1763
+ const childRun = runById.get(childRunId);
1764
+ const runRoot = rootOwnerFor(run.id);
1765
+ if (childRun === undefined || expandedChildren.has(childRun.id) || childRun.stages.length + toolNodesFor(childRun).length === 0 || childRun.parentRunId !== run.id || childRun.parentStageId !== stage.id || runRoot === undefined || rootOwnerFor(childRun.id) !== runRoot) {
1766
+ return;
1767
+ }
1768
+ return childRun;
1769
+ };
1770
+ const resolvedParentIdsFor = (item) => {
1771
+ const parentIds = itemParents(item);
1772
+ if (parentIds.length === 0)
1773
+ return [...incomingParentIds];
1774
+ return parentIds.flatMap((parentId) => {
1775
+ const parentStage = stageById.get(parentId);
1776
+ const parentExpansion = parentStage === undefined ? undefined : boundaryExpansionFor(parentStage);
1777
+ return parentExpansion?.terminalIds.length ? [...parentExpansion.terminalIds] : [virtualNodeId(run.id, parentId, isRootRun)];
1778
+ });
1779
+ };
1780
+ const boundaryExpansionFor = (stage) => {
1781
+ const cached = boundaryExpansions.get(stage.id);
1782
+ if (cached !== undefined)
1783
+ return cached ?? undefined;
1784
+ const childRun = validChildRunFor(stage);
1785
+ if (childRun === undefined)
1786
+ return;
1787
+ boundaryExpansions.set(stage.id, null);
1788
+ expandedChildren.add(childRun.id);
1789
+ const stageItem = { kind: "stage", stage, sourceIndex: 0 };
1790
+ const childExpanded = expandRun(childRun, depth + 1, resolvedParentIdsFor(stageItem));
1791
+ if (childExpanded.stages.length === 0 || childExpanded.terminalIds.length === 0)
1792
+ return;
1793
+ boundaryExpansions.set(stage.id, childExpanded);
1794
+ return childExpanded;
1795
+ };
1796
+ for (const item of items) {
1797
+ const sourceId = itemId(item);
1798
+ const id = virtualNodeId(run.id, sourceId, isRootRun);
1799
+ const resolvedParentIds = resolvedParentIdsFor(item);
1800
+ if (item.kind === "tool") {
1801
+ const tool = {
1802
+ ...item.tool,
1803
+ id,
1804
+ parentIds: Object.freeze(resolvedParentIds),
1805
+ runId: run.id,
1806
+ runName: run.name,
1807
+ depth
1808
+ };
1809
+ const projectionTarget = {
1810
+ runId: run.id,
1811
+ stageId: item.tool.id,
1812
+ runName: run.name,
1813
+ depth
1814
+ };
1815
+ const projection = {
1816
+ id,
1817
+ name: item.tool.name,
1818
+ status: projectedToolStatus(item.tool),
1819
+ parentIds: Object.freeze(resolvedParentIds),
1820
+ executionOrder: item.tool.executionOrder,
1821
+ nodeKind: "tool",
1822
+ toolStatus: item.tool.status,
1823
+ startedAt: item.tool.startedAt,
1824
+ endedAt: item.tool.endedAt,
1825
+ result: item.tool.resultSummary,
1826
+ error: item.tool.error,
1827
+ toolEvents: [],
1828
+ attachable: false,
1829
+ workflowGraphTarget: projectionTarget
1830
+ };
1831
+ targets.set(id, projectionTarget);
1832
+ runTargets.push([id, projectionTarget]);
1833
+ tools.push(tool);
1834
+ stages.push(projection);
1835
+ nodes.push({ kind: "tool", tool });
1836
+ continue;
1837
+ }
1838
+ const childExpanded = boundaryExpansionFor(item.stage);
1839
+ if (childExpanded !== undefined) {
1840
+ stages.push(...childExpanded.stages);
1841
+ tools.push(...childExpanded.tools);
1842
+ nodes.push(...childExpanded.nodes);
1843
+ continue;
1844
+ }
1845
+ const target = {
1846
+ runId: run.id,
1847
+ stageId: item.stage.id,
1848
+ runName: run.name,
1849
+ depth
1850
+ };
1851
+ targets.set(id, target);
1852
+ runTargets.push([id, target]);
1853
+ const stage = {
1854
+ ...item.stage,
1855
+ id,
1856
+ parentIds: Object.freeze(resolvedParentIds),
1857
+ workflowGraphTarget: target
1858
+ };
1859
+ stages.push(stage);
1860
+ nodes.push({ kind: "stage", stage });
1861
+ }
1862
+ const terminalIds = localTerminalIds(items).flatMap((sourceId) => {
1863
+ const stage = stageById.get(sourceId);
1864
+ const replacement = stage === undefined ? undefined : boundaryExpansionFor(stage);
1865
+ return replacement?.terminalIds.length ? [...replacement.terminalIds] : [virtualNodeId(run.id, sourceId, isRootRun)];
1866
+ });
1867
+ visiting.delete(run.id);
1868
+ const result = { stages, tools, nodes, terminalIds };
1869
+ if (memoizable)
1870
+ leafExpansions.set(run, { depth, incomingParentIds: [...incomingParentIds], result, targets: runTargets });
1871
+ return result;
1872
+ };
1873
+ const expanded = expandRun(root, 0, []);
1874
+ const stages = expanded.stages.filter((stage) => stage.nodeKind !== "tool");
1875
+ expandedStageLists.add(stages);
1876
+ return {
1877
+ stages,
1878
+ renderStages: expanded.stages,
1879
+ tools: expanded.tools,
1880
+ nodes: expanded.nodes,
1881
+ targets
1882
+ };
1883
+ }
1884
+ function sameIds(left, right) {
1885
+ if (left.length !== right.length)
1886
+ return false;
1887
+ for (let index = 0;index < left.length; index++) {
1888
+ if (left[index] !== right[index])
1889
+ return false;
1890
+ }
1891
+ return true;
1892
+ }
1893
+
1894
+ // dist/builtin/workflows/src/shared/inspection-store-snapshot.ts
1895
+ var prepareInspectionSnapshotKey = Symbol("workflow.prepare-inspection-snapshot");
1896
+ var expandedRuns = new WeakMap;
1897
+ var inspectionExpanders = new WeakMap;
1898
+
1899
+ // dist/builtin/workflows/src/shared/session-scoped-singleton.ts
1900
+ import { sessionScopedExtensionState } from "@bastani/atomic";
1901
+ var SHARED_STATES_KEY = Symbol.for("atomic-workflows/session-scoped-singleton-pre-adoption@1");
1902
+ function processBag() {
1903
+ return globalThis;
1904
+ }
1905
+ function recordLocal(key, local) {
1906
+ const bag = processBag();
1907
+ let states = bag[SHARED_STATES_KEY];
1908
+ if (states === undefined || states.version !== 1) {
1909
+ states = { version: 1, byKey: new Map };
1910
+ bag[SHARED_STATES_KEY] = states;
1911
+ }
1912
+ const existing = states.byKey.get(key);
1913
+ if (existing !== undefined)
1914
+ return existing;
1915
+ const created = { local, instance: undefined, localClaimed: false };
1916
+ states.byKey.set(key, created);
1917
+ return created;
1918
+ }
1919
+ function createSessionScopedSingleton(key, createLocal) {
1920
+ const local = createLocal();
1921
+ const shared = recordLocal(key, local);
1922
+ let instance = local;
1923
+ const live = () => shared.instance ?? instance;
1924
+ const current = () => live();
1925
+ const adoptWithResult = (scope, options) => {
1926
+ const current = live();
1927
+ let targetCreated = false;
1928
+ const target = sessionScopedExtensionState(scope, key, () => {
1929
+ targetCreated = true;
1930
+ if (shared.localClaimed)
1931
+ return (options?.create ?? createLocal)();
1932
+ shared.localClaimed = true;
1933
+ return shared.local;
1934
+ });
1935
+ const preservedCurrent = !targetCreated && target !== current && options?.preserveCurrentWhenTargetExists?.(current, target) === true;
1936
+ shared.instance = preservedCurrent ? current : target;
1937
+ instance = shared.instance;
1938
+ return { instance: shared.instance, preservedCurrent };
1939
+ };
1940
+ const adopt = (scope, options) => adoptWithResult(scope, options).instance;
1941
+ const facade = new Proxy(local, {
1942
+ get(_target, prop) {
1943
+ const value = Reflect.get(live(), prop, live());
1944
+ if (typeof value === "function") {
1945
+ return value.bind(live());
1946
+ }
1947
+ return value;
1948
+ },
1949
+ set(_target, prop, value) {
1950
+ return Reflect.set(live(), prop, value, live());
1951
+ },
1952
+ has(_target, prop) {
1953
+ return Reflect.has(live(), prop);
1954
+ }
1955
+ });
1956
+ return { facade, adopt, adoptWithResult, current };
1957
+ }
1958
+
1959
+ // dist/builtin/workflows/src/shared/workflow-observation-runtime.ts
1960
+ import { randomUUID } from "node:crypto";
1479
1961
 
1480
1962
  // dist/builtin/workflows/src/shared/workflow-activity.ts
1481
1963
  function workflowActivityNodeKey(runId, nodeId) {
@@ -1889,10 +2371,67 @@ function createStoreContext(state = createStoreState()) {
1889
2371
  let cachedGraphVersion = -1;
1890
2372
  let cachedGraphSnapshot;
1891
2373
  const graphBuilder = new GraphStoreSnapshotBuilder;
2374
+ let inspectionBuilder;
2375
+ let inspectionVersion = -1;
2376
+ let inspectionView;
2377
+ let inspectionTargets = new WeakMap;
1892
2378
  const observation = new WorkflowObservationRuntime({ all: () => state.runs, find: findRun, position: (runId) => runPosition(state, runId) }, bumpAndNotify);
1893
2379
  function snapshot() {
1894
2380
  return JSON.parse(JSON.stringify({ runs: state.runs, notices: state.notices, version: state.version }));
1895
2381
  }
2382
+ async function prepareInspectionSnapshot() {
2383
+ let prepared = 0;
2384
+ for (;; ) {
2385
+ inspectionBuilder ??= new GraphStoreSnapshotBuilder({ fullPayloads: true });
2386
+ const builder = inspectionBuilder;
2387
+ const unhandled = state.runs.filter((run) => !builder.hasRun(run) || !graphBuilder.hasRun(run));
2388
+ if (unhandled.length === 0)
2389
+ return;
2390
+ for (const run of unhandled) {
2391
+ if (builder.hasRun(run) && graphBuilder.hasRun(run))
2392
+ continue;
2393
+ if (prepared >= 16) {
2394
+ prepared = 0;
2395
+ await new Promise((resolve) => setImmediate(resolve));
2396
+ if (inspectionBuilder !== builder)
2397
+ break;
2398
+ }
2399
+ if (findRun(run.id) === run) {
2400
+ if (!builder.hasRun(run))
2401
+ builder.buildRun(run);
2402
+ if (!graphBuilder.hasRun(run))
2403
+ graphBuilder.buildRun(run);
2404
+ prepared++;
2405
+ }
2406
+ }
2407
+ }
2408
+ }
2409
+ function inspectionSnapshot(runId) {
2410
+ if (inspectionView !== undefined && inspectionVersion === state.version)
2411
+ return inspectionView;
2412
+ inspectionBuilder ??= new GraphStoreSnapshotBuilder({ fullPayloads: true });
2413
+ const builder = inspectionBuilder;
2414
+ if (runId === undefined) {
2415
+ inspectionView = builder.build(state.runs, state.notices, state.version);
2416
+ inspectionVersion = state.version;
2417
+ return inspectionView;
2418
+ }
2419
+ const compact = graphSnapshot();
2420
+ let targets = inspectionTargets.get(compact);
2421
+ const known = targets?.get(runId);
2422
+ if (known !== undefined)
2423
+ return known;
2424
+ const graph = expandWorkflowGraph(compact, runId);
2425
+ const fullRunIds = new Set([runId, ...Array.from(graph.targets.values(), (target) => target.runId)]);
2426
+ const runs = compact.runs.map((run) => fullRunIds.has(run.id) ? builder.buildRun(findRun(run.id)) : run);
2427
+ const view = builder.publish(runs, compact.notices, state.version);
2428
+ if (targets === undefined) {
2429
+ targets = new Map;
2430
+ inspectionTargets.set(compact, targets);
2431
+ }
2432
+ targets.set(runId, view);
2433
+ return view;
2434
+ }
1896
2435
  function graphSnapshot() {
1897
2436
  if (cachedGraphSnapshot === undefined || cachedGraphVersion !== state.version) {
1898
2437
  cachedGraphSnapshot = graphBuilder.build(state.runs, state.notices, state.version);
@@ -1911,6 +2450,7 @@ function createStoreContext(state = createStoreState()) {
1911
2450
  let batchedChanges = [];
1912
2451
  function notify(change) {
1913
2452
  graphBuilder.markChanged(change);
2453
+ inspectionBuilder?.markChanged(change);
1914
2454
  if (batchDepth > 0) {
1915
2455
  batchedNotification = true;
1916
2456
  if (change === undefined)
@@ -1977,11 +2517,22 @@ function createStoreContext(state = createStoreState()) {
1977
2517
  if (removed === undefined)
1978
2518
  return;
1979
2519
  state.runPositions.delete(removed.id);
2520
+ graphBuilder.removeRun(removed.id);
2521
+ inspectionBuilder?.removeRun(removed.id);
2522
+ inspectionView = undefined;
2523
+ inspectionTargets = new WeakMap;
1980
2524
  unindexChildRun(state, removed.id);
1981
2525
  for (let index = position;index < state.runs.length; index++)
1982
2526
  state.runPositions.set(state.runs[index].id, index);
1983
2527
  }
1984
2528
  function clearRuns() {
2529
+ graphBuilder.clear();
2530
+ cachedGraphSnapshot = undefined;
2531
+ cachedGraphVersion = -1;
2532
+ inspectionBuilder = undefined;
2533
+ inspectionTargets = new WeakMap;
2534
+ inspectionView = undefined;
2535
+ inspectionVersion = -1;
1985
2536
  state.runs.length = 0;
1986
2537
  state.runPositions.clear();
1987
2538
  state.childRunIds.clear();
@@ -2049,6 +2600,8 @@ function createStoreContext(state = createStoreState()) {
2049
2600
  observation,
2050
2601
  snapshot,
2051
2602
  graphSnapshot,
2603
+ inspectionSnapshot,
2604
+ prepareInspectionSnapshot,
2052
2605
  graphRun,
2053
2606
  notify,
2054
2607
  batch,
@@ -3151,6 +3704,9 @@ function createRunStoreMethods(context) {
3151
3704
  graphSnapshot() {
3152
3705
  return context.graphSnapshot();
3153
3706
  },
3707
+ inspectionSnapshot(runId) {
3708
+ return context.inspectionSnapshot(runId);
3709
+ },
3154
3710
  graphRun(runId) {
3155
3711
  return context.graphRun(runId);
3156
3712
  },
@@ -3631,6 +4187,7 @@ function createStore() {
3631
4187
  context.backend = getDurableBackendProcessOwner().injectedBackend;
3632
4188
  const created = {
3633
4189
  [workflowObservationRuntimeKey]: context.observation,
4190
+ [prepareInspectionSnapshotKey]: context.prepareInspectionSnapshot,
3634
4191
  [workflowStoreBackendOverride]: context.backend,
3635
4192
  get [workflowStoreBackend]() {
3636
4193
  return context.backend;