pi-spine 1.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.
Files changed (156) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +563 -0
  3. package/bin/get-version.mjs +35 -0
  4. package/bin/spine-batch.mjs +425 -0
  5. package/bin/spine-cli/batch.mjs +47 -0
  6. package/bin/spine-cli/gate.mjs +10 -0
  7. package/bin/spine-cli/integrate.mjs +10 -0
  8. package/bin/spine-cli/plan.mjs +12 -0
  9. package/bin/spine-cli/shared.mjs +73 -0
  10. package/bin/spine-cli/status.mjs +18 -0
  11. package/bin/spine-cli/verify.mjs +199 -0
  12. package/bin/spine-config.mjs +212 -0
  13. package/bin/spine-dashboard.mjs +178 -0
  14. package/bin/spine-deps.mjs +69 -0
  15. package/bin/spine-doctor.mjs +468 -0
  16. package/bin/spine-gate.mjs +137 -0
  17. package/bin/spine-init.mjs +398 -0
  18. package/bin/spine-integrate.mjs +77 -0
  19. package/bin/spine-journal.mjs +198 -0
  20. package/bin/spine-migrate-from-taskplane.mjs +136 -0
  21. package/bin/spine-plan.mjs +83 -0
  22. package/bin/spine-preflight.mjs +700 -0
  23. package/bin/spine-report-progress.d.mts +28 -0
  24. package/bin/spine-report-progress.mjs +126 -0
  25. package/bin/spine-review-step.d.mts +24 -0
  26. package/bin/spine-review-step.mjs +124 -0
  27. package/bin/spine-rules.mjs +35 -0
  28. package/bin/spine-settings.mjs +186 -0
  29. package/bin/spine-state.mjs +110 -0
  30. package/bin/spine-status.mjs +79 -0
  31. package/bin/spine-tasks.mjs +293 -0
  32. package/bin/spine-worker-runner.mjs +314 -0
  33. package/bin/spine.mjs +404 -0
  34. package/extensions/spine/settings-slash.ts +20 -0
  35. package/extensions/spine/slash-commands.ts +806 -0
  36. package/extensions/spine/worker-tools.ts +279 -0
  37. package/extensions/spine-orchestrator.ts +12 -0
  38. package/package.json +72 -0
  39. package/skills/.gitkeep +0 -0
  40. package/skills/create-spine-tasks/SKILL.md +375 -0
  41. package/skills/create-spine-tasks/references/context-template.md +25 -0
  42. package/skills/create-spine-tasks/references/contract-template.md +98 -0
  43. package/skills/create-spine-tasks/references/explore-template.md +63 -0
  44. package/skills/create-spine-tasks/references/prompt-template.md +276 -0
  45. package/src/batch/abort.mjs +272 -0
  46. package/src/batch/agent-session-worker.mjs +297 -0
  47. package/src/batch/batch-state-io.mjs +53 -0
  48. package/src/batch/contract-verify.mjs +310 -0
  49. package/src/batch/detached-start.mjs +778 -0
  50. package/src/batch/diagnosis.mjs +378 -0
  51. package/src/batch/engine-lanes/merge.mjs +632 -0
  52. package/src/batch/engine-lanes/queue.mjs +208 -0
  53. package/src/batch/engine-lanes/review.mjs +970 -0
  54. package/src/batch/engine-lanes.mjs +388 -0
  55. package/src/batch/engine-scope.mjs +251 -0
  56. package/src/batch/engine.mjs +445 -0
  57. package/src/batch/evidence-command.mjs +157 -0
  58. package/src/batch/evidence.mjs +203 -0
  59. package/src/batch/gate.mjs +359 -0
  60. package/src/batch/git-commit-env.mjs +46 -0
  61. package/src/batch/git-exec.mjs +26 -0
  62. package/src/batch/heartbeat.mjs +430 -0
  63. package/src/batch/integrate.mjs +414 -0
  64. package/src/batch/journal-rebuild.mjs +388 -0
  65. package/src/batch/journal.mjs +373 -0
  66. package/src/batch/lane-commit.mjs +153 -0
  67. package/src/batch/lifecycle.mjs +364 -0
  68. package/src/batch/metrics.mjs +265 -0
  69. package/src/batch/orphan-detect.mjs +210 -0
  70. package/src/batch/post-merge-limbo.mjs +80 -0
  71. package/src/batch/postmortem.mjs +296 -0
  72. package/src/batch/readers/spine-state.mjs +95 -0
  73. package/src/batch/readers/taskplane-state.mjs +136 -0
  74. package/src/batch/reconcile.mjs +729 -0
  75. package/src/batch/resume-common.mjs +104 -0
  76. package/src/batch/resume-engine.mjs +66 -0
  77. package/src/batch/resume-multi-lanes.mjs +479 -0
  78. package/src/batch/resume-multi-validate.mjs +187 -0
  79. package/src/batch/resume-multi.mjs +254 -0
  80. package/src/batch/resume.mjs +475 -0
  81. package/src/batch/retry.mjs +257 -0
  82. package/src/batch/review-scope.mjs +136 -0
  83. package/src/batch/review.mjs +1045 -0
  84. package/src/batch/salvage.mjs +332 -0
  85. package/src/batch/state.mjs +680 -0
  86. package/src/batch/task-stall-budget.mjs +88 -0
  87. package/src/batch/worker-host.mjs +683 -0
  88. package/src/batch/worker-output.mjs +253 -0
  89. package/src/batch/worker-prompt.mjs +84 -0
  90. package/src/batch/worktree.mjs +290 -0
  91. package/src/cli/deps.mjs +141 -0
  92. package/src/cli/handoff.mjs +364 -0
  93. package/src/cli/rules.mjs +429 -0
  94. package/src/cli/settings-set.mjs +171 -0
  95. package/src/cli/settings-show.mjs +140 -0
  96. package/src/cli/settings-slash.mjs +107 -0
  97. package/src/config/contract.mjs +56 -0
  98. package/src/config/cursor-rules/discover.mjs +416 -0
  99. package/src/config/cursor-rules/index.mjs +39 -0
  100. package/src/config/cursor-rules/match-globs.mjs +132 -0
  101. package/src/config/cursor-rules/parse-frontmatter.mjs +222 -0
  102. package/src/config/cursor-rules/priority.mjs +26 -0
  103. package/src/config/cursor-rules/profile.mjs +403 -0
  104. package/src/config/cursor-rules/select.mjs +259 -0
  105. package/src/config/defaults.mjs +36 -0
  106. package/src/config/env-overrides.mjs +207 -0
  107. package/src/config/merge-defaults.mjs +47 -0
  108. package/src/config/pi-spine-root.mjs +93 -0
  109. package/src/config/reviewer-context.mjs +243 -0
  110. package/src/config/settings-fields.mjs +265 -0
  111. package/src/config/worker-backend.mjs +116 -0
  112. package/src/config/worker-context.mjs +402 -0
  113. package/src/config/worker-launch-script.mjs +130 -0
  114. package/src/config/worktree-setup-hook.mjs +122 -0
  115. package/src/dashboard/public/dashboard.css +77 -0
  116. package/src/dashboard/public/dashboard.js +349 -0
  117. package/src/dashboard/public/index.html +80 -0
  118. package/src/dashboard/server.mjs +155 -0
  119. package/src/dashboard/snapshot.mjs +414 -0
  120. package/src/dashboard/sse.mjs +15 -0
  121. package/src/dashboard/view.mjs +292 -0
  122. package/src/doctor/agent-model-inherit.mjs +101 -0
  123. package/src/doctor/batch-size-guidance.mjs +72 -0
  124. package/src/doctor/coexistence.mjs +312 -0
  125. package/src/doctor/rules-manifest.mjs +133 -0
  126. package/src/doctor/stale-path.mjs +156 -0
  127. package/src/doctor/stall-config.mjs +108 -0
  128. package/src/doctor/suggest-max-parallel.mjs +53 -0
  129. package/src/doctor/task-packet-size.mjs +110 -0
  130. package/src/doctor/worktree-health.mjs +71 -0
  131. package/src/migrate/taskplane-config.mjs +116 -0
  132. package/src/planner/cycles.mjs +63 -0
  133. package/src/planner/format-plan.mjs +132 -0
  134. package/src/planner/graph.mjs +99 -0
  135. package/src/planner/index.mjs +125 -0
  136. package/src/planner/lanes.mjs +111 -0
  137. package/src/planner/pending.mjs +39 -0
  138. package/src/planner/scope.mjs +142 -0
  139. package/src/process/liveness.mjs +21 -0
  140. package/src/tasks/packet/discover.mjs +40 -0
  141. package/src/tasks/packet/index.mjs +81 -0
  142. package/src/tasks/packet/merge-deps.mjs +79 -0
  143. package/src/tasks/packet/parse-prompt.mjs +445 -0
  144. package/src/tasks/packet/parse-status.mjs +118 -0
  145. package/src/tasks/packet/validate-contract.mjs +115 -0
  146. package/src/tasks/packet/validate-prompt.mjs +99 -0
  147. package/src/worker-tools/report-progress.d.mts +18 -0
  148. package/src/worker-tools/report-progress.mjs +59 -0
  149. package/src/worker-tools/request-gate.d.mts +17 -0
  150. package/src/worker-tools/request-gate.mjs +54 -0
  151. package/templates/agents/reviewer.md +123 -0
  152. package/templates/agents/supervisor.md +59 -0
  153. package/templates/agents/worker.md +122 -0
  154. package/templates/rules-profile.json +27 -0
  155. package/templates/spine-config.json +64 -0
  156. package/templates/tasks/CONTEXT.md +30 -0
