@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.
Files changed (216) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +1 -1
  3. package/bin/lib/engine-scripts.js +8 -0
  4. package/docs/TEAM-DB.md +86 -5
  5. package/docs/openapi/team-db.v1.json +1164 -224
  6. package/package.json +1 -1
  7. package/presets/_shared/ai-local/scripts/action-signals.mjs +225 -0
  8. package/presets/_shared/ai-local/scripts/actions-report.mjs +210 -0
  9. package/presets/_shared/ai-local/scripts/processes.mjs +5 -4
  10. package/presets/_shared/ai-local/scripts/rr-scheduler-start.mjs +161 -0
  11. package/presets/_shared/ai-local/scripts/team-db-api.mjs +347 -9
  12. package/presets/_shared/ai-local/scripts/team-db-openapi.mjs +309 -4
  13. package/presets/_shared/ai-local/scripts/team-db-project.mjs +239 -25
  14. package/presets/_shared/ai-local/scripts/team-db-schema.mjs +86 -1
  15. package/presets/_shared/hooks/chain-team-phases.sh.tmpl +15 -1
  16. package/presets/claude/android-kotlin/commands/README.md +4 -0
  17. package/presets/claude/android-kotlin/commands/rr-scheduler-start.md +32 -0
  18. package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +15 -1
  19. package/presets/claude/go/commands/README.md +4 -0
  20. package/presets/claude/go/commands/rr-scheduler-start.md +32 -0
  21. package/presets/claude/go/hooks/chain-team-phases.sh +15 -1
  22. package/presets/claude/ios-swift/commands/README.md +4 -0
  23. package/presets/claude/ios-swift/commands/rr-scheduler-start.md +32 -0
  24. package/presets/claude/ios-swift/hooks/chain-team-phases.sh +15 -1
  25. package/presets/claude/java/commands/README.md +4 -0
  26. package/presets/claude/java/commands/rr-scheduler-start.md +32 -0
  27. package/presets/claude/java/hooks/chain-team-phases.sh +15 -1
  28. package/presets/claude/mcp-ts/commands/README.md +4 -0
  29. package/presets/claude/mcp-ts/commands/rr-scheduler-start.md +32 -0
  30. package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +15 -1
  31. package/presets/claude/mol/commands/README.md +4 -0
  32. package/presets/claude/mol/commands/rr-scheduler-start.md +32 -0
  33. package/presets/claude/mol/hooks/chain-team-phases.sh +15 -1
  34. package/presets/claude/next/commands/README.md +4 -0
  35. package/presets/claude/next/commands/rr-scheduler-start.md +32 -0
  36. package/presets/claude/next/hooks/chain-team-phases.sh +15 -1
  37. package/presets/claude/nuxt/commands/README.md +4 -0
  38. package/presets/claude/nuxt/commands/rr-scheduler-start.md +32 -0
  39. package/presets/claude/nuxt/hooks/chain-team-phases.sh +15 -1
  40. package/presets/claude/php-hexagonal/commands/README.md +4 -0
  41. package/presets/claude/php-hexagonal/commands/rr-scheduler-start.md +32 -0
  42. package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +15 -1
  43. package/presets/claude/php-laravel/commands/README.md +4 -0
  44. package/presets/claude/php-laravel/commands/rr-scheduler-start.md +32 -0
  45. package/presets/claude/php-laravel/hooks/chain-team-phases.sh +15 -1
  46. package/presets/claude/svelte/commands/README.md +4 -0
  47. package/presets/claude/svelte/commands/rr-scheduler-start.md +32 -0
  48. package/presets/claude/svelte/hooks/chain-team-phases.sh +15 -1
  49. package/presets/cursor/android-kotlin/commands/README.md +4 -0
  50. package/presets/cursor/android-kotlin/commands/rr-scheduler-start.md +32 -0
  51. package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +15 -1
  52. package/presets/cursor/go/commands/README.md +4 -0
  53. package/presets/cursor/go/commands/rr-scheduler-start.md +32 -0
  54. package/presets/cursor/go/hooks/chain-team-phases.sh +15 -1
  55. package/presets/cursor/ios-swift/commands/README.md +4 -0
  56. package/presets/cursor/ios-swift/commands/rr-scheduler-start.md +32 -0
  57. package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +15 -1
  58. package/presets/cursor/java/commands/README.md +4 -0
  59. package/presets/cursor/java/commands/rr-scheduler-start.md +32 -0
  60. package/presets/cursor/java/hooks/chain-team-phases.sh +15 -1
  61. package/presets/cursor/mcp-ts/commands/README.md +4 -0
  62. package/presets/cursor/mcp-ts/commands/rr-scheduler-start.md +32 -0
  63. package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +15 -1
  64. package/presets/cursor/mol/commands/README.md +4 -0
  65. package/presets/cursor/mol/commands/rr-scheduler-start.md +32 -0
  66. package/presets/cursor/mol/hooks/chain-team-phases.sh +15 -1
  67. package/presets/cursor/next/commands/README.md +4 -0
  68. package/presets/cursor/next/commands/rr-scheduler-start.md +32 -0
  69. package/presets/cursor/next/hooks/chain-team-phases.sh +15 -1
  70. package/presets/cursor/nuxt/commands/README.md +4 -0
  71. package/presets/cursor/nuxt/commands/rr-scheduler-start.md +32 -0
  72. package/presets/cursor/nuxt/hooks/chain-team-phases.sh +15 -1
  73. package/presets/cursor/php-hexagonal/commands/README.md +4 -0
  74. package/presets/cursor/php-hexagonal/commands/rr-scheduler-start.md +32 -0
  75. package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +15 -1
  76. package/presets/cursor/php-laravel/commands/README.md +4 -0
  77. package/presets/cursor/php-laravel/commands/rr-scheduler-start.md +32 -0
  78. package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +15 -1
  79. package/presets/cursor/svelte/commands/README.md +4 -0
  80. package/presets/cursor/svelte/commands/rr-scheduler-start.md +32 -0
  81. package/presets/cursor/svelte/hooks/chain-team-phases.sh +15 -1
  82. package/presets/mavis/android-kotlin/adapter-map.json +9 -1
  83. package/presets/mavis/android-kotlin/commands/README.md +4 -0
  84. package/presets/mavis/android-kotlin/commands/rr-scheduler-start.md +27 -0
  85. package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +15 -1
  86. package/presets/mavis/go/adapter-map.json +9 -1
  87. package/presets/mavis/go/commands/README.md +4 -0
  88. package/presets/mavis/go/commands/rr-scheduler-start.md +27 -0
  89. package/presets/mavis/go/hooks/chain-team-phases.sh +15 -1
  90. package/presets/mavis/ios-swift/adapter-map.json +9 -1
  91. package/presets/mavis/ios-swift/commands/README.md +4 -0
  92. package/presets/mavis/ios-swift/commands/rr-scheduler-start.md +27 -0
  93. package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +15 -1
  94. package/presets/mavis/java/adapter-map.json +9 -1
  95. package/presets/mavis/java/commands/README.md +4 -0
  96. package/presets/mavis/java/commands/rr-scheduler-start.md +27 -0
  97. package/presets/mavis/java/hooks/chain-team-phases.sh +15 -1
  98. package/presets/mavis/mcp-ts/adapter-map.json +9 -1
  99. package/presets/mavis/mcp-ts/commands/README.md +4 -0
  100. package/presets/mavis/mcp-ts/commands/rr-scheduler-start.md +27 -0
  101. package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +15 -1
  102. package/presets/mavis/mol/adapter-map.json +9 -1
  103. package/presets/mavis/mol/commands/README.md +4 -0
  104. package/presets/mavis/mol/commands/rr-scheduler-start.md +27 -0
  105. package/presets/mavis/mol/hooks/chain-team-phases.sh +15 -1
  106. package/presets/mavis/next/adapter-map.json +9 -1
  107. package/presets/mavis/next/commands/README.md +4 -0
  108. package/presets/mavis/next/commands/rr-scheduler-start.md +27 -0
  109. package/presets/mavis/next/hooks/chain-team-phases.sh +15 -1
  110. package/presets/mavis/nuxt/adapter-map.json +9 -1
  111. package/presets/mavis/nuxt/commands/README.md +4 -0
  112. package/presets/mavis/nuxt/commands/rr-scheduler-start.md +27 -0
  113. package/presets/mavis/nuxt/hooks/chain-team-phases.sh +15 -1
  114. package/presets/mavis/php-hexagonal/adapter-map.json +9 -1
  115. package/presets/mavis/php-hexagonal/commands/README.md +4 -0
  116. package/presets/mavis/php-hexagonal/commands/rr-scheduler-start.md +27 -0
  117. package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +15 -1
  118. package/presets/mavis/php-laravel/adapter-map.json +9 -1
  119. package/presets/mavis/php-laravel/commands/README.md +4 -0
  120. package/presets/mavis/php-laravel/commands/rr-scheduler-start.md +27 -0
  121. package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +15 -1
  122. package/presets/mavis/svelte/adapter-map.json +9 -1
  123. package/presets/mavis/svelte/commands/README.md +4 -0
  124. package/presets/mavis/svelte/commands/rr-scheduler-start.md +27 -0
  125. package/presets/mavis/svelte/hooks/chain-team-phases.sh +15 -1
  126. package/presets/opencode/android-kotlin/adapter-map.json +9 -1
  127. package/presets/opencode/android-kotlin/commands/README.md +4 -0
  128. package/presets/opencode/android-kotlin/commands/rr-scheduler-start.md +35 -0
  129. package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +15 -1
  130. package/presets/opencode/go/adapter-map.json +9 -1
  131. package/presets/opencode/go/commands/README.md +4 -0
  132. package/presets/opencode/go/commands/rr-scheduler-start.md +35 -0
  133. package/presets/opencode/go/hooks/chain-team-phases.sh +15 -1
  134. package/presets/opencode/ios-swift/adapter-map.json +9 -1
  135. package/presets/opencode/ios-swift/commands/README.md +4 -0
  136. package/presets/opencode/ios-swift/commands/rr-scheduler-start.md +35 -0
  137. package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +15 -1
  138. package/presets/opencode/java/adapter-map.json +9 -1
  139. package/presets/opencode/java/commands/README.md +4 -0
  140. package/presets/opencode/java/commands/rr-scheduler-start.md +35 -0
  141. package/presets/opencode/java/hooks/chain-team-phases.sh +15 -1
  142. package/presets/opencode/mcp-ts/adapter-map.json +9 -1
  143. package/presets/opencode/mcp-ts/commands/README.md +4 -0
  144. package/presets/opencode/mcp-ts/commands/rr-scheduler-start.md +35 -0
  145. package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +15 -1
  146. package/presets/opencode/mol/adapter-map.json +9 -1
  147. package/presets/opencode/mol/commands/README.md +4 -0
  148. package/presets/opencode/mol/commands/rr-scheduler-start.md +35 -0
  149. package/presets/opencode/mol/hooks/chain-team-phases.sh +15 -1
  150. package/presets/opencode/next/adapter-map.json +9 -1
  151. package/presets/opencode/next/commands/README.md +4 -0
  152. package/presets/opencode/next/commands/rr-scheduler-start.md +35 -0
  153. package/presets/opencode/next/hooks/chain-team-phases.sh +15 -1
  154. package/presets/opencode/nuxt/adapter-map.json +9 -1
  155. package/presets/opencode/nuxt/commands/README.md +4 -0
  156. package/presets/opencode/nuxt/commands/rr-scheduler-start.md +35 -0
  157. package/presets/opencode/nuxt/hooks/chain-team-phases.sh +15 -1
  158. package/presets/opencode/php-hexagonal/adapter-map.json +9 -1
  159. package/presets/opencode/php-hexagonal/commands/README.md +4 -0
  160. package/presets/opencode/php-hexagonal/commands/rr-scheduler-start.md +35 -0
  161. package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +15 -1
  162. package/presets/opencode/php-laravel/adapter-map.json +9 -1
  163. package/presets/opencode/php-laravel/commands/README.md +4 -0
  164. package/presets/opencode/php-laravel/commands/rr-scheduler-start.md +35 -0
  165. package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +15 -1
  166. package/presets/opencode/plugins/output-guard/README.md +42 -1
  167. package/presets/opencode/plugins/output-guard/actions.ts +201 -0
  168. package/presets/opencode/plugins/output-guard/index.ts +147 -6
  169. package/presets/opencode/svelte/adapter-map.json +9 -1
  170. package/presets/opencode/svelte/commands/README.md +4 -0
  171. package/presets/opencode/svelte/commands/rr-scheduler-start.md +35 -0
  172. package/presets/opencode/svelte/hooks/chain-team-phases.sh +15 -1
  173. package/presets/zcode/android-kotlin/adapter-map.json +9 -1
  174. package/presets/zcode/android-kotlin/commands/README.md +4 -0
  175. package/presets/zcode/android-kotlin/commands/rr-scheduler-start.md +35 -0
  176. package/presets/zcode/android-kotlin/hooks/chain-team-phases.sh +15 -1
  177. package/presets/zcode/go/adapter-map.json +9 -1
  178. package/presets/zcode/go/commands/README.md +4 -0
  179. package/presets/zcode/go/commands/rr-scheduler-start.md +35 -0
  180. package/presets/zcode/go/hooks/chain-team-phases.sh +15 -1
  181. package/presets/zcode/ios-swift/adapter-map.json +9 -1
  182. package/presets/zcode/ios-swift/commands/README.md +4 -0
  183. package/presets/zcode/ios-swift/commands/rr-scheduler-start.md +35 -0
  184. package/presets/zcode/ios-swift/hooks/chain-team-phases.sh +15 -1
  185. package/presets/zcode/java/adapter-map.json +9 -1
  186. package/presets/zcode/java/commands/README.md +4 -0
  187. package/presets/zcode/java/commands/rr-scheduler-start.md +35 -0
  188. package/presets/zcode/java/hooks/chain-team-phases.sh +15 -1
  189. package/presets/zcode/mcp-ts/adapter-map.json +9 -1
  190. package/presets/zcode/mcp-ts/commands/README.md +4 -0
  191. package/presets/zcode/mcp-ts/commands/rr-scheduler-start.md +35 -0
  192. package/presets/zcode/mcp-ts/hooks/chain-team-phases.sh +15 -1
  193. package/presets/zcode/mol/adapter-map.json +9 -1
  194. package/presets/zcode/mol/commands/README.md +4 -0
  195. package/presets/zcode/mol/commands/rr-scheduler-start.md +35 -0
  196. package/presets/zcode/mol/hooks/chain-team-phases.sh +15 -1
  197. package/presets/zcode/next/adapter-map.json +9 -1
  198. package/presets/zcode/next/commands/README.md +4 -0
  199. package/presets/zcode/next/commands/rr-scheduler-start.md +35 -0
  200. package/presets/zcode/next/hooks/chain-team-phases.sh +15 -1
  201. package/presets/zcode/nuxt/adapter-map.json +9 -1
  202. package/presets/zcode/nuxt/commands/README.md +4 -0
  203. package/presets/zcode/nuxt/commands/rr-scheduler-start.md +35 -0
  204. package/presets/zcode/nuxt/hooks/chain-team-phases.sh +15 -1
  205. package/presets/zcode/php-hexagonal/adapter-map.json +9 -1
  206. package/presets/zcode/php-hexagonal/commands/README.md +4 -0
  207. package/presets/zcode/php-hexagonal/commands/rr-scheduler-start.md +35 -0
  208. package/presets/zcode/php-hexagonal/hooks/chain-team-phases.sh +15 -1
  209. package/presets/zcode/php-laravel/adapter-map.json +9 -1
  210. package/presets/zcode/php-laravel/commands/README.md +4 -0
  211. package/presets/zcode/php-laravel/commands/rr-scheduler-start.md +35 -0
  212. package/presets/zcode/php-laravel/hooks/chain-team-phases.sh +15 -1
  213. package/presets/zcode/svelte/adapter-map.json +9 -1
  214. package/presets/zcode/svelte/commands/README.md +4 -0
  215. package/presets/zcode/svelte/commands/rr-scheduler-start.md +35 -0
  216. package/presets/zcode/svelte/hooks/chain-team-phases.sh +15 -1
