@bonesofspring/ai-rules 0.2.73 → 0.2.74
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 +23 -0
- package/README.md +1 -1
- package/bin/lib/engine-scripts.js +8 -0
- package/docs/TEAM-DB.md +86 -5
- package/docs/openapi/team-db.v1.json +1164 -224
- package/package.json +1 -1
- 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/processes.mjs +5 -4
- package/presets/_shared/ai-local/scripts/rr-scheduler-start.mjs +161 -0
- package/presets/_shared/ai-local/scripts/team-db-api.mjs +347 -9
- package/presets/_shared/ai-local/scripts/team-db-openapi.mjs +309 -4
- package/presets/_shared/ai-local/scripts/team-db-project.mjs +239 -25
- package/presets/_shared/ai-local/scripts/team-db-schema.mjs +86 -1
- package/presets/_shared/hooks/chain-team-phases.sh.tmpl +15 -1
- 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 +15 -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 +15 -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 +15 -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 +15 -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 +15 -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 +15 -1
- 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 +15 -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 +15 -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 +15 -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 +15 -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 +15 -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 +15 -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 +15 -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 +15 -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 +15 -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 +15 -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 +15 -1
- 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 +15 -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 +15 -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 +15 -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 +15 -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 +15 -1
- package/presets/mavis/android-kotlin/adapter-map.json +9 -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 +15 -1
- package/presets/mavis/go/adapter-map.json +9 -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 +15 -1
- package/presets/mavis/ios-swift/adapter-map.json +9 -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 +15 -1
- package/presets/mavis/java/adapter-map.json +9 -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 +15 -1
- package/presets/mavis/mcp-ts/adapter-map.json +9 -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 +15 -1
- package/presets/mavis/mol/adapter-map.json +9 -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 +15 -1
- package/presets/mavis/next/adapter-map.json +9 -1
- 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 +15 -1
- package/presets/mavis/nuxt/adapter-map.json +9 -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 +15 -1
- package/presets/mavis/php-hexagonal/adapter-map.json +9 -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 +15 -1
- package/presets/mavis/php-laravel/adapter-map.json +9 -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 +15 -1
- package/presets/mavis/svelte/adapter-map.json +9 -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 +15 -1
- package/presets/opencode/android-kotlin/adapter-map.json +9 -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 +15 -1
- package/presets/opencode/go/adapter-map.json +9 -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 +15 -1
- package/presets/opencode/ios-swift/adapter-map.json +9 -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 +15 -1
- package/presets/opencode/java/adapter-map.json +9 -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 +15 -1
- package/presets/opencode/mcp-ts/adapter-map.json +9 -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 +15 -1
- package/presets/opencode/mol/adapter-map.json +9 -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 +15 -1
- package/presets/opencode/next/adapter-map.json +9 -1
- 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 +15 -1
- package/presets/opencode/nuxt/adapter-map.json +9 -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 +15 -1
- package/presets/opencode/php-hexagonal/adapter-map.json +9 -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 +15 -1
- package/presets/opencode/php-laravel/adapter-map.json +9 -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 +15 -1
- package/presets/opencode/plugins/output-guard/README.md +42 -1
- package/presets/opencode/plugins/output-guard/actions.ts +201 -0
- package/presets/opencode/plugins/output-guard/index.ts +147 -6
- package/presets/opencode/svelte/adapter-map.json +9 -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 +15 -1
- package/presets/zcode/android-kotlin/adapter-map.json +9 -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 +15 -1
- package/presets/zcode/go/adapter-map.json +9 -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 +15 -1
- package/presets/zcode/ios-swift/adapter-map.json +9 -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 +15 -1
- package/presets/zcode/java/adapter-map.json +9 -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 +15 -1
- package/presets/zcode/mcp-ts/adapter-map.json +9 -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 +15 -1
- package/presets/zcode/mol/adapter-map.json +9 -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 +15 -1
- package/presets/zcode/next/adapter-map.json +9 -1
- 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 +15 -1
- package/presets/zcode/nuxt/adapter-map.json +9 -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 +15 -1
- package/presets/zcode/php-hexagonal/adapter-map.json +9 -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 +15 -1
- package/presets/zcode/php-laravel/adapter-map.json +9 -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 +15 -1
- package/presets/zcode/svelte/adapter-map.json +9 -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 +15 -1
package/package.json
CHANGED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The action rollup and its signal set.
|
|
3
|
+
*
|
|
4
|
+
* `task_action_summary` and `action_signals` are views over `tool_calls` and
|
|
5
|
+
* `usage_turns`, so the numbers here are read-time facts of the base rows. This
|
|
6
|
+
* module is the one place the ratios, the thresholds and the rule text live: a
|
|
7
|
+
* recalibration is a code change with no migration and no backfill, which is why
|
|
8
|
+
* the views carry numerators only.
|
|
9
|
+
*
|
|
10
|
+
* The report CLI and the HTTP API both read this module, so a signal the report
|
|
11
|
+
* flags is a signal the API serves.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** The seven intent values, in the order the interface lists them. */
|
|
15
|
+
export const ACTION_INTENTS = ['explore', 'mutate', 'execute', 'delegate', 'ask', 'meta', 'web'];
|
|
16
|
+
|
|
17
|
+
/** The five failure classes a captured action may name. */
|
|
18
|
+
export const ACTION_ERROR_CLASSES = ['validation', 'notfound', 'permission', 'timeout', 'upstream'];
|
|
19
|
+
|
|
20
|
+
/** The three severities of a fired signal, worst first. */
|
|
21
|
+
export const SIGNAL_SEVERITIES = ['critical', 'warn', 'info'];
|
|
22
|
+
|
|
23
|
+
/** The rung names of the attribution ladder, strongest first. */
|
|
24
|
+
export const ATTRIBUTION_RUNGS = ['lease', 'window', 'dir-mtime', 'unmapped'];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The calibrated thresholds.
|
|
28
|
+
*
|
|
29
|
+
* Calibration cases (`analysis.md` §5.11): the `agent-overhead-investigation`
|
|
30
|
+
* session — 155 turns, 277 tool calls, 0 changed files — must fire, and a clean
|
|
31
|
+
* single-work-package task must not. `test-team-db-efficiency.mjs` pins both.
|
|
32
|
+
*/
|
|
33
|
+
export const SIGNAL_THRESHOLDS = {
|
|
34
|
+
/** `noop_actions`: actions and no mutated file. */
|
|
35
|
+
noopMinActions: 20,
|
|
36
|
+
/** `explore_without_mutate`: explore actions and no mutation. */
|
|
37
|
+
exploreMinActions: 20,
|
|
38
|
+
errorWarnRatio: 0.2,
|
|
39
|
+
errorCriticalRatio: 0.4,
|
|
40
|
+
repeatWarnRatio: 0.5,
|
|
41
|
+
guardInfoRatio: 0.25,
|
|
42
|
+
compactionWarnCount: 3,
|
|
43
|
+
weakAttributionInfoRatio: 0.5,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** One line per signal: what fired, in the units of the value it carries. */
|
|
47
|
+
export const SIGNAL_RULES = {
|
|
48
|
+
noop_actions: '20 or more actions and no file was ever mutated',
|
|
49
|
+
explore_without_mutate: '20 or more explore actions and no mutation',
|
|
50
|
+
error_rate: 'a fifth or more of the actions failed',
|
|
51
|
+
repeat_churn: 'half or more of the actions repeated a target',
|
|
52
|
+
guard_pressure: 'a quarter or more of the results needed the guard rewrite',
|
|
53
|
+
context_thrash: 'three or more compactions in one task',
|
|
54
|
+
weak_attribution: 'half or more of the actions were mapped by a fuzzy rung',
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** The signal names, one per rule, in declaration order. */
|
|
58
|
+
export const ACTION_SIGNAL_NAMES = Object.keys(SIGNAL_RULES);
|
|
59
|
+
|
|
60
|
+
const SEVERITY_RANK = new Map(SIGNAL_SEVERITIES.map((severity, index) => [severity, index]));
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The contract order of two fired signals: worst severity first, then by name.
|
|
64
|
+
*
|
|
65
|
+
* A window-wide read collects task by task, so a caller adds the task slug as
|
|
66
|
+
* the final tiebreak and gets one order for the whole array.
|
|
67
|
+
*/
|
|
68
|
+
export const compareSignals = (left, right) =>
|
|
69
|
+
SEVERITY_RANK.get(left.severity) - SEVERITY_RANK.get(right.severity) ||
|
|
70
|
+
(left.signal < right.signal ? -1 : left.signal > right.signal ? 1 : 0);
|
|
71
|
+
|
|
72
|
+
const ratio = (numerator, denominator) => (denominator === 0 ? null : numerator / denominator);
|
|
73
|
+
|
|
74
|
+
/** A `task_action_summary` row as the readers use it, every ratio null-safe. */
|
|
75
|
+
export const summaryOf = (row) => {
|
|
76
|
+
if (!row) return null;
|
|
77
|
+
const actions = Number(row.actions ?? 0);
|
|
78
|
+
const turns = Number(row.turns ?? 0);
|
|
79
|
+
const changedFiles = Number(row.changed_files ?? 0);
|
|
80
|
+
const errorActions = Number(row.error_actions ?? 0);
|
|
81
|
+
const repeatActions = Number(row.repeat_actions ?? 0);
|
|
82
|
+
const guardHits = Number(row.guard_hits ?? 0);
|
|
83
|
+
const firstAt = row.first_at ?? null;
|
|
84
|
+
const lastAt = row.last_at ?? null;
|
|
85
|
+
const first = firstAt === null ? Number.NaN : Date.parse(firstAt);
|
|
86
|
+
const last = lastAt === null ? Number.NaN : Date.parse(lastAt);
|
|
87
|
+
const tokens = Number(row.tokens ?? 0);
|
|
88
|
+
const byRung = {
|
|
89
|
+
lease: Number(row.rung_lease ?? 0),
|
|
90
|
+
window: Number(row.rung_window ?? 0),
|
|
91
|
+
'dir-mtime': Number(row.rung_dir_mtime ?? 0),
|
|
92
|
+
unmapped: Number(row.rung_unmapped ?? 0),
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
actions,
|
|
96
|
+
turns,
|
|
97
|
+
changedFiles,
|
|
98
|
+
errorActions,
|
|
99
|
+
repeatActions,
|
|
100
|
+
guardHits,
|
|
101
|
+
tokens,
|
|
102
|
+
reasoningParts: Number(row.reasoning_parts ?? 0),
|
|
103
|
+
compactions: Number(row.compactions ?? 0),
|
|
104
|
+
measuredActions: Number(row.measured_actions ?? 0),
|
|
105
|
+
busyMs: Number(row.busy_ms ?? 0),
|
|
106
|
+
wallMs: Number.isFinite(first) && Number.isFinite(last) ? Math.max(0, last - first) : null,
|
|
107
|
+
firstAt,
|
|
108
|
+
lastAt,
|
|
109
|
+
errorRate: ratio(errorActions, actions),
|
|
110
|
+
repeatRatio: ratio(repeatActions, actions),
|
|
111
|
+
guardRatio: ratio(guardHits, actions),
|
|
112
|
+
unmappedRatio: ratio(byRung.unmapped, actions),
|
|
113
|
+
weakRatio: ratio(byRung.window + byRung['dir-mtime'], actions),
|
|
114
|
+
tokensPerAction: ratio(tokens, actions),
|
|
115
|
+
actionsPerChangedFile: ratio(actions, changedFiles),
|
|
116
|
+
byIntent: {
|
|
117
|
+
explore: Number(row.explore ?? 0),
|
|
118
|
+
mutate: Number(row.mutate ?? 0),
|
|
119
|
+
execute: Number(row.execute ?? 0),
|
|
120
|
+
delegate: Number(row.delegate ?? 0),
|
|
121
|
+
ask: Number(row.ask ?? 0),
|
|
122
|
+
meta: Number(row.meta ?? 0),
|
|
123
|
+
web: Number(row.web ?? 0),
|
|
124
|
+
},
|
|
125
|
+
byRung,
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const RULES = [
|
|
130
|
+
{
|
|
131
|
+
signal: 'noop_actions',
|
|
132
|
+
fired: (summary) => summary.changedFiles === 0 && summary.actions >= SIGNAL_THRESHOLDS.noopMinActions,
|
|
133
|
+
severity: () => 'critical',
|
|
134
|
+
value: (summary) => summary.actions,
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
signal: 'explore_without_mutate',
|
|
138
|
+
fired: (summary) =>
|
|
139
|
+
summary.byIntent.mutate === 0 && summary.byIntent.explore >= SIGNAL_THRESHOLDS.exploreMinActions,
|
|
140
|
+
severity: () => 'warn',
|
|
141
|
+
value: (summary) => summary.byIntent.explore,
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
signal: 'error_rate',
|
|
145
|
+
fired: (summary) => summary.errorRate !== null && summary.errorRate >= SIGNAL_THRESHOLDS.errorWarnRatio,
|
|
146
|
+
severity: (summary) =>
|
|
147
|
+
summary.errorRate !== null && summary.errorRate >= SIGNAL_THRESHOLDS.errorCriticalRatio ? 'critical' : 'warn',
|
|
148
|
+
value: (summary) => summary.errorActions,
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
signal: 'repeat_churn',
|
|
152
|
+
fired: (summary) => summary.repeatRatio !== null && summary.repeatRatio >= SIGNAL_THRESHOLDS.repeatWarnRatio,
|
|
153
|
+
severity: () => 'warn',
|
|
154
|
+
value: (summary) => summary.repeatActions,
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
signal: 'guard_pressure',
|
|
158
|
+
fired: (summary) => summary.guardRatio !== null && summary.guardRatio >= SIGNAL_THRESHOLDS.guardInfoRatio,
|
|
159
|
+
severity: () => 'info',
|
|
160
|
+
value: (summary) => summary.guardHits,
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
signal: 'context_thrash',
|
|
164
|
+
fired: (summary) => summary.compactions >= SIGNAL_THRESHOLDS.compactionWarnCount,
|
|
165
|
+
severity: () => 'warn',
|
|
166
|
+
value: (summary) => summary.compactions,
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
signal: 'weak_attribution',
|
|
170
|
+
fired: (summary) => summary.weakRatio !== null && summary.weakRatio >= SIGNAL_THRESHOLDS.weakAttributionInfoRatio,
|
|
171
|
+
severity: () => 'info',
|
|
172
|
+
value: (summary) => summary.byRung.window + summary.byRung['dir-mtime'],
|
|
173
|
+
},
|
|
174
|
+
];
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The fired signals of one task, worst first and then by name.
|
|
178
|
+
*
|
|
179
|
+
* The order is part of the contract: the interface renders the array verbatim,
|
|
180
|
+
* so a reader never sorts it and two calls with the same rows agree.
|
|
181
|
+
*/
|
|
182
|
+
export const evaluateSignals = (summary) => {
|
|
183
|
+
if (summary === null) return [];
|
|
184
|
+
const fired = [];
|
|
185
|
+
for (const rule of RULES) {
|
|
186
|
+
if (!rule.fired(summary)) continue;
|
|
187
|
+
fired.push({
|
|
188
|
+
signal: rule.signal,
|
|
189
|
+
severity: rule.severity(summary),
|
|
190
|
+
value: rule.value(summary),
|
|
191
|
+
rule: SIGNAL_RULES[rule.signal],
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
return fired.sort(compareSignals);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/** The rollup row of one task, or `null` when it has no attributed action. */
|
|
198
|
+
export const readActionSummary = (db, slug) =>
|
|
199
|
+
summaryOf(db.prepare('SELECT * FROM task_action_summary WHERE task_slug = ?1').get(slug));
|
|
200
|
+
|
|
201
|
+
/** The raw view row, for a caller that reports the view itself as evidence. */
|
|
202
|
+
export const readActionSummaryRow = (db, slug) =>
|
|
203
|
+
db.prepare('SELECT * FROM task_action_summary WHERE task_slug = ?1').get(slug) ?? null;
|
|
204
|
+
|
|
205
|
+
/** The non-zero signal numerators the view carries for one task. */
|
|
206
|
+
export const readSignalCandidates = (db, slug) =>
|
|
207
|
+
db.prepare('SELECT signal, value FROM action_signals WHERE task_slug = ?1 ORDER BY signal ASC').all(slug);
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The attribution coverage of one task: the per-rung counts, the mapped share and
|
|
211
|
+
* an explicit "counts are a lower bound" flag. A task with no rows reports a null
|
|
212
|
+
* coverage rather than a confident `0`.
|
|
213
|
+
*/
|
|
214
|
+
export const coverageOf = (summary) => {
|
|
215
|
+
const byRung = summary === null ? { lease: 0, window: 0, 'dir-mtime': 0, unmapped: 0 } : summary.byRung;
|
|
216
|
+
const total = Object.values(byRung).reduce((sum, value) => sum + value, 0);
|
|
217
|
+
const mapped = total - byRung.unmapped;
|
|
218
|
+
return {
|
|
219
|
+
byRung,
|
|
220
|
+
mapped,
|
|
221
|
+
total,
|
|
222
|
+
coverage: total === 0 ? null : mapped / total,
|
|
223
|
+
lowerBound: byRung.unmapped > 0,
|
|
224
|
+
};
|
|
225
|
+
};
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* The per-task action rollup and its signal set, read from `team.db`.
|
|
4
|
+
*
|
|
5
|
+
* The database is a projection: this script reads it and writes nothing. Every
|
|
6
|
+
* figure it prints comes from `task_action_summary` / `action_signals` and is
|
|
7
|
+
* paired with a direct count over the base rows in `--json`, so a consumer reads
|
|
8
|
+
* the JSON and never the prose (the shape `measure.mjs` uses).
|
|
9
|
+
*
|
|
10
|
+
* Usage (any cwd, after `team-db.mjs sync`):
|
|
11
|
+
* node .ai.local/scripts/actions-report.mjs --slug <slug> [--since <instant>] [--json]
|
|
12
|
+
* [--root <team-root>]
|
|
13
|
+
*
|
|
14
|
+
* Exit codes: 0 whenever a report is produced — an unknown slug is an empty
|
|
15
|
+
* report with a null coverage, not an error. 2 when the projection cannot be
|
|
16
|
+
* read (missing, busy, older or newer than this code).
|
|
17
|
+
*/
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import { fileURLToPath } from 'node:url';
|
|
20
|
+
|
|
21
|
+
import { openTeamDb, TeamDbError, DEFAULT_TEAM_ROOT } from './team-db-schema.mjs';
|
|
22
|
+
import {
|
|
23
|
+
ACTION_INTENTS,
|
|
24
|
+
ATTRIBUTION_RUNGS,
|
|
25
|
+
coverageOf,
|
|
26
|
+
evaluateSignals,
|
|
27
|
+
readActionSummary,
|
|
28
|
+
readActionSummaryRow,
|
|
29
|
+
readSignalCandidates,
|
|
30
|
+
summaryOf,
|
|
31
|
+
} from './action-signals.mjs';
|
|
32
|
+
|
|
33
|
+
const USAGE =
|
|
34
|
+
'usage: actions-report.mjs --slug <slug> [--since <instant> (recorded, does not filter)] [--json] [--root <team-root>]';
|
|
35
|
+
|
|
36
|
+
const parseArgs = (argv) => {
|
|
37
|
+
const options = { slug: null, since: null, json: false, root: null };
|
|
38
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
39
|
+
const arg = argv[index];
|
|
40
|
+
if (arg === '--json') options.json = true;
|
|
41
|
+
else if (arg === '--slug') options.slug = argv[++index] ?? null;
|
|
42
|
+
else if (arg === '--since') options.since = argv[++index] ?? null;
|
|
43
|
+
else if (arg === '--root') options.root = argv[++index] ?? null;
|
|
44
|
+
else if (arg === '--help' || arg === '-h') {
|
|
45
|
+
process.stdout.write(`${USAGE}\n`);
|
|
46
|
+
process.exit(0);
|
|
47
|
+
} else {
|
|
48
|
+
process.stderr.write(`actions-report: unknown argument ${arg}\n${USAGE}\n`);
|
|
49
|
+
process.exit(0);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return options;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const percent = (value) => (value === null ? '—' : `${Math.round(value * 100)}%`);
|
|
56
|
+
|
|
57
|
+
const number = (value) => (value === null || value === undefined ? '—' : String(value));
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A direct count over the base rows: the evidence beside the view's own figure.
|
|
61
|
+
*
|
|
62
|
+
* `--since` is recorded on the report but never filters these counts: the rollup
|
|
63
|
+
* is a per-task view, and a view cannot take a bound parameter, so filtering one
|
|
64
|
+
* side would compare a window against an all-time figure.
|
|
65
|
+
*/
|
|
66
|
+
const evidenceOf = (db, slug) => {
|
|
67
|
+
const calls = db.prepare('SELECT count(*) AS n FROM tool_calls WHERE task_slug = ?1').get(slug);
|
|
68
|
+
const turns = db.prepare('SELECT count(*) AS n FROM usage_turns WHERE task_slug = ?1').get(slug);
|
|
69
|
+
const changed = db
|
|
70
|
+
.prepare(
|
|
71
|
+
`SELECT count(DISTINCT target_key) AS n FROM tool_calls
|
|
72
|
+
WHERE task_slug = ?1 AND intent = 'mutate' AND target_key IS NOT NULL`,
|
|
73
|
+
)
|
|
74
|
+
.get(slug);
|
|
75
|
+
const rungs = {};
|
|
76
|
+
for (const rung of ATTRIBUTION_RUNGS) {
|
|
77
|
+
rungs[rung] = Number(
|
|
78
|
+
db
|
|
79
|
+
.prepare('SELECT count(*) AS n FROM tool_calls WHERE task_slug = ?1 AND attribution = ?2')
|
|
80
|
+
.get(slug, rung).n,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
actions: Number(calls.n),
|
|
85
|
+
turns: Number(turns.n),
|
|
86
|
+
changedFiles: Number(changed.n),
|
|
87
|
+
byRung: rungs,
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const figure = (id, reported, data, extra = {}) => ({
|
|
92
|
+
id,
|
|
93
|
+
reported,
|
|
94
|
+
data,
|
|
95
|
+
reproduced: reported === data,
|
|
96
|
+
...extra,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const buildReport = ({ db, slug, since }) => {
|
|
100
|
+
const view = readActionSummaryRow(db, slug);
|
|
101
|
+
const summary = summaryOf(view);
|
|
102
|
+
const evidence = evidenceOf(db, slug);
|
|
103
|
+
const signals = evaluateSignals(summary);
|
|
104
|
+
const candidates = new Map(readSignalCandidates(db, slug).map((row) => [row.signal, Number(row.value)]));
|
|
105
|
+
const coverage = coverageOf(summary);
|
|
106
|
+
|
|
107
|
+
const figures = [
|
|
108
|
+
figure('actions', summary === null ? 0 : summary.actions, evidence.actions),
|
|
109
|
+
figure('turns', summary === null ? 0 : summary.turns, evidence.turns),
|
|
110
|
+
figure('changedFiles', summary === null ? 0 : summary.changedFiles, evidence.changedFiles),
|
|
111
|
+
...ATTRIBUTION_RUNGS.map((rung) =>
|
|
112
|
+
figure(`attribution.${rung}`, coverage.byRung[rung] ?? 0, evidence.byRung[rung]),
|
|
113
|
+
),
|
|
114
|
+
{
|
|
115
|
+
id: 'signals',
|
|
116
|
+
reported: signals.map((entry) => entry.signal),
|
|
117
|
+
data: [...candidates.keys()].sort(),
|
|
118
|
+
// Every fired signal has a non-zero numerator in the view; the view may
|
|
119
|
+
// carry a numerator this task's thresholds do not fire on.
|
|
120
|
+
reproduced: signals.every((entry) => candidates.has(entry.signal)),
|
|
121
|
+
rule: 'every fired signal carries a non-zero numerator in action_signals',
|
|
122
|
+
},
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
slug,
|
|
127
|
+
since,
|
|
128
|
+
window: { from: summary?.firstAt ?? null, to: summary?.lastAt ?? null },
|
|
129
|
+
coverage,
|
|
130
|
+
summary,
|
|
131
|
+
signals,
|
|
132
|
+
figures,
|
|
133
|
+
reproduced: figures.every((entry) => entry.reproduced),
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const printProse = (report) => {
|
|
138
|
+
const { slug, summary, coverage, signals, window } = report;
|
|
139
|
+
const lines = [`agent actions — ${slug}`];
|
|
140
|
+
lines.push(`window: ${number(window.from)} – ${number(window.to)}`);
|
|
141
|
+
if (summary === null) {
|
|
142
|
+
lines.push('no attributed action in this projection');
|
|
143
|
+
lines.push(`attribution: coverage — (0 rows); run team-db.mjs sync, then AI_RULES_ACTIONS is not 0`);
|
|
144
|
+
return lines.join('\n');
|
|
145
|
+
}
|
|
146
|
+
lines.push(
|
|
147
|
+
`actions ${summary.actions} turns ${summary.turns} changed files ${summary.changedFiles} tokens ${summary.tokens}`,
|
|
148
|
+
);
|
|
149
|
+
lines.push(
|
|
150
|
+
`by intent: ${ACTION_INTENTS.map((intent) => `${intent} ${summary.byIntent[intent]}`).join(' · ')}`,
|
|
151
|
+
);
|
|
152
|
+
lines.push(
|
|
153
|
+
`error rate ${percent(summary.errorRate)} (${summary.errorActions}/${summary.actions}) ` +
|
|
154
|
+
`repeat ratio ${percent(summary.repeatRatio)} (${summary.repeatActions}/${summary.actions}) ` +
|
|
155
|
+
`guard hits ${summary.guardHits}`,
|
|
156
|
+
);
|
|
157
|
+
lines.push(
|
|
158
|
+
`actions per changed file ${summary.actionsPerChangedFile === null ? '— (no mutated file)' : summary.actionsPerChangedFile.toFixed(1)} ` +
|
|
159
|
+
`tokens per action ${summary.tokensPerAction === null ? '—' : Math.round(summary.tokensPerAction)}`,
|
|
160
|
+
);
|
|
161
|
+
lines.push(
|
|
162
|
+
`wall ${summary.wallMs === null ? '—' : `${Math.round(summary.wallMs / 1000)}s`} ` +
|
|
163
|
+
`busy ${summary.measuredActions === 0 ? '— (no measured duration)' : `${Math.round(summary.busyMs / 1000)}s`}`,
|
|
164
|
+
);
|
|
165
|
+
const rungs = ATTRIBUTION_RUNGS.map((rung) => {
|
|
166
|
+
const share = coverage.total === 0 ? null : (coverage.byRung[rung] ?? 0) / coverage.total;
|
|
167
|
+
return `${rung} ${percent(share)}`;
|
|
168
|
+
});
|
|
169
|
+
lines.push(
|
|
170
|
+
`attribution: ${rungs.join(' · ')} mapped ${coverage.mapped}/${coverage.total} (${percent(coverage.coverage)})`,
|
|
171
|
+
);
|
|
172
|
+
if (coverage.lowerBound) lines.push('counts are a lower bound: some rows are not attributable');
|
|
173
|
+
if (signals.length === 0) lines.push('signals: none above its threshold');
|
|
174
|
+
else {
|
|
175
|
+
lines.push('signals:');
|
|
176
|
+
for (const entry of signals) lines.push(` ${entry.severity} ${entry.signal} = ${entry.value} (${entry.rule})`);
|
|
177
|
+
}
|
|
178
|
+
return lines.join('\n');
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const main = () => {
|
|
182
|
+
const options = parseArgs(process.argv.slice(2));
|
|
183
|
+
if (options.slug === null || options.slug.trim() === '') {
|
|
184
|
+
process.stderr.write(`actions-report: --slug is required\n${USAGE}\n`);
|
|
185
|
+
process.exit(0);
|
|
186
|
+
}
|
|
187
|
+
const teamRoot = options.root === null ? DEFAULT_TEAM_ROOT : path.resolve(options.root);
|
|
188
|
+
let handle;
|
|
189
|
+
try {
|
|
190
|
+
handle = openTeamDb({ teamRoot, readOnly: true });
|
|
191
|
+
} catch (error) {
|
|
192
|
+
const code = error instanceof TeamDbError ? error.code : 'E_DB_OPEN';
|
|
193
|
+
process.stderr.write(`actions-report: ${code}: ${error.message}\n`);
|
|
194
|
+
process.exit(2);
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
const report = buildReport({ db: handle.db, slug: options.slug, since: options.since });
|
|
198
|
+
process.stdout.write(`${options.json ? JSON.stringify(report, null, 2) : printProse(report)}\n`);
|
|
199
|
+
} finally {
|
|
200
|
+
try {
|
|
201
|
+
handle.db.close();
|
|
202
|
+
} catch {
|
|
203
|
+
/* the report is already printed */
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
if (process.argv[1] !== undefined && path.resolve(process.argv[1]) === path.resolve(fileURLToPath(import.meta.url))) {
|
|
209
|
+
main();
|
|
210
|
+
}
|
|
@@ -1360,11 +1360,12 @@ const doctorVerb = (flags) => {
|
|
|
1360
1360
|
|
|
1361
1361
|
/**
|
|
1362
1362
|
* The command-line half of the ownership proof compares the recorded command to
|
|
1363
|
-
* the live one.
|
|
1364
|
-
*
|
|
1365
|
-
* The recorded command must still be a substantive
|
|
1363
|
+
* the live one. Quotes and a leading `exec` are shell syntax — `start --command
|
|
1364
|
+
* 'exec node script.js'` reaches the child as `node script.js` — so they are
|
|
1365
|
+
* dropped on both sides. The recorded command must still be a substantive
|
|
1366
|
+
* substring of the live one.
|
|
1366
1367
|
*/
|
|
1367
|
-
const commandForProof = (value) => normalize(value).replace(/["']/g, '');
|
|
1368
|
+
const commandForProof = (value) => normalize(value).replace(/["']/g, '').replace(/^(?:exec\s+)+/, '');
|
|
1368
1369
|
|
|
1369
1370
|
const proveOwnership = (record) => {
|
|
1370
1371
|
const owner = isRecord(record?.owner) ? record.owner : null;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Start the team-db scheduler, or restart it when one already holds the lease.
|
|
3
|
+
*
|
|
4
|
+
* The manager lease (`processes.mjs`) and `scheduler.lock` are both honored:
|
|
5
|
+
* a recorded manager process is stopped through `processes.mjs stop`, and a
|
|
6
|
+
* scheduler started outside the manager is signalled only when its command
|
|
7
|
+
* line is still `team-db.mjs schedule`. A lock whose pid is already dead is
|
|
8
|
+
* removed so a crash does not block the next start for `LEASE_STALE_MS`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { spawnSync } from 'node:child_process';
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { fileURLToPath } from 'node:url';
|
|
15
|
+
|
|
16
|
+
import { readLease, schedulerPaths } from './team-db-scheduler.mjs';
|
|
17
|
+
|
|
18
|
+
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
|
19
|
+
const teamRoot = path.join(repoRoot, '.ai.local', 'team');
|
|
20
|
+
const processesBin = path.join(repoRoot, '.ai.local', 'scripts', 'processes.mjs');
|
|
21
|
+
const schedulerCommand = 'exec node .ai.local/scripts/team-db.mjs schedule';
|
|
22
|
+
const termWaitMs = 8000;
|
|
23
|
+
const killWaitMs = 3000;
|
|
24
|
+
const onlineWaitMs = 5000;
|
|
25
|
+
|
|
26
|
+
const sleep = (ms) => new Promise((resolve) => {
|
|
27
|
+
setTimeout(resolve, ms);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const pidAlive = (pid) => {
|
|
31
|
+
if (!Number.isInteger(pid) || pid <= 1) return false;
|
|
32
|
+
try {
|
|
33
|
+
process.kill(pid, 0);
|
|
34
|
+
return true;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
return error.code === 'EPERM';
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const commandOf = (pid) => {
|
|
41
|
+
const result = spawnSync('ps', ['-p', String(pid), '-o', 'command='], { encoding: 'utf8' });
|
|
42
|
+
if (result.error || result.status !== 0) return '';
|
|
43
|
+
return String(result.stdout ?? '').trim();
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const isSchedulerProcess = (command) => /team-db\.mjs\s+schedule(?:\s|$)/.test(command);
|
|
47
|
+
|
|
48
|
+
const managedPid = () => {
|
|
49
|
+
try {
|
|
50
|
+
const record = JSON.parse(
|
|
51
|
+
fs.readFileSync(path.join(repoRoot, '.ai.local', '.processes', 'leases', 'scheduler.json'), 'utf8'),
|
|
52
|
+
);
|
|
53
|
+
const pid = Number(record?.owner?.pid);
|
|
54
|
+
return pidAlive(pid) ? pid : null;
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const runProcesses = (args) => {
|
|
61
|
+
const result = spawnSync(process.execPath, [processesBin, ...args], {
|
|
62
|
+
cwd: repoRoot,
|
|
63
|
+
encoding: 'utf8',
|
|
64
|
+
});
|
|
65
|
+
if (result.stdout) process.stdout.write(result.stdout);
|
|
66
|
+
if (result.stderr) process.stderr.write(result.stderr);
|
|
67
|
+
return result.status ?? 1;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const waitUntil = async (predicate, timeoutMs) => {
|
|
71
|
+
const deadline = Date.now() + timeoutMs;
|
|
72
|
+
while (Date.now() < deadline) {
|
|
73
|
+
if (predicate()) return true;
|
|
74
|
+
await sleep(200);
|
|
75
|
+
}
|
|
76
|
+
return predicate();
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const removeDeadLock = () => {
|
|
80
|
+
const current = readLease(teamRoot);
|
|
81
|
+
if (current.state === 'stopped') return;
|
|
82
|
+
const pid = Number(current.lease?.pid);
|
|
83
|
+
if (pidAlive(pid)) return;
|
|
84
|
+
fs.rmSync(schedulerPaths(teamRoot).lock, { force: true });
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const stopLockHolder = async () => {
|
|
88
|
+
const current = readLease(teamRoot);
|
|
89
|
+
if (current.state !== 'running') {
|
|
90
|
+
removeDeadLock();
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
const pid = Number(current.lease?.pid);
|
|
94
|
+
const command = commandOf(pid);
|
|
95
|
+
if (!pidAlive(pid)) {
|
|
96
|
+
removeDeadLock();
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
if (!isSchedulerProcess(command)) {
|
|
100
|
+
process.stderr.write(`error: scheduler.lock pid ${pid} is not team-db schedule\n`);
|
|
101
|
+
process.exit(4);
|
|
102
|
+
}
|
|
103
|
+
process.kill(pid, 'SIGTERM');
|
|
104
|
+
const exited = await waitUntil(() => !pidAlive(pid), termWaitMs);
|
|
105
|
+
if (!exited && pidAlive(pid)) {
|
|
106
|
+
process.kill(pid, 'SIGKILL');
|
|
107
|
+
await waitUntil(() => !pidAlive(pid), killWaitMs);
|
|
108
|
+
}
|
|
109
|
+
if (pidAlive(pid)) {
|
|
110
|
+
process.stderr.write(`error: scheduler pid ${pid} survived SIGKILL\n`);
|
|
111
|
+
process.exit(3);
|
|
112
|
+
}
|
|
113
|
+
removeDeadLock();
|
|
114
|
+
const after = readLease(teamRoot);
|
|
115
|
+
if (after.state === 'running' && pidAlive(Number(after.lease?.pid))) {
|
|
116
|
+
process.stderr.write('error: a scheduler still holds the lease\n');
|
|
117
|
+
process.exit(4);
|
|
118
|
+
}
|
|
119
|
+
return pid;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const tailLog = () => {
|
|
123
|
+
const file = path.join(repoRoot, '.ai.local', '.processes', 'logs', 'scheduler.log');
|
|
124
|
+
try {
|
|
125
|
+
return fs.readFileSync(file, 'utf8').split('\n').filter(Boolean).slice(-20).join('\n');
|
|
126
|
+
} catch {
|
|
127
|
+
return '';
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const main = async () => {
|
|
132
|
+
const recordedPid = managedPid();
|
|
133
|
+
const managed = runProcesses(['stop', '--name', 'scheduler']);
|
|
134
|
+
if (managed === 4) process.exit(4);
|
|
135
|
+
if (managed !== 0) process.exit(managed);
|
|
136
|
+
const stoppedPid = (await stopLockHolder()) ?? recordedPid;
|
|
137
|
+
const started = runProcesses([
|
|
138
|
+
'start',
|
|
139
|
+
'--name',
|
|
140
|
+
'scheduler',
|
|
141
|
+
'--command',
|
|
142
|
+
schedulerCommand,
|
|
143
|
+
]);
|
|
144
|
+
if (started !== 0) process.exit(started);
|
|
145
|
+
const online = await waitUntil(() => {
|
|
146
|
+
const lease = readLease(teamRoot);
|
|
147
|
+
return lease.state === 'running' && pidAlive(Number(lease.lease?.pid));
|
|
148
|
+
}, onlineWaitMs);
|
|
149
|
+
if (!online) {
|
|
150
|
+
const tail = tailLog();
|
|
151
|
+
process.stderr.write('error: scheduler did not take a fresh lease\n');
|
|
152
|
+
if (tail !== '') process.stderr.write(`${tail}\n`);
|
|
153
|
+
process.exit(3);
|
|
154
|
+
}
|
|
155
|
+
const pid = Number(readLease(teamRoot).lease?.pid);
|
|
156
|
+
const verb = stoppedPid === null ? 'started' : 'restarted';
|
|
157
|
+
const previous = stoppedPid === null ? '' : ` (stopped ${stoppedPid})`;
|
|
158
|
+
process.stdout.write(`scheduler: ${verb} pid ${pid}${previous}\n`);
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
await main();
|