cohorte 3.0.0-dev.8 → 3.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 +1637 -0
- package/README.md +322 -62
- package/bin/cli.js +213 -0
- package/bin/report.js +141 -0
- package/core/agents/cohorte-profile-reader.md +40 -0
- package/core/agents/cohorte-release.md +61 -0
- package/core/agents/cohorte-review.md +200 -0
- package/core/agents/implementer.template.md +154 -0
- package/core/commands/cohorte-align-ds.md +33 -0
- package/core/commands/cohorte-audit.md +74 -0
- package/core/commands/cohorte-brainstorm.md +73 -0
- package/core/commands/cohorte-build.md +248 -0
- package/core/commands/cohorte-doctor.md +135 -0
- package/core/commands/cohorte-fix.md +80 -0
- package/core/commands/cohorte-fleet.md +111 -0
- package/core/commands/cohorte-init-pipeline.md +30 -0
- package/core/commands/cohorte-intake.md +92 -0
- package/core/commands/cohorte-patch.md +118 -0
- package/core/commands/cohorte-refactor.md +122 -0
- package/core/commands/cohorte-retro.md +85 -0
- package/core/commands/cohorte-revert.md +104 -0
- package/core/commands/cohorte-review.md +239 -0
- package/core/commands/cohorte-ship.md +167 -0
- package/core/commands/cohorte-spec.md +114 -0
- package/core/commands/cohorte-update-pipeline.md +119 -0
- package/core/commands/cohorte-verify.md +106 -0
- package/core/hooks/gate.py +3236 -0
- package/core/templates/agent-handoff.md +34 -0
- package/core/templates/brainstorm-return.md +42 -0
- package/core/templates/decisions.template.md +42 -0
- package/core/templates/design-brief.md +37 -0
- package/core/templates/patch.template.md +95 -0
- package/core/templates/pr-body.md +25 -0
- package/core/templates/review-feedback.md +46 -0
- package/core/templates/spec.template.md +109 -0
- package/core/templates/steps/init-pipeline/01-detect-stack.md +53 -0
- package/core/templates/steps/init-pipeline/02-interview-gaps.md +78 -0
- package/core/templates/steps/init-pipeline/03-draft-profile.md +10 -0
- package/core/templates/steps/init-pipeline/04-write-render.md +95 -0
- package/core/templates/steps/init-pipeline/05-report.md +11 -0
- package/core/templates/steps/review-pr/01-pr-mode.md +54 -0
- package/core/workflows/audit.js +311 -0
- package/core/workflows/loop.js +692 -0
- package/core/workflows/refactor.js +398 -0
- package/core/workflows/review.js +505 -0
- package/install.ps1 +72 -0
- package/install.sh +69 -0
- package/lib/doctor.js +506 -0
- package/lib/install.js +386 -0
- package/lib/project.js +359 -0
- package/lib/render.js +121 -0
- package/lib/versions.js +145 -0
- package/lib/yaml.js +183 -0
- package/package.json +36 -17
- package/profile/PIPELINE.template.md +288 -0
- package/profile/SCHEMA.md +30 -0
- package/profile/cohorte.config.template.yaml +43 -0
- package/profile/schema/dead-agents.md +29 -0
- package/profile/schema/decisions.md +23 -0
- package/profile/schema/deferred.md +22 -0
- package/profile/schema/feature-diff.md +20 -0
- package/profile/schema/kanban.md +95 -0
- package/profile/schema/metrics.md +23 -0
- package/profile/schema/output-discipline.md +21 -0
- package/profile/schema/preflight.md +81 -0
- package/profile/schema/profile-block.md +103 -0
- package/profile/schema/project-kinds.md +89 -0
- package/profile/schema/readiness.md +17 -0
- package/profile/schema/reconcile.md +64 -0
- package/profile/schema/release-notes.md +29 -0
- package/profile/schema/rendering.md +48 -0
- package/profile/schema/retrieval.md +58 -0
- package/profile/schema/spec-status.md +79 -0
- package/profile/schema/specialization.md +41 -0
- package/profile/schema/verify.md +115 -0
- package/profile/schema/workflows.md +78 -0
- package/scripts/feature-diff.sh +118 -0
- package/scripts/kanban-move.sh +349 -0
- package/scripts/metrics/collect.mjs +542 -0
- package/scripts/metrics/prices.json +42 -0
- package/scripts/new-feature.sh.template +122 -0
- package/scripts/preflight.sh +114 -0
- package/scripts/remove-feature.sh.template +56 -0
- package/scripts/verify-run.sh +509 -0
- package/assets/manifest.json +0 -203
- package/assets/migrations/config/README.md +0 -12
- package/assets/migrations/state/0001_init.sql +0 -154
- package/assets/prompts/README.md +0 -11
- package/assets/prompts/agents/brainstormer.md +0 -3
- package/assets/prompts/agents/fixer.md +0 -7
- package/assets/prompts/agents/implementer.md +0 -12
- package/assets/prompts/agents/reviewer.md +0 -8
- package/assets/prompts/agents/security-reviewer.md +0 -7
- package/assets/prompts/agents/spec-author.md +0 -3
- package/assets/prompts/phases/build.md +0 -5
- package/assets/prompts/phases/review.md +0 -5
- package/assets/prompts/phases/test.md +0 -5
- package/assets/prompts/system.md +0 -11
- package/assets/schemas/agent-output.schema.json +0 -225
- package/assets/schemas/auth-status.schema.json +0 -87
- package/assets/schemas/command-result.schema.json +0 -192
- package/assets/schemas/commands.schema.json +0 -959
- package/assets/schemas/config.schema.json +0 -2281
- package/assets/schemas/doctor-report.schema.json +0 -104
- package/assets/schemas/events.schema.json +0 -8514
- package/assets/schemas/fake-script.schema.json +0 -549
- package/assets/schemas/inspect.schema.json +0 -3133
- package/assets/schemas/manifest.schema.json +0 -79
- package/assets/schemas/ownership.schema.json +0 -52
- package/assets/schemas/policy-verdict.schema.json +0 -282
- package/assets/schemas/project-model.schema.json +0 -783
- package/assets/schemas/project-status.schema.json +0 -217
- package/assets/schemas/reconcile-plan.schema.json +0 -154
- package/assets/schemas/run-diff.schema.json +0 -177
- package/assets/schemas/run-snapshot-manifest.schema.json +0 -2104
- package/assets/schemas/run-state.schema.json +0 -1755
- package/assets/schemas/runtime-capabilities.schema.json +0 -1545
- package/assets/schemas/sandbox-capabilities.schema.json +0 -116
- package/assets/schemas/skill.schema.json +0 -87
- package/assets/schemas/spec.schema.json +0 -202
- package/assets/schemas/tool-catalogue.schema.json +0 -655
- package/assets/schemas/trust-record.schema.json +0 -38
- package/assets/skills/README.md +0 -9
- package/assets/skills/inspect-surface.md +0 -4
- package/dist/agent-host.mjs +0 -2187
- package/dist/agent-host.mjs.map +0 -1
- package/dist/bundle-manifest.json +0 -128
- package/dist/chunks/__host-DgGPyco9.mjs +0 -18
- package/dist/chunks/__host-DgGPyco9.mjs.map +0 -1
- package/dist/chunks/align-ds-r_X_1aQ_.mjs +0 -93
- package/dist/chunks/align-ds-r_X_1aQ_.mjs.map +0 -1
- package/dist/chunks/approve-Cv135UqI.mjs +0 -19
- package/dist/chunks/approve-Cv135UqI.mjs.map +0 -1
- package/dist/chunks/audit-B4ZWQ_DI.mjs +0 -179
- package/dist/chunks/audit-B4ZWQ_DI.mjs.map +0 -1
- package/dist/chunks/auth-BT5oVp76.mjs +0 -132
- package/dist/chunks/auth-BT5oVp76.mjs.map +0 -1
- package/dist/chunks/auth-Dnlksuo5.mjs +0 -141
- package/dist/chunks/auth-Dnlksuo5.mjs.map +0 -1
- package/dist/chunks/brainstorm-SJwOIEif.mjs +0 -123
- package/dist/chunks/brainstorm-SJwOIEif.mjs.map +0 -1
- package/dist/chunks/build-C_-mbZfD.mjs +0 -19
- package/dist/chunks/build-C_-mbZfD.mjs.map +0 -1
- package/dist/chunks/cancel-KV_cpsqS.mjs +0 -15
- package/dist/chunks/cancel-KV_cpsqS.mjs.map +0 -1
- package/dist/chunks/canonical-C8UxeEmB.mjs +0 -57
- package/dist/chunks/canonical-C8UxeEmB.mjs.map +0 -1
- package/dist/chunks/catalogue-IaCJdfB8.mjs +0 -1549
- package/dist/chunks/catalogue-IaCJdfB8.mjs.map +0 -1
- package/dist/chunks/compose-DRKjQcn0.mjs +0 -7333
- package/dist/chunks/compose-DRKjQcn0.mjs.map +0 -1
- package/dist/chunks/config-Cqu5XQoP.mjs +0 -96
- package/dist/chunks/config-Cqu5XQoP.mjs.map +0 -1
- package/dist/chunks/contract-DjssDPpS.mjs +0 -335
- package/dist/chunks/contract-DjssDPpS.mjs.map +0 -1
- package/dist/chunks/contract-bQ9FJl4N.mjs +0 -123
- package/dist/chunks/contract-bQ9FJl4N.mjs.map +0 -1
- package/dist/chunks/deny-ClTxSvHJ.mjs +0 -15
- package/dist/chunks/deny-ClTxSvHJ.mjs.map +0 -1
- package/dist/chunks/diff-Qw0II0sj.mjs +0 -101
- package/dist/chunks/diff-Qw0II0sj.mjs.map +0 -1
- package/dist/chunks/discover-BZPolSZG.mjs +0 -21
- package/dist/chunks/discover-BZPolSZG.mjs.map +0 -1
- package/dist/chunks/doctor-DcDFPvQM.mjs +0 -222
- package/dist/chunks/doctor-DcDFPvQM.mjs.map +0 -1
- package/dist/chunks/fix-C9roGiQz.mjs +0 -82
- package/dist/chunks/fix-C9roGiQz.mjs.map +0 -1
- package/dist/chunks/fleet-yWHQY2ya.mjs +0 -297
- package/dist/chunks/fleet-yWHQY2ya.mjs.map +0 -1
- package/dist/chunks/gc-BBXlBF5w.mjs +0 -161
- package/dist/chunks/gc-BBXlBF5w.mjs.map +0 -1
- package/dist/chunks/init--FxxkrcT.mjs +0 -63
- package/dist/chunks/init--FxxkrcT.mjs.map +0 -1
- package/dist/chunks/inspect-Buby4cQD.mjs +0 -22
- package/dist/chunks/inspect-Buby4cQD.mjs.map +0 -1
- package/dist/chunks/intake-C1pesxCJ.mjs +0 -12
- package/dist/chunks/intake-C1pesxCJ.mjs.map +0 -1
- package/dist/chunks/logs-Giq5sUau.mjs +0 -25
- package/dist/chunks/logs-Giq5sUau.mjs.map +0 -1
- package/dist/chunks/loop-BWsUpyus.mjs +0 -212
- package/dist/chunks/loop-BWsUpyus.mjs.map +0 -1
- package/dist/chunks/migrate-DuYCAbE3.mjs +0 -33
- package/dist/chunks/migrate-DuYCAbE3.mjs.map +0 -1
- package/dist/chunks/models-Btp6X2YZ.mjs +0 -24
- package/dist/chunks/models-Btp6X2YZ.mjs.map +0 -1
- package/dist/chunks/obsidian-BHLkXx_H.mjs +0 -235
- package/dist/chunks/obsidian-BHLkXx_H.mjs.map +0 -1
- package/dist/chunks/patch-sAgVqjpx.mjs +0 -50
- package/dist/chunks/patch-sAgVqjpx.mjs.map +0 -1
- package/dist/chunks/pause-BASBfKLO.mjs +0 -16
- package/dist/chunks/pause-BASBfKLO.mjs.map +0 -1
- package/dist/chunks/policy-Du7WVBFX.mjs +0 -139
- package/dist/chunks/policy-Du7WVBFX.mjs.map +0 -1
- package/dist/chunks/providers-Dx78imKj.mjs +0 -19
- package/dist/chunks/providers-Dx78imKj.mjs.map +0 -1
- package/dist/chunks/reconcile-BHn66X3C.mjs +0 -50
- package/dist/chunks/reconcile-BHn66X3C.mjs.map +0 -1
- package/dist/chunks/refactor-DV3RfGjk.mjs +0 -153
- package/dist/chunks/refactor-DV3RfGjk.mjs.map +0 -1
- package/dist/chunks/render-DyZr5Lgi.mjs +0 -53
- package/dist/chunks/render-DyZr5Lgi.mjs.map +0 -1
- package/dist/chunks/resume-GJQRb2Eb.mjs +0 -15
- package/dist/chunks/resume-GJQRb2Eb.mjs.map +0 -1
- package/dist/chunks/retro-Dr-2RTNN.mjs +0 -78
- package/dist/chunks/retro-Dr-2RTNN.mjs.map +0 -1
- package/dist/chunks/retry-CLrUem1H.mjs +0 -15
- package/dist/chunks/retry-CLrUem1H.mjs.map +0 -1
- package/dist/chunks/review-ZsBqp3vZ.mjs +0 -35
- package/dist/chunks/review-ZsBqp3vZ.mjs.map +0 -1
- package/dist/chunks/run-BGabwbDL.mjs +0 -109
- package/dist/chunks/run-BGabwbDL.mjs.map +0 -1
- package/dist/chunks/run-tool-C3rAv4XF.mjs +0 -26
- package/dist/chunks/run-tool-C3rAv4XF.mjs.map +0 -1
- package/dist/chunks/scan-C5uDr7Yq.mjs +0 -167
- package/dist/chunks/scan-C5uDr7Yq.mjs.map +0 -1
- package/dist/chunks/schema-Bq9qtgsd.mjs +0 -583
- package/dist/chunks/schema-Bq9qtgsd.mjs.map +0 -1
- package/dist/chunks/send-BdnJ85Nz.mjs +0 -19
- package/dist/chunks/send-BdnJ85Nz.mjs.map +0 -1
- package/dist/chunks/ship-BizfUe62.mjs +0 -140
- package/dist/chunks/ship-BizfUe62.mjs.map +0 -1
- package/dist/chunks/shutdown-BM92Iyv2.mjs +0 -15
- package/dist/chunks/shutdown-BM92Iyv2.mjs.map +0 -1
- package/dist/chunks/skip-DZG5kpcH.mjs +0 -18
- package/dist/chunks/skip-DZG5kpcH.mjs.map +0 -1
- package/dist/chunks/spec-C2h3U64g.mjs +0 -65
- package/dist/chunks/spec-C2h3U64g.mjs.map +0 -1
- package/dist/chunks/spec-path-tptqR7W8.mjs +0 -10
- package/dist/chunks/spec-path-tptqR7W8.mjs.map +0 -1
- package/dist/chunks/src-Bfr1yzsn.mjs +0 -1689
- package/dist/chunks/src-Bfr1yzsn.mjs.map +0 -1
- package/dist/chunks/src-CPW2_Ons.mjs +0 -5487
- package/dist/chunks/src-CPW2_Ons.mjs.map +0 -1
- package/dist/chunks/src-Cu3TxNob.mjs +0 -783
- package/dist/chunks/src-Cu3TxNob.mjs.map +0 -1
- package/dist/chunks/src-DNsLSxgm.mjs +0 -928
- package/dist/chunks/src-DNsLSxgm.mjs.map +0 -1
- package/dist/chunks/src-DbS9gkJy.mjs +0 -117
- package/dist/chunks/src-DbS9gkJy.mjs.map +0 -1
- package/dist/chunks/src-yi5whv3n.mjs +0 -2118
- package/dist/chunks/src-yi5whv3n.mjs.map +0 -1
- package/dist/chunks/status-SMjiOMYy.mjs +0 -29
- package/dist/chunks/status-SMjiOMYy.mjs.map +0 -1
- package/dist/chunks/tail-BuhT-2PH.mjs +0 -25
- package/dist/chunks/tail-BuhT-2PH.mjs.map +0 -1
- package/dist/chunks/update-BXIppsbi.mjs +0 -16
- package/dist/chunks/update-BXIppsbi.mjs.map +0 -1
- package/dist/chunks/update-pipeline-Du-Gu1Pl.mjs +0 -56
- package/dist/chunks/update-pipeline-Du-Gu1Pl.mjs.map +0 -1
- package/dist/cli.mjs +0 -186
- package/dist/cli.mjs.map +0 -1
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
// cohorte — /cohorte-refactor as a deterministic workflow (opt-in; the conversational
|
|
2
|
+
// /cohorte-refactor command remains the default path and the fallback).
|
|
3
|
+
//
|
|
4
|
+
// BIG domains only: a domain with just a handful of open backlog items is
|
|
5
|
+
// cheaper through the conversational /cohorte-refactor — this script skips it and says
|
|
6
|
+
// so. Invoke with args = {domains: ["backend", …]} or {domains: "all"}.
|
|
7
|
+
//
|
|
8
|
+
// Shape (profile/schema/workflows.md): profile via profile-reader (phase 0), the open
|
|
9
|
+
// backlog read once, the `shared` domain (contract package — every slice
|
|
10
|
+
// imports it) refactored FIRST and alone, then the other domains' surface
|
|
11
|
+
// implementers in parallel (their trees are disjoint by construction), each
|
|
12
|
+
// verified per-domain with one bounded retry round, then — gates green and ≥ 1 item
|
|
13
|
+
// cleared — the round frozen as ONE `kind: refactor` spec (the route to /cohorte-review →
|
|
14
|
+
// /cohorte-ship, which ships specs, not backlogs) and the backlog ticked with its id.
|
|
15
|
+
|
|
16
|
+
export const meta = {
|
|
17
|
+
name: 'cohorte-refactor',
|
|
18
|
+
description: 'Apply the /cohorte-audit refactor backlog for big domains: shared first, then parallel surface implementers, per-domain verify + one retry',
|
|
19
|
+
whenToUse: 'Only when the human explicitly asks for the refactor workflow on big domains. args = {domains: ["<surface key>", …] | "all"}.',
|
|
20
|
+
phases: [
|
|
21
|
+
{ title: 'Profile', detail: 'PIPELINE.md → JSON via profile-reader', model: 'haiku' },
|
|
22
|
+
{ title: 'Backlog', detail: 'read the open items per requested domain', model: 'haiku' },
|
|
23
|
+
{ title: 'Shared', detail: 'refactor the contract package first, alone' },
|
|
24
|
+
{ title: 'Refactor', detail: 'one surface implementer per domain, parallel' },
|
|
25
|
+
{ title: 'Verify', detail: 'per-domain gates + item check, one retry round', model: 'haiku' },
|
|
26
|
+
{ title: 'Spec', detail: 'freeze the round as a kind: refactor spec for review → ship', model: 'haiku' },
|
|
27
|
+
{ title: 'Tick', detail: 'check cleared items off specs/refactor-backlog.md (· refactor:<id>)', model: 'haiku' },
|
|
28
|
+
],
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// A domain below this many open items is not "big" — the conversational
|
|
32
|
+
// /cohorte-refactor handles it with less overhead than a workflow run.
|
|
33
|
+
const MIN_ITEMS = 5
|
|
34
|
+
|
|
35
|
+
// The Workflow runtime hands `args` to a script verbatim, so a caller that passes a
|
|
36
|
+
// JSON-ENCODED STRING instead of a real object gets that string back here. Parse it
|
|
37
|
+
// back into the object it was meant to be. A bare slug is shorthand for the DOMAIN in
|
|
38
|
+
// this script — mapping it to {feature, target} (review.js's keys, once copy-pasted
|
|
39
|
+
// here) left ARGS.domains undefined, which fell through to 'all': the shorthand
|
|
40
|
+
// "backend" dispatched code-editing implementers on EVERY big domain, not the one
|
|
41
|
+
// the caller named.
|
|
42
|
+
const ARGS = (() => {
|
|
43
|
+
if (typeof args === 'string') {
|
|
44
|
+
const t = args.trim()
|
|
45
|
+
if (t.startsWith('{')) {
|
|
46
|
+
try { const o = JSON.parse(t); if (o && typeof o === 'object' && !Array.isArray(o)) return o } catch {}
|
|
47
|
+
}
|
|
48
|
+
return { domains: [t] }
|
|
49
|
+
}
|
|
50
|
+
return args && typeof args === 'object' ? args : {}
|
|
51
|
+
})()
|
|
52
|
+
|
|
53
|
+
const wanted = (() => {
|
|
54
|
+
const d = ARGS.domains
|
|
55
|
+
if (!d || d === 'all') return 'all'
|
|
56
|
+
return Array.isArray(d) ? d : [String(d)]
|
|
57
|
+
})()
|
|
58
|
+
|
|
59
|
+
// The profile-reader returns through a StructuredOutput tool call, and a haiku agent
|
|
60
|
+
// intermittently nests the whole profile as a JSON *string* under a single wrapper field
|
|
61
|
+
// ({"output": "{\"surfaces\": …}"}) instead of putting the profile's keys at the top level.
|
|
62
|
+
// The schema here used to be {type:'object', additionalProperties:true} — no declared
|
|
63
|
+
// properties, no required keys — so that wrapper validated cleanly and every field then read
|
|
64
|
+
// as undefined: `surfaces` fell back to [], parallel([]) dispatched zero agents, the
|
|
65
|
+
// dead-agent guard had no surfaces to find missing, and the run reported a verdict having
|
|
66
|
+
// done nothing. On the surface it is indistinguishable from a clean run with an empty diff.
|
|
67
|
+
// Declaring the shape gives the tool layer something to validate and the agent something to
|
|
68
|
+
// aim at; unwrapProfile() salvages a wrapped return that still gets through; and the
|
|
69
|
+
// zero-surface abort below makes the silent-success path impossible either way.
|
|
70
|
+
// See also the structured-output section of core/agents/cohorte-profile-reader.md.
|
|
71
|
+
const PROFILE = {
|
|
72
|
+
type: 'object', additionalProperties: true,
|
|
73
|
+
properties: {
|
|
74
|
+
error: { type: 'string', description: 'set ONLY when PIPELINE.md is missing or unparseable' },
|
|
75
|
+
surfaces: {
|
|
76
|
+
type: 'array',
|
|
77
|
+
description: "one entry per surface, at the TOP LEVEL of this object — never a JSON string",
|
|
78
|
+
items: {
|
|
79
|
+
type: 'object', required: ['key'], additionalProperties: true,
|
|
80
|
+
properties: { key: { type: 'string' }, path: { type: 'string' }, agent: { type: 'string' } },
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Salvage a profile handed back as JSON text rather than as an object — either the whole
|
|
87
|
+
// return, or nested under a single wrapper field. Anything already shaped like a profile
|
|
88
|
+
// (has `surfaces`, or is the documented `{error}` failure shape) passes through untouched.
|
|
89
|
+
const unwrapProfile = p => {
|
|
90
|
+
if (typeof p === 'string') { try { return JSON.parse(p) } catch { return null } }
|
|
91
|
+
if (!p || typeof p !== 'object') return null
|
|
92
|
+
if (Array.isArray(p.surfaces) || p.error) return p
|
|
93
|
+
for (const v of Object.values(p)) {
|
|
94
|
+
if (typeof v !== 'string') continue
|
|
95
|
+
try {
|
|
96
|
+
const inner = JSON.parse(v)
|
|
97
|
+
if (inner && typeof inner === 'object' && !Array.isArray(inner)) return inner
|
|
98
|
+
} catch {}
|
|
99
|
+
}
|
|
100
|
+
return p
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const OPEN = {
|
|
104
|
+
type: 'object', required: ['domains'], additionalProperties: false,
|
|
105
|
+
properties: {
|
|
106
|
+
domains: {
|
|
107
|
+
type: 'array',
|
|
108
|
+
items: {
|
|
109
|
+
type: 'object', required: ['key', 'items'], additionalProperties: false,
|
|
110
|
+
properties: {
|
|
111
|
+
key: { type: 'string' },
|
|
112
|
+
items: { type: 'array', items: { type: 'string', description: 'the open `- [ ] …` line verbatim' } },
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// The spec writer settles the id (the script has no clock — Date is unavailable in a
|
|
120
|
+
// workflow) and returns it; anything not shaped like a refactor id counts as a dead writer.
|
|
121
|
+
const SPEC = {
|
|
122
|
+
type: 'object', required: ['id', 'branch'], additionalProperties: false,
|
|
123
|
+
properties: {
|
|
124
|
+
id: { type: 'string', description: 'refactor-<domains>-<YYYY-MM-DD>[-n], the spec file name without .md' },
|
|
125
|
+
branch: { type: 'string', description: 'the branch checked out after step 3' },
|
|
126
|
+
kanban: { type: 'string', description: 'the line kanban-move.sh printed' },
|
|
127
|
+
},
|
|
128
|
+
}
|
|
129
|
+
const SPEC_ID = /^refactor-[a-z0-9][a-z0-9-]*$/
|
|
130
|
+
|
|
131
|
+
const VERIFY = {
|
|
132
|
+
type: 'object', required: ['cleared', 'remaining', 'gatesGreen'], additionalProperties: false,
|
|
133
|
+
properties: {
|
|
134
|
+
cleared: { type: 'array', items: { type: 'string' } },
|
|
135
|
+
remaining: { type: 'array', items: { type: 'string' } },
|
|
136
|
+
gatesGreen: { type: 'boolean' },
|
|
137
|
+
failures: { type: 'string', description: 'one line per gate failure, no output dumps' },
|
|
138
|
+
},
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ── Phase 0 — profile ────────────────────────────────────────────────────────
|
|
142
|
+
phase('Profile')
|
|
143
|
+
const profile = unwrapProfile(await agent(
|
|
144
|
+
'Return this project\'s profile (the `yaml pipeline-profile` block — `.claude/profile.json`, else PIPELINE.md) as JSON, per your instructions.',
|
|
145
|
+
{ agentType: 'cohorte-profile-reader', label: 'profile', schema: PROFILE, effort: 'low' },
|
|
146
|
+
))
|
|
147
|
+
if (!profile || profile.error) {
|
|
148
|
+
return { error: `profile unreadable: ${(profile && profile.error) || 'profile-reader returned nothing'}` }
|
|
149
|
+
}
|
|
150
|
+
const surfaces = Array.isArray(profile.surfaces) ? profile.surfaces : []
|
|
151
|
+
// A profile with no surfaces cannot do this workflow's work, and every later
|
|
152
|
+
// guard compares against `surfaces` — an empty list makes them all vacuously
|
|
153
|
+
// pass. Fail loudly here instead of finishing with nothing done.
|
|
154
|
+
if (!surfaces.length) {
|
|
155
|
+
return { error: 'profile has no surfaces — nothing would be refactored. the `yaml pipeline-profile` block in PIPELINE.md is empty or unparseable, or the profile-reader mis-returned; run /cohorte-doctor' }
|
|
156
|
+
}
|
|
157
|
+
const byKey = Object.fromEntries(surfaces.map(s => [s.key, s]))
|
|
158
|
+
const contractPath = (profile.contract && profile.contract.path) || ''
|
|
159
|
+
const base = (profile.vcs && profile.vcs.default_branch) || 'main'
|
|
160
|
+
const quiet = s => (s.test_quiet_cmd && !String(s.test_quiet_cmd).startsWith('<'))
|
|
161
|
+
? s.test_quiet_cmd : s.test_cmd ? `${s.test_cmd} 2>&1 | tail -40` : ''
|
|
162
|
+
const real = c => c && !String(c).startsWith('<')
|
|
163
|
+
// The `shared` domain (contract package / everything outside the surface trees) has no
|
|
164
|
+
// surface entry, so no per-surface commands — it used to verify with NO gates and report
|
|
165
|
+
// gatesGreen=true by default. Every slice imports it, so it is verified with the REPO-WIDE
|
|
166
|
+
// commands (profile.commands), which is what a change there can actually break.
|
|
167
|
+
const cmds = profile.commands || {}
|
|
168
|
+
const repoGates = [
|
|
169
|
+
real(cmds.test_quiet) ? cmds.test_quiet : real(cmds.test) ? `${cmds.test} 2>&1 | tail -40` : '',
|
|
170
|
+
real(cmds.lint_quiet) ? cmds.lint_quiet : real(cmds.lint) ? `${cmds.lint} 2>&1 | tail -40` : '',
|
|
171
|
+
cmds.typecheck,
|
|
172
|
+
].filter(real)
|
|
173
|
+
|
|
174
|
+
// ── Phase 1 — read the open backlog ──────────────────────────────────────────
|
|
175
|
+
phase('Backlog')
|
|
176
|
+
const backlog = await agent(
|
|
177
|
+
'Read specs/refactor-backlog.md and return, per `## <domain>` heading, the OPEN `- [ ] …` item lines ' +
|
|
178
|
+
'verbatim (skip checked `- [x]` ones). File missing ⇒ return an empty domains array. ' +
|
|
179
|
+
`Requested domains: ${wanted === 'all' ? 'all' : wanted.join(', ')} — return only those (all ⇒ every domain with open items).`,
|
|
180
|
+
{ model: 'haiku', label: 'read-backlog', schema: OPEN, effort: 'low' },
|
|
181
|
+
)
|
|
182
|
+
// A dead reader is not "the backlog is empty" — reporting it as such sends the
|
|
183
|
+
// human to re-run /cohorte-audit on a backlog that is already there.
|
|
184
|
+
if (!backlog) return { error: 'the backlog-reading agent died — nothing was read; re-run the refactor workflow' }
|
|
185
|
+
const open = (backlog.domains || []).filter(d => d.items.length)
|
|
186
|
+
if (!open.length) return { error: 'no open backlog items for the requested domains — run /cohorte-audit (or the audit workflow) first' }
|
|
187
|
+
|
|
188
|
+
const big = open.filter(d => d.items.length >= MIN_ITEMS)
|
|
189
|
+
const small = open.filter(d => d.items.length < MIN_ITEMS)
|
|
190
|
+
for (const d of small) log(`Skipping ${d.key} (${d.items.length} open item(s) < ${MIN_ITEMS}) — use the conversational /cohorte-refactor ${d.key}, it's cheaper`)
|
|
191
|
+
if (!big.length) return { skipped: Object.fromEntries(small.map(d => [d.key, d.items.length])), reason: `every requested domain is below the ${MIN_ITEMS}-item workflow threshold — use /cohorte-refactor` }
|
|
192
|
+
|
|
193
|
+
const implementPrompt = d =>
|
|
194
|
+
'Refactor pass on your surface (no feature spec). Read PIPELINE.md first. Add the missing tests ' +
|
|
195
|
+
'FIRST (pin current behavior / cover the entry points), watch them pass, THEN refactor to clear each ' +
|
|
196
|
+
'item. Preserve current public behavior unless an item marks it a bug. Migrations stay additive. ' +
|
|
197
|
+
`Need the current state of your tree? Compute it yourself: <core>/pipeline/scripts/feature-diff.sh --default ${base} --patch -- <your surface path> ` +
|
|
198
|
+
'(<core> = "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/cohorte"; never a bare git diff — it hides files you created). ' +
|
|
199
|
+
'Lint + format before handoff; return the handoff in the format your agent instructions define. ' +
|
|
200
|
+
'Backlog items for YOUR surface (self-contained — clear exactly these, reading only the files they name):\n' +
|
|
201
|
+
d.items.join('\n')
|
|
202
|
+
|
|
203
|
+
const verifyDomain = async (d, implHandoff) => {
|
|
204
|
+
if (implHandoff == null) return { key: d.key, cleared: [], remaining: d.items, gatesGreen: false, failures: 'implementer died' }
|
|
205
|
+
const s = byKey[d.key]
|
|
206
|
+
const gateCmds = s ? [quiet(s), s.lint_quiet_cmd || (s.lint_cmd ? `${s.lint_cmd} 2>&1 | tail -40` : ''), s.typecheck_cmd]
|
|
207
|
+
.filter(real) : d.key === 'shared' ? repoGates : []
|
|
208
|
+
// No gates at all is not green: it is unverified. Say so instead of defaulting to true.
|
|
209
|
+
const noGates = gateCmds.length === 0
|
|
210
|
+
let v = await agent(
|
|
211
|
+
`Verify a cohorte refactor round for domain ${d.key}. 1. Run these gates, each redirected to ` +
|
|
212
|
+
`specs/reports/refactor-verify.${d.key}.txt (append) — never print their output: ` +
|
|
213
|
+
`${noGates ? '(none declared — skip the gates; return gatesGreen=false and failures="no gate commands declared")' : gateCmds.map(c => JSON.stringify(c)).join(' · ')}. ` +
|
|
214
|
+
'2. For EACH backlog item below, open its file:line and check the prescribed fix actually landed. ' +
|
|
215
|
+
'Return the item lines split into cleared / remaining (verbatim), gatesGreen, and one line per gate failure.\n' +
|
|
216
|
+
'Items:\n' + d.items.join('\n'),
|
|
217
|
+
{ model: 'haiku', label: `verify:${d.key}`, phase: 'Verify', schema: VERIFY, effort: 'low' },
|
|
218
|
+
)
|
|
219
|
+
if (v && noGates) v = { ...v, gatesGreen: false, failures: 'no gate commands declared for this domain — gates unverified' }
|
|
220
|
+
// One bounded retry: re-dispatch the implementer on what verification rejected.
|
|
221
|
+
// The re-verify covers ONLY the retried items, so round 1's cleared list is carried
|
|
222
|
+
// forward — overwriting it un-ticked every item the first pass verified, and the
|
|
223
|
+
// next /cohorte-refactor re-dispatched finished work.
|
|
224
|
+
if (v && (v.remaining.length || (!v.gatesGreen && !noGates)) && byKey[d.key]) {
|
|
225
|
+
const cleared1 = v.cleared || []
|
|
226
|
+
// Never retry items round 1 already verified cleared: on a gates-red round with
|
|
227
|
+
// nothing remaining, retrying ALL items put the same lines in both `cleared` and
|
|
228
|
+
// `remaining` when the re-verifier died — ticked off the backlog AND reported open.
|
|
229
|
+
const retryItems = v.remaining.length ? v.remaining : d.items.filter(i => !cleared1.includes(i))
|
|
230
|
+
log(`${d.key}: ${v.remaining.length} item(s) remaining${v.gatesGreen ? '' : ' + red gates'} — one retry round`)
|
|
231
|
+
await agent(
|
|
232
|
+
implementPrompt({ key: d.key, items: retryItems }) + (v.failures ? `\nGate failures to clear too:\n${v.failures}` : ''),
|
|
233
|
+
{ agentType: byKey[d.key].agent, label: `retry:${d.key}`, phase: 'Refactor' },
|
|
234
|
+
)
|
|
235
|
+
const v2 = await agent(
|
|
236
|
+
`Re-verify domain ${d.key} after a retry round — same procedure as before (gates redirected to ` +
|
|
237
|
+
`specs/reports/refactor-verify.${d.key}.txt, per-item file:line check, verbatim cleared/remaining lines).\n` +
|
|
238
|
+
'Items:\n' + retryItems.join('\n'),
|
|
239
|
+
{ model: 'haiku', label: `reverify:${d.key}`, phase: 'Verify', schema: VERIFY, effort: 'low' },
|
|
240
|
+
)
|
|
241
|
+
// A dead re-verifier loses only the RETRY round's claim — round 1's verified
|
|
242
|
+
// clears stay cleared; the retried items stay open (unverified ≠ cleared).
|
|
243
|
+
v = v2
|
|
244
|
+
? { ...v2, cleared: [...new Set(cleared1.concat(v2.cleared || []))] }
|
|
245
|
+
: { cleared: cleared1, remaining: retryItems, gatesGreen: false, failures: 'verifier died on the retry round' }
|
|
246
|
+
// The retry's verifier is asked the same question with no gates either — its
|
|
247
|
+
// gatesGreen is no more evidence than round 1's was.
|
|
248
|
+
if (noGates) v = { ...v, gatesGreen: false, failures: 'no gate commands declared for this domain — gates unverified' }
|
|
249
|
+
}
|
|
250
|
+
return { key: d.key, ...(v || { cleared: [], remaining: d.items, gatesGreen: false, failures: 'verifier died' }) }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// ── Phase 2 — `shared` first, alone (every slice imports the contract pkg) ───
|
|
254
|
+
const results = []
|
|
255
|
+
const shared = big.find(d => d.key === 'shared')
|
|
256
|
+
if (shared) {
|
|
257
|
+
phase('Shared')
|
|
258
|
+
const handoff = await agent(
|
|
259
|
+
`Refactor the shared contract package (${contractPath || 'the tree outside every surface'}) of this ` +
|
|
260
|
+
'cohorte project — normally lead-owned, so: additive changes only, never break a shape a surface ' +
|
|
261
|
+
'imports (grep consumers before changing any export), migrations stay additive, lint + format before ' +
|
|
262
|
+
'handoff. Clear exactly these open backlog items (self-contained; read only the files they name):\n' +
|
|
263
|
+
shared.items.join('\n'),
|
|
264
|
+
{ label: 'refactor:shared', phase: 'Shared' },
|
|
265
|
+
)
|
|
266
|
+
results.push(await verifyDomain(shared, handoff))
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// ── Phases 3+4 — the other domains in parallel, each verified as it lands ────
|
|
270
|
+
const rest = big.filter(d => d.key !== 'shared' && byKey[d.key])
|
|
271
|
+
for (const d of big) {
|
|
272
|
+
if (d.key !== 'shared' && !byKey[d.key]) log(`Skipping ${d.key} — no matching surface in PIPELINE.md`)
|
|
273
|
+
}
|
|
274
|
+
const restResults = await pipeline(
|
|
275
|
+
rest,
|
|
276
|
+
d => agent(implementPrompt(d), { agentType: byKey[d.key].agent, label: `refactor:${d.key}`, phase: 'Refactor' }),
|
|
277
|
+
(handoff, d) => verifyDomain(d, handoff),
|
|
278
|
+
)
|
|
279
|
+
results.push(...restResults.filter(Boolean))
|
|
280
|
+
|
|
281
|
+
// ── Phase 5 — freeze the round as a `kind: refactor` spec ────────────────────
|
|
282
|
+
// Without it the round is code in the working tree with no route to a PR. ONE spec for the
|
|
283
|
+
// whole round: review's diff, the freshness digest and the release commit all cover the
|
|
284
|
+
// whole tree, so a spec per domain would each claim the others' code. Red gates ⇒ no spec
|
|
285
|
+
// (review's preflight would abort it anyway) — finish the round first.
|
|
286
|
+
const clearedAll = results.flatMap(r => r.cleared)
|
|
287
|
+
const remainingAll = results.flatMap(r => r.remaining)
|
|
288
|
+
const gatesAllGreen = results.length > 0 && results.every(r => r.gatesGreen)
|
|
289
|
+
const clearedDomains = results.filter(r => r.cleared.length)
|
|
290
|
+
// Surface keys are free text (`web_checkout`, `API`); an id is a file name and a branch,
|
|
291
|
+
// and SPEC_ID below rejects anything else — so normalize here, not in the writer.
|
|
292
|
+
const idPart = k => String(k).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '') || 'domain'
|
|
293
|
+
const slug = clearedDomains.length > 3 ? 'all' : clearedDomains.map(r => idPart(r.key)).join('-')
|
|
294
|
+
const prefix = (profile.vcs && profile.vcs.feature_branch_prefix) || 'feature/'
|
|
295
|
+
const bare = l => String(l).replace(/^- \[[ x]\] /, '- ')
|
|
296
|
+
const specTitle = `Refactor ${slug} — ${clearedAll.length} backlog item(s)`
|
|
297
|
+
const specText = [
|
|
298
|
+
'---',
|
|
299
|
+
'feature_id: <ID>',
|
|
300
|
+
'kind: refactor # behaviour-preserving backlog pass — no contract; commit as refactor(<scope>)',
|
|
301
|
+
`title: ${specTitle}`,
|
|
302
|
+
'status: frozen',
|
|
303
|
+
`branch: ${prefix}<ID>`,
|
|
304
|
+
'created: <DATE>',
|
|
305
|
+
'reviewed_base:',
|
|
306
|
+
'reviewed_digest:',
|
|
307
|
+
'---',
|
|
308
|
+
'',
|
|
309
|
+
`# ${specTitle}`,
|
|
310
|
+
'',
|
|
311
|
+
'## 1. Goal',
|
|
312
|
+
'',
|
|
313
|
+
`Clear ${clearedAll.length} \`specs/refactor-backlog.md\` item(s) on ${clearedDomains.map(r => r.key).join(', ')} with no change of public behaviour.`,
|
|
314
|
+
'',
|
|
315
|
+
'## 2. Scope',
|
|
316
|
+
'',
|
|
317
|
+
'**In:** the §6 items, verbatim. **Out:** any behaviour change; the items §7 lists.',
|
|
318
|
+
'',
|
|
319
|
+
'## 6. Surface tasks',
|
|
320
|
+
...clearedDomains.flatMap(r => ['', `### ${r.key}`, '', ...r.cleared.map(bare)]),
|
|
321
|
+
'',
|
|
322
|
+
'## 7. Out of scope',
|
|
323
|
+
'',
|
|
324
|
+
...(remainingAll.length ? remainingAll.map(l => `${bare(l)} (still open in the backlog)`) : ['none']),
|
|
325
|
+
'',
|
|
326
|
+
'## 9. Acceptance criteria / DoD',
|
|
327
|
+
'',
|
|
328
|
+
'- [ ] Public behaviour preserved — no exported signature, route, response shape, CLI flag or persisted format changed (unless an item marks it a bug)',
|
|
329
|
+
'- [ ] Each §6 item cleared at its file:line',
|
|
330
|
+
'- [ ] All tests green — behaviour pinned by tests added first; no existing test loosened',
|
|
331
|
+
'- [ ] `PIPELINE.md` commands.lint · typecheck · test green',
|
|
332
|
+
"- [ ] Nothing outside §6's surfaces changed",
|
|
333
|
+
'',
|
|
334
|
+
'## Remediation',
|
|
335
|
+
'',
|
|
336
|
+
].join('\n')
|
|
337
|
+
|
|
338
|
+
let spec = null
|
|
339
|
+
const wantSpec = clearedAll.length > 0 && gatesAllGreen
|
|
340
|
+
if (wantSpec) {
|
|
341
|
+
phase('Spec')
|
|
342
|
+
const w = await agent(
|
|
343
|
+
'Freeze this cohorte refactor round as a spec (mechanical — no judgment). ' +
|
|
344
|
+
`0. ONE open refactor spec per branch: if \`git rev-parse --abbrev-ref HEAD\` is ${prefix}refactor-<x> and ` +
|
|
345
|
+
'specs/refactor-<x>.md exists with `status:` frozen or in-review, then ID = refactor-<x>: append the §6 item ' +
|
|
346
|
+
"lines below under their `### <domain>` headings there (add a heading if missing), skip to step 4. " +
|
|
347
|
+
`1. Run \`date +%F\` ⇒ DATE; ID = refactor-${slug}-DATE. If specs/ID.md exists and its \`status:\` is ` +
|
|
348
|
+
'shipped/merged/abandoned, suffix -2, -3… until the name is free. If it exists UNSHIPPED (frozen/in-review), ' +
|
|
349
|
+
"do not overwrite it: append this round's §6 item lines under their `### <domain>` headings there and skip step 2. " +
|
|
350
|
+
'2. Write specs/ID.md with EXACTLY the content below, every <ID> and <DATE> substituted. ' +
|
|
351
|
+
`3. \`git rev-parse --abbrev-ref HEAD\`: on ${base} ⇒ \`git switch -c ${prefix}ID\` (uncommitted work carries over); ` +
|
|
352
|
+
`already on ${prefix}ID ⇒ keep it; any other branch ⇒ leave it and report it. ` +
|
|
353
|
+
`4. \`<core>/pipeline/scripts/kanban-move.sh auto ID ready --title "[refactor] ${slug}"\` ` +
|
|
354
|
+
'(<core> = "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/cohorte"). ' +
|
|
355
|
+
'Return id (ID), branch (checked out now) and kanban (the line the script printed).\n' +
|
|
356
|
+
`<<<SPEC\n${specText}SPEC`,
|
|
357
|
+
{ model: 'haiku', label: 'write-spec', schema: SPEC, effort: 'low' },
|
|
358
|
+
)
|
|
359
|
+
spec = w && typeof w === 'object' && SPEC_ID.test(String(w.id || '')) ? w : null
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// ── Phase 6 — tick the cleared items ─────────────────────────────────────────
|
|
363
|
+
phase('Tick')
|
|
364
|
+
let tickedOk = true
|
|
365
|
+
if (clearedAll.length) {
|
|
366
|
+
const ticked = await agent(
|
|
367
|
+
'In specs/refactor-backlog.md flip EXACTLY these open `- [ ]` item lines to `- [x]` (match verbatim, ' +
|
|
368
|
+
(spec ? `append \` · refactor:${spec.id}\` to each flipped line, ` : '') +
|
|
369
|
+
'leave every other line untouched), then return the single word done:\n' + clearedAll.join('\n'),
|
|
370
|
+
{ model: 'haiku', label: 'tick-backlog', effort: 'low' },
|
|
371
|
+
)
|
|
372
|
+
// Reporting items as cleared while the backlog still shows them open means the
|
|
373
|
+
// next /cohorte-refactor re-dispatches work that is already done.
|
|
374
|
+
tickedOk = ticked != null && /done/i.test(String(ticked))
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const branchOk = spec && spec.branch === `${prefix}${spec.id}`
|
|
378
|
+
return {
|
|
379
|
+
domains: Object.fromEntries(results.map(r => [r.key, {
|
|
380
|
+
cleared: r.cleared.length, remaining: r.remaining.length, gatesGreen: r.gatesGreen,
|
|
381
|
+
}])),
|
|
382
|
+
skippedSmall: Object.fromEntries(small.map(d => [d.key, d.items.length])),
|
|
383
|
+
stillOpen: results.flatMap(r => r.remaining.map(line => `[${r.key}] ${line}`)).slice(0, 15),
|
|
384
|
+
backlogTicked: tickedOk,
|
|
385
|
+
// Only a writer that came back with a well-formed id has its spec claimed.
|
|
386
|
+
refactorSpec: spec ? `specs/${spec.id}.md` : null,
|
|
387
|
+
next: !tickedOk
|
|
388
|
+
? `${clearedAll.length} item(s) were cleared in code but NOT ticked off specs/refactor-backlog.md ` +
|
|
389
|
+
'(the ticking agent died) — tick them by hand, or the next /cohorte-refactor re-dispatches finished work'
|
|
390
|
+
: wantSpec && !spec
|
|
391
|
+
? `the refactor spec was NOT written (the spec writer died) — the ${clearedAll.length} cleared item(s) have no ` +
|
|
392
|
+
'route to a PR: write it per /cohorte-refactor §4, then /cohorte-review <id> → /cohorte-ship <id>'
|
|
393
|
+
: spec
|
|
394
|
+
? `/cohorte-review ${spec.id} → /cohorte-ship ${spec.id}` +
|
|
395
|
+
(branchOk ? '' : ` — but the checked-out branch is ${spec.branch || 'unknown'}, not ${prefix}${spec.id}: /cohorte-ship refuses it, switch first`) +
|
|
396
|
+
(remainingAll.length ? ` (${remainingAll.length} item(s) still open, listed in its §7 — /cohorte-refactor <domain> appends them to this spec first, or ship without them)` : '')
|
|
397
|
+
: 'items remain — finish them with the conversational /cohorte-refactor <domain> (gates must be green before the round gets a spec)',
|
|
398
|
+
}
|