@opengsd/gsd-pi 1.3.0-dev.72e3af2a → 1.3.0-dev.738160b9

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 (213) hide show
  1. package/README.md +1 -1
  2. package/dist/cli.js +17 -4
  3. package/dist/headless-answers.js +1 -1
  4. package/dist/headless-ui.d.ts +1 -1
  5. package/dist/headless-ui.js +1 -1
  6. package/dist/headless.js +1 -1
  7. package/dist/resources/.managed-resources-content-hash +1 -1
  8. package/dist/resources/extensions/claude-code-cli/models.js +9 -0
  9. package/dist/resources/extensions/gsd/auto/session.js +8 -0
  10. package/dist/resources/extensions/gsd/auto/verification-retry-policy.js +1 -1
  11. package/dist/resources/extensions/gsd/auto-budget.js +5 -1
  12. package/dist/resources/extensions/gsd/auto-dispatch.js +14 -1
  13. package/dist/resources/extensions/gsd/auto-post-unit.js +1 -1
  14. package/dist/resources/extensions/gsd/auto-prompts.js +3 -3
  15. package/dist/resources/extensions/gsd/auto-recovery.js +3 -3
  16. package/dist/resources/extensions/gsd/auto-verification.js +5 -2
  17. package/dist/resources/extensions/gsd/auto.js +19 -10
  18. package/dist/resources/extensions/gsd/bootstrap/core-session-tools.js +1 -0
  19. package/dist/resources/extensions/gsd/bootstrap/db-tools.js +5 -4
  20. package/dist/resources/extensions/gsd/bootstrap/register-extension.js +2 -0
  21. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +31 -2
  22. package/dist/resources/extensions/gsd/bootstrap/tool-call-loop-guard.js +32 -4
  23. package/dist/resources/extensions/gsd/closeout-consistency-gate.js +3 -1
  24. package/dist/resources/extensions/gsd/commands/handlers/auto.js +1 -1
  25. package/dist/resources/extensions/gsd/commands-prefs-wizard.js +1 -1
  26. package/dist/resources/extensions/gsd/commands-usage.js +1 -1
  27. package/dist/resources/extensions/gsd/db/engine.js +4 -1
  28. package/dist/resources/extensions/gsd/db-memory-fts-schema.js +34 -0
  29. package/dist/resources/extensions/gsd/delegation-policy.js +1 -1
  30. package/dist/resources/extensions/gsd/docs/preferences-reference.md +2 -2
  31. package/dist/resources/extensions/gsd/doctor-engine-checks.js +192 -4
  32. package/dist/resources/extensions/gsd/doctor-runtime-checks.js +3 -0
  33. package/dist/resources/extensions/gsd/exec-sandbox.js +8 -2
  34. package/dist/resources/extensions/gsd/flat-phase-migration.js +45 -0
  35. package/dist/resources/extensions/gsd/git-service.js +57 -0
  36. package/dist/resources/extensions/gsd/gitignore.js +4 -1
  37. package/dist/resources/extensions/gsd/json-persistence.js +20 -0
  38. package/dist/resources/extensions/gsd/knowledge-backfill.js +18 -11
  39. package/dist/resources/extensions/gsd/memory-backfill.js +28 -21
  40. package/dist/resources/extensions/gsd/metrics.js +4 -4
  41. package/dist/resources/extensions/gsd/migrate-external.js +11 -2
  42. package/dist/resources/extensions/gsd/orphan-stash-audit.js +92 -16
  43. package/dist/resources/extensions/gsd/prompts/plan-milestone.md +2 -2
  44. package/dist/resources/extensions/gsd/prompts/plan-slice.md +3 -3
  45. package/dist/resources/extensions/gsd/root-write-leak-guard.js +7 -1
  46. package/dist/resources/extensions/gsd/safety/destructive-guard.js +8 -1
  47. package/dist/resources/extensions/gsd/safety/evidence-collector.js +16 -1
  48. package/dist/resources/extensions/gsd/safety/file-change-validator.js +5 -2
  49. package/dist/resources/extensions/gsd/tools/complete-milestone.js +4 -2
  50. package/dist/resources/extensions/gsd/tools/plan-slice.js +59 -44
  51. package/dist/resources/extensions/gsd/tools/plan-task.js +28 -5
  52. package/dist/resources/extensions/gsd/undo.js +43 -6
  53. package/dist/resources/extensions/gsd/unit-registry.js +4 -2
  54. package/dist/resources/extensions/gsd/verification-gate.js +7 -0
  55. package/dist/resources/extensions/gsd/workflow-manifest.js +126 -13
  56. package/dist/tsconfig.extensions.tsbuildinfo +1 -1
  57. package/dist/web/standalone/.next/BUILD_ID +1 -1
  58. package/dist/web/standalone/.next/app-path-routes-manifest.json +9 -9
  59. package/dist/web/standalone/.next/build-manifest.json +2 -2
  60. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  61. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  62. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  63. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  64. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  65. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  66. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  67. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  68. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  69. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  70. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  71. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  72. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  73. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  74. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  75. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  76. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  77. package/dist/web/standalone/.next/server/app/index.html +1 -1
  78. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  79. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  80. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  81. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  82. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  83. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  84. package/dist/web/standalone/.next/server/app-paths-manifest.json +9 -9
  85. package/dist/web/standalone/.next/server/chunks/8357.js +1 -1
  86. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  87. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  88. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  89. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  90. package/package.json +1 -1
  91. package/packages/cloud-mcp-gateway/package.json +2 -2
  92. package/packages/contracts/package.json +1 -1
  93. package/packages/daemon/package.json +4 -4
  94. package/packages/gsd-agent-core/package.json +5 -5
  95. package/packages/gsd-agent-modes/dist/modes/rpc/rpc-client.d.ts +2 -0
  96. package/packages/gsd-agent-modes/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  97. package/packages/gsd-agent-modes/dist/modes/rpc/rpc-client.js +23 -5
  98. package/packages/gsd-agent-modes/dist/modes/rpc/rpc-client.js.map +1 -1
  99. package/packages/gsd-agent-modes/package.json +7 -7
  100. package/packages/mcp-server/dist/workflow-tools.js +1 -1
  101. package/packages/mcp-server/dist/workflow-tools.js.map +1 -1
  102. package/packages/mcp-server/package.json +4 -4
  103. package/packages/native/package.json +1 -1
  104. package/packages/pi-agent-core/dist/agent-loop.js +3 -2
  105. package/packages/pi-agent-core/dist/agent-loop.js.map +1 -1
  106. package/packages/pi-agent-core/package.json +1 -1
  107. package/packages/pi-ai/dist/models.generated.d.ts +46 -0
  108. package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
  109. package/packages/pi-ai/dist/models.generated.js +38 -0
  110. package/packages/pi-ai/dist/models.generated.js.map +1 -1
  111. package/packages/pi-ai/package.json +1 -1
  112. package/packages/pi-coding-agent/package.json +7 -7
  113. package/packages/pi-tui/package.json +2 -2
  114. package/packages/rpc-client/package.json +2 -2
  115. package/pkg/package.json +1 -1
  116. package/src/resources/extensions/claude-code-cli/models.ts +9 -0
  117. package/src/resources/extensions/claude-code-cli/tests/index.test.ts +14 -0
  118. package/src/resources/extensions/gsd/auto/loop-deps.ts +1 -0
  119. package/src/resources/extensions/gsd/auto/session.ts +9 -0
  120. package/src/resources/extensions/gsd/auto/verification-retry-policy.ts +3 -1
  121. package/src/resources/extensions/gsd/auto-budget.ts +12 -1
  122. package/src/resources/extensions/gsd/auto-dispatch.ts +14 -1
  123. package/src/resources/extensions/gsd/auto-post-unit.ts +1 -1
  124. package/src/resources/extensions/gsd/auto-prompts.ts +3 -3
  125. package/src/resources/extensions/gsd/auto-recovery.ts +3 -3
  126. package/src/resources/extensions/gsd/auto-verification.ts +5 -1
  127. package/src/resources/extensions/gsd/auto.ts +23 -8
  128. package/src/resources/extensions/gsd/bootstrap/core-session-tools.ts +1 -0
  129. package/src/resources/extensions/gsd/bootstrap/db-tools.ts +5 -4
  130. package/src/resources/extensions/gsd/bootstrap/register-extension.ts +2 -0
  131. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +41 -2
  132. package/src/resources/extensions/gsd/bootstrap/tool-call-loop-guard.ts +34 -4
  133. package/src/resources/extensions/gsd/closeout-consistency-gate.ts +2 -1
  134. package/src/resources/extensions/gsd/commands/handlers/auto.ts +1 -1
  135. package/src/resources/extensions/gsd/commands-prefs-wizard.ts +1 -1
  136. package/src/resources/extensions/gsd/commands-usage.ts +1 -1
  137. package/src/resources/extensions/gsd/db/engine.ts +8 -1
  138. package/src/resources/extensions/gsd/db-memory-fts-schema.ts +45 -0
  139. package/src/resources/extensions/gsd/delegation-policy.ts +1 -1
  140. package/src/resources/extensions/gsd/docs/preferences-reference.md +2 -2
  141. package/src/resources/extensions/gsd/doctor-engine-checks.ts +233 -5
  142. package/src/resources/extensions/gsd/doctor-runtime-checks.ts +3 -0
  143. package/src/resources/extensions/gsd/doctor-types.ts +3 -0
  144. package/src/resources/extensions/gsd/exec-sandbox.ts +7 -2
  145. package/src/resources/extensions/gsd/flat-phase-migration.ts +44 -0
  146. package/src/resources/extensions/gsd/git-service.ts +64 -0
  147. package/src/resources/extensions/gsd/gitignore.ts +4 -1
  148. package/src/resources/extensions/gsd/json-persistence.ts +20 -0
  149. package/src/resources/extensions/gsd/knowledge-backfill.ts +16 -10
  150. package/src/resources/extensions/gsd/memory-backfill.ts +26 -20
  151. package/src/resources/extensions/gsd/metrics.ts +4 -4
  152. package/src/resources/extensions/gsd/migrate-external.ts +8 -2
  153. package/src/resources/extensions/gsd/orphan-stash-audit.ts +108 -20
  154. package/src/resources/extensions/gsd/prompts/plan-milestone.md +2 -2
  155. package/src/resources/extensions/gsd/prompts/plan-slice.md +3 -3
  156. package/src/resources/extensions/gsd/root-write-leak-guard.ts +13 -1
  157. package/src/resources/extensions/gsd/safety/destructive-guard.ts +9 -1
  158. package/src/resources/extensions/gsd/safety/evidence-collector.ts +16 -1
  159. package/src/resources/extensions/gsd/safety/file-change-validator.ts +8 -2
  160. package/src/resources/extensions/gsd/tests/artifact-retry-cap.test.ts +4 -3
  161. package/src/resources/extensions/gsd/tests/auto-budget-alerts.test.ts +14 -5
  162. package/src/resources/extensions/gsd/tests/auto-pause-double-entry-guard.test.ts +36 -0
  163. package/src/resources/extensions/gsd/tests/auto-paused-ui-cleanup.test.ts +182 -4
  164. package/src/resources/extensions/gsd/tests/auto-recovery.test.ts +35 -0
  165. package/src/resources/extensions/gsd/tests/auto-retry-mcp-churn-fixes.test.ts +30 -0
  166. package/src/resources/extensions/gsd/tests/complete-milestone.test.ts +28 -0
  167. package/src/resources/extensions/gsd/tests/complete-slice-reopen-handoff.test.ts +69 -0
  168. package/src/resources/extensions/gsd/tests/db-memory-fts-schema.test.ts +58 -3
  169. package/src/resources/extensions/gsd/tests/decisions-projection-from-memories.test.ts +51 -0
  170. package/src/resources/extensions/gsd/tests/destructive-guard.test.ts +44 -0
  171. package/src/resources/extensions/gsd/tests/dispatch-missing-task-plans.test.ts +143 -0
  172. package/src/resources/extensions/gsd/tests/doctor-scope-db-unavailable.test.ts +239 -2
  173. package/src/resources/extensions/gsd/tests/exec-sandbox.test.ts +16 -0
  174. package/src/resources/extensions/gsd/tests/file-change-validator.test.ts +40 -0
  175. package/src/resources/extensions/gsd/tests/flat-phase-migration.test.ts +37 -2
  176. package/src/resources/extensions/gsd/tests/gitignore-bg-shell-runtime.test.ts +4 -0
  177. package/src/resources/extensions/gsd/tests/integration/git-service.test.ts +11 -2
  178. package/src/resources/extensions/gsd/tests/json-persistence-atomic.test.ts +47 -0
  179. package/src/resources/extensions/gsd/tests/knowledge-backfill-projection.test.ts +35 -0
  180. package/src/resources/extensions/gsd/tests/memory-store.test.ts +48 -0
  181. package/src/resources/extensions/gsd/tests/merge-closeout-consistency-gate.test.ts +31 -1
  182. package/src/resources/extensions/gsd/tests/metrics-prune-cache-invalidation.test.ts +6 -0
  183. package/src/resources/extensions/gsd/tests/metrics.test.ts +1 -0
  184. package/src/resources/extensions/gsd/tests/migrate-external-worktree.test.ts +57 -0
  185. package/src/resources/extensions/gsd/tests/orphan-stash-audit.test.ts +91 -7
  186. package/src/resources/extensions/gsd/tests/parsers-legacy-importers.test.ts +2 -0
  187. package/src/resources/extensions/gsd/tests/plan-slice-prompt.test.ts +10 -0
  188. package/src/resources/extensions/gsd/tests/plan-slice.test.ts +103 -4
  189. package/src/resources/extensions/gsd/tests/plan-task.test.ts +44 -1
  190. package/src/resources/extensions/gsd/tests/post-mutation-hook.test.ts +4 -2
  191. package/src/resources/extensions/gsd/tests/prompt-budget-enforcement.test.ts +58 -0
  192. package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +8 -6
  193. package/src/resources/extensions/gsd/tests/register-hooks-loop-guard-auto.test.ts +103 -0
  194. package/src/resources/extensions/gsd/tests/root-write-leak-guard.test.ts +20 -1
  195. package/src/resources/extensions/gsd/tests/runtime-invariant-modules.test.ts +8 -3
  196. package/src/resources/extensions/gsd/tests/tool-call-loop-guard.test.ts +62 -5
  197. package/src/resources/extensions/gsd/tests/tool-param-optionality.test.ts +6 -3
  198. package/src/resources/extensions/gsd/tests/undo.test.ts +74 -0
  199. package/src/resources/extensions/gsd/tests/uok-gitops-turn-action.test.ts +38 -0
  200. package/src/resources/extensions/gsd/tests/verification-retry-policy.test.ts +69 -4
  201. package/src/resources/extensions/gsd/tests/workflow-manifest.test.ts +96 -15
  202. package/src/resources/extensions/gsd/tests/workflow-mcp-preflight.test.ts +1 -0
  203. package/src/resources/extensions/gsd/tests/workflow-mcp.test.ts +8 -4
  204. package/src/resources/extensions/gsd/tests/workflow-tool-executors.test.ts +6 -5
  205. package/src/resources/extensions/gsd/tools/complete-milestone.ts +3 -2
  206. package/src/resources/extensions/gsd/tools/plan-slice.ts +60 -47
  207. package/src/resources/extensions/gsd/tools/plan-task.ts +30 -5
  208. package/src/resources/extensions/gsd/undo.ts +47 -6
  209. package/src/resources/extensions/gsd/unit-registry.ts +4 -2
  210. package/src/resources/extensions/gsd/verification-gate.ts +8 -0
  211. package/src/resources/extensions/gsd/workflow-manifest.ts +141 -7
  212. /package/dist/web/standalone/.next/static/{O7xDYXO0r4zFhIzY1hrWV → 2A_WqqCS-EoYZxN9_v3d2}/_buildManifest.js +0 -0
  213. /package/dist/web/standalone/.next/static/{O7xDYXO0r4zFhIzY1hrWV → 2A_WqqCS-EoYZxN9_v3d2}/_ssgManifest.js +0 -0
