@opengsd/gsd-pi 1.3.0-dev.965d1788 → 1.3.0-dev.cca9c184

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 (168) hide show
  1. package/dist/cli.js +17 -4
  2. package/dist/headless-answers.js +1 -1
  3. package/dist/headless-ui.d.ts +1 -1
  4. package/dist/headless-ui.js +1 -1
  5. package/dist/headless.js +1 -1
  6. package/dist/resources/.managed-resources-content-hash +1 -1
  7. package/dist/resources/extensions/claude-code-cli/models.js +9 -0
  8. package/dist/resources/extensions/gsd/auto/session.js +8 -0
  9. package/dist/resources/extensions/gsd/auto-budget.js +5 -1
  10. package/dist/resources/extensions/gsd/auto-dispatch.js +14 -1
  11. package/dist/resources/extensions/gsd/auto-prompts.js +3 -3
  12. package/dist/resources/extensions/gsd/auto-recovery.js +3 -3
  13. package/dist/resources/extensions/gsd/auto.js +19 -10
  14. package/dist/resources/extensions/gsd/bootstrap/core-session-tools.js +1 -0
  15. package/dist/resources/extensions/gsd/bootstrap/db-tools.js +5 -4
  16. package/dist/resources/extensions/gsd/bootstrap/register-extension.js +2 -0
  17. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +25 -2
  18. package/dist/resources/extensions/gsd/bootstrap/tool-call-loop-guard.js +32 -4
  19. package/dist/resources/extensions/gsd/bootstrap/write-gate.js +15 -4
  20. package/dist/resources/extensions/gsd/commands/handlers/auto.js +1 -1
  21. package/dist/resources/extensions/gsd/commands-prefs-wizard.js +1 -1
  22. package/dist/resources/extensions/gsd/commands-usage.js +1 -1
  23. package/dist/resources/extensions/gsd/db/engine.js +4 -1
  24. package/dist/resources/extensions/gsd/db-memory-fts-schema.js +34 -0
  25. package/dist/resources/extensions/gsd/delegation-policy.js +1 -1
  26. package/dist/resources/extensions/gsd/docs/preferences-reference.md +2 -2
  27. package/dist/resources/extensions/gsd/doctor-engine-checks.js +192 -4
  28. package/dist/resources/extensions/gsd/exec-sandbox.js +8 -2
  29. package/dist/resources/extensions/gsd/git-service.js +56 -0
  30. package/dist/resources/extensions/gsd/gitignore.js +1 -1
  31. package/dist/resources/extensions/gsd/migrate-external.js +11 -2
  32. package/dist/resources/extensions/gsd/prompts/plan-milestone.md +2 -2
  33. package/dist/resources/extensions/gsd/prompts/plan-slice.md +3 -3
  34. package/dist/resources/extensions/gsd/root-write-leak-guard.js +7 -1
  35. package/dist/resources/extensions/gsd/safety/destructive-guard.js +8 -1
  36. package/dist/resources/extensions/gsd/safety/evidence-collector.js +16 -1
  37. package/dist/resources/extensions/gsd/tools/plan-slice.js +59 -44
  38. package/dist/resources/extensions/gsd/tools/plan-task.js +28 -5
  39. package/dist/resources/extensions/gsd/unit-registry.js +4 -2
  40. package/dist/resources/extensions/gsd/workflow-manifest.js +122 -12
  41. package/dist/tsconfig.extensions.tsbuildinfo +1 -1
  42. package/dist/web/standalone/.next/BUILD_ID +1 -1
  43. package/dist/web/standalone/.next/app-path-routes-manifest.json +11 -11
  44. package/dist/web/standalone/.next/build-manifest.json +2 -2
  45. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  46. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  47. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  48. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  49. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  50. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  51. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  52. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  53. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  54. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  55. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  56. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  57. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  58. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  59. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  60. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  61. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  62. package/dist/web/standalone/.next/server/app/index.html +1 -1
  63. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  64. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  65. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  66. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  67. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  68. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  69. package/dist/web/standalone/.next/server/app-paths-manifest.json +11 -11
  70. package/dist/web/standalone/.next/server/chunks/8357.js +1 -1
  71. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  72. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  73. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  74. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  75. package/package.json +1 -1
  76. package/packages/cloud-mcp-gateway/package.json +2 -2
  77. package/packages/contracts/package.json +1 -1
  78. package/packages/daemon/package.json +4 -4
  79. package/packages/gsd-agent-core/package.json +5 -5
  80. package/packages/gsd-agent-modes/dist/modes/rpc/rpc-client.d.ts +2 -0
  81. package/packages/gsd-agent-modes/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  82. package/packages/gsd-agent-modes/dist/modes/rpc/rpc-client.js +23 -5
  83. package/packages/gsd-agent-modes/dist/modes/rpc/rpc-client.js.map +1 -1
  84. package/packages/gsd-agent-modes/package.json +7 -7
  85. package/packages/mcp-server/dist/workflow-tools.js +1 -1
  86. package/packages/mcp-server/dist/workflow-tools.js.map +1 -1
  87. package/packages/mcp-server/package.json +4 -4
  88. package/packages/native/package.json +1 -1
  89. package/packages/pi-agent-core/dist/agent-loop.js +3 -2
  90. package/packages/pi-agent-core/dist/agent-loop.js.map +1 -1
  91. package/packages/pi-agent-core/package.json +1 -1
  92. package/packages/pi-ai/dist/models.generated.d.ts +46 -0
  93. package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
  94. package/packages/pi-ai/dist/models.generated.js +38 -0
  95. package/packages/pi-ai/dist/models.generated.js.map +1 -1
  96. package/packages/pi-ai/package.json +1 -1
  97. package/packages/pi-coding-agent/package.json +7 -7
  98. package/packages/pi-tui/package.json +2 -2
  99. package/packages/rpc-client/package.json +2 -2
  100. package/pkg/package.json +1 -1
  101. package/src/resources/extensions/claude-code-cli/models.ts +9 -0
  102. package/src/resources/extensions/claude-code-cli/tests/index.test.ts +14 -0
  103. package/src/resources/extensions/gsd/auto/loop-deps.ts +1 -0
  104. package/src/resources/extensions/gsd/auto/session.ts +8 -0
  105. package/src/resources/extensions/gsd/auto-budget.ts +12 -1
  106. package/src/resources/extensions/gsd/auto-dispatch.ts +14 -1
  107. package/src/resources/extensions/gsd/auto-prompts.ts +3 -3
  108. package/src/resources/extensions/gsd/auto-recovery.ts +3 -3
  109. package/src/resources/extensions/gsd/auto.ts +23 -8
  110. package/src/resources/extensions/gsd/bootstrap/core-session-tools.ts +1 -0
  111. package/src/resources/extensions/gsd/bootstrap/db-tools.ts +5 -4
  112. package/src/resources/extensions/gsd/bootstrap/register-extension.ts +2 -0
  113. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +32 -2
  114. package/src/resources/extensions/gsd/bootstrap/tests/write-gate-basepath.test.ts +49 -1
  115. package/src/resources/extensions/gsd/bootstrap/tool-call-loop-guard.ts +34 -4
  116. package/src/resources/extensions/gsd/bootstrap/write-gate.ts +16 -4
  117. package/src/resources/extensions/gsd/commands/handlers/auto.ts +1 -1
  118. package/src/resources/extensions/gsd/commands-prefs-wizard.ts +1 -1
  119. package/src/resources/extensions/gsd/commands-usage.ts +1 -1
  120. package/src/resources/extensions/gsd/db/engine.ts +8 -1
  121. package/src/resources/extensions/gsd/db-memory-fts-schema.ts +45 -0
  122. package/src/resources/extensions/gsd/delegation-policy.ts +1 -1
  123. package/src/resources/extensions/gsd/docs/preferences-reference.md +2 -2
  124. package/src/resources/extensions/gsd/doctor-engine-checks.ts +233 -5
  125. package/src/resources/extensions/gsd/doctor-types.ts +3 -0
  126. package/src/resources/extensions/gsd/exec-sandbox.ts +7 -2
  127. package/src/resources/extensions/gsd/git-service.ts +63 -0
  128. package/src/resources/extensions/gsd/gitignore.ts +1 -1
  129. package/src/resources/extensions/gsd/migrate-external.ts +8 -2
  130. package/src/resources/extensions/gsd/prompts/plan-milestone.md +2 -2
  131. package/src/resources/extensions/gsd/prompts/plan-slice.md +3 -3
  132. package/src/resources/extensions/gsd/root-write-leak-guard.ts +13 -1
  133. package/src/resources/extensions/gsd/safety/destructive-guard.ts +9 -1
  134. package/src/resources/extensions/gsd/safety/evidence-collector.ts +16 -1
  135. package/src/resources/extensions/gsd/tests/auto-budget-alerts.test.ts +14 -5
  136. package/src/resources/extensions/gsd/tests/auto-pause-double-entry-guard.test.ts +36 -0
  137. package/src/resources/extensions/gsd/tests/auto-paused-ui-cleanup.test.ts +182 -4
  138. package/src/resources/extensions/gsd/tests/auto-recovery.test.ts +35 -0
  139. package/src/resources/extensions/gsd/tests/auto-retry-mcp-churn-fixes.test.ts +30 -0
  140. package/src/resources/extensions/gsd/tests/db-memory-fts-schema.test.ts +58 -3
  141. package/src/resources/extensions/gsd/tests/destructive-guard.test.ts +44 -0
  142. package/src/resources/extensions/gsd/tests/dispatch-missing-task-plans.test.ts +143 -0
  143. package/src/resources/extensions/gsd/tests/doctor-scope-db-unavailable.test.ts +239 -2
  144. package/src/resources/extensions/gsd/tests/exec-sandbox.test.ts +16 -0
  145. package/src/resources/extensions/gsd/tests/memory-store.test.ts +48 -0
  146. package/src/resources/extensions/gsd/tests/migrate-external-worktree.test.ts +57 -0
  147. package/src/resources/extensions/gsd/tests/parsers-legacy-importers.test.ts +2 -0
  148. package/src/resources/extensions/gsd/tests/plan-slice.test.ts +103 -4
  149. package/src/resources/extensions/gsd/tests/plan-task.test.ts +44 -1
  150. package/src/resources/extensions/gsd/tests/post-mutation-hook.test.ts +4 -2
  151. package/src/resources/extensions/gsd/tests/prompt-budget-enforcement.test.ts +58 -0
  152. package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +8 -6
  153. package/src/resources/extensions/gsd/tests/register-hooks-loop-guard-auto.test.ts +103 -0
  154. package/src/resources/extensions/gsd/tests/root-write-leak-guard.test.ts +20 -1
  155. package/src/resources/extensions/gsd/tests/runtime-invariant-modules.test.ts +8 -3
  156. package/src/resources/extensions/gsd/tests/tool-call-loop-guard.test.ts +62 -5
  157. package/src/resources/extensions/gsd/tests/tool-param-optionality.test.ts +6 -3
  158. package/src/resources/extensions/gsd/tests/uok-gitops-turn-action.test.ts +38 -0
  159. package/src/resources/extensions/gsd/tests/workflow-manifest.test.ts +44 -17
  160. package/src/resources/extensions/gsd/tests/workflow-mcp-preflight.test.ts +1 -0
  161. package/src/resources/extensions/gsd/tests/workflow-mcp.test.ts +8 -4
  162. package/src/resources/extensions/gsd/tests/workflow-tool-executors.test.ts +6 -5
  163. package/src/resources/extensions/gsd/tools/plan-slice.ts +60 -47
  164. package/src/resources/extensions/gsd/tools/plan-task.ts +30 -5
  165. package/src/resources/extensions/gsd/unit-registry.ts +4 -2
  166. package/src/resources/extensions/gsd/workflow-manifest.ts +137 -6
  167. /package/dist/web/standalone/.next/static/{-ji77QJck2IKIwrGza1Bs → Eud_8lddsyi_13HIlB2i7}/_buildManifest.js +0 -0
  168. /package/dist/web/standalone/.next/static/{-ji77QJck2IKIwrGza1Bs → Eud_8lddsyi_13HIlB2i7}/_ssgManifest.js +0 -0
