@bonesofspring/ai-rules 0.2.63 → 0.2.65
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 +96 -1
- package/README.md +11 -4
- package/bin/cli.js +7 -5
- package/bin/lib/ai-local-drift.js +7 -6
- package/bin/lib/engine-scripts.js +17 -4
- package/bin/lib/engine-upgrade.js +10 -2
- package/bin/lib/manifest-schema.js +12 -0
- package/bin/lib/seed-ai-local-team.js +20 -0
- package/bin/mavis-install.js +4 -3
- package/bin/opencode-install.js +22 -6
- package/bin/zcode-install.js +4 -3
- package/docs/MCP-CATALOG.md +1 -1
- package/docs/OPENCODE-COMPATIBILITY.md +3 -2
- package/docs/OPENCODE.md +22 -2
- package/docs/PRESET-CONTRIBUTION.md +43 -2
- package/docs/TEAM-DB.md +213 -5
- package/docs/openapi/team-db.v1.json +5928 -3045
- package/mcp/defectdojo-mcp.mjs +44 -12
- package/mcp/lib/defectdojo.mjs +57 -2
- package/package.json +27 -15
- package/presets/_shared/ai-local/scripts/aggregate-agent-task-metrics.mjs +279 -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 +784 -7
- package/presets/_shared/ai-local/scripts/team-db-dashboard.mjs +235 -10
- package/presets/_shared/ai-local/scripts/team-db-openapi.mjs +813 -7
- package/presets/_shared/ai-local/scripts/team-db-project.mjs +397 -4
- package/presets/_shared/ai-local/scripts/team-db-scheduler.mjs +86 -10
- package/presets/_shared/ai-local/scripts/team-db-schema.mjs +221 -0
- package/presets/_shared/ai-local/scripts/team-db-task-runner.mjs +567 -0
- package/presets/_shared/ai-local/scripts/team-db-tracker.mjs +2069 -0
- package/presets/_shared/ai-local/scripts/team-db-ui.mjs +750 -0
- package/presets/_shared/ai-local/scripts/team-db.mjs +151 -7
- package/presets/_shared/ai-local/scripts/upstream-research.mjs +3 -0
- package/presets/_shared/core/agent-team/agent-task-metrics.md +7 -2
- package/presets/_shared/core/agent-team/agent-team-orchestrator.md +2 -1
- package/presets/_shared/hooks/chain-team-phases.sh.tmpl +314 -1
- package/presets/_shared/hooks/guard-shell-command.mjs.tmpl +40 -2
- package/presets/_shared/hooks/run-hook.mjs +2 -1
- package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +314 -1
- package/presets/claude/android-kotlin/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/android-kotlin/hooks/run-hook.mjs +2 -1
- package/presets/claude/android-kotlin/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
- package/presets/claude/android-kotlin/settings.json +6 -1
- package/presets/claude/go/hooks/chain-team-phases.sh +314 -1
- package/presets/claude/go/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/go/hooks/run-hook.mjs +2 -1
- package/presets/claude/go/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
- package/presets/claude/go/settings.json +6 -1
- package/presets/claude/ios-swift/hooks/chain-team-phases.sh +314 -1
- package/presets/claude/ios-swift/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/ios-swift/hooks/run-hook.mjs +2 -1
- package/presets/claude/ios-swift/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
- package/presets/claude/ios-swift/settings.json +6 -1
- package/presets/claude/java/hooks/chain-team-phases.sh +314 -1
- package/presets/claude/java/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/java/hooks/run-hook.mjs +2 -1
- package/presets/claude/java/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
- package/presets/claude/java/settings.json +6 -1
- package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +314 -1
- package/presets/claude/mcp-ts/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/mcp-ts/hooks/run-hook.mjs +2 -1
- package/presets/claude/mcp-ts/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
- package/presets/claude/mcp-ts/settings.json +6 -1
- package/presets/claude/mol/hooks/chain-team-phases.sh +314 -1
- package/presets/claude/mol/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/mol/hooks/run-hook.mjs +2 -1
- package/presets/claude/mol/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
- 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/code-reviewer.md +1 -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 +4 -1
- 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 +2 -0
- package/presets/claude/next/commands/README.md +6 -0
- package/presets/claude/next/commands/task.md +7 -1
- package/presets/claude/next/commands/technical-retro.md +7 -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/commands/upstream-research.md +1 -1
- package/presets/claude/next/hooks/chain-team-phases.sh +314 -1
- package/presets/claude/next/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/next/hooks/run-hook.mjs +2 -1
- package/presets/claude/next/rules/README.md +2 -1
- package/presets/claude/next/rules/stack/next-app-core.md +1 -0
- package/presets/claude/next/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
- 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/skills/figma-fidelity/SKILL.md +4 -1
- package/presets/claude/next/skills/mockup-review/SKILL.md +4 -0
- package/presets/claude/next/team/README.md +5 -1
- package/presets/claude/next/team/upstream/jobs.json +34 -0
- package/presets/claude/nuxt/hooks/chain-team-phases.sh +314 -1
- package/presets/claude/nuxt/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/nuxt/hooks/run-hook.mjs +2 -1
- package/presets/claude/nuxt/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
- package/presets/claude/nuxt/settings.json +6 -1
- package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +314 -1
- package/presets/claude/php-hexagonal/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/php-hexagonal/hooks/run-hook.mjs +2 -1
- package/presets/claude/php-hexagonal/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
- package/presets/claude/php-hexagonal/settings.json +6 -1
- package/presets/claude/php-laravel/hooks/chain-team-phases.sh +315 -2
- package/presets/claude/php-laravel/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/php-laravel/hooks/run-hook.mjs +2 -1
- package/presets/claude/php-laravel/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
- package/presets/claude/php-laravel/settings.json +6 -1
- package/presets/claude/svelte/hooks/chain-team-phases.sh +314 -1
- package/presets/claude/svelte/hooks/guard-shell-command.mjs +41 -2
- package/presets/claude/svelte/hooks/run-hook.mjs +2 -1
- package/presets/claude/svelte/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
- package/presets/claude/svelte/settings.json +6 -1
- package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +314 -1
- package/presets/cursor/android-kotlin/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/android-kotlin/hooks/run-hook.mjs +2 -1
- package/presets/cursor/go/hooks/chain-team-phases.sh +314 -1
- package/presets/cursor/go/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/go/hooks/run-hook.mjs +2 -1
- package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +314 -1
- package/presets/cursor/ios-swift/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/ios-swift/hooks/run-hook.mjs +2 -1
- package/presets/cursor/java/hooks/chain-team-phases.sh +314 -1
- package/presets/cursor/java/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/java/hooks/run-hook.mjs +2 -1
- package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +314 -1
- package/presets/cursor/mcp-ts/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/mcp-ts/hooks/run-hook.mjs +2 -1
- package/presets/cursor/mol/hooks/chain-team-phases.sh +314 -1
- package/presets/cursor/mol/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/mol/hooks/run-hook.mjs +2 -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/code-reviewer.md +1 -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 +4 -1
- 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 +2 -0
- package/presets/cursor/next/ai-local/rules-library/storybook-dev-compare-figma.md +2 -1
- package/presets/cursor/next/ai-local/rules-library/ui-change-review-gate.md +1 -0
- package/presets/cursor/next/commands/README.md +6 -0
- package/presets/cursor/next/commands/task.md +7 -1
- package/presets/cursor/next/commands/technical-retro.md +7 -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/commands/upstream-research.md +1 -1
- package/presets/cursor/next/hooks/chain-team-phases.sh +314 -1
- package/presets/cursor/next/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/next/hooks/run-hook.mjs +2 -1
- package/presets/cursor/next/rules/README.md +2 -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/skills/figma-fidelity/SKILL.md +4 -1
- package/presets/cursor/next/skills/mockup-review/SKILL.md +4 -0
- package/presets/cursor/next/team/README.md +5 -1
- package/presets/cursor/next/team/upstream/jobs.json +34 -0
- package/presets/cursor/nuxt/hooks/chain-team-phases.sh +314 -1
- package/presets/cursor/nuxt/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/nuxt/hooks/run-hook.mjs +2 -1
- package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +314 -1
- package/presets/cursor/php-hexagonal/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/php-hexagonal/hooks/run-hook.mjs +2 -1
- package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +315 -2
- package/presets/cursor/php-laravel/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/php-laravel/hooks/run-hook.mjs +2 -1
- package/presets/cursor/svelte/hooks/chain-team-phases.sh +314 -1
- package/presets/cursor/svelte/hooks/guard-shell-command.mjs +41 -2
- package/presets/cursor/svelte/hooks/run-hook.mjs +2 -1
- package/presets/mavis/android-kotlin/adapter-map.json +1 -1
- package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +314 -1
- package/presets/mavis/android-kotlin/rules/agent-team-orchestrator.md +2 -1
- package/presets/mavis/go/adapter-map.json +1 -1
- package/presets/mavis/go/hooks/chain-team-phases.sh +314 -1
- package/presets/mavis/go/rules/agent-team-orchestrator.md +2 -1
- package/presets/mavis/ios-swift/adapter-map.json +1 -1
- package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +314 -1
- package/presets/mavis/ios-swift/rules/agent-team-orchestrator.md +2 -1
- package/presets/mavis/java/adapter-map.json +1 -1
- package/presets/mavis/java/hooks/chain-team-phases.sh +314 -1
- package/presets/mavis/java/rules/agent-team-orchestrator.md +2 -1
- package/presets/mavis/mcp-ts/adapter-map.json +1 -1
- package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +314 -1
- package/presets/mavis/mcp-ts/rules/agent-team-orchestrator.md +2 -1
- package/presets/mavis/mol/adapter-map.json +1 -1
- package/presets/mavis/mol/hooks/chain-team-phases.sh +314 -1
- package/presets/mavis/mol/rules/agent-team-orchestrator.md +2 -1
- 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/code-reviewer.md +1 -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 +4 -1
- 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 +2 -0
- package/presets/mavis/next/ai-local/rules-library/storybook-dev-compare-figma.md +2 -1
- package/presets/mavis/next/ai-local/rules-library/ui-change-review-gate.md +1 -0
- package/presets/mavis/next/commands/README.md +6 -0
- package/presets/mavis/next/commands/task.md +7 -1
- package/presets/mavis/next/commands/technical-retro.md +7 -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/commands/upstream-research.md +1 -1
- package/presets/mavis/next/hooks/chain-team-phases.sh +314 -1
- package/presets/mavis/next/rules/agent-team-orchestrator.md +2 -1
- package/presets/mavis/next/rules/dev-process-lifecycle.md +38 -0
- package/presets/mavis/next/rules/next-app-core.md +1 -0
- package/presets/mavis/next/skills/figma-fidelity/SKILL.md +4 -1
- package/presets/mavis/next/skills/mockup-review/SKILL.md +4 -0
- package/presets/mavis/next/team/README.md +5 -1
- package/presets/mavis/next/team/upstream/jobs.json +34 -0
- package/presets/mavis/nuxt/adapter-map.json +1 -1
- package/presets/mavis/nuxt/hooks/chain-team-phases.sh +314 -1
- package/presets/mavis/nuxt/rules/agent-team-orchestrator.md +2 -1
- package/presets/mavis/php-hexagonal/adapter-map.json +1 -1
- package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +314 -1
- package/presets/mavis/php-hexagonal/rules/agent-team-orchestrator.md +2 -1
- package/presets/mavis/php-laravel/adapter-map.json +1 -1
- package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +315 -2
- package/presets/mavis/php-laravel/rules/agent-team-orchestrator.md +2 -1
- package/presets/mavis/svelte/adapter-map.json +1 -1
- package/presets/mavis/svelte/hooks/chain-team-phases.sh +314 -1
- package/presets/mavis/svelte/rules/agent-team-orchestrator.md +2 -1
- package/presets/opencode/android-kotlin/OPENCODE.md +4 -1
- package/presets/opencode/android-kotlin/README.md +1 -0
- package/presets/opencode/android-kotlin/adapter-map.json +1 -1
- package/presets/opencode/android-kotlin/hooks/README.md +4 -2
- package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +314 -1
- package/presets/opencode/android-kotlin/opencode.json +5 -1
- package/presets/opencode/android-kotlin/rules/agent-team-orchestrator.md +2 -1
- package/presets/opencode/go/OPENCODE.md +4 -1
- package/presets/opencode/go/README.md +1 -0
- package/presets/opencode/go/adapter-map.json +1 -1
- package/presets/opencode/go/hooks/README.md +4 -2
- package/presets/opencode/go/hooks/chain-team-phases.sh +314 -1
- package/presets/opencode/go/opencode.json +5 -1
- package/presets/opencode/go/rules/agent-team-orchestrator.md +2 -1
- package/presets/opencode/ios-swift/OPENCODE.md +4 -1
- package/presets/opencode/ios-swift/README.md +1 -0
- package/presets/opencode/ios-swift/adapter-map.json +1 -1
- package/presets/opencode/ios-swift/hooks/README.md +4 -2
- package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +314 -1
- package/presets/opencode/ios-swift/opencode.json +5 -1
- package/presets/opencode/ios-swift/rules/agent-team-orchestrator.md +2 -1
- package/presets/opencode/java/OPENCODE.md +4 -1
- package/presets/opencode/java/README.md +1 -0
- package/presets/opencode/java/adapter-map.json +1 -1
- package/presets/opencode/java/hooks/README.md +4 -2
- package/presets/opencode/java/hooks/chain-team-phases.sh +314 -1
- package/presets/opencode/java/opencode.json +5 -1
- package/presets/opencode/java/rules/agent-team-orchestrator.md +2 -1
- package/presets/opencode/mcp-ts/OPENCODE.md +4 -1
- package/presets/opencode/mcp-ts/README.md +1 -0
- package/presets/opencode/mcp-ts/adapter-map.json +1 -1
- package/presets/opencode/mcp-ts/hooks/README.md +4 -2
- package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +314 -1
- package/presets/opencode/mcp-ts/opencode.json +5 -1
- package/presets/opencode/mcp-ts/rules/agent-team-orchestrator.md +2 -1
- package/presets/opencode/mol/OPENCODE.md +4 -1
- package/presets/opencode/mol/README.md +1 -0
- package/presets/opencode/mol/adapter-map.json +1 -1
- package/presets/opencode/mol/hooks/README.md +4 -2
- package/presets/opencode/mol/hooks/chain-team-phases.sh +314 -1
- package/presets/opencode/mol/opencode.json +4 -0
- package/presets/opencode/mol/rules/agent-team-orchestrator.md +2 -1
- package/presets/opencode/next/MCP.md +2 -0
- package/presets/opencode/next/OPENCODE.md +4 -1
- package/presets/opencode/next/README.md +1 -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/code-reviewer.md +1 -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 +4 -1
- 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 +2 -0
- package/presets/opencode/next/ai-local/rules-library/storybook-dev-compare-figma.md +2 -1
- package/presets/opencode/next/ai-local/rules-library/ui-change-review-gate.md +1 -0
- package/presets/opencode/next/commands/README.md +6 -0
- package/presets/opencode/next/commands/task.md +7 -1
- package/presets/opencode/next/commands/technical-retro.md +7 -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/commands/upstream-research.md +1 -1
- package/presets/opencode/next/hooks/README.md +4 -2
- package/presets/opencode/next/hooks/chain-team-phases.sh +314 -1
- package/presets/opencode/next/opencode.json +4 -0
- package/presets/opencode/next/rules/agent-team-orchestrator.md +2 -1
- package/presets/opencode/next/rules/dev-process-lifecycle.md +38 -0
- package/presets/opencode/next/rules/next-app-core.md +1 -0
- package/presets/opencode/next/skills/figma-fidelity/SKILL.md +4 -1
- package/presets/opencode/next/skills/mockup-review/SKILL.md +4 -0
- package/presets/opencode/next/team/README.md +5 -1
- package/presets/opencode/next/team/upstream/jobs.json +34 -0
- package/presets/opencode/nuxt/OPENCODE.md +4 -1
- package/presets/opencode/nuxt/README.md +1 -0
- package/presets/opencode/nuxt/adapter-map.json +1 -1
- package/presets/opencode/nuxt/hooks/README.md +4 -2
- package/presets/opencode/nuxt/hooks/chain-team-phases.sh +314 -1
- package/presets/opencode/nuxt/opencode.json +4 -0
- package/presets/opencode/nuxt/rules/agent-team-orchestrator.md +2 -1
- package/presets/opencode/php-hexagonal/OPENCODE.md +4 -1
- package/presets/opencode/php-hexagonal/README.md +1 -0
- package/presets/opencode/php-hexagonal/adapter-map.json +1 -1
- package/presets/opencode/php-hexagonal/hooks/README.md +4 -2
- package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +314 -1
- package/presets/opencode/php-hexagonal/opencode.json +5 -1
- package/presets/opencode/php-hexagonal/rules/agent-team-orchestrator.md +2 -1
- package/presets/opencode/php-laravel/OPENCODE.md +4 -1
- package/presets/opencode/php-laravel/README.md +1 -0
- package/presets/opencode/php-laravel/adapter-map.json +1 -1
- package/presets/opencode/php-laravel/hooks/README.md +4 -2
- package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +315 -2
- package/presets/opencode/php-laravel/opencode.json +5 -1
- package/presets/opencode/php-laravel/rules/agent-team-orchestrator.md +2 -1
- package/presets/opencode/platform-contract.json +6 -0
- package/presets/opencode/plugins/task-status/README.md +92 -0
- package/presets/opencode/plugins/task-status/details-data.ts +130 -0
- package/presets/opencode/plugins/task-status/details.tsx +84 -0
- package/presets/opencode/plugins/task-status/eta.ts +230 -0
- package/presets/opencode/plugins/task-status/index.ts +6 -0
- package/presets/opencode/plugins/task-status/package.json +10 -0
- package/presets/opencode/plugins/task-status/task-view.ts +606 -0
- package/presets/opencode/plugins/task-status/tui.tsx +139 -0
- package/presets/opencode/svelte/OPENCODE.md +4 -1
- package/presets/opencode/svelte/README.md +1 -0
- package/presets/opencode/svelte/adapter-map.json +1 -1
- package/presets/opencode/svelte/hooks/README.md +4 -2
- package/presets/opencode/svelte/hooks/chain-team-phases.sh +314 -1
- package/presets/opencode/svelte/opencode.json +4 -0
- package/presets/opencode/svelte/rules/agent-team-orchestrator.md +2 -1
- package/presets/zcode/android-kotlin/adapter-map.json +1 -1
- package/presets/zcode/android-kotlin/hooks/chain-team-phases.sh +314 -1
- package/presets/zcode/android-kotlin/rules/agent-team-orchestrator.md +2 -1
- package/presets/zcode/go/adapter-map.json +1 -1
- package/presets/zcode/go/hooks/chain-team-phases.sh +314 -1
- package/presets/zcode/go/rules/agent-team-orchestrator.md +2 -1
- package/presets/zcode/ios-swift/adapter-map.json +1 -1
- package/presets/zcode/ios-swift/hooks/chain-team-phases.sh +314 -1
- package/presets/zcode/ios-swift/rules/agent-team-orchestrator.md +2 -1
- package/presets/zcode/java/adapter-map.json +1 -1
- package/presets/zcode/java/hooks/chain-team-phases.sh +314 -1
- package/presets/zcode/java/rules/agent-team-orchestrator.md +2 -1
- package/presets/zcode/mcp-ts/adapter-map.json +1 -1
- package/presets/zcode/mcp-ts/hooks/chain-team-phases.sh +314 -1
- package/presets/zcode/mcp-ts/rules/agent-team-orchestrator.md +2 -1
- package/presets/zcode/mol/adapter-map.json +1 -1
- package/presets/zcode/mol/hooks/chain-team-phases.sh +314 -1
- package/presets/zcode/mol/rules/agent-team-orchestrator.md +2 -1
- 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/code-reviewer.md +1 -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 +4 -1
- 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 +2 -0
- package/presets/zcode/next/ai-local/rules-library/storybook-dev-compare-figma.md +2 -1
- package/presets/zcode/next/ai-local/rules-library/ui-change-review-gate.md +1 -0
- package/presets/zcode/next/commands/README.md +6 -0
- package/presets/zcode/next/commands/task.md +7 -1
- package/presets/zcode/next/commands/technical-retro.md +7 -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/commands/upstream-research.md +1 -1
- package/presets/zcode/next/hooks/chain-team-phases.sh +314 -1
- package/presets/zcode/next/rules/agent-team-orchestrator.md +2 -1
- package/presets/zcode/next/rules/dev-process-lifecycle.md +38 -0
- package/presets/zcode/next/rules/next-app-core.md +1 -0
- package/presets/zcode/next/skills/figma-fidelity/SKILL.md +4 -1
- package/presets/zcode/next/skills/mockup-review/SKILL.md +4 -0
- package/presets/zcode/next/team/README.md +5 -1
- package/presets/zcode/next/team/upstream/jobs.json +34 -0
- package/presets/zcode/nuxt/adapter-map.json +1 -1
- package/presets/zcode/nuxt/hooks/chain-team-phases.sh +314 -1
- package/presets/zcode/nuxt/rules/agent-team-orchestrator.md +2 -1
- package/presets/zcode/php-hexagonal/adapter-map.json +1 -1
- package/presets/zcode/php-hexagonal/hooks/chain-team-phases.sh +314 -1
- package/presets/zcode/php-hexagonal/rules/agent-team-orchestrator.md +2 -1
- package/presets/zcode/php-laravel/adapter-map.json +1 -1
- package/presets/zcode/php-laravel/hooks/chain-team-phases.sh +315 -2
- package/presets/zcode/php-laravel/rules/agent-team-orchestrator.md +2 -1
- package/presets/zcode/svelte/adapter-map.json +1 -1
- package/presets/zcode/svelte/hooks/chain-team-phases.sh +314 -1
- package/presets/zcode/svelte/rules/agent-team-orchestrator.md +2 -1
- package/scripts/aggregate-agent-task-metrics.mjs +48 -4
|
@@ -0,0 +1,1618 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `processes` — one view and one protocol for every process a checkout runs.
|
|
3
|
+
*
|
|
4
|
+
* Verbs:
|
|
5
|
+
* acquire --name <res> [--kind exclusive|port|advisory] [--port <n>] [--label <l>]
|
|
6
|
+
* [--task <slug>] [--agent <name>] [--wait <ms>] [--ttl <ms>]
|
|
7
|
+
* [--allow-unlocked] [--json] [--root <dir>]
|
|
8
|
+
* release --name <res> --token <t> [--root <dir>] [--json]
|
|
9
|
+
* run --name <res> [acquire flags] -- <command> [args…]
|
|
10
|
+
* list [--json] [--task <slug>] [--live] [--root <dir>]
|
|
11
|
+
* status --name <res> [--json]
|
|
12
|
+
* watch [--interval 1s] [--json] [--once] [--no-color]
|
|
13
|
+
* scan [--json] [--known-only | --all]
|
|
14
|
+
* doctor [--json]
|
|
15
|
+
* stop --name <res> | --task <slug> [--force]
|
|
16
|
+
* serve [--port 4179] [--host 127.0.0.1]
|
|
17
|
+
*
|
|
18
|
+
* The lease itself lives in `process-lease.mjs`. This module reads the six
|
|
19
|
+
* markers other tools already write into the same snapshot and never writes
|
|
20
|
+
* them; the only state it owns is `.processes/`.
|
|
21
|
+
*
|
|
22
|
+
* `acquire` records the **parent** process (the shell) as the owner because this
|
|
23
|
+
* CLI exits immediately; `run` records the manager, which stays alive for the
|
|
24
|
+
* child. `--pid <n>` overrides either, and an owner that is not provably alive
|
|
25
|
+
* is refused with exit 2.
|
|
26
|
+
*
|
|
27
|
+
* Nothing here signals a process it did not prove: `stop` re-checks the live
|
|
28
|
+
* pid, the recorded start time, and the command line or the lease port. Exit
|
|
29
|
+
* codes are 0 ok · 1 held/timeout · 2 usage · 3 fatal · 4 refused-unowned.
|
|
30
|
+
*/
|
|
31
|
+
import fs from 'node:fs';
|
|
32
|
+
import http from 'node:http';
|
|
33
|
+
import net from 'node:net';
|
|
34
|
+
import os from 'node:os';
|
|
35
|
+
import path from 'node:path';
|
|
36
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
37
|
+
import { fileURLToPath } from 'node:url';
|
|
38
|
+
import {
|
|
39
|
+
acquireLease,
|
|
40
|
+
appendEvent,
|
|
41
|
+
isPidAlive,
|
|
42
|
+
isValidLeaseName,
|
|
43
|
+
leasePath,
|
|
44
|
+
leaseState,
|
|
45
|
+
listLeases,
|
|
46
|
+
processStartTime,
|
|
47
|
+
readLease,
|
|
48
|
+
releaseLease,
|
|
49
|
+
} from './process-lease.mjs';
|
|
50
|
+
|
|
51
|
+
export const SNAPSHOT_SCHEMA_VERSION = 1;
|
|
52
|
+
export const DEFAULT_SERVE_PORT = 4179;
|
|
53
|
+
export const DEFAULT_SERVE_HOST = '127.0.0.1';
|
|
54
|
+
export const DEFAULT_WATCH_MS = 1000;
|
|
55
|
+
|
|
56
|
+
const SERVE_POLL_MS = 1000;
|
|
57
|
+
const SSE_RETRY_MS = 2000;
|
|
58
|
+
const TERMINATE_WAIT_MS = 5000;
|
|
59
|
+
const TERMINATE_POLL_MS = 100;
|
|
60
|
+
const SSE_MIN_INTERVAL_MS = 100;
|
|
61
|
+
const START_TIMEOUT_MS = 30000;
|
|
62
|
+
const START_POLL_MS = 200;
|
|
63
|
+
const PROBE_TIMEOUT_MS = 400;
|
|
64
|
+
const LOG_TAIL_LINES = 8;
|
|
65
|
+
const ACTIVE_RUN_STATES = new Set(['claimed', 'running']);
|
|
66
|
+
const LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost', '::1', '[::1]']);
|
|
67
|
+
const PRESET_LOCK_REL = path.join('packages', 'ai-rules', '.preset-tree-lock', 'owner.json');
|
|
68
|
+
|
|
69
|
+
export const KNOWN_COMMANDS = [
|
|
70
|
+
{ id: 'fix-generated', pattern: /fix-generated\.mjs/ },
|
|
71
|
+
{ id: 'preset-gate', pattern: /check-preset-(?:fast|structure)\.sh/ },
|
|
72
|
+
{ id: 'team-db-serve', pattern: /team-db\.mjs\s+serve/ },
|
|
73
|
+
{ id: 'storybook', pattern: /storybook/ },
|
|
74
|
+
{ id: 'next-dev', pattern: /next\s+dev/ },
|
|
75
|
+
{ id: 'playwright', pattern: /playwright.*(?:chrom|firefox|webkit)/ },
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
const VERB_FLAGS = {
|
|
79
|
+
start: new Set([
|
|
80
|
+
'name', 'port', 'command', 'cwd', 'url', 'task', 'label', 'root', 'wait-ms', 'json',
|
|
81
|
+
]),
|
|
82
|
+
acquire: new Set([
|
|
83
|
+
'name', 'kind', 'port', 'label', 'task', 'agent', 'wait', 'ttl', 'allow-unlocked',
|
|
84
|
+
'json', 'root', 'pid', 'token', 'cwd', 'command',
|
|
85
|
+
]),
|
|
86
|
+
release: new Set(['name', 'token', 'pid', 'root', 'json']),
|
|
87
|
+
run: new Set([
|
|
88
|
+
'name', 'kind', 'port', 'label', 'task', 'agent', 'wait', 'ttl', 'allow-unlocked',
|
|
89
|
+
'root', 'pid', 'token', 'cwd', 'command',
|
|
90
|
+
]),
|
|
91
|
+
list: new Set(['json', 'root', 'task', 'live']),
|
|
92
|
+
status: new Set(['name', 'json', 'root']),
|
|
93
|
+
watch: new Set(['interval', 'json', 'once', 'no-color', 'root', 'task']),
|
|
94
|
+
scan: new Set(['json', 'known-only', 'all', 'root']),
|
|
95
|
+
doctor: new Set(['json', 'root']),
|
|
96
|
+
stop: new Set(['name', 'task', 'force', 'root', 'json']),
|
|
97
|
+
serve: new Set(['port', 'host', 'root']),
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const BOOLEAN_FLAGS = new Set([
|
|
101
|
+
'json', 'allow-unlocked', 'once', 'no-color', 'live', 'force', 'known-only', 'all',
|
|
102
|
+
]);
|
|
103
|
+
|
|
104
|
+
class ProcessError extends Error {
|
|
105
|
+
constructor(code, message, exitCode) {
|
|
106
|
+
super(message);
|
|
107
|
+
this.name = 'ProcessError';
|
|
108
|
+
this.code = code;
|
|
109
|
+
this.exitCode = exitCode;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const fail = (message, exitCode = 2) => {
|
|
114
|
+
throw new ProcessError('E_PROCESSES', message, exitCode);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const out = (line) => process.stdout.write(`${line}\n`);
|
|
118
|
+
const err = (line) => process.stderr.write(`${line}\n`);
|
|
119
|
+
|
|
120
|
+
const isRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
121
|
+
const normalize = (value) => String(value).replace(/\s+/g, ' ').trim();
|
|
122
|
+
const sleep = (ms) => new Promise((resolve) => {
|
|
123
|
+
setTimeout(resolve, ms);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
/* ----------------------------------- roots -------------------------------- */
|
|
127
|
+
|
|
128
|
+
const isDirectory = (target) => {
|
|
129
|
+
try {
|
|
130
|
+
return fs.statSync(target).isDirectory();
|
|
131
|
+
} catch {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/** The git checkout root, or `null` when `git` is absent or this is not a repo. */
|
|
137
|
+
const gitToplevel = (cwd) => {
|
|
138
|
+
const result = spawnSync('git', ['rev-parse', '--show-toplevel'], {
|
|
139
|
+
cwd,
|
|
140
|
+
encoding: 'utf8',
|
|
141
|
+
timeout: 2000,
|
|
142
|
+
});
|
|
143
|
+
if (result.error || result.status !== 0) return null;
|
|
144
|
+
const value = String(result.stdout ?? '').trim();
|
|
145
|
+
return value === '' ? null : value;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* One `.ai.local` per checkout: the git root's. Walking up from a package
|
|
150
|
+
* directory would stop at a nested `.ai.local` stub (`packages/.ai.local`,
|
|
151
|
+
* which holds only `team/`), so the git root wins. Outside a checkout the
|
|
152
|
+
* first `.ai.local` carrying the engine (`scripts/`) wins over a bare stub.
|
|
153
|
+
*/
|
|
154
|
+
const findAiLocalRoot = (start) => {
|
|
155
|
+
const toplevel = gitToplevel(start);
|
|
156
|
+
if (toplevel !== null) return path.join(toplevel, '.ai.local');
|
|
157
|
+
|
|
158
|
+
let first = null;
|
|
159
|
+
let current = path.resolve(start);
|
|
160
|
+
for (;;) {
|
|
161
|
+
const candidate = path.join(current, '.ai.local');
|
|
162
|
+
if (isDirectory(candidate)) {
|
|
163
|
+
if (isDirectory(path.join(candidate, 'scripts'))) return candidate;
|
|
164
|
+
first = first ?? candidate;
|
|
165
|
+
}
|
|
166
|
+
const parent = path.dirname(current);
|
|
167
|
+
if (parent === current) break;
|
|
168
|
+
current = parent;
|
|
169
|
+
}
|
|
170
|
+
return first ?? path.resolve(start, '.ai.local');
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
/** `--root` accepts the `.ai.local` directory or the repository root above it. */
|
|
174
|
+
const resolveRoots = (flags) => {
|
|
175
|
+
let aiLocalRoot;
|
|
176
|
+
if (flags.root) {
|
|
177
|
+
const resolved = path.resolve(flags.root);
|
|
178
|
+
aiLocalRoot = path.basename(resolved) === '.ai.local' ? resolved : path.join(resolved, '.ai.local');
|
|
179
|
+
} else {
|
|
180
|
+
aiLocalRoot = findAiLocalRoot(process.cwd());
|
|
181
|
+
}
|
|
182
|
+
return { aiLocalRoot, repoRoot: path.dirname(aiLocalRoot) };
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/* --------------------------------- process -------------------------------- */
|
|
186
|
+
|
|
187
|
+
const liveCommand = (pid) => {
|
|
188
|
+
const result = spawnSync('ps', ['-p', String(pid), '-o', 'command='], { encoding: 'utf8' });
|
|
189
|
+
if (result.error || result.status !== 0) return null;
|
|
190
|
+
const command = result.stdout.trim();
|
|
191
|
+
return command ? command : null;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const listenPids = (port) => {
|
|
195
|
+
if (!Number.isInteger(port) || port <= 0) return [];
|
|
196
|
+
const result = spawnSync('lsof', ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN', '-t'], {
|
|
197
|
+
encoding: 'utf8',
|
|
198
|
+
});
|
|
199
|
+
if (result.error || result.status !== 0) return [];
|
|
200
|
+
return result.stdout
|
|
201
|
+
.split('\n')
|
|
202
|
+
.map((line) => Number.parseInt(line.trim(), 10))
|
|
203
|
+
.filter((pid) => Number.isInteger(pid) && pid > 0);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const signalPid = (pid, signal) => {
|
|
207
|
+
try {
|
|
208
|
+
process.kill(pid, signal);
|
|
209
|
+
return true;
|
|
210
|
+
} catch {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const terminate = async (pid, { force = false } = {}) => {
|
|
216
|
+
if (force) {
|
|
217
|
+
signalPid(pid, 'SIGKILL');
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (!signalPid(pid, 'SIGTERM')) return;
|
|
221
|
+
const deadline = Date.now() + TERMINATE_WAIT_MS;
|
|
222
|
+
while (Date.now() < deadline) {
|
|
223
|
+
if (!isPidAlive(pid)) return;
|
|
224
|
+
await sleep(TERMINATE_POLL_MS);
|
|
225
|
+
}
|
|
226
|
+
if (isPidAlive(pid)) signalPid(pid, 'SIGKILL');
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/* ---------------------------------- ports --------------------------------- */
|
|
230
|
+
|
|
231
|
+
const portOf = (value) => {
|
|
232
|
+
const token = String(value).replace(/\(\w+\)/g, '').trim();
|
|
233
|
+
const index = token.lastIndexOf(':');
|
|
234
|
+
if (index < 0) return null;
|
|
235
|
+
const port = Number.parseInt(token.slice(index + 1), 10);
|
|
236
|
+
return Number.isInteger(port) && port > 0 && port <= 65535 ? port : null;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/** `lsof -nP -iTCP -sTCP:LISTEN` rows: `COMMAND PID … NAME`. */
|
|
240
|
+
export function parseLsofListeners(output) {
|
|
241
|
+
const rows = [];
|
|
242
|
+
const seen = new Set();
|
|
243
|
+
for (const line of String(output ?? '').split('\n')) {
|
|
244
|
+
const trimmed = line.trim();
|
|
245
|
+
if (!trimmed || /^COMMAND\s/.test(trimmed)) continue;
|
|
246
|
+
const fields = trimmed.split(/\s+/);
|
|
247
|
+
if (fields.length < 9) continue;
|
|
248
|
+
const pid = Number.parseInt(fields[1], 10);
|
|
249
|
+
if (!Number.isInteger(pid) || pid <= 0) continue;
|
|
250
|
+
let name = null;
|
|
251
|
+
for (let index = fields.length - 1; index >= 0; index -= 1) {
|
|
252
|
+
if (fields[index].includes(':')) {
|
|
253
|
+
name = fields[index];
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
const port = name === null ? null : portOf(name);
|
|
258
|
+
if (port === null || seen.has(`${pid}:${port}`)) continue;
|
|
259
|
+
seen.add(`${pid}:${port}`);
|
|
260
|
+
rows.push({ pid, port, address: name });
|
|
261
|
+
}
|
|
262
|
+
return rows;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** `ss -ltnp` rows: `LISTEN 0 511 127.0.0.1:3000 0.0.0.0:* users:(("node",pid=42,fd=23))`. */
|
|
266
|
+
export function parseSsListeners(output) {
|
|
267
|
+
const rows = [];
|
|
268
|
+
const seen = new Set();
|
|
269
|
+
for (const line of String(output ?? '').split('\n')) {
|
|
270
|
+
const trimmed = line.trim();
|
|
271
|
+
if (!trimmed || !/^LISTEN\b/.test(trimmed)) continue;
|
|
272
|
+
const fields = trimmed.split(/\s+/);
|
|
273
|
+
const port = portOf(fields[3] ?? '');
|
|
274
|
+
if (port === null) continue;
|
|
275
|
+
const match = /pid=(\d+)/.exec(trimmed);
|
|
276
|
+
const pid = match ? Number.parseInt(match[1], 10) : null;
|
|
277
|
+
if (seen.has(`${pid}:${port}`)) continue;
|
|
278
|
+
seen.add(`${pid}:${port}`);
|
|
279
|
+
rows.push({ pid, port, address: fields[3] });
|
|
280
|
+
}
|
|
281
|
+
return rows;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** `ps -axo pid=,command=` rows, narrowed to the known command shapes. */
|
|
285
|
+
export function parsePsTable(output) {
|
|
286
|
+
const rows = [];
|
|
287
|
+
for (const line of String(output ?? '').split('\n')) {
|
|
288
|
+
const match = /^\s*(\d+)\s+(.+)$/.exec(line);
|
|
289
|
+
if (!match) continue;
|
|
290
|
+
const pid = Number.parseInt(match[1], 10);
|
|
291
|
+
const command = match[2].trim();
|
|
292
|
+
const known = KNOWN_COMMANDS.find((shape) => shape.pattern.test(command));
|
|
293
|
+
if (!known || !Number.isInteger(pid) || pid <= 0) continue;
|
|
294
|
+
rows.push({ pid, command, shape: known.id });
|
|
295
|
+
}
|
|
296
|
+
return rows;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** `null` means "this host cannot report listeners", never "no listeners". */
|
|
300
|
+
export function listListeningPorts() {
|
|
301
|
+
const lsof = spawnSync('lsof', ['-nP', '-iTCP', '-sTCP:LISTEN'], { encoding: 'utf8' });
|
|
302
|
+
const fromLsof = listenersFromLsof(lsof);
|
|
303
|
+
if (fromLsof !== null) return fromLsof;
|
|
304
|
+
const ss = spawnSync('ss', ['-ltnp'], { encoding: 'utf8' });
|
|
305
|
+
if (!ss.error && ss.status === 0) return parseSsListeners(ss.stdout);
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* `lsof` exits 1 both for "no matching process" and for a failed lookup, so the
|
|
311
|
+
* no-match shape (exit 1, empty stdout) is an empty set — otherwise a host with
|
|
312
|
+
* zero listeners would silently disable `doctor`'s lease-port rule.
|
|
313
|
+
*/
|
|
314
|
+
export function listenersFromLsof(result) {
|
|
315
|
+
if (result.error) return null;
|
|
316
|
+
if (result.status === 0) return parseLsofListeners(result.stdout);
|
|
317
|
+
if (result.status === 1 && String(result.stdout ?? '').trim() === '') return [];
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function knownProcesses() {
|
|
322
|
+
const result = spawnSync('ps', ['-axo', 'pid=,command='], { encoding: 'utf8' });
|
|
323
|
+
if (result.error || result.status !== 0) return null;
|
|
324
|
+
return parsePsTable(result.stdout);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/* -------------------------------- adapters -------------------------------- */
|
|
328
|
+
|
|
329
|
+
const readJsonFile = (file) => {
|
|
330
|
+
try {
|
|
331
|
+
return { value: JSON.parse(fs.readFileSync(file, 'utf8')) };
|
|
332
|
+
} catch (error) {
|
|
333
|
+
if (error instanceof Error && error.code === 'ENOENT') return { missing: true };
|
|
334
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const entry = ({
|
|
339
|
+
name, source, adapter = null, kind = 'exclusive', state, why = null, pid = null,
|
|
340
|
+
port = null, task = null, agent = null, label = null, command = null, cwd = null,
|
|
341
|
+
startedAt = null, heartbeatAt = null,
|
|
342
|
+
}) => {
|
|
343
|
+
const started = Date.parse(startedAt ?? '');
|
|
344
|
+
return {
|
|
345
|
+
name,
|
|
346
|
+
source,
|
|
347
|
+
adapter,
|
|
348
|
+
kind,
|
|
349
|
+
state,
|
|
350
|
+
why,
|
|
351
|
+
pid,
|
|
352
|
+
port,
|
|
353
|
+
task,
|
|
354
|
+
agent,
|
|
355
|
+
label,
|
|
356
|
+
command,
|
|
357
|
+
cwd,
|
|
358
|
+
startedAt: Number.isNaN(started) ? null : new Date(started).toISOString(),
|
|
359
|
+
ageMs: Number.isNaN(started) ? null : Math.max(0, Date.now() - started),
|
|
360
|
+
heartbeatAt,
|
|
361
|
+
};
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const aliveState = (pid) => (isPidAlive(pid) ? 'live' : 'stale');
|
|
365
|
+
|
|
366
|
+
const collectLeases = (roots, processes, warnings) => {
|
|
367
|
+
for (const lease of listLeases(roots.aiLocalRoot)) {
|
|
368
|
+
const record = lease.record;
|
|
369
|
+
if (record === null) {
|
|
370
|
+
warnings.push({
|
|
371
|
+
source: 'lease',
|
|
372
|
+
path: lease.file,
|
|
373
|
+
reason: 'a lease file is unreadable or not JSON',
|
|
374
|
+
severity: 'error',
|
|
375
|
+
});
|
|
376
|
+
processes.push(entry({ name: lease.name, source: 'lease', state: 'invalid', why: 'unparseable' }));
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
const state = leaseState(record);
|
|
380
|
+
const owner = isRecord(record.owner) ? record.owner : {};
|
|
381
|
+
processes.push(
|
|
382
|
+
entry({
|
|
383
|
+
name: lease.name,
|
|
384
|
+
source: 'lease',
|
|
385
|
+
kind: typeof record.kind === 'string' ? record.kind : 'exclusive',
|
|
386
|
+
state: state.state,
|
|
387
|
+
why: state.why,
|
|
388
|
+
pid: owner.pid ?? null,
|
|
389
|
+
port: record.port ?? null,
|
|
390
|
+
task: owner.task ?? null,
|
|
391
|
+
agent: owner.agent ?? null,
|
|
392
|
+
label: owner.label ?? null,
|
|
393
|
+
command: owner.command ?? null,
|
|
394
|
+
cwd: owner.cwd ?? null,
|
|
395
|
+
startedAt: owner.startedAt ?? null,
|
|
396
|
+
heartbeatAt: owner.heartbeatAt ?? null,
|
|
397
|
+
}),
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
const collectPresetTree = (roots, processes, warnings) => {
|
|
403
|
+
const file = path.join(roots.repoRoot, PRESET_LOCK_REL);
|
|
404
|
+
const read = readJsonFile(file);
|
|
405
|
+
if (read.missing) {
|
|
406
|
+
warnings.push({ source: 'preset-tree', path: file, reason: 'missing', severity: 'info' });
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (read.error || !isRecord(read.value) || !Number.isInteger(read.value.pid)) {
|
|
410
|
+
warnings.push({
|
|
411
|
+
source: 'preset-tree',
|
|
412
|
+
path: file,
|
|
413
|
+
reason: read.error ?? 'the owner record has no pid',
|
|
414
|
+
severity: 'error',
|
|
415
|
+
});
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
const owner = read.value;
|
|
419
|
+
processes.push(
|
|
420
|
+
entry({
|
|
421
|
+
name: 'preset-tree',
|
|
422
|
+
source: 'adapter',
|
|
423
|
+
adapter: 'preset-tree',
|
|
424
|
+
state: aliveState(owner.pid),
|
|
425
|
+
why: isPidAlive(owner.pid) ? 'pid-alive' : 'pid-dead',
|
|
426
|
+
pid: owner.pid,
|
|
427
|
+
label: owner.label ?? null,
|
|
428
|
+
startedAt: owner.startedAt ?? null,
|
|
429
|
+
}),
|
|
430
|
+
);
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
const collectDevServers = (roots, processes, warnings) => {
|
|
434
|
+
const dir = path.join(roots.aiLocalRoot, '.dev');
|
|
435
|
+
let files;
|
|
436
|
+
try {
|
|
437
|
+
files = fs.readdirSync(dir).filter((file) => file.endsWith('.json')).sort();
|
|
438
|
+
} catch {
|
|
439
|
+
warnings.push({ source: 'dev', path: dir, reason: 'missing', severity: 'info' });
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
for (const file of files) {
|
|
443
|
+
const target = path.join(dir, file);
|
|
444
|
+
const read = readJsonFile(target);
|
|
445
|
+
if (read.error || !isRecord(read.value) || !Number.isInteger(read.value.pid)) {
|
|
446
|
+
warnings.push({
|
|
447
|
+
source: 'dev',
|
|
448
|
+
path: target,
|
|
449
|
+
reason: read.error ?? 'the marker has no pid',
|
|
450
|
+
severity: 'error',
|
|
451
|
+
});
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
const marker = read.value;
|
|
455
|
+
const name = typeof marker.name === 'string' ? marker.name : file.slice(0, -'.json'.length);
|
|
456
|
+
processes.push(
|
|
457
|
+
entry({
|
|
458
|
+
name: `dev:${name}`,
|
|
459
|
+
source: 'adapter',
|
|
460
|
+
adapter: 'dev',
|
|
461
|
+
kind: 'port',
|
|
462
|
+
state: aliveState(marker.pid),
|
|
463
|
+
why: isPidAlive(marker.pid) ? 'pid-alive' : 'pid-dead',
|
|
464
|
+
pid: marker.pid,
|
|
465
|
+
port: Number.isInteger(marker.port) && marker.port > 0 ? marker.port : null,
|
|
466
|
+
task: marker.task ?? null,
|
|
467
|
+
command: marker.command ?? null,
|
|
468
|
+
cwd: marker.cwd ?? null,
|
|
469
|
+
startedAt: marker.startedAt ?? null,
|
|
470
|
+
}),
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
const collectTeamDb = (roots, processes, warnings) => {
|
|
476
|
+
const file = path.join(roots.aiLocalRoot, '.ui', 'api.json');
|
|
477
|
+
const read = readJsonFile(file);
|
|
478
|
+
if (read.missing) {
|
|
479
|
+
warnings.push({ source: 'team-db-serve', path: file, reason: 'missing', severity: 'info' });
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
if (read.error || !isRecord(read.value) || !Number.isInteger(read.value.pid)) {
|
|
483
|
+
warnings.push({
|
|
484
|
+
source: 'team-db-serve',
|
|
485
|
+
path: file,
|
|
486
|
+
reason: read.error ?? 'the marker has no pid',
|
|
487
|
+
severity: 'error',
|
|
488
|
+
});
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
const marker = read.value;
|
|
492
|
+
processes.push(
|
|
493
|
+
entry({
|
|
494
|
+
name: 'team-db-serve',
|
|
495
|
+
source: 'adapter',
|
|
496
|
+
adapter: 'team-db-serve',
|
|
497
|
+
kind: 'port',
|
|
498
|
+
state: aliveState(marker.pid),
|
|
499
|
+
why: isPidAlive(marker.pid) ? 'pid-alive' : 'pid-dead',
|
|
500
|
+
pid: marker.pid,
|
|
501
|
+
port: Number.isInteger(marker.port) && marker.port > 0 ? marker.port : null,
|
|
502
|
+
startedAt: marker.startedAt ?? null,
|
|
503
|
+
}),
|
|
504
|
+
);
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
const collectScheduler = (roots, processes, warnings) => {
|
|
508
|
+
const file = path.join(roots.aiLocalRoot, 'team', 'scheduler', 'state.json');
|
|
509
|
+
const read = readJsonFile(file);
|
|
510
|
+
if (read.missing) {
|
|
511
|
+
warnings.push({ source: 'scheduler', path: file, reason: 'missing', severity: 'info' });
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if (read.error || !isRecord(read.value)) {
|
|
515
|
+
warnings.push({ source: 'scheduler', path: file, reason: read.error ?? 'not an object', severity: 'error' });
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
const lease = isRecord(read.value.lease) ? read.value.lease : read.value;
|
|
519
|
+
if (!Number.isInteger(lease.pid) || lease.pid <= 0) return;
|
|
520
|
+
processes.push(
|
|
521
|
+
entry({
|
|
522
|
+
name: 'scheduler',
|
|
523
|
+
source: 'adapter',
|
|
524
|
+
adapter: 'scheduler',
|
|
525
|
+
state: aliveState(lease.pid),
|
|
526
|
+
why: isPidAlive(lease.pid) ? 'pid-alive' : 'pid-dead',
|
|
527
|
+
pid: lease.pid,
|
|
528
|
+
startedAt: lease.startedAt ?? null,
|
|
529
|
+
heartbeatAt: lease.heartbeatAt ?? null,
|
|
530
|
+
}),
|
|
531
|
+
);
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
const runManifestFiles = (taskRoot) => {
|
|
535
|
+
const files = [path.join(taskRoot, 'run.json')];
|
|
536
|
+
try {
|
|
537
|
+
for (const child of fs.readdirSync(taskRoot, { withFileTypes: true })) {
|
|
538
|
+
if (child.isDirectory()) files.push(path.join(taskRoot, child.name, 'run.json'));
|
|
539
|
+
}
|
|
540
|
+
} catch {
|
|
541
|
+
/* an unreadable task directory contributes nothing */
|
|
542
|
+
}
|
|
543
|
+
return files;
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
const collectRunners = (roots, processes, warnings) => {
|
|
547
|
+
const dir = path.join(roots.aiLocalRoot, 'team', 'task-runs');
|
|
548
|
+
let taskDirs;
|
|
549
|
+
try {
|
|
550
|
+
taskDirs = fs.readdirSync(dir, { withFileTypes: true }).filter((item) => item.isDirectory());
|
|
551
|
+
} catch {
|
|
552
|
+
warnings.push({ source: 'runner', path: dir, reason: 'missing', severity: 'info' });
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
for (const taskDir of taskDirs) {
|
|
556
|
+
for (const file of runManifestFiles(path.join(dir, taskDir.name))) {
|
|
557
|
+
const read = readJsonFile(file);
|
|
558
|
+
if (read.missing) continue;
|
|
559
|
+
if (read.error || !isRecord(read.value)) {
|
|
560
|
+
warnings.push({ source: 'runner', path: file, reason: read.error ?? 'not an object', severity: 'error' });
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
const manifest = read.value;
|
|
564
|
+
if (!ACTIVE_RUN_STATES.has(manifest.state)) continue;
|
|
565
|
+
const pid = Number.isInteger(manifest.pid) && manifest.pid > 0 ? manifest.pid : null;
|
|
566
|
+
processes.push(
|
|
567
|
+
entry({
|
|
568
|
+
name: `runner:${typeof manifest.taskSlug === 'string' ? manifest.taskSlug : taskDir.name}`,
|
|
569
|
+
source: 'adapter',
|
|
570
|
+
adapter: 'runner',
|
|
571
|
+
state: pid === null ? 'live' : aliveState(pid),
|
|
572
|
+
why: pid === null ? 'run-active' : isPidAlive(pid) ? 'pid-alive' : 'pid-dead',
|
|
573
|
+
pid,
|
|
574
|
+
task: manifest.taskSlug ?? taskDir.name,
|
|
575
|
+
startedAt: manifest.startedAt ?? manifest.createdAt ?? null,
|
|
576
|
+
}),
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
const collectGitIndex = (roots, processes, warnings) => {
|
|
583
|
+
const file = path.join(roots.repoRoot, '.git', 'index.lock');
|
|
584
|
+
if (!fs.existsSync(file)) {
|
|
585
|
+
warnings.push({ source: 'git-index', path: file, reason: 'missing', severity: 'info' });
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
processes.push(
|
|
589
|
+
entry({ name: 'git-index', source: 'adapter', adapter: 'git-index', kind: 'advisory', state: 'live', why: 'lock-file-present' }),
|
|
590
|
+
);
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
/* -------------------------------- snapshot -------------------------------- */
|
|
594
|
+
|
|
595
|
+
export function buildSnapshot(roots) {
|
|
596
|
+
const processes = [];
|
|
597
|
+
const warnings = [];
|
|
598
|
+
for (const collect of [
|
|
599
|
+
collectLeases,
|
|
600
|
+
collectPresetTree,
|
|
601
|
+
collectDevServers,
|
|
602
|
+
collectTeamDb,
|
|
603
|
+
collectScheduler,
|
|
604
|
+
collectRunners,
|
|
605
|
+
collectGitIndex,
|
|
606
|
+
]) {
|
|
607
|
+
try {
|
|
608
|
+
collect(roots, processes, warnings);
|
|
609
|
+
} catch (error) {
|
|
610
|
+
warnings.push({
|
|
611
|
+
source: 'snapshot',
|
|
612
|
+
path: roots.aiLocalRoot,
|
|
613
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
614
|
+
severity: 'error',
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
processes.sort((left, right) => left.name.localeCompare(right.name));
|
|
619
|
+
return {
|
|
620
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
621
|
+
generatedAt: new Date().toISOString(),
|
|
622
|
+
root: roots.repoRoot,
|
|
623
|
+
processes,
|
|
624
|
+
warnings,
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const filterSnapshot = (snapshot, flags) => {
|
|
629
|
+
if (!flags.task && !flags.live) return snapshot;
|
|
630
|
+
return {
|
|
631
|
+
...snapshot,
|
|
632
|
+
processes: snapshot.processes.filter(
|
|
633
|
+
(item) => (!flags.task || item.task === flags.task) && (!flags.live || item.state === 'live'),
|
|
634
|
+
),
|
|
635
|
+
};
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
const hardWarnings = (snapshot) => snapshot.warnings.filter((item) => item.severity !== 'info');
|
|
639
|
+
|
|
640
|
+
/* -------------------------------- rendering ------------------------------- */
|
|
641
|
+
|
|
642
|
+
const ageLabel = (ms) => {
|
|
643
|
+
if (!Number.isFinite(ms) || ms < 0) return '-';
|
|
644
|
+
const seconds = Math.floor(ms / 1000);
|
|
645
|
+
if (seconds < 60) return `${seconds}s`;
|
|
646
|
+
const minutes = Math.floor(seconds / 60);
|
|
647
|
+
if (minutes < 60) return `${minutes}m${String(seconds % 60).padStart(2, '0')}s`;
|
|
648
|
+
const hours = Math.floor(minutes / 60);
|
|
649
|
+
return `${hours}h${String(minutes % 60).padStart(2, '0')}m`;
|
|
650
|
+
};
|
|
651
|
+
|
|
652
|
+
const rowCells = (item) => [
|
|
653
|
+
item.name,
|
|
654
|
+
item.source,
|
|
655
|
+
item.adapter ?? '-',
|
|
656
|
+
item.kind,
|
|
657
|
+
item.state,
|
|
658
|
+
item.pid === null ? '-' : String(item.pid),
|
|
659
|
+
item.port === null ? '-' : String(item.port),
|
|
660
|
+
item.task ?? '-',
|
|
661
|
+
ageLabel(item.ageMs),
|
|
662
|
+
];
|
|
663
|
+
|
|
664
|
+
const TABLE_HEADER = ['NAME', 'SOURCE', 'ADAPTER', 'KIND', 'STATE', 'PID', 'PORT', 'TASK', 'AGE'];
|
|
665
|
+
|
|
666
|
+
const renderTable = (snapshot) => {
|
|
667
|
+
const lines = [`processes — ${snapshot.root} — ${snapshot.generatedAt}`];
|
|
668
|
+
if (snapshot.processes.length === 0) {
|
|
669
|
+
lines.push('nothing recorded');
|
|
670
|
+
} else {
|
|
671
|
+
const rows = [TABLE_HEADER, ...snapshot.processes.map(rowCells)];
|
|
672
|
+
const widths = TABLE_HEADER.map((_, column) =>
|
|
673
|
+
Math.max(...rows.map((row) => String(row[column] ?? '').length)),
|
|
674
|
+
);
|
|
675
|
+
for (const row of rows) {
|
|
676
|
+
lines.push(row.map((cell, column) => String(cell ?? '').padEnd(widths[column])).join(' ').trimEnd());
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
for (const warning of hardWarnings(snapshot)) {
|
|
680
|
+
lines.push(`warn: ${warning.source}: ${warning.reason}${warning.path ? ` (${warning.path})` : ''}`);
|
|
681
|
+
}
|
|
682
|
+
const skipped = snapshot.warnings.length - hardWarnings(snapshot).length;
|
|
683
|
+
if (skipped > 0) lines.push(`${skipped} source(s) not present`);
|
|
684
|
+
return lines.join('\n');
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
const renderJson = (snapshot) => JSON.stringify(snapshot);
|
|
688
|
+
|
|
689
|
+
const escapeHtml = (value) =>
|
|
690
|
+
String(value ?? '').replace(/[&<>"']/g, (character) => ({
|
|
691
|
+
'&': '&',
|
|
692
|
+
'<': '<',
|
|
693
|
+
'>': '>',
|
|
694
|
+
'"': '"',
|
|
695
|
+
"'": ''',
|
|
696
|
+
})[character]);
|
|
697
|
+
|
|
698
|
+
const renderHtml = (snapshot) => {
|
|
699
|
+
const rows = snapshot.processes
|
|
700
|
+
.map(
|
|
701
|
+
(item) =>
|
|
702
|
+
`<tr>${rowCells(item).map((cell) => `<td>${escapeHtml(cell)}</td>`).join('')}</tr>`,
|
|
703
|
+
)
|
|
704
|
+
.join('');
|
|
705
|
+
const warnings = hardWarnings(snapshot)
|
|
706
|
+
.map((warning) => `<p class="warn">${escapeHtml(warning.source)}: ${escapeHtml(warning.reason)}</p>`)
|
|
707
|
+
.join('');
|
|
708
|
+
return `<!doctype html>
|
|
709
|
+
<html lang="en"><head><meta charset="utf-8"><title>processes</title>
|
|
710
|
+
<style>
|
|
711
|
+
body{font:13px/1.5 ui-monospace,SFMono-Regular,Menlo,monospace;margin:2rem;color:#111}
|
|
712
|
+
table{border-collapse:collapse}
|
|
713
|
+
th,td{padding:.25rem .75rem;border-bottom:1px solid #e3e3e3;text-align:left}
|
|
714
|
+
th{font-weight:600;background:#fafafa}
|
|
715
|
+
.warn{color:#a33}
|
|
716
|
+
</style></head>
|
|
717
|
+
<body>
|
|
718
|
+
<h1>processes</h1>
|
|
719
|
+
<p>${escapeHtml(snapshot.root)} — ${escapeHtml(snapshot.generatedAt)}</p>
|
|
720
|
+
${snapshot.processes.length === 0 ? '<p>nothing recorded</p>' : `<table><thead><tr>${TABLE_HEADER.map((cell) => `<th>${cell}</th>`).join('')}</tr></thead><tbody>${rows}</tbody></table>`}
|
|
721
|
+
${warnings}
|
|
722
|
+
</body></html>
|
|
723
|
+
`;
|
|
724
|
+
};
|
|
725
|
+
|
|
726
|
+
/* ----------------------------------- cli ---------------------------------- */
|
|
727
|
+
|
|
728
|
+
const parseFlags = (verb, argv) => {
|
|
729
|
+
const allowed = VERB_FLAGS[verb];
|
|
730
|
+
const flags = {};
|
|
731
|
+
let tail = null;
|
|
732
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
733
|
+
const token = argv[index];
|
|
734
|
+
if (token === '--') {
|
|
735
|
+
tail = argv.slice(index + 1);
|
|
736
|
+
break;
|
|
737
|
+
}
|
|
738
|
+
if (!token.startsWith('--')) fail(`unexpected argument "${token}"`);
|
|
739
|
+
const key = token.slice(2);
|
|
740
|
+
if (!allowed.has(key)) fail(`unknown flag "--${key}" for "${verb}"`);
|
|
741
|
+
if (BOOLEAN_FLAGS.has(key)) {
|
|
742
|
+
flags[key] = true;
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
const value = argv[index + 1];
|
|
746
|
+
if (value === undefined || value.startsWith('--')) fail(`flag "--${key}" requires a value`);
|
|
747
|
+
flags[key] = value;
|
|
748
|
+
index += 1;
|
|
749
|
+
}
|
|
750
|
+
return { flags, tail };
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
const requireName = (flags) => {
|
|
754
|
+
if (!flags.name) fail('--name is required');
|
|
755
|
+
if (!isValidLeaseName(flags.name)) {
|
|
756
|
+
fail(`--name must be a lowercase resource name (a-z, 0-9, ".", "_", "-"), got "${flags.name}"`);
|
|
757
|
+
}
|
|
758
|
+
return flags.name;
|
|
759
|
+
};
|
|
760
|
+
|
|
761
|
+
const positiveInt = (value, flag, { max = Number.MAX_SAFE_INTEGER, allowZero = false } = {}) => {
|
|
762
|
+
if (!/^\d+$/.test(String(value))) fail(`${flag} must be an integer, got "${value}"`);
|
|
763
|
+
const parsed = Number.parseInt(String(value), 10);
|
|
764
|
+
if (parsed > max || (parsed === 0 && !allowZero)) fail(`${flag} is out of range: ${value}`);
|
|
765
|
+
return parsed;
|
|
766
|
+
};
|
|
767
|
+
|
|
768
|
+
const parseKind = (flags) => {
|
|
769
|
+
if (flags.kind === undefined) return 'exclusive';
|
|
770
|
+
if (!['exclusive', 'port', 'advisory'].includes(flags.kind)) {
|
|
771
|
+
fail('--kind must be exclusive, port, or advisory');
|
|
772
|
+
}
|
|
773
|
+
return flags.kind;
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
const parsePort = (flags, fallback) => {
|
|
777
|
+
if (flags.port === undefined) return fallback;
|
|
778
|
+
return positiveInt(flags.port, '--port', { max: 65535 });
|
|
779
|
+
};
|
|
780
|
+
|
|
781
|
+
const parseWait = (flags) => {
|
|
782
|
+
if (flags.wait === undefined) return 0;
|
|
783
|
+
return positiveInt(flags.wait, '--wait', { allowZero: true });
|
|
784
|
+
};
|
|
785
|
+
|
|
786
|
+
const parseInterval = (value) => {
|
|
787
|
+
if (value === undefined) return DEFAULT_WATCH_MS;
|
|
788
|
+
const match = /^(\d+)(ms|s)?$/.exec(String(value).trim());
|
|
789
|
+
if (!match) fail('--interval must be a duration like 500ms or 1s');
|
|
790
|
+
const amount = Number.parseInt(match[1], 10);
|
|
791
|
+
const ms = match[2] === 's' ? amount * 1000 : amount;
|
|
792
|
+
if (ms < SSE_MIN_INTERVAL_MS) fail(`--interval must be at least ${SSE_MIN_INTERVAL_MS}ms`);
|
|
793
|
+
return ms;
|
|
794
|
+
};
|
|
795
|
+
|
|
796
|
+
/**
|
|
797
|
+
* A lease taken from a short-lived CLI must name a process that outlives the
|
|
798
|
+
* command: the parent shell for `acquire`, the manager itself for `run` (alive
|
|
799
|
+
* for the whole child). A lease whose owner is not provably alive would be
|
|
800
|
+
* reclaimed by the next acquire, so it is refused instead of recorded dead.
|
|
801
|
+
*/
|
|
802
|
+
const requireLiveOwner = (flags, fallback) => {
|
|
803
|
+
const pid = flags.pid ? positiveInt(flags.pid, '--pid') : fallback;
|
|
804
|
+
if (!Number.isInteger(pid) || pid <= 1 || !isPidAlive(pid)) {
|
|
805
|
+
fail('acquire: a lease needs a live owner; pass --pid $$ (or use run)');
|
|
806
|
+
}
|
|
807
|
+
return pid;
|
|
808
|
+
};
|
|
809
|
+
|
|
810
|
+
const leaseOptions = (flags, roots, name, pid) => {
|
|
811
|
+
const port = parsePort(flags, null);
|
|
812
|
+
return {
|
|
813
|
+
aiLocalRoot: roots.aiLocalRoot,
|
|
814
|
+
name,
|
|
815
|
+
kind: flags.kind ? parseKind(flags) : port === null ? 'exclusive' : 'port',
|
|
816
|
+
port,
|
|
817
|
+
label: flags.label ?? null,
|
|
818
|
+
task: flags.task ?? null,
|
|
819
|
+
agent: flags.agent ?? null,
|
|
820
|
+
cwd: flags.cwd ? path.resolve(flags.cwd) : process.cwd(),
|
|
821
|
+
command: flags.command ?? null,
|
|
822
|
+
pid,
|
|
823
|
+
token: flags.token,
|
|
824
|
+
waitMs: parseWait(flags),
|
|
825
|
+
ttlMs: flags.ttl === undefined ? null : positiveInt(flags.ttl, '--ttl'),
|
|
826
|
+
allowUnlocked: flags['allow-unlocked'] === true,
|
|
827
|
+
};
|
|
828
|
+
};
|
|
829
|
+
|
|
830
|
+
const formatHeld = (result) =>
|
|
831
|
+
`${result.name} is ${result.reason ?? 'held'}; wait for it to finish, or inspect it with \`processes.mjs list\``;
|
|
832
|
+
|
|
833
|
+
const describeAcquire = (result) => {
|
|
834
|
+
if (result.acquired) {
|
|
835
|
+
const lines = [`processes: acquired ${result.name} (${result.path})`];
|
|
836
|
+
if (result.reason) lines.push(`processes: ${result.reason}`);
|
|
837
|
+
lines.push(`processes: release with --name ${result.name} --token ${result.token}`);
|
|
838
|
+
return lines;
|
|
839
|
+
}
|
|
840
|
+
return [];
|
|
841
|
+
};
|
|
842
|
+
|
|
843
|
+
/* ---------------------------------- verbs --------------------------------- */
|
|
844
|
+
|
|
845
|
+
const acquireVerb = (flags) => {
|
|
846
|
+
const roots = resolveRoots(flags);
|
|
847
|
+
const name = requireName(flags);
|
|
848
|
+
const result = acquireLease(leaseOptions(flags, roots, name, requireLiveOwner(flags, process.ppid)));
|
|
849
|
+
if (flags.json) {
|
|
850
|
+
out(renderJson(result));
|
|
851
|
+
return result.acquired || (result.fatal && result.skipped) ? 0 : result.fatal ? 3 : 1;
|
|
852
|
+
}
|
|
853
|
+
if (result.acquired) {
|
|
854
|
+
for (const line of describeAcquire(result)) out(line);
|
|
855
|
+
return 0;
|
|
856
|
+
}
|
|
857
|
+
if (result.fatal) {
|
|
858
|
+
if (result.skipped) {
|
|
859
|
+
out(`warn: processes: ${result.reason}; continuing unlocked`);
|
|
860
|
+
return 0;
|
|
861
|
+
}
|
|
862
|
+
err(`processes: fatal: ${result.reason}. Fix the state directory, or re-run with --allow-unlocked.`);
|
|
863
|
+
return 3;
|
|
864
|
+
}
|
|
865
|
+
err(`processes: ${formatHeld(result)}`);
|
|
866
|
+
return 1;
|
|
867
|
+
};
|
|
868
|
+
|
|
869
|
+
const releaseVerb = (flags) => {
|
|
870
|
+
const roots = resolveRoots(flags);
|
|
871
|
+
const name = requireName(flags);
|
|
872
|
+
const result = releaseLease({
|
|
873
|
+
aiLocalRoot: roots.aiLocalRoot,
|
|
874
|
+
name,
|
|
875
|
+
token: flags.token,
|
|
876
|
+
pid: flags.pid ? positiveInt(flags.pid, '--pid') : process.pid,
|
|
877
|
+
});
|
|
878
|
+
if (flags.json) {
|
|
879
|
+
out(renderJson({ ...result, name }));
|
|
880
|
+
return result.released ? 0 : 1;
|
|
881
|
+
}
|
|
882
|
+
if (!result.released) {
|
|
883
|
+
err(`processes: not released — ${result.reason}`);
|
|
884
|
+
return 1;
|
|
885
|
+
}
|
|
886
|
+
out(`processes: released ${name}`);
|
|
887
|
+
return 0;
|
|
888
|
+
};
|
|
889
|
+
|
|
890
|
+
const runVerb = async (flags, tail) => {
|
|
891
|
+
if (!tail || tail.length === 0) fail('run needs a command after --');
|
|
892
|
+
const roots = resolveRoots(flags);
|
|
893
|
+
const name = requireName(flags);
|
|
894
|
+
// `run` owns the lease for the child's lifetime, so the manager — not the
|
|
895
|
+
// parent shell — is the recorded holder.
|
|
896
|
+
const options = leaseOptions(flags, roots, name, flags.pid ? positiveInt(flags.pid, '--pid') : process.pid);
|
|
897
|
+
const command = flags.command ?? tail.join(' ');
|
|
898
|
+
const acquired = acquireLease({ ...options, command });
|
|
899
|
+
if (!acquired.acquired) {
|
|
900
|
+
if (acquired.fatal) {
|
|
901
|
+
if (!acquired.skipped) {
|
|
902
|
+
err(`processes: fatal: ${acquired.reason}`);
|
|
903
|
+
return 3;
|
|
904
|
+
}
|
|
905
|
+
out(`warn: processes: ${acquired.reason}; continuing unlocked`);
|
|
906
|
+
} else {
|
|
907
|
+
err(`processes: ${formatHeld(acquired)}`);
|
|
908
|
+
return 1;
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
let released = false;
|
|
913
|
+
const release = () => {
|
|
914
|
+
if (released) return;
|
|
915
|
+
released = true;
|
|
916
|
+
releaseLease({ aiLocalRoot: roots.aiLocalRoot, name, token: acquired.token, pid: options.pid });
|
|
917
|
+
};
|
|
918
|
+
process.on('exit', release);
|
|
919
|
+
|
|
920
|
+
const signo = (signal) => 128 + (os.constants.signals[signal] ?? 0);
|
|
921
|
+
let child = null;
|
|
922
|
+
let forwarded = null;
|
|
923
|
+
let killTimer = null;
|
|
924
|
+
/**
|
|
925
|
+
* A hook or CI timeout signals the manager, not the child: forward the same
|
|
926
|
+
* signal, give the child `TERMINATE_WAIT_MS`, then SIGKILL. The lease is
|
|
927
|
+
* released only after the child is gone — releasing first would let the next
|
|
928
|
+
* acquire run beside a live tree-mutating child, the race the lease exists
|
|
929
|
+
* to prevent. A repeated signal never re-forwards or double-releases.
|
|
930
|
+
*/
|
|
931
|
+
const forward = (signal) => {
|
|
932
|
+
if (forwarded !== null) return;
|
|
933
|
+
forwarded = signal;
|
|
934
|
+
if (child === null || child.exitCode !== null || child.signalCode !== null) return;
|
|
935
|
+
if (!signalPid(child.pid, signal)) return;
|
|
936
|
+
killTimer = setTimeout(() => {
|
|
937
|
+
try {
|
|
938
|
+
child.kill('SIGKILL');
|
|
939
|
+
} catch {
|
|
940
|
+
/* the child exited inside the grace window */
|
|
941
|
+
}
|
|
942
|
+
}, TERMINATE_WAIT_MS);
|
|
943
|
+
};
|
|
944
|
+
process.on('SIGINT', () => forward('SIGINT'));
|
|
945
|
+
process.on('SIGTERM', () => forward('SIGTERM'));
|
|
946
|
+
|
|
947
|
+
try {
|
|
948
|
+
const outcome = await new Promise((resolve) => {
|
|
949
|
+
try {
|
|
950
|
+
child = spawn(tail[0], tail.slice(1), {
|
|
951
|
+
cwd: options.cwd,
|
|
952
|
+
stdio: 'inherit',
|
|
953
|
+
env: process.env,
|
|
954
|
+
});
|
|
955
|
+
} catch (error) {
|
|
956
|
+
resolve({ error: error instanceof Error ? error.message : String(error) });
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
child.once('error', (error) => resolve({ error: error.message }));
|
|
960
|
+
child.once('exit', (code, signal) => {
|
|
961
|
+
if (killTimer !== null) clearTimeout(killTimer);
|
|
962
|
+
resolve({ code: signal ? signo(signal) : code ?? 1 });
|
|
963
|
+
});
|
|
964
|
+
});
|
|
965
|
+
if (outcome.error) {
|
|
966
|
+
err(`processes: could not run "${tail[0]}": ${outcome.error}`);
|
|
967
|
+
return 2;
|
|
968
|
+
}
|
|
969
|
+
// A forwarded signal is the caller-visible status: the manager was
|
|
970
|
+
// terminated, and it waited for the child before giving up the lease.
|
|
971
|
+
const code = forwarded === null ? outcome.code : signo(forwarded);
|
|
972
|
+
if (acquired.acquired) {
|
|
973
|
+
appendEvent({
|
|
974
|
+
aiLocalRoot: roots.aiLocalRoot,
|
|
975
|
+
type: 'exited',
|
|
976
|
+
name,
|
|
977
|
+
pid: options.pid,
|
|
978
|
+
code,
|
|
979
|
+
signal: forwarded,
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
return code;
|
|
983
|
+
} finally {
|
|
984
|
+
release();
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
const listVerb = (flags) => {
|
|
989
|
+
const roots = resolveRoots(flags);
|
|
990
|
+
const snapshot = filterSnapshot(buildSnapshot(roots), flags);
|
|
991
|
+
if (flags.json) {
|
|
992
|
+
out(renderJson(snapshot));
|
|
993
|
+
return 0;
|
|
994
|
+
}
|
|
995
|
+
out(renderTable(snapshot));
|
|
996
|
+
return 0;
|
|
997
|
+
};
|
|
998
|
+
|
|
999
|
+
const statusVerb = (flags) => {
|
|
1000
|
+
const roots = resolveRoots(flags);
|
|
1001
|
+
const name = requireName(flags);
|
|
1002
|
+
const record = readLease(roots.aiLocalRoot, name);
|
|
1003
|
+
const state = record === null ? null : leaseState(record);
|
|
1004
|
+
const payload = {
|
|
1005
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
1006
|
+
generatedAt: new Date().toISOString(),
|
|
1007
|
+
root: roots.repoRoot,
|
|
1008
|
+
name,
|
|
1009
|
+
path: leasePath(roots.aiLocalRoot, name),
|
|
1010
|
+
present: record !== null,
|
|
1011
|
+
state: state?.state ?? 'absent',
|
|
1012
|
+
why: state?.why ?? null,
|
|
1013
|
+
owner: isRecord(record?.owner) ? record.owner : null,
|
|
1014
|
+
};
|
|
1015
|
+
if (flags.json) {
|
|
1016
|
+
out(renderJson(payload));
|
|
1017
|
+
return 0;
|
|
1018
|
+
}
|
|
1019
|
+
if (record === null) {
|
|
1020
|
+
out(`processes: ${name} is not recorded`);
|
|
1021
|
+
return 0;
|
|
1022
|
+
}
|
|
1023
|
+
out(`processes: ${name} is ${payload.state} (${payload.why}), pid ${payload.owner?.pid ?? '?'}`);
|
|
1024
|
+
return 0;
|
|
1025
|
+
};
|
|
1026
|
+
|
|
1027
|
+
const watchVerb = (flags) => {
|
|
1028
|
+
const roots = resolveRoots(flags);
|
|
1029
|
+
const interval = parseInterval(flags.interval);
|
|
1030
|
+
const clear = process.stdout.isTTY === true && flags['no-color'] !== true && flags.json !== true;
|
|
1031
|
+
const render = () => {
|
|
1032
|
+
const snapshot = filterSnapshot(buildSnapshot(roots), flags);
|
|
1033
|
+
if (flags.json) {
|
|
1034
|
+
out(renderJson(snapshot));
|
|
1035
|
+
return;
|
|
1036
|
+
}
|
|
1037
|
+
if (clear) process.stdout.write('\u001b[2J\u001b[H');
|
|
1038
|
+
out(renderTable(snapshot));
|
|
1039
|
+
};
|
|
1040
|
+
render();
|
|
1041
|
+
if (flags.once) return 0;
|
|
1042
|
+
const timer = setInterval(render, interval);
|
|
1043
|
+
process.once('SIGINT', () => {
|
|
1044
|
+
clearInterval(timer);
|
|
1045
|
+
process.exit(0);
|
|
1046
|
+
});
|
|
1047
|
+
return null;
|
|
1048
|
+
};
|
|
1049
|
+
|
|
1050
|
+
const scanSnapshot = (roots, { all = false } = {}) => {
|
|
1051
|
+
const snapshot = buildSnapshot(roots);
|
|
1052
|
+
// Only a live lease covers a process: a stale lease on a reused pid proves
|
|
1053
|
+
// nothing, and labelling its listener `leased` would hide a foreign one.
|
|
1054
|
+
const owners = new Map(
|
|
1055
|
+
snapshot.processes
|
|
1056
|
+
.filter((item) => item.state === 'live' && Number.isInteger(item.pid))
|
|
1057
|
+
.map((item) => [item.pid, item.name]),
|
|
1058
|
+
);
|
|
1059
|
+
const listening = listListeningPorts();
|
|
1060
|
+
const commands = knownProcesses();
|
|
1061
|
+
const warnings = [];
|
|
1062
|
+
if (listening === null) warnings.push('listening TCP ports are unavailable (lsof / ss)');
|
|
1063
|
+
if (commands === null) warnings.push('the process table is unavailable (ps)');
|
|
1064
|
+
|
|
1065
|
+
const shapes = new Map((commands ?? []).map((row) => [row.pid, row.shape]));
|
|
1066
|
+
const ports = [];
|
|
1067
|
+
for (const row of listening ?? []) {
|
|
1068
|
+
const owner = row.pid === null ? null : owners.get(row.pid) ?? null;
|
|
1069
|
+
const shape = row.pid === null ? null : shapes.get(row.pid) ?? null;
|
|
1070
|
+
const state = owner === null ? 'foreign' : 'leased';
|
|
1071
|
+
if (!all && state === 'foreign' && shape === null) continue;
|
|
1072
|
+
ports.push({ port: row.port, pid: row.pid, state, owner, shape });
|
|
1073
|
+
}
|
|
1074
|
+
const portPids = new Set(ports.map((row) => row.pid).filter((pid) => pid !== null));
|
|
1075
|
+
const known = (commands ?? [])
|
|
1076
|
+
.filter((row) => !portPids.has(row.pid))
|
|
1077
|
+
.map((row) => ({
|
|
1078
|
+
pid: row.pid,
|
|
1079
|
+
state: owners.has(row.pid) ? 'leased' : 'foreign',
|
|
1080
|
+
owner: owners.get(row.pid) ?? null,
|
|
1081
|
+
shape: row.shape,
|
|
1082
|
+
command: row.command,
|
|
1083
|
+
}));
|
|
1084
|
+
return {
|
|
1085
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
1086
|
+
generatedAt: new Date().toISOString(),
|
|
1087
|
+
root: roots.repoRoot,
|
|
1088
|
+
ports,
|
|
1089
|
+
commands: known,
|
|
1090
|
+
warnings,
|
|
1091
|
+
};
|
|
1092
|
+
};
|
|
1093
|
+
|
|
1094
|
+
const scanVerb = (flags) => {
|
|
1095
|
+
const roots = resolveRoots(flags);
|
|
1096
|
+
const snapshot = scanSnapshot(roots, { all: flags.all === true });
|
|
1097
|
+
if (flags.json) {
|
|
1098
|
+
out(renderJson(snapshot));
|
|
1099
|
+
return 0;
|
|
1100
|
+
}
|
|
1101
|
+
const lines = [`processes: ${snapshot.ports.length} listener(s), ${snapshot.commands.length} known command(s)`];
|
|
1102
|
+
for (const row of snapshot.ports) {
|
|
1103
|
+
lines.push(
|
|
1104
|
+
`${row.state.padEnd(8)} ${String(row.port).padEnd(6)} pid ${row.pid ?? '?'}${row.owner ? ` ${row.owner}` : ''}${row.shape ? ` (${row.shape})` : ''}`,
|
|
1105
|
+
);
|
|
1106
|
+
}
|
|
1107
|
+
for (const row of snapshot.commands) {
|
|
1108
|
+
lines.push(`${row.state.padEnd(8)} ${'-'.padEnd(6)} pid ${row.pid} ${row.shape} ${row.command}`);
|
|
1109
|
+
}
|
|
1110
|
+
for (const warning of snapshot.warnings) lines.push(`warn: ${warning}`);
|
|
1111
|
+
out(lines.join('\n'));
|
|
1112
|
+
return 0;
|
|
1113
|
+
};
|
|
1114
|
+
|
|
1115
|
+
const doctorVerb = (flags) => {
|
|
1116
|
+
const roots = resolveRoots(flags);
|
|
1117
|
+
const snapshot = buildSnapshot(roots);
|
|
1118
|
+
const issues = [];
|
|
1119
|
+
for (const item of snapshot.processes) {
|
|
1120
|
+
if (item.source === 'lease' && item.state !== 'live') {
|
|
1121
|
+
issues.push({ source: item.source, name: item.name, pid: item.pid, reason: `lease is ${item.state} (${item.why})` });
|
|
1122
|
+
continue;
|
|
1123
|
+
}
|
|
1124
|
+
// Every adapter marker is an owner claim: a dead pid in any of
|
|
1125
|
+
// preset-tree / dev / team-db-serve / scheduler / runner is an issue.
|
|
1126
|
+
if (item.source === 'adapter' && item.state === 'stale') {
|
|
1127
|
+
issues.push({
|
|
1128
|
+
source: item.adapter ?? 'adapter',
|
|
1129
|
+
name: item.name,
|
|
1130
|
+
pid: item.pid,
|
|
1131
|
+
reason: 'marker points at a dead pid',
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
const warnings = hardWarnings(snapshot).map(
|
|
1137
|
+
(warning) => `${warning.source}: ${warning.reason}${warning.path ? ` (${warning.path})` : ''}`,
|
|
1138
|
+
);
|
|
1139
|
+
const listening = listListeningPorts();
|
|
1140
|
+
if (listening === null) {
|
|
1141
|
+
warnings.push('listening TCP ports are unavailable (lsof / ss); lease ports were not checked');
|
|
1142
|
+
} else {
|
|
1143
|
+
const seen = new Set(listening.map((row) => row.port));
|
|
1144
|
+
for (const item of snapshot.processes) {
|
|
1145
|
+
if (item.source !== 'lease' || item.port === null) continue;
|
|
1146
|
+
if (!seen.has(item.port)) {
|
|
1147
|
+
issues.push({
|
|
1148
|
+
source: 'lease',
|
|
1149
|
+
name: item.name,
|
|
1150
|
+
pid: item.pid,
|
|
1151
|
+
reason: `lease port ${item.port} has no listener`,
|
|
1152
|
+
});
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
for (const item of snapshot.processes) {
|
|
1157
|
+
if (item.source === 'lease' && item.why === 'start-time-unavailable') {
|
|
1158
|
+
warnings.push(`${item.name}: the process start time is unavailable on this host; liveness is pid-only`);
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
const payload = {
|
|
1163
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
1164
|
+
generatedAt: new Date().toISOString(),
|
|
1165
|
+
root: roots.repoRoot,
|
|
1166
|
+
ok: issues.length === 0,
|
|
1167
|
+
issues,
|
|
1168
|
+
warnings,
|
|
1169
|
+
};
|
|
1170
|
+
if (flags.json) {
|
|
1171
|
+
out(renderJson(payload));
|
|
1172
|
+
return issues.length === 0 ? 0 : 1;
|
|
1173
|
+
}
|
|
1174
|
+
for (const issue of issues) {
|
|
1175
|
+
err(`processes: doctor: ${issue.name}: ${issue.reason}`);
|
|
1176
|
+
}
|
|
1177
|
+
for (const warning of warnings) {
|
|
1178
|
+
err(`processes: doctor: warn: ${warning}`);
|
|
1179
|
+
}
|
|
1180
|
+
if (issues.length === 0) {
|
|
1181
|
+
out('processes: doctor: no issues');
|
|
1182
|
+
return 0;
|
|
1183
|
+
}
|
|
1184
|
+
return 1;
|
|
1185
|
+
};
|
|
1186
|
+
|
|
1187
|
+
/**
|
|
1188
|
+
* The command-line half of the ownership proof compares the recorded command to
|
|
1189
|
+
* the live one. Quote characters are shell syntax — `start --command 'node -e
|
|
1190
|
+
* "x"'` reaches the child as `node -e x` — so they are dropped on both sides.
|
|
1191
|
+
* The recorded command must still be a substantive substring of the live one.
|
|
1192
|
+
*/
|
|
1193
|
+
const commandForProof = (value) => normalize(value).replace(/["']/g, '');
|
|
1194
|
+
|
|
1195
|
+
const proveOwnership = (record) => {
|
|
1196
|
+
const owner = isRecord(record?.owner) ? record.owner : null;
|
|
1197
|
+
if (!isRecord(owner) || !Number.isInteger(owner.pid) || owner.pid <= 0) {
|
|
1198
|
+
return { ok: false, reason: 'the lease has no owner pid' };
|
|
1199
|
+
}
|
|
1200
|
+
if (!isPidAlive(owner.pid)) {
|
|
1201
|
+
return { ok: false, reason: `pid ${owner.pid} is not alive` };
|
|
1202
|
+
}
|
|
1203
|
+
if (owner.pidStart) {
|
|
1204
|
+
const live = processStartTime(owner.pid);
|
|
1205
|
+
if (live !== null && normalize(live) !== normalize(owner.pidStart)) {
|
|
1206
|
+
return { ok: false, reason: `pid ${owner.pid} was reused (start time ${live}, recorded ${owner.pidStart})` };
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
const command = liveCommand(owner.pid);
|
|
1210
|
+
if (command !== null && typeof owner.command === 'string') {
|
|
1211
|
+
const recorded = commandForProof(owner.command);
|
|
1212
|
+
if (recorded !== '' && commandForProof(command).includes(recorded)) return { ok: true, by: 'command' };
|
|
1213
|
+
}
|
|
1214
|
+
const port = record?.port;
|
|
1215
|
+
if (Number.isInteger(port) && port > 0 && listenPids(port).includes(owner.pid)) {
|
|
1216
|
+
return { ok: true, by: 'port' };
|
|
1217
|
+
}
|
|
1218
|
+
return { ok: false, reason: 'the live pid matches neither the recorded command nor the lease port' };
|
|
1219
|
+
};
|
|
1220
|
+
|
|
1221
|
+
const stopOneLease = async (roots, name, record, { force = false } = {}) => {
|
|
1222
|
+
const state = leaseState(record);
|
|
1223
|
+
if (state.state !== 'live') {
|
|
1224
|
+
try {
|
|
1225
|
+
fs.rmSync(leasePath(roots.aiLocalRoot, name), { force: true });
|
|
1226
|
+
} catch {
|
|
1227
|
+
/* the next acquire reclaims a lease file that cannot be removed */
|
|
1228
|
+
}
|
|
1229
|
+
appendEvent({
|
|
1230
|
+
aiLocalRoot: roots.aiLocalRoot,
|
|
1231
|
+
type: 'cleared',
|
|
1232
|
+
name,
|
|
1233
|
+
reason: state.why,
|
|
1234
|
+
});
|
|
1235
|
+
return { name, stopped: false, cleared: true, pid: record?.owner?.pid ?? null };
|
|
1236
|
+
}
|
|
1237
|
+
const proof = proveOwnership(record);
|
|
1238
|
+
if (!proof.ok) {
|
|
1239
|
+
appendEvent({ aiLocalRoot: roots.aiLocalRoot, type: 'refused', name, reason: proof.reason });
|
|
1240
|
+
return { name, stopped: false, refused: true, pid: record.owner.pid, reason: proof.reason };
|
|
1241
|
+
}
|
|
1242
|
+
const pid = record.owner.pid;
|
|
1243
|
+
await terminate(pid, { force });
|
|
1244
|
+
// Re-read before unlinking: during the kill window a supervisor can restart
|
|
1245
|
+
// the process, reclaim the lease and write a new token. Unlinking by path
|
|
1246
|
+
// would delete the fresh holder's lease and let a third process in.
|
|
1247
|
+
const cleared = releaseLease({
|
|
1248
|
+
aiLocalRoot: roots.aiLocalRoot,
|
|
1249
|
+
name,
|
|
1250
|
+
token: record.owner.token,
|
|
1251
|
+
pid,
|
|
1252
|
+
});
|
|
1253
|
+
appendEvent({ aiLocalRoot: roots.aiLocalRoot, type: 'stopped', name, pid, by: proof.by });
|
|
1254
|
+
return { name, stopped: true, pid, by: proof.by, cleared: cleared.released };
|
|
1255
|
+
};
|
|
1256
|
+
|
|
1257
|
+
const stopVerb = async (flags) => {
|
|
1258
|
+
const roots = resolveRoots(flags);
|
|
1259
|
+
const names = [];
|
|
1260
|
+
if (typeof flags.task === 'string' && flags.task !== '') {
|
|
1261
|
+
for (const entry of listLeases(roots.aiLocalRoot)) {
|
|
1262
|
+
if (entry.record?.owner?.task === flags.task) names.push(entry.name);
|
|
1263
|
+
}
|
|
1264
|
+
} else {
|
|
1265
|
+
if (!flags.name) fail('stop needs --name <res> or --task <slug>');
|
|
1266
|
+
names.push(requireName(flags));
|
|
1267
|
+
}
|
|
1268
|
+
if (names.length === 0) {
|
|
1269
|
+
if (flags.json) {
|
|
1270
|
+
out(renderJson({ task: flags.task ?? null, results: [] }));
|
|
1271
|
+
return 0;
|
|
1272
|
+
}
|
|
1273
|
+
out(`processes: nothing to stop${flags.task ? ` for task ${flags.task}` : ''}`);
|
|
1274
|
+
return 0;
|
|
1275
|
+
}
|
|
1276
|
+
const results = [];
|
|
1277
|
+
for (const name of names) {
|
|
1278
|
+
const record = readLease(roots.aiLocalRoot, name);
|
|
1279
|
+
if (record === null) {
|
|
1280
|
+
results.push({ name, stopped: false, cleared: true, reason: 'not recorded' });
|
|
1281
|
+
continue;
|
|
1282
|
+
}
|
|
1283
|
+
results.push(await stopOneLease(roots, name, record, { force: flags.force === true }));
|
|
1284
|
+
}
|
|
1285
|
+
if (flags.json) {
|
|
1286
|
+
out(renderJson(flags.task ? { task: flags.task, results } : results[0]));
|
|
1287
|
+
} else {
|
|
1288
|
+
for (const row of results) {
|
|
1289
|
+
if (row.stopped) out(`processes: stopped ${row.name} (pid ${row.pid}, proven by ${row.by})`);
|
|
1290
|
+
else if (row.refused) err(`processes: stop: refused: ${row.name}: ${row.reason}`);
|
|
1291
|
+
else out(`processes: cleared stale lease ${row.name}`);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
return results.some((row) => row.refused) ? 4 : 0;
|
|
1295
|
+
};
|
|
1296
|
+
|
|
1297
|
+
const parseReadyWait = (flags) => {
|
|
1298
|
+
if (flags['wait-ms'] === undefined) return START_TIMEOUT_MS;
|
|
1299
|
+
return positiveInt(flags['wait-ms'], '--wait-ms', { allowZero: true });
|
|
1300
|
+
};
|
|
1301
|
+
|
|
1302
|
+
const probePort = (port) =>
|
|
1303
|
+
new Promise((resolve) => {
|
|
1304
|
+
if (!Number.isInteger(port) || port <= 0) {
|
|
1305
|
+
resolve(false);
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
const socket = new net.Socket();
|
|
1309
|
+
let settled = false;
|
|
1310
|
+
const settle = (listening) => {
|
|
1311
|
+
if (settled) return;
|
|
1312
|
+
settled = true;
|
|
1313
|
+
socket.destroy();
|
|
1314
|
+
resolve(listening);
|
|
1315
|
+
};
|
|
1316
|
+
socket.setTimeout(PROBE_TIMEOUT_MS);
|
|
1317
|
+
socket.once('connect', () => settle(true));
|
|
1318
|
+
socket.once('timeout', () => settle(false));
|
|
1319
|
+
socket.once('error', () => settle(false));
|
|
1320
|
+
socket.connect(port, '127.0.0.1');
|
|
1321
|
+
});
|
|
1322
|
+
|
|
1323
|
+
/** A detached child is its own process-group leader: signal the group first. */
|
|
1324
|
+
const terminateDetached = async (pid) => {
|
|
1325
|
+
const signal = (name) => {
|
|
1326
|
+
try {
|
|
1327
|
+
process.kill(-pid, name);
|
|
1328
|
+
return true;
|
|
1329
|
+
} catch {
|
|
1330
|
+
return signalPid(pid, name);
|
|
1331
|
+
}
|
|
1332
|
+
};
|
|
1333
|
+
if (!signal('SIGTERM')) return;
|
|
1334
|
+
const deadline = Date.now() + TERMINATE_WAIT_MS;
|
|
1335
|
+
while (Date.now() < deadline) {
|
|
1336
|
+
if (!isPidAlive(pid)) return;
|
|
1337
|
+
await sleep(TERMINATE_POLL_MS);
|
|
1338
|
+
}
|
|
1339
|
+
if (isPidAlive(pid)) signal('SIGKILL');
|
|
1340
|
+
};
|
|
1341
|
+
|
|
1342
|
+
const startVerb = async (flags) => {
|
|
1343
|
+
const roots = resolveRoots(flags);
|
|
1344
|
+
const name = requireName(flags);
|
|
1345
|
+
if (!flags.command) fail('start needs --command');
|
|
1346
|
+
const port = flags.port === undefined ? 0 : positiveInt(flags.port, '--port', { max: 65535, allowZero: true });
|
|
1347
|
+
const waitMs = parseReadyWait(flags);
|
|
1348
|
+
const cwd = flags.cwd ? path.resolve(roots.repoRoot, flags.cwd) : roots.repoRoot;
|
|
1349
|
+
if (!fs.existsSync(cwd)) fail(`--cwd does not exist: ${cwd}`);
|
|
1350
|
+
const url = flags.url || (port > 0 ? `http://127.0.0.1:${port}` : null);
|
|
1351
|
+
const task = flags.task ?? null;
|
|
1352
|
+
|
|
1353
|
+
const existing = readLease(roots.aiLocalRoot, name);
|
|
1354
|
+
if (existing) {
|
|
1355
|
+
const state = leaseState(existing);
|
|
1356
|
+
if (state.state === 'live') {
|
|
1357
|
+
const payload = {
|
|
1358
|
+
ok: true,
|
|
1359
|
+
verb: 'start',
|
|
1360
|
+
running: true,
|
|
1361
|
+
spawned: false,
|
|
1362
|
+
owner: 'mine',
|
|
1363
|
+
name,
|
|
1364
|
+
pid: existing.owner.pid,
|
|
1365
|
+
port: existing.port ?? port,
|
|
1366
|
+
url: existing.port ? `http://127.0.0.1:${existing.port}` : url,
|
|
1367
|
+
command: existing.owner.command ?? flags.command,
|
|
1368
|
+
task: existing.owner.task ?? task,
|
|
1369
|
+
path: leasePath(roots.aiLocalRoot, name),
|
|
1370
|
+
};
|
|
1371
|
+
if (flags.json) out(renderJson(payload));
|
|
1372
|
+
else out(`processes: already running ${name} (pid ${existing.owner.pid})${payload.url ? ` ${payload.url}` : ''}`);
|
|
1373
|
+
return 0;
|
|
1374
|
+
}
|
|
1375
|
+
if (state.state === 'invalid') {
|
|
1376
|
+
fail(`lease "${name}" has no provable owner; inspect ${leasePath(roots.aiLocalRoot, name)} and remove it by hand`);
|
|
1377
|
+
}
|
|
1378
|
+
releaseLease({ aiLocalRoot: roots.aiLocalRoot, name, token: existing.owner?.token, pid: existing.owner?.pid });
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
if (await probePort(port)) {
|
|
1382
|
+
const payload = {
|
|
1383
|
+
ok: true,
|
|
1384
|
+
verb: 'start',
|
|
1385
|
+
running: true,
|
|
1386
|
+
spawned: false,
|
|
1387
|
+
owner: 'foreign',
|
|
1388
|
+
name,
|
|
1389
|
+
pid: null,
|
|
1390
|
+
port,
|
|
1391
|
+
url,
|
|
1392
|
+
command: flags.command,
|
|
1393
|
+
task,
|
|
1394
|
+
warning: 'already listening; `stop` will not touch a process this repository did not start',
|
|
1395
|
+
};
|
|
1396
|
+
if (flags.json) out(renderJson(payload));
|
|
1397
|
+
else out(`processes: reusing ${url ?? `port ${port}`} (already listening; not started by this repository)`);
|
|
1398
|
+
return 0;
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
const logDir = path.join(roots.aiLocalRoot, '.processes', 'logs');
|
|
1402
|
+
fs.mkdirSync(logDir, { recursive: true });
|
|
1403
|
+
const logFile = path.join(logDir, `${name}.log`);
|
|
1404
|
+
const logFd = fs.openSync(logFile, 'w');
|
|
1405
|
+
fs.writeSync(logFd, `# processes start ${name} — ${new Date().toISOString()}\n# cwd: ${cwd}\n# command: ${flags.command}\n\n`);
|
|
1406
|
+
let child;
|
|
1407
|
+
try {
|
|
1408
|
+
child = spawn(flags.command, {
|
|
1409
|
+
cwd,
|
|
1410
|
+
shell: true,
|
|
1411
|
+
detached: true,
|
|
1412
|
+
stdio: ['ignore', logFd, logFd],
|
|
1413
|
+
env: process.env,
|
|
1414
|
+
});
|
|
1415
|
+
} finally {
|
|
1416
|
+
fs.closeSync(logFd);
|
|
1417
|
+
}
|
|
1418
|
+
child.unref();
|
|
1419
|
+
const pid = child.pid;
|
|
1420
|
+
if (!Number.isInteger(pid)) fail(`could not spawn "${flags.command}"`, 3);
|
|
1421
|
+
|
|
1422
|
+
const lease = acquireLease({
|
|
1423
|
+
aiLocalRoot: roots.aiLocalRoot,
|
|
1424
|
+
name,
|
|
1425
|
+
kind: 'port',
|
|
1426
|
+
port: port > 0 ? port : null,
|
|
1427
|
+
label: flags.label ?? 'dev-server',
|
|
1428
|
+
task,
|
|
1429
|
+
command: flags.command,
|
|
1430
|
+
cwd,
|
|
1431
|
+
pid,
|
|
1432
|
+
});
|
|
1433
|
+
if (!lease.acquired) {
|
|
1434
|
+
await terminateDetached(pid);
|
|
1435
|
+
fail(
|
|
1436
|
+
`could not record "${name}": ${lease.reason ?? lease.code}${lease.owner ? ` (held by pid ${lease.owner.pid})` : ''}`,
|
|
1437
|
+
3,
|
|
1438
|
+
);
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
let exited = null;
|
|
1442
|
+
child.once('exit', (code, signal) => {
|
|
1443
|
+
exited = { code, signal };
|
|
1444
|
+
});
|
|
1445
|
+
let listening = port <= 0;
|
|
1446
|
+
const deadline = Date.now() + waitMs;
|
|
1447
|
+
while (port > 0) {
|
|
1448
|
+
if (await probePort(port)) {
|
|
1449
|
+
listening = true;
|
|
1450
|
+
break;
|
|
1451
|
+
}
|
|
1452
|
+
if (exited || child.exitCode !== null) break;
|
|
1453
|
+
if (Date.now() >= deadline) break;
|
|
1454
|
+
await sleep(START_POLL_MS);
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
if (!listening) {
|
|
1458
|
+
await terminateDetached(pid);
|
|
1459
|
+
releaseLease({ aiLocalRoot: roots.aiLocalRoot, name, token: lease.token, pid });
|
|
1460
|
+
let tail = '';
|
|
1461
|
+
try {
|
|
1462
|
+
tail = fs.readFileSync(logFile, 'utf8').split('\n').filter(Boolean).slice(-LOG_TAIL_LINES).join('\n');
|
|
1463
|
+
} catch {
|
|
1464
|
+
/* the log is advisory */
|
|
1465
|
+
}
|
|
1466
|
+
const reason = exited
|
|
1467
|
+
? `exited (${exited.signal === null ? `code ${exited.code}` : exited.signal})`
|
|
1468
|
+
: `did not listen on port ${port} within ${waitMs} ms`;
|
|
1469
|
+
fail(`"${flags.command}" ${reason}${tail ? `\n${tail}` : ''} (log: ${logFile})`, 3);
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
const payload = {
|
|
1473
|
+
ok: true,
|
|
1474
|
+
verb: 'start',
|
|
1475
|
+
running: true,
|
|
1476
|
+
spawned: true,
|
|
1477
|
+
owner: 'mine',
|
|
1478
|
+
name,
|
|
1479
|
+
pid,
|
|
1480
|
+
port,
|
|
1481
|
+
url,
|
|
1482
|
+
command: flags.command,
|
|
1483
|
+
task,
|
|
1484
|
+
path: leasePath(roots.aiLocalRoot, name),
|
|
1485
|
+
};
|
|
1486
|
+
if (flags.json) out(renderJson(payload));
|
|
1487
|
+
else {
|
|
1488
|
+
out(`processes: started ${name} (pid ${pid})${url ? ` ${url}` : ''}`);
|
|
1489
|
+
out(`processes: stop with \`processes.mjs stop --name ${name}\` or \`--task ${task ?? '<slug>'}\``);
|
|
1490
|
+
}
|
|
1491
|
+
return 0;
|
|
1492
|
+
};
|
|
1493
|
+
|
|
1494
|
+
const serveVerb = (flags) => {
|
|
1495
|
+
const roots = resolveRoots(flags);
|
|
1496
|
+
const host = flags.host ?? DEFAULT_SERVE_HOST;
|
|
1497
|
+
if (!LOOPBACK_HOSTS.has(host)) fail('serve binds a loopback host only (127.0.0.1, localhost, ::1)');
|
|
1498
|
+
const port = parsePort(flags, DEFAULT_SERVE_PORT);
|
|
1499
|
+
|
|
1500
|
+
const server = http.createServer((request, response) => {
|
|
1501
|
+
const pathname = new URL(request.url ?? '/', `http://${host}:${port}`).pathname;
|
|
1502
|
+
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
1503
|
+
response.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
1504
|
+
response.end('not found\n');
|
|
1505
|
+
return;
|
|
1506
|
+
}
|
|
1507
|
+
if (pathname === '/api/v1/processes') {
|
|
1508
|
+
const body = renderJson(buildSnapshot(roots));
|
|
1509
|
+
response.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1510
|
+
response.end(`${body}\n`);
|
|
1511
|
+
return;
|
|
1512
|
+
}
|
|
1513
|
+
if (pathname === '/api/v1/processes/stream') {
|
|
1514
|
+
response.writeHead(200, {
|
|
1515
|
+
'Content-Type': 'text/event-stream',
|
|
1516
|
+
'Cache-Control': 'no-cache',
|
|
1517
|
+
Connection: 'keep-alive',
|
|
1518
|
+
});
|
|
1519
|
+
response.write(`retry: ${SSE_RETRY_MS}\n\n`);
|
|
1520
|
+
let last = null;
|
|
1521
|
+
const send = () => {
|
|
1522
|
+
const snapshot = buildSnapshot(roots);
|
|
1523
|
+
// `generatedAt` changes every tick; only a change in state is an event.
|
|
1524
|
+
const stable = renderJson({ ...snapshot, generatedAt: null });
|
|
1525
|
+
if (stable === last) return;
|
|
1526
|
+
last = stable;
|
|
1527
|
+
response.write(`event: snapshot\ndata: ${renderJson(snapshot)}\n\n`);
|
|
1528
|
+
};
|
|
1529
|
+
send();
|
|
1530
|
+
const timer = setInterval(send, SERVE_POLL_MS);
|
|
1531
|
+
const stop = () => clearInterval(timer);
|
|
1532
|
+
request.on('close', stop);
|
|
1533
|
+
response.on('close', stop);
|
|
1534
|
+
return;
|
|
1535
|
+
}
|
|
1536
|
+
if (pathname === '/') {
|
|
1537
|
+
response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
1538
|
+
response.end(renderHtml(buildSnapshot(roots)));
|
|
1539
|
+
return;
|
|
1540
|
+
}
|
|
1541
|
+
response.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
1542
|
+
response.end('not found\n');
|
|
1543
|
+
});
|
|
1544
|
+
|
|
1545
|
+
return new Promise((resolve) => {
|
|
1546
|
+
server.once('error', (error) => {
|
|
1547
|
+
err(`processes: serve failed: ${error.message}`);
|
|
1548
|
+
resolve(1);
|
|
1549
|
+
});
|
|
1550
|
+
server.listen(port, host, () => {
|
|
1551
|
+
out(`processes: serving http://${host}:${port} (Ctrl-C to stop)`);
|
|
1552
|
+
resolve(0);
|
|
1553
|
+
});
|
|
1554
|
+
process.once('SIGINT', () => {
|
|
1555
|
+
server.close(() => process.exit(0));
|
|
1556
|
+
});
|
|
1557
|
+
});
|
|
1558
|
+
};
|
|
1559
|
+
|
|
1560
|
+
/* ---------------------------------- entry --------------------------------- */
|
|
1561
|
+
|
|
1562
|
+
const USAGE = [
|
|
1563
|
+
'usage: processes.mjs <start|acquire|release|run|list|status|watch|scan|doctor|stop|serve> [flags]',
|
|
1564
|
+
' start --name <res> --command <cmd> [--port <n>] [--cwd <dir>] [--task <slug>]',
|
|
1565
|
+
' [--label <l>] [--url <u>] [--wait-ms <n>] [--json] [--root <dir>]',
|
|
1566
|
+
' acquire --name <res> [--kind exclusive|port|advisory] [--port <n>] [--label <l>] [--task <slug>]',
|
|
1567
|
+
' [--agent <name>] [--wait <ms>] [--ttl <ms>] [--allow-unlocked] [--json] [--root <dir>]',
|
|
1568
|
+
' the owner is the parent shell (--pid $$ overrides); use run for a command',
|
|
1569
|
+
' release --name <res> --token <t> [--root <dir>] [--json]',
|
|
1570
|
+
' run --name <res> [acquire flags] -- <command> [args…]',
|
|
1571
|
+
' list [--json] [--task <slug>] [--live] [--root <dir>]',
|
|
1572
|
+
' status --name <res> [--json] [--root <dir>]',
|
|
1573
|
+
' watch [--interval 1s] [--json] [--once] [--no-color] [--root <dir>]',
|
|
1574
|
+
' scan [--json] [--known-only | --all] [--root <dir>]',
|
|
1575
|
+
' doctor [--json] [--root <dir>]',
|
|
1576
|
+
' stop (--name <res> | --task <slug>) [--force] [--root <dir>] [--json]',
|
|
1577
|
+
' serve [--port 4179] [--host 127.0.0.1] [--root <dir>]',
|
|
1578
|
+
].join('\n');
|
|
1579
|
+
|
|
1580
|
+
const main = async (argv) => {
|
|
1581
|
+
const [verb, ...rest] = argv;
|
|
1582
|
+
if (!verb || !Object.prototype.hasOwnProperty.call(VERB_FLAGS, verb)) {
|
|
1583
|
+
err(USAGE);
|
|
1584
|
+
return 2;
|
|
1585
|
+
}
|
|
1586
|
+
const { flags, tail } = parseFlags(verb, rest);
|
|
1587
|
+
if (verb === 'start') return startVerb(flags);
|
|
1588
|
+
if (verb === 'acquire') return acquireVerb(flags);
|
|
1589
|
+
if (verb === 'release') return releaseVerb(flags);
|
|
1590
|
+
if (verb === 'run') return runVerb(flags, tail);
|
|
1591
|
+
if (verb === 'list') return listVerb(flags);
|
|
1592
|
+
if (verb === 'status') return statusVerb(flags);
|
|
1593
|
+
if (verb === 'watch') return watchVerb(flags);
|
|
1594
|
+
if (verb === 'scan') return scanVerb(flags);
|
|
1595
|
+
if (verb === 'doctor') return doctorVerb(flags);
|
|
1596
|
+
if (verb === 'stop') return stopVerb(flags);
|
|
1597
|
+
return serveVerb(flags);
|
|
1598
|
+
};
|
|
1599
|
+
|
|
1600
|
+
const invokedDirectly = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
|
|
1601
|
+
if (invokedDirectly) {
|
|
1602
|
+
main(process.argv.slice(2)).then(
|
|
1603
|
+
(code) => {
|
|
1604
|
+
if (code !== null) process.exitCode = code;
|
|
1605
|
+
},
|
|
1606
|
+
(error) => {
|
|
1607
|
+
if (error instanceof ProcessError) {
|
|
1608
|
+
err(`processes: ${error.message}`);
|
|
1609
|
+
process.exitCode = error.exitCode;
|
|
1610
|
+
return;
|
|
1611
|
+
}
|
|
1612
|
+
err(`processes: ${error instanceof Error ? error.stack ?? error.message : String(error)}`);
|
|
1613
|
+
process.exitCode = 2;
|
|
1614
|
+
},
|
|
1615
|
+
);
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
export { main };
|