@apralabs/apra-fleet 0.2.2 → 0.3.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.
- package/README.md +29 -8
- package/dist/agents/ci-watcher.md +46 -0
- package/dist/agents/deployer.md +75 -0
- package/dist/agents/doer.md +71 -0
- package/dist/agents/harvester.md +94 -0
- package/dist/agents/integ-test-runner.md +96 -0
- package/dist/agents/plan-reviewer.md +64 -0
- package/dist/agents/planner.md +94 -0
- package/dist/agents/reviewer.md +82 -0
- package/dist/cli/agent-transform.d.ts +11 -0
- package/dist/cli/agent-transform.d.ts.map +1 -0
- package/dist/cli/agent-transform.js +63 -0
- package/dist/cli/agent-transform.js.map +1 -0
- package/dist/cli/config.d.ts +1 -0
- package/dist/cli/config.d.ts.map +1 -1
- package/dist/cli/config.js +15 -0
- package/dist/cli/config.js.map +1 -1
- package/dist/cli/install.d.ts +5 -0
- package/dist/cli/install.d.ts.map +1 -1
- package/dist/cli/install.js +201 -23
- package/dist/cli/install.js.map +1 -1
- package/dist/cli/uninstall.js +1 -1
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +3 -0
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/opencode.d.ts +40 -0
- package/dist/providers/opencode.d.ts.map +1 -0
- package/dist/providers/opencode.js +178 -0
- package/dist/providers/opencode.js.map +1 -0
- package/dist/services/cloud/task-wrapper.js +1 -1
- package/dist/services/cloud/task-wrapper.js.map +1 -1
- package/dist/skills/pm/SKILL.md +343 -0
- package/dist/skills/pm/beads.md +137 -0
- package/dist/skills/pm/cost.md +152 -0
- package/dist/skills/pm/doer-reviewer-loop.md +297 -0
- package/dist/skills/pm/fleet-addendum.md +115 -0
- package/dist/skills/pm/simple-sprint.md +64 -0
- package/dist/skills/pm/sprint.md +311 -0
- package/dist/skills/pm/tpl-progress.json +35 -0
- package/dist/skills/pm/worktrees.md +92 -0
- package/dist/tools/cloud-control.d.ts +2 -2
- package/dist/tools/credential-store-set.d.ts +2 -2
- package/dist/tools/credential-store-update.d.ts +2 -2
- package/dist/tools/execute-prompt.d.ts +3 -0
- package/dist/tools/execute-prompt.d.ts.map +1 -1
- package/dist/tools/execute-prompt.js +17 -3
- package/dist/tools/execute-prompt.js.map +1 -1
- package/dist/tools/register-member.d.ts +29 -6
- package/dist/tools/register-member.d.ts.map +1 -1
- package/dist/tools/register-member.js +43 -3
- package/dist/tools/register-member.js.map +1 -1
- package/dist/tools/update-member.d.ts +29 -6
- package/dist/tools/update-member.d.ts.map +1 -1
- package/dist/tools/update-member.js +43 -3
- package/dist/tools/update-member.js.map +1 -1
- package/dist/types.d.ts +6 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/opencode-model-validation.d.ts +15 -0
- package/dist/utils/opencode-model-validation.d.ts.map +1 -0
- package/dist/utils/opencode-model-validation.js +40 -0
- package/dist/utils/opencode-model-validation.js.map +1 -0
- package/dist/workflows/auto-sprint.js +2046 -0
- package/package.json +2 -2
- package/version.json +1 -1
- package/skills/pm/SKILL.md +0 -110
- package/skills/pm/backlog-item.md +0 -65
- package/skills/pm/beads.md +0 -192
- package/skills/pm/cleanup.md +0 -15
- package/skills/pm/context-file.md +0 -40
- package/skills/pm/doer-reviewer.md +0 -123
- package/skills/pm/init.md +0 -25
- package/skills/pm/multi-pair-sprint.md +0 -64
- package/skills/pm/plan-prompt.md +0 -94
- package/skills/pm/simple-sprint.md +0 -42
- package/skills/pm/single-pair-sprint.md +0 -178
- package/skills/pm/tpl-deploy.md +0 -24
- package/skills/pm/tpl-design.md +0 -29
- package/skills/pm/tpl-doer.md +0 -43
- package/skills/pm/tpl-plan.md +0 -72
- package/skills/pm/tpl-pm.md +0 -2
- package/skills/pm/tpl-progress.json +0 -28
- package/skills/pm/tpl-projects.md +0 -4
- package/skills/pm/tpl-requirements.md +0 -21
- package/skills/pm/tpl-reviewer-plan.md +0 -53
- package/skills/pm/tpl-reviewer.md +0 -72
- package/skills/pm/tpl-status.md +0 -29
|
@@ -0,0 +1,2046 @@
|
|
|
1
|
+
export const meta = {
|
|
2
|
+
name: 'auto-sprint',
|
|
3
|
+
description: `Multi-cycle sprint workflow: plan -> develop -> test -> harvest.
|
|
4
|
+
|
|
5
|
+
args must be a JSON object (not a string) with these fields:
|
|
6
|
+
issues REQUIRED. Array of beads issue IDs (sprint roots), e.g. ["BD-1","BD-2"].
|
|
7
|
+
branch REQUIRED. Sprint branch name, e.g. "feat/auth". Created if it does not exist.
|
|
8
|
+
goal Optional. Exit when no open issues at or above this priority. "P1" | "P1/P2" | "P1/P2/P3". Default: "P1/P2".
|
|
9
|
+
max_cycles Optional. Hard cycle ceiling. Default: 5.
|
|
10
|
+
base_branch Optional. PR target branch. Default: "main".
|
|
11
|
+
requirementsFile Optional. Path to an additional context file for the planner.
|
|
12
|
+
|
|
13
|
+
Minimal invocation example: { "issues": ["BD-7"], "branch": "feat/my-feature" }`,
|
|
14
|
+
phases: [
|
|
15
|
+
{ title: 'Plan' },
|
|
16
|
+
{ title: 'Develop' },
|
|
17
|
+
{ title: 'Test' },
|
|
18
|
+
{ title: 'Harvest' },
|
|
19
|
+
],
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// Multi-cycle sprint workflow. Drives 8 agents against a beads backlog until a
|
|
23
|
+
// priority-based quality goal is met or the cycle ceiling is reached.
|
|
24
|
+
//
|
|
25
|
+
// Agent roster:
|
|
26
|
+
// planner -- reads open beads sprint goals/features/bugs, creates feature+task DAG
|
|
27
|
+
// plan-reviewer -- validates DAG: coverage, task size, acceptance criteria
|
|
28
|
+
// doer -- works bd-ready tasks (impl and test-dev), VERIFY checkpoint
|
|
29
|
+
// reviewer -- reviews doer output, can reopen tasks
|
|
30
|
+
// deployer -- follows deploy.md + integ-test-playbook.md (setup/reset/teardown)
|
|
31
|
+
// integ-test-runner -- executes tests, closes features, files bugs/enhancements
|
|
32
|
+
// ci-watcher -- polls CI; creates beads task if not configured
|
|
33
|
+
// harvester -- docs, CHANGELOG, token summary, PR
|
|
34
|
+
//
|
|
35
|
+
// Beads owns all work items and is the exit signal.
|
|
36
|
+
// JS workflow owns all routing. No LLM ever decides whether to continue.
|
|
37
|
+
//
|
|
38
|
+
// args (JSON object serialised to string by the Workflow runtime):
|
|
39
|
+
// branch -- sprint branch (required); asserted/created at startup
|
|
40
|
+
// issues -- beads issue IDs to implement (sprint roots), e.g. ["BD-1","BD-2"] (required)
|
|
41
|
+
// goal -- exit criterion: "P1" | "P1/P2" | "P1/P2/P3" (default: "P1/P2")
|
|
42
|
+
// max_cycles -- hard ceiling on sprint cycles (default: 5)
|
|
43
|
+
// requirementsFile -- optional context file for the planner (default: none)
|
|
44
|
+
// base_branch -- PR target (default: "main")
|
|
45
|
+
|
|
46
|
+
// NOTE: the arg-parsing block below is intentionally duplicated from lib/parse-sprint-args.mjs,
|
|
47
|
+
// which exists only for unit testing (workflow scripts cannot import arbitrary files).
|
|
48
|
+
// Keep both in sync when modifying parsing logic.
|
|
49
|
+
//
|
|
50
|
+
// Accepted forms:
|
|
51
|
+
// "BD-1" bare issue ID
|
|
52
|
+
// "BD-1 BD-2" space/comma-separated issue IDs
|
|
53
|
+
// ["BD-1","BD-2"] JSON array of issue IDs
|
|
54
|
+
// {"issues":["BD-1"],"goal":"P1"} JSON object (full control)
|
|
55
|
+
//
|
|
56
|
+
// branch always defaults to the current git branch when omitted.
|
|
57
|
+
let opts = {};
|
|
58
|
+
if (args) {
|
|
59
|
+
let parsed = null;
|
|
60
|
+
try { parsed = JSON.parse(args); } catch {}
|
|
61
|
+
|
|
62
|
+
if (Array.isArray(parsed)) {
|
|
63
|
+
opts = { issues: parsed };
|
|
64
|
+
} else if (parsed && typeof parsed === 'object') {
|
|
65
|
+
opts = parsed;
|
|
66
|
+
} else {
|
|
67
|
+
// bare string: treat as space/comma-separated issue IDs
|
|
68
|
+
const ids = String(args).split(/[\s,]+/).map(s => s.trim()).filter(Boolean);
|
|
69
|
+
opts = { issues: ids };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let branch = opts.branch || ''; // empty = auto-detect in setup; reassigned after setup resolves
|
|
74
|
+
const rawIssues = opts.issues || [];
|
|
75
|
+
const rootIds = Array.isArray(rawIssues) ? rawIssues : [rawIssues];
|
|
76
|
+
const goal = opts.goal || 'P1/P2';
|
|
77
|
+
const maxCycles = Number(opts.max_cycles) || 5;
|
|
78
|
+
const requirementsFile = opts.requirementsFile || '';
|
|
79
|
+
const base_branch = opts.base_branch || 'main';
|
|
80
|
+
|
|
81
|
+
if (rootIds.length === 0) {
|
|
82
|
+
log('ERROR: at least one beads issue ID is required (pass as arg: /auto-sprint BD-1)');
|
|
83
|
+
return { error: 'missing issues' };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Goal -> numeric priority threshold.
|
|
87
|
+
// Exit when open issues in sprint-goal subtree at priority <= threshold reaches zero.
|
|
88
|
+
const GOAL_THRESHOLD = { 'P1': 1, 'P1/P2': 2, 'P1/P2/P3': 3 };
|
|
89
|
+
const threshold = GOAL_THRESHOLD[goal] || 2;
|
|
90
|
+
|
|
91
|
+
// PURE_FUNCTIONS_BEGIN -- extracted by test/sprint-cost.test.mjs via vm; keep this block self-contained
|
|
92
|
+
|
|
93
|
+
// Provider-agnostic tier names. These are the only strings that appear in
|
|
94
|
+
// calibration.json and in dispatch calls. Provider-specific model IDs live
|
|
95
|
+
// exclusively in TIER_TO_MODEL below -- the single place to update when models change.
|
|
96
|
+
const TIER_CHEAP = 'cheap';
|
|
97
|
+
const TIER_STANDARD = 'standard';
|
|
98
|
+
const TIER_PREMIUM = 'premium';
|
|
99
|
+
|
|
100
|
+
// Claude model IDs -- change here only, nowhere else in this file.
|
|
101
|
+
const TIER_TO_MODEL = {
|
|
102
|
+
[TIER_CHEAP]: 'claude-haiku-4-5',
|
|
103
|
+
[TIER_STANDARD]: 'claude-sonnet-4-6',
|
|
104
|
+
[TIER_PREMIUM]: 'claude-opus-4-8',
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// Legacy aliases kept so existing dispatch call-sites (model: MODEL_OPUS etc.) are unchanged.
|
|
108
|
+
const MODEL_OPUS = TIER_PREMIUM;
|
|
109
|
+
const MODEL_SONNET = TIER_STANDARD;
|
|
110
|
+
const MODEL_HAIKU = TIER_CHEAP;
|
|
111
|
+
|
|
112
|
+
// ------------------------------------------------------------------ schemas
|
|
113
|
+
|
|
114
|
+
const REVIEW_SCHEMA = {
|
|
115
|
+
type: 'object', required: ['verdict', 'notes'],
|
|
116
|
+
properties: {
|
|
117
|
+
verdict: { type: 'string', enum: ['APPROVED', 'CHANGES NEEDED'] },
|
|
118
|
+
notes: { type: 'string' },
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const PLAN_REVIEW_SCHEMA = {
|
|
123
|
+
type: 'object', required: ['verdict', 'notes', 'taskAssignments'],
|
|
124
|
+
properties: {
|
|
125
|
+
verdict: { type: 'string', enum: ['APPROVED', 'CHANGES NEEDED'] },
|
|
126
|
+
notes: { type: 'string' },
|
|
127
|
+
taskAssignments: {
|
|
128
|
+
type: 'array',
|
|
129
|
+
items: {
|
|
130
|
+
type: 'object', required: ['id', 'bucket', 'model'],
|
|
131
|
+
properties: {
|
|
132
|
+
id: { type: 'string' },
|
|
133
|
+
bucket: { type: 'string', enum: ['S', 'M', 'L'] },
|
|
134
|
+
model: { type: 'string' },
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const SHELL_OUTPUTS_SCHEMA = {
|
|
142
|
+
type: 'object', required: ['outputs'],
|
|
143
|
+
properties: {
|
|
144
|
+
outputs: { type: 'array', items: { type: 'string' } },
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const DOER_STATUS_SCHEMA = {
|
|
149
|
+
type: 'object', required: ['status'],
|
|
150
|
+
properties: {
|
|
151
|
+
status: { type: 'string', enum: ['VERIFY'] },
|
|
152
|
+
notes: { type: 'string' },
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const HARVEST_SCHEMA = {
|
|
157
|
+
type: 'object', required: ['status'],
|
|
158
|
+
properties: {
|
|
159
|
+
status: { type: 'string', enum: ['OK', 'FAILED'] },
|
|
160
|
+
notes: { type: 'string' },
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const SETUP_SCHEMA = {
|
|
165
|
+
type: 'object', required: ['repo', 'branch', 'deployMdExists', 'playbookExists', 'startedAt'],
|
|
166
|
+
properties: {
|
|
167
|
+
repo: { type: 'string' },
|
|
168
|
+
branch: { type: 'string' },
|
|
169
|
+
deployMdExists: { type: 'boolean' },
|
|
170
|
+
playbookExists: { type: 'boolean' },
|
|
171
|
+
startedAt: { type: 'string', description: 'yyyymmdd_hhmmss from date +%Y%m%d_%H%M%S' },
|
|
172
|
+
calibrationRaw: { type: 'string' }, // verbatim stdout of cat calibration.json; JS parses it
|
|
173
|
+
transcriptDir: { type: 'string', description: 'Directory where subagent JSONL conversation logs are written (best-effort; may be empty string if not resolvable)' },
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const BEADS_BLOCKERS_SCHEMA = {
|
|
178
|
+
type: 'object', required: ['count', 'ids'],
|
|
179
|
+
properties: {
|
|
180
|
+
count: { type: 'number' },
|
|
181
|
+
ids: { type: 'array', items: { type: 'string' } },
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// One entry per model streak: tasks sharing the same model that can be worked
|
|
186
|
+
// in a single doer dispatch. Ordered by priority (P0 first).
|
|
187
|
+
const READY_STREAKS_SCHEMA = {
|
|
188
|
+
type: 'object', required: ['streaks', 'totalCount'],
|
|
189
|
+
properties: {
|
|
190
|
+
totalCount: { type: 'number' },
|
|
191
|
+
streaks: {
|
|
192
|
+
type: 'array',
|
|
193
|
+
items: {
|
|
194
|
+
type: 'object', required: ['model', 'ids'],
|
|
195
|
+
properties: {
|
|
196
|
+
model: { type: 'string' },
|
|
197
|
+
ids: { type: 'array', items: { type: 'string' } },
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const CI_SCHEMA = {
|
|
205
|
+
type: 'object', required: ['status'],
|
|
206
|
+
properties: {
|
|
207
|
+
status: { type: 'string', enum: ['green', 'red', 'not_configured', 'pending'] },
|
|
208
|
+
notes: { type: 'string' },
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const INTEG_RUN_SCHEMA = {
|
|
213
|
+
type: 'object', required: ['featuresClosed', 'issuesCreated', 'summary'],
|
|
214
|
+
properties: {
|
|
215
|
+
featuresClosed: { type: 'number' },
|
|
216
|
+
issuesCreated: { type: 'number' },
|
|
217
|
+
summary: { type: 'string' },
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// Returned by the resume-check agent at the top of every cycle.
|
|
222
|
+
// planDone -- true if the sprint goal already has children AND every feature has at
|
|
223
|
+
// least one task with non-empty acceptance criteria; skip plan loop.
|
|
224
|
+
// inProgressIds -- tasks currently in_progress; reset to open before the develop
|
|
225
|
+
// loop so a crashed doer never orphans work forever.
|
|
226
|
+
|
|
227
|
+
// ------------------------------------------------------------------ helpers
|
|
228
|
+
|
|
229
|
+
function approved(review) {
|
|
230
|
+
return review && typeof review.verdict === 'string' && review.verdict.trim() === 'APPROVED';
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Live pricing for dispatch cost tracking. Initialized from DEFAULT_CALIBRATION;
|
|
234
|
+
// synced to calibration.json values after setup() returns.
|
|
235
|
+
// Prices are output tokens only in USD per 1M. Source: Anthropic pricing 2026-06-04.
|
|
236
|
+
// Keys are tier names (cheap/standard/premium), not model IDs.
|
|
237
|
+
// See also: sprint-logs/calibration.json model_prices_per_1m_output_tokens.
|
|
238
|
+
let OUTPUT_PRICE_PER_M = {
|
|
239
|
+
[TIER_CHEAP]: 5.00,
|
|
240
|
+
[TIER_STANDARD]: 15.00,
|
|
241
|
+
[TIER_PREMIUM]: 25.00,
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
// ------------------------------------------------------------------ CALIBRATION DEFAULTS
|
|
245
|
+
// Single source of truth for all estimation constants. On first sprint run the setup
|
|
246
|
+
// agent writes this to sprint-logs/calibration.json; subsequent runs read that file.
|
|
247
|
+
// To change prices or buckets: update this object -- the file is regenerated next run.
|
|
248
|
+
const DEFAULT_CALIBRATION = {
|
|
249
|
+
_doc: 'Sprint cost calibration. All estimation constants live here -- nothing is hardcoded in agents. Fields named _doc are documentation strings; skip them when reading values. The historical section is written automatically by the harvester after each sprint; do not edit it manually.',
|
|
250
|
+
schema_version: 1,
|
|
251
|
+
model_prices_per_1m_output_tokens: {
|
|
252
|
+
_doc: 'USD per 1M output tokens, keyed by tier name (cheap/standard/premium). Source: Anthropic published pricing 2026-06-04. Update when pricing changes. Provider-specific model IDs are resolved from tier names in auto-sprint.js TIER_TO_MODEL -- they never appear in this file.',
|
|
253
|
+
[TIER_CHEAP]: 5.00,
|
|
254
|
+
[TIER_STANDARD]: 15.00,
|
|
255
|
+
[TIER_PREMIUM]: 25.00,
|
|
256
|
+
},
|
|
257
|
+
role_models: {
|
|
258
|
+
_doc: "Tier name per workflow role. 'doer' and 'reviewer' are NOT here -- the planner sets tier per task in beads metadata; reviewer escalates to max(task_tier, standard). Change an entry here to reroute a role to a different tier.",
|
|
259
|
+
'setup': TIER_CHEAP,
|
|
260
|
+
'planner': TIER_PREMIUM,
|
|
261
|
+
'plan-reviewer': TIER_STANDARD,
|
|
262
|
+
'deployer': TIER_STANDARD,
|
|
263
|
+
'integ-test-runner': TIER_STANDARD,
|
|
264
|
+
'ci-watcher': TIER_CHEAP,
|
|
265
|
+
'harvester': TIER_STANDARD,
|
|
266
|
+
'log-flush': TIER_CHEAP,
|
|
267
|
+
'check-blockers': TIER_CHEAP,
|
|
268
|
+
'ready-streaks': TIER_CHEAP,
|
|
269
|
+
},
|
|
270
|
+
doer_model_fallback: {
|
|
271
|
+
_doc: 'Tier assumed for doer cost estimation when a task has no tier metadata in beads. In practice the planner always sets tier metadata -- this is a safety net only.',
|
|
272
|
+
model: TIER_STANDARD,
|
|
273
|
+
},
|
|
274
|
+
reviewer_model_rule: {
|
|
275
|
+
_doc: 'Reviewer tier is max(doer_task_tier, minimum). If the doer used premium, reviewer uses premium; otherwise reviewer uses standard. This mirrors the reviewerModel selection in auto-sprint.js.',
|
|
276
|
+
minimum: TIER_STANDARD,
|
|
277
|
+
},
|
|
278
|
+
complexity_buckets: {
|
|
279
|
+
_doc: 'Estimated doer output tokens per task by complexity bucket S/M/L. Plan-reviewer assigns a bucket to each task by reading its description. historical.bucket_avg_tokens overrides these defaults once enough sprint data has been collected.',
|
|
280
|
+
S: { _doc: 'Small: 1 file, narrow scoped change -- rename, config key, simple wiring, pure boilerplate', doer_tokens: 600 },
|
|
281
|
+
M: { _doc: 'Medium: 2-3 files, moderate logic -- new API endpoint, test suite, small focused refactor', doer_tokens: 1400 },
|
|
282
|
+
L: { _doc: 'Large: 3+ files or non-trivial design -- new auth flow, data migration, cross-cutting refactor', doer_tokens: 2800 },
|
|
283
|
+
},
|
|
284
|
+
reviewer_ratio: {
|
|
285
|
+
_doc: 'Reviewer estimated output tokens as a fraction of doer tokens for the same task. 0.4 means reviewer uses ~40% as many output tokens as the doer. Overridden by historical.reviewer_ratio_avg when available.',
|
|
286
|
+
value: 0.4,
|
|
287
|
+
},
|
|
288
|
+
cycle_assumptions: {
|
|
289
|
+
_doc: "Expected number of dev/review cycles. Per-task cost is multiplied by these to produce sprint-level cost scenarios. Update 'expected' if your team consistently lands at a different cycle count.",
|
|
290
|
+
optimistic: 1.0, expected: 1.5, pessimistic: 2.5,
|
|
291
|
+
},
|
|
292
|
+
fixed_overhead_tokens: {
|
|
293
|
+
_doc: "Estimated output tokens for agents that run once per sprint regardless of task count. 'log_flush_per_cycle' is multiplied by the expected cycle count. Keys use underscores; the role lookup strips underscores to match role_models keys.",
|
|
294
|
+
setup: 200,
|
|
295
|
+
planner: 2000,
|
|
296
|
+
plan_reviewer: 1500,
|
|
297
|
+
harvester: 3000,
|
|
298
|
+
ci_watcher: 300,
|
|
299
|
+
log_flush_per_cycle: 100,
|
|
300
|
+
},
|
|
301
|
+
input_cost_multiplier: {
|
|
302
|
+
_doc: 'Multiply output-only USD estimate by this to approximate true cost (input + output). 4.0 is conservative for long-context agents. Replace with an empirical ratio once the workflow harness exposes input token counts.',
|
|
303
|
+
value: 4.0,
|
|
304
|
+
},
|
|
305
|
+
outlier_thresholds: {
|
|
306
|
+
_doc: 'Percentage deviation bands used to classify estimation accuracy in the sprint analysis report.',
|
|
307
|
+
notable_pct: 50,
|
|
308
|
+
outlier_pct: 200,
|
|
309
|
+
calibration_failure_pct: 500,
|
|
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
|
+
},
|
|
317
|
+
historical: {
|
|
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.',
|
|
319
|
+
max_sprints_in_sample: 5,
|
|
320
|
+
sprints_sampled: 0,
|
|
321
|
+
last_updated: null,
|
|
322
|
+
cycle_avg: null,
|
|
323
|
+
reviewer_ratio_avg: null,
|
|
324
|
+
bucket_avg_tokens: {},
|
|
325
|
+
roles: {},
|
|
326
|
+
},
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
// ------------------------------------------------------------------ COST ARITHMETIC
|
|
330
|
+
// All sprint cost computations are pure JavaScript -- no agent touches a number.
|
|
331
|
+
|
|
332
|
+
// Reviewer tier mirrors auto-sprint dispatch logic: max(taskTier, standard).
|
|
333
|
+
function reviewerModelFor(taskModel) {
|
|
334
|
+
return taskModel === TIER_PREMIUM ? TIER_PREMIUM : TIER_STANDARD;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function computeSprintQuote(taskAssignments, calibration) {
|
|
338
|
+
const prices = calibration.model_prices_per_1m_output_tokens;
|
|
339
|
+
const hist = calibration.historical || {};
|
|
340
|
+
const buckets = calibration.complexity_buckets;
|
|
341
|
+
const revRatio = (hist.sprints_sampled >= 1 && hist.reviewer_ratio_avg != null)
|
|
342
|
+
? hist.reviewer_ratio_avg : calibration.reviewer_ratio.value;
|
|
343
|
+
const cycles = calibration.cycle_assumptions;
|
|
344
|
+
const overhead = calibration.fixed_overhead_tokens;
|
|
345
|
+
const inputMult = calibration.input_cost_multiplier.value;
|
|
346
|
+
const fallback = (calibration.doer_model_fallback || {}).model || MODEL_SONNET;
|
|
347
|
+
const calibSrc = hist.sprints_sampled >= 1
|
|
348
|
+
? `historical (${hist.sprints_sampled} sprint${hist.sprints_sampled !== 1 ? 's' : ''})`
|
|
349
|
+
: 'defaults';
|
|
350
|
+
|
|
351
|
+
const tasks = (taskAssignments || []).map(t => {
|
|
352
|
+
const model = t.model || fallback;
|
|
353
|
+
const histToks = (hist.bucket_avg_tokens || {})[t.bucket];
|
|
354
|
+
const doerTokens = (hist.sprints_sampled >= 1 && histToks != null)
|
|
355
|
+
? Math.round(histToks) : (buckets[t.bucket] || buckets.M).doer_tokens;
|
|
356
|
+
const reviewerTokens = Math.round(doerTokens * revRatio);
|
|
357
|
+
const doerPrice = prices[model] || prices[MODEL_SONNET];
|
|
358
|
+
const revPrice = prices[reviewerModelFor(model)] || prices[MODEL_SONNET];
|
|
359
|
+
const outputUsd = (doerTokens * doerPrice + reviewerTokens * revPrice) / 1_000_000;
|
|
360
|
+
return { id: t.id, bucket: t.bucket, model, doerTokens, reviewerTokens, outputUsd };
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
const perTaskSubtotal = tasks.reduce((s, t) => s + t.outputUsd, 0);
|
|
364
|
+
|
|
365
|
+
const rm = calibration.role_models || {};
|
|
366
|
+
let overheadUsd = 0;
|
|
367
|
+
for (const [key, tokens] of Object.entries(overhead)) {
|
|
368
|
+
if (typeof tokens !== 'number') continue;
|
|
369
|
+
if (key === 'log_flush_per_cycle') continue;
|
|
370
|
+
const role = key.replace(/_/g, '-');
|
|
371
|
+
const model = rm[role] || MODEL_SONNET;
|
|
372
|
+
overheadUsd += tokens * (prices[model] || prices[MODEL_SONNET]) / 1_000_000;
|
|
373
|
+
}
|
|
374
|
+
const logFlushUsd = (overhead.log_flush_per_cycle || 0)
|
|
375
|
+
* (prices[rm['log-flush'] || MODEL_HAIKU] || prices[MODEL_HAIKU]) / 1_000_000;
|
|
376
|
+
|
|
377
|
+
const scenario = mult => {
|
|
378
|
+
const out = perTaskSubtotal * mult + overheadUsd + logFlushUsd * mult;
|
|
379
|
+
return { outputOnly: out, total: out * inputMult };
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
return {
|
|
383
|
+
tasks,
|
|
384
|
+
calibrationSource: calibSrc,
|
|
385
|
+
inputMultiplier: inputMult,
|
|
386
|
+
scenarios: {
|
|
387
|
+
optimistic: scenario(cycles.optimistic),
|
|
388
|
+
expected: scenario(cycles.expected),
|
|
389
|
+
pessimistic: scenario(cycles.pessimistic),
|
|
390
|
+
},
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function computeSprintAnalysis(quote, logEntries, calibration, actualCycles) {
|
|
395
|
+
const prices = calibration.model_prices_per_1m_output_tokens;
|
|
396
|
+
const inputMult = calibration.input_cost_multiplier.value;
|
|
397
|
+
const thr = calibration.outlier_thresholds;
|
|
398
|
+
const cycles = calibration.cycle_assumptions;
|
|
399
|
+
const rm = calibration.role_models || {};
|
|
400
|
+
const overhead = calibration.fixed_overhead_tokens;
|
|
401
|
+
|
|
402
|
+
const roleOf = label => label.replace(/-c\d.*$/, '');
|
|
403
|
+
|
|
404
|
+
const byRole = {};
|
|
405
|
+
for (const e of (logEntries || [])) {
|
|
406
|
+
const role = roleOf(e.label || '');
|
|
407
|
+
if (!role) continue;
|
|
408
|
+
if (!byRole[role]) byRole[role] = { tokens: 0, costUsd: 0, dispatches: 0 };
|
|
409
|
+
byRole[role].tokens += e.outTokens || 0;
|
|
410
|
+
byRole[role].costUsd += e.costUsd || 0;
|
|
411
|
+
byRole[role].dispatches++;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const estCycles = cycles.expected;
|
|
415
|
+
const tasks = quote ? quote.tasks || [] : [];
|
|
416
|
+
|
|
417
|
+
const estDoerTokens = tasks.reduce((s, t) => s + t.doerTokens, 0) * estCycles;
|
|
418
|
+
const estReviewerTokens = tasks.reduce((s, t) => s + t.reviewerTokens, 0) * estCycles;
|
|
419
|
+
const estDoerUsd = tasks.reduce((s, t) =>
|
|
420
|
+
s + t.doerTokens * (prices[t.model] || prices[MODEL_SONNET]) / 1_000_000, 0) * estCycles;
|
|
421
|
+
const estReviewerUsd = tasks.reduce((s, t) =>
|
|
422
|
+
s + t.reviewerTokens * (prices[reviewerModelFor(t.model)] || prices[MODEL_SONNET]) / 1_000_000, 0) * estCycles;
|
|
423
|
+
|
|
424
|
+
let estOverheadTokens = 0, estOverheadUsd = 0;
|
|
425
|
+
for (const [key, tokens] of Object.entries(overhead)) {
|
|
426
|
+
if (typeof tokens !== 'number') continue;
|
|
427
|
+
const mult = key === 'log_flush_per_cycle' ? estCycles : 1;
|
|
428
|
+
const role = key.replace(/_/g, '-');
|
|
429
|
+
const model = key === 'log_flush_per_cycle' ? (rm['log-flush'] || MODEL_HAIKU) : (rm[role] || MODEL_SONNET);
|
|
430
|
+
estOverheadTokens += tokens * mult;
|
|
431
|
+
estOverheadUsd += tokens * mult * (prices[model] || prices[MODEL_SONNET]) / 1_000_000;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const actDoerTokens = byRole['doer']?.tokens || 0;
|
|
435
|
+
const actDoerUsd = byRole['doer']?.costUsd || 0;
|
|
436
|
+
const actReviewerTokens = byRole['reviewer']?.tokens || 0;
|
|
437
|
+
const actReviewerUsd = byRole['reviewer']?.costUsd || 0;
|
|
438
|
+
let actOverheadTokens = 0, actOverheadUsd = 0;
|
|
439
|
+
for (const [role, data] of Object.entries(byRole)) {
|
|
440
|
+
if (role !== 'doer' && role !== 'reviewer') {
|
|
441
|
+
actOverheadTokens += data.tokens;
|
|
442
|
+
actOverheadUsd += data.costUsd;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const totEstTokens = Math.round(estDoerTokens + estReviewerTokens + estOverheadTokens);
|
|
447
|
+
const totActTokens = actDoerTokens + actReviewerTokens + actOverheadTokens;
|
|
448
|
+
const totEstUsd = estDoerUsd + estReviewerUsd + estOverheadUsd;
|
|
449
|
+
const totActUsd = actDoerUsd + actReviewerUsd + actOverheadUsd;
|
|
450
|
+
|
|
451
|
+
const pct = (est, act) => est > 0 ? `${act > est ? '+' : ''}${Math.round((act - est) / est * 100)}%` : 'n/a';
|
|
452
|
+
const fmtN = n => Math.round(n).toLocaleString('en-US');
|
|
453
|
+
const fmtU = n => `$${n.toFixed(3)}`;
|
|
454
|
+
|
|
455
|
+
const rows = [
|
|
456
|
+
['doer', Math.round(estDoerTokens), actDoerTokens, estDoerUsd, actDoerUsd ],
|
|
457
|
+
['reviewer', Math.round(estReviewerTokens), actReviewerTokens, estReviewerUsd, actReviewerUsd ],
|
|
458
|
+
['overhead', Math.round(estOverheadTokens), actOverheadTokens, estOverheadUsd, actOverheadUsd ],
|
|
459
|
+
['TOTAL', totEstTokens, totActTokens, totEstUsd, totActUsd ],
|
|
460
|
+
];
|
|
461
|
+
|
|
462
|
+
const header =
|
|
463
|
+
`| Role | Est tokens | Act tokens | D% | Est USD | Act USD |\n` +
|
|
464
|
+
`|------------|------------|------------|-------|----------|----------|\n`;
|
|
465
|
+
const body = rows.map(([role, et, at, eu, au]) =>
|
|
466
|
+
`| ${role.padEnd(10)} | ${fmtN(et).padStart(10)} | ${fmtN(at).padStart(10)} | ${pct(et, at).padStart(5)} | ${fmtU(eu).padStart(8)} | ${fmtU(au).padStart(8)} |`
|
|
467
|
+
).join('\n');
|
|
468
|
+
|
|
469
|
+
const outliers = rows.slice(0, 3).filter(([, et, at]) => et > 0 && Math.abs((at - et) / et * 100) > thr.outlier_pct).map(r => r[0]);
|
|
470
|
+
const failures = rows.slice(0, 3).filter(([, et, at]) => et > 0 && Math.abs((at - et) / et * 100) > thr.calibration_failure_pct).map(r => r[0]);
|
|
471
|
+
|
|
472
|
+
const src = quote ? quote.calibrationSource : 'none';
|
|
473
|
+
const analysisText =
|
|
474
|
+
`#### Sprint cost analysis\n` +
|
|
475
|
+
`Calibration: ${src} Cycles: estimated ${estCycles}, actual ${actualCycles}\n\n` +
|
|
476
|
+
header + body + `\n` +
|
|
477
|
+
`True-cost estimate (output x ${inputMult}x): ${fmtU(totEstUsd * inputMult)}\n\n` +
|
|
478
|
+
`Outliers (>${thr.outlier_pct}% variance): ${outliers.length ? outliers.join(', ') : 'none'}\n` +
|
|
479
|
+
`Calibration failures (>${thr.calibration_failure_pct}%): ${failures.length ? failures.join(', ') : 'none'}\n`;
|
|
480
|
+
|
|
481
|
+
return { analysisText, byRole, actualCycles, totEstOutputUsd: totEstUsd, totEstTrueUsd: totEstUsd * inputMult, totActUsd };
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// accumulateBucketTokens: join doer log entries back to their S/M/L buckets.
|
|
485
|
+
// Each doer dispatch entry carries label 'doer-c<N>-i<M>' and context
|
|
486
|
+
// 'tasks A, B, C'. We map every listed task ID to its bucket (via the
|
|
487
|
+
// id->bucket map built from taskAssignments) and attribute the entry's
|
|
488
|
+
// outTokens to those buckets, split evenly across the listed IDs that resolve
|
|
489
|
+
// to a known bucket. Returns { S?:{tokens,n}, M?:..., L?:... } -- only buckets
|
|
490
|
+
// that were actually exercised appear (absent buckets stay absent so
|
|
491
|
+
// computeSprintQuote keeps using its calibration defaults).
|
|
492
|
+
function accumulateBucketTokens(logEntries, taskAssignments) {
|
|
493
|
+
const bucketOf = {};
|
|
494
|
+
for (const t of (taskAssignments || [])) {
|
|
495
|
+
if (t && t.id != null && t.bucket != null) bucketOf[String(t.id)] = t.bucket;
|
|
496
|
+
}
|
|
497
|
+
const acc = {};
|
|
498
|
+
for (const e of (logEntries || [])) {
|
|
499
|
+
const label = e.label || '';
|
|
500
|
+
if (label.replace(/-c\d.*$/, '') !== 'doer') continue; // doer entries only
|
|
501
|
+
const tokens = e.outTokens || 0;
|
|
502
|
+
if (tokens <= 0) continue;
|
|
503
|
+
const ctx = String(e.context || '');
|
|
504
|
+
const m = ctx.match(/tasks\s+(.+)$/i);
|
|
505
|
+
if (!m) continue;
|
|
506
|
+
const ids = m[1].split(',').map(s => s.trim()).filter(Boolean);
|
|
507
|
+
// Resolve each listed ID to a bucket; skip IDs we can't map.
|
|
508
|
+
const buckets = ids.map(id => bucketOf[id]).filter(b => b != null);
|
|
509
|
+
if (buckets.length === 0) continue;
|
|
510
|
+
const share = tokens / buckets.length; // even split
|
|
511
|
+
for (const b of buckets) {
|
|
512
|
+
if (!acc[b]) acc[b] = { tokens: 0, n: 0 };
|
|
513
|
+
acc[b].tokens += share;
|
|
514
|
+
acc[b].n += 1;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return acc;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function computeUpdatedCalibration(calibration, analysis, startedAt, taskAssignments, logEntries) {
|
|
521
|
+
const hist = JSON.parse(JSON.stringify(calibration.historical || {}));
|
|
522
|
+
const max = hist.max_sprints_in_sample || 5;
|
|
523
|
+
const prev = Math.min(hist.sprints_sampled || 0, max - 1);
|
|
524
|
+
const n = prev + 1;
|
|
525
|
+
const blend = (old, val) => old == null ? val : (old * prev + val) / n;
|
|
526
|
+
|
|
527
|
+
hist.sprints_sampled = n;
|
|
528
|
+
hist.last_updated = startedAt.replace(/^(\d{4})(\d{2})(\d{2}).*/, '$1-$2-$3');
|
|
529
|
+
hist.cycle_avg = blend(hist.cycle_avg, analysis.actualCycles);
|
|
530
|
+
hist.roles = hist.roles || {};
|
|
531
|
+
|
|
532
|
+
for (const [role, data] of Object.entries(analysis.byRole)) {
|
|
533
|
+
if (data.tokens === 0) continue;
|
|
534
|
+
const avg = data.tokens / data.dispatches;
|
|
535
|
+
const prev_r = hist.roles[role] || { avg_output_tokens: null, sample_n: 0 };
|
|
536
|
+
hist.roles[role] = {
|
|
537
|
+
avg_output_tokens: blend(prev_r.avg_output_tokens, avg),
|
|
538
|
+
sample_n: prev_r.sample_n + data.dispatches,
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const doerTok = analysis.byRole['doer']?.tokens || 0;
|
|
543
|
+
const revTok = analysis.byRole['reviewer']?.tokens || 0;
|
|
544
|
+
if (doerTok > 0) hist.reviewer_ratio_avg = blend(hist.reviewer_ratio_avg, revTok / doerTok);
|
|
545
|
+
|
|
546
|
+
// bucket_avg_tokens join: attribute each doer log entry's outTokens to the
|
|
547
|
+
// S/M/L bucket(s) of the task IDs in its context string, then blend the
|
|
548
|
+
// per-bucket average into hist.bucket_avg_tokens using the same sprints_sampled
|
|
549
|
+
// accounting as roles above. Buckets with no data this sprint keep their prior
|
|
550
|
+
// value untouched (and unexercised buckets stay absent), so computeSprintQuote
|
|
551
|
+
// defaults still apply where we have no history.
|
|
552
|
+
hist.bucket_avg_tokens = hist.bucket_avg_tokens || {};
|
|
553
|
+
const bucketAcc = accumulateBucketTokens(logEntries, taskAssignments);
|
|
554
|
+
for (const [bucket, data] of Object.entries(bucketAcc)) {
|
|
555
|
+
if (data.n === 0) continue;
|
|
556
|
+
const avg = data.tokens / data.n;
|
|
557
|
+
hist.bucket_avg_tokens[bucket] = blend(hist.bucket_avg_tokens[bucket], avg);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return { ...calibration, historical: hist };
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// ---- shell-dispatch parsers (pure) -------------------------------------------
|
|
564
|
+
// These parse the outputs[] array returned by the bounded Haiku shell dispatches
|
|
565
|
+
// (countBeadsBlockers / getReadyStreaks / checkCycleState). Factoring the parse
|
|
566
|
+
// logic out keeps it unit-testable and guarantees a single, side-effect-free
|
|
567
|
+
// parse path -- the dispatch wrappers never loop or branch on parse failure, they
|
|
568
|
+
// just feed outputs here and accept whatever this returns.
|
|
569
|
+
//
|
|
570
|
+
// `outputs` is the agent-returned array of strings (one per command). `rootCount`
|
|
571
|
+
// is the number of leading `bd graph` ID-list commands; the final element is the
|
|
572
|
+
// JSON list command. All functions degrade safely on missing/garbage input.
|
|
573
|
+
|
|
574
|
+
// collectSubtreeIds: union the IDs from the leading rootCount whitespace-joined
|
|
575
|
+
// ID-list outputs (one per sprint goal) into a Set.
|
|
576
|
+
function collectSubtreeIds(outputs, rootCount) {
|
|
577
|
+
const ids = new Set();
|
|
578
|
+
for (let i = 0; i < rootCount; i++) {
|
|
579
|
+
String(outputs[i] || '').trim().split(/\s+/).filter(Boolean).forEach(id => ids.add(id));
|
|
580
|
+
}
|
|
581
|
+
return ids;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// parseBlockers: contract {count, ids} of open issues with priority<=threshold inside
|
|
585
|
+
// the sprint-goal subtree. Missing/short outputs => sentinel {count: 999, ids: []} so the
|
|
586
|
+
// caller treats blockers as present (fail-safe, never exits the sprint early).
|
|
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: [] };
|
|
592
|
+
const subtree = collectSubtreeIds(outputs, rootCount);
|
|
593
|
+
const rootSet = Array.isArray(rootIds) && rootIds.length > 0 ? new Set(rootIds) : null;
|
|
594
|
+
let ids = [];
|
|
595
|
+
try {
|
|
596
|
+
const open = JSON.parse(outputs[openListIdx]);
|
|
597
|
+
ids = Array.isArray(open)
|
|
598
|
+
? open.filter(x => subtree.has(x.id) && (!rootSet || rootSet.has(x.id)) && x.p <= threshold).map(x => x.id)
|
|
599
|
+
: [];
|
|
600
|
+
} catch { ids = []; }
|
|
601
|
+
return { count: ids.length, ids };
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// parseReadyStreaks: contract {totalCount, streaks[]} grouping ready tasks in the
|
|
605
|
+
// subtree by model, ordered by min priority.
|
|
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: [] };
|
|
609
|
+
const subtree = collectSubtreeIds(outputs, rootCount);
|
|
610
|
+
let readyTasks = [];
|
|
611
|
+
try {
|
|
612
|
+
const all = JSON.parse(outputs[readyListIdx]);
|
|
613
|
+
readyTasks = Array.isArray(all) ? all.filter(t => subtree.has(t.id)) : [];
|
|
614
|
+
} catch { readyTasks = []; }
|
|
615
|
+
|
|
616
|
+
// Hoist the known-tiers set and reverse map outside the loop (constant across tasks).
|
|
617
|
+
const KNOWN_TIERS = new Set([TIER_CHEAP, TIER_STANDARD, TIER_PREMIUM]);
|
|
618
|
+
const MODEL_TO_TIER = Object.fromEntries(Object.entries(TIER_TO_MODEL).map(([t, id]) => [id, t]));
|
|
619
|
+
|
|
620
|
+
const byModel = {};
|
|
621
|
+
for (const t of readyTasks) {
|
|
622
|
+
const rawModel = t.m || defaultModel;
|
|
623
|
+
// Normalise: if the stored value is a provider-specific model ID rather than a tier
|
|
624
|
+
// name, resolve it back to a tier so dispatch() works correctly. This handles tasks
|
|
625
|
+
// created before the cheap/standard/premium tier rename. Uses console.warn (not log)
|
|
626
|
+
// so it is safe when this block is extracted into a vm/require context for testing.
|
|
627
|
+
let model = rawModel;
|
|
628
|
+
if (!KNOWN_TIERS.has(rawModel)) {
|
|
629
|
+
const tier = MODEL_TO_TIER[rawModel];
|
|
630
|
+
if (tier) {
|
|
631
|
+
model = tier;
|
|
632
|
+
typeof console !== 'undefined' && console.warn(`[apra-pm] Task ${t.id}: pre-migration model '${rawModel}' normalised to tier '${model}'`);
|
|
633
|
+
} else {
|
|
634
|
+
model = defaultModel;
|
|
635
|
+
typeof console !== 'undefined' && console.warn(`[apra-pm] Task ${t.id}: unrecognised model '${rawModel}', defaulting to '${defaultModel}'`);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
if (!byModel[model]) byModel[model] = [];
|
|
639
|
+
byModel[model].push({ id: t.id, priority: t.p });
|
|
640
|
+
}
|
|
641
|
+
const streaks = Object.entries(byModel).map(([model, tasks]) => ({
|
|
642
|
+
model,
|
|
643
|
+
ids: tasks.slice().sort((a, b) => a.priority - b.priority).map(x => x.id),
|
|
644
|
+
_min: Math.min(...tasks.map(x => x.priority)),
|
|
645
|
+
})).sort((a, b) => a._min - b._min).map(({ model, ids }) => ({ model, ids }));
|
|
646
|
+
|
|
647
|
+
return { totalCount: readyTasks.length, streaks };
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// parseCycleState: contract {planDone, inProgressIds}. planDone is true for a
|
|
651
|
+
// sprint goal when it has >=1 feature and either all features closed or every task has a
|
|
652
|
+
// description. Missing/short outputs => {planDone: false, ...} (fail-safe: never
|
|
653
|
+
// declares planning complete on bad input).
|
|
654
|
+
function parseCycleState(outputs, rootCount) {
|
|
655
|
+
if (!Array.isArray(outputs) || outputs.length < rootCount + 1) return { planDone: false, inProgressIds: [] };
|
|
656
|
+
const inProgressIds = String(outputs[rootCount] || '').trim().split(/\s+/).filter(Boolean);
|
|
657
|
+
const planDone = Array.from({ length: rootCount }).every((_, i) => {
|
|
658
|
+
try {
|
|
659
|
+
const issues = JSON.parse(outputs[i]);
|
|
660
|
+
if (!Array.isArray(issues)) return false;
|
|
661
|
+
const features = issues.filter(x => x.t === 'feature');
|
|
662
|
+
if (features.length === 0) return false;
|
|
663
|
+
const openFts = features.filter(x => x.s !== 'closed');
|
|
664
|
+
if (openFts.length === 0) return true;
|
|
665
|
+
const tasks = issues.filter(x => x.t === 'task');
|
|
666
|
+
if (tasks.length === 0) return false;
|
|
667
|
+
return tasks.every(x => x.d);
|
|
668
|
+
} catch { return false; }
|
|
669
|
+
});
|
|
670
|
+
return { planDone, inProgressIds };
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
// buildSprintSummary assembles a structured human-readable end-of-sprint summary
|
|
674
|
+
// from already-computed inputs. Pure function -- no I/O.
|
|
675
|
+
//
|
|
676
|
+
// Parameters:
|
|
677
|
+
// analysis -- result of computeSprintAnalysis (analysisText, byRole, actualCycles, totActUsd...)
|
|
678
|
+
// sprintQuote -- result of computeSprintQuote or null
|
|
679
|
+
// calibration -- current calibration object
|
|
680
|
+
// opts -- { branch, goal, goalMet, cycleCount, tasksCompleted, tasksOpen, startedAt }
|
|
681
|
+
//
|
|
682
|
+
// Returns: { summaryText } -- markdown string suitable for writing to .analysis.md
|
|
683
|
+
function buildSprintSummary(analysis, sprintQuote, calibration, opts) {
|
|
684
|
+
const { branch = '', goal = '', goalMet = false, cycleCount = 0,
|
|
685
|
+
tasksCompleted = 0, tasksOpen = 0, startedAt = '' } = opts || {};
|
|
686
|
+
const thr = (calibration && calibration.outlier_thresholds) || { outlier_pct: 200, calibration_failure_pct: 500 };
|
|
687
|
+
const cycles = (calibration && calibration.cycle_assumptions) || {};
|
|
688
|
+
const estCycles = cycles.expected || 1;
|
|
689
|
+
|
|
690
|
+
// ---- goal / cycle section
|
|
691
|
+
const goalLine = `**Goal:** ${goal || '(unset)'} -> ${goalMet ? 'MET' : 'NOT MET'}`;
|
|
692
|
+
const cyclesLine = `**Cycles:** estimated ${estCycles}, actual ${cycleCount}`;
|
|
693
|
+
const tasksLine = `**Tasks:** ${tasksCompleted} completed, ${tasksOpen} open/carried-forward`;
|
|
694
|
+
|
|
695
|
+
// ---- cost table (re-use the analysisText already computed)
|
|
696
|
+
const costSection = analysis ? analysis.analysisText : '(no cost analysis available)';
|
|
697
|
+
|
|
698
|
+
// ---- outlier role suggestions
|
|
699
|
+
// sprintQuote.tasks have shape {id,bucket,model,doerTokens,reviewerTokens,outputUsd} -- no `role` field.
|
|
700
|
+
// Estimates per role:
|
|
701
|
+
// doer -> sum of t.doerTokens across all tasks, scaled by estCycles
|
|
702
|
+
// reviewer -> sum of t.reviewerTokens across all tasks, scaled by estCycles
|
|
703
|
+
// overhead -> calibration.fixed_overhead_tokens[role_key] (log-flush scaled by estCycles)
|
|
704
|
+
const overhead = (calibration && calibration.fixed_overhead_tokens) || {};
|
|
705
|
+
const suggestions = [];
|
|
706
|
+
if (analysis && analysis.byRole) {
|
|
707
|
+
for (const [role, data] of Object.entries(analysis.byRole)) {
|
|
708
|
+
if (!data.tokens) continue;
|
|
709
|
+
let estTokensForRole = 0;
|
|
710
|
+
if (role === 'doer') {
|
|
711
|
+
if (!sprintQuote) continue;
|
|
712
|
+
estTokensForRole = (sprintQuote.tasks || [])
|
|
713
|
+
.reduce((s, t) => s + (t.doerTokens || 0), 0) * estCycles;
|
|
714
|
+
} else if (role === 'reviewer') {
|
|
715
|
+
if (!sprintQuote) continue;
|
|
716
|
+
estTokensForRole = (sprintQuote.tasks || [])
|
|
717
|
+
.reduce((s, t) => s + (t.reviewerTokens || 0), 0) * estCycles;
|
|
718
|
+
} else {
|
|
719
|
+
// overhead role: look up in fixed_overhead_tokens (keys use underscores)
|
|
720
|
+
const key = role.replace(/-/g, '_');
|
|
721
|
+
const tok = overhead[key];
|
|
722
|
+
if (typeof tok !== 'number') continue;
|
|
723
|
+
// log-flush runs once per cycle; other overhead roles run once per sprint
|
|
724
|
+
estTokensForRole = role === 'log-flush' ? tok * estCycles : tok;
|
|
725
|
+
}
|
|
726
|
+
if (estTokensForRole <= 0) continue;
|
|
727
|
+
const pctOver = (data.tokens - estTokensForRole) / estTokensForRole * 100;
|
|
728
|
+
if (Math.abs(pctOver) > thr.outlier_pct) {
|
|
729
|
+
const dir = pctOver > 0 ? 'over' : 'under';
|
|
730
|
+
suggestions.push(
|
|
731
|
+
`- \`${role}\` actual ${Math.round(Math.abs(pctOver))}% ${dir} estimate -> ` +
|
|
732
|
+
`consider ${pctOver > 0 ? 'bumping' : 'reducing'} \`fixed_overhead_tokens.${role.replace(/-/g, '_')}\` or bucket sizes`
|
|
733
|
+
);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
const suggestSection = suggestions.length
|
|
738
|
+
? `### Suggested calibration adjustments\n\n${suggestions.join('\n')}\n`
|
|
739
|
+
: `### Suggested calibration adjustments\n\n_No outliers detected -- calibration looks good._\n`;
|
|
740
|
+
|
|
741
|
+
const summaryText =
|
|
742
|
+
`# Sprint summary: ${branch}\n\n` +
|
|
743
|
+
`**Started:** ${startedAt || '(unknown)'} \n` +
|
|
744
|
+
`${goalLine} \n` +
|
|
745
|
+
`${cyclesLine} \n` +
|
|
746
|
+
`${tasksLine}\n\n` +
|
|
747
|
+
`---\n\n` +
|
|
748
|
+
`### Cost analysis\n\n` +
|
|
749
|
+
costSection + `\n` +
|
|
750
|
+
`---\n\n` +
|
|
751
|
+
suggestSection;
|
|
752
|
+
|
|
753
|
+
return { summaryText };
|
|
754
|
+
}
|
|
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
|
+
|
|
953
|
+
// PURE_FUNCTIONS_END
|
|
954
|
+
|
|
955
|
+
function outputCostUsd(tier, tokens) {
|
|
956
|
+
const rate = OUTPUT_PRICE_PER_M[tier] || OUTPUT_PRICE_PER_M[TIER_STANDARD];
|
|
957
|
+
return (tokens / 1_000_000) * rate;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
// Resolve a tier name to the provider-specific model ID for agent() dispatch.
|
|
961
|
+
// Falls back to standard tier if the tier is unrecognised.
|
|
962
|
+
function resolveModel(tier) {
|
|
963
|
+
return TIER_TO_MODEL[tier] || TIER_TO_MODEL[TIER_STANDARD];
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
// Real output-token cost via differential budget.spent() snapshots.
|
|
967
|
+
// budget.spent() is the only actual usage the harness exposes (output tokens only).
|
|
968
|
+
// opts.model is a tier name (cheap/standard/premium); resolved to a model ID for agent().
|
|
969
|
+
// opts.context -- short human string describing what was worked (e.g. "tasks BD-5,BD-6")
|
|
970
|
+
let cycleCostUsd = 0;
|
|
971
|
+
const dispatchLedger = []; // accumulates across all cycles; flushed to sprint-logs/<branch>.jsonl per cycle
|
|
972
|
+
|
|
973
|
+
async function dispatch(prompt, opts) {
|
|
974
|
+
const tier = opts.model || TIER_STANDARD;
|
|
975
|
+
const modelId = resolveModel(tier);
|
|
976
|
+
const before = budget.spent();
|
|
977
|
+
const result = await agent(prompt, { ...opts, model: modelId });
|
|
978
|
+
const outTokens = budget.spent() - before;
|
|
979
|
+
const cost = outputCostUsd(tier, outTokens);
|
|
980
|
+
cycleCostUsd += cost;
|
|
981
|
+
const entry = {
|
|
982
|
+
cycle: cycleCount === 0 ? 'setup' : cycleCount,
|
|
983
|
+
phase: opts.phase || '?',
|
|
984
|
+
label: opts.label || '?',
|
|
985
|
+
model: tier,
|
|
986
|
+
context: opts.context || '',
|
|
987
|
+
outTokens,
|
|
988
|
+
costUsd: parseFloat(cost.toFixed(4)),
|
|
989
|
+
};
|
|
990
|
+
dispatchLedger.push(entry);
|
|
991
|
+
if (outTokens > 0) log(`$${cost.toFixed(4)} ${opts.label || '?'} -- ${opts.context || opts.phase || '?'}`);
|
|
992
|
+
return result;
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
// ------------------------------------------------------------ shell dispatch
|
|
997
|
+
//
|
|
998
|
+
// All three exit-check helpers below run a tiny, fixed set of read-only commands
|
|
999
|
+
// (`bd graph --json <goal-id> | node-extract` plus one `bd list ... | node-extract`)
|
|
1000
|
+
// via a single Haiku dispatch and parse the result with a pure parser above.
|
|
1001
|
+
//
|
|
1002
|
+
// Latency-hardening (gh-7: check-blockers once took 12.5 min):
|
|
1003
|
+
// * Root cause -- the dispatch prompt said "return each command's stdout
|
|
1004
|
+
// verbatim" against a strict array-of-strings schema, with NO turn cap. A
|
|
1005
|
+
// stray escaping/parse concern on the (potentially large) `bd graph` output
|
|
1006
|
+
// made the agent re-run commands and re-emit output across many turns, so a
|
|
1007
|
+
// 5-second job could burn minutes. The node extractors already shrink output
|
|
1008
|
+
// to an ID list / tiny JSON, so the only real failure mode was looping.
|
|
1009
|
+
// * Fix -- (a) every shell dispatch is a SINGLE-ATTEMPT contract: maxTurns is
|
|
1010
|
+
// bounded to (#commands + 1), so the agent physically cannot loop for
|
|
1011
|
+
// minutes; (b) the prompt is explicit that it must run each command exactly
|
|
1012
|
+
// once and never retry/re-run; (c) parsing is fully tolerant (pure parsers
|
|
1013
|
+
// return a fail-safe sentinel on bad/short output) so there is never a reason
|
|
1014
|
+
// to bounce work back to the agent.
|
|
1015
|
+
const SHELL_DISPATCH_PROMPT_HEADER =
|
|
1016
|
+
`Run each command below EXACTLY ONCE, in order. Return each command's stdout ` +
|
|
1017
|
+
`as one string element of outputs[] (same order; outputs.length must equal the ` +
|
|
1018
|
+
`number of commands).\n` +
|
|
1019
|
+
`Rules: do NOT summarize, reformat, interpret, or escape the output. Do NOT ` +
|
|
1020
|
+
`re-run any command. Do NOT retry on empty or unexpected output -- an empty ` +
|
|
1021
|
+
`string is a valid result; just return it. This is a single attempt: run, ` +
|
|
1022
|
+
`capture, return, stop.\n\n`;
|
|
1023
|
+
|
|
1024
|
+
function buildShellPrompt(cmds) {
|
|
1025
|
+
return SHELL_DISPATCH_PROMPT_HEADER + cmds.map((c, i) => `${i + 1}. ${c}`).join('\n');
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
// Bound the agent to one turn per command plus a single wrap-up turn. The harness
|
|
1029
|
+
// stops the dispatch at maxTurns, so a misbehaving agent cannot stall for minutes.
|
|
1030
|
+
function shellMaxTurns(cmds) {
|
|
1031
|
+
return cmds.length + 1;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
async function dispatchShell(cmds, opts) {
|
|
1035
|
+
return dispatch(buildShellPrompt(cmds), {
|
|
1036
|
+
schema: SHELL_OUTPUTS_SCHEMA,
|
|
1037
|
+
maxTurns: shellMaxTurns(cmds),
|
|
1038
|
+
...opts,
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
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
|
+
|
|
1048
|
+
async function countBeadsBlockers(thr, roots) {
|
|
1049
|
+
// Extract only IDs from bd graph to keep output small (avoids $(cat ...) file-reference issue).
|
|
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{}"`;
|
|
1051
|
+
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('[]')}"`;
|
|
1052
|
+
const cmds = [
|
|
1053
|
+
...roots.map(id => `bd graph --json ${id} | ${idExtract}`),
|
|
1054
|
+
`bd list --status=open --json | ${openExtract}`,
|
|
1055
|
+
];
|
|
1056
|
+
const r = await dispatchShell(cmds, { model: MODEL_HAIKU, label: 'check-blockers', phase: 'Develop' });
|
|
1057
|
+
return parseBlockers(r?.outputs, roots.length, roots.length, thr, roots);
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
async function getReadyStreaks(rootIds) {
|
|
1061
|
+
// Extract only IDs from bd graph to keep output small (avoids $(cat ...) file-reference issue).
|
|
1062
|
+
const idExtract = `node -e "const d=require('fs').readFileSync(0,'utf8');try{console.log(JSON.parse(d).issues.map(i=>i.id).join(' '))}catch{}"`;
|
|
1063
|
+
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('[]')}"`;
|
|
1064
|
+
const cmds = [
|
|
1065
|
+
...rootIds.map(id => `bd graph --json ${id} | ${idExtract}`),
|
|
1066
|
+
`bd list --ready --type=task --json | ${taskExtract}`,
|
|
1067
|
+
];
|
|
1068
|
+
const r = await dispatchShell(cmds, { model: MODEL_HAIKU, label: 'ready-streaks', phase: 'Develop' });
|
|
1069
|
+
return parseReadyStreaks(r?.outputs, rootIds.length, rootIds.length, TIER_STANDARD);
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
async function commitFeedback(repo, branch, notes, role, label, phase) {
|
|
1073
|
+
await dispatch(
|
|
1074
|
+
`Repo: ${repo}\nBranch: ${branch}\n\n` +
|
|
1075
|
+
`Write the following reviewer feedback to feedback.md (overwrite if it exists):\n\n` +
|
|
1076
|
+
`${notes}\n\n` +
|
|
1077
|
+
`Then commit and push:\n` +
|
|
1078
|
+
` git -C "${repo}" add feedback.md\n` +
|
|
1079
|
+
` git -C "${repo}" -c user.name='${role}' -c user.email='${role}@pm.local' commit -m "feedback: ${label}"\n` +
|
|
1080
|
+
` git -C "${repo}" push origin ${branch}`,
|
|
1081
|
+
{ model: MODEL_HAIKU, label: `feedback-commit-${label}`, phase }
|
|
1082
|
+
);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
async function checkCycleState(rootIds) {
|
|
1086
|
+
// Extract only the fields needed for planDone check to keep output small.
|
|
1087
|
+
const graphExtract = `node -e "const d=require('fs').readFileSync(0,'utf8');try{const issues=(JSON.parse(d).issues||[]);console.log(JSON.stringify(issues.map(i=>({id:i.id,t:i.issue_type,s:i.status,d:!!(i.description||'').trim()}))))}catch{console.log('[]')}"`;
|
|
1088
|
+
const ipExtract = `node -e "const d=require('fs').readFileSync(0,'utf8');try{console.log(JSON.parse(d).map(i=>i.id).join(' '))}catch{}"`;
|
|
1089
|
+
const cmds = [
|
|
1090
|
+
...rootIds.map(id => `bd graph --json ${id} | ${graphExtract}`),
|
|
1091
|
+
`bd list --status=in_progress --type=task --json | ${ipExtract}`,
|
|
1092
|
+
];
|
|
1093
|
+
const r = await dispatchShell(cmds, { model: MODEL_HAIKU, label: 'cycle-state', phase: 'Plan' });
|
|
1094
|
+
return parseCycleState(r?.outputs, rootIds.length);
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
// ------------------------------------------------------------------ STATE
|
|
1098
|
+
|
|
1099
|
+
let cycleCount = 0;
|
|
1100
|
+
let goalMet = false;
|
|
1101
|
+
let prevOpenIds = [];
|
|
1102
|
+
let headSha = '';
|
|
1103
|
+
let abortReason = '';
|
|
1104
|
+
|
|
1105
|
+
// ------------------------------------------------------------------ SETUP
|
|
1106
|
+
|
|
1107
|
+
phase('Plan');
|
|
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.
|
|
1147
|
+
const setup = await dispatch(
|
|
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` +
|
|
1157
|
+
`Step 5: Merge deploy permissions into .claude/settings.json.\n` +
|
|
1158
|
+
` For each of deploy.md and integ-test-playbook.md that exists:\n` +
|
|
1159
|
+
` a. Read the file and extract lines under the "## Permissions" section\n` +
|
|
1160
|
+
` (stop at the next ## heading). Each non-empty line is a permission entry\n` +
|
|
1161
|
+
` such as "Bash(docker *)" or "Bash(npm run *)".\n` +
|
|
1162
|
+
` b. Read .claude/settings.json (create it as {} if absent).\n` +
|
|
1163
|
+
` c. For each extracted permission not already in permissions.allow, add it.\n` +
|
|
1164
|
+
` d. Write the updated .claude/settings.json back.\n` +
|
|
1165
|
+
` If neither file has a ## Permissions section, skip this step.\n\n` +
|
|
1166
|
+
`Step 6: Load or bootstrap calibration data.\n` +
|
|
1167
|
+
` If sprint-logs/calibration.json exists in the repo root:\n` +
|
|
1168
|
+
` Run: cat sprint-logs/calibration.json\n` +
|
|
1169
|
+
` Return the exact stdout of that command as the "calibrationRaw" field (verbatim -- do not reformat or summarize).\n` +
|
|
1170
|
+
` If it does NOT exist (first run):\n` +
|
|
1171
|
+
` Create the sprint-logs/ directory: mkdir -p sprint-logs\n` +
|
|
1172
|
+
` Write the following JSON exactly to sprint-logs/calibration.json:\n` +
|
|
1173
|
+
JSON.stringify(DEFAULT_CALIBRATION, null, 2) + `\n` +
|
|
1174
|
+
` Return that same JSON text verbatim as the "calibrationRaw" field.\n\n` +
|
|
1175
|
+
`Step 7: Resolve transcript directory.\n` +
|
|
1176
|
+
` Claude subagent conversation logs (JSONL) are stored under:\n` +
|
|
1177
|
+
` $HOME/.claude/projects/<project-slug>/\n` +
|
|
1178
|
+
` where <project-slug> is derived from the repo absolute path by uppercasing the\n` +
|
|
1179
|
+
` drive letter and replacing path separators with dashes.\n` +
|
|
1180
|
+
` Run: ls "$HOME/.claude/projects/" 2>/dev/null\n` +
|
|
1181
|
+
` Find the entry whose slug matches the repo path (e.g. repo=/c/foo/bar -> C--foo-bar).\n` +
|
|
1182
|
+
` If found, return the full path as transcriptDir. If not found, return an empty string.\n\n` +
|
|
1183
|
+
`Return repo (absolute path), branch (confirmed), deployMdExists, playbookExists, startedAt, calibrationRaw, transcriptDir.`,
|
|
1184
|
+
{ model: MODEL_HAIKU, label: 'setup', phase: 'Plan', schema: SETUP_SCHEMA, maxTurns: 20 }
|
|
1185
|
+
);
|
|
1186
|
+
|
|
1187
|
+
if (!setup || !setup.repo || !setup.branch) {
|
|
1188
|
+
log('ERROR: setup failed -- could not assert branch or locate repo');
|
|
1189
|
+
return { error: 'setup failed' };
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
const repo = setup.repo;
|
|
1193
|
+
branch = setup.branch; // use the confirmed/detected branch for all subsequent agent prompts
|
|
1194
|
+
|
|
1195
|
+
// Refuse to run a sprint directly on a protected branch -- all work must go on a feature branch.
|
|
1196
|
+
if (branch === 'main' || branch === 'master') {
|
|
1197
|
+
log(`ERROR: sprint branch resolved to "${branch}" -- refusing to run on a protected branch. Pass a branch arg to create or switch to a sprint branch.`);
|
|
1198
|
+
return { error: 'protected branch' };
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
// Parse calibration from the raw string the setup agent returned verbatim.
|
|
1202
|
+
// Deep-merge with DEFAULT_CALIBRATION so any missing/new field always has a valid value.
|
|
1203
|
+
// historical gets its own merge because it accumulates real sprint history on top of the zeros.
|
|
1204
|
+
let _parsedCalib = {};
|
|
1205
|
+
try { _parsedCalib = JSON.parse(setup.calibrationRaw || '{}'); } catch {}
|
|
1206
|
+
const calibration = Object.assign({}, DEFAULT_CALIBRATION, _parsedCalib, {
|
|
1207
|
+
historical: Object.assign({}, DEFAULT_CALIBRATION.historical, _parsedCalib.historical || {}),
|
|
1208
|
+
complexity_buckets: _parsedCalib.complexity_buckets || DEFAULT_CALIBRATION.complexity_buckets,
|
|
1209
|
+
model_prices_per_1m_output_tokens: _parsedCalib.model_prices_per_1m_output_tokens || DEFAULT_CALIBRATION.model_prices_per_1m_output_tokens,
|
|
1210
|
+
role_models: _parsedCalib.role_models || DEFAULT_CALIBRATION.role_models,
|
|
1211
|
+
fixed_overhead_tokens: _parsedCalib.fixed_overhead_tokens || DEFAULT_CALIBRATION.fixed_overhead_tokens,
|
|
1212
|
+
cycle_assumptions: _parsedCalib.cycle_assumptions || DEFAULT_CALIBRATION.cycle_assumptions,
|
|
1213
|
+
reviewer_ratio: _parsedCalib.reviewer_ratio || DEFAULT_CALIBRATION.reviewer_ratio,
|
|
1214
|
+
input_cost_multiplier: _parsedCalib.input_cost_multiplier || DEFAULT_CALIBRATION.input_cost_multiplier,
|
|
1215
|
+
outlier_thresholds: _parsedCalib.outlier_thresholds || DEFAULT_CALIBRATION.outlier_thresholds,
|
|
1216
|
+
doer_token_ceiling: _parsedCalib.doer_token_ceiling || DEFAULT_CALIBRATION.doer_token_ceiling,
|
|
1217
|
+
});
|
|
1218
|
+
// Sync output prices from loaded calibration so dispatchLedger uses correct rates.
|
|
1219
|
+
Object.assign(OUTPUT_PRICE_PER_M, calibration.model_prices_per_1m_output_tokens || {});
|
|
1220
|
+
|
|
1221
|
+
// State for cost estimation -- populated after plan is APPROVED.
|
|
1222
|
+
let sprintQuote = null;
|
|
1223
|
+
// taskAssignments (id->bucket->model) from the last approved plan-review.
|
|
1224
|
+
// Held at workflow scope so computeUpdatedCalibration can join doer log entries
|
|
1225
|
+
// back to S/M/L buckets at sprint close without re-querying beads.
|
|
1226
|
+
let taskAssignments = [];
|
|
1227
|
+
|
|
1228
|
+
// Derive a filename-safe version of the branch for sprint-logs/.
|
|
1229
|
+
// Replaces path separators and non-safe chars with dashes so that parallel
|
|
1230
|
+
// sprints on different branches never write to the same file.
|
|
1231
|
+
// Timestamp (yyyymmdd_hhmmss) is captured by the setup agent so it stays
|
|
1232
|
+
// stable across workflow resumes (Date.now() is banned in workflow scripts).
|
|
1233
|
+
const sprintLogBranch = branch.replace(/[^a-zA-Z0-9._-]/g, '-').replace(/-{2,}/g, '-').replace(/^-|-$/g, '') || 'default';
|
|
1234
|
+
const sprintLogFile = `sprint-logs/${sprintLogBranch}-${setup.startedAt}.jsonl`;
|
|
1235
|
+
const integTestEnabled = setup.deployMdExists && setup.playbookExists;
|
|
1236
|
+
// startedAt "20260622_020952" -> ISO "2026-06-22T02:09:52Z" (pure string, no Date.now())
|
|
1237
|
+
const sprintTs = setup.startedAt.replace(/^(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})$/, '$1-$2-$3T$4:$5:$6Z');
|
|
1238
|
+
let flushedCount = 0; // how many dispatchLedger entries have been appended to sprintLogFile
|
|
1239
|
+
|
|
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.
|
|
1245
|
+
async function appendNewEntries(label, phase) {
|
|
1246
|
+
const newEntries = dispatchLedger.slice(flushedCount);
|
|
1247
|
+
if (newEntries.length === 0) return;
|
|
1248
|
+
// ts placeholder is replaced by the agent with the real wall-clock time at flush,
|
|
1249
|
+
// not the fixed sprint-start timestamp, so each flush batch gets its own timestamp.
|
|
1250
|
+
const lines = newEntries.map(e => JSON.stringify({ ts: '__FLUSH_TS__', ...e })).join('\n');
|
|
1251
|
+
flushedCount = dispatchLedger.length; // update before dispatch so log-append entry goes in next batch
|
|
1252
|
+
dispatch( // intentionally NOT awaited -- fire-and-forget write to disk only
|
|
1253
|
+
`Step 1: Run: date +%Y-%m-%dT%H:%M:%S%z\n` +
|
|
1254
|
+
`Save the output as FLUSH_TS (e.g. "2026-06-22T22:15:30+0530").\n\n` +
|
|
1255
|
+
`Step 2: Append (do NOT overwrite) the following lines to ${sprintLogFile} (full path: "${repo}/${sprintLogFile}").\n` +
|
|
1256
|
+
`If the file does not exist, create it. If the sprint-logs/ directory does not exist, create it first:\n` +
|
|
1257
|
+
` mkdir -p "${repo}/sprint-logs"\n\n` +
|
|
1258
|
+
`In the lines below, replace every occurrence of "__FLUSH_TS__" with the FLUSH_TS value from Step 1.\n` +
|
|
1259
|
+
`Lines to append (one JSON object per line):\n${lines}\n\n` +
|
|
1260
|
+
`Do not commit, push, or modify any other file. Write the lines to disk and stop.`,
|
|
1261
|
+
{ model: MODEL_HAIKU, label: `log-append-${label}`, phase: phase || 'Develop',
|
|
1262
|
+
context: `${newEntries.length} new entries` }
|
|
1263
|
+
);
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
log(`Repo: ${repo} | Branch: ${setup.branch}`);
|
|
1267
|
+
log(`deploy.md: ${setup.deployMdExists} | integ-test-playbook.md: ${setup.playbookExists}`);
|
|
1268
|
+
if (!setup.deployMdExists) log('WARNING: deploy.md not found -- integration test phase will be skipped');
|
|
1269
|
+
if (!setup.playbookExists) log('WARNING: integ-test-playbook.md not found -- integration test phase will be skipped');
|
|
1270
|
+
if (!integTestEnabled) log('Integration testing disabled for this sprint. Harvest will run after Develop.');
|
|
1271
|
+
|
|
1272
|
+
const rootSummary = rootIds.join(', ');
|
|
1273
|
+
log(`Sprint goals: ${rootSummary} | Goal: ${goal} (P<=${threshold}) | Max cycles: ${maxCycles}`);
|
|
1274
|
+
|
|
1275
|
+
// ------------------------------------------------------------------ SPRINT META RECORD
|
|
1276
|
+
// Write the first JSONL entry (type=meta) capturing sprint metadata and transcript dir.
|
|
1277
|
+
// This is benign for existing consumers: computeSprintAnalysis skips entries without a label.
|
|
1278
|
+
{
|
|
1279
|
+
const metaLine = JSON.stringify({
|
|
1280
|
+
ts: sprintTs, type: 'meta',
|
|
1281
|
+
branch, startedAt: setup.startedAt,
|
|
1282
|
+
roots: rootIds, goal,
|
|
1283
|
+
transcriptDir: setup.transcriptDir || '',
|
|
1284
|
+
});
|
|
1285
|
+
await dispatch(
|
|
1286
|
+
`Write sprint meta record and commit.\n\n` +
|
|
1287
|
+
`Step 1: Ensure sprint-logs/ directory exists:\n` +
|
|
1288
|
+
` mkdir -p "${repo}/sprint-logs"\n\n` +
|
|
1289
|
+
`Step 2: Append (do NOT overwrite) the following line to ${sprintLogFile} (full path: "${repo}/${sprintLogFile}").\n` +
|
|
1290
|
+
` If the file does not exist, create it first.\n\n` +
|
|
1291
|
+
`Line:\n${metaLine}\n\n` +
|
|
1292
|
+
`Step 3: Commit and push:\n` +
|
|
1293
|
+
` git -C "${repo}" add sprint-logs/\n` +
|
|
1294
|
+
` git -C "${repo}" -c user.name='pm' -c user.email='pm@pm.local' commit -m "chore: sprint-meta ${branch} ${setup.startedAt}"\n` +
|
|
1295
|
+
` git -C "${repo}" push origin ${branch}\n` +
|
|
1296
|
+
`Do not modify any other file. Return "OK" when done.`,
|
|
1297
|
+
{ model: MODEL_HAIKU, label: 'sprint-meta', phase: 'Plan' }
|
|
1298
|
+
);
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
// ------------------------------------------------------------------ SPRINT LOOP
|
|
1302
|
+
|
|
1303
|
+
while (cycleCount < maxCycles) {
|
|
1304
|
+
cycleCount++;
|
|
1305
|
+
cycleCostUsd = 0;
|
|
1306
|
+
log(`\n=== Cycle ${cycleCount}/${maxCycles} | goal: ${goal} ===`);
|
|
1307
|
+
|
|
1308
|
+
// ---------------------------------------------------------------- RESUME CHECK + CYCLE CHECKPOINT
|
|
1309
|
+
|
|
1310
|
+
phase('Plan');
|
|
1311
|
+
|
|
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
|
+
]);
|
|
1328
|
+
log(`Cycle state: planDone=${cycleState.planDone} inProgress=[${cycleState.inProgressIds.join(', ')}]`);
|
|
1329
|
+
|
|
1330
|
+
// Reset any tasks orphaned in_progress from a previous crashed run.
|
|
1331
|
+
if (cycleState.inProgressIds.length > 0) {
|
|
1332
|
+
log(`Resetting ${cycleState.inProgressIds.length} orphaned in_progress task(s) to open`);
|
|
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
|
+
});
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
// ---------------------------------------------------------------- PLAN
|
|
1342
|
+
|
|
1343
|
+
let planApproved = cycleState.planDone;
|
|
1344
|
+
let planFeedback = '';
|
|
1345
|
+
const MAX_PLAN_ITER = 3;
|
|
1346
|
+
|
|
1347
|
+
if (planApproved) {
|
|
1348
|
+
log(`Plan already complete -- skipping plan loop for cycle ${cycleCount}`);
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
for (let pi = 0; pi < MAX_PLAN_ITER && !planApproved; pi++) {
|
|
1352
|
+
const plannerLabel = `planner-c${cycleCount}-r${pi}`;
|
|
1353
|
+
|
|
1354
|
+
const plannerResult = await dispatch(
|
|
1355
|
+
`Repo: ${repo}\nBranch: ${branch}\nBase branch: ${base_branch}\n` +
|
|
1356
|
+
`Sprint goals: ${rootSummary}\n` +
|
|
1357
|
+
(requirementsFile ? `Additional context: ${requirementsFile}\n` : '') +
|
|
1358
|
+
`\n` +
|
|
1359
|
+
(planFeedback
|
|
1360
|
+
? `Plan-reviewer feedback from the previous round (read feedback.md in ${repo} for full details):\n${planFeedback}\nAddress every item before proceeding.\n\n`
|
|
1361
|
+
: '') +
|
|
1362
|
+
`Inspect existing state first:\n` +
|
|
1363
|
+
` ${rootIds.map(id => `bd show ${id} && bd graph --compact ${id}`).join('\n ')}\n` +
|
|
1364
|
+
`Run: bd show <id> on any existing features/tasks to read their current descriptions.\n` +
|
|
1365
|
+
`Then build or complete the feature+task DAG -- create only what is missing:\n` +
|
|
1366
|
+
` - BEFORE creating any feature or task, run: bd search "<title>" --status all\n` +
|
|
1367
|
+
` If a matching issue already exists, update it instead of creating a duplicate.\n` +
|
|
1368
|
+
`\n` +
|
|
1369
|
+
`DEPENDENCY WIRING -- read this carefully. "bd dep add A B" means A CANNOT CLOSE until B is done.\n` +
|
|
1370
|
+
`The correct wiring direction is: parents depend on children (children unblock first).\n` +
|
|
1371
|
+
`\n` +
|
|
1372
|
+
` Step 1 -- wire sprint goal -> child (goal waits for children):\n` +
|
|
1373
|
+
` bd dep add <goal-id> <child-id>\n` +
|
|
1374
|
+
` After this: "bd ready" will NOT show the sprint goal (it's waiting). Children show as ready.\n` +
|
|
1375
|
+
`\n` +
|
|
1376
|
+
` Step 2 -- wire feature -> tasks (feature waits for tasks):\n` +
|
|
1377
|
+
` bd dep add <feature-id> <impl-task-id>\n` +
|
|
1378
|
+
` bd dep add <feature-id> <test-task-id>\n` +
|
|
1379
|
+
` After this: "bd ready" will show impl-task (the leaf). Feature is now blocked.\n` +
|
|
1380
|
+
`\n` +
|
|
1381
|
+
` Step 3 -- wire test after impl:\n` +
|
|
1382
|
+
` bd dep add <test-task-id> <impl-task-id>\n` +
|
|
1383
|
+
` After this: "bd ready" shows only impl-task. test-task unblocks once impl-task closes.\n` +
|
|
1384
|
+
`\n` +
|
|
1385
|
+
` VERIFY after wiring: run "bd ready" -- it must return impl tasks, NOT sprint goals or blocked parents.\n` +
|
|
1386
|
+
` If sprint goals appear in "bd ready" the deps are backwards -- fix them before continuing.\n` +
|
|
1387
|
+
`\n` +
|
|
1388
|
+
` IMPORTANT: Each task belongs to exactly ONE feature. Never share a task across features.\n` +
|
|
1389
|
+
`\n` +
|
|
1390
|
+
` Break each sprint goal into child issues: bd create --parent <goal-id> (use type=feature for sub-goals, type=task for leaf work).\n` +
|
|
1391
|
+
` Create type=task issues for each feature: implementation tasks AND integration\n` +
|
|
1392
|
+
` test development tasks (prefix test tasks with "[test]" in the title)\n` +
|
|
1393
|
+
` Features P1/P2; tasks one level below their parent feature (P1 feature -> P2 tasks, P2 feature -> P3 tasks)\n` +
|
|
1394
|
+
` Each task must be completable in one agent session (1-3 file changes max)\n` +
|
|
1395
|
+
` Every task needs clear acceptance criteria in its description\n` +
|
|
1396
|
+
` - Assign each task a tier AND complexity bucket based on complexity -- after creating or updating each\n` +
|
|
1397
|
+
` task, run: bd update <id> --set-metadata model=<tier>\n` +
|
|
1398
|
+
` Available tiers and when to use them:\n` +
|
|
1399
|
+
` ${TIER_CHEAP} -- mechanical work: rename, config tweak, move file, simple wiring\n` +
|
|
1400
|
+
` ${TIER_STANDARD} -- standard work: new function, test suite, API endpoint, refactor\n` +
|
|
1401
|
+
` ${TIER_PREMIUM} -- hard work: architecture, multi-file design, ambiguous requirements\n` +
|
|
1402
|
+
` Complexity buckets (S/M/L) are assigned by the plan-reviewer based on task scope.\n` +
|
|
1403
|
+
` Every task MUST receive a bucket assignment -- tasks without a bucket cannot be cost-estimated.\n` +
|
|
1404
|
+
` - Group tasks so consecutive tasks in dependency order share a tier where\n` +
|
|
1405
|
+
` possible -- this minimises tier-switching overhead during execution\n` +
|
|
1406
|
+
(cycleCount > 1
|
|
1407
|
+
? `This is cycle ${cycleCount}. Focus on open issues only.\n` +
|
|
1408
|
+
`Do NOT add new scope beyond the original sprint goals and open bugs/enhancements.\n` +
|
|
1409
|
+
`Do NOT re-create tasks that are already closed.\n`
|
|
1410
|
+
: '') +
|
|
1411
|
+
`Confirm with any text when done.`,
|
|
1412
|
+
{ model: MODEL_OPUS, label: plannerLabel, phase: 'Plan', agentType: 'planner',
|
|
1413
|
+
context: `planning sprint goals ${rootSummary}` }
|
|
1414
|
+
);
|
|
1415
|
+
|
|
1416
|
+
if (!plannerResult) {
|
|
1417
|
+
log(`Planner returned null on cycle ${cycleCount} round ${pi} -- retrying`);
|
|
1418
|
+
continue;
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
const planReviewerLabel = `plan-reviewer-c${cycleCount}-r${pi}`;
|
|
1422
|
+
const planReview = await dispatch(
|
|
1423
|
+
`Repo: ${repo}\nBranch: ${branch}\nSprint goals: ${rootSummary}\n` +
|
|
1424
|
+
`Calibration file: ${repo}/sprint-logs/calibration.json (read this first if it exists)\n\n` +
|
|
1425
|
+
`Review the beads DAG for these sprint goals ONLY: ${rootSummary}\n` +
|
|
1426
|
+
`Run: ${rootIds.map(id => `bd show ${id}`).join(' && ')} to inspect each sprint goal.\n` +
|
|
1427
|
+
`Run: ${rootIds.map(id => `bd graph --compact ${id}`).join(' && ')} for the full dependency subtree.\n` +
|
|
1428
|
+
`Run: bd show <id> to inspect individual issues in depth.\n` +
|
|
1429
|
+
`Run: bd ready -- this is your FIRST correctness check.\n` +
|
|
1430
|
+
`Do NOT review or comment on issues outside these sprint goals.\n\n` +
|
|
1431
|
+
`Follow your runbook (plan-reviewer.md) step by step:\n` +
|
|
1432
|
+
` Steps 1-2: inspect the DAG and check all quality criteria.\n` +
|
|
1433
|
+
` Step 3: classify each task -- assign complexity bucket (S/M/L) and read its model\n` +
|
|
1434
|
+
` from beads metadata. If a task has no model metadata, note it in your verdict\n` +
|
|
1435
|
+
` notes as a warning but do NOT return CHANGES NEEDED for it -- the workflow has a fallback.\n` +
|
|
1436
|
+
` Step 4: return verdict, notes, and taskAssignments (id + bucket + model per task).\n\n` +
|
|
1437
|
+
`Notes must be specific: include issue IDs and exact "bd dep add" commands to fix\n` +
|
|
1438
|
+
`any dependency direction problems.`,
|
|
1439
|
+
{ model: MODEL_SONNET, label: planReviewerLabel, phase: 'Plan', schema: PLAN_REVIEW_SCHEMA, agentType: 'plan-reviewer',
|
|
1440
|
+
context: `reviewing plan for sprint goals ${rootSummary}` }
|
|
1441
|
+
);
|
|
1442
|
+
|
|
1443
|
+
if (approved(planReview)) {
|
|
1444
|
+
planApproved = true;
|
|
1445
|
+
log(`Plan APPROVED on cycle ${cycleCount} round ${pi + 1}`);
|
|
1446
|
+
|
|
1447
|
+
// Persist taskAssignments at workflow scope for the calibration join at sprint close.
|
|
1448
|
+
taskAssignments = planReview.taskAssignments || [];
|
|
1449
|
+
|
|
1450
|
+
// Compute sprint cost quote in pure JS -- no agent does arithmetic.
|
|
1451
|
+
sprintQuote = computeSprintQuote(taskAssignments, calibration);
|
|
1452
|
+
const sc = sprintQuote.scenarios;
|
|
1453
|
+
log(`Sprint quote (${sprintQuote.calibrationSource}, ${taskAssignments.length} tasks): ` +
|
|
1454
|
+
`output-only: opt=$${sc.optimistic.outputOnly.toFixed(3)} ` +
|
|
1455
|
+
`exp=$${sc.expected.outputOnly.toFixed(3)} ` +
|
|
1456
|
+
`pess=$${sc.pessimistic.outputOnly.toFixed(3)} ` +
|
|
1457
|
+
`| true-est (x${sprintQuote.inputMultiplier.toFixed(1)}): ` +
|
|
1458
|
+
`exp=$${sc.expected.total.toFixed(3)}`);
|
|
1459
|
+
|
|
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 =>
|
|
1465
|
+
`bd update ${t.id} --notes="cost-estimate: bucket=${t.bucket} model=${t.model} ` +
|
|
1466
|
+
`doer_tokens=${t.doerTokens} reviewer_tokens=${t.reviewerTokens} output_usd=${t.outputUsd.toFixed(4)}"`
|
|
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
|
+
});
|
|
1477
|
+
} else {
|
|
1478
|
+
planFeedback = (planReview && planReview.notes) || '';
|
|
1479
|
+
log(`Plan needs changes: ${planFeedback.slice(0, 120)}`);
|
|
1480
|
+
await commitFeedback(repo, branch, planFeedback, 'pm-plan-reviewer', planReviewerLabel, 'Plan');
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
if (!planApproved) {
|
|
1485
|
+
log(`Plan not approved after ${MAX_PLAN_ITER} rounds -- aborting sprint`);
|
|
1486
|
+
abortReason = 'plan not approved';
|
|
1487
|
+
break;
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
// ---------------------------------------------------------------- DEVELOP
|
|
1491
|
+
|
|
1492
|
+
phase('Develop');
|
|
1493
|
+
|
|
1494
|
+
const MAX_DEV_ITER = 20;
|
|
1495
|
+
let devIter = 0;
|
|
1496
|
+
let devFeedback = '';
|
|
1497
|
+
|
|
1498
|
+
while (devIter < MAX_DEV_ITER) {
|
|
1499
|
+
const streakResult = await getReadyStreaks(rootIds);
|
|
1500
|
+
if (streakResult.totalCount === 0) {
|
|
1501
|
+
log(`No ready tasks -- develop phase complete (${devIter} iterations)`);
|
|
1502
|
+
break;
|
|
1503
|
+
}
|
|
1504
|
+
log(`Dev iter ${devIter} c${cycleCount}: ${streakResult.totalCount} ready task(s) across ${streakResult.streaks.length} model streak(s)`);
|
|
1505
|
+
|
|
1506
|
+
// Dispatch one doer per model streak; collect all worked task IDs for the reviewer.
|
|
1507
|
+
const workedIds = [];
|
|
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]));
|
|
1514
|
+
|
|
1515
|
+
for (const streak of streakResult.streaks) {
|
|
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)}` : ''}]`);
|
|
1531
|
+
|
|
1532
|
+
const doerResult = await dispatch(
|
|
1533
|
+
`Repo: ${repo}\nBranch: ${branch}\n\n` +
|
|
1534
|
+
(devFeedback
|
|
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`
|
|
1536
|
+
: '') +
|
|
1537
|
+
`Work ONLY these tasks (in order): ${fittedIds.join(', ')}\n` +
|
|
1538
|
+
`Confirm each is still unblocked with: bd show <id>\n` +
|
|
1539
|
+
`For each task:\n` +
|
|
1540
|
+
` - Run: bd update <id> --claim\n` +
|
|
1541
|
+
` - Implement the work described (code, tests, config -- whatever the task requires)\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` +
|
|
1544
|
+
` - NEVER close a type=feature or type=bug issue -- only close type=task\n` +
|
|
1545
|
+
`Work all listed tasks then stop and return status "VERIFY".\n` +
|
|
1546
|
+
`Always return VERIFY -- never return anything else.`,
|
|
1547
|
+
{ model: streak.model, label: doerLabel, phase: 'Develop', schema: DOER_STATUS_SCHEMA, agentType: 'doer',
|
|
1548
|
+
context: `tasks ${fittedIds.join(', ')}` }
|
|
1549
|
+
);
|
|
1550
|
+
|
|
1551
|
+
if (!doerResult) {
|
|
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;
|
|
1565
|
+
break;
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
if (doerResult.status !== 'VERIFY') {
|
|
1569
|
+
log(`Unexpected doer status "${doerResult.status}" -- aborting`);
|
|
1570
|
+
abortReason = 'unexpected doer status';
|
|
1571
|
+
streakAbort = true;
|
|
1572
|
+
break;
|
|
1573
|
+
}
|
|
1574
|
+
workedIds.push(...fittedIds);
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
devIter++;
|
|
1578
|
+
if (doerNullReset) continue;
|
|
1579
|
+
if (streakAbort) break;
|
|
1580
|
+
|
|
1581
|
+
// Reviewer tier matches the highest tier used across all streaks:
|
|
1582
|
+
// any premium streak -> premium; otherwise standard (cheap work reviewed at standard minimum).
|
|
1583
|
+
const usedModels = streakResult.streaks.map(s => s.model);
|
|
1584
|
+
const reviewerModel = usedModels.includes(TIER_PREMIUM) ? TIER_PREMIUM : TIER_STANDARD;
|
|
1585
|
+
|
|
1586
|
+
// One reviewer pass covering all streaks worked this iteration.
|
|
1587
|
+
const reviewerLabel = `reviewer-c${cycleCount}-i${devIter}: ${labelTaskIds(workedIds)}`;
|
|
1588
|
+
const review = await dispatch(
|
|
1589
|
+
`Repo: ${repo}\nBranch: ${branch}\nBase branch: ${base_branch}\n` +
|
|
1590
|
+
`Sprint goals: ${rootSummary}\nTasks worked this iteration: ${workedIds.join(', ')}\n\n` +
|
|
1591
|
+
`Review ONLY the work done for the tasks listed above.\n` +
|
|
1592
|
+
`Run: bd show <id> for each task to read its acceptance criteria.\n` +
|
|
1593
|
+
`Run: git -C "${repo}" diff ${base_branch}...${branch} to see the changes.\n` +
|
|
1594
|
+
`Do NOT comment on code or issues outside the listed tasks.\n` +
|
|
1595
|
+
`Check: code correctness, test coverage, adherence to each task's acceptance criteria.\n` +
|
|
1596
|
+
`If a task needs rework, reopen it: bd update <id> --status=open\n` +
|
|
1597
|
+
`CHANGES NEEDED verdict must include specific actionable feedback tied to a task ID.\n` +
|
|
1598
|
+
`APPROVED means all committed work meets acceptance criteria.`,
|
|
1599
|
+
{ model: reviewerModel, label: reviewerLabel, phase: 'Develop', schema: REVIEW_SCHEMA, agentType: 'reviewer',
|
|
1600
|
+
context: `reviewing tasks ${workedIds.join(', ')}` }
|
|
1601
|
+
);
|
|
1602
|
+
log(`Reviewer c${cycleCount}-i${devIter}: ${(review && review.verdict) || 'null'} -- ${labelTaskIds(workedIds)}`);
|
|
1603
|
+
|
|
1604
|
+
if (!approved(review)) {
|
|
1605
|
+
devFeedback = (review && review.notes) || '';
|
|
1606
|
+
log(`Reviewer feedback: ${devFeedback.slice(0, 120)}`);
|
|
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
|
|
1617
|
+
// Reopened tasks will show in bd ready next iteration
|
|
1618
|
+
} else {
|
|
1619
|
+
devFeedback = '';
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
// JIT flush: append this iteration's entries immediately so the log grows as work lands.
|
|
1623
|
+
await appendNewEntries(`iter-c${cycleCount}-i${devIter}`, 'Develop');
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
if (abortReason) break;
|
|
1627
|
+
|
|
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' }
|
|
1635
|
+
);
|
|
1636
|
+
if (pushShaResult && Array.isArray(pushShaResult.outputs) && pushShaResult.outputs[1]) {
|
|
1637
|
+
headSha = pushShaResult.outputs[1].trim();
|
|
1638
|
+
}
|
|
1639
|
+
log(`HEAD SHA: ${headSha}`);
|
|
1640
|
+
|
|
1641
|
+
// ---------------------------------------------------------------- INTEGRATION TEST (skip if files missing)
|
|
1642
|
+
|
|
1643
|
+
if (integTestEnabled) {
|
|
1644
|
+
phase('Test');
|
|
1645
|
+
|
|
1646
|
+
// -- Deploy --
|
|
1647
|
+
const deployLabel = `deployer-c${cycleCount}`;
|
|
1648
|
+
const deployResult = await dispatch(
|
|
1649
|
+
`Repo: ${repo}\nBranch: ${branch}\nCycle: ${cycleCount}\n\n` +
|
|
1650
|
+
`Follow the integration test playbook and deploy.md:\n` +
|
|
1651
|
+
(cycleCount === 1
|
|
1652
|
+
? `1. Run the Setup section of integ-test-playbook.md to bring up the test environment.\n`
|
|
1653
|
+
: `1. Run the Reset section of integ-test-playbook.md to restore pristine state.\n`) +
|
|
1654
|
+
`2. Follow all steps in deploy.md to deploy the build.\n` +
|
|
1655
|
+
`3. Run the smoke test defined in deploy.md.\n` +
|
|
1656
|
+
`4. Return deployed: true if the smoke test passes, false otherwise.\n` +
|
|
1657
|
+
`5. If deployed is false, include the error output in notes.`,
|
|
1658
|
+
{ model: MODEL_SONNET, label: deployLabel, phase: 'Test', agentType: 'deployer',
|
|
1659
|
+
schema: { type: 'object', required: ['deployed'], properties: {
|
|
1660
|
+
deployed: { type: 'boolean' }, notes: { type: 'string' } } } }
|
|
1661
|
+
);
|
|
1662
|
+
|
|
1663
|
+
if (!deployResult || !deployResult.deployed) {
|
|
1664
|
+
const msg = (deployResult && deployResult.notes) || 'no details';
|
|
1665
|
+
log(`Deploy failed on cycle ${cycleCount}: ${msg.slice(0, 200)}`);
|
|
1666
|
+
log('Skipping integration tests this cycle -- teardown and continue');
|
|
1667
|
+
// Teardown before next cycle
|
|
1668
|
+
await dispatch(
|
|
1669
|
+
`Run the Teardown section of integ-test-playbook.md to clean up the test environment.`,
|
|
1670
|
+
{ model: MODEL_SONNET, label: `teardown-c${cycleCount}-fail`, phase: 'Test', agentType: 'deployer' }
|
|
1671
|
+
);
|
|
1672
|
+
} else {
|
|
1673
|
+
// -- Integration test run --
|
|
1674
|
+
const integLabel = `integ-runner-c${cycleCount}`;
|
|
1675
|
+
|
|
1676
|
+
const integResult = await dispatch(
|
|
1677
|
+
`Repo: ${repo}\nBranch: ${branch}\nCycle: ${cycleCount}\n` +
|
|
1678
|
+
`Sprint goals: ${rootSummary}\n\n` +
|
|
1679
|
+
`Run: bd list --type=feature --status=open\n` +
|
|
1680
|
+
`For each open feature, execute its integration tests.\n\n` +
|
|
1681
|
+
`For each feature:\n` +
|
|
1682
|
+
` PASS: all tests pass -> bd close <feature-id>\n` +
|
|
1683
|
+
` FAIL: tests fail -> bd create --title="[integ] <description>" ` +
|
|
1684
|
+
`--description="Feature: <id>\\nExpected: <what>\\nActual: <what>\\nTest: <which>" ` +
|
|
1685
|
+
`--type=bug --priority=<1=core requirement unmet, 2=partial, 3=quality>\n` +
|
|
1686
|
+
` Keep feature open on failure or if inconclusive.\n\n` +
|
|
1687
|
+
`Priority rules:\n` +
|
|
1688
|
+
` P0: system won't start or core path completely broken\n` +
|
|
1689
|
+
` P1: requirement from sprint goal explicitly not met\n` +
|
|
1690
|
+
` P2: requirement partially met, degraded behaviour\n` +
|
|
1691
|
+
` P3: quality, performance, or UX issue not blocking core function\n\n` +
|
|
1692
|
+
`Before creating a new bug, check bd search "[integ]" -- update existing if duplicate.\n\n` +
|
|
1693
|
+
`Return featuresClosed (count), issuesCreated (count), summary (one paragraph).`,
|
|
1694
|
+
{ model: MODEL_SONNET, label: integLabel, phase: 'Test', schema: INTEG_RUN_SCHEMA, agentType: 'integ-test-runner' }
|
|
1695
|
+
);
|
|
1696
|
+
if (integResult) {
|
|
1697
|
+
log(`Integration: ${integResult.featuresClosed} features closed, ${integResult.issuesCreated} issues created`);
|
|
1698
|
+
log(`Summary: ${integResult.summary}`);
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
// -- Teardown --
|
|
1702
|
+
await dispatch(
|
|
1703
|
+
`Run the Teardown section of integ-test-playbook.md to fully clean up the test environment.`,
|
|
1704
|
+
{ model: MODEL_SONNET, label: `teardown-c${cycleCount}`, phase: 'Test', agentType: 'deployer' }
|
|
1705
|
+
);
|
|
1706
|
+
|
|
1707
|
+
// JIT flush: append test-phase entries (deployer, integ-runner, teardown) immediately.
|
|
1708
|
+
await appendNewEntries(`test-c${cycleCount}`, 'Test');
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
// ---------------------------------------------------------------- EXIT CHECK
|
|
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
|
+
}
|
|
1742
|
+
const currentOpenIds = (blockers.ids || []).slice().sort();
|
|
1743
|
+
log(`Exit check: ${blockers.count} open issues at P<=${threshold} -- IDs: [${currentOpenIds.join(', ')}]`);
|
|
1744
|
+
|
|
1745
|
+
// Straggler flush: catches plan-phase, push, head-sha, check-blockers, and any entries
|
|
1746
|
+
// not yet appended (e.g. when integ tests are disabled, or on abortReason paths).
|
|
1747
|
+
const cycleLedger = dispatchLedger.filter(e => e.cycle === cycleCount);
|
|
1748
|
+
const cycleTotal = cycleLedger.reduce((s, e) => s + e.costUsd, 0);
|
|
1749
|
+
log(`Cycle ${cycleCount} cost: $${cycleTotal.toFixed(4)} output across ${cycleLedger.length} dispatches`);
|
|
1750
|
+
await appendNewEntries(`end-c${cycleCount}`, integTestEnabled ? 'Test' : 'Develop');
|
|
1751
|
+
|
|
1752
|
+
// No-progress check: if no issue from the previous cycle was resolved, abort.
|
|
1753
|
+
if (cycleCount > 1 && prevOpenIds.length > 0) {
|
|
1754
|
+
const closedFromPrev = prevOpenIds.filter(id => !currentOpenIds.includes(id));
|
|
1755
|
+
if (closedFromPrev.length === 0) {
|
|
1756
|
+
log(`No progress in cycle ${cycleCount}: same ${prevOpenIds.length} issues unresolved -- aborting`);
|
|
1757
|
+
abortReason = 'no progress';
|
|
1758
|
+
break;
|
|
1759
|
+
}
|
|
1760
|
+
log(`Progress: ${closedFromPrev.length} issue(s) resolved this cycle`);
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
if (blockers.count === 0) {
|
|
1764
|
+
goalMet = true;
|
|
1765
|
+
log(`Goal met after ${cycleCount} cycle(s)`);
|
|
1766
|
+
break;
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
prevOpenIds = currentOpenIds;
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1772
|
+
// ------------------------------------------------------------------ FINAL REVIEW
|
|
1773
|
+
|
|
1774
|
+
phase('Harvest');
|
|
1775
|
+
|
|
1776
|
+
const finalReviewLabel = 'final-reviewer';
|
|
1777
|
+
const finalReview = await dispatch(
|
|
1778
|
+
`Repo: ${repo}\nBranch: ${branch}\nBase branch: ${base_branch}\n` +
|
|
1779
|
+
`Sprint goals: ${rootSummary}\nGoal: ${goal}\n` +
|
|
1780
|
+
(abortReason ? `Sprint ended early: ${abortReason}. Review what was completed.\n` : '') +
|
|
1781
|
+
(goalMet ? `Goal was met: all P<=${threshold} issues resolved.\n` : `Goal not yet met.\n`) +
|
|
1782
|
+
`\nReview the overall output of this sprint:\n` +
|
|
1783
|
+
` - Does the work address the original sprint goals?\n` +
|
|
1784
|
+
` - Are there obvious gaps or regressions?\n` +
|
|
1785
|
+
` - Is the codebase in a releasable state for what was completed?\n` +
|
|
1786
|
+
`APPROVED means the work is ready to harvest and raise as a PR.\n` +
|
|
1787
|
+
`CHANGES NEEDED means critical issues were found; include specific findings in notes.`,
|
|
1788
|
+
{ model: MODEL_OPUS, label: finalReviewLabel, phase: 'Harvest', schema: REVIEW_SCHEMA, agentType: 'reviewer' }
|
|
1789
|
+
);
|
|
1790
|
+
log(`Final review: ${finalReview && finalReview.verdict || 'null'}`);
|
|
1791
|
+
|
|
1792
|
+
if (!approved(finalReview)) {
|
|
1793
|
+
const notes = (finalReview && finalReview.notes) || '';
|
|
1794
|
+
log(`Final review not approved -- aborting before harvest. Notes: ${notes.slice(0, 300)}`);
|
|
1795
|
+
return { cycles: cycleCount, goalMet, goal, abortReason: abortReason || 'final review rejected', finalReviewNotes: notes };
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
// ------------------------------------------------------------------ HARVEST
|
|
1799
|
+
|
|
1800
|
+
const logEntries = dispatchLedger;
|
|
1801
|
+
|
|
1802
|
+
// Compute estimate-vs-actual analysis entirely in JS.
|
|
1803
|
+
const sprintAnalysis = computeSprintAnalysis(sprintQuote, logEntries, calibration, cycleCount);
|
|
1804
|
+
log('Sprint cost analysis computed (JS):\n' + sprintAnalysis.analysisText);
|
|
1805
|
+
|
|
1806
|
+
// Build structured summary and write sprint-logs/<branch>-<ts>.analysis.md artefact.
|
|
1807
|
+
const tasksCompleted = goalMet
|
|
1808
|
+
? (sprintQuote ? sprintQuote.tasks.length : 0)
|
|
1809
|
+
: Math.max(0, (sprintQuote ? sprintQuote.tasks.length : 0) - prevOpenIds.length);
|
|
1810
|
+
const tasksOpen = goalMet ? 0 : prevOpenIds.length;
|
|
1811
|
+
const sprintSummary = buildSprintSummary(sprintAnalysis, sprintQuote, calibration, {
|
|
1812
|
+
branch, goal, goalMet, cycleCount, tasksCompleted, tasksOpen, startedAt: setup.startedAt,
|
|
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;
|
|
1819
|
+
log('Sprint summary:\n' + sprintSummary.summaryText);
|
|
1820
|
+
const analysisArtifactFile = `sprint-logs/${sprintLogBranch}-${setup.startedAt}.analysis.md`;
|
|
1821
|
+
|
|
1822
|
+
const harvestLabel = 'harvester';
|
|
1823
|
+
const harvestResult = await dispatch(
|
|
1824
|
+
`Repo: ${repo}\nBranch: ${branch}\nBase branch: ${base_branch}\n` +
|
|
1825
|
+
`Sprint goals: ${rootSummary}\nCycles completed: ${cycleCount}\nGoal met: ${goalMet}\n` +
|
|
1826
|
+
`sprintLogFile: ${sprintLogFile}\n` +
|
|
1827
|
+
`analysisArtifactFile: ${analysisArtifactFile}\n\n` +
|
|
1828
|
+
`The sprint is complete. Harvest the sprint artefacts.\n` +
|
|
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` +
|
|
1834
|
+
`costAnalysis (insert this block verbatim into CHANGELOG.md after the summary paragraph):\n` +
|
|
1835
|
+
`${sprintAnalysis.analysisText}\n` +
|
|
1836
|
+
`Final review notes to include in CHANGELOG:\n` +
|
|
1837
|
+
`${(finalReview && finalReview.notes) || '(none)'}\n\n` +
|
|
1838
|
+
`Return status "OK" if successful, "FAILED" with notes otherwise.`,
|
|
1839
|
+
{ model: MODEL_SONNET, label: harvestLabel, phase: 'Harvest', schema: HARVEST_SCHEMA, agentType: 'harvester' }
|
|
1840
|
+
);
|
|
1841
|
+
|
|
1842
|
+
if (!harvestResult || harvestResult.status !== 'OK') {
|
|
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}`);
|
|
1857
|
+
return { cycles: cycleCount, goalMet, goal, harvest: 'failed' };
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
// ------------------------------------------------------------------ CALIBRATION UPDATE + CLOSE GOALS (parallel)
|
|
1861
|
+
// Update historical averages in calibration.json after every successful sprint.
|
|
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.
|
|
1865
|
+
|
|
1866
|
+
const updatedCalibration = computeUpdatedCalibration(calibration, sprintAnalysis, setup.startedAt, taskAssignments, logEntries);
|
|
1867
|
+
const calibrationJson = JSON.stringify(updatedCalibration, null, 2);
|
|
1868
|
+
|
|
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
|
+
]);
|
|
1890
|
+
|
|
1891
|
+
// ------------------------------------------------------------------ BEADS EXPORT + SCAFFOLD CLEANUP
|
|
1892
|
+
|
|
1893
|
+
// Export beads state so committed .beads/*.jsonl reflects all closed P1 issues.
|
|
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.
|
|
1897
|
+
await dispatch(
|
|
1898
|
+
`Persist beads state and clean sprint scaffolding from the PR diff.\n\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` +
|
|
1904
|
+
` bd export -o "${repo}/.beads/issues.jsonl"\n` +
|
|
1905
|
+
` git -C "${repo}" add .beads/issues.jsonl\n` +
|
|
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` +
|
|
1907
|
+
` (The "diff --cached --quiet || commit" pattern only commits if something actually changed.)\n\n` +
|
|
1908
|
+
`Step 3 -- Check what process files are still in the PR diff:\n` +
|
|
1909
|
+
` git -C "${repo}" diff --name-only ${base_branch}...${branch}\n\n` +
|
|
1910
|
+
`Step 4 -- For each of requirements.md, feedback.md that appears in the diff:\n` +
|
|
1911
|
+
` a) Check if the file existed on ${base_branch}:\n` +
|
|
1912
|
+
` git -C "${repo}" ls-tree --name-only ${base_branch} | grep -F <filename>\n` +
|
|
1913
|
+
` b) If NOT on base (sprint created it): git -C "${repo}" rm --force <filepath>\n` +
|
|
1914
|
+
` c) If on base (sprint modified it): git -C "${repo}" checkout ${base_branch} -- <filepath>\n` +
|
|
1915
|
+
` After handling all such files:\n` +
|
|
1916
|
+
` git -C "${repo}" add -A\n` +
|
|
1917
|
+
` git -C "${repo}" -c user.name='pm' -c user.email='pm@pm.local' commit -m "chore: drop sprint scaffolding"\n` +
|
|
1918
|
+
` (If no scaffold files remain in diff, skip the commit.)\n\n` +
|
|
1919
|
+
`Step 5 -- Verify the diff is clean:\n` +
|
|
1920
|
+
` git -C "${repo}" diff --name-only ${base_branch}...${branch}\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` +
|
|
1923
|
+
` git -C "${repo}" push origin ${branch}\n\n` +
|
|
1924
|
+
`Return "OK" when done.`,
|
|
1925
|
+
{ model: MODEL_HAIKU, label: 'beads-export-cleanup', phase: 'Harvest' }
|
|
1926
|
+
);
|
|
1927
|
+
|
|
1928
|
+
// ------------------------------------------------------------------ DOLT PUSH
|
|
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.
|
|
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(
|
|
1947
|
+
`In repo ${repo} on branch ${branch}, create a GitHub pull request targeting ${base_branch}.\n` +
|
|
1948
|
+
`Command: gh pr create --base ${base_branch} --head ${branch}\n` +
|
|
1949
|
+
`Title: summarise what was implemented across ${cycleCount} cycle(s).\n` +
|
|
1950
|
+
`Body:\n` +
|
|
1951
|
+
` - What was built (per sprint goal)\n` +
|
|
1952
|
+
` - Sprint goal: ${goal} -- ${goalMet ? 'MET' : 'NOT MET (partial delivery)'}\n` +
|
|
1953
|
+
` - Cycles run: ${cycleCount}\n` +
|
|
1954
|
+
` - Open items carried forward (if any): bd list --status=open and summarise\n` +
|
|
1955
|
+
` - Final review notes: ${(finalReview && finalReview.notes) || '(none)'}\n` +
|
|
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' } } } }
|
|
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
|
+
}
|
|
2020
|
+
|
|
2021
|
+
// ------------------------------------------------------------------ COST SUMMARY
|
|
2022
|
+
// Pure JS -- no agent call. Groups dispatchLedger by role (derived from label prefix)
|
|
2023
|
+
// and by model, prints a table, and reports total sprint cost.
|
|
2024
|
+
|
|
2025
|
+
const roleOf = label => label.replace(/-c\d.*$/, ''); // "doer-c1-i0-haiku" -> "doer"
|
|
2026
|
+
|
|
2027
|
+
const byRole = {};
|
|
2028
|
+
for (const e of dispatchLedger) {
|
|
2029
|
+
const role = roleOf(e.label);
|
|
2030
|
+
if (!byRole[role]) byRole[role] = { costUsd: 0, outTokens: 0, calls: 0 };
|
|
2031
|
+
byRole[role].costUsd += e.costUsd;
|
|
2032
|
+
byRole[role].outTokens += e.outTokens;
|
|
2033
|
+
byRole[role].calls += 1;
|
|
2034
|
+
}
|
|
2035
|
+
|
|
2036
|
+
const sprintTotal = dispatchLedger.reduce((s, e) => s + e.costUsd, 0);
|
|
2037
|
+
|
|
2038
|
+
log('\n=== Sprint cost summary (output tokens only) ===');
|
|
2039
|
+
for (const [role, s] of Object.entries(byRole).sort((a, b) => b[1].costUsd - a[1].costUsd)) {
|
|
2040
|
+
log(` ${role.padEnd(20)} $${s.costUsd.toFixed(4).padStart(8)} ${String(s.outTokens).padStart(8)} tok ${s.calls} call(s)`);
|
|
2041
|
+
}
|
|
2042
|
+
log(` ${'TOTAL'.padEnd(20)} $${sprintTotal.toFixed(4).padStart(8)}`);
|
|
2043
|
+
log(` (input token cost not included -- see ${sprintLogFile} for per-dispatch detail)`);
|
|
2044
|
+
log('================================================\n');
|
|
2045
|
+
|
|
2046
|
+
return { cycles: cycleCount, goalMet, goal, harvest: 'ok', sprintCostUsd: parseFloat(sprintTotal.toFixed(4)) };
|