@opengsd/gsd-pi 1.2.0-dev.844675c9 → 1.2.0-dev.84c56d87
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli-style.d.ts +17 -0
- package/dist/cli-style.js +28 -0
- package/dist/cli-web-branch.d.ts +2 -0
- package/dist/cli-web-branch.js +9 -2
- package/dist/cli.js +1 -1
- package/dist/headless-events.d.ts +4 -2
- package/dist/headless-events.js +7 -29
- package/dist/help-text.js +5 -0
- package/dist/models-resolver.d.ts +3 -13
- package/dist/models-resolver.js +3 -22
- package/dist/resource-loader.js +2 -14
- package/dist/resources/.managed-resources-content-hash +1 -1
- package/dist/resources/extensions/ask-user-questions.js +78 -23
- package/dist/resources/extensions/bg-shell/utilities.js +5 -2
- package/dist/resources/extensions/browser-tools/engine/managed-gsd-browser.js +209 -88
- package/dist/resources/extensions/browser-tools/engine/selection.js +73 -5
- package/dist/resources/extensions/browser-tools/index.js +69 -12
- package/dist/resources/extensions/claude-code-cli/models.js +9 -0
- package/dist/resources/extensions/claude-code-cli/stream-adapter.js +122 -234
- package/dist/resources/extensions/claude-code-cli/turn-assembler.js +224 -0
- package/dist/resources/extensions/github-sync/templates.js +3 -3
- package/dist/resources/extensions/gsd/artifact-projection.js +14 -0
- package/dist/resources/extensions/gsd/auto/loop.js +74 -56
- package/dist/resources/extensions/gsd/auto/orchestrator.js +142 -15
- package/dist/resources/extensions/gsd/auto/phases.js +34 -4
- package/dist/resources/extensions/gsd/auto/run-unit.js +2 -1
- package/dist/resources/extensions/gsd/auto/session.js +3 -0
- package/dist/resources/extensions/gsd/auto-dashboard.js +16 -4
- package/dist/resources/extensions/gsd/auto-dispatch.js +6 -5
- package/dist/resources/extensions/gsd/auto-model-selection.js +8 -0
- package/dist/resources/extensions/gsd/auto-post-unit.js +23 -11
- package/dist/resources/extensions/gsd/auto-prompts.js +84 -8
- package/dist/resources/extensions/gsd/auto-recovery.js +48 -49
- package/dist/resources/extensions/gsd/auto-runtime-state.js +14 -0
- package/dist/resources/extensions/gsd/auto-start.js +24 -37
- package/dist/resources/extensions/gsd/auto-timers.js +16 -2
- package/dist/resources/extensions/gsd/auto-tool-tracking.js +50 -0
- package/dist/resources/extensions/gsd/auto-unit-tool-scope.js +11 -45
- package/dist/resources/extensions/gsd/auto-verification.js +7 -7
- package/dist/resources/extensions/gsd/auto-worktree-repair.js +10 -2
- package/dist/resources/extensions/gsd/auto-worktree.js +53 -368
- package/dist/resources/extensions/gsd/auto.js +19 -27
- package/dist/resources/extensions/gsd/bootstrap/agent-end-recovery.js +3 -2
- package/dist/resources/extensions/gsd/bootstrap/db-tools.js +28 -37
- package/dist/resources/extensions/gsd/bootstrap/dynamic-tools.js +20 -43
- package/dist/resources/extensions/gsd/bootstrap/query-tools.js +2 -2
- package/dist/resources/extensions/gsd/bootstrap/register-extension.js +19 -0
- package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +209 -149
- package/dist/resources/extensions/gsd/bootstrap/write-gate.js +93 -8
- package/dist/resources/extensions/gsd/branch-patterns.js +2 -0
- package/dist/resources/extensions/gsd/browser-evidence.js +8 -2
- package/dist/resources/extensions/gsd/captures.js +5 -15
- package/dist/resources/extensions/gsd/closeout-consistency-gate.js +21 -4
- package/dist/resources/extensions/gsd/closeout-recovery.js +3 -2
- package/dist/resources/extensions/gsd/codebase-generator.js +8 -4
- package/dist/resources/extensions/gsd/commands/catalog.js +6 -62
- package/dist/resources/extensions/gsd/commands/handlers/auto.js +3 -0
- package/dist/resources/extensions/gsd/commands-handlers.js +20 -0
- package/dist/resources/extensions/gsd/commands-inspect.js +4 -8
- package/dist/resources/extensions/gsd/commands-maintenance.js +61 -41
- package/dist/resources/extensions/gsd/commands-ship.js +2 -2
- package/dist/resources/extensions/gsd/commands-verdict.js +12 -2
- package/dist/resources/extensions/gsd/crash-recovery.js +4 -12
- package/dist/resources/extensions/gsd/db/engine.js +755 -0
- package/dist/resources/extensions/gsd/db/queries.js +372 -0
- package/dist/resources/extensions/gsd/db/sql-constants.js +11 -0
- package/dist/resources/extensions/gsd/db/writers/cascades.js +194 -0
- package/dist/resources/extensions/gsd/db/writers/import-restore.js +182 -0
- package/dist/resources/extensions/gsd/db/writers/memory.js +149 -0
- package/dist/resources/extensions/gsd/db/writers/reconcile.js +458 -0
- package/dist/resources/extensions/gsd/db/writers/status.js +70 -0
- package/dist/resources/extensions/gsd/db-workspace.js +103 -0
- package/dist/resources/extensions/gsd/delegation-policy.js +2 -10
- package/dist/resources/extensions/gsd/discussion-handoff.js +218 -0
- package/dist/resources/extensions/gsd/docs/preferences-reference.md +9 -0
- package/dist/resources/extensions/gsd/doctor-environment.js +5 -11
- package/dist/resources/extensions/gsd/doctor-format.js +9 -6
- package/dist/resources/extensions/gsd/doctor-git-checks.js +4 -3
- package/dist/resources/extensions/gsd/doctor-runtime-checks.js +21 -16
- package/dist/resources/extensions/gsd/doctor.js +16 -9
- package/dist/resources/extensions/gsd/error-classifier.js +10 -1
- package/dist/resources/extensions/gsd/git-conflict-state.js +16 -1
- package/dist/resources/extensions/gsd/git-service.js +1 -0
- package/dist/resources/extensions/gsd/gitignore.js +3 -0
- package/dist/resources/extensions/gsd/gsd-db.js +183 -2048
- package/dist/resources/extensions/gsd/guidance.js +98 -0
- package/dist/resources/extensions/gsd/guided-flow.js +83 -471
- package/dist/resources/extensions/gsd/guided-unit-completion.js +225 -0
- package/dist/resources/extensions/gsd/markdown-renderer.js +2 -1
- package/dist/resources/extensions/gsd/mcp-filter.js +4 -20
- package/dist/resources/extensions/gsd/mcp-tool-name.js +18 -0
- package/dist/resources/extensions/gsd/md-importer.js +4 -3
- package/dist/resources/extensions/gsd/memory-consolidation-scanner.js +1 -1
- package/dist/resources/extensions/gsd/migrate/safety.js +22 -11
- package/dist/resources/extensions/gsd/migration-auto-check.js +27 -5
- package/dist/resources/extensions/gsd/milestone-closeout-proof.js +72 -0
- package/dist/resources/extensions/gsd/milestone-closeout.js +12 -4
- package/dist/resources/extensions/gsd/milestone-merge-transaction.js +10 -0
- package/dist/resources/extensions/gsd/milestone-planning-persistence.js +156 -0
- package/dist/resources/extensions/gsd/milestone-readiness.js +77 -0
- package/dist/resources/extensions/gsd/milestone-settlement.js +50 -0
- package/dist/resources/extensions/gsd/milestone-validation-evidence.js +73 -0
- package/dist/resources/extensions/gsd/milestone-validation-verdict.js +57 -0
- package/dist/resources/extensions/gsd/model-cost-table.js +1 -0
- package/dist/resources/extensions/gsd/model-router.js +3 -0
- package/dist/resources/extensions/gsd/notification-store.js +11 -4
- package/dist/resources/extensions/gsd/parallel-eligibility.js +3 -6
- package/dist/resources/extensions/gsd/parallel-merge.js +14 -11
- package/dist/resources/extensions/gsd/parallel-monitor-overlay.js +11 -7
- package/dist/resources/extensions/gsd/parallel-orchestrator.js +3 -2
- package/dist/resources/extensions/gsd/paths.js +37 -24
- package/dist/resources/extensions/gsd/pre-execution-checks.js +91 -3
- package/dist/resources/extensions/gsd/preferences-diagnostics.js +67 -0
- package/dist/resources/extensions/gsd/preferences-models.js +12 -46
- package/dist/resources/extensions/gsd/preferences.js +161 -29
- package/dist/resources/extensions/gsd/prompts/complete-slice.md +1 -0
- package/dist/resources/extensions/gsd/prompts/execute-task.md +2 -0
- package/dist/resources/extensions/gsd/prompts/guided-discuss-project.md +3 -1
- package/dist/resources/extensions/gsd/prompts/plan-milestone.md +2 -0
- package/dist/resources/extensions/gsd/prompts/plan-slice.md +2 -1
- package/dist/resources/extensions/gsd/prompts/refine-slice.md +2 -1
- package/dist/resources/extensions/gsd/prompts/replan-slice.md +1 -1
- package/dist/resources/extensions/gsd/prompts/system.md +1 -1
- package/dist/resources/extensions/gsd/provider-error-guidance.js +1 -5
- package/dist/resources/extensions/gsd/provider-payload-policy.js +83 -0
- package/dist/resources/extensions/gsd/provider-switch-observer.js +1 -1
- package/dist/resources/extensions/gsd/publication.js +87 -0
- package/dist/resources/extensions/gsd/pull-request-process.js +13 -0
- package/dist/resources/extensions/gsd/quality-gate-closure.js +109 -0
- package/dist/resources/extensions/gsd/question-transport.js +86 -0
- package/dist/resources/extensions/gsd/recovery-classification.js +41 -87
- package/dist/resources/extensions/gsd/roadmap-slices.js +8 -2
- package/dist/resources/extensions/gsd/safety/evidence-collector.js +37 -4
- package/dist/resources/extensions/gsd/safety/evidence-cross-ref.js +7 -2
- package/dist/resources/extensions/gsd/safety/file-change-validator.js +10 -0
- package/dist/resources/extensions/gsd/slice-parallel-orchestrator.js +3 -2
- package/dist/resources/extensions/gsd/state-transition-matrix.js +38 -0
- package/dist/resources/extensions/gsd/state.js +14 -25
- package/dist/resources/extensions/gsd/status-guards.js +56 -8
- package/dist/resources/extensions/gsd/stop-notice.js +57 -0
- package/dist/resources/extensions/gsd/templates/plan.md +7 -0
- package/dist/resources/extensions/gsd/templates/project.md +1 -0
- package/dist/resources/extensions/gsd/templates/roadmap.md +1 -1
- package/dist/resources/extensions/gsd/templates/uat.md +5 -1
- package/dist/resources/extensions/gsd/tool-contract.js +52 -8
- package/dist/resources/extensions/gsd/tool-presentation-plan.js +15 -34
- package/dist/resources/extensions/gsd/tool-surface-readiness.js +56 -0
- package/dist/resources/extensions/gsd/tool-surface-snapshot.js +17 -0
- package/dist/resources/extensions/gsd/tools/complete-slice.js +24 -43
- package/dist/resources/extensions/gsd/tools/exec-tool.js +5 -8
- package/dist/resources/extensions/gsd/tools/plan-milestone.js +15 -143
- package/dist/resources/extensions/gsd/tools/plan-slice.js +12 -6
- package/dist/resources/extensions/gsd/tools/reassess-roadmap.js +39 -0
- package/dist/resources/extensions/gsd/tools/reopen-milestone.js +11 -29
- package/dist/resources/extensions/gsd/tools/reopen-slice.js +14 -33
- package/dist/resources/extensions/gsd/tools/skip-slice.js +18 -36
- package/dist/resources/extensions/gsd/tools/validate-milestone.js +15 -78
- package/dist/resources/extensions/gsd/uat-policy.js +17 -10
- package/dist/resources/extensions/gsd/uat-run.js +9 -14
- package/dist/resources/extensions/gsd/undo.js +8 -7
- package/dist/resources/extensions/gsd/unit-closeout.js +138 -0
- package/dist/resources/extensions/gsd/unit-context-composer.js +49 -21
- package/dist/resources/extensions/gsd/unit-context-manifest.js +4 -27
- package/dist/resources/extensions/gsd/unit-registry.js +337 -0
- package/dist/resources/extensions/gsd/unit-runtime.js +3 -2
- package/dist/resources/extensions/gsd/unit-tool-contracts.js +9 -181
- package/dist/resources/extensions/gsd/user-input-boundary.js +23 -0
- package/dist/resources/extensions/gsd/validation-block-guard.js +2 -0
- package/dist/resources/extensions/gsd/web-app-uat.js +117 -0
- package/dist/resources/extensions/gsd/workflow-mcp.js +15 -102
- package/dist/resources/extensions/gsd/workflow-reconcile.js +4 -3
- package/dist/resources/extensions/gsd/workflow-tool-surface.js +46 -0
- package/dist/resources/extensions/gsd/workspace-git-guard.js +2 -0
- package/dist/resources/extensions/gsd/worktree-git-recovery.js +293 -0
- package/dist/resources/extensions/gsd/worktree-lifecycle.js +9 -1
- package/dist/resources/extensions/gsd/worktree-manager.js +45 -28
- package/dist/resources/extensions/gsd/worktree-placement.js +59 -0
- package/dist/resources/extensions/gsd/worktree-reentry.js +12 -8
- package/dist/resources/extensions/gsd/worktree-root.js +28 -6
- package/dist/resources/extensions/gsd/worktree-safety.js +8 -5
- package/dist/resources/extensions/gsd/worktree-session-state.js +12 -11
- package/dist/resources/extensions/gsd/worktree-state-projection.js +33 -4
- package/dist/resources/extensions/gsd/worktree-telemetry.js +12 -0
- package/dist/resources/extensions/search-the-web/native-search.js +5 -3
- package/dist/resources/extensions/shared/browser-contract.js +59 -0
- package/dist/resources/extensions/shared/gsd-browser-cli.js +72 -4
- package/dist/resources/extensions/shared/interview-ui.js +2 -2
- package/dist/resources/shared/claude-runtime-floor.js +182 -0
- package/dist/resources/skills/create-skill/references/executable-code.md +1 -1
- package/dist/resources/skills/create-skill/workflows/add-reference.md +8 -3
- package/dist/resources/skills/create-skill/workflows/add-script.md +4 -2
- package/dist/resources/skills/create-skill/workflows/add-template.md +3 -1
- package/dist/resources/skills/create-skill/workflows/add-workflow.md +8 -3
- package/dist/resources/skills/create-skill/workflows/upgrade-to-router.md +10 -5
- package/dist/resources/skills/create-skill/workflows/verify-skill.md +9 -4
- package/dist/resources/skills/gsd-browser/SKILL.md +1 -1
- package/dist/resources/skills/spike-wrap-up/SKILL.md +9 -9
- package/dist/tsconfig.extensions.tsbuildinfo +1 -1
- package/dist/update-cmd.js +20 -0
- package/dist/web/standalone/.next/BUILD_ID +1 -1
- package/dist/web/standalone/.next/app-path-routes-manifest.json +6 -6
- package/dist/web/standalone/.next/build-manifest.json +3 -3
- package/dist/web/standalone/.next/prerender-manifest.json +3 -3
- package/dist/web/standalone/.next/react-loadable-manifest.json +8 -8
- package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/api/boot/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/bridge-terminal/input/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/bridge-terminal/resize/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/bridge-terminal/stream/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/captures/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/cleanup/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/doctor/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/export-data/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/files/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/forensics/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/git/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/history/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/hooks/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/inspect/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/knowledge/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/live-state/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/mcp-connections/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/notifications/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/onboarding/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/projects/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/recovery/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/session/browser/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/session/command/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/session/events/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/session/manage/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/settings-data/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/shutdown/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/skill-health/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/steer/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/switch-root/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/terminal/sessions/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/terminal/stream/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/undo/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/api/visualizer/route.js.nft.json +1 -1
- package/dist/web/standalone/.next/server/app/index.html +1 -1
- package/dist/web/standalone/.next/server/app/index.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app-paths-manifest.json +6 -6
- package/dist/web/standalone/.next/server/chunks/5124.js +1 -1
- package/dist/web/standalone/.next/server/chunks/{5047.js → 5942.js} +2 -2
- package/dist/web/standalone/.next/server/chunks/8357.js +1 -1
- package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
- package/dist/web/standalone/.next/server/middleware-react-loadable-manifest.js +1 -1
- package/dist/web/standalone/.next/server/pages/404.html +1 -1
- package/dist/web/standalone/.next/server/pages/500.html +1 -1
- package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
- package/dist/web/standalone/.next/static/chunks/2659.b7b129ee6a769448.js +1 -0
- package/dist/web/standalone/.next/static/chunks/2772.bfa657f49f955239.js +1 -0
- package/dist/web/standalone/.next/static/chunks/{3616.4113d484a994e411.js → 3616.3c60753b8ffcbd2e.js} +1 -1
- package/dist/web/standalone/.next/static/chunks/4283.e4873b058df143a1.js +2 -0
- package/dist/web/standalone/.next/static/chunks/5826.a46ecdd1cfe8dabc.js +1 -0
- package/dist/web/standalone/.next/static/chunks/796.cf859a427a2cb2ac.js +10 -0
- package/dist/web/standalone/.next/static/chunks/8785.2e5a118797fb2dd2.js +1 -0
- package/dist/web/standalone/.next/static/chunks/{webpack-dda80a1ef5587410.js → webpack-fbea77b5f9953368.js} +1 -1
- package/dist/web/standalone/node_modules/node-pty/build/Makefile +1 -1
- package/dist/web/standalone/node_modules/postcss/lib/container.js +18 -26
- package/dist/web/standalone/node_modules/postcss/lib/css-syntax-error.js +14 -47
- package/dist/web/standalone/node_modules/postcss/lib/declaration.js +4 -4
- package/dist/web/standalone/node_modules/postcss/lib/fromJSON.js +3 -3
- package/dist/web/standalone/node_modules/postcss/lib/input.js +29 -54
- package/dist/web/standalone/node_modules/postcss/lib/lazy-result.js +37 -47
- package/dist/web/standalone/node_modules/postcss/lib/map-generator.js +9 -26
- package/dist/web/standalone/node_modules/postcss/lib/no-work-result.js +55 -57
- package/dist/web/standalone/node_modules/postcss/lib/node.js +31 -99
- package/dist/web/standalone/node_modules/postcss/lib/parse.js +1 -1
- package/dist/web/standalone/node_modules/postcss/lib/parser.js +9 -10
- package/dist/web/standalone/node_modules/postcss/lib/postcss.js +12 -12
- package/dist/web/standalone/node_modules/postcss/lib/previous-map.js +11 -30
- package/dist/web/standalone/node_modules/postcss/lib/processor.js +7 -7
- package/dist/web/standalone/node_modules/postcss/lib/result.js +5 -5
- package/dist/web/standalone/node_modules/postcss/lib/rule.js +6 -6
- package/dist/web/standalone/node_modules/postcss/lib/stringifier.js +28 -69
- package/dist/web/standalone/node_modules/postcss/lib/tokenize.js +2 -6
- package/dist/web/standalone/node_modules/postcss/package.json +48 -48
- package/dist/web/standalone/package.json +1 -1
- package/dist/web-mode.d.ts +2 -0
- package/dist/web-mode.js +20 -8
- package/dist/worktree-cli.js +3 -6
- package/dist/worktree-status-banner.js +7 -11
- package/package.json +2 -1
- package/packages/cloud-mcp-gateway/package.json +2 -2
- package/packages/contracts/dist/workflow.d.ts +4 -0
- package/packages/contracts/dist/workflow.d.ts.map +1 -1
- package/packages/contracts/dist/workflow.js.map +1 -1
- package/packages/contracts/package.json +1 -1
- package/packages/daemon/package.json +4 -4
- package/packages/gsd-agent-core/dist/session/agent-session-extensions.d.ts +2 -0
- package/packages/gsd-agent-core/dist/session/agent-session-extensions.d.ts.map +1 -1
- package/packages/gsd-agent-core/dist/session/agent-session-extensions.js +14 -0
- package/packages/gsd-agent-core/dist/session/agent-session-extensions.js.map +1 -1
- package/packages/gsd-agent-core/package.json +5 -5
- package/packages/gsd-agent-modes/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/packages/gsd-agent-modes/dist/modes/interactive/components/tool-execution.js +3 -0
- package/packages/gsd-agent-modes/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/packages/gsd-agent-modes/dist/modes/interactive/controllers/chat-controller.d.ts.map +1 -1
- package/packages/gsd-agent-modes/dist/modes/interactive/controllers/chat-controller.js +106 -40
- package/packages/gsd-agent-modes/dist/modes/interactive/controllers/chat-controller.js.map +1 -1
- package/packages/gsd-agent-modes/dist/modes/interactive/interactive-extension-widgets.d.ts.map +1 -1
- package/packages/gsd-agent-modes/dist/modes/interactive/interactive-extension-widgets.js +6 -0
- package/packages/gsd-agent-modes/dist/modes/interactive/interactive-extension-widgets.js.map +1 -1
- package/packages/gsd-agent-modes/package.json +7 -7
- package/packages/mcp-server/dist/cli.js +6 -3
- package/packages/mcp-server/dist/cli.js.map +1 -1
- package/packages/mcp-server/dist/server.d.ts +10 -0
- package/packages/mcp-server/dist/server.d.ts.map +1 -1
- package/packages/mcp-server/dist/server.js +8 -0
- package/packages/mcp-server/dist/server.js.map +1 -1
- package/packages/mcp-server/dist/workflow-tools.d.ts +49 -0
- package/packages/mcp-server/dist/workflow-tools.d.ts.map +1 -1
- package/packages/mcp-server/dist/workflow-tools.js +48 -22
- package/packages/mcp-server/dist/workflow-tools.js.map +1 -1
- package/packages/mcp-server/package.json +3 -3
- package/packages/native/package.json +1 -1
- package/packages/pi-agent-core/package.json +1 -1
- package/packages/pi-ai/dist/image-models.generated.d.ts +2 -2
- package/packages/pi-ai/dist/image-models.generated.js +6 -6
- package/packages/pi-ai/dist/image-models.generated.js.map +1 -1
- package/packages/pi-ai/dist/models.generated.d.ts +455 -546
- package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
- package/packages/pi-ai/dist/models.generated.js +481 -599
- package/packages/pi-ai/dist/models.generated.js.map +1 -1
- package/packages/pi-ai/package.json +1 -1
- package/packages/pi-coding-agent/dist/core/capability-patches.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/core/capability-patches.js +3 -1
- package/packages/pi-coding-agent/dist/core/capability-patches.js.map +1 -1
- package/packages/pi-coding-agent/package.json +7 -7
- package/packages/pi-tui/dist/components/input.js +1 -1
- package/packages/pi-tui/dist/components/input.js.map +1 -1
- package/packages/pi-tui/dist/keys.d.ts.map +1 -1
- package/packages/pi-tui/dist/keys.js +39 -30
- package/packages/pi-tui/dist/keys.js.map +1 -1
- package/packages/pi-tui/dist/stdin-buffer.d.ts.map +1 -1
- package/packages/pi-tui/dist/stdin-buffer.js +22 -0
- package/packages/pi-tui/dist/stdin-buffer.js.map +1 -1
- package/packages/pi-tui/dist/tui.d.ts.map +1 -1
- package/packages/pi-tui/dist/tui.js +9 -0
- package/packages/pi-tui/dist/tui.js.map +1 -1
- package/packages/pi-tui/package.json +2 -2
- package/packages/rpc-client/package.json +2 -2
- package/pkg/package.json +1 -1
- package/src/resources/extensions/ask-user-questions.ts +87 -24
- package/src/resources/extensions/bg-shell/utilities.ts +5 -2
- package/src/resources/extensions/browser-tools/engine/managed-gsd-browser.ts +265 -98
- package/src/resources/extensions/browser-tools/engine/selection.ts +90 -4
- package/src/resources/extensions/browser-tools/index.ts +71 -13
- package/src/resources/extensions/browser-tools/tests/browser-engine-selection.test.mjs +83 -13
- package/src/resources/extensions/browser-tools/tests/managed-gsd-browser-tools.test.mjs +136 -0
- package/src/resources/extensions/claude-code-cli/models.ts +9 -0
- package/src/resources/extensions/claude-code-cli/stream-adapter.ts +148 -285
- package/src/resources/extensions/claude-code-cli/tests/stream-adapter.test.ts +268 -0
- package/src/resources/extensions/claude-code-cli/turn-assembler.ts +287 -0
- package/src/resources/extensions/github-sync/templates.ts +3 -3
- package/src/resources/extensions/github-sync/tests/templates.test.ts +2 -2
- package/src/resources/extensions/gsd/artifact-projection.ts +31 -0
- package/src/resources/extensions/gsd/auto/contracts.ts +32 -2
- package/src/resources/extensions/gsd/auto/loop-deps.ts +3 -1
- package/src/resources/extensions/gsd/auto/loop.ts +83 -61
- package/src/resources/extensions/gsd/auto/orchestrator.ts +164 -17
- package/src/resources/extensions/gsd/auto/phases.ts +45 -4
- package/src/resources/extensions/gsd/auto/run-unit.ts +2 -1
- package/src/resources/extensions/gsd/auto/session.ts +4 -0
- package/src/resources/extensions/gsd/auto-dashboard.ts +18 -4
- package/src/resources/extensions/gsd/auto-dispatch.ts +20 -7
- package/src/resources/extensions/gsd/auto-model-selection.ts +8 -0
- package/src/resources/extensions/gsd/auto-post-unit.ts +29 -10
- package/src/resources/extensions/gsd/auto-prompts.ts +110 -9
- package/src/resources/extensions/gsd/auto-recovery.ts +50 -50
- package/src/resources/extensions/gsd/auto-runtime-state.ts +26 -0
- package/src/resources/extensions/gsd/auto-start.ts +29 -35
- package/src/resources/extensions/gsd/auto-timers.ts +16 -2
- package/src/resources/extensions/gsd/auto-tool-tracking.ts +54 -0
- package/src/resources/extensions/gsd/auto-unit-tool-scope.ts +19 -47
- package/src/resources/extensions/gsd/auto-verification.ts +7 -8
- package/src/resources/extensions/gsd/auto-worktree-repair.ts +13 -2
- package/src/resources/extensions/gsd/auto-worktree.ts +68 -384
- package/src/resources/extensions/gsd/auto.ts +35 -32
- package/src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts +3 -5
- package/src/resources/extensions/gsd/bootstrap/db-tools.ts +29 -37
- package/src/resources/extensions/gsd/bootstrap/dynamic-tools.ts +20 -43
- package/src/resources/extensions/gsd/bootstrap/query-tools.ts +2 -2
- package/src/resources/extensions/gsd/bootstrap/register-extension.ts +24 -0
- package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +251 -161
- package/src/resources/extensions/gsd/bootstrap/write-gate.ts +136 -6
- package/src/resources/extensions/gsd/branch-patterns.ts +3 -0
- package/src/resources/extensions/gsd/browser-evidence.ts +18 -2
- package/src/resources/extensions/gsd/captures.ts +5 -16
- package/src/resources/extensions/gsd/closeout-consistency-gate.ts +27 -5
- package/src/resources/extensions/gsd/closeout-recovery.ts +2 -1
- package/src/resources/extensions/gsd/codebase-generator.ts +9 -5
- package/src/resources/extensions/gsd/commands/catalog.ts +6 -68
- package/src/resources/extensions/gsd/commands/handlers/auto.ts +3 -0
- package/src/resources/extensions/gsd/commands-handlers.ts +18 -0
- package/src/resources/extensions/gsd/commands-inspect.ts +7 -8
- package/src/resources/extensions/gsd/commands-maintenance.ts +74 -40
- package/src/resources/extensions/gsd/commands-ship.ts +2 -2
- package/src/resources/extensions/gsd/commands-verdict.ts +19 -2
- package/src/resources/extensions/gsd/crash-recovery.ts +3 -9
- package/src/resources/extensions/gsd/db/engine.ts +809 -0
- package/src/resources/extensions/gsd/db/queries.ts +453 -0
- package/src/resources/extensions/gsd/db/sql-constants.ts +12 -0
- package/src/resources/extensions/gsd/db/writers/cascades.ts +237 -0
- package/src/resources/extensions/gsd/db/writers/import-restore.ts +310 -0
- package/src/resources/extensions/gsd/db/writers/memory.ts +220 -0
- package/src/resources/extensions/gsd/db/writers/reconcile.ts +500 -0
- package/src/resources/extensions/gsd/db/writers/status.ts +88 -0
- package/src/resources/extensions/gsd/db-workspace.ts +170 -0
- package/src/resources/extensions/gsd/delegation-policy.ts +3 -11
- package/src/resources/extensions/gsd/discussion-handoff.ts +276 -0
- package/src/resources/extensions/gsd/docs/preferences-reference.md +9 -0
- package/src/resources/extensions/gsd/doctor-environment.ts +5 -13
- package/src/resources/extensions/gsd/doctor-format.ts +12 -7
- package/src/resources/extensions/gsd/doctor-git-checks.ts +3 -3
- package/src/resources/extensions/gsd/doctor-runtime-checks.ts +22 -17
- package/src/resources/extensions/gsd/doctor.ts +15 -5
- package/src/resources/extensions/gsd/error-classifier.ts +12 -1
- package/src/resources/extensions/gsd/git-conflict-state.ts +17 -1
- package/src/resources/extensions/gsd/git-service.ts +1 -0
- package/src/resources/extensions/gsd/gitignore.ts +3 -0
- package/src/resources/extensions/gsd/gsd-db.ts +185 -2373
- package/src/resources/extensions/gsd/guidance.ts +139 -0
- package/src/resources/extensions/gsd/guided-flow.ts +95 -561
- package/src/resources/extensions/gsd/guided-unit-completion.ts +275 -0
- package/src/resources/extensions/gsd/markdown-renderer.ts +2 -1
- package/src/resources/extensions/gsd/mcp-filter.ts +4 -24
- package/src/resources/extensions/gsd/mcp-tool-name.ts +30 -0
- package/src/resources/extensions/gsd/md-importer.ts +3 -3
- package/src/resources/extensions/gsd/memory-consolidation-scanner.ts +1 -1
- package/src/resources/extensions/gsd/migrate/safety.ts +20 -9
- package/src/resources/extensions/gsd/migration-auto-check.ts +30 -5
- package/src/resources/extensions/gsd/milestone-closeout-proof.ts +131 -0
- package/src/resources/extensions/gsd/milestone-closeout.ts +12 -4
- package/src/resources/extensions/gsd/milestone-merge-transaction.ts +47 -0
- package/src/resources/extensions/gsd/milestone-planning-persistence.ts +224 -0
- package/src/resources/extensions/gsd/milestone-readiness.ts +125 -0
- package/src/resources/extensions/gsd/milestone-settlement.ts +81 -0
- package/src/resources/extensions/gsd/milestone-validation-evidence.ts +95 -0
- package/src/resources/extensions/gsd/milestone-validation-verdict.ts +80 -0
- package/src/resources/extensions/gsd/model-cost-table.ts +1 -0
- package/src/resources/extensions/gsd/model-router.ts +3 -0
- package/src/resources/extensions/gsd/notification-store.ts +26 -3
- package/src/resources/extensions/gsd/parallel-eligibility.ts +4 -5
- package/src/resources/extensions/gsd/parallel-merge.ts +12 -9
- package/src/resources/extensions/gsd/parallel-monitor-overlay.ts +10 -7
- package/src/resources/extensions/gsd/parallel-orchestrator.ts +6 -2
- package/src/resources/extensions/gsd/paths.ts +42 -22
- package/src/resources/extensions/gsd/pre-execution-checks.ts +109 -3
- package/src/resources/extensions/gsd/preferences-diagnostics.ts +98 -0
- package/src/resources/extensions/gsd/preferences-models.ts +10 -46
- package/src/resources/extensions/gsd/preferences-types.ts +16 -0
- package/src/resources/extensions/gsd/preferences.ts +191 -28
- package/src/resources/extensions/gsd/prompts/complete-slice.md +1 -0
- package/src/resources/extensions/gsd/prompts/execute-task.md +2 -0
- package/src/resources/extensions/gsd/prompts/guided-discuss-project.md +3 -1
- package/src/resources/extensions/gsd/prompts/plan-milestone.md +2 -0
- package/src/resources/extensions/gsd/prompts/plan-slice.md +2 -1
- package/src/resources/extensions/gsd/prompts/refine-slice.md +2 -1
- package/src/resources/extensions/gsd/prompts/replan-slice.md +1 -1
- package/src/resources/extensions/gsd/prompts/system.md +1 -1
- package/src/resources/extensions/gsd/provider-error-guidance.ts +4 -9
- package/src/resources/extensions/gsd/provider-payload-policy.ts +140 -0
- package/src/resources/extensions/gsd/provider-switch-observer.ts +1 -1
- package/src/resources/extensions/gsd/publication.ts +122 -0
- package/src/resources/extensions/gsd/pull-request-process.ts +41 -0
- package/src/resources/extensions/gsd/quality-gate-closure.ts +140 -0
- package/src/resources/extensions/gsd/question-transport.ts +138 -0
- package/src/resources/extensions/gsd/recovery-classification.ts +47 -88
- package/src/resources/extensions/gsd/roadmap-slices.ts +8 -2
- package/src/resources/extensions/gsd/safety/evidence-collector.ts +36 -4
- package/src/resources/extensions/gsd/safety/evidence-cross-ref.ts +7 -2
- package/src/resources/extensions/gsd/safety/file-change-validator.ts +14 -0
- package/src/resources/extensions/gsd/slice-parallel-orchestrator.ts +6 -2
- package/src/resources/extensions/gsd/state-transition-matrix.ts +42 -0
- package/src/resources/extensions/gsd/state.ts +19 -26
- package/src/resources/extensions/gsd/status-guards.ts +59 -8
- package/src/resources/extensions/gsd/stop-notice.ts +75 -0
- package/src/resources/extensions/gsd/templates/plan.md +7 -0
- package/src/resources/extensions/gsd/templates/project.md +1 -0
- package/src/resources/extensions/gsd/templates/roadmap.md +1 -1
- package/src/resources/extensions/gsd/templates/uat.md +5 -1
- package/src/resources/extensions/gsd/tests/ask-user-questions-render.test.ts +92 -0
- package/src/resources/extensions/gsd/tests/auto-dashboard.test.ts +29 -1
- package/src/resources/extensions/gsd/tests/auto-loop.test.ts +444 -5
- package/src/resources/extensions/gsd/tests/auto-milestone-target.test.ts +23 -0
- package/src/resources/extensions/gsd/tests/auto-model-selection-tool-poisoning.test.ts +18 -0
- package/src/resources/extensions/gsd/tests/auto-orchestrator.test.ts +133 -4
- package/src/resources/extensions/gsd/tests/auto-paused-ui-cleanup.test.ts +3 -1
- package/src/resources/extensions/gsd/tests/auto-post-unit-evidence-crossref-4909.test.ts +46 -0
- package/src/resources/extensions/gsd/tests/auto-runtime-state.test.ts +34 -0
- package/src/resources/extensions/gsd/tests/auto-worktree-registry.test.ts +2 -2
- package/src/resources/extensions/gsd/tests/auto-worktree-repair.test.ts +4 -2
- package/src/resources/extensions/gsd/tests/browser-automation-contract-fixture.ts +39 -0
- package/src/resources/extensions/gsd/tests/browser-contract.test.ts +44 -0
- package/src/resources/extensions/gsd/tests/canonical-milestone-root.test.ts +20 -0
- package/src/resources/extensions/gsd/tests/checkout-branch-stash-guard.test.ts +66 -1
- package/src/resources/extensions/gsd/tests/clear-stale-autostart.test.ts +44 -0
- package/src/resources/extensions/gsd/tests/codebase-generator.test.ts +22 -0
- package/src/resources/extensions/gsd/tests/commands-dispatcher-workspace-git.test.ts +11 -0
- package/src/resources/extensions/gsd/tests/commands-verdict.test.ts +46 -8
- package/src/resources/extensions/gsd/tests/dispatch-complete-milestone-guard.test.ts +34 -3
- package/src/resources/extensions/gsd/tests/dispatch-run-uat-browser-tools.test.ts +89 -0
- package/src/resources/extensions/gsd/tests/doctor-scope-db-unavailable.test.ts +18 -0
- package/src/resources/extensions/gsd/tests/evidence-xref-gsd-exec.test.ts +157 -0
- package/src/resources/extensions/gsd/tests/execute-task-rendering.test.ts +1 -0
- package/src/resources/extensions/gsd/tests/extension-bootstrap-isolation.test.ts +35 -1
- package/src/resources/extensions/gsd/tests/file-change-validator.test.ts +33 -1
- package/src/resources/extensions/gsd/tests/fixtures/pr-body/swarm-lane-no-blockers.md +1 -5
- package/src/resources/extensions/gsd/tests/fixtures/pr-body/swarm-lane-with-blockers.md +1 -5
- package/src/resources/extensions/gsd/tests/gate-state-canonicalization.test.ts +48 -1
- package/src/resources/extensions/gsd/tests/guidance.test.ts +125 -0
- package/src/resources/extensions/gsd/tests/integration/auto-worktree-milestone-merge.test.ts +58 -15
- package/src/resources/extensions/gsd/tests/integration/auto-worktree.test.ts +74 -59
- package/src/resources/extensions/gsd/tests/integration/git-service.test.ts +3 -2
- package/src/resources/extensions/gsd/tests/integration/gsd-integration-fixture.ts +80 -0
- package/src/resources/extensions/gsd/tests/mcp-tool-name.test.ts +34 -0
- package/src/resources/extensions/gsd/tests/migration-auto-check.test.ts +143 -1
- package/src/resources/extensions/gsd/tests/milestone-closeout-proof.test.ts +99 -0
- package/src/resources/extensions/gsd/tests/milestone-closeout.test.ts +25 -0
- package/src/resources/extensions/gsd/tests/milestone-merge-transaction.test.ts +46 -0
- package/src/resources/extensions/gsd/tests/milestone-readiness.test.ts +65 -0
- package/src/resources/extensions/gsd/tests/milestone-validation-evidence.test.ts +41 -0
- package/src/resources/extensions/gsd/tests/milestone-validation-verdict.test.ts +55 -0
- package/src/resources/extensions/gsd/tests/notification-store.test.ts +32 -0
- package/src/resources/extensions/gsd/tests/plan-milestone.test.ts +45 -0
- package/src/resources/extensions/gsd/tests/plan-slice-prompt.test.ts +2 -0
- package/src/resources/extensions/gsd/tests/planning-crossval.test.ts +45 -0
- package/src/resources/extensions/gsd/tests/pre-execution-checks.test.ts +193 -1
- package/src/resources/extensions/gsd/tests/preferences-diagnostics.test.ts +67 -0
- package/src/resources/extensions/gsd/tests/preferences.test.ts +183 -0
- package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +46 -0
- package/src/resources/extensions/gsd/tests/provider-error-guidance.test.ts +3 -3
- package/src/resources/extensions/gsd/tests/provider-errors.test.ts +9 -0
- package/src/resources/extensions/gsd/tests/provider-payload-policy.test.ts +165 -0
- package/src/resources/extensions/gsd/tests/publication.test.ts +120 -0
- package/src/resources/extensions/gsd/tests/pull-request-process.test.ts +47 -0
- package/src/resources/extensions/gsd/tests/recovery-classification-illegal-transition.test.ts +30 -0
- package/src/resources/extensions/gsd/tests/register-hooks-depth-verification.test.ts +342 -1
- package/src/resources/extensions/gsd/tests/roadmap-parse-regression.test.ts +40 -0
- package/src/resources/extensions/gsd/tests/runtime-invariant-modules.test.ts +26 -1
- package/src/resources/extensions/gsd/tests/safety-harness-false-positives.test.ts +38 -0
- package/src/resources/extensions/gsd/tests/session-start-footer.test.ts +80 -0
- package/src/resources/extensions/gsd/tests/session-switch-clears-pending-autostart.test.ts +108 -0
- package/src/resources/extensions/gsd/tests/single-writer-invariant.test.ts +144 -7
- package/src/resources/extensions/gsd/tests/stale-queued-milestone.test.ts +27 -0
- package/src/resources/extensions/gsd/tests/start-auto-detached.test.ts +2 -1
- package/src/resources/extensions/gsd/tests/state-transition-matrix.test.ts +36 -0
- package/src/resources/extensions/gsd/tests/status-guards.test.ts +38 -0
- package/src/resources/extensions/gsd/tests/stop-notice.test.ts +70 -0
- package/src/resources/extensions/gsd/tests/token-tool-gating.test.ts +35 -0
- package/src/resources/extensions/gsd/tests/tool-availability-audit.test.ts +35 -0
- package/src/resources/extensions/gsd/tests/tool-invocation-error-loop-break.test.ts +8 -0
- package/src/resources/extensions/gsd/tests/tool-naming.test.ts +35 -42
- package/src/resources/extensions/gsd/tests/tool-surface-readiness.test.ts +155 -0
- package/src/resources/extensions/gsd/tests/uat-policy.test.ts +24 -6
- package/src/resources/extensions/gsd/tests/unit-closeout.test.ts +209 -0
- package/src/resources/extensions/gsd/tests/unit-context-composer.test.ts +70 -2
- package/src/resources/extensions/gsd/tests/unit-registry.test.ts +163 -0
- package/src/resources/extensions/gsd/tests/user-input-boundary.test.ts +86 -1
- package/src/resources/extensions/gsd/tests/validate-milestone-stuck-guard.test.ts +39 -0
- package/src/resources/extensions/gsd/tests/web-app-uat.test.ts +193 -0
- package/src/resources/extensions/gsd/tests/workflow-mcp.test.ts +128 -11
- package/src/resources/extensions/gsd/tests/workflow-tool-executors.test.ts +2 -2
- package/src/resources/extensions/gsd/tests/workspace-git-preflight.test.ts +15 -0
- package/src/resources/extensions/gsd/tests/worktree-lifecycle.test.ts +41 -4
- package/src/resources/extensions/gsd/tests/worktree-manager.test.ts +43 -1
- package/src/resources/extensions/gsd/tests/worktree-placement.test.ts +113 -0
- package/src/resources/extensions/gsd/tests/worktree-projection-writers.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/worktree-reentry.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/worktree-safety.test.ts +3 -1
- package/src/resources/extensions/gsd/tests/worktree-symlink-removal.test.ts +12 -6
- package/src/resources/extensions/gsd/tests/worktree-teardown-safety.test.ts +2 -2
- package/src/resources/extensions/gsd/tests/worktree-telemetry.test.ts +22 -0
- package/src/resources/extensions/gsd/tests/write-gate.test.ts +121 -0
- package/src/resources/extensions/gsd/tool-contract.ts +86 -8
- package/src/resources/extensions/gsd/tool-presentation-plan.ts +16 -33
- package/src/resources/extensions/gsd/tool-surface-readiness.ts +76 -0
- package/src/resources/extensions/gsd/tool-surface-snapshot.ts +47 -0
- package/src/resources/extensions/gsd/tools/complete-slice.ts +23 -58
- package/src/resources/extensions/gsd/tools/exec-tool.ts +5 -8
- package/src/resources/extensions/gsd/tools/plan-milestone.ts +19 -160
- package/src/resources/extensions/gsd/tools/plan-slice.ts +12 -6
- package/src/resources/extensions/gsd/tools/reassess-roadmap.ts +43 -0
- package/src/resources/extensions/gsd/tools/reopen-milestone.ts +11 -38
- package/src/resources/extensions/gsd/tools/reopen-slice.ts +14 -42
- package/src/resources/extensions/gsd/tools/skip-slice.ts +18 -44
- package/src/resources/extensions/gsd/tools/validate-milestone.ts +25 -84
- package/src/resources/extensions/gsd/uat-policy.ts +20 -10
- package/src/resources/extensions/gsd/uat-run.ts +10 -14
- package/src/resources/extensions/gsd/undo.ts +9 -8
- package/src/resources/extensions/gsd/unit-closeout.ts +201 -0
- package/src/resources/extensions/gsd/unit-context-composer.ts +97 -21
- package/src/resources/extensions/gsd/unit-context-manifest.ts +4 -28
- package/src/resources/extensions/gsd/unit-registry.ts +412 -0
- package/src/resources/extensions/gsd/unit-runtime.ts +3 -2
- package/src/resources/extensions/gsd/unit-tool-contracts.ts +27 -191
- package/src/resources/extensions/gsd/user-input-boundary.ts +18 -0
- package/src/resources/extensions/gsd/validation-block-guard.ts +2 -0
- package/src/resources/extensions/gsd/web-app-uat.ts +144 -0
- package/src/resources/extensions/gsd/workflow-mcp.ts +22 -110
- package/src/resources/extensions/gsd/workflow-reconcile.ts +3 -3
- package/src/resources/extensions/gsd/workflow-tool-surface.ts +76 -0
- package/src/resources/extensions/gsd/workspace-git-guard.ts +1 -0
- package/src/resources/extensions/gsd/worktree-git-recovery.ts +314 -0
- package/src/resources/extensions/gsd/worktree-lifecycle.ts +17 -17
- package/src/resources/extensions/gsd/worktree-manager.ts +47 -28
- package/src/resources/extensions/gsd/worktree-placement.ts +63 -0
- package/src/resources/extensions/gsd/worktree-reentry.ts +10 -7
- package/src/resources/extensions/gsd/worktree-root.ts +29 -6
- package/src/resources/extensions/gsd/worktree-safety.ts +8 -5
- package/src/resources/extensions/gsd/worktree-session-state.ts +11 -11
- package/src/resources/extensions/gsd/worktree-state-projection.ts +55 -7
- package/src/resources/extensions/gsd/worktree-telemetry.ts +16 -0
- package/src/resources/extensions/search-the-web/native-search.ts +5 -3
- package/src/resources/extensions/shared/browser-contract.ts +66 -0
- package/src/resources/extensions/shared/gsd-browser-cli.ts +88 -4
- package/src/resources/extensions/shared/interview-ui.ts +15 -2
- package/src/resources/shared/claude-runtime-floor.ts +248 -0
- package/src/resources/skills/create-skill/references/executable-code.md +1 -1
- package/src/resources/skills/create-skill/workflows/add-reference.md +8 -3
- package/src/resources/skills/create-skill/workflows/add-script.md +4 -2
- package/src/resources/skills/create-skill/workflows/add-template.md +3 -1
- package/src/resources/skills/create-skill/workflows/add-workflow.md +8 -3
- package/src/resources/skills/create-skill/workflows/upgrade-to-router.md +10 -5
- package/src/resources/skills/create-skill/workflows/verify-skill.md +9 -4
- package/src/resources/skills/gsd-browser/SKILL.md +1 -1
- package/src/resources/skills/spike-wrap-up/SKILL.md +9 -9
- package/dist/web/standalone/.next/static/chunks/2659.feb6499ca863ebfc.js +0 -1
- package/dist/web/standalone/.next/static/chunks/2772.151789db0edea835.js +0 -1
- package/dist/web/standalone/.next/static/chunks/4283.10a065467b5340d8.js +0 -2
- package/dist/web/standalone/.next/static/chunks/5826.960dc4634cc9b0d3.js +0 -1
- package/dist/web/standalone/.next/static/chunks/796.46f811c0fac23aab.js +0 -10
- package/dist/web/standalone/.next/static/chunks/8785.d32f7a61f55c1600.js +0 -1
- /package/dist/web/standalone/.next/static/{Qbr81pQ-pbQXP4bq2VXLv → AOpDeK_gJHU8OZjRo31gQ}/_buildManifest.js +0 -0
- /package/dist/web/standalone/.next/static/{Qbr81pQ-pbQXP4bq2VXLv → AOpDeK_gJHU8OZjRo31gQ}/_ssgManifest.js +0 -0
|
@@ -1,757 +1,44 @@
|
|
|
1
1
|
// Project/App: gsd-pi
|
|
2
|
-
// File Purpose: GSD
|
|
3
|
-
// GSD Database Abstraction Layer
|
|
4
|
-
// Provides a SQLite database with provider fallback chain:
|
|
5
|
-
// node:sqlite (built-in) → better-sqlite3 (npm) → null (unavailable)
|
|
6
|
-
//
|
|
7
|
-
// Exposes a unified sync API for decisions and requirements storage.
|
|
8
|
-
// Schema is initialized on first open with WAL mode for file-backed DBs.
|
|
2
|
+
// File Purpose: GSD single-writer barrel + write/read wrappers.
|
|
9
3
|
//
|
|
10
4
|
// ─── Single-writer invariant ─────────────────────────────────────────────
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
5
|
+
// Every write-SQL statement against `.gsd/gsd.db` lives behind a typed
|
|
6
|
+
// wrapper in the single-writer layer (this file plus db/writers/*). Connection
|
|
7
|
+
// ownership, lifecycle, schema/migrations and transaction primitives live in
|
|
8
|
+
// db/engine.ts and are re-exported here for backward compatibility, so callers
|
|
9
|
+
// keep importing from "./gsd-db.js".
|
|
16
10
|
//
|
|
17
|
-
// `_getAdapter()` is retained for read-only
|
|
18
|
-
//
|
|
19
|
-
// Do NOT use it for writes — add a wrapper here instead.
|
|
11
|
+
// `_getAdapter()` (re-exported from the engine) is retained for read-only
|
|
12
|
+
// SELECTs in query modules. Do NOT use it for writes — add a wrapper here.
|
|
20
13
|
//
|
|
21
|
-
// The separate `.gsd/unit-claims.db`
|
|
22
|
-
//
|
|
23
|
-
// excluded from this invariant.
|
|
24
|
-
import { createRequire } from "node:module";
|
|
14
|
+
// The separate `.gsd/unit-claims.db` (unit-ownership.ts) is an intentionally
|
|
15
|
+
// independent store and is excluded from this invariant.
|
|
25
16
|
import { createHash } from "node:crypto";
|
|
26
|
-
import { existsSync, copyFileSync, mkdirSync, realpathSync } from "node:fs";
|
|
27
|
-
import { dirname, join } from "node:path";
|
|
28
17
|
import { GSDError, GSD_STALE_STATE } from "./errors.js";
|
|
29
18
|
import { getGateIdsForTurn } from "./gate-registry.js";
|
|
30
|
-
import {
|
|
31
|
-
import { createDbAdapter } from "./db-adapter.js";
|
|
32
|
-
import { createBaseSchemaObjects } from "./db-base-schema.js";
|
|
33
|
-
import { createCoordinationTablesV24 } from "./db-coordination-schema.js";
|
|
34
|
-
import { createDbConnectionCache } from "./db-connection-cache.js";
|
|
35
|
-
import { emptyTaskStatusCounts, rowToActiveTaskSummary, rowToIdStatusSummary, rowToTaskStatusCounts, rowsToStringColumn, } from "./db-lightweight-query-rows.js";
|
|
36
|
-
import { rowToActiveDecision, rowToActiveRequirement, rowToDecision, rowToRequirement, rowsToRequirementCounts, } from "./db-decision-requirement-rows.js";
|
|
37
|
-
import { rowToGate } from "./db-gate-rows.js";
|
|
38
|
-
import { rowToArtifact, rowToMilestone } from "./db-milestone-artifact-rows.js";
|
|
39
|
-
import { backupDatabaseBeforeMigration } from "./db-migration-backup.js";
|
|
19
|
+
import { logWarning } from "./workflow-logger.js";
|
|
40
20
|
import { isClosedStatus } from "./status-guards.js";
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
import {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
_gsdRequire = null;
|
|
59
|
-
}
|
|
60
|
-
return _gsdRequire;
|
|
61
|
-
}
|
|
62
|
-
const providerLoader = createSqliteProviderLoader({
|
|
63
|
-
tryRequireNodeSqlite: () => {
|
|
64
|
-
const req = getGsdRequire();
|
|
65
|
-
if (!req)
|
|
66
|
-
throw new Error("unavailable");
|
|
67
|
-
return req("node:sqlite");
|
|
68
|
-
},
|
|
69
|
-
tryRequireBetterSqlite3: () => {
|
|
70
|
-
const req = getGsdRequire();
|
|
71
|
-
if (!req)
|
|
72
|
-
throw new Error("unavailable");
|
|
73
|
-
return req(BETTER_SQLITE3_PACKAGE);
|
|
74
|
-
},
|
|
75
|
-
suppressSqliteWarning,
|
|
76
|
-
nodeVersion: process.versions.node,
|
|
77
|
-
writeStderr: (message) => process.stderr.write(message),
|
|
78
|
-
});
|
|
79
|
-
export const SCHEMA_VERSION = 29;
|
|
80
|
-
const TERMINAL_STATUS_SQL = "'complete', 'done', 'skipped', 'closed'";
|
|
81
|
-
function initSchema(db, fileBacked, dbPath) {
|
|
82
|
-
const conservativeFilePragmas = fileBacked && _isLikelyWslDrvFsPathForTest(dbPath);
|
|
83
|
-
if (fileBacked)
|
|
84
|
-
db.exec(conservativeFilePragmas ? "PRAGMA journal_mode=DELETE" : "PRAGMA journal_mode=WAL");
|
|
85
|
-
if (fileBacked)
|
|
86
|
-
db.exec("PRAGMA busy_timeout = 5000");
|
|
87
|
-
if (fileBacked)
|
|
88
|
-
db.exec(conservativeFilePragmas ? "PRAGMA synchronous = FULL" : "PRAGMA synchronous = NORMAL");
|
|
89
|
-
if (fileBacked)
|
|
90
|
-
db.exec("PRAGMA auto_vacuum = INCREMENTAL");
|
|
91
|
-
if (fileBacked)
|
|
92
|
-
db.exec("PRAGMA cache_size = -8000"); // 8 MB page cache
|
|
93
|
-
if (fileBacked && !conservativeFilePragmas && process.platform !== "darwin")
|
|
94
|
-
db.exec("PRAGMA mmap_size = 67108864"); // 64 MB mmap
|
|
95
|
-
db.exec("PRAGMA temp_store = MEMORY");
|
|
96
|
-
db.exec("PRAGMA foreign_keys = ON");
|
|
97
|
-
db.exec("BEGIN");
|
|
98
|
-
try {
|
|
99
|
-
createBaseSchemaObjects(db, {
|
|
100
|
-
tryCreateMemoriesFts,
|
|
101
|
-
ensureVerificationEvidenceDedupIndex,
|
|
102
|
-
});
|
|
103
|
-
const existing = db.prepare("SELECT count(*) as cnt FROM schema_version").get();
|
|
104
|
-
if (existing && existing["cnt"] === 0) {
|
|
105
|
-
createCoordinationTablesV24(db);
|
|
106
|
-
createRuntimeKvTableV25(db);
|
|
107
|
-
// Fresh install — all tables are created above with the full current schema,
|
|
108
|
-
// so it is safe to create all migration-specific indexes here. For existing
|
|
109
|
-
// databases these indexes are created inside the individual migration guards
|
|
110
|
-
// in migrateSchema() after the corresponding columns have been added.
|
|
111
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_tasks_escalation_pending ON tasks(milestone_id, slice_id, escalation_pending)");
|
|
112
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_memories_scope ON memories(scope)");
|
|
113
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_memory_sources_kind ON memory_sources(kind)");
|
|
114
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_memory_sources_scope ON memory_sources(scope)");
|
|
115
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_memory_relations_from ON memory_relations(from_id)");
|
|
116
|
-
db.exec("CREATE INDEX IF NOT EXISTS idx_memory_relations_to ON memory_relations(to_id)");
|
|
117
|
-
recordSchemaVersion(db, SCHEMA_VERSION);
|
|
118
|
-
}
|
|
119
|
-
db.exec("COMMIT");
|
|
120
|
-
}
|
|
121
|
-
catch (err) {
|
|
122
|
-
db.exec("ROLLBACK");
|
|
123
|
-
throw err;
|
|
124
|
-
}
|
|
125
|
-
migrateSchema(db);
|
|
126
|
-
}
|
|
127
|
-
export function _isLikelyWslDrvFsPathForTest(dbPath) {
|
|
128
|
-
if (!dbPath || process.platform !== "linux")
|
|
129
|
-
return false;
|
|
130
|
-
const drvFsPathPattern = /^\/mnt\/[a-z](?:\/|$)/i;
|
|
131
|
-
if (drvFsPathPattern.test(dbPath))
|
|
132
|
-
return true;
|
|
133
|
-
try {
|
|
134
|
-
return drvFsPathPattern.test(realpathSync(dbPath));
|
|
135
|
-
}
|
|
136
|
-
catch {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Create the FTS5 virtual table for memories plus the triggers that keep it
|
|
142
|
-
* in sync with the base table. FTS5 may be unavailable on stripped-down
|
|
143
|
-
* SQLite builds — callers should treat failure as non-fatal and fall back
|
|
144
|
-
* to LIKE-based scans in `memory-store.queryMemoriesRanked`.
|
|
145
|
-
*/
|
|
146
|
-
export function tryCreateMemoriesFts(db) {
|
|
147
|
-
return tryCreateMemoriesFtsSchema(db, {
|
|
148
|
-
onUnavailable: (message) => logWarning("db", message),
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
export function isMemoriesFtsAvailable(db) {
|
|
152
|
-
return isMemoriesFtsAvailableSchema(db);
|
|
153
|
-
}
|
|
154
|
-
function backfillMemoriesFts(db) {
|
|
155
|
-
db.exec(`INSERT INTO memories_fts(rowid, content) SELECT seq, content FROM memories`);
|
|
156
|
-
}
|
|
157
|
-
function copyQualityGateRowsToRepairedTable(db) {
|
|
158
|
-
db.exec(`
|
|
159
|
-
INSERT OR IGNORE INTO quality_gates_new
|
|
160
|
-
(milestone_id, slice_id, gate_id, scope, task_id, status, verdict, rationale, findings, evaluated_at)
|
|
161
|
-
SELECT milestone_id, slice_id, gate_id, scope, COALESCE(task_id, ''), status, verdict, rationale, findings, evaluated_at
|
|
162
|
-
FROM quality_gates
|
|
163
|
-
`);
|
|
164
|
-
}
|
|
165
|
-
function migrateSchema(db) {
|
|
166
|
-
const currentVersion = getCurrentSchemaVersion(db);
|
|
167
|
-
if (currentVersion >= SCHEMA_VERSION)
|
|
168
|
-
return;
|
|
169
|
-
backupDatabaseBeforeMigration(db, currentPath, currentVersion, {
|
|
170
|
-
existsSync,
|
|
171
|
-
copyFileSync,
|
|
172
|
-
logWarning,
|
|
173
|
-
});
|
|
174
|
-
db.exec("BEGIN");
|
|
175
|
-
try {
|
|
176
|
-
if (currentVersion < 2) {
|
|
177
|
-
applyMigrationV2Artifacts(db);
|
|
178
|
-
recordSchemaVersion(db, 2);
|
|
179
|
-
}
|
|
180
|
-
if (currentVersion < 3) {
|
|
181
|
-
applyMigrationV3Memories(db);
|
|
182
|
-
recordSchemaVersion(db, 3);
|
|
183
|
-
}
|
|
184
|
-
if (currentVersion < 4) {
|
|
185
|
-
applyMigrationV4DecisionMadeBy(db);
|
|
186
|
-
recordSchemaVersion(db, 4);
|
|
187
|
-
}
|
|
188
|
-
if (currentVersion < 5) {
|
|
189
|
-
applyMigrationV5HierarchyTables(db);
|
|
190
|
-
recordSchemaVersion(db, 5);
|
|
191
|
-
}
|
|
192
|
-
if (currentVersion < 6) {
|
|
193
|
-
applyMigrationV6SliceSummaries(db);
|
|
194
|
-
recordSchemaVersion(db, 6);
|
|
195
|
-
}
|
|
196
|
-
if (currentVersion < 7) {
|
|
197
|
-
applyMigrationV7Dependencies(db);
|
|
198
|
-
recordSchemaVersion(db, 7);
|
|
199
|
-
}
|
|
200
|
-
if (currentVersion < 8) {
|
|
201
|
-
applyMigrationV8PlanningFields(db);
|
|
202
|
-
recordSchemaVersion(db, 8);
|
|
203
|
-
}
|
|
204
|
-
if (currentVersion < 9) {
|
|
205
|
-
applyMigrationV9Ordering(db);
|
|
206
|
-
recordSchemaVersion(db, 9);
|
|
207
|
-
}
|
|
208
|
-
if (currentVersion < 10) {
|
|
209
|
-
applyMigrationV10ReplanTrigger(db);
|
|
210
|
-
recordSchemaVersion(db, 10);
|
|
211
|
-
}
|
|
212
|
-
if (currentVersion < 11) {
|
|
213
|
-
applyMigrationV11TaskPlanning(db);
|
|
214
|
-
recordSchemaVersion(db, 11);
|
|
215
|
-
}
|
|
216
|
-
if (currentVersion < 12) {
|
|
217
|
-
// NOTE: The original DDL used COALESCE(task_id, '') in the PRIMARY KEY
|
|
218
|
-
// expression, which is invalid SQLite syntax and causes startup errors on
|
|
219
|
-
// DBs that migrate through v12. The corrected DDL uses
|
|
220
|
-
// task_id TEXT NOT NULL DEFAULT '' with a plain column list PK. DBs that
|
|
221
|
-
// were created with the broken DDL are repaired by the v22 migration below.
|
|
222
|
-
applyMigrationV12QualityGates(db);
|
|
223
|
-
recordSchemaVersion(db, 12);
|
|
224
|
-
}
|
|
225
|
-
if (currentVersion < 13) {
|
|
226
|
-
applyMigrationV13HotPathIndexes(db, ensureVerificationEvidenceDedupIndex);
|
|
227
|
-
recordSchemaVersion(db, 13);
|
|
228
|
-
}
|
|
229
|
-
if (currentVersion < 14) {
|
|
230
|
-
applyMigrationV14SliceDependencies(db);
|
|
231
|
-
recordSchemaVersion(db, 14);
|
|
232
|
-
}
|
|
233
|
-
if (currentVersion < 15) {
|
|
234
|
-
applyMigrationV15AuditTables(db);
|
|
235
|
-
recordSchemaVersion(db, 15);
|
|
236
|
-
}
|
|
237
|
-
if (currentVersion < 16) {
|
|
238
|
-
applyMigrationV16EscalationSource(db);
|
|
239
|
-
recordSchemaVersion(db, 16);
|
|
240
|
-
}
|
|
241
|
-
if (currentVersion < 17) {
|
|
242
|
-
applyMigrationV17TaskEscalation(db);
|
|
243
|
-
recordSchemaVersion(db, 17);
|
|
244
|
-
}
|
|
245
|
-
if (currentVersion < 18) {
|
|
246
|
-
applyMigrationV18MemorySources(db);
|
|
247
|
-
recordSchemaVersion(db, 18);
|
|
248
|
-
}
|
|
249
|
-
if (currentVersion < 19) {
|
|
250
|
-
applyMigrationV19MemoryFts(db, {
|
|
251
|
-
tryCreateMemoriesFts,
|
|
252
|
-
isMemoriesFtsAvailable,
|
|
253
|
-
backfillMemoriesFts,
|
|
254
|
-
logWarning,
|
|
255
|
-
});
|
|
256
|
-
recordSchemaVersion(db, 19);
|
|
257
|
-
}
|
|
258
|
-
if (currentVersion < 20) {
|
|
259
|
-
applyMigrationV20MemoryRelations(db);
|
|
260
|
-
recordSchemaVersion(db, 20);
|
|
261
|
-
}
|
|
262
|
-
if (currentVersion < 21) {
|
|
263
|
-
applyMigrationV21StructuredMemories(db);
|
|
264
|
-
recordSchemaVersion(db, 21);
|
|
265
|
-
}
|
|
266
|
-
if (currentVersion < 22) {
|
|
267
|
-
applyMigrationV22QualityGateRepair(db, { copyQualityGateRowsToRepairedTable });
|
|
268
|
-
recordSchemaVersion(db, 22);
|
|
269
|
-
}
|
|
270
|
-
if (currentVersion < 23) {
|
|
271
|
-
applyMigrationV23MilestoneQueue(db);
|
|
272
|
-
recordSchemaVersion(db, 23);
|
|
273
|
-
}
|
|
274
|
-
if (currentVersion < 24) {
|
|
275
|
-
// v24: auto-mode coordination tables. See createCoordinationTablesV24
|
|
276
|
-
// for full schema + invariants. No-op for fresh installs (the same
|
|
277
|
-
// helper runs in the fresh-install path); for upgraded DBs this is
|
|
278
|
-
// the only place these tables get created.
|
|
279
|
-
createCoordinationTablesV24(db);
|
|
280
|
-
recordSchemaVersion(db, 24);
|
|
281
|
-
}
|
|
282
|
-
if (currentVersion < 25) {
|
|
283
|
-
// v25: runtime_kv non-correctness-critical key-value storage. See
|
|
284
|
-
// createRuntimeKvTableV25 for the full schema + invariants.
|
|
285
|
-
createRuntimeKvTableV25(db);
|
|
286
|
-
recordSchemaVersion(db, 25);
|
|
287
|
-
}
|
|
288
|
-
if (currentVersion < 26) {
|
|
289
|
-
applyMigrationV26MilestoneCommitAttributions(db);
|
|
290
|
-
recordSchemaVersion(db, 26);
|
|
291
|
-
}
|
|
292
|
-
if (currentVersion < 27) {
|
|
293
|
-
applyMigrationV27ArtifactHash(db);
|
|
294
|
-
recordSchemaVersion(db, 27);
|
|
295
|
-
}
|
|
296
|
-
if (currentVersion < 28) {
|
|
297
|
-
applyMigrationV28MemoryLastHitAt(db);
|
|
298
|
-
recordSchemaVersion(db, 28);
|
|
299
|
-
}
|
|
300
|
-
if (currentVersion < 29) {
|
|
301
|
-
applyMigrationV29RepositoryTargets(db);
|
|
302
|
-
recordSchemaVersion(db, 29);
|
|
303
|
-
}
|
|
304
|
-
db.exec("COMMIT");
|
|
305
|
-
}
|
|
306
|
-
catch (err) {
|
|
307
|
-
db.exec("ROLLBACK");
|
|
308
|
-
throw err;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
let currentDb = null;
|
|
312
|
-
let currentPath = null;
|
|
313
|
-
let currentPid = 0;
|
|
314
|
-
let _exitHandlerRegistered = false;
|
|
315
|
-
const _dbOpenState = createDbOpenState();
|
|
316
|
-
/**
|
|
317
|
-
* Identity key of the workspace whose connection is currently active
|
|
318
|
-
* (currentDb). Set by openDatabaseByWorkspace(); null when the active
|
|
319
|
-
* connection was opened via the legacy openDatabase(path) path.
|
|
320
|
-
*/
|
|
321
|
-
let _currentIdentityKey = null;
|
|
322
|
-
/**
|
|
323
|
-
* Workspace-scoped connection cache.
|
|
324
|
-
* Key: GsdWorkspace.identityKey (realpath-normalized project root).
|
|
325
|
-
* Value: the DB path and open adapter for that workspace.
|
|
326
|
-
*
|
|
327
|
-
* Sibling worktrees of the same project share the same identityKey (set by
|
|
328
|
-
* createWorkspace) and therefore reuse the same cached connection, preserving
|
|
329
|
-
* shared-WAL semantics. Different projects get distinct cache entries.
|
|
330
|
-
*
|
|
331
|
-
* NOTE: Only one connection is "active" at a time (currentDb/currentPath).
|
|
332
|
-
* The cache allows fast re-activation of a previously opened connection when
|
|
333
|
-
* callers switch between known workspaces via openDatabaseByWorkspace().
|
|
334
|
-
*/
|
|
335
|
-
const _dbCache = createDbConnectionCache();
|
|
336
|
-
/** Test helper: expose the internal cache for inspection. Not for production use. */
|
|
337
|
-
export function _getDbCache() {
|
|
338
|
-
return _dbCache.asReadonlyMap();
|
|
339
|
-
}
|
|
340
|
-
function closeCachedConnection(entry, source) {
|
|
341
|
-
try {
|
|
342
|
-
entry.db.exec("PRAGMA wal_checkpoint(TRUNCATE)");
|
|
343
|
-
}
|
|
344
|
-
catch (e) {
|
|
345
|
-
if (source === "workspace")
|
|
346
|
-
logWarning("db", `WAL checkpoint (byWorkspace) failed: ${e.message}`);
|
|
347
|
-
}
|
|
348
|
-
try {
|
|
349
|
-
entry.db.exec("PRAGMA incremental_vacuum(64)");
|
|
350
|
-
}
|
|
351
|
-
catch (e) {
|
|
352
|
-
if (source === "workspace")
|
|
353
|
-
logWarning("db", `incremental vacuum (byWorkspace) failed: ${e.message}`);
|
|
354
|
-
}
|
|
355
|
-
try {
|
|
356
|
-
entry.db.close();
|
|
357
|
-
}
|
|
358
|
-
catch (e) {
|
|
359
|
-
if (source === "workspace")
|
|
360
|
-
logWarning("db", `database close (byWorkspace) failed: ${e.message}`);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* Close and evict every entry in the workspace connection cache, then call
|
|
365
|
-
* closeDatabase() to close the active connection.
|
|
366
|
-
*
|
|
367
|
-
* Use this for test teardown or process-shutdown paths where every open
|
|
368
|
-
* connection must be flushed. Normal callers should use closeDatabase() or
|
|
369
|
-
* closeDatabaseByWorkspace() instead.
|
|
370
|
-
*/
|
|
371
|
-
export function closeAllDatabases() {
|
|
372
|
-
// Close all non-active cached connections first.
|
|
373
|
-
_dbCache.closeNonActive(currentDb, (entry) => closeCachedConnection(entry, "all"));
|
|
374
|
-
closeDatabase();
|
|
375
|
-
}
|
|
376
|
-
/**
|
|
377
|
-
* Open (or reuse) the database connection scoped to the given workspace.
|
|
378
|
-
*
|
|
379
|
-
* Uses workspace.identityKey as the cache key, so sibling worktrees of the
|
|
380
|
-
* same project resolve to the same connection. On a cache hit the existing
|
|
381
|
-
* adapter is reactivated as the current connection without re-opening the
|
|
382
|
-
* file. On a cache miss, delegates to openDatabase() for the full
|
|
383
|
-
* open + schema-init + migration flow, then caches the result.
|
|
384
|
-
*
|
|
385
|
-
* When switching to a different workspace, the previously active connection
|
|
386
|
-
* is preserved in the cache (not closed), so callers can switch back to it
|
|
387
|
-
* cheaply via a subsequent openDatabaseByWorkspace() call.
|
|
388
|
-
*
|
|
389
|
-
* @param workspace A GsdWorkspace created by createWorkspace().
|
|
390
|
-
* @returns true if the connection is open and ready, false otherwise.
|
|
391
|
-
*/
|
|
392
|
-
export function openDatabaseByWorkspace(workspace) {
|
|
393
|
-
const key = workspace.identityKey;
|
|
394
|
-
const dbPath = workspace.contract.projectDb;
|
|
395
|
-
const cached = _dbCache.get(key);
|
|
396
|
-
if (cached) {
|
|
397
|
-
// Reactivate the cached connection as the current singleton.
|
|
398
|
-
currentDb = cached.db;
|
|
399
|
-
currentPath = cached.dbPath;
|
|
400
|
-
currentPid = process.pid;
|
|
401
|
-
_dbOpenState.markAttempted();
|
|
402
|
-
_currentIdentityKey = key;
|
|
403
|
-
return true;
|
|
404
|
-
}
|
|
405
|
-
// Cache miss — need to open a new connection.
|
|
406
|
-
//
|
|
407
|
-
// If there is a currently active workspace connection, stash it in the
|
|
408
|
-
// cache under its identity key before calling openDatabase(), because
|
|
409
|
-
// openDatabase() will call closeDatabase() when the path changes (which
|
|
410
|
-
// would destroy the existing adapter). By nulling out currentDb first,
|
|
411
|
-
// we prevent openDatabase() from closing the live adapter.
|
|
412
|
-
let oldDb = null;
|
|
413
|
-
let oldPath = null;
|
|
414
|
-
let oldPid = 0;
|
|
415
|
-
let oldKey = null;
|
|
416
|
-
if (currentDb !== null && _currentIdentityKey !== null) {
|
|
417
|
-
// Snapshot the old globals so we can restore them on failure.
|
|
418
|
-
oldDb = currentDb;
|
|
419
|
-
oldPath = currentPath;
|
|
420
|
-
oldPid = currentPid;
|
|
421
|
-
oldKey = _currentIdentityKey;
|
|
422
|
-
// Save the current connection so it stays alive in the cache.
|
|
423
|
-
_dbCache.set(_currentIdentityKey, {
|
|
424
|
-
dbPath: currentPath,
|
|
425
|
-
db: currentDb,
|
|
426
|
-
});
|
|
427
|
-
// Detach from globals so openDatabase() opens fresh without closing it.
|
|
428
|
-
currentDb = null;
|
|
429
|
-
currentPath = null;
|
|
430
|
-
currentPid = 0;
|
|
431
|
-
_currentIdentityKey = null;
|
|
432
|
-
}
|
|
433
|
-
// Run the full open/schema/migration flow for the new workspace.
|
|
434
|
-
// openDatabase() can throw on corrupt DB or permission error — catch so we
|
|
435
|
-
// can restore the previous connection rather than leaving globals null.
|
|
436
|
-
let opened;
|
|
437
|
-
try {
|
|
438
|
-
opened = openDatabase(dbPath);
|
|
439
|
-
}
|
|
440
|
-
catch (err) {
|
|
441
|
-
// Failed to open the new DB. Restore the previous workspace connection so
|
|
442
|
-
// the caller's workspace remains active (it is still safe in _dbCache).
|
|
443
|
-
if (oldDb !== null) {
|
|
444
|
-
currentDb = oldDb;
|
|
445
|
-
currentPath = oldPath;
|
|
446
|
-
currentPid = oldPid;
|
|
447
|
-
_currentIdentityKey = oldKey;
|
|
448
|
-
}
|
|
449
|
-
throw err;
|
|
450
|
-
}
|
|
451
|
-
if (opened && currentDb) {
|
|
452
|
-
_dbCache.set(key, { dbPath, db: currentDb });
|
|
453
|
-
_currentIdentityKey = key;
|
|
454
|
-
}
|
|
455
|
-
else if (!opened && oldDb !== null) {
|
|
456
|
-
// Restore the previous connection so the caller's workspace remains active.
|
|
457
|
-
// The failed attempt left no live adapter, so the globals stayed null.
|
|
458
|
-
currentDb = oldDb;
|
|
459
|
-
currentPath = oldPath;
|
|
460
|
-
currentPid = oldPid;
|
|
461
|
-
_currentIdentityKey = oldKey;
|
|
462
|
-
}
|
|
463
|
-
return opened;
|
|
464
|
-
}
|
|
465
|
-
/**
|
|
466
|
-
* Open (or reuse) the database connection scoped to the workspace in a
|
|
467
|
-
* MilestoneScope. Thin delegation to openDatabaseByWorkspace().
|
|
468
|
-
*/
|
|
469
|
-
export function openDatabaseByScope(scope) {
|
|
470
|
-
return openDatabaseByWorkspace(scope.workspace);
|
|
471
|
-
}
|
|
472
|
-
/**
|
|
473
|
-
* Close the database connection for the given workspace and remove it from
|
|
474
|
-
* the cache. If the workspace's connection is currently active (currentDb),
|
|
475
|
-
* performs a full closeDatabase() including WAL checkpoint. Otherwise only
|
|
476
|
-
* removes the cache entry (the adapter was already replaced by a later open).
|
|
477
|
-
*/
|
|
478
|
-
export function closeDatabaseByWorkspace(workspace) {
|
|
479
|
-
const key = workspace.identityKey;
|
|
480
|
-
const cached = _dbCache.get(key);
|
|
481
|
-
if (!cached)
|
|
482
|
-
return;
|
|
483
|
-
_dbCache.delete(key);
|
|
484
|
-
if (currentDb === cached.db) {
|
|
485
|
-
// This workspace's connection is the active one — full close.
|
|
486
|
-
closeDatabase();
|
|
487
|
-
}
|
|
488
|
-
else {
|
|
489
|
-
// Connection was displaced by a later open; close the adapter directly.
|
|
490
|
-
closeCachedConnection(cached, "workspace");
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
export function getDbProvider() {
|
|
494
|
-
providerLoader.load();
|
|
495
|
-
return providerLoader.getProviderName();
|
|
496
|
-
}
|
|
497
|
-
export function isDbAvailable() {
|
|
498
|
-
return currentDb !== null;
|
|
499
|
-
}
|
|
500
|
-
/**
|
|
501
|
-
* Returns true if openDatabase() has been called at least once this session.
|
|
502
|
-
* Used to distinguish "DB not yet initialized" from "DB genuinely unavailable"
|
|
503
|
-
* so that early callers (e.g. before_agent_start context injection) don't
|
|
504
|
-
* trigger a false degraded-mode warning.
|
|
505
|
-
*/
|
|
506
|
-
export function wasDbOpenAttempted() {
|
|
507
|
-
return _dbOpenState.snapshot().attempted;
|
|
508
|
-
}
|
|
509
|
-
export function getDbStatus() {
|
|
510
|
-
providerLoader.load();
|
|
511
|
-
const openState = _dbOpenState.snapshot();
|
|
512
|
-
return {
|
|
513
|
-
available: currentDb !== null,
|
|
514
|
-
provider: providerLoader.getProviderName(),
|
|
515
|
-
attempted: openState.attempted,
|
|
516
|
-
lastError: openState.lastError,
|
|
517
|
-
lastPhase: openState.lastPhase,
|
|
518
|
-
};
|
|
519
|
-
}
|
|
520
|
-
export function openDatabase(path) {
|
|
521
|
-
_dbOpenState.markAttempted();
|
|
522
|
-
if (currentDb && currentPath !== path)
|
|
523
|
-
closeDatabase();
|
|
524
|
-
if (currentDb && currentPath === path)
|
|
525
|
-
return true;
|
|
526
|
-
// Reset error state only when a new open attempt is actually going to run.
|
|
527
|
-
_dbOpenState.clearError();
|
|
528
|
-
let rawDb;
|
|
529
|
-
let fallbackOpen = null;
|
|
530
|
-
try {
|
|
531
|
-
rawDb = providerLoader.openRaw(path);
|
|
532
|
-
}
|
|
533
|
-
catch (primaryErr) {
|
|
534
|
-
_dbOpenState.recordError("open", primaryErr);
|
|
535
|
-
// node:sqlite loaded but failed to open this file — try better-sqlite3 as fallback.
|
|
536
|
-
fallbackOpen = providerLoader.tryOpenBetterSqliteFallback(path);
|
|
537
|
-
if (fallbackOpen) {
|
|
538
|
-
rawDb = fallbackOpen.rawDb;
|
|
539
|
-
_dbOpenState.clearError();
|
|
540
|
-
}
|
|
541
|
-
if (!rawDb)
|
|
542
|
-
throw primaryErr;
|
|
543
|
-
}
|
|
544
|
-
if (!rawDb)
|
|
545
|
-
return false;
|
|
546
|
-
const adapter = createDbAdapter(rawDb);
|
|
547
|
-
const fileBacked = path !== ":memory:";
|
|
548
|
-
try {
|
|
549
|
-
initSchema(adapter, fileBacked, path);
|
|
550
|
-
}
|
|
551
|
-
catch (err) {
|
|
552
|
-
// Corrupt freelist: DDL fails with "malformed" but VACUUM can rebuild.
|
|
553
|
-
// Attempt VACUUM recovery before giving up (see #2519).
|
|
554
|
-
if (fileBacked && err instanceof Error && err.message?.includes("malformed")) {
|
|
555
|
-
try {
|
|
556
|
-
adapter.exec("VACUUM");
|
|
557
|
-
initSchema(adapter, fileBacked, path);
|
|
558
|
-
process.stderr.write("gsd-db: recovered corrupt database via VACUUM\n");
|
|
559
|
-
}
|
|
560
|
-
catch (retryErr) {
|
|
561
|
-
_dbOpenState.recordError("vacuum-recovery", retryErr);
|
|
562
|
-
try {
|
|
563
|
-
adapter.close();
|
|
564
|
-
}
|
|
565
|
-
catch (e) {
|
|
566
|
-
logWarning("db", `close after VACUUM failed: ${e.message}`);
|
|
567
|
-
}
|
|
568
|
-
throw retryErr;
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
else {
|
|
572
|
-
_dbOpenState.recordError("initSchema", err);
|
|
573
|
-
try {
|
|
574
|
-
adapter.close();
|
|
575
|
-
}
|
|
576
|
-
catch (e) {
|
|
577
|
-
logWarning("db", `close after initSchema failed: ${e.message}`);
|
|
578
|
-
}
|
|
579
|
-
throw err;
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
// Commit fallback provider switch only after open + schema both succeeded.
|
|
583
|
-
if (fallbackOpen)
|
|
584
|
-
providerLoader.commitFallback(fallbackOpen);
|
|
585
|
-
currentDb = adapter;
|
|
586
|
-
currentPath = path;
|
|
587
|
-
currentPid = process.pid;
|
|
588
|
-
if (!_exitHandlerRegistered) {
|
|
589
|
-
_exitHandlerRegistered = true;
|
|
590
|
-
process.on("exit", () => { try {
|
|
591
|
-
closeDatabase();
|
|
592
|
-
}
|
|
593
|
-
catch (e) {
|
|
594
|
-
logWarning("db", `exit handler close failed: ${e.message}`);
|
|
595
|
-
} });
|
|
596
|
-
}
|
|
597
|
-
return true;
|
|
598
|
-
}
|
|
599
|
-
export function closeDatabase() {
|
|
600
|
-
if (currentDb) {
|
|
601
|
-
try {
|
|
602
|
-
currentDb.exec('PRAGMA wal_checkpoint(TRUNCATE)');
|
|
603
|
-
}
|
|
604
|
-
catch (e) {
|
|
605
|
-
logWarning("db", `WAL checkpoint failed: ${e.message}`);
|
|
606
|
-
}
|
|
607
|
-
try {
|
|
608
|
-
// Incremental vacuum to reclaim space without blocking
|
|
609
|
-
currentDb.exec('PRAGMA incremental_vacuum(64)');
|
|
610
|
-
}
|
|
611
|
-
catch (e) {
|
|
612
|
-
logWarning("db", `incremental vacuum failed: ${e.message}`);
|
|
613
|
-
}
|
|
614
|
-
try {
|
|
615
|
-
currentDb.close();
|
|
616
|
-
}
|
|
617
|
-
catch (e) {
|
|
618
|
-
logWarning("db", `database close failed: ${e.message}`);
|
|
619
|
-
}
|
|
620
|
-
// If this connection was workspace-tracked, evict it from the cache so
|
|
621
|
-
// subsequent openDatabaseByWorkspace() calls re-open rather than reactivate
|
|
622
|
-
// a closed adapter.
|
|
623
|
-
if (_currentIdentityKey !== null) {
|
|
624
|
-
_dbCache.delete(_currentIdentityKey);
|
|
625
|
-
_currentIdentityKey = null;
|
|
626
|
-
}
|
|
627
|
-
currentDb = null;
|
|
628
|
-
currentPath = null;
|
|
629
|
-
currentPid = 0;
|
|
630
|
-
}
|
|
631
|
-
// Reset session-scoped state unconditionally so stale error info from a
|
|
632
|
-
// failed open doesn't persist into the next open attempt or status check.
|
|
633
|
-
_dbOpenState.reset();
|
|
634
|
-
}
|
|
635
|
-
/**
|
|
636
|
-
* Re-open the active database connection from disk.
|
|
637
|
-
*
|
|
638
|
-
* Auto-mode can observe artifacts written by a workflow server running in a
|
|
639
|
-
* different process before its long-lived singleton has re-synchronized. The
|
|
640
|
-
* recovery path uses this to force the next state derivation to read from the
|
|
641
|
-
* current on-disk database instead of continuing with a possibly stale handle.
|
|
642
|
-
*/
|
|
643
|
-
export function refreshOpenDatabaseFromDisk() {
|
|
644
|
-
if (!currentDb || !currentPath)
|
|
645
|
-
return false;
|
|
646
|
-
if (currentPath === ":memory:")
|
|
647
|
-
return false;
|
|
648
|
-
const dbPath = currentPath;
|
|
649
|
-
const identityKey = _currentIdentityKey;
|
|
650
|
-
try {
|
|
651
|
-
closeDatabase();
|
|
652
|
-
const opened = openDatabase(dbPath);
|
|
653
|
-
if (opened && identityKey && currentDb) {
|
|
654
|
-
_dbCache.set(identityKey, { dbPath, db: currentDb });
|
|
655
|
-
_currentIdentityKey = identityKey;
|
|
656
|
-
}
|
|
657
|
-
return opened;
|
|
658
|
-
}
|
|
659
|
-
catch (e) {
|
|
660
|
-
logWarning("db", `database refresh failed: ${e.message}`);
|
|
661
|
-
return false;
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
/** Run a full VACUUM — call sparingly (e.g. after milestone completion). */
|
|
665
|
-
export function vacuumDatabase() {
|
|
666
|
-
if (!currentDb)
|
|
667
|
-
return;
|
|
668
|
-
try {
|
|
669
|
-
currentDb.exec('VACUUM');
|
|
670
|
-
}
|
|
671
|
-
catch (e) {
|
|
672
|
-
logWarning("db", `VACUUM failed: ${e.message}`);
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
/** Flush WAL into gsd.db so `git add .gsd/gsd.db` stages current state — safe while DB is open. */
|
|
676
|
-
export function checkpointDatabase() {
|
|
677
|
-
if (!currentDb)
|
|
678
|
-
return;
|
|
679
|
-
try {
|
|
680
|
-
currentDb.exec('PRAGMA wal_checkpoint(TRUNCATE)');
|
|
681
|
-
}
|
|
682
|
-
catch (e) {
|
|
683
|
-
logWarning("db", `WAL checkpoint failed: ${e.message}`);
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
/**
|
|
687
|
-
* Copy the live database file to `.gsd/backups/<label>-<timestamp>.db` so a
|
|
688
|
-
* destructive operation (e.g. recover, which clears the hierarchy tables) is
|
|
689
|
-
* reversible. Checkpoints the WAL first so the snapshot is complete. Returns
|
|
690
|
-
* the backup path, or null if no DB is open or the copy failed.
|
|
691
|
-
*/
|
|
692
|
-
export function backupDatabaseSnapshot(label) {
|
|
693
|
-
if (!currentPath)
|
|
694
|
-
return null;
|
|
695
|
-
try {
|
|
696
|
-
checkpointDatabase();
|
|
697
|
-
const backupsDir = join(dirname(currentPath), "backups");
|
|
698
|
-
mkdirSync(backupsDir, { recursive: true });
|
|
699
|
-
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
700
|
-
const dest = join(backupsDir, `${label}-${stamp}.db`);
|
|
701
|
-
copyFileSync(currentPath, dest);
|
|
702
|
-
return dest;
|
|
703
|
-
}
|
|
704
|
-
catch (e) {
|
|
705
|
-
logWarning("db", `database snapshot failed: ${e.message}`);
|
|
706
|
-
return null;
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
const _transactionRunner = createDbTransactionRunner();
|
|
710
|
-
function createTransactionControls(db) {
|
|
711
|
-
return {
|
|
712
|
-
begin: () => db.exec("BEGIN"),
|
|
713
|
-
beginRead: () => db.exec("BEGIN DEFERRED"),
|
|
714
|
-
commit: () => db.exec("COMMIT"),
|
|
715
|
-
rollback: () => db.exec("ROLLBACK"),
|
|
716
|
-
};
|
|
717
|
-
}
|
|
718
|
-
/**
|
|
719
|
-
* Whether the current call is running inside an active SQLite transaction.
|
|
720
|
-
* Statement-time recovery paths (e.g. VACUUM retry on a malformed memory
|
|
721
|
-
* store) MUST gate on this — SQLite refuses VACUUM inside a transaction
|
|
722
|
-
* and would mask the original error with a secondary "cannot VACUUM" throw.
|
|
723
|
-
*/
|
|
724
|
-
export function isInTransaction() {
|
|
725
|
-
return _transactionRunner.isInTransaction();
|
|
726
|
-
}
|
|
727
|
-
export function transaction(fn) {
|
|
728
|
-
if (!currentDb)
|
|
729
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
730
|
-
return _transactionRunner.transaction(createTransactionControls(currentDb), fn);
|
|
731
|
-
}
|
|
732
|
-
/**
|
|
733
|
-
* Wrap a block of reads in a DEFERRED transaction so that all SELECTs observe
|
|
734
|
-
* a consistent snapshot of the DB even if a concurrent writer commits between
|
|
735
|
-
* them. Use this for multi-query read flows (e.g. tool executors that query
|
|
736
|
-
* milestone + slices + counts and want one snapshot). Re-entrant — if already
|
|
737
|
-
* inside a transaction, runs fn() without starting a nested one.
|
|
738
|
-
*/
|
|
739
|
-
export function readTransaction(fn) {
|
|
740
|
-
if (!currentDb)
|
|
741
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
742
|
-
return _transactionRunner.readTransaction(createTransactionControls(currentDb), fn, (rollbackErr) => {
|
|
743
|
-
// A failed ROLLBACK after a failed read is a split-brain signal —
|
|
744
|
-
// the transaction is in an indeterminate state. Surface it via the
|
|
745
|
-
// logger instead of swallowing it.
|
|
746
|
-
logError("db", "snapshotState ROLLBACK failed", {
|
|
747
|
-
error: rollbackErr.message,
|
|
748
|
-
});
|
|
749
|
-
});
|
|
750
|
-
}
|
|
21
|
+
// Connection ownership, lifecycle, schema/migrations and transaction
|
|
22
|
+
// primitives now live in the engine; re-export the full public surface so
|
|
23
|
+
// existing `from "./gsd-db.js"` imports keep working.
|
|
24
|
+
export * from "./db/engine.js";
|
|
25
|
+
import { transaction, getDbOrNull } from "./db/engine.js";
|
|
26
|
+
// ─── Single Writer Layer re-exports ──────────────────────────────────────
|
|
27
|
+
// Write subsystems live in db/writers/*; re-exported here so callers keep
|
|
28
|
+
// importing from "./gsd-db.js".
|
|
29
|
+
export * from "./db/writers/memory.js";
|
|
30
|
+
export * from "./db/writers/reconcile.js";
|
|
31
|
+
export * from "./db/writers/import-restore.js";
|
|
32
|
+
// Query Module (read-only seam) — extracted from the single-writer file.
|
|
33
|
+
export * from "./db/queries.js";
|
|
34
|
+
// Domain Write Operations (Hierarchy Status Cascades).
|
|
35
|
+
export * from "./db/writers/cascades.js";
|
|
36
|
+
import { TERMINAL_STATUS_SQL } from "./db/sql-constants.js";
|
|
37
|
+
import { applyStatusTransition } from "./db/writers/status.js";
|
|
751
38
|
export function insertDecision(d) {
|
|
752
|
-
if (!
|
|
39
|
+
if (!getDbOrNull())
|
|
753
40
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
754
|
-
|
|
41
|
+
getDbOrNull().prepare(`INSERT INTO decisions (id, when_context, scope, decision, choice, rationale, revisable, made_by, source, superseded_by)
|
|
755
42
|
VALUES (:id, :when_context, :scope, :decision, :choice, :rationale, :revisable, :made_by, :source, :superseded_by)`).run({
|
|
756
43
|
":id": d.id,
|
|
757
44
|
":when_context": d.when_context,
|
|
@@ -765,24 +52,10 @@ export function insertDecision(d) {
|
|
|
765
52
|
":superseded_by": d.superseded_by,
|
|
766
53
|
});
|
|
767
54
|
}
|
|
768
|
-
export function getDecisionById(id) {
|
|
769
|
-
if (!currentDb)
|
|
770
|
-
return null;
|
|
771
|
-
const row = currentDb.prepare("SELECT * FROM decisions WHERE id = ?").get(id);
|
|
772
|
-
if (!row)
|
|
773
|
-
return null;
|
|
774
|
-
return rowToDecision(row);
|
|
775
|
-
}
|
|
776
|
-
export function getActiveDecisions() {
|
|
777
|
-
if (!currentDb)
|
|
778
|
-
return [];
|
|
779
|
-
const rows = currentDb.prepare("SELECT * FROM active_decisions").all();
|
|
780
|
-
return rows.map(rowToActiveDecision);
|
|
781
|
-
}
|
|
782
55
|
export function insertRequirement(r) {
|
|
783
|
-
if (!
|
|
56
|
+
if (!getDbOrNull())
|
|
784
57
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
785
|
-
|
|
58
|
+
getDbOrNull().prepare(`INSERT INTO requirements (id, class, status, description, why, source, primary_owner, supporting_slices, validation, notes, full_content, superseded_by)
|
|
786
59
|
VALUES (:id, :class, :status, :description, :why, :source, :primary_owner, :supporting_slices, :validation, :notes, :full_content, :superseded_by)`).run({
|
|
787
60
|
":id": r.id,
|
|
788
61
|
":class": r.class,
|
|
@@ -798,48 +71,13 @@ export function insertRequirement(r) {
|
|
|
798
71
|
":superseded_by": r.superseded_by,
|
|
799
72
|
});
|
|
800
73
|
}
|
|
801
|
-
export function getRequirementById(id) {
|
|
802
|
-
if (!currentDb)
|
|
803
|
-
return null;
|
|
804
|
-
const row = currentDb.prepare("SELECT * FROM requirements WHERE id = ?").get(id);
|
|
805
|
-
if (!row)
|
|
806
|
-
return null;
|
|
807
|
-
return rowToRequirement(row);
|
|
808
|
-
}
|
|
809
|
-
export function getActiveRequirements() {
|
|
810
|
-
if (!currentDb)
|
|
811
|
-
return [];
|
|
812
|
-
const rows = currentDb.prepare("SELECT * FROM active_requirements").all();
|
|
813
|
-
return rows.map(rowToActiveRequirement);
|
|
814
|
-
}
|
|
815
|
-
export function getRequirementCounts() {
|
|
816
|
-
if (!currentDb) {
|
|
817
|
-
return { active: 0, validated: 0, deferred: 0, outOfScope: 0, blocked: 0, total: 0 };
|
|
818
|
-
}
|
|
819
|
-
const rows = currentDb
|
|
820
|
-
.prepare("SELECT lower(status) as status, COUNT(*) as count FROM requirements GROUP BY lower(status)")
|
|
821
|
-
.all();
|
|
822
|
-
return rowsToRequirementCounts(rows);
|
|
823
|
-
}
|
|
824
|
-
export function getDbOwnerPid() {
|
|
825
|
-
return currentPid;
|
|
826
|
-
}
|
|
827
|
-
export function getDbPath() {
|
|
828
|
-
return currentPath;
|
|
829
|
-
}
|
|
830
|
-
export function _getAdapter() {
|
|
831
|
-
return currentDb;
|
|
832
|
-
}
|
|
833
|
-
export function _resetProvider() {
|
|
834
|
-
providerLoader.reset();
|
|
835
|
-
}
|
|
836
74
|
export function upsertDecision(d) {
|
|
837
|
-
if (!
|
|
75
|
+
if (!getDbOrNull())
|
|
838
76
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
839
77
|
// Use ON CONFLICT DO UPDATE instead of INSERT OR REPLACE to preserve the
|
|
840
78
|
// seq column. INSERT OR REPLACE deletes then reinserts, resetting seq and
|
|
841
79
|
// corrupting decision ordering in DECISIONS.md after reconcile replay.
|
|
842
|
-
|
|
80
|
+
getDbOrNull().prepare(`INSERT INTO decisions (id, when_context, scope, decision, choice, rationale, revisable, made_by, source, superseded_by)
|
|
843
81
|
VALUES (:id, :when_context, :scope, :decision, :choice, :rationale, :revisable, :made_by, :source, :superseded_by)
|
|
844
82
|
ON CONFLICT(id) DO UPDATE SET
|
|
845
83
|
when_context = excluded.when_context,
|
|
@@ -864,9 +102,9 @@ export function upsertDecision(d) {
|
|
|
864
102
|
});
|
|
865
103
|
}
|
|
866
104
|
export function upsertRequirement(r) {
|
|
867
|
-
if (!
|
|
105
|
+
if (!getDbOrNull())
|
|
868
106
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
869
|
-
|
|
107
|
+
getDbOrNull().prepare(`INSERT OR REPLACE INTO requirements (id, class, status, description, why, source, primary_owner, supporting_slices, validation, notes, full_content, superseded_by)
|
|
870
108
|
VALUES (:id, :class, :status, :description, :why, :source, :primary_owner, :supporting_slices, :validation, :notes, :full_content, :superseded_by)`).run({
|
|
871
109
|
":id": r.id,
|
|
872
110
|
":class": r.class,
|
|
@@ -883,40 +121,40 @@ export function upsertRequirement(r) {
|
|
|
883
121
|
});
|
|
884
122
|
}
|
|
885
123
|
export function clearArtifacts() {
|
|
886
|
-
if (!
|
|
124
|
+
if (!getDbOrNull())
|
|
887
125
|
return;
|
|
888
126
|
try {
|
|
889
|
-
|
|
127
|
+
getDbOrNull().exec("DELETE FROM artifacts");
|
|
890
128
|
}
|
|
891
129
|
catch (e) {
|
|
892
130
|
logWarning("db", `clearArtifacts failed: ${e.message}`);
|
|
893
131
|
}
|
|
894
132
|
}
|
|
895
133
|
export function clearDecisions() {
|
|
896
|
-
if (!
|
|
134
|
+
if (!getDbOrNull())
|
|
897
135
|
return;
|
|
898
136
|
try {
|
|
899
|
-
|
|
137
|
+
getDbOrNull().exec("DELETE FROM decisions");
|
|
900
138
|
}
|
|
901
139
|
catch (e) {
|
|
902
140
|
logWarning("db", `clearDecisions failed: ${e.message}`);
|
|
903
141
|
}
|
|
904
142
|
}
|
|
905
143
|
export function clearRequirements() {
|
|
906
|
-
if (!
|
|
144
|
+
if (!getDbOrNull())
|
|
907
145
|
return;
|
|
908
146
|
try {
|
|
909
|
-
|
|
147
|
+
getDbOrNull().exec("DELETE FROM requirements");
|
|
910
148
|
}
|
|
911
149
|
catch (e) {
|
|
912
150
|
logWarning("db", `clearRequirements failed: ${e.message}`);
|
|
913
151
|
}
|
|
914
152
|
}
|
|
915
153
|
export function insertArtifact(a) {
|
|
916
|
-
if (!
|
|
154
|
+
if (!getDbOrNull())
|
|
917
155
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
918
156
|
const contentHash = createHash("sha256").update(a.full_content).digest("hex");
|
|
919
|
-
|
|
157
|
+
getDbOrNull().prepare(`INSERT OR REPLACE INTO artifacts (path, artifact_type, milestone_id, slice_id, task_id, full_content, imported_at, content_hash)
|
|
920
158
|
VALUES (:path, :artifact_type, :milestone_id, :slice_id, :task_id, :full_content, :imported_at, :content_hash)`).run({
|
|
921
159
|
":path": a.path,
|
|
922
160
|
":artifact_type": a.artifact_type,
|
|
@@ -929,9 +167,9 @@ export function insertArtifact(a) {
|
|
|
929
167
|
});
|
|
930
168
|
}
|
|
931
169
|
export function insertMilestone(m) {
|
|
932
|
-
if (!
|
|
170
|
+
if (!getDbOrNull())
|
|
933
171
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
934
|
-
|
|
172
|
+
getDbOrNull().prepare(`INSERT OR IGNORE INTO milestones (
|
|
935
173
|
id, title, status, depends_on, created_at,
|
|
936
174
|
vision, success_criteria, key_risks, proof_strategy,
|
|
937
175
|
verification_contract, verification_integration, verification_operational, verification_uat,
|
|
@@ -963,9 +201,9 @@ export function insertMilestone(m) {
|
|
|
963
201
|
});
|
|
964
202
|
}
|
|
965
203
|
export function upsertMilestonePlanning(milestoneId, planning) {
|
|
966
|
-
if (!
|
|
204
|
+
if (!getDbOrNull())
|
|
967
205
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
968
|
-
|
|
206
|
+
getDbOrNull().prepare(`UPDATE milestones SET
|
|
969
207
|
title = COALESCE(NULLIF(:title, ''), title),
|
|
970
208
|
status = COALESCE(NULLIF(:status, ''), status),
|
|
971
209
|
depends_on = COALESCE(:depends_on, depends_on),
|
|
@@ -999,9 +237,14 @@ export function upsertMilestonePlanning(milestoneId, planning) {
|
|
|
999
237
|
});
|
|
1000
238
|
}
|
|
1001
239
|
export function insertSlice(s) {
|
|
1002
|
-
if (!
|
|
240
|
+
if (!getDbOrNull())
|
|
1003
241
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1004
|
-
|
|
242
|
+
const SLICE_ID_RE = /^[A-Za-z0-9][A-Za-z0-9-]*$/;
|
|
243
|
+
const invalidDep = (s.depends ?? []).find(d => !SLICE_ID_RE.test(d));
|
|
244
|
+
if (invalidDep !== undefined) {
|
|
245
|
+
throw new GSDError(GSD_STALE_STATE, `insertSlice: depends element "${invalidDep}" is not a valid slice ID`);
|
|
246
|
+
}
|
|
247
|
+
getDbOrNull().prepare(`INSERT INTO slices (
|
|
1005
248
|
milestone_id, id, title, status, risk, depends, demo, created_at,
|
|
1006
249
|
goal, success_criteria, proof_level, integration_closure, observability_impact, target_repositories, sequence,
|
|
1007
250
|
is_sketch, sketch_scope
|
|
@@ -1062,26 +305,14 @@ export function insertSlice(s) {
|
|
|
1062
305
|
}
|
|
1063
306
|
// ADR-011: sketch-then-refine helpers
|
|
1064
307
|
export function setSliceSketchFlag(milestoneId, sliceId, isSketch) {
|
|
1065
|
-
if (!
|
|
308
|
+
if (!getDbOrNull())
|
|
1066
309
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1067
|
-
|
|
1068
|
-
}
|
|
1069
|
-
/**
|
|
1070
|
-
* ADR-017 raw primitive: returns slice IDs in a milestone whose is_sketch flag
|
|
1071
|
-
* is still 1. The stale-sketch-flag drift handler at
|
|
1072
|
-
* `state-reconciliation/drift/sketch-flag.ts` composes this with PLAN.md
|
|
1073
|
-
* existence checks to detect drift, then writes via `setSliceSketchFlag`.
|
|
1074
|
-
*/
|
|
1075
|
-
export function getSketchedSliceIds(milestoneId) {
|
|
1076
|
-
if (!currentDb)
|
|
1077
|
-
return [];
|
|
1078
|
-
const rows = currentDb.prepare(`SELECT id FROM slices WHERE milestone_id = :mid AND is_sketch = 1`).all({ ":mid": milestoneId });
|
|
1079
|
-
return rows.map((r) => r.id);
|
|
310
|
+
getDbOrNull().prepare(`UPDATE slices SET is_sketch = :is_sketch WHERE milestone_id = :mid AND id = :sid`).run({ ":is_sketch": isSketch ? 1 : 0, ":mid": milestoneId, ":sid": sliceId });
|
|
1080
311
|
}
|
|
1081
312
|
export function upsertSlicePlanning(milestoneId, sliceId, planning) {
|
|
1082
|
-
if (!
|
|
313
|
+
if (!getDbOrNull())
|
|
1083
314
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1084
|
-
|
|
315
|
+
getDbOrNull().prepare(`UPDATE slices SET
|
|
1085
316
|
goal = COALESCE(:goal, goal),
|
|
1086
317
|
success_criteria = COALESCE(:success_criteria, success_criteria),
|
|
1087
318
|
proof_level = COALESCE(:proof_level, proof_level),
|
|
@@ -1100,9 +331,9 @@ export function upsertSlicePlanning(milestoneId, sliceId, planning) {
|
|
|
1100
331
|
});
|
|
1101
332
|
}
|
|
1102
333
|
export function insertTask(t) {
|
|
1103
|
-
if (!
|
|
334
|
+
if (!getDbOrNull())
|
|
1104
335
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1105
|
-
|
|
336
|
+
getDbOrNull().prepare(`INSERT INTO tasks (
|
|
1106
337
|
milestone_id, slice_id, id, title, status, one_liner, narrative,
|
|
1107
338
|
verification_result, duration, completed_at, blocker_discovered,
|
|
1108
339
|
deviations, known_issues, key_files, key_decisions, full_summary_md,
|
|
@@ -1174,26 +405,17 @@ export function insertTask(t) {
|
|
|
1174
405
|
});
|
|
1175
406
|
}
|
|
1176
407
|
export function updateTaskStatus(milestoneId, sliceId, taskId, status, completedAt) {
|
|
1177
|
-
|
|
1178
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1179
|
-
currentDb.prepare(`UPDATE tasks SET status = :status, completed_at = :completed_at
|
|
1180
|
-
WHERE milestone_id = :milestone_id AND slice_id = :slice_id AND id = :id`).run({
|
|
1181
|
-
":status": status,
|
|
1182
|
-
":completed_at": completedAt ?? null,
|
|
1183
|
-
":milestone_id": milestoneId,
|
|
1184
|
-
":slice_id": sliceId,
|
|
1185
|
-
":id": taskId,
|
|
1186
|
-
});
|
|
408
|
+
applyStatusTransition({ entity: "task", milestoneId, sliceId, taskId, status, completedAt });
|
|
1187
409
|
}
|
|
1188
410
|
export function setTaskBlockerDiscovered(milestoneId, sliceId, taskId, discovered) {
|
|
1189
|
-
if (!
|
|
411
|
+
if (!getDbOrNull())
|
|
1190
412
|
return;
|
|
1191
|
-
|
|
413
|
+
getDbOrNull().prepare(`UPDATE tasks SET blocker_discovered = :discovered WHERE milestone_id = :mid AND slice_id = :sid AND id = :tid`).run({ ":discovered": discovered ? 1 : 0, ":mid": milestoneId, ":sid": sliceId, ":tid": taskId });
|
|
1192
414
|
}
|
|
1193
415
|
export function upsertTaskPlanning(milestoneId, sliceId, taskId, planning) {
|
|
1194
|
-
if (!
|
|
416
|
+
if (!getDbOrNull())
|
|
1195
417
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1196
|
-
|
|
418
|
+
getDbOrNull().prepare(`UPDATE tasks SET
|
|
1197
419
|
title = COALESCE(:title, title),
|
|
1198
420
|
description = COALESCE(:description, description),
|
|
1199
421
|
estimate = COALESCE(:estimate, estimate),
|
|
@@ -1220,96 +442,25 @@ export function upsertTaskPlanning(milestoneId, sliceId, taskId, planning) {
|
|
|
1220
442
|
":target_repositories": planning.targetRepositories ? JSON.stringify(planning.targetRepositories) : null,
|
|
1221
443
|
});
|
|
1222
444
|
}
|
|
1223
|
-
export function getSlice(milestoneId, sliceId) {
|
|
1224
|
-
if (!currentDb)
|
|
1225
|
-
return null;
|
|
1226
|
-
const row = currentDb.prepare("SELECT * FROM slices WHERE milestone_id = :mid AND id = :sid").get({ ":mid": milestoneId, ":sid": sliceId });
|
|
1227
|
-
if (!row)
|
|
1228
|
-
return null;
|
|
1229
|
-
return rowToSlice(row);
|
|
1230
|
-
}
|
|
1231
445
|
export function updateSliceStatus(milestoneId, sliceId, status, completedAt) {
|
|
1232
|
-
|
|
1233
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1234
|
-
currentDb.prepare(`UPDATE slices SET status = :status, completed_at = :completed_at
|
|
1235
|
-
WHERE milestone_id = :milestone_id AND id = :id`).run({
|
|
1236
|
-
":status": status,
|
|
1237
|
-
":completed_at": completedAt ?? null,
|
|
1238
|
-
":milestone_id": milestoneId,
|
|
1239
|
-
":id": sliceId,
|
|
1240
|
-
});
|
|
446
|
+
applyStatusTransition({ entity: "slice", milestoneId, sliceId, status, completedAt });
|
|
1241
447
|
}
|
|
1242
448
|
export function setTaskSummaryMd(milestoneId, sliceId, taskId, md) {
|
|
1243
|
-
if (!
|
|
449
|
+
if (!getDbOrNull())
|
|
1244
450
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1245
|
-
|
|
451
|
+
getDbOrNull().prepare(`UPDATE tasks SET full_summary_md = :md WHERE milestone_id = :mid AND slice_id = :sid AND id = :tid`).run({ ":mid": milestoneId, ":sid": sliceId, ":tid": taskId, ":md": md });
|
|
1246
452
|
}
|
|
1247
453
|
export function setSliceSummaryMd(milestoneId, sliceId, summaryMd, uatMd) {
|
|
1248
|
-
if (!
|
|
454
|
+
if (!getDbOrNull())
|
|
1249
455
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1250
|
-
|
|
1251
|
-
}
|
|
1252
|
-
export function getTask(milestoneId, sliceId, taskId) {
|
|
1253
|
-
if (!currentDb)
|
|
1254
|
-
return null;
|
|
1255
|
-
const row = currentDb.prepare("SELECT * FROM tasks WHERE milestone_id = :mid AND slice_id = :sid AND id = :tid").get({ ":mid": milestoneId, ":sid": sliceId, ":tid": taskId });
|
|
1256
|
-
if (!row)
|
|
1257
|
-
return null;
|
|
1258
|
-
return rowToTask(row);
|
|
1259
|
-
}
|
|
1260
|
-
export function getSliceTasks(milestoneId, sliceId) {
|
|
1261
|
-
if (!currentDb)
|
|
1262
|
-
return [];
|
|
1263
|
-
const rows = currentDb.prepare("SELECT * FROM tasks WHERE milestone_id = :mid AND slice_id = :sid ORDER BY sequence, id").all({ ":mid": milestoneId, ":sid": sliceId });
|
|
1264
|
-
return rows.map(rowToTask);
|
|
1265
|
-
}
|
|
1266
|
-
export function getCompletedMilestoneTaskFileHints(milestoneId) {
|
|
1267
|
-
if (!currentDb)
|
|
1268
|
-
return [];
|
|
1269
|
-
const rows = currentDb.prepare(`SELECT files, key_files
|
|
1270
|
-
FROM tasks
|
|
1271
|
-
WHERE milestone_id = :mid AND status IN ('complete', 'done')`).all({ ":mid": milestoneId });
|
|
1272
|
-
const hints = new Set();
|
|
1273
|
-
for (const row of rows) {
|
|
1274
|
-
for (const raw of [row["files"], row["key_files"]]) {
|
|
1275
|
-
for (const file of parseStringArrayColumn(raw)) {
|
|
1276
|
-
const normalized = normalizeRepoPath(file);
|
|
1277
|
-
if (normalized)
|
|
1278
|
-
hints.add(normalized);
|
|
1279
|
-
}
|
|
1280
|
-
}
|
|
1281
|
-
}
|
|
1282
|
-
return [...hints];
|
|
1283
|
-
}
|
|
1284
|
-
function parseStringArrayColumn(raw) {
|
|
1285
|
-
if (Array.isArray(raw))
|
|
1286
|
-
return raw.filter((entry) => typeof entry === "string");
|
|
1287
|
-
if (typeof raw !== "string")
|
|
1288
|
-
return [];
|
|
1289
|
-
const trimmed = raw.trim();
|
|
1290
|
-
if (!trimmed)
|
|
1291
|
-
return [];
|
|
1292
|
-
try {
|
|
1293
|
-
const parsed = JSON.parse(trimmed);
|
|
1294
|
-
if (Array.isArray(parsed))
|
|
1295
|
-
return parsed.filter((entry) => typeof entry === "string");
|
|
1296
|
-
if (typeof parsed === "string")
|
|
1297
|
-
return [parsed];
|
|
1298
|
-
}
|
|
1299
|
-
catch {
|
|
1300
|
-
return trimmed.split(",");
|
|
1301
|
-
}
|
|
1302
|
-
return [];
|
|
1303
|
-
}
|
|
1304
|
-
function normalizeRepoPath(file) {
|
|
1305
|
-
return file.trim().replace(/\\/g, "/").replace(/^\.\/+/, "");
|
|
456
|
+
getDbOrNull().prepare(`UPDATE slices SET full_summary_md = :summary_md, full_uat_md = :uat_md WHERE milestone_id = :mid AND id = :sid`).run({ ":mid": milestoneId, ":sid": sliceId, ":summary_md": summaryMd, ":uat_md": uatMd });
|
|
1306
457
|
}
|
|
1307
458
|
// ─── ADR-011 Phase 2 escalation helpers ──────────────────────────────────
|
|
1308
459
|
/** Set pause-on-escalation state on a completed task. Mutually exclusive with awaiting_review. */
|
|
1309
460
|
export function setTaskEscalationPending(milestoneId, sliceId, taskId, artifactPath) {
|
|
1310
|
-
if (!
|
|
461
|
+
if (!getDbOrNull())
|
|
1311
462
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1312
|
-
|
|
463
|
+
getDbOrNull().prepare(`UPDATE tasks
|
|
1313
464
|
SET escalation_pending = 1,
|
|
1314
465
|
escalation_awaiting_review = 0,
|
|
1315
466
|
escalation_artifact_path = :path
|
|
@@ -1317,9 +468,9 @@ export function setTaskEscalationPending(milestoneId, sliceId, taskId, artifactP
|
|
|
1317
468
|
}
|
|
1318
469
|
/** Set awaiting-review state (artifact exists and requires explicit user review). Mutually exclusive with pending. */
|
|
1319
470
|
export function setTaskEscalationAwaitingReview(milestoneId, sliceId, taskId, artifactPath) {
|
|
1320
|
-
if (!
|
|
471
|
+
if (!getDbOrNull())
|
|
1321
472
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1322
|
-
|
|
473
|
+
getDbOrNull().prepare(`UPDATE tasks
|
|
1323
474
|
SET escalation_awaiting_review = 1,
|
|
1324
475
|
escalation_pending = 0,
|
|
1325
476
|
escalation_artifact_path = :path
|
|
@@ -1327,9 +478,9 @@ export function setTaskEscalationAwaitingReview(milestoneId, sliceId, taskId, ar
|
|
|
1327
478
|
}
|
|
1328
479
|
/** Clear escalation-pending and awaiting-review flags once the user has resolved it. */
|
|
1329
480
|
export function clearTaskEscalationFlags(milestoneId, sliceId, taskId) {
|
|
1330
|
-
if (!
|
|
481
|
+
if (!getDbOrNull())
|
|
1331
482
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1332
|
-
|
|
483
|
+
getDbOrNull().prepare(`UPDATE tasks
|
|
1333
484
|
SET escalation_pending = 0,
|
|
1334
485
|
escalation_awaiting_review = 0
|
|
1335
486
|
WHERE milestone_id = :mid AND slice_id = :sid AND id = :tid`).run({ ":mid": milestoneId, ":sid": sliceId, ":tid": taskId });
|
|
@@ -1340,10 +491,10 @@ export function clearTaskEscalationFlags(milestoneId, sliceId, taskId) {
|
|
|
1340
491
|
* another caller already claimed it (must skip).
|
|
1341
492
|
*/
|
|
1342
493
|
export function claimEscalationOverride(milestoneId, sliceId, sourceTaskId) {
|
|
1343
|
-
if (!
|
|
494
|
+
if (!getDbOrNull())
|
|
1344
495
|
return false;
|
|
1345
496
|
const now = new Date().toISOString();
|
|
1346
|
-
const result =
|
|
497
|
+
const result = getDbOrNull().prepare(`UPDATE tasks
|
|
1347
498
|
SET escalation_override_applied_at = :now
|
|
1348
499
|
WHERE milestone_id = :mid AND slice_id = :sid AND id = :tid
|
|
1349
500
|
AND escalation_override_applied_at IS NULL
|
|
@@ -1352,51 +503,19 @@ export function claimEscalationOverride(milestoneId, sliceId, sourceTaskId) {
|
|
|
1352
503
|
const changes = result.changes ?? 0;
|
|
1353
504
|
return changes > 0;
|
|
1354
505
|
}
|
|
1355
|
-
/** Find the most recent resolved-but-unapplied escalation override in a slice. */
|
|
1356
|
-
export function findUnappliedEscalationOverride(milestoneId, sliceId) {
|
|
1357
|
-
if (!currentDb)
|
|
1358
|
-
return null;
|
|
1359
|
-
// Filter BOTH flags: escalation_pending=0 AND escalation_awaiting_review=0
|
|
1360
|
-
// ensures we only claim overrides the user has explicitly resolved.
|
|
1361
|
-
// Without the awaiting_review filter, continueWithDefault=true artifacts
|
|
1362
|
-
// (not yet responded to) would be prematurely claimed, causing the override
|
|
1363
|
-
// to be lost when the user later resolves (#ADR-011 Phase 2 peer-review Bug 2).
|
|
1364
|
-
const row = currentDb.prepare(`SELECT id, escalation_artifact_path AS path
|
|
1365
|
-
FROM tasks
|
|
1366
|
-
WHERE milestone_id = :mid AND slice_id = :sid
|
|
1367
|
-
AND escalation_artifact_path IS NOT NULL
|
|
1368
|
-
AND escalation_override_applied_at IS NULL
|
|
1369
|
-
AND escalation_pending = 0
|
|
1370
|
-
AND escalation_awaiting_review = 0
|
|
1371
|
-
ORDER BY sequence DESC, id DESC
|
|
1372
|
-
LIMIT 1`).get({ ":mid": milestoneId, ":sid": sliceId });
|
|
1373
|
-
if (!row || !row.path)
|
|
1374
|
-
return null;
|
|
1375
|
-
return { taskId: row.id, artifactPath: row.path };
|
|
1376
|
-
}
|
|
1377
506
|
/** Set the blocker_source provenance field (used when rejecting an escalation). */
|
|
1378
507
|
export function setTaskBlockerSource(milestoneId, sliceId, taskId, source) {
|
|
1379
|
-
if (!
|
|
508
|
+
if (!getDbOrNull())
|
|
1380
509
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1381
|
-
|
|
510
|
+
getDbOrNull().prepare(`UPDATE tasks
|
|
1382
511
|
SET blocker_discovered = 1,
|
|
1383
512
|
blocker_source = :src
|
|
1384
513
|
WHERE milestone_id = :mid AND slice_id = :sid AND id = :tid`).run({ ":src": source, ":mid": milestoneId, ":sid": sliceId, ":tid": taskId });
|
|
1385
514
|
}
|
|
1386
|
-
/** List tasks with active escalation artifacts across a milestone (for /gsd escalate list). */
|
|
1387
|
-
export function listEscalationArtifacts(milestoneId, includeResolved = false) {
|
|
1388
|
-
if (!currentDb)
|
|
1389
|
-
return [];
|
|
1390
|
-
const filter = includeResolved
|
|
1391
|
-
? "escalation_artifact_path IS NOT NULL"
|
|
1392
|
-
: "(escalation_pending = 1 OR escalation_awaiting_review = 1) AND escalation_artifact_path IS NOT NULL";
|
|
1393
|
-
const rows = currentDb.prepare(`SELECT * FROM tasks WHERE milestone_id = :mid AND ${filter} ORDER BY slice_id, sequence, id`).all({ ":mid": milestoneId });
|
|
1394
|
-
return rows.map(rowToTask);
|
|
1395
|
-
}
|
|
1396
515
|
export function insertVerificationEvidence(e) {
|
|
1397
|
-
if (!
|
|
516
|
+
if (!getDbOrNull())
|
|
1398
517
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1399
|
-
|
|
518
|
+
getDbOrNull().prepare(`INSERT OR IGNORE INTO verification_evidence (task_id, slice_id, milestone_id, command, exit_code, verdict, duration_ms, created_at)
|
|
1400
519
|
VALUES (:task_id, :slice_id, :milestone_id, :command, :exit_code, :verdict, :duration_ms, :created_at)`).run({
|
|
1401
520
|
":task_id": e.taskId,
|
|
1402
521
|
":slice_id": e.sliceId,
|
|
@@ -1408,53 +527,33 @@ export function insertVerificationEvidence(e) {
|
|
|
1408
527
|
":created_at": new Date().toISOString(),
|
|
1409
528
|
});
|
|
1410
529
|
}
|
|
1411
|
-
export function getVerificationEvidence(milestoneId, sliceId, taskId) {
|
|
1412
|
-
if (!currentDb)
|
|
1413
|
-
return [];
|
|
1414
|
-
const rows = currentDb.prepare("SELECT * FROM verification_evidence WHERE milestone_id = :mid AND slice_id = :sid AND task_id = :tid ORDER BY id").all({ ":mid": milestoneId, ":sid": sliceId, ":tid": taskId });
|
|
1415
|
-
return rows;
|
|
1416
|
-
}
|
|
1417
|
-
export function getAllMilestones() {
|
|
1418
|
-
if (!currentDb)
|
|
1419
|
-
return [];
|
|
1420
|
-
const rows = currentDb.prepare("SELECT * FROM milestones ORDER BY CASE WHEN sequence > 0 THEN 0 ELSE 1 END, sequence, id").all();
|
|
1421
|
-
return rows.map(rowToMilestone);
|
|
1422
|
-
}
|
|
1423
|
-
export function getMilestone(id) {
|
|
1424
|
-
if (!currentDb)
|
|
1425
|
-
return null;
|
|
1426
|
-
const row = currentDb.prepare("SELECT * FROM milestones WHERE id = :id").get({ ":id": id });
|
|
1427
|
-
if (!row)
|
|
1428
|
-
return null;
|
|
1429
|
-
return rowToMilestone(row);
|
|
1430
|
-
}
|
|
1431
530
|
export function setMilestoneQueueOrder(order) {
|
|
1432
|
-
if (!
|
|
531
|
+
if (!getDbOrNull())
|
|
1433
532
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1434
|
-
|
|
533
|
+
getDbOrNull().exec("BEGIN IMMEDIATE");
|
|
1435
534
|
try {
|
|
1436
|
-
|
|
1437
|
-
const stmt =
|
|
535
|
+
getDbOrNull().prepare("UPDATE milestones SET sequence = 0").run();
|
|
536
|
+
const stmt = getDbOrNull().prepare("UPDATE milestones SET sequence = :sequence WHERE id = :id");
|
|
1438
537
|
order.forEach((id, index) => {
|
|
1439
538
|
stmt.run({ ":id": id, ":sequence": index + 1 });
|
|
1440
539
|
});
|
|
1441
|
-
|
|
540
|
+
getDbOrNull().exec("COMMIT");
|
|
1442
541
|
}
|
|
1443
542
|
catch (err) {
|
|
1444
|
-
|
|
543
|
+
getDbOrNull().exec("ROLLBACK");
|
|
1445
544
|
throw err;
|
|
1446
545
|
}
|
|
1447
546
|
}
|
|
1448
547
|
function getMilestoneStatusForUpdate(milestoneId) {
|
|
1449
|
-
if (!
|
|
548
|
+
if (!getDbOrNull())
|
|
1450
549
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1451
|
-
const row =
|
|
550
|
+
const row = getDbOrNull().prepare("SELECT status FROM milestones WHERE id = :id").get({ ":id": milestoneId });
|
|
1452
551
|
return typeof row?.["status"] === "string" ? row["status"] : null;
|
|
1453
552
|
}
|
|
1454
553
|
function writeMilestoneStatus(milestoneId, status, completedAt) {
|
|
1455
|
-
if (!
|
|
554
|
+
if (!getDbOrNull())
|
|
1456
555
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1457
|
-
|
|
556
|
+
getDbOrNull().prepare(`UPDATE milestones SET status = :status, completed_at = :completed_at WHERE id = :id`).run({ ":status": status, ":completed_at": completedAt ?? null, ":id": milestoneId });
|
|
1458
557
|
}
|
|
1459
558
|
/**
|
|
1460
559
|
* Update a milestone's status in the database.
|
|
@@ -1464,19 +563,13 @@ function writeMilestoneStatus(milestoneId, status, completedAt) {
|
|
|
1464
563
|
* must use reopenMilestoneStatus(), which is reserved for gsd_milestone_reopen.
|
|
1465
564
|
*/
|
|
1466
565
|
export function updateMilestoneStatus(milestoneId, status, completedAt) {
|
|
1467
|
-
|
|
1468
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1469
|
-
const currentStatus = getMilestoneStatusForUpdate(milestoneId);
|
|
1470
|
-
if (currentStatus && isClosedStatus(currentStatus) && !isClosedStatus(status)) {
|
|
1471
|
-
throw new Error(`Cannot update closed milestone ${milestoneId} from ${currentStatus} to ${status}; use gsd_milestone_reopen for an explicit reopen.`);
|
|
1472
|
-
}
|
|
1473
|
-
writeMilestoneStatus(milestoneId, status, completedAt);
|
|
566
|
+
applyStatusTransition({ entity: "milestone", milestoneId, status, completedAt });
|
|
1474
567
|
}
|
|
1475
568
|
/**
|
|
1476
569
|
* Explicit closed -> active transition for gsd_milestone_reopen only.
|
|
1477
570
|
*/
|
|
1478
571
|
export function reopenMilestoneStatus(milestoneId) {
|
|
1479
|
-
if (!
|
|
572
|
+
if (!getDbOrNull())
|
|
1480
573
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
1481
574
|
const currentStatus = getMilestoneStatusForUpdate(milestoneId);
|
|
1482
575
|
if (!currentStatus) {
|
|
@@ -1487,562 +580,24 @@ export function reopenMilestoneStatus(milestoneId) {
|
|
|
1487
580
|
}
|
|
1488
581
|
writeMilestoneStatus(milestoneId, "active", null);
|
|
1489
582
|
}
|
|
1490
|
-
export function getActiveMilestoneFromDb() {
|
|
1491
|
-
if (!currentDb)
|
|
1492
|
-
return null;
|
|
1493
|
-
const row = currentDb.prepare("SELECT * FROM milestones WHERE status NOT IN ('complete', 'done', 'skipped', 'closed', 'parked') ORDER BY id LIMIT 1").get();
|
|
1494
|
-
if (!row)
|
|
1495
|
-
return null;
|
|
1496
|
-
return rowToMilestone(row);
|
|
1497
|
-
}
|
|
1498
|
-
export function getActiveSliceFromDb(milestoneId) {
|
|
1499
|
-
if (!currentDb)
|
|
1500
|
-
return null;
|
|
1501
|
-
// Single query: find the first non-complete slice whose dependencies are all satisfied.
|
|
1502
|
-
// Uses json_each() to expand the JSON depends array and checks each dep is complete.
|
|
1503
|
-
const row = currentDb.prepare(`SELECT s.* FROM slices s
|
|
1504
|
-
WHERE s.milestone_id = :mid
|
|
1505
|
-
AND s.status NOT IN ('complete', 'done', 'skipped')
|
|
1506
|
-
AND NOT EXISTS (
|
|
1507
|
-
SELECT 1 FROM json_each(s.depends) AS dep
|
|
1508
|
-
WHERE dep.value NOT IN (
|
|
1509
|
-
SELECT id FROM slices WHERE milestone_id = :mid AND status IN ('complete', 'done', 'skipped')
|
|
1510
|
-
)
|
|
1511
|
-
)
|
|
1512
|
-
ORDER BY s.sequence, s.id
|
|
1513
|
-
LIMIT 1`).get({ ":mid": milestoneId });
|
|
1514
|
-
if (!row)
|
|
1515
|
-
return null;
|
|
1516
|
-
return rowToSlice(row);
|
|
1517
|
-
}
|
|
1518
|
-
export function getActiveTaskFromDb(milestoneId, sliceId) {
|
|
1519
|
-
if (!currentDb)
|
|
1520
|
-
return null;
|
|
1521
|
-
const row = currentDb.prepare("SELECT * FROM tasks WHERE milestone_id = :mid AND slice_id = :sid AND status NOT IN ('complete', 'done') ORDER BY sequence, id LIMIT 1").get({ ":mid": milestoneId, ":sid": sliceId });
|
|
1522
|
-
if (!row)
|
|
1523
|
-
return null;
|
|
1524
|
-
return rowToTask(row);
|
|
1525
|
-
}
|
|
1526
|
-
export function getMilestoneSlices(milestoneId) {
|
|
1527
|
-
if (!currentDb)
|
|
1528
|
-
return [];
|
|
1529
|
-
const rows = currentDb.prepare("SELECT * FROM slices WHERE milestone_id = :mid ORDER BY sequence, id").all({ ":mid": milestoneId });
|
|
1530
|
-
return rows.map(rowToSlice);
|
|
1531
|
-
}
|
|
1532
|
-
export function getArtifact(path) {
|
|
1533
|
-
if (!currentDb)
|
|
1534
|
-
return null;
|
|
1535
|
-
const row = currentDb.prepare("SELECT * FROM artifacts WHERE path = :path").get({ ":path": path });
|
|
1536
|
-
if (!row)
|
|
1537
|
-
return null;
|
|
1538
|
-
return rowToArtifact(row);
|
|
1539
|
-
}
|
|
1540
583
|
// ─── Lightweight Query Variants (hot-path optimized) ─────────────────────
|
|
1541
|
-
/** Fast milestone status check — avoids deserializing JSON planning fields. */
|
|
1542
|
-
export function getActiveMilestoneIdFromDb() {
|
|
1543
|
-
if (!currentDb)
|
|
1544
|
-
return null;
|
|
1545
|
-
const row = currentDb.prepare("SELECT id, status FROM milestones WHERE status NOT IN ('complete', 'done', 'skipped', 'closed', 'parked') ORDER BY id LIMIT 1").get();
|
|
1546
|
-
if (!row)
|
|
1547
|
-
return null;
|
|
1548
|
-
return rowToIdStatusSummary(row);
|
|
1549
|
-
}
|
|
1550
|
-
/** Fast slice status check — avoids deserializing JSON depends/planning fields. */
|
|
1551
|
-
export function getSliceStatusSummary(milestoneId) {
|
|
1552
|
-
if (!currentDb)
|
|
1553
|
-
return [];
|
|
1554
|
-
return currentDb.prepare("SELECT id, status FROM slices WHERE milestone_id = :mid ORDER BY sequence, id").all({ ":mid": milestoneId }).map(rowToIdStatusSummary);
|
|
1555
|
-
}
|
|
1556
|
-
/** Fast task status check — avoids deserializing JSON arrays and large text fields. */
|
|
1557
|
-
export function getActiveTaskIdFromDb(milestoneId, sliceId) {
|
|
1558
|
-
if (!currentDb)
|
|
1559
|
-
return null;
|
|
1560
|
-
const row = currentDb.prepare("SELECT id, status, title FROM tasks WHERE milestone_id = :mid AND slice_id = :sid AND status NOT IN ('complete', 'done') ORDER BY sequence, id LIMIT 1").get({ ":mid": milestoneId, ":sid": sliceId });
|
|
1561
|
-
if (!row)
|
|
1562
|
-
return null;
|
|
1563
|
-
return rowToActiveTaskSummary(row);
|
|
1564
|
-
}
|
|
1565
|
-
/** Count tasks by status for a slice — useful for progress reporting without full row load. */
|
|
1566
|
-
export function getSliceTaskCounts(milestoneId, sliceId) {
|
|
1567
|
-
if (!currentDb)
|
|
1568
|
-
return emptyTaskStatusCounts();
|
|
1569
|
-
const row = currentDb.prepare(`SELECT
|
|
1570
|
-
COUNT(*) as total,
|
|
1571
|
-
SUM(CASE WHEN status IN ('complete', 'done') THEN 1 ELSE 0 END) as done,
|
|
1572
|
-
SUM(CASE WHEN status NOT IN ('complete', 'done') THEN 1 ELSE 0 END) as pending
|
|
1573
|
-
FROM tasks WHERE milestone_id = :mid AND slice_id = :sid`).get({ ":mid": milestoneId, ":sid": sliceId });
|
|
1574
|
-
return rowToTaskStatusCounts(row);
|
|
1575
|
-
}
|
|
1576
584
|
// ─── Slice Dependencies (junction table) ─────────────────────────────────
|
|
1577
585
|
/** Sync the slice_dependencies junction table from a slice's JSON depends array. */
|
|
1578
586
|
export function syncSliceDependencies(milestoneId, sliceId, depends) {
|
|
1579
|
-
if (!
|
|
587
|
+
if (!getDbOrNull())
|
|
1580
588
|
return;
|
|
1581
|
-
|
|
589
|
+
getDbOrNull().prepare("DELETE FROM slice_dependencies WHERE milestone_id = :mid AND slice_id = :sid").run({ ":mid": milestoneId, ":sid": sliceId });
|
|
1582
590
|
for (const dep of depends) {
|
|
1583
|
-
|
|
591
|
+
getDbOrNull().prepare("INSERT OR IGNORE INTO slice_dependencies (milestone_id, slice_id, depends_on_slice_id) VALUES (:mid, :sid, :dep)").run({ ":mid": milestoneId, ":sid": sliceId, ":dep": dep });
|
|
1584
592
|
}
|
|
1585
593
|
}
|
|
1586
|
-
/** Get all slices that depend on a given slice. */
|
|
1587
|
-
export function getDependentSlices(milestoneId, sliceId) {
|
|
1588
|
-
if (!currentDb)
|
|
1589
|
-
return [];
|
|
1590
|
-
const rows = currentDb.prepare("SELECT slice_id FROM slice_dependencies WHERE milestone_id = :mid AND depends_on_slice_id = :sid").all({ ":mid": milestoneId, ":sid": sliceId });
|
|
1591
|
-
return rowsToStringColumn(rows, "slice_id");
|
|
1592
|
-
}
|
|
1593
594
|
// ─── Worktree DB Helpers ──────────────────────────────────────────────────
|
|
1594
|
-
export function copyWorktreeDb(srcDbPath, destDbPath) {
|
|
1595
|
-
try {
|
|
1596
|
-
if (!existsSync(srcDbPath))
|
|
1597
|
-
return false;
|
|
1598
|
-
const destDir = dirname(destDbPath);
|
|
1599
|
-
mkdirSync(destDir, { recursive: true });
|
|
1600
|
-
copyFileSync(srcDbPath, destDbPath);
|
|
1601
|
-
return true;
|
|
1602
|
-
}
|
|
1603
|
-
catch (err) {
|
|
1604
|
-
logError("db", "failed to copy DB to worktree", { error: err.message });
|
|
1605
|
-
return false;
|
|
1606
|
-
}
|
|
1607
|
-
}
|
|
1608
|
-
export function reconcileWorktreeDb(mainDbPath, worktreeDbPath) {
|
|
1609
|
-
const zero = {
|
|
1610
|
-
decisions: 0,
|
|
1611
|
-
requirements: 0,
|
|
1612
|
-
artifacts: 0,
|
|
1613
|
-
milestones: 0,
|
|
1614
|
-
slices: 0,
|
|
1615
|
-
tasks: 0,
|
|
1616
|
-
memories: 0,
|
|
1617
|
-
replan_history: 0,
|
|
1618
|
-
assessments: 0,
|
|
1619
|
-
quality_gates: 0,
|
|
1620
|
-
slice_dependencies: 0,
|
|
1621
|
-
verification_evidence: 0,
|
|
1622
|
-
gate_runs: 0,
|
|
1623
|
-
milestone_commit_attributions: 0,
|
|
1624
|
-
conflicts: [],
|
|
1625
|
-
};
|
|
1626
|
-
if (!existsSync(worktreeDbPath))
|
|
1627
|
-
return zero;
|
|
1628
|
-
// Guard: bail when both paths resolve to the same physical file.
|
|
1629
|
-
// ATTACHing a WAL-mode DB to itself corrupts the WAL (#2823).
|
|
1630
|
-
try {
|
|
1631
|
-
if (realpathSync(mainDbPath) === realpathSync(worktreeDbPath))
|
|
1632
|
-
return zero;
|
|
1633
|
-
}
|
|
1634
|
-
catch (e) {
|
|
1635
|
-
logWarning("db", `realpathSync failed: ${e.message}`);
|
|
1636
|
-
}
|
|
1637
|
-
// Sanitize path: reject any characters that could break ATTACH syntax.
|
|
1638
|
-
// ATTACH DATABASE doesn't support parameterized paths in all providers,
|
|
1639
|
-
// so we use strict allowlist validation instead.
|
|
1640
|
-
if (/['";\x00]/.test(worktreeDbPath)) {
|
|
1641
|
-
logError("db", "worktree DB reconciliation failed: path contains unsafe characters");
|
|
1642
|
-
return zero;
|
|
1643
|
-
}
|
|
1644
|
-
if (!currentDb) {
|
|
1645
|
-
const opened = openDatabase(mainDbPath);
|
|
1646
|
-
if (!opened) {
|
|
1647
|
-
logError("db", "worktree DB reconciliation failed: cannot open main DB");
|
|
1648
|
-
return zero;
|
|
1649
|
-
}
|
|
1650
|
-
}
|
|
1651
|
-
const adapter = currentDb;
|
|
1652
|
-
const conflicts = [];
|
|
1653
|
-
try {
|
|
1654
|
-
adapter.exec(`ATTACH DATABASE '${worktreeDbPath}' AS wt`);
|
|
1655
|
-
try {
|
|
1656
|
-
function countChanges(result) {
|
|
1657
|
-
return typeof result === "object" && result !== null ? (result.changes ?? 0) : 0;
|
|
1658
|
-
}
|
|
1659
|
-
function wtTableInfo(tableName) {
|
|
1660
|
-
return adapter.prepare(`PRAGMA wt.table_info('${tableName}')`).all();
|
|
1661
|
-
}
|
|
1662
|
-
const wtInfo = wtTableInfo("decisions");
|
|
1663
|
-
const hasWtDecisions = wtInfo.length > 0;
|
|
1664
|
-
const hasMadeBy = wtInfo.some((col) => col["name"] === "made_by");
|
|
1665
|
-
// ADR-011: worktree may predate schema v16/v17. For missing columns we
|
|
1666
|
-
// fall through to the main DB's existing value (not a literal default)
|
|
1667
|
-
// so reconcile never silently clears state the main tree has recorded.
|
|
1668
|
-
const hasDecisionSource = wtInfo.some((col) => col["name"] === "source");
|
|
1669
|
-
const wtRequirementInfo = wtTableInfo("requirements");
|
|
1670
|
-
const hasWtRequirements = wtRequirementInfo.length > 0;
|
|
1671
|
-
const wtMilestoneInfo = wtTableInfo("milestones");
|
|
1672
|
-
const hasWtMilestones = wtMilestoneInfo.length > 0;
|
|
1673
|
-
const hasMilestoneSequence = wtMilestoneInfo.some((col) => col["name"] === "sequence");
|
|
1674
|
-
const wtSliceInfo = wtTableInfo("slices");
|
|
1675
|
-
const hasWtSlices = wtSliceInfo.length > 0;
|
|
1676
|
-
const hasIsSketch = wtSliceInfo.some((col) => col["name"] === "is_sketch");
|
|
1677
|
-
const hasSketchScope = wtSliceInfo.some((col) => col["name"] === "sketch_scope");
|
|
1678
|
-
const hasSliceTargetRepositories = wtSliceInfo.some((col) => col["name"] === "target_repositories");
|
|
1679
|
-
const wtTaskInfo = wtTableInfo("tasks");
|
|
1680
|
-
const hasWtTasks = wtTaskInfo.length > 0;
|
|
1681
|
-
const hasTaskTargetRepositories = wtTaskInfo.some((col) => col["name"] === "target_repositories");
|
|
1682
|
-
const hasBlockerSource = wtTaskInfo.some((col) => col["name"] === "blocker_source");
|
|
1683
|
-
const hasEscalationPending = wtTaskInfo.some((col) => col["name"] === "escalation_pending");
|
|
1684
|
-
const hasEscalationAwaiting = wtTaskInfo.some((col) => col["name"] === "escalation_awaiting_review");
|
|
1685
|
-
const hasEscalationArtifact = wtTaskInfo.some((col) => col["name"] === "escalation_artifact_path");
|
|
1686
|
-
const hasEscalationOverride = wtTaskInfo.some((col) => col["name"] === "escalation_override_applied_at");
|
|
1687
|
-
const wtArtifactInfo = wtTableInfo("artifacts");
|
|
1688
|
-
const hasWtArtifacts = wtArtifactInfo.length > 0;
|
|
1689
|
-
const wtMemoryInfo = wtTableInfo("memories");
|
|
1690
|
-
const hasWtMemories = wtMemoryInfo.length > 0;
|
|
1691
|
-
const hasMemoryScope = wtMemoryInfo.some((col) => col["name"] === "scope");
|
|
1692
|
-
const hasMemoryTags = wtMemoryInfo.some((col) => col["name"] === "tags");
|
|
1693
|
-
const hasMemoryStructuredFields = wtMemoryInfo.some((col) => col["name"] === "structured_fields");
|
|
1694
|
-
const hasMemoryLastHitAt = wtMemoryInfo.some((col) => col["name"] === "last_hit_at");
|
|
1695
|
-
const hasWtReplanHistory = wtTableInfo("replan_history").length > 0;
|
|
1696
|
-
const hasWtAssessments = wtTableInfo("assessments").length > 0;
|
|
1697
|
-
const hasWtQualityGates = wtTableInfo("quality_gates").length > 0;
|
|
1698
|
-
const hasWtSliceDependencies = wtTableInfo("slice_dependencies").length > 0;
|
|
1699
|
-
const hasWtVerificationEvidence = wtTableInfo("verification_evidence").length > 0;
|
|
1700
|
-
const hasWtGateRuns = wtTableInfo("gate_runs").length > 0;
|
|
1701
|
-
const hasWtMilestoneCommitAttributions = wtTableInfo("milestone_commit_attributions").length > 0;
|
|
1702
|
-
if (hasWtDecisions) {
|
|
1703
|
-
const decConf = adapter.prepare(`SELECT m.id FROM decisions m INNER JOIN wt.decisions w ON m.id = w.id WHERE m.decision != w.decision OR m.choice != w.choice OR m.rationale != w.rationale OR ${hasMadeBy ? "m.made_by != w.made_by" : "'agent' != 'agent'"} OR m.superseded_by IS NOT w.superseded_by`).all();
|
|
1704
|
-
for (const row of decConf)
|
|
1705
|
-
conflicts.push(`decision ${row["id"]}: modified in both`);
|
|
1706
|
-
}
|
|
1707
|
-
if (hasWtRequirements) {
|
|
1708
|
-
const reqConf = adapter.prepare(`SELECT m.id FROM requirements m INNER JOIN wt.requirements w ON m.id = w.id WHERE m.description != w.description OR m.status != w.status OR m.notes != w.notes OR m.superseded_by IS NOT w.superseded_by`).all();
|
|
1709
|
-
for (const row of reqConf)
|
|
1710
|
-
conflicts.push(`requirement ${row["id"]}: modified in both`);
|
|
1711
|
-
}
|
|
1712
|
-
const merged = {
|
|
1713
|
-
decisions: 0,
|
|
1714
|
-
requirements: 0,
|
|
1715
|
-
artifacts: 0,
|
|
1716
|
-
milestones: 0,
|
|
1717
|
-
slices: 0,
|
|
1718
|
-
tasks: 0,
|
|
1719
|
-
memories: 0,
|
|
1720
|
-
replan_history: 0,
|
|
1721
|
-
assessments: 0,
|
|
1722
|
-
quality_gates: 0,
|
|
1723
|
-
slice_dependencies: 0,
|
|
1724
|
-
verification_evidence: 0,
|
|
1725
|
-
gate_runs: 0,
|
|
1726
|
-
milestone_commit_attributions: 0,
|
|
1727
|
-
};
|
|
1728
|
-
const sliceTargetRepositoriesSql = hasSliceTargetRepositories
|
|
1729
|
-
? `CASE
|
|
1730
|
-
WHEN w.target_repositories = '[]' AND COALESCE(m.target_repositories, '[]') <> '[]'
|
|
1731
|
-
THEN m.target_repositories
|
|
1732
|
-
ELSE COALESCE(w.target_repositories, m.target_repositories, '[]')
|
|
1733
|
-
END`
|
|
1734
|
-
: "COALESCE(m.target_repositories, '[]')";
|
|
1735
|
-
const taskTargetRepositoriesSql = hasTaskTargetRepositories
|
|
1736
|
-
? `CASE
|
|
1737
|
-
WHEN w.target_repositories = '[]' AND COALESCE(m.target_repositories, '[]') <> '[]'
|
|
1738
|
-
THEN m.target_repositories
|
|
1739
|
-
ELSE COALESCE(w.target_repositories, m.target_repositories, '[]')
|
|
1740
|
-
END`
|
|
1741
|
-
: "COALESCE(m.target_repositories, '[]')";
|
|
1742
|
-
adapter.exec("BEGIN");
|
|
1743
|
-
try {
|
|
1744
|
-
// Join the target decisions so we can prefer an existing main.source
|
|
1745
|
-
// when the worktree predates v16 — otherwise a write-through reconcile
|
|
1746
|
-
// would clobber 'escalation'-sourced decisions with the literal default.
|
|
1747
|
-
if (hasWtDecisions) {
|
|
1748
|
-
merged.decisions = countChanges(adapter.prepare(`
|
|
1749
|
-
INSERT INTO decisions (
|
|
1750
|
-
id, when_context, scope, decision, choice, rationale, revisable, made_by, source, superseded_by
|
|
1751
|
-
)
|
|
1752
|
-
SELECT w.id, w.when_context, w.scope, w.decision, w.choice, w.rationale, w.revisable, ${hasMadeBy ? "w.made_by" : "COALESCE(m.made_by, 'agent')"}, ${hasDecisionSource ? "w.source" : "COALESCE(m.source, 'discussion')"}, w.superseded_by
|
|
1753
|
-
FROM wt.decisions w
|
|
1754
|
-
LEFT JOIN decisions m ON m.id = w.id
|
|
1755
|
-
WHERE true
|
|
1756
|
-
ON CONFLICT(id) DO UPDATE SET
|
|
1757
|
-
when_context = excluded.when_context,
|
|
1758
|
-
scope = excluded.scope,
|
|
1759
|
-
decision = excluded.decision,
|
|
1760
|
-
choice = excluded.choice,
|
|
1761
|
-
rationale = excluded.rationale,
|
|
1762
|
-
revisable = excluded.revisable,
|
|
1763
|
-
made_by = excluded.made_by,
|
|
1764
|
-
source = excluded.source,
|
|
1765
|
-
superseded_by = excluded.superseded_by
|
|
1766
|
-
`).run());
|
|
1767
|
-
}
|
|
1768
|
-
if (hasWtRequirements) {
|
|
1769
|
-
merged.requirements = countChanges(adapter.prepare(`
|
|
1770
|
-
INSERT OR REPLACE INTO requirements (
|
|
1771
|
-
id, class, status, description, why, source, primary_owner,
|
|
1772
|
-
supporting_slices, validation, notes, full_content, superseded_by
|
|
1773
|
-
)
|
|
1774
|
-
SELECT id, class, status, description, why, source, primary_owner,
|
|
1775
|
-
supporting_slices, validation, notes, full_content, superseded_by
|
|
1776
|
-
FROM wt.requirements
|
|
1777
|
-
`).run());
|
|
1778
|
-
}
|
|
1779
|
-
// Always recompute artifact hashes from the content being merged. Older
|
|
1780
|
-
// worktree DBs may not have content_hash at all, and migrated old DBs can
|
|
1781
|
-
// carry stale default/null hashes after their content changed.
|
|
1782
|
-
if (hasWtArtifacts) {
|
|
1783
|
-
const artifactRows = adapter.prepare(`
|
|
1784
|
-
SELECT path, artifact_type, milestone_id, slice_id, task_id, full_content, imported_at
|
|
1785
|
-
FROM wt.artifacts
|
|
1786
|
-
`).all();
|
|
1787
|
-
const artifactStmt = adapter.prepare(`
|
|
1788
|
-
INSERT OR REPLACE INTO artifacts (
|
|
1789
|
-
path, artifact_type, milestone_id, slice_id, task_id, full_content, imported_at, content_hash
|
|
1790
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
1791
|
-
`);
|
|
1792
|
-
for (const row of artifactRows) {
|
|
1793
|
-
const fullContent = String(row["full_content"] ?? "");
|
|
1794
|
-
merged.artifacts += countChanges(artifactStmt.run(row["path"], row["artifact_type"], row["milestone_id"] ?? null, row["slice_id"] ?? null, row["task_id"] ?? null, fullContent, row["imported_at"], createHash("sha256").update(fullContent).digest("hex")));
|
|
1795
|
-
}
|
|
1796
|
-
}
|
|
1797
|
-
// Merge milestones — worktree may have updated status/planning fields.
|
|
1798
|
-
// Never downgrade status: complete > active > pre-planning (#4372).
|
|
1799
|
-
// A stale worktree may carry an older 'active' status for a milestone
|
|
1800
|
-
// that the main DB has already marked 'complete'; preserve the higher status.
|
|
1801
|
-
if (hasWtMilestones) {
|
|
1802
|
-
merged.milestones = countChanges(adapter.prepare(`
|
|
1803
|
-
INSERT OR REPLACE INTO milestones (
|
|
1804
|
-
id, title, status, depends_on, created_at, completed_at,
|
|
1805
|
-
vision, success_criteria, key_risks, proof_strategy,
|
|
1806
|
-
verification_contract, verification_integration, verification_operational, verification_uat,
|
|
1807
|
-
definition_of_done, requirement_coverage, boundary_map_markdown, sequence
|
|
1808
|
-
)
|
|
1809
|
-
SELECT w.id, w.title,
|
|
1810
|
-
CASE
|
|
1811
|
-
WHEN m.status IN (${TERMINAL_STATUS_SQL}) AND w.status NOT IN (${TERMINAL_STATUS_SQL})
|
|
1812
|
-
THEN m.status ELSE w.status
|
|
1813
|
-
END,
|
|
1814
|
-
w.depends_on,
|
|
1815
|
-
CASE
|
|
1816
|
-
WHEN m.status IN (${TERMINAL_STATUS_SQL}) AND w.status NOT IN (${TERMINAL_STATUS_SQL})
|
|
1817
|
-
THEN m.created_at ELSE w.created_at
|
|
1818
|
-
END,
|
|
1819
|
-
CASE
|
|
1820
|
-
WHEN m.status IN (${TERMINAL_STATUS_SQL}) AND w.status NOT IN (${TERMINAL_STATUS_SQL})
|
|
1821
|
-
THEN m.completed_at ELSE w.completed_at
|
|
1822
|
-
END,
|
|
1823
|
-
w.vision, w.success_criteria, w.key_risks, w.proof_strategy,
|
|
1824
|
-
w.verification_contract, w.verification_integration, w.verification_operational, w.verification_uat,
|
|
1825
|
-
w.definition_of_done, w.requirement_coverage, w.boundary_map_markdown,
|
|
1826
|
-
${hasMilestoneSequence ? "COALESCE(w.sequence, 0)" : "COALESCE(m.sequence, 0)"}
|
|
1827
|
-
FROM wt.milestones w
|
|
1828
|
-
LEFT JOIN milestones m ON m.id = w.id
|
|
1829
|
-
`).run());
|
|
1830
|
-
}
|
|
1831
|
-
// Merge slices — preserve worktree progress but never downgrade completed status (#2558).
|
|
1832
|
-
// ADR-011 Phase 1: carry is_sketch + sketch_scope so reconcile doesn't
|
|
1833
|
-
// silently clear sketch metadata. When the worktree predates v16,
|
|
1834
|
-
// fall back to the main DB's existing value rather than a literal 0/''.
|
|
1835
|
-
if (hasWtSlices) {
|
|
1836
|
-
merged.slices = countChanges(adapter.prepare(`
|
|
1837
|
-
INSERT OR REPLACE INTO slices (
|
|
1838
|
-
milestone_id, id, title, status, risk, depends, demo, created_at, completed_at,
|
|
1839
|
-
full_summary_md, full_uat_md, goal, success_criteria, proof_level,
|
|
1840
|
-
integration_closure, observability_impact, target_repositories, sequence, replan_triggered_at,
|
|
1841
|
-
is_sketch, sketch_scope
|
|
1842
|
-
)
|
|
1843
|
-
SELECT w.milestone_id, w.id, w.title,
|
|
1844
|
-
CASE
|
|
1845
|
-
WHEN m.status IN (${TERMINAL_STATUS_SQL}) AND w.status NOT IN (${TERMINAL_STATUS_SQL})
|
|
1846
|
-
THEN m.status ELSE w.status
|
|
1847
|
-
END,
|
|
1848
|
-
w.risk, w.depends, w.demo, w.created_at,
|
|
1849
|
-
CASE
|
|
1850
|
-
WHEN m.status IN (${TERMINAL_STATUS_SQL}) AND w.status NOT IN (${TERMINAL_STATUS_SQL})
|
|
1851
|
-
THEN m.completed_at ELSE w.completed_at
|
|
1852
|
-
END,
|
|
1853
|
-
w.full_summary_md, w.full_uat_md, w.goal, w.success_criteria, w.proof_level,
|
|
1854
|
-
w.integration_closure, w.observability_impact,
|
|
1855
|
-
${sliceTargetRepositoriesSql},
|
|
1856
|
-
w.sequence, w.replan_triggered_at,
|
|
1857
|
-
${hasIsSketch ? "w.is_sketch" : "COALESCE(m.is_sketch, 0)"},
|
|
1858
|
-
${hasSketchScope ? "w.sketch_scope" : "COALESCE(m.sketch_scope, '')"}
|
|
1859
|
-
FROM wt.slices w
|
|
1860
|
-
LEFT JOIN slices m ON m.milestone_id = w.milestone_id AND m.id = w.id
|
|
1861
|
-
`).run());
|
|
1862
|
-
}
|
|
1863
|
-
// Merge tasks — preserve execution results, never downgrade completed status (#2558).
|
|
1864
|
-
// ADR-011 P2: carry blocker_source + escalation_* columns so worktree reconcile
|
|
1865
|
-
// doesn't silently clear escalation state back to defaults.
|
|
1866
|
-
if (hasWtTasks) {
|
|
1867
|
-
merged.tasks = countChanges(adapter.prepare(`
|
|
1868
|
-
INSERT OR REPLACE INTO tasks (
|
|
1869
|
-
milestone_id, slice_id, id, title, status, one_liner, narrative,
|
|
1870
|
-
verification_result, duration, completed_at, blocker_discovered,
|
|
1871
|
-
deviations, known_issues, key_files, key_decisions, full_summary_md,
|
|
1872
|
-
description, estimate, files, verify, inputs, expected_output,
|
|
1873
|
-
observability_impact, full_plan_md, target_repositories, sequence,
|
|
1874
|
-
blocker_source, escalation_pending, escalation_awaiting_review,
|
|
1875
|
-
escalation_artifact_path, escalation_override_applied_at
|
|
1876
|
-
)
|
|
1877
|
-
SELECT w.milestone_id, w.slice_id, w.id, w.title,
|
|
1878
|
-
CASE
|
|
1879
|
-
WHEN m.status IN (${TERMINAL_STATUS_SQL}) AND w.status NOT IN (${TERMINAL_STATUS_SQL})
|
|
1880
|
-
THEN m.status ELSE w.status
|
|
1881
|
-
END,
|
|
1882
|
-
w.one_liner, w.narrative,
|
|
1883
|
-
w.verification_result, w.duration,
|
|
1884
|
-
CASE
|
|
1885
|
-
WHEN m.status IN (${TERMINAL_STATUS_SQL}) AND w.status NOT IN (${TERMINAL_STATUS_SQL})
|
|
1886
|
-
THEN m.completed_at ELSE w.completed_at
|
|
1887
|
-
END,
|
|
1888
|
-
w.blocker_discovered,
|
|
1889
|
-
w.deviations, w.known_issues, w.key_files, w.key_decisions, w.full_summary_md,
|
|
1890
|
-
w.description, w.estimate, w.files, w.verify, w.inputs, w.expected_output,
|
|
1891
|
-
w.observability_impact, w.full_plan_md,
|
|
1892
|
-
${taskTargetRepositoriesSql},
|
|
1893
|
-
w.sequence,
|
|
1894
|
-
${hasBlockerSource ? "w.blocker_source" : "COALESCE(m.blocker_source, '')"},
|
|
1895
|
-
${hasEscalationPending ? "w.escalation_pending" : "COALESCE(m.escalation_pending, 0)"},
|
|
1896
|
-
${hasEscalationAwaiting ? "w.escalation_awaiting_review" : "COALESCE(m.escalation_awaiting_review, 0)"},
|
|
1897
|
-
${hasEscalationArtifact ? "w.escalation_artifact_path" : "m.escalation_artifact_path"},
|
|
1898
|
-
${hasEscalationOverride ? "w.escalation_override_applied_at" : "m.escalation_override_applied_at"}
|
|
1899
|
-
FROM wt.tasks w
|
|
1900
|
-
LEFT JOIN tasks m ON m.milestone_id = w.milestone_id AND m.slice_id = w.slice_id AND m.id = w.id
|
|
1901
|
-
`).run());
|
|
1902
|
-
}
|
|
1903
|
-
// Merge memories — keep worktree-learned insights.
|
|
1904
|
-
// V18 (scope, tags), V21 (structured_fields), V28 (last_hit_at): for each
|
|
1905
|
-
// column the wt may not yet have (older worktree DB), fall back to the
|
|
1906
|
-
// main DB's existing value via LEFT JOIN so reconcile never silently
|
|
1907
|
-
// resets these fields to defaults on rows that already had them.
|
|
1908
|
-
if (hasWtMemories) {
|
|
1909
|
-
merged.memories = countChanges(adapter.prepare(`
|
|
1910
|
-
INSERT OR REPLACE INTO memories (
|
|
1911
|
-
seq, id, category, content, confidence, source_unit_type, source_unit_id,
|
|
1912
|
-
created_at, updated_at, superseded_by, hit_count,
|
|
1913
|
-
scope, tags, structured_fields, last_hit_at
|
|
1914
|
-
)
|
|
1915
|
-
SELECT w.seq, w.id, w.category, w.content, w.confidence, w.source_unit_type, w.source_unit_id,
|
|
1916
|
-
w.created_at, w.updated_at, w.superseded_by, w.hit_count,
|
|
1917
|
-
${hasMemoryScope ? "w.scope" : "COALESCE(m.scope, 'project')"},
|
|
1918
|
-
${hasMemoryTags ? "w.tags" : "COALESCE(m.tags, '[]')"},
|
|
1919
|
-
${hasMemoryStructuredFields ? "w.structured_fields" : "m.structured_fields"},
|
|
1920
|
-
${hasMemoryLastHitAt ? "w.last_hit_at" : "m.last_hit_at"}
|
|
1921
|
-
FROM wt.memories w
|
|
1922
|
-
LEFT JOIN memories m ON m.id = w.id
|
|
1923
|
-
`).run());
|
|
1924
|
-
}
|
|
1925
|
-
if (hasWtReplanHistory) {
|
|
1926
|
-
merged.replan_history = countChanges(adapter.prepare(`
|
|
1927
|
-
INSERT INTO replan_history (
|
|
1928
|
-
milestone_id, slice_id, task_id, summary, previous_artifact_path, replacement_artifact_path, created_at
|
|
1929
|
-
)
|
|
1930
|
-
SELECT w.milestone_id, w.slice_id, w.task_id, w.summary, w.previous_artifact_path, w.replacement_artifact_path, w.created_at
|
|
1931
|
-
FROM wt.replan_history w
|
|
1932
|
-
WHERE EXISTS (SELECT 1 FROM milestones m WHERE m.id = w.milestone_id)
|
|
1933
|
-
AND NOT EXISTS (
|
|
1934
|
-
SELECT 1 FROM replan_history m
|
|
1935
|
-
WHERE m.milestone_id = w.milestone_id
|
|
1936
|
-
AND m.slice_id IS w.slice_id
|
|
1937
|
-
AND m.task_id IS w.task_id
|
|
1938
|
-
AND m.summary = w.summary
|
|
1939
|
-
AND m.previous_artifact_path IS w.previous_artifact_path
|
|
1940
|
-
AND m.replacement_artifact_path IS w.replacement_artifact_path
|
|
1941
|
-
)
|
|
1942
|
-
`).run());
|
|
1943
|
-
}
|
|
1944
|
-
if (hasWtAssessments) {
|
|
1945
|
-
merged.assessments = countChanges(adapter.prepare(`
|
|
1946
|
-
INSERT OR REPLACE INTO assessments (
|
|
1947
|
-
path, milestone_id, slice_id, task_id, status, scope, full_content, created_at
|
|
1948
|
-
)
|
|
1949
|
-
SELECT w.path, w.milestone_id, w.slice_id, w.task_id, w.status, w.scope, w.full_content, w.created_at
|
|
1950
|
-
FROM wt.assessments w
|
|
1951
|
-
WHERE EXISTS (SELECT 1 FROM milestones m WHERE m.id = w.milestone_id)
|
|
1952
|
-
`).run());
|
|
1953
|
-
}
|
|
1954
|
-
if (hasWtQualityGates) {
|
|
1955
|
-
merged.quality_gates = countChanges(adapter.prepare(`
|
|
1956
|
-
INSERT OR REPLACE INTO quality_gates (
|
|
1957
|
-
milestone_id, slice_id, gate_id, scope, task_id, status, verdict, rationale, findings, evaluated_at
|
|
1958
|
-
)
|
|
1959
|
-
SELECT w.milestone_id, w.slice_id, w.gate_id, w.scope, COALESCE(w.task_id, ''), w.status, w.verdict, w.rationale, w.findings, w.evaluated_at
|
|
1960
|
-
FROM wt.quality_gates w
|
|
1961
|
-
WHERE EXISTS (SELECT 1 FROM slices s WHERE s.milestone_id = w.milestone_id AND s.id = w.slice_id)
|
|
1962
|
-
`).run());
|
|
1963
|
-
}
|
|
1964
|
-
if (hasWtSliceDependencies) {
|
|
1965
|
-
merged.slice_dependencies = countChanges(adapter.prepare(`
|
|
1966
|
-
INSERT OR IGNORE INTO slice_dependencies (milestone_id, slice_id, depends_on_slice_id)
|
|
1967
|
-
SELECT w.milestone_id, w.slice_id, w.depends_on_slice_id
|
|
1968
|
-
FROM wt.slice_dependencies w
|
|
1969
|
-
WHERE EXISTS (SELECT 1 FROM slices s WHERE s.milestone_id = w.milestone_id AND s.id = w.slice_id)
|
|
1970
|
-
AND EXISTS (SELECT 1 FROM slices d WHERE d.milestone_id = w.milestone_id AND d.id = w.depends_on_slice_id)
|
|
1971
|
-
`).run());
|
|
1972
|
-
}
|
|
1973
|
-
// Merge verification evidence — append-only, use INSERT OR IGNORE to avoid duplicates
|
|
1974
|
-
if (hasWtVerificationEvidence) {
|
|
1975
|
-
merged.verification_evidence = countChanges(adapter.prepare(`
|
|
1976
|
-
INSERT OR IGNORE INTO verification_evidence (
|
|
1977
|
-
task_id, slice_id, milestone_id, command, exit_code, verdict, duration_ms, created_at
|
|
1978
|
-
)
|
|
1979
|
-
SELECT task_id, slice_id, milestone_id, command, exit_code, verdict, duration_ms, created_at
|
|
1980
|
-
FROM wt.verification_evidence
|
|
1981
|
-
`).run());
|
|
1982
|
-
}
|
|
1983
|
-
if (hasWtGateRuns) {
|
|
1984
|
-
merged.gate_runs = countChanges(adapter.prepare(`
|
|
1985
|
-
INSERT INTO gate_runs (
|
|
1986
|
-
trace_id, turn_id, gate_id, gate_type, unit_type, unit_id, milestone_id, slice_id, task_id,
|
|
1987
|
-
outcome, failure_class, rationale, findings, attempt, max_attempts, retryable, evaluated_at
|
|
1988
|
-
)
|
|
1989
|
-
SELECT w.trace_id, w.turn_id, w.gate_id, w.gate_type, w.unit_type, w.unit_id, w.milestone_id, w.slice_id, w.task_id,
|
|
1990
|
-
w.outcome, w.failure_class, w.rationale, w.findings, w.attempt, w.max_attempts, w.retryable, w.evaluated_at
|
|
1991
|
-
FROM wt.gate_runs w
|
|
1992
|
-
WHERE NOT EXISTS (
|
|
1993
|
-
SELECT 1 FROM gate_runs m
|
|
1994
|
-
WHERE m.trace_id = w.trace_id
|
|
1995
|
-
AND m.turn_id = w.turn_id
|
|
1996
|
-
AND m.gate_id = w.gate_id
|
|
1997
|
-
AND m.attempt = w.attempt
|
|
1998
|
-
AND m.evaluated_at = w.evaluated_at
|
|
1999
|
-
)
|
|
2000
|
-
`).run());
|
|
2001
|
-
}
|
|
2002
|
-
if (hasWtMilestoneCommitAttributions) {
|
|
2003
|
-
merged.milestone_commit_attributions = countChanges(adapter.prepare(`
|
|
2004
|
-
INSERT OR REPLACE INTO milestone_commit_attributions (
|
|
2005
|
-
commit_sha, milestone_id, slice_id, task_id, source, confidence, files_json, created_at
|
|
2006
|
-
)
|
|
2007
|
-
SELECT w.commit_sha, w.milestone_id, w.slice_id, w.task_id, w.source, w.confidence, w.files_json, w.created_at
|
|
2008
|
-
FROM wt.milestone_commit_attributions w
|
|
2009
|
-
WHERE EXISTS (SELECT 1 FROM milestones m WHERE m.id = w.milestone_id)
|
|
2010
|
-
`).run());
|
|
2011
|
-
}
|
|
2012
|
-
adapter.exec("COMMIT");
|
|
2013
|
-
}
|
|
2014
|
-
catch (txErr) {
|
|
2015
|
-
try {
|
|
2016
|
-
adapter.exec("ROLLBACK");
|
|
2017
|
-
}
|
|
2018
|
-
catch (e) {
|
|
2019
|
-
logWarning("db", `rollback failed: ${e.message}`);
|
|
2020
|
-
}
|
|
2021
|
-
throw txErr;
|
|
2022
|
-
}
|
|
2023
|
-
return { ...merged, conflicts };
|
|
2024
|
-
}
|
|
2025
|
-
finally {
|
|
2026
|
-
try {
|
|
2027
|
-
adapter.exec("DETACH DATABASE wt");
|
|
2028
|
-
}
|
|
2029
|
-
catch (e) {
|
|
2030
|
-
logWarning("db", `detach worktree DB failed: ${e.message}`);
|
|
2031
|
-
}
|
|
2032
|
-
}
|
|
2033
|
-
}
|
|
2034
|
-
catch (err) {
|
|
2035
|
-
logError("db", "worktree DB reconciliation failed", { error: err.message });
|
|
2036
|
-
return { ...zero, conflicts };
|
|
2037
|
-
}
|
|
2038
|
-
}
|
|
2039
|
-
// ─── Replan & Assessment Helpers ──────────────────────────────────────────
|
|
2040
595
|
export function insertReplanHistory(entry) {
|
|
2041
|
-
if (!
|
|
596
|
+
if (!getDbOrNull())
|
|
2042
597
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2043
598
|
// INSERT OR REPLACE: idempotent on (milestone_id, slice_id, task_id) via schema v11 unique index.
|
|
2044
599
|
// Retrying the same replan silently updates summary instead of accumulating duplicate rows.
|
|
2045
|
-
|
|
600
|
+
getDbOrNull().prepare(`INSERT OR REPLACE INTO replan_history (milestone_id, slice_id, task_id, summary, previous_artifact_path, replacement_artifact_path, created_at)
|
|
2046
601
|
VALUES (:milestone_id, :slice_id, :task_id, :summary, :previous_artifact_path, :replacement_artifact_path, :created_at)`).run({
|
|
2047
602
|
":milestone_id": entry.milestoneId,
|
|
2048
603
|
":slice_id": entry.sliceId ?? null,
|
|
@@ -2054,12 +609,12 @@ export function insertReplanHistory(entry) {
|
|
|
2054
609
|
});
|
|
2055
610
|
}
|
|
2056
611
|
export function insertAssessment(entry) {
|
|
2057
|
-
if (!
|
|
612
|
+
if (!getDbOrNull())
|
|
2058
613
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2059
614
|
// Idempotent: PRIMARY KEY is `path`, which is deterministic given (milestone_id, scope) per
|
|
2060
615
|
// the artifact-path resolver. Retrying the same reassess-roadmap silently overwrites the row
|
|
2061
616
|
// instead of accumulating duplicates.
|
|
2062
|
-
|
|
617
|
+
getDbOrNull().prepare(`INSERT OR REPLACE INTO assessments (path, milestone_id, slice_id, task_id, status, scope, full_content, created_at)
|
|
2063
618
|
VALUES (:path, :milestone_id, :slice_id, :task_id, :status, :scope, :full_content, :created_at)`).run({
|
|
2064
619
|
":path": entry.path,
|
|
2065
620
|
":milestone_id": entry.milestoneId,
|
|
@@ -2072,59 +627,66 @@ export function insertAssessment(entry) {
|
|
|
2072
627
|
});
|
|
2073
628
|
}
|
|
2074
629
|
export function deleteAssessmentByScope(milestoneId, scope) {
|
|
2075
|
-
if (!
|
|
630
|
+
if (!getDbOrNull())
|
|
2076
631
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2077
|
-
|
|
632
|
+
getDbOrNull().prepare(`DELETE FROM assessments WHERE milestone_id = :mid AND scope = :scope`).run({ ":mid": milestoneId, ":scope": scope });
|
|
2078
633
|
}
|
|
2079
634
|
export function deleteVerificationEvidence(milestoneId, sliceId, taskId) {
|
|
2080
|
-
if (!
|
|
635
|
+
if (!getDbOrNull())
|
|
2081
636
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2082
|
-
|
|
637
|
+
getDbOrNull().prepare(`DELETE FROM verification_evidence WHERE milestone_id = :mid AND slice_id = :sid AND task_id = :tid`).run({ ":mid": milestoneId, ":sid": sliceId, ":tid": taskId });
|
|
2083
638
|
}
|
|
2084
639
|
export function deleteTask(milestoneId, sliceId, taskId) {
|
|
2085
|
-
if (!
|
|
640
|
+
if (!getDbOrNull())
|
|
2086
641
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2087
642
|
transaction(() => {
|
|
2088
643
|
// Must delete verification_evidence first (FK constraint)
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
644
|
+
getDbOrNull().prepare(`DELETE FROM verification_evidence WHERE milestone_id = :mid AND slice_id = :sid AND task_id = :tid`).run({ ":mid": milestoneId, ":sid": sliceId, ":tid": taskId });
|
|
645
|
+
getDbOrNull().prepare(`DELETE FROM quality_gates WHERE milestone_id = :mid AND slice_id = :sid AND task_id = :tid`).run({ ":mid": milestoneId, ":sid": sliceId, ":tid": taskId });
|
|
646
|
+
getDbOrNull().prepare(`DELETE FROM tasks WHERE milestone_id = :mid AND slice_id = :sid AND id = :tid`).run({ ":mid": milestoneId, ":sid": sliceId, ":tid": taskId });
|
|
2092
647
|
});
|
|
2093
648
|
}
|
|
2094
649
|
export function deleteSlice(milestoneId, sliceId) {
|
|
2095
|
-
if (!
|
|
650
|
+
if (!getDbOrNull())
|
|
2096
651
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2097
652
|
transaction(() => {
|
|
2098
653
|
// Cascade-style manual deletion: evidence → tasks → dependencies → slice
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
654
|
+
getDbOrNull().prepare(`DELETE FROM verification_evidence WHERE milestone_id = :mid AND slice_id = :sid`).run({ ":mid": milestoneId, ":sid": sliceId });
|
|
655
|
+
getDbOrNull().prepare(`DELETE FROM tasks WHERE milestone_id = :mid AND slice_id = :sid`).run({ ":mid": milestoneId, ":sid": sliceId });
|
|
656
|
+
getDbOrNull().prepare(`DELETE FROM slice_dependencies WHERE milestone_id = :mid AND slice_id = :sid`).run({ ":mid": milestoneId, ":sid": sliceId });
|
|
657
|
+
getDbOrNull().prepare(`DELETE FROM slice_dependencies WHERE milestone_id = :mid AND depends_on_slice_id = :sid`).run({ ":mid": milestoneId, ":sid": sliceId });
|
|
658
|
+
getDbOrNull().prepare(`DELETE FROM slices WHERE milestone_id = :mid AND id = :sid`).run({ ":mid": milestoneId, ":sid": sliceId });
|
|
2104
659
|
});
|
|
2105
660
|
}
|
|
2106
661
|
export function deleteMilestone(milestoneId) {
|
|
2107
|
-
if (!
|
|
662
|
+
if (!getDbOrNull())
|
|
2108
663
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2109
664
|
transaction(() => {
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
665
|
+
getDbOrNull().prepare(`DELETE FROM verification_evidence WHERE milestone_id = :mid`).run({ ":mid": milestoneId });
|
|
666
|
+
getDbOrNull().prepare(`DELETE FROM quality_gates WHERE milestone_id = :mid`).run({ ":mid": milestoneId });
|
|
667
|
+
getDbOrNull().prepare(`DELETE FROM gate_runs WHERE milestone_id = :mid`).run({ ":mid": milestoneId });
|
|
668
|
+
getDbOrNull().prepare(`DELETE FROM tasks WHERE milestone_id = :mid`).run({ ":mid": milestoneId });
|
|
669
|
+
getDbOrNull().prepare(`DELETE FROM slice_dependencies WHERE milestone_id = :mid`).run({ ":mid": milestoneId });
|
|
670
|
+
getDbOrNull().prepare(`DELETE FROM slices WHERE milestone_id = :mid`).run({ ":mid": milestoneId });
|
|
671
|
+
getDbOrNull().prepare(`DELETE FROM replan_history WHERE milestone_id = :mid`).run({ ":mid": milestoneId });
|
|
672
|
+
getDbOrNull().prepare(`DELETE FROM assessments WHERE milestone_id = :mid`).run({ ":mid": milestoneId });
|
|
673
|
+
getDbOrNull().prepare(`DELETE FROM artifacts WHERE milestone_id = :mid`).run({ ":mid": milestoneId });
|
|
674
|
+
getDbOrNull().prepare(`DELETE FROM milestone_commit_attributions WHERE milestone_id = :mid`).run({ ":mid": milestoneId });
|
|
675
|
+
getDbOrNull().prepare(`DELETE FROM milestone_leases WHERE milestone_id = :mid`).run({ ":mid": milestoneId });
|
|
676
|
+
getDbOrNull().prepare(`DELETE FROM milestones WHERE id = :mid`).run({ ":mid": milestoneId });
|
|
2122
677
|
});
|
|
2123
678
|
}
|
|
2124
679
|
export function updateSliceFields(milestoneId, sliceId, fields) {
|
|
2125
|
-
if (!
|
|
680
|
+
if (!getDbOrNull())
|
|
2126
681
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2127
|
-
|
|
682
|
+
const SLICE_ID_RE = /^[A-Za-z0-9][A-Za-z0-9-]*$/;
|
|
683
|
+
if (fields.depends !== undefined) {
|
|
684
|
+
const invalidDep = fields.depends.find(d => !SLICE_ID_RE.test(d));
|
|
685
|
+
if (invalidDep !== undefined) {
|
|
686
|
+
throw new GSDError(GSD_STALE_STATE, `updateSliceFields: depends element "${invalidDep}" is not a valid slice ID`);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
getDbOrNull().prepare(`UPDATE slices SET
|
|
2128
690
|
title = COALESCE(:title, title),
|
|
2129
691
|
risk = COALESCE(:risk, risk),
|
|
2130
692
|
depends = COALESCE(:depends, depends),
|
|
@@ -2138,34 +700,11 @@ export function updateSliceFields(milestoneId, sliceId, fields) {
|
|
|
2138
700
|
":demo": fields.demo ?? null,
|
|
2139
701
|
});
|
|
2140
702
|
}
|
|
2141
|
-
export function getReplanHistory(milestoneId, sliceId) {
|
|
2142
|
-
if (!currentDb)
|
|
2143
|
-
return [];
|
|
2144
|
-
if (sliceId) {
|
|
2145
|
-
return currentDb.prepare(`SELECT * FROM replan_history WHERE milestone_id = :mid AND slice_id = :sid ORDER BY created_at DESC`).all({ ":mid": milestoneId, ":sid": sliceId });
|
|
2146
|
-
}
|
|
2147
|
-
return currentDb.prepare(`SELECT * FROM replan_history WHERE milestone_id = :mid ORDER BY created_at DESC`).all({ ":mid": milestoneId });
|
|
2148
|
-
}
|
|
2149
|
-
export function getAssessment(path) {
|
|
2150
|
-
if (!currentDb)
|
|
2151
|
-
return null;
|
|
2152
|
-
const row = currentDb.prepare(`SELECT * FROM assessments WHERE path = :path`).get({ ":path": path });
|
|
2153
|
-
return row ?? null;
|
|
2154
|
-
}
|
|
2155
|
-
export function getLatestAssessmentByScope(milestoneId, scope) {
|
|
2156
|
-
if (!currentDb)
|
|
2157
|
-
return null;
|
|
2158
|
-
const row = currentDb.prepare(`SELECT * FROM assessments
|
|
2159
|
-
WHERE milestone_id = :mid AND scope = :scope
|
|
2160
|
-
ORDER BY created_at DESC
|
|
2161
|
-
LIMIT 1`).get({ ":mid": milestoneId, ":scope": scope });
|
|
2162
|
-
return row ?? null;
|
|
2163
|
-
}
|
|
2164
703
|
// ─── Quality Gates ───────────────────────────────────────────────────────
|
|
2165
704
|
export function insertGateRow(g) {
|
|
2166
|
-
if (!
|
|
705
|
+
if (!getDbOrNull())
|
|
2167
706
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2168
|
-
|
|
707
|
+
getDbOrNull().prepare(`INSERT OR IGNORE INTO quality_gates (milestone_id, slice_id, gate_id, scope, task_id, status)
|
|
2169
708
|
VALUES (:mid, :sid, :gid, :scope, :tid, :status)`).run({
|
|
2170
709
|
":mid": g.milestoneId,
|
|
2171
710
|
":sid": g.sliceId,
|
|
@@ -2176,10 +715,10 @@ export function insertGateRow(g) {
|
|
|
2176
715
|
});
|
|
2177
716
|
}
|
|
2178
717
|
export function saveGateResult(g) {
|
|
2179
|
-
if (!
|
|
718
|
+
if (!getDbOrNull())
|
|
2180
719
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2181
720
|
const evaluatedAt = new Date().toISOString();
|
|
2182
|
-
const result =
|
|
721
|
+
const result = getDbOrNull().prepare(`UPDATE quality_gates
|
|
2183
722
|
SET status = 'complete', verdict = :verdict, rationale = :rationale,
|
|
2184
723
|
findings = :findings, evaluated_at = :evaluated_at
|
|
2185
724
|
WHERE milestone_id = :mid AND slice_id = :sid AND gate_id = :gid
|
|
@@ -2219,32 +758,10 @@ export function saveGateResult(g) {
|
|
|
2219
758
|
evaluatedAt,
|
|
2220
759
|
});
|
|
2221
760
|
}
|
|
2222
|
-
export function getPendingGates(milestoneId, sliceId, scope) {
|
|
2223
|
-
if (!currentDb)
|
|
2224
|
-
return [];
|
|
2225
|
-
const sql = scope
|
|
2226
|
-
? `SELECT * FROM quality_gates WHERE milestone_id = :mid AND slice_id = :sid AND scope = :scope AND status = 'pending'`
|
|
2227
|
-
: `SELECT * FROM quality_gates WHERE milestone_id = :mid AND slice_id = :sid AND status = 'pending'`;
|
|
2228
|
-
const params = { ":mid": milestoneId, ":sid": sliceId };
|
|
2229
|
-
if (scope)
|
|
2230
|
-
params[":scope"] = scope;
|
|
2231
|
-
return currentDb.prepare(sql).all(params).map(rowToGate);
|
|
2232
|
-
}
|
|
2233
|
-
export function getGateResults(milestoneId, sliceId, scope) {
|
|
2234
|
-
if (!currentDb)
|
|
2235
|
-
return [];
|
|
2236
|
-
const sql = scope
|
|
2237
|
-
? `SELECT * FROM quality_gates WHERE milestone_id = :mid AND slice_id = :sid AND scope = :scope`
|
|
2238
|
-
: `SELECT * FROM quality_gates WHERE milestone_id = :mid AND slice_id = :sid`;
|
|
2239
|
-
const params = { ":mid": milestoneId, ":sid": sliceId };
|
|
2240
|
-
if (scope)
|
|
2241
|
-
params[":scope"] = scope;
|
|
2242
|
-
return currentDb.prepare(sql).all(params).map(rowToGate);
|
|
2243
|
-
}
|
|
2244
761
|
export function markAllGatesOmitted(milestoneId, sliceId) {
|
|
2245
|
-
if (!
|
|
762
|
+
if (!getDbOrNull())
|
|
2246
763
|
return;
|
|
2247
|
-
|
|
764
|
+
getDbOrNull().prepare(`UPDATE quality_gates SET status = 'complete', verdict = 'omitted', evaluated_at = :now
|
|
2248
765
|
WHERE milestone_id = :mid AND slice_id = :sid AND status = 'pending'`).run({
|
|
2249
766
|
":mid": milestoneId,
|
|
2250
767
|
":sid": sliceId,
|
|
@@ -2252,7 +769,7 @@ export function markAllGatesOmitted(milestoneId, sliceId) {
|
|
|
2252
769
|
});
|
|
2253
770
|
}
|
|
2254
771
|
export function markPendingGatesOmittedForTurn(milestoneId, sliceId, turn) {
|
|
2255
|
-
if (!
|
|
772
|
+
if (!getDbOrNull())
|
|
2256
773
|
return;
|
|
2257
774
|
const gateIds = [...getGateIdsForTurn(turn)];
|
|
2258
775
|
if (gateIds.length === 0)
|
|
@@ -2266,61 +783,14 @@ export function markPendingGatesOmittedForTurn(milestoneId, sliceId, turn) {
|
|
|
2266
783
|
gateIds.forEach((id, index) => {
|
|
2267
784
|
params[`:gid${index}`] = id;
|
|
2268
785
|
});
|
|
2269
|
-
|
|
786
|
+
getDbOrNull().prepare(`UPDATE quality_gates SET status = 'complete', verdict = 'omitted', evaluated_at = :now
|
|
2270
787
|
WHERE milestone_id = :mid AND slice_id = :sid AND status = 'pending'
|
|
2271
788
|
AND gate_id IN (${placeholders})`).run(params);
|
|
2272
789
|
}
|
|
2273
|
-
export function getPendingSliceGateCount(milestoneId, sliceId) {
|
|
2274
|
-
if (!currentDb)
|
|
2275
|
-
return 0;
|
|
2276
|
-
const row = currentDb.prepare(`SELECT COUNT(*) as cnt FROM quality_gates
|
|
2277
|
-
WHERE milestone_id = :mid AND slice_id = :sid AND scope = 'slice' AND status = 'pending'`).get({ ":mid": milestoneId, ":sid": sliceId });
|
|
2278
|
-
return row ? row["cnt"] : 0;
|
|
2279
|
-
}
|
|
2280
|
-
/**
|
|
2281
|
-
* Return pending gate rows owned by a specific workflow turn.
|
|
2282
|
-
*
|
|
2283
|
-
* Unlike `getPendingGates(..., scope)`, this filters by the registry's
|
|
2284
|
-
* `ownerTurn` metadata so callers can distinguish Q3/Q4 (owned by
|
|
2285
|
-
* gate-evaluate) from Q8 (owned by complete-slice) even though both are
|
|
2286
|
-
* scope:"slice". Pass `taskId` to narrow task-scoped results to one task.
|
|
2287
|
-
*/
|
|
2288
|
-
export function getPendingGatesForTurn(milestoneId, sliceId, turn, taskId) {
|
|
2289
|
-
if (!currentDb)
|
|
2290
|
-
return [];
|
|
2291
|
-
const ids = getGateIdsForTurn(turn);
|
|
2292
|
-
if (ids.size === 0)
|
|
2293
|
-
return [];
|
|
2294
|
-
const idList = [...ids];
|
|
2295
|
-
const placeholders = idList.map((_, i) => `:gid${i}`).join(",");
|
|
2296
|
-
const params = {
|
|
2297
|
-
":mid": milestoneId,
|
|
2298
|
-
":sid": sliceId,
|
|
2299
|
-
};
|
|
2300
|
-
idList.forEach((id, i) => {
|
|
2301
|
-
params[`:gid${i}`] = id;
|
|
2302
|
-
});
|
|
2303
|
-
let sql = `SELECT * FROM quality_gates
|
|
2304
|
-
WHERE milestone_id = :mid AND slice_id = :sid
|
|
2305
|
-
AND status = 'pending'
|
|
2306
|
-
AND gate_id IN (${placeholders})`;
|
|
2307
|
-
if (taskId !== undefined) {
|
|
2308
|
-
sql += ` AND task_id = :tid`;
|
|
2309
|
-
params[":tid"] = taskId;
|
|
2310
|
-
}
|
|
2311
|
-
return currentDb.prepare(sql).all(params).map(rowToGate);
|
|
2312
|
-
}
|
|
2313
|
-
/**
|
|
2314
|
-
* Count pending gates for a turn. Convenience wrapper used by state
|
|
2315
|
-
* derivation to decide whether a phase transition should pause.
|
|
2316
|
-
*/
|
|
2317
|
-
export function getPendingGateCountForTurn(milestoneId, sliceId, turn) {
|
|
2318
|
-
return getPendingGatesForTurn(milestoneId, sliceId, turn).length;
|
|
2319
|
-
}
|
|
2320
790
|
export function insertGateRun(entry) {
|
|
2321
|
-
if (!
|
|
791
|
+
if (!getDbOrNull())
|
|
2322
792
|
return;
|
|
2323
|
-
|
|
793
|
+
getDbOrNull().prepare(`INSERT INTO gate_runs (
|
|
2324
794
|
trace_id, turn_id, gate_id, gate_type, unit_type, unit_id, milestone_id, slice_id, task_id,
|
|
2325
795
|
outcome, failure_class, rationale, findings, attempt, max_attempts, retryable, evaluated_at
|
|
2326
796
|
) VALUES (
|
|
@@ -2347,9 +817,9 @@ export function insertGateRun(entry) {
|
|
|
2347
817
|
});
|
|
2348
818
|
}
|
|
2349
819
|
export function upsertTurnGitTransaction(entry) {
|
|
2350
|
-
if (!
|
|
820
|
+
if (!getDbOrNull())
|
|
2351
821
|
return;
|
|
2352
|
-
|
|
822
|
+
getDbOrNull().prepare(`INSERT OR REPLACE INTO turn_git_transactions (
|
|
2353
823
|
trace_id, turn_id, unit_type, unit_id, stage, action, push, status, error, metadata_json, updated_at
|
|
2354
824
|
) VALUES (
|
|
2355
825
|
:trace_id, :turn_id, :unit_type, :unit_id, :stage, :action, :push, :status, :error, :metadata_json, :updated_at
|
|
@@ -2367,22 +837,11 @@ export function upsertTurnGitTransaction(entry) {
|
|
|
2367
837
|
":updated_at": entry.updatedAt,
|
|
2368
838
|
});
|
|
2369
839
|
}
|
|
2370
|
-
export function getMilestoneCommitAttributionShas(milestoneId) {
|
|
2371
|
-
if (!currentDb)
|
|
2372
|
-
return [];
|
|
2373
|
-
const rows = currentDb.prepare(`SELECT commit_sha
|
|
2374
|
-
FROM milestone_commit_attributions
|
|
2375
|
-
WHERE milestone_id = :mid
|
|
2376
|
-
ORDER BY created_at, commit_sha`).all({ ":mid": milestoneId });
|
|
2377
|
-
return rows
|
|
2378
|
-
.map((row) => typeof row["commit_sha"] === "string" ? row["commit_sha"] : "")
|
|
2379
|
-
.filter(Boolean);
|
|
2380
|
-
}
|
|
2381
840
|
export function recordMilestoneCommitAttribution(entry) {
|
|
2382
|
-
if (!
|
|
841
|
+
if (!getDbOrNull())
|
|
2383
842
|
return;
|
|
2384
843
|
transaction(() => {
|
|
2385
|
-
|
|
844
|
+
getDbOrNull().prepare(`INSERT OR REPLACE INTO milestone_commit_attributions (
|
|
2386
845
|
commit_sha, milestone_id, slice_id, task_id, source, confidence, files_json, created_at
|
|
2387
846
|
) VALUES (
|
|
2388
847
|
:commit_sha, :milestone_id, :slice_id, :task_id, :source, :confidence, :files_json, :created_at
|
|
@@ -2396,7 +855,7 @@ export function recordMilestoneCommitAttribution(entry) {
|
|
|
2396
855
|
":files_json": JSON.stringify(entry.files),
|
|
2397
856
|
":created_at": entry.createdAt,
|
|
2398
857
|
});
|
|
2399
|
-
|
|
858
|
+
getDbOrNull().prepare(`INSERT OR IGNORE INTO audit_events (
|
|
2400
859
|
event_id, trace_id, turn_id, caused_by, category, type, ts, payload_json
|
|
2401
860
|
) VALUES (
|
|
2402
861
|
:event_id, :trace_id, :turn_id, :caused_by, :category, :type, :ts, :payload_json
|
|
@@ -2421,10 +880,10 @@ export function recordMilestoneCommitAttribution(entry) {
|
|
|
2421
880
|
});
|
|
2422
881
|
}
|
|
2423
882
|
export function insertAuditEvent(entry) {
|
|
2424
|
-
if (!
|
|
883
|
+
if (!getDbOrNull())
|
|
2425
884
|
return;
|
|
2426
885
|
transaction(() => {
|
|
2427
|
-
|
|
886
|
+
getDbOrNull().prepare(`INSERT OR IGNORE INTO audit_events (
|
|
2428
887
|
event_id, trace_id, turn_id, caused_by, category, type, ts, payload_json
|
|
2429
888
|
) VALUES (
|
|
2430
889
|
:event_id, :trace_id, :turn_id, :caused_by, :category, :type, :ts, :payload_json
|
|
@@ -2439,14 +898,14 @@ export function insertAuditEvent(entry) {
|
|
|
2439
898
|
":payload_json": JSON.stringify(entry.payload ?? {}),
|
|
2440
899
|
});
|
|
2441
900
|
if (entry.turnId) {
|
|
2442
|
-
const row =
|
|
901
|
+
const row = getDbOrNull().prepare(`SELECT event_count, first_ts, last_ts
|
|
2443
902
|
FROM audit_turn_index
|
|
2444
903
|
WHERE trace_id = :trace_id AND turn_id = :turn_id`).get({
|
|
2445
904
|
":trace_id": entry.traceId,
|
|
2446
905
|
":turn_id": entry.turnId,
|
|
2447
906
|
});
|
|
2448
907
|
if (row) {
|
|
2449
|
-
|
|
908
|
+
getDbOrNull().prepare(`UPDATE audit_turn_index
|
|
2450
909
|
SET first_ts = CASE WHEN :ts < first_ts THEN :ts ELSE first_ts END,
|
|
2451
910
|
last_ts = CASE WHEN :ts > last_ts THEN :ts ELSE last_ts END,
|
|
2452
911
|
event_count = event_count + 1
|
|
@@ -2457,7 +916,7 @@ export function insertAuditEvent(entry) {
|
|
|
2457
916
|
});
|
|
2458
917
|
}
|
|
2459
918
|
else {
|
|
2460
|
-
|
|
919
|
+
getDbOrNull().prepare(`INSERT INTO audit_turn_index (trace_id, turn_id, first_ts, last_ts, event_count)
|
|
2461
920
|
VALUES (:trace_id, :turn_id, :first_ts, :last_ts, :event_count)`).run({
|
|
2462
921
|
":trace_id": entry.traceId,
|
|
2463
922
|
":turn_id": entry.turnId,
|
|
@@ -2477,40 +936,40 @@ export function insertAuditEvent(entry) {
|
|
|
2477
936
|
// add new behavior.
|
|
2478
937
|
/** Delete a decision row by id. Used by db-writer.ts rollback on disk-write failure. */
|
|
2479
938
|
export function deleteDecisionById(id) {
|
|
2480
|
-
if (!
|
|
939
|
+
if (!getDbOrNull())
|
|
2481
940
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2482
|
-
|
|
941
|
+
getDbOrNull().prepare("DELETE FROM decisions WHERE id = :id").run({ ":id": id });
|
|
2483
942
|
}
|
|
2484
943
|
/** Delete a requirement row by id. Used by db-writer.ts rollback on disk-write failure. */
|
|
2485
944
|
export function deleteRequirementById(id) {
|
|
2486
|
-
if (!
|
|
945
|
+
if (!getDbOrNull())
|
|
2487
946
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2488
|
-
|
|
947
|
+
getDbOrNull().prepare("DELETE FROM requirements WHERE id = :id").run({ ":id": id });
|
|
2489
948
|
}
|
|
2490
949
|
/** Delete an artifact row by path. Used by db-writer.ts rollback on disk-write failure. */
|
|
2491
950
|
export function deleteArtifactByPath(path) {
|
|
2492
|
-
if (!
|
|
951
|
+
if (!getDbOrNull())
|
|
2493
952
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2494
|
-
|
|
953
|
+
getDbOrNull().prepare("DELETE FROM artifacts WHERE path = :path").run({ ":path": path });
|
|
2495
954
|
}
|
|
2496
955
|
/**
|
|
2497
956
|
* Drop hierarchy rows in dependency order inside a transaction. Used by
|
|
2498
957
|
* `gsd recover --confirm` to rebuild engine state from markdown.
|
|
2499
958
|
*/
|
|
2500
959
|
export function clearEngineHierarchy() {
|
|
2501
|
-
if (!
|
|
960
|
+
if (!getDbOrNull())
|
|
2502
961
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2503
962
|
transaction(() => {
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
963
|
+
getDbOrNull().exec("DELETE FROM verification_evidence");
|
|
964
|
+
getDbOrNull().exec("DELETE FROM quality_gates");
|
|
965
|
+
getDbOrNull().exec("DELETE FROM slice_dependencies");
|
|
966
|
+
getDbOrNull().exec("DELETE FROM assessments");
|
|
967
|
+
getDbOrNull().exec("DELETE FROM replan_history");
|
|
968
|
+
getDbOrNull().exec("DELETE FROM milestone_commit_attributions");
|
|
969
|
+
getDbOrNull().exec("DELETE FROM tasks");
|
|
970
|
+
getDbOrNull().exec("DELETE FROM slices");
|
|
971
|
+
getDbOrNull().exec("DELETE FROM milestone_leases");
|
|
972
|
+
getDbOrNull().exec("DELETE FROM milestones");
|
|
2514
973
|
});
|
|
2515
974
|
}
|
|
2516
975
|
/**
|
|
@@ -2520,9 +979,9 @@ export function clearEngineHierarchy() {
|
|
|
2520
979
|
* slice back to 'pending'.
|
|
2521
980
|
*/
|
|
2522
981
|
export function insertOrIgnoreSlice(args) {
|
|
2523
|
-
if (!
|
|
982
|
+
if (!getDbOrNull())
|
|
2524
983
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2525
|
-
|
|
984
|
+
getDbOrNull().prepare(`INSERT OR IGNORE INTO slices (milestone_id, id, title, status, created_at)
|
|
2526
985
|
VALUES (:mid, :sid, :title, 'pending', :ts)`).run({
|
|
2527
986
|
":mid": args.milestoneId,
|
|
2528
987
|
":sid": args.sliceId,
|
|
@@ -2535,9 +994,9 @@ export function insertOrIgnoreSlice(args) {
|
|
|
2535
994
|
* Same rationale as `insertOrIgnoreSlice`.
|
|
2536
995
|
*/
|
|
2537
996
|
export function insertOrIgnoreTask(args) {
|
|
2538
|
-
if (!
|
|
997
|
+
if (!getDbOrNull())
|
|
2539
998
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2540
|
-
|
|
999
|
+
getDbOrNull().prepare(`INSERT OR IGNORE INTO tasks (milestone_id, slice_id, id, title, status, created_at)
|
|
2541
1000
|
VALUES (:mid, :sid, :tid, :title, 'pending', :ts)`).run({
|
|
2542
1001
|
":mid": args.milestoneId,
|
|
2543
1002
|
":sid": args.sliceId,
|
|
@@ -2552,18 +1011,18 @@ export function insertOrIgnoreTask(args) {
|
|
|
2552
1011
|
* trigger via DB in addition to the on-disk REPLAN-TRIGGER.md marker.
|
|
2553
1012
|
*/
|
|
2554
1013
|
export function setSliceReplanTriggeredAt(milestoneId, sliceId, ts) {
|
|
2555
|
-
if (!
|
|
1014
|
+
if (!getDbOrNull())
|
|
2556
1015
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2557
|
-
|
|
1016
|
+
getDbOrNull().prepare("UPDATE slices SET replan_triggered_at = :ts WHERE milestone_id = :mid AND id = :sid").run({ ":ts": ts, ":mid": milestoneId, ":sid": sliceId });
|
|
2558
1017
|
}
|
|
2559
1018
|
/**
|
|
2560
1019
|
* INSERT OR REPLACE a quality_gates row. Used by milestone-validation-gates.ts
|
|
2561
1020
|
* to persist milestone-level (MV*) gate outcomes after validate-milestone runs.
|
|
2562
1021
|
*/
|
|
2563
1022
|
export function upsertQualityGate(g) {
|
|
2564
|
-
if (!
|
|
1023
|
+
if (!getDbOrNull())
|
|
2565
1024
|
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2566
|
-
|
|
1025
|
+
getDbOrNull().prepare(`INSERT OR REPLACE INTO quality_gates
|
|
2567
1026
|
(milestone_id, slice_id, gate_id, scope, task_id, status, verdict, rationale, findings, evaluated_at)
|
|
2568
1027
|
VALUES (:mid, :sid, :gid, :scope, :tid, :status, :verdict, :rationale, :findings, :evaluated_at)`).run({
|
|
2569
1028
|
":mid": g.milestoneId,
|
|
@@ -2578,327 +1037,3 @@ export function upsertQualityGate(g) {
|
|
|
2578
1037
|
":evaluated_at": g.evaluatedAt,
|
|
2579
1038
|
});
|
|
2580
1039
|
}
|
|
2581
|
-
/**
|
|
2582
|
-
* Atomically replace all workflow state from a manifest. Lifted verbatim from
|
|
2583
|
-
* workflow-manifest.ts so the single-writer invariant holds. Restores
|
|
2584
|
-
* correctness-bearing workflow tables; runtime soft state and append-only audit
|
|
2585
|
-
* streams stay outside this recovery path.
|
|
2586
|
-
*/
|
|
2587
|
-
export function restoreManifest(manifest) {
|
|
2588
|
-
if (!currentDb)
|
|
2589
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2590
|
-
const db = currentDb;
|
|
2591
|
-
transaction(() => {
|
|
2592
|
-
const restoredMilestoneIds = new Set(manifest.milestones.map((m) => m.id));
|
|
2593
|
-
const restoredSliceKeys = new Set(manifest.slices.map((s) => JSON.stringify([s.milestone_id, s.id])));
|
|
2594
|
-
const preservedReplanHistory = manifest.replan_history === undefined
|
|
2595
|
-
? db.prepare("SELECT * FROM replan_history ORDER BY id").all()
|
|
2596
|
-
: [];
|
|
2597
|
-
const preservedAssessments = manifest.assessments === undefined
|
|
2598
|
-
? db.prepare("SELECT * FROM assessments ORDER BY path").all()
|
|
2599
|
-
: [];
|
|
2600
|
-
const preservedQualityGates = manifest.quality_gates === undefined
|
|
2601
|
-
? db.prepare("SELECT * FROM quality_gates ORDER BY milestone_id, slice_id, gate_id, task_id").all()
|
|
2602
|
-
: [];
|
|
2603
|
-
const preservedCommitAttributions = manifest.milestone_commit_attributions === undefined
|
|
2604
|
-
? db.prepare("SELECT * FROM milestone_commit_attributions ORDER BY milestone_id, commit_sha").all()
|
|
2605
|
-
: [];
|
|
2606
|
-
// Clear workflow tables in dependency order.
|
|
2607
|
-
db.exec("DELETE FROM verification_evidence");
|
|
2608
|
-
db.exec("DELETE FROM quality_gates");
|
|
2609
|
-
db.exec("DELETE FROM slice_dependencies");
|
|
2610
|
-
db.exec("DELETE FROM assessments");
|
|
2611
|
-
db.exec("DELETE FROM replan_history");
|
|
2612
|
-
db.exec("DELETE FROM milestone_commit_attributions");
|
|
2613
|
-
db.exec("DELETE FROM tasks");
|
|
2614
|
-
db.exec("DELETE FROM slices");
|
|
2615
|
-
db.exec("DELETE FROM milestone_leases");
|
|
2616
|
-
db.exec("DELETE FROM milestones");
|
|
2617
|
-
db.exec("DELETE FROM decisions WHERE 1=1");
|
|
2618
|
-
db.exec(`DELETE FROM memories WHERE category = 'architecture' AND structured_fields LIKE '%"sourceDecisionId":"%'`);
|
|
2619
|
-
if (manifest.artifacts !== undefined)
|
|
2620
|
-
db.exec("DELETE FROM artifacts");
|
|
2621
|
-
if (manifest.requirements !== undefined)
|
|
2622
|
-
db.exec("DELETE FROM requirements");
|
|
2623
|
-
if (manifest.requirements !== undefined) {
|
|
2624
|
-
const reqStmt = db.prepare(`INSERT INTO requirements (
|
|
2625
|
-
id, class, status, description, why, source, primary_owner,
|
|
2626
|
-
supporting_slices, validation, notes, full_content, superseded_by
|
|
2627
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
2628
|
-
for (const r of manifest.requirements) {
|
|
2629
|
-
reqStmt.run(r.id, r.class, r.status, r.description, r.why, r.source, r.primary_owner, r.supporting_slices, r.validation, r.notes, r.full_content, r.superseded_by);
|
|
2630
|
-
}
|
|
2631
|
-
}
|
|
2632
|
-
if (manifest.artifacts !== undefined) {
|
|
2633
|
-
const artStmt = db.prepare(`INSERT INTO artifacts (
|
|
2634
|
-
path, artifact_type, milestone_id, slice_id, task_id, full_content, imported_at, content_hash
|
|
2635
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
2636
|
-
for (const a of manifest.artifacts) {
|
|
2637
|
-
const fullContent = a.full_content ?? "";
|
|
2638
|
-
artStmt.run(a.path, a.artifact_type, a.milestone_id, a.slice_id, a.task_id, fullContent, a.imported_at, a.content_hash ?? createHash("sha256").update(fullContent).digest("hex"));
|
|
2639
|
-
}
|
|
2640
|
-
}
|
|
2641
|
-
// Restore milestones
|
|
2642
|
-
const msStmt = db.prepare(`INSERT INTO milestones (id, title, status, depends_on, created_at, completed_at,
|
|
2643
|
-
vision, success_criteria, key_risks, proof_strategy,
|
|
2644
|
-
verification_contract, verification_integration, verification_operational, verification_uat,
|
|
2645
|
-
definition_of_done, requirement_coverage, boundary_map_markdown, sequence)
|
|
2646
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
2647
|
-
for (const m of manifest.milestones) {
|
|
2648
|
-
msStmt.run(m.id, m.title, m.status, JSON.stringify(m.depends_on), m.created_at, m.completed_at, m.vision, JSON.stringify(m.success_criteria), JSON.stringify(m.key_risks), JSON.stringify(m.proof_strategy), m.verification_contract, m.verification_integration, m.verification_operational, m.verification_uat, JSON.stringify(m.definition_of_done), m.requirement_coverage, m.boundary_map_markdown, m.sequence ?? 0);
|
|
2649
|
-
}
|
|
2650
|
-
// Restore slices (ADR-011 Phase 1: includes is_sketch + sketch_scope)
|
|
2651
|
-
const slStmt = db.prepare(`INSERT INTO slices (milestone_id, id, title, status, risk, depends, demo,
|
|
2652
|
-
created_at, completed_at, full_summary_md, full_uat_md,
|
|
2653
|
-
goal, success_criteria, proof_level, integration_closure, observability_impact,
|
|
2654
|
-
target_repositories, sequence, replan_triggered_at, is_sketch, sketch_scope)
|
|
2655
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
2656
|
-
for (const s of manifest.slices) {
|
|
2657
|
-
slStmt.run(s.milestone_id, s.id, s.title, s.status, s.risk, JSON.stringify(s.depends), s.demo, s.created_at, s.completed_at, s.full_summary_md, s.full_uat_md, s.goal, s.success_criteria, s.proof_level, s.integration_closure, s.observability_impact, JSON.stringify(s.target_repositories ?? []), s.sequence, s.replan_triggered_at, s.is_sketch ?? 0, s.sketch_scope ?? "");
|
|
2658
|
-
}
|
|
2659
|
-
const depStmt = db.prepare("INSERT OR IGNORE INTO slice_dependencies (milestone_id, slice_id, depends_on_slice_id) VALUES (?, ?, ?)");
|
|
2660
|
-
for (const s of manifest.slices) {
|
|
2661
|
-
for (const dep of s.depends ?? []) {
|
|
2662
|
-
depStmt.run(s.milestone_id, s.id, dep);
|
|
2663
|
-
}
|
|
2664
|
-
}
|
|
2665
|
-
// Restore tasks (ADR-011 P2: includes blocker_source + escalation_* columns)
|
|
2666
|
-
const tkStmt = db.prepare(`INSERT INTO tasks (milestone_id, slice_id, id, title, status,
|
|
2667
|
-
one_liner, narrative, verification_result, duration, completed_at,
|
|
2668
|
-
blocker_discovered, deviations, known_issues, key_files, key_decisions,
|
|
2669
|
-
full_summary_md, description, estimate, files, verify,
|
|
2670
|
-
inputs, expected_output, observability_impact, full_plan_md, target_repositories, sequence,
|
|
2671
|
-
blocker_source, escalation_pending, escalation_awaiting_review,
|
|
2672
|
-
escalation_artifact_path, escalation_override_applied_at)
|
|
2673
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
2674
|
-
for (const t of manifest.tasks) {
|
|
2675
|
-
tkStmt.run(t.milestone_id, t.slice_id, t.id, t.title, t.status, t.one_liner, t.narrative, t.verification_result, t.duration, t.completed_at, t.blocker_discovered ? 1 : 0, t.deviations, t.known_issues, JSON.stringify(t.key_files), JSON.stringify(t.key_decisions), t.full_summary_md, t.description, t.estimate, JSON.stringify(t.files), t.verify, JSON.stringify(t.inputs), JSON.stringify(t.expected_output), t.observability_impact, t.full_plan_md ?? "", JSON.stringify(t.target_repositories ?? []), t.sequence, t.blocker_source ?? "", t.escalation_pending ?? 0, t.escalation_awaiting_review ?? 0, t.escalation_artifact_path ?? null, t.escalation_override_applied_at ?? null);
|
|
2676
|
-
}
|
|
2677
|
-
// Restore decisions (ADR-011 P2: include source so escalation decisions survive)
|
|
2678
|
-
const dcStmt = db.prepare(`INSERT INTO decisions (seq, id, when_context, scope, decision, choice, rationale, revisable, made_by, source, superseded_by)
|
|
2679
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
2680
|
-
for (const d of manifest.decisions) {
|
|
2681
|
-
dcStmt.run(d.seq, d.id, d.when_context, d.scope, d.decision, d.choice, d.rationale, d.revisable, d.made_by, d.source ?? "discussion", d.superseded_by);
|
|
2682
|
-
}
|
|
2683
|
-
const replanHistoryRows = manifest.replan_history ?? preservedReplanHistory.filter((r) => restoredMilestoneIds.has(r.milestone_id));
|
|
2684
|
-
if (replanHistoryRows.length > 0) {
|
|
2685
|
-
const replStmt = db.prepare(`INSERT INTO replan_history (
|
|
2686
|
-
id, milestone_id, slice_id, task_id, summary, previous_artifact_path, replacement_artifact_path, created_at
|
|
2687
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
2688
|
-
for (const r of replanHistoryRows) {
|
|
2689
|
-
replStmt.run(r.id, r.milestone_id, r.slice_id, r.task_id, r.summary, r.previous_artifact_path, r.replacement_artifact_path, r.created_at);
|
|
2690
|
-
}
|
|
2691
|
-
}
|
|
2692
|
-
const assessmentRows = manifest.assessments ?? preservedAssessments.filter((a) => restoredMilestoneIds.has(a.milestone_id));
|
|
2693
|
-
if (assessmentRows.length > 0) {
|
|
2694
|
-
const assessStmt = db.prepare(`INSERT INTO assessments (
|
|
2695
|
-
path, milestone_id, slice_id, task_id, status, scope, full_content, created_at
|
|
2696
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
2697
|
-
for (const a of assessmentRows) {
|
|
2698
|
-
assessStmt.run(a.path, a.milestone_id, a.slice_id, a.task_id, a.status, a.scope, a.full_content, a.created_at);
|
|
2699
|
-
}
|
|
2700
|
-
}
|
|
2701
|
-
const qualityGateRows = manifest.quality_gates ?? preservedQualityGates.filter((g) => (restoredSliceKeys.has(JSON.stringify([g.milestone_id, g.slice_id]))));
|
|
2702
|
-
if (qualityGateRows.length > 0) {
|
|
2703
|
-
const gateStmt = db.prepare(`INSERT INTO quality_gates (
|
|
2704
|
-
milestone_id, slice_id, gate_id, scope, task_id, status, verdict, rationale, findings, evaluated_at
|
|
2705
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
2706
|
-
for (const g of qualityGateRows) {
|
|
2707
|
-
gateStmt.run(g.milestone_id, g.slice_id, g.gate_id, g.scope, g.task_id, g.status, g.verdict ?? "", g.rationale, g.findings, g.evaluated_at);
|
|
2708
|
-
}
|
|
2709
|
-
}
|
|
2710
|
-
const commitAttributionRows = manifest.milestone_commit_attributions ??
|
|
2711
|
-
preservedCommitAttributions.filter((a) => restoredMilestoneIds.has(a.milestone_id));
|
|
2712
|
-
if (commitAttributionRows.length > 0) {
|
|
2713
|
-
const attrStmt = db.prepare(`INSERT OR REPLACE INTO milestone_commit_attributions (
|
|
2714
|
-
commit_sha, milestone_id, slice_id, task_id, source, confidence, files_json, created_at
|
|
2715
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
2716
|
-
for (const a of commitAttributionRows) {
|
|
2717
|
-
attrStmt.run(a.commit_sha, a.milestone_id, a.slice_id, a.task_id, a.source, a.confidence, a.files_json, a.created_at);
|
|
2718
|
-
}
|
|
2719
|
-
}
|
|
2720
|
-
// Restore verification evidence
|
|
2721
|
-
const evStmt = db.prepare(`INSERT INTO verification_evidence (task_id, slice_id, milestone_id, command, exit_code, verdict, duration_ms, created_at)
|
|
2722
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
2723
|
-
for (const e of manifest.verification_evidence) {
|
|
2724
|
-
evStmt.run(e.task_id, e.slice_id, e.milestone_id, e.command, e.exit_code, e.verdict, e.duration_ms, e.created_at);
|
|
2725
|
-
}
|
|
2726
|
-
});
|
|
2727
|
-
}
|
|
2728
|
-
/**
|
|
2729
|
-
* Bulk delete + insert a legacy milestone hierarchy for markdown → DB migration.
|
|
2730
|
-
* Used by workflow-migration.ts to populate engine tables from parsed ROADMAP/PLAN
|
|
2731
|
-
* files. All operations run inside a single transaction.
|
|
2732
|
-
*/
|
|
2733
|
-
export function bulkInsertLegacyHierarchy(payload) {
|
|
2734
|
-
if (!currentDb)
|
|
2735
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2736
|
-
const db = currentDb;
|
|
2737
|
-
const { milestones, slices, tasks, clearMilestoneIds, createdAt } = payload;
|
|
2738
|
-
if (clearMilestoneIds.length === 0)
|
|
2739
|
-
return;
|
|
2740
|
-
const placeholders = clearMilestoneIds.map(() => "?").join(",");
|
|
2741
|
-
transaction(() => {
|
|
2742
|
-
db.prepare(`DELETE FROM tasks WHERE milestone_id IN (${placeholders})`).run(...clearMilestoneIds);
|
|
2743
|
-
db.prepare(`DELETE FROM slices WHERE milestone_id IN (${placeholders})`).run(...clearMilestoneIds);
|
|
2744
|
-
db.prepare(`DELETE FROM milestone_leases WHERE milestone_id IN (${placeholders})`).run(...clearMilestoneIds);
|
|
2745
|
-
db.prepare(`DELETE FROM milestones WHERE id IN (${placeholders})`).run(...clearMilestoneIds);
|
|
2746
|
-
const insertMilestone = db.prepare("INSERT INTO milestones (id, title, status, created_at) VALUES (?, ?, ?, ?)");
|
|
2747
|
-
for (const m of milestones) {
|
|
2748
|
-
insertMilestone.run(m.id, m.title, m.status, createdAt);
|
|
2749
|
-
}
|
|
2750
|
-
const insertSliceStmt = db.prepare("INSERT INTO slices (id, milestone_id, title, status, risk, depends, sequence, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
|
2751
|
-
for (const s of slices) {
|
|
2752
|
-
insertSliceStmt.run(s.id, s.milestoneId, s.title, s.status, s.risk, "[]", s.sequence, createdAt);
|
|
2753
|
-
}
|
|
2754
|
-
const insertTaskStmt = db.prepare("INSERT INTO tasks (id, slice_id, milestone_id, title, description, status, estimate, files, sequence) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
|
2755
|
-
for (const t of tasks) {
|
|
2756
|
-
insertTaskStmt.run(t.id, t.sliceId, t.milestoneId, t.title, "", t.status, "", "[]", t.sequence);
|
|
2757
|
-
}
|
|
2758
|
-
});
|
|
2759
|
-
}
|
|
2760
|
-
// ─── Memory store writers ────────────────────────────────────────────────
|
|
2761
|
-
// All memory writes go through gsd-db.ts so the single-writer invariant
|
|
2762
|
-
// holds. These are direct pass-throughs to the SQL previously in
|
|
2763
|
-
// memory-store.ts — same bindings, same behavior.
|
|
2764
|
-
export function insertMemoryRow(args) {
|
|
2765
|
-
if (!currentDb)
|
|
2766
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2767
|
-
currentDb.prepare(`INSERT INTO memories (id, category, content, confidence, source_unit_type, source_unit_id, created_at, updated_at, scope, tags, structured_fields)
|
|
2768
|
-
VALUES (:id, :category, :content, :confidence, :source_unit_type, :source_unit_id, :created_at, :updated_at, :scope, :tags, :structured_fields)`).run({
|
|
2769
|
-
":id": args.id,
|
|
2770
|
-
":category": args.category,
|
|
2771
|
-
":content": args.content,
|
|
2772
|
-
":confidence": args.confidence,
|
|
2773
|
-
":source_unit_type": args.sourceUnitType,
|
|
2774
|
-
":source_unit_id": args.sourceUnitId,
|
|
2775
|
-
":created_at": args.createdAt,
|
|
2776
|
-
":updated_at": args.updatedAt,
|
|
2777
|
-
":scope": args.scope ?? "project",
|
|
2778
|
-
":tags": JSON.stringify(args.tags ?? []),
|
|
2779
|
-
":structured_fields": args.structuredFields == null ? null : JSON.stringify(args.structuredFields),
|
|
2780
|
-
});
|
|
2781
|
-
}
|
|
2782
|
-
export function insertMemorySourceRow(args) {
|
|
2783
|
-
if (!currentDb)
|
|
2784
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2785
|
-
currentDb.prepare(`INSERT OR IGNORE INTO memory_sources (id, kind, uri, title, content, content_hash, imported_at, scope, tags)
|
|
2786
|
-
VALUES (:id, :kind, :uri, :title, :content, :content_hash, :imported_at, :scope, :tags)`).run({
|
|
2787
|
-
":id": args.id,
|
|
2788
|
-
":kind": args.kind,
|
|
2789
|
-
":uri": args.uri,
|
|
2790
|
-
":title": args.title,
|
|
2791
|
-
":content": args.content,
|
|
2792
|
-
":content_hash": args.contentHash,
|
|
2793
|
-
":imported_at": args.importedAt,
|
|
2794
|
-
":scope": args.scope ?? "project",
|
|
2795
|
-
":tags": JSON.stringify(args.tags ?? []),
|
|
2796
|
-
});
|
|
2797
|
-
}
|
|
2798
|
-
export function deleteMemorySourceRow(id) {
|
|
2799
|
-
if (!currentDb)
|
|
2800
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2801
|
-
const res = currentDb
|
|
2802
|
-
.prepare("DELETE FROM memory_sources WHERE id = :id")
|
|
2803
|
-
.run({ ":id": id });
|
|
2804
|
-
return (res?.changes ?? 0) > 0;
|
|
2805
|
-
}
|
|
2806
|
-
export function upsertMemoryEmbedding(args) {
|
|
2807
|
-
if (!currentDb)
|
|
2808
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2809
|
-
currentDb.prepare(`INSERT INTO memory_embeddings (memory_id, model, dim, vector, updated_at)
|
|
2810
|
-
VALUES (:memory_id, :model, :dim, :vector, :updated_at)
|
|
2811
|
-
ON CONFLICT(memory_id) DO UPDATE SET
|
|
2812
|
-
model = excluded.model,
|
|
2813
|
-
dim = excluded.dim,
|
|
2814
|
-
vector = excluded.vector,
|
|
2815
|
-
updated_at = excluded.updated_at`).run({
|
|
2816
|
-
":memory_id": args.memoryId,
|
|
2817
|
-
":model": args.model,
|
|
2818
|
-
":dim": args.dim,
|
|
2819
|
-
":vector": args.vector,
|
|
2820
|
-
":updated_at": args.updatedAt,
|
|
2821
|
-
});
|
|
2822
|
-
}
|
|
2823
|
-
export function deleteMemoryEmbedding(memoryId) {
|
|
2824
|
-
if (!currentDb)
|
|
2825
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2826
|
-
const res = currentDb
|
|
2827
|
-
.prepare("DELETE FROM memory_embeddings WHERE memory_id = :id")
|
|
2828
|
-
.run({ ":id": memoryId });
|
|
2829
|
-
return (res?.changes ?? 0) > 0;
|
|
2830
|
-
}
|
|
2831
|
-
export function insertMemoryRelationRow(args) {
|
|
2832
|
-
if (!currentDb)
|
|
2833
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2834
|
-
currentDb.prepare(`INSERT OR REPLACE INTO memory_relations (from_id, to_id, rel, confidence, created_at)
|
|
2835
|
-
VALUES (:from_id, :to_id, :rel, :confidence, :created_at)`).run({
|
|
2836
|
-
":from_id": args.fromId,
|
|
2837
|
-
":to_id": args.toId,
|
|
2838
|
-
":rel": args.rel,
|
|
2839
|
-
":confidence": args.confidence,
|
|
2840
|
-
":created_at": args.createdAt,
|
|
2841
|
-
});
|
|
2842
|
-
}
|
|
2843
|
-
export function deleteMemoryRelationsFor(memoryId) {
|
|
2844
|
-
if (!currentDb)
|
|
2845
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2846
|
-
currentDb
|
|
2847
|
-
.prepare("DELETE FROM memory_relations WHERE from_id = :id OR to_id = :id")
|
|
2848
|
-
.run({ ":id": memoryId });
|
|
2849
|
-
}
|
|
2850
|
-
export function rewriteMemoryId(placeholderId, realId) {
|
|
2851
|
-
if (!currentDb)
|
|
2852
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2853
|
-
currentDb.prepare("UPDATE memories SET id = :real_id WHERE id = :placeholder").run({
|
|
2854
|
-
":real_id": realId,
|
|
2855
|
-
":placeholder": placeholderId,
|
|
2856
|
-
});
|
|
2857
|
-
}
|
|
2858
|
-
export function updateMemoryContentRow(id, content, confidence, updatedAt) {
|
|
2859
|
-
if (!currentDb)
|
|
2860
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2861
|
-
if (confidence != null) {
|
|
2862
|
-
currentDb.prepare("UPDATE memories SET content = :content, confidence = :confidence, updated_at = :updated_at WHERE id = :id").run({ ":content": content, ":confidence": confidence, ":updated_at": updatedAt, ":id": id });
|
|
2863
|
-
}
|
|
2864
|
-
else {
|
|
2865
|
-
currentDb.prepare("UPDATE memories SET content = :content, updated_at = :updated_at WHERE id = :id").run({ ":content": content, ":updated_at": updatedAt, ":id": id });
|
|
2866
|
-
}
|
|
2867
|
-
}
|
|
2868
|
-
export function incrementMemoryHitCount(id, updatedAt) {
|
|
2869
|
-
if (!currentDb)
|
|
2870
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2871
|
-
currentDb.prepare("UPDATE memories SET hit_count = hit_count + 1, updated_at = :updated_at, last_hit_at = :last_hit_at WHERE id = :id").run({ ":updated_at": updatedAt, ":last_hit_at": updatedAt, ":id": id });
|
|
2872
|
-
}
|
|
2873
|
-
export function supersedeMemoryRow(oldId, newId, updatedAt) {
|
|
2874
|
-
if (!currentDb)
|
|
2875
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2876
|
-
currentDb.prepare("UPDATE memories SET superseded_by = :new_id, updated_at = :updated_at WHERE id = :old_id").run({ ":new_id": newId, ":updated_at": updatedAt, ":old_id": oldId });
|
|
2877
|
-
}
|
|
2878
|
-
export function markMemoryUnitProcessed(unitKey, activityFile, processedAt) {
|
|
2879
|
-
if (!currentDb)
|
|
2880
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2881
|
-
currentDb.prepare(`INSERT OR IGNORE INTO memory_processed_units (unit_key, activity_file, processed_at)
|
|
2882
|
-
VALUES (:key, :file, :at)`).run({ ":key": unitKey, ":file": activityFile, ":at": processedAt });
|
|
2883
|
-
}
|
|
2884
|
-
export function decayMemoriesBefore(cutoffTs, now) {
|
|
2885
|
-
if (!currentDb)
|
|
2886
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2887
|
-
currentDb.prepare(`UPDATE memories
|
|
2888
|
-
SET confidence = MAX(0.1, confidence - 0.1), updated_at = :now
|
|
2889
|
-
WHERE superseded_by IS NULL
|
|
2890
|
-
AND updated_at < :cutoff
|
|
2891
|
-
AND confidence > 0.1
|
|
2892
|
-
AND (structured_fields IS NULL OR structured_fields NOT LIKE '%"sourceDecisionId"%')`).run({ ":now": now, ":cutoff": cutoffTs });
|
|
2893
|
-
}
|
|
2894
|
-
export function supersedeLowestRankedMemories(limit, now) {
|
|
2895
|
-
if (!currentDb)
|
|
2896
|
-
throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
|
|
2897
|
-
currentDb.prepare(`UPDATE memories SET superseded_by = 'CAP_EXCEEDED', updated_at = :now
|
|
2898
|
-
WHERE id IN (
|
|
2899
|
-
SELECT id FROM memories
|
|
2900
|
-
WHERE superseded_by IS NULL
|
|
2901
|
-
ORDER BY (confidence * (1.0 + hit_count * 0.1)) ASC
|
|
2902
|
-
LIMIT :limit
|
|
2903
|
-
)`).run({ ":now": now, ":limit": limit });
|
|
2904
|
-
}
|