botmux-workflow-core 3.56.0 → 3.57.1

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.
@@ -479,6 +479,12 @@ function validateDag(raw) {
479
479
  if (n.bot !== void 0) problems.push(`host node "${id}".bot is not supported \u2014 host nodes do not spawn a CLI`);
480
480
  if (n.override !== void 0) problems.push(`host node "${id}".override is not supported`);
481
481
  if (n.revisitTo !== void 0) problems.push(`host node "${id}".revisitTo is not supported`);
482
+ if (n.checkpoint !== void 0) {
483
+ problems.push(`host node "${id}".checkpoint is not supported \u2014 only goal nodes can be checkpoints`);
484
+ }
485
+ if (n.placement !== void 0) {
486
+ problems.push(`host node "${id}".placement is not supported \u2014 host nodes are executed by the main bot, never handed off`);
487
+ }
482
488
  if (n.resultSchema !== void 0) {
483
489
  problems.push(`host node "${id}".resultSchema is not supported \u2014 host output uses the trusted executor result contract`);
484
490
  }
@@ -550,6 +556,8 @@ function validateDag(raw) {
550
556
  const humanGate = normHumanGate(n.humanGate, `node "${id}"`, problems);
551
557
  const override = normOverride(n.override, `node "${id}"`, problems);
552
558
  const revisitTo = normRevisitTo(n.revisitTo, id, problems);
559
+ const checkpoint = normCheckpoint(n.checkpoint, `goal node "${id}"`, problems);
560
+ const placement = normPlacement(n.placement, `goal node "${id}"`, problems);
553
561
  if (schemaVersion !== 2 && n.outputs !== void 0) {
554
562
  problems.push(`goal node "${id}".outputs requires schemaVersion 2`);
555
563
  }
@@ -567,7 +575,9 @@ function validateDag(raw) {
567
575
  timeoutSec,
568
576
  humanGate,
569
577
  resultSchema,
570
- ...revisitTo ? { revisitTo } : {}
578
+ ...revisitTo ? { revisitTo } : {},
579
+ ...checkpoint ? { checkpoint } : {},
580
+ ...placement ? { placement } : {}
571
581
  });
572
582
  }
573
583
  for (const node of nodes) {
@@ -937,6 +947,18 @@ function normTimeoutSec(v, where, problems) {
937
947
  }
938
948
  return v;
939
949
  }
