@memnexus-ai/mx-agent-cli 0.1.294 → 0.1.296-next.0

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 (649) hide show
  1. package/dist/__tests__/agents-md-canonicalization.test.js +31 -1
  2. package/dist/__tests__/agents-md-canonicalization.test.js.map +1 -1
  3. package/dist/__tests__/behavioral-extraction.test.js +50 -0
  4. package/dist/__tests__/behavioral-extraction.test.js.map +1 -1
  5. package/dist/__tests__/bootstrap-behavioral-contract.test.js +6 -3
  6. package/dist/__tests__/bootstrap-behavioral-contract.test.js.map +1 -1
  7. package/dist/__tests__/bundle-config.test.js +28 -2
  8. package/dist/__tests__/bundle-config.test.js.map +1 -1
  9. package/dist/__tests__/catalog-interaction.test.d.ts +25 -0
  10. package/dist/__tests__/catalog-interaction.test.d.ts.map +1 -0
  11. package/dist/__tests__/catalog-interaction.test.js +269 -0
  12. package/dist/__tests__/catalog-interaction.test.js.map +1 -0
  13. package/dist/__tests__/catalog-named-memories.test.d.ts +2 -0
  14. package/dist/__tests__/catalog-named-memories.test.d.ts.map +1 -0
  15. package/dist/__tests__/catalog-named-memories.test.js +204 -0
  16. package/dist/__tests__/catalog-named-memories.test.js.map +1 -0
  17. package/dist/__tests__/catalog-origin-main.test.d.ts +15 -0
  18. package/dist/__tests__/catalog-origin-main.test.d.ts.map +1 -0
  19. package/dist/__tests__/catalog-origin-main.test.js +296 -0
  20. package/dist/__tests__/catalog-origin-main.test.js.map +1 -0
  21. package/dist/__tests__/catalog.test.d.ts +2 -0
  22. package/dist/__tests__/catalog.test.d.ts.map +1 -0
  23. package/dist/__tests__/catalog.test.js +131 -0
  24. package/dist/__tests__/catalog.test.js.map +1 -0
  25. package/dist/__tests__/claude-local-sanitizer.test.d.ts +24 -0
  26. package/dist/__tests__/claude-local-sanitizer.test.d.ts.map +1 -0
  27. package/dist/__tests__/claude-local-sanitizer.test.js +906 -0
  28. package/dist/__tests__/claude-local-sanitizer.test.js.map +1 -0
  29. package/dist/__tests__/cli-build-integration.test.d.ts +2 -0
  30. package/dist/__tests__/cli-build-integration.test.d.ts.map +1 -0
  31. package/dist/__tests__/cli-build-integration.test.js +64 -0
  32. package/dist/__tests__/cli-build-integration.test.js.map +1 -0
  33. package/dist/__tests__/cli-build.test.d.ts +2 -0
  34. package/dist/__tests__/cli-build.test.d.ts.map +1 -0
  35. package/dist/__tests__/cli-build.test.js +640 -0
  36. package/dist/__tests__/cli-build.test.js.map +1 -0
  37. package/dist/__tests__/codex-deploy-config.test.js +60 -0
  38. package/dist/__tests__/codex-deploy-config.test.js.map +1 -1
  39. package/dist/__tests__/completion-rows-discriminate.test.d.ts +20 -0
  40. package/dist/__tests__/completion-rows-discriminate.test.d.ts.map +1 -0
  41. package/dist/__tests__/completion-rows-discriminate.test.js +481 -0
  42. package/dist/__tests__/completion-rows-discriminate.test.js.map +1 -0
  43. package/dist/__tests__/config-fidelity-archived-symlinks.test.d.ts +2 -0
  44. package/dist/__tests__/config-fidelity-archived-symlinks.test.d.ts.map +1 -0
  45. package/dist/__tests__/config-fidelity-archived-symlinks.test.js +127 -0
  46. package/dist/__tests__/config-fidelity-archived-symlinks.test.js.map +1 -0
  47. package/dist/__tests__/config-fidelity-display.test.d.ts +2 -0
  48. package/dist/__tests__/config-fidelity-display.test.d.ts.map +1 -0
  49. package/dist/__tests__/config-fidelity-display.test.js +77 -0
  50. package/dist/__tests__/config-fidelity-display.test.js.map +1 -0
  51. package/dist/__tests__/config-fidelity.test.js +166 -33
  52. package/dist/__tests__/config-fidelity.test.js.map +1 -1
  53. package/dist/__tests__/config-generation-gate.test.js +78 -0
  54. package/dist/__tests__/config-generation-gate.test.js.map +1 -1
  55. package/dist/__tests__/config-sync.test.js +89 -1
  56. package/dist/__tests__/config-sync.test.js.map +1 -1
  57. package/dist/__tests__/coordination-401-production-wiring.test.js +12 -7
  58. package/dist/__tests__/coordination-401-production-wiring.test.js.map +1 -1
  59. package/dist/__tests__/coordination-401-recovery.test.js +39 -32
  60. package/dist/__tests__/coordination-401-recovery.test.js.map +1 -1
  61. package/dist/__tests__/coordination-brief-pin.test.js +21 -8
  62. package/dist/__tests__/coordination-brief-pin.test.js.map +1 -1
  63. package/dist/__tests__/coordination-client.test.js +316 -3
  64. package/dist/__tests__/coordination-client.test.js.map +1 -1
  65. package/dist/__tests__/coordination-credential-endpoint.test.d.ts +24 -0
  66. package/dist/__tests__/coordination-credential-endpoint.test.d.ts.map +1 -0
  67. package/dist/__tests__/coordination-credential-endpoint.test.js +1290 -0
  68. package/dist/__tests__/coordination-credential-endpoint.test.js.map +1 -0
  69. package/dist/__tests__/coordination-memory.test.js +20 -4
  70. package/dist/__tests__/coordination-memory.test.js.map +1 -1
  71. package/dist/__tests__/coordination-provision.test.js +22 -14
  72. package/dist/__tests__/coordination-provision.test.js.map +1 -1
  73. package/dist/__tests__/coordination-status-registration.test.d.ts +16 -0
  74. package/dist/__tests__/coordination-status-registration.test.d.ts.map +1 -0
  75. package/dist/__tests__/coordination-status-registration.test.js +480 -0
  76. package/dist/__tests__/coordination-status-registration.test.js.map +1 -0
  77. package/dist/__tests__/coordination-token-multitenant.test.js +24 -24
  78. package/dist/__tests__/coordination-token-multitenant.test.js.map +1 -1
  79. package/dist/__tests__/coordination-token.test.js +35 -28
  80. package/dist/__tests__/coordination-token.test.js.map +1 -1
  81. package/dist/__tests__/creation-pr-class-check.test.d.ts +17 -0
  82. package/dist/__tests__/creation-pr-class-check.test.d.ts.map +1 -0
  83. package/dist/__tests__/creation-pr-class-check.test.js +346 -0
  84. package/dist/__tests__/creation-pr-class-check.test.js.map +1 -0
  85. package/dist/__tests__/decision-check.test.d.ts +2 -0
  86. package/dist/__tests__/decision-check.test.d.ts.map +1 -0
  87. package/dist/__tests__/decision-check.test.js +105 -0
  88. package/dist/__tests__/decision-check.test.js.map +1 -0
  89. package/dist/__tests__/digest-team-omission.test.d.ts +15 -0
  90. package/dist/__tests__/digest-team-omission.test.d.ts.map +1 -0
  91. package/dist/__tests__/digest-team-omission.test.js +247 -0
  92. package/dist/__tests__/digest-team-omission.test.js.map +1 -0
  93. package/dist/__tests__/doctor-worktree-path.test.d.ts +17 -0
  94. package/dist/__tests__/doctor-worktree-path.test.d.ts.map +1 -0
  95. package/dist/__tests__/doctor-worktree-path.test.js +158 -0
  96. package/dist/__tests__/doctor-worktree-path.test.js.map +1 -0
  97. package/dist/__tests__/fleet-data.test.js +40 -7
  98. package/dist/__tests__/fleet-data.test.js.map +1 -1
  99. package/dist/__tests__/fleet-tmux.test.js +2 -2
  100. package/dist/__tests__/fleet-tmux.test.js.map +1 -1
  101. package/dist/__tests__/generate-claude.test.js +137 -3
  102. package/dist/__tests__/generate-claude.test.js.map +1 -1
  103. package/dist/__tests__/git-env.test.d.ts +14 -0
  104. package/dist/__tests__/git-env.test.d.ts.map +1 -0
  105. package/dist/__tests__/git-env.test.js +131 -0
  106. package/dist/__tests__/git-env.test.js.map +1 -0
  107. package/dist/__tests__/git-identity-isolation.test.d.ts +33 -0
  108. package/dist/__tests__/git-identity-isolation.test.d.ts.map +1 -0
  109. package/dist/__tests__/git-identity-isolation.test.js +626 -0
  110. package/dist/__tests__/git-identity-isolation.test.js.map +1 -0
  111. package/dist/__tests__/harness-extract.test.d.ts +19 -0
  112. package/dist/__tests__/harness-extract.test.d.ts.map +1 -0
  113. package/dist/__tests__/harness-extract.test.js +566 -0
  114. package/dist/__tests__/harness-extract.test.js.map +1 -0
  115. package/dist/__tests__/harness-wiring.test.d.ts +16 -0
  116. package/dist/__tests__/harness-wiring.test.d.ts.map +1 -0
  117. package/dist/__tests__/harness-wiring.test.js +469 -0
  118. package/dist/__tests__/harness-wiring.test.js.map +1 -0
  119. package/dist/__tests__/hook-heal.test.js +17 -0
  120. package/dist/__tests__/hook-heal.test.js.map +1 -1
  121. package/dist/__tests__/init-coordination-url.test.d.ts +11 -0
  122. package/dist/__tests__/init-coordination-url.test.d.ts.map +1 -0
  123. package/dist/__tests__/init-coordination-url.test.js +114 -0
  124. package/dist/__tests__/init-coordination-url.test.js.map +1 -0
  125. package/dist/__tests__/memory-autocreate.test.js +574 -8
  126. package/dist/__tests__/memory-autocreate.test.js.map +1 -1
  127. package/dist/__tests__/memory-log-result.test.d.ts +13 -0
  128. package/dist/__tests__/memory-log-result.test.d.ts.map +1 -0
  129. package/dist/__tests__/memory-log-result.test.js +113 -0
  130. package/dist/__tests__/memory-log-result.test.js.map +1 -0
  131. package/dist/__tests__/memory-named-exists.test.d.ts +16 -0
  132. package/dist/__tests__/memory-named-exists.test.d.ts.map +1 -0
  133. package/dist/__tests__/memory-named-exists.test.js +288 -0
  134. package/dist/__tests__/memory-named-exists.test.js.map +1 -0
  135. package/dist/__tests__/mx-agent-cli-pin-validation.test.js +23 -47
  136. package/dist/__tests__/mx-agent-cli-pin-validation.test.js.map +1 -1
  137. package/dist/__tests__/mx-agent-cli-version-pin.test.js +34 -88
  138. package/dist/__tests__/mx-agent-cli-version-pin.test.js.map +1 -1
  139. package/dist/__tests__/new-project-first-team.test.d.ts +15 -0
  140. package/dist/__tests__/new-project-first-team.test.d.ts.map +1 -0
  141. package/dist/__tests__/new-project-first-team.test.js +263 -0
  142. package/dist/__tests__/new-project-first-team.test.js.map +1 -0
  143. package/dist/__tests__/otel-env.test.d.ts +2 -0
  144. package/dist/__tests__/otel-env.test.d.ts.map +1 -0
  145. package/dist/__tests__/otel-env.test.js +127 -0
  146. package/dist/__tests__/otel-env.test.js.map +1 -0
  147. package/dist/__tests__/project-config.test.js +283 -1
  148. package/dist/__tests__/project-config.test.js.map +1 -1
  149. package/dist/__tests__/provision-behavioral-contract.test.js +96 -4
  150. package/dist/__tests__/provision-behavioral-contract.test.js.map +1 -1
  151. package/dist/__tests__/provision-catalog-precondition.test.d.ts +26 -0
  152. package/dist/__tests__/provision-catalog-precondition.test.d.ts.map +1 -0
  153. package/dist/__tests__/provision-catalog-precondition.test.js +620 -0
  154. package/dist/__tests__/provision-catalog-precondition.test.js.map +1 -0
  155. package/dist/__tests__/provision-completion-rows.test.d.ts +17 -0
  156. package/dist/__tests__/provision-completion-rows.test.d.ts.map +1 -0
  157. package/dist/__tests__/provision-completion-rows.test.js +3187 -0
  158. package/dist/__tests__/provision-completion-rows.test.js.map +1 -0
  159. package/dist/__tests__/provision-identity-gate.test.d.ts +20 -0
  160. package/dist/__tests__/provision-identity-gate.test.d.ts.map +1 -0
  161. package/dist/__tests__/provision-identity-gate.test.js +600 -0
  162. package/dist/__tests__/provision-identity-gate.test.js.map +1 -0
  163. package/dist/__tests__/provision-resume.test.d.ts +26 -0
  164. package/dist/__tests__/provision-resume.test.d.ts.map +1 -0
  165. package/dist/__tests__/provision-resume.test.js +1164 -0
  166. package/dist/__tests__/provision-resume.test.js.map +1 -0
  167. package/dist/__tests__/provision-roleguide-anchors.test.d.ts +27 -0
  168. package/dist/__tests__/provision-roleguide-anchors.test.d.ts.map +1 -0
  169. package/dist/__tests__/provision-roleguide-anchors.test.js +747 -0
  170. package/dist/__tests__/provision-roleguide-anchors.test.js.map +1 -0
  171. package/dist/__tests__/provision-skeleton-fill.test.d.ts +16 -0
  172. package/dist/__tests__/provision-skeleton-fill.test.d.ts.map +1 -0
  173. package/dist/__tests__/provision-skeleton-fill.test.js +488 -0
  174. package/dist/__tests__/provision-skeleton-fill.test.js.map +1 -0
  175. package/dist/__tests__/provision-teambuilder-flow.test.js +93 -7
  176. package/dist/__tests__/provision-teambuilder-flow.test.js.map +1 -1
  177. package/dist/__tests__/repo-guard-wiring.test.d.ts +21 -0
  178. package/dist/__tests__/repo-guard-wiring.test.d.ts.map +1 -0
  179. package/dist/__tests__/repo-guard-wiring.test.js +638 -0
  180. package/dist/__tests__/repo-guard-wiring.test.js.map +1 -0
  181. package/dist/__tests__/report-format.test.d.ts +2 -0
  182. package/dist/__tests__/report-format.test.d.ts.map +1 -0
  183. package/dist/__tests__/report-format.test.js +219 -0
  184. package/dist/__tests__/report-format.test.js.map +1 -0
  185. package/dist/__tests__/row15-detail-sanitizer.test.d.ts +20 -0
  186. package/dist/__tests__/row15-detail-sanitizer.test.d.ts.map +1 -0
  187. package/dist/__tests__/row15-detail-sanitizer.test.js +61 -0
  188. package/dist/__tests__/row15-detail-sanitizer.test.js.map +1 -0
  189. package/dist/__tests__/runtime-codex-gateway-pilot.test.d.ts +2 -0
  190. package/dist/__tests__/runtime-codex-gateway-pilot.test.d.ts.map +1 -0
  191. package/dist/__tests__/runtime-codex-gateway-pilot.test.js +309 -0
  192. package/dist/__tests__/runtime-codex-gateway-pilot.test.js.map +1 -0
  193. package/dist/__tests__/runtime-codex-gateway-provider.test.d.ts +2 -0
  194. package/dist/__tests__/runtime-codex-gateway-provider.test.d.ts.map +1 -0
  195. package/dist/__tests__/runtime-codex-gateway-provider.test.js +163 -0
  196. package/dist/__tests__/runtime-codex-gateway-provider.test.js.map +1 -0
  197. package/dist/__tests__/runtime-identity.test.d.ts +2 -0
  198. package/dist/__tests__/runtime-identity.test.d.ts.map +1 -0
  199. package/dist/__tests__/runtime-identity.test.js +117 -0
  200. package/dist/__tests__/runtime-identity.test.js.map +1 -0
  201. package/dist/__tests__/session-pause-provisioning.test.js +6 -1
  202. package/dist/__tests__/session-pause-provisioning.test.js.map +1 -1
  203. package/dist/__tests__/session-record-disclosure.test.d.ts +24 -0
  204. package/dist/__tests__/session-record-disclosure.test.d.ts.map +1 -0
  205. package/dist/__tests__/session-record-disclosure.test.js +494 -0
  206. package/dist/__tests__/session-record-disclosure.test.js.map +1 -0
  207. package/dist/__tests__/session.test.js +35 -0
  208. package/dist/__tests__/session.test.js.map +1 -1
  209. package/dist/__tests__/slug-validation.test.js +27 -21
  210. package/dist/__tests__/slug-validation.test.js.map +1 -1
  211. package/dist/__tests__/standards-injection.test.d.ts +2 -0
  212. package/dist/__tests__/standards-injection.test.d.ts.map +1 -0
  213. package/dist/__tests__/standards-injection.test.js +179 -0
  214. package/dist/__tests__/standards-injection.test.js.map +1 -0
  215. package/dist/__tests__/start-interactive-role.test.d.ts +22 -0
  216. package/dist/__tests__/start-interactive-role.test.d.ts.map +1 -0
  217. package/dist/__tests__/start-interactive-role.test.js +204 -0
  218. package/dist/__tests__/start-interactive-role.test.js.map +1 -0
  219. package/dist/__tests__/start-team-identity.test.d.ts +25 -0
  220. package/dist/__tests__/start-team-identity.test.d.ts.map +1 -0
  221. package/dist/__tests__/start-team-identity.test.js +307 -0
  222. package/dist/__tests__/start-team-identity.test.js.map +1 -0
  223. package/dist/__tests__/team-memory-commands.test.js +142 -19
  224. package/dist/__tests__/team-memory-commands.test.js.map +1 -1
  225. package/dist/__tests__/template-sync.test.d.ts +19 -0
  226. package/dist/__tests__/template-sync.test.d.ts.map +1 -0
  227. package/dist/__tests__/template-sync.test.js +270 -0
  228. package/dist/__tests__/template-sync.test.js.map +1 -0
  229. package/dist/__tests__/test-project.test.d.ts +18 -0
  230. package/dist/__tests__/test-project.test.d.ts.map +1 -0
  231. package/dist/__tests__/test-project.test.js +465 -0
  232. package/dist/__tests__/test-project.test.js.map +1 -0
  233. package/dist/__tests__/usage-per-call-grouping.test.d.ts +25 -0
  234. package/dist/__tests__/usage-per-call-grouping.test.d.ts.map +1 -0
  235. package/dist/__tests__/usage-per-call-grouping.test.js +952 -0
  236. package/dist/__tests__/usage-per-call-grouping.test.js.map +1 -0
  237. package/dist/__tests__/verify-hooks-check-all.test.d.ts +32 -0
  238. package/dist/__tests__/verify-hooks-check-all.test.d.ts.map +1 -0
  239. package/dist/__tests__/verify-hooks-check-all.test.js +553 -0
  240. package/dist/__tests__/verify-hooks-check-all.test.js.map +1 -0
  241. package/dist/__tests__/worktree-sync-fixture.test.d.ts +17 -0
  242. package/dist/__tests__/worktree-sync-fixture.test.d.ts.map +1 -0
  243. package/dist/__tests__/worktree-sync-fixture.test.js +519 -0
  244. package/dist/__tests__/worktree-sync-fixture.test.js.map +1 -0
  245. package/dist/__tests__/worktree-sync-hardening.test.d.ts +13 -0
  246. package/dist/__tests__/worktree-sync-hardening.test.d.ts.map +1 -0
  247. package/dist/__tests__/worktree-sync-hardening.test.js +919 -0
  248. package/dist/__tests__/worktree-sync-hardening.test.js.map +1 -0
  249. package/dist/__tests__/worktree-sync-shell-free.test.d.ts +25 -0
  250. package/dist/__tests__/worktree-sync-shell-free.test.d.ts.map +1 -0
  251. package/dist/__tests__/worktree-sync-shell-free.test.js +185 -0
  252. package/dist/__tests__/worktree-sync-shell-free.test.js.map +1 -0
  253. package/dist/__tests__/worktree.test.js +75 -1
  254. package/dist/__tests__/worktree.test.js.map +1 -1
  255. package/dist/agent-config/.mx-config-generation.json +2 -2
  256. package/dist/agent-config/AGENTS.md.template +2 -2
  257. package/dist/agent-config/agents/bar-raiser.md +34 -1
  258. package/dist/agent-config/agents/red-team.md +34 -1
  259. package/dist/agent-config/agents/security-reviewer.md +22 -1
  260. package/dist/agent-config/claude-md/CLAUDE.core.md +6 -1
  261. package/dist/agent-config/claude-md/CLAUDE.overlay.memnexus.md +1 -3
  262. package/dist/agent-config/coordination-rosters.generated.json +10 -0
  263. package/dist/agent-config/hooks/HOOK-TIMEOUTS.md +27 -1
  264. package/dist/agent-config/hooks/OR-TRUE-AUDIT.md +8 -8
  265. package/dist/agent-config/hooks/auto-capture-precompact.sh +180 -83
  266. package/dist/agent-config/hooks/auto-capture-session-end.sh +216 -87
  267. package/dist/agent-config/hooks/auto-checkpoint.sh +75 -5
  268. package/dist/agent-config/hooks/coordination-brief.sh +290 -245
  269. package/dist/agent-config/hooks/deploy-verification-reminder.sh +359 -22
  270. package/dist/agent-config/hooks/git-mutation-guard.sh +85 -13
  271. package/dist/agent-config/hooks/observability-sync.sh +46 -9
  272. package/dist/agent-config/hooks/session-idle-gate.sh +177 -12
  273. package/dist/agent-config/hooks/subagent-write-guard.sh +670 -0
  274. package/dist/agent-config/hooks/worktree-guard.sh +36 -7
  275. package/dist/agent-config/settings.json +15 -2
  276. package/dist/agent-config/shared/creation-pr-review-checklist.md +317 -0
  277. package/dist/commands/bootstrap.d.ts.map +1 -1
  278. package/dist/commands/bootstrap.js +22 -10
  279. package/dist/commands/bootstrap.js.map +1 -1
  280. package/dist/commands/config-sync.d.ts +37 -0
  281. package/dist/commands/config-sync.d.ts.map +1 -1
  282. package/dist/commands/config-sync.js +36 -18
  283. package/dist/commands/config-sync.js.map +1 -1
  284. package/dist/commands/config.d.ts.map +1 -1
  285. package/dist/commands/config.js +28 -1
  286. package/dist/commands/config.js.map +1 -1
  287. package/dist/commands/coordination.d.ts +150 -8
  288. package/dist/commands/coordination.d.ts.map +1 -1
  289. package/dist/commands/coordination.js +524 -25
  290. package/dist/commands/coordination.js.map +1 -1
  291. package/dist/commands/create.d.ts +922 -1
  292. package/dist/commands/create.d.ts.map +1 -1
  293. package/dist/commands/create.js +4934 -345
  294. package/dist/commands/create.js.map +1 -1
  295. package/dist/commands/dashboard.d.ts.map +1 -1
  296. package/dist/commands/dashboard.js +10 -2
  297. package/dist/commands/dashboard.js.map +1 -1
  298. package/dist/commands/decision.d.ts +20 -0
  299. package/dist/commands/decision.d.ts.map +1 -0
  300. package/dist/commands/decision.js +54 -0
  301. package/dist/commands/decision.js.map +1 -0
  302. package/dist/commands/digest.d.ts.map +1 -1
  303. package/dist/commands/digest.js +91 -7
  304. package/dist/commands/digest.js.map +1 -1
  305. package/dist/commands/fleet-tmux.js +1 -1
  306. package/dist/commands/fleet-tmux.js.map +1 -1
  307. package/dist/commands/init.d.ts +8 -0
  308. package/dist/commands/init.d.ts.map +1 -1
  309. package/dist/commands/init.js +26 -1
  310. package/dist/commands/init.js.map +1 -1
  311. package/dist/commands/list.d.ts +32 -1
  312. package/dist/commands/list.d.ts.map +1 -1
  313. package/dist/commands/list.js +98 -5
  314. package/dist/commands/list.js.map +1 -1
  315. package/dist/commands/monitor.d.ts.map +1 -1
  316. package/dist/commands/monitor.js +8 -2
  317. package/dist/commands/monitor.js.map +1 -1
  318. package/dist/commands/new.d.ts +2 -0
  319. package/dist/commands/new.d.ts.map +1 -1
  320. package/dist/commands/new.js +114 -21
  321. package/dist/commands/new.js.map +1 -1
  322. package/dist/commands/report.d.ts +33 -0
  323. package/dist/commands/report.d.ts.map +1 -0
  324. package/dist/commands/report.js +73 -0
  325. package/dist/commands/report.js.map +1 -0
  326. package/dist/commands/session.js +1 -1
  327. package/dist/commands/session.js.map +1 -1
  328. package/dist/commands/start.d.ts +42 -0
  329. package/dist/commands/start.d.ts.map +1 -1
  330. package/dist/commands/start.js +183 -8
  331. package/dist/commands/start.js.map +1 -1
  332. package/dist/commands/sync.d.ts +57 -0
  333. package/dist/commands/sync.d.ts.map +1 -1
  334. package/dist/commands/sync.js +117 -3
  335. package/dist/commands/sync.js.map +1 -1
  336. package/dist/commands/update.d.ts +13 -1
  337. package/dist/commands/update.d.ts.map +1 -1
  338. package/dist/commands/update.js +50 -1
  339. package/dist/commands/update.js.map +1 -1
  340. package/dist/commands/usage.d.ts +64 -0
  341. package/dist/commands/usage.d.ts.map +1 -1
  342. package/dist/commands/usage.js +96 -3
  343. package/dist/commands/usage.js.map +1 -1
  344. package/dist/commands/verify-hooks.d.ts +37 -1
  345. package/dist/commands/verify-hooks.d.ts.map +1 -1
  346. package/dist/commands/verify-hooks.js +114 -2
  347. package/dist/commands/verify-hooks.js.map +1 -1
  348. package/dist/commands/window.d.ts +10 -0
  349. package/dist/commands/window.d.ts.map +1 -0
  350. package/dist/commands/window.js +265 -0
  351. package/dist/commands/window.js.map +1 -0
  352. package/dist/commands/worktree-sync.d.ts +54 -0
  353. package/dist/commands/worktree-sync.d.ts.map +1 -0
  354. package/dist/commands/worktree-sync.js +135 -0
  355. package/dist/commands/worktree-sync.js.map +1 -0
  356. package/dist/index.d.ts.map +1 -1
  357. package/dist/index.js +189 -14
  358. package/dist/index.js.map +1 -1
  359. package/dist/lib/agent-session.d.ts +12 -0
  360. package/dist/lib/agent-session.d.ts.map +1 -1
  361. package/dist/lib/agent-session.js +189 -42
  362. package/dist/lib/agent-session.js.map +1 -1
  363. package/dist/lib/behavioral-injection.d.ts +9 -0
  364. package/dist/lib/behavioral-injection.d.ts.map +1 -1
  365. package/dist/lib/behavioral-injection.js +10 -1
  366. package/dist/lib/behavioral-injection.js.map +1 -1
  367. package/dist/lib/catalog.d.ts +321 -2
  368. package/dist/lib/catalog.d.ts.map +1 -1
  369. package/dist/lib/catalog.js +490 -11
  370. package/dist/lib/catalog.js.map +1 -1
  371. package/dist/lib/claude.d.ts +113 -23
  372. package/dist/lib/claude.d.ts.map +1 -1
  373. package/dist/lib/claude.js +498 -32
  374. package/dist/lib/claude.js.map +1 -1
  375. package/dist/lib/cli-build.d.ts +172 -0
  376. package/dist/lib/cli-build.d.ts.map +1 -0
  377. package/dist/lib/cli-build.js +654 -0
  378. package/dist/lib/cli-build.js.map +1 -0
  379. package/dist/lib/code-context-file.d.ts +109 -0
  380. package/dist/lib/code-context-file.d.ts.map +1 -0
  381. package/dist/lib/code-context-file.js +219 -0
  382. package/dist/lib/code-context-file.js.map +1 -0
  383. package/dist/lib/code-context-file.test.d.ts +19 -0
  384. package/dist/lib/code-context-file.test.d.ts.map +1 -0
  385. package/dist/lib/code-context-file.test.js +202 -0
  386. package/dist/lib/code-context-file.test.js.map +1 -0
  387. package/dist/lib/config-fidelity.d.ts +40 -20
  388. package/dist/lib/config-fidelity.d.ts.map +1 -1
  389. package/dist/lib/config-fidelity.js +144 -56
  390. package/dist/lib/config-fidelity.js.map +1 -1
  391. package/dist/lib/coordination-client.d.ts +101 -0
  392. package/dist/lib/coordination-client.d.ts.map +1 -1
  393. package/dist/lib/coordination-client.js +232 -8
  394. package/dist/lib/coordination-client.js.map +1 -1
  395. package/dist/lib/coordination-token.d.ts +333 -6
  396. package/dist/lib/coordination-token.d.ts.map +1 -1
  397. package/dist/lib/coordination-token.js +875 -52
  398. package/dist/lib/coordination-token.js.map +1 -1
  399. package/dist/lib/decision-check.d.ts +48 -0
  400. package/dist/lib/decision-check.d.ts.map +1 -0
  401. package/dist/lib/decision-check.js +149 -0
  402. package/dist/lib/decision-check.js.map +1 -0
  403. package/dist/lib/fleet-data.d.ts +34 -0
  404. package/dist/lib/fleet-data.d.ts.map +1 -1
  405. package/dist/lib/fleet-data.js +37 -9
  406. package/dist/lib/fleet-data.js.map +1 -1
  407. package/dist/lib/fleet-render.js +1 -1
  408. package/dist/lib/fleet-render.js.map +1 -1
  409. package/dist/lib/git-identity.d.ts +302 -0
  410. package/dist/lib/git-identity.d.ts.map +1 -0
  411. package/dist/lib/git-identity.js +546 -0
  412. package/dist/lib/git-identity.js.map +1 -0
  413. package/dist/lib/harness/console/announce.d.ts +45 -0
  414. package/dist/lib/harness/console/announce.d.ts.map +1 -0
  415. package/dist/lib/harness/console/announce.js +48 -0
  416. package/dist/lib/harness/console/announce.js.map +1 -0
  417. package/dist/lib/harness/console/announce.test.d.ts +2 -0
  418. package/dist/lib/harness/console/announce.test.d.ts.map +1 -0
  419. package/dist/lib/harness/console/announce.test.js +104 -0
  420. package/dist/lib/harness/console/announce.test.js.map +1 -0
  421. package/dist/lib/harness/console/by-session.d.ts +50 -0
  422. package/dist/lib/harness/console/by-session.d.ts.map +1 -0
  423. package/dist/lib/harness/console/by-session.js +83 -0
  424. package/dist/lib/harness/console/by-session.js.map +1 -0
  425. package/dist/lib/harness/console/by-session.test.d.ts +2 -0
  426. package/dist/lib/harness/console/by-session.test.d.ts.map +1 -0
  427. package/dist/lib/harness/console/by-session.test.js +86 -0
  428. package/dist/lib/harness/console/by-session.test.js.map +1 -0
  429. package/dist/lib/harness/console/console.d.ts +67 -0
  430. package/dist/lib/harness/console/console.d.ts.map +1 -0
  431. package/dist/lib/harness/console/console.js +373 -0
  432. package/dist/lib/harness/console/console.js.map +1 -0
  433. package/dist/lib/harness/console/console.test.d.ts +2 -0
  434. package/dist/lib/harness/console/console.test.d.ts.map +1 -0
  435. package/dist/lib/harness/console/console.test.js +269 -0
  436. package/dist/lib/harness/console/console.test.js.map +1 -0
  437. package/dist/lib/harness/console/decision-request.d.ts +32 -0
  438. package/dist/lib/harness/console/decision-request.d.ts.map +1 -0
  439. package/dist/lib/harness/console/decision-request.js +112 -0
  440. package/dist/lib/harness/console/decision-request.js.map +1 -0
  441. package/dist/lib/harness/console/decision-request.test.d.ts +2 -0
  442. package/dist/lib/harness/console/decision-request.test.d.ts.map +1 -0
  443. package/dist/lib/harness/console/decision-request.test.js +75 -0
  444. package/dist/lib/harness/console/decision-request.test.js.map +1 -0
  445. package/dist/lib/harness/console/fixtures.d.ts +16 -0
  446. package/dist/lib/harness/console/fixtures.d.ts.map +1 -0
  447. package/dist/lib/harness/console/fixtures.js +49 -0
  448. package/dist/lib/harness/console/fixtures.js.map +1 -0
  449. package/dist/lib/harness/console/index.d.ts +15 -0
  450. package/dist/lib/harness/console/index.d.ts.map +1 -0
  451. package/dist/lib/harness/console/index.js +14 -0
  452. package/dist/lib/harness/console/index.js.map +1 -0
  453. package/dist/lib/harness/console/rank.d.ts +73 -0
  454. package/dist/lib/harness/console/rank.d.ts.map +1 -0
  455. package/dist/lib/harness/console/rank.js +113 -0
  456. package/dist/lib/harness/console/rank.js.map +1 -0
  457. package/dist/lib/harness/console/rank.test.d.ts +2 -0
  458. package/dist/lib/harness/console/rank.test.d.ts.map +1 -0
  459. package/dist/lib/harness/console/rank.test.js +124 -0
  460. package/dist/lib/harness/console/rank.test.js.map +1 -0
  461. package/dist/lib/harness/console/render.d.ts +22 -0
  462. package/dist/lib/harness/console/render.d.ts.map +1 -0
  463. package/dist/lib/harness/console/render.js +65 -0
  464. package/dist/lib/harness/console/render.js.map +1 -0
  465. package/dist/lib/harness/console/render.test.d.ts +2 -0
  466. package/dist/lib/harness/console/render.test.d.ts.map +1 -0
  467. package/dist/lib/harness/console/render.test.js +96 -0
  468. package/dist/lib/harness/console/render.test.js.map +1 -0
  469. package/dist/lib/harness/console/replay-cli.test.d.ts +2 -0
  470. package/dist/lib/harness/console/replay-cli.test.d.ts.map +1 -0
  471. package/dist/lib/harness/console/replay-cli.test.js +96 -0
  472. package/dist/lib/harness/console/replay-cli.test.js.map +1 -0
  473. package/dist/lib/harness/console/replay.d.ts +36 -0
  474. package/dist/lib/harness/console/replay.d.ts.map +1 -0
  475. package/dist/lib/harness/console/replay.js +59 -0
  476. package/dist/lib/harness/console/replay.js.map +1 -0
  477. package/dist/lib/harness/console/replay.test.d.ts +2 -0
  478. package/dist/lib/harness/console/replay.test.d.ts.map +1 -0
  479. package/dist/lib/harness/console/replay.test.js +81 -0
  480. package/dist/lib/harness/console/replay.test.js.map +1 -0
  481. package/dist/lib/harness/console/turn-reader.d.ts +50 -0
  482. package/dist/lib/harness/console/turn-reader.d.ts.map +1 -0
  483. package/dist/lib/harness/console/turn-reader.js +184 -0
  484. package/dist/lib/harness/console/turn-reader.js.map +1 -0
  485. package/dist/lib/harness/console/turn-reader.test.d.ts +2 -0
  486. package/dist/lib/harness/console/turn-reader.test.d.ts.map +1 -0
  487. package/dist/lib/harness/console/turn-reader.test.js +108 -0
  488. package/dist/lib/harness/console/turn-reader.test.js.map +1 -0
  489. package/dist/lib/harness/console/types.d.ts +109 -0
  490. package/dist/lib/harness/console/types.d.ts.map +1 -0
  491. package/dist/lib/harness/console/types.js +2 -0
  492. package/dist/lib/harness/console/types.js.map +1 -0
  493. package/dist/lib/harness/contract.d.ts +229 -0
  494. package/dist/lib/harness/contract.d.ts.map +1 -0
  495. package/dist/lib/harness/contract.js +22 -0
  496. package/dist/lib/harness/contract.js.map +1 -0
  497. package/dist/lib/harness/credential.d.ts +23 -0
  498. package/dist/lib/harness/credential.d.ts.map +1 -0
  499. package/dist/lib/harness/credential.js +81 -0
  500. package/dist/lib/harness/credential.js.map +1 -0
  501. package/dist/lib/harness/credential.test.d.ts +2 -0
  502. package/dist/lib/harness/credential.test.d.ts.map +1 -0
  503. package/dist/lib/harness/credential.test.js +74 -0
  504. package/dist/lib/harness/credential.test.js.map +1 -0
  505. package/dist/lib/harness/evaluate.d.ts +390 -0
  506. package/dist/lib/harness/evaluate.d.ts.map +1 -0
  507. package/dist/lib/harness/evaluate.js +988 -0
  508. package/dist/lib/harness/evaluate.js.map +1 -0
  509. package/dist/lib/harness/evaluate.test.d.ts +2 -0
  510. package/dist/lib/harness/evaluate.test.d.ts.map +1 -0
  511. package/dist/lib/harness/evaluate.test.js +621 -0
  512. package/dist/lib/harness/evaluate.test.js.map +1 -0
  513. package/dist/lib/harness/extract.d.ts +152 -0
  514. package/dist/lib/harness/extract.d.ts.map +1 -0
  515. package/dist/lib/harness/extract.js +1601 -0
  516. package/dist/lib/harness/extract.js.map +1 -0
  517. package/dist/lib/harness/gate.d.ts +48 -0
  518. package/dist/lib/harness/gate.d.ts.map +1 -0
  519. package/dist/lib/harness/gate.js +60 -0
  520. package/dist/lib/harness/gate.js.map +1 -0
  521. package/dist/lib/harness/gate.test.d.ts +2 -0
  522. package/dist/lib/harness/gate.test.d.ts.map +1 -0
  523. package/dist/lib/harness/gate.test.js +111 -0
  524. package/dist/lib/harness/gate.test.js.map +1 -0
  525. package/dist/lib/harness/self-check.d.ts +59 -0
  526. package/dist/lib/harness/self-check.d.ts.map +1 -0
  527. package/dist/lib/harness/self-check.js +56 -0
  528. package/dist/lib/harness/self-check.js.map +1 -0
  529. package/dist/lib/harness/self-check.test.d.ts +2 -0
  530. package/dist/lib/harness/self-check.test.d.ts.map +1 -0
  531. package/dist/lib/harness/self-check.test.js +43 -0
  532. package/dist/lib/harness/self-check.test.js.map +1 -0
  533. package/dist/lib/harness/transcript.d.ts +65 -0
  534. package/dist/lib/harness/transcript.d.ts.map +1 -0
  535. package/dist/lib/harness/transcript.js +144 -0
  536. package/dist/lib/harness/transcript.js.map +1 -0
  537. package/dist/lib/harness/transcript.test.d.ts +2 -0
  538. package/dist/lib/harness/transcript.test.d.ts.map +1 -0
  539. package/dist/lib/harness/transcript.test.js +118 -0
  540. package/dist/lib/harness/transcript.test.js.map +1 -0
  541. package/dist/lib/harness-wiring.d.ts +145 -0
  542. package/dist/lib/harness-wiring.d.ts.map +1 -0
  543. package/dist/lib/harness-wiring.js +280 -0
  544. package/dist/lib/harness-wiring.js.map +1 -0
  545. package/dist/lib/hook-staleness.d.ts +95 -0
  546. package/dist/lib/hook-staleness.d.ts.map +1 -0
  547. package/dist/lib/hook-staleness.js +304 -0
  548. package/dist/lib/hook-staleness.js.map +1 -0
  549. package/dist/lib/jsonl.d.ts +270 -2
  550. package/dist/lib/jsonl.d.ts.map +1 -1
  551. package/dist/lib/jsonl.js +472 -76
  552. package/dist/lib/jsonl.js.map +1 -1
  553. package/dist/lib/memory.d.ts +350 -23
  554. package/dist/lib/memory.d.ts.map +1 -1
  555. package/dist/lib/memory.js +751 -38
  556. package/dist/lib/memory.js.map +1 -1
  557. package/dist/lib/otel-env.d.ts +90 -0
  558. package/dist/lib/otel-env.d.ts.map +1 -0
  559. package/dist/lib/otel-env.js +98 -0
  560. package/dist/lib/otel-env.js.map +1 -0
  561. package/dist/lib/project-config.d.ts +167 -0
  562. package/dist/lib/project-config.d.ts.map +1 -1
  563. package/dist/lib/project-config.js +321 -5
  564. package/dist/lib/project-config.js.map +1 -1
  565. package/dist/lib/projects-registry.d.ts +82 -0
  566. package/dist/lib/projects-registry.d.ts.map +1 -0
  567. package/dist/lib/projects-registry.js +145 -0
  568. package/dist/lib/projects-registry.js.map +1 -0
  569. package/dist/lib/projects-registry.test.d.ts +2 -0
  570. package/dist/lib/projects-registry.test.d.ts.map +1 -0
  571. package/dist/lib/projects-registry.test.js +176 -0
  572. package/dist/lib/projects-registry.test.js.map +1 -0
  573. package/dist/lib/repo-guard-scope.d.ts +126 -0
  574. package/dist/lib/repo-guard-scope.d.ts.map +1 -0
  575. package/dist/lib/repo-guard-scope.js +249 -0
  576. package/dist/lib/repo-guard-scope.js.map +1 -0
  577. package/dist/lib/report-format.d.ts +132 -0
  578. package/dist/lib/report-format.d.ts.map +1 -0
  579. package/dist/lib/report-format.js +317 -0
  580. package/dist/lib/report-format.js.map +1 -0
  581. package/dist/lib/roster.d.ts +99 -0
  582. package/dist/lib/roster.d.ts.map +1 -0
  583. package/dist/lib/roster.js +336 -0
  584. package/dist/lib/roster.js.map +1 -0
  585. package/dist/lib/roster.test.d.ts +15 -0
  586. package/dist/lib/roster.test.d.ts.map +1 -0
  587. package/dist/lib/roster.test.js +299 -0
  588. package/dist/lib/roster.test.js.map +1 -0
  589. package/dist/lib/runtime/codex-adapter.d.ts.map +1 -1
  590. package/dist/lib/runtime/codex-adapter.js +5 -0
  591. package/dist/lib/runtime/codex-adapter.js.map +1 -1
  592. package/dist/lib/runtime/codex-config.d.ts +129 -0
  593. package/dist/lib/runtime/codex-config.d.ts.map +1 -1
  594. package/dist/lib/runtime/codex-config.js +225 -0
  595. package/dist/lib/runtime/codex-config.js.map +1 -1
  596. package/dist/lib/runtime/identity.d.ts +72 -0
  597. package/dist/lib/runtime/identity.d.ts.map +1 -0
  598. package/dist/lib/runtime/identity.js +128 -0
  599. package/dist/lib/runtime/identity.js.map +1 -0
  600. package/dist/lib/standards.d.ts +99 -0
  601. package/dist/lib/standards.d.ts.map +1 -0
  602. package/dist/lib/standards.js +201 -0
  603. package/dist/lib/standards.js.map +1 -0
  604. package/dist/lib/team-identity.d.ts +134 -0
  605. package/dist/lib/team-identity.d.ts.map +1 -0
  606. package/dist/lib/team-identity.js +193 -0
  607. package/dist/lib/team-identity.js.map +1 -0
  608. package/dist/lib/templates.d.ts +29 -2
  609. package/dist/lib/templates.d.ts.map +1 -1
  610. package/dist/lib/templates.js +1042 -82
  611. package/dist/lib/templates.js.map +1 -1
  612. package/dist/lib/terminal-ui.d.ts.map +1 -1
  613. package/dist/lib/terminal-ui.js +12 -1
  614. package/dist/lib/terminal-ui.js.map +1 -1
  615. package/dist/lib/test-project.d.ts +95 -0
  616. package/dist/lib/test-project.d.ts.map +1 -0
  617. package/dist/lib/test-project.js +103 -0
  618. package/dist/lib/test-project.js.map +1 -0
  619. package/dist/lib/vault.d.ts +60 -0
  620. package/dist/lib/vault.d.ts.map +1 -0
  621. package/dist/lib/vault.js +102 -0
  622. package/dist/lib/vault.js.map +1 -0
  623. package/dist/lib/vault.test.d.ts +17 -0
  624. package/dist/lib/vault.test.d.ts.map +1 -0
  625. package/dist/lib/vault.test.js +169 -0
  626. package/dist/lib/vault.test.js.map +1 -0
  627. package/dist/lib/window-view.d.ts +63 -0
  628. package/dist/lib/window-view.d.ts.map +1 -0
  629. package/dist/lib/window-view.js +217 -0
  630. package/dist/lib/window-view.js.map +1 -0
  631. package/dist/lib/window-view.test.d.ts +2 -0
  632. package/dist/lib/window-view.test.d.ts.map +1 -0
  633. package/dist/lib/window-view.test.js +168 -0
  634. package/dist/lib/window-view.test.js.map +1 -0
  635. package/dist/lib/worktree-sync.d.ts +439 -0
  636. package/dist/lib/worktree-sync.d.ts.map +1 -0
  637. package/dist/lib/worktree-sync.js +1167 -0
  638. package/dist/lib/worktree-sync.js.map +1 -0
  639. package/dist/lib/worktree.d.ts +58 -1
  640. package/dist/lib/worktree.d.ts.map +1 -1
  641. package/dist/lib/worktree.js +91 -4
  642. package/dist/lib/worktree.js.map +1 -1
  643. package/dist/standards/decision-request.md +98 -0
  644. package/dist/standards/fail-loud-standard.md +495 -0
  645. package/dist/standards/po-communication-standard.md +126 -0
  646. package/dist/standards/report-format.md +128 -0
  647. package/dist/standards/writing-standard.md +382 -0
  648. package/package.json +3 -2
  649. package/scripts/generate-claude.mjs +119 -46
