@bonesofspring/ai-rules 0.2.73 → 0.2.76
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 +88 -0
- package/README.md +2 -2
- package/bin/lib/engine-scripts.js +12 -0
- package/docs/TEAM-DB.md +96 -5
- package/docs/TOKEN-ECONOMY.md +18 -5
- package/docs/openapi/team-db.v1.json +1164 -224
- package/package.json +2 -2
- package/presets/_shared/ai-local/scripts/action-signals.mjs +225 -0
- package/presets/_shared/ai-local/scripts/actions-report.mjs +210 -0
- package/presets/_shared/ai-local/scripts/check-write-scope.mjs +56 -26
- package/presets/_shared/ai-local/scripts/lock.mjs +103 -30
- package/presets/_shared/ai-local/scripts/processes.mjs +5 -4
- package/presets/_shared/ai-local/scripts/rr-scheduler-start.mjs +161 -0
- package/presets/_shared/ai-local/scripts/rule-improver.mjs +4 -0
- package/presets/_shared/ai-local/scripts/run-compact.mjs +132 -0
- package/presets/_shared/ai-local/scripts/signals.mjs +3 -1
- package/presets/_shared/ai-local/scripts/team-db-api.mjs +354 -11
- package/presets/_shared/ai-local/scripts/team-db-openapi.mjs +309 -4
- package/presets/_shared/ai-local/scripts/team-db-project.mjs +515 -26
- package/presets/_shared/ai-local/scripts/team-db-scheduler.mjs +32 -2
- package/presets/_shared/ai-local/scripts/team-db-schema.mjs +185 -1
- package/presets/_shared/ai-local/scripts/team-db-task-runner.mjs +3 -1
- package/presets/_shared/core/agent-team/agent-task-metrics.md +29 -0
- package/presets/_shared/core/agent-team/agent-team-orchestrator.md +1 -1
- package/presets/_shared/core/meta/preset-pr-checklist.md +2 -2
- package/presets/_shared/core/quality/context-and-iteration.md +1 -1
- package/presets/_shared/hooks/chain-team-phases.sh.tmpl +18 -1
- package/presets/_shared/hooks/compact-shell-output.mjs +68 -0
- package/presets/claude/android-kotlin/commands/README.md +4 -0
- package/presets/claude/android-kotlin/commands/rr-scheduler-start.md +32 -0
- package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +18 -1
- package/presets/claude/android-kotlin/rules/tooling-and-review/agent-team-orchestrator.md +1 -1
- package/presets/claude/android-kotlin/rules/tooling-and-review/preset-pr-checklist.md +2 -2
- package/presets/claude/android-kotlin/team/README.md +1 -1
- package/presets/claude/go/commands/README.md +4 -0
- package/presets/claude/go/commands/rr-scheduler-start.md +32 -0
- package/presets/claude/go/hooks/chain-team-phases.sh +18 -1
- package/presets/claude/go/rules/tooling-and-review/agent-team-orchestrator.md +1 -1
- package/presets/claude/go/rules/tooling-and-review/preset-pr-checklist.md +2 -2
- package/presets/claude/go/team/README.md +1 -1
- package/presets/claude/ios-swift/commands/README.md +4 -0
- package/presets/claude/ios-swift/commands/rr-scheduler-start.md +32 -0
- package/presets/claude/ios-swift/hooks/chain-team-phases.sh +18 -1
- package/presets/claude/ios-swift/rules/tooling-and-review/agent-team-orchestrator.md +1 -1
- package/presets/claude/ios-swift/rules/tooling-and-review/preset-pr-checklist.md +2 -2
- package/presets/claude/ios-swift/team/README.md +1 -1
- package/presets/claude/java/commands/README.md +4 -0
- package/presets/claude/java/commands/rr-scheduler-start.md +32 -0
- package/presets/claude/java/hooks/chain-team-phases.sh +18 -1
- package/presets/claude/java/rules/tooling-and-review/agent-team-orchestrator.md +1 -1
- package/presets/claude/java/rules/tooling-and-review/preset-pr-checklist.md +2 -2
- package/presets/claude/java/team/README.md +1 -1
- package/presets/claude/mcp-ts/commands/README.md +4 -0
- package/presets/claude/mcp-ts/commands/rr-scheduler-start.md +32 -0
- package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +18 -1
- package/presets/claude/mcp-ts/rules/tooling-and-review/agent-team-orchestrator.md +1 -1
- package/presets/claude/mcp-ts/rules/tooling-and-review/preset-pr-checklist.md +2 -2
- package/presets/claude/mcp-ts/team/README.md +1 -1
- package/presets/claude/mol/commands/README.md +4 -0
- package/presets/claude/mol/commands/rr-scheduler-start.md +32 -0
- package/presets/claude/mol/hooks/chain-team-phases.sh +18 -1
- package/presets/claude/mol/rules/tooling-and-review/agent-team-orchestrator.md +1 -1
- package/presets/claude/mol/rules/tooling-and-review/preset-pr-checklist.md +2 -2
- package/presets/claude/mol/team/README.md +1 -1
- package/presets/claude/next/agents/build-verifier.md +1 -1
- package/presets/claude/next/agents/code-reviewer.md +2 -0
- package/presets/claude/next/commands/README.md +4 -0
- package/presets/claude/next/commands/rr-scheduler-start.md +32 -0
- package/presets/claude/next/hooks/chain-team-phases.sh +18 -1
- package/presets/claude/next/rules/tooling-and-review/agent-team-orchestrator.md +1 -1
- package/presets/claude/next/rules/tooling-and-review/preset-pr-checklist.md +2 -2
- package/presets/claude/next/team/README.md +1 -1
- package/presets/claude/nuxt/commands/README.md +4 -0
- package/presets/claude/nuxt/commands/rr-scheduler-start.md +32 -0
- package/presets/claude/nuxt/hooks/chain-team-phases.sh +18 -1
- package/presets/claude/nuxt/rules/tooling-and-review/agent-team-orchestrator.md +1 -1
- package/presets/claude/nuxt/rules/tooling-and-review/preset-pr-checklist.md +2 -2
- package/presets/claude/nuxt/team/README.md +1 -1
- package/presets/claude/php-hexagonal/commands/README.md +4 -0
- package/presets/claude/php-hexagonal/commands/rr-scheduler-start.md +32 -0
- package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +18 -1
- package/presets/claude/php-hexagonal/rules/tooling-and-review/agent-team-orchestrator.md +1 -1
- package/presets/claude/php-hexagonal/rules/tooling-and-review/preset-pr-checklist.md +2 -2
- package/presets/claude/php-hexagonal/team/README.md +1 -1
- package/presets/claude/php-laravel/commands/README.md +4 -0
- package/presets/claude/php-laravel/commands/rr-scheduler-start.md +32 -0
- package/presets/claude/php-laravel/hooks/chain-team-phases.sh +18 -1
- package/presets/claude/php-laravel/rules/tooling-and-review/agent-team-orchestrator.md +1 -1
- package/presets/claude/php-laravel/rules/tooling-and-review/preset-pr-checklist.md +2 -2
- package/presets/claude/php-laravel/team/README.md +1 -1
- package/presets/claude/svelte/commands/README.md +4 -0
- package/presets/claude/svelte/commands/rr-scheduler-start.md +32 -0
- package/presets/claude/svelte/hooks/chain-team-phases.sh +18 -1
- package/presets/claude/svelte/rules/tooling-and-review/agent-team-orchestrator.md +1 -1
- package/presets/claude/svelte/rules/tooling-and-review/preset-pr-checklist.md +2 -2
- package/presets/claude/svelte/team/README.md +1 -1
- package/presets/cursor/android-kotlin/commands/README.md +4 -0
- package/presets/cursor/android-kotlin/commands/rr-scheduler-start.md +32 -0
- package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +18 -1
- package/presets/cursor/android-kotlin/hooks/compact-shell-output.mjs +68 -0
- package/presets/cursor/android-kotlin/hooks.json +7 -0
- package/presets/cursor/android-kotlin/rules/preset-pr-checklist.mdc +2 -2
- package/presets/cursor/android-kotlin/team/README.md +1 -1
- package/presets/cursor/go/commands/README.md +4 -0
- package/presets/cursor/go/commands/rr-scheduler-start.md +32 -0
- package/presets/cursor/go/hooks/chain-team-phases.sh +18 -1
- package/presets/cursor/go/hooks/compact-shell-output.mjs +68 -0
- package/presets/cursor/go/hooks.json +7 -0
- package/presets/cursor/go/rules/preset-pr-checklist.mdc +2 -2
- package/presets/cursor/go/team/README.md +1 -1
- package/presets/cursor/ios-swift/commands/README.md +4 -0
- package/presets/cursor/ios-swift/commands/rr-scheduler-start.md +32 -0
- package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +18 -1
- package/presets/cursor/ios-swift/hooks/compact-shell-output.mjs +68 -0
- package/presets/cursor/ios-swift/hooks.json +7 -0
- package/presets/cursor/ios-swift/rules/preset-pr-checklist.mdc +2 -2
- package/presets/cursor/ios-swift/team/README.md +1 -1
- package/presets/cursor/java/commands/README.md +4 -0
- package/presets/cursor/java/commands/rr-scheduler-start.md +32 -0
- package/presets/cursor/java/hooks/chain-team-phases.sh +18 -1
- package/presets/cursor/java/hooks/compact-shell-output.mjs +68 -0
- package/presets/cursor/java/hooks.json +7 -0
- package/presets/cursor/java/rules/preset-pr-checklist.mdc +2 -2
- package/presets/cursor/java/team/README.md +1 -1
- package/presets/cursor/mcp-ts/commands/README.md +4 -0
- package/presets/cursor/mcp-ts/commands/rr-scheduler-start.md +32 -0
- package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +18 -1
- package/presets/cursor/mcp-ts/hooks/compact-shell-output.mjs +68 -0
- package/presets/cursor/mcp-ts/hooks.json +7 -0
- package/presets/cursor/mcp-ts/rules/preset-pr-checklist.mdc +2 -2
- package/presets/cursor/mcp-ts/team/README.md +1 -1
- package/presets/cursor/mol/commands/README.md +4 -0
- package/presets/cursor/mol/commands/rr-scheduler-start.md +32 -0
- package/presets/cursor/mol/hooks/chain-team-phases.sh +18 -1
- package/presets/cursor/mol/hooks/compact-shell-output.mjs +68 -0
- package/presets/cursor/mol/hooks.json +7 -0
- package/presets/cursor/mol/rules/preset-pr-checklist.mdc +2 -2
- package/presets/cursor/mol/team/README.md +1 -1
- package/presets/cursor/next/agents/build-verifier.md +1 -1
- package/presets/cursor/next/agents/code-reviewer.md +2 -0
- package/presets/cursor/next/commands/README.md +4 -0
- package/presets/cursor/next/commands/rr-scheduler-start.md +32 -0
- package/presets/cursor/next/hooks/chain-team-phases.sh +18 -1
- package/presets/cursor/next/hooks/compact-shell-output.mjs +68 -0
- package/presets/cursor/next/hooks.json +7 -0
- package/presets/cursor/next/rules/preset-pr-checklist.mdc +2 -2
- package/presets/cursor/next/team/README.md +1 -1
- package/presets/cursor/nuxt/commands/README.md +4 -0
- package/presets/cursor/nuxt/commands/rr-scheduler-start.md +32 -0
- package/presets/cursor/nuxt/hooks/chain-team-phases.sh +18 -1
- package/presets/cursor/nuxt/hooks/compact-shell-output.mjs +68 -0
- package/presets/cursor/nuxt/hooks.json +7 -0
- package/presets/cursor/nuxt/rules/preset-pr-checklist.mdc +2 -2
- package/presets/cursor/nuxt/team/README.md +1 -1
- package/presets/cursor/php-hexagonal/commands/README.md +4 -0
- package/presets/cursor/php-hexagonal/commands/rr-scheduler-start.md +32 -0
- package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +18 -1
- package/presets/cursor/php-hexagonal/hooks/compact-shell-output.mjs +68 -0
- package/presets/cursor/php-hexagonal/hooks.json +7 -0
- package/presets/cursor/php-hexagonal/rules/preset-pr-checklist.mdc +2 -2
- package/presets/cursor/php-hexagonal/team/README.md +1 -1
- package/presets/cursor/php-laravel/commands/README.md +4 -0
- package/presets/cursor/php-laravel/commands/rr-scheduler-start.md +32 -0
- package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +18 -1
- package/presets/cursor/php-laravel/hooks/compact-shell-output.mjs +68 -0
- package/presets/cursor/php-laravel/hooks.json +7 -0
- package/presets/cursor/php-laravel/rules/preset-pr-checklist.mdc +2 -2
- package/presets/cursor/php-laravel/team/README.md +1 -1
- package/presets/cursor/svelte/commands/README.md +4 -0
- package/presets/cursor/svelte/commands/rr-scheduler-start.md +32 -0
- package/presets/cursor/svelte/hooks/chain-team-phases.sh +18 -1
- package/presets/cursor/svelte/hooks/compact-shell-output.mjs +68 -0
- package/presets/cursor/svelte/hooks.json +7 -0
- package/presets/cursor/svelte/rules/preset-pr-checklist.mdc +2 -2
- package/presets/cursor/svelte/team/README.md +1 -1
- package/presets/mavis/android-kotlin/adapter-map.json +17 -1
- package/presets/mavis/android-kotlin/commands/README.md +4 -0
- package/presets/mavis/android-kotlin/commands/rr-scheduler-start.md +27 -0
- package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +18 -1
- package/presets/mavis/android-kotlin/rules/agent-team-orchestrator.md +1 -1
- package/presets/mavis/android-kotlin/rules/preset-pr-checklist.md +2 -2
- package/presets/mavis/android-kotlin/team/README.md +1 -1
- package/presets/mavis/go/adapter-map.json +17 -1
- package/presets/mavis/go/commands/README.md +4 -0
- package/presets/mavis/go/commands/rr-scheduler-start.md +27 -0
- package/presets/mavis/go/hooks/chain-team-phases.sh +18 -1
- package/presets/mavis/go/rules/agent-team-orchestrator.md +1 -1
- package/presets/mavis/go/rules/preset-pr-checklist.md +2 -2
- package/presets/mavis/go/team/README.md +1 -1
- package/presets/mavis/ios-swift/adapter-map.json +17 -1
- package/presets/mavis/ios-swift/commands/README.md +4 -0
- package/presets/mavis/ios-swift/commands/rr-scheduler-start.md +27 -0
- package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +18 -1
- package/presets/mavis/ios-swift/rules/agent-team-orchestrator.md +1 -1
- package/presets/mavis/ios-swift/rules/preset-pr-checklist.md +2 -2
- package/presets/mavis/ios-swift/team/README.md +1 -1
- package/presets/mavis/java/adapter-map.json +17 -1
- package/presets/mavis/java/commands/README.md +4 -0
- package/presets/mavis/java/commands/rr-scheduler-start.md +27 -0
- package/presets/mavis/java/hooks/chain-team-phases.sh +18 -1
- package/presets/mavis/java/rules/agent-team-orchestrator.md +1 -1
- package/presets/mavis/java/rules/preset-pr-checklist.md +2 -2
- package/presets/mavis/java/team/README.md +1 -1
- package/presets/mavis/mcp-ts/adapter-map.json +17 -1
- package/presets/mavis/mcp-ts/commands/README.md +4 -0
- package/presets/mavis/mcp-ts/commands/rr-scheduler-start.md +27 -0
- package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +18 -1
- package/presets/mavis/mcp-ts/rules/agent-team-orchestrator.md +1 -1
- package/presets/mavis/mcp-ts/rules/preset-pr-checklist.md +2 -2
- package/presets/mavis/mcp-ts/team/README.md +1 -1
- package/presets/mavis/mol/adapter-map.json +17 -1
- package/presets/mavis/mol/commands/README.md +4 -0
- package/presets/mavis/mol/commands/rr-scheduler-start.md +27 -0
- package/presets/mavis/mol/hooks/chain-team-phases.sh +18 -1
- package/presets/mavis/mol/rules/agent-team-orchestrator.md +1 -1
- package/presets/mavis/mol/rules/preset-pr-checklist.md +2 -2
- package/presets/mavis/mol/team/README.md +1 -1
- package/presets/mavis/next/adapter-map.json +17 -1
- package/presets/mavis/next/agents/build-verifier.md +1 -1
- package/presets/mavis/next/agents/code-reviewer.md +2 -0
- package/presets/mavis/next/commands/README.md +4 -0
- package/presets/mavis/next/commands/rr-scheduler-start.md +27 -0
- package/presets/mavis/next/hooks/chain-team-phases.sh +18 -1
- package/presets/mavis/next/rules/agent-team-orchestrator.md +1 -1
- package/presets/mavis/next/rules/preset-pr-checklist.md +2 -2
- package/presets/mavis/next/team/README.md +1 -1
- package/presets/mavis/nuxt/adapter-map.json +17 -1
- package/presets/mavis/nuxt/commands/README.md +4 -0
- package/presets/mavis/nuxt/commands/rr-scheduler-start.md +27 -0
- package/presets/mavis/nuxt/hooks/chain-team-phases.sh +18 -1
- package/presets/mavis/nuxt/rules/agent-team-orchestrator.md +1 -1
- package/presets/mavis/nuxt/rules/preset-pr-checklist.md +2 -2
- package/presets/mavis/nuxt/team/README.md +1 -1
- package/presets/mavis/php-hexagonal/adapter-map.json +17 -1
- package/presets/mavis/php-hexagonal/commands/README.md +4 -0
- package/presets/mavis/php-hexagonal/commands/rr-scheduler-start.md +27 -0
- package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +18 -1
- package/presets/mavis/php-hexagonal/rules/agent-team-orchestrator.md +1 -1
- package/presets/mavis/php-hexagonal/rules/preset-pr-checklist.md +2 -2
- package/presets/mavis/php-hexagonal/team/README.md +1 -1
- package/presets/mavis/php-laravel/adapter-map.json +17 -1
- package/presets/mavis/php-laravel/commands/README.md +4 -0
- package/presets/mavis/php-laravel/commands/rr-scheduler-start.md +27 -0
- package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +18 -1
- package/presets/mavis/php-laravel/rules/agent-team-orchestrator.md +1 -1
- package/presets/mavis/php-laravel/rules/preset-pr-checklist.md +2 -2
- package/presets/mavis/php-laravel/team/README.md +1 -1
- package/presets/mavis/svelte/adapter-map.json +17 -1
- package/presets/mavis/svelte/commands/README.md +4 -0
- package/presets/mavis/svelte/commands/rr-scheduler-start.md +27 -0
- package/presets/mavis/svelte/hooks/chain-team-phases.sh +18 -1
- package/presets/mavis/svelte/rules/agent-team-orchestrator.md +1 -1
- package/presets/mavis/svelte/rules/preset-pr-checklist.md +2 -2
- package/presets/mavis/svelte/team/README.md +1 -1
- package/presets/opencode/android-kotlin/adapter-map.json +17 -1
- package/presets/opencode/android-kotlin/commands/README.md +4 -0
- package/presets/opencode/android-kotlin/commands/rr-scheduler-start.md +35 -0
- package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +18 -1
- package/presets/opencode/android-kotlin/rules/agent-team-orchestrator.md +1 -1
- package/presets/opencode/android-kotlin/rules/preset-pr-checklist.md +2 -2
- package/presets/opencode/android-kotlin/team/README.md +1 -1
- package/presets/opencode/go/adapter-map.json +17 -1
- package/presets/opencode/go/commands/README.md +4 -0
- package/presets/opencode/go/commands/rr-scheduler-start.md +35 -0
- package/presets/opencode/go/hooks/chain-team-phases.sh +18 -1
- package/presets/opencode/go/rules/agent-team-orchestrator.md +1 -1
- package/presets/opencode/go/rules/preset-pr-checklist.md +2 -2
- package/presets/opencode/go/team/README.md +1 -1
- package/presets/opencode/ios-swift/adapter-map.json +17 -1
- package/presets/opencode/ios-swift/commands/README.md +4 -0
- package/presets/opencode/ios-swift/commands/rr-scheduler-start.md +35 -0
- package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +18 -1
- package/presets/opencode/ios-swift/rules/agent-team-orchestrator.md +1 -1
- package/presets/opencode/ios-swift/rules/preset-pr-checklist.md +2 -2
- package/presets/opencode/ios-swift/team/README.md +1 -1
- package/presets/opencode/java/adapter-map.json +17 -1
- package/presets/opencode/java/commands/README.md +4 -0
- package/presets/opencode/java/commands/rr-scheduler-start.md +35 -0
- package/presets/opencode/java/hooks/chain-team-phases.sh +18 -1
- package/presets/opencode/java/rules/agent-team-orchestrator.md +1 -1
- package/presets/opencode/java/rules/preset-pr-checklist.md +2 -2
- package/presets/opencode/java/team/README.md +1 -1
- package/presets/opencode/mcp-ts/adapter-map.json +17 -1
- package/presets/opencode/mcp-ts/commands/README.md +4 -0
- package/presets/opencode/mcp-ts/commands/rr-scheduler-start.md +35 -0
- package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +18 -1
- package/presets/opencode/mcp-ts/rules/agent-team-orchestrator.md +1 -1
- package/presets/opencode/mcp-ts/rules/preset-pr-checklist.md +2 -2
- package/presets/opencode/mcp-ts/team/README.md +1 -1
- package/presets/opencode/mol/adapter-map.json +17 -1
- package/presets/opencode/mol/commands/README.md +4 -0
- package/presets/opencode/mol/commands/rr-scheduler-start.md +35 -0
- package/presets/opencode/mol/hooks/chain-team-phases.sh +18 -1
- package/presets/opencode/mol/rules/agent-team-orchestrator.md +1 -1
- package/presets/opencode/mol/rules/preset-pr-checklist.md +2 -2
- package/presets/opencode/mol/team/README.md +1 -1
- package/presets/opencode/next/adapter-map.json +17 -1
- package/presets/opencode/next/agents/build-verifier.md +1 -1
- package/presets/opencode/next/agents/code-reviewer.md +2 -0
- package/presets/opencode/next/commands/README.md +4 -0
- package/presets/opencode/next/commands/rr-scheduler-start.md +35 -0
- package/presets/opencode/next/hooks/chain-team-phases.sh +18 -1
- package/presets/opencode/next/rules/agent-team-orchestrator.md +1 -1
- package/presets/opencode/next/rules/preset-pr-checklist.md +2 -2
- package/presets/opencode/next/team/README.md +1 -1
- package/presets/opencode/nuxt/adapter-map.json +17 -1
- package/presets/opencode/nuxt/commands/README.md +4 -0
- package/presets/opencode/nuxt/commands/rr-scheduler-start.md +35 -0
- package/presets/opencode/nuxt/hooks/chain-team-phases.sh +18 -1
- package/presets/opencode/nuxt/rules/agent-team-orchestrator.md +1 -1
- package/presets/opencode/nuxt/rules/preset-pr-checklist.md +2 -2
- package/presets/opencode/nuxt/team/README.md +1 -1
- package/presets/opencode/php-hexagonal/adapter-map.json +17 -1
- package/presets/opencode/php-hexagonal/commands/README.md +4 -0
- package/presets/opencode/php-hexagonal/commands/rr-scheduler-start.md +35 -0
- package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +18 -1
- package/presets/opencode/php-hexagonal/rules/agent-team-orchestrator.md +1 -1
- package/presets/opencode/php-hexagonal/rules/preset-pr-checklist.md +2 -2
- package/presets/opencode/php-hexagonal/team/README.md +1 -1
- package/presets/opencode/php-laravel/adapter-map.json +17 -1
- package/presets/opencode/php-laravel/commands/README.md +4 -0
- package/presets/opencode/php-laravel/commands/rr-scheduler-start.md +35 -0
- package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +18 -1
- package/presets/opencode/php-laravel/rules/agent-team-orchestrator.md +1 -1
- package/presets/opencode/php-laravel/rules/preset-pr-checklist.md +2 -2
- package/presets/opencode/php-laravel/team/README.md +1 -1
- package/presets/opencode/plugins/output-guard/README.md +45 -3
- package/presets/opencode/plugins/output-guard/actions.ts +201 -0
- package/presets/opencode/plugins/output-guard/index.ts +147 -6
- package/presets/opencode/plugins/output-guard/rewrite.ts +10 -1
- package/presets/opencode/svelte/adapter-map.json +17 -1
- package/presets/opencode/svelte/commands/README.md +4 -0
- package/presets/opencode/svelte/commands/rr-scheduler-start.md +35 -0
- package/presets/opencode/svelte/hooks/chain-team-phases.sh +18 -1
- package/presets/opencode/svelte/rules/agent-team-orchestrator.md +1 -1
- package/presets/opencode/svelte/rules/preset-pr-checklist.md +2 -2
- package/presets/opencode/svelte/team/README.md +1 -1
- package/presets/zcode/android-kotlin/adapter-map.json +17 -1
- package/presets/zcode/android-kotlin/commands/README.md +4 -0
- package/presets/zcode/android-kotlin/commands/rr-scheduler-start.md +35 -0
- package/presets/zcode/android-kotlin/hooks/chain-team-phases.sh +18 -1
- package/presets/zcode/android-kotlin/rules/agent-team-orchestrator.md +1 -1
- package/presets/zcode/android-kotlin/rules/preset-pr-checklist.md +2 -2
- package/presets/zcode/android-kotlin/team/README.md +1 -1
- package/presets/zcode/go/adapter-map.json +17 -1
- package/presets/zcode/go/commands/README.md +4 -0
- package/presets/zcode/go/commands/rr-scheduler-start.md +35 -0
- package/presets/zcode/go/hooks/chain-team-phases.sh +18 -1
- package/presets/zcode/go/rules/agent-team-orchestrator.md +1 -1
- package/presets/zcode/go/rules/preset-pr-checklist.md +2 -2
- package/presets/zcode/go/team/README.md +1 -1
- package/presets/zcode/ios-swift/adapter-map.json +17 -1
- package/presets/zcode/ios-swift/commands/README.md +4 -0
- package/presets/zcode/ios-swift/commands/rr-scheduler-start.md +35 -0
- package/presets/zcode/ios-swift/hooks/chain-team-phases.sh +18 -1
- package/presets/zcode/ios-swift/rules/agent-team-orchestrator.md +1 -1
- package/presets/zcode/ios-swift/rules/preset-pr-checklist.md +2 -2
- package/presets/zcode/ios-swift/team/README.md +1 -1
- package/presets/zcode/java/adapter-map.json +17 -1
- package/presets/zcode/java/commands/README.md +4 -0
- package/presets/zcode/java/commands/rr-scheduler-start.md +35 -0
- package/presets/zcode/java/hooks/chain-team-phases.sh +18 -1
- package/presets/zcode/java/rules/agent-team-orchestrator.md +1 -1
- package/presets/zcode/java/rules/preset-pr-checklist.md +2 -2
- package/presets/zcode/java/team/README.md +1 -1
- package/presets/zcode/mcp-ts/adapter-map.json +17 -1
- package/presets/zcode/mcp-ts/commands/README.md +4 -0
- package/presets/zcode/mcp-ts/commands/rr-scheduler-start.md +35 -0
- package/presets/zcode/mcp-ts/hooks/chain-team-phases.sh +18 -1
- package/presets/zcode/mcp-ts/rules/agent-team-orchestrator.md +1 -1
- package/presets/zcode/mcp-ts/rules/preset-pr-checklist.md +2 -2
- package/presets/zcode/mcp-ts/team/README.md +1 -1
- package/presets/zcode/mol/adapter-map.json +17 -1
- package/presets/zcode/mol/commands/README.md +4 -0
- package/presets/zcode/mol/commands/rr-scheduler-start.md +35 -0
- package/presets/zcode/mol/hooks/chain-team-phases.sh +18 -1
- package/presets/zcode/mol/rules/agent-team-orchestrator.md +1 -1
- package/presets/zcode/mol/rules/preset-pr-checklist.md +2 -2
- package/presets/zcode/mol/team/README.md +1 -1
- package/presets/zcode/next/adapter-map.json +17 -1
- package/presets/zcode/next/agents/build-verifier.md +1 -1
- package/presets/zcode/next/agents/code-reviewer.md +2 -0
- package/presets/zcode/next/commands/README.md +4 -0
- package/presets/zcode/next/commands/rr-scheduler-start.md +35 -0
- package/presets/zcode/next/hooks/chain-team-phases.sh +18 -1
- package/presets/zcode/next/rules/agent-team-orchestrator.md +1 -1
- package/presets/zcode/next/rules/preset-pr-checklist.md +2 -2
- package/presets/zcode/next/team/README.md +1 -1
- package/presets/zcode/nuxt/adapter-map.json +17 -1
- package/presets/zcode/nuxt/commands/README.md +4 -0
- package/presets/zcode/nuxt/commands/rr-scheduler-start.md +35 -0
- package/presets/zcode/nuxt/hooks/chain-team-phases.sh +18 -1
- package/presets/zcode/nuxt/rules/agent-team-orchestrator.md +1 -1
- package/presets/zcode/nuxt/rules/preset-pr-checklist.md +2 -2
- package/presets/zcode/nuxt/team/README.md +1 -1
- package/presets/zcode/php-hexagonal/adapter-map.json +17 -1
- package/presets/zcode/php-hexagonal/commands/README.md +4 -0
- package/presets/zcode/php-hexagonal/commands/rr-scheduler-start.md +35 -0
- package/presets/zcode/php-hexagonal/hooks/chain-team-phases.sh +18 -1
- package/presets/zcode/php-hexagonal/rules/agent-team-orchestrator.md +1 -1
- package/presets/zcode/php-hexagonal/rules/preset-pr-checklist.md +2 -2
- package/presets/zcode/php-hexagonal/team/README.md +1 -1
- package/presets/zcode/php-laravel/adapter-map.json +17 -1
- package/presets/zcode/php-laravel/commands/README.md +4 -0
- package/presets/zcode/php-laravel/commands/rr-scheduler-start.md +35 -0
- package/presets/zcode/php-laravel/hooks/chain-team-phases.sh +18 -1
- package/presets/zcode/php-laravel/rules/agent-team-orchestrator.md +1 -1
- package/presets/zcode/php-laravel/rules/preset-pr-checklist.md +2 -2
- package/presets/zcode/php-laravel/team/README.md +1 -1
- package/presets/zcode/svelte/adapter-map.json +17 -1
- package/presets/zcode/svelte/commands/README.md +4 -0
- package/presets/zcode/svelte/commands/rr-scheduler-start.md +35 -0
- package/presets/zcode/svelte/hooks/chain-team-phases.sh +18 -1
- package/presets/zcode/svelte/rules/agent-team-orchestrator.md +1 -1
- package/presets/zcode/svelte/rules/preset-pr-checklist.md +2 -2
- package/presets/zcode/svelte/team/README.md +1 -1
- package/scripts/close-agent-task-human-gate.mjs +3 -3
- package/scripts/report-context-cost.mjs +10 -3
- package/scripts/aggregate-agent-task-metrics.mjs +0 -480
|
@@ -32,6 +32,7 @@ import {
|
|
|
32
32
|
readSearchMode,
|
|
33
33
|
teamDbFail,
|
|
34
34
|
} from './team-db-schema.mjs';
|
|
35
|
+
import { ACTION_ERROR_CLASSES } from './action-signals.mjs';
|
|
35
36
|
import { projectScheduler, retainRuns } from './team-db-scheduler.mjs';
|
|
36
37
|
import { validateEntrySchema, validateItemSchema, validateLinkIdentity, validateLinkSchema } from './team-db-tracker.mjs';
|
|
37
38
|
|
|
@@ -40,6 +41,10 @@ export const MAX_RAW_BYTES = 32 * 1024 * 1024;
|
|
|
40
41
|
export const RETENTION_DAYS = 90;
|
|
41
42
|
export const SYNC_RUN_RETENTION = 200;
|
|
42
43
|
export const DEFAULT_SLUG_DEADLINE_MS = 4500;
|
|
44
|
+
/** The grace a lease window keeps after its last write, in milliseconds. */
|
|
45
|
+
export const LEASE_TTL_MS = 30 * 60 * 1000;
|
|
46
|
+
/** The fuzz around the last write of `tasks/<slug>` for the weakest rung. */
|
|
47
|
+
export const DIR_WINDOW_MS = 30 * 60 * 1000;
|
|
43
48
|
|
|
44
49
|
const HASH_CHUNK = 64 * 1024;
|
|
45
50
|
const MAX_KNOWN_BYTES = 8 * 1024 * 1024;
|
|
@@ -745,7 +750,12 @@ const normalizeEvent = (event, index, slug) => {
|
|
|
745
750
|
attemptId: typeof event.attemptId === 'string' ? event.attemptId : null,
|
|
746
751
|
stepIndex: integerOrNull(event.stepIndex),
|
|
747
752
|
stepLabel: typeof event.stepLabel === 'string' ? event.stepLabel : null,
|
|
748
|
-
agent:
|
|
753
|
+
agent:
|
|
754
|
+
typeof event.agent === 'string' && event.agent !== ''
|
|
755
|
+
? event.agent
|
|
756
|
+
: typeof event.nextAgent === 'string' && event.nextAgent !== ''
|
|
757
|
+
? event.nextAgent
|
|
758
|
+
: null,
|
|
749
759
|
role: typeof event.role === 'string' ? event.role : null,
|
|
750
760
|
attemptNo: integerOrNull(event.attempt),
|
|
751
761
|
outcome: typeof event.outcome === 'string' ? event.outcome : null,
|
|
@@ -800,6 +810,54 @@ const numberOrNull = (value) =>
|
|
|
800
810
|
|
|
801
811
|
const realOrNull = (value) => (typeof value === 'number' && Number.isFinite(value) ? value : null);
|
|
802
812
|
|
|
813
|
+
/** A counter the plugin reported: a non-negative integer, or nothing at all. */
|
|
814
|
+
const countOrNull = (value) =>
|
|
815
|
+
typeof value === 'number' && Number.isInteger(value) && value >= 0 ? value : null;
|
|
816
|
+
|
|
817
|
+
/** A hash the capture plane is allowed to store. */
|
|
818
|
+
const HASH_RE = /^[0-9a-f]{64}$/;
|
|
819
|
+
/** An agent name: a short identifier, never a path or a sentence. */
|
|
820
|
+
const AGENT_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
821
|
+
/** A host session or call id. */
|
|
822
|
+
const HOST_ID_RE = /^[A-Za-z0-9_.:-]{1,128}$/;
|
|
823
|
+
/** The failure classes of `design.md` D-3; anything else is refused. */
|
|
824
|
+
export const ERROR_CLASSES = new Set(ACTION_ERROR_CLASSES);
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* The captured half of an action row.
|
|
828
|
+
*
|
|
829
|
+
* The projector never truncates text into the database: a value that is not a
|
|
830
|
+
* hash, an enum member or a bounded identifier is dropped to `null` and counted,
|
|
831
|
+
* so one line of `team-db-errors.log` names the refusal without ever carrying
|
|
832
|
+
* the value.
|
|
833
|
+
*/
|
|
834
|
+
const captureFields = (row, onRefuse) => {
|
|
835
|
+
const refuse = () => onRefuse();
|
|
836
|
+
const hash = typeof row.targetKey === 'string' && HASH_RE.test(row.targetKey) ? row.targetKey : null;
|
|
837
|
+
if (row.targetKey !== undefined && row.targetKey !== null && hash === null) refuse();
|
|
838
|
+
const agent = typeof row.agent === 'string' && AGENT_RE.test(row.agent) ? row.agent : null;
|
|
839
|
+
if (row.agent !== undefined && row.agent !== null && row.agent !== '' && agent === null) refuse();
|
|
840
|
+
const parentSessionId =
|
|
841
|
+
typeof row.parentSessionID === 'string' && HOST_ID_RE.test(row.parentSessionID) ? row.parentSessionID : null;
|
|
842
|
+
if (row.parentSessionID !== undefined && row.parentSessionID !== null && parentSessionId === null) refuse();
|
|
843
|
+
const repeatOf = typeof row.repeatOf === 'string' && HOST_ID_RE.test(row.repeatOf) ? row.repeatOf : null;
|
|
844
|
+
if (row.repeatOf !== undefined && row.repeatOf !== null && repeatOf === null) refuse();
|
|
845
|
+
const errorClass =
|
|
846
|
+
typeof row.errorClass === 'string' && ERROR_CLASSES.has(row.errorClass) ? row.errorClass : null;
|
|
847
|
+
if (row.errorClass !== undefined && row.errorClass !== null && errorClass === null) refuse();
|
|
848
|
+
return {
|
|
849
|
+
agent,
|
|
850
|
+
parentSessionId,
|
|
851
|
+
durationMs: countOrNull(row.durationMs),
|
|
852
|
+
gapMs: countOrNull(row.gapMs),
|
|
853
|
+
targetKey: hash,
|
|
854
|
+
repeatOf,
|
|
855
|
+
repeatIndex: countOrNull(row.repeatIndex),
|
|
856
|
+
exitCode: countOrNull(row.exit),
|
|
857
|
+
errorClass,
|
|
858
|
+
};
|
|
859
|
+
};
|
|
860
|
+
|
|
803
861
|
const usageModel = (value) => {
|
|
804
862
|
if (typeof value === 'string' && value !== '') {
|
|
805
863
|
const separator = value.indexOf('/');
|
|
@@ -2095,7 +2153,54 @@ const classifyToolCall = (row) => {
|
|
|
2095
2153
|
return 'other';
|
|
2096
2154
|
};
|
|
2097
2155
|
|
|
2098
|
-
|
|
2156
|
+
/** The tool → intent map of `design.md` D-2; nothing outside it is a new value. */
|
|
2157
|
+
const INTENT_BY_TOOL = new Map([
|
|
2158
|
+
['read', 'explore'],
|
|
2159
|
+
['grep', 'explore'],
|
|
2160
|
+
['glob', 'explore'],
|
|
2161
|
+
['list', 'explore'],
|
|
2162
|
+
['edit', 'mutate'],
|
|
2163
|
+
['write', 'mutate'],
|
|
2164
|
+
['patch', 'mutate'],
|
|
2165
|
+
['shell', 'execute'],
|
|
2166
|
+
['bash', 'execute'],
|
|
2167
|
+
['execute', 'execute'],
|
|
2168
|
+
['subagent', 'delegate'],
|
|
2169
|
+
['task', 'delegate'],
|
|
2170
|
+
['question', 'ask'],
|
|
2171
|
+
['permission', 'ask'],
|
|
2172
|
+
['skill', 'meta'],
|
|
2173
|
+
['todowrite', 'meta'],
|
|
2174
|
+
['todoread', 'meta'],
|
|
2175
|
+
['plan', 'meta'],
|
|
2176
|
+
['webfetch', 'web'],
|
|
2177
|
+
['websearch', 'web'],
|
|
2178
|
+
]);
|
|
2179
|
+
|
|
2180
|
+
/**
|
|
2181
|
+
* The seven-value intent of one tool call.
|
|
2182
|
+
* * A sibling of `classifyToolCall` rather than a rewrite: `category` is read by
|
|
2183
|
+
* `report-efficiency.mjs` and pinned against it by a drift test, so the two
|
|
2184
|
+
* classifiers stay independent. An unknown tool lands in `meta` with its name
|
|
2185
|
+
* kept in `tool` — never back in a bucket that hides it.
|
|
2186
|
+
*/
|
|
2187
|
+
export const intentOf = (row) => {
|
|
2188
|
+
const tool = String(row?.tool ?? '').toLowerCase();
|
|
2189
|
+
if (INTENT_BY_TOOL.has(tool)) return INTENT_BY_TOOL.get(tool);
|
|
2190
|
+
// The legacy command shapes still mean what they meant; a tool outside the
|
|
2191
|
+
// map is read through its command before it falls to `meta`. A network fetch
|
|
2192
|
+
// is decided before the test/build shapes, so `curl https://host.test` stays
|
|
2193
|
+
// a `web` action.
|
|
2194
|
+
const command = String(row?.command ?? '');
|
|
2195
|
+
if (/\b(curl|wget|http)\b/i.test(command)) return 'web';
|
|
2196
|
+
if (/\b(git|test|lint|type-check|tsc|vitest|jest|playwright|eslint|build|compile|bundle)\b/i.test(command)) {
|
|
2197
|
+
return 'execute';
|
|
2198
|
+
}
|
|
2199
|
+
if (/\b(grep|rg|find|search)\b/i.test(command)) return 'explore';
|
|
2200
|
+
return 'meta';
|
|
2201
|
+
};
|
|
2202
|
+
|
|
2203
|
+
const normalizeToolCall = (row, onRefuse = () => {}) => {
|
|
2099
2204
|
if (!isRecord(row)) return null;
|
|
2100
2205
|
const sessionID = typeof row.sessionID === 'string' ? row.sessionID : '';
|
|
2101
2206
|
const callID = typeof row.callID === 'string' ? row.callID : '';
|
|
@@ -2113,12 +2218,14 @@ const normalizeToolCall = (row) => {
|
|
|
2113
2218
|
callID,
|
|
2114
2219
|
tool,
|
|
2115
2220
|
category: classifyToolCall({ tool, command: row.command }),
|
|
2221
|
+
intent: intentOf({ tool, command: row.command }),
|
|
2116
2222
|
rawBytes,
|
|
2117
2223
|
returnedBytes,
|
|
2118
2224
|
keptOutBytes: Math.max(0, rawBytes - returnedBytes),
|
|
2119
2225
|
replaced: booleanFlag(row.replaced),
|
|
2120
2226
|
observedAt,
|
|
2121
2227
|
day: observedAt.slice(0, 10),
|
|
2228
|
+
...captureFields(row, onRefuse),
|
|
2122
2229
|
};
|
|
2123
2230
|
};
|
|
2124
2231
|
|
|
@@ -2148,14 +2255,22 @@ const normalizeUsageTurn = (row) => {
|
|
|
2148
2255
|
observedAt,
|
|
2149
2256
|
eventAt: null,
|
|
2150
2257
|
day: observedAt.slice(0, 10),
|
|
2258
|
+
reasoningParts: countOrNull(row.reasoningParts),
|
|
2259
|
+
reasoningChars: countOrNull(row.reasoningChars),
|
|
2260
|
+
reasoningMs: countOrNull(row.reasoningMs),
|
|
2261
|
+
textChars: countOrNull(row.textChars),
|
|
2262
|
+
toolCalls: countOrNull(row.toolCalls),
|
|
2263
|
+
compactions: countOrNull(row.compactions),
|
|
2151
2264
|
};
|
|
2152
2265
|
};
|
|
2153
2266
|
|
|
2154
2267
|
const UPSERT_TOOL_CALL_SQL = `
|
|
2155
2268
|
INSERT INTO tool_calls (
|
|
2156
2269
|
call_key, session_id, call_id, tool, category, raw_bytes, returned_bytes, kept_out_bytes,
|
|
2157
|
-
replaced, origin, observed_at, day, task_slug, attribution
|
|
2158
|
-
|
|
2270
|
+
replaced, origin, observed_at, day, task_slug, attribution,
|
|
2271
|
+
intent, agent, parent_session_id, duration_ms, gap_ms, target_key, repeat_of, repeat_index,
|
|
2272
|
+
exit_code, error_class
|
|
2273
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'measured', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
2159
2274
|
ON CONFLICT(call_key) DO UPDATE SET
|
|
2160
2275
|
session_id = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.session_id ELSE tool_calls.session_id END,
|
|
2161
2276
|
call_id = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.call_id ELSE tool_calls.call_id END,
|
|
@@ -2165,6 +2280,16 @@ ON CONFLICT(call_key) DO UPDATE SET
|
|
|
2165
2280
|
returned_bytes = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.returned_bytes ELSE tool_calls.returned_bytes END,
|
|
2166
2281
|
kept_out_bytes = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.kept_out_bytes ELSE tool_calls.kept_out_bytes END,
|
|
2167
2282
|
replaced = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.replaced ELSE tool_calls.replaced END,
|
|
2283
|
+
intent = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.intent ELSE tool_calls.intent END,
|
|
2284
|
+
agent = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.agent ELSE tool_calls.agent END,
|
|
2285
|
+
parent_session_id = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.parent_session_id ELSE tool_calls.parent_session_id END,
|
|
2286
|
+
duration_ms = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.duration_ms ELSE tool_calls.duration_ms END,
|
|
2287
|
+
gap_ms = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.gap_ms ELSE tool_calls.gap_ms END,
|
|
2288
|
+
target_key = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.target_key ELSE tool_calls.target_key END,
|
|
2289
|
+
repeat_of = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.repeat_of ELSE tool_calls.repeat_of END,
|
|
2290
|
+
repeat_index = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.repeat_index ELSE tool_calls.repeat_index END,
|
|
2291
|
+
exit_code = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.exit_code ELSE tool_calls.exit_code END,
|
|
2292
|
+
error_class = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.error_class ELSE tool_calls.error_class END,
|
|
2168
2293
|
observed_at = MIN(tool_calls.observed_at, excluded.observed_at),
|
|
2169
2294
|
day = MIN(tool_calls.day, excluded.day),
|
|
2170
2295
|
task_slug = CASE WHEN excluded.observed_at < tool_calls.observed_at THEN excluded.task_slug ELSE tool_calls.task_slug END,
|
|
@@ -2175,8 +2300,9 @@ const UPSERT_USAGE_TURN_SQL = `
|
|
|
2175
2300
|
INSERT INTO usage_turns (
|
|
2176
2301
|
turn_key, session_id, message_id, agent, provider_id, model_id, model_variant,
|
|
2177
2302
|
input_tokens, output_tokens, reasoning_tokens, cache_read_tokens, cache_write_tokens,
|
|
2178
|
-
cost_usd, finish, origin, observed_at, event_at, day, task_slug, attribution
|
|
2179
|
-
|
|
2303
|
+
cost_usd, finish, origin, observed_at, event_at, day, task_slug, attribution,
|
|
2304
|
+
reasoning_parts, reasoning_chars, reasoning_ms, text_chars, tool_calls, compactions
|
|
2305
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'measured', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
2180
2306
|
ON CONFLICT(turn_key) DO UPDATE SET
|
|
2181
2307
|
session_id = CASE WHEN excluded.observed_at < usage_turns.observed_at THEN excluded.session_id ELSE usage_turns.session_id END,
|
|
2182
2308
|
message_id = CASE WHEN excluded.observed_at < usage_turns.observed_at THEN excluded.message_id ELSE usage_turns.message_id END,
|
|
@@ -2191,6 +2317,12 @@ ON CONFLICT(turn_key) DO UPDATE SET
|
|
|
2191
2317
|
cache_write_tokens = CASE WHEN excluded.observed_at < usage_turns.observed_at THEN excluded.cache_write_tokens ELSE usage_turns.cache_write_tokens END,
|
|
2192
2318
|
cost_usd = CASE WHEN excluded.observed_at < usage_turns.observed_at THEN excluded.cost_usd ELSE usage_turns.cost_usd END,
|
|
2193
2319
|
finish = CASE WHEN excluded.observed_at < usage_turns.observed_at THEN excluded.finish ELSE usage_turns.finish END,
|
|
2320
|
+
reasoning_parts = CASE WHEN excluded.observed_at < usage_turns.observed_at THEN excluded.reasoning_parts ELSE usage_turns.reasoning_parts END,
|
|
2321
|
+
reasoning_chars = CASE WHEN excluded.observed_at < usage_turns.observed_at THEN excluded.reasoning_chars ELSE usage_turns.reasoning_chars END,
|
|
2322
|
+
reasoning_ms = CASE WHEN excluded.observed_at < usage_turns.observed_at THEN excluded.reasoning_ms ELSE usage_turns.reasoning_ms END,
|
|
2323
|
+
text_chars = CASE WHEN excluded.observed_at < usage_turns.observed_at THEN excluded.text_chars ELSE usage_turns.text_chars END,
|
|
2324
|
+
tool_calls = CASE WHEN excluded.observed_at < usage_turns.observed_at THEN excluded.tool_calls ELSE usage_turns.tool_calls END,
|
|
2325
|
+
compactions = CASE WHEN excluded.observed_at < usage_turns.observed_at THEN excluded.compactions ELSE usage_turns.compactions END,
|
|
2194
2326
|
observed_at = MIN(usage_turns.observed_at, excluded.observed_at),
|
|
2195
2327
|
day = MIN(usage_turns.day, excluded.day),
|
|
2196
2328
|
task_slug = CASE WHEN excluded.observed_at < usage_turns.observed_at THEN excluded.task_slug ELSE usage_turns.task_slug END,
|
|
@@ -2235,34 +2367,139 @@ const projectSessionLeases = (db, teamRoot) => {
|
|
|
2235
2367
|
const slug = typeof value.slug === 'string' ? value.slug : '';
|
|
2236
2368
|
const at = isoTimestamp(value.updatedAt);
|
|
2237
2369
|
if (sessionID === '' || slug === '' || at === null) continue;
|
|
2238
|
-
|
|
2370
|
+
// A v5 lease carries one instant; the window then starts where it ends.
|
|
2371
|
+
const first = isoTimestamp(value.firstSeenAt) ?? at;
|
|
2372
|
+
statement.run(sessionID, slug, first < at ? first : at, at);
|
|
2239
2373
|
projected += 1;
|
|
2240
2374
|
}
|
|
2241
2375
|
return projected;
|
|
2242
2376
|
};
|
|
2243
2377
|
|
|
2378
|
+
const INSTANT_LEASE_MS = 1000;
|
|
2379
|
+
const LEASE_ATTEMPT_SPAN_MS = 7 * 24 * 60 * 60 * 1000;
|
|
2380
|
+
|
|
2381
|
+
/**
|
|
2382
|
+
* An instant lease (the file still has no grown `firstSeenAt`) covers the
|
|
2383
|
+
* attempt span of its slug, at most a week either side of that instant. A
|
|
2384
|
+
* lease that already has a window is left alone. The next-lease clip runs
|
|
2385
|
+
* after this, so a later slug of the same session still owns the overlap.
|
|
2386
|
+
*/
|
|
2387
|
+
const expandInstantLeases = (db, bySession) => {
|
|
2388
|
+
let attempts;
|
|
2389
|
+
try {
|
|
2390
|
+
attempts = db
|
|
2391
|
+
.prepare('SELECT task_slug, started_at, finished_at FROM attempts WHERE started_at IS NOT NULL')
|
|
2392
|
+
.all();
|
|
2393
|
+
} catch {
|
|
2394
|
+
return;
|
|
2395
|
+
}
|
|
2396
|
+
const bySlug = new Map();
|
|
2397
|
+
for (const row of attempts) {
|
|
2398
|
+
const list = bySlug.get(row.task_slug) ?? [];
|
|
2399
|
+
list.push(row);
|
|
2400
|
+
bySlug.set(row.task_slug, list);
|
|
2401
|
+
}
|
|
2402
|
+
for (const sessions of bySession.values()) {
|
|
2403
|
+
for (const window of sessions) {
|
|
2404
|
+
if (window.last - window.first > INSTANT_LEASE_MS) continue;
|
|
2405
|
+
for (const row of bySlug.get(window.slug) ?? []) {
|
|
2406
|
+
const start = Date.parse(row.started_at);
|
|
2407
|
+
const end = row.finished_at ? Date.parse(row.finished_at) : start;
|
|
2408
|
+
if (!Number.isFinite(start) || !Number.isFinite(end)) continue;
|
|
2409
|
+
if (start > window.last + LEASE_ATTEMPT_SPAN_MS) continue;
|
|
2410
|
+
if (end < window.first - LEASE_ATTEMPT_SPAN_MS) continue;
|
|
2411
|
+
if (start < window.first) window.first = start;
|
|
2412
|
+
if (end > window.last) window.last = end;
|
|
2413
|
+
}
|
|
2414
|
+
window.end = window.last + LEASE_TTL_MS;
|
|
2415
|
+
}
|
|
2416
|
+
}
|
|
2417
|
+
};
|
|
2418
|
+
|
|
2419
|
+
/**
|
|
2420
|
+
* The attribution context: the lease windows of every session, the same
|
|
2421
|
+
* windows indexed by task, and the mtime of every task directory.
|
|
2422
|
+
*
|
|
2423
|
+
* A window is `[first, min(last + TTL, next lease first of that session))`, so
|
|
2424
|
+
* two leases of one session never claim the same instant and the later lease
|
|
2425
|
+
* owns the overlap.
|
|
2426
|
+
*/
|
|
2244
2427
|
const leaseWindows = (db) => {
|
|
2245
|
-
const
|
|
2428
|
+
const bySession = new Map();
|
|
2429
|
+
const byTask = new Map();
|
|
2246
2430
|
for (const row of db.prepare('SELECT session_id, task_slug, first_seen_at, last_seen_at FROM session_tasks').all()) {
|
|
2247
2431
|
const first = Date.parse(row.first_seen_at);
|
|
2248
2432
|
const last = Date.parse(row.last_seen_at);
|
|
2249
2433
|
if (!Number.isFinite(first) || !Number.isFinite(last)) continue;
|
|
2250
|
-
const
|
|
2251
|
-
|
|
2252
|
-
|
|
2434
|
+
const window = { slug: row.task_slug, first, last, end: last + LEASE_TTL_MS };
|
|
2435
|
+
const sessions = bySession.get(row.session_id) ?? [];
|
|
2436
|
+
sessions.push(window);
|
|
2437
|
+
bySession.set(row.session_id, sessions);
|
|
2438
|
+
const tasks = byTask.get(row.task_slug) ?? [];
|
|
2439
|
+
tasks.push(window);
|
|
2440
|
+
byTask.set(row.task_slug, tasks);
|
|
2441
|
+
}
|
|
2442
|
+
expandInstantLeases(db, bySession);
|
|
2443
|
+
for (const sessions of bySession.values()) {
|
|
2444
|
+
sessions.sort((left, right) => left.first - right.first || (left.slug < right.slug ? -1 : left.slug > right.slug ? 1 : 0));
|
|
2445
|
+
for (let index = 0; index < sessions.length - 1; index += 1) {
|
|
2446
|
+
sessions[index].end = Math.min(sessions[index].end, sessions[index + 1].first);
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
return { bySession, byTask };
|
|
2450
|
+
};
|
|
2451
|
+
|
|
2452
|
+
const taskDirectoryWindows = (teamRoot) => {
|
|
2453
|
+
const directory = path.join(teamRoot, 'tasks');
|
|
2454
|
+
let names;
|
|
2455
|
+
try {
|
|
2456
|
+
names = fs.readdirSync(directory).sort();
|
|
2457
|
+
} catch {
|
|
2458
|
+
return [];
|
|
2459
|
+
}
|
|
2460
|
+
const windows = [];
|
|
2461
|
+
for (const name of names) {
|
|
2462
|
+
try {
|
|
2463
|
+
const stats = fs.statSync(path.join(directory, name));
|
|
2464
|
+
if (stats.isDirectory()) windows.push({ slug: name, mtime: stats.mtimeMs });
|
|
2465
|
+
} catch {
|
|
2466
|
+
/* a directory that vanished between the read and the stat is no window */
|
|
2467
|
+
}
|
|
2253
2468
|
}
|
|
2254
|
-
return
|
|
2469
|
+
return windows;
|
|
2255
2470
|
};
|
|
2256
2471
|
|
|
2257
|
-
|
|
2258
|
-
|
|
2472
|
+
/**
|
|
2473
|
+
* The ordered ladder `lease | window | dir-mtime | unmapped`. A `lease` hit is
|
|
2474
|
+
* final: a weaker rung never outranks it.
|
|
2475
|
+
*/
|
|
2476
|
+
const resolveAttribution = (context, sessionID, observedAt) => {
|
|
2259
2477
|
const at = Date.parse(observedAt);
|
|
2260
|
-
if (!
|
|
2261
|
-
let
|
|
2262
|
-
for (const window of
|
|
2263
|
-
if (window.first <= at && at
|
|
2478
|
+
if (!Number.isFinite(at)) return { taskSlug: null, attribution: 'unmapped' };
|
|
2479
|
+
let lease = null;
|
|
2480
|
+
for (const window of context.bySession.get(sessionID) ?? []) {
|
|
2481
|
+
if (window.first <= at && at < window.end && (lease === null || window.first >= lease.first)) lease = window;
|
|
2482
|
+
}
|
|
2483
|
+
if (lease !== null) return { taskSlug: lease.slug, attribution: 'lease' };
|
|
2484
|
+
let taskLevel = null;
|
|
2485
|
+
for (const [slug, windows] of context.byTask) {
|
|
2486
|
+
for (const window of windows) {
|
|
2487
|
+
if (window.first <= at && at < window.end && (taskLevel === null || window.first > taskLevel.first)) {
|
|
2488
|
+
taskLevel = { slug, first: window.first };
|
|
2489
|
+
}
|
|
2490
|
+
}
|
|
2491
|
+
}
|
|
2492
|
+
if (taskLevel !== null) return { taskSlug: taskLevel.slug, attribution: 'window' };
|
|
2493
|
+
let nearest = null;
|
|
2494
|
+
for (const entry of context.dirs) {
|
|
2495
|
+
const distance = Math.abs(at - entry.mtime);
|
|
2496
|
+
if (distance > DIR_WINDOW_MS) continue;
|
|
2497
|
+
if (nearest === null || distance < nearest.distance || (distance === nearest.distance && entry.slug < nearest.slug)) {
|
|
2498
|
+
nearest = { slug: entry.slug, distance };
|
|
2499
|
+
}
|
|
2264
2500
|
}
|
|
2265
|
-
|
|
2501
|
+
if (nearest !== null) return { taskSlug: nearest.slug, attribution: 'dir-mtime' };
|
|
2502
|
+
return { taskSlug: null, attribution: 'unmapped' };
|
|
2266
2503
|
};
|
|
2267
2504
|
|
|
2268
2505
|
const sourcePathFor = (teamRoot, file) => {
|
|
@@ -2286,7 +2523,7 @@ const listLedgerFiles = (directory, stem) => {
|
|
|
2286
2523
|
});
|
|
2287
2524
|
};
|
|
2288
2525
|
|
|
2289
|
-
const ingestLedgerFile = ({ db, file, kind, keyOf, ingestedAt, deadlineAt,
|
|
2526
|
+
const ingestLedgerFile = ({ db, file, kind, keyOf, ingestedAt, deadlineAt, context, teamRoot }) => {
|
|
2290
2527
|
let stats;
|
|
2291
2528
|
try {
|
|
2292
2529
|
stats = fs.statSync(file);
|
|
@@ -2314,6 +2551,7 @@ const ingestLedgerFile = ({ db, file, kind, keyOf, ingestedAt, deadlineAt, windo
|
|
|
2314
2551
|
|
|
2315
2552
|
const statement = db.prepare(kind === 'results' ? UPSERT_TOOL_CALL_SQL : UPSERT_USAGE_TURN_SQL);
|
|
2316
2553
|
let invalid = 0;
|
|
2554
|
+
let refused = 0;
|
|
2317
2555
|
let rows = 0;
|
|
2318
2556
|
let lastKey = previous?.last_key ?? '';
|
|
2319
2557
|
db.exec('BEGIN IMMEDIATE');
|
|
@@ -2335,13 +2573,13 @@ const ingestLedgerFile = ({ db, file, kind, keyOf, ingestedAt, deadlineAt, windo
|
|
|
2335
2573
|
lastKey = '';
|
|
2336
2574
|
continue;
|
|
2337
2575
|
}
|
|
2338
|
-
const row = kind === 'results' ? normalizeToolCall(raw) : normalizeUsageTurn(raw);
|
|
2576
|
+
const row = kind === 'results' ? normalizeToolCall(raw, () => { refused += 1; }) : normalizeUsageTurn(raw);
|
|
2339
2577
|
if (row === null) {
|
|
2340
2578
|
invalid += 1;
|
|
2341
2579
|
lastKey = '';
|
|
2342
2580
|
continue;
|
|
2343
2581
|
}
|
|
2344
|
-
const { taskSlug, attribution } = resolveAttribution(
|
|
2582
|
+
const { taskSlug, attribution } = resolveAttribution(context, row.sessionID, row.observedAt);
|
|
2345
2583
|
if (kind === 'results') {
|
|
2346
2584
|
statement.run(
|
|
2347
2585
|
row.key,
|
|
@@ -2357,6 +2595,16 @@ const ingestLedgerFile = ({ db, file, kind, keyOf, ingestedAt, deadlineAt, windo
|
|
|
2357
2595
|
row.day,
|
|
2358
2596
|
taskSlug,
|
|
2359
2597
|
attribution,
|
|
2598
|
+
row.intent,
|
|
2599
|
+
row.agent,
|
|
2600
|
+
row.parentSessionId,
|
|
2601
|
+
row.durationMs,
|
|
2602
|
+
row.gapMs,
|
|
2603
|
+
row.targetKey,
|
|
2604
|
+
row.repeatOf,
|
|
2605
|
+
row.repeatIndex,
|
|
2606
|
+
row.exitCode,
|
|
2607
|
+
row.errorClass,
|
|
2360
2608
|
);
|
|
2361
2609
|
} else {
|
|
2362
2610
|
statement.run(
|
|
@@ -2379,6 +2627,12 @@ const ingestLedgerFile = ({ db, file, kind, keyOf, ingestedAt, deadlineAt, windo
|
|
|
2379
2627
|
row.day,
|
|
2380
2628
|
taskSlug,
|
|
2381
2629
|
attribution,
|
|
2630
|
+
row.reasoningParts,
|
|
2631
|
+
row.reasoningChars,
|
|
2632
|
+
row.reasoningMs,
|
|
2633
|
+
row.textChars,
|
|
2634
|
+
row.toolCalls,
|
|
2635
|
+
row.compactions,
|
|
2382
2636
|
);
|
|
2383
2637
|
}
|
|
2384
2638
|
rows += 1;
|
|
@@ -2394,9 +2648,230 @@ const ingestLedgerFile = ({ db, file, kind, keyOf, ingestedAt, deadlineAt, windo
|
|
|
2394
2648
|
}
|
|
2395
2649
|
throw error;
|
|
2396
2650
|
}
|
|
2397
|
-
return { rows, invalid, ingested: true };
|
|
2651
|
+
return { rows, invalid, refused, ingested: true };
|
|
2398
2652
|
};
|
|
2399
2653
|
|
|
2654
|
+
const RUNG_RANK = { unmapped: 0, 'dir-mtime': 1, window: 2, lease: 3 };
|
|
2655
|
+
const BOUND_RUNGS = new Set(['lease', 'window']);
|
|
2656
|
+
const EPISODE_CAP = 40;
|
|
2657
|
+
|
|
2658
|
+
/**
|
|
2659
|
+
* The attempt whose window contains the call. A matching agent outranks a
|
|
2660
|
+
* shorter window; otherwise the narrowest window wins, then the later start.
|
|
2661
|
+
* Returns null when the call has no task or no attempt covers it.
|
|
2662
|
+
*/
|
|
2663
|
+
export function matchAttemptId(call, attempts) {
|
|
2664
|
+
const at = Date.parse(call.observedAt ?? '');
|
|
2665
|
+
if (!Number.isFinite(at) || !call.taskSlug) return null;
|
|
2666
|
+
let best = null;
|
|
2667
|
+
for (const attempt of attempts) {
|
|
2668
|
+
if (attempt.taskSlug !== call.taskSlug || !attempt.attemptId) continue;
|
|
2669
|
+
const start = Date.parse(attempt.startedAt ?? '');
|
|
2670
|
+
if (!Number.isFinite(start) || start > at) continue;
|
|
2671
|
+
const finished = attempt.finishedAt ? Date.parse(attempt.finishedAt) : Number.POSITIVE_INFINITY;
|
|
2672
|
+
if (!Number.isFinite(finished) || at > finished) continue;
|
|
2673
|
+
const agentMatch = Boolean(call.agent && attempt.agent && call.agent === attempt.agent);
|
|
2674
|
+
const span = finished === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : finished - start;
|
|
2675
|
+
const rank = { agentMatch, span, start, attemptId: attempt.attemptId, agent: attempt.agent ?? null };
|
|
2676
|
+
if (
|
|
2677
|
+
best === null
|
|
2678
|
+
|| rank.agentMatch > best.agentMatch
|
|
2679
|
+
|| (rank.agentMatch === best.agentMatch && rank.span < best.span)
|
|
2680
|
+
|| (rank.agentMatch === best.agentMatch && rank.span === best.span && rank.start > best.start)
|
|
2681
|
+
) {
|
|
2682
|
+
best = rank;
|
|
2683
|
+
}
|
|
2684
|
+
}
|
|
2685
|
+
return best;
|
|
2686
|
+
}
|
|
2687
|
+
|
|
2688
|
+
const reattributeRows = (db, teamRoot) => {
|
|
2689
|
+
const context = { ...leaseWindows(db), dirs: taskDirectoryWindows(teamRoot) };
|
|
2690
|
+
const updateCall = db.prepare('UPDATE tool_calls SET task_slug = ?, attribution = ? WHERE call_key = ?');
|
|
2691
|
+
const updateTurn = db.prepare('UPDATE usage_turns SET task_slug = ?, attribution = ? WHERE turn_key = ?');
|
|
2692
|
+
let upgraded = 0;
|
|
2693
|
+
for (const row of db.prepare('SELECT call_key, session_id, observed_at, attribution FROM tool_calls').all()) {
|
|
2694
|
+
const next = resolveAttribution(context, row.session_id, row.observed_at);
|
|
2695
|
+
if ((RUNG_RANK[next.attribution] ?? 0) <= (RUNG_RANK[row.attribution] ?? 0)) continue;
|
|
2696
|
+
updateCall.run(next.taskSlug, next.attribution, row.call_key);
|
|
2697
|
+
upgraded += 1;
|
|
2698
|
+
}
|
|
2699
|
+
for (const row of db.prepare('SELECT turn_key, session_id, observed_at, attribution FROM usage_turns').all()) {
|
|
2700
|
+
const next = resolveAttribution(context, row.session_id, row.observed_at);
|
|
2701
|
+
if ((RUNG_RANK[next.attribution] ?? 0) <= (RUNG_RANK[row.attribution] ?? 0)) continue;
|
|
2702
|
+
updateTurn.run(next.taskSlug, next.attribution, row.turn_key);
|
|
2703
|
+
upgraded += 1;
|
|
2704
|
+
}
|
|
2705
|
+
return upgraded;
|
|
2706
|
+
};
|
|
2707
|
+
|
|
2708
|
+
const bindAttemptIds = (db) => {
|
|
2709
|
+
const attempts = db
|
|
2710
|
+
.prepare(
|
|
2711
|
+
`SELECT attempt_id AS attemptId, task_slug AS taskSlug, agent, started_at AS startedAt, finished_at AS finishedAt
|
|
2712
|
+
FROM attempts WHERE started_at IS NOT NULL`,
|
|
2713
|
+
)
|
|
2714
|
+
.all();
|
|
2715
|
+
const update = db.prepare('UPDATE tool_calls SET attempt_id = ?, agent = COALESCE(agent, ?) WHERE call_key = ?');
|
|
2716
|
+
const clear = db.prepare('UPDATE tool_calls SET attempt_id = NULL WHERE call_key = ?');
|
|
2717
|
+
let bound = 0;
|
|
2718
|
+
for (const call of db.prepare('SELECT call_key, task_slug, observed_at, agent, attribution FROM tool_calls').all()) {
|
|
2719
|
+
if (!BOUND_RUNGS.has(call.attribution)) {
|
|
2720
|
+
clear.run(call.call_key);
|
|
2721
|
+
continue;
|
|
2722
|
+
}
|
|
2723
|
+
const match = matchAttemptId(
|
|
2724
|
+
{ taskSlug: call.task_slug, observedAt: call.observed_at, agent: call.agent },
|
|
2725
|
+
attempts,
|
|
2726
|
+
);
|
|
2727
|
+
if (!match) {
|
|
2728
|
+
clear.run(call.call_key);
|
|
2729
|
+
continue;
|
|
2730
|
+
}
|
|
2731
|
+
update.run(match.attemptId, match.agent, call.call_key);
|
|
2732
|
+
bound += 1;
|
|
2733
|
+
}
|
|
2734
|
+
return bound;
|
|
2735
|
+
};
|
|
2736
|
+
|
|
2737
|
+
const rebuildEpisodes = (db) => {
|
|
2738
|
+
db.exec('DELETE FROM attempt_episodes');
|
|
2739
|
+
const insert = db.prepare(
|
|
2740
|
+
`INSERT INTO attempt_episodes (attempt_id, ordinal, intent, target_key, error_class, observed_at)
|
|
2741
|
+
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
2742
|
+
);
|
|
2743
|
+
const counts = new Map();
|
|
2744
|
+
let episodes = 0;
|
|
2745
|
+
for (const call of db
|
|
2746
|
+
.prepare(
|
|
2747
|
+
`SELECT attempt_id, intent, target_key, error_class, observed_at
|
|
2748
|
+
FROM tool_calls WHERE attempt_id IS NOT NULL
|
|
2749
|
+
ORDER BY attempt_id, observed_at, call_key`,
|
|
2750
|
+
)
|
|
2751
|
+
.all()) {
|
|
2752
|
+
const ordinal = counts.get(call.attempt_id) ?? 0;
|
|
2753
|
+
if (ordinal >= EPISODE_CAP) continue;
|
|
2754
|
+
counts.set(call.attempt_id, ordinal + 1);
|
|
2755
|
+
insert.run(call.attempt_id, ordinal, call.intent, call.target_key, call.error_class, call.observed_at);
|
|
2756
|
+
episodes += 1;
|
|
2757
|
+
}
|
|
2758
|
+
return episodes;
|
|
2759
|
+
};
|
|
2760
|
+
|
|
2761
|
+
const fillGaps = (db) => {
|
|
2762
|
+
const update = db.prepare('UPDATE tool_calls SET gap_ms = ? WHERE call_key = ? AND gap_ms IS NULL');
|
|
2763
|
+
let filled = 0;
|
|
2764
|
+
let previous = null;
|
|
2765
|
+
for (const row of db
|
|
2766
|
+
.prepare('SELECT call_key, session_id, observed_at, gap_ms FROM tool_calls ORDER BY session_id, observed_at, call_key')
|
|
2767
|
+
.all()) {
|
|
2768
|
+
if (previous && previous.session_id === row.session_id && row.gap_ms === null) {
|
|
2769
|
+
const gap = Date.parse(row.observed_at) - Date.parse(previous.observed_at);
|
|
2770
|
+
if (Number.isFinite(gap) && gap >= 0 && gap <= LEASE_TTL_MS) {
|
|
2771
|
+
update.run(gap, row.call_key);
|
|
2772
|
+
filled += 1;
|
|
2773
|
+
}
|
|
2774
|
+
}
|
|
2775
|
+
previous = row;
|
|
2776
|
+
}
|
|
2777
|
+
return filled;
|
|
2778
|
+
};
|
|
2779
|
+
|
|
2780
|
+
const foldSessionUsage = (db) => {
|
|
2781
|
+
const attempts = db
|
|
2782
|
+
.prepare(
|
|
2783
|
+
`SELECT attempt_id, task_slug, started_at, finished_at, usage_source FROM attempts
|
|
2784
|
+
WHERE (usage_source IS NULL OR usage_source = 'session') AND started_at IS NOT NULL`,
|
|
2785
|
+
)
|
|
2786
|
+
.all();
|
|
2787
|
+
const byTask = new Map();
|
|
2788
|
+
for (const turn of db
|
|
2789
|
+
.prepare(
|
|
2790
|
+
`SELECT task_slug, observed_at, input_tokens, output_tokens, reasoning_tokens,
|
|
2791
|
+
cache_read_tokens, cache_write_tokens, cost_usd, provider_id, model_id
|
|
2792
|
+
FROM usage_turns WHERE attribution IN ('lease', 'window') AND task_slug IS NOT NULL`,
|
|
2793
|
+
)
|
|
2794
|
+
.all()) {
|
|
2795
|
+
const list = byTask.get(turn.task_slug) ?? [];
|
|
2796
|
+
list.push(turn);
|
|
2797
|
+
byTask.set(turn.task_slug, list);
|
|
2798
|
+
}
|
|
2799
|
+
const update = db.prepare(
|
|
2800
|
+
`UPDATE attempts SET
|
|
2801
|
+
input_tokens = ?, output_tokens = ?, reasoning_tokens = ?,
|
|
2802
|
+
cache_read_tokens = ?, cache_write_tokens = ?, cost_usd = ?,
|
|
2803
|
+
usage_origin = 'measured', usage_source = 'session', usage_turn_count = ?,
|
|
2804
|
+
model_provider = ?, model_id = ?
|
|
2805
|
+
WHERE attempt_id = ? AND (usage_source IS NULL OR usage_source = 'session')`,
|
|
2806
|
+
);
|
|
2807
|
+
const clear = db.prepare(
|
|
2808
|
+
`UPDATE attempts SET
|
|
2809
|
+
input_tokens = NULL, output_tokens = NULL, reasoning_tokens = NULL,
|
|
2810
|
+
cache_read_tokens = NULL, cache_write_tokens = NULL, cost_usd = NULL,
|
|
2811
|
+
usage_origin = NULL, usage_source = NULL, usage_turn_count = NULL,
|
|
2812
|
+
model_provider = NULL, model_id = NULL
|
|
2813
|
+
WHERE attempt_id = ? AND usage_source = 'session'`,
|
|
2814
|
+
);
|
|
2815
|
+
let usage = 0;
|
|
2816
|
+
for (const attempt of attempts) {
|
|
2817
|
+
const start = Date.parse(attempt.started_at);
|
|
2818
|
+
const end = attempt.finished_at ? Date.parse(attempt.finished_at) : Number.POSITIVE_INFINITY;
|
|
2819
|
+
const matched = (byTask.get(attempt.task_slug) ?? []).filter((turn) => {
|
|
2820
|
+
const at = Date.parse(turn.observed_at);
|
|
2821
|
+
return Number.isFinite(at) && at >= start && at <= end;
|
|
2822
|
+
});
|
|
2823
|
+
if (matched.length === 0) {
|
|
2824
|
+
if (attempt.usage_source === 'session') clear.run(attempt.attempt_id);
|
|
2825
|
+
continue;
|
|
2826
|
+
}
|
|
2827
|
+
const sum = (key) => matched.reduce((total, turn) => total + Number(turn[key] ?? 0), 0);
|
|
2828
|
+
let model = matched[0];
|
|
2829
|
+
for (const turn of matched) {
|
|
2830
|
+
if (Number(turn.input_tokens) > Number(model.input_tokens)) model = turn;
|
|
2831
|
+
}
|
|
2832
|
+
update.run(
|
|
2833
|
+
sum('input_tokens'),
|
|
2834
|
+
sum('output_tokens'),
|
|
2835
|
+
sum('reasoning_tokens'),
|
|
2836
|
+
sum('cache_read_tokens'),
|
|
2837
|
+
sum('cache_write_tokens'),
|
|
2838
|
+
sum('cost_usd'),
|
|
2839
|
+
matched.length,
|
|
2840
|
+
model.provider_id,
|
|
2841
|
+
model.model_id,
|
|
2842
|
+
attempt.attempt_id,
|
|
2843
|
+
);
|
|
2844
|
+
usage += 1;
|
|
2845
|
+
}
|
|
2846
|
+
return usage;
|
|
2847
|
+
};
|
|
2848
|
+
|
|
2849
|
+
/**
|
|
2850
|
+
* Join already-projected calls to attempts. Stronger attribution replaces a
|
|
2851
|
+
* weaker one; host and CLI usage are never overwritten. Safe to re-run.
|
|
2852
|
+
*/
|
|
2853
|
+
export function bindBehaviorResearch(db, teamRoot) {
|
|
2854
|
+
const columns = db.prepare('PRAGMA table_info(tool_calls)').all().map((row) => row.name);
|
|
2855
|
+
if (!columns.includes('attempt_id')) return { upgraded: 0, bound: 0, episodes: 0, gaps: 0, usage: 0 };
|
|
2856
|
+
db.exec('BEGIN IMMEDIATE');
|
|
2857
|
+
try {
|
|
2858
|
+
const upgraded = teamRoot ? reattributeRows(db, teamRoot) : 0;
|
|
2859
|
+
const bound = bindAttemptIds(db);
|
|
2860
|
+
const episodes = rebuildEpisodes(db);
|
|
2861
|
+
const gaps = fillGaps(db);
|
|
2862
|
+
const usage = foldSessionUsage(db);
|
|
2863
|
+
db.exec('COMMIT');
|
|
2864
|
+
return { upgraded, bound, episodes, gaps, usage };
|
|
2865
|
+
} catch (error) {
|
|
2866
|
+
try {
|
|
2867
|
+
db.exec('ROLLBACK');
|
|
2868
|
+
} catch {
|
|
2869
|
+
/* the original error is the finding */
|
|
2870
|
+
}
|
|
2871
|
+
throw error;
|
|
2872
|
+
}
|
|
2873
|
+
}
|
|
2874
|
+
|
|
2400
2875
|
/**
|
|
2401
2876
|
* Ingest the OpenCode ledgers into `tool_calls` / `usage_turns` and the
|
|
2402
2877
|
* session leases into `session_tasks`. Full sync only; a per-file watermark
|
|
@@ -2405,10 +2880,11 @@ const ingestLedgerFile = ({ db, file, kind, keyOf, ingestedAt, deadlineAt, windo
|
|
|
2405
2880
|
export function projectTelemetry({ db, teamRoot, now, deadlineAt = Number.POSITIVE_INFINITY, env = process.env }) {
|
|
2406
2881
|
const ingestedAt = isoTimestamp(now) ?? new Date().toISOString();
|
|
2407
2882
|
projectSessionLeases(db, teamRoot);
|
|
2408
|
-
const
|
|
2883
|
+
const context = { ...leaseWindows(db), dirs: taskDirectoryWindows(teamRoot) };
|
|
2409
2884
|
const directory = telemetryDirectory(teamRoot, env);
|
|
2410
2885
|
let rows = 0;
|
|
2411
2886
|
let invalid = 0;
|
|
2887
|
+
let refused = 0;
|
|
2412
2888
|
let sources = 0;
|
|
2413
2889
|
let expired = false;
|
|
2414
2890
|
for (const [stem, kind, keyOf] of [
|
|
@@ -2420,14 +2896,20 @@ export function projectTelemetry({ db, teamRoot, now, deadlineAt = Number.POSITI
|
|
|
2420
2896
|
expired = true;
|
|
2421
2897
|
break;
|
|
2422
2898
|
}
|
|
2423
|
-
const outcome = ingestLedgerFile({ db, file, kind, keyOf, ingestedAt, deadlineAt,
|
|
2899
|
+
const outcome = ingestLedgerFile({ db, file, kind, keyOf, ingestedAt, deadlineAt, context, teamRoot });
|
|
2424
2900
|
rows += outcome.rows;
|
|
2425
2901
|
invalid += outcome.invalid;
|
|
2902
|
+
refused += outcome.refused ?? 0;
|
|
2426
2903
|
if (outcome.ingested) sources += 1;
|
|
2427
2904
|
}
|
|
2428
2905
|
if (expired) break;
|
|
2429
2906
|
}
|
|
2430
|
-
|
|
2907
|
+
// One line per run, never one per row: a refused value is dropped, and the
|
|
2908
|
+
// error log names the count without carrying the value.
|
|
2909
|
+
if (refused > 0) {
|
|
2910
|
+
logProjectionFailure('E_CAPTURE_REFUSED', `${refused} captured value(s) dropped as text, absolute or out of range`, env);
|
|
2911
|
+
}
|
|
2912
|
+
return { rows, invalid, refused, sources, expired };
|
|
2431
2913
|
}
|
|
2432
2914
|
|
|
2433
2915
|
const logProjectionFailure = (code, message, env = process.env) => {
|
|
@@ -2670,6 +3152,13 @@ export function syncProjection(options = {}) {
|
|
|
2670
3152
|
logProjectionFailure('E_TELEMETRY_FAILED', error.message, options.env);
|
|
2671
3153
|
}
|
|
2672
3154
|
}
|
|
3155
|
+
if (!slug) {
|
|
3156
|
+
try {
|
|
3157
|
+
bindBehaviorResearch(db, handle.teamRoot);
|
|
3158
|
+
} catch (error) {
|
|
3159
|
+
logProjectionFailure('E_TELEMETRY_FAILED', error.message, options.env);
|
|
3160
|
+
}
|
|
3161
|
+
}
|
|
2673
3162
|
if (maintain) {
|
|
2674
3163
|
result.retained += maintainDatabase(db, startedAt).retained;
|
|
2675
3164
|
retainRuns(db);
|