@bonesofspring/ai-rules 0.2.64 → 0.2.66
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 +80 -11
- package/README.md +24 -16
- package/bin/lib/engine-scripts.js +52 -12
- package/bin/lib/engine-upgrade.js +5 -2
- package/bin/lib/seed-ai-local-team.js +13 -3
- package/docs/OPENCODE.md +17 -9
- package/docs/PRESET-CONTRIBUTION.md +41 -0
- package/docs/TEAM-DB.md +240 -60
- package/docs/TOKEN-ECONOMY.md +79 -0
- package/docs/openapi/team-db.v1.json +1238 -377
- package/mcp/defectdojo-mcp.mjs +7 -5
- package/mcp/lib/defectdojo.mjs +26 -14
- package/package.json +31 -20
- package/presets/_shared/ai-local/scripts/dump-output.mjs +371 -0
- package/presets/_shared/ai-local/scripts/process-lease.mjs +576 -0
- package/presets/_shared/ai-local/scripts/processes.mjs +1618 -0
- package/presets/_shared/ai-local/scripts/run.mjs +5 -5
- package/presets/_shared/ai-local/scripts/team-db-api.mjs +465 -152
- package/presets/_shared/ai-local/scripts/team-db-dashboard.mjs +235 -10
- package/presets/_shared/ai-local/scripts/team-db-openapi.mjs +416 -176
- package/presets/_shared/ai-local/scripts/team-db-project.mjs +171 -62
- package/presets/_shared/ai-local/scripts/team-db-scheduler.mjs +4 -0
- package/presets/_shared/ai-local/scripts/team-db-schema.mjs +143 -5
- package/presets/_shared/ai-local/scripts/team-db-task-runner.mjs +567 -0
- package/presets/_shared/ai-local/scripts/{team-db-backlog.mjs → team-db-tracker.mjs} +900 -197
- package/presets/_shared/ai-local/scripts/team-db-ui.mjs +750 -0
- package/presets/_shared/ai-local/scripts/team-db.mjs +184 -23
- package/presets/_shared/core/agent-team/agent-team-orchestrator.md +4 -2
- package/presets/_shared/core/architecture/feature-delivery-workflow.md +4 -0
- package/presets/_shared/core/quality/context-and-iteration.md +1 -0
- package/presets/_shared/hooks/chain-team-phases.sh.tmpl +105 -17
- package/presets/_shared/hooks/guard-shell-command.mjs.tmpl +40 -2
- package/presets/claude/android-kotlin/agents/task-analyst.md +1 -1
- package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +105 -17
- package/presets/claude/android-kotlin/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/android-kotlin/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
- package/presets/claude/android-kotlin/settings.json +6 -1
- package/presets/claude/go/agents/task-analyst.md +1 -1
- package/presets/claude/go/hooks/chain-team-phases.sh +105 -17
- package/presets/claude/go/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/go/rules/architecture/feature-delivery.md +4 -0
- package/presets/claude/go/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
- package/presets/claude/go/settings.json +6 -1
- package/presets/claude/ios-swift/agents/task-analyst.md +1 -1
- package/presets/claude/ios-swift/hooks/chain-team-phases.sh +105 -17
- package/presets/claude/ios-swift/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/ios-swift/rules/architecture/feature-delivery.md +4 -0
- package/presets/claude/ios-swift/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
- package/presets/claude/ios-swift/settings.json +6 -1
- package/presets/claude/java/agents/task-analyst.md +1 -1
- package/presets/claude/java/hooks/chain-team-phases.sh +105 -17
- package/presets/claude/java/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/java/rules/architecture/feature-delivery.md +4 -0
- package/presets/claude/java/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
- package/presets/claude/java/settings.json +6 -1
- package/presets/claude/mcp-ts/agents/task-analyst.md +1 -1
- package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +105 -17
- package/presets/claude/mcp-ts/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/mcp-ts/rules/architecture/feature-delivery.md +4 -0
- package/presets/claude/mcp-ts/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
- package/presets/claude/mcp-ts/settings.json +6 -1
- package/presets/claude/mol/agents/task-analyst.md +1 -1
- package/presets/claude/mol/hooks/chain-team-phases.sh +105 -17
- package/presets/claude/mol/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/mol/rules/architecture/feature-delivery-workflow.md +4 -0
- package/presets/claude/mol/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
- package/presets/claude/mol/settings.json +6 -1
- package/presets/claude/next/MCP.md +2 -0
- package/presets/claude/next/REPO_AGENTS.md +2 -0
- package/presets/claude/next/agents/build-verifier.md +1 -1
- package/presets/claude/next/agents/ci-investigator.md +3 -0
- package/presets/claude/next/agents/debugger.md +3 -0
- package/presets/claude/next/agents/feature-developer.md +3 -0
- package/presets/claude/next/agents/mockup-reviewer.md +2 -0
- package/presets/claude/next/agents/performance-auditor.md +2 -0
- package/presets/claude/next/agents/playwright-test-generator.md +3 -0
- package/presets/claude/next/agents/playwright-test-healer.md +3 -0
- package/presets/claude/next/agents/playwright-test-planner.md +3 -0
- package/presets/claude/next/agents/qa-tester.md +3 -0
- package/presets/claude/next/agents/task-analyst.md +1 -1
- package/presets/claude/next/commands/README.md +7 -1
- package/presets/claude/next/commands/task.md +1 -1
- package/presets/claude/next/commands/ui-start.md +78 -0
- package/presets/claude/next/commands/ui-stop.md +71 -0
- package/presets/claude/next/hooks/chain-team-phases.sh +105 -17
- package/presets/claude/next/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/next/rules/README.md +2 -1
- package/presets/claude/next/rules/architecture/feature-delivery-workflow.md +4 -0
- package/presets/claude/next/rules/stack/next-app-core.md +1 -0
- package/presets/claude/next/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
- package/presets/claude/next/rules/tooling-and-review/dev-process-lifecycle.md +43 -0
- package/presets/claude/next/settings.json +6 -1
- package/presets/claude/next/team/README.md +7 -3
- package/presets/claude/nuxt/agents/task-analyst.md +1 -1
- package/presets/claude/nuxt/hooks/chain-team-phases.sh +105 -17
- package/presets/claude/nuxt/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/nuxt/rules/architecture/feature-delivery-workflow.md +4 -0
- package/presets/claude/nuxt/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
- package/presets/claude/nuxt/settings.json +6 -1
- package/presets/claude/php-hexagonal/agents/task-analyst.md +1 -1
- package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +105 -17
- package/presets/claude/php-hexagonal/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/php-hexagonal/rules/architecture/feature-delivery.md +4 -0
- package/presets/claude/php-hexagonal/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
- package/presets/claude/php-hexagonal/settings.json +6 -1
- package/presets/claude/php-laravel/agents/task-analyst.md +1 -1
- package/presets/claude/php-laravel/hooks/chain-team-phases.sh +106 -18
- package/presets/claude/php-laravel/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/php-laravel/rules/architecture/feature-delivery.md +4 -0
- package/presets/claude/php-laravel/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
- package/presets/claude/php-laravel/settings.json +6 -1
- package/presets/claude/svelte/agents/task-analyst.md +1 -1
- package/presets/claude/svelte/hooks/chain-team-phases.sh +105 -17
- package/presets/claude/svelte/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/svelte/rules/architecture/feature-delivery-workflow.md +4 -0
- package/presets/claude/svelte/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
- package/presets/claude/svelte/settings.json +6 -1
- package/presets/cursor/android-kotlin/agents/task-analyst.md +1 -1
- package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +105 -17
- package/presets/cursor/android-kotlin/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/android-kotlin/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/cursor/go/agents/task-analyst.md +1 -1
- package/presets/cursor/go/hooks/chain-team-phases.sh +105 -17
- package/presets/cursor/go/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/go/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/cursor/ios-swift/agents/task-analyst.md +1 -1
- package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +105 -17
- package/presets/cursor/ios-swift/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/ios-swift/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/cursor/java/agents/task-analyst.md +1 -1
- package/presets/cursor/java/hooks/chain-team-phases.sh +105 -17
- package/presets/cursor/java/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/java/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/cursor/mcp-ts/agents/task-analyst.md +1 -1
- package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +105 -17
- package/presets/cursor/mcp-ts/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/mcp-ts/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/cursor/mol/agents/task-analyst.md +1 -1
- package/presets/cursor/mol/hooks/chain-team-phases.sh +105 -17
- package/presets/cursor/mol/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/mol/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/cursor/next/MCP.md +2 -0
- package/presets/cursor/next/REPO_AGENTS.md +2 -0
- package/presets/cursor/next/agents/build-verifier.md +1 -1
- package/presets/cursor/next/agents/ci-investigator.md +2 -0
- package/presets/cursor/next/agents/debugger.md +2 -0
- package/presets/cursor/next/agents/feature-developer.md +2 -0
- package/presets/cursor/next/agents/mockup-reviewer.md +2 -0
- package/presets/cursor/next/agents/performance-auditor.md +2 -0
- package/presets/cursor/next/agents/playwright-test-generator.md +2 -0
- package/presets/cursor/next/agents/playwright-test-healer.md +2 -0
- package/presets/cursor/next/agents/playwright-test-planner.md +2 -0
- package/presets/cursor/next/agents/qa-tester.md +2 -0
- package/presets/cursor/next/agents/task-analyst.md +1 -1
- package/presets/cursor/next/ai-local/rules-library/storybook-dev-compare-figma.md +1 -1
- package/presets/cursor/next/commands/README.md +7 -1
- package/presets/cursor/next/commands/task.md +1 -1
- package/presets/cursor/next/commands/ui-start.md +78 -0
- package/presets/cursor/next/commands/ui-stop.md +71 -0
- package/presets/cursor/next/hooks/chain-team-phases.sh +105 -17
- package/presets/cursor/next/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/next/rules/README.md +2 -1
- package/presets/cursor/next/rules/agent-team-orchestrator.mdc +3 -1
- package/presets/cursor/next/rules/dev-process-lifecycle.mdc +43 -0
- package/presets/cursor/next/rules/next-app-core.mdc +1 -0
- package/presets/cursor/next/team/README.md +7 -3
- package/presets/cursor/nuxt/agents/task-analyst.md +1 -1
- package/presets/cursor/nuxt/hooks/chain-team-phases.sh +105 -17
- package/presets/cursor/nuxt/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/nuxt/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/cursor/php-hexagonal/agents/task-analyst.md +1 -1
- package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +105 -17
- package/presets/cursor/php-hexagonal/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/php-hexagonal/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/cursor/php-laravel/agents/task-analyst.md +1 -1
- package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +106 -18
- package/presets/cursor/php-laravel/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/php-laravel/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/cursor/svelte/agents/task-analyst.md +1 -1
- package/presets/cursor/svelte/hooks/chain-team-phases.sh +105 -17
- package/presets/cursor/svelte/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/svelte/rules/agent-team-orchestrator.mdc +1 -1
- package/presets/mavis/android-kotlin/adapter-map.json +1 -1
- package/presets/mavis/android-kotlin/agents/task-analyst.md +1 -1
- package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +105 -17
- package/presets/mavis/android-kotlin/rules/agent-team-orchestrator.md +4 -2
- package/presets/mavis/go/adapter-map.json +1 -1
- package/presets/mavis/go/agents/task-analyst.md +1 -1
- package/presets/mavis/go/hooks/chain-team-phases.sh +105 -17
- package/presets/mavis/go/rules/agent-team-orchestrator.md +4 -2
- package/presets/mavis/go/rules/feature-delivery-workflow.md +4 -0
- package/presets/mavis/ios-swift/adapter-map.json +1 -1
- package/presets/mavis/ios-swift/agents/task-analyst.md +1 -1
- package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +105 -17
- package/presets/mavis/ios-swift/rules/agent-team-orchestrator.md +4 -2
- package/presets/mavis/ios-swift/rules/feature-delivery-workflow.md +4 -0
- package/presets/mavis/java/adapter-map.json +1 -1
- package/presets/mavis/java/agents/task-analyst.md +1 -1
- package/presets/mavis/java/hooks/chain-team-phases.sh +105 -17
- package/presets/mavis/java/rules/agent-team-orchestrator.md +4 -2
- package/presets/mavis/java/rules/feature-delivery-workflow.md +4 -0
- package/presets/mavis/mcp-ts/adapter-map.json +1 -1
- package/presets/mavis/mcp-ts/agents/task-analyst.md +1 -1
- package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +105 -17
- package/presets/mavis/mcp-ts/rules/agent-team-orchestrator.md +4 -2
- package/presets/mavis/mcp-ts/rules/feature-delivery-workflow.md +4 -0
- package/presets/mavis/mol/adapter-map.json +1 -1
- package/presets/mavis/mol/agents/task-analyst.md +1 -1
- package/presets/mavis/mol/hooks/chain-team-phases.sh +105 -17
- package/presets/mavis/mol/rules/agent-team-orchestrator.md +4 -2
- package/presets/mavis/mol/rules/feature-delivery-workflow.md +4 -0
- package/presets/mavis/next/MCP.md +2 -0
- package/presets/mavis/next/REPO_AGENTS.md +2 -0
- package/presets/mavis/next/adapter-map.json +33 -1
- package/presets/mavis/next/agents/build-verifier.md +1 -1
- package/presets/mavis/next/agents/ci-investigator.md +3 -0
- package/presets/mavis/next/agents/debugger.md +3 -0
- package/presets/mavis/next/agents/feature-developer.md +3 -0
- package/presets/mavis/next/agents/mockup-reviewer.md +2 -0
- package/presets/mavis/next/agents/performance-auditor.md +2 -0
- package/presets/mavis/next/agents/playwright-test-generator.md +3 -0
- package/presets/mavis/next/agents/playwright-test-healer.md +3 -0
- package/presets/mavis/next/agents/playwright-test-planner.md +3 -0
- package/presets/mavis/next/agents/qa-tester.md +3 -0
- package/presets/mavis/next/agents/task-analyst.md +1 -1
- package/presets/mavis/next/ai-local/rules-library/storybook-dev-compare-figma.md +1 -1
- package/presets/mavis/next/commands/README.md +7 -1
- package/presets/mavis/next/commands/task.md +1 -1
- package/presets/mavis/next/commands/ui-start.md +73 -0
- package/presets/mavis/next/commands/ui-stop.md +66 -0
- package/presets/mavis/next/hooks/chain-team-phases.sh +105 -17
- package/presets/mavis/next/rules/agent-team-orchestrator.md +4 -2
- package/presets/mavis/next/rules/dev-process-lifecycle.md +38 -0
- package/presets/mavis/next/rules/feature-delivery-workflow.md +4 -0
- package/presets/mavis/next/rules/next-app-core.md +1 -0
- package/presets/mavis/next/team/README.md +7 -3
- package/presets/mavis/nuxt/adapter-map.json +1 -1
- package/presets/mavis/nuxt/agents/task-analyst.md +1 -1
- package/presets/mavis/nuxt/hooks/chain-team-phases.sh +105 -17
- package/presets/mavis/nuxt/rules/agent-team-orchestrator.md +4 -2
- package/presets/mavis/nuxt/rules/feature-delivery-workflow.md +4 -0
- package/presets/mavis/php-hexagonal/adapter-map.json +1 -1
- package/presets/mavis/php-hexagonal/agents/task-analyst.md +1 -1
- package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +105 -17
- package/presets/mavis/php-hexagonal/rules/agent-team-orchestrator.md +4 -2
- package/presets/mavis/php-hexagonal/rules/feature-delivery-workflow.md +4 -0
- package/presets/mavis/php-laravel/adapter-map.json +1 -1
- package/presets/mavis/php-laravel/agents/task-analyst.md +1 -1
- package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +106 -18
- package/presets/mavis/php-laravel/rules/agent-team-orchestrator.md +4 -2
- package/presets/mavis/php-laravel/rules/feature-delivery-workflow.md +4 -0
- package/presets/mavis/svelte/adapter-map.json +1 -1
- package/presets/mavis/svelte/agents/task-analyst.md +1 -1
- package/presets/mavis/svelte/hooks/chain-team-phases.sh +105 -17
- package/presets/mavis/svelte/rules/agent-team-orchestrator.md +4 -2
- package/presets/mavis/svelte/rules/feature-delivery-workflow.md +4 -0
- package/presets/opencode/android-kotlin/adapter-map.json +1 -1
- package/presets/opencode/android-kotlin/agents/task-analyst.md +1 -1
- package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +105 -17
- package/presets/opencode/android-kotlin/opencode.json +5 -1
- package/presets/opencode/android-kotlin/rules/agent-team-orchestrator.md +4 -2
- package/presets/opencode/go/adapter-map.json +1 -1
- package/presets/opencode/go/agents/task-analyst.md +1 -1
- package/presets/opencode/go/hooks/chain-team-phases.sh +105 -17
- package/presets/opencode/go/opencode.json +5 -1
- package/presets/opencode/go/rules/agent-team-orchestrator.md +4 -2
- package/presets/opencode/go/rules/feature-delivery-workflow.md +4 -0
- package/presets/opencode/ios-swift/adapter-map.json +1 -1
- package/presets/opencode/ios-swift/agents/task-analyst.md +1 -1
- package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +105 -17
- package/presets/opencode/ios-swift/opencode.json +5 -1
- package/presets/opencode/ios-swift/rules/agent-team-orchestrator.md +4 -2
- package/presets/opencode/ios-swift/rules/feature-delivery-workflow.md +4 -0
- package/presets/opencode/java/adapter-map.json +1 -1
- package/presets/opencode/java/agents/task-analyst.md +1 -1
- package/presets/opencode/java/hooks/chain-team-phases.sh +105 -17
- package/presets/opencode/java/opencode.json +5 -1
- package/presets/opencode/java/rules/agent-team-orchestrator.md +4 -2
- package/presets/opencode/java/rules/feature-delivery-workflow.md +4 -0
- package/presets/opencode/mcp-ts/adapter-map.json +1 -1
- package/presets/opencode/mcp-ts/agents/task-analyst.md +1 -1
- package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +105 -17
- package/presets/opencode/mcp-ts/opencode.json +5 -1
- package/presets/opencode/mcp-ts/rules/agent-team-orchestrator.md +4 -2
- package/presets/opencode/mcp-ts/rules/feature-delivery-workflow.md +4 -0
- package/presets/opencode/mol/adapter-map.json +1 -1
- package/presets/opencode/mol/agents/task-analyst.md +1 -1
- package/presets/opencode/mol/hooks/chain-team-phases.sh +105 -17
- package/presets/opencode/mol/opencode.json +4 -0
- package/presets/opencode/mol/rules/agent-team-orchestrator.md +4 -2
- package/presets/opencode/mol/rules/feature-delivery-workflow.md +4 -0
- package/presets/opencode/next/MCP.md +2 -0
- package/presets/opencode/next/REPO_AGENTS.md +2 -0
- package/presets/opencode/next/adapter-map.json +33 -1
- package/presets/opencode/next/agents/build-verifier.md +1 -1
- package/presets/opencode/next/agents/ci-investigator.md +3 -0
- package/presets/opencode/next/agents/debugger.md +3 -0
- package/presets/opencode/next/agents/feature-developer.md +3 -0
- package/presets/opencode/next/agents/mockup-reviewer.md +2 -0
- package/presets/opencode/next/agents/performance-auditor.md +2 -0
- package/presets/opencode/next/agents/playwright-test-generator.md +3 -0
- package/presets/opencode/next/agents/playwright-test-healer.md +3 -0
- package/presets/opencode/next/agents/playwright-test-planner.md +3 -0
- package/presets/opencode/next/agents/qa-tester.md +3 -0
- package/presets/opencode/next/agents/task-analyst.md +1 -1
- package/presets/opencode/next/ai-local/rules-library/storybook-dev-compare-figma.md +1 -1
- package/presets/opencode/next/commands/README.md +7 -1
- package/presets/opencode/next/commands/task.md +1 -1
- package/presets/opencode/next/commands/ui-start.md +81 -0
- package/presets/opencode/next/commands/ui-stop.md +74 -0
- package/presets/opencode/next/hooks/chain-team-phases.sh +105 -17
- package/presets/opencode/next/opencode.json +4 -0
- package/presets/opencode/next/rules/agent-team-orchestrator.md +4 -2
- package/presets/opencode/next/rules/dev-process-lifecycle.md +38 -0
- package/presets/opencode/next/rules/feature-delivery-workflow.md +4 -0
- package/presets/opencode/next/rules/next-app-core.md +1 -0
- package/presets/opencode/next/team/README.md +7 -3
- package/presets/opencode/nuxt/adapter-map.json +1 -1
- package/presets/opencode/nuxt/agents/task-analyst.md +1 -1
- package/presets/opencode/nuxt/hooks/chain-team-phases.sh +105 -17
- package/presets/opencode/nuxt/opencode.json +4 -0
- package/presets/opencode/nuxt/rules/agent-team-orchestrator.md +4 -2
- package/presets/opencode/nuxt/rules/feature-delivery-workflow.md +4 -0
- package/presets/opencode/php-hexagonal/adapter-map.json +1 -1
- package/presets/opencode/php-hexagonal/agents/task-analyst.md +1 -1
- package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +105 -17
- package/presets/opencode/php-hexagonal/opencode.json +5 -1
- package/presets/opencode/php-hexagonal/rules/agent-team-orchestrator.md +4 -2
- package/presets/opencode/php-hexagonal/rules/feature-delivery-workflow.md +4 -0
- package/presets/opencode/php-laravel/adapter-map.json +1 -1
- package/presets/opencode/php-laravel/agents/task-analyst.md +1 -1
- package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +106 -18
- package/presets/opencode/php-laravel/opencode.json +5 -1
- package/presets/opencode/php-laravel/rules/agent-team-orchestrator.md +4 -2
- package/presets/opencode/php-laravel/rules/feature-delivery-workflow.md +4 -0
- package/presets/opencode/plugins/task-status/README.md +23 -11
- package/presets/opencode/plugins/task-status/details-data.ts +64 -46
- package/presets/opencode/plugins/task-status/details.tsx +12 -2
- package/presets/opencode/plugins/task-status/format.ts +16 -0
- package/presets/opencode/plugins/task-status/prompt-text.ts +37 -0
- package/presets/opencode/plugins/task-status/task-view.ts +241 -177
- package/presets/opencode/plugins/task-status/tui.tsx +57 -15
- package/presets/opencode/plugins/task-status/types.ts +74 -0
- package/presets/opencode/svelte/adapter-map.json +1 -1
- package/presets/opencode/svelte/agents/task-analyst.md +1 -1
- package/presets/opencode/svelte/hooks/chain-team-phases.sh +105 -17
- package/presets/opencode/svelte/opencode.json +4 -0
- package/presets/opencode/svelte/rules/agent-team-orchestrator.md +4 -2
- package/presets/opencode/svelte/rules/feature-delivery-workflow.md +4 -0
- package/presets/zcode/android-kotlin/adapter-map.json +1 -1
- package/presets/zcode/android-kotlin/agents/task-analyst.md +1 -1
- package/presets/zcode/android-kotlin/hooks/chain-team-phases.sh +105 -17
- package/presets/zcode/android-kotlin/rules/agent-team-orchestrator.md +4 -2
- package/presets/zcode/go/adapter-map.json +1 -1
- package/presets/zcode/go/agents/task-analyst.md +1 -1
- package/presets/zcode/go/hooks/chain-team-phases.sh +105 -17
- package/presets/zcode/go/rules/agent-team-orchestrator.md +4 -2
- package/presets/zcode/go/rules/feature-delivery-workflow.md +4 -0
- package/presets/zcode/ios-swift/adapter-map.json +1 -1
- package/presets/zcode/ios-swift/agents/task-analyst.md +1 -1
- package/presets/zcode/ios-swift/hooks/chain-team-phases.sh +105 -17
- package/presets/zcode/ios-swift/rules/agent-team-orchestrator.md +4 -2
- package/presets/zcode/ios-swift/rules/feature-delivery-workflow.md +4 -0
- package/presets/zcode/java/adapter-map.json +1 -1
- package/presets/zcode/java/agents/task-analyst.md +1 -1
- package/presets/zcode/java/hooks/chain-team-phases.sh +105 -17
- package/presets/zcode/java/rules/agent-team-orchestrator.md +4 -2
- package/presets/zcode/java/rules/feature-delivery-workflow.md +4 -0
- package/presets/zcode/mcp-ts/adapter-map.json +1 -1
- package/presets/zcode/mcp-ts/agents/task-analyst.md +1 -1
- package/presets/zcode/mcp-ts/hooks/chain-team-phases.sh +105 -17
- package/presets/zcode/mcp-ts/rules/agent-team-orchestrator.md +4 -2
- package/presets/zcode/mcp-ts/rules/feature-delivery-workflow.md +4 -0
- package/presets/zcode/mol/adapter-map.json +1 -1
- package/presets/zcode/mol/agents/task-analyst.md +1 -1
- package/presets/zcode/mol/hooks/chain-team-phases.sh +105 -17
- package/presets/zcode/mol/rules/agent-team-orchestrator.md +4 -2
- package/presets/zcode/mol/rules/feature-delivery-workflow.md +4 -0
- package/presets/zcode/next/MCP.md +2 -0
- package/presets/zcode/next/REPO_AGENTS.md +2 -0
- package/presets/zcode/next/adapter-map.json +33 -1
- package/presets/zcode/next/agents/build-verifier.md +1 -1
- package/presets/zcode/next/agents/ci-investigator.md +3 -0
- package/presets/zcode/next/agents/debugger.md +3 -0
- package/presets/zcode/next/agents/feature-developer.md +3 -0
- package/presets/zcode/next/agents/mockup-reviewer.md +2 -0
- package/presets/zcode/next/agents/performance-auditor.md +2 -0
- package/presets/zcode/next/agents/playwright-test-generator.md +3 -0
- package/presets/zcode/next/agents/playwright-test-healer.md +3 -0
- package/presets/zcode/next/agents/playwright-test-planner.md +3 -0
- package/presets/zcode/next/agents/qa-tester.md +3 -0
- package/presets/zcode/next/agents/task-analyst.md +1 -1
- package/presets/zcode/next/ai-local/rules-library/storybook-dev-compare-figma.md +1 -1
- package/presets/zcode/next/commands/README.md +7 -1
- package/presets/zcode/next/commands/task.md +1 -1
- package/presets/zcode/next/commands/ui-start.md +81 -0
- package/presets/zcode/next/commands/ui-stop.md +74 -0
- package/presets/zcode/next/hooks/chain-team-phases.sh +105 -17
- package/presets/zcode/next/rules/agent-team-orchestrator.md +4 -2
- package/presets/zcode/next/rules/dev-process-lifecycle.md +38 -0
- package/presets/zcode/next/rules/feature-delivery-workflow.md +4 -0
- package/presets/zcode/next/rules/next-app-core.md +1 -0
- package/presets/zcode/next/team/README.md +7 -3
- package/presets/zcode/nuxt/adapter-map.json +1 -1
- package/presets/zcode/nuxt/agents/task-analyst.md +1 -1
- package/presets/zcode/nuxt/hooks/chain-team-phases.sh +105 -17
- package/presets/zcode/nuxt/rules/agent-team-orchestrator.md +4 -2
- package/presets/zcode/nuxt/rules/feature-delivery-workflow.md +4 -0
- package/presets/zcode/php-hexagonal/adapter-map.json +1 -1
- package/presets/zcode/php-hexagonal/agents/task-analyst.md +1 -1
- package/presets/zcode/php-hexagonal/hooks/chain-team-phases.sh +105 -17
- package/presets/zcode/php-hexagonal/rules/agent-team-orchestrator.md +4 -2
- package/presets/zcode/php-hexagonal/rules/feature-delivery-workflow.md +4 -0
- package/presets/zcode/php-laravel/adapter-map.json +1 -1
- package/presets/zcode/php-laravel/agents/task-analyst.md +1 -1
- package/presets/zcode/php-laravel/hooks/chain-team-phases.sh +106 -18
- package/presets/zcode/php-laravel/rules/agent-team-orchestrator.md +4 -2
- package/presets/zcode/php-laravel/rules/feature-delivery-workflow.md +4 -0
- package/presets/zcode/svelte/adapter-map.json +1 -1
- package/presets/zcode/svelte/agents/task-analyst.md +1 -1
- package/presets/zcode/svelte/hooks/chain-team-phases.sh +105 -17
- package/presets/zcode/svelte/rules/agent-team-orchestrator.md +4 -2
- package/presets/zcode/svelte/rules/feature-delivery-workflow.md +4 -0
- package/presets/_shared/ai-local/scripts/upstream-research.mjs +0 -2964
- package/presets/claude/next/commands/upstream-research.md +0 -23
- package/presets/claude/next/team/upstream/config.json +0 -31
- package/presets/claude/next/team/upstream/digest-template.md +0 -55
- package/presets/claude/next/team/upstream/jobs.json +0 -34
- package/presets/claude/next/team/upstream/opencode.json +0 -34
- package/presets/claude/next/team/upstream/projects.json +0 -4
- package/presets/claude/next/team/upstream/prompt.md +0 -77
- package/presets/claude/next/team/upstream/signatures.json +0 -9
- package/presets/cursor/next/commands/upstream-research.md +0 -23
- package/presets/cursor/next/team/upstream/config.json +0 -31
- package/presets/cursor/next/team/upstream/digest-template.md +0 -55
- package/presets/cursor/next/team/upstream/jobs.json +0 -34
- package/presets/cursor/next/team/upstream/opencode.json +0 -34
- package/presets/cursor/next/team/upstream/projects.json +0 -4
- package/presets/cursor/next/team/upstream/prompt.md +0 -77
- package/presets/cursor/next/team/upstream/signatures.json +0 -9
- package/presets/mavis/next/commands/upstream-research.md +0 -18
- package/presets/mavis/next/team/upstream/config.json +0 -31
- package/presets/mavis/next/team/upstream/digest-template.md +0 -55
- package/presets/mavis/next/team/upstream/jobs.json +0 -34
- package/presets/mavis/next/team/upstream/opencode.json +0 -34
- package/presets/mavis/next/team/upstream/projects.json +0 -4
- package/presets/mavis/next/team/upstream/prompt.md +0 -77
- package/presets/mavis/next/team/upstream/signatures.json +0 -9
- package/presets/opencode/next/commands/upstream-research.md +0 -26
- package/presets/opencode/next/team/upstream/config.json +0 -31
- package/presets/opencode/next/team/upstream/digest-template.md +0 -55
- package/presets/opencode/next/team/upstream/jobs.json +0 -34
- package/presets/opencode/next/team/upstream/opencode.json +0 -34
- package/presets/opencode/next/team/upstream/projects.json +0 -4
- package/presets/opencode/next/team/upstream/prompt.md +0 -77
- package/presets/opencode/next/team/upstream/signatures.json +0 -9
- package/presets/zcode/next/commands/upstream-research.md +0 -26
- package/presets/zcode/next/team/upstream/config.json +0 -31
- package/presets/zcode/next/team/upstream/digest-template.md +0 -55
- package/presets/zcode/next/team/upstream/jobs.json +0 -34
- package/presets/zcode/next/team/upstream/opencode.json +0 -34
- package/presets/zcode/next/team/upstream/projects.json +0 -4
- package/presets/zcode/next/team/upstream/prompt.md +0 -77
- package/presets/zcode/next/team/upstream/signatures.json +0 -9
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process leases: one JSON file per named resource, one holder at a time.
|
|
3
|
+
*
|
|
4
|
+
* State lives in `<aiLocalRoot>/.processes/`: `leases/<name>.json`, an
|
|
5
|
+
* append-only `events.jsonl`, and a `.reclaim/` guard directory per lease that
|
|
6
|
+
* was judged stale. There is no daemon — the files are the truth and a crash
|
|
7
|
+
* leaves a lease the next acquire can reclaim.
|
|
8
|
+
*
|
|
9
|
+
* A lease is taken with `openSync(path, 'wx')`, so exactly one contender wins
|
|
10
|
+
* the create and no separate write window exists. A holder is *live* when its
|
|
11
|
+
* pid is alive **and** the recorded start time matches the live one; a
|
|
12
|
+
* different start time means the pid was reused, so the lease is stale. A
|
|
13
|
+
* stale lease is reclaimed inside `.reclaim/<name>`: `mkdir` is the
|
|
14
|
+
* serialisation point, the lease is re-read after the guard is taken, and only
|
|
15
|
+
* a still-stale file is unlinked. A heartbeat never makes a live holder stale.
|
|
16
|
+
*
|
|
17
|
+
* Every acquire result carries `fatal` and `code` before `skipped`: `skipped`
|
|
18
|
+
* is `true` only when the caller opted in with `allowUnlocked`.
|
|
19
|
+
*/
|
|
20
|
+
import crypto from 'node:crypto';
|
|
21
|
+
import fs from 'node:fs';
|
|
22
|
+
import path from 'node:path';
|
|
23
|
+
import { spawnSync } from 'node:child_process';
|
|
24
|
+
|
|
25
|
+
export const LEASE_SCHEMA_VERSION = 1;
|
|
26
|
+
|
|
27
|
+
export const PROCESSES_DIR_NAME = '.processes';
|
|
28
|
+
|
|
29
|
+
/** Poll interval for `waitMs`; the engine lock's busy-wait precedent. */
|
|
30
|
+
export const DEFAULT_POLL_MS = 500;
|
|
31
|
+
|
|
32
|
+
const LEASES_DIR_NAME = 'leases';
|
|
33
|
+
const RECLAIM_DIR_NAME = '.reclaim';
|
|
34
|
+
const EVENT_FILE_NAME = 'events.jsonl';
|
|
35
|
+
const EVENT_MAX_BYTES = 512;
|
|
36
|
+
/** A guard left by a killed reclaimer only delays the next one. */
|
|
37
|
+
const GUARD_STALE_MS = 30000;
|
|
38
|
+
/**
|
|
39
|
+
* An unparseable lease is either a crash-truncated file or a writer between
|
|
40
|
+
* `open` and `write`; it is reclaimable only after this window, so a create in
|
|
41
|
+
* flight is never stolen.
|
|
42
|
+
*/
|
|
43
|
+
const LEASE_WARM_MS = 5000;
|
|
44
|
+
const NAME_PATTERN = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
|
45
|
+
const KINDS = new Set(['exclusive', 'port', 'advisory']);
|
|
46
|
+
|
|
47
|
+
const newToken = () => crypto.randomBytes(12).toString('hex');
|
|
48
|
+
|
|
49
|
+
const isRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
50
|
+
|
|
51
|
+
const normalize = (value) => String(value).replace(/\s+/g, ' ').trim();
|
|
52
|
+
|
|
53
|
+
/** Synchronous sleep: `acquireLease` has a synchronous contract. */
|
|
54
|
+
function sleepSync(ms) {
|
|
55
|
+
if (ms > 0) {
|
|
56
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/* ----------------------------------- paths -------------------------------- */
|
|
61
|
+
|
|
62
|
+
export const processesRoot = (aiLocalRoot) => path.join(aiLocalRoot, PROCESSES_DIR_NAME);
|
|
63
|
+
|
|
64
|
+
export const leasesDir = (aiLocalRoot) => path.join(processesRoot(aiLocalRoot), LEASES_DIR_NAME);
|
|
65
|
+
|
|
66
|
+
export const reclaimDir = (aiLocalRoot) => path.join(processesRoot(aiLocalRoot), RECLAIM_DIR_NAME);
|
|
67
|
+
|
|
68
|
+
export const leasePath = (aiLocalRoot, name) => path.join(leasesDir(aiLocalRoot), `${name}.json`);
|
|
69
|
+
|
|
70
|
+
export const eventsFile = (aiLocalRoot) => path.join(processesRoot(aiLocalRoot), EVENT_FILE_NAME);
|
|
71
|
+
|
|
72
|
+
export const isValidLeaseName = (name) => typeof name === 'string' && NAME_PATTERN.test(name);
|
|
73
|
+
|
|
74
|
+
/* --------------------------------- process -------------------------------- */
|
|
75
|
+
|
|
76
|
+
/** True when the OS still knows the pid; EPERM means alive but not ours. */
|
|
77
|
+
export function isPidAlive(pid) {
|
|
78
|
+
if (!Number.isInteger(pid) || pid <= 0) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
process.kill(pid, 0);
|
|
83
|
+
return true;
|
|
84
|
+
} catch (error) {
|
|
85
|
+
return error instanceof Error && error.code === 'EPERM';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Linux fallback for a host without `ps`: field 22 of `/proc/<pid>/stat` is the
|
|
91
|
+
* boot-relative start time, which is stable for the life of the process. The
|
|
92
|
+
* `proc:` prefix keeps it distinguishable from a `ps -o lstart=` value, so the
|
|
93
|
+
* two formats are never compared against each other.
|
|
94
|
+
*/
|
|
95
|
+
function procStartTime(pid) {
|
|
96
|
+
try {
|
|
97
|
+
const stat = fs.readFileSync(`/proc/${pid}/stat`, 'utf8');
|
|
98
|
+
const close = stat.lastIndexOf(')');
|
|
99
|
+
if (close < 0) return null;
|
|
100
|
+
const fields = stat.slice(close + 2).trim().split(/\s+/);
|
|
101
|
+
const startTime = fields[19];
|
|
102
|
+
return startTime ? `proc:${startTime}` : null;
|
|
103
|
+
} catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The process start time, or `null` when the platform cannot report it
|
|
110
|
+
* (Windows has no `ps`). Callers degrade to pid-only liveness and say so; they
|
|
111
|
+
* never treat "unknown" as "matches".
|
|
112
|
+
*/
|
|
113
|
+
export function processStartTime(pid) {
|
|
114
|
+
if (!Number.isInteger(pid) || pid <= 0) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
const result = spawnSync('ps', ['-o', 'lstart=', '-p', String(pid)], {
|
|
118
|
+
encoding: 'utf8',
|
|
119
|
+
// `ps` localises `lstart`; a lease written under one locale must compare
|
|
120
|
+
// equal under another, or a live holder reads as stale.
|
|
121
|
+
env: { ...process.env, LC_ALL: 'C', LC_TIME: 'C' },
|
|
122
|
+
});
|
|
123
|
+
if (result.error) {
|
|
124
|
+
// `ps` itself is missing, not the pid: only then is the /proc shape safe to
|
|
125
|
+
// mix into a comparison.
|
|
126
|
+
return procStartTime(pid);
|
|
127
|
+
}
|
|
128
|
+
if (result.status !== 0) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
const value = String(result.stdout ?? '').trim();
|
|
132
|
+
return value ? value : null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/* ---------------------------------- lease --------------------------------- */
|
|
136
|
+
|
|
137
|
+
export function readLease(aiLocalRoot, name) {
|
|
138
|
+
try {
|
|
139
|
+
const parsed = JSON.parse(fs.readFileSync(leasePath(aiLocalRoot, name), 'utf8'));
|
|
140
|
+
return isRecord(parsed) ? parsed : null;
|
|
141
|
+
} catch {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function listLeases(aiLocalRoot) {
|
|
147
|
+
const dir = leasesDir(aiLocalRoot);
|
|
148
|
+
let entries;
|
|
149
|
+
try {
|
|
150
|
+
entries = fs.readdirSync(dir);
|
|
151
|
+
} catch {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
return entries
|
|
155
|
+
.filter((file) => file.endsWith('.json'))
|
|
156
|
+
.sort()
|
|
157
|
+
.map((file) => {
|
|
158
|
+
const name = file.slice(0, -'.json'.length);
|
|
159
|
+
return { name, file: path.join(dir, file), record: readLease(aiLocalRoot, name) };
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Judge one lease. `startTime` is injectable so a caller (or a fixture) can
|
|
165
|
+
* prove its own view without a second `ps` call.
|
|
166
|
+
*
|
|
167
|
+
* @returns {{ state: 'live'|'stale'|'invalid', why: string, alive?: boolean, recordedPidStart?: string|null, livePidStart?: string|null }}
|
|
168
|
+
*/
|
|
169
|
+
export function leaseState(record, { startTime = processStartTime } = {}) {
|
|
170
|
+
const owner = isRecord(record) ? record.owner : null;
|
|
171
|
+
if (!isRecord(owner) || !Number.isInteger(owner.pid) || owner.pid <= 0) {
|
|
172
|
+
return { state: 'invalid', why: 'no-owner' };
|
|
173
|
+
}
|
|
174
|
+
if (!isPidAlive(owner.pid)) {
|
|
175
|
+
return { state: 'stale', why: 'pid-dead', alive: false, recordedPidStart: owner.pidStart ?? null };
|
|
176
|
+
}
|
|
177
|
+
if (owner.pidStart) {
|
|
178
|
+
const live = startTime(owner.pid);
|
|
179
|
+
if (live === null) {
|
|
180
|
+
return {
|
|
181
|
+
state: 'live',
|
|
182
|
+
why: 'start-time-unavailable',
|
|
183
|
+
alive: true,
|
|
184
|
+
recordedPidStart: owner.pidStart,
|
|
185
|
+
livePidStart: null,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
if (normalize(live) !== normalize(owner.pidStart)) {
|
|
189
|
+
return {
|
|
190
|
+
state: 'stale',
|
|
191
|
+
why: 'start-time-mismatch',
|
|
192
|
+
alive: true,
|
|
193
|
+
recordedPidStart: owner.pidStart,
|
|
194
|
+
livePidStart: live,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return { state: 'live', why: 'pid-alive', alive: true, recordedPidStart: owner.pidStart ?? null };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/* ---------------------------------- state --------------------------------- */
|
|
202
|
+
|
|
203
|
+
function ensureStateDirs(aiLocalRoot) {
|
|
204
|
+
for (const dir of [processesRoot(aiLocalRoot), leasesDir(aiLocalRoot), reclaimDir(aiLocalRoot)]) {
|
|
205
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function fileAgeMs(file) {
|
|
210
|
+
try {
|
|
211
|
+
return Date.now() - fs.statSync(file).mtimeMs;
|
|
212
|
+
} catch {
|
|
213
|
+
return Number.POSITIVE_INFINITY;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/* --------------------------------- events --------------------------------- */
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Append one event line. The log is observability only: a write failure is
|
|
221
|
+
* reported as `false` and never fails the caller's operation.
|
|
222
|
+
*/
|
|
223
|
+
export function appendEvent({ aiLocalRoot, type, name, pid, reason, ...rest }) {
|
|
224
|
+
if (typeof type !== 'string' || type === '') return false;
|
|
225
|
+
const base = { at: new Date().toISOString(), type, name: name ?? null };
|
|
226
|
+
if (Number.isInteger(pid) && pid > 0) base.pid = pid;
|
|
227
|
+
const event = { ...base, ...rest };
|
|
228
|
+
if (reason !== undefined && reason !== null) event.reason = String(reason);
|
|
229
|
+
let line = JSON.stringify(event);
|
|
230
|
+
if (Buffer.byteLength(line, 'utf8') > EVENT_MAX_BYTES) {
|
|
231
|
+
delete event.reason;
|
|
232
|
+
line = JSON.stringify(event);
|
|
233
|
+
}
|
|
234
|
+
if (Buffer.byteLength(line, 'utf8') > EVENT_MAX_BYTES) {
|
|
235
|
+
line = JSON.stringify(base);
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
fs.mkdirSync(processesRoot(aiLocalRoot), { recursive: true });
|
|
239
|
+
fs.appendFileSync(eventsFile(aiLocalRoot), `${line}\n`, 'utf8');
|
|
240
|
+
return true;
|
|
241
|
+
} catch {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/* -------------------------------- reclaim --------------------------------- */
|
|
247
|
+
|
|
248
|
+
/** Serialise reclaim through a guard directory; the winner re-reads the lease. */
|
|
249
|
+
function takeGuard(aiLocalRoot, name) {
|
|
250
|
+
const guard = path.join(reclaimDir(aiLocalRoot), name);
|
|
251
|
+
try {
|
|
252
|
+
fs.mkdirSync(guard);
|
|
253
|
+
return { guard, taken: true };
|
|
254
|
+
} catch (error) {
|
|
255
|
+
if (error instanceof Error && error.code === 'EEXIST') {
|
|
256
|
+
// A guard older than the stale window belonged to a killed reclaimer.
|
|
257
|
+
if (fileAgeMs(guard) > GUARD_STALE_MS) {
|
|
258
|
+
try {
|
|
259
|
+
fs.rmSync(guard, { recursive: true, force: true });
|
|
260
|
+
} catch {
|
|
261
|
+
/* the holder still owns it */
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return { guard: null, taken: false };
|
|
265
|
+
}
|
|
266
|
+
return { guard: null, taken: false, error };
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function releaseGuard(guard) {
|
|
271
|
+
if (!guard) return;
|
|
272
|
+
try {
|
|
273
|
+
fs.rmSync(guard, { recursive: true, force: true });
|
|
274
|
+
} catch {
|
|
275
|
+
/* best effort: a leftover guard ages out */
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/* -------------------------------- acquire --------------------------------- */
|
|
280
|
+
|
|
281
|
+
const describeOwner = (record) => {
|
|
282
|
+
const owner = isRecord(record) ? record.owner : null;
|
|
283
|
+
if (!isRecord(owner)) return 'an unreadable lease';
|
|
284
|
+
const label = owner.label ? ` (${owner.label})` : '';
|
|
285
|
+
return `pid ${owner.pid}${label} since ${owner.startedAt ?? '?'}`;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Take one named lease.
|
|
290
|
+
*
|
|
291
|
+
* @returns {{ acquired: boolean, fatal: boolean, code: 'acquired'|'held'|'timeout'|'unwritable', skipped: boolean, name: string, path: string, owner?: object, token?: string, reason?: string }}
|
|
292
|
+
*/
|
|
293
|
+
export function acquireLease({
|
|
294
|
+
aiLocalRoot,
|
|
295
|
+
name,
|
|
296
|
+
kind = 'exclusive',
|
|
297
|
+
port = null,
|
|
298
|
+
label = null,
|
|
299
|
+
task = null,
|
|
300
|
+
agent = null,
|
|
301
|
+
cwd = process.cwd(),
|
|
302
|
+
command = null,
|
|
303
|
+
pid = process.pid,
|
|
304
|
+
pidStart,
|
|
305
|
+
token = newToken(),
|
|
306
|
+
waitMs = 0,
|
|
307
|
+
ttlMs = null,
|
|
308
|
+
allowUnlocked = false,
|
|
309
|
+
}) {
|
|
310
|
+
const target = leasePath(aiLocalRoot, name);
|
|
311
|
+
const budget = Number.isFinite(waitMs) && waitMs > 0 ? Math.floor(waitMs) : 0;
|
|
312
|
+
const deadline = Date.now() + budget;
|
|
313
|
+
const recordedStart = pidStart === undefined ? processStartTime(pid) : pidStart;
|
|
314
|
+
|
|
315
|
+
try {
|
|
316
|
+
ensureStateDirs(aiLocalRoot);
|
|
317
|
+
} catch (error) {
|
|
318
|
+
const code = error instanceof Error ? error.code ?? error.message : String(error);
|
|
319
|
+
return {
|
|
320
|
+
acquired: false,
|
|
321
|
+
fatal: true,
|
|
322
|
+
code: 'unwritable',
|
|
323
|
+
skipped: allowUnlocked === true,
|
|
324
|
+
name,
|
|
325
|
+
path: target,
|
|
326
|
+
reason: `the process state directory is not writable (${code})`,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const record = () => {
|
|
331
|
+
const startedAt = new Date().toISOString();
|
|
332
|
+
return {
|
|
333
|
+
schemaVersion: LEASE_SCHEMA_VERSION,
|
|
334
|
+
name,
|
|
335
|
+
kind,
|
|
336
|
+
owner: {
|
|
337
|
+
pid,
|
|
338
|
+
pidStart: recordedStart,
|
|
339
|
+
token,
|
|
340
|
+
label,
|
|
341
|
+
task,
|
|
342
|
+
agent,
|
|
343
|
+
cwd,
|
|
344
|
+
command,
|
|
345
|
+
startedAt,
|
|
346
|
+
heartbeatAt: startedAt,
|
|
347
|
+
},
|
|
348
|
+
port: Number.isInteger(port) && port > 0 ? port : null,
|
|
349
|
+
children: [],
|
|
350
|
+
ttlMs: Number.isFinite(ttlMs) && ttlMs > 0 ? Math.floor(ttlMs) : null,
|
|
351
|
+
};
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
const take = () => {
|
|
355
|
+
const value = record();
|
|
356
|
+
const fd = fs.openSync(target, 'wx');
|
|
357
|
+
try {
|
|
358
|
+
fs.writeFileSync(fd, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
|
|
359
|
+
} finally {
|
|
360
|
+
fs.closeSync(fd);
|
|
361
|
+
}
|
|
362
|
+
return value;
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const held = (owner, why) => ({
|
|
366
|
+
acquired: false,
|
|
367
|
+
fatal: false,
|
|
368
|
+
code: 'held',
|
|
369
|
+
skipped: false,
|
|
370
|
+
name,
|
|
371
|
+
path: target,
|
|
372
|
+
owner: isRecord(owner) ? owner : undefined,
|
|
373
|
+
reason: why,
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
const attempt = () => {
|
|
377
|
+
try {
|
|
378
|
+
const value = take();
|
|
379
|
+
appendEvent({ aiLocalRoot, type: 'acquired', name, pid, kind });
|
|
380
|
+
return {
|
|
381
|
+
acquired: true,
|
|
382
|
+
fatal: false,
|
|
383
|
+
code: 'acquired',
|
|
384
|
+
skipped: false,
|
|
385
|
+
name,
|
|
386
|
+
path: target,
|
|
387
|
+
owner: value.owner,
|
|
388
|
+
token,
|
|
389
|
+
};
|
|
390
|
+
} catch (error) {
|
|
391
|
+
if (!(error instanceof Error) || error.code !== 'EEXIST') {
|
|
392
|
+
const code = error instanceof Error ? error.code ?? error.message : String(error);
|
|
393
|
+
return {
|
|
394
|
+
acquired: false,
|
|
395
|
+
fatal: true,
|
|
396
|
+
code: 'unwritable',
|
|
397
|
+
skipped: false,
|
|
398
|
+
name,
|
|
399
|
+
path: target,
|
|
400
|
+
reason: `the lease file is not writable (${code})`,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const existing = readLease(aiLocalRoot, name);
|
|
406
|
+
if (existing === null && !fs.existsSync(target)) {
|
|
407
|
+
return held(undefined, 'the lease disappeared while it was being judged');
|
|
408
|
+
}
|
|
409
|
+
const state = existing === null ? { state: 'invalid', why: 'unparseable' } : leaseState(existing);
|
|
410
|
+
if (state.state === 'invalid' && fileAgeMs(target) <= LEASE_WARM_MS) {
|
|
411
|
+
return held(existing, `${describeOwner(existing)} is being written`);
|
|
412
|
+
}
|
|
413
|
+
if (state.state === 'live') {
|
|
414
|
+
return held(existing, `held by ${describeOwner(existing)}`);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const guard = takeGuard(aiLocalRoot, name);
|
|
418
|
+
if (!guard.taken) {
|
|
419
|
+
const current = readLease(aiLocalRoot, name) ?? existing;
|
|
420
|
+
const currentState = current === null ? { state: 'invalid' } : leaseState(current);
|
|
421
|
+
if (currentState.state === 'live') {
|
|
422
|
+
return held(current, `held by ${describeOwner(current)}`);
|
|
423
|
+
}
|
|
424
|
+
return held(current, `a stale lease is being reclaimed by another process (${currentState.why})`);
|
|
425
|
+
}
|
|
426
|
+
let reclaimed = false;
|
|
427
|
+
try {
|
|
428
|
+
const fresh = readLease(aiLocalRoot, name);
|
|
429
|
+
if (fresh === null && !fs.existsSync(target)) {
|
|
430
|
+
reclaimed = true;
|
|
431
|
+
} else {
|
|
432
|
+
const freshState = fresh === null ? { state: 'invalid', why: 'unparseable' } : leaseState(fresh);
|
|
433
|
+
if (freshState.state === 'live') {
|
|
434
|
+
return held(fresh, `held by ${describeOwner(fresh)}`);
|
|
435
|
+
}
|
|
436
|
+
appendEvent({
|
|
437
|
+
aiLocalRoot,
|
|
438
|
+
type: 'reclaimed',
|
|
439
|
+
name,
|
|
440
|
+
pid: isRecord(fresh?.owner) ? fresh.owner.pid : undefined,
|
|
441
|
+
why: freshState.why,
|
|
442
|
+
});
|
|
443
|
+
try {
|
|
444
|
+
fs.rmSync(target, { force: true });
|
|
445
|
+
} catch {
|
|
446
|
+
return {
|
|
447
|
+
acquired: false,
|
|
448
|
+
fatal: true,
|
|
449
|
+
code: 'unwritable',
|
|
450
|
+
skipped: false,
|
|
451
|
+
name,
|
|
452
|
+
path: target,
|
|
453
|
+
reason: 'a stale lease could not be removed',
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
reclaimed = true;
|
|
457
|
+
}
|
|
458
|
+
} finally {
|
|
459
|
+
releaseGuard(guard.guard);
|
|
460
|
+
}
|
|
461
|
+
if (!reclaimed) {
|
|
462
|
+
return held(readLease(aiLocalRoot, name), 'the stale lease could not be judged');
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
try {
|
|
466
|
+
const value = take();
|
|
467
|
+
appendEvent({ aiLocalRoot, type: 'acquired', name, pid, kind });
|
|
468
|
+
return {
|
|
469
|
+
acquired: true,
|
|
470
|
+
fatal: false,
|
|
471
|
+
code: 'acquired',
|
|
472
|
+
skipped: false,
|
|
473
|
+
name,
|
|
474
|
+
path: target,
|
|
475
|
+
owner: value.owner,
|
|
476
|
+
token,
|
|
477
|
+
reason: `reclaimed a stale lease (${state.why})`,
|
|
478
|
+
};
|
|
479
|
+
} catch (error) {
|
|
480
|
+
const fresh = readLease(aiLocalRoot, name);
|
|
481
|
+
if (error instanceof Error && error.code === 'EEXIST') {
|
|
482
|
+
return held(fresh, `held by ${describeOwner(fresh)}`);
|
|
483
|
+
}
|
|
484
|
+
const code = error instanceof Error ? error.code ?? error.message : String(error);
|
|
485
|
+
return {
|
|
486
|
+
acquired: false,
|
|
487
|
+
fatal: true,
|
|
488
|
+
code: 'unwritable',
|
|
489
|
+
skipped: false,
|
|
490
|
+
name,
|
|
491
|
+
path: target,
|
|
492
|
+
reason: `the reclaimed lease file is not writable (${code})`,
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
let result;
|
|
498
|
+
for (;;) {
|
|
499
|
+
result = attempt();
|
|
500
|
+
if (result.acquired || result.fatal || budget === 0) {
|
|
501
|
+
break;
|
|
502
|
+
}
|
|
503
|
+
const remaining = deadline - Date.now();
|
|
504
|
+
if (remaining <= 0) {
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
sleepSync(Math.min(DEFAULT_POLL_MS, remaining));
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
if (result.fatal) {
|
|
511
|
+
return { ...result, skipped: allowUnlocked === true };
|
|
512
|
+
}
|
|
513
|
+
if (result.acquired || budget === 0) {
|
|
514
|
+
return result;
|
|
515
|
+
}
|
|
516
|
+
return {
|
|
517
|
+
acquired: false,
|
|
518
|
+
fatal: false,
|
|
519
|
+
code: 'timeout',
|
|
520
|
+
skipped: false,
|
|
521
|
+
name,
|
|
522
|
+
path: target,
|
|
523
|
+
owner: result.owner,
|
|
524
|
+
reason: `timed out after ${budget}ms; ${result.owner ? describeOwner(result.owner) : 'owner unknown'}`,
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/* -------------------------------- release --------------------------------- */
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Drop one lease only when the caller owns it: the token must match, or — for
|
|
532
|
+
* a token-less legacy record — the pid. A foreign token is refused and the
|
|
533
|
+
* lease is left standing.
|
|
534
|
+
*/
|
|
535
|
+
export function releaseLease({ aiLocalRoot, name, token, pid = process.pid }) {
|
|
536
|
+
const target = leasePath(aiLocalRoot, name);
|
|
537
|
+
const record = readLease(aiLocalRoot, name);
|
|
538
|
+
if (record === null && !fs.existsSync(target)) {
|
|
539
|
+
return { released: false, reason: `no lease named "${name}"` };
|
|
540
|
+
}
|
|
541
|
+
const owner = isRecord(record) ? record.owner : null;
|
|
542
|
+
if (!isRecord(owner)) {
|
|
543
|
+
// A crash-truncated record and a writer between `open` and `write` look the
|
|
544
|
+
// same, and an unreadable record names no owner to compare a token with:
|
|
545
|
+
// inside the warm window the lease is not ours to judge; outside it no
|
|
546
|
+
// writer is left, so the dead record can be cleared.
|
|
547
|
+
if (fileAgeMs(target) <= LEASE_WARM_MS) {
|
|
548
|
+
return { released: false, reason: 'the lease is being written' };
|
|
549
|
+
}
|
|
550
|
+
try {
|
|
551
|
+
fs.rmSync(target, { force: true });
|
|
552
|
+
return { released: true };
|
|
553
|
+
} catch {
|
|
554
|
+
return { released: false, reason: 'the unreadable lease could not be removed' };
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
if (owner.token) {
|
|
558
|
+
if (owner.token !== token) {
|
|
559
|
+
appendEvent({ aiLocalRoot, type: 'refused', name, pid, reason: 'release token does not match' });
|
|
560
|
+
return { released: false, reason: `held by ${describeOwner(record)}` };
|
|
561
|
+
}
|
|
562
|
+
} else if (owner.pid !== pid) {
|
|
563
|
+
appendEvent({ aiLocalRoot, type: 'refused', name, pid, reason: 'release pid does not match' });
|
|
564
|
+
return { released: false, reason: `held by ${describeOwner(record)}` };
|
|
565
|
+
}
|
|
566
|
+
try {
|
|
567
|
+
fs.rmSync(target, { force: true });
|
|
568
|
+
} catch (error) {
|
|
569
|
+
return {
|
|
570
|
+
released: false,
|
|
571
|
+
reason: `the lease file could not be removed (${error instanceof Error ? error.code ?? error.message : String(error)})`,
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
appendEvent({ aiLocalRoot, type: 'released', name, pid: owner.pid });
|
|
575
|
+
return { released: true };
|
|
576
|
+
}
|