package/dist/lib/jsonl.js CHANGED
@@ -75,7 +75,17 @@ export function normalizeModelId(model) {
75
75
  * getRates('opus') test in usage-pricing.test.ts (#5283 item 3).
76
76
  */
77
77
  export function getRates(model) {
78
- return MODEL_PRICING[normalizeModelId(model)] || DEFAULT_RATES;
78
+ // hasOwnProperty, not a bare lookup, and the same call hasRates makes.
79
+ // MODEL_PRICING is a plain object literal, so a bare lookup reaches
80
+ // Object.prototype: a transcript model of 'constructor', 'toString',
81
+ // 'valueOf' or '__proto__' returned a function or an object instead of rates,
82
+ // every field of it was undefined, and calcLineCost returned NaN, which then
83
+ // poisoned the fleet total. hasRates already read it correctly and returned
84
+ // false for those four, so the two disagreed. Reproduced on all four.
85
+ const id = normalizeModelId(model);
86
+ return Object.prototype.hasOwnProperty.call(MODEL_PRICING, id)
87
+ ? MODEL_PRICING[id]
88
+ : DEFAULT_RATES;
79
89
  }
80
90
  /**
81
91
  * True when the model can be priced from an explicit MODEL_PRICING entry (not a
@@ -102,6 +112,11 @@ export function newTotals() {
102
112
  models: new Set(),
103
113
  mxCalls: 0,
104
114
  mxEstTokens: 0,
115
+ usageGrouped: 0,
116
+ usageUngrouped: 0,
117
+ usageInconsistent: 0,
118
+ usageUndated: 0,
119
+ mxUndated: 0,
105
120
  };
106
121
  }
107
122
  /**
@@ -123,6 +138,11 @@ export function addToTotals(dest, src) {
123
138
  dest.cost += src.cost;
124
139
  dest.mxCalls += src.mxCalls;
125
140
  dest.mxEstTokens += src.mxEstTokens;
141
+ dest.usageGrouped += src.usageGrouped;
142
+ dest.usageUngrouped += src.usageUngrouped;
143
+ dest.usageInconsistent += src.usageInconsistent;
144
+ dest.usageUndated += src.usageUndated;
145
+ dest.mxUndated += src.mxUndated;
126
146
  src.sessions.forEach((s) => dest.sessions.add(s));
127
147
  src.models.forEach((m) => dest.models.add(m));
128
148
  }
@@ -210,9 +230,179 @@ export function collectJsonlFiles(projectDir) {
210
230
  const { leadFiles, childFiles, legacyChildFiles } = scanProjectDir(projectDir);
211
231
  return [...leadFiles, ...childFiles.map((c) => c.path), ...legacyChildFiles];
212
232
  }
233
+ export function newUsageGrouper() {
234
+ return { byKey: new Map(), unkeyed: [] };
235
+ }
236
+ /**
237
+ * The grouping key for a transcript entry: `requestId`, else `message.id`, else
238
+ * '' when the entry carries neither.
239
+ */
240
+ export function usageGroupKey(entry) {
241
+ const requestId = entry?.requestId;
242
+ if (typeof requestId === 'string' && requestId)
243
+ return requestId;
244
+ const message = entry?.message;
245
+ const messageId = message?.id;
246
+ if (typeof messageId === 'string' && messageId)
247
+ return messageId;
248
+ return '';
249
+ }
250
+ /**
251
+ * One usage field as a token count. The transcript reports these as JSON
252
+ * numbers; every other shape is one we cannot price.
253
+ *
254
+ * A cast plus `|| 0` kept a non-number and then added to it. Measured on the
255
+ * pre-change code, a single line with `input_tokens: '100'` reported
256
+ * `inputTokens` as the STRING '0100', because `0 + '100'` concatenates. That
257
+ * figure printed as a token count and was summed into cost, with no signal to
258
+ * the operator. Read the type, and count an unreadable value as 0.
259
+ *
260
+ * A negative count is as unreadable as a string: no API call bills a negative
261
+ * number of tokens, and a negative SUBTRACTS. Measured on the code that accepted
262
+ * one, a single line carrying `input_tokens: -6000000000` cancelled the whole
263
+ * corrected fleet input total of 6,649,443,346 (issue #307), printed nothing,
264
+ * warned nothing, and still counted as one healthy grouped call. An in-fleet
265
+ * agent writes these transcript lines, and this tool attributes spend to those
266
+ * same agents, so a line that makes one agent's spend read low is inside what
267
+ * this function defends against. `> 0` rather than `>= 0` so that both 0 and -0
268
+ * return the literal 0: -0 is a valid count but it is not what a reader expects
269
+ * a token total to hold.
270
+ */
271
+ export function usageTokenCount(value) {
272
+ return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : 0;
273
+ }
274
+ /**
275
+ * The entry's timestamp as epoch ms, or undefined when it carries none this code
276
+ * can read. Only a string or a number is read; an object, an array or a boolean
277
+ * is not a timestamp.
278
+ *
279
+ * `new Date(x).getTime()` is NaN for an object or an unparseable string, and
280
+ * every comparison against NaN is false. Measured on the pre-change code, one
281
+ * line whose timestamp was an object reported `in=10` with `recentIn=0`: the row
282
+ * counted in the all-time total and never in the recent window. NaN also poisoned
283
+ * the group, because `ts > existing.ts` is false against NaN, so one unreadable
284
+ * first line kept a whole call out of the window even when later lines were
285
+ * dated.
286
+ */
287
+ export function usageEntryTs(entry) {
288
+ const raw = entry?.timestamp;
289
+ if (typeof raw !== 'string' && typeof raw !== 'number')
290
+ return undefined;
291
+ const t = new Date(raw).getTime();
292
+ return Number.isFinite(t) ? t : undefined;
293
+ }
294
+ /**
295
+ * Merge one transcript entry into the grouper. Returns false when the entry has
296
+ * no usage object, so callers can keep their own per-line work separate.
297
+ *
298
+ * NINE transcript fields are read here, and no content field is among them:
299
+ * `message.usage.input_tokens`, `message.usage.output_tokens`,
300
+ * `message.usage.cache_read_input_tokens`,
301
+ * `message.usage.cache_creation_input_tokens`, `message.model`, `message.id`
302
+ * (through usageGroupKey), `requestId` (through usageGroupKey), `sessionId`, and
303
+ * `timestamp` (through usageEntryTs). `message.content` is never read by this
304
+ * function. The gateway team's security review of PR #325 reported seven; the
305
+ * bootstrap team's security-reviewer corrected it to nine (INFO-4 on that pull
306
+ * request). The list above is re-derived from this function, not copied from
307
+ * either report.
308
+ */
309
+ export function addUsageLine(g, entry) {
310
+ const message = entry?.message;
311
+ const usage = message?.usage;
312
+ if (!usage || typeof usage !== 'object' || !('input_tokens' in usage))
313
+ return false;
314
+ const input = usageTokenCount(usage.input_tokens);
315
+ const output = usageTokenCount(usage.output_tokens);
316
+ const cacheRead = usageTokenCount(usage.cache_read_input_tokens);
317
+ const cacheWrite = usageTokenCount(usage.cache_creation_input_tokens);
318
+ // A cast plus `|| ''` keeps a truthy non-string, and normalizeModelId then
319
+ // calls .replace on it and throws. Pricing used to run inside the per-line
320
+ // try/catch, which swallowed that; it now runs once per group after the file
321
+ // is read, so one malformed line would abort the whole usage report and name
322
+ // no file. Read the type, not the cast.
323
+ const model = typeof message?.model === 'string' ? message.model : '';
324
+ // Same reason as the model line above. A cast plus `|| ''` keeps a truthy
325
+ // non-string, and `allTime.sessions` is a Set: it treats each object as
326
+ // distinct, so N malformed lines inflated the session count by N. Measured on
327
+ // the code that cast: three lines with `sessionId: {id:'x'}` gave
328
+ // sessions.size 3, every member an object.
329
+ const session = typeof entry?.sessionId === 'string' ? entry.sessionId : '';
330
+ const parsedTs = usageEntryTs(entry);
331
+ const key = usageGroupKey(entry);
332
+ const row = {
333
+ key, keyed: key !== '',
334
+ input, output, cacheRead, cacheWrite,
335
+ model, session,
336
+ ts: parsedTs ?? 0, dated: parsedTs !== undefined,
337
+ lines: 1, inconsistent: false,
338
+ };
339
+ // No requestId and no message.id: the line cannot be joined to anything, so it
340
+ // is its own row. Counted once, reported under ungrouped, never dropped.
341
+ if (!key) {
342
+ g.unkeyed.push(row);
343
+ return true;
344
+ }
345
+ const existing = g.byKey.get(key);
346
+ if (!existing) {
347
+ g.byKey.set(key, row);
348
+ return true;
349
+ }
350
+ existing.lines++;
351
+ // Constancy check. These three are equal on every line of a group in the
352
+ // measured corpus. When they are not, take the maximum, which never sums, and
353
+ // record the group so the count reaches the operator.
354
+ if (input !== existing.input || cacheRead !== existing.cacheRead || cacheWrite !== existing.cacheWrite) {
355
+ existing.inconsistent = true;
356
+ existing.input = Math.max(existing.input, input);
357
+ existing.cacheRead = Math.max(existing.cacheRead, cacheRead);
358
+ existing.cacheWrite = Math.max(existing.cacheWrite, cacheWrite);
359
+ }
360
+ // output_tokens grows across the group's lines. The maximum is the final value.
361
+ if (output > existing.output)
362
+ existing.output = output;
363
+ // An undated line never overwrites a dated one, and the first dated line in a
364
+ // group sets the timestamp whatever its position.
365
+ if (parsedTs !== undefined && (!existing.dated || parsedTs > existing.ts)) {
366
+ existing.ts = parsedTs;
367
+ existing.dated = true;
368
+ }
369
+ // model is the field that selects the rate in calcLineCost, and it was the one
370
+ // grouped field exempt from the constancy check above: the first line of a
371
+ // group set the price and every later line's model was discarded with
372
+ // usageInconsistent staying 0. Measured on the code that skipped this check, a
373
+ // two-line group of claude-haiku-4-5 then claude-opus-5 priced at $0.0055
374
+ // instead of $0.0275 and reported no inconsistency. Before grouping, mispricing
375
+ // a call took a wrong model on every line; now the first line decides, so the
376
+ // check has to cover it. Compared only when both sides are non-empty, because a
377
+ // message_delta line carries no model and that is not a disagreement.
378
+ if (existing.model && model && model !== existing.model)
379
+ existing.inconsistent = true;
380
+ if (!existing.model && model)
381
+ existing.model = model;
382
+ if (!existing.session && session)
383
+ existing.session = session;
384
+ return true;
385
+ }
386
+ /** Every row the grouper holds, plus the three outcome counts. */
387
+ export function drainUsageGroups(g) {
388
+ const keyed = Array.from(g.byKey.values());
389
+ return {
390
+ rows: [...keyed, ...g.unkeyed],
391
+ stats: {
392
+ grouped: keyed.length,
393
+ ungrouped: g.unkeyed.length,
394
+ inconsistent: keyed.filter((r) => r.inconsistent).length,
395
+ undated: [...keyed, ...g.unkeyed].filter((r) => !r.dated).length,
396
+ },
397
+ };
398
+ }
213
399
  // ── Aggregated usage (used by mx-agent usage) ─────────────────────────────────
214
400
  /**
215
401
  * Read a project dir's JSONL logs and return all-time and recent token totals.
402
+ *
403
+ * Usage is counted once per billed API call, not once per transcript line: lines
404
+ * are grouped by requestId, falling back to message.id (#307). See the per-call
405
+ * usage grouping section above for the mechanism and the measured overcount.
216
406
  */
217
407
  export function readProjectUsage(projectDir, recentCutoffMs) {
218
408
  const allTime = newTotals();
@@ -227,12 +417,17 @@ export function readProjectUsage(projectDir, recentCutoffMs) {
227
417
  continue;
228
418
  }
229
419
  const mxToolUseIds = new Map();
420
+ // One row per billed call rather than one per transcript line (#307).
421
+ const grouper = newUsageGrouper();
230
422
  for (const line of content.split('\n')) {
231
423
  if (!line.trim())
232
424
  continue;
233
425
  try {
234
426
  const entry = JSON.parse(line);
235
- const ts = entry?.timestamp ? new Date(entry.timestamp).getTime() : 0;
427
+ // undefined when the line carries no timestamp this code can read. The
428
+ // MemNexus tool counters below treat that the same way the usage rows do:
429
+ // counted in the recent window rather than silently dropped from it.
430
+ const ts = usageEntryTs(entry);
236
431
  const role = entry?.message?.role;
237
432
  const msgContent = entry?.message?.content;
238
433
  // ── MemNexus tool tracking ──────────────────────────────────────
@@ -247,7 +442,16 @@ export function readProjectUsage(projectDir, recentCutoffMs) {
247
442
  if (isMcp || isCli) {
248
443
  mxToolUseIds.set(block.id, ts);
249
444
  allTime.mxCalls++;
250
- if (ts >= cutoff)
445
+ // Undated counts as recent, the rule the usage rows use, and it is
446
+ // counted so the recent column is not silently resting on it. On
447
+ // origin/main a missing timestamp gave 0 and `0 >= cutoff` was
448
+ // false, so these lines did not count as recent; they do now, and
449
+ // that is what this counter discloses.
450
+ if (ts === undefined) {
451
+ allTime.mxUndated++;
452
+ recent.mxUndated++;
453
+ }
454
+ if (ts === undefined || ts >= cutoff)
251
455
  recent.mxCalls++;
252
456
  }
253
457
  }
@@ -260,45 +464,62 @@ export function readProjectUsage(projectDir, recentCutoffMs) {
260
464
  const origTs = mxToolUseIds.get(block.tool_use_id);
261
465
  const estTokens = Math.round(JSON.stringify(block.content ?? '').length / 4);
262
466
  allTime.mxEstTokens += estTokens;
263
- if (origTs >= cutoff)
467
+ if (origTs === undefined || origTs >= cutoff)
264
468
  recent.mxEstTokens += estTokens;
265
469
  }
266
470
  }
267
471
  }
268
472
  // ── Token usage ─────────────────────────────────────────────────
269
- const usage = entry?.message?.usage;
270
- if (!usage || typeof usage !== 'object' || !('input_tokens' in usage))
271
- continue;
272
- const input = usage.input_tokens || 0;
273
- const output = usage.output_tokens || 0;
274
- const cacheR = usage.cache_read_input_tokens || 0;
275
- const cacheW = usage.cache_creation_input_tokens || 0;
276
- const model = entry?.message?.model || '';
277
- const session = entry?.sessionId || '';
278
- const lineCost = calcLineCost(input, output, cacheR, cacheW, getRates(model));
279
- allTime.inputTokens += input;
280
- allTime.outputTokens += output;
281
- allTime.cacheReadTokens += cacheR;
282
- allTime.cacheWriteTokens += cacheW;
283
- allTime.cost += lineCost;
284
- if (session)
285
- allTime.sessions.add(session);
286
- if (model)
287
- allTime.models.add(model);
288
- if (ts >= cutoff) {
289
- recent.inputTokens += input;
290
- recent.outputTokens += output;
291
- recent.cacheReadTokens += cacheR;
292
- recent.cacheWriteTokens += cacheW;
293
- recent.cost += lineCost;
294
- if (session)
295
- recent.sessions.add(session);
296
- if (model)
297
- recent.models.add(model);
298
- }
473
+ // Held per requestId (or message.id) and priced once the file is read.
474
+ addUsageLine(grouper, entry);
299
475
  }
300
476
  catch { /* skip malformed line */ }
301
477
  }
478
+ // ── One priced row per billed call (#307) ──────────────────────────
479
+ for (const row of drainUsageGroups(grouper).rows) {
480
+ const rowCost = calcLineCost(row.input, row.output, row.cacheRead, row.cacheWrite, getRates(row.model));
481
+ allTime.inputTokens += row.input;
482
+ allTime.outputTokens += row.output;
483
+ allTime.cacheReadTokens += row.cacheRead;
484
+ allTime.cacheWriteTokens += row.cacheWrite;
485
+ allTime.cost += rowCost;
486
+ if (row.keyed)
487
+ allTime.usageGrouped++;
488
+ else
489
+ allTime.usageUngrouped++;
490
+ if (row.inconsistent)
491
+ allTime.usageInconsistent++;
492
+ if (!row.dated)
493
+ allTime.usageUndated++;
494
+ if (row.session)
495
+ allTime.sessions.add(row.session);
496
+ if (row.model)
497
+ allTime.models.add(row.model);
498
+ // A group's window membership is its latest dated line, the point the call
499
+ // ended. A row no line of which is dated counts in the recent window as
500
+ // well as the all-time total: excluding it understates the window and the
501
+ // operator cannot see the difference, while including it can only make the
502
+ // window read closer to the all-time total, which is visible.
503
+ if (!row.dated || row.ts >= cutoff) {
504
+ recent.inputTokens += row.input;
505
+ recent.outputTokens += row.output;
506
+ recent.cacheReadTokens += row.cacheRead;
507
+ recent.cacheWriteTokens += row.cacheWrite;
508
+ recent.cost += rowCost;
509
+ if (row.keyed)
510
+ recent.usageGrouped++;
511
+ else
512
+ recent.usageUngrouped++;
513
+ if (row.inconsistent)
514
+ recent.usageInconsistent++;
515
+ if (!row.dated)
516
+ recent.usageUndated++;
517
+ if (row.session)
518
+ recent.sessions.add(row.session);
519
+ if (row.model)
520
+ recent.models.add(row.model);
521
+ }
522
+ }
302
523
  }
