cohorte 3.0.0-dev.7 → 3.0.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 +1532 -0
- package/README.md +106 -185
- package/bin/cli.js +212 -0
- package/bin/report.js +141 -0
- package/core/agents/cohorte-profile-reader.md +50 -0
- package/core/agents/cohorte-release.md +58 -0
- package/core/agents/cohorte-review.md +174 -0
- package/core/agents/implementer.template.md +146 -0
- package/core/commands/cohorte-align-ds.md +33 -0
- package/core/commands/cohorte-audit.md +67 -0
- package/core/commands/cohorte-brainstorm.md +73 -0
- package/core/commands/cohorte-build.md +241 -0
- package/core/commands/cohorte-doctor.md +133 -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 +57 -0
- package/core/commands/cohorte-retro.md +85 -0
- package/core/commands/cohorte-review.md +224 -0
- package/core/commands/cohorte-ship.md +155 -0
- package/core/commands/cohorte-spec.md +105 -0
- package/core/commands/cohorte-update-pipeline.md +115 -0
- package/core/hooks/gate.py +2931 -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 +86 -0
- package/core/templates/pr-body.md +25 -0
- package/core/templates/review-feedback.md +46 -0
- package/core/templates/spec.template.md +88 -0
- package/core/templates/steps/init-pipeline/01-detect-stack.md +44 -0
- package/core/templates/steps/init-pipeline/02-interview-gaps.md +55 -0
- package/core/templates/steps/init-pipeline/03-draft-profile.md +9 -0
- package/core/templates/steps/init-pipeline/04-write-render.md +91 -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 +306 -0
- package/core/workflows/loop.js +687 -0
- package/core/workflows/refactor.js +291 -0
- package/core/workflows/review.js +491 -0
- package/install.ps1 +72 -0
- package/install.sh +69 -0
- package/lib/doctor.js +472 -0
- package/lib/install.js +377 -0
- package/lib/project.js +322 -0
- package/lib/render.js +121 -0
- package/lib/versions.js +145 -0
- package/lib/yaml.js +174 -0
- package/package.json +36 -17
- package/profile/PIPELINE.template.md +250 -0
- package/profile/SCHEMA.md +749 -0
- package/profile/cohorte.config.template.yaml +43 -0
- package/scripts/feature-diff.sh +118 -0
- package/scripts/kanban-move.sh +307 -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/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-DasVwI1D.mjs +0 -123
- package/dist/chunks/brainstorm-DasVwI1D.mjs.map +0 -1
- package/dist/chunks/build-crOld8gE.mjs +0 -19
- package/dist/chunks/build-crOld8gE.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-B_oGzGVn.mjs +0 -82
- package/dist/chunks/fix-B_oGzGVn.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-Dd1KVXnO.mjs +0 -12
- package/dist/chunks/intake-Dd1KVXnO.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-CoNoqLZr.mjs +0 -153
- package/dist/chunks/refactor-CoNoqLZr.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-NGAxdsIX.mjs +0 -105
- package/dist/chunks/run-NGAxdsIX.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
package/lib/project.js
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
// cohorte — wire ONE project, deterministically.
|
|
4
|
+
//
|
|
5
|
+
// node <core>/lib/project.js sync [root] # copy the gate, register its hook, pointer, .gitignore
|
|
6
|
+
// node <core>/lib/project.js unsync [root] # undo exactly what sync wrote
|
|
7
|
+
//
|
|
8
|
+
// Everything that describes a project lives in that project and is committed with it:
|
|
9
|
+
// PIPELINE.md, the surface agents, gate-config.json, the gate hook itself and its
|
|
10
|
+
// registration. The machine-wide install (`cohorte install`) only provides the commands
|
|
11
|
+
// and the generic core. A teammate who clones the repo therefore gets the gate even
|
|
12
|
+
// before installing Cohorte.
|
|
13
|
+
//
|
|
14
|
+
// This file is SELF-CONTAINED on purpose: the installer copies it to
|
|
15
|
+
// `~/.claude/cohorte/lib/project.js`, and `/cohorte-init-pipeline` runs it from there, so it
|
|
16
|
+
// must work without the `cohorte` binary on PATH (npx users) and without its siblings.
|
|
17
|
+
|
|
18
|
+
const fs = require('fs');
|
|
19
|
+
const path = require('path');
|
|
20
|
+
const { spawnSync } = require('child_process');
|
|
21
|
+
|
|
22
|
+
const GATE_FILE = path.join('.claude', 'hooks', 'cohorte-gate.py');
|
|
23
|
+
const GATE_COMMAND = 'python3 "$CLAUDE_PROJECT_DIR/.claude/hooks/cohorte-gate.py"';
|
|
24
|
+
const GATE_MATCHER = 'Bash|Task|Agent';
|
|
25
|
+
const EDIT_MATCHER = 'Write|Edit|MultiEdit';
|
|
26
|
+
const INSTALL_HINT = 'npm i -g cohorte && cohorte install';
|
|
27
|
+
const IGNORE_LINES = ['.claude/preflight.ok', '.claude/pipeline-metrics.jsonl', 'specs/reports/'];
|
|
28
|
+
const IGNORE_HEADER = '# cohorte — local-only pipeline artifacts (never commit these)';
|
|
29
|
+
const MARKERS = ['Generated by the cohorte adapter', 'Generated by the cohorte installer'];
|
|
30
|
+
|
|
31
|
+
// --- where the shipped assets are -------------------------------------------
|
|
32
|
+
// Installed: <core>/lib/project.js, <core>/hooks/gate.py, <core>/pipeline/VERSION
|
|
33
|
+
// From source: <pkg>/lib/project.js, <pkg>/core/hooks/gate.py, <pkg>/package.json
|
|
34
|
+
function assets() {
|
|
35
|
+
const up = path.join(__dirname, '..');
|
|
36
|
+
const installed = fs.existsSync(path.join(up, 'pipeline', 'VERSION'));
|
|
37
|
+
const coreDir = installed ? up : path.join(up, 'core');
|
|
38
|
+
let version = null;
|
|
39
|
+
try {
|
|
40
|
+
version = installed
|
|
41
|
+
? fs.readFileSync(path.join(up, 'pipeline', 'VERSION'), 'utf8').trim()
|
|
42
|
+
: JSON.parse(fs.readFileSync(path.join(up, 'package.json'), 'utf8')).version;
|
|
43
|
+
} catch { /* reported by the caller */ }
|
|
44
|
+
return {
|
|
45
|
+
gate: path.join(coreDir, 'hooks', 'gate.py'),
|
|
46
|
+
specTemplate: path.join(coreDir, 'templates', 'spec.template.md'),
|
|
47
|
+
templatesDir: path.join(coreDir, 'templates'),
|
|
48
|
+
version,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// --- JSON that is never clobbered ---------------------------------------------
|
|
53
|
+
class ProjectError extends Error {}
|
|
54
|
+
|
|
55
|
+
// Read a JSON object file. Absent ⇒ {}. Present but unparseable ⇒ throw: a settings file
|
|
56
|
+
// with a trailing comma or a BOM used to be silently replaced by a fresh one, taking the
|
|
57
|
+
// user's model, env and permissions with it.
|
|
58
|
+
function readJsonObject(file) {
|
|
59
|
+
let text;
|
|
60
|
+
try { text = fs.readFileSync(file, 'utf8'); } catch (e) {
|
|
61
|
+
if (e.code === 'ENOENT') return {};
|
|
62
|
+
throw new ProjectError(`cannot read ${file}: ${e.message}`);
|
|
63
|
+
}
|
|
64
|
+
if (text.charCodeAt(0) === 0xfeff) text = text.slice(1);
|
|
65
|
+
if (!text.trim()) return {};
|
|
66
|
+
let data;
|
|
67
|
+
try { data = JSON.parse(text); } catch (e) {
|
|
68
|
+
throw new ProjectError(`${file} is not valid JSON (${e.message}). Fix it and re-run — `
|
|
69
|
+
+ 'cohorte never rewrites a file it cannot parse.');
|
|
70
|
+
}
|
|
71
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
72
|
+
throw new ProjectError(`${file} must contain a JSON object — leaving it untouched.`);
|
|
73
|
+
}
|
|
74
|
+
return data;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function writeIfChanged(file, content, changes, label) {
|
|
78
|
+
let before = null;
|
|
79
|
+
try { before = fs.readFileSync(file, 'utf8'); } catch { /* new file */ }
|
|
80
|
+
if (before === content) return false;
|
|
81
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
82
|
+
fs.writeFileSync(file, content);
|
|
83
|
+
changes.push(`${before === null ? 'created' : 'updated'} ${label}`);
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// --- the hook registration -----------------------------------------------------
|
|
88
|
+
// Ours: the 3.x command, or a 2.x registration of `.claude/hooks/gate.py` (what 2.x
|
|
89
|
+
// /cohorte-init-pipeline wrote: relative, $CLAUDE_PROJECT_DIR-anchored or absolute). Matching
|
|
90
|
+
// is on that exact path, never on a bare `gate.py` suffix — another tool's
|
|
91
|
+
// `~/tools/gate.py` is not ours to remove.
|
|
92
|
+
function isOurCommand(cmd) {
|
|
93
|
+
if (typeof cmd !== 'string') return false;
|
|
94
|
+
if (cmd.includes('cohorte-gate.py')) return true;
|
|
95
|
+
return /(^|["'\s/])\.claude\/hooks\/gate\.py(["'\s]|$)/.test(cmd.replace(/\\/g, '/'));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Remove our hook objects from every PreToolUse group; drop a group only when it ends up
|
|
99
|
+
// empty, so a user's own hook sharing a matcher group survives.
|
|
100
|
+
function stripOurHooks(list) {
|
|
101
|
+
const out = [];
|
|
102
|
+
let removed = 0;
|
|
103
|
+
for (const group of Array.isArray(list) ? list : []) {
|
|
104
|
+
if (!group || typeof group !== 'object' || !Array.isArray(group.hooks)) { out.push(group); continue; }
|
|
105
|
+
const kept = group.hooks.filter((h) => !(h && isOurCommand(h.command)));
|
|
106
|
+
removed += group.hooks.length - kept.length;
|
|
107
|
+
if (kept.length) out.push(kept.length === group.hooks.length ? group : { ...group, hooks: kept });
|
|
108
|
+
}
|
|
109
|
+
return { list: out, removed };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function gateGroups() {
|
|
113
|
+
const hook = { type: 'command', command: GATE_COMMAND, onFailure: 'block' };
|
|
114
|
+
return [
|
|
115
|
+
{ matcher: GATE_MATCHER, hooks: [hook] },
|
|
116
|
+
{ matcher: EDIT_MATCHER, hooks: [{ ...hook }] },
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// 2.x allowlisted the pipeline scripts at their old homes. Point them at the 3.x core.
|
|
121
|
+
function migratePermissions(data) {
|
|
122
|
+
const allow = data.permissions && Array.isArray(data.permissions.allow) ? data.permissions.allow : null;
|
|
123
|
+
if (!allow) return false;
|
|
124
|
+
const next = [];
|
|
125
|
+
for (const rule of allow) {
|
|
126
|
+
let r = rule;
|
|
127
|
+
if (typeof r === 'string') {
|
|
128
|
+
r = r.replace('~/.claude/pipeline/scripts/', '~/.claude/cohorte/pipeline/scripts/');
|
|
129
|
+
if (/\(\s*(bash\s+)?\.claude\/pipeline\/scripts\//.test(r)) continue; // bundled core is gone
|
|
130
|
+
}
|
|
131
|
+
if (!next.includes(r)) next.push(r);
|
|
132
|
+
}
|
|
133
|
+
if (next.length === allow.length && next.every((r, i) => r === allow[i])) return false;
|
|
134
|
+
data.permissions.allow = next;
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// --- 2.x bundled-core cleanup ----------------------------------------------------
|
|
139
|
+
function gitTrackedClean(root, rel) {
|
|
140
|
+
const r = spawnSync('git', ['-C', root, 'ls-files', '--error-unmatch', '--', rel], { stdio: 'ignore' });
|
|
141
|
+
if (r.status !== 0) return false;
|
|
142
|
+
const d = spawnSync('git', ['-C', root, 'diff', '--quiet', 'HEAD', '--', rel], { stdio: 'ignore' });
|
|
143
|
+
return d.status === 0;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function hasMarker(file) {
|
|
147
|
+
try { const t = fs.readFileSync(file, 'utf8'); return MARKERS.some((m) => t.includes(m)); } catch { return false; }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function walk(dir) {
|
|
151
|
+
let out = [];
|
|
152
|
+
let entries = [];
|
|
153
|
+
try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return out; }
|
|
154
|
+
for (const e of entries) {
|
|
155
|
+
const p = path.join(dir, e.name);
|
|
156
|
+
out = e.isDirectory() ? out.concat(walk(p)) : out.concat([p]);
|
|
157
|
+
}
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// A 2.x "bundled" install vendored the whole core into <repo>/.claude. 3.x never does:
|
|
162
|
+
// remove what is provably ours. A file git tracks unmodified is simply deleted (git
|
|
163
|
+
// keeps it); anything else is moved to .claude/cohorte-legacy-backup/ for the human.
|
|
164
|
+
function legacyBundled(root, A) {
|
|
165
|
+
const claude = path.join(root, '.claude');
|
|
166
|
+
if (!fs.existsSync(path.join(claude, 'pipeline', 'VERSION'))) return [];
|
|
167
|
+
const candidates = [];
|
|
168
|
+
candidates.push(...walk(path.join(claude, 'pipeline')));
|
|
169
|
+
for (const f of walk(path.join(claude, 'commands'))) {
|
|
170
|
+
if (/cohorte-[\w-]+\.md$/.test(f) && hasMarker(f)) candidates.push(f);
|
|
171
|
+
}
|
|
172
|
+
for (const n of ['review', 'release', 'profile-reader']) {
|
|
173
|
+
const f = path.join(claude, 'agents', `${n}.md`);
|
|
174
|
+
if (fs.existsSync(f) && hasMarker(f)) candidates.push(f);
|
|
175
|
+
}
|
|
176
|
+
const shipped = new Set(walk(A.templatesDir).map((f) => path.relative(A.templatesDir, f)));
|
|
177
|
+
for (const f of walk(path.join(claude, 'templates'))) {
|
|
178
|
+
if (shipped.has(path.relative(path.join(claude, 'templates'), f))) candidates.push(f);
|
|
179
|
+
}
|
|
180
|
+
for (const n of ['review', 'audit', 'refactor', 'loop']) {
|
|
181
|
+
const f = path.join(claude, 'workflows', `${n}.js`);
|
|
182
|
+
try { if (/name:\s*'cohorte-/.test(fs.readFileSync(f, 'utf8'))) candidates.push(f); } catch { /* absent */ }
|
|
183
|
+
}
|
|
184
|
+
const gate = path.join(claude, 'hooks', 'gate.py');
|
|
185
|
+
if (fs.existsSync(gate)) candidates.push(gate);
|
|
186
|
+
|
|
187
|
+
const moved = [];
|
|
188
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
189
|
+
for (const f of candidates) {
|
|
190
|
+
const rel = path.relative(root, f);
|
|
191
|
+
if (gitTrackedClean(root, rel)) {
|
|
192
|
+
fs.rmSync(f, { force: true });
|
|
193
|
+
moved.push(`removed ${rel} (2.x bundled core — still in git history)`);
|
|
194
|
+
} else {
|
|
195
|
+
const dest = path.join(claude, 'cohorte-legacy-backup', stamp, path.relative(claude, f));
|
|
196
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
197
|
+
fs.renameSync(f, dest);
|
|
198
|
+
moved.push(`moved ${rel} → ${path.relative(root, dest)} (2.x bundled core, not tracked or modified)`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
for (const d of ['pipeline', 'templates', 'workflows', 'commands', 'hooks']) pruneEmpty(path.join(claude, d));
|
|
202
|
+
return moved;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function pruneEmpty(dir) {
|
|
206
|
+
let entries;
|
|
207
|
+
try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; }
|
|
208
|
+
for (const e of entries) if (e.isDirectory()) pruneEmpty(path.join(dir, e.name));
|
|
209
|
+
try { if (!fs.readdirSync(dir).length) fs.rmdirSync(dir); } catch { /* not empty */ }
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// --- .gitignore -------------------------------------------------------------------
|
|
213
|
+
function ensureIgnored(root, changes) {
|
|
214
|
+
const file = path.join(root, '.gitignore');
|
|
215
|
+
let text = '';
|
|
216
|
+
try { text = fs.readFileSync(file, 'utf8'); } catch { /* new */ }
|
|
217
|
+
const have = new Set(text.split(/\r?\n/).map((l) => l.trim().replace(/^\/+/, '')));
|
|
218
|
+
const missing = IGNORE_LINES.filter((l) => !have.has(l) && !have.has(l.replace(/\/$/, '')));
|
|
219
|
+
if (!missing.length) return;
|
|
220
|
+
const sep = text && !text.endsWith('\n') ? '\n' : '';
|
|
221
|
+
const block = (text.includes(IGNORE_HEADER) ? '' : `${text ? '\n' : ''}${IGNORE_HEADER}\n`) + missing.join('\n') + '\n';
|
|
222
|
+
fs.writeFileSync(file, text + sep + block);
|
|
223
|
+
changes.push(`added ${missing.join(', ')} to .gitignore`);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// --- sync / unsync -----------------------------------------------------------------
|
|
227
|
+
function sync(rootArg) {
|
|
228
|
+
const root = path.resolve(rootArg || process.cwd());
|
|
229
|
+
if (!fs.statSync(root).isDirectory()) throw new ProjectError(`${root} is not a directory`);
|
|
230
|
+
const A = assets();
|
|
231
|
+
if (!fs.existsSync(A.gate)) throw new ProjectError(`gate hook not found at ${A.gate} — reinstall cohorte`);
|
|
232
|
+
const changes = [];
|
|
233
|
+
|
|
234
|
+
// Parse everything we will touch BEFORE writing anything: an invalid file aborts the
|
|
235
|
+
// whole sync rather than leaving the project half-wired.
|
|
236
|
+
const settingsPath = path.join(root, '.claude', 'settings.json');
|
|
237
|
+
const pointerPath = path.join(root, '.claude', 'pipeline.json');
|
|
238
|
+
const settings = readJsonObject(settingsPath);
|
|
239
|
+
const pointer = readJsonObject(pointerPath);
|
|
240
|
+
|
|
241
|
+
changes.push(...legacyBundled(root, A));
|
|
242
|
+
|
|
243
|
+
// 1. The gate itself, committed with the repo.
|
|
244
|
+
const gateText = fs.readFileSync(A.gate, 'utf8');
|
|
245
|
+
if (writeIfChanged(path.join(root, GATE_FILE), gateText, changes, GATE_FILE) && process.platform !== 'win32') {
|
|
246
|
+
try { fs.chmodSync(path.join(root, GATE_FILE), 0o755); } catch { /* optional */ }
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// 2. Its registration — exactly one of ours, every foreign hook untouched.
|
|
250
|
+
if (!settings.hooks || typeof settings.hooks !== 'object' || Array.isArray(settings.hooks)) settings.hooks = {};
|
|
251
|
+
const { list } = stripOurHooks(settings.hooks.PreToolUse);
|
|
252
|
+
settings.hooks.PreToolUse = list.concat(gateGroups());
|
|
253
|
+
migratePermissions(settings);
|
|
254
|
+
writeIfChanged(settingsPath, JSON.stringify(settings, null, 2) + '\n', changes, '.claude/settings.json (gate hook)');
|
|
255
|
+
|
|
256
|
+
// 3. The pointer: tells a teammate (and /cohorte-doctor) which core this repo expects.
|
|
257
|
+
delete pointer.mode; // 3.x has a single mode
|
|
258
|
+
Object.assign(pointer, { pipeline: 'cohorte', core_version: A.version, install: INSTALL_HINT });
|
|
259
|
+
writeIfChanged(pointerPath, JSON.stringify(pointer, null, 2) + '\n', changes, '.claude/pipeline.json');
|
|
260
|
+
|
|
261
|
+
// 4. Local-only artifacts stay out of git.
|
|
262
|
+
ensureIgnored(root, changes);
|
|
263
|
+
|
|
264
|
+
// 5. The spec template /cohorte-spec copies from.
|
|
265
|
+
const specTpl = path.join(root, 'specs', '_template.md');
|
|
266
|
+
if (!fs.existsSync(specTpl) && fs.existsSync(A.specTemplate)) {
|
|
267
|
+
fs.mkdirSync(path.dirname(specTpl), { recursive: true });
|
|
268
|
+
fs.copyFileSync(A.specTemplate, specTpl);
|
|
269
|
+
changes.push('created specs/_template.md');
|
|
270
|
+
}
|
|
271
|
+
return { root, version: A.version, changes };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function unsync(rootArg) {
|
|
275
|
+
const root = path.resolve(rootArg || process.cwd());
|
|
276
|
+
const changes = [];
|
|
277
|
+
const settingsPath = path.join(root, '.claude', 'settings.json');
|
|
278
|
+
const settings = readJsonObject(settingsPath);
|
|
279
|
+
if (settings.hooks && Array.isArray(settings.hooks.PreToolUse)) {
|
|
280
|
+
const { list, removed } = stripOurHooks(settings.hooks.PreToolUse);
|
|
281
|
+
if (removed) {
|
|
282
|
+
if (list.length) settings.hooks.PreToolUse = list; else delete settings.hooks.PreToolUse;
|
|
283
|
+
if (!Object.keys(settings.hooks).length) delete settings.hooks;
|
|
284
|
+
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n');
|
|
285
|
+
changes.push(`removed ${removed} gate hook registration(s) from .claude/settings.json`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
for (const rel of [GATE_FILE, path.join('.claude', 'pipeline.json')]) {
|
|
289
|
+
if (fs.existsSync(path.join(root, rel))) { fs.rmSync(path.join(root, rel)); changes.push(`removed ${rel}`); }
|
|
290
|
+
}
|
|
291
|
+
pruneEmpty(path.join(root, '.claude', 'hooks'));
|
|
292
|
+
const left = ['PIPELINE.md', '.claude/gate-config.json', '.claude/agents', 'specs', 'scripts/new-feature.sh',
|
|
293
|
+
'scripts/remove-feature.sh'].filter((p) => fs.existsSync(path.join(root, p)));
|
|
294
|
+
return { root, changes, left };
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
module.exports = { sync, unsync, readJsonObject, stripOurHooks, isOurCommand, ProjectError,
|
|
298
|
+
GATE_COMMAND, GATE_MATCHER, EDIT_MATCHER, GATE_FILE, IGNORE_LINES };
|
|
299
|
+
|
|
300
|
+
// --- CLI -----------------------------------------------------------------------------
|
|
301
|
+
if (require.main === module) {
|
|
302
|
+
const [verb, root] = process.argv.slice(2);
|
|
303
|
+
try {
|
|
304
|
+
if (verb === 'sync') {
|
|
305
|
+
const r = sync(root);
|
|
306
|
+
console.log(`cohorte sync — ${r.root} (core ${r.version})`);
|
|
307
|
+
for (const c of r.changes) console.log(` · ${c}`);
|
|
308
|
+
if (!r.changes.length) console.log(' · already in sync');
|
|
309
|
+
else console.log(' Commit .claude/settings.json, .claude/hooks/cohorte-gate.py, .claude/pipeline.json and .gitignore.');
|
|
310
|
+
} else if (verb === 'unsync') {
|
|
311
|
+
const r = unsync(root);
|
|
312
|
+
for (const c of r.changes) console.log(` · ${c}`);
|
|
313
|
+
if (r.left.length) console.log(` Project files kept (yours to delete): ${r.left.join(', ')}`);
|
|
314
|
+
} else {
|
|
315
|
+
console.error('usage: project.js sync|unsync [root]');
|
|
316
|
+
process.exit(2);
|
|
317
|
+
}
|
|
318
|
+
} catch (e) {
|
|
319
|
+
console.error(`error: ${e.message}`);
|
|
320
|
+
process.exit(1);
|
|
321
|
+
}
|
|
322
|
+
}
|
package/lib/render.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// cohorte — render the core prompts for Claude Code.
|
|
3
|
+
//
|
|
4
|
+
// The sources in core/commands/*.md and core/agents/*.md are written with a handful of
|
|
5
|
+
// path tokens (`<core>`, `<state>`, `<agents>`, …) so the prose never hardcodes where a
|
|
6
|
+
// machine keeps things. Rendering prepends one preamble that resolves every token, keeps
|
|
7
|
+
// only the frontmatter keys Claude Code understands (an unknown key is prose the model
|
|
8
|
+
// reads as an instruction), and leaves the body untouched.
|
|
9
|
+
//
|
|
10
|
+
// 3.0.0 made Claude Code the only target and dropped the capability conditionals that served
|
|
11
|
+
// the other coding agents: the last multi-runtime tree is on the `archive/multi-runtime` branch.
|
|
12
|
+
|
|
13
|
+
const os = require('os');
|
|
14
|
+
const path = require('path');
|
|
15
|
+
|
|
16
|
+
const COMMAND_KEYS = ['model', 'description', 'argument-hint', 'allowed-tools', 'disable-model-invocation'];
|
|
17
|
+
const AGENT_KEYS = ['name', 'description', 'tools', 'model'];
|
|
18
|
+
|
|
19
|
+
// The matcher the project hook is registered with. `Agent` is the subagent tool's name
|
|
20
|
+
// since Claude Code 2.1.63; `Task` is its former name, kept so an older client still
|
|
21
|
+
// delivers dispatches to the preflight phase gate.
|
|
22
|
+
const GATE_MATCHER = 'Bash|Task|Agent';
|
|
23
|
+
|
|
24
|
+
// Path as the human/model should see it: `~/…` reads better than an expanded homedir,
|
|
25
|
+
// and a project-relative path must stay relative (the agent's cwd is the repo).
|
|
26
|
+
function displayPath(p) {
|
|
27
|
+
if (!p) return null;
|
|
28
|
+
const home = os.homedir();
|
|
29
|
+
if (p === home) return '~';
|
|
30
|
+
if (p.startsWith(home + path.sep)) return '~/' + path.relative(home, p).split(path.sep).join('/');
|
|
31
|
+
return p.split(path.sep).join('/');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// A path a shell will expand even inside double quotes: `$HOME/…` rather than `~/…`.
|
|
35
|
+
function shellPath(p) {
|
|
36
|
+
const home = os.homedir();
|
|
37
|
+
if (p.startsWith(home + path.sep)) return '$HOME/' + path.relative(home, p).split(path.sep).join('/');
|
|
38
|
+
return p.split(path.sep).join('/');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The machine layout every rendered file is written against.
|
|
43
|
+
* @param {string} globalDir Claude Code's config dir (~/.claude, or $CLAUDE_CONFIG_DIR)
|
|
44
|
+
*/
|
|
45
|
+
function layout(globalDir) {
|
|
46
|
+
const core = path.join(globalDir, 'cohorte');
|
|
47
|
+
return {
|
|
48
|
+
globalDir,
|
|
49
|
+
core,
|
|
50
|
+
commands: path.join(globalDir, 'commands'),
|
|
51
|
+
fixedAgents: path.join(globalDir, 'agents'),
|
|
52
|
+
workflows: path.join(globalDir, 'workflows'),
|
|
53
|
+
config: path.join(globalDir, 'cohorte.config.yaml'),
|
|
54
|
+
projectLib: path.join(core, 'lib', 'project.js'),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function preamble(L, { kind = 'command' } = {}) {
|
|
59
|
+
const core = displayPath(L.core);
|
|
60
|
+
const sync = `node "${shellPath(L.projectLib)}" sync .`;
|
|
61
|
+
const out = [];
|
|
62
|
+
out.push(`> **Cohorte — Claude Code.** Generated by the cohorte installer — do not edit this file;`);
|
|
63
|
+
out.push(`> edit \`core/${kind === 'agent' ? 'agents' : 'commands'}/\` in the cohorte source and re-install.`);
|
|
64
|
+
out.push('>');
|
|
65
|
+
out.push(`> - \`<core>\` = \`${core}\` — the pipeline's shared, machine-wide assets (\`pipeline/scripts/\`, \`pipeline/SCHEMA.md\`, \`templates/\`). Every \`<core>/…\` path below resolves there, and nowhere else.`);
|
|
66
|
+
out.push('> - `<state>` = `.claude/` in **this repo** — what the pipeline generates for this project (`gate-config.json`, `preflight.ok`, `pipeline-metrics.jsonl`, `pipeline.json`).');
|
|
67
|
+
out.push('> - `<memory>` = `CLAUDE.md` — the project-instructions file at the repo root, loaded into every session here.');
|
|
68
|
+
out.push(`> - \`<config>\` = \`${displayPath(L.config)}\` — your user-level config (kanban boards, shared vault). One per human, never committed.`);
|
|
69
|
+
out.push('> - `<agents>` = `.claude/agents` in **this repo** — the surface agents rendered from `PIPELINE.md`, committed with the code. Never write a surface agent anywhere else.');
|
|
70
|
+
out.push(`> - \`<fixed-agents>\` = \`${displayPath(L.fixedAgents)}\` — the shipped \`cohorte-review\`, \`cohorte-release\` and \`cohorte-profile-reader\` agents.`);
|
|
71
|
+
out.push(`> - \`<workflows>\` = \`${displayPath(L.workflows)}\` — the shipped workflow scripts (\`cohorte-review.js\`, \`cohorte-audit.js\`, \`cohorte-refactor.js\`, \`cohorte-loop.js\`).`);
|
|
72
|
+
out.push('> - Dispatch: the `Agent` tool (formerly `Task`), `subagent_type: <name>`.');
|
|
73
|
+
out.push('> - **Gate** — `.claude/hooks/cohorte-gate.py` is committed in this repo and registered as a blocking `PreToolUse` hook in `.claude/settings.json`, reading `<state>/gate-config.json`. It fires whether or not you cooperate, and can deny outright or ask the human to confirm.');
|
|
74
|
+
out.push(`> - \`<sync>\` = \`${sync}\` — wires THIS repo deterministically: copies the gate, registers its hook, writes the \`<state>/pipeline.json\` pointer and the \`.gitignore\` entries. Run it from the repo root; never hand-write those files.`);
|
|
75
|
+
return out.join('\n') + '\n';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function parseFrontmatter(text) {
|
|
79
|
+
if (!text.startsWith('---\n')) return { keys: [], body: text };
|
|
80
|
+
const end = text.indexOf('\n---\n', 3);
|
|
81
|
+
if (end === -1) return { keys: [], body: text };
|
|
82
|
+
const block = text.slice(4, end + 1);
|
|
83
|
+
const body = text.slice(end + 5);
|
|
84
|
+
const keys = [];
|
|
85
|
+
for (const line of block.split('\n')) {
|
|
86
|
+
const m = line.match(/^([A-Za-z][\w-]*):\s?(.*)$/);
|
|
87
|
+
// Continuation lines (a wrapped value) belong to the previous key.
|
|
88
|
+
if (!m) {
|
|
89
|
+
if (line.trim() && keys.length) keys[keys.length - 1][1] += '\n' + line;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
keys.push([m[1], m[2]]);
|
|
93
|
+
}
|
|
94
|
+
return { keys, body };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function emitFrontmatter(keys) {
|
|
98
|
+
if (!keys.length) return '';
|
|
99
|
+
return '---\n' + keys.map(([k, v]) => `${k}: ${v}`).join('\n') + '\n---\n\n';
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function render(source, L, allowed, kind) {
|
|
103
|
+
const { keys, body } = parseFrontmatter(source);
|
|
104
|
+
const kept = keys.filter(([k]) => allowed.includes(k));
|
|
105
|
+
return emitFrontmatter(kept) + preamble(L, { kind }) + '\n' + body.replace(/^\n+/, '');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const renderCommand = (source, L) => render(source, L, COMMAND_KEYS, 'command');
|
|
109
|
+
const renderAgent = (source, L) => render(source, L, AGENT_KEYS, 'agent');
|
|
110
|
+
|
|
111
|
+
module.exports = {
|
|
112
|
+
GATE_MATCHER,
|
|
113
|
+
layout,
|
|
114
|
+
displayPath,
|
|
115
|
+
shellPath,
|
|
116
|
+
preamble,
|
|
117
|
+
parseFrontmatter,
|
|
118
|
+
emitFrontmatter,
|
|
119
|
+
renderCommand,
|
|
120
|
+
renderAgent,
|
|
121
|
+
};
|
package/lib/versions.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Freshness data: which core is installed where, and how it compares to npm latest.
|
|
3
|
+
// Dependency-free — reads VERSION files + the committed pointer, fetches the npm registry.
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { spawnSync } = require('child_process');
|
|
8
|
+
|
|
9
|
+
function readTrimmed(file) {
|
|
10
|
+
try { return fs.readFileSync(file, 'utf8').trim(); } catch { return null; }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function readJson(file) {
|
|
14
|
+
try { return JSON.parse(fs.readFileSync(file, 'utf8')); } catch { return null; }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// A core rooted at `dir` (its `pipeline/VERSION`). 3.x: `<globalDir>/cohorte`. 2.x left one
|
|
18
|
+
// straight in `<globalDir>` (global mode) or in a repo's `.claude` (bundled mode).
|
|
19
|
+
function coreAt(dir) {
|
|
20
|
+
const version = readTrimmed(path.join(dir, 'pipeline', 'VERSION'));
|
|
21
|
+
return { present: version != null, version, dir };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// The committed per-repo pointer; names the core_version (and, on a 2.x repo, a mode).
|
|
25
|
+
function pointerAt(projectRoot) {
|
|
26
|
+
const ptr = readJson(path.join(projectRoot, '.claude', 'pipeline.json'));
|
|
27
|
+
if (!ptr || typeof ptr !== 'object') return { present: false };
|
|
28
|
+
return { present: true, mode: ptr.mode || null, core_version: ptr.core_version || null };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Latest published version — registry fetch first, `npm view` as a fallback (it uses the
|
|
32
|
+
// user's configured registry/proxy, which works where a raw fetch may be blocked). Cached
|
|
33
|
+
// briefly so repeated calls don't hammer the network. null only if both fail.
|
|
34
|
+
let _cache = { value: null, at: 0 };
|
|
35
|
+
const CACHE_MS = 5 * 60 * 1000;
|
|
36
|
+
// Failures are cached too, briefly. Without this an offline machine paid the FULL
|
|
37
|
+
// 5s fetch timeout + 8s `npm view` timeout on every call — and /api/fleet calls
|
|
38
|
+
// this once per tracked project, so a batch of probes never finished.
|
|
39
|
+
const FAIL_CACHE_MS = 60 * 1000;
|
|
40
|
+
|
|
41
|
+
async function fetchRegistry(timeoutMs = 5000) {
|
|
42
|
+
const ctrl = new AbortController();
|
|
43
|
+
const t = setTimeout(() => ctrl.abort(), timeoutMs);
|
|
44
|
+
try {
|
|
45
|
+
const res = await fetch('https://registry.npmjs.org/cohorte/latest', {
|
|
46
|
+
signal: ctrl.signal,
|
|
47
|
+
headers: { accept: 'application/vnd.npm.install-v1+json' },
|
|
48
|
+
});
|
|
49
|
+
if (!res.ok) return null;
|
|
50
|
+
const body = await res.json();
|
|
51
|
+
return typeof body.version === 'string' ? body.version : null;
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
} finally {
|
|
55
|
+
clearTimeout(t);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Fallback only — spawnSync blocks the single-threaded server for up to its
|
|
60
|
+
// timeout, so it must stay behind latestNpm()'s cache + in-flight dedupe (at most
|
|
61
|
+
// one call a minute, and only when the registry fetch already failed).
|
|
62
|
+
function npmView() {
|
|
63
|
+
try {
|
|
64
|
+
// One static string on Windows (npm is npm.cmd — needs a shell; the
|
|
65
|
+
// shell+args-array combination is DEP0190-deprecated). Nothing dynamic.
|
|
66
|
+
const r = process.platform === 'win32'
|
|
67
|
+
? spawnSync('npm view cohorte version', { encoding: 'utf8', timeout: 8000, shell: true })
|
|
68
|
+
: spawnSync('npm', ['view', 'cohorte', 'version'], { encoding: 'utf8', timeout: 8000 });
|
|
69
|
+
if (r.status === 0 && r.stdout) {
|
|
70
|
+
const v = r.stdout.trim();
|
|
71
|
+
return /^\d+\.\d+\.\d+/.test(v) ? v : null;
|
|
72
|
+
}
|
|
73
|
+
} catch { /* npm absent or slow */ }
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let _inflight = null;
|
|
78
|
+
// `opts` bounds what a CALLER is willing to pay for the answer. The CLI's staleness
|
|
79
|
+
// notice runs at the tail of an install, where the default 5s fetch + 8s `npm view`
|
|
80
|
+
// fallback would make an OFFLINE install appear to hang for 13 seconds after it had
|
|
81
|
+
// already succeeded — so it asks for a short fetch and no fallback. The cache is
|
|
82
|
+
// shared across callers either way: a cheap lookup still serves an expensive one.
|
|
83
|
+
async function latestNpm({ timeoutMs = 5000, fallback = true } = {}) {
|
|
84
|
+
// Opt-out honoured by every caller (install notice, doctor, panels): offline machines and CI.
|
|
85
|
+
if (process.env.COHORTE_NO_VERSION_CHECK) return null;
|
|
86
|
+
const age = Date.now() - _cache.at;
|
|
87
|
+
if (_cache.at && age < (_cache.value ? CACHE_MS : FAIL_CACHE_MS)) return _cache.value;
|
|
88
|
+
// /api/fleet resolves N projects concurrently — without this, N lookups race and
|
|
89
|
+
// each pays the full timeout instead of sharing one.
|
|
90
|
+
if (_inflight) return _inflight;
|
|
91
|
+
_inflight = (async () => {
|
|
92
|
+
try {
|
|
93
|
+
const v = (await fetchRegistry(timeoutMs)) || (fallback ? npmView() : null);
|
|
94
|
+
_cache = { value: v || null, at: Date.now() };
|
|
95
|
+
return v || null;
|
|
96
|
+
} finally {
|
|
97
|
+
_inflight = null;
|
|
98
|
+
}
|
|
99
|
+
})();
|
|
100
|
+
return _inflight;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Semver compare — returns -1|0|1 (a<b|a==b|a>b). A pre-release sorts before its release
|
|
104
|
+
// (3.0.0-dev.8 < 3.0.0), and between pre-releases the tags compare as strings.
|
|
105
|
+
function cmpSemver(a, b) {
|
|
106
|
+
if (!a || !b) return null;
|
|
107
|
+
const split = (v) => { const [core, pre] = String(v).split('-', 2); return [core.split('.').map(n => parseInt(n, 10) || 0), pre || null]; };
|
|
108
|
+
const [pa, ra] = split(a);
|
|
109
|
+
const [pb, rb] = split(b);
|
|
110
|
+
for (let i = 0; i < 3; i++) {
|
|
111
|
+
if ((pa[i] || 0) < (pb[i] || 0)) return -1;
|
|
112
|
+
if ((pa[i] || 0) > (pb[i] || 0)) return 1;
|
|
113
|
+
}
|
|
114
|
+
if (ra === rb) return 0;
|
|
115
|
+
if (!ra) return 1;
|
|
116
|
+
if (!rb) return -1;
|
|
117
|
+
return ra < rb ? -1 : 1;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function versions({ projectRoot, globalDir, cliVersion, offline = false }) {
|
|
121
|
+
const global = coreAt(path.join(globalDir, 'cohorte'));
|
|
122
|
+
const pointer = pointerAt(projectRoot);
|
|
123
|
+
const latest = offline ? null : await latestNpm();
|
|
124
|
+
// 2.x layouts, reported so doctor can name the migration — never used to serve the project.
|
|
125
|
+
const legacyGlobal = coreAt(globalDir);
|
|
126
|
+
const legacyBundled = coreAt(path.join(projectRoot, '.claude'));
|
|
127
|
+
|
|
128
|
+
const installedVersion = global.present ? global.version : null;
|
|
129
|
+
return {
|
|
130
|
+
cli: cliVersion,
|
|
131
|
+
latest,
|
|
132
|
+
global,
|
|
133
|
+
// Kept for consumers of `doctor --json` (the Francois panel): 3.x has no bundled core.
|
|
134
|
+
bundled: { present: false, version: null, dir: path.join(projectRoot, '.claude') },
|
|
135
|
+
legacy: { global: legacyGlobal, bundled: legacyBundled },
|
|
136
|
+
pointer,
|
|
137
|
+
installMode: global.present ? 'global' : 'none',
|
|
138
|
+
installedVersion,
|
|
139
|
+
// -1 behind, 0 up-to-date, 1 ahead of registry (dev), null unknown
|
|
140
|
+
freshness: cmpSemver(installedVersion, latest),
|
|
141
|
+
globalFreshness: global.present ? cmpSemver(global.version, latest) : null,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
module.exports = { versions, latestNpm, cmpSemver };
|