@bonesofspring/ai-rules 0.2.51 → 0.2.53
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/CHANGELOG.md +92 -0
- package/README.md +27 -3
- package/bin/cli.js +565 -61
- package/bin/lib/ai-local-drift.js +55 -14
- package/bin/lib/engine-scripts.js +10 -0
- package/bin/lib/mcp/clients.js +168 -0
- package/bin/lib/mcp/credentials.js +298 -0
- package/bin/lib/mcp/doctor.js +394 -0
- package/bin/lib/mcp/prompt.js +115 -0
- package/bin/lib/mcp/registry.js +170 -0
- package/bin/lib/mcp/run.js +123 -0
- package/bin/lib/merged-config.js +45 -0
- package/bin/lib/seed-ai-local-team.js +23 -7
- package/bin/lib/tool-installs.js +27 -0
- package/bin/mavis-install.js +2 -0
- package/bin/opencode-install.js +2 -0
- package/docs/LOCAL-OVERLAY.md +1 -1
- package/docs/MCP-SETUP.md +62 -0
- package/docs/PRESET-CONTRIBUTION.md +14 -10
- package/docs/TEAM-DB.md +173 -0
- package/docs/openapi/team-db.v1.json +5085 -0
- package/mcp/gitlab.mjs +123 -0
- package/mcp/lib/envfile.mjs +126 -0
- package/mcp/lib/yandex.mjs +384 -0
- package/mcp/yandex-mcp-proxy.mjs +155 -0
- package/mcp/yandex-token-refresh.mjs +65 -0
- package/package.json +18 -1
- package/presets/_shared/ai-local/scripts/manifest.mjs +112 -3
- package/presets/_shared/ai-local/scripts/rule-improver.mjs +428 -91
- package/presets/_shared/ai-local/scripts/run.mjs +22 -4
- package/presets/_shared/ai-local/scripts/signals.mjs +4 -1
- package/presets/_shared/ai-local/scripts/team-db-api.mjs +1275 -0
- package/presets/_shared/ai-local/scripts/team-db-dashboard.mjs +1067 -0
- package/presets/_shared/ai-local/scripts/team-db-openapi.mjs +1301 -0
- package/presets/_shared/ai-local/scripts/team-db-project.mjs +1506 -0
- package/presets/_shared/ai-local/scripts/team-db-schema.mjs +831 -0
- package/presets/_shared/ai-local/scripts/team-db.mjs +251 -0
- package/presets/_shared/core/meta/preset-pr-checklist.md +1 -1
- package/presets/_shared/core/quality/interactive-questions.md +8 -8
- package/presets/_shared/hooks/chain-team-phases.sh.tmpl +109 -0
- package/presets/claude/android-kotlin/agents/code-reviewer.md +1 -0
- package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/android-kotlin/rules/tooling-and-review/interactive-questions.md +8 -8
- package/presets/claude/android-kotlin/rules/tooling-and-review/language-policy.md +1 -1
- package/presets/claude/android-kotlin/rules/tooling-and-review/preset-pr-checklist.md +1 -1
- package/presets/claude/android-kotlin/team/README.md +10 -0
- package/presets/claude/go/agents/code-reviewer.md +1 -0
- package/presets/claude/go/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/go/rules/tooling-and-review/interactive-questions.md +8 -8
- package/presets/claude/go/rules/tooling-and-review/language-policy.md +1 -1
- package/presets/claude/go/rules/tooling-and-review/preset-pr-checklist.md +1 -1
- package/presets/claude/go/team/README.md +10 -0
- package/presets/claude/ios-swift/agents/code-reviewer.md +1 -0
- package/presets/claude/ios-swift/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/ios-swift/rules/tooling-and-review/interactive-questions.md +8 -8
- package/presets/claude/ios-swift/rules/tooling-and-review/language-policy.md +1 -1
- package/presets/claude/ios-swift/rules/tooling-and-review/post-change-build.md +1 -1
- package/presets/claude/ios-swift/rules/tooling-and-review/preset-pr-checklist.md +1 -1
- package/presets/claude/ios-swift/team/README.md +10 -0
- package/presets/claude/java/agents/code-reviewer.md +1 -0
- package/presets/claude/java/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/java/rules/tooling-and-review/interactive-questions.md +8 -8
- package/presets/claude/java/rules/tooling-and-review/language-policy.md +1 -1
- package/presets/claude/java/rules/tooling-and-review/preset-pr-checklist.md +1 -1
- package/presets/claude/java/team/README.md +10 -0
- package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/mcp-ts/rules/tooling-and-review/interactive-questions.md +8 -8
- package/presets/claude/mcp-ts/rules/tooling-and-review/language-policy.md +1 -1
- package/presets/claude/mcp-ts/rules/tooling-and-review/preset-pr-checklist.md +1 -1
- package/presets/claude/mcp-ts/team/README.md +10 -0
- package/presets/claude/next/MCP.md +7 -0
- package/presets/claude/next/README.md +1 -1
- package/presets/claude/next/REPO_AGENTS.md +1 -1
- package/presets/claude/next/agents/README.md +3 -3
- package/presets/claude/next/agents/build-verifier.md +9 -3
- package/presets/claude/next/agents/code-reviewer.md +2 -1
- package/presets/claude/next/agents/codebase-analyzer.md +1 -1
- package/presets/claude/next/agents/feature-developer.md +18 -8
- package/presets/claude/next/agents/openspec-author.md +1 -1
- package/presets/claude/next/agents/playwright-test-generator.md +22 -68
- package/presets/claude/next/agents/playwright-test-healer.md +27 -54
- package/presets/claude/next/agents/playwright-test-planner.md +18 -51
- package/presets/claude/next/agents/task-analyst.md +25 -9
- package/presets/claude/next/agents/task-router.md +1 -1
- package/presets/claude/next/agents/tech-writer.md +1 -1
- package/presets/claude/next/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/next/mcp.json +3 -3
- package/presets/claude/next/rules/anti-hallucination/react-next-anti-hallucination.md +1 -1
- package/presets/claude/next/rules/architecture/layer-barrel-exports.md +1 -1
- package/presets/claude/next/rules/architecture/reference-features.md +1 -1
- package/presets/claude/next/rules/stack/navigation-router-ui.md +1 -1
- package/presets/claude/next/rules/stack/navigation-router.md +14 -0
- package/presets/claude/next/rules/stack/next-app-core.md +19 -17
- package/presets/claude/next/rules/stack/next-app-router.md +5 -17
- package/presets/claude/next/rules/stack/next-static-export.md +1 -1
- package/presets/claude/next/rules/tooling-and-review/interactive-questions.md +8 -8
- package/presets/claude/next/rules/tooling-and-review/language-policy.md +1 -1
- package/presets/claude/next/rules/tooling-and-review/post-change-lint.md +1 -1
- package/presets/claude/next/rules/tooling-and-review/preset-pr-checklist.md +1 -1
- package/presets/claude/next/rules/tooling-and-review/preset-token-budget.md +1 -1
- package/presets/claude/next/rules/tooling-and-review/security-next.md +28 -3
- package/presets/claude/next/rules/ui-and-accessibility/css-property-order.md +1 -1
- package/presets/claude/next/rules/ui-and-accessibility/design-tokens-binding.md +4 -4
- package/presets/claude/next/rules/ui-and-accessibility/forms-validation.md +2 -2
- package/presets/claude/next/team/README.md +21 -0
- package/presets/claude/next/team/fixtures/feature-with-figma-url.json +10 -0
- package/presets/claude/next/team/fixtures/feature-with-ux-no-figma.json +10 -0
- package/presets/claude/next/team/fixtures/mockup-review-standalone.json +9 -0
- package/presets/claude/next/team/fixtures/ux-design-standalone.json +9 -0
- package/presets/claude/nuxt/agents/code-reviewer.md +1 -0
- package/presets/claude/nuxt/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/nuxt/rules/architecture/layer-barrel-exports.md +1 -1
- package/presets/claude/nuxt/rules/stack/navigation-router-ui.md +1 -1
- package/presets/claude/nuxt/rules/tooling-and-review/interactive-questions.md +8 -8
- package/presets/claude/nuxt/rules/tooling-and-review/language-policy.md +1 -1
- package/presets/claude/nuxt/rules/tooling-and-review/post-change-lint.md +1 -1
- package/presets/claude/nuxt/rules/tooling-and-review/preset-pr-checklist.md +1 -1
- package/presets/claude/nuxt/team/README.md +10 -0
- package/presets/claude/php-hexagonal/agents/code-reviewer.md +1 -0
- package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/php-hexagonal/rules/tooling-and-review/interactive-questions.md +8 -8
- package/presets/claude/php-hexagonal/rules/tooling-and-review/language-policy.md +1 -1
- package/presets/claude/php-hexagonal/rules/tooling-and-review/preset-pr-checklist.md +1 -1
- package/presets/claude/php-hexagonal/team/README.md +10 -0
- package/presets/claude/php-laravel/agents/code-reviewer.md +1 -0
- package/presets/claude/php-laravel/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/php-laravel/rules/tooling-and-review/interactive-questions.md +8 -8
- package/presets/claude/php-laravel/rules/tooling-and-review/language-policy.md +1 -1
- package/presets/claude/php-laravel/rules/tooling-and-review/preset-pr-checklist.md +1 -1
- package/presets/claude/php-laravel/team/README.md +10 -0
- package/presets/claude/svelte/agents/code-reviewer.md +1 -0
- package/presets/claude/svelte/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/svelte/rules/architecture/layer-barrel-exports.md +1 -1
- package/presets/claude/svelte/rules/tooling-and-review/interactive-questions.md +8 -8
- package/presets/claude/svelte/rules/tooling-and-review/language-policy.md +1 -1
- package/presets/claude/svelte/rules/tooling-and-review/post-change-lint.md +1 -1
- package/presets/claude/svelte/rules/tooling-and-review/preset-pr-checklist.md +1 -1
- package/presets/claude/svelte/team/README.md +10 -0
- package/presets/cursor/android-kotlin/agents/code-reviewer.md +1 -0
- package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/android-kotlin/rules/README.md +1 -0
- package/presets/cursor/android-kotlin/rules/agent-team-orchestrator.mdc +3 -3
- package/presets/cursor/android-kotlin/rules/interactive-questions.mdc +8 -8
- package/presets/cursor/android-kotlin/rules/preset-pr-checklist.mdc +1 -1
- package/presets/cursor/android-kotlin/team/README.md +10 -0
- package/presets/cursor/go/agents/code-reviewer.md +1 -0
- package/presets/cursor/go/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/go/rules/agent-team-orchestrator.mdc +2 -2
- package/presets/cursor/go/rules/interactive-questions.mdc +8 -8
- package/presets/cursor/go/rules/preset-pr-checklist.mdc +1 -1
- package/presets/cursor/go/team/README.md +10 -0
- package/presets/cursor/ios-swift/agents/code-reviewer.md +1 -0
- package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/ios-swift/rules/README.md +1 -0
- package/presets/cursor/ios-swift/rules/agent-team-orchestrator.mdc +3 -3
- package/presets/cursor/ios-swift/rules/interactive-questions.mdc +8 -8
- package/presets/cursor/ios-swift/rules/preset-pr-checklist.mdc +1 -1
- package/presets/cursor/ios-swift/team/README.md +10 -0
- package/presets/cursor/java/agents/code-reviewer.md +1 -0
- package/presets/cursor/java/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/java/rules/agent-team-orchestrator.mdc +2 -2
- package/presets/cursor/java/rules/interactive-questions.mdc +8 -8
- package/presets/cursor/java/rules/preset-pr-checklist.mdc +1 -1
- package/presets/cursor/java/team/README.md +10 -0
- package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/mcp-ts/rules/agent-team-orchestrator.mdc +2 -2
- package/presets/cursor/mcp-ts/rules/interactive-questions.mdc +8 -8
- package/presets/cursor/mcp-ts/rules/preset-pr-checklist.mdc +1 -1
- package/presets/cursor/mcp-ts/team/README.md +10 -0
- package/presets/cursor/next/MCP.md +14 -0
- package/presets/cursor/next/REPO_AGENTS.md +1 -1
- package/presets/cursor/next/agents/code-reviewer.md +1 -0
- package/presets/cursor/next/agents/playwright-test-healer.md +1 -0
- package/presets/cursor/next/ai-local/rules-library/router-pages.md +5 -5
- package/presets/cursor/next/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/next/mcp.json +3 -3
- package/presets/cursor/next/rules/README.md +1 -1
- package/presets/cursor/next/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/cursor/next/rules/css-property-order-stylelint.mdc +1 -1
- package/presets/cursor/next/rules/design-tokens-binding.mdc +4 -4
- package/presets/cursor/next/rules/forms-validation.mdc +2 -2
- package/presets/cursor/next/rules/interactive-questions.mdc +8 -8
- package/presets/cursor/next/rules/navigation-router-stack.mdc +14 -1
- package/presets/cursor/next/rules/next-app-core.mdc +19 -17
- package/presets/cursor/next/rules/next-app-router.mdc +5 -17
- package/presets/cursor/next/rules/next-static-export.mdc +1 -1
- package/presets/cursor/next/rules/preset-pr-checklist.mdc +1 -1
- package/presets/cursor/next/rules/preset-token-budget.mdc +1 -1
- package/presets/cursor/next/rules/react-next-anti-hallucination.mdc +1 -1
- package/presets/cursor/next/rules/reference-features.mdc +1 -1
- package/presets/cursor/next/rules/security-next.mdc +28 -3
- package/presets/cursor/next/team/README.md +21 -0
- package/presets/cursor/next/team/fixtures/feature-with-figma-url.json +10 -0
- package/presets/cursor/next/team/fixtures/feature-with-ux-no-figma.json +10 -0
- package/presets/cursor/next/team/fixtures/mockup-review-standalone.json +9 -0
- package/presets/cursor/next/team/fixtures/ux-design-standalone.json +9 -0
- package/presets/cursor/nuxt/agents/code-reviewer.md +1 -0
- package/presets/cursor/nuxt/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/nuxt/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/cursor/nuxt/rules/interactive-questions.mdc +8 -8
- package/presets/cursor/nuxt/rules/preset-pr-checklist.mdc +1 -1
- package/presets/cursor/nuxt/team/README.md +10 -0
- package/presets/cursor/php-hexagonal/agents/code-reviewer.md +1 -0
- package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/php-hexagonal/rules/agent-team-orchestrator.mdc +2 -2
- package/presets/cursor/php-hexagonal/rules/interactive-questions.mdc +8 -8
- package/presets/cursor/php-hexagonal/rules/preset-pr-checklist.mdc +1 -1
- package/presets/cursor/php-hexagonal/team/README.md +10 -0
- package/presets/cursor/php-laravel/agents/code-reviewer.md +1 -0
- package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/php-laravel/rules/agent-team-orchestrator.mdc +2 -2
- package/presets/cursor/php-laravel/rules/interactive-questions.mdc +8 -8
- package/presets/cursor/php-laravel/rules/preset-pr-checklist.mdc +1 -1
- package/presets/cursor/php-laravel/team/README.md +10 -0
- package/presets/cursor/svelte/agents/code-reviewer.md +1 -0
- package/presets/cursor/svelte/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/svelte/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/cursor/svelte/rules/interactive-questions.mdc +8 -8
- package/presets/cursor/svelte/rules/preset-pr-checklist.mdc +1 -1
- package/presets/cursor/svelte/team/README.md +10 -0
- package/presets/mavis/android-kotlin/adapter-map.json +1 -1
- package/presets/mavis/android-kotlin/agents/code-reviewer.md +1 -0
- package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/android-kotlin/rules/interactive-questions.md +8 -8
- package/presets/mavis/android-kotlin/rules/preset-pr-checklist.md +1 -1
- package/presets/mavis/android-kotlin/team/README.md +10 -0
- package/presets/mavis/go/adapter-map.json +1 -1
- package/presets/mavis/go/agents/code-reviewer.md +1 -0
- package/presets/mavis/go/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/go/rules/interactive-questions.md +8 -8
- package/presets/mavis/go/rules/preset-pr-checklist.md +1 -1
- package/presets/mavis/go/team/README.md +10 -0
- package/presets/mavis/ios-swift/adapter-map.json +1 -1
- package/presets/mavis/ios-swift/agents/code-reviewer.md +1 -0
- package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/ios-swift/rules/interactive-questions.md +8 -8
- package/presets/mavis/ios-swift/rules/preset-pr-checklist.md +1 -1
- package/presets/mavis/ios-swift/team/README.md +10 -0
- package/presets/mavis/java/adapter-map.json +1 -1
- package/presets/mavis/java/agents/code-reviewer.md +1 -0
- package/presets/mavis/java/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/java/rules/interactive-questions.md +8 -8
- package/presets/mavis/java/rules/preset-pr-checklist.md +1 -1
- package/presets/mavis/java/team/README.md +10 -0
- package/presets/mavis/mcp-ts/adapter-map.json +1 -1
- package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/mcp-ts/rules/interactive-questions.md +8 -8
- package/presets/mavis/mcp-ts/rules/preset-pr-checklist.md +1 -1
- package/presets/mavis/mcp-ts/team/README.md +10 -0
- package/presets/mavis/next/MCP.md +14 -0
- package/presets/mavis/next/REPO_AGENTS.md +1 -1
- package/presets/mavis/next/adapter-map.json +1 -1
- package/presets/mavis/next/agents/code-reviewer.md +1 -0
- package/presets/mavis/next/agents/playwright-test-healer.md +1 -0
- package/presets/mavis/next/ai-local/rules-library/router-pages.md +5 -5
- package/presets/mavis/next/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/next/mcp.json +3 -3
- package/presets/mavis/next/rules/css-property-order-stylelint.md +1 -1
- package/presets/mavis/next/rules/design-tokens-binding.md +4 -4
- package/presets/mavis/next/rules/forms-validation.md +2 -2
- package/presets/mavis/next/rules/interactive-questions.md +8 -8
- package/presets/mavis/next/rules/navigation-router-stack.md +12 -0
- package/presets/mavis/next/rules/next-app-core.md +18 -17
- package/presets/mavis/next/rules/next-app-router.md +4 -17
- package/presets/mavis/next/rules/next-static-export.md +1 -1
- package/presets/mavis/next/rules/preset-pr-checklist.md +1 -1
- package/presets/mavis/next/rules/react-next-anti-hallucination.md +1 -1
- package/presets/mavis/next/rules/reference-features.md +1 -1
- package/presets/mavis/next/rules/security-next.md +24 -3
- package/presets/mavis/next/team/README.md +21 -0
- package/presets/mavis/next/team/fixtures/feature-with-figma-url.json +10 -0
- package/presets/mavis/next/team/fixtures/feature-with-ux-no-figma.json +10 -0
- package/presets/mavis/next/team/fixtures/mockup-review-standalone.json +9 -0
- package/presets/mavis/next/team/fixtures/ux-design-standalone.json +9 -0
- package/presets/mavis/nuxt/adapter-map.json +1 -1
- package/presets/mavis/nuxt/agents/code-reviewer.md +1 -0
- package/presets/mavis/nuxt/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/nuxt/rules/interactive-questions.md +8 -8
- package/presets/mavis/nuxt/rules/preset-pr-checklist.md +1 -1
- package/presets/mavis/nuxt/team/README.md +10 -0
- package/presets/mavis/php-hexagonal/adapter-map.json +1 -1
- package/presets/mavis/php-hexagonal/agents/code-reviewer.md +1 -0
- package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/php-hexagonal/rules/interactive-questions.md +8 -8
- package/presets/mavis/php-hexagonal/rules/preset-pr-checklist.md +1 -1
- package/presets/mavis/php-hexagonal/team/README.md +10 -0
- package/presets/mavis/php-laravel/adapter-map.json +1 -1
- package/presets/mavis/php-laravel/agents/code-reviewer.md +1 -0
- package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/php-laravel/rules/interactive-questions.md +8 -8
- package/presets/mavis/php-laravel/rules/preset-pr-checklist.md +1 -1
- package/presets/mavis/php-laravel/team/README.md +10 -0
- package/presets/mavis/svelte/adapter-map.json +1 -1
- package/presets/mavis/svelte/agents/code-reviewer.md +1 -0
- package/presets/mavis/svelte/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/svelte/rules/interactive-questions.md +8 -8
- package/presets/mavis/svelte/rules/preset-pr-checklist.md +1 -1
- package/presets/mavis/svelte/team/README.md +10 -0
- package/presets/opencode/android-kotlin/adapter-map.json +1 -1
- package/presets/opencode/android-kotlin/agents/code-reviewer.md +1 -0
- package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/android-kotlin/rules/interactive-questions.md +8 -8
- package/presets/opencode/android-kotlin/rules/preset-pr-checklist.md +1 -1
- package/presets/opencode/android-kotlin/team/README.md +10 -0
- package/presets/opencode/go/adapter-map.json +1 -1
- package/presets/opencode/go/agents/code-reviewer.md +1 -0
- package/presets/opencode/go/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/go/rules/interactive-questions.md +8 -8
- package/presets/opencode/go/rules/preset-pr-checklist.md +1 -1
- package/presets/opencode/go/team/README.md +10 -0
- package/presets/opencode/ios-swift/adapter-map.json +1 -1
- package/presets/opencode/ios-swift/agents/code-reviewer.md +1 -0
- package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/ios-swift/rules/interactive-questions.md +8 -8
- package/presets/opencode/ios-swift/rules/preset-pr-checklist.md +1 -1
- package/presets/opencode/ios-swift/team/README.md +10 -0
- package/presets/opencode/java/adapter-map.json +1 -1
- package/presets/opencode/java/agents/code-reviewer.md +1 -0
- package/presets/opencode/java/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/java/rules/interactive-questions.md +8 -8
- package/presets/opencode/java/rules/preset-pr-checklist.md +1 -1
- package/presets/opencode/java/team/README.md +10 -0
- package/presets/opencode/mcp-ts/adapter-map.json +1 -1
- package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/mcp-ts/rules/interactive-questions.md +8 -8
- package/presets/opencode/mcp-ts/rules/preset-pr-checklist.md +1 -1
- package/presets/opencode/mcp-ts/team/README.md +10 -0
- package/presets/opencode/next/MCP.md +14 -0
- package/presets/opencode/next/REPO_AGENTS.md +1 -1
- package/presets/opencode/next/adapter-map.json +1 -1
- package/presets/opencode/next/agents/code-reviewer.md +1 -0
- package/presets/opencode/next/agents/playwright-test-healer.md +1 -0
- package/presets/opencode/next/ai-local/rules-library/router-pages.md +5 -5
- package/presets/opencode/next/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/next/opencode.json +3 -3
- package/presets/opencode/next/rules/css-property-order-stylelint.md +1 -1
- package/presets/opencode/next/rules/design-tokens-binding.md +4 -4
- package/presets/opencode/next/rules/forms-validation.md +2 -2
- package/presets/opencode/next/rules/interactive-questions.md +8 -8
- package/presets/opencode/next/rules/navigation-router-stack.md +12 -0
- package/presets/opencode/next/rules/next-app-core.md +18 -17
- package/presets/opencode/next/rules/next-app-router.md +4 -17
- package/presets/opencode/next/rules/next-static-export.md +1 -1
- package/presets/opencode/next/rules/preset-pr-checklist.md +1 -1
- package/presets/opencode/next/rules/react-next-anti-hallucination.md +1 -1
- package/presets/opencode/next/rules/reference-features.md +1 -1
- package/presets/opencode/next/rules/security-next.md +24 -3
- package/presets/opencode/next/team/README.md +21 -0
- package/presets/opencode/next/team/fixtures/feature-with-figma-url.json +10 -0
- package/presets/opencode/next/team/fixtures/feature-with-ux-no-figma.json +10 -0
- package/presets/opencode/next/team/fixtures/mockup-review-standalone.json +9 -0
- package/presets/opencode/next/team/fixtures/ux-design-standalone.json +9 -0
- package/presets/opencode/nuxt/adapter-map.json +1 -1
- package/presets/opencode/nuxt/agents/code-reviewer.md +1 -0
- package/presets/opencode/nuxt/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/nuxt/rules/interactive-questions.md +8 -8
- package/presets/opencode/nuxt/rules/preset-pr-checklist.md +1 -1
- package/presets/opencode/nuxt/team/README.md +10 -0
- package/presets/opencode/php-hexagonal/adapter-map.json +1 -1
- package/presets/opencode/php-hexagonal/agents/code-reviewer.md +1 -0
- package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/php-hexagonal/rules/interactive-questions.md +8 -8
- package/presets/opencode/php-hexagonal/rules/preset-pr-checklist.md +1 -1
- package/presets/opencode/php-hexagonal/team/README.md +10 -0
- package/presets/opencode/php-laravel/adapter-map.json +1 -1
- package/presets/opencode/php-laravel/agents/code-reviewer.md +1 -0
- package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/php-laravel/rules/interactive-questions.md +8 -8
- package/presets/opencode/php-laravel/rules/preset-pr-checklist.md +1 -1
- package/presets/opencode/php-laravel/team/README.md +10 -0
- package/presets/opencode/svelte/adapter-map.json +1 -1
- package/presets/opencode/svelte/agents/code-reviewer.md +1 -0
- package/presets/opencode/svelte/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/svelte/rules/interactive-questions.md +8 -8
- package/presets/opencode/svelte/rules/preset-pr-checklist.md +1 -1
- package/presets/opencode/svelte/team/README.md +10 -0
|
@@ -0,0 +1,831 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema, migrations and connection handling for the `.ai.local/team` SQLite
|
|
3
|
+
* projection.
|
|
4
|
+
*
|
|
5
|
+
* The database is a rebuildable projection: files stay authoritative, so every
|
|
6
|
+
* schema change that cannot be migrated is repaired by moving the old file
|
|
7
|
+
* aside and recreating it. `node:sqlite` is probed at runtime; there is no
|
|
8
|
+
* top-level import, so this module loads on a runtime that does not ship it.
|
|
9
|
+
*
|
|
10
|
+
* Error codes are part of the contract (`E_MIGRATION_CHECKSUM`,
|
|
11
|
+
* `E_MIGRATION_DRIFT`, `E_MIGRATION_FAILED`, `E_SCHEMA_NEWER`,
|
|
12
|
+
* `E_SCHEMA_MISMATCH`, `E_SCHEMA_REBUILD_FAILED`, `E_DB_CORRUPT`,
|
|
13
|
+
* `E_DB_BUSY`, `E_DB_MISSING`, `E_SCHEMA_OUTDATED`); each carries the process
|
|
14
|
+
* exit code the CLI must use.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import crypto from 'node:crypto';
|
|
18
|
+
import fs from 'node:fs';
|
|
19
|
+
import { createRequire } from 'node:module';
|
|
20
|
+
import path from 'node:path';
|
|
21
|
+
|
|
22
|
+
/** Node floor for the unflagged `node:sqlite` module. */
|
|
23
|
+
export const MIN_NODE = '22.13.0';
|
|
24
|
+
export const MIN_NODE_MAJOR = 22;
|
|
25
|
+
export const MIN_NODE_MINOR = 13;
|
|
26
|
+
|
|
27
|
+
export const DB_FILE = 'team.db';
|
|
28
|
+
export const DB_ENGINE = 'sqlite';
|
|
29
|
+
export const SEARCH_TABLE = 'search_fts';
|
|
30
|
+
export const SEARCH_MODE_FTS5 = 'fts5';
|
|
31
|
+
export const SEARCH_MODE_LIKE = 'like';
|
|
32
|
+
export const INDEX_UNAVAILABLE_LINE = `team-db: node:sqlite unavailable (requires Node >= ${MIN_NODE}); file-only mode`;
|
|
33
|
+
|
|
34
|
+
export const DEFAULT_TEAM_ROOT = path.join(process.cwd(), '.ai.local', 'team');
|
|
35
|
+
|
|
36
|
+
const SQL = String.raw;
|
|
37
|
+
|
|
38
|
+
export const BASE_SQL = SQL`
|
|
39
|
+
CREATE TABLE schema_migrations (
|
|
40
|
+
version INTEGER PRIMARY KEY,
|
|
41
|
+
name TEXT NOT NULL,
|
|
42
|
+
checksum TEXT NOT NULL,
|
|
43
|
+
applied_at TEXT NOT NULL
|
|
44
|
+
) STRICT;
|
|
45
|
+
|
|
46
|
+
CREATE TABLE schema_meta (
|
|
47
|
+
key TEXT PRIMARY KEY,
|
|
48
|
+
value TEXT NOT NULL
|
|
49
|
+
) STRICT;
|
|
50
|
+
|
|
51
|
+
CREATE TABLE tasks (
|
|
52
|
+
slug TEXT PRIMARY KEY,
|
|
53
|
+
title TEXT,
|
|
54
|
+
intent TEXT,
|
|
55
|
+
profile TEXT,
|
|
56
|
+
spec_mode TEXT,
|
|
57
|
+
complexity TEXT,
|
|
58
|
+
phase TEXT,
|
|
59
|
+
state TEXT,
|
|
60
|
+
pipeline_index INTEGER,
|
|
61
|
+
current_agent TEXT,
|
|
62
|
+
awaiting_human_gate INTEGER NOT NULL DEFAULT 0,
|
|
63
|
+
metrics_run_id TEXT,
|
|
64
|
+
routing_summary TEXT,
|
|
65
|
+
routing_json TEXT,
|
|
66
|
+
context_budget TEXT,
|
|
67
|
+
plan_revision TEXT,
|
|
68
|
+
step_count INTEGER,
|
|
69
|
+
start_at TEXT,
|
|
70
|
+
end_at TEXT,
|
|
71
|
+
updated_at TEXT,
|
|
72
|
+
projected_at TEXT NOT NULL,
|
|
73
|
+
last_seen_at TEXT NOT NULL,
|
|
74
|
+
content_hash TEXT NOT NULL,
|
|
75
|
+
file_count INTEGER NOT NULL DEFAULT 0,
|
|
76
|
+
document_bytes INTEGER NOT NULL DEFAULT 0,
|
|
77
|
+
parse_error_count INTEGER NOT NULL DEFAULT 0,
|
|
78
|
+
first_seen_at TEXT NOT NULL,
|
|
79
|
+
absent_at TEXT
|
|
80
|
+
) STRICT;
|
|
81
|
+
|
|
82
|
+
CREATE TABLE pipeline_steps (
|
|
83
|
+
task_slug TEXT NOT NULL REFERENCES tasks(slug) ON DELETE CASCADE,
|
|
84
|
+
step_index INTEGER NOT NULL,
|
|
85
|
+
label TEXT,
|
|
86
|
+
scope TEXT,
|
|
87
|
+
model TEXT,
|
|
88
|
+
skip_if TEXT,
|
|
89
|
+
parallel INTEGER NOT NULL DEFAULT 0,
|
|
90
|
+
depends_on_json TEXT,
|
|
91
|
+
task_ids_json TEXT,
|
|
92
|
+
write_scopes_json TEXT,
|
|
93
|
+
PRIMARY KEY (task_slug, step_index)
|
|
94
|
+
) STRICT;
|
|
95
|
+
|
|
96
|
+
CREATE TABLE pipeline_step_agents (
|
|
97
|
+
task_slug TEXT NOT NULL,
|
|
98
|
+
step_index INTEGER NOT NULL,
|
|
99
|
+
ordinal INTEGER NOT NULL,
|
|
100
|
+
agent TEXT NOT NULL,
|
|
101
|
+
PRIMARY KEY (task_slug, step_index, ordinal),
|
|
102
|
+
FOREIGN KEY (task_slug, step_index)
|
|
103
|
+
REFERENCES pipeline_steps(task_slug, step_index) ON DELETE CASCADE
|
|
104
|
+
) STRICT;
|
|
105
|
+
|
|
106
|
+
CREATE TABLE attempts (
|
|
107
|
+
attempt_id TEXT PRIMARY KEY,
|
|
108
|
+
task_slug TEXT NOT NULL REFERENCES tasks(slug) ON DELETE CASCADE,
|
|
109
|
+
step_index INTEGER,
|
|
110
|
+
step_label TEXT,
|
|
111
|
+
agent TEXT,
|
|
112
|
+
role TEXT,
|
|
113
|
+
attempt_no INTEGER,
|
|
114
|
+
wave_id TEXT,
|
|
115
|
+
parallel_group_id TEXT,
|
|
116
|
+
started_at TEXT,
|
|
117
|
+
finished_at TEXT,
|
|
118
|
+
duration_ms INTEGER,
|
|
119
|
+
outcome TEXT,
|
|
120
|
+
source TEXT,
|
|
121
|
+
attribution_confidence TEXT,
|
|
122
|
+
is_current INTEGER NOT NULL DEFAULT 0
|
|
123
|
+
) STRICT;
|
|
124
|
+
|
|
125
|
+
CREATE TABLE events (
|
|
126
|
+
event_id TEXT PRIMARY KEY,
|
|
127
|
+
task_slug TEXT NOT NULL REFERENCES tasks(slug) ON DELETE CASCADE,
|
|
128
|
+
seq INTEGER NOT NULL,
|
|
129
|
+
type TEXT NOT NULL,
|
|
130
|
+
at TEXT,
|
|
131
|
+
attempt_id TEXT,
|
|
132
|
+
step_index INTEGER,
|
|
133
|
+
step_label TEXT,
|
|
134
|
+
agent TEXT,
|
|
135
|
+
role TEXT,
|
|
136
|
+
attempt_no INTEGER,
|
|
137
|
+
outcome TEXT,
|
|
138
|
+
gate_started_event_id TEXT,
|
|
139
|
+
wave_id TEXT,
|
|
140
|
+
parallel_group_id TEXT,
|
|
141
|
+
duration_ms INTEGER,
|
|
142
|
+
source TEXT,
|
|
143
|
+
attribution_confidence TEXT,
|
|
144
|
+
payload_json TEXT NOT NULL
|
|
145
|
+
) STRICT;
|
|
146
|
+
|
|
147
|
+
CREATE TABLE gates (
|
|
148
|
+
start_event_id TEXT PRIMARY KEY,
|
|
149
|
+
task_slug TEXT NOT NULL REFERENCES tasks(slug) ON DELETE CASCADE,
|
|
150
|
+
started_at TEXT,
|
|
151
|
+
end_event_id TEXT,
|
|
152
|
+
ended_at TEXT,
|
|
153
|
+
duration_ms INTEGER
|
|
154
|
+
) STRICT;
|
|
155
|
+
|
|
156
|
+
CREATE TABLE artifacts (
|
|
157
|
+
task_slug TEXT NOT NULL REFERENCES tasks(slug) ON DELETE CASCADE,
|
|
158
|
+
artifact_id TEXT NOT NULL,
|
|
159
|
+
producer TEXT,
|
|
160
|
+
status TEXT,
|
|
161
|
+
outcome TEXT,
|
|
162
|
+
summary TEXT,
|
|
163
|
+
receipt_attempt_id TEXT,
|
|
164
|
+
created_at TEXT,
|
|
165
|
+
updated_at TEXT,
|
|
166
|
+
completed_at TEXT,
|
|
167
|
+
ac_ids_json TEXT NOT NULL DEFAULT '[]',
|
|
168
|
+
task_ids_json TEXT NOT NULL DEFAULT '[]',
|
|
169
|
+
PRIMARY KEY (task_slug, artifact_id)
|
|
170
|
+
) STRICT;
|
|
171
|
+
|
|
172
|
+
CREATE TABLE artifact_paths (
|
|
173
|
+
task_slug TEXT NOT NULL,
|
|
174
|
+
artifact_id TEXT NOT NULL,
|
|
175
|
+
kind TEXT NOT NULL CHECK (kind IN ('authoritative', 'evidence')),
|
|
176
|
+
ordinal INTEGER NOT NULL,
|
|
177
|
+
path TEXT NOT NULL,
|
|
178
|
+
PRIMARY KEY (task_slug, artifact_id, kind, ordinal),
|
|
179
|
+
FOREIGN KEY (task_slug, artifact_id)
|
|
180
|
+
REFERENCES artifacts(task_slug, artifact_id) ON DELETE CASCADE
|
|
181
|
+
) STRICT;
|
|
182
|
+
|
|
183
|
+
CREATE TABLE prompts (
|
|
184
|
+
task_slug TEXT NOT NULL REFERENCES tasks(slug) ON DELETE CASCADE,
|
|
185
|
+
seq INTEGER NOT NULL,
|
|
186
|
+
source TEXT,
|
|
187
|
+
command TEXT,
|
|
188
|
+
captured_at TEXT,
|
|
189
|
+
language TEXT,
|
|
190
|
+
content TEXT NOT NULL,
|
|
191
|
+
sha256 TEXT NOT NULL,
|
|
192
|
+
bytes INTEGER NOT NULL,
|
|
193
|
+
PRIMARY KEY (task_slug, seq)
|
|
194
|
+
) STRICT;
|
|
195
|
+
|
|
196
|
+
CREATE TABLE review_ledger (
|
|
197
|
+
task_slug TEXT NOT NULL REFERENCES tasks(slug) ON DELETE CASCADE,
|
|
198
|
+
row_id TEXT NOT NULL,
|
|
199
|
+
at TEXT,
|
|
200
|
+
source TEXT,
|
|
201
|
+
summary TEXT,
|
|
202
|
+
fair TEXT,
|
|
203
|
+
disposition TEXT,
|
|
204
|
+
evidence TEXT,
|
|
205
|
+
signature TEXT,
|
|
206
|
+
PRIMARY KEY (task_slug, row_id)
|
|
207
|
+
) STRICT;
|
|
208
|
+
|
|
209
|
+
CREATE TABLE outcomes (
|
|
210
|
+
task_slug TEXT PRIMARY KEY REFERENCES tasks(slug) ON DELETE CASCADE,
|
|
211
|
+
goal_achieved TEXT,
|
|
212
|
+
value TEXT,
|
|
213
|
+
review_total INTEGER,
|
|
214
|
+
review_fair INTEGER,
|
|
215
|
+
review_rejected INTEGER,
|
|
216
|
+
review_open INTEGER,
|
|
217
|
+
review_rework INTEGER,
|
|
218
|
+
body TEXT NOT NULL,
|
|
219
|
+
sha256 TEXT NOT NULL
|
|
220
|
+
) STRICT;
|
|
221
|
+
|
|
222
|
+
CREATE TABLE documents (
|
|
223
|
+
task_slug TEXT NOT NULL REFERENCES tasks(slug) ON DELETE CASCADE,
|
|
224
|
+
path TEXT NOT NULL,
|
|
225
|
+
sha256 TEXT,
|
|
226
|
+
bytes INTEGER,
|
|
227
|
+
media_type TEXT NOT NULL,
|
|
228
|
+
is_text INTEGER NOT NULL DEFAULT 1,
|
|
229
|
+
content TEXT,
|
|
230
|
+
content_omitted TEXT,
|
|
231
|
+
parse_error TEXT,
|
|
232
|
+
projected_at TEXT NOT NULL,
|
|
233
|
+
PRIMARY KEY (task_slug, path)
|
|
234
|
+
) STRICT;
|
|
235
|
+
|
|
236
|
+
CREATE TABLE sync_runs (
|
|
237
|
+
run_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
238
|
+
mode TEXT NOT NULL,
|
|
239
|
+
started_at TEXT NOT NULL,
|
|
240
|
+
finished_at TEXT,
|
|
241
|
+
inserted INTEGER NOT NULL DEFAULT 0,
|
|
242
|
+
updated INTEGER NOT NULL DEFAULT 0,
|
|
243
|
+
unchanged INTEGER NOT NULL DEFAULT 0,
|
|
244
|
+
failed INTEGER NOT NULL DEFAULT 0,
|
|
245
|
+
absent INTEGER NOT NULL DEFAULT 0,
|
|
246
|
+
pruned INTEGER NOT NULL DEFAULT 0,
|
|
247
|
+
rebuilt INTEGER NOT NULL DEFAULT 0,
|
|
248
|
+
exit_code INTEGER,
|
|
249
|
+
error TEXT
|
|
250
|
+
) STRICT;
|
|
251
|
+
|
|
252
|
+
CREATE INDEX idx_tasks_state ON tasks(state);
|
|
253
|
+
CREATE INDEX idx_tasks_intent ON tasks(intent);
|
|
254
|
+
CREATE INDEX idx_tasks_profile ON tasks(profile);
|
|
255
|
+
CREATE INDEX idx_tasks_updated ON tasks(updated_at DESC);
|
|
256
|
+
CREATE INDEX idx_tasks_absent ON tasks(absent_at);
|
|
257
|
+
CREATE INDEX idx_attempts_task_step ON attempts(task_slug, step_index, attempt_no);
|
|
258
|
+
CREATE INDEX idx_attempts_agent ON attempts(agent);
|
|
259
|
+
CREATE INDEX idx_attempts_started ON attempts(started_at);
|
|
260
|
+
CREATE INDEX idx_events_task_at ON events(task_slug, at);
|
|
261
|
+
CREATE INDEX idx_events_task_type ON events(task_slug, type);
|
|
262
|
+
CREATE INDEX idx_events_attempt ON events(attempt_id);
|
|
263
|
+
CREATE INDEX idx_events_type ON events(type);
|
|
264
|
+
CREATE INDEX idx_gates_task ON gates(task_slug, started_at);
|
|
265
|
+
CREATE INDEX idx_artifacts_status ON artifacts(status);
|
|
266
|
+
CREATE INDEX idx_artifacts_producer ON artifacts(producer);
|
|
267
|
+
CREATE INDEX idx_artifact_paths ON artifact_paths(task_slug, path);
|
|
268
|
+
CREATE INDEX idx_prompts_task ON prompts(task_slug, seq);
|
|
269
|
+
CREATE INDEX idx_documents_sha ON documents(sha256);
|
|
270
|
+
CREATE INDEX idx_sync_runs_started ON sync_runs(started_at DESC);
|
|
271
|
+
|
|
272
|
+
CREATE VIEW attempt_timeline AS
|
|
273
|
+
SELECT task_slug, attempt_id, agent, role, step_index, step_label,
|
|
274
|
+
attempt_no AS attempt, started_at, finished_at, duration_ms, outcome,
|
|
275
|
+
source, attribution_confidence, is_current
|
|
276
|
+
FROM attempts
|
|
277
|
+
ORDER BY task_slug, step_index, attempt_no;
|
|
278
|
+
`;
|
|
279
|
+
|
|
280
|
+
const sha256 = (value) => crypto.createHash('sha256').update(value).digest('hex');
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Ordered, immutable migrations. The literal checksum is verified before any
|
|
284
|
+
* DDL runs; a mismatch means the SQL changed without a new migration.
|
|
285
|
+
*/
|
|
286
|
+
export const MIGRATIONS = [
|
|
287
|
+
{
|
|
288
|
+
version: 1,
|
|
289
|
+
name: '0001-init',
|
|
290
|
+
sql: BASE_SQL,
|
|
291
|
+
checksum: '2f642c80d1049a1f90fffbccab603f12f4b8f5b7fc4eed7d9a3a018d70669b5f',
|
|
292
|
+
},
|
|
293
|
+
];
|
|
294
|
+
|
|
295
|
+
/** Highest migration version shipped by this module. */
|
|
296
|
+
export const SCHEMA_VERSION = MIGRATIONS[MIGRATIONS.length - 1].version;
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* A named failure. `code` is stable, `exitCode` is what the CLI returns.
|
|
300
|
+
* Database-level failures exit 2, load-time checksum failures exit 1, and a
|
|
301
|
+
* deadline or a busy database used by the hook path exits 4.
|
|
302
|
+
*/
|
|
303
|
+
export class TeamDbError extends Error {
|
|
304
|
+
constructor(code, message, exitCode = 2) {
|
|
305
|
+
super(`${code}: ${message}`);
|
|
306
|
+
this.name = 'TeamDbError';
|
|
307
|
+
this.code = code;
|
|
308
|
+
this.exitCode = exitCode;
|
|
309
|
+
this.detail = message;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export const teamDbFail = (code, message, exitCode = 2) => {
|
|
314
|
+
throw new TeamDbError(code, message, exitCode);
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const isTruthy = (value) => {
|
|
318
|
+
if (typeof value !== 'string') return false;
|
|
319
|
+
const normalized = value.trim().toLowerCase();
|
|
320
|
+
return normalized === '1' || normalized === 'true' || normalized === 'yes';
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
export const isKillSwitchSet = (env = process.env) => isTruthy(env.AI_RULES_TEAM_DB_DISABLE);
|
|
324
|
+
|
|
325
|
+
const parseNodeVersion = (version) => {
|
|
326
|
+
const match = /^(\d+)\.(\d+)\.(\d+)/.exec(String(version || ''));
|
|
327
|
+
if (!match) return null;
|
|
328
|
+
return { major: Number(match[1]), minor: Number(match[2]), patch: Number(match[3]) };
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
const meetsSqliteFloor = (version) => {
|
|
332
|
+
const parsed = parseNodeVersion(version);
|
|
333
|
+
if (!parsed) return false;
|
|
334
|
+
if (parsed.major > MIN_NODE_MAJOR) return true;
|
|
335
|
+
if (parsed.major < MIN_NODE_MAJOR) return false;
|
|
336
|
+
return parsed.minor >= MIN_NODE_MINOR;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Probe `node:sqlite` without importing it statically. Returns the module or
|
|
341
|
+
* `null`; the test seam `AI_RULES_TEAM_DB_FORCE_FALLBACK` forces `null`.
|
|
342
|
+
*/
|
|
343
|
+
export function loadSqlite(env = process.env) {
|
|
344
|
+
if (isTruthy(env.AI_RULES_TEAM_DB_FORCE_FALLBACK)) return null;
|
|
345
|
+
if (!meetsSqliteFloor(process.versions.node)) return null;
|
|
346
|
+
try {
|
|
347
|
+
const required = createRequire(import.meta.url);
|
|
348
|
+
const sqlite = required('node:sqlite');
|
|
349
|
+
return typeof sqlite?.DatabaseSync === 'function' ? sqlite : null;
|
|
350
|
+
} catch {
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export const isSqliteAvailable = (env = process.env) => loadSqlite(env) !== null;
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Verify every in-code migration checksum. Separate from module load so a
|
|
359
|
+
* fixture can hand in a tampered array and observe `E_MIGRATION_CHECKSUM`.
|
|
360
|
+
*/
|
|
361
|
+
export function verifyCodeChecksums(migrations = MIGRATIONS) {
|
|
362
|
+
for (const migration of migrations) {
|
|
363
|
+
const digest = sha256(migration.sql);
|
|
364
|
+
if (!/^[0-9a-f]{64}$/.test(migration.checksum) || digest !== migration.checksum) {
|
|
365
|
+
teamDbFail(
|
|
366
|
+
'E_MIGRATION_CHECKSUM',
|
|
367
|
+
`migration ${migration.version} (${migration.name}) checksum mismatch: code has ${digest}, literal is ${migration.checksum}`,
|
|
368
|
+
1,
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export const pragmaValue = (db, pragma) => {
|
|
375
|
+
const row = db.prepare(`PRAGMA ${pragma}`).get();
|
|
376
|
+
if (!row) return null;
|
|
377
|
+
const values = Object.values(row);
|
|
378
|
+
return values.length > 0 ? values[0] : null;
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
export function configureConnection(db, options = {}) {
|
|
382
|
+
// The busy timeout must be set before the first lock-taking pragma: two first
|
|
383
|
+
// projections both create the file, and a default 0 ms timeout turns the
|
|
384
|
+
// WAL switch on the loser into an immediate SQLITE_BUSY.
|
|
385
|
+
db.exec('PRAGMA busy_timeout=5000');
|
|
386
|
+
// WAL is persistent. Only switch a freshly created database; re-issuing the
|
|
387
|
+
// switch on every open needs an exclusive lock and would wait the whole busy
|
|
388
|
+
// timeout behind another connection's transaction for no state change.
|
|
389
|
+
if (options.setWal) db.exec('PRAGMA journal_mode=WAL');
|
|
390
|
+
db.exec('PRAGMA foreign_keys=ON');
|
|
391
|
+
db.exec('PRAGMA synchronous=NORMAL');
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
export function readPragmas(db) {
|
|
395
|
+
return {
|
|
396
|
+
journalMode: pragmaValue(db, 'journal_mode'),
|
|
397
|
+
busyTimeout: pragmaValue(db, 'busy_timeout'),
|
|
398
|
+
foreignKeys: pragmaValue(db, 'foreign_keys'),
|
|
399
|
+
synchronous: pragmaValue(db, 'synchronous'),
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
export const readUserVersion = (db) => Number(pragmaValue(db, 'user_version') || 0);
|
|
404
|
+
|
|
405
|
+
const tableExists = (db, name) =>
|
|
406
|
+
Boolean(
|
|
407
|
+
db.prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?").get(name),
|
|
408
|
+
);
|
|
409
|
+
|
|
410
|
+
export function listAppliedMigrations(db) {
|
|
411
|
+
if (!tableExists(db, 'schema_migrations')) return [];
|
|
412
|
+
return db
|
|
413
|
+
.prepare('SELECT version, name, checksum, applied_at FROM schema_migrations ORDER BY version')
|
|
414
|
+
.all()
|
|
415
|
+
.map((row) => ({ ...row }));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const assertNoDrift = (applied, migrations) => {
|
|
419
|
+
const byVersion = new Map(migrations.map((migration) => [migration.version, migration]));
|
|
420
|
+
for (const row of applied) {
|
|
421
|
+
const migration = byVersion.get(Number(row.version));
|
|
422
|
+
if (!migration) {
|
|
423
|
+
teamDbFail(
|
|
424
|
+
'E_MIGRATION_DRIFT',
|
|
425
|
+
`database has migration ${row.version} (${row.name}) that this code does not ship`,
|
|
426
|
+
1,
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
if (row.checksum !== migration.checksum) {
|
|
430
|
+
teamDbFail(
|
|
431
|
+
'E_MIGRATION_DRIFT',
|
|
432
|
+
`applied migration ${row.version} checksum ${row.checksum} does not match code ${migration.checksum}`,
|
|
433
|
+
1,
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Apply ordered migrations in one transaction each. A failing migration rolls
|
|
441
|
+
* back completely; `PRAGMA user_version` moves inside the same transaction.
|
|
442
|
+
*/
|
|
443
|
+
export function applyMigrations(db, migrations = MIGRATIONS) {
|
|
444
|
+
verifyCodeChecksums(migrations);
|
|
445
|
+
const maxVersion = migrations[migrations.length - 1].version;
|
|
446
|
+
const current = readUserVersion(db);
|
|
447
|
+
if (current > maxVersion) {
|
|
448
|
+
teamDbFail('E_SCHEMA_NEWER', `database schema v${current} is newer than code v${maxVersion}`, 2);
|
|
449
|
+
}
|
|
450
|
+
const applied = listAppliedMigrations(db);
|
|
451
|
+
assertNoDrift(applied, migrations);
|
|
452
|
+
const appliedVersions = new Set(applied.map((row) => Number(row.version)));
|
|
453
|
+
let count = 0;
|
|
454
|
+
for (const migration of migrations) {
|
|
455
|
+
if (migration.version <= current) continue;
|
|
456
|
+
if (appliedVersions.has(migration.version)) continue;
|
|
457
|
+
const appliedAt = new Date().toISOString();
|
|
458
|
+
db.exec('BEGIN IMMEDIATE');
|
|
459
|
+
try {
|
|
460
|
+
// Two first projections can both read user_version 0 before either writes.
|
|
461
|
+
// The write lock serializes them, so re-read inside the transaction: the
|
|
462
|
+
// loser must see the winner's rows and skip instead of replaying DDL.
|
|
463
|
+
if (readUserVersion(db) >= migration.version) {
|
|
464
|
+
db.exec('COMMIT');
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
if (listAppliedMigrations(db).some((row) => Number(row.version) === migration.version)) {
|
|
468
|
+
db.exec('COMMIT');
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
db.exec(migration.sql);
|
|
472
|
+
db.prepare(
|
|
473
|
+
'INSERT INTO schema_migrations(version, name, checksum, applied_at) VALUES (?, ?, ?, ?)',
|
|
474
|
+
).run(migration.version, migration.name, migration.checksum, appliedAt);
|
|
475
|
+
db.exec(`PRAGMA user_version = ${Number(migration.version)}`);
|
|
476
|
+
db.exec('COMMIT');
|
|
477
|
+
} catch (error) {
|
|
478
|
+
try {
|
|
479
|
+
db.exec('ROLLBACK');
|
|
480
|
+
} catch {
|
|
481
|
+
/* the transaction may already be gone; the original error is the finding */
|
|
482
|
+
}
|
|
483
|
+
teamDbFail(
|
|
484
|
+
'E_MIGRATION_FAILED',
|
|
485
|
+
`migration ${migration.version} (${migration.name}) failed and rolled back: ${error.message}`,
|
|
486
|
+
2,
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
count += 1;
|
|
490
|
+
}
|
|
491
|
+
return count;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* FTS5 is a runtime capability, not a schema version. A runtime without it
|
|
496
|
+
* keeps the table untouched (when present) and records `like` in `schema_meta`.
|
|
497
|
+
*/
|
|
498
|
+
export function ensureSearchIndex(db, options = {}) {
|
|
499
|
+
const forced = options.searchMode === SEARCH_MODE_LIKE || options.searchMode === 'like';
|
|
500
|
+
if (forced) {
|
|
501
|
+
writeSchemaMeta(db, 'search', SEARCH_MODE_LIKE);
|
|
502
|
+
return SEARCH_MODE_LIKE;
|
|
503
|
+
}
|
|
504
|
+
try {
|
|
505
|
+
db.exec(
|
|
506
|
+
`CREATE VIRTUAL TABLE IF NOT EXISTS ${SEARCH_TABLE} USING fts5(
|
|
507
|
+
task_slug UNINDEXED,
|
|
508
|
+
doc_path UNINDEXED,
|
|
509
|
+
kind UNINDEXED,
|
|
510
|
+
body,
|
|
511
|
+
tokenize = 'unicode61 remove_diacritics 2'
|
|
512
|
+
)`,
|
|
513
|
+
);
|
|
514
|
+
writeSchemaMeta(db, 'search', SEARCH_MODE_FTS5);
|
|
515
|
+
return SEARCH_MODE_FTS5;
|
|
516
|
+
} catch {
|
|
517
|
+
writeSchemaMeta(db, 'search', SEARCH_MODE_LIKE);
|
|
518
|
+
return SEARCH_MODE_LIKE;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
export function readSearchMode(db) {
|
|
523
|
+
const meta = readSchemaMeta(db, 'search');
|
|
524
|
+
if (!tableExists(db, SEARCH_TABLE)) return SEARCH_MODE_LIKE;
|
|
525
|
+
return meta === SEARCH_MODE_LIKE ? SEARCH_MODE_LIKE : SEARCH_MODE_FTS5;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
export function readSchemaMeta(db, key) {
|
|
529
|
+
if (!tableExists(db, 'schema_meta')) return null;
|
|
530
|
+
const row = db.prepare('SELECT value FROM schema_meta WHERE key = ?').get(key);
|
|
531
|
+
return row ? row.value : null;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
export function writeSchemaMeta(db, key, value) {
|
|
535
|
+
db.prepare(
|
|
536
|
+
'INSERT INTO schema_meta(key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value',
|
|
537
|
+
).run(key, String(value));
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* sha256 over `type|name|sql` for every DDL row, excluding SQLite internals,
|
|
542
|
+
* the search index, and `schema_meta` (its content is capability state, not
|
|
543
|
+
* schema state).
|
|
544
|
+
*/
|
|
545
|
+
export function computeSchemaFingerprint(db) {
|
|
546
|
+
const rows = db
|
|
547
|
+
.prepare(
|
|
548
|
+
`SELECT type, name, sql FROM sqlite_master
|
|
549
|
+
WHERE sql IS NOT NULL
|
|
550
|
+
AND name NOT LIKE 'sqlite_%'
|
|
551
|
+
AND name NOT LIKE '${SEARCH_TABLE}%'
|
|
552
|
+
AND name <> 'schema_meta'
|
|
553
|
+
ORDER BY type, name`,
|
|
554
|
+
)
|
|
555
|
+
.all();
|
|
556
|
+
const digest = crypto.createHash('sha256');
|
|
557
|
+
for (const row of rows) digest.update(`${row.type}|${row.name}|${row.sql}\n`);
|
|
558
|
+
return digest.digest('hex');
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
export function integrityCheck(db) {
|
|
562
|
+
const row = db.prepare('PRAGMA integrity_check').get();
|
|
563
|
+
const value = row ? Object.values(row)[0] : null;
|
|
564
|
+
return value === 'ok';
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
export function sqliteVersion(db) {
|
|
568
|
+
return db.prepare('SELECT sqlite_version() AS version').get()?.version ?? null;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
const chmodIfSupported = (filePath) => {
|
|
572
|
+
if (process.platform === 'win32') return;
|
|
573
|
+
try {
|
|
574
|
+
fs.chmodSync(filePath, 0o600);
|
|
575
|
+
} catch {
|
|
576
|
+
/* the file may have vanished between the write and the chmod */
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
export function chmodDatabaseFiles(dbPath) {
|
|
581
|
+
chmodIfSupported(dbPath);
|
|
582
|
+
chmodIfSupported(`${dbPath}-wal`);
|
|
583
|
+
chmodIfSupported(`${dbPath}-shm`);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const ensureDatabaseFile = (dbPath) => {
|
|
587
|
+
fs.mkdirSync(path.dirname(dbPath), { recursive: true, mode: 0o700 });
|
|
588
|
+
if (fs.existsSync(dbPath)) return;
|
|
589
|
+
const fd = fs.openSync(dbPath, 'a', 0o600);
|
|
590
|
+
fs.closeSync(fd);
|
|
591
|
+
chmodIfSupported(dbPath);
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
export const isCorruptError = (error) => {
|
|
595
|
+
const message = String(error?.message || '');
|
|
596
|
+
return (
|
|
597
|
+
error?.errcode === 26 ||
|
|
598
|
+
error?.errcode === 11 ||
|
|
599
|
+
/not a database|file is encrypted|malformed database|database disk image is malformed/i.test(message)
|
|
600
|
+
);
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
export const isBusyError = (error) => {
|
|
604
|
+
const message = String(error?.message || '');
|
|
605
|
+
return error?.errcode === 5 || error?.errcode === 6 || /SQLITE_BUSY|database is locked/i.test(message);
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
const stamp = () => new Date().toISOString().replace(/[:.]/g, '-');
|
|
609
|
+
|
|
610
|
+
const sleepSync = (ms) => {
|
|
611
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
/**
|
|
615
|
+
* Open + configure with a bounded retry for the first-creation race. Two
|
|
616
|
+
* processes can both create the file; the loser's WAL switch or migration can
|
|
617
|
+
* return SQLITE_BUSY before the in-connection busy handler applies. Retry for
|
|
618
|
+
* five seconds, matching `busy_timeout`, then surface `E_DB_BUSY`.
|
|
619
|
+
*/
|
|
620
|
+
const openConfiguredDatabase = (sqlite, dbPath, readOnly, setWal = false) => {
|
|
621
|
+
let lastError = null;
|
|
622
|
+
const deadline = Date.now() + 5000;
|
|
623
|
+
let attempt = 0;
|
|
624
|
+
while (attempt < 50 && Date.now() < deadline) {
|
|
625
|
+
attempt += 1;
|
|
626
|
+
let db;
|
|
627
|
+
try {
|
|
628
|
+
db = new sqlite.DatabaseSync(dbPath, readOnly ? { readOnly: true } : {});
|
|
629
|
+
configureConnection(db, { setWal: setWal && !readOnly });
|
|
630
|
+
return db;
|
|
631
|
+
} catch (error) {
|
|
632
|
+
lastError = error;
|
|
633
|
+
try {
|
|
634
|
+
db?.close();
|
|
635
|
+
} catch {
|
|
636
|
+
/* the open/configure error is the finding */
|
|
637
|
+
}
|
|
638
|
+
if (!isBusyError(error) || Date.now() >= deadline) throw error;
|
|
639
|
+
sleepSync(100);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
throw lastError;
|
|
643
|
+
};
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Move `team.db` and its companions aside. Returns the new base suffix so the
|
|
647
|
+
* caller can report which recovery artifact exists.
|
|
648
|
+
*/
|
|
649
|
+
export function moveDatabaseAside(dbPath, reason) {
|
|
650
|
+
const suffix = `${reason}-${stamp()}`;
|
|
651
|
+
for (const companion of ['', '-wal', '-shm']) {
|
|
652
|
+
const source = `${dbPath}${companion}`;
|
|
653
|
+
if (!fs.existsSync(source)) continue;
|
|
654
|
+
fs.renameSync(source, `${dbPath}.${suffix}${companion}`);
|
|
655
|
+
}
|
|
656
|
+
return suffix;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Open, migrate, verify and return a connection handle.
|
|
661
|
+
*
|
|
662
|
+
* `rebuild` always moves the file aside and recreates it. `noRebuild` turns a
|
|
663
|
+
* fingerprint mismatch into `E_SCHEMA_MISMATCH` instead of a rebuild. Read-only
|
|
664
|
+
* opens never migrate or rebuild; they fail with a named schema code.
|
|
665
|
+
*/
|
|
666
|
+
export function openTeamDb(options = {}) {
|
|
667
|
+
const sqlite = loadSqlite(options.env ?? process.env);
|
|
668
|
+
if (!sqlite) teamDbFail('E_SQLITE_UNAVAILABLE', `node:sqlite is unavailable; requires Node >= ${MIN_NODE}`, 0);
|
|
669
|
+
|
|
670
|
+
const teamRoot = path.resolve(
|
|
671
|
+
options.teamRoot || process.env.AI_RULES_TEAM_DB_ROOT || DEFAULT_TEAM_ROOT,
|
|
672
|
+
);
|
|
673
|
+
const dbPath = path.resolve(
|
|
674
|
+
options.dbPath ||
|
|
675
|
+
process.env.AI_RULES_TEAM_DB_PATH ||
|
|
676
|
+
path.join(teamRoot, DB_FILE),
|
|
677
|
+
);
|
|
678
|
+
const readOnly = Boolean(options.readOnly);
|
|
679
|
+
const noRebuild = Boolean(options.noRebuild);
|
|
680
|
+
const forceSearch = options.searchMode ?? null;
|
|
681
|
+
|
|
682
|
+
// Verify the code before touching the filesystem: a tampered migration must
|
|
683
|
+
// create nothing, not leave an empty file behind.
|
|
684
|
+
verifyCodeChecksums();
|
|
685
|
+
|
|
686
|
+
if (readOnly) {
|
|
687
|
+
if (!fs.existsSync(dbPath)) {
|
|
688
|
+
teamDbFail('E_DB_MISSING', `${dbPath} does not exist; run "team-db.mjs sync" first`, 2);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
const hadDatabase = fs.existsSync(dbPath);
|
|
693
|
+
if (!readOnly && options.rebuild && hadDatabase) moveDatabaseAside(dbPath, 'corrupt');
|
|
694
|
+
let rebuilt = Boolean(!readOnly && options.rebuild && hadDatabase);
|
|
695
|
+
let mismatchCount = 0;
|
|
696
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
697
|
+
const freshFile = (() => {
|
|
698
|
+
try {
|
|
699
|
+
return fs.statSync(dbPath).size === 0;
|
|
700
|
+
} catch {
|
|
701
|
+
return true;
|
|
702
|
+
}
|
|
703
|
+
})();
|
|
704
|
+
if (!readOnly) ensureDatabaseFile(dbPath);
|
|
705
|
+
let db;
|
|
706
|
+
try {
|
|
707
|
+
db = openConfiguredDatabase(sqlite, dbPath, readOnly, freshFile);
|
|
708
|
+
} catch (error) {
|
|
709
|
+
if (isCorruptError(error) || error?.code === 'EISDIR') {
|
|
710
|
+
// Corruption is never repaired implicitly; `--rebuild` moves the file
|
|
711
|
+
// aside before the open and is the documented recovery command.
|
|
712
|
+
teamDbFail(
|
|
713
|
+
'E_DB_CORRUPT',
|
|
714
|
+
`${dbPath} is not a usable database: ${error.message}; run "team-db.mjs sync --rebuild"`,
|
|
715
|
+
2,
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
if (isBusyError(error)) teamDbFail('E_DB_BUSY', `${dbPath}: ${error.message}`, 2);
|
|
719
|
+
teamDbFail('E_DB_OPEN', `${dbPath}: ${error.message}`, 2);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
try {
|
|
723
|
+
verifyCodeChecksums();
|
|
724
|
+
|
|
725
|
+
const current = readUserVersion(db);
|
|
726
|
+
if (current > SCHEMA_VERSION) {
|
|
727
|
+
teamDbFail('E_SCHEMA_NEWER', `database schema v${current} is newer than code v${SCHEMA_VERSION}`, 2);
|
|
728
|
+
}
|
|
729
|
+
assertNoDrift(listAppliedMigrations(db), MIGRATIONS);
|
|
730
|
+
|
|
731
|
+
if (!readOnly) {
|
|
732
|
+
if (current < SCHEMA_VERSION) applyMigrations(db, MIGRATIONS);
|
|
733
|
+
// FTS5 is created once with the database; re-issuing the virtual-table
|
|
734
|
+
// DDL on every open buys a write lock and can spend the whole busy
|
|
735
|
+
// timeout behind another writer for no state change.
|
|
736
|
+
if (
|
|
737
|
+
forceSearch === SEARCH_MODE_LIKE ||
|
|
738
|
+
!tableExists(db, SEARCH_TABLE) ||
|
|
739
|
+
readSchemaMeta(db, 'search') === null
|
|
740
|
+
) {
|
|
741
|
+
ensureSearchIndex(db, { searchMode: forceSearch });
|
|
742
|
+
}
|
|
743
|
+
} else if (current < SCHEMA_VERSION) {
|
|
744
|
+
teamDbFail('E_SCHEMA_OUTDATED', `database schema v${current} is older than code v${SCHEMA_VERSION}`, 2);
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
const searchMode = readSearchMode(db);
|
|
748
|
+
const fingerprint = computeSchemaFingerprint(db);
|
|
749
|
+
const stored = readSchemaMeta(db, 'fingerprint');
|
|
750
|
+
if (stored && stored !== fingerprint) {
|
|
751
|
+
if (readOnly || noRebuild) {
|
|
752
|
+
teamDbFail(
|
|
753
|
+
'E_SCHEMA_MISMATCH',
|
|
754
|
+
`schema fingerprint ${fingerprint} does not match the stored ${stored}; run "team-db.mjs sync"`,
|
|
755
|
+
2,
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
mismatchCount += 1;
|
|
759
|
+
if (mismatchCount > 1) {
|
|
760
|
+
db.close();
|
|
761
|
+
teamDbFail('E_SCHEMA_REBUILD_FAILED', `${dbPath} still mismatches after a rebuild`, 2);
|
|
762
|
+
}
|
|
763
|
+
db.close();
|
|
764
|
+
moveDatabaseAside(dbPath, 'mismatch');
|
|
765
|
+
rebuilt = true;
|
|
766
|
+
continue;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
if (!readOnly) {
|
|
770
|
+
if (!stored) writeSchemaMeta(db, 'fingerprint', fingerprint);
|
|
771
|
+
if (!readSchemaMeta(db, 'sqlite_version')) writeSchemaMeta(db, 'sqlite_version', sqliteVersion(db));
|
|
772
|
+
if (!readSchemaMeta(db, 'created_at')) writeSchemaMeta(db, 'created_at', new Date().toISOString());
|
|
773
|
+
chmodDatabaseFiles(dbPath);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
return {
|
|
777
|
+
sqlite,
|
|
778
|
+
db,
|
|
779
|
+
teamRoot,
|
|
780
|
+
dbPath,
|
|
781
|
+
readOnly,
|
|
782
|
+
rebuilt,
|
|
783
|
+
schemaVersion: current < SCHEMA_VERSION ? SCHEMA_VERSION : current,
|
|
784
|
+
fingerprint,
|
|
785
|
+
searchMode,
|
|
786
|
+
};
|
|
787
|
+
} catch (error) {
|
|
788
|
+
try {
|
|
789
|
+
db.close();
|
|
790
|
+
} catch {
|
|
791
|
+
/* the open error is the finding */
|
|
792
|
+
}
|
|
793
|
+
if (error instanceof TeamDbError) throw error;
|
|
794
|
+
if (isCorruptError(error)) {
|
|
795
|
+
teamDbFail('E_DB_CORRUPT', `${dbPath} is not a usable database: ${error.message}`, 2);
|
|
796
|
+
}
|
|
797
|
+
if (isBusyError(error)) teamDbFail('E_DB_BUSY', `${dbPath}: ${error.message}`, 2);
|
|
798
|
+
throw error;
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
teamDbFail('E_SCHEMA_REBUILD_FAILED', `${dbPath} could not be rebuilt`, 2);
|
|
802
|
+
return null;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* `check` reports without writing: it opens read-only and runs the stronger
|
|
807
|
+
* `integrity_check` as well as the schema checks `openTeamDb` already makes.
|
|
808
|
+
*/
|
|
809
|
+
export function checkTeamDb(options = {}) {
|
|
810
|
+
const handle = openTeamDb({ ...options, readOnly: true });
|
|
811
|
+
try {
|
|
812
|
+
if (!integrityCheck(handle.db)) {
|
|
813
|
+
teamDbFail('E_DB_CORRUPT', `${handle.dbPath} failed PRAGMA integrity_check`, 2);
|
|
814
|
+
}
|
|
815
|
+
return { ...handle };
|
|
816
|
+
} finally {
|
|
817
|
+
handle.db.close();
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
/** One-line human summary shared by the CLI and fixtures. */
|
|
822
|
+
export function describeHandle(handle) {
|
|
823
|
+
const parts = [
|
|
824
|
+
`schema v${handle.schemaVersion}`,
|
|
825
|
+
`fingerprint ${handle.fingerprint.slice(0, 12)}`,
|
|
826
|
+
`search ${handle.searchMode}`,
|
|
827
|
+
'integrity ok',
|
|
828
|
+
];
|
|
829
|
+
if (handle.rebuilt) parts.push('rebuilt=1');
|
|
830
|
+
return `${handle.dbPath}: ok (${parts.join(', ')})`;
|
|
831
|
+
}
|