303
524
  return { allTime, recent };
304
525
  }
@@ -430,12 +651,81 @@ export function isMachineGeneratedUserRecord(entry) {
430
651
  return isEntirelyMachineWrapper(trimmed);
431
652
  }
432
653
  /**
433
- * Parse all JSONL files in a project dir and return one SessionRecord per session.
654
+ * Object keys this code never publishes in a JSON body.
655
+ *
656
+ * `subagentTypes` keys are `tool_use.input.subagent_type`, chosen by the model
657
+ * that writes the Agent call, so they are the most caller-influenced strings in
658
+ * a SessionRecord. The local accumulator is a Map and nothing is polluted here,
659
+ * but `Object.fromEntries` produced a plain object whose `__proto__` key
660
+ * survived JSON.stringify into the /v1/sessions/sync body. Measured on the
661
+ * pre-change code, one line with `subagent_type: "__proto__"` gave
662
+ * `"subagentTypes":{"__proto__":1}` with hasOwnProperty true.
663
+ *
664
+ * `JSON.parse` on the receiving side creates an own property and is safe by
665
+ * itself. The sink is any later `Object.assign`, spread-merge or deep-merge on
666
+ * the server, where an own `__proto__` data property is copied with [[Set]] and
667
+ * reaches the target's prototype. Nobody here has reviewed the observability
668
+ * service, so this makes no claim that such a sink exists. The filter is three
669
+ * string comparisons per session and it removes the question.
670
+ *
671
+ * Reported by the bootstrap team's security-reviewer on PR #325 (SF-2).
672
+ */
673
+ const UNSAFE_WIRE_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
674
+ /**
675
+ * The `subagentTypes` object that goes on the wire, with every UNSAFE_WIRE_KEYS
676
+ * key dropped. Exported for tests.
677
+ *
678
+ * Dropping loses a spawn count. `agentSpawns` still counts those spawns, and
679
+ * SessionRecordStats.droppedSubagentTypeKeys counts the keys dropped here, so no
680
+ * count disappears without a number an operator can read.
681
+ */
682
+ export function toWireSubagentTypes(counts) {
683
+ const out = {};
684
+ for (const [key, n] of counts) {
685
+ if (UNSAFE_WIRE_KEYS.has(key))
686
+ continue;
687
+ out[key] = n;
688
+ }
689
+ return out;
690
+ }
691
+ /** How many of a session's subagent_type keys toWireSubagentTypes will drop. */
692
+ export function unsafeWireKeyCount(counts) {
693
+ let n = 0;
694
+ for (const key of counts.keys())
695
+ if (UNSAFE_WIRE_KEYS.has(key))
696
+ n++;
697
+ return n;
698
+ }
699
+ /**
700
+ * Parse all JSONL files in a project dir and return one SessionRecord per
701
+ * session, with the counts of what the walk discarded.
434
702
  *
435
703
  * Only returns sessions that have at least one assistant record with usage data.
436
704
  * Sessions without token data (e.g. failed starts) are excluded.
705
+ *
706
+ * Token counts are one row per billed API call, not one per transcript line:
707
+ * lines are grouped by requestId, falling back to message.id (#307). These
708
+ * records are what `mx-agent sync` POSTs to /v1/sessions/sync, so a per-line sum
709
+ * here writes the same overcount into the observability store, where correcting
710
+ * it needs a backfill. `model` is now the model of the most billed CALLS in the
711
+ * session rather than of the most lines.
712
+ *
713
+ * `sessionId`, `timestamp` and every usage field are read by type here, not by
714
+ * cast. Commit b6b8e23 is titled "read every transcript timestamp and sessionId
715
+ * by type"; its sessionId change covered readProjectUsage only, and this walk's
716
+ * own sessionId read was fixed afterwards. Both are covered now, and the title
717
+ * is what made the gap easy to miss.
437
718
  */