@@ -0,0 +1,203 @@
1
+ /**
2
+ * Integrate gate evidence bundle (PRD §12.2, FR-GATE).
3
+ */
4
+
5
+ import fs from "node:fs";
6
+ import path from "node:path";
7
+ import { execFileSync } from "node:child_process";
8
+ import { runEvidenceCommand } from "./evidence-command.mjs";
9
+ import { readJournalEvents, readJournalTail } from "./journal.mjs";
10
+ import { generateBatchPostMortem } from "./postmortem.mjs";
11
+ import { reconcileBatch } from "./reconcile.mjs";
12
+
13
+ const TASKPLANE_CONFIG_PATH = ".pi/taskplane-config.json";
14
+
15
+ /**
16
+ * @param {string} projectRoot
17
+ * @param {string} batchId
18
+ */
19
+ export function evidenceDir(projectRoot, batchId) {
20
+ return path.join(projectRoot, ".spine", "runtime", batchId, "evidence");
21
+ }
22
+
23
+ /**
24
+ * @param {string} projectRoot
25
+ */
26
+ function readTaskplaneTestingCommands(projectRoot) {
27
+ const configPath = path.join(projectRoot, TASKPLANE_CONFIG_PATH);
28
+ if (!fs.existsSync(configPath)) return null;
29
+
30
+ try {
31
+ const parsed = JSON.parse(fs.readFileSync(configPath, "utf-8"));
32
+ return parsed?.taskRunner?.testing?.commands ?? null;
33
+ } catch {
34
+ return null;
35
+ }
36
+ }
37
+
38
+ /**
39
+ * @param {import("../../bin/spine-config.mjs").SpineConfig|null} config
40
+ * @param {string} projectRoot
41
+ */
42
+ export function resolveTestingCommands(config, projectRoot) {
43
+ const spineTesting = config?.testing ?? {};
44
+ const taskplane = readTaskplaneTestingCommands(projectRoot) ?? {};
45
+
46
+ return {
47
+ test:
48
+ (typeof spineTesting.test === "string" && spineTesting.test.trim()) ||
49
+ (typeof taskplane.unit === "string" && taskplane.unit.trim()) ||
50
+ (typeof taskplane.test === "string" && taskplane.test.trim()) ||
51
+ "",
52
+ build:
53
+ (typeof spineTesting.build === "string" && spineTesting.build.trim()) ||
54
+ (typeof taskplane.build === "string" && taskplane.build.trim()) ||
55
+ "",
56
+ testWithCoverage:
57
+ (typeof spineTesting.testWithCoverage === "string" && spineTesting.testWithCoverage.trim()) ||
58
+ (typeof taskplane.testWithCoverage === "string" && taskplane.testWithCoverage.trim()) ||
59
+ "",
60
+ };
61
+ }
62
+
63
+ /**
64
+ * @param {string} projectRoot
65
+ * @param {string} baseBranch
66
+ * @param {string} orchBranch
67
+ */
68
+ function collectDiffStat(projectRoot, baseBranch, orchBranch) {
69
+ try {
70
+ return execFileSync("git", ["diff", "--stat", `${baseBranch}..${orchBranch}`], {
71
+ cwd: projectRoot,
72
+ encoding: "utf-8",
73
+ stdio: ["ignore", "pipe", "pipe"],
74
+ }).trim();
75
+ } catch (err) {
76
+ const message = err instanceof Error ? err.message : String(err);
77
+ return `(git diff --stat failed: ${message})`;
78
+ }
79
+ }
80
+
81
+ /**
82
+ * @param {object|null|undefined} batchState
83
+ */
84
+ export function buildTaskScorecard(batchState) {
85
+ const tasks = Array.isArray(batchState?.tasks) ? batchState.tasks : [];
86
+ const lines = [
87
+ "# Batch evidence summary",
88
+ "",
89
+ `| Field | Value |`,
90
+ `|-------|-------|`,
91
+ `| Batch ID | ${batchState?.batchId ?? batchState?.id ?? "—"} |`,
92
+ `| Phase | ${batchState?.phase ?? "—"} |`,
93
+ `| Base branch | ${batchState?.baseBranch ?? "—"} |`,
94
+ `| Orch branch | ${batchState?.orchBranch ?? "—"} |`,
95
+ `| Tasks (total) | ${batchState?.totalTasks ?? tasks.length} |`,
96
+ `| Succeeded | ${batchState?.succeededTasks ?? "—"} |`,
97
+ `| Failed | ${batchState?.failedTasks ?? "—"} |`,
98
+ "",
99
+ "## Task scorecard",
100
+ "",
101
+ ];
102
+
103
+ if (tasks.length === 0) {
104
+ lines.push("_No task rows in batch state._");
105
+ } else {
106
+ lines.push("| Task | Status | Done file |");
107
+ lines.push("|------|--------|-----------|");
108
+ for (const task of tasks) {
109
+ lines.push(
110
+ `| ${task.taskId ?? "—"} | ${task.status ?? "—"} | ${task.doneFileFound ? "yes" : "no"} |`,
111
+ );
112
+ }
113
+ }
114
+
115
+ lines.push("", "_Generated by pi-spine integrate gate (TP-021)._");
116
+ return `${lines.join("\n")}\n`;
117
+ }
118
+
119
+ /**
120
+ * @param {object} ctx
121
+ * @param {string} ctx.projectRoot
122
+ * @param {string} ctx.batchId
123
+ * @param {object|null} [ctx.batchState]
124
+ * @param {import("../../bin/spine-config.mjs").SpineConfig|null} [ctx.config]
125
+ */
126
+ export function collectEvidenceBundle(ctx) {
127
+ const { projectRoot, batchId, batchState = null, config = null } = ctx;
128
+ const dir = evidenceDir(projectRoot, batchId);
129
+ fs.mkdirSync(dir, { recursive: true });
130
+
131
+ /** @type {string[]} */
132
+ const evidenceRefs = [];
133
+
134
+ const reconciliation = reconcileBatch({ projectRoot, batchState, verbose: true });
135
+ const journalTail = readJournalTail(readJournalEvents(projectRoot, batchId));
136
+ const summary = generateBatchPostMortem(batchState, journalTail, reconciliation, projectRoot);
137
+ fs.writeFileSync(path.join(dir, "summary.md"), summary, "utf-8");
138
+ evidenceRefs.push("evidence/summary.md");
139
+
140
+ const baseBranch = batchState?.baseBranch ?? config?.baseBranch ?? "main";
141
+ const orchBranch = batchState?.orchBranch ?? null;
142
+ if (orchBranch) {
143
+ const diffStat = collectDiffStat(projectRoot, baseBranch, orchBranch);
144
+ fs.writeFileSync(path.join(dir, "diff-stat.txt"), `${diffStat}\n`, "utf-8");
145
+ evidenceRefs.push("evidence/diff-stat.txt");
146
+ }
147
+
148
+ const gates = config?.gates ?? {};
149
+ const testing = resolveTestingCommands(config, projectRoot);
150
+ const collectTest = gates.collectTestEvidence !== false;
151
+ const collectBuild = gates.collectBuildEvidence === true;
152
+
153
+ if (collectTest && testing.test) {
154
+ const result = runEvidenceCommand(projectRoot, testing.test);
155
+ fs.writeFileSync(path.join(dir, "test-output.txt"), `${result.output}\n`, "utf-8");
156
+ evidenceRefs.push("evidence/test-output.txt");
157
+ }
158
+
159
+ if (collectBuild && testing.build) {
160
+ const result = runEvidenceCommand(projectRoot, testing.build);
161
+ fs.writeFileSync(path.join(dir, "build-output.txt"), `${result.output}\n`, "utf-8");
162
+ evidenceRefs.push("evidence/build-output.txt");
163
+ }
164
+
165
+ if (testing.testWithCoverage) {
166
+ const result = runEvidenceCommand(projectRoot, testing.testWithCoverage);
167
+ fs.writeFileSync(path.join(dir, "coverage-output.txt"), `${result.output}\n`, "utf-8");
168
+ evidenceRefs.push("evidence/coverage-output.txt");
169
+ }
170
+
171
+ for (const ref of collectWorkerOutputEvidenceRefs(projectRoot, batchId)) {
172
+ evidenceRefs.push(ref);
173
+ }
174
+
175
+ for (const name of fs.readdirSync(dir)) {
176
+ if (name.startsWith("salvage-") && name.endsWith(".json")) {
177
+ evidenceRefs.push(`evidence/${name}`);
178
+ }
179
+ }
180
+
181
+ return { evidenceDir: dir, evidenceRefs };
182
+ }
183
+
184
+ /**
185
+ * @param {string} projectRoot
186
+ * @param {string} batchId
187
+ */
188
+ function collectWorkerOutputEvidenceRefs(projectRoot, batchId) {
189
+ const lanesDir = path.join(projectRoot, ".spine", "runtime", batchId, "lanes");
190
+ if (!fs.existsSync(lanesDir)) return [];
191
+
192
+ /** @type {string[]} */
193
+ const refs = [];
194
+ for (const laneDir of fs.readdirSync(lanesDir)) {
195
+ if (!laneDir.startsWith("lane-")) continue;
196
+ const lanePath = path.join(lanesDir, laneDir);
197
+ for (const name of fs.readdirSync(lanePath)) {
198
+ if (!name.startsWith("worker-output-") || !name.endsWith(".log")) continue;
199
+ refs.push(path.join(".spine", "runtime", batchId, "lanes", laneDir, name));
200
+ }
201
+ }
202
+ return refs.sort();
203
+ }
@@ -0,0 +1,359 @@
1
+ /**
2
+ * Integrate gate FSM and persistence (PRD §12, FR-GATE, FR-INT-02).
3
+ */
4
+
5
+ import crypto from "node:crypto";
6
+ import fs from "node:fs";
7
+ import path from "node:path";
8
+ import { loadSpineConfig } from "../../bin/spine-config.mjs";
9
+ import { collectEvidenceBundle } from "./evidence.mjs";
10
+ import { appendJournalEvent } from "./journal.mjs";
11
+
12
+ /**
13
+ * @param {string} projectRoot
14
+ * @param {string} batchId
15
+ */
16
+ export function gateRecordPath(projectRoot, batchId) {
17
+ return path.join(projectRoot, ".spine", "runtime", batchId, "gate.json");
18
+ }
19
+
20
+ /**
21
+ * @param {string} projectRoot
22
+ * @param {string} batchId
23
+ * @returns {object|null}
24
+ */
25
+ export function loadGateRecord(projectRoot, batchId) {
26
+ const filePath = gateRecordPath(projectRoot, batchId);
27
+ if (!fs.existsSync(filePath)) return null;
28
+
29
+ try {
30
+ return JSON.parse(fs.readFileSync(filePath, "utf-8"));
31
+ } catch {
32
+ return null;
33
+ }
34
+ }
35
+
36
+ /**
37
+ * @param {string} projectRoot
38
+ * @param {object} gate
39
+ */
40
+ export function saveGateRecord(projectRoot, gate) {
41
+ const filePath = gateRecordPath(projectRoot, gate.batchId);
42
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
43
+ fs.writeFileSync(filePath, `${JSON.stringify(gate, null, 2)}\n`, "utf-8");
44
+
45
+ const fd = fs.openSync(filePath, "r");
46
+ try {
47
+ fs.fsyncSync(fd);
48
+ } finally {
49
+ fs.closeSync(fd);
50
+ }
51
+ }
52
+
53
+ /**
54
+ * @param {object} gate
55
+ */
56
+ export function formatGateSummary(gate) {
57
+ const status = String(gate.status ?? "unknown");
58
+ const kind = String(gate.kind ?? "integrate");
59
+ const evidenceCount = Array.isArray(gate.evidenceRefs) ? gate.evidenceRefs.length : 0;
60
+ return `${kind} gate ${status} (${evidenceCount} evidence artifact(s))`;
61
+ }
62
+
63
+ /**
64
+ * @param {object} ctx
65
+ * @param {string} ctx.projectRoot
66
+ * @param {string} ctx.batchId
67
+ * @param {object|null} [ctx.batchState]
68
+ * @param {import("../../bin/spine-config.mjs").SpineConfig|null} [ctx.config]
69
+ */
70
+ export function openIntegrateGate(ctx) {
71
+ const { projectRoot, batchId, batchState = null, config = null } = ctx;
72
+ const existing = loadGateRecord(projectRoot, batchId);
73
+ if (existing) {
74
+ return { gate: existing, opened: false, evidenceRefs: existing.evidenceRefs ?? [] };
75
+ }
76
+
77
+ const evidence = collectEvidenceBundle({ projectRoot, batchId, batchState, config });
78
+ const gate = {
79
+ gateId: crypto.randomUUID(),
80
+ batchId,
81
+ kind: "integrate",
82
+ status: "pending",
83
+ openedAt: new Date().toISOString(),
84
+ evidenceRefs: evidence.evidenceRefs,
85
+ summary: formatGateSummary({ kind: "integrate", status: "pending", evidenceRefs: evidence.evidenceRefs }),
86
+ };
87
+
88
+ saveGateRecord(projectRoot, gate);
89
+ appendJournalEvent(projectRoot, batchId, "gate.opened", {
90
+ gateId: gate.gateId,
91
+ kind: gate.kind,
92
+ status: gate.status,
93
+ evidenceRefs: gate.evidenceRefs,
94
+ });
95
+
96
+ return { gate, opened: true, evidenceRefs: evidence.evidenceRefs };
97
+ }
98
+
99
+ /**
100
+ * Auto-open integrate gate when batch reaches terminal success (AC-4.1).
101
+ *
102
+ * @param {object} ctx
103
+ * @param {string} ctx.projectRoot
104
+ * @param {string} ctx.batchId
105
+ * @param {object} ctx.batchState
106
+ */
107
+ export function openIntegrateGateAfterBatchComplete(ctx) {
108
+ const { projectRoot, batchId, batchState } = ctx;
109
+ const configResult = loadSpineConfig(projectRoot);
110
+ const config = configResult.config ?? null;
111
+
112
+ if (batchState?.phase !== "completed") {
113
+ return { skipped: true, reason: "batch_not_completed" };
114
+ }
115
+
116
+ if (!batchState?.orchBranch) {
117
+ return { skipped: true, reason: "no_orch_branch" };
118
+ }
119
+
120
+ return {
121
+ skipped: false,
122
+ ...openIntegrateGate({ projectRoot, batchId, batchState, config }),
123
+ };
124
+ }
125
+
126
+ /**
127
+ * @param {object} ctx
128
+ * @param {string} ctx.projectRoot
129
+ * @param {string} ctx.batchId
130
+ */
131
+ export function approveIntegrateGate(ctx) {
132
+ const { projectRoot, batchId } = ctx;
133
+ const gate = loadGateRecord(projectRoot, batchId);
134
+
135
+ if (!gate) {
136
+ return {
137
+ ok: false,
138
+ exitCode: 1,
139
+ error: "No integrate gate found for this batch",
140
+ headline: "Cannot approve — gate not opened",
141
+ suggestedCommand: "spine status --diagnose",
142
+ };
143
+ }
144
+
145
+ if (gate.status === "approved") {
146
+ return { ok: true, exitCode: 0, gate, headline: "Integrate gate already approved", alreadyDecided: true };
147
+ }
148
+
149
+ if (gate.status === "rejected") {
150
+ return {
151
+ ok: false,
152
+ exitCode: 1,
153
+ error: "Gate was rejected — reopen batch evidence or start a new gate cycle",
154
+ headline: "Cannot approve a rejected gate",
155
+ suggestedCommand: "spine gate status",
156
+ gate,
157
+ };
158
+ }
159
+
160
+ gate.status = "approved";
161
+ gate.decidedAt = new Date().toISOString();
162
+ gate.decidedBy = "human";
163
+ saveGateRecord(projectRoot, gate);
164
+
165
+ appendJournalEvent(projectRoot, batchId, "gate.approved", {
166
+ gateId: gate.gateId,
167
+ kind: gate.kind,
168
+ status: gate.status,
169
+ });
170
+
171
+ return {
172
+ ok: true,
173
+ exitCode: 0,
174
+ gate,
175
+ headline: "Integrate gate approved",
176
+ suggestedCommand: "spine integrate",
177
+ alternatives: ["/spine-integrate"],
178
+ };
179
+ }
180
+
181
+ /**
182
+ * @param {object} ctx
183
+ * @param {string} ctx.projectRoot
184
+ * @param {string} ctx.batchId
185
+ * @param {string} [ctx.reason]
186
+ */
187
+ export function rejectIntegrateGate(ctx) {
188
+ const { projectRoot, batchId, reason } = ctx;
189
+ const gate = loadGateRecord(projectRoot, batchId);
190
+
191
+ if (!gate) {
192
+ return {
193
+ ok: false,
194
+ exitCode: 1,
195
+ error: "No integrate gate found for this batch",
196
+ headline: "Cannot reject — gate not opened",
197
+ suggestedCommand: "spine status --diagnose",
198
+ };
199
+ }
200
+
201
+ if (gate.status === "rejected") {
202
+ return { ok: true, exitCode: 0, gate, headline: "Integrate gate already rejected", alreadyDecided: true };
203
+ }
204
+
205
+ if (gate.status === "approved") {
206
+ return {
207
+ ok: false,
208
+ exitCode: 1,
209
+ error: "Gate already approved",
210
+ headline: "Cannot reject an approved gate",
211
+ suggestedCommand: "spine integrate",
212
+ gate,
213
+ };
214
+ }
215
+
216
+ gate.status = "rejected";
217
+ gate.decidedAt = new Date().toISOString();
218
+ gate.decidedBy = "human";
219
+ if (reason) gate.rejectionReason = reason;
220
+ saveGateRecord(projectRoot, gate);
221
+
222
+ appendJournalEvent(projectRoot, batchId, "gate.rejected", {
223
+ gateId: gate.gateId,
224
+ kind: gate.kind,
225
+ status: gate.status,
226
+ reason: reason ?? null,
227
+ });
228
+
229
+ return {
230
+ ok: true,
231
+ exitCode: 0,
232
+ gate,
233
+ headline: "Integrate gate rejected",
234
+ suggestedCommand: "spine status --diagnose",
235
+ alternatives: ["/spine-gate status"],
236
+ };
237
+ }
238
+
239
+ /**
240
+ * @param {object} ctx
241
+ * @param {string} ctx.projectRoot
242
+ * @param {string} ctx.batchId
243
+ */
244
+ export function getIntegrateGateStatus(ctx) {
245
+ const { projectRoot, batchId } = ctx;
246
+ const gate = loadGateRecord(projectRoot, batchId);
247
+
248
+ if (!gate) {
249
+ return {
250
+ ok: true,
251
+ exitCode: 0,
252
+ gate: null,
253
+ headline: "No integrate gate on record",
254
+ suggestedCommand: "spine status --diagnose",
255
+ };
256
+ }
257
+
258
+ const commands =
259
+ gate.status === "pending"
260
+ ? { suggestedCommand: "spine gate approve", alternatives: ["spine gate reject", "/spine-gate approve"] }
261
+ : gate.status === "approved"
262
+ ? { suggestedCommand: "spine integrate", alternatives: ["/spine-integrate"] }
263
+ : { suggestedCommand: "spine status --diagnose", alternatives: [] };
264
+
265
+ return {
266
+ ok: true,
267
+ exitCode: 0,
268
+ gate,
269
+ headline: formatGateSummary(gate),
270
+ ...commands,
271
+ };
272
+ }
273
+
274
+ /**
275
+ * Fail-closed integrate gate check (§12.4).
276
+ *
277
+ * @param {object} ctx
278
+ * @param {string} ctx.projectRoot
279
+ * @param {string} ctx.batchId
280
+ * @param {import("../../bin/spine-config.mjs").SpineConfig|null} [ctx.config]
281
+ * @param {boolean} [ctx.forceIntegrate]
282
+ * @param {boolean} [ctx.dryRun]
283
+ */
284
+ export function checkIntegrateGate(ctx) {
285
+ const { projectRoot, batchId, forceIntegrate = false, dryRun = false } = ctx;
286
+ const config = ctx.config ?? loadSpineConfig(projectRoot).config ?? null;
287
+ const requireGate = config?.gates?.requireBeforeIntegrate === true;
288
+
289
+ if (!requireGate) {
290
+ return { ok: true, required: false };
291
+ }
292
+
293
+ if (forceIntegrate) {
294
+ if (process.env.SPINE_ALLOW_FORCE !== "1") {
295
+ return {
296
+ ok: false,
297
+ required: true,
298
+ exitCode: 2,
299
+ failureClass: "GateBlocked",
300
+ error: "Force integrate requires SPINE_ALLOW_FORCE=1",
301
+ headline: "Force integrate blocked — set SPINE_ALLOW_FORCE=1 to bypass gate",
302
+ suggestedCommand: "spine gate approve",
303
+ };
304
+ }
305
+
306
+ if (!dryRun) {
307
+ appendJournalEvent(projectRoot, batchId, "integrate.force_bypass", {
308
+ env: "SPINE_ALLOW_FORCE=1",
309
+ gateRequired: true,
310
+ });
311
+ }
312
+
313
+ return { ok: true, required: true, forced: true };
314
+ }
315
+
316
+ const gate = loadGateRecord(projectRoot, batchId);
317
+ if (!gate) {
318
+ return {
319
+ ok: false,
320
+ required: true,
321
+ exitCode: 2,
322
+ failureClass: "GateBlocked",
323
+ error: "Integrate gate not opened — approve evidence before merging",
324
+ headline: "Integrate blocked — no gate record (run batch to completion or spine gate status)",
325
+ suggestedCommand: "spine gate status",
326
+ alternatives: ["/spine-gate"],
327
+ };
328
+ }
329
+
330
+ if (gate.status === "approved") {
331
+ return { ok: true, required: true, gate };
332
+ }
333
+
334
+ if (gate.status === "rejected") {
335
+ return {
336
+ ok: false,
337
+ required: true,
338
+ exitCode: 2,
339
+ failureClass: "GateBlocked",
340
+ error: gate.rejectionReason ?? "Integrate gate was rejected",
341
+ headline: "Integrate blocked — gate rejected",
342
+ suggestedCommand: "spine gate status",
343
+ gate,
344
+ alternatives: ["/spine-gate status"],
345
+ };
346
+ }
347
+
348
+ return {
349
+ ok: false,
350
+ required: true,
351
+ exitCode: 2,
352
+ failureClass: "GateBlocked",
353
+ error: "Integrate gate pending human approval",
354
+ headline: "Integrate blocked — gate pending approval",
355
+ suggestedCommand: "spine gate approve",
356
+ gate,
357
+ alternatives: ["/spine-gate approve", "spine gate reject"],
358
+ };
359
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Resolve git author/committer identity for lane worktree commits without mutating git config.
3
+ */
4
+
5
+ import { execFileSync } from "node:child_process";
6
+
7
+ const FALLBACK_NAME = "pi-spine";
8
+ const FALLBACK_EMAIL = "pi-spine@localhost";
9
+
10
+ /**
11
+ * @param {string} repoPath
12
+ * @param {string} key
13
+ */
14
+ function readGitConfig(repoPath, key) {
15
+ try {
16
+ return execFileSync("git", ["config", "--get", key], {
17
+ cwd: repoPath,
18
+ encoding: "utf-8",
19
+ stdio: ["ignore", "pipe", "pipe"],
20
+ }).trim();
21
+ } catch {
22
+ return "";
23
+ }
24
+ }
25
+
26
+ /**
27
+ * @param {string} [projectRoot]
28
+ * @returns {Record<string, string>}
29
+ */
30
+ export function resolveGitCommitEnv(projectRoot) {
31
+ const name =
32
+ process.env.SPINE_GIT_USER_NAME?.trim() ||
33
+ (projectRoot ? readGitConfig(projectRoot, "user.name") : "") ||
34
+ FALLBACK_NAME;
35
+ const email =
36
+ process.env.SPINE_GIT_USER_EMAIL?.trim() ||
37
+ (projectRoot ? readGitConfig(projectRoot, "user.email") : "") ||
38
+ FALLBACK_EMAIL;
39
+
40
+ return {
41
+ GIT_AUTHOR_NAME: name,
42
+ GIT_AUTHOR_EMAIL: email,
43
+ GIT_COMMITTER_NAME: name,
44
+ GIT_COMMITTER_EMAIL: email,
45
+ };
46
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Git exec helpers with commit identity env for spine batch operations.
3
+ */
4
+
5
+ import { execFileSync } from "node:child_process";
6
+ import { resolveGitCommitEnv } from "./git-commit-env.mjs";
7
+
8
+ /**
9
+ * @param {string} cwd
10
+ * @param {string[]} args
11
+ * @param {{ throwOnError?: boolean; projectRoot?: string }} [options]
12
+ */
13
+ export function gitExec(cwd, args, { throwOnError = true, projectRoot } = {}) {
14
+ const identityRoot = projectRoot ?? cwd;
15
+ try {
16
+ return execFileSync("git", args, {
17
+ cwd,
18
+ encoding: "utf-8",
19
+ stdio: ["ignore", "pipe", "pipe"],
20
+ env: { ...process.env, ...resolveGitCommitEnv(identityRoot) },
21
+ }).trim();
22
+ } catch (err) {
23
+ if (throwOnError) throw err;
24
+ return null;
25
+ }
26
+ }