cohorte 3.3.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.
- package/CHANGELOG.md +63 -0
- package/README.md +23 -3
- package/core/agents/cohorte-review.md +11 -3
- package/core/agents/implementer.template.md +19 -1
- package/core/commands/cohorte-acceptance.md +35 -0
- package/core/commands/cohorte-build.md +17 -0
- package/core/commands/cohorte-fix.md +6 -0
- package/core/commands/cohorte-foundation.md +90 -0
- package/core/commands/cohorte-lead.md +90 -0
- package/core/commands/cohorte-learn.md +67 -0
- package/core/commands/cohorte-po.md +76 -0
- package/core/commands/cohorte-product-map.md +53 -0
- package/core/commands/cohorte-retro.md +44 -13
- package/core/commands/cohorte-scaffold.md +59 -0
- package/core/commands/cohorte-vision.md +80 -0
- package/lib/install.js +2 -0
- package/lib/project.js +1 -1
- package/lib/registry.js +4 -3
- package/package.json +2 -2
- package/profile/PIPELINE.template.md +3 -1
- package/profile/schema/kanban.md +23 -0
- package/scripts/kanban-move.sh +45 -11
- package/ui/dist-server/server/agents.js +100 -0
- package/ui/dist-server/server/decider.js +241 -0
- package/ui/dist-server/server/features.js +128 -0
- package/ui/dist-server/server/flow.js +934 -0
- package/ui/dist-server/server/founding.js +706 -0
- package/ui/dist-server/server/gitinfo.js +22 -0
- package/ui/dist-server/server/history.js +10 -3
- package/ui/dist-server/server/legacy.js +3 -1
- package/ui/dist-server/server/next.js +91 -0
- package/ui/dist-server/server/runs.js +73 -11
- package/ui/dist-server/server/server.js +222 -6
- package/ui/dist-server/server/settings.js +81 -0
- package/ui/dist-server/server/tracker.js +438 -0
- package/ui/dist-server/shared/blocks.js +165 -0
- package/ui/dist-web/assets/index-BfN944Jm.js +165 -0
- package/ui/dist-web/assets/{style-0oNNMjwU.css → style-BMfadlCl.css} +1 -1
- package/ui/dist-web/index.html +2 -2
- package/ui/dist-web/assets/index-Clp1YW4S.js +0 -152
|
@@ -0,0 +1,934 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The company flow — business → developers → the code host → business — driven from cohorte ui.
|
|
3
|
+
//
|
|
4
|
+
// PO conversation ──► draft tickets ──[CP0: the requester validates]──► backlog
|
|
5
|
+
// a developer takes one ──► worktree ──► lead conversation ──► plan ──[CP1: the developer validates]──► spec frozen
|
|
6
|
+
// autopilot: build → review → (fix → review)* → verify, each stage a FRESH `claude -p` session (no /clear, ever)
|
|
7
|
+
// ├─ questions mid-run go to the decider first; what it cannot settle waits for the developer
|
|
8
|
+
// └─ [CP2: course check] opens only on a signal (next.ts)
|
|
9
|
+
// [CP3: the developer signs off] ──► /cohorte-ship opens the PR ──► the human merges (or tells cohorte ui to)
|
|
10
|
+
// merged ──► /cohorte-learn writes the journal ──► the PO writes the acceptance note ──[CP4: the requester accepts]
|
|
11
|
+
//
|
|
12
|
+
// Every agent run here is a Runs run (runs.ts) with a role; this module reacts to their ends (hooks), owns the
|
|
13
|
+
// tracker writes and the feature record, and never lets its own failure take a run down.
|
|
14
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
17
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
18
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
19
|
+
}
|
|
20
|
+
Object.defineProperty(o, k2, desc);
|
|
21
|
+
}) : (function(o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
}));
|
|
25
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
26
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
27
|
+
}) : function(o, v) {
|
|
28
|
+
o["default"] = v;
|
|
29
|
+
});
|
|
30
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
31
|
+
var ownKeys = function(o) {
|
|
32
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
33
|
+
var ar = [];
|
|
34
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
35
|
+
return ar;
|
|
36
|
+
};
|
|
37
|
+
return ownKeys(o);
|
|
38
|
+
};
|
|
39
|
+
return function (mod) {
|
|
40
|
+
if (mod && mod.__esModule) return mod;
|
|
41
|
+
var result = {};
|
|
42
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
43
|
+
__setModuleDefault(result, mod);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
})();
|
|
47
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
48
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
49
|
+
};
|
|
50
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
+
exports.Flow = exports.FlowError = exports.expandCommand = void 0;
|
|
52
|
+
const fs_1 = __importDefault(require("fs"));
|
|
53
|
+
const path_1 = __importDefault(require("path"));
|
|
54
|
+
const legacy_1 = require("./legacy");
|
|
55
|
+
const features_1 = require("./features");
|
|
56
|
+
const settings_1 = require("./settings");
|
|
57
|
+
const decider_1 = require("./decider");
|
|
58
|
+
const blocks_1 = require("../shared/blocks");
|
|
59
|
+
const tracker_1 = require("./tracker");
|
|
60
|
+
const next_1 = require("./next");
|
|
61
|
+
const pipeline_1 = require("./pipeline");
|
|
62
|
+
const findings_1 = require("./findings");
|
|
63
|
+
const gitinfo = __importStar(require("./gitinfo"));
|
|
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; } });
|
|
69
|
+
const { scanSpecs, checkTrust } = legacy_1.doctor;
|
|
70
|
+
const PR_POLL_MS = 60000;
|
|
71
|
+
class Flow {
|
|
72
|
+
globalDir;
|
|
73
|
+
runs;
|
|
74
|
+
jobs;
|
|
75
|
+
features;
|
|
76
|
+
settings;
|
|
77
|
+
prefs;
|
|
78
|
+
decider;
|
|
79
|
+
changed;
|
|
80
|
+
/** New project: the founding conversations, the skeleton and the pipeline set-up (founding.ts). */
|
|
81
|
+
founding;
|
|
82
|
+
prTimer = null;
|
|
83
|
+
/** Feature ids with an advance() in flight — so two run ends never start two stages. */
|
|
84
|
+
busy = new Set();
|
|
85
|
+
constructor(o) {
|
|
86
|
+
this.globalDir = o.globalDir;
|
|
87
|
+
this.runs = o.runs;
|
|
88
|
+
this.jobs = o.jobs;
|
|
89
|
+
this.features = new features_1.FeatureStore(path_1.default.join(o.dataDir, 'features'));
|
|
90
|
+
this.settings = new settings_1.SettingsStore(path_1.default.join(o.dataDir, 'settings'));
|
|
91
|
+
this.prefs = new decider_1.Preferences(path_1.default.join(o.dataDir, 'prefs'));
|
|
92
|
+
this.decider = o.decider || new decider_1.Decider();
|
|
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'));
|
|
95
|
+
this.runs.hooks = {
|
|
96
|
+
onEnd: (r) => setImmediate(() => { void this.onEnd(r).catch((e) => this.fail(r, e)); }),
|
|
97
|
+
onQuestion: (r, p) => this.onQuestion(r, p),
|
|
98
|
+
onAnswered: (r, p, b, a, by) => this.onAnswered(r, p, b, a, by),
|
|
99
|
+
};
|
|
100
|
+
this.recover();
|
|
101
|
+
this.founding.recover();
|
|
102
|
+
}
|
|
103
|
+
start() {
|
|
104
|
+
if (this.prTimer)
|
|
105
|
+
return;
|
|
106
|
+
this.prTimer = setInterval(() => { void this.pollPRs(); }, PR_POLL_MS);
|
|
107
|
+
this.prTimer.unref();
|
|
108
|
+
}
|
|
109
|
+
stop() { if (this.prTimer)
|
|
110
|
+
clearInterval(this.prTimer); this.prTimer = null; }
|
|
111
|
+
// -------------------------------------------------------------------------------------------------------------
|
|
112
|
+
// helpers
|
|
113
|
+
project(projectId) {
|
|
114
|
+
const p = legacy_1.registry.get(this.globalDir, projectId);
|
|
115
|
+
if (!p)
|
|
116
|
+
throw new agents_1.FlowError('unknown project', 404);
|
|
117
|
+
return p;
|
|
118
|
+
}
|
|
119
|
+
tracker(p) { return (0, tracker_1.trackerFor)(this.globalDir, p.root, p.id); }
|
|
120
|
+
fail(r, e) {
|
|
121
|
+
const id = r.meta.featureId;
|
|
122
|
+
if (!id)
|
|
123
|
+
return;
|
|
124
|
+
const f = this.features.get(r.projectId, id);
|
|
125
|
+
if (f)
|
|
126
|
+
this.features.phase(f, 'cp2', { kind: 'cp2', reason: `cohorte ui hit an error: ${String(e?.message || e)}`, openedAt: (0, agents_1.now)() });
|
|
127
|
+
this.changed(r.projectId);
|
|
128
|
+
}
|
|
129
|
+
profileOf(root) { return ops.readProfile(root); }
|
|
130
|
+
specOf(f) {
|
|
131
|
+
if (!f.worktree)
|
|
132
|
+
return null;
|
|
133
|
+
return scanSpecs(f.worktree).find((s) => s.id === f.id) || null;
|
|
134
|
+
}
|
|
135
|
+
setTicket(p, id, patch) {
|
|
136
|
+
const tr = this.tracker(p);
|
|
137
|
+
const t = tr.update(id, patch);
|
|
138
|
+
if (t && patch.status)
|
|
139
|
+
void tr.move(t);
|
|
140
|
+
}
|
|
141
|
+
ticketOf(p, id) { return this.tracker(p).get(id); }
|
|
142
|
+
// -------------------------------------------------------------------------------------------------------------
|
|
143
|
+
// PO — the business conversation (CP0)
|
|
144
|
+
startPO(projectId, body) {
|
|
145
|
+
const p = this.project(projectId);
|
|
146
|
+
const text = typeof body.text === 'string' ? body.text.trim() : '';
|
|
147
|
+
if (!text || text.length > 8000)
|
|
148
|
+
throw new agents_1.FlowError('a message of 1 to 8000 characters is required');
|
|
149
|
+
const s = this.settings.get(p.id);
|
|
150
|
+
const requester = typeof body.requester === 'string' && body.requester.trim() ? body.requester.trim().slice(0, 80) : s.requester;
|
|
151
|
+
const parent = (0, tracker_1.isTicketId)(body.parent) ? body.parent : null;
|
|
152
|
+
const prof = this.profileOf(p.root);
|
|
153
|
+
const tickets = this.tracker(p).list().filter((t) => t.status !== 'rejected').slice(0, 80);
|
|
154
|
+
const productMap = path_1.default.join(p.root, 'PRODUCT.md');
|
|
155
|
+
const parentTicket = parent ? this.ticketOf(p, parent) : null;
|
|
156
|
+
const prompt = (0, agents_1.expandCommand)(this.globalDir, 'cohorte-po', text) + (0, agents_1.contextBlock)([
|
|
157
|
+
`Project: ${prof.name || path_1.default.basename(p.root)}${prof.one_liner ? ` — ${prof.one_liner}` : ''}`,
|
|
158
|
+
`Talk in: ${prof.ui_language || 'the language the person writes in'}`,
|
|
159
|
+
`The person: ${requester || 'a member of the business team'}`,
|
|
160
|
+
`Product map: ${fs_1.default.existsSync(productMap) ? productMap : 'none yet (PRODUCT.md is absent — rely on what the person tells you, and on the tickets below)'}`,
|
|
161
|
+
'', 'Existing tickets (never duplicate one — say it exists, or propose a follow-up with "parent"):',
|
|
162
|
+
...(tickets.length ? tickets.map((t) => `- ${t.id} · ${t.status} · ${t.type} · ${t.title}`) : ['(none)']),
|
|
163
|
+
...(parentTicket ? ['', 'This conversation follows up a delivered ticket — the person is giving acceptance feedback on it:', (0, tracker_1.ticketBrief)(parentTicket),
|
|
164
|
+
...(parentTicket.acceptance ? ['', 'Its acceptance note said:', parentTicket.acceptance.summary] : [])] : []),
|
|
165
|
+
]);
|
|
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,
|
|
167
|
+
meta: { role: 'po', title: text.slice(0, 80), ticketId: parent || undefined } });
|
|
168
|
+
this.changed(p.id);
|
|
169
|
+
return run;
|
|
170
|
+
}
|
|
171
|
+
/** Upserts the draft tickets a PO turn proposed (status `validate` until the requester validates them: CP0). */
|
|
172
|
+
async onPOEnd(r) {
|
|
173
|
+
const p = this.project(r.projectId);
|
|
174
|
+
const tr = this.tracker(p);
|
|
175
|
+
const props = (0, blocks_1.proposalsOf)((0, agents_1.textsOf)(r.events));
|
|
176
|
+
const requester = this.settings.get(p.id).requester;
|
|
177
|
+
for (const prop of props) {
|
|
178
|
+
const existing = r.tickets[prop.ref] ? tr.get(r.tickets[prop.ref]) : null;
|
|
179
|
+
if (existing && !['discussion', 'validate'].includes(existing.status))
|
|
180
|
+
continue; // validated already: later revisions do not rewrite it
|
|
181
|
+
const fields = { title: prop.title, type: prop.type, need: prop.need, value: prop.value, criteria: prop.criteria, outOfScope: prop.outOfScope,
|
|
182
|
+
priority: prop.priority, size: prop.size, parent: prop.parent || r.meta.ticketId || null };
|
|
183
|
+
const t = existing ? tr.update(existing.id, fields) : tr.create(prop, { status: 'validate', requester, conversation: r.id, parent: fields.parent });
|
|
184
|
+
if (!t)
|
|
185
|
+
continue;
|
|
186
|
+
r.tickets[prop.ref] = t.id;
|
|
187
|
+
if (!existing)
|
|
188
|
+
await tr.move(t);
|
|
189
|
+
}
|
|
190
|
+
this.runs.store?.schedule(r, true);
|
|
191
|
+
}
|
|
192
|
+
conversations(projectId) {
|
|
193
|
+
return this.runs.forProject(projectId).filter((r) => r.meta.role === 'po').sort((a, b) => b.startedAt.localeCompare(a.startedAt))
|
|
194
|
+
.map((r) => ({ runId: r.id, role: 'po', title: r.meta.title || r.command, status: r.status, startedAt: r.startedAt, ticketId: r.meta.ticketId || null,
|
|
195
|
+
tickets: { ...r.tickets }, pending: [...r.pending.values()].filter((x) => !x.deciding).length }));
|
|
196
|
+
}
|
|
197
|
+
/** CP0. `validate` → backlog; `reject` → rejected. */
|
|
198
|
+
decideTicket(projectId, id, action) {
|
|
199
|
+
const p = this.project(projectId);
|
|
200
|
+
const tr = this.tracker(p);
|
|
201
|
+
const t = tr.get(id);
|
|
202
|
+
if (!t)
|
|
203
|
+
throw new agents_1.FlowError('unknown ticket', 404);
|
|
204
|
+
if (action !== 'validate' && action !== 'reject')
|
|
205
|
+
throw new agents_1.FlowError('action must be validate or reject');
|
|
206
|
+
if (!['discussion', 'validate'].includes(t.status))
|
|
207
|
+
throw new agents_1.FlowError(`this ticket is already ${t.status}`, 409);
|
|
208
|
+
const next = tr.update(id, { status: action === 'validate' ? 'backlog' : 'rejected' });
|
|
209
|
+
void tr.move(next);
|
|
210
|
+
this.changed(p.id);
|
|
211
|
+
return next;
|
|
212
|
+
}
|
|
213
|
+
/** A ticket that needs no business discussion (tech debt, an upgrade, a bug a developer found): straight to the backlog. */
|
|
214
|
+
createTicket(projectId, body) {
|
|
215
|
+
const p = this.project(projectId);
|
|
216
|
+
const title = typeof body.title === 'string' ? body.title.trim().slice(0, 200) : '';
|
|
217
|
+
if (!title)
|
|
218
|
+
throw new agents_1.FlowError('a title is required');
|
|
219
|
+
const type = body.type === 'bug' || body.type === 'feature' ? body.type : 'tech';
|
|
220
|
+
const need = typeof body.need === 'string' ? body.need.trim().slice(0, 8000) : '';
|
|
221
|
+
const criteria = Array.isArray(body.criteria) ? body.criteria.filter((x) => typeof x === 'string' && !!x.trim()).map((x) => x.trim()).slice(0, 30) : [];
|
|
222
|
+
const tr = this.tracker(p);
|
|
223
|
+
const t = tr.create({ title, type, need, criteria }, { status: 'backlog', requester: this.settings.get(p.id).requester });
|
|
224
|
+
void tr.move(t);
|
|
225
|
+
this.changed(p.id);
|
|
226
|
+
return t;
|
|
227
|
+
}
|
|
228
|
+
// -------------------------------------------------------------------------------------------------------------
|
|
229
|
+
// Lead — a developer takes a ticket (worktree, framing, CP1)
|
|
230
|
+
take(projectId, ticketId, { confirmed = false } = {}) {
|
|
231
|
+
const p = this.project(projectId);
|
|
232
|
+
const t = this.ticketOf(p, ticketId);
|
|
233
|
+
if (!t)
|
|
234
|
+
throw new agents_1.FlowError('unknown ticket', 404);
|
|
235
|
+
const existing = this.features.get(p.id, ticketId);
|
|
236
|
+
if (existing && !['abandoned', 'failed'].includes(existing.phase))
|
|
237
|
+
throw new agents_1.FlowError('this ticket is already being worked on', 409);
|
|
238
|
+
if (!['backlog', 'framing'].includes(t.status) && !existing)
|
|
239
|
+
throw new agents_1.FlowError(`only a backlog ticket can be taken (this one is ${t.status})`, 409);
|
|
240
|
+
if (!ops.FEATURE_ID.test(ticketId))
|
|
241
|
+
throw new agents_1.FlowError('the ticket id is not a valid feature id');
|
|
242
|
+
const wt = this.worktreeFor(p, ticketId);
|
|
243
|
+
const f = existing || this.features.create(p.id, ticketId, t.title);
|
|
244
|
+
f.error = null;
|
|
245
|
+
f.checkpoint = null;
|
|
246
|
+
if (wt) {
|
|
247
|
+
f.worktree = wt.path;
|
|
248
|
+
f.branch = wt.branch;
|
|
249
|
+
this.features.save(f);
|
|
250
|
+
this.startLead(p, f);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
ops.createWorktree(this.jobs, p, ticketId, { confirmed }); // throws OpError (gate ask, bad id) before anything is recorded
|
|
254
|
+
this.features.phase(f, 'provisioning');
|
|
255
|
+
}
|
|
256
|
+
this.setTicket(p, ticketId, { status: 'framing' });
|
|
257
|
+
this.changed(p.id);
|
|
258
|
+
return f;
|
|
259
|
+
}
|
|
260
|
+
worktreeFor(p, id) {
|
|
261
|
+
const prefix = this.profileOf(p.root).vcs?.feature_branch_prefix || 'feature/';
|
|
262
|
+
const wts = legacy_1.registry.worktrees(p.root).slice(1);
|
|
263
|
+
return wts.find((w) => w.branch === `${prefix}${id}`) || wts.find((w) => path_1.default.basename(w.path) === `${path_1.default.basename(p.root)}-${id}`) || null;
|
|
264
|
+
}
|
|
265
|
+
onJob(job) {
|
|
266
|
+
if (job.kind !== 'worktree-create' || !job.tag || job.status === 'running' || job.status === 'starting')
|
|
267
|
+
return;
|
|
268
|
+
const f = this.features.get(job.projectId, job.tag);
|
|
269
|
+
if (!f || f.phase !== 'provisioning')
|
|
270
|
+
return;
|
|
271
|
+
let p;
|
|
272
|
+
try {
|
|
273
|
+
p = this.project(job.projectId);
|
|
274
|
+
}
|
|
275
|
+
catch {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (job.status !== 'done') {
|
|
279
|
+
this.features.phase(f, 'failed', null, `the worktree could not be created:\n${job.lines.slice(-12).join('\n')}`);
|
|
280
|
+
this.changed(p.id);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const wt = this.worktreeFor(p, f.id);
|
|
284
|
+
if (!wt) {
|
|
285
|
+
this.features.phase(f, 'failed', null, 'the worktree job finished but no worktree was found for this feature');
|
|
286
|
+
this.changed(p.id);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
f.worktree = wt.path;
|
|
290
|
+
f.branch = wt.branch;
|
|
291
|
+
this.features.save(f);
|
|
292
|
+
try {
|
|
293
|
+
this.startLead(p, f);
|
|
294
|
+
}
|
|
295
|
+
catch (e) {
|
|
296
|
+
this.features.phase(f, 'failed', null, String(e.message || e));
|
|
297
|
+
}
|
|
298
|
+
this.changed(p.id);
|
|
299
|
+
}
|
|
300
|
+
startLead(p, f) {
|
|
301
|
+
const t = this.ticketOf(p, f.id);
|
|
302
|
+
if (!t || !f.worktree)
|
|
303
|
+
throw new agents_1.FlowError('the ticket or its worktree is gone');
|
|
304
|
+
const trust = checkTrust(f.worktree, this.globalDir);
|
|
305
|
+
f.warning = trust.status === 'warn' ? `${trust.detail} — fix: ${trust.fix}` : null;
|
|
306
|
+
const s = this.settings.get(p.id);
|
|
307
|
+
const prompt = (0, agents_1.expandCommand)(this.globalDir, 'cohorte-lead', f.id) + (0, agents_1.contextBlock)([
|
|
308
|
+
`Worktree: ${f.worktree} (branch ${f.branch || '?'}) — every file you write goes here.`,
|
|
309
|
+
`Product map: ${fs_1.default.existsSync(path_1.default.join(p.root, 'PRODUCT.md')) ? path_1.default.join(p.root, 'PRODUCT.md') : 'none'}`,
|
|
310
|
+
'', 'THE TICKET (validated by the business at CP0):', (0, tracker_1.ticketBrief)(t),
|
|
311
|
+
]);
|
|
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',
|
|
313
|
+
meta: { role: 'lead', featureId: f.id, ticketId: f.id, title: t.title } });
|
|
314
|
+
f.leadRunId = run.id;
|
|
315
|
+
this.features.phase(f, 'framing');
|
|
316
|
+
}
|
|
317
|
+
/** The developer writes to the lead (framing, or after CP1/CP2 asked to re-frame). */
|
|
318
|
+
replyLead(projectId, id, text) {
|
|
319
|
+
const p = this.project(projectId);
|
|
320
|
+
const f = this.mustFeature(p.id, id);
|
|
321
|
+
if (!['framing', 'cp1', 'cp2'].includes(f.phase))
|
|
322
|
+
throw new agents_1.FlowError(`the lead is not in a conversation (the feature is in ${f.phase})`, 409);
|
|
323
|
+
const run = f.leadRunId ? this.runs.get(f.leadRunId) : null;
|
|
324
|
+
if (!run)
|
|
325
|
+
throw new agents_1.FlowError('the lead conversation is gone', 410);
|
|
326
|
+
const err = this.runs.reply(run, text);
|
|
327
|
+
if (err)
|
|
328
|
+
throw new agents_1.FlowError(err, 409);
|
|
329
|
+
if (f.phase === 'cp2')
|
|
330
|
+
f.corrections.push({ at: (0, agents_1.now)(), checkpoint: 'cp2', text: String(text) });
|
|
331
|
+
this.features.phase(f, 'framing');
|
|
332
|
+
this.changed(p.id);
|
|
333
|
+
}
|
|
334
|
+
async onLeadEnd(r, f) {
|
|
335
|
+
const p = this.project(r.projectId);
|
|
336
|
+
const spec = this.specOf(f);
|
|
337
|
+
if (f.phase === 'freezing') {
|
|
338
|
+
if (spec && spec.status && spec.status !== 'draft') {
|
|
339
|
+
this.features.phase(f, 'autopilot');
|
|
340
|
+
this.setTicket(p, f.id, { status: 'building' });
|
|
341
|
+
await this.advance(p, f);
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
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)() });
|
|
345
|
+
}
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const plan = (0, blocks_1.lastOf)((0, agents_1.textsOf)(r.events), 'cohorte-plan', blocks_1.toPlan);
|
|
349
|
+
if (plan && r.status === 'done') {
|
|
350
|
+
f.plan = plan;
|
|
351
|
+
this.features.phase(f, 'cp1', { kind: 'cp1', reason: 'the plan is ready', openedAt: (0, agents_1.now)() });
|
|
352
|
+
}
|
|
353
|
+
else if (r.status !== 'done') {
|
|
354
|
+
this.features.phase(f, 'framing', null, `the lead run ${r.status}${r.result?.text ? `: ${r.result.text.slice(0, 300)}` : ''}`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
// -------------------------------------------------------------------------------------------------------------
|
|
358
|
+
// Checkpoints
|
|
359
|
+
checkpoint(projectId, id, body) {
|
|
360
|
+
const p = this.project(projectId);
|
|
361
|
+
const f = this.mustFeature(p.id, id);
|
|
362
|
+
const text = typeof body.text === 'string' ? body.text.trim().slice(0, 4000) : '';
|
|
363
|
+
const action = body.action;
|
|
364
|
+
const cp = f.checkpoint?.kind;
|
|
365
|
+
if (action === 'abandon') {
|
|
366
|
+
const run = f.currentRunId ? this.runs.get(f.currentRunId) : null;
|
|
367
|
+
if (run)
|
|
368
|
+
this.runs.stop(run);
|
|
369
|
+
this.features.phase(f, 'abandoned');
|
|
370
|
+
this.setTicket(p, f.id, { status: 'backlog' });
|
|
371
|
+
this.learn(p, f, 'abandoned');
|
|
372
|
+
this.changed(p.id);
|
|
373
|
+
return f;
|
|
374
|
+
}
|
|
375
|
+
if (!cp)
|
|
376
|
+
throw new agents_1.FlowError('no checkpoint is open on this feature', 409);
|
|
377
|
+
if (cp === 'cp1') {
|
|
378
|
+
if (action === 'approve') {
|
|
379
|
+
const run = f.leadRunId ? this.runs.get(f.leadRunId) : null;
|
|
380
|
+
if (!run)
|
|
381
|
+
throw new agents_1.FlowError('the lead conversation is gone', 410);
|
|
382
|
+
const err = this.runs.reply(run, [
|
|
383
|
+
'CP1 APPROVED in cohorte ui — the developer validated your plan.', text ? `They add: ${text}` : '',
|
|
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.',
|
|
385
|
+
].filter(Boolean).join('\n'));
|
|
386
|
+
if (err)
|
|
387
|
+
throw new agents_1.FlowError(err, 409);
|
|
388
|
+
this.features.phase(f, 'freezing');
|
|
389
|
+
}
|
|
390
|
+
else if (action === 'reply') {
|
|
391
|
+
this.replyLead(p.id, id, text);
|
|
392
|
+
}
|
|
393
|
+
else
|
|
394
|
+
throw new agents_1.FlowError('CP1 takes approve, reply or abandon');
|
|
395
|
+
}
|
|
396
|
+
else if (cp === 'cp2') {
|
|
397
|
+
if (action === 'continue') {
|
|
398
|
+
f.reservationsAck = true;
|
|
399
|
+
f.failures = {};
|
|
400
|
+
const last = f.stages[f.stages.length - 1];
|
|
401
|
+
if (last && last.status === 'stopped')
|
|
402
|
+
last.status = 'failed';
|
|
403
|
+
if (text)
|
|
404
|
+
f.corrections.push({ at: (0, agents_1.now)(), checkpoint: 'cp2', text });
|
|
405
|
+
this.features.phase(f, 'autopilot');
|
|
406
|
+
void this.advance(p, f);
|
|
407
|
+
}
|
|
408
|
+
else if (action === 'correct') {
|
|
409
|
+
if (!text)
|
|
410
|
+
throw new agents_1.FlowError('say what to correct');
|
|
411
|
+
f.corrections.push({ at: (0, agents_1.now)(), checkpoint: 'cp2', text });
|
|
412
|
+
f.reservationsAck = true;
|
|
413
|
+
f.failures = {};
|
|
414
|
+
this.features.phase(f, 'autopilot');
|
|
415
|
+
this.startStage(p, f, 'fix', this.settings.get(p.id).models.fix, `${f.id} ${text.replace(/\s+/g, ' ')}`, 'correction from the developer at CP2');
|
|
416
|
+
}
|
|
417
|
+
else if (action === 'reframe') {
|
|
418
|
+
if (!text)
|
|
419
|
+
throw new agents_1.FlowError('say what the lead should change');
|
|
420
|
+
const run = f.leadRunId ? this.runs.get(f.leadRunId) : null;
|
|
421
|
+
if (!run)
|
|
422
|
+
throw new agents_1.FlowError('the lead conversation is gone', 410);
|
|
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.`);
|
|
424
|
+
if (err)
|
|
425
|
+
throw new agents_1.FlowError(err, 409);
|
|
426
|
+
f.corrections.push({ at: (0, agents_1.now)(), checkpoint: 'cp2', text });
|
|
427
|
+
this.features.phase(f, 'framing');
|
|
428
|
+
}
|
|
429
|
+
else
|
|
430
|
+
throw new agents_1.FlowError('CP2 takes continue, correct, reframe or abandon');
|
|
431
|
+
}
|
|
432
|
+
else if (cp === 'cp3') {
|
|
433
|
+
if (action === 'approve') {
|
|
434
|
+
for (const d of f.decisions)
|
|
435
|
+
if (d.by === 'decider' && !d.contested && !d.confirmed) {
|
|
436
|
+
d.confirmed = true;
|
|
437
|
+
this.prefs.add(p.id, { feature: f.id, question: d.question, header: d.header, answer: d.chosen, kind: 'confirmed' });
|
|
438
|
+
}
|
|
439
|
+
this.features.save(f);
|
|
440
|
+
this.startStage(p, f, 'ship', this.settings.get(p.id).models.ship, f.id, 'approved at CP3');
|
|
441
|
+
this.features.phase(f, 'shipping');
|
|
442
|
+
}
|
|
443
|
+
else if (action === 'correct') {
|
|
444
|
+
if (!text)
|
|
445
|
+
throw new agents_1.FlowError('say what to correct');
|
|
446
|
+
f.corrections.push({ at: (0, agents_1.now)(), checkpoint: 'cp3', text });
|
|
447
|
+
this.features.phase(f, 'autopilot');
|
|
448
|
+
f.rounds = 0; // a human correction starts a fresh loop budget
|
|
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');
|
|
450
|
+
}
|
|
451
|
+
else
|
|
452
|
+
throw new agents_1.FlowError('CP3 takes approve, correct or abandon');
|
|
453
|
+
}
|
|
454
|
+
const status = ['framing', 'cp1', 'freezing'].includes(f.phase) ? 'framing' : ['cp3', 'shipping'].includes(f.phase) ? 'checkpoint' : 'building';
|
|
455
|
+
this.setTicket(p, f.id, { status });
|
|
456
|
+
this.changed(p.id);
|
|
457
|
+
return f;
|
|
458
|
+
}
|
|
459
|
+
/** "I would not have decided that": the decision is marked, the preference learned, and the note queued as a correction. */
|
|
460
|
+
contest(projectId, id, decisionId, note) {
|
|
461
|
+
const p = this.project(projectId);
|
|
462
|
+
const f = this.mustFeature(p.id, id);
|
|
463
|
+
const d = f.decisions.find((x) => x.id === decisionId);
|
|
464
|
+
if (!d)
|
|
465
|
+
throw new agents_1.FlowError('unknown decision', 404);
|
|
466
|
+
const n = typeof note === 'string' ? note.trim().slice(0, 2000) : '';
|
|
467
|
+
d.contested = { at: (0, agents_1.now)(), note: n };
|
|
468
|
+
d.confirmed = false;
|
|
469
|
+
this.prefs.add(p.id, { feature: f.id, question: d.question, header: d.header, answer: d.chosen, kind: 'contested', note: n });
|
|
470
|
+
this.features.save(f);
|
|
471
|
+
this.changed(p.id);
|
|
472
|
+
return f;
|
|
473
|
+
}
|
|
474
|
+
// -------------------------------------------------------------------------------------------------------------
|
|
475
|
+
// Autopilot
|
|
476
|
+
signals(f, spec) {
|
|
477
|
+
const wt = f.worktree;
|
|
478
|
+
const rep = (0, pipeline_1.reportsOf)(wt, f.id);
|
|
479
|
+
const sev = (rep.verdict?.severity || {});
|
|
480
|
+
return {
|
|
481
|
+
pipeline: (0, pipeline_1.pipelineOf)(wt, spec), readiness: typeof rep.readiness?.verdict === 'string' ? rep.readiness.verdict : null,
|
|
482
|
+
verdict: rep.verdict ? { verdict: typeof rep.verdict.verdict === 'string' ? rep.verdict.verdict : null, blocking: typeof rep.verdict.blocking === 'number' ? rep.verdict.blocking : null,
|
|
483
|
+
high: Number(sev.HIGH) || 0, medium: Number(sev.MEDIUM) || 0, aborted: typeof rep.verdict.aborted === 'string' ? rep.verdict.aborted : null } : null,
|
|
484
|
+
verify: typeof rep.verify?.verdict === 'string' ? rep.verify.verdict : null,
|
|
485
|
+
buildDead: Array.isArray(rep.build?.dead) && rep.build.dead.length > 0,
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
async advance(p, f) {
|
|
489
|
+
if (f.phase !== 'autopilot' || this.busy.has(f.id))
|
|
490
|
+
return;
|
|
491
|
+
if (f.currentRunId) {
|
|
492
|
+
const r = this.runs.get(f.currentRunId);
|
|
493
|
+
if (r && r.status === 'running')
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
this.busy.add(f.id);
|
|
497
|
+
try {
|
|
498
|
+
const s = this.settings.get(p.id);
|
|
499
|
+
const spec = this.specOf(f);
|
|
500
|
+
if (!spec) {
|
|
501
|
+
this.features.phase(f, 'cp2', { kind: 'cp2', reason: `no specs/${f.id}.md in the worktree`, openedAt: (0, agents_1.now)() });
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
if (!s.autopilot) {
|
|
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)() });
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
const step = (0, next_1.nextStep)(f.id, this.signals(f, spec), f, s);
|
|
509
|
+
if (step.kind === 'checkpoint') {
|
|
510
|
+
this.features.phase(f, step.cp, { kind: step.cp, reason: step.reason, openedAt: (0, agents_1.now)() });
|
|
511
|
+
this.setTicket(p, f.id, { status: step.cp === 'cp3' ? 'checkpoint' : 'building' });
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if (step.stage === 'fix')
|
|
515
|
+
f.rounds += 1;
|
|
516
|
+
this.startStage(p, f, step.stage, step.model, step.args, step.note);
|
|
517
|
+
}
|
|
518
|
+
finally {
|
|
519
|
+
this.busy.delete(f.id);
|
|
520
|
+
this.changed(p.id);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
startStage(p, f, stage, model, args, note) {
|
|
524
|
+
if (!f.worktree)
|
|
525
|
+
throw new agents_1.FlowError('the feature has no worktree');
|
|
526
|
+
const s = this.settings.get(p.id);
|
|
527
|
+
const routing = stage === 'build' || stage === 'fix' ? s.models.implementer : null;
|
|
528
|
+
let extra = (0, agents_1.AUTOPILOT_NOTE)(routing);
|
|
529
|
+
if (stage === 'ship')
|
|
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.';
|
|
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',
|
|
533
|
+
meta: { role: 'stage', featureId: f.id, ticketId: f.ticketId, stage, title: f.title } });
|
|
534
|
+
f.stages.push({ stage, runId: run.id, model, status: 'running', costUsd: null, startedAt: run.startedAt, endedAt: null, note: note || undefined });
|
|
535
|
+
f.currentRunId = run.id;
|
|
536
|
+
this.features.save(f);
|
|
537
|
+
return run;
|
|
538
|
+
}
|
|
539
|
+
async onStageEnd(r, f) {
|
|
540
|
+
const p = this.project(r.projectId);
|
|
541
|
+
const rec = f.stages.find((x) => x.runId === r.id);
|
|
542
|
+
if (rec) {
|
|
543
|
+
rec.status = r.status;
|
|
544
|
+
rec.costUsd = r.result?.costUsd ?? null;
|
|
545
|
+
rec.endedAt = r.endedAt;
|
|
546
|
+
if (r.status !== 'done' && r.result?.text)
|
|
547
|
+
rec.note = r.result.text.slice(0, 200);
|
|
548
|
+
}
|
|
549
|
+
if (f.currentRunId === r.id)
|
|
550
|
+
f.currentRunId = null;
|
|
551
|
+
const stage = r.meta.stage || '';
|
|
552
|
+
if (r.status === 'done')
|
|
553
|
+
f.failures[stage] = 0;
|
|
554
|
+
else
|
|
555
|
+
f.failures[stage] = (f.failures[stage] || 0) + 1;
|
|
556
|
+
if (stage === 'review' && r.status === 'done' && f.worktree) {
|
|
557
|
+
const v = (0, pipeline_1.reportsOf)(f.worktree, f.id).verdict;
|
|
558
|
+
if (v && typeof v.blocking === 'number')
|
|
559
|
+
f.lastBlocking = v.blocking;
|
|
560
|
+
}
|
|
561
|
+
this.features.save(f);
|
|
562
|
+
if (stage === 'ship')
|
|
563
|
+
return this.onShipEnd(p, f, r);
|
|
564
|
+
if (stage === 'learn' || r.meta.stage === 'reconcile')
|
|
565
|
+
return;
|
|
566
|
+
if (f.phase === 'autopilot')
|
|
567
|
+
await this.advance(p, f);
|
|
568
|
+
}
|
|
569
|
+
async onShipEnd(p, f, r) {
|
|
570
|
+
const spec = this.specOf(f);
|
|
571
|
+
if (!spec || !['shipped', 'merged'].includes(String(spec.status))) {
|
|
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)() });
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
const pull = f.branch && (await gitinfo.ghAvailable()) ? await gitinfo.pullFor(p.root, f.branch) : null;
|
|
576
|
+
f.pr = pull ? { number: pull.number, url: pull.url, state: pull.state, checks: pull.checks } : null;
|
|
577
|
+
this.features.phase(f, 'pr');
|
|
578
|
+
this.setTicket(p, f.id, { status: 'pr', pr: pull ? `#${pull.number}` : null });
|
|
579
|
+
if (pull && pull.state === 'MERGED')
|
|
580
|
+
await this.onMerged(p, f);
|
|
581
|
+
}
|
|
582
|
+
// -------------------------------------------------------------------------------------------------------------
|
|
583
|
+
// PR → merged → learn → acceptance (CP4)
|
|
584
|
+
merge(projectId, id) {
|
|
585
|
+
const p = this.project(projectId);
|
|
586
|
+
const f = this.mustFeature(p.id, id);
|
|
587
|
+
if (f.phase !== 'pr' || !f.pr)
|
|
588
|
+
throw new agents_1.FlowError('there is no open PR for this feature', 409);
|
|
589
|
+
if (f.pr.checks === 'failure')
|
|
590
|
+
throw new agents_1.FlowError('the PR checks are red — fix them first', 409);
|
|
591
|
+
const method = this.settings.get(p.id).mergeMethod;
|
|
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',
|
|
593
|
+
args: ['pr', 'merge', String(f.pr.number), `--${method}`] });
|
|
594
|
+
this.features.phase(f, 'merging');
|
|
595
|
+
this.changed(p.id);
|
|
596
|
+
return job;
|
|
597
|
+
}
|
|
598
|
+
onMergeJob(job) {
|
|
599
|
+
if (job.kind !== 'merge' || !job.tag || job.status === 'running' || job.status === 'starting')
|
|
600
|
+
return;
|
|
601
|
+
const f = this.features.get(job.projectId, job.tag);
|
|
602
|
+
if (!f || f.phase !== 'merging')
|
|
603
|
+
return;
|
|
604
|
+
let p;
|
|
605
|
+
try {
|
|
606
|
+
p = this.project(job.projectId);
|
|
607
|
+
}
|
|
608
|
+
catch {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
if (job.status !== 'done') {
|
|
612
|
+
this.features.phase(f, 'pr', null, `the merge failed:\n${job.lines.slice(-8).join('\n')}`);
|
|
613
|
+
this.changed(p.id);
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
void this.onMerged(p, f);
|
|
617
|
+
}
|
|
618
|
+
async pollPRs() {
|
|
619
|
+
if (!(await gitinfo.ghAvailable()))
|
|
620
|
+
return;
|
|
621
|
+
for (const p of legacy_1.registry.load(this.globalDir)) {
|
|
622
|
+
for (const f of this.features.list(p.id)) {
|
|
623
|
+
if (f.phase !== 'pr' || !f.branch)
|
|
624
|
+
continue;
|
|
625
|
+
const pull = await gitinfo.pullFor(p.root, f.branch);
|
|
626
|
+
if (!pull)
|
|
627
|
+
continue;
|
|
628
|
+
f.pr = { number: pull.number, url: pull.url, state: pull.state, checks: pull.checks };
|
|
629
|
+
this.features.save(f);
|
|
630
|
+
if (pull.state === 'MERGED')
|
|
631
|
+
await this.onMerged(p, f);
|
|
632
|
+
this.changed(p.id);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
async onMerged(p, f) {
|
|
637
|
+
if (['closing', 'acceptance', 'done'].includes(f.phase))
|
|
638
|
+
return;
|
|
639
|
+
if (f.pr)
|
|
640
|
+
f.pr.state = 'MERGED';
|
|
641
|
+
this.features.phase(f, 'closing');
|
|
642
|
+
this.learn(p, f, 'merged');
|
|
643
|
+
}
|
|
644
|
+
/** /cohorte-learn writes the journal; on a merge the acceptance note follows, then the ship reconcile. */
|
|
645
|
+
learn(p, f, why) {
|
|
646
|
+
if (!f.worktree || !fs_1.default.existsSync(f.worktree)) {
|
|
647
|
+
if (why === 'merged')
|
|
648
|
+
this.startAcceptance(p, f);
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
try {
|
|
652
|
+
this.startStage(p, f, 'learn', this.settings.get(p.id).models.learn, `${f.id} ${why}`, why);
|
|
653
|
+
}
|
|
654
|
+
catch (e) {
|
|
655
|
+
f.warning = `the journal was not written: ${String(e.message || e)}`;
|
|
656
|
+
this.features.save(f);
|
|
657
|
+
if (why === 'merged')
|
|
658
|
+
this.startAcceptance(p, f);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
startAcceptance(p, f) {
|
|
662
|
+
const t = this.ticketOf(p, f.id);
|
|
663
|
+
if (!t) {
|
|
664
|
+
this.features.phase(f, 'done');
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
const s = this.settings.get(p.id);
|
|
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)([
|
|
670
|
+
`Talk in: ${this.profileOf(p.root).ui_language || 'the language of the ticket'}`,
|
|
671
|
+
f.pr ? `Merged: PR #${f.pr.number} ${f.pr.url}` : 'Merged.',
|
|
672
|
+
'', 'THE TICKET:', (0, tracker_1.ticketBrief)(t), '', 'THE SPEC THE DEVELOPERS BUILT (technical — translate, never copy):', specText.slice(0, 30000) || '(not found)',
|
|
673
|
+
]);
|
|
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,
|
|
675
|
+
meta: { role: 'acceptance', featureId: f.id, ticketId: f.id, title: t.title } });
|
|
676
|
+
f.stages.push({ stage: 'acceptance', runId: run.id, model: s.models.acceptance, status: 'running', costUsd: null, startedAt: run.startedAt, endedAt: null });
|
|
677
|
+
this.features.save(f);
|
|
678
|
+
}
|
|
679
|
+
async onAcceptanceEnd(r, f) {
|
|
680
|
+
const p = this.project(r.projectId);
|
|
681
|
+
const rec = f.stages.find((x) => x.runId === r.id);
|
|
682
|
+
if (rec) {
|
|
683
|
+
rec.status = r.status;
|
|
684
|
+
rec.costUsd = r.result?.costUsd ?? null;
|
|
685
|
+
rec.endedAt = r.endedAt;
|
|
686
|
+
}
|
|
687
|
+
const note = (0, blocks_1.lastOf)((0, agents_1.textsOf)(r.events), 'cohorte-acceptance', blocks_1.toAcceptance);
|
|
688
|
+
if (!note) {
|
|
689
|
+
this.features.phase(f, 'acceptance', null, 'the acceptance note could not be written — the ticket waits for acceptance without it');
|
|
690
|
+
}
|
|
691
|
+
else {
|
|
692
|
+
this.setTicket(p, f.id, { status: 'acceptance', acceptance: { ...note, at: (0, agents_1.now)() } });
|
|
693
|
+
this.features.phase(f, 'acceptance');
|
|
694
|
+
}
|
|
695
|
+
// The pipeline's own close-out: spec → merged, worktree teardown proposal, report buffer cleared.
|
|
696
|
+
if (f.worktree && fs_1.default.existsSync(f.worktree)) {
|
|
697
|
+
try {
|
|
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),
|
|
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 } });
|
|
700
|
+
f.stages.push({ stage: 'reconcile', runId: run.id, model: run.model || '', status: 'running', costUsd: null, startedAt: run.startedAt, endedAt: null });
|
|
701
|
+
this.features.save(f);
|
|
702
|
+
}
|
|
703
|
+
catch { /* the reconcile is a convenience; doctor does the same */ }
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
/** CP4 — the requester's answer on the delivery. `not-quite` opens a follow-up PO conversation, seeded and linked. */
|
|
707
|
+
acceptance(projectId, ticketId, body) {
|
|
708
|
+
const p = this.project(projectId);
|
|
709
|
+
const tr = this.tracker(p);
|
|
710
|
+
const t = tr.get(ticketId);
|
|
711
|
+
if (!t)
|
|
712
|
+
throw new agents_1.FlowError('unknown ticket', 404);
|
|
713
|
+
if (t.status !== 'acceptance')
|
|
714
|
+
throw new agents_1.FlowError(`this ticket is not waiting for acceptance (it is ${t.status})`, 409);
|
|
715
|
+
const text = typeof body.text === 'string' ? body.text.trim().slice(0, 4000) : '';
|
|
716
|
+
let run = null;
|
|
717
|
+
let next;
|
|
718
|
+
if (body.action === 'accept') {
|
|
719
|
+
next = tr.update(ticketId, { status: 'done', feedback: text ? [...t.feedback, `${(0, agents_1.now)().slice(0, 10)} · accepted · ${text}`] : t.feedback });
|
|
720
|
+
}
|
|
721
|
+
else if (body.action === 'not-quite') {
|
|
722
|
+
if (!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}`] });
|
|
725
|
+
run = this.startPO(p.id, { text: `Feedback on the delivered ticket "${t.title}" (${t.id}): ${text}`, requester: t.requester, parent: t.id });
|
|
726
|
+
}
|
|
727
|
+
else
|
|
728
|
+
throw new agents_1.FlowError('action must be accept or not-quite');
|
|
729
|
+
void tr.move(next);
|
|
730
|
+
const f = this.features.get(p.id, ticketId);
|
|
731
|
+
if (f)
|
|
732
|
+
this.features.phase(f, 'done');
|
|
733
|
+
this.changed(p.id);
|
|
734
|
+
return { ticket: next, run };
|
|
735
|
+
}
|
|
736
|
+
// -------------------------------------------------------------------------------------------------------------
|
|
737
|
+
// Product map
|
|
738
|
+
startProductMap(projectId) {
|
|
739
|
+
const p = this.project(projectId);
|
|
740
|
+
if (this.runs.find(p.id, p.root))
|
|
741
|
+
throw new agents_1.FlowError('something is already running in the main checkout', 409);
|
|
742
|
+
const s = this.settings.get(p.id);
|
|
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),
|
|
744
|
+
model: s.models.productMap, permissionMode: 'acceptEdits', meta: { role: 'product-map', title: 'Product map' } });
|
|
745
|
+
}
|
|
746
|
+
// -------------------------------------------------------------------------------------------------------------
|
|
747
|
+
// Run hooks
|
|
748
|
+
async onEnd(r) {
|
|
749
|
+
if (founding_1.FOUNDING_ROLES.includes(r.meta.role)) {
|
|
750
|
+
await this.founding.onEnd(r);
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
if (r.meta.role === 'po') {
|
|
754
|
+
await this.onPOEnd(r);
|
|
755
|
+
this.changed(r.projectId);
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
const id = r.meta.featureId;
|
|
759
|
+
if (!id)
|
|
760
|
+
return;
|
|
761
|
+
const f = this.features.get(r.projectId, id);
|
|
762
|
+
if (!f)
|
|
763
|
+
return;
|
|
764
|
+
if (r.meta.role === 'lead')
|
|
765
|
+
await this.onLeadEnd(r, f);
|
|
766
|
+
else if (r.meta.role === 'acceptance')
|
|
767
|
+
await this.onAcceptanceEnd(r, f);
|
|
768
|
+
else if (r.meta.role === 'stage') {
|
|
769
|
+
await this.onStageEnd(r, f);
|
|
770
|
+
if (r.meta.stage === 'learn' && f.phase === 'closing')
|
|
771
|
+
this.startAcceptance(this.project(r.projectId), f);
|
|
772
|
+
}
|
|
773
|
+
this.changed(r.projectId);
|
|
774
|
+
}
|
|
775
|
+
/** A stage run asked a question: the decider takes it first (the ship stage and the conversations never). */
|
|
776
|
+
onQuestion(r, p) {
|
|
777
|
+
if (r.meta.role !== 'stage' || !r.meta.featureId || ['ship', 'reconcile'].includes(r.meta.stage || ''))
|
|
778
|
+
return false;
|
|
779
|
+
const f = this.features.get(r.projectId, r.meta.featureId);
|
|
780
|
+
if (!f)
|
|
781
|
+
return false;
|
|
782
|
+
void this.decide(r, p, f).catch(() => this.runs.annotate(r, p.requestId, { deciding: false }));
|
|
783
|
+
return true;
|
|
784
|
+
}
|
|
785
|
+
deciderContext(f, stage, projectId) {
|
|
786
|
+
const wt = f.worktree || '';
|
|
787
|
+
return {
|
|
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,
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
async decide(r, p, f) {
|
|
793
|
+
const s = this.settings.get(r.projectId);
|
|
794
|
+
const ctx = this.deciderContext(f, r.meta.stage || '', r.projectId);
|
|
795
|
+
const qs = p.questions || [];
|
|
796
|
+
const outcomes = await Promise.all(qs.map((q) => this.decider.decide(q, ctx, { autonomy: s.autonomy, votes: s.deciderVotes, model: s.models.decider, escalation: s.models.deciderEscalation }, r.cwd)));
|
|
797
|
+
if (!r.pending.has(p.requestId))
|
|
798
|
+
return; // answered by hand meanwhile
|
|
799
|
+
const undecided = outcomes.find((o) => !o.decided);
|
|
800
|
+
if (undecided && !undecided.decided) {
|
|
801
|
+
this.runs.annotate(r, p.requestId, { deciding: false, advice: undecided.advice });
|
|
802
|
+
this.changed(r.projectId);
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
const answers = {};
|
|
806
|
+
qs.forEach((q, i) => { const o = outcomes[i]; if (o.decided)
|
|
807
|
+
answers[q.question] = o.choice; });
|
|
808
|
+
qs.forEach((q, i) => {
|
|
809
|
+
const o = outcomes[i];
|
|
810
|
+
if (!o.decided)
|
|
811
|
+
return;
|
|
812
|
+
this.features.addDecision(f, { runId: r.id, stage: r.meta.stage || '', question: q.question, header: q.header, options: q.options.map((x) => x.label), chosen: o.choice,
|
|
813
|
+
by: 'decider', agreement: o.tally.agreement, category: o.tally.category, source: o.tally.source, rationale: `${o.tally.rationale} (${o.model}, ${Math.round(o.tally.agreement * o.tally.votes)}/${o.tally.votes})` });
|
|
814
|
+
});
|
|
815
|
+
this.runs.respond(r, p.requestId, 'allow', answers, 'decider');
|
|
816
|
+
this.changed(r.projectId);
|
|
817
|
+
}
|
|
818
|
+
/** What the developer answers by hand becomes a preference the decider reads next time. */
|
|
819
|
+
onAnswered(r, p, behavior, answers, by) {
|
|
820
|
+
if (by !== 'human' || p.kind !== 'question' || behavior !== 'allow' || !r.meta.featureId)
|
|
821
|
+
return;
|
|
822
|
+
const f = this.features.get(r.projectId, r.meta.featureId);
|
|
823
|
+
for (const q of p.questions || []) {
|
|
824
|
+
const a = answers[q.question];
|
|
825
|
+
if (!a)
|
|
826
|
+
continue;
|
|
827
|
+
this.prefs.add(r.projectId, { feature: r.meta.featureId, question: q.question, header: q.header, answer: a, kind: 'answer' });
|
|
828
|
+
if (f && r.meta.role === 'stage')
|
|
829
|
+
this.features.addDecision(f, { runId: r.id, stage: r.meta.stage || '', question: q.question, header: q.header, options: q.options.map((x) => x.label),
|
|
830
|
+
chosen: a, by: 'human', agreement: null, category: p.advice?.category || null, source: null, rationale: p.advice ? `the decider handed it over: ${p.advice.escalated}` : '' });
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
/** After a restart nothing is running: a feature that was mid-stage opens CP2 instead of guessing. */
|
|
834
|
+
recover() {
|
|
835
|
+
for (const p of legacy_1.registry.load(this.globalDir)) {
|
|
836
|
+
for (const f of this.features.list(p.id)) {
|
|
837
|
+
if (!['autopilot', 'shipping', 'closing', 'freezing'].includes(f.phase))
|
|
838
|
+
continue;
|
|
839
|
+
const r = f.currentRunId ? this.runs.get(f.currentRunId) : null;
|
|
840
|
+
if (r && r.status === 'running')
|
|
841
|
+
continue;
|
|
842
|
+
for (const s of f.stages)
|
|
843
|
+
if (s.status === 'running')
|
|
844
|
+
s.status = 'stopped';
|
|
845
|
+
f.currentRunId = null;
|
|
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)() });
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
// -------------------------------------------------------------------------------------------------------------
|
|
851
|
+
// Reads
|
|
852
|
+
mustFeature(projectId, id) {
|
|
853
|
+
const f = this.features.get(projectId, id);
|
|
854
|
+
if (!f)
|
|
855
|
+
throw new agents_1.FlowError('unknown feature', 404);
|
|
856
|
+
return f;
|
|
857
|
+
}
|
|
858
|
+
async detail(projectId, id) {
|
|
859
|
+
const p = this.project(projectId);
|
|
860
|
+
const f = this.mustFeature(p.id, id);
|
|
861
|
+
const spec = this.specOf(f);
|
|
862
|
+
const wt = f.worktree && fs_1.default.existsSync(f.worktree) ? f.worktree : null;
|
|
863
|
+
const specText = wt ? (0, agents_1.readText)(path_1.default.join(wt, 'specs', `${f.id}.md`)) : '';
|
|
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);
|
|
865
|
+
const notes = wt ? (0, agents_1.readText)(path_1.default.join(wt, 'specs', 'reports', `${f.id}.notes.md`)) : '';
|
|
866
|
+
const assumptions = [...notes.matchAll(/^\s*- (.+)$/gm)].map((m) => m[1].trim()).slice(0, 40);
|
|
867
|
+
const rr = wt ? (0, findings_1.parseReviewReport)((0, agents_1.readText)(path_1.default.join(wt, 'specs', 'reports', `${f.id}.md`))) : null;
|
|
868
|
+
let diff = null;
|
|
869
|
+
if (wt) {
|
|
870
|
+
try {
|
|
871
|
+
diff = (await gitinfo.worktreeGit(wt, this.profileOf(p.root).vcs?.default_branch || 'main')).diff;
|
|
872
|
+
}
|
|
873
|
+
catch {
|
|
874
|
+
diff = null;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
return { ...f, pipeline: wt && spec ? (0, pipeline_1.pipelineOf)(wt, spec) : null, ticket: this.ticketOf(p, f.id), review: rr ? { verdict: rr.verdict, findings: rr.findings, deferred: rr.deferred } : null, dod, assumptions, diff };
|
|
878
|
+
}
|
|
879
|
+
inbox() {
|
|
880
|
+
const out = [];
|
|
881
|
+
for (const p of legacy_1.registry.load(this.globalDir)) {
|
|
882
|
+
if (!fs_1.default.existsSync(p.root))
|
|
883
|
+
continue;
|
|
884
|
+
const name = this.profileOf(p.root).name || path_1.default.basename(p.root);
|
|
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
|
+
}
|
|
891
|
+
for (const t of this.tracker(p).list()) {
|
|
892
|
+
if (t.status === 'validate')
|
|
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 });
|
|
894
|
+
if (t.status === 'acceptance')
|
|
895
|
+
out.push({ ...base, kind: 'cp4', ref: t.id, feature: null, title: t.title, detail: 'delivered — waiting for acceptance', at: t.updated });
|
|
896
|
+
}
|
|
897
|
+
for (const f of this.features.list(p.id)) {
|
|
898
|
+
if (f.checkpoint)
|
|
899
|
+
out.push({ ...base, kind: f.checkpoint.kind, ref: f.id, feature: f.id, title: f.title, detail: f.checkpoint.reason, at: f.checkpoint.openedAt });
|
|
900
|
+
else if (f.phase === 'pr' && f.pr)
|
|
901
|
+
out.push({ ...base, kind: 'pr', ref: f.id, feature: f.id, title: f.title, detail: `PR #${f.pr.number} open${f.pr.checks ? ` · checks ${f.pr.checks}` : ''} — merge it, or tell cohorte to`, at: f.updatedAt });
|
|
902
|
+
}
|
|
903
|
+
for (const r of this.runs.forProject(p.id)) {
|
|
904
|
+
for (const x of r.pending.values()) {
|
|
905
|
+
if (x.deciding)
|
|
906
|
+
continue;
|
|
907
|
+
out.push({ ...base, kind: x.kind === 'question' ? 'question' : 'permission', ref: r.id, feature: r.meta.featureId || null, title: r.meta.title || r.command,
|
|
908
|
+
detail: x.kind === 'question' ? (x.questions || []).map((q) => q.question).join(' · ') : `${x.tool}: ${x.summary || ''}`, at: r.startedAt });
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
return out.sort((a, b) => b.at.localeCompare(a.at));
|
|
913
|
+
}
|
|
914
|
+
learning(projectId) {
|
|
915
|
+
const fs_ = this.features.list(projectId);
|
|
916
|
+
const ds = fs_.flatMap((f) => f.decisions);
|
|
917
|
+
const by = new Map();
|
|
918
|
+
for (const st of fs_.flatMap((f) => f.stages)) {
|
|
919
|
+
const k = `${st.stage}|${st.model}`;
|
|
920
|
+
const cur = by.get(k) || { stage: st.stage, model: st.model, runs: 0, ok: 0, costUsd: 0 };
|
|
921
|
+
cur.runs += 1;
|
|
922
|
+
if (st.status === 'done')
|
|
923
|
+
cur.ok += 1;
|
|
924
|
+
cur.costUsd += st.costUsd || 0;
|
|
925
|
+
by.set(k, cur);
|
|
926
|
+
}
|
|
927
|
+
return {
|
|
928
|
+
decisions: { total: ds.length, byDecider: ds.filter((d) => d.by === 'decider').length, contested: ds.filter((d) => !!d.contested).length, confirmed: ds.filter((d) => d.confirmed).length },
|
|
929
|
+
preferences: this.prefs.list(projectId, 100000).length,
|
|
930
|
+
stages: [...by.values()].map((x) => ({ ...x, costUsd: Math.round(x.costUsd * 10000) / 10000 })).sort((a, b) => a.stage.localeCompare(b.stage)),
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
exports.Flow = Flow;
|