pi-spine 2.3.2 → 2.5.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.
@@ -1,20 +1,9 @@
1
1
  // @ts-nocheck
2
2
  /**
3
- * Operator salvage after batch abort/dismiss (FR-REL220-03, #158).
4
- * List API → salvage-batch-list.mjs (SP-591); integrate remains here until SP-605.
3
+ * Operator salvage after batch abort/dismiss (FR-REL220-03 / FR-REL220-04, #158).
4
+ * Thin re-export shim: list → salvage-batch-list.mjs (SP-591); integrate → salvage-batch-integrate.mjs (SP-605).
5
5
  */
6
6
 
7
- import readline from "node:readline/promises";
8
- import { loadSpineConfig } from "../config/spine-config-load.mjs";
9
- import { countCommitsAhead } from "./lane-commit.mjs";
10
- import { checkIntegrateGate } from "./gate.mjs";
11
- import { gitExec } from "./git-exec.mjs";
12
- import { mergeOrchIntoBaseIsolated, syncPlumbingMergePathsToWorktree } from "./integrate-worktree.mjs";
13
- import { appendJournalEvent } from "./journal.mjs";
14
- import { resolveRulesManifestIntegrateDrift } from "./rules-manifest-drift.mjs";
15
- import { laneTaskBranch } from "./worktree.mjs";
16
- import { listSalvageableLanes } from "./salvage-batch-list.mjs";
17
-
18
7
  export {
19
8
  NON_SALVAGEABLE_EXIT_REASONS,
20
9
  formatSalvageListOutput,
@@ -22,396 +11,8 @@ export {
22
11
  listSalvageableLanes,
23
12
  } from "./salvage-batch-list.mjs";
24
13
 
25
- /**
26
- * @param {string} projectRoot
27
- * @param {string[]} args
28
- */
29
- function git(projectRoot, args) {
30
- return gitExec(projectRoot, args, { projectRoot });
31
- }
32
-
33
- /**
34
- * Default interactive confirmation for salvage integrate.
35
- *
36
- * @param {object} summary
37
- */
38
- export async function confirmSalvageIntegrate(summary) {
39
- if (process.stdin.isTTY !== true) {
40
- return false;
41
- }
42
-
43
- const lines = [
44
- "",
45
- `Salvage integrate lane ${summary.laneNumber}: ${summary.taskBranch} → ${summary.baseBranch}`,
46
- ` Commits ahead: ${summary.commitsAhead}`,
47
- ` Salvageable tasks: ${summary.salvageableTasks.join(", ") || "(none)"}`,
48
- ];
49
- if (summary.excludedTasks?.length) {
50
- lines.push(` Excluded tasks: ${summary.excludedTasks.join(", ")}`);
51
- }
52
- if (summary.diffStat) {
53
- lines.push(" Diff:");
54
- for (const statLine of String(summary.diffStat).split("\n")) {
55
- lines.push(` ${statLine}`);
56
- }
57
- }
58
- lines.push("", "Proceed with salvage integrate? [y/N] ");
59
- process.stdout.write(lines.join("\n"));
60
-
61
- const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
62
- try {
63
- const answer = String(await rl.question("")).trim().toLowerCase();
64
- return answer === "y" || answer === "yes";
65
- } finally {
66
- rl.close();
67
- }
68
- }
69
-
70
- /**
71
- * Integrate salvageable lane commits into base after batch abort/dismiss (FR-REL220-04, #158).
72
- *
73
- * @param {string} projectRoot
74
- * @param {string} batchId
75
- * @param {number|string} laneNumber
76
- * @param {object} [options]
77
- * @param {boolean} [options.yes]
78
- * @param {boolean} [options.forceIntegrate]
79
- * @param {(summary: object) => Promise<boolean>} [options.confirmFn]
80
- */
81
- export async function integrateSalvageableLane(projectRoot, batchId, laneNumber, options = {}) {
82
- const resolvedBatchId = String(batchId ?? "").trim();
83
- const laneNum = Number(laneNumber);
84
- const yes = Boolean(options.yes);
85
- const forceIntegrate = Boolean(options.forceIntegrate);
86
- const confirmFn = options.confirmFn ?? confirmSalvageIntegrate;
87
-
88
- if (!resolvedBatchId) {
89
- return {
90
- ok: false,
91
- exitCode: 1,
92
- error: "batch_id_required",
93
- headline: "Batch id is required",
94
- suggestedCommand: "spine batch salvage --batch <batchId> --lane <n> --integrate",
95
- };
96
- }
97
-
98
- if (!Number.isFinite(laneNum) || laneNum <= 0) {
99
- return {
100
- ok: false,
101
- exitCode: 1,
102
- error: "lane_number_invalid",
103
- headline: "Lane number must be a positive integer",
104
- suggestedCommand: `spine batch salvage --batch ${resolvedBatchId} --dry-run`,
105
- batchId: resolvedBatchId,
106
- };
107
- }
108
-
109
- const listResult = listSalvageableLanes(projectRoot, resolvedBatchId);
110
- if (!listResult.ok) {
111
- return { ...listResult, integrate: true };
112
- }
113
-
114
- const lane = (listResult.lanes ?? []).find((entry) => entry.laneNumber === laneNum);
115
- if (!lane) {
116
- return {
117
- ok: false,
118
- exitCode: 1,
119
- error: "lane_not_salvageable",
120
- headline: `Lane ${laneNum} has no salvageable commits for batch ${resolvedBatchId}`,
121
- suggestedCommand: `spine batch salvage --batch ${resolvedBatchId} --dry-run`,
122
- batchId: resolvedBatchId,
123
- laneNumber: laneNum,
124
- };
125
- }
126
-
127
- const baseBranch = String(listResult.baseBranch ?? "main");
128
- const taskBranch = String(lane.taskBranch ?? laneTaskBranch(resolvedBatchId, laneNum));
129
-
130
- let commitsAhead = lane.commitsAhead;
131
- try {
132
- commitsAhead = countCommitsAhead(projectRoot, baseBranch, taskBranch);
133
- } catch (err) {
134
- const message = err instanceof Error ? err.message : String(err);
135
- return {
136
- ok: false,
137
- exitCode: 1,
138
- error: message,
139
- headline: `Cannot salvage integrate — ${message}`,
140
- suggestedCommand: "spine status --diagnose",
141
- batchId: resolvedBatchId,
142
- laneNumber: laneNum,
143
- };
144
- }
145
-
146
- if (commitsAhead <= 0) {
147
- return {
148
- ok: true,
149
- exitCode: 0,
150
- alreadyMerged: true,
151
- batchId: resolvedBatchId,
152
- laneNumber: laneNum,
153
- baseBranch,
154
- taskBranch,
155
- salvageableTasks: lane.salvageableTasks,
156
- excludedTasks: lane.excludedTasks,
157
- headline: `Lane ${laneNum} already merged into ${baseBranch}`,
158
- suggestedCommand: "spine status --diagnose",
159
- };
160
- }
161
-
162
- const configResult = loadSpineConfig(projectRoot);
163
- const gateCheck = checkIntegrateGate({
164
- projectRoot,
165
- batchId: resolvedBatchId,
166
- config: configResult.config ?? null,
167
- forceIntegrate,
168
- });
169
-
170
- if (!gateCheck.ok) {
171
- appendJournalEvent(projectRoot, resolvedBatchId, "batch.salvage_integrate_failed", {
172
- laneNumber: laneNum,
173
- taskBranch,
174
- baseBranch,
175
- gateBlocked: true,
176
- gateStatus: gateCheck.gate?.status ?? "missing",
177
- error: gateCheck.error ?? gateCheck.headline,
178
- });
179
-
180
- return {
181
- ok: false,
182
- exitCode: gateCheck.exitCode ?? 2,
183
- failureClass: gateCheck.failureClass ?? "GateBlocked",
184
- error: gateCheck.error,
185
- headline: gateCheck.headline ?? "Salvage integrate blocked by gate",
186
- suggestedCommand: gateCheck.suggestedCommand ?? "spine gate approve",
187
- alternatives: gateCheck.alternatives ?? ["/spine-gate approve"],
188
- batchId: resolvedBatchId,
189
- laneNumber: laneNum,
190
- gate: gateCheck.gate ?? null,
191
- };
192
- }
193
-
194
- if (!yes) {
195
- if (process.stdin.isTTY !== true) {
196
- return {
197
- ok: false,
198
- exitCode: 1,
199
- error: "confirmation_required",
200
- headline: "Non-interactive mode requires --yes to salvage integrate",
201
- suggestedCommand: `spine batch salvage --batch ${resolvedBatchId} --lane ${laneNum} --integrate --yes`,
202
- batchId: resolvedBatchId,
203
- laneNumber: laneNum,
204
- baseBranch,
205
- taskBranch,
206
- commitsAhead,
207
- salvageableTasks: lane.salvageableTasks,
208
- excludedTasks: lane.excludedTasks,
209
- };
210
- }
211
-
212
- const confirmed = await confirmFn({
213
- laneNumber: laneNum,
214
- taskBranch,
215
- baseBranch,
216
- commitsAhead,
217
- salvageableTasks: lane.salvageableTasks,
218
- excludedTasks: lane.excludedTasks,
219
- diffStat: lane.diffStat,
220
- });
221
-
222
- if (!confirmed) {
223
- return {
224
- ok: false,
225
- exitCode: 1,
226
- error: "confirmation_declined",
227
- cancelled: true,
228
- headline: "Salvage integrate cancelled",
229
- suggestedCommand: `spine batch salvage --batch ${resolvedBatchId} --lane ${laneNum} --integrate --yes`,
230
- batchId: resolvedBatchId,
231
- laneNumber: laneNum,
232
- };
233
- }
234
- }
235
-
236
- const drift = resolveRulesManifestIntegrateDrift({
237
- projectRoot,
238
- baseBranch,
239
- orchBranch: taskBranch,
240
- isolatedMerge: true,
241
- });
242
- if (!drift.ok) {
243
- appendJournalEvent(projectRoot, resolvedBatchId, "batch.salvage_integrate_failed", {
244
- laneNumber: laneNum,
245
- taskBranch,
246
- baseBranch,
247
- error: drift.error,
248
- failureClass: drift.failureClass ?? "DirtyWorktree",
249
- });
250
- return {
251
- ok: false,
252
- exitCode: 1,
253
- error: drift.error,
254
- failureClass: drift.failureClass ?? "DirtyWorktree",
255
- headline: drift.error ?? "Salvage integrate refused — dirty rules-manifest",
256
- suggestedCommand: "spine status --diagnose",
257
- batchId: resolvedBatchId,
258
- laneNumber: laneNum,
259
- };
260
- }
261
-
262
- appendJournalEvent(projectRoot, resolvedBatchId, "batch.salvage_integrate_started", {
263
- laneNumber: laneNum,
264
- taskBranch,
265
- baseBranch,
266
- commitsAhead,
267
- salvageableTasks: lane.salvageableTasks,
268
- excludedTasks: lane.excludedTasks,
269
- gateRequired: gateCheck.required ?? false,
270
- gateForced: gateCheck.forced ?? false,
271
- });
272
-
273
- const mergeResult = mergeOrchIntoBaseIsolated({
274
- projectRoot,
275
- baseBranch,
276
- orchBranch: taskBranch,
277
- batchId: resolvedBatchId,
278
- });
279
-
280
- if (!mergeResult.ok) {
281
- const conflict = mergeResult.failureClass === "MergeConflict";
282
- appendJournalEvent(projectRoot, resolvedBatchId, "batch.salvage_integrate_failed", {
283
- laneNumber: laneNum,
284
- taskBranch,
285
- baseBranch,
286
- error: String(mergeResult.error ?? "merge failed").slice(0, 500),
287
- conflict,
288
- failureClass: mergeResult.failureClass ?? "IntegrateFailed",
289
- });
290
-
291
- return {
292
- ok: false,
293
- exitCode: 1,
294
- error: mergeResult.error,
295
- failureClass: mergeResult.failureClass ?? (conflict ? "MergeConflict" : "IntegrateFailed"),
296
- headline: conflict
297
- ? `Merge conflict salvaging ${taskBranch} into ${baseBranch} — resolve manually`
298
- : `Salvage integrate failed: ${mergeResult.error ?? "merge failed"}`,
299
- suggestedCommand: "spine status --diagnose",
300
- batchId: resolvedBatchId,
301
- laneNumber: laneNum,
302
- alternatives: ["/spine-gate"],
303
- };
304
- }
305
-
306
- const mergeCommit = mergeResult.mergeCommit;
307
- /** @type {{ ok: boolean, timedOut?: boolean, error?: string } | null} */
308
- let syncResult = null;
309
- if (mergeResult.mode === "plumbing") {
310
- const baseSha = git(projectRoot, ["rev-parse", `${mergeCommit}^1`]);
311
- syncResult = syncPlumbingMergePathsToWorktree(projectRoot, baseSha, mergeCommit);
312
- }
313
-
314
- if (syncResult && !syncResult.ok) {
315
- appendJournalEvent(projectRoot, resolvedBatchId, "batch.salvage_integrate_failed", {
316
- laneNumber: laneNum,
317
- taskBranch,
318
- baseBranch,
319
- timeout: Boolean(syncResult.timedOut),
320
- error: (syncResult.error ?? "sync failed").slice(0, 500),
321
- mergeCommitLanded: true,
322
- mergeCommit,
323
- });
324
-
325
- return {
326
- ok: false,
327
- exitCode: 1,
328
- error: syncResult.error ?? "post-merge sync failed",
329
- failureClass: syncResult.timedOut ? "IntegrateTimeout" : "IntegrateFailed",
330
- headline: syncResult.timedOut
331
- ? `Salvage integrate sync timed out after merge landed — ${taskBranch} into ${baseBranch}`
332
- : `Salvage integrate post-merge sync failed — ${syncResult.error}`,
333
- suggestedCommand: "spine status --diagnose",
334
- batchId: resolvedBatchId,
335
- laneNumber: laneNum,
336
- mergeCommitLanded: true,
337
- };
338
- }
339
-
340
- appendJournalEvent(projectRoot, resolvedBatchId, "batch.salvage_integrated", {
341
- laneNumber: laneNum,
342
- taskBranch,
343
- baseBranch,
344
- mergeCommit,
345
- commitsAhead,
346
- salvageableTasks: lane.salvageableTasks,
347
- excludedTasks: lane.excludedTasks,
348
- });
349
-
350
- return {
351
- ok: true,
352
- exitCode: 0,
353
- batchId: resolvedBatchId,
354
- laneNumber: laneNum,
355
- baseBranch,
356
- taskBranch,
357
- mergeCommit,
358
- commitsAhead,
359
- salvageableTasks: lane.salvageableTasks,
360
- excludedTasks: lane.excludedTasks,
361
- headline: `Salvaged lane ${laneNum} (${taskBranch}) into ${baseBranch}`,
362
- suggestedCommand: "spine status --diagnose",
363
- };
364
- }
365
-
366
- /**
367
- * @param {Awaited<ReturnType<typeof integrateSalvageableLane>>} result
368
- * @param {{ json?: boolean }} [options]
369
- */
370
- export function formatSalvageIntegrateOutput(result, options = {}) {
371
- if (options.json) {
372
- return `${JSON.stringify(result, null, 2)}\n`;
373
- }
374
-
375
- const lines = [
376
- "",
377
- result.ok ? "Salvage integrate" : result.cancelled ? "Salvage integrate cancelled" : "Salvage integrate failed",
378
- "",
379
- ` ${result.headline}`,
380
- ];
381
-
382
- if (result.error) {
383
- lines.push("", ` Error: ${result.error}`);
384
- }
385
- if (result.batchId) {
386
- lines.push("", ` Batch: ${result.batchId}`);
387
- }
388
- if (result.laneNumber != null) {
389
- lines.push(` Lane: ${result.laneNumber}`);
390
- }
391
- if (result.baseBranch && result.taskBranch) {
392
- lines.push(` Merge: ${result.taskBranch} → ${result.baseBranch}`);
393
- }
394
- if (result.salvageableTasks?.length) {
395
- lines.push(` Tasks: ${result.salvageableTasks.join(", ")}`);
396
- }
397
- if (result.excludedTasks?.length) {
398
- lines.push(` Excluded: ${result.excludedTasks.join(", ")}`);
399
- }
400
- if (result.mergeCommit) {
401
- lines.push(` Merge commit: ${result.mergeCommit}`);
402
- }
403
-
404
- if (result.suggestedCommand) {
405
- lines.push("", ` → ${result.suggestedCommand}`);
406
- }
407
-
408
- if (result.alternatives?.length) {
409
- lines.push("", " Alternatives:");
410
- for (const alt of result.alternatives) {
411
- lines.push(` • ${alt}`);
412
- }
413
- }
414
-
415
- lines.push("");
416
- return lines.join("\n");
417
- }
14
+ export {
15
+ confirmSalvageIntegrate,
16
+ formatSalvageIntegrateOutput,
17
+ integrateSalvageableLane,
18
+ } from "./salvage-batch-integrate.mjs";
@@ -5,7 +5,7 @@
5
5
  import { loadSpineConfig } from "../config/spine-config-load.mjs";
6
6
  import { DEFAULT_SEQUENCE_POLL_MS } from "../config/spine-config-schema.mjs";
7
7
  import { isProcessAlive } from "../process/liveness.mjs";
8
- import { approveIntegrateGate, loadGateRecord } from "./gate.mjs";
8
+ import { approveIntegrateGate, loadGateRecord, maybeAutoApproveIntegrateGate } from "./gate.mjs";
9
9
  import { integrateOrchToBase } from "./integrate.mjs";
10
10
  import { completeBatch } from "./lifecycle.mjs";
11
11
  import { reconcileBatch } from "./reconcile.mjs";
@@ -127,7 +127,14 @@ export function runSequenceWaveLandLoop({ projectRoot, batchId, autoApproveGate
127
127
  const gate = activeBatchId ? loadGateRecord(projectRoot, activeBatchId) : null;
128
128
 
129
129
  if (gateRequired && gate?.status === "pending") {
130
- if (!autoApproveGate) {
130
+ const postureAuto = maybeAutoApproveIntegrateGate({
131
+ projectRoot,
132
+ batchId: activeBatchId,
133
+ config,
134
+ });
135
+ if (postureAuto.approved) {
136
+ // Posture opt-in allowed auto-approve; continue to integrate.
137
+ } else if (!autoApproveGate) {
131
138
  return {
132
139
  ok: false,
133
140
  step: "gate_approve",
@@ -136,11 +143,18 @@ export function runSequenceWaveLandLoop({ projectRoot, batchId, autoApproveGate
136
143
  batchId: activeBatchId,
137
144
  headline: "Integrate gate requires approval — pass autoApproveGate or approve manually",
138
145
  suggestedCommand: "spine gate approve",
146
+ postureEvaluation: postureAuto.evaluation ?? null,
139
147
  };
140
- }
141
- const approve = approveIntegrateGate({ projectRoot, batchId: activeBatchId });
142
- if (!approve.ok) {
143
- return { ok: false, step: "gate_approve", diagnosis, batchId: activeBatchId, ...approve };
148
+ } else {
149
+ // Blunt --auto-approve-gate path (still gated by validateSequenceAutoApproveGate upstream).
150
+ const approve = approveIntegrateGate({
151
+ projectRoot,
152
+ batchId: activeBatchId,
153
+ decidedBy: "auto",
154
+ });
155
+ if (!approve.ok) {
156
+ return { ok: false, step: "gate_approve", diagnosis, batchId: activeBatchId, ...approve };
157
+ }
144
158
  }
145
159
  }
146
160
 
@@ -0,0 +1,190 @@
1
+ // @ts-check
2
+ /**
3
+ * Resolve gate postures from spine-config with fail-closed defaults (SP-629 / FR-REL250-07 / #123).
4
+ * Invalid or missing overlay never unlocks locked categories and never rejects whole config load.
5
+ */
6
+
7
+ import {
8
+ DEFAULT_POSTURES,
9
+ GATE_CATEGORIES,
10
+ LOCKED_CATEGORIES,
11
+ POSTURES,
12
+ } from "../batch/gate-posture-defaults.mjs";
13
+
14
+ /** @typedef {import("../batch/gate-posture-defaults.mjs").GateCategory} GateCategory */
15
+ /** @typedef {import("../batch/gate-posture-defaults.mjs").GatePosture} GatePosture */
16
+ /** @typedef {import("../batch/gate-posture-defaults.mjs").CategoryPostureDefault} CategoryPostureDefault */
17
+
18
+ /**
19
+ * @typedef {{
20
+ * categories: Readonly<Record<GateCategory, Readonly<CategoryPostureDefault>>>,
21
+ * alwaysBreakOn: ReadonlyArray<string>,
22
+ * }} GatePostureConfig
23
+ */
24
+
25
+ const KNOWN_POSTURES = new Set(Object.values(POSTURES));
26
+ const LOCKED_SET = new Set(/** @type {GateCategory[]} */ ([...LOCKED_CATEGORIES]));
27
+
28
+ /**
29
+ * @param {unknown} value
30
+ * @returns {value is Record<string, unknown>}
31
+ */
32
+ function isPlainObject(value) {
33
+ return typeof value === "object" && value !== null && !Array.isArray(value);
34
+ }
35
+
36
+ /**
37
+ * Fail-closed locked entry used for unknown postures and locked categories.
38
+ * @returns {Readonly<CategoryPostureDefault>}
39
+ */
40
+ function lockedEntry() {
41
+ return Object.freeze({ posture: POSTURES.LOCKED, autoApproveAfterN: null });
42
+ }
43
+
44
+ /**
45
+ * Clone DEFAULT_POSTURES into a mutable record for overlay.
46
+ * @returns {Record<GateCategory, CategoryPostureDefault>}
47
+ */
48
+ function cloneDefaults() {
49
+ /** @type {Record<GateCategory, CategoryPostureDefault>} */
50
+ const out = /** @type {Record<GateCategory, CategoryPostureDefault>} */ ({});
51
+ for (const category of GATE_CATEGORIES) {
52
+ const entry = DEFAULT_POSTURES[category];
53
+ out[category] = { posture: entry.posture, autoApproveAfterN: entry.autoApproveAfterN };
54
+ }
55
+ return out;
56
+ }
57
+
58
+ /**
59
+ * @param {unknown} raw
60
+ * @returns {Readonly<CategoryPostureDefault>}
61
+ */
62
+ function normalizeCategoryEntry(raw) {
63
+ if (!isPlainObject(raw)) {
64
+ return lockedEntry();
65
+ }
66
+
67
+ const postureRaw = raw.posture;
68
+ if (typeof postureRaw !== "string" || !KNOWN_POSTURES.has(/** @type {GatePosture} */ (postureRaw))) {
69
+ return lockedEntry();
70
+ }
71
+
72
+ const posture = /** @type {GatePosture} */ (postureRaw);
73
+ if (posture === POSTURES.LOCKED) {
74
+ return lockedEntry();
75
+ }
76
+
77
+ const thresholdRaw = raw.autoApproveAfterN;
78
+ if (thresholdRaw === undefined) {
79
+ return Object.freeze({ posture, autoApproveAfterN: 0 });
80
+ }
81
+ if (
82
+ typeof thresholdRaw !== "number" ||
83
+ !Number.isInteger(thresholdRaw) ||
84
+ thresholdRaw < 0
85
+ ) {
86
+ return lockedEntry();
87
+ }
88
+
89
+ return Object.freeze({ posture, autoApproveAfterN: thresholdRaw });
90
+ }
91
+
92
+ /**
93
+ * Accept string tags only; invalid shapes fail closed to [].
94
+ * @param {unknown} raw
95
+ * @returns {string[]}
96
+ */
97
+ function normalizeAlwaysBreakOn(raw) {
98
+ if (raw == null) {
99
+ return [];
100
+ }
101
+ if (!Array.isArray(raw)) {
102
+ return [];
103
+ }
104
+ /** @type {string[]} */
105
+ const tags = [];
106
+ for (const entry of raw) {
107
+ if (typeof entry === "string" && entry.trim() !== "") {
108
+ tags.push(entry.trim());
109
+ }
110
+ }
111
+ return tags;
112
+ }
113
+
114
+ /**
115
+ * Locate optional postures overlay and alwaysBreakOn from spine-config.
116
+ * Supports `gates.postures` map and `alwaysBreakOn` on `gates` or under `gates.postures`.
117
+ *
118
+ * @param {unknown} config
119
+ * @returns {{ overlay: Record<string, unknown> | null, alwaysBreakOnRaw: unknown }}
120
+ */
121
+ function extractPostureSection(config) {
122
+ if (!isPlainObject(config)) {
123
+ return { overlay: null, alwaysBreakOnRaw: undefined };
124
+ }
125
+ const gates = config.gates;
126
+ if (!isPlainObject(gates)) {
127
+ return { overlay: null, alwaysBreakOnRaw: undefined };
128
+ }
129
+
130
+ const posturesRaw = gates.postures;
131
+ /** @type {unknown} */
132
+ let alwaysBreakOnRaw = gates.alwaysBreakOn;
133
+
134
+ if (posturesRaw == null) {
135
+ return { overlay: null, alwaysBreakOnRaw };
136
+ }
137
+
138
+ if (!isPlainObject(posturesRaw)) {
139
+ // Invalid postures container — fail closed: ignore overlay, keep alwaysBreakOn if present.
140
+ return { overlay: null, alwaysBreakOnRaw };
141
+ }
142
+
143
+ if (alwaysBreakOnRaw === undefined && "alwaysBreakOn" in posturesRaw) {
144
+ alwaysBreakOnRaw = posturesRaw.alwaysBreakOn;
145
+ }
146
+
147
+ return { overlay: posturesRaw, alwaysBreakOnRaw };
148
+ }
149
+
150
+ /**
151
+ * Merge optional `gates.postures` / `gates.alwaysBreakOn` over DEFAULT_POSTURES.
152
+ * Missing config → defaults. Unknown posture / invalid threshold → locked.
153
+ * destroy/auth stay locked even if config tries to relax them.
154
+ *
155
+ * @param {unknown} config Parsed spine-config (or partial with `gates`)
156
+ * @returns {GatePostureConfig}
157
+ */
158
+ export function resolveGatePostureConfig(config) {
159
+ const { overlay, alwaysBreakOnRaw } = extractPostureSection(config);
160
+ const categories = cloneDefaults();
161
+
162
+ if (overlay) {
163
+ for (const category of GATE_CATEGORIES) {
164
+ if (!(category in overlay)) {
165
+ continue;
166
+ }
167
+ if (LOCKED_SET.has(category)) {
168
+ categories[category] = lockedEntry();
169
+ continue;
170
+ }
171
+ categories[category] = normalizeCategoryEntry(overlay[category]);
172
+ }
173
+ }
174
+
175
+ // Hard fail-closed: locked categories never leave locked.
176
+ for (const category of LOCKED_CATEGORIES) {
177
+ categories[category] = lockedEntry();
178
+ }
179
+
180
+ /** @type {Record<GateCategory, Readonly<CategoryPostureDefault>>} */
181
+ const frozenCategories = /** @type {Record<GateCategory, Readonly<CategoryPostureDefault>>} */ ({});
182
+ for (const category of GATE_CATEGORIES) {
183
+ frozenCategories[category] = Object.freeze(categories[category]);
184
+ }
185
+
186
+ return Object.freeze({
187
+ categories: Object.freeze(frozenCategories),
188
+ alwaysBreakOn: Object.freeze(normalizeAlwaysBreakOn(alwaysBreakOnRaw)),
189
+ });
190
+ }
@@ -5,12 +5,15 @@ import path from "node:path";
5
5
  import { applyEnvOverrides } from "./env-overrides.mjs";
6
6
  import { applyConfigDefaults } from "./merge-defaults.mjs";
7
7
  import { validateContractConfig } from "./contract.mjs";
8
+ import { resolveGatePostureConfig } from "./gate-posture-config.mjs";
8
9
  import { validateWorkerBackendConfig } from "./worker-backend.mjs";
9
10
  import { validateWorkerContextConfig } from "./worker-context.mjs";
10
11
  import { validateWorkerLaunchScriptConfig } from "./worker-launch-script.mjs";
11
12
  import { validateWorktreeSetupHookConfig } from "./worktree-setup-hook.mjs";
12
13
  import { validateOrchestratorConfig } from "./spine-config-schema.mjs";
13
14
 
15
+ export { resolveGatePostureConfig } from "./gate-posture-config.mjs";
16
+
14
17
  const REQUIRED_TOP_LEVEL = [
15
18
  "configVersion",
16
19
  "project",
@@ -107,10 +110,14 @@ export function loadSpineConfig(projectRoot) {
107
110
  };
108
111
  }
109
112
 
113
+ // Soft attach: posture merge fails closed inside the helper and never rejects load.
114
+ const gatePostureConfig = resolveGatePostureConfig(envResult.config);
115
+
110
116
  return {
111
117
  configPath: fileResult.configPath,
112
118
  config: envResult.config,
113
119
  fileConfig: config,
120
+ gatePostureConfig,
114
121
  sources: envResult.sources,
115
122
  envVars: envResult.envVars,
116
123
  error: null,