cohorte 4.0.0 → 4.1.0

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.
@@ -48,8 +48,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
48
48
  return (mod && mod.__esModule) ? mod : { "default": mod };
49
49
  };
50
50
  Object.defineProperty(exports, "__esModule", { value: true });
51
- exports.Flow = exports.FlowError = void 0;
52
- exports.expandCommand = expandCommand;
51
+ exports.Flow = exports.FlowError = exports.expandCommand = void 0;
53
52
  const fs_1 = __importDefault(require("fs"));
54
53
  const path_1 = __importDefault(require("path"));
55
54
  const legacy_1 = require("./legacy");
@@ -63,57 +62,12 @@ const pipeline_1 = require("./pipeline");
63
62
  const findings_1 = require("./findings");
64
63
  const gitinfo = __importStar(require("./gitinfo"));
65
64
  const ops = __importStar(require("./ops"));
65
+ const founding_1 = require("./founding");
66
+ const agents_1 = require("./agents");
67
+ Object.defineProperty(exports, "FlowError", { enumerable: true, get: function () { return agents_1.FlowError; } });
68
+ Object.defineProperty(exports, "expandCommand", { enumerable: true, get: function () { return agents_1.expandCommand; } });
66
69
  const { scanSpecs, checkTrust } = legacy_1.doctor;
67
- /** The business-facing agents read; they never write, run or delegate. The server writes what they propose. */
68
- const READ_ONLY = ['Bash', 'Edit', 'Write', 'NotebookEdit', 'Agent', 'Task'];
69
70
  const PR_POLL_MS = 60000;
