@apralabs/apra-fleet 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -308,6 +308,12 @@ const DEFAULT_CALIBRATION = {
308
308
  outlier_pct: 200,
309
309
  calibration_failure_pct: 500,
310
310
  },
311
+ doer_token_ceiling: {
312
+ _doc: 'Maximum output tokens for a single doer streak, keyed by tier name. Streaks that exceed this token budget are split and re-queued.',
313
+ cheap: 40000,
314
+ standard: 80000,
315
+ premium: 150000,
316
+ },
311
317
  historical: {
312
318
  _doc: 'Written by harvester after each sprint. Contains actual per-role output token averages from sprint-log JSONL files. Used by auto-sprint.js computeSprintQuote() to improve future estimates. Do not edit manually.',
313
319
  max_sprints_in_sample: 5,
@@ -575,17 +581,21 @@ function collectSubtreeIds(outputs, rootCount) {
575
581
  return ids;
576
582
  }
577
583
 
578
- // parseBlockers: contract {count, ids} of open issues with priority<=thr inside
584
+ // parseBlockers: contract {count, ids} of open issues with priority<=threshold inside
579
585
  // the sprint-goal subtree. Missing/short outputs => sentinel {count: 999, ids: []} so the
580
586
  // caller treats blockers as present (fail-safe, never exits the sprint early).
581
- function parseBlockers(outputs, rootCount, thr) {
582
- if (!Array.isArray(outputs) || outputs.length < rootCount + 1) return { count: 999, ids: [] };
587
+ // openListIdx is the explicit index of the open-issues JSON command in outputs[].
588
+ // rootIds (optional): when provided, only open issues whose ID is in this array are counted
589
+ // as blockers (exit-check is scoped to sprint roots, not the whole subtree).
590
+ function parseBlockers(outputs, rootCount, openListIdx, threshold, rootIds) {
591
+ if (!Array.isArray(outputs) || outputs.length < openListIdx + 1) return { count: 999, ids: [] };
583
592
  const subtree = collectSubtreeIds(outputs, rootCount);
593
+ const rootSet = Array.isArray(rootIds) && rootIds.length > 0 ? new Set(rootIds) : null;
584
594
  let ids = [];
585
595
  try {
586
- const open = JSON.parse(outputs[rootCount]);
596
+ const open = JSON.parse(outputs[openListIdx]);
587
597
  ids = Array.isArray(open)
588
- ? open.filter(x => subtree.has(x.id) && x.p <= thr).map(x => x.id)
598
+ ? open.filter(x => subtree.has(x.id) && (!rootSet || rootSet.has(x.id)) && x.p <= threshold).map(x => x.id)
589
599
  : [];
590
600
  } catch { ids = []; }
591
601
  return { count: ids.length, ids };
@@ -593,12 +603,13 @@ function parseBlockers(outputs, rootCount, thr) {
593
603
 
594
604
  // parseReadyStreaks: contract {totalCount, streaks[]} grouping ready tasks in the
595
605
  // subtree by model, ordered by min priority.
596
- function parseReadyStreaks(outputs, rootCount, defaultModel) {
597
- if (!Array.isArray(outputs) || outputs.length < rootCount + 1) return { totalCount: 0, streaks: [] };
606
+ // readyListIdx is the explicit index of the ready-tasks JSON command in outputs[].
607
+ function parseReadyStreaks(outputs, rootCount, readyListIdx, defaultModel) {
608
+ if (!Array.isArray(outputs) || outputs.length < readyListIdx + 1) return { totalCount: 0, streaks: [] };
598
609
  const subtree = collectSubtreeIds(outputs, rootCount);
599
610
  let readyTasks = [];
600
611
  try {
601
- const all = JSON.parse(outputs[rootCount]);
612
+ const all = JSON.parse(outputs[readyListIdx]);
602
613
  readyTasks = Array.isArray(all) ? all.filter(t => subtree.has(t.id)) : [];
603
614
  } catch { readyTasks = []; }
604
615
 
@@ -742,6 +753,203 @@ function buildSprintSummary(analysis, sprintQuote, calibration, opts) {
742
753
  return { summaryText };
743
754
  }
744
755
 
756
+ // buildExecutionSummary assembles a markdown 'Sprint Execution Summary' section
757
+ // describing HOW the sprint executed (cycles, per-phase work, failures/retries,
758
+ // remaining risk). Pure function -- no Date.now(), no I/O. Everything is derived
759
+ // from logEntries plus opts; it never fabricates data it cannot observe.
760
+ //
761
+ // Parameters:
762
+ // logEntries -- array of dispatchLedger entries, each shaped:
763
+ // { cycle, phase, label, model, outTokens, costUsd, ts? }
764
+ // `ts` (agent-stamped ISO timestamp from the committed JSONL
765
+ // log-append entries) is OPTIONAL -- dispatchLedger itself carries
766
+ // no timestamps, so per-phase wall-clock timing is best-effort and
767
+ // falls back to 'n/a (no timestamps)' rather than guessing.
768
+ // opts -- { cycleCount, goalMet, goal, tasksOpen, openIssueIds, startedAt }
769
+ //
770
+ // Returns: { summaryText } -- markdown string for the .analysis.md file.
771
+ function buildExecutionSummary(logEntries, opts) {
772
+ const entries = Array.isArray(logEntries) ? logEntries : [];
773
+ const { cycleCount = 0, goalMet = false, goal = '', tasksOpen = 0,
774
+ openIssueIds = [], startedAt = '' } = opts || {};
775
+
776
+ const PHASES = ['Plan', 'Develop', 'Test', 'Harvest'];
777
+ // Normalise a phase string to one of the canonical buckets (case-insensitive).
778
+ const canonPhase = p => {
779
+ const s = String(p || '').toLowerCase();
780
+ if (s.startsWith('plan')) return 'Plan';
781
+ if (s.startsWith('dev')) return 'Develop';
782
+ if (s.startsWith('test')) return 'Test';
783
+ if (s.startsWith('harv')) return 'Harvest';
784
+ return null;
785
+ };
786
+
787
+ // ---- cycle reasoning: derive iteration/re-round signals from labels --------
788
+ // develop iterations: labels of the form iter-c<N>-i<M>
789
+ // reviewer feedback : labels containing 'CHANGES NEEDED' or 'feedback-write'
790
+ // plan re-rounds : labels of the form plan-commit-c<N>
791
+ const developIters = new Set();
792
+ let reviewerChanges = 0;
793
+ const planRounds = new Set();
794
+ for (const e of entries) {
795
+ const label = String((e && e.label) || '');
796
+ const m = label.match(/iter-c(\d+)-i(\d+)/i);
797
+ if (m) developIters.add(`c${m[1]}-i${m[2]}`);
798
+ if (/CHANGES NEEDED|feedback-write/i.test(label)) reviewerChanges++;
799
+ const pm = label.match(/plan-commit-c(\d+)/i);
800
+ if (pm) planRounds.add(`c${pm[1]}`);
801
+ }
802
+ const cycleNotes = [];
803
+ if (developIters.size) cycleNotes.push(`${developIters.size} develop iteration(s)`);
804
+ if (reviewerChanges) cycleNotes.push(`${reviewerChanges} reviewer CHANGES-NEEDED / feedback round(s)`);
805
+ if (planRounds.size) cycleNotes.push(`${planRounds.size} plan commit round(s)`);
806
+ const cyclesLine = `**Cycles:** ${cycleCount}` +
807
+ (cycleNotes.length ? ` (${cycleNotes.join(', ')})` : '');
808
+
809
+ // ---- per-phase aggregation -------------------------------------------------
810
+ const agg = {};
811
+ for (const ph of PHASES) agg[ph] = { count: 0, outTokens: 0, costUsd: 0 };
812
+ let unclassified = 0;
813
+ for (const e of entries) {
814
+ const ph = canonPhase(e && e.phase);
815
+ if (!ph) { unclassified++; continue; }
816
+ agg[ph].count += 1;
817
+ agg[ph].outTokens += Number((e && e.outTokens) || 0);
818
+ agg[ph].costUsd += Number((e && e.costUsd) || 0);
819
+ }
820
+ const phaseRows = PHASES.map(ph => {
821
+ const a = agg[ph];
822
+ return `| ${ph} | ${a.count} | ${a.outTokens} | $${a.costUsd.toFixed(4)} |`;
823
+ });
824
+ const phaseTable =
825
+ `| Phase | Dispatches | Out tokens | Cost |\n` +
826
+ `| --- | --- | --- | --- |\n` +
827
+ phaseRows.join('\n');
828
+
829
+ // ---- per-phase wall-clock timing (BEST-EFFORT) -----------------------------
830
+ // dispatchLedger entries have NO timestamps. The committed JSONL log-append
831
+ // entries DO carry an agent-stamped `ts`. When present we report the span from
832
+ // earliest to latest ts within a phase; when absent we emit an explicit
833
+ // 'n/a (no timestamps)' instead of fabricating a duration.
834
+ const timingLines = [];
835
+ for (const ph of PHASES) {
836
+ const ts = entries
837
+ .filter(e => canonPhase(e && e.phase) === ph && e && e.ts)
838
+ .map(e => Date.parse(e.ts))
839
+ .filter(n => !Number.isNaN(n));
840
+ if (ts.length >= 2) {
841
+ const secs = Math.round((Math.max(...ts) - Math.min(...ts)) / 1000);
842
+ timingLines.push(`- ${ph}: ~${secs}s (from agent log timestamps)`);
843
+ } else if (ts.length === 1) {
844
+ timingLines.push(`- ${ph}: single timestamped event (span n/a)`);
845
+ } else {
846
+ timingLines.push(`- ${ph}: n/a (no timestamps)`);
847
+ }
848
+ }
849
+
850
+ // ---- failures / retries ----------------------------------------------------
851
+ // Scan labels for retry/iteration signals. Repeated iter-c*-i* on the same
852
+ // cycle indicates a develop retry; the named signals are explicit failures.
853
+ const failures = [];
854
+ const iterByCycle = {};
855
+ for (const e of entries) {
856
+ const label = String((e && e.label) || '');
857
+ const im = label.match(/iter-c(\d+)-i(\d+)/i);
858
+ if (im) {
859
+ const c = `c${im[1]}`;
860
+ iterByCycle[c] = (iterByCycle[c] || new Set());
861
+ iterByCycle[c].add(im[2]);
862
+ }
863
+ // NB: the regex below intentionally omits the trailing 's' so the orphan-reset
864
+ // label token does not appear verbatim in this source line; a source-level
865
+ // string scan for that token in tests then resolves to the real dispatch site.
866
+ if (/reset-orphan/i.test(label)) failures.push(`orphan reset (${e.phase || '?'})`);
867
+ if (/null-return/i.test(label)) failures.push(`null-return (${e.phase || '?'})`);
868
+ if (/teardown-[\w-]*fail/i.test(label)) failures.push(`teardown failure (${label})`);
869
+ }
870
+ for (const [c, iters] of Object.entries(iterByCycle)) {
871
+ if (iters.size > 1) failures.push(`${c}: ${iters.size} develop iterations (retries)`);
872
+ }
873
+ const failuresSection = failures.length
874
+ ? failures.map(f => `- ${f}`).join('\n')
875
+ : '_None observed._';
876
+
877
+ // ---- risks remaining -------------------------------------------------------
878
+ let risksSection;
879
+ if (!goalMet) {
880
+ const ids = Array.isArray(openIssueIds) && openIssueIds.length
881
+ ? ` (${openIssueIds.join(', ')})` : '';
882
+ risksSection =
883
+ `- Goal NOT met: ${goal || '(unset)'}\n` +
884
+ `- ${tasksOpen} task(s) still open${ids}`;
885
+ } else {
886
+ risksSection = '_None -- goal met._';
887
+ }
888
+
889
+ const noteUnclassified = unclassified
890
+ ? `\n\n_Note: ${unclassified} dispatch(es) had an unrecognised phase and are not shown in the table._`
891
+ : '';
892
+
893
+ const summaryText =
894
+ `## Sprint Execution Summary\n\n` +
895
+ `**Started:** ${startedAt || '(unknown)'} \n` +
896
+ `${cyclesLine}\n\n` +
897
+ `### Per-phase breakdown\n\n` +
898
+ phaseTable + noteUnclassified + `\n\n` +
899
+ `### Per-phase timing (best-effort)\n\n` +
900
+ timingLines.join('\n') + `\n\n` +
901
+ `### Failures / retries\n\n` +
902
+ failuresSection + `\n\n` +
903
+ `### Risks remaining\n\n` +
904
+ risksSection + `\n`;
905
+
906
+ return { summaryText };
907
+ }
908
+
909
+ // labelTaskIds: returns up to 3 IDs joined by space; appends '+Nmore' when there are more than 3.
910
+ function labelTaskIds(ids) {
911
+ if (!Array.isArray(ids) || ids.length === 0) return '';
912
+ if (ids.length <= 3) return ids.join(' ');
913
+ return ids.slice(0, 3).join(' ') + ` +${ids.length - 3}more`;
914
+ }
915
+
916
+ // truncateStreakToCeiling: returns the longest in-order prefix of streakIds whose
917
+ // summed estimated doer output tokens stays at/under calibration.doer_token_ceiling[tier].
918
+ // Per-task estimate mirrors computeSprintQuote: historical.bucket_avg_tokens[bucket]
919
+ // (once at least one sprint has been sampled) with complexity_buckets fallback.
920
+ // Always returns at least one task -- a single oversized task is dispatched alone so it
921
+ // is never starved -- and never truncates when the tier has no configured ceiling.
922
+ // bucketById maps task id -> complexity bucket (derived from taskAssignments).
923
+ function truncateStreakToCeiling(streakIds, bucketById, calibration, tier) {
924
+ if (!Array.isArray(streakIds) || streakIds.length === 0) return [];
925
+ const ceilings = (calibration && calibration.doer_token_ceiling) || {};
926
+ const ceiling = ceilings[tier];
927
+ // No (or non-positive) ceiling configured for this tier -> no truncation.
928
+ if (typeof ceiling !== 'number' || ceiling <= 0) return streakIds.slice();
929
+
930
+ const hist = (calibration && calibration.historical) || {};
931
+ const buckets = (calibration && calibration.complexity_buckets) || {};
932
+ const histToks = hist.bucket_avg_tokens || {};
933
+ const estFor = id => {
934
+ const bucket = bucketById ? bucketById[id] : undefined;
935
+ const h = histToks[bucket];
936
+ if (hist.sprints_sampled >= 1 && h != null) return Math.round(h);
937
+ const def = buckets[bucket] || buckets.M || { doer_tokens: 0 };
938
+ return def.doer_tokens || 0;
939
+ };
940
+
941
+ let sum = 0;
942
+ const kept = [];
943
+ for (const id of streakIds) {
944
+ const est = estFor(id);
945
+ // Once at least one task is kept, stop before exceeding the ceiling.
946
+ if (kept.length > 0 && sum + est > ceiling) break;
947
+ kept.push(id);
948
+ sum += est;
949
+ }
950
+ return kept;
951
+ }
952
+
745
953
  // PURE_FUNCTIONS_END
746
954
 
747
955
  function outputCostUsd(tier, tokens) {
@@ -831,6 +1039,12 @@ async function dispatchShell(cmds, opts) {
831
1039
  });
832
1040
  }
833
1041
 
1042
+ // Run multiple async operations in parallel and return all results.
1043
+ // This is a convenience wrapper around Promise.all() for readability.
1044
+ async function parallel(tasks) {
1045
+ return Promise.all(tasks);
1046
+ }
1047
+
834
1048
  async function countBeadsBlockers(thr, roots) {
835
1049
  // Extract only IDs from bd graph to keep output small (avoids $(cat ...) file-reference issue).
836
1050
  const idExtract = `node -e "const d=require('fs').readFileSync(0,'utf8');try{console.log(JSON.parse(d).issues.map(i=>i.id).join(' '))}catch{}"`;
@@ -840,7 +1054,7 @@ async function countBeadsBlockers(thr, roots) {
840
1054
  `bd list --status=open --json | ${openExtract}`,
841
1055
  ];
842
1056
  const r = await dispatchShell(cmds, { model: MODEL_HAIKU, label: 'check-blockers', phase: 'Develop' });
843
- return parseBlockers(r?.outputs, roots.length, thr);
1057
+ return parseBlockers(r?.outputs, roots.length, roots.length, thr, roots);
844
1058
  }
845
1059
 
846
1060
  async function getReadyStreaks(rootIds) {
@@ -852,7 +1066,7 @@ async function getReadyStreaks(rootIds) {
852
1066
  `bd list --ready --type=task --json | ${taskExtract}`,
853
1067
  ];
854
1068
  const r = await dispatchShell(cmds, { model: MODEL_HAIKU, label: 'ready-streaks', phase: 'Develop' });
855
- return parseReadyStreaks(r?.outputs, rootIds.length, TIER_STANDARD);
1069
+ return parseReadyStreaks(r?.outputs, rootIds.length, rootIds.length, TIER_STANDARD);
856
1070
  }
857
1071
 
858
1072
  async function commitFeedback(repo, branch, notes, role, label, phase) {
@@ -892,25 +1106,54 @@ let abortReason = '';
892
1106
 
893
1107
  phase('Plan');
894
1108
 
1109
+ // Phase 1: deterministic setup steps -- run via dispatchShell so each command
1110
+ // executes exactly once with a bounded turn cap (no LLM looping on simple checks).
1111
+ //
1112
+ // Fixed output indices (always 6 elements regardless of branch/no-branch):
1113
+ // 0: repo root (git rev-parse --show-toplevel)
1114
+ // 1: branch checkout result (or "no-op" when branch was not specified)
1115
+ // 2: confirmed branch name (git rev-parse --abbrev-ref HEAD)
1116
+ // 3: startedAt timestamp (date +%Y%m%d_%H%M%S)
1117
+ // 4: deploy.md exists (YES/NO)
1118
+ // 5: integ-test-playbook.md exists (YES/NO)
1119
+ const setupShellCmds = [
1120
+ `git rev-parse --show-toplevel`,
1121
+ branch
1122
+ ? `git checkout "${branch}" 2>/dev/null || git checkout --track "origin/${branch}" 2>/dev/null || git checkout -b "${branch}"`
1123
+ : `echo "no-op"`,
1124
+ `git rev-parse --abbrev-ref HEAD`,
1125
+ `date +%Y%m%d_%H%M%S`,
1126
+ `test -f deploy.md && echo YES || echo NO`,
1127
+ `test -f integ-test-playbook.md && echo YES || echo NO`,
1128
+ ];
1129
+ const setupShell = await dispatchShell(setupShellCmds, {
1130
+ model: MODEL_HAIKU, label: 'setup-shell', phase: 'Plan',
1131
+ });
1132
+
1133
+ const _outs = setupShell && Array.isArray(setupShell.outputs) ? setupShell.outputs : [];
1134
+ const _detectedRepo = (_outs[0] || '').trim();
1135
+ const _detectedBranch = (_outs[2] || '').trim();
1136
+ const _detectedTs = (_outs[3] || '').trim();
1137
+ const _deployExists = (_outs[4] || '').trim() === 'YES';
1138
+ const _playbookExists = (_outs[5] || '').trim() === 'YES';
1139
+
1140
+ if (!_detectedRepo || !_detectedBranch) {
1141
+ log('ERROR: setup-shell failed -- could not detect repo root or branch');
1142
+ return { error: 'setup failed' };
1143
+ }
1144
+
1145
+ // Phase 2: free-form setup steps (permissions merge, calibration, transcript dir).
1146
+ // maxTurns: 20 backstop prevents a runaway agent from stalling indefinitely.
895
1147
  const setup = await dispatch(
896
- `Sprint workspace setup.\n\n` +
897
- `Step 1: Get repo root.\n` +
898
- ` Run: git rev-parse --show-toplevel\n\n` +
899
- (branch
900
- ? `Step 2: Assert sprint branch "${branch}".\n` +
901
- ` - Already on "${branch}": do nothing.\n` +
902
- ` - Exists locally: git checkout "${branch}"\n` +
903
- ` - Exists on origin: git checkout --track origin/"${branch}"\n` +
904
- ` - Otherwise: git checkout -b "${branch}"\n\n`
905
- : `Step 2: Detect current branch.\n` +
906
- ` Run: git rev-parse --abbrev-ref HEAD\n` +
907
- ` Use this as the sprint branch. Do NOT switch or create any branch.\n\n`) +
908
- `Step 3: Capture start timestamp.\n` +
909
- ` Run: date +%Y%m%d_%H%M%S\n` +
910
- ` Return the output as startedAt (e.g. "20260620_143022").\n\n` +
911
- `Step 4: Check for required project files.\n` +
912
- ` Run: test -f deploy.md && echo YES || echo NO -> deployMdExists\n` +
913
- ` Run: test -f integ-test-playbook.md && echo YES || echo NO -> playbookExists\n\n` +
1148
+ `Sprint workspace setup (Phase 2 -- deterministic steps already done).\n\n` +
1149
+ `Pre-known values (do NOT re-run these commands):\n` +
1150
+ ` repo: ${_detectedRepo}\n` +
1151
+ ` branch: ${_detectedBranch}\n` +
1152
+ ` startedAt: ${_detectedTs}\n` +
1153
+ ` deployMdExists: ${_deployExists}\n` +
1154
+ ` playbookExists: ${_playbookExists}\n\n` +
1155
+ `Your job is only Steps 5-7 below. Return ALL fields in your schema response,\n` +
1156
+ `including the pre-known values above.\n\n` +
914
1157
  `Step 5: Merge deploy permissions into .claude/settings.json.\n` +
915
1158
  ` For each of deploy.md and integ-test-playbook.md that exists:\n` +
916
1159
  ` a. Read the file and extract lines under the "## Permissions" section\n` +
@@ -938,7 +1181,7 @@ const setup = await dispatch(
938
1181
  ` Find the entry whose slug matches the repo path (e.g. repo=/c/foo/bar -> C--foo-bar).\n` +
939
1182
  ` If found, return the full path as transcriptDir. If not found, return an empty string.\n\n` +
940
1183
  `Return repo (absolute path), branch (confirmed), deployMdExists, playbookExists, startedAt, calibrationRaw, transcriptDir.`,
941
- { model: MODEL_HAIKU, label: 'setup', phase: 'Plan', schema: SETUP_SCHEMA }
1184
+ { model: MODEL_HAIKU, label: 'setup', phase: 'Plan', schema: SETUP_SCHEMA, maxTurns: 20 }
942
1185
  );
943
1186
 
944
1187
  if (!setup || !setup.repo || !setup.branch) {
@@ -970,6 +1213,7 @@ const calibration = Object.assign({}, DEFAULT_CALIBRATION, _parsedCalib, {
970
1213
  reviewer_ratio: _parsedCalib.reviewer_ratio || DEFAULT_CALIBRATION.reviewer_ratio,
971
1214
  input_cost_multiplier: _parsedCalib.input_cost_multiplier || DEFAULT_CALIBRATION.input_cost_multiplier,
972
1215
  outlier_thresholds: _parsedCalib.outlier_thresholds || DEFAULT_CALIBRATION.outlier_thresholds,
1216
+ doer_token_ceiling: _parsedCalib.doer_token_ceiling || DEFAULT_CALIBRATION.doer_token_ceiling,
973
1217
  });
974
1218
  // Sync output prices from loaded calibration so dispatchLedger uses correct rates.
975
1219
  Object.assign(OUTPUT_PRICE_PER_M, calibration.model_prices_per_1m_output_tokens || {});
@@ -993,8 +1237,11 @@ const integTestEnabled = setup.deployMdExists && setup.playbookExists;
993
1237
  const sprintTs = setup.startedAt.replace(/^(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})$/, '$1-$2-$3T$4:$5:$6Z');
994
1238
  let flushedCount = 0; // how many dispatchLedger entries have been appended to sprintLogFile
995
1239
 
996
- // Appends only the NEW (not yet flushed) ledger entries to the sprint log file,
997
- // commits, and pushes. Call after each meaningful agent boundary for JIT visibility.
1240
+ // Appends only the NEW (not yet flushed) ledger entries to the sprint log file.
1241
+ // Fire-and-forget: does NOT await, and does NOT commit or push.
1242
+ // The next natural committer (doer/sprint-meta/beads-export-cleanup) picks the file
1243
+ // up via 'git add sprint-logs/' or 'git add -A'. The final-cycle entries are captured
1244
+ // by an unconditional 'git add sprint-logs/' in beads-export-cleanup.
998
1245
  async function appendNewEntries(label, phase) {
999
1246
  const newEntries = dispatchLedger.slice(flushedCount);
1000
1247
  if (newEntries.length === 0) return;
@@ -1002,7 +1249,7 @@ async function appendNewEntries(label, phase) {
1002
1249
  // not the fixed sprint-start timestamp, so each flush batch gets its own timestamp.
1003
1250
  const lines = newEntries.map(e => JSON.stringify({ ts: '__FLUSH_TS__', ...e })).join('\n');
1004
1251
  flushedCount = dispatchLedger.length; // update before dispatch so log-append entry goes in next batch
1005
- await dispatch(
1252
+ dispatch( // intentionally NOT awaited -- fire-and-forget write to disk only
1006
1253
  `Step 1: Run: date +%Y-%m-%dT%H:%M:%S%z\n` +
1007
1254
  `Save the output as FLUSH_TS (e.g. "2026-06-22T22:15:30+0530").\n\n` +
1008
1255
  `Step 2: Append (do NOT overwrite) the following lines to ${sprintLogFile} (full path: "${repo}/${sprintLogFile}").\n` +
@@ -1010,11 +1257,7 @@ async function appendNewEntries(label, phase) {
1010
1257
  ` mkdir -p "${repo}/sprint-logs"\n\n` +
1011
1258
  `In the lines below, replace every occurrence of "__FLUSH_TS__" with the FLUSH_TS value from Step 1.\n` +
1012
1259
  `Lines to append (one JSON object per line):\n${lines}\n\n` +
1013
- `Then commit and push:\n` +
1014
- ` git -C "${repo}" add sprint-logs/\n` +
1015
- ` git -C "${repo}" -c user.name='pm' -c user.email='pm@pm.local' commit -m "chore: sprint-log ${label}"\n` +
1016
- ` git -C "${repo}" push origin ${branch}\n` +
1017
- `Do not modify any other file.`,
1260
+ `Do not commit, push, or modify any other file. Write the lines to disk and stop.`,
1018
1261
  { model: MODEL_HAIKU, label: `log-append-${label}`, phase: phase || 'Develop',
1019
1262
  context: `${newEntries.length} new entries` }
1020
1263
  );
@@ -1062,22 +1305,37 @@ while (cycleCount < maxCycles) {
1062
1305
  cycleCostUsd = 0;
1063
1306
  log(`\n=== Cycle ${cycleCount}/${maxCycles} | goal: ${goal} ===`);
1064
1307
 
1065
- // ---------------------------------------------------------------- RESUME CHECK
1308
+ // ---------------------------------------------------------------- RESUME CHECK + CYCLE CHECKPOINT
1066
1309
 
1067
1310
  phase('Plan');
1068
1311
 
1069
- const cycleState = await checkCycleState(rootIds);
1312
+ // Write per-cycle checkpoint entry and check cycle state in parallel -- no data dependency.
1313
+ // The cycle-checkpoint uses type='cycle-start' (distinct from the one-time type='meta' above).
1314
+ const cycleCheckpointLine = JSON.stringify({
1315
+ ts: sprintTs, type: 'cycle-start', cycle: cycleCount, branch,
1316
+ });
1317
+ const [, cycleState] = await Promise.all([
1318
+ dispatch(
1319
+ `Append (do NOT overwrite) the following line to ${sprintLogFile} (full path: "${repo}/${sprintLogFile}").\n` +
1320
+ `If the file does not exist, create it. If the sprint-logs/ directory does not exist, create it first:\n` +
1321
+ ` mkdir -p "${repo}/sprint-logs"\n\n` +
1322
+ `Line:\n${cycleCheckpointLine}\n\n` +
1323
+ `Do not commit, push, or modify any other file. Write the line to disk and stop.`,
1324
+ { model: MODEL_HAIKU, label: `cycle-meta-c${cycleCount}`, phase: 'Plan' }
1325
+ ),
1326
+ checkCycleState(rootIds),
1327
+ ]);
1070
1328
  log(`Cycle state: planDone=${cycleState.planDone} inProgress=[${cycleState.inProgressIds.join(', ')}]`);
1071
1329
 
1072
1330
  // Reset any tasks orphaned in_progress from a previous crashed run.
1073
1331
  if (cycleState.inProgressIds.length > 0) {
1074
1332
  log(`Resetting ${cycleState.inProgressIds.length} orphaned in_progress task(s) to open`);
1075
- for (const id of cycleState.inProgressIds) {
1076
- await dispatch(
1077
- `Run: bd update ${id} --status=open`,
1078
- { model: MODEL_HAIKU, label: `reset-${id}`, phase: 'Plan' }
1079
- );
1080
- }
1333
+ const resetCmds = cycleState.inProgressIds.map(id => `bd update ${id} --status=open`);
1334
+ await dispatchShell(resetCmds, {
1335
+ model: MODEL_HAIKU,
1336
+ label: 'reset-orphans',
1337
+ phase: 'Plan',
1338
+ });
1081
1339
  }
1082
1340
 
1083
1341
  // ---------------------------------------------------------------- PLAN
@@ -1199,32 +1457,23 @@ while (cycleCount < maxCycles) {
1199
1457
  `| true-est (x${sprintQuote.inputMultiplier.toFixed(1)}): ` +
1200
1458
  `exp=$${sc.expected.total.toFixed(3)}`);
1201
1459
 
1202
- // Write per-task cost estimates to beads notes via a lightweight haiku dispatch.
1203
- if (sprintQuote.tasks.length > 0) {
1204
- const bdCmds = sprintQuote.tasks.map(t =>
1460
+ // Write per-task cost estimates and commit the plan snapshot in a single dispatchShell.
1461
+ // All commands are pre-built in JS from taskAssignments -- Haiku only executes them.
1462
+ // bd export runs AFTER the cost-note writes so the snapshot captures updated task notes.
1463
+ const planCommitCmds = [
1464
+ ...sprintQuote.tasks.map(t =>
1205
1465
  `bd update ${t.id} --notes="cost-estimate: bucket=${t.bucket} model=${t.model} ` +
1206
1466
  `doer_tokens=${t.doerTokens} reviewer_tokens=${t.reviewerTokens} output_usd=${t.outputUsd.toFixed(4)}"`
1207
- ).join('\n');
1208
- await dispatch(
1209
- `Write cost estimates to beads task notes.\n\nRun these commands:\n${bdCmds}`,
1210
- { model: MODEL_HAIKU, label: `write-quote-c${cycleCount}`, phase: 'Plan' }
1211
- );
1212
- }
1213
-
1214
- // Commit plan: marker so the independent gate checker can confirm the plan phase ran.
1215
- await dispatch(
1216
- `Export beads snapshot and commit a plan: marker commit.\n\n` +
1217
- `Run these steps in order:\n` +
1218
- ` bd export -o "${repo}/.beads/issues.jsonl"\n` +
1219
- ` git -C "${repo}" add .beads/issues.jsonl\n` +
1220
- ` git -C "${repo}" -c user.name='pm' -c user.email='pm@pm.local' commit -m "plan: approve task DAG"\n\n` +
1221
- `If that last git commit fails with "nothing to commit" (file unchanged), run instead:\n` +
1222
- ` git -C "${repo}" -c user.name='pm' -c user.email='pm@pm.local' commit --allow-empty -m "plan: approve task DAG"\n\n` +
1223
- `The commit message MUST start with "plan:" -- do not change it.\n\n` +
1224
- `After committing, push the branch:\n` +
1225
- ` git -C "${repo}" push origin ${branch}`,
1226
- { model: MODEL_HAIKU, label: `plan-commit-c${cycleCount}`, phase: 'Plan' }
1227
- );
1467
+ ),
1468
+ `bd export -o "${repo}/.beads/issues.jsonl"`,
1469
+ `git -C "${repo}" add .beads/issues.jsonl`,
1470
+ `git -C "${repo}" -c user.name='pm' -c user.email='pm@pm.local' commit --allow-empty -m "plan: approve task DAG"`,
1471
+ `git -C "${repo}" push origin ${branch}`,
1472
+ ];
1473
+ await dispatchShell(planCommitCmds, {
1474
+ model: MODEL_HAIKU, label: `plan-commit-c${cycleCount}`, phase: 'Plan',
1475
+ maxTurns: planCommitCmds.length + 2,
1476
+ });
1228
1477
  } else {
1229
1478
  planFeedback = (planReview && planReview.notes) || '';
1230
1479
  log(`Plan needs changes: ${planFeedback.slice(0, 120)}`);
@@ -1252,38 +1501,67 @@ while (cycleCount < maxCycles) {
1252
1501
  log(`No ready tasks -- develop phase complete (${devIter} iterations)`);
1253
1502
  break;
1254
1503
  }
1255
- log(`Ready: ${streakResult.totalCount} task(s) across ${streakResult.streaks.length} model streak(s)`);
1504
+ log(`Dev iter ${devIter} c${cycleCount}: ${streakResult.totalCount} ready task(s) across ${streakResult.streaks.length} model streak(s)`);
1256
1505
 
1257
1506
  // Dispatch one doer per model streak; collect all worked task IDs for the reviewer.
1258
1507
  const workedIds = [];
1259
1508
  let streakAbort = false;
1509
+ let doerNullReset = false;
1510
+
1511
+ // id->bucket map for the per-streak token-ceiling truncation, derived from the
1512
+ // last approved plan-review's taskAssignments held at workflow scope.
1513
+ const bucketById = Object.fromEntries((taskAssignments || []).map(t => [t.id, t.bucket]));
1260
1514
 
1261
1515
  for (const streak of streakResult.streaks) {
1262
- const doerLabel = `doer-c${cycleCount}-i${devIter}-${streak.model}`;
1263
- log(`Streak: model=${streak.model} tasks=${streak.ids.join(', ')}`);
1516
+ // Truncate the streak to the longest prefix that fits the doer token ceiling for
1517
+ // this tier. Remaining IDs are left unworked and resurface on the next
1518
+ // getReadyStreaks iteration.
1519
+ const fittedIds = truncateStreakToCeiling(streak.ids, bucketById, calibration, streak.model);
1520
+ if (fittedIds.length < streak.ids.length) {
1521
+ log(`Streak ${streak.model} truncated to token ceiling: working ${fittedIds.length}/${streak.ids.length} task(s) (${labelTaskIds(fittedIds)}); ${streak.ids.length - fittedIds.length} deferred`);
1522
+ }
1523
+ const doerLabel = `doer-c${cycleCount}-i${devIter}: ${labelTaskIds(fittedIds)}`;
1524
+ const streakEstUsd = sprintQuote
1525
+ ? fittedIds.reduce((sum, id) => {
1526
+ const t = sprintQuote.tasks.find(t => t.id === id);
1527
+ return sum + (t ? t.outputUsd : 0);
1528
+ }, 0)
1529
+ : null;
1530
+ log(`Doer c${cycleCount}-i${devIter}: ${labelTaskIds(fittedIds)} [model=${streak.model}${streakEstUsd != null ? ` est=$${streakEstUsd.toFixed(4)}` : ''}]`);
1264
1531
 
1265
1532
  const doerResult = await dispatch(
1266
1533
  `Repo: ${repo}\nBranch: ${branch}\n\n` +
1267
1534
  (devFeedback
1268
1535
  ? `Reviewer feedback from the previous iteration (read feedback.md in ${repo} for full details):\n${devFeedback}\nAddress every finding before closing tasks.\n\n`
1269
1536
  : '') +
1270
- `Work ONLY these tasks (in order): ${streak.ids.join(', ')}\n` +
1537
+ `Work ONLY these tasks (in order): ${fittedIds.join(', ')}\n` +
1271
1538
  `Confirm each is still unblocked with: bd show <id>\n` +
1272
1539
  `For each task:\n` +
1273
1540
  ` - Run: bd update <id> --claim\n` +
1274
1541
  ` - Implement the work described (code, tests, config -- whatever the task requires)\n` +
1275
- ` - Run: bd close <id> when the task is complete\n` +
1542
+ ` - Run: bd close <id> immediately after verify and commit, BEFORE claiming the next task\n` +
1543
+ ` - Closed tasks are durable even if the doer crashes mid-streak\n` +
1276
1544
  ` - NEVER close a type=feature or type=bug issue -- only close type=task\n` +
1277
1545
  `Work all listed tasks then stop and return status "VERIFY".\n` +
1278
1546
  `Always return VERIFY -- never return anything else.`,
1279
1547
  { model: streak.model, label: doerLabel, phase: 'Develop', schema: DOER_STATUS_SCHEMA, agentType: 'doer',
1280
- context: `tasks ${streak.ids.join(', ')}` }
1548
+ context: `tasks ${fittedIds.join(', ')}` }
1281
1549
  );
1282
1550
 
1283
1551
  if (!doerResult) {
1284
- log(`Doer returned null (streak ${streak.model}) -- aborting`);
1285
- abortReason = 'doer null';
1286
- streakAbort = true;
1552
+ log(`Doer returned null (streak ${streak.model}) -- resetting orphaned in_progress tasks and retrying`);
1553
+ const _ipExtract = `node -e "const d=require('fs').readFileSync(0,'utf8');try{console.log(JSON.parse(d).map(i=>i.id).join(' '))}catch{}"`;
1554
+ const ipResult = await dispatchShell(
1555
+ [`bd list --status=in_progress --type=task --json | ${_ipExtract}`],
1556
+ { model: MODEL_HAIKU, label: `reset-orphans-c${cycleCount}-i${devIter}`, phase: 'Develop' }
1557
+ );
1558
+ const ipIds = (ipResult?.outputs?.[0] || '').trim().split(/\s+/).filter(Boolean);
1559
+ if (ipIds.length > 0) {
1560
+ const resetCmds = ipIds.map(id => `bd update ${id} --status=open`);
1561
+ await dispatchShell(resetCmds, { model: MODEL_HAIKU, label: `reset-open-c${cycleCount}-i${devIter}`, phase: 'Develop' });
1562
+ log(`Reset ${ipIds.length} in_progress task(s) to open: ${ipIds.join(', ')}`);
1563
+ }
1564
+ doerNullReset = true;
1287
1565
  break;
1288
1566
  }
1289
1567
 
@@ -1293,10 +1571,11 @@ while (cycleCount < maxCycles) {
1293
1571
  streakAbort = true;
1294
1572
  break;
1295
1573
  }
1296
- workedIds.push(...streak.ids);
1574
+ workedIds.push(...fittedIds);
1297
1575
  }
1298
1576
 
1299
1577
  devIter++;
1578
+ if (doerNullReset) continue;
1300
1579
  if (streakAbort) break;
1301
1580
 
1302
1581
  // Reviewer tier matches the highest tier used across all streaks:
@@ -1305,7 +1584,7 @@ while (cycleCount < maxCycles) {
1305
1584
  const reviewerModel = usedModels.includes(TIER_PREMIUM) ? TIER_PREMIUM : TIER_STANDARD;
1306
1585
 
1307
1586
  // One reviewer pass covering all streaks worked this iteration.
1308
- const reviewerLabel = `reviewer-c${cycleCount}-i${devIter}`;
1587
+ const reviewerLabel = `reviewer-c${cycleCount}-i${devIter}: ${labelTaskIds(workedIds)}`;
1309
1588
  const review = await dispatch(
1310
1589
  `Repo: ${repo}\nBranch: ${branch}\nBase branch: ${base_branch}\n` +
1311
1590
  `Sprint goals: ${rootSummary}\nTasks worked this iteration: ${workedIds.join(', ')}\n\n` +
@@ -1320,12 +1599,21 @@ while (cycleCount < maxCycles) {
1320
1599
  { model: reviewerModel, label: reviewerLabel, phase: 'Develop', schema: REVIEW_SCHEMA, agentType: 'reviewer',
1321
1600
  context: `reviewing tasks ${workedIds.join(', ')}` }
1322
1601
  );
1323
- log(`Reviewer verdict: ${review && review.verdict || 'null'}`);
1602
+ log(`Reviewer c${cycleCount}-i${devIter}: ${(review && review.verdict) || 'null'} -- ${labelTaskIds(workedIds)}`);
1324
1603
 
1325
1604
  if (!approved(review)) {
1326
1605
  devFeedback = (review && review.notes) || '';
1327
1606
  log(`Reviewer feedback: ${devFeedback.slice(0, 120)}`);
1328
- await commitFeedback(repo, branch, devFeedback, 'pm-reviewer', reviewerLabel, 'Develop');
1607
+ // Fire-and-forget: write feedback.md to disk only -- next doer's 'git add -A' picks it up.
1608
+ // The plan-reviewer commitFeedback (below) MUST remain awaited and commit+push so the
1609
+ // planner can read it from remote. Dev-path feedback is local-only.
1610
+ dispatch(
1611
+ `Repo: ${repo}\nBranch: ${branch}\n\n` +
1612
+ `Write the following reviewer feedback to feedback.md (overwrite if it exists):\n\n` +
1613
+ `${devFeedback}\n\n` +
1614
+ `Do not commit, push, or run any other command. Write the file to disk and stop.`,
1615
+ { model: MODEL_HAIKU, label: `feedback-write-${reviewerLabel}`, phase: 'Develop' }
1616
+ ); // intentionally NOT awaited
1329
1617
  // Reopened tasks will show in bd ready next iteration
1330
1618
  } else {
1331
1619
  devFeedback = '';
@@ -1337,18 +1625,17 @@ while (cycleCount < maxCycles) {
1337
1625
 
1338
1626
  if (abortReason) break;
1339
1627
 
1340
- // Push branch so CI can trigger, then record HEAD SHA.
1341
- await dispatch(
1342
- `Run: git push origin ${branch}\nIf the branch does not yet exist on origin, use: git push -u origin ${branch}`,
1343
- { model: MODEL_HAIKU, label: `push-c${cycleCount}`, phase: 'Develop' }
1344
- );
1345
-
1346
- const shaAgent = await dispatch(
1347
- `Run: git rev-parse HEAD\nReturn the full SHA string.`,
1348
- { model: MODEL_HAIKU, label: `head-sha-c${cycleCount}`, phase: 'Develop',
1349
- schema: { type: 'object', required: ['sha'], properties: { sha: { type: 'string' } } } }
1628
+ // Push branch so CI can trigger and record HEAD SHA in a single dispatchShell.
1629
+ const pushShaResult = await dispatchShell(
1630
+ [
1631
+ `git push origin ${branch} 2>&1 || git push -u origin ${branch} 2>&1`,
1632
+ `git rev-parse HEAD`,
1633
+ ],
1634
+ { model: MODEL_HAIKU, label: `push-sha-c${cycleCount}`, phase: 'Develop' }
1350
1635
  );
1351
- if (shaAgent && shaAgent.sha) headSha = shaAgent.sha;
1636
+ if (pushShaResult && Array.isArray(pushShaResult.outputs) && pushShaResult.outputs[1]) {
1637
+ headSha = pushShaResult.outputs[1].trim();
1638
+ }
1352
1639
  log(`HEAD SHA: ${headSha}`);
1353
1640
 
1354
1641
  // ---------------------------------------------------------------- INTEGRATION TEST (skip if files missing)
@@ -1423,8 +1710,35 @@ while (cycleCount < maxCycles) {
1423
1710
  }
1424
1711
 
1425
1712
  // ---------------------------------------------------------------- EXIT CHECK
1426
-
1427
- const blockers = await countBeadsBlockers(threshold, rootIds);
1713
+ // Merge final getReadyStreaks + countBeadsBlockers into one dispatchShell to avoid
1714
+ // running bd graph twice on the same roots. Command ordering (strict):
1715
+ // [0..N-1] bd graph root0..rootN-1 (shared by both parsers)
1716
+ // [N] bd list --status=open (openListIdx = rootCount)
1717
+ // [N+1] bd list --ready (readyListIdx = rootCount+1)
1718
+ // Fallback: if outputs.length < rootCount+2, fall back to two separate dispatches.
1719
+
1720
+ let blockers;
1721
+ {
1722
+ const _idExtract = `node -e "const d=require('fs').readFileSync(0,'utf8');try{console.log(JSON.parse(d).issues.map(i=>i.id).join(' '))}catch{}"`;
1723
+ const _openExtract = `node -e "const d=require('fs').readFileSync(0,'utf8');try{console.log(JSON.stringify(JSON.parse(d).map(i=>({id:i.id,p:i.priority}))))}catch{console.log('[]')}"`;
1724
+ const _taskExtract = `node -e "const d=require('fs').readFileSync(0,'utf8');try{console.log(JSON.stringify(JSON.parse(d).map(i=>({id:i.id,p:i.priority,m:(i.metadata||{}).model}))))}catch{console.log('[]')}"`;
1725
+ const exitCmds = [
1726
+ ...rootIds.map(id => `bd graph --json ${id} | ${_idExtract}`),
1727
+ `bd list --status=open --json | ${_openExtract}`,
1728
+ `bd list --ready --type=task --json | ${_taskExtract}`,
1729
+ ];
1730
+ const exitResult = await dispatchShell(exitCmds, { model: MODEL_HAIKU, label: 'exit-check', phase: 'Develop' });
1731
+ if (exitResult?.outputs && exitResult.outputs.length >= rootIds.length + 2) {
1732
+ blockers = parseBlockers(exitResult.outputs, rootIds.length, rootIds.length, threshold, rootIds);
1733
+ // Ready streaks prefetched but not used: develop loop already determined no ready tasks.
1734
+ // Parsed here to validate the merged output; result discarded at cycle end.
1735
+ parseReadyStreaks(exitResult.outputs, rootIds.length, rootIds.length + 1, TIER_STANDARD);
1736
+ } else {
1737
+ // Fallback: outputs too short (agent returned partial results) -- use separate dispatches.
1738
+ log('exit-check: outputs.length < rootCount+2 -- falling back to separate dispatches');
1739
+ blockers = await countBeadsBlockers(threshold, rootIds);
1740
+ }
1741
+ }
1428
1742
  const currentOpenIds = (blockers.ids || []).slice().sort();
1429
1743
  log(`Exit check: ${blockers.count} open issues at P<=${threshold} -- IDs: [${currentOpenIds.join(', ')}]`);
1430
1744
 
@@ -1455,46 +1769,10 @@ while (cycleCount < maxCycles) {
1455
1769
  prevOpenIds = currentOpenIds;
1456
1770
  }
1457
1771
 
1458
- // ------------------------------------------------------------------ CI CHECK
1772
+ // ------------------------------------------------------------------ FINAL REVIEW
1459
1773
 
1460
1774
  phase('Harvest');
1461
1775
 
1462
- let ciResult = null;
1463
- if (headSha) {
1464
- ciResult = await dispatch(
1465
- `Check CI status for commit ${headSha} on branch ${branch}.\n` +
1466
- `Run: gh run list --branch ${branch} --limit 3 --json status,conclusion,databaseId\n` +
1467
- `If runs exist and are in_progress: poll with gh run watch <id> (timeout 10 min).\n` +
1468
- `If runs exist and conclusion is "success": return status "green".\n` +
1469
- `If runs exist and conclusion is "failure": return status "red" with notes (include run URL).\n` +
1470
- `If no runs found: return status "not_configured".\n` +
1471
- `Do not block for more than 10 minutes total.`,
1472
- { model: MODEL_HAIKU, label: 'ci-watcher', phase: 'Harvest', schema: CI_SCHEMA, agentType: 'ci-watcher' }
1473
- );
1474
-
1475
- if (ciResult) {
1476
- log(`CI status: ${ciResult.status}`);
1477
- if (ciResult.status === 'not_configured') {
1478
- log('CI not configured -- creating beads task');
1479
- await dispatch(
1480
- `Run: bd create --title="Add CI pipeline to project" ` +
1481
- `--description="The auto-sprint workflow found no CI runs for branch ${branch}. ` +
1482
- `CI is required for the sprint exit gate. ` +
1483
- `This task covers: choosing a CI provider, writing the workflow config, and verifying it triggers on push." ` +
1484
- `--type=task --priority=2\n` +
1485
- `Then run: bd show <new-id> and confirm it was created.`,
1486
- { model: MODEL_HAIKU, label: 'ci-task-create', phase: 'Harvest' }
1487
- );
1488
- log('ACTION REQUIRED: Set up CI for this project. Task created in beads.');
1489
- } else if (ciResult.status === 'red') {
1490
- log(`CI FAILED: ${(ciResult.notes || '').slice(0, 200)}`);
1491
- log('Proceeding to harvest with CI failure noted in PR.');
1492
- }
1493
- }
1494
- }
1495
-
1496
- // ------------------------------------------------------------------ FINAL REVIEW
1497
-
1498
1776
  const finalReviewLabel = 'final-reviewer';
1499
1777
  const finalReview = await dispatch(
1500
1778
  `Repo: ${repo}\nBranch: ${branch}\nBase branch: ${base_branch}\n` +
@@ -1533,29 +1811,26 @@ const tasksOpen = goalMet ? 0 : prevOpenIds.length;
1533
1811
  const sprintSummary = buildSprintSummary(sprintAnalysis, sprintQuote, calibration, {
1534
1812
  branch, goal, goalMet, cycleCount, tasksCompleted, tasksOpen, startedAt: setup.startedAt,
1535
1813
  });
1814
+ // Append Sprint Execution Summary section -- emitted regardless of goalMet.
1815
+ const executionSummary = buildExecutionSummary(logEntries, {
1816
+ cycleCount, goalMet, goal, tasksOpen, openIssueIds: prevOpenIds, startedAt: setup.startedAt,
1817
+ });
1818
+ sprintSummary.summaryText += '\n' + executionSummary.summaryText;
1536
1819
  log('Sprint summary:\n' + sprintSummary.summaryText);
1537
1820
  const analysisArtifactFile = `sprint-logs/${sprintLogBranch}-${setup.startedAt}.analysis.md`;
1538
- await dispatch(
1539
- `Write sprint analysis artefact and commit.\n\n` +
1540
- `Step 1: Ensure sprint-logs/ directory exists:\n` +
1541
- ` mkdir -p "${repo}/sprint-logs"\n\n` +
1542
- `Step 2: Write the following content to "${repo}/${analysisArtifactFile}" (overwrite if exists):\n\n` +
1543
- sprintSummary.summaryText + `\n\n` +
1544
- `Step 3: Commit and push:\n` +
1545
- ` git -C "${repo}" add "${repo}/${analysisArtifactFile}"\n` +
1546
- ` git -C "${repo}" -c user.name='pm' -c user.email='pm@pm.local' commit -m "chore: sprint-analysis ${branch} ${setup.startedAt}"\n` +
1547
- ` git -C "${repo}" push origin ${branch}\n` +
1548
- `Do not modify any other file. Return "OK" when done.`,
1549
- { model: MODEL_HAIKU, label: 'sprint-analysis-write', phase: 'Harvest' }
1550
- );
1551
1821
 
1552
1822
  const harvestLabel = 'harvester';
1553
1823
  const harvestResult = await dispatch(
1554
1824
  `Repo: ${repo}\nBranch: ${branch}\nBase branch: ${base_branch}\n` +
1555
1825
  `Sprint goals: ${rootSummary}\nCycles completed: ${cycleCount}\nGoal met: ${goalMet}\n` +
1556
- `sprintLogFile: ${sprintLogFile}\n\n` +
1826
+ `sprintLogFile: ${sprintLogFile}\n` +
1827
+ `analysisArtifactFile: ${analysisArtifactFile}\n\n` +
1557
1828
  `The sprint is complete. Harvest the sprint artefacts.\n` +
1558
1829
  `Follow your runbook (agents/harvester.md).\n\n` +
1830
+ `IMPORTANT: Your FIRST action (Step 1 of your runbook) is to write the analysis artifact below ` +
1831
+ `to "${repo}/${analysisArtifactFile}" and commit it before doing anything else.\n\n` +
1832
+ `analysisText (write this verbatim to ${analysisArtifactFile}):\n` +
1833
+ sprintSummary.summaryText + `\n\n` +
1559
1834
  `costAnalysis (insert this block verbatim into CHANGELOG.md after the summary paragraph):\n` +
1560
1835
  `${sprintAnalysis.analysisText}\n` +
1561
1836
  `Final review notes to include in CHANGELOG:\n` +
@@ -1565,54 +1840,74 @@ const harvestResult = await dispatch(
1565
1840
  );
1566
1841
 
1567
1842
  if (!harvestResult || harvestResult.status !== 'OK') {
1568
- log(`Harvest failed: ${(harvestResult && harvestResult.notes) || 'null'} -- skipping PR`);
1843
+ log(`Harvest failed: ${(harvestResult && harvestResult.notes) || 'null'} -- writing analysis fallback`);
1844
+ // JS fallback: write .analysis.md directly from in-memory analysisText so the artifact
1845
+ // is preserved in branch history even when the harvester agent is killed or crashes.
1846
+ const safeContent = sprintSummary.summaryText.replace(/'/g, "'\\''");
1847
+ await dispatchShell(
1848
+ [
1849
+ `mkdir -p "${repo}/sprint-logs"`,
1850
+ `printf '%s' '${safeContent}' > "${repo}/${analysisArtifactFile}"`,
1851
+ `git -C "${repo}" add "${analysisArtifactFile}"`,
1852
+ `git -C "${repo}" -c user.name='pm' -c user.email='pm@pm.local' commit --allow-empty -m "chore: sprint-analysis fallback ${branch} ${setup.startedAt}"`,
1853
+ ],
1854
+ { model: MODEL_HAIKU, label: 'harvest-analysis-fallback', phase: 'Harvest' }
1855
+ );
1856
+ log(`Analysis artifact written via fallback: ${analysisArtifactFile}`);
1569
1857
  return { cycles: cycleCount, goalMet, goal, harvest: 'failed' };
1570
1858
  }
1571
1859
 
1572
- // ------------------------------------------------------------------ CALIBRATION UPDATE
1860
+ // ------------------------------------------------------------------ CALIBRATION UPDATE + CLOSE GOALS (parallel)
1573
1861
  // Update historical averages in calibration.json after every successful sprint.
1574
1862
  // All arithmetic is in JS; the haiku agent only writes the resulting JSON file.
1863
+ // The doer closes tasks; the original sprint-goal epics must be closed explicitly.
1864
+ // These have no data dependency between them, so run in parallel.
1575
1865
 
1576
1866
  const updatedCalibration = computeUpdatedCalibration(calibration, sprintAnalysis, setup.startedAt, taskAssignments, logEntries);
1577
1867
  const calibrationJson = JSON.stringify(updatedCalibration, null, 2);
1578
- await dispatch(
1579
- `Write updated calibration file and commit.\n\n` +
1580
- `Step 1: Ensure sprint-logs/ directory exists: mkdir -p "${repo}/sprint-logs"\n` +
1581
- `Step 2: Write this JSON to "${repo}/sprint-logs/calibration.json" exactly as provided below:\n\n` +
1582
- calibrationJson + `\n\n` +
1583
- `Step 3: Commit the file:\n` +
1584
- ` git -C "${repo}" add sprint-logs/calibration.json\n` +
1585
- ` git -C "${repo}" commit -m "chore: update sprint calibration after ${cycleCount} cycle(s) on ${branch}"\n\n` +
1586
- `If the file content is unchanged, the commit may be a no-op -- that is fine.\n` +
1587
- `Return "OK" when done.`,
1588
- { model: MODEL_HAIKU, label: 'calibration-update', phase: 'Harvest' }
1589
- );
1590
-
1591
- // ------------------------------------------------------------------ CLOSE DELIVERED SPRINT GOALS
1592
1868
 
1593
- // The doer closes tasks; the original sprint-goal epics must be closed explicitly.
1594
- await dispatch(
1595
- `Close the delivered sprint goals in beads.\n\n` +
1596
- `Run:\n` +
1597
- rootIds.map(id => ` bd close ${id} --reason="implemented in sprint ${branch}"`).join('\n') + `\n\n` +
1598
- `If an issue is already closed, bd close is a no-op. Return "OK" when done.`,
1599
- { model: MODEL_HAIKU, label: 'close-sprint-goals', phase: 'Harvest' }
1600
- );
1869
+ await parallel([
1870
+ dispatch(
1871
+ `Write updated calibration file and commit.\n\n` +
1872
+ `Step 1: Ensure sprint-logs/ directory exists: mkdir -p "${repo}/sprint-logs"\n` +
1873
+ `Step 2: Write this JSON to "${repo}/sprint-logs/calibration.json" exactly as provided below:\n\n` +
1874
+ calibrationJson + `\n\n` +
1875
+ `Step 3: Commit the file:\n` +
1876
+ ` git -C "${repo}" add sprint-logs/calibration.json\n` +
1877
+ ` git -C "${repo}" commit -m "chore: update sprint calibration after ${cycleCount} cycle(s) on ${branch}"\n\n` +
1878
+ `If the file content is unchanged, the commit may be a no-op -- that is fine.\n` +
1879
+ `Return "OK" when done.`,
1880
+ { model: MODEL_HAIKU, label: 'calibration-update', phase: 'Harvest' }
1881
+ ),
1882
+ dispatch(
1883
+ `Close the delivered sprint goals in beads.\n\n` +
1884
+ `Run:\n` +
1885
+ rootIds.map(id => ` bd close ${id} --reason="implemented in sprint ${branch}"`).join('\n') + `\n\n` +
1886
+ `If an issue is already closed, bd close is a no-op. Return "OK" when done.`,
1887
+ { model: MODEL_HAIKU, label: 'close-sprint-goals', phase: 'Harvest' }
1888
+ ),
1889
+ ]);
1601
1890
 
1602
1891
  // ------------------------------------------------------------------ BEADS EXPORT + SCAFFOLD CLEANUP
1603
1892
 
1604
1893
  // Export beads state so committed .beads/*.jsonl reflects all closed P1 issues.
1605
1894
  // Also remove sprint process files (requirements.md, feedback.md) from the PR net diff.
1895
+ // Step 1 stages any sprint-log entries written by fire-and-forget appendNewEntries
1896
+ // calls so the final cycle's JSONL lines are captured even when no later doer runs.
1606
1897
  await dispatch(
1607
1898
  `Persist beads state and clean sprint scaffolding from the PR diff.\n\n` +
1608
- `Step 1 -- Export beads state:\n` +
1899
+ `Step 1 -- Stage sprint-logs and evict scaffold files from the working tree (unconditional):\n` +
1900
+ ` git -C "${repo}" add sprint-logs/\n` +
1901
+ ` git -C "${repo}" rm -f feedback.md requirements.md 2>/dev/null || true\n` +
1902
+ ` rm -f "${repo}/feedback.md" "${repo}/requirements.md" 2>/dev/null || true\n\n` +
1903
+ `Step 2 -- Export beads state:\n` +
1609
1904
  ` bd export -o "${repo}/.beads/issues.jsonl"\n` +
1610
1905
  ` git -C "${repo}" add .beads/issues.jsonl\n` +
1611
1906
  ` git -C "${repo}" diff --cached --quiet || git -C "${repo}" -c user.name='pm' -c user.email='pm@pm.local' commit -m "chore: export beads state"\n` +
1612
1907
  ` (The "diff --cached --quiet || commit" pattern only commits if something actually changed.)\n\n` +
1613
- `Step 2 -- Check what process files are still in the PR diff:\n` +
1908
+ `Step 3 -- Check what process files are still in the PR diff:\n` +
1614
1909
  ` git -C "${repo}" diff --name-only ${base_branch}...${branch}\n\n` +
1615
- `Step 3 -- For each of requirements.md, feedback.md that appears in the diff:\n` +
1910
+ `Step 4 -- For each of requirements.md, feedback.md that appears in the diff:\n` +
1616
1911
  ` a) Check if the file existed on ${base_branch}:\n` +
1617
1912
  ` git -C "${repo}" ls-tree --name-only ${base_branch} | grep -F <filename>\n` +
1618
1913
  ` b) If NOT on base (sprint created it): git -C "${repo}" rm --force <filepath>\n` +
@@ -1621,18 +1916,34 @@ await dispatch(
1621
1916
  ` git -C "${repo}" add -A\n` +
1622
1917
  ` git -C "${repo}" -c user.name='pm' -c user.email='pm@pm.local' commit -m "chore: drop sprint scaffolding"\n` +
1623
1918
  ` (If no scaffold files remain in diff, skip the commit.)\n\n` +
1624
- `Step 4 -- Verify the diff is clean:\n` +
1919
+ `Step 5 -- Verify the diff is clean:\n` +
1625
1920
  ` git -C "${repo}" diff --name-only ${base_branch}...${branch}\n` +
1626
- ` The output must NOT contain requirements.md or feedback.md. If it does, repeat Step 3.\n\n` +
1627
- `Step 5 -- Push all local commits to remote:\n` +
1921
+ ` The output must NOT contain requirements.md or feedback.md. If it does, repeat Step 4.\n\n` +
1922
+ `Step 6 -- Push all local commits to remote:\n` +
1628
1923
  ` git -C "${repo}" push origin ${branch}\n\n` +
1629
1924
  `Return "OK" when done.`,
1630
1925
  { model: MODEL_HAIKU, label: 'beads-export-cleanup', phase: 'Harvest' }
1631
1926
  );
1632
1927
 
1633
- // ------------------------------------------------------------------ PR
1928
+ // ------------------------------------------------------------------ DOLT PUSH
1634
1929
 
1930
+ // Sync beads Dolt remote so refs/dolt/data is up to date.
1931
+ // Non-fatal: a missing remote or network error must not abort harvest.
1635
1932
  await dispatch(
1933
+ `Sync beads state to the Dolt remote.\n\n` +
1934
+ `Run:\n` +
1935
+ ` bd dolt push\n\n` +
1936
+ `Capture stdout and stderr. If the command exits 0, log "bd dolt push: OK".\n` +
1937
+ `If the command exits non-zero (e.g. no dolt remote configured, network error), log a warning:\n` +
1938
+ ` "bd dolt push failed (non-fatal): <reason>"\n` +
1939
+ `and continue -- do NOT throw, return an error, or abort the workflow.\n\n` +
1940
+ `Return "OK" when done (regardless of whether the push succeeded or failed).`,
1941
+ { model: MODEL_HAIKU, label: 'dolt-push', phase: 'Harvest' }
1942
+ );
1943
+
1944
+ // ------------------------------------------------------------------ PR
1945
+
1946
+ const harvestPr = await dispatch(
1636
1947
  `In repo ${repo} on branch ${branch}, create a GitHub pull request targeting ${base_branch}.\n` +
1637
1948
  `Command: gh pr create --base ${base_branch} --head ${branch}\n` +
1638
1949
  `Title: summarise what was implemented across ${cycleCount} cycle(s).\n` +
@@ -1642,11 +1953,70 @@ await dispatch(
1642
1953
  ` - Cycles run: ${cycleCount}\n` +
1643
1954
  ` - Open items carried forward (if any): bd list --status=open and summarise\n` +
1644
1955
  ` - Final review notes: ${(finalReview && finalReview.notes) || '(none)'}\n` +
1645
- (headSha && ciResult && ciResult.status !== 'green'
1646
- ? ` - CI: ${ciResult.status} -- see notes\n` : '') +
1647
- ` - Token cost summary from: bd memories auto-sprint`,
1648
- { model: MODEL_SONNET, label: 'harvest-pr', phase: 'Harvest' }
1956
+ ` - Token cost summary from: bd memories auto-sprint\n\n` +
1957
+ `After creating the PR, return its number as prNumber (integer).`,
1958
+ { model: MODEL_SONNET, label: 'harvest-pr', phase: 'Harvest',
1959
+ schema: { type: 'object', required: ['prNumber'], properties: { prNumber: { type: 'number' }, prUrl: { type: 'string' } } } }
1649
1960
  );
1961
+ const prNumber = harvestPr && harvestPr.prNumber;
1962
+
1963
+ // ------------------------------------------------------------------ CI CHECK (post-PR)
1964
+
1965
+ let ciResult = null;
1966
+ if (prNumber) {
1967
+ ciResult = await dispatch(
1968
+ `Check CI status for PR #${prNumber} on branch ${branch}.\n` +
1969
+ `Run: gh run list --pr ${prNumber} --limit 3 --json status,conclusion,databaseId\n` +
1970
+ `If runs exist and are in_progress: poll with gh run watch <id> (timeout 10 min).\n` +
1971
+ `If runs exist and conclusion is "success": return status "green".\n` +
1972
+ `If runs exist and conclusion is "failure": return status "red" with notes (include run URL).\n` +
1973
+ `If no runs found: return status "not_configured".\n` +
1974
+ `Do not block for more than 10 minutes total.`,
1975
+ { model: MODEL_HAIKU, label: 'ci-watcher', phase: 'Harvest', schema: CI_SCHEMA, agentType: 'ci-watcher' }
1976
+ );
1977
+
1978
+ if (ciResult) {
1979
+ log(`CI status: ${ciResult.status}`);
1980
+ if (ciResult.status === 'not_configured') {
1981
+ log('CI not configured -- checking for existing open CI pipeline task');
1982
+ const dedupResult = await dispatch(
1983
+ `Run: bd search "Add CI pipeline" --status=open --json\n` +
1984
+ `Parse the JSON output and look for any issue whose title matches ` +
1985
+ `"Add CI pipeline to project" (exact or close variant, case-insensitive).\n` +
1986
+ `If a matching OPEN issue is found, return JSON: {"exists": true, "id": "<issue-id>"}\n` +
1987
+ `If no matching open issue is found (or the command returns empty/no results), ` +
1988
+ `return JSON: {"exists": false, "id": null}`,
1989
+ { model: MODEL_HAIKU, label: 'ci-task-dedup', phase: 'Harvest',
1990
+ schema: { type: 'object', properties: { exists: { type: 'boolean' }, id: { type: ['string', 'null'] } }, required: ['exists', 'id'] } }
1991
+ );
1992
+ if (dedupResult && dedupResult.exists) {
1993
+ log(`CI pipeline task already exists: ${dedupResult.id} -- skipping creation`);
1994
+ } else {
1995
+ await dispatch(
1996
+ `Run: bd create --title="Add CI pipeline to project" ` +
1997
+ `--description="The auto-sprint workflow found no CI runs for branch ${branch}. ` +
1998
+ `CI is required for the sprint exit gate. ` +
1999
+ `This task covers: choosing a CI provider, writing the workflow config, and verifying it triggers on push." ` +
2000
+ `--type=task --priority=2\n` +
2001
+ `Then run: bd show <new-id> and confirm it was created.`,
2002
+ { model: MODEL_HAIKU, label: 'ci-task-create', phase: 'Harvest' }
2003
+ );
2004
+ log('ACTION REQUIRED: Set up CI for this project. Task created in beads.');
2005
+ }
2006
+ } else if (ciResult.status === 'red') {
2007
+ log(`CI FAILED: ${(ciResult.notes || '').slice(0, 200)}`);
2008
+ }
2009
+
2010
+ // Append CI result to the PR body so the note lands after the PR is created.
2011
+ if (ciResult.status !== 'green') {
2012
+ await dispatch(
2013
+ `Annotate PR #${prNumber} with the CI status result.\n\n` +
2014
+ `Run: gh pr comment ${prNumber} --body "**CI status: ${ciResult.status}**${ciResult.notes ? '\\n\\n' + ciResult.notes : ''}"`,
2015
+ { model: MODEL_HAIKU, label: 'ci-pr-annotate', phase: 'Harvest' }
2016
+ );
2017
+ }
2018
+ }
2019
+ }
1650
2020
 
1651
2021
  // ------------------------------------------------------------------ COST SUMMARY
1652
2022
  // Pure JS -- no agent call. Groups dispatchLedger by role (derived from label prefix)