438
- export function parseSessionRecords(projectDir) {
719
+ export function parseSessionRecordsWithStats(projectDir) {
720
+ const stats = {
721
+ skippedLines: 0,
722
+ skippedUsageLines: 0,
723
+ skippedUsageInputTokens: 0,
724
+ skippedUsageOutputTokens: 0,
725
+ skippedUsageCacheReadTokens: 0,
726
+ skippedUsageCacheWriteTokens: 0,
727
+ droppedSubagentTypeKeys: 0,
728
+ };
439
729
  // Per-session accumulators
440
730
  const sessions = new Map();
441
731
  for (const filePath of collectJsonlFiles(projectDir)) {
@@ -446,15 +736,64 @@ export function parseSessionRecords(projectDir) {
446
736
  catch {
447
737
  continue;
448
738
  }
739
+ // One grouper per session per FILE (#307). Grouping scope is one transcript
740
+ // file, the same scope readProjectUsage uses: every line of one call is
741
+ // written by the file that made the call, so a key never spans files.
742
+ const fileGroupers = new Map();
743
+ // The dropped lines get a grouper of their own, on the same one-file scope,
744
+ // so their tokens are counted once per billed call exactly as
745
+ // readProjectUsage counts the same lines. See the skip branch below.
746
+ const skippedGrouper = newUsageGrouper();
449
747
  for (const line of content.split('\n')) {
450
748
  if (!line.trim())
451
749
  continue;
452
750
  try {
453
751
  const entry = JSON.parse(line);
454
- const sessionId = entry?.sessionId;
455
- if (!sessionId)
752
+ // Read the type, do not cast. Any truthy value passed this check, and
753
+ // both Maps below key on it: a JS Map keys an object by reference, so N
754
+ // lines carrying `sessionId: {}` made N `sessions` entries and N
755
+ // `fileGroupers` entries. Measured on the code that cast: three such
756
+ // lines produced three SessionRecords, each with an object sessionId,
757
+ // and `records.push({ sessionId, ... })` puts that value in the array
758
+ // mx-agent sync POSTs to /v1/sessions/sync, where it is durable.
759
+ // A string of blanks is unreadable in the same way a number or an
760
+ // object is, and `!sessionId` is false for ' ', so such a line was
761
+ // neither dropped nor counted: it created a session record keyed by
762
+ // ' ' and mx-agent sync POSTed it. Measured on the code without the
763
+ // trim, two lines where one carries `sessionId: ' '` and 1000 input
764
+ // tokens gave 2 records with sessionIds [' ', 'session-good'] and
765
+ // every counter at 0. Only the emptiness test trims. The untrimmed
766
+ // value stays the key whenever it is not blank, so no session that
767
+ // syncs today changes identity.
768
+ const rawSessionId = typeof entry?.sessionId === 'string' ? entry.sessionId : '';
769
+ const sessionId = rawSessionId.trim() ? rawSessionId : '';
770
+ if (!sessionId) {
771
+ // Counted rather than dropped in silence. readProjectUsage keeps this
772
+ // line and counts its tokens, so a usage-bearing line dropped here is
773
+ // a difference between what the observability store holds and what
774
+ // `mx-agent usage` prints, with nothing else for an operator to read.
775
+ //
776
+ // The tokens go through addUsageLine and a grouper rather than a
777
+ // per-line sum. Two dropped lines of ONE streamed call repeat the same
778
+ // input number, and adding them reports 2,000 tokens for a gap of
779
+ // 1,000. That is the #307 overcount, and putting it inside the
780
+ // disclosure that measures what #307 left behind would be the same
781
+ // defect one level up. Measured on this fixture: naive per-line sum
782
+ // 2,000 against a real gap of 1,000, 2x.
783
+ stats.skippedLines++;
784
+ if (addUsageLine(skippedGrouper, entry)) {
785
+ stats.skippedUsageLines++;
786
+ }
456
787
  continue;
457
- const ts = entry?.timestamp ? new Date(entry.timestamp).getTime() : 0;
788
+ }
789
+ // 0 when the line carries no timestamp this code can read, never NaN.
790
+ // The `ts > 0` guards below already skip 0, but they cannot skip NaN:
791
+ // `firstTs: ts` at the accumulator's first line kept NaN, `ts < NaN` and
792
+ // `ts > NaN` are both false, and every later dated line was then ignored.
793
+ // Measured on the code that cast: a session whose FIRST line had an
794
+ // object timestamp reported durationMs 0 and a `date` of the moment the
795
+ // report ran, discarding the timestamps its other lines carried.
796
+ const ts = usageEntryTs(entry) ?? 0;
458
797
  const entryType = entry?.type;
459
798
  const msgContent = entry?.message?.content;
460
799
  // Get or create session accumulator
@@ -616,23 +955,43 @@ export function parseSessionRecords(projectDir) {
616
955
  }
617
956
  }
618
957
  // ── Token usage ──────────────────────────────────────────────
619
- const usage = entry?.message?.usage;
620
- if (usage && typeof usage === 'object' && 'input_tokens' in usage) {
621
- s.inputTokens += usage.input_tokens || 0;
622
- s.outputTokens += usage.output_tokens || 0;
623
- s.cacheReadTokens += usage.cache_read_input_tokens || 0;
624
- s.cacheWriteTokens += usage.cache_creation_input_tokens || 0;
625
- const model = entry?.message?.model || '';
626
- if (model) {
627
- s.modelCounts.set(model, (s.modelCounts.get(model) || 0) + 1);
628
- }
958
+ // Held per requestId (or message.id) and totalled once the file is read.
959
+ let grouper = fileGroupers.get(sessionId);
960
+ if (!grouper) {
961
+ grouper = newUsageGrouper();
962
+ fileGroupers.set(sessionId, grouper);
629
963
  }
964
+ addUsageLine(grouper, entry);
630
965
  // Track last entry timestamp for wait-time calculation
631
966
  if (ts > 0)
632
967
  s.lastEntryTs = ts;
633
968
  }
634
969
  catch { /* skip malformed line */ }
635
970
  }
971
+ // One row per billed call for the dropped lines too, so the number the
972
+ // summary prints is the same arithmetic mx-agent usage does.
973
+ for (const row of drainUsageGroups(skippedGrouper).rows) {
974
+ stats.skippedUsageInputTokens += row.input;
975
+ stats.skippedUsageOutputTokens += row.output;
976
+ stats.skippedUsageCacheReadTokens += row.cacheRead;
977
+ stats.skippedUsageCacheWriteTokens += row.cacheWrite;
978
+ }
979
+ // ── One row per billed call, added to the session's totals (#307) ──────
980
+ for (const [sessionId, grouper] of fileGroupers) {
981
+ const s = sessions.get(sessionId);
982
+ if (!s)
983
+ continue;
984
+ for (const row of drainUsageGroups(grouper).rows) {
985
+ s.inputTokens += row.input;
986
+ s.outputTokens += row.output;
987
+ s.cacheReadTokens += row.cacheRead;
988
+ s.cacheWriteTokens += row.cacheWrite;
989
+ // One count per billed call. Counting per line made the model of the
990
+ // longest-streamed responses win, which is not the most-used model.
991
+ if (row.model)
992
+ s.modelCounts.set(row.model, (s.modelCounts.get(row.model) || 0) + 1);
993
+ }
994
+ }
636
995
  }
637
996
  // Convert accumulators to SessionRecord[], dropping sessions with no token data
638
997
  const records = [];
@@ -648,6 +1007,9 @@ export function parseSessionRecords(projectDir) {
648
1007
  topCount = count;
649
1008
  }
650
1009
  }
1010
+ // Counted only for sessions that become a record, because only those reach
1011
+ // the wire object toWireSubagentTypes builds.
1012
+ stats.droppedSubagentTypeKeys += unsafeWireKeyCount(s.subagentTypes);
651
1013
  records.push({
652
1014
  sessionId,
653
1015
  date: s.firstTs ? new Date(s.firstTs).toISOString() : new Date().toISOString(),
@@ -667,11 +1029,22 @@ export function parseSessionRecords(projectDir) {
667
1029
  prNumbers: Array.from(s.prNumbers),
668
1030
  memorySaves: s.memorySaves,
669
1031
  agentSpawns: s.agentSpawns,
670
- subagentTypes: Object.fromEntries(s.subagentTypes),
1032
+ subagentTypes: toWireSubagentTypes(s.subagentTypes),
671
1033
  deployVerified: s.deployVerified,
672
1034
  });
673
1035
  }
674
- return records;
1036
+ return { records, stats };
1037
+ }
1038
+ /**
1039
+ * The records alone, for the callers that read no counter.
1040
+ *
1041
+ * `commands/pager.ts` and `commands/pr-sync.ts` read toolCalls, memorySaves,
1042
+ * agentSpawns and PR numbers, never a token field, so a dropped line changes
1043
+ * nothing they publish. `commands/sync.ts` POSTs the token fields and calls
1044
+ * parseSessionRecordsWithStats instead.
1045
+ */
1046
+ export function parseSessionRecords(projectDir) {
1047
+ return parseSessionRecordsWithStats(projectDir).records;
675
1048
  }
676
1049
  function newAgentUsage(subagentType) {
677
1050
  return {
@@ -754,7 +1127,10 @@ function collectJoins(content, into) {
754
1127
  * Build per-subagent-definition token/cost attribution for a project dir.
755
1128
  *
756
1129
  * Additive: does not touch Totals, SessionRecord, or readProjectUsage. Prices
757
- * child transcripts line-by-line with the existing calcLineCost/getRates.
1130
+ * child transcripts once per billed API call with the existing
1131
+ * calcLineCost/getRates. Lines are grouped by requestId, falling back to
1132
+ * message.id (#307); before that they were priced one row per transcript line,
1133
+ * which counted each streamed call two or three times.
758
1134
  */
759
1135
  export function parseAgentAttribution(projectDir) {
760
1136
  const { parentFiles, childFiles } = discoverAttributionFiles(projectDir);
@@ -795,6 +1171,10 @@ export function parseAgentAttribution(projectDir) {
795
1171
  let unattributedTokens = 0;
796
1172
  let oldestTs = 0;
797
1173
  let newestTs = 0;
1174
+ let usageGrouped = 0;
1175
+ let usageUngrouped = 0;
1176
+ let usageInconsistent = 0;
1177
+ let usageUndated = 0;
798
1178
  for (const { path, agentId } of childFiles) {
799
1179
  const content = childContents.get(path);
800
1180
  if (content === undefined)
@@ -815,6 +1195,8 @@ export function parseAgentAttribution(projectDir) {
815
1195
  unattributed.fileCount++;
816
1196
  }
817
1197
  const counts = subagentType ? modelCounts.get(subagentType) : null;
1198
+ // One row per billed call rather than one per transcript line (#307).
1199
+ const grouper = newUsageGrouper();
818
1200
  for (const line of content.split('\n')) {
819
1201
  if (!line.trim())
820
1202
  continue;
@@ -827,7 +1209,10 @@ export function parseAgentAttribution(projectDir) {
827
1209
  }
828
1210
  const message = entry.message;
829
1211
  const usage = message?.usage;
830
- const ts = entry.timestamp ? new Date(entry.timestamp).getTime() : 0;
1212
+ // 0 rather than NaN, the same read as the other two walks. This one's
1213
+ // `ts > 0` guard already rejected NaN, so the range it reports is
1214
+ // unchanged; the read is here so the three paths cannot drift.
1215
+ const ts = usageEntryTs(entry) ?? 0;
831
1216
  if (ts > 0) {
832
1217
  if (oldestTs === 0 || ts < oldestTs)
833
1218
  oldestTs = ts;
@@ -836,39 +1221,46 @@ export function parseAgentAttribution(projectDir) {
836
1221
  }
837
1222
  if (!usage || typeof usage !== 'object' || !('input_tokens' in usage))
838
1223
  continue;
839
- const input = usage.input_tokens || 0;
840
- const output = usage.output_tokens || 0;
841
- const cacheR = usage.cache_read_input_tokens || 0;
842
- const cacheW = usage.cache_creation_input_tokens || 0;
843
- const model = message?.model || '';
1224
+ // Held per requestId (or message.id) and priced once the file is read.
1225
+ addUsageLine(grouper, entry);
1226
+ }
1227
+ // ── One priced row per billed call (#307) ────────────────────────────
1228
+ const { rows, stats } = drainUsageGroups(grouper);
1229
+ usageGrouped += stats.grouped;
1230
+ usageUngrouped += stats.ungrouped;
1231
+ usageInconsistent += stats.inconsistent;
1232
+ usageUndated += stats.undated;
1233
+ for (const row of rows) {
1234
+ const model = row.model;
844
1235
  // getRates/hasRates normalize internally (#5283); passing the raw ID here
845
- // is now identical to the previous explicit normalizeModelId() call —
846
- // normalization is idempotent, so this is a simplification, not a change.
847
- // The raw ID is what gets REPORTED when unknown, so the operator sees the
848
- // exact string the transcript carried.
849
- const lineCost = calcLineCost(input, output, cacheR, cacheW, getRates(model));
850
- const lineTokens = input + output + cacheR + cacheW;
1236
+ // is identical to an explicit normalizeModelId() call, since normalization
1237
+ // is idempotent. The raw ID is what gets REPORTED when unknown, so the
1238
+ // operator sees the exact string the transcript carried.
1239
+ const rowCost = calcLineCost(row.input, row.output, row.cacheRead, row.cacheWrite, getRates(model));
1240
+ const rowTokens = row.input + row.output + row.cacheRead + row.cacheWrite;
851
1241
  if (model && !hasRates(model))
852
1242
  unknownModels.add(model);
853
1243
  if (bucket) {
854
- bucket.inputTokens += input;
855
- bucket.outputTokens += output;
856
- bucket.cacheReadTokens += cacheR;
857
- bucket.cacheWriteTokens += cacheW;
858
- bucket.cost += lineCost;
1244
+ bucket.inputTokens += row.input;
1245
+ bucket.outputTokens += row.output;
1246
+ bucket.cacheReadTokens += row.cacheRead;
1247
+ bucket.cacheWriteTokens += row.cacheWrite;
1248
+ bucket.cost += rowCost;
859
1249
  if (model) {
860
1250
  bucket.models.add(model);
1251
+ // Dominant model is now weighted by billed call, not by transcript
1252
+ // line, so a streamed call no longer votes two or three times.
861
1253
  counts.set(model, (counts.get(model) || 0) + 1);
862
1254
  }
863
- attributedTokens += lineTokens;
1255
+ attributedTokens += rowTokens;
864
1256
  }
865
1257
  else {
866
- unattributed.inputTokens += input;
867
- unattributed.outputTokens += output;
868
- unattributed.cacheReadTokens += cacheR;
869
- unattributed.cacheWriteTokens += cacheW;
870
- unattributed.cost += lineCost;
871
- unattributedTokens += lineTokens;
1258
+ unattributed.inputTokens += row.input;
1259
+ unattributed.outputTokens += row.output;
1260
+ unattributed.cacheReadTokens += row.cacheRead;
1261
+ unattributed.cacheWriteTokens += row.cacheWrite;
1262
+ unattributed.cost += rowCost;
1263
+ unattributedTokens += rowTokens;
872
1264
  }
873
1265
  }
874
1266
  }
@@ -896,6 +1288,10 @@ export function parseAgentAttribution(projectDir) {
896
1288
  coveragePct,
897
1289
  oldestTs,
898
1290
  newestTs,
1291
+ usageGrouped,
1292
+ usageUngrouped,
1293
+ usageInconsistent,
1294
+ usageUndated,
899
1295
  };
900
1296
  }
901
1297
  //# sourceMappingURL=jsonl.js.map