950
+ function normPlacement(v, where, problems) {
951
+ if (v === void 0 || v === "topic") return void 0;
952
+ if (v === "main") return "main";
953
+ problems.push(`${where}.placement must be "topic" or "main"`);
954
+ return void 0;
955
+ }
956
+ function normCheckpoint(v, where, problems) {
957
+ if (v === void 0 || v === false) return void 0;
958
+ if (v === true) return true;
959
+ problems.push(`${where}.checkpoint must be a boolean`);
960
+ return void 0;
961
+ }
940
962
  function normRevisitTo(v, id, problems) {
941
963
  if (v === void 0) return void 0;
942
964
  if (!Array.isArray(v)) {
@@ -1005,6 +1027,12 @@ function normLoopFields(n, id, problems, schemaVersion) {
1005
1027
  if (n.override !== void 0) {
1006
1028
  problems.push(`${where}.override is not supported \u2014 a loop composite never spawns a worker; set override on body nodes instead`);
1007
1029
  }
1030
+ if (n.checkpoint !== void 0) {
1031
+ problems.push(`${where}.checkpoint is not supported \u2014 mark a goal node outside the loop instead`);
1032
+ }
1033
+ if (n.placement !== void 0) {
1034
+ problems.push(`${where}.placement is not supported \u2014 a loop's body nodes always run in node topics`);
1035
+ }
1008
1036
  let maxIterations;
1009
1037
  if (typeof n.maxIterations !== "number" || !Number.isInteger(n.maxIterations) || n.maxIterations < 1) {
1010
1038
  problems.push(`${where}.maxIterations must be a positive integer`);
@@ -1051,6 +1079,12 @@ function normLoopFields(n, id, problems, schemaVersion) {
1051
1079
  if (b.triggerRule !== void 0) {
1052
1080
  problems.push(`${where}.body node "${bid}".triggerRule is not supported inside a loop body (first cut)`);
1053
1081
  }
1082
+ if (b.checkpoint !== void 0) {
1083
+ problems.push(`${where}.body node "${bid}".checkpoint is not supported inside a loop body (first cut)`);
1084
+ }
1085
+ if (b.placement !== void 0) {
1086
+ problems.push(`${where}.body node "${bid}".placement is not supported \u2014 loop body nodes always run in node topics`);
1087
+ }
1054
1088
  const bdepends = normDepends(b.depends, `${where}.body node "${bid}"`, problems);
1055
1089
  const bFromList = bdepends.map((d) => d.from);
1056
1090
  if (bFromList.includes(bid)) problems.push(`${where}.body node "${bid}" depends on itself`);
@@ -2603,6 +2637,7 @@ function materialize(events) {
2603
2637
  const attempts = /* @__PURE__ */ new Map();
2604
2638
  const loops = /* @__PURE__ */ new Map();
2605
2639
  const edges = /* @__PURE__ */ new Map();
2640
+ const assignments = /* @__PURE__ */ new Map();
2606
2641
  let runStatus = "running";
2607
2642
  let cancelRequestId;
2608
2643
  let cancelRequestedBy;
@@ -2612,6 +2647,7 @@ function materialize(events) {
2612
2647
  let blockedNodeId;
2613
2648
  let uncertainHostEffects;
2614
2649
  const cancelledAttempts = /* @__PURE__ */ new Map();
2650
+ const regateInstances = /* @__PURE__ */ new Set();
2615
2651
  const resolvedWaitIds = /* @__PURE__ */ new Set();
2616
2652
  const set = (id, status, gateCleared) => {
2617
2653
  const prev = nodes.get(id);
@@ -2632,6 +2668,16 @@ function materialize(events) {
2632
2668
  nodes.set(nodeId, { ...prev, status });
2633
2669
  }
2634
2670
  };
2671
+ const supersedeInstance = (nodeId, instanceId) => {
2672
+ instances.set(instanceId, { status: "superseded" });
2673
+ if (nodes.get(nodeId)?.effectiveInstanceId === instanceId) {
2674
+ nodes.set(nodeId, { status: "pending" });
2675
+ }
2676
+ if (runStatus === "blocked" && blockedNodeId === nodeId) {
2677
+ runStatus = "running";
2678
+ blockedNodeId = void 0;
2679
+ }
2680
+ };
2635
2681
  const loop = (id) => {
2636
2682
  let ls = loops.get(id);
2637
2683
  if (!ls) {
@@ -2664,6 +2710,7 @@ function materialize(events) {
2664
2710
  attempts.set(e.instanceId ?? e.nodeId, e.attemptId);
2665
2711
  break;
2666
2712
  case "hostInputPrepared":
2713
+ if (instances.get(e.instanceId)?.status === "superseded") break;
2667
2714
  instances.set(e.instanceId, { status: "pending" });
2668
2715
  nodes.set(e.nodeId, {
2669
2716
  status: "pending",
@@ -2673,6 +2720,7 @@ function materialize(events) {
2673
2720
  break;
2674
2721
  case "hostEffectIntent":
2675
2722
  if (instances.get(e.instanceId)?.status === "cancelled") break;
2723
+ if (instances.get(e.instanceId)?.status === "superseded") break;
2676
2724
  recordInstance(e.nodeId, e.instanceId, "running", true);
2677
2725
  attempts.set(e.instanceId, e.attemptId);
2678
2726
  break;
@@ -2712,6 +2760,15 @@ function materialize(events) {
2712
2760
  } else set(e.nodeId, "blocked");
2713
2761
  break;
2714
2762
  case "nodeRetryRequested":
2763
+ if (e.instanceId && regateInstances.has(e.instanceId)) {
2764
+ regateInstances.delete(e.instanceId);
2765
+ supersedeInstance(e.nodeId, e.instanceId);
2766
+ if (runStatus === "blocked") {
2767
+ runStatus = "running";
2768
+ blockedNodeId = void 0;
2769
+ }
2770
+ break;
2771
+ }
2715
2772
  if (e.resetGate) {
2716
2773
  nodes.set(e.nodeId, {
2717
2774
  status: "pending",
@@ -2731,15 +2788,40 @@ function materialize(events) {
2731
2788
  // ── cross-node revisit / instance refresh ──
2732
2789
  case "nodeRevisitRequested":
2733
2790
  break;
2734
- case "nodeInstanceSuperseded": {
2735
- instances.set(e.instanceId, { status: "superseded" });
2736
- if (nodes.get(e.nodeId)?.effectiveInstanceId === e.instanceId) {
2737
- nodes.set(e.nodeId, { status: "pending" });
2791
+ case "nodeInstanceSuperseded":
2792
+ supersedeInstance(e.nodeId, e.instanceId);
2793
+ break;
2794
+ case "dagRevised": {
2795
+ const effects = e.effects;
2796
+ for (const item of effects?.supersede ?? []) {
2797
+ if (item.waitId) resolvedWaitIds.add(item.waitId);
2798
+ supersedeInstance(item.nodeId, item.instanceId);
2738
2799
  }
2739
- if (runStatus === "blocked" && blockedNodeId === e.nodeId) {
2800
+ for (const item of effects?.cancel ?? []) {
2801
+ cancelledAttempts.set(item.instanceId ?? item.nodeId, item.attemptId);
2802
+ if (item.instanceId) instances.set(item.instanceId, { status: "cancelled" });
2803
+ const prev = nodes.get(item.nodeId);
2804
+ nodes.set(item.nodeId, {
2805
+ status: "cancelled",
2806
+ ...item.instanceId ?? prev?.effectiveInstanceId ? { effectiveInstanceId: item.instanceId ?? prev?.effectiveInstanceId } : {}
2807
+ });
2808
+ }
2809
+ if (runStatus === "blocked" && (effects?.cancel ?? []).length > 0 && ((effects?.cancel ?? []).some((item) => item.nodeId === blockedNodeId) || ![...nodes.values()].some((state) => state.status === "blocked"))) {
2740
2810
  runStatus = "running";
2741
2811
  blockedNodeId = void 0;
2742
2812
  }
2813
+ for (const item of effects?.regate ?? []) {
2814
+ const prev = nodes.get(item.nodeId);
2815
+ if (prev?.effectiveInstanceId !== item.instanceId) continue;
2816
+ const { gateCleared: _dropped, ...rest } = prev;
2817
+ nodes.set(item.nodeId, rest);
2818
+ const instance = instances.get(item.instanceId);
2819
+ if (instance) {
2820
+ const { gateCleared: _instanceDropped, ...instanceRest } = instance;
2821
+ instances.set(item.instanceId, instanceRest);
2822
+ }
2823
+ regateInstances.add(item.instanceId);
2824
+ }
2743
2825
  break;
2744
2826
  }
2745
2827
  case "gateDispatched":
@@ -2797,6 +2879,33 @@ function materialize(events) {
2797
2879
  if (e.instanceId) recordInstance(e.nodeId, e.instanceId, "cancelled", false);
2798
2880
  else set(e.nodeId, "cancelled");
2799
2881
  break;
2882
+ case "nodeHandoffPrepared":
2883
+ case "nodeHandoffSent":
2884
+ case "nodeHandoffAccepted":
2885
+ case "nodeHandoffStarted":
2886
+ case "nodeStalled":
2887
+ break;
2888
+ case "nodeReassigned": {
2889
+ assignments.set(e.nodeId, e.to);
2890
+ if (!e.attemptId || runStatus !== "running" && runStatus !== "blocked") break;
2891
+ const key = e.instanceId ?? e.nodeId;
2892
+ if (attempts.get(key) !== e.attemptId) break;
2893
+ if (e.instanceId) {
2894
+ if (instances.get(e.instanceId)?.status !== "running") break;
2895
+ instances.set(e.instanceId, { status: "pending" });
2896
+ const current = nodes.get(e.nodeId);
2897
+ if (current?.effectiveInstanceId === e.instanceId && current.status === "running") {
2898
+ nodes.set(e.nodeId, {
2899
+ status: "pending",
2900
+ effectiveInstanceId: e.instanceId,
2901
+ ...current.gateCleared ? { gateCleared: true } : {}
2902
+ });
2903
+ }
2904
+ } else if (nodes.get(e.nodeId)?.status === "running") {
2905
+ set(e.nodeId, "pending");
2906
+ }
2907
+ break;
2908
+ }
2800
2909
  case "nodeAttemptDrained": {
2801
2910
  if (runStatus !== "running" && runStatus !== "blocked") break;
2802
2911
  const key = e.instanceId ?? e.nodeId;
@@ -2891,7 +3000,9 @@ function materialize(events) {
2891
3000
  instances,
2892
3001
  attempts,
2893
3002
  loops,
2894
- edges
3003
+ edges,
3004
+ assignments,
3005
+ ...regateInstances.size > 0 ? { regatePending: regateInstances } : {}
2895
3006
  };
2896
3007
  }
2897
3008
  function writeState(statePath, snap) {
@@ -2911,6 +3022,7 @@ function writeState(statePath, snap) {
2911
3022
  attempts: Object.fromEntries(snap.attempts),
2912
3023
  loops: snap.loops.size > 0 ? Object.fromEntries(snap.loops) : void 0,
2913
3024
  edges: snap.edges.size > 0 ? Object.fromEntries(snap.edges) : void 0,
3025
+ assignments: snap.assignments.size > 0 ? Object.fromEntries(snap.assignments) : void 0,
2914
3026
  updatedAt: Date.now()
2915
3027
  };
2916
3028
  const tmp = `${statePath}.tmp`;
@@ -3257,9 +3369,9 @@ function assertOrCreatePortableRunSnapshot(runDir, dag, profiles) {
3257
3369
  }
3258
3370
 
3259
3371
  // src/workflows/v3/shared-node-runtime.ts
3260
- var import_node_crypto7 = require("node:crypto");
3261
- var import_node_fs11 = require("node:fs");
3262
- var import_node_path9 = require("node:path");
3372
+ var import_node_crypto9 = require("node:crypto");
3373
+ var import_node_fs15 = require("node:fs");
3374
+ var import_node_path13 = require("node:path");
3263
3375
 
3264
3376
  // src/workflows/v3/core-control.ts
3265
3377
  function matchLoopExitWhen(when, value) {
@@ -3281,6 +3393,15 @@ function revisitBudgetStatus(events, sourceNodeId, toNodeId) {
3281
3393
  if (event.type === "nodeRevisitRequested") {
3282
3394
  runUsed++;
3283
3395
  if (event.nodeId === sourceNodeId && event.toNodeId === toNodeId) pairUsed++;
3396
+ } else if (event.type === "dagRevised") {
3397
+ for (const nodeId of event.effects?.rerun ?? []) {
3398
+ runUsed++;
3399
+ if (nodeId === sourceNodeId && nodeId === toNodeId) pairUsed++;
3400
+ }
3401
+ for (const grant of event.effects?.budgetGrants ?? []) {
3402
+ if (grant.tier === "run") runGranted++;
3403
+ else if (grant.nodeId === sourceNodeId && grant.nodeId === toNodeId) pairGranted++;
3404
+ }
3284
3405
  } else if (event.type === "revisitBudgetGranted") {
3285
3406
  if (event.sourceNodeId === sourceNodeId && event.toNodeId === toNodeId) pairGranted++;
3286
3407
  else if (event.sourceNodeId === void 0 && event.toNodeId === void 0) runGranted++;
@@ -3305,6 +3426,72 @@ function revisitBudgetStatus(events, sourceNodeId, toNodeId) {
3305
3426
  return { ok: true };
3306
3427
  }
3307
3428
 
3429
+ // src/workflows/v3/revisit-plan.ts
3430
+ var import_node_fs9 = require("node:fs");
3431
+ var import_node_path7 = require("node:path");
3432
+ function affectedNodesFrom(dag, root) {
3433
+ const reachable = /* @__PURE__ */ new Set([root]);
3434
+ let changed = true;
3435
+ while (changed) {
3436
+ changed = false;
3437
+ for (const n of dag.nodes) {
3438
+ if (reachable.has(n.id)) continue;
3439
+ if (n.depends.some((d) => reachable.has(d.from))) {
3440
+ reachable.add(n.id);
3441
+ changed = true;
3442
+ }
3443
+ }
3444
+ }
3445
+ return [...reachable];
3446
+ }
3447
+ function planV3Revisit(input) {
3448
+ const { runDir, request } = input;
3449
+ const snap = materialize([...input.events]);
3450
+ const targetEff = snap.nodes.get(request.toNodeId)?.effectiveInstanceId;
3451
+ const targetSucc = targetEff ? [...input.events].reverse().find((e) => e.type === "nodeSucceeded" && (e.instanceId ?? e.nodeId) === targetEff) : void 0;
3452
+ let reasonPathRel;
3453
+ if (request.reason) {
3454
+ const dir = (0, import_node_path7.join)(runDir, "revisits");
3455
+ (0, import_node_fs9.mkdirSync)(dir, { recursive: true });
3456
+ const abs = (0, import_node_path7.join)(dir, `${input.instanceId.replace(/[#/]/g, "-")}-reason.md`);
3457
+ (0, import_node_fs9.writeFileSync)(abs, `# Revisit reason (from ${input.nodeId} / ${input.instanceId})
3458
+
3459
+ ${request.reason}
3460
+ `);
3461
+ reasonPathRel = (0, import_node_path7.relative)(runDir, abs);
3462
+ }
3463
+ const events = [{
3464
+ type: "nodeRevisitRequested",
3465
+ nodeId: input.nodeId,
3466
+ instanceId: input.instanceId,
3467
+ attemptId: input.attemptId,
3468
+ toNodeId: request.toNodeId,
3469
+ ...request.reason ? { reason: request.reason } : {},
3470
+ ...reasonPathRel ? { reasonPath: reasonPathRel } : {},
3471
+ sourceManifestPath: (0, import_node_path7.relative)(runDir, input.sourceManifestPath),
3472
+ ...targetSucc ? { targetPreviousManifestPath: (0, import_node_path7.relative)(runDir, targetSucc.manifestPath) } : {}
3473
+ }];
3474
+ const supersededInstanceIds = [];
3475
+ for (const affectedNodeId of affectedNodesFrom(input.dag, request.toNodeId)) {
3476
+ const eff = snap.nodes.get(affectedNodeId)?.effectiveInstanceId;
3477
+ if (!eff) continue;
3478
+ events.push({
3479
+ type: "nodeInstanceSuperseded",
3480
+ nodeId: affectedNodeId,
3481
+ instanceId: eff,
3482
+ byNodeId: request.toNodeId,
3483
+ reason: "refresh"
3484
+ });
3485
+ supersededInstanceIds.push(eff);
3486
+ }
3487
+ return { events, supersededInstanceIds };
3488
+ }
3489
+
3490
+ // src/workflows/v3/dag-revision.ts
3491
+ var import_node_crypto4 = require("node:crypto");
3492
+ var import_node_fs10 = require("node:fs");
3493
+ var import_node_path8 = require("node:path");
3494
+
3308
3495
  // src/workflows/v3/gate-policy.ts
3309
3496
  function normalizeGateWaitInput(gate) {
3310
3497
  const options = gate.options ?? [...DEFAULT_HUMAN_GATE_OPTIONS];
@@ -3316,15 +3503,514 @@ function normalizeGateWaitInput(gate) {
3316
3503
  };
3317
3504
  }
3318
3505
 
3506
+ // src/workflows/v3/attempt-numbering.ts
3507
+ var ATTEMPT_NNN_RE = /\/attempts\/(\d{3})$/;
3508
+ function attemptNumber(attemptId) {
3509
+ const m = ATTEMPT_NNN_RE.exec(attemptId);
3510
+ return m ? parseInt(m[1], 10) : void 0;
3511
+ }
3512
+ function nextAttemptIdFor(events, key) {
3513
+ const matches = (e) => (e.instanceId ?? e.nodeId) === key;
3514
+ let maxSeen = 0;
3515
+ let reserved;
3516
+ let prepared;
3517
+ for (const e of events) {
3518
+ if (e.type === "nodeDispatched" && matches(e)) {
3519
+ const n2 = attemptNumber(e.attemptId);
3520
+ if (n2 === void 0) continue;
3521
+ maxSeen = Math.max(maxSeen, n2);
3522
+ if (reserved === n2) reserved = void 0;
3523
+ } else if (e.type === "hostInputPrepared" && matches(e)) {
3524
+ const n2 = attemptNumber(e.attemptId);
3525
+ if (n2 === void 0) continue;
3526
+ maxSeen = Math.max(maxSeen, n2);
3527
+ if (reserved === n2) reserved = void 0;
3528
+ prepared = e.attemptId;
3529
+ } else if ((e.type === "nodeSucceeded" || e.type === "nodeFailed" || e.type === "nodeBlocked") && matches(e)) {
3530
+ const n2 = attemptNumber(e.attemptId);
3531
+ if (n2 !== void 0) maxSeen = Math.max(maxSeen, n2);
3532
+ if (prepared === e.attemptId) prepared = void 0;
3533
+ } else if (e.type === "hostEffectIntent" && matches(e) && prepared === e.attemptId) {
3534
+ prepared = void 0;
3535
+ } else if (e.type === "nodeRetryRequested" && matches(e)) {
3536
+ const n2 = attemptNumber(e.nextAttemptId);
3537
+ if (n2 === void 0) continue;
3538
+ reserved = n2;
3539
+ prepared = void 0;
3540
+ maxSeen = Math.max(maxSeen, n2);
3541
+ }
3542
+ }
3543
+ if (prepared) return prepared;
3544
+ const n = reserved ?? maxSeen + 1;
3545
+ return `${key}/attempts/${String(n).padStart(3, "0")}`;
3546
+ }
3547
+ function latestAttemptIdFor(events, key) {
3548
+ let latest;
3549
+ let latestNumber = -1;
3550
+ for (const e of events) {
3551
+ if ((e.type === "nodeDispatched" || e.type === "hostInputPrepared" || e.type === "hostEffectIntent" || e.type === "nodeSucceeded" || e.type === "nodeFailed" || e.type === "nodeBlocked") && (e.instanceId ?? e.nodeId) === key) {
3552
+ const n = attemptNumber(e.attemptId);
3553
+ if (n === void 0) {
3554
+ if (latestNumber < 0) latest = e.attemptId;
3555
+ } else if (n >= latestNumber) {
3556
+ latest = e.attemptId;
3557
+ latestNumber = n;
3558
+ }
3559
+ }
3560
+ }
3561
+ return latest;
3562
+ }
3563
+
3564
+ // src/workflows/v3/dag-revision.ts
3565
+ var V3_DAG_REVISIONS_DIR = "dag-revisions";
3566
+ function v3RevisionStem(revision) {
3567
+ if (!Number.isSafeInteger(revision) || revision < 2) {
3568
+ throw new Error(`v3 revision: invalid revision number ${revision}`);
3569
+ }
3570
+ return String(revision).padStart(3, "0");
3571
+ }
3572
+ function v3RevisionFilePaths(runDir, revision) {
3573
+ const stem = v3RevisionStem(revision);
3574
+ const dir = (0, import_node_path8.join)(runDir, V3_DAG_REVISIONS_DIR);
3575
+ return {
3576
+ dag: (0, import_node_path8.join)(dir, `${stem}.json`),
3577
+ patch: (0, import_node_path8.join)(dir, `${stem}.patch.json`),
3578
+ bots: (0, import_node_path8.join)(dir, `${stem}.bots.json`)
3579
+ };
3580
+ }
3581
+ function v3RevisionDigest(bytes) {
3582
+ return `sha256:${(0, import_node_crypto4.createHash)("sha256").update(bytes).digest("hex")}`;
3583
+ }
3584
+ var V3RevisionIntegrityError = class extends Error {
3585
+ constructor(message) {
3586
+ super(message);
3587
+ this.name = "V3RevisionIntegrityError";
3588
+ }
3589
+ };
3590
+ function readPinnedFile(path, expected, label) {
3591
+ let stat;
3592
+ try {
3593
+ stat = (0, import_node_fs10.lstatSync)(path);
3594
+ } catch {
3595
+ throw new V3RevisionIntegrityError(`${label} is missing (${path})`);
3596
+ }
3597
+ if (!stat.isFile() || stat.isSymbolicLink()) {
3598
+ throw new V3RevisionIntegrityError(`${label} must be a regular file (${path})`);
3599
+ }
3600
+ const dirStat = (0, import_node_fs10.lstatSync)((0, import_node_path8.dirname)(path));
3601
+ if (!dirStat.isDirectory() || dirStat.isSymbolicLink()) {
3602
+ throw new V3RevisionIntegrityError(`${label} directory must be a real directory`);
3603
+ }
3604
+ const bytes = (0, import_node_fs10.readFileSync)(path);
3605
+ const actual = v3RevisionDigest(bytes);
3606
+ if (actual !== expected) {
3607
+ throw new V3RevisionIntegrityError(`${label} digest mismatch (journal ${expected}, file ${actual})`);
3608
+ }
3609
+ return bytes;
3610
+ }
3611
+ var V3_UPDATE_NODE_FIELDS = [
3612
+ "goal",
3613
+ "outputs",
3614
+ "resultSchema",
3615
+ "timeoutSec",
3616
+ "humanGate",
3617
+ "override",
3618
+ "revisitTo",
3619
+ "checkpoint",
3620
+ "placement",
3621
+ "input",
3622
+ "maxIterations",
3623
+ "body",
3624
+ "exit",
3625
+ "feedback",
3626
+ "output"
3627
+ ];
3628
+ var UPDATE_FIELDS = new Set(V3_UPDATE_NODE_FIELDS);
3629
+ function isPlainObject(value) {
3630
+ return typeof value === "object" && value !== null && !Array.isArray(value);
3631
+ }
3632
+ function foldV3Revisions(events) {
3633
+ let current = 1;
3634
+ let maxRevision = 1;
3635
+ const records = [];
3636
+ const byRevision = /* @__PURE__ */ new Map();
3637
+ const applied = [];
3638
+ for (const event of events) {
3639
+ switch (event.type) {
3640
+ case "dagRevisionProposed": {
3641
+ if (byRevision.has(event.revision) || event.revision <= maxRevision) {
3642
+ throw new V3RevisionIntegrityError(`revision ${event.revision} was allocated twice`);
3643
+ }
3644
+ for (const prior of records) {
3645
+ if (prior.state !== "pending") continue;
3646
+ prior.state = "rejected";
3647
+ prior.rejected = { by: "system", reason: "superseded" };
3648
+ }
3649
+ const record = {
3650
+ revision: event.revision,
3651
+ baseRevision: event.baseRevision,
3652
+ sha256: event.sha256,
3653
+ patchSha256: event.patchSha256,
3654
+ ...event.botsSha256 ? { botsSha256: event.botsSha256 } : {},
3655
+ summary: event.summary,
3656
+ ...event.note ? { note: event.note } : {},
3657
+ by: event.by,
3658
+ ...event.auto ? { auto: event.auto } : {},
3659
+ state: "pending",
3660
+ ts: event.ts,
3661
+ reasons: [...event.reasons],
3662
+ touched: [...event.touched],
3663
+ ...event.carriedFrom !== void 0 ? { carriedFrom: event.carriedFrom } : {}
3664
+ };
3665
+ records.push(record);
3666
+ byRevision.set(event.revision, record);
3667
+ maxRevision = event.revision;
3668
+ break;
3669
+ }
3670
+ case "dagRevised": {
3671
+ if (event.baseRevision !== current) {
3672
+ throw new V3RevisionIntegrityError(
3673
+ `revision ${event.revision} is based on ${event.baseRevision} but ${current} was in effect`
3674
+ );
3675
+ }
3676
+ let record = byRevision.get(event.revision);
3677
+ if (event.approval) {
3678
+ if (!record || record.state !== "pending" || record.sha256 !== event.sha256 || record.patchSha256 !== event.patchSha256 || record.botsSha256 !== event.botsSha256 || record.baseRevision !== event.baseRevision) {
3679
+ throw new V3RevisionIntegrityError(
3680
+ `approved revision ${event.revision} does not match a pending proposal`
3681
+ );
3682
+ }
3683
+ record.approvedBy = event.approval.by;
3684
+ } else {
3685
+ if (record || event.revision <= maxRevision) {
3686
+ throw new V3RevisionIntegrityError(`revision ${event.revision} was allocated twice`);
3687
+ }
3688
+ record = {
3689
+ revision: event.revision,
3690
+ baseRevision: event.baseRevision,
3691
+ sha256: event.sha256,
3692
+ patchSha256: event.patchSha256,
3693
+ ...event.botsSha256 ? { botsSha256: event.botsSha256 } : {},
3694
+ summary: event.summary,
3695
+ ...event.note ? { note: event.note } : {},
3696
+ by: event.by,
3697
+ ...event.auto ? { auto: event.auto } : {},
3698
+ state: "pending",
3699
+ ts: event.ts
3700
+ };
3701
+ records.push(record);
3702
+ byRevision.set(event.revision, record);
3703
+ maxRevision = event.revision;
3704
+ }
3705
+ record.state = "applied";
3706
+ record.appliedAt = event.ts;
3707
+ if (event.effects) record.effects = event.effects;
3708
+ applied.push(record);
3709
+ current = event.revision;
3710
+ break;
3711
+ }
3712
+ case "dagRevisionApproved": {
3713
+ const record = byRevision.get(event.revision);
3714
+ if (record && !record.approvedBy) record.approvedBy = event.by;
3715
+ break;
3716
+ }
3717
+ case "dagRevisionRejected": {
3718
+ const record = byRevision.get(event.revision);
3719
+ if (record?.state === "pending") {
3720
+ record.state = "rejected";
3721
+ record.rejected = {
3722
+ by: event.by,
3723
+ reason: event.reason,
3724
+ ...event.detail ? { detail: event.detail } : {}
3725
+ };
3726
+ }
3727
+ break;
3728
+ }
3729
+ default:
3730
+ break;
3731
+ }
3732
+ }
3733
+ let pending;
3734
+ for (const record of records) {
3735
+ if (record.state !== "pending") continue;
3736
+ if (record.baseRevision !== current) {
3737
+ record.state = "rejected";
3738
+ record.rejected = { by: "system", reason: "stale" };
3739
+ continue;
3740
+ }
3741
+ pending = record;
3742
+ }
3743
+ return { current, records, applied, ...pending ? { pending } : {}, maxRevision };
3744
+ }
3745
+ function currentV3Revision(events) {
3746
+ let current = 1;
3747
+ for (const event of events) if (event.type === "dagRevised") current = event.revision;
3748
+ return current;
3749
+ }
3750
+ function parseRevisionDag(bytes, base, label) {
3751
+ let raw;
3752
+ try {
3753
+ raw = JSON.parse(bytes.toString("utf-8"));
3754
+ } catch (err) {
3755
+ throw new V3RevisionIntegrityError(`${label} is not JSON: ${err instanceof Error ? err.message : String(err)}`);
3756
+ }
3757
+ let dag;
3758
+ try {
3759
+ dag = validateDag(raw);
3760
+ } catch (err) {
3761
+ throw new V3RevisionIntegrityError(
3762
+ `${label} failed validation: ${err instanceof DagValidationError ? err.problems.join("; ") : String(err)}`
3763
+ );
3764
+ }
3765
+ if (dag.runId !== base.runId) throw new V3RevisionIntegrityError(`${label} belongs to run ${dag.runId}`);
3766
+ if ((dag.schemaVersion ?? 1) !== (base.schemaVersion ?? 1)) {
3767
+ throw new V3RevisionIntegrityError(`${label} changed the DAG schemaVersion`);
3768
+ }
3769
+ return dag;
3770
+ }
3771
+ function parseRevisionBotSnapshots(raw, label = "revision bot snapshots") {
3772
+ if (!isPlainObject(raw)) throw new V3RevisionIntegrityError(`${label} must be an object`);
3773
+ const out = /* @__PURE__ */ new Map();
3774
+ for (const [selector, value] of Object.entries(raw)) {
3775
+ if (!isPlainObject(value) || typeof value.larkAppId !== "string" || value.larkAppId.length === 0 || typeof value.cliId !== "string" || typeof value.workingDir !== "string") {
3776
+ throw new V3RevisionIntegrityError(`${label}[${JSON.stringify(selector)}] is not a bot snapshot`);
3777
+ }
3778
+ out.set(selector, value);
3779
+ }
3780
+ return out;
3781
+ }
3782
+ function readVerifiedV3Revision(runDir, record, base) {
3783
+ const paths = v3RevisionFilePaths(runDir, record.revision);
3784
+ const label = `dag-revisions/${v3RevisionStem(record.revision)}`;
3785
+ const dag = parseRevisionDag(readPinnedFile(paths.dag, record.sha256, `${label}.json`), base, `${label}.json`);
3786
+ readPinnedFile(paths.patch, record.patchSha256, `${label}.patch.json`);
3787
+ let bots = /* @__PURE__ */ new Map();
3788
+ if (record.botsSha256) {
3789
+ const bytes = readPinnedFile(paths.bots, record.botsSha256, `${label}.bots.json`);
3790
+ let raw;
3791
+ try {
3792
+ raw = JSON.parse(bytes.toString("utf-8"));
3793
+ } catch {
3794
+ throw new V3RevisionIntegrityError(`${label}.bots.json is not JSON`);
3795
+ }
3796
+ bots = parseRevisionBotSnapshots(raw, `${label}.bots.json`);
3797
+ }
3798
+ return { dag, bots };
3799
+ }
3800
+ function loadEffectiveV3Dag(runDir, base, events) {
3801
+ const history = foldV3Revisions(events);
3802
+ const botAdditions = /* @__PURE__ */ new Map();
3803
+ if (history.applied.length === 0) return { dag: base, revision: 1, history, botAdditions };
3804
+ let dag = base;
3805
+ for (const record of history.applied) {
3806
+ const verified = readVerifiedV3Revision(runDir, record, base);
3807
+ dag = verified.dag;
3808
+ for (const [selector, snapshot] of verified.bots) botAdditions.set(selector, snapshot);
3809
+ }
3810
+ return { dag, revision: history.current, history, botAdditions };
3811
+ }
3812
+ function snapshotForSelector(snapshots, selector) {
3813
+ const key = selector ?? "";
3814
+ const exact = snapshots.get(key);
3815
+ if (exact) return exact;
3816
+ if (!key) return void 0;
3817
+ for (const snapshot of snapshots.values()) if (snapshot.larkAppId === key) return snapshot;
3818
+ return void 0;
3819
+ }
3820
+ function latestBlockOf(events, snap, nodeId) {
3821
+ const key = snap.nodes.get(nodeId)?.effectiveInstanceId ?? nodeId;
3822
+ for (let i = events.length - 1; i >= 0; i--) {
3823
+ const event = events[i];
3824
+ if (event.type === "hostEffectUncertain" && (event.instanceId ?? event.nodeId) === key) return { kind: "hostUncertain" };
3825
+ if (event.type === "nodeBlocked" && (event.instanceId ?? event.nodeId) === key) {
3826
+ return {
3827
+ kind: "nodeBlocked",
3828
+ errorClass: event.errorClass,
3829
+ ...event.errorCode ? { errorCode: event.errorCode } : {},
3830
+ reviseRequired: event.recovery === "reviseWorkflow"
3831
+ };
3832
+ }
3833
+ }
3834
+ return void 0;
3835
+ }
3836
+ function v3ApprovedBotScope(frozen, botAdditions) {
3837
+ const scope = new Map(frozen);
3838
+ for (const [selector, snapshot] of botAdditions) if (!scope.has(selector)) scope.set(selector, snapshot);
3839
+ return scope;
3840
+ }
3841
+ function v3NodeHeldByProposal(pendingTouched, nodeId, loopId) {
3842
+ if (!pendingTouched || pendingTouched.size === 0) return false;
3843
+ return pendingTouched.has(nodeId) || loopId !== void 0 && pendingTouched.has(loopId);
3844
+ }
3845
+ function v3RevisionFollowUps(events, input) {
3846
+ const out = [];
3847
+ const provenance = { by: input.by, ...input.auto ? { auto: input.auto } : {} };
3848
+ const snap = materialize([...events]);
3849
+ for (const item of input.assign ?? []) {
3850
+ if (snap.nodes.get(item.nodeId)?.status === "cancelled") continue;
3851
+ if (input.liveNodeIds && !input.liveNodeIds.has(item.nodeId)) continue;
3852
+ out.push({
3853
+ type: "nodeReassigned",
3854
+ nodeId: item.nodeId,
3855
+ ...snap.nodes.get(item.nodeId)?.effectiveInstanceId ? { instanceId: snap.nodes.get(item.nodeId).effectiveInstanceId } : {},
3856
+ from: item.from ?? snap.assignments.get(item.nodeId) ?? "",
3857
+ to: item.to,
3858
+ ...provenance
3859
+ });
3860
+ }
3861
+ for (const item of input.retry ?? []) {
3862
+ const state = snap.nodes.get(item.nodeId);
3863
+ if (state?.status !== "blocked" || state.effectiveInstanceId !== item.instanceId) continue;
3864
+ const key = item.instanceId ?? item.nodeId;
3865
+ const previousAttemptId = latestAttemptIdFor([...events], key);
3866
+ if (!previousAttemptId) continue;
3867
+ const blocked = latestBlockOf(events, snap, item.nodeId);
3868
+ out.push({
3869
+ type: "nodeRetryRequested",
3870
+ nodeId: item.nodeId,
3871
+ ...item.instanceId ? { instanceId: item.instanceId } : {},
3872
+ previousAttemptId,
3873
+ nextAttemptId: nextAttemptIdFor([...events], key),
3874
+ reason: "blockedRetry",
3875
+ ...blocked?.kind === "nodeBlocked" && blocked.errorClass ? { previousErrorClass: blocked.errorClass } : {},
3876
+ ...blocked?.kind === "nodeBlocked" && blocked.errorCode ? { previousErrorCode: blocked.errorCode } : {},
3877
+ ...provenance
3878
+ });
3879
+ }
3880
+ return out;
3881
+ }
3882
+ function missingV3RevisionFollowUps(events, liveNodeIds) {
3883
+ if (!events.some((event) => event.type === "dagRevised" && (event.effects?.retry || event.effects?.assign))) return [];
3884
+ const snap = materialize([...events]);
3885
+ if (snap.runStatus !== "running" && snap.runStatus !== "blocked") return [];
3886
+ const missing = [];
3887
+ events.forEach((event, index) => {
3888
+ if (event.type !== "dagRevised" || !event.effects?.retry && !event.effects?.assign) return;
3889
+ const later = events.slice(index + 1);
3890
+ const assign = (event.effects.assign ?? []).filter((item) => !later.some((after) => after.type === "nodeReassigned" && after.nodeId === item.nodeId || after.type === "dagRevised" && after.effects?.assign?.some((other) => other.nodeId === item.nodeId)));
3891
+ const retry = (event.effects.retry ?? []).filter((item) => !later.some((after) => after.type === "nodeRetryRequested" && after.nodeId === item.nodeId));
3892
+ if (assign.length === 0 && retry.length === 0) return;
3893
+ missing.push(...v3RevisionFollowUps(events, {
3894
+ assign,
3895
+ retry,
3896
+ by: event.by,
3897
+ ...event.auto ? { auto: event.auto } : {},
3898
+ ...liveNodeIds ? { liveNodeIds } : {}
3899
+ }));
3900
+ });
3901
+ return missing;
3902
+ }
3903
+ function appendMissingV3RevisionFollowUps(journalPath, scope) {
3904
+ return withJournalMutationSync(journalPath, ({ events, append }) => {
3905
+ if (currentV3Revision(events) !== scope.revision) return { kind: "stale" };
3906
+ const missing = missingV3RevisionFollowUps(events, scope.liveNodeIds);
3907
+ for (const event of missing) append(event, { durable: true });
3908
+ return { kind: "appended", count: missing.length };
3909
+ });
3910
+ }
3911
+
3912
+ // src/workflows/v3/checkpoint-hold.ts
3913
+ function marked(dag, nodeId) {
3914
+ const node = dag.nodes.find((candidate) => candidate.id === nodeId);
3915
+ return !!node && isGoalNode(node) && node.checkpoint === true;
3916
+ }
3917
+ function v3CheckpointMarkedSince(runDir, authorizedDag, events) {
3918
+ const cache = /* @__PURE__ */ new Map();
3919
+ let revisions;
3920
+ const loadRevisions = () => {
3921
+ if (revisions !== void 0) return revisions;
3922
+ try {
3923
+ const positions = /* @__PURE__ */ new Map();
3924
+ events.forEach((event, index) => {
3925
+ if (event.type === "dagRevised") positions.set(event.revision, index + 1);
3926
+ });
3927
+ revisions = foldV3Revisions(events).applied.map((record) => ({
3928
+ dag: readVerifiedV3Revision(runDir, record, authorizedDag).dag,
3929
+ position: positions.get(record.revision) ?? 0
3930
+ }));
3931
+ } catch {
3932
+ revisions = null;
3933
+ }
3934
+ return revisions;
3935
+ };
3936
+ return (nodeId) => {
3937
+ if (cache.has(nodeId)) return cache.get(nodeId);
3938
+ let since = marked(authorizedDag, nodeId) ? 0 : void 0;
3939
+ if (events.some((event) => event.type === "dagRevised")) {
3940
+ const list = loadRevisions();
3941
+ if (list === null) since = void 0;
3942
+ else {
3943
+ for (const revision of list) {
3944
+ if (!marked(revision.dag, nodeId)) since = void 0;
3945
+ else if (since === void 0) since = revision.position;
3946
+ }
3947
+ }
3948
+ }
3949
+ cache.set(nodeId, since);
3950
+ return since;
3951
+ };
3952
+ }
3953
+ function v3CheckpointSuccesses(input) {
3954
+ const events = [...input.events];
3955
+ const snap = materialize(events);
3956
+ const live = snap.runStatus === "running" || snap.runStatus === "blocked";
3957
+ const releases = /* @__PURE__ */ new Map();
3958
+ for (const event of events) {
3959
+ if (event.type === "checkpointReleased" && !releases.has(event.attemptId)) {
3960
+ releases.set(event.attemptId, { reason: event.reason, at: event.ts });
3961
+ }
3962
+ }
3963
+ const out = [];
3964
+ for (const node of input.dag.nodes) {
3965
+ if (!isGoalNode(node) || node.checkpoint !== true) continue;
3966
+ const state = snap.nodes.get(node.id);
3967
+ if (state?.status !== "done") continue;
3968
+ const key = state.effectiveInstanceId ?? node.id;
3969
+ const attemptId = latestAttemptIdFor(events, key);
3970
+ if (!attemptId) continue;
3971
+ let index = -1;
3972
+ for (let i = events.length - 1; i >= 0; i--) {
3973
+ const event = events[i];
3974
+ if (event.type === "nodeSucceeded" && event.attemptId === attemptId && (event.instanceId ?? event.nodeId) === key) {
3975
+ index = i;
3976
+ break;
3977
+ }
3978
+ }
3979
+ if (index < 0) continue;
3980
+ const since = input.markedSince(node.id);
3981
+ if (since === void 0 || index < since) continue;
3982
+ const released = releases.get(attemptId);
3983
+ if (!released && !live) continue;
3984
+ out.push({
3985
+ nodeId: node.id,
3986
+ ...state.effectiveInstanceId ? { instanceId: state.effectiveInstanceId } : {},
3987
+ attemptId,
3988
+ eventSeq: index + 1,
3989
+ at: events[index].ts,
3990
+ ...released ? { released } : {}
3991
+ });
3992
+ }
3993
+ return out;
3994
+ }
3995
+ function v3CheckpointHeldNodes(dag, successes) {
3996
+ const unreleased = new Set(successes.filter((success) => !success.released).map((success) => success.nodeId));
3997
+ const held = /* @__PURE__ */ new Set();
3998
+ if (unreleased.size === 0) return held;
3999
+ for (const node of dag.nodes) {
4000
+ if (node.depends.some((dep) => unreleased.has(dep.from))) held.add(node.id);
4001
+ }
4002
+ return held;
4003
+ }
4004
+
3319
4005
  // src/workflows/v3/gate-wait-store.ts
3320
- var import_node_crypto4 = require("node:crypto");
3321
- var import_node_fs9 = require("node:fs");
3322
- var import_node_path7 = require("node:path");
4006
+ var import_node_crypto5 = require("node:crypto");
4007
+ var import_node_fs11 = require("node:fs");
4008
+ var import_node_path9 = require("node:path");
3323
4009
  function waitsDir(runDir) {
3324
- return (0, import_node_path7.join)(runDir, "waits");
4010
+ return (0, import_node_path9.join)(runDir, "waits");
3325
4011
  }
3326
4012
  function waitPath(runDir, waitId) {
3327
- return (0, import_node_path7.join)(waitsDir(runDir), `${waitId}.json`);
4013
+ return (0, import_node_path9.join)(waitsDir(runDir), `${waitId}.json`);
3328
4014
  }
3329
4015
  function v3GateWaitId(nodeId, instanceId, hostApproval) {
3330
4016
  const base = instanceId ?? nodeId;
@@ -3336,56 +4022,56 @@ function v3GateWaitId(nodeId, instanceId, hostApproval) {
3336
4022
  return `${base}-host-${attempt}-gate`;
3337
4023
  }
3338
4024
  function atomicWriteJson(path, value) {
3339
- const dir = (0, import_node_path7.dirname)(path);
3340
- const runDir = (0, import_node_path7.dirname)(dir);
4025
+ const dir = (0, import_node_path9.dirname)(path);
4026
+ const runDir = (0, import_node_path9.dirname)(dir);
3341
4027
  assertRealDirectory(runDir, "run directory");
3342
- if (!(0, import_node_fs9.existsSync)(dir)) {
4028
+ if (!(0, import_node_fs11.existsSync)(dir)) {
3343
4029
  try {
3344
- (0, import_node_fs9.mkdirSync)(dir, { mode: 448 });
4030
+ (0, import_node_fs11.mkdirSync)(dir, { mode: 448 });
3345
4031
  } catch {
3346
4032
  }
3347
4033
  }
3348
4034
  assertRealDirectory(dir, "waits directory");
3349
- (0, import_node_fs9.chmodSync)(dir, 448);
3350
- if ((0, import_node_fs9.existsSync)(path)) assertPrivateRegularWaitFile(path, false);
3351
- const tmp = `${path}.${process.pid}.${(0, import_node_crypto4.randomBytes)(4).toString("hex")}.tmp`;
4035
+ (0, import_node_fs11.chmodSync)(dir, 448);
4036
+ if ((0, import_node_fs11.existsSync)(path)) assertPrivateRegularWaitFile(path, false);
4037
+ const tmp = `${path}.${process.pid}.${(0, import_node_crypto5.randomBytes)(4).toString("hex")}.tmp`;
3352
4038
  let fd;
3353
4039
  try {
3354
- fd = (0, import_node_fs9.openSync)(
4040
+ fd = (0, import_node_fs11.openSync)(
3355
4041
  tmp,
3356
- import_node_fs9.constants.O_WRONLY | import_node_fs9.constants.O_CREAT | import_node_fs9.constants.O_EXCL | (import_node_fs9.constants.O_NOFOLLOW ?? 0),
4042
+ import_node_fs11.constants.O_WRONLY | import_node_fs11.constants.O_CREAT | import_node_fs11.constants.O_EXCL | (import_node_fs11.constants.O_NOFOLLOW ?? 0),
3357
4043
  384
3358
4044
  );
3359
- (0, import_node_fs9.writeFileSync)(fd, JSON.stringify(value, null, 2), "utf-8");
3360
- (0, import_node_fs9.fsyncSync)(fd);
3361
- (0, import_node_fs9.closeSync)(fd);
4045
+ (0, import_node_fs11.writeFileSync)(fd, JSON.stringify(value, null, 2), "utf-8");
4046
+ (0, import_node_fs11.fsyncSync)(fd);
4047
+ (0, import_node_fs11.closeSync)(fd);
3362
4048
  fd = void 0;
3363
- (0, import_node_fs9.renameSync)(tmp, path);
4049
+ (0, import_node_fs11.renameSync)(tmp, path);
3364
4050
  } catch (err) {
3365
4051
  if (fd !== void 0) {
3366
4052
  try {
3367
- (0, import_node_fs9.closeSync)(fd);
4053
+ (0, import_node_fs11.closeSync)(fd);
3368
4054
  } catch {
3369
4055
  }
3370
4056
  }
3371
4057
  try {
3372
- (0, import_node_fs9.unlinkSync)(tmp);
4058
+ (0, import_node_fs11.unlinkSync)(tmp);
3373
4059
  } catch {
3374
4060
  }
3375
4061
  throw err;
3376
4062
  }
3377
- (0, import_node_fs9.chmodSync)(path, 384);
4063
+ (0, import_node_fs11.chmodSync)(path, 384);
3378
4064
  fsyncRegularFileSync(path);
3379
4065
  fsyncDirectorySyncPortable(dir);
3380
4066
  }
3381
4067
  function assertRealDirectory(path, label) {
3382
- const stat = (0, import_node_fs9.lstatSync)(path);
4068
+ const stat = (0, import_node_fs11.lstatSync)(path);
3383
4069
  if (!stat.isDirectory() || stat.isSymbolicLink()) {
3384
4070
  throw new Error(`v3 human-gate: ${label} must be a real directory`);
3385
4071
  }
3386
4072
  }
3387
4073
  function assertPrivateRegularWaitFile(path, requirePrivate = true) {
3388
- const stat = (0, import_node_fs9.lstatSync)(path);
4074
+ const stat = (0, import_node_fs11.lstatSync)(path);
3389
4075
  if (!stat.isFile() || stat.isSymbolicLink()) {
3390
4076
  throw new Error("v3 human-gate: wait file must be a regular file");
3391
4077
  }
@@ -3499,6 +4185,7 @@ function openV3WorkerAttempts(events) {
3499
4185
  const opened = /* @__PURE__ */ new Map();
3500
4186
  const closed = /* @__PURE__ */ new Set();
3501
4187
  for (const event of events) {
4188
+ if (event.type === "nodeDispatched" && event.nodeExecutor === "bot") continue;
3502
4189
  if (isAttemptOpened(event)) {
3503
4190
  const prior = opened.get(event.attemptId);
3504
4191
  if (prior && (prior.nodeId !== event.nodeId || prior.instanceId !== event.instanceId)) {
@@ -3606,15 +4293,558 @@ function sameIdentity(left, right) {
3606
4293
  return left.nodeId === right.nodeId && left.instanceId === right.instanceId && left.attemptId === right.attemptId && left.executor === right.executor && left.provider === right.provider && left.inputRef.path === right.inputRef.path && left.inputRef.sha256 === right.inputRef.sha256 && left.inputRef.bytes === right.inputRef.bytes && left.inputHash === right.inputHash && left.idempotencyKey === right.idempotencyKey && left.idempotencyTtlMs === right.idempotencyTtlMs && left.approvalDigest === right.approvalDigest && left.attemptedAtMs === right.ts;
3607
4294
  }
3608
4295
 
3609
- // src/workflows/v3/host-execution.ts
3610
- var import_node_crypto6 = require("node:crypto");
3611
- var import_node_fs10 = require("node:fs");
3612
- var import_node_path8 = require("node:path");
3613
-
3614
- // src/workflows/shared/idempotency-key.ts
3615
- var import_node_crypto5 = require("node:crypto");
3616
- function deriveIdempotencyKey(tuple, opts = {}) {
3617
- const namespace = opts.namespace ?? "wf_";
4296
+ // src/workflows/v3/external-attempt-ledger.ts
4297
+ function closureOf(event) {
4298
+ switch (event.type) {
4299
+ case "nodeSucceeded":
4300
+ return { kind: "succeeded" };
4301
+ case "nodeBlocked":
4302
+ return { kind: "blocked", ...event.errorCode ? { errorCode: event.errorCode } : {} };
4303
+ case "nodeFailed":
4304
+ return { kind: "failed", ...event.errorCode ? { errorCode: event.errorCode } : {} };
4305
+ case "nodeCancelled":
4306
+ return event.reason === "runCancelled" && event.attemptId ? { kind: "cancelled" } : void 0;
4307
+ case "nodeAttemptDrained":
4308
+ return { kind: "drained", reason: event.reason };
4309
+ case "nodeReassigned":
4310
+ return event.attemptId ? { kind: "reassigned", to: event.to } : void 0;
4311
+ default:
4312
+ return void 0;
4313
+ }
4314
+ }
4315
+ function attemptIdOf(event) {
4316
+ return "attemptId" in event && typeof event.attemptId === "string" ? event.attemptId : void 0;
4317
+ }
4318
+ function identityOf(event) {
4319
+ const record = event;
4320
+ return {
4321
+ ...typeof record.nodeId === "string" ? { nodeId: record.nodeId } : {},
4322
+ ...typeof record.instanceId === "string" ? { instanceId: record.instanceId } : {}
4323
+ };
4324
+ }
4325
+ function foldV3ExternalAttempts(events) {
4326
+ const attempts = /* @__PURE__ */ new Map();
4327
+ for (const event of events) {
4328
+ if (event.type === "nodeDispatched") {
4329
+ if (event.nodeExecutor !== "bot") continue;
4330
+ const prior = attempts.get(event.attemptId);
4331
+ if (prior) {
4332
+ if (prior.nodeId !== event.nodeId || prior.instanceId !== event.instanceId) {
4333
+ throw new Error(`v3 external ledger: identity changed for ${event.attemptId}`);
4334
+ }
4335
+ continue;
4336
+ }
4337
+ attempts.set(event.attemptId, {
4338
+ nodeId: event.nodeId,
4339
+ ...event.instanceId ? { instanceId: event.instanceId } : {},
4340
+ attemptId: event.attemptId,
4341
+ ...event.loop ? { loop: event.loop } : {},
4342
+ phase: "dispatched"
4343
+ });
4344
+ continue;
4345
+ }
4346
+ const attemptId = attemptIdOf(event);
4347
+ if (!attemptId) continue;
4348
+ const attempt = attempts.get(attemptId);
4349
+ if (!attempt) continue;
4350
+ if (attempt.closed) continue;
4351
+ const identity = identityOf(event);
4352
+ if (identity.nodeId !== attempt.nodeId || identity.instanceId !== attempt.instanceId) {
4353
+ throw new Error(`v3 external ledger: event identity changed for ${attemptId}`);
4354
+ }
4355
+ switch (event.type) {
4356
+ case "nodeHandoffPrepared":
4357
+ if (attempt.phase !== "dispatched") break;
4358
+ attempt.phase = "prepared";
4359
+ attempt.targetLarkAppId = event.targetLarkAppId;
4360
+ attempt.placement = event.placement;
4361
+ attempt.revision = event.revision;
4362
+ attempt.idempotency = event.idempotency;
4363
+ attempt.preparedAt = event.preparedAt;
4364
+ break;
4365
+ case "nodeHandoffSent":
4366
+ if (attempt.phase === "dispatched" || attempt.handoffMessageId !== void 0) break;
4367
+ if (attempt.phase === "prepared") attempt.phase = "sent";
4368
+ attempt.handoffMessageId = event.handoffMessageId;
4369
+ if (event.nodeCardMessageId) attempt.nodeCardMessageId = event.nodeCardMessageId;
4370
+ if (event.threadId) attempt.threadId = event.threadId;
4371
+ attempt.sentAt = event.sentAt;
4372
+ attempt.acceptDeadlineAt = event.acceptDeadlineAt;
4373
+ break;
4374
+ case "nodeHandoffAccepted":
4375
+ if (attempt.phase !== "prepared" && attempt.phase !== "sent") break;
4376
+ attempt.phase = "accepted";
4377
+ attempt.sessionId = event.sessionId;
4378
+ attempt.acceptedAt = event.acceptedAt;
4379
+ attempt.deadlineAt = event.deadlineAt;
4380
+ if (event.startPending) attempt.startPending = true;
4381
+ break;
4382
+ case "nodeHandoffStarted":
4383
+ if (attempt.phase !== "accepted" || !attempt.startPending) break;
4384
+ delete attempt.startPending;
4385
+ attempt.startedAt = event.startedAt;
4386
+ attempt.deadlineAt = event.deadlineAt;
4387
+ break;
4388
+ case "nodeStalled":
4389
+ attempt.stalledAt = event.at;
4390
+ break;
4391
+ case "nodeHandedToHuman":
4392
+ attempt.handedToHuman = { reason: event.reason };
4393
+ break;
4394
+ default: {
4395
+ const closure = closureOf(event);
4396
+ if (closure) attempt.closed = closure;
4397
+ }
4398
+ }
4399
+ }
4400
+ return attempts;
4401
+ }
4402
+ function openV3ExternalAttempts(events) {
4403
+ return [...foldV3ExternalAttempts(events).values()].filter((attempt) => !attempt.closed);
4404
+ }
4405
+
4406
+ // src/workflows/v3/external-executor.ts
4407
+ var V3_EXTERNAL_ERROR_CODES = {
4408
+ BOT_NOT_IN_CHAT: "BOT_NOT_IN_CHAT",
4409
+ BOT_OFFLINE: "BOT_OFFLINE",
4410
+ COLLABORATION_DENIED: "COLLABORATION_DENIED",
4411
+ IDENTITY_UNAVAILABLE: "IDENTITY_UNAVAILABLE",
4412
+ HANDOFF_NOT_ACCEPTED: "HANDOFF_NOT_ACCEPTED",
4413
+ HANDOFF_UNCERTAIN: "HANDOFF_UNCERTAIN",
4414
+ BOT_REPORTED_BLOCKED: "BOT_REPORTED_BLOCKED",
4415
+ /** The executing bot's daemon would not start the node session in the
4416
+ * run's Gate-2 working dir: not inside its configured range (design §15.4). */
4417
+ WORKING_DIR_REFUSED: "WORKING_DIR_REFUSED",
4418
+ /** Main-topic placement (design §5.5): the executing bot's session in the
4419
+ * main topic is executing another workflow node right now (another run in
4420
+ * the same topic); one session cannot carry two nodes at once. */
4421
+ MAIN_TOPIC_BUSY: "MAIN_TOPIC_BUSY",
4422
+ NODE_TIMEOUT: "NODE_TIMEOUT",
4423
+ /** The main bot (or the initiator) handed the node to a human (§13.6). */
4424
+ HANDED_TO_HUMAN: "HANDED_TO_HUMAN"
4425
+ };
4426
+ var V3_HANDOFF_IDEMPOTENCY_WINDOW_MS = 50 * 60 * 1e3;
4427
+ var V3_EXTERNAL_CALL_TIMEOUT_MS = 3e4;
4428
+ var V3_MAIN_TOPIC_QUEUE_GRACE_MIN_MS = 60 * 60 * 1e3;
4429
+ function v3MainTopicQueueGraceMs(nodeTimeoutMs2) {
4430
+ return Math.max(nodeTimeoutMs2, V3_MAIN_TOPIC_QUEUE_GRACE_MIN_MS);
4431
+ }
4432
+ var V3_DEFAULT_HANDOFF_ACCEPT_TIMEOUT_MS = 15e3;
4433
+
4434
+ // src/workflows/v3/external-control.ts
4435
+ var import_node_crypto6 = require("node:crypto");
4436
+ var import_node_fs13 = require("node:fs");
4437
+ var import_node_path11 = require("node:path");
4438
+
4439
+ // src/workflows/v3/result-contract.ts
4440
+ var import_node_fs12 = require("node:fs");
4441
+ var import_node_path10 = require("node:path");
4442
+ function readRevisitRequest(manifest, outputDir) {
4443
+ const entry = manifest.files.find((f) => f.path === "result.json");
4444
+ if (!entry) return { ok: true };
4445
+ let value;
4446
+ try {
4447
+ value = JSON.parse((0, import_node_fs12.readFileSync)((0, import_node_path10.join)(outputDir, entry.path), "utf-8"));
4448
+ } catch {
4449
+ return { ok: true };
4450
+ }
4451
+ if (!value || typeof value !== "object" || value.status !== "revisit") {
4452
+ return { ok: true };
4453
+ }
4454
+ const v = value;
4455
+ const problems = [];
4456
+ if (typeof v.revisitTo !== "string" || v.revisitTo.trim() === "") {
4457
+ problems.push('result.json status "revisit" requires a non-empty string "revisitTo"');
4458
+ }
4459
+ if (v.reason !== void 0 && typeof v.reason !== "string") {
4460
+ problems.push('result.json "reason" must be a string when present');
4461
+ }
4462
+ if (problems.length > 0) return { ok: false, problems };
4463
+ return {
4464
+ ok: true,
4465
+ request: {
4466
+ toNodeId: v.revisitTo,
4467
+ ...typeof v.reason === "string" && v.reason ? { reason: v.reason } : {}
4468
+ }
4469
+ };
4470
+ }
4471
+ function validateResult(filePath, schema) {
4472
+ if (!(0, import_node_fs12.existsSync)(filePath)) return { ok: false, problems: [`result.json not found at ${filePath}`] };
4473
+ let parsed;
4474
+ try {
4475
+ parsed = JSON.parse((0, import_node_fs12.readFileSync)(filePath, "utf-8"));
4476
+ } catch (err) {
4477
+ return { ok: false, problems: [`result.json is not valid JSON: ${err instanceof Error ? err.message : String(err)}`] };
4478
+ }
4479
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
4480
+ return { ok: false, problems: ["result.json root must be a JSON object"] };
4481
+ }
4482
+ const obj = parsed;
4483
+ const problems = [];
4484
+ for (const field of schema.required ?? []) {
4485
+ if (!(field in obj)) problems.push(`missing required field "${field}"`);
4486
+ }
4487
+ for (const [name, spec] of Object.entries(schema.properties)) {
4488
+ if (!(name in obj)) continue;
4489
+ const v = obj[name];
4490
+ const okType = spec.type === "string" ? typeof v === "string" : spec.type === "number" ? typeof v === "number" && Number.isFinite(v) : spec.type === "boolean" ? typeof v === "boolean" : spec.type === "array" ? Array.isArray(v) : typeof v === "object" && v !== null && !Array.isArray(v);
4491
+ if (!okType) {
4492
+ problems.push(`field "${name}" must be of type ${spec.type}`);
4493
+ continue;
4494
+ }
4495
+ if (spec.type === "string" && spec.enum && !spec.enum.includes(v)) {
4496
+ problems.push(`field "${name}" must be one of [${spec.enum.join(", ")}] (got ${JSON.stringify(v)})`);
4497
+ }
4498
+ }
4499
+ return problems.length > 0 ? { ok: false, problems } : { ok: true };
4500
+ }
4501
+
4502
+ // src/workflows/v3/external-control.ts
4503
+ function classifyExternalAttempt(events, attemptId, expect = {}) {
4504
+ const attempt = foldV3ExternalAttempts(events).get(attemptId);
4505
+ if (!attempt) return { ok: false, reason: "unknown-attempt" };
4506
+ if (expect.instanceId !== void 0 && expect.instanceId !== attempt.instanceId) {
4507
+ return { ok: false, reason: "unknown-attempt" };
4508
+ }
4509
+ const snap = materialize([...events]);
4510
+ const newerAttempt = snap.attempts.get(attempt.instanceId ?? attempt.nodeId) !== attemptId;
4511
+ const state = attempt.instanceId ? snap.instances.get(attempt.instanceId) : snap.nodes.get(attempt.nodeId);
4512
+ const closed = attempt.closed;
4513
+ if (closed) {
4514
+ const reason = closed.kind === "succeeded" ? "already-succeeded" : closed.kind === "reassigned" ? "reassigned" : closed.kind === "cancelled" ? "cancelled" : closed.kind === "blocked" && closed.errorCode === V3_EXTERNAL_ERROR_CODES.NODE_TIMEOUT ? "timed-out" : closed.kind === "drained" && closed.reason === "runCancellation" ? "cancelled" : closed.kind === "drained" && closed.reason === "obsoleteAttempt" ? state?.status === "cancelled" ? "cancelled" : "superseded" : newerAttempt ? "retried" : "closed";
4515
+ return { ok: false, reason, attempt };
4516
+ }
4517
+ if (snap.cancelRequestId !== void 0) return { ok: false, reason: "cancelled", attempt };
4518
+ if (snap.runStatus !== "running") return { ok: false, reason: "run-not-running", attempt };
4519
+ if (state?.status === "superseded") return { ok: false, reason: "superseded", attempt };
4520
+ if (state?.status === "cancelled") return { ok: false, reason: "cancelled", attempt };
4521
+ if (newerAttempt) return { ok: false, reason: "retried", attempt };
4522
+ if (state?.status !== "running") return { ok: false, reason: "closed", attempt };
4523
+ if (expect.sessionId !== void 0 && attempt.sessionId !== void 0 && expect.sessionId !== attempt.sessionId) {
4524
+ return { ok: false, reason: "session-mismatch", attempt };
4525
+ }
4526
+ if (attempt.handedToHuman && expect.sessionId !== void 0) {
4527
+ return { ok: false, reason: "handed-to-human", attempt };
4528
+ }
4529
+ return { ok: true, attempt, snap };
4530
+ }
4531
+ function externalAttemptRef(runId, attempt) {
4532
+ if (!attempt.targetLarkAppId || !attempt.placement || attempt.revision === void 0) {
4533
+ return void 0;
4534
+ }
4535
+ return {
4536
+ runId,
4537
+ nodeId: attempt.nodeId,
4538
+ ...attempt.instanceId ? { instanceId: attempt.instanceId } : {},
4539
+ attemptId: attempt.attemptId,
4540
+ revision: attempt.revision,
4541
+ targetLarkAppId: attempt.targetLarkAppId,
4542
+ placement: attempt.placement
4543
+ };
4544
+ }
4545
+ async function withExternalCallTimeout(call, timeoutMs, label) {
4546
+ let timer;
4547
+ try {
4548
+ return await Promise.race([
4549
+ call,
4550
+ new Promise((_resolve, reject) => {
4551
+ timer = setTimeout(() => reject(new Error(`external ${label} did not settle within ${timeoutMs}ms`)), timeoutMs);
4552
+ timer.unref?.();
4553
+ })
4554
+ ]);
4555
+ } finally {
4556
+ if (timer) clearTimeout(timer);
4557
+ }
4558
+ }
4559
+ async function stopExternalAttempt(executor, runId, attempt, reason, timeoutMs = V3_EXTERNAL_CALL_TIMEOUT_MS) {
4560
+ const ref = externalAttemptRef(runId, attempt);
4561
+ if (!executor || !ref) return { ok: true };
4562
+ try {
4563
+ await withExternalCallTimeout(Promise.resolve().then(() => executor.stop({
4564
+ ...ref,
4565
+ reason,
4566
+ ...attempt.idempotency ? { idempotency: attempt.idempotency } : {},
4567
+ ...attempt.handoffMessageId ? { handoffMessageId: attempt.handoffMessageId } : {},
4568
+ ...attempt.nodeCardMessageId ? { nodeCardMessageId: attempt.nodeCardMessageId } : {},
4569
+ ...attempt.threadId ? { threadId: attempt.threadId } : {},
4570
+ ...attempt.sessionId ? { sessionId: attempt.sessionId } : {}
4571
+ })), timeoutMs, "stop");
4572
+ return { ok: true };
4573
+ } catch (error) {
4574
+ return { ok: false, error };
4575
+ }
4576
+ }
4577
+ function journalPathOf(runDir) {
4578
+ return (0, import_node_path11.join)(runDir, "journal.ndjson");
4579
+ }
4580
+ function externalAttemptNode(dag, attempt) {
4581
+ if (!attempt.loop) return dag.nodes.find((node) => node.id === attempt.nodeId);
4582
+ const loopNode = dag.nodes.find((node) => node.id === attempt.loop.loopId);
4583
+ if (!loopNode || !isLoopNode(loopNode)) return void 0;
4584
+ const body = loopNode.body.nodes.find((node) => node.id === attempt.loop.bodyNodeId);
4585
+ if (!body) return void 0;
4586
+ const ref = attempt.loop;
4587
+ return {
4588
+ ...body,
4589
+ id: loopInstanceId(ref.loopId, ref.iteration, ref.bodyNodeId),
4590
+ bot: body.bot ?? loopNode.bot,
4591
+ depends: body.depends.map((d) => ({ from: loopInstanceId(ref.loopId, ref.iteration, d.from) })),
4592
+ inputs: body.inputs.map((r) => ({
4593
+ from: loopInstanceId(ref.loopId, ref.iteration, r.from),
4594
+ ...r.select ? { select: r.select } : {},
4595
+ ...r.output !== void 0 ? { output: r.output } : {}
4596
+ }))
4597
+ };
4598
+ }
4599
+ function nodeTimeoutMs(node) {
4600
+ return (node?.timeoutSec ?? DEFAULT_NODE_TIMEOUT_SEC) * 1e3;
4601
+ }
4602
+ function safeExternalAttemptDir(runDir, attemptId) {
4603
+ const root = (0, import_node_path11.resolve)(runDir);
4604
+ const candidate = (0, import_node_path11.resolve)(root, attemptId);
4605
+ const rel = (0, import_node_path11.relative)(root, candidate);
4606
+ if (!rel || (0, import_node_path11.isAbsolute)(rel) || rel === ".." || rel.startsWith(`..${import_node_path11.sep}`)) {
4607
+ throw new Error(`v3 external: attempt path escapes run dir (${attemptId})`);
4608
+ }
4609
+ return candidate;
4610
+ }
4611
+ function appendAcceptedLocked(events, append, dag, input, now) {
4612
+ const current = classifyExternalAttempt(events, input.attemptId, {
4613
+ ...input.instanceId !== void 0 ? { instanceId: input.instanceId } : {},
4614
+ sessionId: input.sessionId
4615
+ });
4616
+ if (!current.ok) return { ok: false, reason: current.reason };
4617
+ const { attempt } = current;
4618
+ const queued = attempt.placement === "main";
4619
+ const startedAt = queued && input.startedAt !== void 0 && Number.isFinite(input.startedAt) ? Math.min(input.startedAt, now) : void 0;
4620
+ if (attempt.phase === "accepted") {
4621
+ if (attempt.startPending && startedAt !== void 0) {
4622
+ const started = Math.max(startedAt, attempt.acceptedAt ?? startedAt);
4623
+ const deadlineAt2 = started + nodeTimeoutMs(externalAttemptNode(dag, attempt));
4624
+ append({
4625
+ type: "nodeHandoffStarted",
4626
+ nodeId: attempt.nodeId,
4627
+ ...attempt.instanceId ? { instanceId: attempt.instanceId } : {},
4628
+ attemptId: attempt.attemptId,
4629
+ startedAt: started,
4630
+ deadlineAt: deadlineAt2
4631
+ }, { durable: true });
4632
+ return { ok: true, deadlineAt: deadlineAt2, alreadyAccepted: false };
4633
+ }
4634
+ return { ok: true, deadlineAt: attempt.deadlineAt, alreadyAccepted: true };
4635
+ }
4636
+ if (attempt.phase === "dispatched") return { ok: false, reason: "unknown-attempt" };
4637
+ const acceptedAt = Math.min(input.acceptedAt ?? now, now);
4638
+ const timeoutMs = nodeTimeoutMs(externalAttemptNode(dag, attempt));
4639
+ const pending = queued && startedAt === void 0;
4640
+ const countFrom = startedAt !== void 0 ? Math.max(startedAt, acceptedAt) : acceptedAt;
4641
+ const deadlineAt = countFrom + timeoutMs + (pending ? v3MainTopicQueueGraceMs(timeoutMs) : 0);
4642
+ append({
4643
+ type: "nodeHandoffAccepted",
4644
+ nodeId: attempt.nodeId,
4645
+ ...attempt.instanceId ? { instanceId: attempt.instanceId } : {},
4646
+ attemptId: attempt.attemptId,
4647
+ sessionId: input.sessionId,
4648
+ acceptedAt,
4649
+ deadlineAt,
4650
+ ...pending ? { startPending: true } : {}
4651
+ }, { durable: true });
4652
+ return { ok: true, deadlineAt, alreadyAccepted: false };
4653
+ }
4654
+ var RESULT_FILE = "result.json";
4655
+ function judgeStagedSubmission(node, manifest, workDir) {
4656
+ const revisit = readRevisitRequest(manifest, workDir);
4657
+ if (!revisit.ok) return { kind: "invalid", problems: revisit.problems };
4658
+ if (revisit.request) {
4659
+ if (!node.revisitTo?.includes(revisit.request.toNodeId)) {
4660
+ return {
4661
+ kind: "invalid",
4662
+ problems: [`result.json requests revisit to "${revisit.request.toNodeId}", not in node "${node.id}".revisitTo`]
4663
+ };
4664
+ }
4665
+ return { kind: "revisit", request: revisit.request };
4666
+ }
4667
+ if (node.resultSchema) {
4668
+ const entry = manifest.files.find((f) => f.path === RESULT_FILE);
4669
+ if (!entry) {
4670
+ return { kind: "invalid", problems: ["node declares resultSchema but the submission has no result.json"] };
4671
+ }
4672
+ const res = validateResult((0, import_node_path11.join)(workDir, entry.path), node.resultSchema);
4673
+ if (!res.ok) return { kind: "invalid", problems: res.problems ?? ["result.json failed schema validation"] };
4674
+ }
4675
+ const contract = validateManifestArtifactContract(node.outputs, manifest);
4676
+ if (!contract.ok) return { kind: "invalid", problems: contract.problems };
4677
+ return { kind: "success" };
4678
+ }
4679
+ function moveAsideIfPresent(path, tag) {
4680
+ if (!(0, import_node_fs13.existsSync)(path)) return;
4681
+ (0, import_node_fs13.renameSync)(path, `${path}.uncommitted-${tag}`);
4682
+ }
4683
+ var V3_SUBMISSION_INTENT_FILE = "submission.intent.json";
4684
+ function submissionIntentPath(attemptDir) {
4685
+ return (0, import_node_path11.join)(attemptDir, V3_SUBMISSION_INTENT_FILE);
4686
+ }
4687
+ function removeSubmissionIntent(attemptDir, stageTag) {
4688
+ (0, import_node_fs13.rmSync)(submissionIntentPath(attemptDir), { force: true });
4689
+ (0, import_node_fs13.rmSync)((0, import_node_path11.join)(attemptDir, "submissions", stageTag), { recursive: true, force: true });
4690
+ try {
4691
+ (0, import_node_fs13.rmdirSync)((0, import_node_path11.join)(attemptDir, "submissions"));
4692
+ } catch {
4693
+ }
4694
+ }
4695
+ function readSubmissionIntent(attemptDir) {
4696
+ const path = submissionIntentPath(attemptDir);
4697
+ if (!(0, import_node_fs13.existsSync)(path)) return void 0;
4698
+ try {
4699
+ const raw = JSON.parse((0, import_node_fs13.readFileSync)(path, "utf-8"));
4700
+ if (raw.schemaVersion !== 1 || typeof raw.attemptId !== "string" || typeof raw.stageTag !== "string") {
4701
+ return "corrupt";
4702
+ }
4703
+ if (!/^[0-9a-f-]{36}$/.test(raw.stageTag)) return "corrupt";
4704
+ return { schemaVersion: 1, attemptId: raw.attemptId, stageTag: raw.stageTag };
4705
+ } catch {
4706
+ return "corrupt";
4707
+ }
4708
+ }
4709
+ function verifyInstalledSubmissionSync(manifestPath, workDir) {
4710
+ let manifest;
4711
+ try {
4712
+ manifest = JSON.parse((0, import_node_fs13.readFileSync)(manifestPath, "utf-8"));
4713
+ } catch (err) {
4714
+ return { ok: false, problems: [`manifest unreadable: ${err instanceof Error ? err.message : String(err)}`] };
4715
+ }
4716
+ if (manifest?.schemaVersion !== MANIFEST_SCHEMA_VERSION || manifest.status !== "ok" || !Array.isArray(manifest.files) || manifest.files.length === 0) {
4717
+ return { ok: false, problems: ["manifest is not an engine-generated success manifest"] };
4718
+ }
4719
+ const root = (0, import_node_path11.resolve)(workDir);
4720
+ const problems = [];
4721
+ for (const file of manifest.files) {
4722
+ const target = (0, import_node_path11.resolve)(root, file.path);
4723
+ const rel = (0, import_node_path11.relative)(root, target);
4724
+ if (!rel || (0, import_node_path11.isAbsolute)(rel) || rel.startsWith("..")) {
4725
+ problems.push(`${file.path}: escapes the output directory`);
4726
+ continue;
4727
+ }
4728
+ try {
4729
+ if (!(0, import_node_fs13.lstatSync)(target).isFile()) {
4730
+ problems.push(`${file.path}: not a regular file`);
4731
+ continue;
4732
+ }
4733
+ const bytes = (0, import_node_fs13.readFileSync)(target);
4734
+ if (bytes.length !== file.bytes || (0, import_node_crypto6.createHash)("sha256").update(bytes).digest("hex") !== file.sha256) {
4735
+ problems.push(`${file.path}: bytes do not match the manifest`);
4736
+ }
4737
+ } catch (err) {
4738
+ problems.push(`${file.path}: ${err instanceof Error ? err.message : String(err)}`);
4739
+ }
4740
+ }
4741
+ return problems.length > 0 ? { ok: false, problems } : { ok: true, manifest };
4742
+ }
4743
+ function appendInstalledVerdictLocked(input) {
4744
+ const { attempt, append, events, verdict, manifestPath } = input;
4745
+ const identity = {
4746
+ nodeId: attempt.nodeId,
4747
+ ...attempt.instanceId ? { instanceId: attempt.instanceId } : {},
4748
+ attemptId: attempt.attemptId
4749
+ };
4750
+ if (verdict.kind === "revisit") {
4751
+ const budget = revisitBudgetStatus([...events], attempt.nodeId, verdict.request.toNodeId);
4752
+ if (!budget.ok) {
4753
+ append({
4754
+ type: "nodeBlocked",
4755
+ ...identity,
4756
+ errorClass: "resultInvalid",
4757
+ errorCode: "REVISIT_BUDGET_EXHAUSTED",
4758
+ message: budget.detail,
4759
+ revisitTo: verdict.request.toNodeId
4760
+ }, { durable: true });
4761
+ return { ok: true, kind: "blocked", errorCode: "REVISIT_BUDGET_EXHAUSTED" };
4762
+ }
4763
+ const plan = planV3Revisit({
4764
+ runDir: input.runDir,
4765
+ dag: input.dag,
4766
+ events,
4767
+ nodeId: attempt.nodeId,
4768
+ instanceId: attempt.instanceId ?? attempt.nodeId,
4769
+ attemptId: attempt.attemptId,
4770
+ request: verdict.request,
4771
+ sourceManifestPath: manifestPath
4772
+ });
4773
+ for (const event of plan.events) append(event);
4774
+ append({ type: "nodeAttemptDrained", ...identity, reason: "obsoleteAttempt" }, { durable: true });
4775
+ return { ok: true, kind: "revisit", toNodeId: verdict.request.toNodeId };
4776
+ }
4777
+ append({ type: "nodeSucceeded", ...identity, manifestPath }, { durable: true });
4778
+ return { ok: true, kind: "succeeded", manifestPath };
4779
+ }
4780
+ function installVerdictCommitted(events, attemptId) {
4781
+ return events.some((event) => event.type === "nodeSucceeded" && event.attemptId === attemptId || event.type === "nodeRevisitRequested" && event.attemptId === attemptId || event.type === "nodeBlocked" && event.attemptId === attemptId && event.errorCode === "REVISIT_BUDGET_EXHAUSTED");
4782
+ }
4783
+ function settleInstallIntentLocked(runDir, dag, events, append, attemptId) {
4784
+ const attemptDir = safeExternalAttemptDir(runDir, attemptId);
4785
+ const intent = readSubmissionIntent(attemptDir);
4786
+ if (intent === void 0) return { kind: "none" };
4787
+ if (intent === "corrupt" || intent.attemptId !== attemptId) {
4788
+ (0, import_node_fs13.rmSync)(submissionIntentPath(attemptDir), { force: true });
4789
+ return { kind: "discarded" };
4790
+ }
4791
+ const workDir = (0, import_node_path11.join)(attemptDir, "work");
4792
+ const manifestPath = (0, import_node_path11.join)(attemptDir, "manifest.json");
4793
+ const stageDir = (0, import_node_path11.join)(attemptDir, "submissions", intent.stageTag);
4794
+ const discard = () => {
4795
+ if (!installVerdictCommitted(events, attemptId)) {
4796
+ moveAsideIfPresent(workDir, intent.stageTag);
4797
+ moveAsideIfPresent(manifestPath, intent.stageTag);
4798
+ }
4799
+ removeSubmissionIntent(attemptDir, intent.stageTag);
4800
+ return { kind: "discarded" };
4801
+ };
4802
+ if (installVerdictCommitted(events, attemptId)) {
4803
+ removeSubmissionIntent(attemptDir, intent.stageTag);
4804
+ return { kind: "cleaned" };
4805
+ }
4806
+ const current = classifyExternalAttempt(events, attemptId);
4807
+ if (!current.ok) return discard();
4808
+ const node = externalAttemptNode(dag, current.attempt);
4809
+ if (!node || !isGoalNode(node)) return discard();
4810
+ if ((0, import_node_fs13.existsSync)((0, import_node_path11.join)(stageDir, "work"))) {
4811
+ moveAsideIfPresent(workDir, intent.stageTag);
4812
+ (0, import_node_fs13.renameSync)((0, import_node_path11.join)(stageDir, "work"), workDir);
4813
+ }
4814
+ if ((0, import_node_fs13.existsSync)((0, import_node_path11.join)(stageDir, "manifest.json"))) (0, import_node_fs13.renameSync)((0, import_node_path11.join)(stageDir, "manifest.json"), manifestPath);
4815
+ const verified = verifyInstalledSubmissionSync(manifestPath, workDir);
4816
+ if (!verified.ok) return discard();
4817
+ const verdict = judgeStagedSubmission(node, verified.manifest, workDir);
4818
+ if (verdict.kind === "invalid") return discard();
4819
+ const outcome = appendInstalledVerdictLocked({
4820
+ runDir,
4821
+ dag,
4822
+ events,
4823
+ append,
4824
+ attempt: current.attempt,
4825
+ verdict,
4826
+ manifestPath
4827
+ });
4828
+ removeSubmissionIntent(attemptDir, intent.stageTag);
4829
+ return { kind: "committed", outcome };
4830
+ }
4831
+ function settleExternalInstallIntent(runDir, dag, attemptId) {
4832
+ if (!(0, import_node_fs13.existsSync)(submissionIntentPath(safeExternalAttemptDir(runDir, attemptId)))) return { kind: "none" };
4833
+ return withJournalMutationSync(journalPathOf(runDir), ({ events, append }) => settleInstallIntentLocked(runDir, dag, events, append, attemptId));
4834
+ }
4835
+ function hasExternalInstallIntent(runDir, attemptId) {
4836
+ return (0, import_node_fs13.existsSync)(submissionIntentPath(safeExternalAttemptDir(runDir, attemptId)));
4837
+ }
4838
+
4839
+ // src/workflows/v3/host-execution.ts
4840
+ var import_node_crypto8 = require("node:crypto");
4841
+ var import_node_fs14 = require("node:fs");
4842
+ var import_node_path12 = require("node:path");
4843
+
4844
+ // src/workflows/shared/idempotency-key.ts
4845
+ var import_node_crypto7 = require("node:crypto");
4846
+ function deriveIdempotencyKey(tuple, opts = {}) {
4847
+ const namespace = opts.namespace ?? "wf_";
3618
4848
  const maxLength = opts.maxLength ?? 50;
3619
4849
  if (namespace.length >= maxLength) {
3620
4850
  throw new Error(
@@ -3629,7 +4859,7 @@ function deriveIdempotencyKey(tuple, opts = {}) {
3629
4859
  }
3630
4860
  }
3631
4861
  const seed = canonicalJson(tuple);
3632
- const hash = (0, import_node_crypto5.createHash)("sha256").update(seed, "utf-8").digest("hex");
4862
+ const hash = (0, import_node_crypto7.createHash)("sha256").update(seed, "utf-8").digest("hex");
3633
4863
  return namespace + hash.substring(0, maxLength - namespace.length);
3634
4864
  }
3635
4865
 
@@ -3690,23 +4920,23 @@ function prepareV3HostInputArtifact(input) {
3690
4920
  idempotencyTtlMs: input.registered.executor.idempotencyTtlMs,
3691
4921
  approvalDigest
3692
4922
  };
3693
- const absolutePath = (0, import_node_path8.join)(input.attemptDir, "host-input.json");
4923
+ const absolutePath = (0, import_node_path12.join)(input.attemptDir, "host-input.json");
3694
4924
  assertPathInside(input.runDir, input.attemptDir);
3695
4925
  ensureSecureDirectoryTree(input.runDir, input.attemptDir);
3696
4926
  const content = `${JSON.stringify(prepared, null, 2)}
3697
4927
  `;
3698
4928
  writeCreateOrVerify(absolutePath, content, 384, "host input");
3699
- const existing = (0, import_node_fs10.readFileSync)(absolutePath, "utf-8");
4929
+ const existing = (0, import_node_fs14.readFileSync)(absolutePath, "utf-8");
3700
4930
  if (canonicalJson(parsePreparedHostInput(JSON.parse(existing))) !== canonicalJson(prepared)) {
3701
4931
  throw new Error(`v3 host input conflict at ${absolutePath}; refusing to overwrite prepared bytes`);
3702
4932
  }
3703
4933
  fsyncRegularFileSync(absolutePath);
3704
4934
  fsyncDirectorySyncPortable(input.attemptDir);
3705
- const bytes = (0, import_node_fs10.readFileSync)(absolutePath);
4935
+ const bytes = (0, import_node_fs14.readFileSync)(absolutePath);
3706
4936
  return {
3707
4937
  prepared,
3708
4938
  inputRef: {
3709
- path: (0, import_node_path8.relative)(input.runDir, absolutePath),
4939
+ path: (0, import_node_path12.relative)(input.runDir, absolutePath),
3710
4940
  sha256: sha256(bytes),
3711
4941
  bytes: bytes.length
3712
4942
  },
@@ -3714,12 +4944,12 @@ function prepareV3HostInputArtifact(input) {
3714
4944
  };
3715
4945
  }
3716
4946
  function readCrashLeftV3PreparedHostInput(input) {
3717
- const absolutePath = (0, import_node_path8.join)(input.attemptDir, "host-input.json");
3718
- if (!(0, import_node_fs10.existsSync)(absolutePath)) return void 0;
4947
+ const absolutePath = (0, import_node_path12.join)(input.attemptDir, "host-input.json");
4948
+ if (!(0, import_node_fs14.existsSync)(absolutePath)) return void 0;
3719
4949
  assertPathInside(input.runDir, input.attemptDir);
3720
4950
  assertSecureExistingPath(input.runDir, absolutePath, "host input");
3721
4951
  assertRegularFile(absolutePath, "host input");
3722
- const bytes = (0, import_node_fs10.readFileSync)(absolutePath);
4952
+ const bytes = (0, import_node_fs14.readFileSync)(absolutePath);
3723
4953
  let prepared;
3724
4954
  try {
3725
4955
  prepared = parsePreparedHostInput(JSON.parse(bytes.toString("utf-8")));
@@ -3738,7 +4968,7 @@ function readCrashLeftV3PreparedHostInput(input) {
3738
4968
  inputHash
3739
4969
  });
3740
4970
  const inputRef = {
3741
- path: (0, import_node_path8.relative)(input.runDir, absolutePath),
4971
+ path: (0, import_node_path12.relative)(input.runDir, absolutePath),
3742
4972
  sha256: sha256(bytes),
3743
4973
  bytes: bytes.length
3744
4974
  };
@@ -3761,14 +4991,14 @@ function readCrashLeftV3PreparedHostInput(input) {
3761
4991
  });
3762
4992
  }
3763
4993
  function readAndVerifyV3PreparedHostInput(input) {
3764
- if ((0, import_node_path8.isAbsolute)(input.inputRef.path) || input.inputRef.path.includes("\0")) {
4994
+ if ((0, import_node_path12.isAbsolute)(input.inputRef.path) || input.inputRef.path.includes("\0")) {
3765
4995
  throw new Error("v3 host inputRef.path must be run-relative");
3766
4996
  }
3767
- const absolutePath = (0, import_node_path8.resolve)(input.runDir, input.inputRef.path);
4997
+ const absolutePath = (0, import_node_path12.resolve)(input.runDir, input.inputRef.path);
3768
4998
  assertPathInside(input.runDir, absolutePath);
3769
4999
  assertSecureExistingPath(input.runDir, absolutePath, "host input");
3770
5000
  assertRegularFile(absolutePath, "host input");
3771
- const bytes = (0, import_node_fs10.readFileSync)(absolutePath);
5001
+ const bytes = (0, import_node_fs14.readFileSync)(absolutePath);
3772
5002
  if (bytes.length !== input.inputRef.bytes || sha256(bytes) !== input.inputRef.sha256) {
3773
5003
  throw new Error("v3 host input sidecar bytes/hash mismatch");
3774
5004
  }
@@ -3811,10 +5041,10 @@ function readAndVerifyV3PreparedHostInput(input) {
3811
5041
  function writeV3HostSuccessArtifacts(input) {
3812
5042
  canonicalJson(input.output);
3813
5043
  canonicalJson(input.externalRefs);
3814
- const outputDir = (0, import_node_path8.join)(input.attemptDir, "work");
5044
+ const outputDir = (0, import_node_path12.join)(input.attemptDir, "work");
3815
5045
  assertPathInside(input.runDir, input.attemptDir);
3816
5046
  ensureSecureDirectoryTree(input.runDir, outputDir);
3817
- const resultPath = (0, import_node_path8.join)(outputDir, "result.json");
5047
+ const resultPath = (0, import_node_path12.join)(outputDir, "result.json");
3818
5048
  const resultContent = `${JSON.stringify({
3819
5049
  schemaVersion: 1,
3820
5050
  runId: input.runId,
@@ -3845,7 +5075,7 @@ function writeV3HostSuccessArtifacts(input) {
3845
5075
  mime: "application/json"
3846
5076
  }]
3847
5077
  };
3848
- const manifestPath = (0, import_node_path8.join)(input.attemptDir, "manifest.json");
5078
+ const manifestPath = (0, import_node_path12.join)(input.attemptDir, "manifest.json");
3849
5079
  writeCreateOrVerify(manifestPath, `${JSON.stringify(manifest, null, 2)}
3850
5080
  `, 384, "host manifest");
3851
5081
  fsyncRegularFileSync(resultPath);
@@ -3855,7 +5085,7 @@ function writeV3HostSuccessArtifacts(input) {
3855
5085
  return { manifestPath, resultPath };
3856
5086
  }
3857
5087
  function readAndVerifyV3HostSuccessResult(input) {
3858
- assertRegularFile((0, import_node_path8.join)(input.attemptDir, "manifest.json"), "host manifest");
5088
+ assertRegularFile((0, import_node_path12.join)(input.attemptDir, "manifest.json"), "host manifest");
3859
5089
  if (input.manifest.status !== "ok") {
3860
5090
  throw new Error("v3 host success manifest status must be ok");
3861
5091
  }
@@ -3866,11 +5096,11 @@ function readAndVerifyV3HostSuccessResult(input) {
3866
5096
  if (file.name !== "result" || file.path !== "result.json" || file.kind !== "json") {
3867
5097
  throw new Error("v3 host success manifest must expose exactly result.json as JSON");
3868
5098
  }
3869
- const resultPath = (0, import_node_path8.join)(input.attemptDir, "work", "result.json");
5099
+ const resultPath = (0, import_node_path12.join)(input.attemptDir, "work", "result.json");
3870
5100
  assertPathInside(input.runDir, resultPath);
3871
5101
  assertSecureExistingPath(input.runDir, resultPath, "host result");
3872
5102
  assertRegularFile(resultPath, "host result");
3873
- const bytes = (0, import_node_fs10.readFileSync)(resultPath);
5103
+ const bytes = (0, import_node_fs14.readFileSync)(resultPath);
3874
5104
  if (bytes.length !== file.bytes || sha256(bytes) !== file.sha256) {
3875
5105
  throw new Error("v3 host success result bytes/hash mismatch");
3876
5106
  }
@@ -3957,41 +5187,41 @@ function parsePreparedHostInput(raw) {
3957
5187
  return raw;
3958
5188
  }
3959
5189
  function writeCreateOrVerify(path, content, mode, label) {
3960
- const tempPath = `${path}.tmp-${process.pid}-${(0, import_node_crypto6.randomUUID)()}`;
5190
+ const tempPath = `${path}.tmp-${process.pid}-${(0, import_node_crypto8.randomUUID)()}`;
3961
5191
  let fd;
3962
5192
  try {
3963
- fd = (0, import_node_fs10.openSync)(
5193
+ fd = (0, import_node_fs14.openSync)(
3964
5194
  tempPath,
3965
- import_node_fs10.constants.O_WRONLY | import_node_fs10.constants.O_CREAT | import_node_fs10.constants.O_EXCL | (import_node_fs10.constants.O_NOFOLLOW ?? 0),
5195
+ import_node_fs14.constants.O_WRONLY | import_node_fs14.constants.O_CREAT | import_node_fs14.constants.O_EXCL | (import_node_fs14.constants.O_NOFOLLOW ?? 0),
3966
5196
  mode
3967
5197
  );
3968
- (0, import_node_fs10.writeFileSync)(fd, content, "utf-8");
3969
- (0, import_node_fs10.fsyncSync)(fd);
3970
- (0, import_node_fs10.closeSync)(fd);
5198
+ (0, import_node_fs14.writeFileSync)(fd, content, "utf-8");
5199
+ (0, import_node_fs14.fsyncSync)(fd);
5200
+ (0, import_node_fs14.closeSync)(fd);
3971
5201
  fd = void 0;
3972
5202
  try {
3973
- (0, import_node_fs10.linkSync)(tempPath, path);
3974
- fsyncDirectorySyncPortable((0, import_node_path8.dirname)(path));
5203
+ (0, import_node_fs14.linkSync)(tempPath, path);
5204
+ fsyncDirectorySyncPortable((0, import_node_path12.dirname)(path));
3975
5205
  } catch (err) {
3976
5206
  if (err.code !== "EEXIST") throw err;
3977
5207
  assertRegularFile(path, label);
3978
- if ((0, import_node_fs10.readFileSync)(path, "utf-8") !== content) {
5208
+ if ((0, import_node_fs14.readFileSync)(path, "utf-8") !== content) {
3979
5209
  throw new Error(`v3 ${label} conflict at ${path}; refusing to overwrite committed bytes`);
3980
5210
  }
3981
5211
  }
3982
5212
  } finally {
3983
- if (fd !== void 0) (0, import_node_fs10.closeSync)(fd);
5213
+ if (fd !== void 0) (0, import_node_fs14.closeSync)(fd);
3984
5214
  try {
3985
- (0, import_node_fs10.unlinkSync)(tempPath);
3986
- fsyncDirectorySyncPortable((0, import_node_path8.dirname)(path));
5215
+ (0, import_node_fs14.unlinkSync)(tempPath);
5216
+ fsyncDirectorySyncPortable((0, import_node_path12.dirname)(path));
3987
5217
  } catch {
3988
5218
  }
3989
5219
  }
3990
5220
  fsyncRegularFileSync(path);
3991
- fsyncDirectorySyncPortable((0, import_node_path8.dirname)(path));
5221
+ fsyncDirectorySyncPortable((0, import_node_path12.dirname)(path));
3992
5222
  }
3993
5223
  function assertRegularFile(path, label) {
3994
- const stat = (0, import_node_fs10.lstatSync)(path);
5224
+ const stat = (0, import_node_fs14.lstatSync)(path);
3995
5225
  if (!stat.isFile() || stat.isSymbolicLink()) {
3996
5226
  throw new Error(`v3 ${label} must be a regular file: ${path}`);
3997
5227
  }
@@ -4000,47 +5230,47 @@ function assertRegularFile(path, label) {
4000
5230
  }
4001
5231
  }
4002
5232
  function assertPathInside(parent, child) {
4003
- const root = (0, import_node_path8.resolve)(parent);
4004
- const rel = (0, import_node_path8.relative)(root, (0, import_node_path8.resolve)(child));
4005
- if (rel === "" || rel === ".." || rel.startsWith(`..${import_node_path8.sep}`) || (0, import_node_path8.isAbsolute)(rel)) {
5233
+ const root = (0, import_node_path12.resolve)(parent);
5234
+ const rel = (0, import_node_path12.relative)(root, (0, import_node_path12.resolve)(child));
5235
+ if (rel === "" || rel === ".." || rel.startsWith(`..${import_node_path12.sep}`) || (0, import_node_path12.isAbsolute)(rel)) {
4006
5236
  throw new Error("v3 host path escapes runDir");
4007
5237
  }
4008
5238
  }
4009
5239
  function ensureSecureDirectoryTree(rootPath, targetPath) {
4010
- const root = (0, import_node_path8.resolve)(rootPath);
4011
- const target = (0, import_node_path8.resolve)(targetPath);
4012
- const rel = (0, import_node_path8.relative)(root, target);
4013
- if (rel === ".." || rel.startsWith(`..${import_node_path8.sep}`) || (0, import_node_path8.isAbsolute)(rel)) {
5240
+ const root = (0, import_node_path12.resolve)(rootPath);
5241
+ const target = (0, import_node_path12.resolve)(targetPath);
5242
+ const rel = (0, import_node_path12.relative)(root, target);
5243
+ if (rel === ".." || rel.startsWith(`..${import_node_path12.sep}`) || (0, import_node_path12.isAbsolute)(rel)) {
4014
5244
  throw new Error("v3 host directory escapes runDir");
4015
5245
  }
4016
5246
  assertDirectory(root, "run directory");
4017
5247
  let current = root;
4018
5248
  for (const segment of rel.split(/[\\/]/).filter(Boolean)) {
4019
- current = (0, import_node_path8.join)(current, segment);
4020
- if (!(0, import_node_fs10.existsSync)(current)) (0, import_node_fs10.mkdirSync)(current, { mode: 448 });
5249
+ current = (0, import_node_path12.join)(current, segment);
5250
+ if (!(0, import_node_fs14.existsSync)(current)) (0, import_node_fs14.mkdirSync)(current, { mode: 448 });
4021
5251
  assertDirectory(current, "host artifact directory");
4022
5252
  }
4023
5253
  }
4024
5254
  function assertSecureExistingPath(rootPath, filePath, label) {
4025
- const root = (0, import_node_path8.resolve)(rootPath);
4026
- const target = (0, import_node_path8.resolve)(filePath);
5255
+ const root = (0, import_node_path12.resolve)(rootPath);
5256
+ const target = (0, import_node_path12.resolve)(filePath);
4027
5257
  assertPathInside(root, target);
4028
5258
  assertDirectory(root, "run directory");
4029
- const rel = (0, import_node_path8.relative)(root, (0, import_node_path8.dirname)(target));
5259
+ const rel = (0, import_node_path12.relative)(root, (0, import_node_path12.dirname)(target));
4030
5260
  let current = root;
4031
5261
  for (const segment of rel.split(/[\\/]/).filter(Boolean)) {
4032
- current = (0, import_node_path8.join)(current, segment);
5262
+ current = (0, import_node_path12.join)(current, segment);
4033
5263
  assertDirectory(current, `${label} parent`);
4034
5264
  }
4035
5265
  }
4036
5266
  function assertDirectory(path, label) {
4037
- const stat = (0, import_node_fs10.lstatSync)(path);
5267
+ const stat = (0, import_node_fs14.lstatSync)(path);
4038
5268
  if (!stat.isDirectory() || stat.isSymbolicLink()) {
4039
5269
  throw new Error(`v3 ${label} must be a real directory: ${path}`);
4040
5270
  }
4041
5271
  }
4042
5272
  function sha256(value) {
4043
- return (0, import_node_crypto6.createHash)("sha256").update(value).digest("hex");
5273
+ return (0, import_node_crypto8.createHash)("sha256").update(value).digest("hex");
4044
5274
  }
4045
5275
  function isRecord3(value) {
4046
5276
  return typeof value === "object" && value !== null && !Array.isArray(value);
@@ -4102,7 +5332,7 @@ function renderGoalFile(goal, resultSchema, loopCtx, nodeInstructions, hasWorkfl
4102
5332
  ...artifactSection,
4103
5333
  "## How to complete this node",
4104
5334
  "You are an autonomous agent completing exactly ONE botmux v3 workflow node.",
4105
- "Work toward the goal above until it is done, then stop. Do NOT ask the user with interactive tools (they are disabled in this mode). If you genuinely need a human DECISION to proceed, use the human-ask escape hatch described below (also available as the `botmux-goal-ask` skill).",
5335
+ "Work toward the goal above until it is done, then stop. Do NOT ask the user with interactive tools (they are disabled in this mode). If you genuinely need a human DECISION to proceed, use the human-ask escape hatch described below (the same protocol is available via `botmux skill show botmux-goal-ask`).",
4106
5336
  "",
4107
5337
  `- Upstream inputs: the file at $${E.INPUTS_PATH} is a JSON object \`{ "inputs": [...] }\` listing upstream products, each with an absolute \`path\`. Read only the ones the goal needs (it may be empty). If it includes an input entry \`{ "from": "human", "name": "answer", "path": "..." }\`, read that JSON file before continuing. If an \`omitted\` array is present, those declared inputs were intentionally not produced (their workflow branch was not taken) \u2014 treat their absence as by-design, do NOT invent their content.`,
4108
5338
  `- Revisit feedback: if any input has \`"from": "revisit"\`, a DOWNSTREAM node sent this node back because its product was inadequate. You MUST read these before doing anything else: \`reason\` (why you were sent back), \`source:*\` (the downstream node's output \u2014 the evidence of what was wrong), and \`previous:*\` (YOUR OWN previous output \u2014 edit/fix it, do not rewrite from scratch). Address the reason; do not just reproduce the prior output.`,
@@ -4156,10 +5386,10 @@ function classifyTerminal(errorClass, opts) {
4156
5386
  }
4157
5387
  }
4158
5388
  function readGoalAsk(askPath) {
4159
- if (!(0, import_node_fs11.existsSync)(askPath)) return void 0;
5389
+ if (!(0, import_node_fs15.existsSync)(askPath)) return void 0;
4160
5390
  let parsed;
4161
5391
  try {
4162
- parsed = JSON.parse((0, import_node_fs11.readFileSync)(askPath, "utf-8"));
5392
+ parsed = JSON.parse((0, import_node_fs15.readFileSync)(askPath, "utf-8"));
4163
5393
  } catch {
4164
5394
  return void 0;
4165
5395
  }
@@ -4183,126 +5413,12 @@ function mergeNodeCapability(snap, override) {
4183
5413
  ...override.model ? { model: override.model } : {}
4184
5414
  };
4185
5415
  }
4186
- function readRevisitRequest(manifest, outputDir) {
4187
- const entry = manifest.files.find((f) => f.path === "result.json");
4188
- if (!entry) return { ok: true };
4189
- let value;
4190
- try {
4191
- value = JSON.parse((0, import_node_fs11.readFileSync)((0, import_node_path9.join)(outputDir, entry.path), "utf-8"));
4192
- } catch {
4193
- return { ok: true };
4194
- }
4195
- if (!value || typeof value !== "object" || value.status !== "revisit") {
4196
- return { ok: true };
4197
- }
4198
- const v = value;
4199
- const problems = [];
4200
- if (typeof v.revisitTo !== "string" || v.revisitTo.trim() === "") {
4201
- problems.push('result.json status "revisit" requires a non-empty string "revisitTo"');
4202
- }
4203
- if (v.reason !== void 0 && typeof v.reason !== "string") {
4204
- problems.push('result.json "reason" must be a string when present');
4205
- }
4206
- if (problems.length > 0) return { ok: false, problems };
4207
- return {
4208
- ok: true,
4209
- request: {
4210
- toNodeId: v.revisitTo,
4211
- ...typeof v.reason === "string" && v.reason ? { reason: v.reason } : {}
4212
- }
4213
- };
4214
- }
4215
- function validateResult(filePath, schema) {
4216
- if (!(0, import_node_fs11.existsSync)(filePath)) return { ok: false, problems: [`result.json not found at ${filePath}`] };
4217
- let parsed;
4218
- try {
4219
- parsed = JSON.parse((0, import_node_fs11.readFileSync)(filePath, "utf-8"));
4220
- } catch (err) {
4221
- return { ok: false, problems: [`result.json is not valid JSON: ${err instanceof Error ? err.message : String(err)}`] };
4222
- }
4223
- if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
4224
- return { ok: false, problems: ["result.json root must be a JSON object"] };
4225
- }
4226
- const obj = parsed;
4227
- const problems = [];
4228
- for (const field of schema.required ?? []) {
4229
- if (!(field in obj)) problems.push(`missing required field "${field}"`);
4230
- }
4231
- for (const [name, spec] of Object.entries(schema.properties)) {
4232
- if (!(name in obj)) continue;
4233
- const v = obj[name];
4234
- const okType = spec.type === "string" ? typeof v === "string" : spec.type === "number" ? typeof v === "number" && Number.isFinite(v) : spec.type === "boolean" ? typeof v === "boolean" : spec.type === "array" ? Array.isArray(v) : typeof v === "object" && v !== null && !Array.isArray(v);
4235
- if (!okType) {
4236
- problems.push(`field "${name}" must be of type ${spec.type}`);
4237
- continue;
4238
- }
4239
- if (spec.type === "string" && spec.enum && !spec.enum.includes(v)) {
4240
- problems.push(`field "${name}" must be one of [${spec.enum.join(", ")}] (got ${JSON.stringify(v)})`);
4241
- }
4242
- }
4243
- return problems.length > 0 ? { ok: false, problems } : { ok: true };
4244
- }
4245
- var ATTEMPT_NNN_RE = /\/attempts\/(\d{3})$/;
4246
- function attemptNumber(attemptId) {
4247
- const m = ATTEMPT_NNN_RE.exec(attemptId);
4248
- return m ? parseInt(m[1], 10) : void 0;
4249
- }
4250
- function nextAttemptIdFor(events, key) {
4251
- const matches = (e) => (e.instanceId ?? e.nodeId) === key;
4252
- let maxSeen = 0;
4253
- let reserved;
4254
- let prepared;
4255
- for (const e of events) {
4256
- if (e.type === "nodeDispatched" && matches(e)) {
4257
- const n2 = attemptNumber(e.attemptId);
4258
- if (n2 === void 0) continue;
4259
- maxSeen = Math.max(maxSeen, n2);
4260
- if (reserved === n2) reserved = void 0;
4261
- } else if (e.type === "hostInputPrepared" && matches(e)) {
4262
- const n2 = attemptNumber(e.attemptId);
4263
- if (n2 === void 0) continue;
4264
- maxSeen = Math.max(maxSeen, n2);
4265
- if (reserved === n2) reserved = void 0;
4266
- prepared = e.attemptId;
4267
- } else if ((e.type === "nodeSucceeded" || e.type === "nodeFailed" || e.type === "nodeBlocked") && matches(e)) {
4268
- const n2 = attemptNumber(e.attemptId);
4269
- if (n2 !== void 0) maxSeen = Math.max(maxSeen, n2);
4270
- if (prepared === e.attemptId) prepared = void 0;
4271
- } else if (e.type === "hostEffectIntent" && matches(e) && prepared === e.attemptId) {
4272
- prepared = void 0;
4273
- } else if (e.type === "nodeRetryRequested" && matches(e)) {
4274
- const n2 = attemptNumber(e.nextAttemptId);
4275
- if (n2 === void 0) continue;
4276
- reserved = n2;
4277
- prepared = void 0;
4278
- maxSeen = Math.max(maxSeen, n2);
4279
- }
4280
- }
4281
- if (prepared) return prepared;
4282
- const n = reserved ?? maxSeen + 1;
4283
- return `${key}/attempts/${String(n).padStart(3, "0")}`;
4284
- }
4285
- function latestAttemptIdFor(events, key) {
4286
- let latest;
4287
- let latestNumber = -1;
4288
- for (const e of events) {
4289
- if ((e.type === "nodeDispatched" || e.type === "hostInputPrepared" || e.type === "hostEffectIntent" || e.type === "nodeSucceeded" || e.type === "nodeFailed" || e.type === "nodeBlocked") && (e.instanceId ?? e.nodeId) === key) {
4290
- const n = attemptNumber(e.attemptId);
4291
- if (n === void 0) {
4292
- if (latestNumber < 0) latest = e.attemptId;
4293
- } else if (n >= latestNumber) {
4294
- latest = e.attemptId;
4295
- latestNumber = n;
4296
- }
4297
- }
4298
- }
4299
- return latest;
4300
- }
5416
+ var V3_DEFAULT_CONCURRENCY = Object.freeze({ global: 4, perBot: 1, perCli: 2 });
4301
5417
  function assertOrCreateFrozenJson(path, value, label, normalize = (candidate) => candidate) {
4302
5418
  const expectedHash = computeInputHash(normalize(value));
4303
- if (!(0, import_node_fs11.existsSync)(path)) {
5419
+ if (!(0, import_node_fs15.existsSync)(path)) {
4304
5420
  try {
4305
- (0, import_node_fs11.writeFileSync)(path, `${JSON.stringify(value, null, 2)}
5421
+ (0, import_node_fs15.writeFileSync)(path, `${JSON.stringify(value, null, 2)}
4306
5422
  `, {
4307
5423
  encoding: "utf-8",
4308
5424
  flag: "wx",
@@ -4316,7 +5432,7 @@ function assertOrCreateFrozenJson(path, value, label, normalize = (candidate) =>
4316
5432
  }
4317
5433
  let stored;
4318
5434
  try {
4319
- stored = normalize(JSON.parse((0, import_node_fs11.readFileSync)(path, "utf-8")));
5435
+ stored = normalize(JSON.parse((0, import_node_fs15.readFileSync)(path, "utf-8")));
4320
5436
  } catch (error) {
4321
5437
  throw new Error(
4322
5438
  `v3 runtime: frozen ${label} is unreadable: ${error instanceof Error ? error.message : String(error)}`
@@ -4328,14 +5444,27 @@ function assertOrCreateFrozenJson(path, value, label, normalize = (candidate) =>
4328
5444
  );
4329
5445
  }
4330
5446
  }
4331
- async function runWorkflow(dag, deps, opts) {
4332
- const runDir = (0, import_node_path9.join)(opts.baseDir, dag.runId);
4333
- (0, import_node_fs11.mkdirSync)(runDir, { recursive: true });
4334
- const journalPath = (0, import_node_path9.join)(runDir, "journal.ndjson");
4335
- const statePath = (0, import_node_path9.join)(runDir, "STATE");
4336
- const globalCap = opts.globalConcurrency ?? 4;
4337
- const perBotCap = opts.perBotConcurrency ?? 1;
4338
- const perCliCap = opts.perCliConcurrency ?? 2;
5447
+ var HANDOFF_RESEND_BACKOFF_MS = 3e4;
5448
+ var PROBE_RETRY_BACKOFF_MS = 3e4;
5449
+ var PROBE_UNCERTAIN_AFTER_MS = 10 * 6e4;
5450
+ var probeFailures = /* @__PURE__ */ new Map();
5451
+ function forgetProbeFailuresOf(runDir) {
5452
+ const prefix = `${runDir}\0`;
5453
+ for (const key of probeFailures.keys()) {
5454
+ if (key.startsWith(prefix)) probeFailures.delete(key);
5455
+ }
5456
+ }
5457
+ async function runWorkflow(authorizedDag, deps, opts) {
5458
+ let dag = authorizedDag;
5459
+ const runDir = (0, import_node_path13.join)(opts.baseDir, dag.runId);
5460
+ (0, import_node_fs15.mkdirSync)(runDir, { recursive: true });
5461
+ const journalPath = (0, import_node_path13.join)(runDir, "journal.ndjson");
5462
+ const statePath = (0, import_node_path13.join)(runDir, "STATE");
5463
+ const globalCap = opts.globalConcurrency ?? V3_DEFAULT_CONCURRENCY.global;
5464
+ const perBotCap = opts.perBotConcurrency ?? V3_DEFAULT_CONCURRENCY.perBot;
5465
+ const perCliDefaultCap = opts.perCliConcurrency ?? V3_DEFAULT_CONCURRENCY.perCli;
5466
+ const perCliById = opts.perCliConcurrencyById ?? {};
5467
+ const perCliCap = (cliId) => Object.prototype.hasOwnProperty.call(perCliById, cliId) ? perCliById[cliId] : perCliDefaultCap;
4339
5468
  const gateMode = opts.gateMode ?? "blocking";
4340
5469
  const hostResponseWaitMs = opts.hostResponseWaitMs ?? 15e3;
4341
5470
  const executionContext = opts.executionContext ?? opts.resolvedWorkflowData;
@@ -4345,7 +5474,25 @@ async function runWorkflow(dag, deps, opts) {
4345
5474
  if (!Number.isSafeInteger(hostResponseWaitMs) || hostResponseWaitMs < 1) {
4346
5475
  throw new Error("v3 runtime: hostResponseWaitMs must be a positive safe integer");
4347
5476
  }
4348
- const nodesById = new Map(dag.nodes.map((n) => [n.id, n]));
5477
+ const externalMode = opts.nodeExecutor === "bot";
5478
+ const externalExecutor = deps.externalExecutor;
5479
+ if (externalMode && !externalExecutor) {
5480
+ throw new Error('v3 runtime: nodeExecutor "bot" requires an external node executor');
5481
+ }
5482
+ const externalAcceptTimeoutMs = opts.externalAcceptTimeoutMs ?? V3_DEFAULT_HANDOFF_ACCEPT_TIMEOUT_MS;
5483
+ if (!Number.isSafeInteger(externalAcceptTimeoutMs) || externalAcceptTimeoutMs < 0) {
5484
+ throw new Error("v3 runtime: externalAcceptTimeoutMs must be a non-negative safe integer");
5485
+ }
5486
+ const clock = () => deps.now?.() ?? Date.now();
5487
+ const externalCallTimeoutMs = opts.externalCallTimeoutMs ?? V3_EXTERNAL_CALL_TIMEOUT_MS;
5488
+ if (!Number.isSafeInteger(externalCallTimeoutMs) || externalCallTimeoutMs < 1) {
5489
+ throw new Error("v3 runtime: externalCallTimeoutMs must be a positive safe integer");
5490
+ }
5491
+ const probeKey = (attemptId) => `${runDir}\0${attemptId}`;
5492
+ let nodesById = new Map(dag.nodes.map((n) => [n.id, n]));
5493
+ let dagRevision = 1;
5494
+ let pendingTouched;
5495
+ let pendingRevision;
4349
5496
  const botSnapshots = opts.frozenBotSnapshots ? new Map(opts.frozenBotSnapshots) : /* @__PURE__ */ new Map();
4350
5497
  const freezeBot = (bot) => {
4351
5498
  const key = bot ?? "";
@@ -4366,7 +5513,7 @@ async function runWorkflow(dag, deps, opts) {
4366
5513
  for (const b of node.body.nodes) freezeBot(b.bot ?? node.bot);
4367
5514
  }
4368
5515
  }
4369
- for (const [key, snap] of botSnapshots) {
5516
+ const validateSnapshot = (key, snap) => {
4370
5517
  if (deps.validateExecutionSnapshot) {
4371
5518
  deps.validateExecutionSnapshot(snap, key);
4372
5519
  } else if (!isV3SupportedCli(snap.cliId)) {
@@ -4374,22 +5521,83 @@ async function runWorkflow(dag, deps, opts) {
4374
5521
  `v3 runtime: bot "${key || "<default>"}" resolves to CLI "${snap.cliId}", which is not supported by v3 goal-mode (supported: ${V3_SUPPORTED_CLIS.join(", ")})`
4375
5522
  );
4376
5523
  }
4377
- }
5524
+ };
5525
+ for (const [key, snap] of botSnapshots) validateSnapshot(key, snap);
5526
+ const botSnapshotFor2 = (selector) => {
5527
+ const snap = snapshotForSelector(botSnapshots, selector);
5528
+ if (!snap) {
5529
+ throw new Error(`v3 runtime: no frozen bot snapshot for selector "${selector || "<default>"}"`);
5530
+ }
5531
+ return snap;
5532
+ };
5533
+ const refreshEffectiveDag = (events) => {
5534
+ const history = foldV3Revisions(events);
5535
+ pendingTouched = history.pending ? new Set(history.pending.touched) : void 0;
5536
+ pendingRevision = history.pending?.revision;
5537
+ if (history.current === dagRevision) return;
5538
+ const effective = loadEffectiveV3Dag(runDir, authorizedDag, events);
5539
+ for (const [selector, snap] of v3ApprovedBotScope(botSnapshots, effective.botAdditions)) {
5540
+ if (botSnapshots.has(selector)) continue;
5541
+ validateSnapshot(selector, snap);
5542
+ botSnapshots.set(selector, snap);
5543
+ }
5544
+ for (const node of effective.dag.nodes) {
5545
+ if (isHostNode(node) && !deps.hostExecutors?.has(node.executor)) {
5546
+ throw new Error(`v3 runtime: host executor "${node.executor}" is not registered`);
5547
+ }
5548
+ if (isGoalNode(node)) botSnapshotFor2(node.bot);
5549
+ if (isLoopNode(node)) for (const body of node.body.nodes) botSnapshotFor2(body.bot ?? node.bot);
5550
+ }
5551
+ dag = effective.dag;
5552
+ nodesById = new Map(dag.nodes.map((n) => [n.id, n]));
5553
+ dagRevision = effective.revision;
5554
+ };
5555
+ const decisionStillCurrent = (latest, nodeId, loopId) => {
5556
+ if (currentV3Revision(latest) !== dagRevision) return false;
5557
+ if (nodeId === void 0) return true;
5558
+ const pending = foldV3Revisions(latest).pending;
5559
+ return !pending || !v3NodeHeldByProposal(new Set(pending.touched), nodeId, loopId);
5560
+ };
5561
+ const appendControlEvent = (event) => withJournalMutationSync(journalPath, ({ events, append }) => {
5562
+ if (!decisionStillCurrent(events)) return false;
5563
+ append(event);
5564
+ return true;
5565
+ });
5566
+ let checkpointHeld = /* @__PURE__ */ new Set();
5567
+ const refreshCheckpointHolds = (events) => {
5568
+ if (!externalMode) return;
5569
+ checkpointHeld = v3CheckpointHeldNodes(dag, v3CheckpointSuccesses({
5570
+ events,
5571
+ dag,
5572
+ markedSince: v3CheckpointMarkedSince(runDir, authorizedDag, events)
5573
+ }));
5574
+ };
5575
+ const heldByProposal = (nodeId, loopId) => v3NodeHeldByProposal(pendingTouched, nodeId, loopId) || checkpointHeld.has(nodeId) || loopId !== void 0 && checkpointHeld.has(loopId);
5576
+ const reconcileRevisionFollowUps = (events) => {
5577
+ const live = new Set(dag.nodes.map((node) => node.id));
5578
+ if (missingV3RevisionFollowUps(events, live).length === 0) return false;
5579
+ const result = appendMissingV3RevisionFollowUps(journalPath, { revision: dagRevision, liveNodeIds: live });
5580
+ return result.kind === "stale" || result.count > 0;
5581
+ };
4378
5582
  if (!opts.authorizedArtifacts) {
4379
5583
  assertOrCreateFrozenJson(
4380
- (0, import_node_path9.join)(runDir, "bots.snapshot.json"),
5584
+ (0, import_node_path13.join)(runDir, "bots.snapshot.json"),
4381
5585
  Object.fromEntries(botSnapshots),
4382
5586
  "execution profile snapshot"
4383
5587
  );
4384
5588
  assertOrCreateFrozenJson(
4385
- (0, import_node_path9.join)(runDir, "dag.json"),
5589
+ (0, import_node_path13.join)(runDir, "dag.json"),
4386
5590
  dag,
4387
5591
  "workflow definition",
4388
5592
  validateDag
4389
5593
  );
4390
5594
  }
4391
5595
  if (readJournal(journalPath).length === 0) {
4392
- appendEvent(journalPath, { type: "runStarted", runId: dag.runId });
5596
+ appendEvent(journalPath, {
5597
+ type: "runStarted",
5598
+ runId: dag.runId,
5599
+ ...externalMode ? { nodeExecutor: "bot" } : {}
5600
+ });
4393
5601
  }
4394
5602
  const inFlight = /* @__PURE__ */ new Map();
4395
5603
  const botInFlight = /* @__PURE__ */ new Map();
@@ -4400,6 +5608,9 @@ async function runWorkflow(dag, deps, opts) {
4400
5608
  const hostPromiseAttempts = /* @__PURE__ */ new Map();
4401
5609
  const hostDeadlineCancels = /* @__PURE__ */ new Map();
4402
5610
  const blockingGateOwners = /* @__PURE__ */ new Map();
5611
+ const handoffControllers = /* @__PURE__ */ new Map();
5612
+ const handoffRetryAt = /* @__PURE__ */ new Map();
5613
+ const acceptanceObserved = /* @__PURE__ */ new Set();
4403
5614
  class HostProviderResponseTimeoutError extends Error {
4404
5615
  constructor() {
4405
5616
  super(`host provider did not settle within ${hostResponseWaitMs}ms`);
@@ -4446,6 +5657,9 @@ async function runWorkflow(dag, deps, opts) {
4446
5657
  };
4447
5658
  while (true) {
4448
5659
  const events = readJournal(journalPath);
5660
+ refreshEffectiveDag(events);
5661
+ refreshCheckpointHolds(events);
5662
+ if (reconcileRevisionFollowUps(events)) continue;
4449
5663
  const snap = materialize(events);
4450
5664
  writeState(statePath, snap);
4451
5665
  if (opts.cancelSignal?.aborted && snap.runStatus !== "cancelling" && !isTrueRunTerminal(snap.runStatus)) {
@@ -4456,15 +5670,21 @@ async function runWorkflow(dag, deps, opts) {
4456
5670
  if (openHostEffects.length > 0) {
4457
5671
  const settled = await reconcileOpenHostEffects(openHostEffects);
4458
5672
  detachClosedHostInvocations();
4459
- if (!settled) await new Promise((resolve5) => setTimeout(resolve5, 1e3));
5673
+ if (!settled) await new Promise((resolve6) => setTimeout(resolve6, 1e3));
4460
5674
  continue;
4461
5675
  }
5676
+ if (settleExternalLeftovers(events)) continue;
4462
5677
  if (snap.runStatus === "cancelling" && snap.cancelRequestId) {
4463
5678
  for (const controller of nodeControllers.values()) {
4464
5679
  if (!controller.signal.aborted) {
4465
5680
  controller.abort({ kind: "run", cancelRequestId: snap.cancelRequestId });
4466
5681
  }
4467
5682
  }
5683
+ for (const controller of handoffControllers.values()) {
5684
+ if (!controller.signal.aborted) {
5685
+ controller.abort({ kind: "run", cancelRequestId: snap.cancelRequestId });
5686
+ }
5687
+ }
4468
5688
  cancelUnfinishedNodes(snap.cancelRequestId, { includeRunning: false });
4469
5689
  for (const key of [...inFlight.keys()]) {
4470
5690
  if (!key.endsWith("::gate")) continue;
@@ -4475,6 +5695,10 @@ async function runWorkflow(dag, deps, opts) {
4475
5695
  await Promise.race(inFlight.values());
4476
5696
  continue;
4477
5697
  }
5698
+ if (openV3ExternalAttempts(events).length > 0) {
5699
+ await closeExternalAttemptsForCancellation(snap.cancelRequestId);
5700
+ continue;
5701
+ }
4478
5702
  let externalDrained = false;
4479
5703
  try {
4480
5704
  externalDrained = drainExternallyOwnedAttempts({
@@ -4484,7 +5708,7 @@ async function runWorkflow(dag, deps, opts) {
4484
5708
  } catch {
4485
5709
  }
4486
5710
  if (!externalDrained) {
4487
- await new Promise((resolve5) => setTimeout(resolve5, 250));
5711
+ await new Promise((resolve6) => setTimeout(resolve6, 250));
4488
5712
  continue;
4489
5713
  }
4490
5714
  finalizeRuntimeCancellation(snap.cancelRequestId);
@@ -4499,7 +5723,16 @@ async function runWorkflow(dag, deps, opts) {
4499
5723
  open,
4500
5724
  trueTerminal ? "orphanRecovery" : "terminalPeer"
4501
5725
  );
4502
- if (!drained) await new Promise((resolve5) => setTimeout(resolve5, 250));
5726
+ if (!drained) await new Promise((resolve6) => setTimeout(resolve6, 250));
5727
+ continue;
5728
+ }
5729
+ const openExternal = openV3ExternalAttempts(events);
5730
+ if (openExternal.length > 0) {
5731
+ await drainExternalAttempts(
5732
+ openExternal,
5733
+ "runBoundary",
5734
+ trueTerminal ? "orphanRecovery" : "terminalPeer"
5735
+ );
4503
5736
  continue;
4504
5737
  }
4505
5738
  break;
@@ -4509,7 +5742,7 @@ async function runWorkflow(dag, deps, opts) {
4509
5742
  const obsolete = obsoleteOpenAttempts(events, snap);
4510
5743
  if (obsolete.length > 0) {
4511
5744
  const drained = await quiesceAttempts(obsolete, "obsoleteAttempt");
4512
- if (!drained) await new Promise((resolve5) => setTimeout(resolve5, 250));
5745
+ if (!drained) await new Promise((resolve6) => setTimeout(resolve6, 250));
4513
5746
  continue;
4514
5747
  }
4515
5748
  const orphaned = openV3WorkerAttempts(events).filter((attempt) => {
@@ -4518,34 +5751,56 @@ async function runWorkflow(dag, deps, opts) {
4518
5751
  });
4519
5752
  if (orphaned.length > 0) {
4520
5753
  const drained = await quiesceAttempts(orphaned, "orphanRecovery");
4521
- if (!drained) await new Promise((resolve5) => setTimeout(resolve5, 250));
5754
+ if (!drained) await new Promise((resolve6) => setTimeout(resolve6, 250));
5755
+ continue;
5756
+ }
5757
+ const obsoleteExternal = obsoleteExternalAttempts(events, snap);
5758
+ if (obsoleteExternal.length > 0) {
5759
+ await drainExternalAttempts(obsoleteExternal, "obsolete", "obsoleteAttempt");
4522
5760
  continue;
4523
5761
  }
5762
+ if (await reconcileExternalAttempts(events)) continue;
4524
5763
  const actions = decideNext(dag, snap.nodes, snap.loops, snap.edges, snap.instances);
4525
5764
  const terminal = actions.find(
4526
5765
  (a) => a.kind === "completeRunSucceeded" || a.kind === "completeRunFailed" || a.kind === "completeRunBlocked"
4527
5766
  );
4528
5767
  if (terminal) {
5768
+ if (terminal.kind === "completeRunSucceeded" && pendingRevision !== void 0) {
5769
+ if (inFlight.size > 0) {
5770
+ await waitForInFlightOrDurableCancellation();
5771
+ continue;
5772
+ }
5773
+ return { reason: "awaitingGate", pendingWaits: [], pendingRevision, runDir };
5774
+ }
4529
5775
  dropAllBlockingGatePromises();
4530
5776
  const open = openV3WorkerAttempts(events);
4531
5777
  if (open.length > 0) {
4532
5778
  const drained = await quiesceAttempts(open, "terminalPeer");
4533
- if (!drained) await new Promise((resolve5) => setTimeout(resolve5, 250));
5779
+ if (!drained) await new Promise((resolve6) => setTimeout(resolve6, 250));
5780
+ continue;
5781
+ }
5782
+ const openExternal = openV3ExternalAttempts(events);
5783
+ if (openExternal.length > 0) {
5784
+ await drainExternalAttempts(openExternal, "runBoundary", "terminalPeer");
4534
5785
  continue;
4535
5786
  }
4536
5787
  appendRunTerminalIfQuiescent();
4537
5788
  continue;
4538
5789
  }
4539
5790
  if (gateMode === "blocking") reattachBlockingGates(snap);
4540
- const controls = actions.filter(
5791
+ const allControls = actions.filter(
4541
5792
  (a) => a.kind === "startLoop" || a.kind === "startLoopIteration" || a.kind === "evaluateLoopIteration" || a.kind === "completeLoop" || a.kind === "resolveEdge" || a.kind === "skipNode"
4542
5793
  );
5794
+ const controls = allControls.filter((a) => !heldByProposal(
5795
+ a.kind === "resolveEdge" ? a.to : a.kind === "skipNode" ? a.nodeId : a.loopId
5796
+ ));
5797
+ const heldControls = allControls.length - controls.length;
4543
5798
  if (controls.length > 0) {
4544
5799
  const eventsForControl = readJournal(journalPath);
4545
5800
  for (const a of controls) {
4546
5801
  if (a.kind === "resolveEdge") applyResolveEdge(a, eventsForControl);
4547
5802
  else if (a.kind === "skipNode") {
4548
- appendEvent(journalPath, {
5803
+ appendControlEvent({
4549
5804
  type: "nodeSkipped",
4550
5805
  nodeId: a.nodeId,
4551
5806
  reason: "triggerRuleUnsatisfied",
@@ -4555,19 +5810,42 @@ async function runWorkflow(dag, deps, opts) {
4555
5810
  }
4556
5811
  continue;
4557
5812
  }
5813
+ const openExternalNow = openV3ExternalAttempts(events);
5814
+ let externalOccupancy = openExternalNow.length;
5815
+ const externalByBot = /* @__PURE__ */ new Map();
5816
+ for (const attempt of openExternalNow) {
5817
+ if (!attempt.targetLarkAppId) continue;
5818
+ externalByBot.set(attempt.targetLarkAppId, (externalByBot.get(attempt.targetLarkAppId) ?? 0) + 1);
5819
+ }
5820
+ const occupancy = () => inFlight.size - [...inFlight.keys()].filter((key) => key.endsWith("::handoff")).length + externalOccupancy;
4558
5821
  let startedThisTick = 0;
5822
+ let heldThisTick = heldControls;
4559
5823
  for (const a of actions) {
4560
- if (inFlight.size >= globalCap) break;
5824
+ if (occupancy() >= globalCap) break;
5825
+ if ((a.kind === "dispatchWork" || a.kind === "dispatchGate") && heldByProposal(a.nodeId, a.kind === "dispatchWork" ? a.loop?.loopId : void 0)) {
5826
+ heldThisTick++;
5827
+ continue;
5828
+ }
4561
5829
  if (a.kind === "dispatchWork") {
4562
5830
  const node = a.loop ? instanceNodeFor(a.loop) : nodesById.get(a.nodeId);
4563
5831
  if (isHostNode(node)) {
4564
5832
  if (!a.instanceId) throw new Error(`v3 runtime: host node "${node.id}" has no runtime instance`);
4565
5833
  if (startHost(node, events, a.instanceId)) startedThisTick++;
4566
5834
  } else {
4567
- const botKey = node.bot ?? "";
4568
- const botSnap = botSnapshots.get(botKey);
5835
+ const botSnap = botSnapshotFor2(node.bot);
5836
+ if (externalMode) {
5837
+ const target = snap.assignments.get(node.id) ?? botSnap.larkAppId;
5838
+ if ((botInFlight.get(target) ?? 0) + (externalByBot.get(target) ?? 0) >= perBotCap) continue;
5839
+ if (startExternal(node, botSnap, events, a.loop, a.omitted, a.instanceId)) {
5840
+ startedThisTick++;
5841
+ externalOccupancy++;
5842
+ externalByBot.set(target, (externalByBot.get(target) ?? 0) + 1);
5843
+ }
5844
+ continue;
5845
+ }
5846
+ const botKey = botSnap.larkAppId;
4569
5847
  if ((botInFlight.get(botKey) ?? 0) >= perBotCap) continue;
4570
- if ((cliInFlight.get(botSnap.cliId) ?? 0) >= perCliCap) continue;
5848
+ if ((cliInFlight.get(botSnap.cliId) ?? 0) >= perCliCap(botSnap.cliId)) continue;
4571
5849
  if (startWork(node, botSnap, botKey, events, a.loop, a.omitted, a.instanceId)) {
4572
5850
  startedThisTick++;
4573
5851
  }
@@ -4576,7 +5854,7 @@ async function runWorkflow(dag, deps, opts) {
4576
5854
  if (startGate(nodesById.get(a.nodeId), a.instanceId)) startedThisTick++;
4577
5855
  }
4578
5856
  }
4579
- const cancels = actions.filter((a) => a.kind === "cancelNode");
5857
+ const cancels = actions.filter((a) => a.kind === "cancelNode" && !heldByProposal(a.nodeId) && !checkpointHeld.has(a.byNodeId));
4580
5858
  let cancelledThisTick = false;
4581
5859
  for (const a of cancels) {
4582
5860
  cancelledThisTick = applyCancelNode(a) || cancelledThisTick;
@@ -4585,20 +5863,31 @@ async function runWorkflow(dag, deps, opts) {
4585
5863
  if (inFlight.size === 0) {
4586
5864
  if (startedThisTick === 0) {
4587
5865
  const pendingWaits = gateMode === "suspend" ? pendingGateWaits(snap.nodes) : [];
4588
- if (pendingWaits.length > 0) {
5866
+ const latestEvents = readJournal(journalPath);
5867
+ if (externalMode && latestEvents.length !== events.length) continue;
5868
+ const waitingExternal = openV3ExternalAttempts(latestEvents);
5869
+ if (latestEvents.length !== events.length && currentV3Revision(latestEvents) !== dagRevision) continue;
5870
+ if (pendingWaits.length > 0 || waitingExternal.length > 0 || heldThisTick > 0) {
4589
5871
  const open = openV3WorkerAttempts(events);
4590
5872
  if (open.length > 0) {
4591
5873
  const drained = await quiesceAttempts(open, "orphanRecovery");
4592
- if (!drained) await new Promise((resolve5) => setTimeout(resolve5, 250));
5874
+ if (!drained) await new Promise((resolve6) => setTimeout(resolve6, 250));
4593
5875
  continue;
4594
5876
  }
4595
- return { reason: "awaitingGate", pendingWaits, runDir };
5877
+ if (waitingExternal.length > 0) return awaitingExternalOutcome(waitingExternal, pendingWaits);
5878
+ return {
5879
+ reason: "awaitingGate",
5880
+ pendingWaits,
5881
+ ...heldThisTick > 0 && pendingRevision !== void 0 ? { pendingRevision } : {},
5882
+ runDir
5883
+ };
4596
5884
  }
4597
5885
  throw new Error("v3 runtime: no progress possible and run is not terminal");
4598
5886
  }
4599
5887
  }
4600
5888
  if (inFlight.size > 0) await waitForInFlightOrDurableCancellation();
4601
5889
  }
5890
+ forgetProbeFailuresOf(runDir);
4602
5891
  const finalSnap = materialize(readJournal(journalPath));
4603
5892
  return {
4604
5893
  reason: "terminal",
@@ -4621,21 +5910,25 @@ async function runWorkflow(dag, deps, opts) {
4621
5910
  if (isTrueRunTerminal(snap.runStatus) || snap.runStatus === "cancelling") return;
4622
5911
  append({
4623
5912
  type: "runCancelRequested",
4624
- cancelRequestId: `cancel-${(0, import_node_crypto7.randomUUID)()}`,
5913
+ cancelRequestId: `cancel-${(0, import_node_crypto9.randomUUID)()}`,
4625
5914
  by: "runtime-signal"
4626
5915
  }, { durable: true });
4627
5916
  });
4628
5917
  }
4629
5918
  function appendRunTerminalIfQuiescent() {
4630
5919
  return withJournalMutationSync(journalPath, ({ events, append }) => {
5920
+ if (!decisionStillCurrent(events)) return false;
4631
5921
  const snap = materialize([...events]);
4632
5922
  if (snap.runStatus !== "running") return false;
4633
5923
  if (openV3WorkerAttempts(events).length > 0) return false;
5924
+ const pendingProposal = foldV3Revisions(events).pending;
4634
5925
  if (openV3HostEffects(events).length > 0) return false;
5926
+ if (openV3ExternalAttempts(events).length > 0) return false;
4635
5927
  const terminal = decideNext(dag, snap.nodes, snap.loops, snap.edges, snap.instances).find(
4636
5928
  (action) => action.kind === "completeRunSucceeded" || action.kind === "completeRunFailed" || action.kind === "completeRunBlocked"
4637
5929
  );
4638
5930
  if (!terminal) return false;
5931
+ if (terminal.kind === "completeRunSucceeded" && pendingProposal) return false;
4639
5932
  const event = terminal.kind === "completeRunSucceeded" ? { type: "runSucceeded" } : terminal.kind === "completeRunFailed" ? {
4640
5933
  type: "runFailed",
4641
5934
  failedNodeId: terminal.failedNodeId,
@@ -4648,21 +5941,42 @@ async function runWorkflow(dag, deps, opts) {
4648
5941
  }
4649
5942
  function claimDispatch(event) {
4650
5943
  return withJournalMutationSync(journalPath, ({ events, append }) => {
5944
+ if (!decisionStillCurrent(
5945
+ events,
5946
+ event.nodeId,
5947
+ event.type === "nodeDispatched" ? event.loop?.loopId : void 0
5948
+ )) return false;
4651
5949
  const snap = materialize([...events]);
4652
5950
  if (snap.runStatus !== "running") return false;
4653
5951
  const status = snap.nodes.get(event.nodeId)?.status ?? "pending";
4654
5952
  if (status !== "pending") return false;
5953
+ if (event.type === "gateDispatched" && gateWaitAlreadyDecided(events, event.waitId)) {
5954
+ throw new Error(
5955
+ `v3 runtime: gate ${event.waitId} of node "${event.nodeId}" was already decided, but the node has no approval to run on; refusing to re-dispatch the same gate`
5956
+ );
5957
+ }
4655
5958
  append(event, { durable: true });
4656
5959
  return true;
4657
5960
  });
4658
5961
  }
5962
+ function gateWaitAlreadyDecided(events, waitId) {
5963
+ return events.some((event) => event.type === "gateResolved" && event.waitId === waitId || event.type === "dagRevised" && (event.effects?.supersede?.some((item) => item.waitId === waitId) === true || event.effects?.cancel?.some((item) => item.waitId === waitId) === true));
5964
+ }
5965
+ function revisionSignature(events) {
5966
+ let count = 0;
5967
+ for (const event of events) {
5968
+ if (event.type === "dagRevised" || event.type === "dagRevisionProposed" || event.type === "dagRevisionRejected") count++;
5969
+ }
5970
+ return count;
5971
+ }
4659
5972
  function isRunCancelling() {
4660
5973
  return materialize(readJournal(journalPath)).runStatus === "cancelling";
4661
5974
  }
4662
5975
  async function waitForInFlightOrDurableCancellation() {
4663
5976
  const active = [...inFlight.values()];
4664
5977
  if (active.length === 0) return;
4665
- await new Promise((resolve5, reject) => {
5978
+ const revisionSignatureAtStart = revisionSignature(readJournal(journalPath));
5979
+ await new Promise((resolve6, reject) => {
4666
5980
  let done = false;
4667
5981
  let timer;
4668
5982
  const signal = opts.cancelSignal;
@@ -4671,13 +5985,14 @@ async function runWorkflow(dag, deps, opts) {
4671
5985
  done = true;
4672
5986
  if (timer) clearTimeout(timer);
4673
5987
  signal?.removeEventListener("abort", finish);
4674
- resolve5();
5988
+ resolve6();
4675
5989
  };
4676
5990
  for (const promise of active) promise.then(finish, finish);
4677
5991
  const poll = () => {
4678
5992
  if (done) return;
4679
5993
  try {
4680
- if (signal?.aborted || isRunCancelling()) {
5994
+ const latest = readJournal(journalPath);
5995
+ if (signal?.aborted || materialize(latest).runStatus === "cancelling" || revisionSignature(latest) !== revisionSignatureAtStart) {
4681
5996
  finish();
4682
5997
  return;
4683
5998
  }
@@ -4840,10 +6155,10 @@ async function runWorkflow(dag, deps, opts) {
4840
6155
  return allDrained;
4841
6156
  }
4842
6157
  function safeAttemptDirFromId(attemptId) {
4843
- const root = (0, import_node_path9.resolve)(runDir);
4844
- const candidate = (0, import_node_path9.resolve)(root, attemptId);
4845
- const rel = (0, import_node_path9.relative)(root, candidate);
4846
- if (!rel || (0, import_node_path9.isAbsolute)(rel) || rel === ".." || rel.startsWith(`..${import_node_path9.sep}`)) {
6158
+ const root = (0, import_node_path13.resolve)(runDir);
6159
+ const candidate = (0, import_node_path13.resolve)(root, attemptId);
6160
+ const rel = (0, import_node_path13.relative)(root, candidate);
6161
+ if (!rel || (0, import_node_path13.isAbsolute)(rel) || rel === ".." || rel.startsWith(`..${import_node_path13.sep}`)) {
4847
6162
  throw new Error(`v3 runtime: attempt path escapes run dir (${attemptId})`);
4848
6163
  }
4849
6164
  return candidate;
@@ -4880,6 +6195,7 @@ async function runWorkflow(dag, deps, opts) {
4880
6195
  const snap = materialize([...events]);
4881
6196
  if (snap.runStatus === "cancelled") return;
4882
6197
  if (snap.runStatus !== "cancelling" || snap.cancelRequestId !== cancelRequestId) return;
6198
+ if (openV3ExternalAttempts(events).length > 0) return;
4883
6199
  const ids = /* @__PURE__ */ new Set([
4884
6200
  ...dag.nodes.map((node) => node.id),
4885
6201
  ...snap.nodes.keys()
@@ -4966,7 +6282,7 @@ async function runWorkflow(dag, deps, opts) {
4966
6282
  (event) => event.type === "nodeSucceeded" && (event.instanceId ?? event.nodeId) === key
4967
6283
  );
4968
6284
  if (!success) throw new Error(`v3 host input source "${nodeId}" has no successful current instance`);
4969
- const outputDir = (0, import_node_path9.join)((0, import_node_path9.dirname)(success.manifestPath), "work");
6285
+ const outputDir = (0, import_node_path13.join)((0, import_node_path13.dirname)(success.manifestPath), "work");
4970
6286
  const verdict = await deps.validateManifest(success.manifestPath, outputDir);
4971
6287
  if (!verdict.ok || verdict.manifest?.status !== "ok") {
4972
6288
  throw new Error(
@@ -4978,7 +6294,7 @@ async function runWorkflow(dag, deps, opts) {
4978
6294
  throw new Error(`v3 host input source "${nodeId}" must expose exactly one result.json (found ${matches.length})`);
4979
6295
  }
4980
6296
  try {
4981
- return JSON.parse((0, import_node_fs11.readFileSync)((0, import_node_path9.join)(outputDir, matches[0].path), "utf-8"));
6297
+ return JSON.parse((0, import_node_fs15.readFileSync)((0, import_node_path13.join)(outputDir, matches[0].path), "utf-8"));
4982
6298
  } catch (err) {
4983
6299
  throw new Error(
4984
6300
  `v3 host input source "${nodeId}" result.json is unreadable: ${err instanceof Error ? err.message : String(err)}`
@@ -5034,6 +6350,7 @@ async function runWorkflow(dag, deps, opts) {
5034
6350
  executionContext
5035
6351
  });
5036
6352
  const committed = withJournalMutationSync(journalPath, ({ events: latest, append }) => {
6353
+ if (!decisionStillCurrent(latest, node.id)) return false;
5037
6354
  const current = materialize([...latest]);
5038
6355
  if (current.runStatus !== "running") return false;
5039
6356
  if ((current.nodes.get(node.id)?.status ?? "pending") !== "pending") return false;
@@ -5106,6 +6423,7 @@ async function runWorkflow(dag, deps, opts) {
5106
6423
  executionContext
5107
6424
  });
5108
6425
  withJournalMutationSync(journalPath, ({ events: latest, append }) => {
6426
+ if (!decisionStillCurrent(latest, node.id)) return;
5109
6427
  const current = materialize([...latest]);
5110
6428
  if (current.runStatus !== "running") return;
5111
6429
  const status = current.nodes.get(node.id)?.status ?? "pending";
@@ -5204,6 +6522,7 @@ async function runWorkflow(dag, deps, opts) {
5204
6522
  return blocked;
5205
6523
  }
5206
6524
  const claimed = withJournalMutationSync(journalPath, ({ events: latest, append }) => {
6525
+ if (!decisionStillCurrent(latest, node.id)) return false;
5207
6526
  const current = materialize([...latest]);
5208
6527
  if (current.runStatus !== "running") return false;
5209
6528
  const state = current.nodes.get(node.id);
@@ -5321,9 +6640,9 @@ async function runWorkflow(dag, deps, opts) {
5321
6640
  continue;
5322
6641
  }
5323
6642
  const attemptDir = safeAttemptDirFromId(effect.attemptId);
5324
- const existingManifest = (0, import_node_path9.join)(attemptDir, "manifest.json");
5325
- if ((0, import_node_fs11.existsSync)(existingManifest)) {
5326
- const validated = await deps.validateManifest(existingManifest, (0, import_node_path9.join)(attemptDir, "work"));
6643
+ const existingManifest = (0, import_node_path13.join)(attemptDir, "manifest.json");
6644
+ if ((0, import_node_fs15.existsSync)(existingManifest)) {
6645
+ const validated = await deps.validateManifest(existingManifest, (0, import_node_path13.join)(attemptDir, "work"));
5327
6646
  if (validated.ok && validated.manifest?.status === "ok") {
5328
6647
  try {
5329
6648
  readAndVerifyV3HostSuccessResult({
@@ -5600,47 +6919,434 @@ async function runWorkflow(dag, deps, opts) {
5600
6919
  function computeHostCanonicalHash(value) {
5601
6920
  return computeInputHash(value);
5602
6921
  }
5603
- function startWork(node, botSnap, botKey, events, loopRef, omitted, instanceId) {
6922
+ function writeWorkflowDataFile(node, attemptDir) {
6923
+ if (!executionContext) return void 0;
6924
+ const bindings = savedWorkflowBindingsForNode(node);
6925
+ if (bindings.params.length === 0 && bindings.context.length === 0) return void 0;
6926
+ const params = /* @__PURE__ */ Object.create(null);
6927
+ const context = /* @__PURE__ */ Object.create(null);
6928
+ for (const name of bindings.params) {
6929
+ if (!Object.prototype.hasOwnProperty.call(executionContext.params, name)) {
6930
+ throw new Error(`v3 runtime: resolved params missing referenced key ${name}`);
6931
+ }
6932
+ params[name] = executionContext.params[name];
6933
+ }
6934
+ for (const name of bindings.context) {
6935
+ if (!Object.prototype.hasOwnProperty.call(executionContext.context, name)) {
6936
+ throw new Error(`v3 runtime: resolved context missing referenced key ${name}`);
6937
+ }
6938
+ context[name] = executionContext.context[name];
6939
+ }
6940
+ const workflowDataPath = (0, import_node_path13.join)(attemptDir, "workflow-inputs.json");
6941
+ (0, import_node_fs15.writeFileSync)(
6942
+ workflowDataPath,
6943
+ `${JSON.stringify({ params, context }, null, 2)}
6944
+ `,
6945
+ { mode: 384 }
6946
+ );
6947
+ return workflowDataPath;
6948
+ }
6949
+ function reportExternalError(error, attemptId, operation) {
6950
+ try {
6951
+ deps.onExternalError?.(error, { attemptId, operation });
6952
+ } catch {
6953
+ }
6954
+ }
6955
+ async function stopExternal(attempt, reason) {
6956
+ const stopped = await stopExternalAttempt(externalExecutor, dag.runId, attempt, reason, externalCallTimeoutMs);
6957
+ if (!stopped.ok) reportExternalError(stopped.error, attempt.attemptId, "stop");
6958
+ }
6959
+ function externalNodeFor(attempt) {
6960
+ return attempt.loop ? instanceNodeFor(attempt.loop) : nodesById.get(attempt.nodeId);
6961
+ }
6962
+ function startExternal(node, botSnap, events, loopRef, omitted, instanceId) {
6963
+ if (!isGoalNode(node)) throw new Error(`v3 runtime: node "${node.id}" is not a goal node`);
5604
6964
  const dispatchKey = instanceId ?? node.id;
5605
6965
  const attemptId = nextAttemptIdFor(events, dispatchKey);
5606
6966
  const attemptNNN = attemptId.slice(attemptId.lastIndexOf("/") + 1);
5607
- const attemptDir = (0, import_node_path9.join)(runDir, dispatchKey, "attempts", attemptNNN);
5608
- const outputDir = (0, import_node_path9.join)(attemptDir, "work");
5609
- (0, import_node_fs11.mkdirSync)(outputDir, { recursive: true });
5610
- let workflowDataPath;
5611
- if (executionContext) {
5612
- const bindings = savedWorkflowBindingsForNode(node);
5613
- if (bindings.params.length > 0 || bindings.context.length > 0) {
5614
- const params = /* @__PURE__ */ Object.create(null);
5615
- const context = /* @__PURE__ */ Object.create(null);
5616
- for (const name of bindings.params) {
5617
- if (!Object.prototype.hasOwnProperty.call(executionContext.params, name)) {
5618
- throw new Error(`v3 runtime: resolved params missing referenced key ${name}`);
5619
- }
5620
- params[name] = executionContext.params[name];
6967
+ const attemptDir = (0, import_node_path13.join)(runDir, dispatchKey, "attempts", attemptNNN);
6968
+ (0, import_node_fs15.mkdirSync)(attemptDir, { recursive: true });
6969
+ const workflowDataPath = writeWorkflowDataFile(node, attemptDir);
6970
+ (0, import_node_fs15.writeFileSync)(
6971
+ (0, import_node_path13.join)(attemptDir, "inputs.json"),
6972
+ JSON.stringify(buildInputs(node, events, attemptId, loopRef, omitted, workflowDataPath), null, 2)
6973
+ );
6974
+ const claimed = withJournalMutationSync(journalPath, ({ events: latest, append }) => {
6975
+ if (!decisionStillCurrent(latest, node.id, loopRef?.loopId)) return false;
6976
+ const current = materialize([...latest]);
6977
+ if (current.runStatus !== "running") return false;
6978
+ if ((current.nodes.get(node.id)?.status ?? "pending") !== "pending") return false;
6979
+ const identity = { nodeId: node.id, ...instanceId ? { instanceId } : {}, attemptId };
6980
+ append({
6981
+ type: "nodeDispatched",
6982
+ ...identity,
6983
+ loop: loopRef,
6984
+ nodeExecutor: "bot",
6985
+ ...dagRevision > 1 ? { revision: dagRevision } : {}
6986
+ }, { durable: true });
6987
+ append({
6988
+ type: "nodeHandoffPrepared",
6989
+ ...identity,
6990
+ targetLarkAppId: current.assignments.get(node.id) ?? botSnap.larkAppId,
6991
+ ...externalPlacementIntent(loopRef ? void 0 : node),
6992
+ revision: dagRevision,
6993
+ preparedAt: clock()
6994
+ }, { durable: true });
6995
+ return true;
6996
+ });
6997
+ if (!claimed) return false;
6998
+ const attempt = foldV3ExternalAttempts(readJournal(journalPath)).get(attemptId);
6999
+ if (attempt && !attempt.closed) startHandoffSend(attempt, false);
7000
+ return true;
7001
+ }
7002
+ function externalPlacementIntent(node) {
7003
+ return node && isGoalNode(node) && node.placement === "main" ? { placement: "main", idempotency: { handoff: (0, import_node_crypto9.randomUUID)() } } : { placement: "topic", idempotency: { nodeCard: (0, import_node_crypto9.randomUUID)(), handoff: (0, import_node_crypto9.randomUUID)() } };
7004
+ }
7005
+ function appendMissingHandoffIntent(attempt) {
7006
+ const node = externalNodeFor(attempt);
7007
+ const frozen = node ? snapshotForSelector(botSnapshots, node.bot) : void 0;
7008
+ if (!frozen) return false;
7009
+ return withJournalMutationSync(journalPath, ({ events, append }) => {
7010
+ const current = classifyExternalAttempt(events, attempt.attemptId);
7011
+ if (!current.ok || current.attempt.phase !== "dispatched") return false;
7012
+ const dispatched = events.find((event) => event.type === "nodeDispatched" && event.attemptId === attempt.attemptId);
7013
+ append({
7014
+ type: "nodeHandoffPrepared",
7015
+ nodeId: attempt.nodeId,
7016
+ ...attempt.instanceId ? { instanceId: attempt.instanceId } : {},
7017
+ attemptId: attempt.attemptId,
7018
+ targetLarkAppId: current.snap.assignments.get(attempt.nodeId) ?? frozen.larkAppId,
7019
+ ...externalPlacementIntent(attempt.loop ? void 0 : node),
7020
+ revision: dispatched?.revision ?? 1,
7021
+ preparedAt: clock()
7022
+ }, { durable: true });
7023
+ return true;
7024
+ });
7025
+ }
7026
+ function buildExternalDispatchRequest(attempt, redispatch, signal) {
7027
+ const ref = externalAttemptRef(dag.runId, attempt);
7028
+ const node = externalNodeFor(attempt);
7029
+ if (!ref || !attempt.idempotency || !node || !isGoalNode(node)) {
7030
+ throw new Error(`v3 runtime: external attempt ${attempt.attemptId} has no prepared goal handoff`);
7031
+ }
7032
+ const attemptDir = safeAttemptDirFromId(attempt.attemptId);
7033
+ const inputsPath = (0, import_node_path13.join)(attemptDir, "inputs.json");
7034
+ const inputs = JSON.parse((0, import_node_fs15.readFileSync)(inputsPath, "utf-8"));
7035
+ const events = readJournal(journalPath);
7036
+ const key = attempt.instanceId ?? attempt.nodeId;
7037
+ const retry = events.find(
7038
+ (event) => event.type === "nodeRetryRequested" && (event.instanceId ?? event.nodeId) === key && event.nextAttemptId === attempt.attemptId
7039
+ );
7040
+ const previousBlock = retry ? [...events].reverse().find(
7041
+ (event) => event.type === "nodeBlocked" && event.attemptId === retry.previousAttemptId
7042
+ ) : void 0;
7043
+ const reassigned = [...events].reverse().find(
7044
+ (event) => event.type === "nodeReassigned" && event.nodeId === attempt.nodeId && event.to === ref.targetLarkAppId
7045
+ );
7046
+ const revised = [...events].reverse().find(
7047
+ (event) => event.type === "dagRevised" && event.revision <= ref.revision && (event.effects?.supersede?.some((item) => item.nodeId === attempt.nodeId) === true || event.effects?.assign?.some((item) => item.nodeId === attempt.nodeId) === true)
7048
+ );
7049
+ const revisedAssign = revised?.effects?.assign?.find((item) => item.nodeId === attempt.nodeId && item.to === ref.targetLarkAppId);
7050
+ return {
7051
+ ...ref,
7052
+ idempotency: attempt.idempotency,
7053
+ redispatch,
7054
+ node,
7055
+ attemptNumber: attemptNumber(attempt.attemptId) ?? 1,
7056
+ ...attempt.loop ? { loop: attempt.loop } : {},
7057
+ inputs,
7058
+ inputsPath,
7059
+ attemptDir,
7060
+ runDir,
7061
+ timeoutSec: node.timeoutSec ?? DEFAULT_NODE_TIMEOUT_SEC,
7062
+ ...opts.chatBinding ? { chatBinding: opts.chatBinding } : {},
7063
+ ...retry ? {
7064
+ retryOf: {
7065
+ attemptId: retry.previousAttemptId,
7066
+ ...retry.previousErrorCode ? { errorCode: retry.previousErrorCode } : {},
7067
+ ...previousBlock?.message ? { message: previousBlock.message } : {}
5621
7068
  }
5622
- for (const name of bindings.context) {
5623
- if (!Object.prototype.hasOwnProperty.call(executionContext.context, name)) {
5624
- throw new Error(`v3 runtime: resolved context missing referenced key ${name}`);
7069
+ } : {},
7070
+ ...reassigned ? { reassignedFrom: reassigned.from } : revisedAssign?.from ? { reassignedFrom: revisedAssign.from } : {},
7071
+ ...revised ? { revisedBy: { revision: revised.revision, summary: revised.summary } } : {},
7072
+ signal
7073
+ };
7074
+ }
7075
+ function startHandoffSend(attempt, redispatch) {
7076
+ const executor = externalExecutor;
7077
+ const key = `${attempt.instanceId ?? attempt.nodeId}::handoff`;
7078
+ if (!executor || inFlight.has(key) || handoffControllers.has(attempt.attemptId)) return;
7079
+ const controller = new AbortController();
7080
+ handoffControllers.set(attempt.attemptId, controller);
7081
+ const p = Promise.resolve().then(() => withExternalCallTimeout(
7082
+ Promise.resolve().then(() => executor.dispatch(buildExternalDispatchRequest(attempt, redispatch, controller.signal))),
7083
+ externalCallTimeoutMs,
7084
+ "dispatch"
7085
+ ).catch((err) => {
7086
+ if (!controller.signal.aborted) controller.abort(err);
7087
+ throw err;
7088
+ })).then(async (outcome) => {
7089
+ handoffRetryAt.delete(attempt.attemptId);
7090
+ if (outcome.kind === "rejected") {
7091
+ withJournalMutationSync(journalPath, ({ events, append }) => {
7092
+ const current = classifyExternalAttempt(events, attempt.attemptId);
7093
+ if (!current.ok || current.attempt.phase !== "prepared") return;
7094
+ append({
7095
+ type: "nodeBlocked",
7096
+ nodeId: attempt.nodeId,
7097
+ ...attempt.instanceId ? { instanceId: attempt.instanceId } : {},
7098
+ attemptId: attempt.attemptId,
7099
+ errorClass: "workerError",
7100
+ errorCode: outcome.code,
7101
+ message: outcome.message ?? `handoff was not sent (${outcome.code})`
7102
+ }, { durable: true });
7103
+ });
7104
+ return;
7105
+ }
7106
+ acceptanceObserved.add(attempt.attemptId);
7107
+ const sentAt = clock();
7108
+ const recorded = withJournalMutationSync(journalPath, ({ events, append }) => {
7109
+ const latest = foldV3ExternalAttempts(events).get(attempt.attemptId);
7110
+ if (!latest || latest.closed) return false;
7111
+ if (latest.handoffMessageId !== void 0) return true;
7112
+ append({
7113
+ type: "nodeHandoffSent",
7114
+ nodeId: attempt.nodeId,
7115
+ ...attempt.instanceId ? { instanceId: attempt.instanceId } : {},
7116
+ attemptId: attempt.attemptId,
7117
+ handoffMessageId: outcome.handoffMessageId,
7118
+ ...outcome.nodeCardMessageId ? { nodeCardMessageId: outcome.nodeCardMessageId } : {},
7119
+ ...outcome.threadId ? { threadId: outcome.threadId } : {},
7120
+ sentAt,
7121
+ acceptDeadlineAt: sentAt + externalAcceptTimeoutMs
7122
+ }, { durable: true });
7123
+ return true;
7124
+ });
7125
+ if (!recorded) {
7126
+ await stopExternal({ ...attempt, handoffMessageId: outcome.handoffMessageId }, "obsolete");
7127
+ }
7128
+ }).catch((err) => {
7129
+ reportExternalError(err, attempt.attemptId, "dispatch");
7130
+ handoffRetryAt.set(attempt.attemptId, clock() + HANDOFF_RESEND_BACKOFF_MS);
7131
+ }).finally(() => {
7132
+ if (handoffControllers.get(attempt.attemptId) === controller) {
7133
+ handoffControllers.delete(attempt.attemptId);
7134
+ }
7135
+ if (inFlight.get(key) === p) inFlight.delete(key);
7136
+ });
7137
+ inFlight.set(key, p);
7138
+ }
7139
+ async function blockExternalAttempt(attempt, stopReason, verdict) {
7140
+ if (hasExternalInstallIntent(runDir, attempt.attemptId)) return true;
7141
+ await stopExternal(attempt, stopReason);
7142
+ return withJournalMutationSync(journalPath, ({ events, append }) => {
7143
+ if (hasExternalInstallIntent(runDir, attempt.attemptId)) return true;
7144
+ const current = classifyExternalAttempt(events, attempt.attemptId);
7145
+ if (!current.ok) return false;
7146
+ append({
7147
+ type: "nodeBlocked",
7148
+ nodeId: attempt.nodeId,
7149
+ ...attempt.instanceId ? { instanceId: attempt.instanceId } : {},
7150
+ attemptId: attempt.attemptId,
7151
+ ...verdict
7152
+ }, { durable: true });
7153
+ return true;
7154
+ });
7155
+ }
7156
+ async function probeExternalAcceptance(attempt) {
7157
+ const ref = externalAttemptRef(dag.runId, attempt);
7158
+ const executor = externalExecutor;
7159
+ if (!executor || !ref || !attempt.handoffMessageId) return { status: "unknown" };
7160
+ const handoffMessageId = attempt.handoffMessageId;
7161
+ try {
7162
+ const probe = await withExternalCallTimeout(
7163
+ Promise.resolve().then(() => executor.probeAcceptance({ ...ref, handoffMessageId })),
7164
+ externalCallTimeoutMs,
7165
+ "probe"
7166
+ );
7167
+ return probe.status === "accepted" ? {
7168
+ status: "accepted",
7169
+ sessionId: probe.sessionId,
7170
+ ...probe.acceptedAt !== void 0 ? { acceptedAt: probe.acceptedAt } : {}
7171
+ } : { status: "notAccepted", ...typeof probe.detail === "string" && probe.detail ? { detail: probe.detail.slice(0, 2e3) } : {} };
7172
+ } catch (err) {
7173
+ reportExternalError(err, attempt.attemptId, "probe");
7174
+ return { status: "unknown" };
7175
+ }
7176
+ }
7177
+ async function reconcileExternalAttempts(events) {
7178
+ if (!externalMode) return false;
7179
+ let progressed = false;
7180
+ for (const attempt of openV3ExternalAttempts(events)) {
7181
+ if (handoffControllers.has(attempt.attemptId)) continue;
7182
+ if (!classifyExternalAttempt(events, attempt.attemptId).ok) continue;
7183
+ const now = clock();
7184
+ if (attempt.handedToHuman) {
7185
+ await blockExternalAttempt(attempt, "handedToHuman", {
7186
+ errorClass: "workerError",
7187
+ errorCode: V3_EXTERNAL_ERROR_CODES.HANDED_TO_HUMAN,
7188
+ message: attempt.handedToHuman.reason,
7189
+ recovery: "retry"
7190
+ });
7191
+ progressed = true;
7192
+ continue;
7193
+ }
7194
+ if (attempt.phase === "dispatched") {
7195
+ progressed = appendMissingHandoffIntent(attempt) || progressed;
7196
+ } else if (attempt.phase === "prepared") {
7197
+ if (now >= attempt.preparedAt + V3_HANDOFF_IDEMPOTENCY_WINDOW_MS) {
7198
+ await blockExternalAttempt(attempt, "uncertain", {
7199
+ errorClass: "workerError",
7200
+ errorCode: V3_EXTERNAL_ERROR_CODES.HANDOFF_UNCERTAIN,
7201
+ message: "handoff delivery could not be confirmed within the idempotency window; check the chat before retrying"
7202
+ });
7203
+ progressed = true;
7204
+ } else if (now >= (handoffRetryAt.get(attempt.attemptId) ?? 0)) {
7205
+ startHandoffSend(attempt, true);
7206
+ }
7207
+ } else if (attempt.phase === "sent") {
7208
+ const due = now >= attempt.acceptDeadlineAt;
7209
+ const key = probeKey(attempt.attemptId);
7210
+ const failure = probeFailures.get(key);
7211
+ const probeDue = failure ? now >= failure.retryAt : !acceptanceObserved.has(attempt.attemptId) || due;
7212
+ if (probeDue) {
7213
+ const probe = await probeExternalAcceptance(attempt);
7214
+ progressed = true;
7215
+ if (probe.status === "unknown") {
7216
+ const firstFailedAt = failure?.firstFailedAt ?? now;
7217
+ if (due && now - firstFailedAt >= PROBE_UNCERTAIN_AFTER_MS) {
7218
+ probeFailures.delete(key);
7219
+ await blockExternalAttempt(attempt, "uncertain", {
7220
+ errorClass: "workerError",
7221
+ errorCode: V3_EXTERNAL_ERROR_CODES.HANDOFF_UNCERTAIN,
7222
+ message: "whether the target bot accepted the handoff could not be confirmed; check the chat before retrying"
7223
+ });
7224
+ } else {
7225
+ probeFailures.set(key, { firstFailedAt, retryAt: now + PROBE_RETRY_BACKOFF_MS });
7226
+ }
7227
+ } else {
7228
+ probeFailures.delete(key);
7229
+ acceptanceObserved.add(attempt.attemptId);
7230
+ if (probe.status === "accepted") {
7231
+ const { sessionId, acceptedAt } = probe;
7232
+ withJournalMutationSync(journalPath, ({ events: latest, append }) => appendAcceptedLocked(latest, append, dag, {
7233
+ attemptId: attempt.attemptId,
7234
+ sessionId,
7235
+ ...acceptedAt !== void 0 ? { acceptedAt } : {}
7236
+ }, clock()));
7237
+ } else if (due) {
7238
+ await blockExternalAttempt(attempt, "notAccepted", {
7239
+ errorClass: "workerError",
7240
+ errorCode: V3_EXTERNAL_ERROR_CODES.HANDOFF_NOT_ACCEPTED,
7241
+ message: probe.detail ? `the target bot did not accept the handoff before the acceptance deadline: ${probe.detail}` : "the target bot did not accept the handoff before the acceptance deadline"
7242
+ });
7243
+ }
5625
7244
  }
5626
- context[name] = executionContext.context[name];
5627
7245
  }
5628
- workflowDataPath = (0, import_node_path9.join)(attemptDir, "workflow-inputs.json");
5629
- (0, import_node_fs11.writeFileSync)(
5630
- workflowDataPath,
5631
- `${JSON.stringify({ params, context }, null, 2)}
5632
- `,
5633
- { mode: 384 }
5634
- );
7246
+ } else if (now >= attempt.deadlineAt) {
7247
+ await blockExternalAttempt(attempt, "timeout", {
7248
+ errorClass: "timeout",
7249
+ errorCode: V3_EXTERNAL_ERROR_CODES.NODE_TIMEOUT,
7250
+ message: attempt.startPending ? "\u5230\u671F\u9650\u8FD8\u6CA1\u6536\u5230\u8FD9\u4E00\u8F6E\u5F00\u59CB\u7684\u4FE1\u53F7\uFF1A\u591A\u534A\u662F\u4E00\u76F4\u5728\u6267\u884C Bot \u7684\u4E3B\u8BDD\u9898\u4F1A\u8BDD\u91CC\u6392\u961F\uFF08\u5B83\u524D\u9762\u7684\u56DE\u5408\u4E00\u76F4\u6CA1\u7ED3\u675F\uFF09\uFF0C\u4E5F\u53EF\u80FD\u662F\u5F00\u59CB\u4FE1\u53F7\u6CA1\u9001\u5230\uFF1B\u53EF\u4EE5\u91CD\u8BD5\uFF0C\u6216\u628A\u8282\u70B9\u6539\u6210\u8282\u70B9\u8BDD\u9898" : "the node did not submit a valid result before its timeout"
7251
+ });
7252
+ progressed = true;
7253
+ }
7254
+ }
7255
+ return progressed;
7256
+ }
7257
+ function settleExternalLeftovers(events) {
7258
+ let progressed = false;
7259
+ for (const attempt of foldV3ExternalAttempts(events).values()) {
7260
+ if (hasExternalInstallIntent(runDir, attempt.attemptId)) {
7261
+ if (settleExternalInstallIntent(runDir, dag, attempt.attemptId).kind !== "none") progressed = true;
7262
+ } else if (attempt.closed) {
7263
+ (0, import_node_fs15.rmSync)((0, import_node_path13.join)(safeAttemptDirFromId(attempt.attemptId), "submissions"), { recursive: true, force: true });
5635
7264
  }
5636
7265
  }
5637
- const goalPath = (0, import_node_path9.join)(attemptDir, "goal.txt");
7266
+ return progressed;
7267
+ }
7268
+ function obsoleteExternalAttempts(events, snap) {
7269
+ return openV3ExternalAttempts(events).filter((attempt) => {
7270
+ const state = attempt.instanceId ? snap.instances.get(attempt.instanceId) : snap.nodes.get(attempt.nodeId);
7271
+ return state?.status === "cancelled" || state?.status === "superseded";
7272
+ });
7273
+ }
7274
+ async function drainExternalAttempts(attempts, stopReason, drainReason) {
7275
+ for (const attempt of attempts) {
7276
+ await stopExternal(attempt, stopReason);
7277
+ withJournalMutationSync(journalPath, ({ events, append }) => {
7278
+ const latest = foldV3ExternalAttempts(events).get(attempt.attemptId);
7279
+ if (!latest || latest.closed) return;
7280
+ append({
7281
+ type: "nodeAttemptDrained",
7282
+ nodeId: attempt.nodeId,
7283
+ ...attempt.instanceId ? { instanceId: attempt.instanceId } : {},
7284
+ attemptId: attempt.attemptId,
7285
+ reason: drainReason
7286
+ }, { durable: true });
7287
+ });
7288
+ }
7289
+ }
7290
+ async function closeExternalAttemptsForCancellation(cancelRequestId) {
7291
+ for (const attempt of openV3ExternalAttempts(readJournal(journalPath))) {
7292
+ await stopExternal(attempt, "runCancelled");
7293
+ withJournalMutationSync(journalPath, ({ events, append }) => {
7294
+ const latest = foldV3ExternalAttempts(events).get(attempt.attemptId);
7295
+ if (!latest || latest.closed) return;
7296
+ const snap = materialize([...events]);
7297
+ if (snap.runStatus !== "cancelling" || snap.cancelRequestId !== cancelRequestId) return;
7298
+ append({
7299
+ type: "nodeAttemptDrained",
7300
+ nodeId: attempt.nodeId,
7301
+ ...attempt.instanceId ? { instanceId: attempt.instanceId } : {},
7302
+ attemptId: attempt.attemptId,
7303
+ reason: "runCancellation"
7304
+ }, { durable: true });
7305
+ });
7306
+ }
7307
+ }
7308
+ function sentWakeAt(attempt, now) {
7309
+ const failure = probeFailures.get(probeKey(attempt.attemptId));
7310
+ if (!failure) return attempt.acceptDeadlineAt;
7311
+ return attempt.acceptDeadlineAt > now ? Math.min(attempt.acceptDeadlineAt, failure.retryAt) : failure.retryAt;
7312
+ }
7313
+ function awaitingExternalOutcome(open, pendingWaits) {
7314
+ const now = clock();
7315
+ const externalAttempts = open.map((attempt) => {
7316
+ const wakeAt = attempt.phase === "dispatched" ? now : attempt.phase === "prepared" ? Math.min(handoffRetryAt.get(attempt.attemptId) ?? now, attempt.preparedAt + V3_HANDOFF_IDEMPOTENCY_WINDOW_MS) : attempt.phase === "sent" ? sentWakeAt(attempt, now) : attempt.deadlineAt;
7317
+ return {
7318
+ nodeId: attempt.nodeId,
7319
+ ...attempt.instanceId ? { instanceId: attempt.instanceId } : {},
7320
+ attemptId: attempt.attemptId,
7321
+ phase: attempt.phase,
7322
+ ...attempt.targetLarkAppId ? { targetLarkAppId: attempt.targetLarkAppId } : {},
7323
+ ...wakeAt !== void 0 ? { wakeAt } : {}
7324
+ };
7325
+ });
7326
+ const wakes = externalAttempts.flatMap((attempt) => attempt.wakeAt !== void 0 ? [attempt.wakeAt] : []);
7327
+ return {
7328
+ reason: "awaitingExternal",
7329
+ pendingWaits,
7330
+ externalAttempts,
7331
+ ...wakes.length > 0 ? { wakeAt: Math.min(...wakes) } : {},
7332
+ runDir
7333
+ };
7334
+ }
7335
+ function startWork(node, botSnap, botKey, events, loopRef, omitted, instanceId) {
7336
+ const dispatchKey = instanceId ?? node.id;
7337
+ const attemptId = nextAttemptIdFor(events, dispatchKey);
7338
+ const attemptNNN = attemptId.slice(attemptId.lastIndexOf("/") + 1);
7339
+ const attemptDir = (0, import_node_path13.join)(runDir, dispatchKey, "attempts", attemptNNN);
7340
+ const outputDir = (0, import_node_path13.join)(attemptDir, "work");
7341
+ (0, import_node_fs15.mkdirSync)(outputDir, { recursive: true });
7342
+ const workflowDataPath = writeWorkflowDataFile(node, attemptDir);
7343
+ const goalPath = (0, import_node_path13.join)(attemptDir, "goal.txt");
5638
7344
  const loopCtx = loopRef ? {
5639
7345
  loopId: loopRef.loopId,
5640
7346
  iteration: loopRef.iteration,
5641
7347
  maxIterations: nodesById.get(loopRef.loopId).maxIterations
5642
7348
  } : void 0;
5643
- (0, import_node_fs11.writeFileSync)(
7349
+ (0, import_node_fs15.writeFileSync)(
5644
7350
  goalPath,
5645
7351
  renderGoalFile(
5646
7352
  node.goal ?? "",
@@ -5652,12 +7358,12 @@ async function runWorkflow(dag, deps, opts) {
5652
7358
  )
5653
7359
  );
5654
7360
  const effSnap = mergeNodeCapability(botSnap, node.override);
5655
- const inputsPath = (0, import_node_path9.join)(attemptDir, "inputs.json");
5656
- (0, import_node_fs11.writeFileSync)(
7361
+ const inputsPath = (0, import_node_path13.join)(attemptDir, "inputs.json");
7362
+ (0, import_node_fs15.writeFileSync)(
5657
7363
  inputsPath,
5658
7364
  JSON.stringify(buildInputs(node, events, attemptId, loopRef, omitted, workflowDataPath), null, 2)
5659
7365
  );
5660
- const manifestPath = (0, import_node_path9.join)(attemptDir, "manifest.json");
7366
+ const manifestPath = (0, import_node_path13.join)(attemptDir, "manifest.json");
5661
7367
  const env = {
5662
7368
  [GOAL_ENV.GOAL_PATH]: goalPath,
5663
7369
  [GOAL_ENV.INPUTS_PATH]: inputsPath,
@@ -5671,7 +7377,8 @@ async function runWorkflow(dag, deps, opts) {
5671
7377
  nodeId: node.id,
5672
7378
  ...instanceId ? { instanceId } : {},
5673
7379
  attemptId,
5674
- loop: loopRef
7380
+ loop: loopRef,
7381
+ ...dagRevision > 1 ? { revision: dagRevision } : {}
5675
7382
  })) return false;
5676
7383
  const leaseBinding = {
5677
7384
  attemptDir,
@@ -5877,7 +7584,7 @@ async function runWorkflow(dag, deps, opts) {
5877
7584
  });
5878
7585
  return;
5879
7586
  }
5880
- const res = validateResult((0, import_node_path9.join)(outputDir, entry.path), node.resultSchema);
7587
+ const res = validateResult((0, import_node_path13.join)(outputDir, entry.path), node.resultSchema);
5881
7588
  if (!res.ok) {
5882
7589
  appendWorkerOutcome({
5883
7590
  type: "nodeBlocked",
@@ -5937,7 +7644,7 @@ async function runWorkflow(dag, deps, opts) {
5937
7644
  }
5938
7645
  const kind = classifyTerminal(errorClass, { selfReportedFail, retryable });
5939
7646
  if (kind === "blocked") {
5940
- const ask = errorCode === ASK_HUMAN_ERROR_CODE ? readGoalAsk((0, import_node_path9.join)(attemptDir, GOAL_ASK_FILE)) : void 0;
7647
+ const ask = errorCode === ASK_HUMAN_ERROR_CODE ? readGoalAsk((0, import_node_path13.join)(attemptDir, GOAL_ASK_FILE)) : void 0;
5941
7648
  appendWorkerOutcome({
5942
7649
  type: "nodeBlocked",
5943
7650
  nodeId: node.id,
@@ -5994,58 +7701,19 @@ async function runWorkflow(dag, deps, opts) {
5994
7701
  inFlight.set(dispatchKey, p);
5995
7702
  return true;
5996
7703
  }
5997
- function affectedNodesFrom(root) {
5998
- const reachable = /* @__PURE__ */ new Set([root]);
5999
- let changed = true;
6000
- while (changed) {
6001
- changed = false;
6002
- for (const n of dag.nodes) {
6003
- if (reachable.has(n.id)) continue;
6004
- if (n.depends.some((d) => reachable.has(d.from))) {
6005
- reachable.add(n.id);
6006
- changed = true;
6007
- }
6008
- }
6009
- }
6010
- return [...reachable];
6011
- }
6012
7704
  function appendRevisitEvents(nodeId, instanceId, attemptId, request, sourceManifestPath) {
6013
- const events0 = readJournal(journalPath);
6014
- const snap = materialize(events0);
6015
- const targetEff = snap.nodes.get(request.toNodeId)?.effectiveInstanceId;
6016
- const targetSucc = targetEff ? [...events0].reverse().find((e) => e.type === "nodeSucceeded" && (e.instanceId ?? e.nodeId) === targetEff) : void 0;
6017
- let reasonPathRel;
6018
- if (request.reason) {
6019
- const dir = (0, import_node_path9.join)(runDir, "revisits");
6020
- (0, import_node_fs11.mkdirSync)(dir, { recursive: true });
6021
- const abs = (0, import_node_path9.join)(dir, `${instanceId.replace(/[#/]/g, "-")}-reason.md`);
6022
- (0, import_node_fs11.writeFileSync)(abs, `# Revisit reason (from ${nodeId} / ${instanceId})
6023
-
6024
- ${request.reason}
6025
- `);
6026
- reasonPathRel = (0, import_node_path9.relative)(runDir, abs);
6027
- }
6028
- appendEvent(journalPath, {
6029
- type: "nodeRevisitRequested",
7705
+ const plan = planV3Revisit({
7706
+ runDir,
7707
+ dag,
7708
+ events: readJournal(journalPath),
6030
7709
  nodeId,
6031
7710
  instanceId,
6032
7711
  attemptId,
6033
- toNodeId: request.toNodeId,
6034
- ...request.reason ? { reason: request.reason } : {},
6035
- ...reasonPathRel ? { reasonPath: reasonPathRel } : {},
6036
- sourceManifestPath: (0, import_node_path9.relative)(runDir, sourceManifestPath),
6037
- ...targetSucc ? { targetPreviousManifestPath: (0, import_node_path9.relative)(runDir, targetSucc.manifestPath) } : {}
7712
+ request,
7713
+ sourceManifestPath
6038
7714
  });
6039
- for (const affectedNodeId of affectedNodesFrom(request.toNodeId)) {
6040
- const eff = snap.nodes.get(affectedNodeId)?.effectiveInstanceId;
6041
- if (!eff) continue;
6042
- appendEvent(journalPath, {
6043
- type: "nodeInstanceSuperseded",
6044
- nodeId: affectedNodeId,
6045
- instanceId: eff,
6046
- byNodeId: request.toNodeId,
6047
- reason: "refresh"
6048
- });
7715
+ for (const event of plan.events) appendEvent(journalPath, event);
7716
+ for (const eff of plan.supersededInstanceIds) {
6049
7717
  const controller = nodeControllers.get(eff);
6050
7718
  if (controller && !controller.signal.aborted) {
6051
7719
  controller.abort({ kind: "attemptDrain", drainReason: "obsoleteAttempt" });
@@ -6167,11 +7835,11 @@ ${request.reason}
6167
7835
  }
6168
7836
  function applyLoopControl(a) {
6169
7837
  if (a.kind === "startLoop") {
6170
- appendEvent(journalPath, { type: "loopStarted", loopId: a.loopId });
7838
+ appendControlEvent({ type: "loopStarted", loopId: a.loopId });
6171
7839
  return;
6172
7840
  }
6173
7841
  if (a.kind === "startLoopIteration") {
6174
- appendEvent(journalPath, { type: "loopIterationStarted", loopId: a.loopId, iteration: a.iteration });
7842
+ appendControlEvent({ type: "loopIterationStarted", loopId: a.loopId, iteration: a.iteration });
6175
7843
  return;
6176
7844
  }
6177
7845
  const loopNode = nodesById.get(a.loopId);
@@ -6180,7 +7848,7 @@ ${request.reason}
6180
7848
  const outInstId = loopInstanceId(a.loopId, a.iteration, loopNode.output.from);
6181
7849
  const succ2 = [...events2].reverse().find((e) => e.type === "nodeSucceeded" && e.nodeId === outInstId);
6182
7850
  if (!succ2) {
6183
- appendEvent(journalPath, {
7851
+ appendControlEvent({
6184
7852
  type: "nodeFailed",
6185
7853
  nodeId: a.loopId,
6186
7854
  attemptId: `${a.loopId}/iterations/${String(a.iteration).padStart(3, "0")}`,
@@ -6189,7 +7857,7 @@ ${request.reason}
6189
7857
  });
6190
7858
  return;
6191
7859
  }
6192
- appendEvent(journalPath, {
7860
+ appendControlEvent({
6193
7861
  type: "nodeSucceeded",
6194
7862
  nodeId: a.loopId,
6195
7863
  attemptId: succ2.attemptId,
@@ -6205,11 +7873,11 @@ ${request.reason}
6205
7873
  let observed = `${loopNode.exit.when.path}=<unreadable>`;
6206
7874
  if (succ) {
6207
7875
  try {
6208
- const manifest = JSON.parse((0, import_node_fs11.readFileSync)(succ.manifestPath, "utf-8"));
7876
+ const manifest = JSON.parse((0, import_node_fs15.readFileSync)(succ.manifestPath, "utf-8"));
6209
7877
  const entry = manifest.files.find((f) => f.path === "result.json");
6210
7878
  if (entry) {
6211
7879
  const result = JSON.parse(
6212
- (0, import_node_fs11.readFileSync)((0, import_node_path9.join)((0, import_node_path9.dirname)(succ.manifestPath), "work", entry.path), "utf-8")
7880
+ (0, import_node_fs15.readFileSync)((0, import_node_path13.join)((0, import_node_path13.dirname)(succ.manifestPath), "work", entry.path), "utf-8")
6213
7881
  );
6214
7882
  observed = `${loopNode.exit.when.path}=${JSON.stringify(result[key])}`;
6215
7883
  matched = matchLoopExitWhen(loopNode.exit.when, result[key]);
@@ -6223,7 +7891,7 @@ ${request.reason}
6223
7891
  const effectiveMax = loopNode.maxIterations + granted;
6224
7892
  const anomalous = observed.endsWith("<unreadable>");
6225
7893
  const decision = matched ? "exit" : !anomalous && a.iteration < effectiveMax ? "continue" : "exhausted";
6226
- appendEvent(journalPath, {
7894
+ appendControlEvent({
6227
7895
  type: "loopIterationDecision",
6228
7896
  loopId: a.loopId,
6229
7897
  iteration: a.iteration,
@@ -6240,7 +7908,7 @@ ${request.reason}
6240
7908
  const target = nodesById.get(a.to);
6241
7909
  const dep = target?.depends.find((d) => d.from === a.from);
6242
7910
  if (!target || !dep?.when) {
6243
- appendEvent(journalPath, {
7911
+ appendControlEvent({
6244
7912
  type: "edgeResolved",
6245
7913
  from: a.from,
6246
7914
  to: a.to,
@@ -6258,11 +7926,11 @@ ${request.reason}
6258
7926
  let detail = `${dep.when.path}=<unreadable>`;
6259
7927
  if (succ) {
6260
7928
  try {
6261
- const manifest = JSON.parse((0, import_node_fs11.readFileSync)(succ.manifestPath, "utf-8"));
7929
+ const manifest = JSON.parse((0, import_node_fs15.readFileSync)(succ.manifestPath, "utf-8"));
6262
7930
  const entry = manifest.files.find((f) => f.path === "result.json");
6263
7931
  if (entry) {
6264
7932
  const result = JSON.parse(
6265
- (0, import_node_fs11.readFileSync)((0, import_node_path9.join)((0, import_node_path9.dirname)(succ.manifestPath), "work", entry.path), "utf-8")
7933
+ (0, import_node_fs15.readFileSync)((0, import_node_path13.join)((0, import_node_path13.dirname)(succ.manifestPath), "work", entry.path), "utf-8")
6266
7934
  );
6267
7935
  detail = `${dep.when.path}=${JSON.stringify(result[key])}`;
6268
7936
  active = matchLoopExitWhen(dep.when, result[key]);
@@ -6270,7 +7938,7 @@ ${request.reason}
6270
7938
  } catch {
6271
7939
  }
6272
7940
  }
6273
- appendEvent(journalPath, {
7941
+ appendControlEvent({
6274
7942
  type: "edgeResolved",
6275
7943
  from: a.from,
6276
7944
  to: a.to,
@@ -6287,7 +7955,7 @@ ${request.reason}
6287
7955
  if (status !== "pending" && status !== "gateWaiting" && status !== "running") return false;
6288
7956
  const instanceId = snap.nodes.get(a.nodeId)?.effectiveInstanceId;
6289
7957
  const attemptId = latestAttemptIdFor(events, instanceId ?? a.nodeId);
6290
- appendEvent(journalPath, {
7958
+ const appended = appendControlEvent({
6291
7959
  type: "nodeCancelled",
6292
7960
  nodeId: a.nodeId,
6293
7961
  ...instanceId ? { instanceId } : {},
@@ -6296,6 +7964,7 @@ ${request.reason}
6296
7964
  byNodeId: a.byNodeId,
6297
7965
  detail: a.detail
6298
7966
  });
7967
+ if (!appended) return true;
6299
7968
  const controller = nodeControllers.get(instanceId ?? a.nodeId);
6300
7969
  if (controller && !controller.signal.aborted) {
6301
7970
  controller.abort({ kind: "attemptDrain", drainReason: "obsoleteAttempt" });
@@ -6365,8 +8034,8 @@ ${request.reason}
6365
8034
  const key = snap.nodes.get(nodeId)?.effectiveInstanceId ?? nodeId;
6366
8035
  const succ = latestSuccess(key);
6367
8036
  if (!succ) return;
6368
- const upstreamOutputDir = (0, import_node_path9.join)((0, import_node_path9.dirname)(succ.manifestPath), "work");
6369
- const manifest = JSON.parse((0, import_node_fs11.readFileSync)(succ.manifestPath, "utf-8"));
8037
+ const upstreamOutputDir = (0, import_node_path13.join)((0, import_node_path13.dirname)(succ.manifestPath), "work");
8038
+ const manifest = JSON.parse((0, import_node_fs15.readFileSync)(succ.manifestPath, "utf-8"));
6370
8039
  for (const f of manifest.files) {
6371
8040
  if (filter && !filter(f)) continue;
6372
8041
  inputs.push({
@@ -6374,29 +8043,29 @@ ${request.reason}
6374
8043
  ...output ? { output } : {},
6375
8044
  ...succ.instanceId ? { instanceId: succ.instanceId } : {},
6376
8045
  name: f.name,
6377
- path: (0, import_node_path9.join)(upstreamOutputDir, f.path),
8046
+ path: (0, import_node_path13.join)(upstreamOutputDir, f.path),
6378
8047
  kind: f.kind,
6379
8048
  preview: f.preview
6380
8049
  });
6381
8050
  }
6382
8051
  };
6383
- const resolveRunPath = (p) => (0, import_node_path9.isAbsolute)(p) ? p : (0, import_node_path9.join)(runDir, p);
8052
+ const resolveRunPath = (p) => (0, import_node_path13.isAbsolute)(p) ? p : (0, import_node_path13.join)(runDir, p);
6384
8053
  const pushManifestByPath = (label, manifestPath, namePrefix) => {
6385
8054
  if (!manifestPath) return;
6386
8055
  const abs = resolveRunPath(manifestPath);
6387
- if (!(0, import_node_fs11.existsSync)(abs)) return;
8056
+ if (!(0, import_node_fs15.existsSync)(abs)) return;
6388
8057
  let manifest;
6389
8058
  try {
6390
- manifest = JSON.parse((0, import_node_fs11.readFileSync)(abs, "utf-8"));
8059
+ manifest = JSON.parse((0, import_node_fs15.readFileSync)(abs, "utf-8"));
6391
8060
  } catch {
6392
8061
  return;
6393
8062
  }
6394
- const outDir = (0, import_node_path9.join)((0, import_node_path9.dirname)(abs), "work");
8063
+ const outDir = (0, import_node_path13.join)((0, import_node_path13.dirname)(abs), "work");
6395
8064
  for (const f of manifest.files) {
6396
8065
  inputs.push({
6397
8066
  from: label,
6398
8067
  name: `${namePrefix}:${f.name}`,
6399
- path: (0, import_node_path9.join)(outDir, f.path),
8068
+ path: (0, import_node_path13.join)(outDir, f.path),
6400
8069
  kind: f.kind,
6401
8070
  preview: f.preview
6402
8071
  });
@@ -6466,7 +8135,7 @@ ${request.reason}
6466
8135
  }
6467
8136
 
6468
8137
  // src/workflows/v3/portable-runtime.ts
6469
- var import_node_path10 = require("node:path");
8138
+ var import_node_path14 = require("node:path");
6470
8139
  function defaultValidateProfile(profile, selector) {
6471
8140
  if (!profile.profileId || !profile.executorId || !profile.workingDirectory) {
6472
8141
  throw new Error(
@@ -6515,7 +8184,7 @@ async function runPortableWorkflow(dag, deps, options) {
6515
8184
  validateProfile(executionProfile, selector);
6516
8185
  }
6517
8186
  const frozenDefinition = assertOrCreatePortableRunSnapshot(
6518
- (0, import_node_path10.join)(options.baseDir, normalizedDag.runId),
8187
+ (0, import_node_path14.join)(options.baseDir, normalizedDag.runId),
6519
8188
  normalizedDag,
6520
8189
  profiles
6521
8190
  );
@@ -6593,6 +8262,9 @@ async function runPortableWorkflow(dag, deps, options) {
6593
8262
  ])
6594
8263
  )
6595
8264
  });
8265
+ if (outcome.reason === "awaitingExternal") {
8266
+ throw new Error("portable workflow runtime does not drive bot-executed goal nodes");
8267
+ }
6596
8268
  if (outcome.reason !== "terminal") return outcome;
6597
8269
  return {
6598
8270
  ...outcome,