@@ -8,6 +8,7 @@ import {
8
8
  getNewBudgetAlertLevel,
9
9
  resolveCompactionThresholdPercent,
10
10
  shouldRerootStepSessionForContext,
11
+ shouldWarnStepSessionForContext,
11
12
  } from "../auto.js";
12
13
  import {
13
14
  getUnitCostSpikeAction,
@@ -73,11 +74,19 @@ test("resolveCompactionThresholdPercent defaults to 60 and accepts ratio prefs",
73
74
  assert.equal(resolveCompactionThresholdPercent(0.4), 60);
74
75
  });
75
76
 
76
- test("shouldRerootStepSessionForContext uses compaction threshold pref", () => {
77
- assert.equal(shouldRerootStepSessionForContext(59.9, 0.6), false);
78
- assert.equal(shouldRerootStepSessionForContext(60, 0.6), true);
79
- assert.equal(shouldRerootStepSessionForContext(273.8, 0.6), true);
80
- assert.equal(shouldRerootStepSessionForContext(undefined, 0.6), false);
77
+ test("shouldWarnStepSessionForContext uses compaction threshold pref", () => {
78
+ assert.equal(shouldWarnStepSessionForContext(59.9, 0.6), false);
79
+ assert.equal(shouldWarnStepSessionForContext(60, 0.6), true);
80
+ assert.equal(shouldWarnStepSessionForContext(72, 0.6), true);
81
+ assert.equal(shouldWarnStepSessionForContext(undefined, 0.6), false);
82
+ });
83
+
84
+ test("shouldRerootStepSessionForContext only fires at the hard context threshold", () => {
85
+ assert.equal(shouldRerootStepSessionForContext(72), false);
86
+ assert.equal(shouldRerootStepSessionForContext(89.9), false);
87
+ assert.equal(shouldRerootStepSessionForContext(90), true);
88
+ assert.equal(shouldRerootStepSessionForContext(273.8), true);
89
+ assert.equal(shouldRerootStepSessionForContext(undefined), false);
81
90
  });
82
91
 
83
92
  test("resolveUnitCostSpikeMultiplier disables the spike pause for burn-max", () => {
@@ -14,6 +14,7 @@ import { capturePauseAutoUnitIdentity, pauseAuto, isAutoActive } from "../auto.t
14
14
  import { _resetPendingResolve, _setCurrentResolve } from "../auto/resolve.ts";
15
15
  import { autoSession } from "../auto-runtime-state.ts";
16
16
  import { _isPauseOriginCancelledResult } from "../auto/phase-helpers.ts";
17
+ import { handleAutoCommand } from "../commands/handlers/auto.ts";
17
18
 
18
19
  test("pauseAuto sets s.active = false synchronously before first await (blocks concurrent re-entry)", async () => {
19
20
  const base = mkdtempSync(join(tmpdir(), "gsd-double-pause-guard-"));
@@ -80,6 +81,41 @@ test("pauseAuto marks paused and cancels the in-flight unit before first await",
80
81
  }
81
82
  });
82
83
 
84
+ test("/gsd pause aborts the active pi turn before first await", async () => {
85
+ autoSession.reset();
86
+ autoSession.active = true;
87
+ let abortCount = 0;
88
+ const ctx = {
89
+ hasUI: false,
90
+ isIdle: () => false,
91
+ abort: () => {
92
+ abortCount += 1;
93
+ },
94
+ sessionManager: {
95
+ getSessionFile: () => null,
96
+ },
97
+ ui: {
98
+ setStatus() {},
99
+ setWidget() {},
100
+ notify() {},
101
+ },
102
+ } as any;
103
+
104
+ try {
105
+ const pausePromise = handleAutoCommand("pause", ctx, {} as any);
106
+
107
+ assert.equal(
108
+ abortCount,
109
+ 1,
110
+ "pauseAuto must abort the active pi turn before reporting auto-mode paused",
111
+ );
112
+
113
+ await pausePromise.catch(() => {});
114
+ } finally {
115
+ autoSession.reset();
116
+ }
117
+ });
118
+
83
119
  test("transient-abort errorContext is not classified as pause-origin cancellation", () => {
84
120
  // The phases.ts fix passes unitResult.errorContext to pauseAuto for transient aborts.
85
121
  // _isPauseOriginCancelledResult must return false when errorContext is present, ensuring
@@ -3,13 +3,14 @@
3
3
  import { test } from "node:test";
4
4
  import assert from "node:assert/strict";
5
5
  import { execFileSync } from "node:child_process";
6
- import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
6
+ import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
7
7
  import { tmpdir } from "node:os";
8
8
  import { join, dirname } from "node:path";
9
9
 
10
10
  import {
11
11
  anchorProcessCwdForAutoResume,
12
12
  cleanupAfterLoopExit,
13
+ maybeRerootStepSessionForHighContext,
13
14
  pauseAuto,
14
15
  rerootCommandSession,
15
16
  stopAuto,
@@ -478,8 +479,8 @@ test("cleanupAfterLoopExit preserves step-mode surface and worktree session afte
478
479
  }
479
480
  });
480
481
 
481
- test("cleanupAfterLoopExit re-roots step-mode session when context exceeds compaction threshold", async (t) => {
482
- const base = mkdtempSync(join(tmpdir(), "gsd-step-reroot-"));
482
+ test("cleanupAfterLoopExit warns but does not re-root step-mode session at soft compaction threshold", async (t) => {
483
+ const base = mkdtempSync(join(tmpdir(), "gsd-step-soft-context-"));
483
484
  const worktree = join(base, ".gsd", "worktrees", "M001");
484
485
  const previousCwd = process.cwd();
485
486
  const newSessionWorkspaces: string[] = [];
@@ -515,7 +516,59 @@ test("cleanupAfterLoopExit re-roots step-mode session when context exceeds compa
515
516
  },
516
517
  } as any);
517
518
 
518
- assert.deepEqual(newSessionWorkspaces, [worktree], "hot step-mode cleanup should re-root in the active worktree");
519
+ assert.deepEqual(newSessionWorkspaces, [], "soft context pressure must not force a new command session");
520
+ assert.equal(
521
+ notifyMessages.some((msg) => msg.includes("context at 72.0%") && msg.includes("Use /compact")),
522
+ true,
523
+ "soft threshold should warn with a non-disruptive compaction prompt",
524
+ );
525
+ assert.equal(autoSession.basePath, worktree);
526
+ assert.equal(realpathSync(process.cwd()), realpathSync(worktree));
527
+ } finally {
528
+ autoSession.reset();
529
+ process.chdir(previousCwd);
530
+ rmSync(base, { recursive: true, force: true });
531
+ }
532
+ });
533
+
534
+ test("cleanupAfterLoopExit re-roots step-mode session at hard context threshold", async (t) => {
535
+ const base = mkdtempSync(join(tmpdir(), "gsd-step-reroot-"));
536
+ const worktree = join(base, ".gsd", "worktrees", "M001");
537
+ const previousCwd = process.cwd();
538
+ const newSessionWorkspaces: string[] = [];
539
+ const notifyMessages: string[] = [];
540
+
541
+ t.mock.method(WorktreeLifecycle.prototype, "restoreToProjectRoot", function () {});
542
+
543
+ mkdirSync(worktree, { recursive: true });
544
+ process.chdir(worktree);
545
+ autoSession.reset();
546
+ autoSession.active = true;
547
+ autoSession.paused = false;
548
+ autoSession.stepMode = true;
549
+ autoSession.preserveStepSurfaceAfterLoopExit = true;
550
+ autoSession.basePath = worktree;
551
+ autoSession.originalBasePath = base;
552
+ autoSession.cmdCtx = {
553
+ getContextUsage: () => ({ percent: 92, tokens: 920_000, contextWindow: 1_000_000 }),
554
+ newSession: async ({ workspaceRoot }: { workspaceRoot: string }) => {
555
+ newSessionWorkspaces.push(workspaceRoot);
556
+ return { cancelled: false };
557
+ },
558
+ } as any;
559
+
560
+ try {
561
+ await cleanupAfterLoopExit({
562
+ hasUI: true,
563
+ ui: {
564
+ setStatus: () => {},
565
+ setWidget: () => {},
566
+ setHeader: () => {},
567
+ notify: (_msg: string) => notifyMessages.push(_msg),
568
+ },
569
+ } as any);
570
+
571
+ assert.deepEqual(newSessionWorkspaces, [worktree], "critical context pressure should re-root in the active worktree");
519
572
  assert.equal(
520
573
  notifyMessages.some((msg) => msg.includes("Fresh session ready for /gsd next")),
521
574
  true,
@@ -530,6 +583,131 @@ test("cleanupAfterLoopExit re-roots step-mode session when context exceeds compa
530
583
  }
531
584
  });
532
585
 
586
+ test("maybeRerootStepSessionForHighContext re-roots at the hard threshold even when the soft pref is set above it", async () => {
587
+ // Regression: the hard 90% re-root must not be gated behind the configurable
588
+ // soft warn threshold, which is allowed up to 0.95. At 92% usage with a 0.95
589
+ // soft pref the soft warn predicate is false, but the session must still
590
+ // re-root because usage crossed the fixed hard boundary.
591
+ const dir = mkdtempSync(join(tmpdir(), "gsd-hard-reroot-high-soft-"));
592
+ const project = join(dir, "project");
593
+ const gsdHomeDir = join(dir, "home");
594
+ const previousCwd = process.cwd();
595
+ const previousGsdHome = process.env.GSD_HOME;
596
+
597
+ mkdirSync(join(project, ".gsd"), { recursive: true });
598
+ mkdirSync(gsdHomeDir, { recursive: true });
599
+ writeFileSync(
600
+ join(project, ".gsd", "PREFERENCES.md"),
601
+ ["---", "version: 1", "context_management:", " compaction_threshold_percent: 0.95", "---", ""].join("\n"),
602
+ "utf-8",
603
+ );
604
+
605
+ process.env.GSD_HOME = gsdHomeDir;
606
+ process.chdir(project);
607
+
608
+ const newSessionWorkspaces: string[] = [];
609
+ const notifyMessages: string[] = [];
610
+ const cmdCtx = {
611
+ getContextUsage: () => ({ percent: 92, tokens: 920_000, contextWindow: 1_000_000 }),
612
+ newSession: async ({ workspaceRoot }: { workspaceRoot: string }) => {
613
+ newSessionWorkspaces.push(workspaceRoot);
614
+ return { cancelled: false };
615
+ },
616
+ } as any;
617
+ const ctx = { ui: { notify: (msg: string) => notifyMessages.push(msg) } } as any;
618
+
619
+ try {
620
+ const result = await maybeRerootStepSessionForHighContext(ctx, project, cmdCtx, project);
621
+
622
+ assert.equal(result.rerooted, true, "92% usage must re-root even when the soft pref is 95%");
623
+ assert.deepEqual(newSessionWorkspaces, [project], "hard context pressure must re-root the command session");
624
+ const rerootMessage = notifyMessages.find((msg) => msg.includes("Fresh session ready for /gsd next"));
625
+ assert.ok(rerootMessage, "hard re-root should notify the user");
626
+ // The re-root was triggered by the 90% hard limit, so the notification must
627
+ // cite that limit and not the configured 95% soft compaction threshold.
628
+ assert.match(
629
+ rerootMessage,
630
+ /hard threshold: 90%/,
631
+ "hard re-root notification must cite the 90% hard threshold",
632
+ );
633
+ assert.equal(
634
+ rerootMessage.includes("95%"),
635
+ false,
636
+ "hard re-root notification must not cite the configured soft compaction threshold",
637
+ );
638
+ } finally {
639
+ process.chdir(previousCwd);
640
+ if (previousGsdHome === undefined) delete process.env.GSD_HOME;
641
+ else process.env.GSD_HOME = previousGsdHome;
642
+ rmSync(dir, { recursive: true, force: true });
643
+ }
644
+ });
645
+
646
+ test("maybeRerootStepSessionForHighContext writes a compaction snapshot only at the hard boundary", async () => {
647
+ // Regression: the snapshot is reserved for the hard re-root. Soft-only
648
+ // pressure must warn without writing a compaction snapshot.
649
+ const dir = mkdtempSync(join(tmpdir(), "gsd-soft-no-snapshot-"));
650
+ const project = join(dir, "project");
651
+ const gsdHomeDir = join(dir, "home");
652
+ const previousCwd = process.cwd();
653
+ const previousGsdHome = process.env.GSD_HOME;
654
+
655
+ mkdirSync(join(project, ".gsd"), { recursive: true });
656
+ mkdirSync(gsdHomeDir, { recursive: true });
657
+ writeFileSync(
658
+ join(project, ".gsd", "PREFERENCES.md"),
659
+ ["---", "version: 1", "context_management:", " compaction_threshold_percent: 0.6", "---", ""].join("\n"),
660
+ "utf-8",
661
+ );
662
+
663
+ process.env.GSD_HOME = gsdHomeDir;
664
+ process.chdir(project);
665
+
666
+ const newSessionWorkspaces: string[] = [];
667
+ const notifyMessages: string[] = [];
668
+ let contextPercent = 72;
669
+ const cmdCtx = {
670
+ getContextUsage: () => ({ percent: contextPercent, tokens: contextPercent * 10_000, contextWindow: 1_000_000 }),
671
+ newSession: async ({ workspaceRoot }: { workspaceRoot: string }) => {
672
+ newSessionWorkspaces.push(workspaceRoot);
673
+ return { cancelled: false };
674
+ },
675
+ } as any;
676
+ const ctx = { ui: { notify: (msg: string) => notifyMessages.push(msg) } } as any;
677
+ const snapshotPath = join(project, ".gsd", "last-snapshot.md");
678
+
679
+ try {
680
+ const softResult = await maybeRerootStepSessionForHighContext(ctx, project, cmdCtx, project);
681
+ assert.equal(softResult.rerooted, false, "soft pressure must not re-root");
682
+ assert.deepEqual(newSessionWorkspaces, [], "soft pressure must not open a new session");
683
+ assert.equal(
684
+ existsSync(snapshotPath),
685
+ false,
686
+ "soft-only pressure must not write a compaction snapshot",
687
+ );
688
+ assert.equal(
689
+ notifyMessages.some((msg) => msg.includes("context at 72.0%") && msg.includes("Use /compact")),
690
+ true,
691
+ "soft threshold should warn with a non-disruptive compaction prompt",
692
+ );
693
+
694
+ contextPercent = 92;
695
+ const hardResult = await maybeRerootStepSessionForHighContext(ctx, project, cmdCtx, project);
696
+ assert.equal(hardResult.rerooted, true, "hard pressure must re-root");
697
+ assert.deepEqual(newSessionWorkspaces, [project], "hard pressure should re-root once");
698
+ assert.equal(
699
+ existsSync(snapshotPath),
700
+ true,
701
+ "hard boundary must write the compaction snapshot",
702
+ );
703
+ } finally {
704
+ process.chdir(previousCwd);
705
+ if (previousGsdHome === undefined) delete process.env.GSD_HOME;
706
+ else process.env.GSD_HOME = previousGsdHome;
707
+ rmSync(dir, { recursive: true, force: true });
708
+ }
709
+ });
710
+
533
711
  test("cleanupAfterLoopExit restores project root through lifecycle and preserves chdir", async (t) => {
534
712
  const base = mkdtempSync(join(tmpdir(), "gsd-cleanup-lifecycle-"));
535
713
  const worktree = join(base, ".gsd", "worktrees", "M001");
@@ -1917,6 +1917,41 @@ test("#57: writeReactiveExecuteBlocker reconciles batch task statuses and append
1917
1917
  }
1918
1918
  });
1919
1919
 
1920
+ test("#1088: writeReactiveExecuteBlocker preserves deferred batch task statuses", () => {
1921
+ const base = makeTmpBase();
1922
+ try {
1923
+ openDatabase(join(base, ".gsd", "gsd.db"));
1924
+ insertMilestone({ id: "M001", title: "Milestone", status: "active" });
1925
+ insertSlice({ id: "S01", milestoneId: "M001", title: "Slice", status: "pending" });
1926
+ insertTask({ id: "T01", milestoneId: "M001", sliceId: "S01", title: "One", status: "deferred" });
1927
+ insertTask({ id: "T02", milestoneId: "M001", sliceId: "S01", title: "Two", status: "deferred" });
1928
+ writeFileSync(
1929
+ join(base, ".gsd", "milestones", "M001", "slices", "S01", "tasks", "T01-SUMMARY.md"),
1930
+ "# T01 Summary\n",
1931
+ "utf-8",
1932
+ );
1933
+
1934
+ const recovery = writeReactiveExecuteBlocker(
1935
+ "M001/S01/reactive+T01,T02",
1936
+ base,
1937
+ "verification retries exhausted",
1938
+ );
1939
+
1940
+ assert.ok(recovery, "recovery should run with DB available");
1941
+ assert.deepEqual(recovery!.completedTaskIds, []);
1942
+ assert.deepEqual(recovery!.skippedTaskIds, []);
1943
+ assert.deepEqual(recovery!.unchangedTaskIds, ["T01", "T02"]);
1944
+ assert.equal(getTask("M001", "S01", "T01")?.status, "deferred");
1945
+ assert.equal(getTask("M001", "S01", "T02")?.status, "deferred");
1946
+
1947
+ const events = readEvents(join(base, ".gsd", "event-log.jsonl"));
1948
+ assert.equal(events.some((e) => e.params.taskId === "T01" || e.params.taskId === "T02"), false);
1949
+ } finally {
1950
+ closeDatabase();
1951
+ cleanup(base);
1952
+ }
1953
+ });
1954
+
1920
1955
  test("#4414: verifyExpectedArtifact parallel-research succeeds when all research-ready slices have RESEARCH", () => {
1921
1956
  const base = makeTmpBase();
1922
1957
  try {
@@ -15,15 +15,21 @@
15
15
 
16
16
  import { describe, it, beforeEach } from "node:test";
17
17
  import assert from "node:assert/strict";
18
+ import { mkdtempSync, rmSync, statSync } from "node:fs";
19
+ import { tmpdir } from "node:os";
20
+ import { join } from "node:path";
18
21
 
19
22
  import {
20
23
  resetEvidence,
21
24
  getEvidence,
22
25
  recordToolCall,
23
26
  recordToolResult,
27
+ saveEvidenceToDisk,
24
28
  type BashEvidence,
25
29
  } from "../safety/evidence-collector.js";
26
30
 
31
+ const waitForMtimeTick = () => new Promise(resolve => setTimeout(resolve, 25));
32
+
27
33
  describe("evidence-collector: toolCallId-based matching (A-3)", () => {
28
34
  beforeEach(() => {
29
35
  resetEvidence();
@@ -119,4 +125,28 @@ describe("evidence-collector: toolCallId-based matching (A-3)", () => {
119
125
  assert.equal(entries[1].kind, "bash");
120
126
  assert.equal(entries[1].command, "rg TODO");
121
127
  });
128
+
129
+ it("skips byte-identical evidence file rewrites", async (t) => {
130
+ const base = mkdtempSync(join(tmpdir(), "gsd-evidence-dedupe-"));
131
+ t.after(() => rmSync(base, { recursive: true, force: true }));
132
+
133
+ recordToolCall("tc-bash-1", "bash", { command: "pnpm test" });
134
+ saveEvidenceToDisk(base, "M001", "S01", "T03");
135
+
136
+ const evidenceFile = join(base, ".gsd", "safety", "evidence-M001-S01-T03.json");
137
+ const firstMtime = statSync(evidenceFile, { bigint: true }).mtimeNs;
138
+
139
+ await waitForMtimeTick();
140
+ saveEvidenceToDisk(base, "M001", "S01", "T03");
141
+ const secondMtime = statSync(evidenceFile, { bigint: true }).mtimeNs;
142
+
143
+ assert.equal(secondMtime, firstMtime, "unchanged evidence must not rewrite the evidence file");
144
+
145
+ await waitForMtimeTick();
146
+ recordToolResult("tc-bash-1", "bash", "Command exited with code 0\nok\n", false);
147
+ saveEvidenceToDisk(base, "M001", "S01", "T03");
148
+ const thirdMtime = statSync(evidenceFile, { bigint: true }).mtimeNs;
149
+
150
+ assert.ok(thirdMtime > secondMtime, "changed evidence must still be persisted");
151
+ });
122
152
  });
@@ -312,6 +312,34 @@ console.log('\n=== complete-milestone: incomplete slices block closeout ===');
312
312
  cleanup(dbPath);
313
313
  }
314
314
 
315
+ // ═══════════════════════════════════════════════════════════════════════════
316
+ // complete-milestone: deferred slices are inactive for closeout
317
+ // ═══════════════════════════════════════════════════════════════════════════
318
+
319
+ console.log('\n=== complete-milestone: deferred slices do not block closeout ===');
320
+ {
321
+ const dbPath = tempDbPath();
322
+ openDatabase(dbPath);
323
+ const { basePath } = createTempProject();
324
+ seedCompletedMilestone({ basePath, validationVerdict: 'pass' });
325
+ insertSlice({ id: 'S02', milestoneId: 'M001', title: 'Deferred Slice', status: 'deferred' });
326
+ insertTask({
327
+ id: 'T02',
328
+ sliceId: 'S02',
329
+ milestoneId: 'M001',
330
+ status: 'pending',
331
+ title: 'Deferred Slice Task',
332
+ });
333
+
334
+ const result = await handleCompleteMilestone(makeValidParams(), basePath);
335
+
336
+ assertTrue(!('error' in result), 'deferred slice should not block milestone completion');
337
+ assertEq(getMilestone('M001')!.status, 'complete', 'milestone should complete with deferred inactive slice');
338
+
339
+ cleanupDir(basePath);
340
+ cleanup(dbPath);
341
+ }
342
+
315
343
  // ═══════════════════════════════════════════════════════════════════════════
316
344
  // complete-milestone: deep task check (slice closed, task not)
317
345
  // ═══════════════════════════════════════════════════════════════════════════
@@ -7,6 +7,10 @@ import { join } from "node:path";
7
7
 
8
8
  import { postUnitPreVerification } from "../auto-post-unit.ts";
9
9
  import { AutoSession } from "../auto/session.ts";
10
+ import {
11
+ decideVerificationRetry,
12
+ hashVerificationFailureContext,
13
+ } from "../auto/verification-retry-policy.ts";
10
14
  import { cleanup, makeTempRepo } from "./test-utils.ts";
11
15
 
12
16
  function makePostUnitContext(base: string, s: AutoSession, notifications: string[]) {
@@ -227,3 +231,68 @@ test("complete-slice with gsd_replan_slice but no REPLAN artifact retries", asyn
227
231
  cleanup(base);
228
232
  }
229
233
  });
234
+
235
+ test("artifact retry context stays stable across attempts while notifications show attempt count", async () => {
236
+ const base = makeTempRepo("gsd-artifact-retry-stable-context-");
237
+ try {
238
+ mkdirSync(join(base, ".gsd", "milestones", "M001", "slices", "S01"), { recursive: true });
239
+
240
+ const s = new AutoSession();
241
+ s.active = true;
242
+ s.basePath = base;
243
+ s.currentUnit = { type: "complete-slice", id: "M001/S01", startedAt: Date.now() };
244
+
245
+ const notifications: string[] = [];
246
+ const pctx = makePostUnitContext(base, s, notifications);
247
+ const opts = {
248
+ skipSettleDelay: true,
249
+ skipWorktreeSync: true,
250
+ agentEndMessages: [
251
+ {
252
+ role: "toolResult",
253
+ toolName: "gsd_replan_slice",
254
+ isError: false,
255
+ content: "Slice replanned with reopened task T02.",
256
+ },
257
+ ],
258
+ };
259
+
260
+ assert.equal(await postUnitPreVerification(pctx, opts), "retry");
261
+ const firstRetry = s.pendingVerificationRetry;
262
+ assert.ok(firstRetry);
263
+ assert.equal(firstRetry.attempt, 1);
264
+ assert.doesNotMatch(firstRetry.failureContext, /\(attempt \d\/3\)\.$/);
265
+ const firstFailureHash = hashVerificationFailureContext(firstRetry.failureContext);
266
+
267
+ assert.equal(await postUnitPreVerification(pctx, opts), "retry");
268
+ const secondRetry = s.pendingVerificationRetry;
269
+ assert.ok(secondRetry);
270
+ assert.equal(secondRetry.attempt, 2);
271
+ assert.equal(secondRetry.failureContext, firstRetry.failureContext);
272
+ assert.equal(hashVerificationFailureContext(secondRetry.failureContext), firstFailureHash);
273
+ assert.deepEqual(
274
+ decideVerificationRetry({
275
+ unitType: s.currentUnit.type,
276
+ retryInfo: secondRetry,
277
+ previousFailureHash: firstFailureHash,
278
+ random: () => 0.5,
279
+ }),
280
+ {
281
+ action: "pause",
282
+ reason: "duplicate-failure-context",
283
+ key: "complete-slice:M001/S01",
284
+ failureHash: firstFailureHash,
285
+ },
286
+ );
287
+ assert.ok(
288
+ notifications.some((message) => message.includes("Retrying (attempt 1/3).")),
289
+ `expected first attempt notification, got: ${notifications.join("\n")}`,
290
+ );
291
+ assert.ok(
292
+ notifications.some((message) => message.includes("Retrying (attempt 2/3).")),
293
+ `expected second attempt notification, got: ${notifications.join("\n")}`,
294
+ );
295
+ } finally {
296
+ cleanup(base);
297
+ }
298
+ });
@@ -3,11 +3,16 @@
3
3
 
4
4
  import { describe, test } from "node:test";
5
5
  import assert from "node:assert/strict";
6
+ import { createRequire } from "node:module";
6
7
  import {
8
+ MEMORIES_FTS_REBUILT_KEY,
7
9
  isMemoriesFtsAvailableSchema,
10
+ rebuildMemoriesFtsSchemaOnce,
8
11
  tryCreateMemoriesFtsSchema,
9
12
  } from "../db-memory-fts-schema.ts";
10
- import type { DbAdapter, DbStatement } from "../db-adapter.ts";
13
+ import { createDbAdapter, type DbAdapter, type DbStatement } from "../db-adapter.ts";
14
+
15
+ const _require = createRequire(import.meta.url);
11
16
 
12
17
  class FakeStatement implements DbStatement {
13
18
  private readonly row: Record<string, unknown> | undefined;
@@ -32,6 +37,7 @@ class FakeStatement implements DbStatement {
32
37
  class FakeAdapter implements DbAdapter {
33
38
  readonly execCalls: string[] = [];
34
39
  hasFts = false;
40
+ hasRebuildMarker = false;
35
41
  failExec = false;
36
42
 
37
43
  exec(sql: string): void {
@@ -39,13 +45,26 @@ class FakeAdapter implements DbAdapter {
39
45
  if (this.failExec) throw new Error("fts unavailable");
40
46
  }
41
47
 
42
- prepare(): DbStatement {
43
- return new FakeStatement(this.hasFts ? { name: "memories_fts" } : undefined);
48
+ prepare(sql: string): DbStatement {
49
+ if (sql.includes("sqlite_master")) {
50
+ return new FakeStatement(this.hasFts ? { name: "memories_fts" } : undefined);
51
+ }
52
+ if (sql.includes("runtime_kv")) {
53
+ return new FakeStatement(this.hasRebuildMarker ? { present: 1 } : undefined);
54
+ }
55
+ return new FakeStatement(undefined);
44
56
  }
45
57
 
46
58
  close(): void {}
47
59
  }
48
60
 
61
+ function openMemoryAdapter(): { db: DbAdapter; close: () => void } {
62
+ const sqlite = _require("node:sqlite") as { DatabaseSync: new (path: string) => unknown };
63
+ const raw = new sqlite.DatabaseSync(":memory:");
64
+ const db = createDbAdapter(raw);
65
+ return { db, close: () => db.close() };
66
+ }
67
+
49
68
  describe("db-memory-fts-schema", () => {
50
69
  test("creates memories_fts and insert/delete/update triggers", () => {
51
70
  const db = new FakeAdapter();
@@ -83,4 +102,40 @@ describe("db-memory-fts-schema", () => {
83
102
  db.hasFts = true;
84
103
  assert.equal(isMemoriesFtsAvailableSchema(db), true);
85
104
  });
105
+
106
+ test("rebuilds the external-content index once and records a marker", () => {
107
+ const { db, close } = openMemoryAdapter();
108
+ try {
109
+ db.exec("CREATE TABLE memories (seq INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT NOT NULL)");
110
+ db.exec("INSERT INTO memories (content) VALUES ('pre FTS auth memory must be searchable')");
111
+
112
+ const ok = tryCreateMemoriesFtsSchema(db);
113
+ if (!ok) return;
114
+
115
+ const before = db.prepare("SELECT rowid FROM memories_fts WHERE memories_fts MATCH 'auth'").all();
116
+ assert.equal(before.length, 0, "external-content FTS starts empty for existing rows");
117
+
118
+ rebuildMemoriesFtsSchemaOnce(db);
119
+
120
+ const after = db.prepare("SELECT rowid FROM memories_fts WHERE memories_fts MATCH 'auth'").all();
121
+ assert.deepEqual(after.map((row) => row["rowid"]), [1]);
122
+ const marker = db.prepare(
123
+ "SELECT value_json FROM runtime_kv WHERE scope = 'global' AND scope_id = '' AND key = :key",
124
+ ).get({ ":key": MEMORIES_FTS_REBUILT_KEY });
125
+ assert.ok(marker, "rebuild marker should be stored");
126
+ } finally {
127
+ close();
128
+ }
129
+ });
130
+
131
+ test("skips rebuild when marker already exists", () => {
132
+ const db = new FakeAdapter();
133
+ db.hasFts = true;
134
+ db.hasRebuildMarker = true;
135
+
136
+ rebuildMemoriesFtsSchemaOnce(db);
137
+
138
+ assert.ok(db.execCalls.some((sql) => sql.includes("CREATE TABLE IF NOT EXISTS runtime_kv")));
139
+ assert.ok(!db.execCalls.some((sql) => sql.includes("VALUES('rebuild')")));
140
+ });
86
141
  });
@@ -142,6 +142,57 @@ test("backfill is idempotent — re-running over migrated rows is a no-op", () =
142
142
  }
143
143
  });
144
144
 
145
+ test("backfill skips a decision whose memory insert throws and continues later rows", () => {
146
+ const base = makeTmpBase();
147
+ try {
148
+ seedRawDecision({
149
+ id: "D001",
150
+ when_context: "M001",
151
+ scope: "M001",
152
+ decision: "before poison",
153
+ choice: "A",
154
+ rationale: "first row",
155
+ });
156
+ seedRawDecision({
157
+ id: "D002",
158
+ when_context: "M001",
159
+ scope: "M001",
160
+ decision: "poison",
161
+ choice: "B",
162
+ rationale: "this insert throws",
163
+ });
164
+ seedRawDecision({
165
+ id: "D003",
166
+ when_context: "M001",
167
+ scope: "M001",
168
+ decision: "after poison",
169
+ choice: "C",
170
+ rationale: "tail row",
171
+ });
172
+
173
+ const adapter = _getAdapter();
174
+ assert.ok(adapter);
175
+ adapter.exec(`
176
+ CREATE TRIGGER fail_d002_memory_insert
177
+ BEFORE INSERT ON memories
178
+ WHEN json_extract(NEW.structured_fields, '$.sourceDecisionId') = 'D002'
179
+ BEGIN
180
+ SELECT RAISE(ABORT, 'poison decision D002');
181
+ END
182
+ `);
183
+
184
+ const written = backfillDecisionsToMemories();
185
+ assert.equal(written, 2, "one poisoned row should not abort the batch");
186
+ assert.deepEqual(
187
+ getAllDecisionsFromMemories().map((d) => d.id),
188
+ ["D001", "D003"],
189
+ "rows after the poisoned decision should still be migrated",
190
+ );
191
+ } finally {
192
+ cleanup(base);
193
+ }
194
+ });
195
+
145
196
  // ─── Drift auto-heal ───────────────────────────────────────────────────────
146
197
 
147
198
  test("backfill auto-heals when a source decision's superseded_by changes after migration", () => {