@@ -28,9 +28,13 @@ function validateRepositoryTargetIds(field, value) {
28
28
  return deduped;
29
29
  }
30
30
  function validateTasks(value) {
31
- if (!Array.isArray(value) || value.length === 0) {
32
- throw new Error("tasks must be a non-empty array");
31
+ if (value === undefined)
32
+ return undefined;
33
+ if (!Array.isArray(value)) {
34
+ throw new Error("tasks must be an array");
33
35
  }
36
+ if (value.length === 0)
37
+ return undefined;
34
38
  const seen = new Set();
35
39
  return value.map((entry, index) => {
36
40
  if (!entry || typeof entry !== "object") {
@@ -129,7 +133,7 @@ function validateReferencedRepositories(params, registry, defaultTargets) {
129
133
  };
130
134
  for (const id of params.targetRepositories ?? defaultTargets)
131
135
  noteMissing(id);
132
- for (const task of params.tasks) {
136
+ for (const task of params.tasks ?? []) {
133
137
  for (const id of task.targetRepositories ?? params.targetRepositories ?? defaultTargets)
134
138
  noteMissing(id);
135
139
  }
@@ -141,7 +145,7 @@ function resolveAllowedRootsForPathScope(params, registry, defaultTargets) {
141
145
  const requested = new Set();
142
146
  for (const id of params.targetRepositories ?? defaultTargets)
143
147
  requested.add(id);
144
- for (const task of params.tasks) {
148
+ for (const task of params.tasks ?? []) {
145
149
  for (const id of task.targetRepositories ?? params.targetRepositories ?? defaultTargets)
146
150
  requested.add(id);
147
151
  }
@@ -153,7 +157,7 @@ function resolveAllowedRootsForPathScope(params, registry, defaultTargets) {
153
157
  return roots.length > 0 ? roots : [registry.projectRoot];
154
158
  }
155
159
  function toTaskRows(params, defaultTargets) {
156
- return params.tasks.map((task, index) => ({
160
+ return (params.tasks ?? []).map((task, index) => ({
157
161
  milestone_id: params.milestoneId,
158
162
  slice_id: params.sliceId,
159
163
  id: task.taskId,
@@ -230,19 +234,21 @@ export async function handlePlanSlice(rawParams, basePath) {
230
234
  return { error: `validation failed: ${message}` };
231
235
  }
232
236
  const defaultTargets = defaultRepositoryTargets(repositoryRegistry);
237
+ const taskPayload = params.tasks ?? [];
238
+ const hasTaskPayload = taskPayload.length > 0;
233
239
  const repoValidationError = validateReferencedRepositories(params, repositoryRegistry, defaultTargets);
234
240
  if (repoValidationError) {
235
241
  return { error: `validation failed: ${repoValidationError}` };
236
242
  }
237
243
  const allowedAbsoluteRoots = resolveAllowedRootsForPathScope(params, repositoryRegistry, defaultTargets);
238
- const pathOnlyError = validatePathOnlyPlanningFields(params.tasks.map((task, index) => ({
244
+ const pathOnlyError = validatePathOnlyPlanningFields(taskPayload.map((task, index) => ({
239
245
  field: `tasks[${index}].expectedOutput`,
240
246
  values: task.expectedOutput,
241
247
  })));
242
248
  if (pathOnlyError) {
243
249
  return { error: `validation failed: ${pathOnlyError}` };
244
250
  }
245
- const pathScopeError = validatePlanningPathScope(basePath, params.tasks.flatMap((task, index) => [
251
+ const pathScopeError = validatePlanningPathScope(basePath, taskPayload.flatMap((task, index) => [
246
252
  { field: `tasks[${index}].files`, values: task.files },
247
253
  { field: `tasks[${index}].inputs`, values: task.inputs },
248
254
  { field: `tasks[${index}].expectedOutput`, values: task.expectedOutput },
@@ -279,15 +285,17 @@ export async function handlePlanSlice(rawParams, basePath) {
279
285
  guardError = `cannot re-plan slice ${params.sliceId}: it is already complete — use gsd_slice_reopen first`;
280
286
  return;
281
287
  }
282
- const newTaskIds = new Set(params.tasks.map((task) => task.taskId));
288
+ const newTaskIds = new Set(taskPayload.map((task) => task.taskId));
283
289
  const existingTasks = getSliceTasks(params.milestoneId, params.sliceId);
284
- omittedTaskIds = existingTasks
285
- .filter((task) => !newTaskIds.has(task.id))
286
- .map((task) => task.id);
287
- for (const task of existingTasks) {
288
- if (!newTaskIds.has(task.id) && isClosedStatus(task.status)) {
289
- guardError = `cannot remove completed task ${task.id}`;
290
- return;
290
+ if (hasTaskPayload) {
291
+ omittedTaskIds = existingTasks
292
+ .filter((task) => !newTaskIds.has(task.id))
293
+ .map((task) => task.id);
294
+ for (const task of existingTasks) {
295
+ if (!newTaskIds.has(task.id) && isClosedStatus(task.status)) {
296
+ guardError = `cannot remove completed task ${task.id}`;
297
+ return;
298
+ }
291
299
  }
292
300
  }
293
301
  if (isDeferredStatus(parentSlice.status)) {
@@ -301,34 +309,36 @@ export async function handlePlanSlice(rawParams, basePath) {
301
309
  observabilityImpact: params.observabilityImpact,
302
310
  targetRepositories: params.targetRepositories ?? defaultTargets,
303
311
  });
304
- for (const taskId of omittedTaskIds) {
305
- deleteTask(params.milestoneId, params.sliceId, taskId);
306
- }
307
- const existingTaskById = new Map(existingTasks.map((task) => [task.id, task]));
308
- for (const task of params.tasks) {
309
- const planning = {
310
- title: task.title,
311
- description: task.description,
312
- estimate: task.estimate,
313
- files: task.files,
314
- verify: task.verify,
315
- inputs: task.inputs,
316
- expectedOutput: task.expectedOutput,
317
- observabilityImpact: task.observabilityImpact ?? "",
318
- fullPlanMd: task.fullPlanMd,
319
- targetRepositories: task.targetRepositories ?? params.targetRepositories ?? defaultTargets,
320
- };
321
- const existingTask = existingTaskById.get(task.taskId);
322
- if (!existingTask || !isClosedStatus(existingTask.status)) {
323
- insertTask({
324
- id: task.taskId,
325
- sliceId: params.sliceId,
326
- milestoneId: params.milestoneId,
312
+ if (hasTaskPayload) {
313
+ for (const taskId of omittedTaskIds) {
314
+ deleteTask(params.milestoneId, params.sliceId, taskId);
315
+ }
316
+ const existingTaskById = new Map(existingTasks.map((task) => [task.id, task]));
317
+ for (const task of taskPayload) {
318
+ const planning = {
327
319
  title: task.title,
328
- status: "pending",
329
- });
320
+ description: task.description,
321
+ estimate: task.estimate,
322
+ files: task.files,
323
+ verify: task.verify,
324
+ inputs: task.inputs,
325
+ expectedOutput: task.expectedOutput,
326
+ observabilityImpact: task.observabilityImpact ?? "",
327
+ fullPlanMd: task.fullPlanMd,
328
+ targetRepositories: task.targetRepositories ?? params.targetRepositories ?? defaultTargets,
329
+ };
330
+ const existingTask = existingTaskById.get(task.taskId);
331
+ if (!existingTask || !isClosedStatus(existingTask.status)) {
332
+ insertTask({
333
+ id: task.taskId,
334
+ sliceId: params.sliceId,
335
+ milestoneId: params.milestoneId,
336
+ title: task.title,
337
+ status: "pending",
338
+ });
339
+ }
340
+ upsertTaskPlanning(params.milestoneId, params.sliceId, task.taskId, planning);
330
341
  }
331
- upsertTaskPlanning(params.milestoneId, params.sliceId, task.taskId, planning);
332
342
  }
333
343
  // Seed quality gate rows inside the transaction — all-or-nothing with
334
344
  // the plan data so a crash can't leave orphaned gates without tasks.
@@ -337,7 +347,7 @@ export async function handlePlanSlice(rawParams, basePath) {
337
347
  insertGateRow({ milestoneId: params.milestoneId, sliceId: params.sliceId, gateId: gid, scope: "slice" });
338
348
  }
339
349
  const taskGates = resolveTaskGates(gateEvaluation);
340
- for (const task of params.tasks) {
350
+ for (const task of taskPayload) {
341
351
  for (const gid of taskGates) {
342
352
  insertGateRow({ milestoneId: params.milestoneId, sliceId: params.sliceId, gateId: gid, scope: "task", taskId: task.taskId });
343
353
  }
@@ -360,8 +370,13 @@ export async function handlePlanSlice(rawParams, basePath) {
360
370
  const artifactPath = relative(gsdProjectionRoot(basePath), taskPlanPath).replace(/\\/g, "/");
361
371
  deleteArtifactByPath(artifactPath);
362
372
  }
363
- const renderResult = await renderPlanFromDb(basePath, params.milestoneId, params.sliceId);
364
- setSliceSketchFlag(params.milestoneId, params.sliceId, false);
373
+ const sliceTasks = getSliceTasks(params.milestoneId, params.sliceId);
374
+ const renderResult = sliceTasks.length === 0
375
+ ? { planPath: "", taskPlanPaths: [] }
376
+ : await renderPlanFromDb(basePath, params.milestoneId, params.sliceId);
377
+ if (sliceTasks.length > 0) {
378
+ setSliceSketchFlag(params.milestoneId, params.sliceId, false);
379
+ }
365
380
  invalidateStateCache();
366
381
  clearParseCache();
367
382
  // ── Post-mutation hook: manifest, event log ─────────────────────────
@@ -1,14 +1,16 @@
1
1
  import { clearParseCache } from "../files.js";
2
2
  import { isClosedStatus } from "../status-guards.js";
3
3
  import { isNonEmptyString, validateStringArray } from "../validation.js";
4
- import { transaction, getSlice, getTask, insertTask, upsertTaskPlanning } from "../gsd-db.js";
4
+ import { getGateIdsForTurn } from "../gate-registry.js";
5
+ import { transaction, getSlice, getTask, insertTask, upsertTaskPlanning, insertGateRow, setSliceSketchFlag } from "../gsd-db.js";
5
6
  import { invalidateStateCache } from "../state.js";
6
7
  import { renderTaskPlanFromDb, renderPlanFromDb } from "../markdown-renderer.js";
7
- import { resolveSliceFile, resolveTasksDir } from "../paths.js";
8
+ import { resolveTasksDir } from "../paths.js";
8
9
  import { flushWorkflowProjections } from "../projection-flush.js";
9
10
  import { writeManifest } from "../workflow-manifest.js";
10
11
  import { appendEvent } from "../workflow-events.js";
11
12
  import { logWarning } from "../workflow-logger.js";
13
+ import { loadEffectiveGSDPreferences } from "../preferences.js";
12
14
  import { validatePathOnlyPlanningFields, validatePlanningPathScope } from "../planning-path-scope.js";
13
15
  function validateParams(params) {
14
16
  if (!isNonEmptyString(params?.milestoneId))
@@ -35,6 +37,12 @@ function validateParams(params) {
35
37
  expectedOutput: validateStringArray(params.expectedOutput, "expectedOutput"),
36
38
  };
37
39
  }
40
+ function resolveTaskGates(basePath) {
41
+ const loaded = loadEffectiveGSDPreferences(basePath);
42
+ if (loaded?.preferences?.gate_evaluation?.task_gates === false)
43
+ return [];
44
+ return [...getGateIdsForTurn("execute-task")];
45
+ }
38
46
  export async function handlePlanTask(rawParams, basePath) {
39
47
  let params;
40
48
  try {
@@ -57,6 +65,14 @@ export async function handlePlanTask(rawParams, basePath) {
57
65
  if (pathScopeError) {
58
66
  return { error: `validation failed: ${pathScopeError}` };
59
67
  }
68
+ let taskGates;
69
+ try {
70
+ taskGates = resolveTaskGates(basePath);
71
+ }
72
+ catch (err) {
73
+ const message = err instanceof Error ? err.message : String(err);
74
+ return { error: `validation failed: ${message}` };
75
+ }
60
76
  // ── Guards + DB writes inside a single transaction (prevents TOCTOU) ───
61
77
  // Guards must be inside the transaction so the state they check cannot
62
78
  // change between the read and the write (#2723).
@@ -97,6 +113,9 @@ export async function handlePlanTask(rawParams, basePath) {
97
113
  observabilityImpact: params.observabilityImpact ?? "",
98
114
  fullPlanMd: params.fullPlanMd,
99
115
  });
116
+ for (const gid of taskGates) {
117
+ insertGateRow({ milestoneId: params.milestoneId, sliceId: params.sliceId, gateId: gid, scope: "task", taskId: params.taskId });
118
+ }
100
119
  });
101
120
  }
102
121
  catch (err) {
@@ -110,17 +129,21 @@ export async function handlePlanTask(rawParams, basePath) {
110
129
  // Flat-phase: tasks live as checkboxes in the slice plan's <tasks> block,
111
130
  // not as standalone TID-PLAN.md files. Re-render the slice plan so the
112
131
  // new/updated task appears in the plan file that gsd-core reads.
113
- // Guard: resolveTasksDir is null in flat-phase (no tasks/ subdir exists);
114
- // only sync if a slice plan already exists (guards pre-plan-slice calls).
132
+ // Guard: resolveTasksDir is null in flat-phase (no tasks/ subdir exists).
133
+ let slicePlanSynced = false;
115
134
  try {
116
135
  const tDir = resolveTasksDir(basePath, params.milestoneId, params.sliceId);
117
- if (!tDir && resolveSliceFile(basePath, params.milestoneId, params.sliceId, "PLAN")) {
136
+ if (!tDir) {
118
137
  await renderPlanFromDb(basePath, params.milestoneId, params.sliceId);
138
+ slicePlanSynced = true;
119
139
  }
120
140
  }
121
141
  catch (syncErr) {
122
142
  logWarning("tool", `plan-task: slice-plan sync failed: ${syncErr.message}`);
123
143
  }
144
+ if (slicePlanSynced) {
145
+ setSliceSketchFlag(params.milestoneId, params.sliceId, false);
146
+ }
124
147
  invalidateStateCache();
125
148
  clearParseCache();
126
149
  // ── Post-mutation hook: projections, manifest, event log ─────────────
@@ -70,10 +70,11 @@ export const UNIT_REGISTRY = {
70
70
  "gsd_milestone_status",
71
71
  "gsd_plan_milestone",
72
72
  "gsd_plan_slice",
73
+ "gsd_plan_task",
73
74
  "gsd_decision_save",
74
75
  "gsd_requirement_update",
75
76
  ],
76
- requiredWorkflowTools: ["gsd_milestone_status", "gsd_plan_milestone", "gsd_plan_slice"],
77
+ requiredWorkflowTools: ["gsd_milestone_status", "gsd_plan_milestone", "gsd_plan_slice", "gsd_plan_task"],
77
78
  },
78
79
  },
79
80
  "discuss-milestone": {
@@ -172,13 +173,14 @@ export const UNIT_REGISTRY = {
172
173
  allowedGsdTools: [
173
174
  "gsd_milestone_status",
174
175
  "gsd_plan_slice",
176
+ "gsd_plan_task",
175
177
  "gsd_reassess_roadmap",
176
178
  "gsd_decision_save",
177
179
  "gsd_exec",
178
180
  "gsd_exec_search",
179
181
  "gsd_resume",
180
182
  ],
181
- requiredWorkflowTools: ["gsd_plan_slice", "gsd_reassess_roadmap"],
183
+ requiredWorkflowTools: ["gsd_plan_slice", "gsd_plan_task", "gsd_reassess_roadmap"],
182
184
  },
183
185
  },
184
186
  "refine-slice": {
@@ -1,11 +1,12 @@
1
1
  // Project/App: gsd-pi
2
2
  // File Purpose: State manifest snapshot and restore orchestration for GSD workflow data.
3
3
  import { _getAdapter, readTransaction, restoreManifest, } from "./gsd-db.js";
4
- import { atomicWriteSync } from "./atomic-write.js";
4
+ import { atomicWriteAsync, atomicWriteSync } from "./atomic-write.js";
5
5
  import { getAllDecisionsFromMemories } from "./context-store.js";
6
6
  import { backfillDecisionsToMemories } from "./memory-backfill.js";
7
7
  import { invalidateAllCaches } from "./cache.js";
8
- import { readFileSync, existsSync, mkdirSync } from "node:fs";
8
+ import { logWarning } from "./workflow-logger.js";
9
+ import { readFileSync, existsSync } from "node:fs";
9
10
  import { isAbsolute, join, relative, resolve } from "node:path";
10
11
  // ─── helpers ─────────────────────────────────────────────────────────────
11
12
  function requireDb() {
@@ -257,22 +258,131 @@ export function snapshotState() {
257
258
  return result;
258
259
  });
259
260
  }
260
- // ─── restore ─────────────────────────────────────────────────────────────
261
- //
262
- // The actual restore() implementation lives in gsd-db.ts (single-writer
263
- // invariant). This module only orchestrates reading the manifest file
264
- // and handing it to the writer.
265
- // ─── writeManifest ───────────────────────────────────────────────────────
261
+ const manifestWrites = new Map();
262
+ let processTeardownFlushInstalled = false;
263
+ let beforeExitFlushStarted = false;
264
+ function manifestWriteKey(basePath) {
265
+ return resolve(basePath);
266
+ }
267
+ function manifestFilePath(basePath) {
268
+ return join(resolve(basePath), ".gsd", "state-manifest.json");
269
+ }
270
+ function describeError(err) {
271
+ return err instanceof Error ? err.message : String(err);
272
+ }
273
+ function logManifestWriteFailure(filePath, err) {
274
+ logWarning("manifest", `state manifest write failed: ${describeError(err)}`, { file: filePath });
275
+ }
276
+ async function drainManifestWrites(key, state) {
277
+ let lastWriteError = null;
278
+ try {
279
+ while (state.latestJson !== null && state.syncFlushJson === null) {
280
+ const json = state.latestJson;
281
+ state.latestJson = null;
282
+ state.inFlightJson = json;
283
+ try {
284
+ await atomicWriteAsync(state.filePath, json);
285
+ if (state.syncFlushJson !== null) {
286
+ atomicWriteSync(state.filePath, state.syncFlushJson);
287
+ break;
288
+ }
289
+ lastWriteError = null;
290
+ }
291
+ catch (err) {
292
+ lastWriteError = err;
293
+ logManifestWriteFailure(state.filePath, err);
294
+ }
295
+ finally {
296
+ state.inFlightJson = null;
297
+ }
298
+ }
299
+ }
300
+ finally {
301
+ state.active = null;
302
+ if (state.latestJson === null && state.inFlightJson === null) {
303
+ manifestWrites.delete(key);
304
+ }
305
+ }
306
+ if (lastWriteError !== null && state.syncFlushJson === null) {
307
+ throw lastWriteError;
308
+ }
309
+ }
310
+ function enqueueManifestWrite(basePath, json) {
311
+ const key = manifestWriteKey(basePath);
312
+ let state = manifestWrites.get(key);
313
+ if (!state) {
314
+ state = {
315
+ filePath: manifestFilePath(basePath),
316
+ latestJson: null,
317
+ inFlightJson: null,
318
+ syncFlushJson: null,
319
+ active: null,
320
+ };
321
+ manifestWrites.set(key, state);
322
+ }
323
+ state.latestJson = json;
324
+ if (!state.active) {
325
+ const active = drainManifestWrites(key, state);
326
+ void active.catch(() => { });
327
+ state.active = active;
328
+ }
329
+ }
266
330
  /**
267
- * Write current DB state to .gsd/state-manifest.json via atomicWriteSync.
331
+ * Queue current DB state for an async atomic write to .gsd/state-manifest.json.
268
332
  * Uses JSON.stringify with 2-space indent for git three-way merge friendliness.
269
333
  */
270
334
  export function writeManifest(basePath) {
271
335
  const manifest = snapshotState();
272
336
  const json = JSON.stringify(manifest, null, 2);
273
- const dir = join(basePath, ".gsd");
274
- mkdirSync(dir, { recursive: true });
275
- atomicWriteSync(join(dir, "state-manifest.json"), json);
337
+ enqueueManifestWrite(basePath, json);
338
+ }
339
+ async function flushManifestByKey(key) {
340
+ for (;;) {
341
+ const active = manifestWrites.get(key)?.active;
342
+ if (!active)
343
+ return;
344
+ await active;
345
+ }
346
+ }
347
+ /**
348
+ * Wait for any queued manifest write for this base path to become durable.
349
+ */
350
+ export async function flushManifest(basePath) {
351
+ await flushManifestByKey(manifestWriteKey(basePath));
352
+ }
353
+ export async function flushAllManifests() {
354
+ await Promise.all(Array.from(manifestWrites.keys(), key => flushManifestByKey(key)));
355
+ }
356
+ function flushAllManifestsSync() {
357
+ for (const [key, state] of manifestWrites) {
358
+ const json = state.latestJson ?? state.inFlightJson;
359
+ if (json === null)
360
+ continue;
361
+ try {
362
+ atomicWriteSync(state.filePath, json);
363
+ state.latestJson = null;
364
+ state.inFlightJson = null;
365
+ state.syncFlushJson = json;
366
+ manifestWrites.delete(key);
367
+ }
368
+ catch (err) {
369
+ logManifestWriteFailure(state.filePath, err);
370
+ }
371
+ }
372
+ }
373
+ export function installManifestFlushOnProcessTeardown() {
374
+ if (processTeardownFlushInstalled)
375
+ return;
376
+ processTeardownFlushInstalled = true;
377
+ process.once("beforeExit", () => {
378
+ if (beforeExitFlushStarted)
379
+ return;
380
+ beforeExitFlushStarted = true;
381
+ void flushAllManifests();
382
+ });
383
+ process.once("exit", () => {
384
+ flushAllManifestsSync();
385
+ });
276
386
  }
277
387
  // ─── readManifest ────────────────────────────────────────────────────────
278
388
  /**