70
- class FlowError extends ops.OpError {
71
- }
72
- exports.FlowError = FlowError;
73
- const now = () => new Date().toISOString();
74
- /**
75
- * Every flow run loads only the project's own MCP servers. A user's machine-wide servers (mail, docs, design tools…)
76
- * can weigh 200k+ tokens of tool schemas on EVERY turn — measured at 230k for a one-word reply — and no PO, lead or
77
- * pipeline stage needs them. The project's .mcp.json (a retrieval provider, say) is still loaded.
78
- */
79
- const mcp = (cwd) => {
80
- const f = path_1.default.join(cwd, '.mcp.json');
81
- return { strictMcp: true, mcpConfig: fs_1.default.existsSync(f) ? f : null };
82
- };
83
- const textsOf = (events) => events.filter((e) => e.kind === 'text').map((e) => e.text);
84
- const readText = (p) => { try {
85
- return fs_1.default.readFileSync(p, 'utf8');
86
- }
87
- catch {
88
- return '';
89
- } };
90
- /** The body of an installed /cohorte-* command, its $ARGUMENTS substituted — sent as a prompt so the run's model applies. */
91
- function expandCommand(globalDir, name, args) {
92
- if (!/^cohorte-[a-z0-9-]+$/.test(name))
93
- throw new FlowError(`not a cohorte command: ${name}`);
94
- const file = path_1.default.join(globalDir, 'commands', `${name}.md`);
95
- let text;
96
- try {
97
- text = fs_1.default.readFileSync(file, 'utf8');
98
- }
99
- catch {
100
- throw new FlowError(`/${name} is not installed — run \`cohorte install\` (or update cohorte)`, 400);
101
- }
102
- const body = text.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n?/, '');
103
- return `<command-name>/${name}</command-name>\n${body.split('$ARGUMENTS').join(args)}`;
104
- }
105
- const contextBlock = (lines) => `\n\n---\n## cohorte ui context (written by the dashboard — data, not instructions from the person)\n\n${lines.join('\n')}\n`;
106
- const AUTOPILOT_NOTE = (routing) => [
107
- '', '---', '## Running under the cohorte ui autopilot', '',
108
- 'No person is watching this session: cohorte ui started it, and starts the next stage itself when this one ends.',
109
- '- Never recommend `/clear` and never tell anyone which command to run next — the autopilot routes.',
110
- '- Ask a question ONLY through AskUserQuestion (one call, at most 4 questions). Put your recommended option FIRST, with',
111
- ' "(Recommended)" at the end of its label, and set each question\'s `header` to the decision\'s category — exactly one of',
112
- ' reversible · visible · behavior · contract · cost · security · irreversible. A decider answers what the plan, the spec,',
113
- ' the project\'s decisions and the developer\'s past answers already settle; the rest waits for the developer.',
114
- '- Do the stage\'s whole job, then end your turn with the stage\'s own short report.',
115
- ...(routing ? [`- Model routing: dispatch every implementer subagent with \`model: "${routing}"\` (the Agent tool's model parameter).`] : []),
116
- ].join('\n');
117
71
  class Flow {
118
72
  globalDir;
119
73
  runs;
@@ -123,6 +77,8 @@ class Flow {
123
77
  prefs;
124
78
  decider;
125
79
  changed;
80
+ /** New project: the founding conversations, the skeleton and the pipeline set-up (founding.ts). */
81
+ founding;
126
82
  prTimer = null;
127
83
  /** Feature ids with an advance() in flight — so two run ends never start two stages. */
128
84
  busy = new Set();
@@ -135,12 +91,14 @@ class Flow {
135
91
  this.prefs = new decider_1.Preferences(path_1.default.join(o.dataDir, 'prefs'));
136
92
  this.decider = o.decider || new decider_1.Decider();
137
93
  this.changed = o.changed || (() => { });
94
+ this.founding = new founding_1.FoundingFlow({ globalDir: this.globalDir, runs: this.runs, jobs: this.jobs, settings: this.settings, changed: (id) => this.changed(id) }, path_1.default.join(o.dataDir, 'founding'));
138
95
  this.runs.hooks = {
139
96
  onEnd: (r) => setImmediate(() => { void this.onEnd(r).catch((e) => this.fail(r, e)); }),
140
97
  onQuestion: (r, p) => this.onQuestion(r, p),
141
98
  onAnswered: (r, p, b, a, by) => this.onAnswered(r, p, b, a, by),
142
99
  };
143
100
  this.recover();
101
+ this.founding.recover();
144
102
  }
145
103
  start() {
146
104
  if (this.prTimer)
@@ -155,7 +113,7 @@ class Flow {
155
113
  project(projectId) {
156
114
  const p = legacy_1.registry.get(this.globalDir, projectId);
157
115
  if (!p)
158
- throw new FlowError('unknown project', 404);
116
+ throw new agents_1.FlowError('unknown project', 404);
159
117
  return p;
160
118
  }
161
119
  tracker(p) { return (0, tracker_1.trackerFor)(this.globalDir, p.root, p.id); }
@@ -165,7 +123,7 @@ class Flow {
165
123
  return;
166
124
  const f = this.features.get(r.projectId, id);
167
125
  if (f)
168
- this.features.phase(f, 'cp2', { kind: 'cp2', reason: `cohorte ui hit an error: ${String(e?.message || e)}`, openedAt: now() });
126
+ this.features.phase(f, 'cp2', { kind: 'cp2', reason: `cohorte ui hit an error: ${String(e?.message || e)}`, openedAt: (0, agents_1.now)() });
169
127
  this.changed(r.projectId);
170
128
  }
171
129
  profileOf(root) { return ops.readProfile(root); }
@@ -187,7 +145,7 @@ class Flow {
187
145
  const p = this.project(projectId);
188
146
  const text = typeof body.text === 'string' ? body.text.trim() : '';
189
147
  if (!text || text.length > 8000)
190
- throw new FlowError('a message of 1 to 8000 characters is required');
148
+ throw new agents_1.FlowError('a message of 1 to 8000 characters is required');
191
149
  const s = this.settings.get(p.id);
192
150
  const requester = typeof body.requester === 'string' && body.requester.trim() ? body.requester.trim().slice(0, 80) : s.requester;
193
151
  const parent = (0, tracker_1.isTicketId)(body.parent) ? body.parent : null;
@@ -195,7 +153,7 @@ class Flow {
195
153
  const tickets = this.tracker(p).list().filter((t) => t.status !== 'rejected').slice(0, 80);
196
154
  const productMap = path_1.default.join(p.root, 'PRODUCT.md');
197
155
  const parentTicket = parent ? this.ticketOf(p, parent) : null;
198
- const prompt = expandCommand(this.globalDir, 'cohorte-po', text) + contextBlock([
156
+ const prompt = (0, agents_1.expandCommand)(this.globalDir, 'cohorte-po', text) + (0, agents_1.contextBlock)([
199
157
  `Project: ${prof.name || path_1.default.basename(p.root)}${prof.one_liner ? ` — ${prof.one_liner}` : ''}`,
200
158
  `Talk in: ${prof.ui_language || 'the language the person writes in'}`,
201
159
  `The person: ${requester || 'a member of the business team'}`,
@@ -205,7 +163,7 @@ class Flow {
205
163
  ...(parentTicket ? ['', 'This conversation follows up a delivered ticket — the person is giving acceptance feedback on it:', (0, tracker_1.ticketBrief)(parentTicket),
206
164
  ...(parentTicket.acceptance ? ['', 'Its acceptance note said:', parentTicket.acceptance.summary] : [])] : []),
207
165
  ]);
208
- const run = this.runs.start({ ...mcp(p.root), projectId: p.id, cwd: p.root, command: '/cohorte-po', prompt, model: s.models.po, disallowedTools: READ_ONLY, echo: text,
166
+ const run = this.runs.start({ ...(0, agents_1.mcp)(p.root), projectId: p.id, cwd: p.root, command: '/cohorte-po', prompt, model: s.models.po, disallowedTools: agents_1.READ_ONLY, echo: text,
209
167
  meta: { role: 'po', title: text.slice(0, 80), ticketId: parent || undefined } });
210
168
  this.changed(p.id);
211
169
  return run;
@@ -214,7 +172,7 @@ class Flow {
214
172
  async onPOEnd(r) {
215
173
  const p = this.project(r.projectId);
216
174
  const tr = this.tracker(p);
217
- const props = (0, blocks_1.proposalsOf)(textsOf(r.events));
175
+ const props = (0, blocks_1.proposalsOf)((0, agents_1.textsOf)(r.events));
218
176
  const requester = this.settings.get(p.id).requester;
219
177
  for (const prop of props) {
220
178
  const existing = r.tickets[prop.ref] ? tr.get(r.tickets[prop.ref]) : null;
@@ -242,11 +200,11 @@ class Flow {
242
200
  const tr = this.tracker(p);
243
201
  const t = tr.get(id);
244
202
  if (!t)
245
- throw new FlowError('unknown ticket', 404);
203
+ throw new agents_1.FlowError('unknown ticket', 404);
246
204
  if (action !== 'validate' && action !== 'reject')
247
- throw new FlowError('action must be validate or reject');
205
+ throw new agents_1.FlowError('action must be validate or reject');
248
206
  if (!['discussion', 'validate'].includes(t.status))
249
- throw new FlowError(`this ticket is already ${t.status}`, 409);
207
+ throw new agents_1.FlowError(`this ticket is already ${t.status}`, 409);
250
208
  const next = tr.update(id, { status: action === 'validate' ? 'backlog' : 'rejected' });
251
209
  void tr.move(next);
252
210
  this.changed(p.id);
@@ -257,7 +215,7 @@ class Flow {
257
215
  const p = this.project(projectId);
258
216
  const title = typeof body.title === 'string' ? body.title.trim().slice(0, 200) : '';
259
217
  if (!title)
260
- throw new FlowError('a title is required');
218
+ throw new agents_1.FlowError('a title is required');
261
219
  const type = body.type === 'bug' || body.type === 'feature' ? body.type : 'tech';
262
220
  const need = typeof body.need === 'string' ? body.need.trim().slice(0, 8000) : '';
263
221
  const criteria = Array.isArray(body.criteria) ? body.criteria.filter((x) => typeof x === 'string' && !!x.trim()).map((x) => x.trim()).slice(0, 30) : [];
@@ -273,14 +231,14 @@ class Flow {
273
231
  const p = this.project(projectId);
274
232
  const t = this.ticketOf(p, ticketId);
275
233
  if (!t)
276
- throw new FlowError('unknown ticket', 404);
234
+ throw new agents_1.FlowError('unknown ticket', 404);
277
235
  const existing = this.features.get(p.id, ticketId);
278
236
  if (existing && !['abandoned', 'failed'].includes(existing.phase))
279
- throw new FlowError('this ticket is already being worked on', 409);
237
+ throw new agents_1.FlowError('this ticket is already being worked on', 409);
280
238
  if (!['backlog', 'framing'].includes(t.status) && !existing)
281
- throw new FlowError(`only a backlog ticket can be taken (this one is ${t.status})`, 409);
239
+ throw new agents_1.FlowError(`only a backlog ticket can be taken (this one is ${t.status})`, 409);
282
240
  if (!ops.FEATURE_ID.test(ticketId))
283
- throw new FlowError('the ticket id is not a valid feature id');
241
+ throw new agents_1.FlowError('the ticket id is not a valid feature id');
284
242
  const wt = this.worktreeFor(p, ticketId);
285
243
  const f = existing || this.features.create(p.id, ticketId, t.title);
286
244
  f.error = null;
@@ -342,16 +300,16 @@ class Flow {
342
300
  startLead(p, f) {
343
301
  const t = this.ticketOf(p, f.id);
344
302
  if (!t || !f.worktree)
345
- throw new FlowError('the ticket or its worktree is gone');
303
+ throw new agents_1.FlowError('the ticket or its worktree is gone');
346
304
  const trust = checkTrust(f.worktree, this.globalDir);
347
305
  f.warning = trust.status === 'warn' ? `${trust.detail} — fix: ${trust.fix}` : null;
348
306
  const s = this.settings.get(p.id);
349
- const prompt = expandCommand(this.globalDir, 'cohorte-lead', f.id) + contextBlock([
307
+ const prompt = (0, agents_1.expandCommand)(this.globalDir, 'cohorte-lead', f.id) + (0, agents_1.contextBlock)([
350
308
  `Worktree: ${f.worktree} (branch ${f.branch || '?'}) — every file you write goes here.`,
351
309
  `Product map: ${fs_1.default.existsSync(path_1.default.join(p.root, 'PRODUCT.md')) ? path_1.default.join(p.root, 'PRODUCT.md') : 'none'}`,
352
310
  '', 'THE TICKET (validated by the business at CP0):', (0, tracker_1.ticketBrief)(t),
353
311
  ]);
354
- const run = this.runs.start({ ...mcp(f.worktree), projectId: p.id, cwd: f.worktree, command: `/cohorte-lead ${f.id}`, prompt, model: s.models.lead, permissionMode: 'acceptEdits',
312
+ const run = this.runs.start({ ...(0, agents_1.mcp)(f.worktree), projectId: p.id, cwd: f.worktree, command: `/cohorte-lead ${f.id}`, prompt, model: s.models.lead, permissionMode: 'acceptEdits',
355
313
  meta: { role: 'lead', featureId: f.id, ticketId: f.id, title: t.title } });
356
314
  f.leadRunId = run.id;
357
315
  this.features.phase(f, 'framing');
@@ -361,15 +319,15 @@ class Flow {
361
319
  const p = this.project(projectId);
362
320
  const f = this.mustFeature(p.id, id);
363
321
  if (!['framing', 'cp1', 'cp2'].includes(f.phase))
364
- throw new FlowError(`the lead is not in a conversation (the feature is in ${f.phase})`, 409);
322
+ throw new agents_1.FlowError(`the lead is not in a conversation (the feature is in ${f.phase})`, 409);
365
323
  const run = f.leadRunId ? this.runs.get(f.leadRunId) : null;
366
324
  if (!run)
367
- throw new FlowError('the lead conversation is gone', 410);
325
+ throw new agents_1.FlowError('the lead conversation is gone', 410);
368
326
  const err = this.runs.reply(run, text);
369
327
  if (err)
370
- throw new FlowError(err, 409);
328
+ throw new agents_1.FlowError(err, 409);
371
329
  if (f.phase === 'cp2')
372
- f.corrections.push({ at: now(), checkpoint: 'cp2', text: String(text) });
330
+ f.corrections.push({ at: (0, agents_1.now)(), checkpoint: 'cp2', text: String(text) });
373
331
  this.features.phase(f, 'framing');
374
332
  this.changed(p.id);
375
333
  }
@@ -383,14 +341,14 @@ class Flow {
383
341
  await this.advance(p, f);
384
342
  }
385
343
  else {
386
- this.features.phase(f, 'cp1', { kind: 'cp1', reason: 'the lead did not freeze the spec — answer it, or approve again', openedAt: now() });
344
+ this.features.phase(f, 'cp1', { kind: 'cp1', reason: 'the lead did not freeze the spec — answer it, or approve again', openedAt: (0, agents_1.now)() });
387
345
  }
388
346
  return;
389
347
  }
390
- const plan = (0, blocks_1.lastOf)(textsOf(r.events), 'cohorte-plan', blocks_1.toPlan);
348
+ const plan = (0, blocks_1.lastOf)((0, agents_1.textsOf)(r.events), 'cohorte-plan', blocks_1.toPlan);
391
349
  if (plan && r.status === 'done') {
392
350
  f.plan = plan;
393
- this.features.phase(f, 'cp1', { kind: 'cp1', reason: 'the plan is ready', openedAt: now() });
351
+ this.features.phase(f, 'cp1', { kind: 'cp1', reason: 'the plan is ready', openedAt: (0, agents_1.now)() });
394
352
  }
395
353
  else if (r.status !== 'done') {
396
354
  this.features.phase(f, 'framing', null, `the lead run ${r.status}${r.result?.text ? `: ${r.result.text.slice(0, 300)}` : ''}`);
@@ -415,25 +373,25 @@ class Flow {
415
373
  return f;
416
374
  }
417
375
  if (!cp)
418
- throw new FlowError('no checkpoint is open on this feature', 409);
376
+ throw new agents_1.FlowError('no checkpoint is open on this feature', 409);
419
377
  if (cp === 'cp1') {
420
378
  if (action === 'approve') {
421
379
  const run = f.leadRunId ? this.runs.get(f.leadRunId) : null;
422
380
  if (!run)
423
- throw new FlowError('the lead conversation is gone', 410);
381
+ throw new agents_1.FlowError('the lead conversation is gone', 410);
424
382
  const err = this.runs.reply(run, [
425
383
  'CP1 APPROVED in cohorte ui — the developer validated your plan.', text ? `They add: ${text}` : '',
426
384
  'Freeze the spec now (status: frozen, decisions journal, design brief if any), exactly as your instructions say for the freeze, then end your turn with one line.',
427
385
  ].filter(Boolean).join('\n'));
428
386
  if (err)
429
- throw new FlowError(err, 409);
387
+ throw new agents_1.FlowError(err, 409);
430
388
  this.features.phase(f, 'freezing');
431
389
  }
432
390
  else if (action === 'reply') {
433
391
  this.replyLead(p.id, id, text);
434
392
  }
435
393
  else
436
- throw new FlowError('CP1 takes approve, reply or abandon');
394
+ throw new agents_1.FlowError('CP1 takes approve, reply or abandon');
437
395
  }
438
396
  else if (cp === 'cp2') {
439
397
  if (action === 'continue') {
@@ -443,14 +401,14 @@ class Flow {
443
401
  if (last && last.status === 'stopped')
444
402
  last.status = 'failed';
445
403
  if (text)
446
- f.corrections.push({ at: now(), checkpoint: 'cp2', text });
404
+ f.corrections.push({ at: (0, agents_1.now)(), checkpoint: 'cp2', text });
447
405
  this.features.phase(f, 'autopilot');
448
406
  void this.advance(p, f);
449
407
  }
450
408
  else if (action === 'correct') {
451
409
  if (!text)
452
- throw new FlowError('say what to correct');
453
- f.corrections.push({ at: now(), checkpoint: 'cp2', text });
410
+ throw new agents_1.FlowError('say what to correct');
411
+ f.corrections.push({ at: (0, agents_1.now)(), checkpoint: 'cp2', text });
454
412
  f.reservationsAck = true;
455
413
  f.failures = {};
456
414
  this.features.phase(f, 'autopilot');
@@ -458,18 +416,18 @@ class Flow {
458
416
  }
459
417
  else if (action === 'reframe') {
460
418
  if (!text)
461
- throw new FlowError('say what the lead should change');
419
+ throw new agents_1.FlowError('say what the lead should change');
462
420
  const run = f.leadRunId ? this.runs.get(f.leadRunId) : null;
463
421
  if (!run)
464
- throw new FlowError('the lead conversation is gone', 410);
422
+ throw new agents_1.FlowError('the lead conversation is gone', 410);
465
423
  const err = this.runs.reply(run, `The autopilot paused at CP2: ${f.checkpoint?.reason}.\nThe developer asks you to re-frame: ${text}\nUpdate the spec (it may be frozen: edit it in place, keep it frozen) and post a new cohorte-plan block.`);
466
424
  if (err)
467
- throw new FlowError(err, 409);
468
- f.corrections.push({ at: now(), checkpoint: 'cp2', text });
425
+ throw new agents_1.FlowError(err, 409);
426
+ f.corrections.push({ at: (0, agents_1.now)(), checkpoint: 'cp2', text });
469
427
  this.features.phase(f, 'framing');
470
428
  }
471
429
  else
472
- throw new FlowError('CP2 takes continue, correct, reframe or abandon');
430
+ throw new agents_1.FlowError('CP2 takes continue, correct, reframe or abandon');
473
431
  }
474
432
  else if (cp === 'cp3') {
475
433
  if (action === 'approve') {
@@ -484,14 +442,14 @@ class Flow {
484
442
  }
485
443
  else if (action === 'correct') {
486
444
  if (!text)
487
- throw new FlowError('say what to correct');
488
- f.corrections.push({ at: now(), checkpoint: 'cp3', text });
445
+ throw new agents_1.FlowError('say what to correct');
446
+ f.corrections.push({ at: (0, agents_1.now)(), checkpoint: 'cp3', text });
489
447
  this.features.phase(f, 'autopilot');
490
448
  f.rounds = 0; // a human correction starts a fresh loop budget
491
449
  this.startStage(p, f, 'fix', this.settings.get(p.id).models.fix, `${f.id} ${text.replace(/\s+/g, ' ')}`, 'correction from the developer at CP3');
492
450
  }
493
451
  else
494
- throw new FlowError('CP3 takes approve, correct or abandon');
452
+ throw new agents_1.FlowError('CP3 takes approve, correct or abandon');
495
453
  }
496
454
  const status = ['framing', 'cp1', 'freezing'].includes(f.phase) ? 'framing' : ['cp3', 'shipping'].includes(f.phase) ? 'checkpoint' : 'building';
497
455
  this.setTicket(p, f.id, { status });
@@ -504,9 +462,9 @@ class Flow {
504
462
  const f = this.mustFeature(p.id, id);
505
463
  const d = f.decisions.find((x) => x.id === decisionId);
506
464
  if (!d)
507
- throw new FlowError('unknown decision', 404);
465
+ throw new agents_1.FlowError('unknown decision', 404);
508
466
  const n = typeof note === 'string' ? note.trim().slice(0, 2000) : '';
509
- d.contested = { at: now(), note: n };
467
+ d.contested = { at: (0, agents_1.now)(), note: n };
510
468
  d.confirmed = false;
511
469
  this.prefs.add(p.id, { feature: f.id, question: d.question, header: d.header, answer: d.chosen, kind: 'contested', note: n });
512
470
  this.features.save(f);
@@ -540,16 +498,16 @@ class Flow {
540
498
  const s = this.settings.get(p.id);
541
499
  const spec = this.specOf(f);
542
500
  if (!spec) {
543
- this.features.phase(f, 'cp2', { kind: 'cp2', reason: `no specs/${f.id}.md in the worktree`, openedAt: now() });
501
+ this.features.phase(f, 'cp2', { kind: 'cp2', reason: `no specs/${f.id}.md in the worktree`, openedAt: (0, agents_1.now)() });
544
502
  return;
545
503
  }
546
504
  if (!s.autopilot) {
547
- this.features.phase(f, 'cp2', { kind: 'cp2', reason: 'the autopilot is off for this project — continue to run the next stage', openedAt: now() });
505
+ this.features.phase(f, 'cp2', { kind: 'cp2', reason: 'the autopilot is off for this project — continue to run the next stage', openedAt: (0, agents_1.now)() });
548
506
  return;
549
507
  }
550
508
  const step = (0, next_1.nextStep)(f.id, this.signals(f, spec), f, s);
551
509
  if (step.kind === 'checkpoint') {
552
- this.features.phase(f, step.cp, { kind: step.cp, reason: step.reason, openedAt: now() });
510
+ this.features.phase(f, step.cp, { kind: step.cp, reason: step.reason, openedAt: (0, agents_1.now)() });
553
511
  this.setTicket(p, f.id, { status: step.cp === 'cp3' ? 'checkpoint' : 'building' });
554
512
  return;
555
513
  }
@@ -564,14 +522,14 @@ class Flow {
564
522
  }
565
523
  startStage(p, f, stage, model, args, note) {
566
524
  if (!f.worktree)
567
- throw new FlowError('the feature has no worktree');
525
+ throw new agents_1.FlowError('the feature has no worktree');
568
526
  const s = this.settings.get(p.id);
569
527
  const routing = stage === 'build' || stage === 'fix' ? s.models.implementer : null;
570
- let extra = AUTOPILOT_NOTE(routing);
528
+ let extra = (0, agents_1.AUTOPILOT_NOTE)(routing);
571
529
  if (stage === 'ship')
572
530
  extra += '\n- The developer APPROVED shipping at CP3 in cohorte ui: that is the confirmation your §1 asks for — do not ask it again. Every OTHER question (ship anyway on open DoD items, not verified at runtime, the release level) still goes through AskUserQuestion.';
573
- const prompt = expandCommand(this.globalDir, `cohorte-${stage}`, args) + extra;
574
- const run = this.runs.start({ ...mcp(f.worktree), projectId: p.id, cwd: f.worktree, command: `/cohorte-${stage} ${args.split(' ')[0]}`, prompt, model, permissionMode: 'acceptEdits',
531
+ const prompt = (0, agents_1.expandCommand)(this.globalDir, `cohorte-${stage}`, args) + extra;
532
+ const run = this.runs.start({ ...(0, agents_1.mcp)(f.worktree), projectId: p.id, cwd: f.worktree, command: `/cohorte-${stage} ${args.split(' ')[0]}`, prompt, model, permissionMode: 'acceptEdits',
575
533
  meta: { role: 'stage', featureId: f.id, ticketId: f.ticketId, stage, title: f.title } });
576
534
  f.stages.push({ stage, runId: run.id, model, status: 'running', costUsd: null, startedAt: run.startedAt, endedAt: null, note: note || undefined });
577
535
  f.currentRunId = run.id;
@@ -611,7 +569,7 @@ class Flow {
611
569
  async onShipEnd(p, f, r) {
612
570
  const spec = this.specOf(f);
613
571
  if (!spec || !['shipped', 'merged'].includes(String(spec.status))) {
614
- this.features.phase(f, 'cp3', { kind: 'cp3', reason: `the ship run ended without opening the PR${r.result?.text ? `: ${r.result.text.slice(0, 300)}` : ''}`, openedAt: now() });
572
+ this.features.phase(f, 'cp3', { kind: 'cp3', reason: `the ship run ended without opening the PR${r.result?.text ? `: ${r.result.text.slice(0, 300)}` : ''}`, openedAt: (0, agents_1.now)() });
615
573
  return;
616
574
  }
617
575
  const pull = f.branch && (await gitinfo.ghAvailable()) ? await gitinfo.pullFor(p.root, f.branch) : null;
@@ -627,9 +585,9 @@ class Flow {
627
585
  const p = this.project(projectId);
628
586
  const f = this.mustFeature(p.id, id);
629
587
  if (f.phase !== 'pr' || !f.pr)
630
- throw new FlowError('there is no open PR for this feature', 409);
588
+ throw new agents_1.FlowError('there is no open PR for this feature', 409);
631
589
  if (f.pr.checks === 'failure')
632
- throw new FlowError('the PR checks are red — fix them first', 409);
590
+ throw new agents_1.FlowError('the PR checks are red — fix them first', 409);
633
591
  const method = this.settings.get(p.id).mergeMethod;
634
592
  const job = this.jobs.start({ projectId: p.id, kind: 'merge', tag: f.id, label: `merge · PR #${f.pr.number}`, cwd: p.root, file: process.env.COHORTE_GH_BIN || 'gh',
635
593
  args: ['pr', 'merge', String(f.pr.number), `--${method}`] });
@@ -707,13 +665,13 @@ class Flow {
707
665
  return;
708
666
  }
709
667
  const s = this.settings.get(p.id);
710
- const specText = f.worktree ? readText(path_1.default.join(f.worktree, 'specs', `${f.id}.md`)) : readText(path_1.default.join(p.root, 'specs', `${f.id}.md`));
711
- const prompt = expandCommand(this.globalDir, 'cohorte-acceptance', f.id) + contextBlock([
668
+ const specText = f.worktree ? (0, agents_1.readText)(path_1.default.join(f.worktree, 'specs', `${f.id}.md`)) : (0, agents_1.readText)(path_1.default.join(p.root, 'specs', `${f.id}.md`));
669
+ const prompt = (0, agents_1.expandCommand)(this.globalDir, 'cohorte-acceptance', f.id) + (0, agents_1.contextBlock)([
712
670
  `Talk in: ${this.profileOf(p.root).ui_language || 'the language of the ticket'}`,
713
671
  f.pr ? `Merged: PR #${f.pr.number} ${f.pr.url}` : 'Merged.',
714
672
  '', 'THE TICKET:', (0, tracker_1.ticketBrief)(t), '', 'THE SPEC THE DEVELOPERS BUILT (technical — translate, never copy):', specText.slice(0, 30000) || '(not found)',
715
673
  ]);
716
- const run = this.runs.start({ ...mcp(p.root), projectId: p.id, cwd: p.root, command: `/cohorte-acceptance ${f.id}`, prompt, model: s.models.acceptance, disallowedTools: READ_ONLY,
674
+ const run = this.runs.start({ ...(0, agents_1.mcp)(p.root), projectId: p.id, cwd: p.root, command: `/cohorte-acceptance ${f.id}`, prompt, model: s.models.acceptance, disallowedTools: agents_1.READ_ONLY,
717
675
  meta: { role: 'acceptance', featureId: f.id, ticketId: f.id, title: t.title } });
718
676
  f.stages.push({ stage: 'acceptance', runId: run.id, model: s.models.acceptance, status: 'running', costUsd: null, startedAt: run.startedAt, endedAt: null });
719
677
  this.features.save(f);
@@ -726,18 +684,18 @@ class Flow {
726
684
  rec.costUsd = r.result?.costUsd ?? null;
727
685
  rec.endedAt = r.endedAt;
728
686
  }
729
- const note = (0, blocks_1.lastOf)(textsOf(r.events), 'cohorte-acceptance', blocks_1.toAcceptance);
687
+ const note = (0, blocks_1.lastOf)((0, agents_1.textsOf)(r.events), 'cohorte-acceptance', blocks_1.toAcceptance);
730
688
  if (!note) {
731
689
  this.features.phase(f, 'acceptance', null, 'the acceptance note could not be written — the ticket waits for acceptance without it');
732
690
  }
733
691
  else {
734
- this.setTicket(p, f.id, { status: 'acceptance', acceptance: { ...note, at: now() } });
692
+ this.setTicket(p, f.id, { status: 'acceptance', acceptance: { ...note, at: (0, agents_1.now)() } });
735
693
  this.features.phase(f, 'acceptance');
736
694
  }
737
695
  // The pipeline's own close-out: spec → merged, worktree teardown proposal, report buffer cleared.
738
696
  if (f.worktree && fs_1.default.existsSync(f.worktree)) {
739
697
  try {
740
- const run = this.runs.start({ ...mcp(f.worktree), projectId: p.id, cwd: f.worktree, command: `/cohorte-ship ${f.id}`, prompt: expandCommand(this.globalDir, 'cohorte-ship', f.id) + AUTOPILOT_NOTE(null),
698
+ const run = this.runs.start({ ...(0, agents_1.mcp)(f.worktree), projectId: p.id, cwd: f.worktree, command: `/cohorte-ship ${f.id}`, prompt: (0, agents_1.expandCommand)(this.globalDir, 'cohorte-ship', f.id) + (0, agents_1.AUTOPILOT_NOTE)(null),
741
699
  model: this.settings.get(p.id).models.ship, permissionMode: 'acceptEdits', meta: { role: 'stage', featureId: f.id, ticketId: f.id, stage: 'reconcile', title: f.title } });
742
700
  f.stages.push({ stage: 'reconcile', runId: run.id, model: run.model || '', status: 'running', costUsd: null, startedAt: run.startedAt, endedAt: null });
743
701
  this.features.save(f);
@@ -751,23 +709,23 @@ class Flow {
751
709
  const tr = this.tracker(p);
752
710
  const t = tr.get(ticketId);
753
711
  if (!t)
754
- throw new FlowError('unknown ticket', 404);
712
+ throw new agents_1.FlowError('unknown ticket', 404);
755
713
  if (t.status !== 'acceptance')
756
- throw new FlowError(`this ticket is not waiting for acceptance (it is ${t.status})`, 409);
714
+ throw new agents_1.FlowError(`this ticket is not waiting for acceptance (it is ${t.status})`, 409);
757
715
  const text = typeof body.text === 'string' ? body.text.trim().slice(0, 4000) : '';
758
716
  let run = null;
759
717
  let next;
760
718
  if (body.action === 'accept') {
761
- next = tr.update(ticketId, { status: 'done', feedback: text ? [...t.feedback, `${now().slice(0, 10)} · accepted · ${text}`] : t.feedback });
719
+ next = tr.update(ticketId, { status: 'done', feedback: text ? [...t.feedback, `${(0, agents_1.now)().slice(0, 10)} · accepted · ${text}`] : t.feedback });
762
720
  }
763
721
  else if (body.action === 'not-quite') {
764
722
  if (!text)
765
- throw new FlowError('say what is not quite right');
766
- next = tr.update(ticketId, { status: 'done', feedback: [...t.feedback, `${now().slice(0, 10)} · not quite · ${text}`] });
723
+ throw new agents_1.FlowError('say what is not quite right');
724
+ next = tr.update(ticketId, { status: 'done', feedback: [...t.feedback, `${(0, agents_1.now)().slice(0, 10)} · not quite · ${text}`] });
767
725
  run = this.startPO(p.id, { text: `Feedback on the delivered ticket "${t.title}" (${t.id}): ${text}`, requester: t.requester, parent: t.id });
768
726
  }
769
727
  else
770
- throw new FlowError('action must be accept or not-quite');
728
+ throw new agents_1.FlowError('action must be accept or not-quite');
771
729
  void tr.move(next);
772
730
  const f = this.features.get(p.id, ticketId);
773
731
  if (f)
@@ -780,14 +738,18 @@ class Flow {
780
738
  startProductMap(projectId) {
781
739
  const p = this.project(projectId);
782
740
  if (this.runs.find(p.id, p.root))
783
- throw new FlowError('something is already running in the main checkout', 409);
741
+ throw new agents_1.FlowError('something is already running in the main checkout', 409);
784
742
  const s = this.settings.get(p.id);
785
- return this.runs.start({ ...mcp(p.root), projectId: p.id, cwd: p.root, command: '/cohorte-product-map', prompt: expandCommand(this.globalDir, 'cohorte-product-map', '') + AUTOPILOT_NOTE(null),
743
+ return this.runs.start({ ...(0, agents_1.mcp)(p.root), projectId: p.id, cwd: p.root, command: '/cohorte-product-map', prompt: (0, agents_1.expandCommand)(this.globalDir, 'cohorte-product-map', '') + (0, agents_1.AUTOPILOT_NOTE)(null),
786
744
  model: s.models.productMap, permissionMode: 'acceptEdits', meta: { role: 'product-map', title: 'Product map' } });
787
745
  }
788
746
  // -------------------------------------------------------------------------------------------------------------
789
747
  // Run hooks
790
748
  async onEnd(r) {
749
+ if (founding_1.FOUNDING_ROLES.includes(r.meta.role)) {
750
+ await this.founding.onEnd(r);
751
+ return;
752
+ }
791
753
  if (r.meta.role === 'po') {
792
754
  await this.onPOEnd(r);
793
755
  this.changed(r.projectId);
@@ -823,8 +785,8 @@ class Flow {
823
785
  deciderContext(f, stage, projectId) {
824
786
  const wt = f.worktree || '';
825
787
  return {
826
- plan: f.plan, spec: readText(path_1.default.join(wt, 'specs', `${f.id}.md`)), decisions: readText(path_1.default.join(wt, 'specs', '_decisions.md')),
827
- conventions: (0, decider_1.conventionsOf)(readText(path_1.default.join(wt, 'PIPELINE.md'))), preferences: this.prefs.list(projectId), stage, feature: f.id,
788
+ plan: f.plan, spec: (0, agents_1.readText)(path_1.default.join(wt, 'specs', `${f.id}.md`)), decisions: (0, agents_1.readText)(path_1.default.join(wt, 'specs', '_decisions.md')),
789
+ conventions: (0, decider_1.conventionsOf)((0, agents_1.readText)(path_1.default.join(wt, 'PIPELINE.md'))), preferences: this.prefs.list(projectId), stage, feature: f.id,
828
790
  };
829
791
  }
830
792
  async decide(r, p, f) {
@@ -881,7 +843,7 @@ class Flow {
881
843
  if (s.status === 'running')
882
844
  s.status = 'stopped';
883
845
  f.currentRunId = null;
884
- this.features.phase(f, 'cp2', { kind: 'cp2', reason: 'cohorte ui restarted while this feature was running — continue to resume', openedAt: now() });
846
+ this.features.phase(f, 'cp2', { kind: 'cp2', reason: 'cohorte ui restarted while this feature was running — continue to resume', openedAt: (0, agents_1.now)() });
885
847
  }
886
848
  }
887
849
  }
@@ -890,7 +852,7 @@ class Flow {
890
852
  mustFeature(projectId, id) {
891
853
  const f = this.features.get(projectId, id);
892
854
  if (!f)
893
- throw new FlowError('unknown feature', 404);
855
+ throw new agents_1.FlowError('unknown feature', 404);
894
856
  return f;
895
857
  }
896
858
  async detail(projectId, id) {
@@ -898,11 +860,11 @@ class Flow {
898
860
  const f = this.mustFeature(p.id, id);
899
861
  const spec = this.specOf(f);
900
862
  const wt = f.worktree && fs_1.default.existsSync(f.worktree) ? f.worktree : null;
901
- const specText = wt ? readText(path_1.default.join(wt, 'specs', `${f.id}.md`)) : '';
863
+ const specText = wt ? (0, agents_1.readText)(path_1.default.join(wt, 'specs', `${f.id}.md`)) : '';
902
864
  const dod = [...specText.matchAll(/^\s*- \[( |x|X)\] (.+)$/gm)].filter((m) => !/·\s*(CRITICAL|HIGH|MEDIUM|LOW)\s*·/.test(m[2])).map((m) => ({ done: m[1] !== ' ', text: m[2].trim() })).slice(0, 60);
903
- const notes = wt ? readText(path_1.default.join(wt, 'specs', 'reports', `${f.id}.notes.md`)) : '';
865
+ const notes = wt ? (0, agents_1.readText)(path_1.default.join(wt, 'specs', 'reports', `${f.id}.notes.md`)) : '';
904
866
  const assumptions = [...notes.matchAll(/^\s*- (.+)$/gm)].map((m) => m[1].trim()).slice(0, 40);
905
- const rr = wt ? (0, findings_1.parseReviewReport)(readText(path_1.default.join(wt, 'specs', 'reports', `${f.id}.md`))) : null;
867
+ const rr = wt ? (0, findings_1.parseReviewReport)((0, agents_1.readText)(path_1.default.join(wt, 'specs', 'reports', `${f.id}.md`))) : null;
906
868
  let diff = null;
907
869
  if (wt) {
908
870
  try {
@@ -921,6 +883,11 @@ class Flow {
921
883
  continue;
922
884
  const name = this.profileOf(p.root).name || path_1.default.basename(p.root);
923
885
  const base = { projectId: p.id, projectName: name };
886
+ const fd = this.founding.store.get(p.id);
887
+ if (fd && ['cp-vision', 'cp-foundation', 'cp-scaffold'].includes(fd.phase)) {
888
+ const what = fd.phase === 'cp-vision' ? 'the vision is ready to validate' : fd.phase === 'cp-foundation' ? 'the foundations plan is ready to approve' : fd.error || 'the skeleton needs you';
889
+ out.push({ ...base, kind: 'founding', ref: p.id, feature: null, title: `New project · ${fd.name}`, detail: what, at: fd.updatedAt });
890
+ }
924
891
  for (const t of this.tracker(p).list()) {
925
892
  if (t.status === 'validate')
926
893
  out.push({ ...base, kind: 'cp0', ref: t.id, feature: null, title: t.title, detail: `ticket to validate${t.requester ? ` · ${t.requester}` : ''}`, at: t.updated });