@@ -51,6 +51,13 @@ import {
51
51
  TASK_STATES,
52
52
  serializeOpenApiDocument,
53
53
  } from './team-db-openapi.mjs';
54
+ import {
55
+ ACTION_INTENTS,
56
+ SIGNAL_SEVERITIES,
57
+ compareSignals,
58
+ evaluateSignals,
59
+ summaryOf,
60
+ } from './action-signals.mjs';
54
61
  import {
55
62
  DEFAULT_LOG_LIMIT,
56
63
  JOB_ID_RE,
@@ -69,6 +76,7 @@ import {
69
76
  import {
70
77
  EFFECTIVE_ENTRY_STATUSES,
71
78
  EFFECTIVE_ITEM_STATUSES,
79
+ HOLDING_TASK_STATES,
72
80
  countActiveTasks,
73
81
  deriveTrackerOrdering,
74
82
  effectiveEntryStatus,
@@ -205,6 +213,34 @@ const EVENT_ORDER = `
205
213
  CASE ?5 WHEN 'asc' THEN e.at END ASC NULLS LAST,
206
214
  e.seq ASC`;
207
215
 
216
+ // The window and task cut, shared by both action routes; `/actions` adds the
217
+ // intent/agent clauses it declares, `/signals` does not declare them.
218
+ const ACTION_WINDOW_FILTER = `
219
+ day >= ?1 AND day <= ?2
220
+ AND (?3 = '' OR task_slug = ?3)
221
+ AND (?4 = 1 OR task_slug IS NOT NULL)`;
222
+
223
+ const ACTION_FILTER = `${ACTION_WINDOW_FILTER}
224
+ AND (json_array_length(?5) = 0 OR intent IN (SELECT value FROM json_each(?5)))
225
+ AND (?6 = '' OR agent = ?6)`;
226
+
227
+ const ACTION_TOTALS_SELECT = `SELECT count(*) AS actions,
228
+ COALESCE(SUM(CASE WHEN error_class IS NOT NULL THEN 1 ELSE 0 END), 0) AS errors,
229
+ COALESCE(SUM(CASE WHEN repeat_index IS NOT NULL THEN 1 ELSE 0 END), 0) AS repeats,
230
+ COALESCE(SUM(replaced), 0) AS guard_hits,
231
+ COALESCE(SUM(CASE WHEN duration_ms IS NOT NULL THEN duration_ms ELSE 0 END), 0) AS busy_ms,
232
+ COUNT(DISTINCT CASE WHEN intent = 'mutate' THEN target_key END) AS changed_files,
233
+ MIN(observed_at) AS first_at,
234
+ MAX(observed_at) AS last_at,
235
+ COALESCE(SUM(CASE WHEN attribution = 'lease' THEN 1 ELSE 0 END), 0) AS rung_lease,
236
+ COALESCE(SUM(CASE WHEN attribution = 'window' THEN 1 ELSE 0 END), 0) AS rung_window,
237
+ COALESCE(SUM(CASE WHEN attribution = 'dir-mtime' THEN 1 ELSE 0 END), 0) AS rung_dir_mtime,
238
+ COALESCE(SUM(CASE WHEN attribution = 'unmapped' THEN 1 ELSE 0 END), 0) AS rung_unmapped`;
239
+
240
+ const ACTION_SESSIONS_SELECT = `SELECT count(DISTINCT session_id) AS sessions,
241
+ count(DISTINCT CASE WHEN task_slug IS NOT NULL THEN session_id END) AS mapped_sessions
242
+ FROM tool_calls`;
243
+
208
244
  const SQL = {
209
245
  listTasks: `SELECT
210
246
  t.slug, t.title, t.intent, t.profile, t.spec_mode, t.state, t.phase,
@@ -455,6 +491,33 @@ const SQL = {
455
491
  FROM attempts WHERE task_slug = ?1 GROUP BY step_index ORDER BY step_index ASC`,
456
492
  efficiencyGates: `SELECT start_event_id, duration_ms, outcome FROM gates
457
493
  WHERE task_slug = ?1 ORDER BY started_at ASC, start_event_id ASC`,
494
+ // The action routes aggregate the base rows directly: the per-task views have
495
+ // no window parameter, and a route that answers a `from`/`to` question cannot
496
+ // read an all-time rollup. The views stay the per-task report's source.
497
+ actionTotals: `${ACTION_TOTALS_SELECT} FROM tool_calls WHERE ${ACTION_FILTER}`,
498
+ // The same aggregates over the window+task cut alone, for `/signals`, which
499
+ // declares no intent and no agent.
500
+ actionTotalsWindow: `${ACTION_TOTALS_SELECT} FROM tool_calls WHERE ${ACTION_WINDOW_FILTER}`,
501
+ actionTurns: `SELECT count(*) AS turns,
502
+ COALESCE(SUM(input_tokens + output_tokens + reasoning_tokens), 0) AS tokens
503
+ FROM usage_turns WHERE day >= ?1 AND day <= ?2 AND (?3 = '' OR task_slug = ?3) AND (?4 = 1 OR task_slug IS NOT NULL)`,
504
+ actionSessions: `${ACTION_SESSIONS_SELECT} WHERE ${ACTION_FILTER}`,
505
+ actionSessionsWindow: `${ACTION_SESSIONS_SELECT} WHERE ${ACTION_WINDOW_FILTER}`,
506
+ actionUnmappedTurns: `SELECT count(*) AS n FROM usage_turns
507
+ WHERE day >= ?1 AND day <= ?2 AND (?3 = '' OR task_slug = ?3) AND (?4 = 1 OR task_slug IS NOT NULL)
508
+ AND attribution = 'unmapped'`,
509
+ actionByIntent: `SELECT intent, count(*) AS actions FROM tool_calls WHERE ${ACTION_FILTER}
510
+ GROUP BY intent ORDER BY actions DESC, intent ASC`,
511
+ actionByAgent: `SELECT COALESCE(agent, '') AS agent, count(*) AS actions FROM tool_calls WHERE ${ACTION_FILTER}
512
+ GROUP BY agent ORDER BY actions DESC, agent ASC`,
513
+ countActions: `SELECT count(*) AS total FROM tool_calls WHERE ${ACTION_FILTER}`,
514
+ listActions: `SELECT observed_at, task_slug, call_id, tool, intent, agent, error_class, duration_ms, gap_ms,
515
+ exit_code, repeat_index, attribution
516
+ FROM tool_calls WHERE ${ACTION_FILTER}
517
+ ORDER BY observed_at DESC, call_id DESC LIMIT ?7 OFFSET ?8`,
518
+ listActionSummaries: `SELECT * FROM task_action_summary
519
+ WHERE (?1 = '' OR task_slug = ?1) AND (?2 = '' OR last_at >= ?2) AND (?3 = '' OR last_at <= ?3)
520
+ ORDER BY actions DESC, task_slug ASC`,
458
521
  latestContextSnapshot: 'SELECT max(snapshot_at) AS snapshot_at FROM context_cost_hosts',
459
522
  listContextHosts: `SELECT host, rule, always_on_files, always_on_bytes, estimated_tokens
460
523
  FROM context_cost_hosts WHERE snapshot_at = ?1 ORDER BY host ASC`,
@@ -505,7 +568,8 @@ const SQL = {
505
568
  WHERE item_id = ?1 AND absent_at IS NULL AND parse_error IS NULL ORDER BY created_at, link_id`,
506
569
  listTrackerLinksForTask: `SELECT * FROM tracker_item_tasks
507
570
  WHERE task_slug = ?1 AND absent_at IS NULL AND parse_error IS NULL ORDER BY created_at, link_id`,
508
- listTrackerTaskRows: 'SELECT slug, state, start_at, end_at FROM tasks WHERE absent_at IS NULL',
571
+ listTrackerTaskRows:
572
+ 'SELECT slug, title, state, start_at, end_at, updated_at FROM tasks WHERE absent_at IS NULL',
509
573
  getTrackerTaskRow: 'SELECT state, start_at, end_at FROM tasks WHERE slug = ?1 AND absent_at IS NULL',
510
574
  };
511
575
 
@@ -1492,8 +1556,205 @@ const getEfficiencyContextCost = (db) => {
1492
1556
  };
1493
1557
  };
1494
1558
 
1495
- const listSyncRuns = (db, query) => {
1496
- const limit = parseInteger(query.get('limit'), 'limit', { min: 1, max: MAX_LIMIT, fallback: DEFAULT_LIMIT });
1559
+ /* ---------------------------- action telemetry ---------------------------- */
1560
+
1561
+ const ACTION_INTENT_SET = new Set(ACTION_INTENTS);
1562
+ const ACTION_SEVERITY_SET = new Set(SIGNAL_SEVERITIES);
1563
+ const ACTION_DEFAULT_LIMIT = 50;
1564
+ const ACTION_MAX_LIMIT = 200;
1565
+ const EMPTY_RUNG_SPLIT = { lease: 0, window: 0, 'dir-mtime': 0, unmapped: 0 };
1566
+
1567
+ /**
1568
+ * The filter of `/actions`.
1569
+ *
1570
+ * `task` narrows a window-wide read; `from`/`to` keep the shipped 30-day
1571
+ * default; `intent` is a repeated closed enum and `agent` an exact identifier.
1572
+ */
1573
+ const actionFilter = (query) => {
1574
+ const { from, to, fromDay, toDay } = efficiencyWindow(query);
1575
+ const { task, includeAbsent } = efficiencyFilter(query);
1576
+ const intents = parseCsv(query.get('intent'), 'intent', ACTION_INTENT_SET);
1577
+ const agent = parsePattern(query.get('agent'), 'agent', agentPattern, 64);
1578
+ const limit = parseInteger(query.get('limit'), 'limit', {
1579
+ min: 1,
1580
+ max: ACTION_MAX_LIMIT,
1581
+ fallback: ACTION_DEFAULT_LIMIT,
1582
+ });
1583
+ const offset = parseInteger(query.get('offset'), 'offset', { min: 0, max: MAX_OFFSET, fallback: 0 });
1584
+ return {
1585
+ from,
1586
+ to,
1587
+ task,
1588
+ limit,
1589
+ offset,
1590
+ params: [fromDay, toDay, task, includeAbsent, JSON.stringify(intents), agent],
1591
+ };
1592
+ };
1593
+
1594
+ /**
1595
+ * The filter of `/signals`.
1596
+ *
1597
+ * Narrower than `actionFilter` on purpose: the route declares `task`, `from`,
1598
+ * `to`, `severity`, `limit` and `offset`, so the three parameters that would
1599
+ * otherwise bind half of the `attribution` block are refused by name instead of
1600
+ * silently narrowing one half. `includeAbsent` stays at its default, which keeps
1601
+ * every row the block counts a task row.
1602
+ */
1603
+ const signalsFilter = (query) => {
1604
+ for (const name of ['intent', 'agent', 'includeAbsent']) {
1605
+ if (query.get(name) !== null) {
1606
+ fail('E_VALIDATION', 422, `${name} is not a parameter of this route`, { parameter: name });
1607
+ }
1608
+ }
1609
+ const { from, to, fromDay, toDay } = efficiencyWindow(query);
1610
+ const { task } = efficiencyFilter(query);
1611
+ const severities = parseCsv(query.get('severity'), 'severity', ACTION_SEVERITY_SET);
1612
+ const limit = parseInteger(query.get('limit'), 'limit', {
1613
+ min: 1,
1614
+ max: ACTION_MAX_LIMIT,
1615
+ fallback: ACTION_DEFAULT_LIMIT,
1616
+ });
1617
+ const offset = parseInteger(query.get('offset'), 'offset', { min: 0, max: MAX_OFFSET, fallback: 0 });
1618
+ return { from, to, task, severities, limit, offset, params: [fromDay, toDay, task, 0] };
1619
+ };
1620
+
1621
+ const rungOf = (totalsRow) => ({
1622
+ lease: Number(totalsRow.rung_lease),
1623
+ window: Number(totalsRow.rung_window),
1624
+ 'dir-mtime': Number(totalsRow.rung_dir_mtime),
1625
+ unmapped: Number(totalsRow.rung_unmapped),
1626
+ });
1627
+
1628
+ /**
1629
+ * The `attribution` block, assembled from one filter tuple's three reads.
1630
+ *
1631
+ * Passing the rows rather than the filter keeps every `prepare()` argument a
1632
+ * frozen constant, and it makes the "one block, one filter set" property visible:
1633
+ * a caller cannot hand in a sessions row read through a different cut.
1634
+ * `unmappedTurns` is window+task only — a turn carries neither an intent nor an
1635
+ * agent — and `coverage` is null at a zero denominator, never a confident `0`.
1636
+ */
1637
+ const actionAttribution = (totalsRow, sessions, unmappedTurns) => {
1638
+ const byRung = rungOf(totalsRow);
1639
+ const total = Object.values(byRung).reduce((sum, value) => sum + value, 0);
1640
+ return {
1641
+ sessions: Number(sessions.sessions),
1642
+ mappedSessions: Number(sessions.mapped_sessions),
1643
+ unmappedCalls: byRung.unmapped,
1644
+ unmappedTurns: Number(unmappedTurns.n),
1645
+ byRung,
1646
+ coverage: total === 0 ? null : (total - byRung.unmapped) / total,
1647
+ };
1648
+ };
1649
+
1650
+ const actionRow = (row) => ({
1651
+ at: row.observed_at,
1652
+ task: row.task_slug,
1653
+ tool: row.tool,
1654
+ intent: row.intent ?? 'meta',
1655
+ agent: row.agent ?? null,
1656
+ errorClass: row.error_class ?? null,
1657
+ durationMs: row.duration_ms === null ? null : Number(row.duration_ms),
1658
+ gapMs: row.gap_ms === null ? null : Number(row.gap_ms),
1659
+ exitCode: row.exit_code === null ? null : Number(row.exit_code),
1660
+ repeatIndex: row.repeat_index === null ? null : Number(row.repeat_index),
1661
+ });
1662
+
1663
+ /** The rung split of one page of items (B-6: the chip reads `attribution.byRung`). */
1664
+ const rungSplitOf = (rows) => {
1665
+ const counts = { ...EMPTY_RUNG_SPLIT };
1666
+ for (const row of rows) {
1667
+ if (row.attribution in counts) counts[row.attribution] += 1;
1668
+ }
1669
+ return Object.entries(counts).map(([rung, actions]) => ({ rung, actions }));
1670
+ };
1671
+
1672
+ const getEfficiencyActions = (db, query) => {
1673
+ const filter = actionFilter(query);
1674
+ if (filter.task !== '') requireTask(db, filter.task);
1675
+ const totalsRow = db.prepare(SQL.actionTotals).get(...filter.params);
1676
+ const sessions = db.prepare(SQL.actionSessions).get(...filter.params);
1677
+ // The turns tables carry no intent and no agent, so the block's fifth key
1678
+ // reads the first four parameters of the same tuple.
1679
+ const unmappedTurns = db.prepare(SQL.actionUnmappedTurns).get(
1680
+ filter.params[0],
1681
+ filter.params[1],
1682
+ filter.params[2],
1683
+ filter.params[3],
1684
+ );
1685
+ const turns = db.prepare(SQL.actionTurns).get(
1686
+ filter.params[0],
1687
+ filter.params[1],
1688
+ filter.params[2],
1689
+ filter.params[3],
1690
+ );
1691
+ const actions = Number(totalsRow.actions);
1692
+ const errorActions = Number(totalsRow.errors);
1693
+ const repeatActions = Number(totalsRow.repeats);
1694
+ const firstAt = totalsRow.first_at ?? null;
1695
+ const lastAt = totalsRow.last_at ?? null;
1696
+ const first = firstAt === null ? Number.NaN : Date.parse(firstAt);
1697
+ const last = lastAt === null ? Number.NaN : Date.parse(lastAt);
1698
+ const byIntentRows = db.prepare(SQL.actionByIntent).all(...filter.params);
1699
+ const byAgentRows = db.prepare(SQL.actionByAgent).all(...filter.params);
1700
+ const total = Number(db.prepare(SQL.countActions).get(...filter.params).total);
1701
+ const rows = db.prepare(SQL.listActions).all(...filter.params, filter.limit, filter.offset);
1702
+ return {
1703
+ window: { from: filter.from, to: filter.to },
1704
+ task: filter.task === '' ? null : filter.task,
1705
+ // B-1: `actions` and `turns` are the two sample sizes a reader needs; a
1706
+ // ratio whose sample is below its floor is muted by the consumer.
1707
+ totals: {
1708
+ actions,
1709
+ turns: Number(turns.turns),
1710
+ changedFiles: Number(totalsRow.changed_files),
1711
+ errorRate: actions === 0 ? null : errorActions / actions,
1712
+ repeatRatio: actions === 0 ? null : repeatActions / actions,
1713
+ guardHits: Number(totalsRow.guard_hits),
1714
+ wallMs: Number.isFinite(first) && Number.isFinite(last) ? Math.max(0, last - first) : null,
1715
+ // Only the actions whose duration was observed contribute to `busyMs`.
1716
+ busyMs: Number(totalsRow.busy_ms),
1717
+ },
1718
+ byIntent: byIntentRows.map((row) => ({ intent: row.intent ?? 'meta', actions: Number(row.actions) })),
1719
+ byAgent: byAgentRows.map((row) => ({ agent: row.agent === '' ? null : row.agent, actions: Number(row.actions) })),
1720
+ // B-6: defined as the page's rung split; the chip reads `attribution.byRung`.
1721
+ coverage: rungSplitOf(rows),
1722
+ ...page(rows.map(actionRow), total, filter.limit, filter.offset),
1723
+ attribution: actionAttribution(totalsRow, sessions, unmappedTurns),
1724
+ generatedAt: new Date().toISOString(),
1725
+ };
1726
+ };
1727
+
1728
+ const getEfficiencySignals = (db, query) => {
1729
+ const filter = signalsFilter(query);
1730
+ if (filter.task !== '') requireTask(db, filter.task);
1731
+ // The rollup is per task, so the window selects tasks by their last action:
1732
+ // a task whose last action is older than `from` is not answered for.
1733
+ const rows = db.prepare(SQL.listActionSummaries).all(filter.task, filter.from, filter.to);
1734
+ const fired = [];
1735
+ for (const row of rows) {
1736
+ for (const signal of evaluateSignals(summaryOf(row))) {
1737
+ if (filter.severities.length > 0 && !filter.severities.includes(signal.severity)) continue;
1738
+ fired.push({ task: row.task_slug, ...signal });
1739
+ }
1740
+ }
1741
+ // The array order is the contract: worst severity, then the signal name, then
1742
+ // the task, so a window-wide read is ordered like a single task's.
1743
+ fired.sort((left, right) => compareSignals(left, right) || left.task.localeCompare(right.task));
1744
+ const total = fired.length;
1745
+ const totalsRow = db.prepare(SQL.actionTotalsWindow).get(...filter.params);
1746
+ const sessions = db.prepare(SQL.actionSessionsWindow).get(...filter.params);
1747
+ const unmappedTurns = db.prepare(SQL.actionUnmappedTurns).get(...filter.params);
1748
+ return {
1749
+ window: { from: filter.from, to: filter.to },
1750
+ task: filter.task === '' ? null : filter.task,
1751
+ ...page(fired.slice(filter.offset, filter.offset + filter.limit), total, filter.limit, filter.offset),
1752
+ attribution: actionAttribution(totalsRow, sessions, unmappedTurns),
1753
+ generatedAt: new Date().toISOString(),
1754
+ };
1755
+ };
1756
+
1757
+ const listSyncRuns = (db, query) => { const limit = parseInteger(query.get('limit'), 'limit', { min: 1, max: MAX_LIMIT, fallback: DEFAULT_LIMIT });
1497
1758
  const offset = parseInteger(query.get('offset'), 'offset', { min: 0, max: MAX_OFFSET, fallback: 0 });
1498
1759
  const total = Number(db.prepare(SQL.countSyncRuns).get().total);
1499
1760
  const rows = db.prepare(SQL.listSyncRuns).all(limit, offset);
@@ -2077,7 +2338,13 @@ const readTrackerSnapshotRows = (db) => {
2077
2338
  const links = new Map();
2078
2339
  const taskRows = new Map();
2079
2340
  for (const row of db.prepare(SQL.listTrackerTaskRows).all()) {
2080
- taskRows.set(row.slug, { state: row.state ?? null, startAt: row.start_at ?? null, endAt: row.end_at ?? null });
2341
+ taskRows.set(row.slug, {
2342
+ state: row.state ?? null,
2343
+ title: row.title ?? null,
2344
+ startAt: row.start_at ?? null,
2345
+ endAt: row.end_at ?? null,
2346
+ updatedAt: row.updated_at ?? null,
2347
+ });
2081
2348
  }
2082
2349
  const depsByItem = new Map();
2083
2350
  for (const dep of db.prepare(SQL.listTrackerDeps).all()) {
@@ -2295,9 +2562,12 @@ const trackerItemDetail = (db, id, teamRoot = null) => {
2295
2562
  };
2296
2563
  })
2297
2564
  .sort((left, right) => left.createdAt.localeCompare(right.createdAt) || left.linkId.localeCompare(right.linkId));
2298
- const queue = [...snapshot.entries.values()]
2299
- .filter((entry) => entry.itemId === id)
2300
- .map((entry) => trackerQueueEntryJson(entry, snapshot.taskRows, annotated.item.title));
2565
+ const queue = [
2566
+ ...liveHoldingTaskRows(snapshot).filter((entry) => entry.itemId === id),
2567
+ ...[...snapshot.entries.values()]
2568
+ .filter((entry) => entry.itemId === id)
2569
+ .map((entry) => trackerQueueEntryJson(entry, snapshot.taskRows, annotated.item.title)),
2570
+ ];
2301
2571
  return {
2302
2572
  item: trackerItemJson(annotated),
2303
2573
  dependencies,
@@ -2308,19 +2578,81 @@ const trackerItemDetail = (db, id, teamRoot = null) => {
2308
2578
  };
2309
2579
  };
2310
2580
 
2581
+ /**
2582
+ * A holding task occupies a launch slot even when no tracker item is linked
2583
+ * and no queue file exists. The row is derived: `entryId` stays null so cancel
2584
+ * cannot target it, and a non-terminal queue entry for the same slug wins.
2585
+ */
2586
+ const liveHoldingTaskRows = (snapshot) => {
2587
+ const covered = new Set();
2588
+ for (const entry of snapshot.entries.values()) {
2589
+ const taskSlug = entry.taskSlug ?? `tracker-${entry.itemId}`;
2590
+ const taskState = snapshot.taskRows.get(taskSlug)?.state ?? null;
2591
+ if (!['cancelled', 'expired', 'done'].includes(effectiveEntryStatus(entry, taskState))) {
2592
+ covered.add(taskSlug);
2593
+ }
2594
+ }
2595
+ const linksBySlug = new Map();
2596
+ for (const link of snapshot.links.values()) {
2597
+ if (link.decision === 'unlinked') continue;
2598
+ const list = linksBySlug.get(link.taskSlug) ?? [];
2599
+ list.push(link);
2600
+ linksBySlug.set(link.taskSlug, list);
2601
+ }
2602
+ const rows = [];
2603
+ for (const [taskSlug, taskRow] of snapshot.taskRows) {
2604
+ if (!HOLDING_TASK_STATES.has(taskRow.state)) continue;
2605
+ if (covered.has(taskSlug)) continue;
2606
+ const links = linksBySlug.get(taskSlug) ?? [];
2607
+ links.sort((left, right) => {
2608
+ if (left.role === 'primary' && right.role !== 'primary') return -1;
2609
+ if (right.role === 'primary' && left.role !== 'primary') return 1;
2610
+ return String(left.linkId).localeCompare(String(right.linkId));
2611
+ });
2612
+ const link = links[0] ?? null;
2613
+ let item = link === null ? null : (snapshot.items.get(link.itemId) ?? null);
2614
+ if (item === null && link === null && /^[1-9][0-9]*$/.test(taskSlug)) {
2615
+ item = snapshot.items.get(Number(taskSlug)) ?? null;
2616
+ }
2617
+ rows.push({
2618
+ entryId: null,
2619
+ itemId: link?.itemId ?? item?.id ?? null,
2620
+ taskSlug,
2621
+ linkId: link?.linkId ?? null,
2622
+ status: 'active',
2623
+ reason: 'current task',
2624
+ requestedBy: 'task',
2625
+ command: null,
2626
+ revision: 1,
2627
+ requestedAt: taskRow.startAt ?? taskRow.updatedAt ?? '1970-01-01T00:00:00.000Z',
2628
+ claimedAt: null,
2629
+ finishedAt: null,
2630
+ history: [],
2631
+ itemTitle: item?.title ?? taskRow.title ?? taskSlug,
2632
+ taskState: taskRow.state,
2633
+ taskStartAt: taskRow.startAt ?? null,
2634
+ taskEndAt: taskRow.endAt ?? null,
2635
+ });
2636
+ }
2637
+ rows.sort((left, right) => left.taskSlug.localeCompare(right.taskSlug));
2638
+ return rows;
2639
+ };
2640
+
2311
2641
  const listTrackerQueue = (db, query) => {
2312
2642
  const limit = parseInteger(query.get('limit'), 'limit', { min: 1, max: TRACKER_MAX_LIMIT, fallback: TRACKER_DEFAULT_LIMIT });
2313
2643
  const offset = parseInteger(query.get('offset'), 'offset', { min: 0, max: MAX_OFFSET, fallback: 0 });
2314
2644
  const statuses = parseCsv(query.get('status'), 'status', TRACKER_EFFECTIVE_ENTRY_SET);
2315
2645
  const itemId = query.get('itemId') === null ? null : parseItemId(query.get('itemId'), 'itemId');
2316
2646
  const snapshot = readTrackerSnapshotRows(db);
2317
- let items = [...snapshot.entries.values()]
2647
+ const stored = [...snapshot.entries.values()]
2318
2648
  .filter((entry) => itemId === null || entry.itemId === itemId)
2319
2649
  .map((entry) => trackerQueueEntryJson(entry, snapshot.taskRows, snapshot.items.get(entry.itemId)?.title ?? String(entry.itemId)));
2650
+ const live = liveHoldingTaskRows(snapshot).filter((entry) => itemId === null || entry.itemId === itemId);
2651
+ let items = [...live, ...stored];
2320
2652
  if (statuses.length > 0) items = items.filter((entry) => statuses.includes(entry.status));
2321
2653
  const total = items.length;
2322
2654
  const pageItems = items.slice(offset, offset + limit);
2323
- const activeEntryId = items.find((entry) => entry.status === 'active')?.entryId ?? null;
2655
+ const activeEntryId = items.find((entry) => entry.status === 'active' && entry.entryId !== null)?.entryId ?? null;
2324
2656
  return {
2325
2657
  items: pageItems,
2326
2658
  total,
@@ -2754,6 +3086,12 @@ export const ROUTES = [
2754
3086
  route('GET', '/api/v1/efficiency/context-cost', 'getEfficiencyContextCost', (context) => ({
2755
3087
  json: getEfficiencyContextCost(context.db()),
2756
3088
  })),
3089
+ route('GET', '/api/v1/efficiency/actions', 'getEfficiencyActions', (context) => ({
3090
+ json: getEfficiencyActions(context.db(), context.query),
3091
+ })),
3092
+ route('GET', '/api/v1/efficiency/signals', 'getEfficiencySignals', (context) => ({
3093
+ json: getEfficiencySignals(context.db(), context.query),
3094
+ })),
2757
3095
  route('GET', '/api/v1/sync-runs', 'listSyncRuns', (context) => ({ json: listSyncRuns(context.db(), context.query) })),
2758
3096
  route('POST', '/api/v1/sync', 'triggerSync', async (context) => {
2759
3097
  const text = context.body.toString('utf8').trim();