@andorra-labs/code 0.1.11 → 0.1.13

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 (614) hide show
  1. package/README.md +8 -2
  2. package/THIRD_PARTY_NOTICES.md +15 -0
  3. package/dist/src/account-presentation.d.ts +1 -37
  4. package/dist/src/account-presentation.js +2 -42
  5. package/dist/src/activity.d.ts +0 -25
  6. package/dist/src/activity.js +1 -23
  7. package/dist/src/agent-definitions.d.ts +0 -26
  8. package/dist/src/agent-definitions.js +0 -43
  9. package/dist/src/agent-execution.d.ts +0 -2
  10. package/dist/src/agent-execution.js +59 -414
  11. package/dist/src/agent-mcp-catalog.d.ts +0 -8
  12. package/dist/src/agent-mcp-catalog.js +0 -2
  13. package/dist/src/agent-mcp.d.ts +0 -56
  14. package/dist/src/agent-mcp.js +14 -153
  15. package/dist/src/agent-modifiers.d.ts +0 -1
  16. package/dist/src/agent-modifiers.js +25 -68
  17. package/dist/src/analytics.d.ts +0 -66
  18. package/dist/src/analytics.js +0 -45
  19. package/dist/src/anthropic-console-auth.d.ts +0 -24
  20. package/dist/src/anthropic-console-auth.js +0 -37
  21. package/dist/src/anthropic-credentials.d.ts +0 -25
  22. package/dist/src/anthropic-credentials.js +0 -28
  23. package/dist/src/app-server/protocol.d.ts +19 -244
  24. package/dist/src/app-server/protocol.js +1 -132
  25. package/dist/src/app-server/reasoning.d.ts +0 -31
  26. package/dist/src/app-server/reasoning.js +0 -54
  27. package/dist/src/app-server/terminal.d.ts +0 -36
  28. package/dist/src/app-server/terminal.js +3 -39
  29. package/dist/src/app-server/workspace.d.ts +0 -167
  30. package/dist/src/app-server/workspace.js +4 -406
  31. package/dist/src/app-server.d.ts +33 -918
  32. package/dist/src/app-server.js +608 -2984
  33. package/dist/src/artifacts.d.ts +0 -10
  34. package/dist/src/artifacts.js +1 -11
  35. package/dist/src/attachments.d.ts +4 -98
  36. package/dist/src/attachments.js +10 -130
  37. package/dist/src/auth/AuthManager.d.ts +0 -93
  38. package/dist/src/auth/AuthManager.js +3 -165
  39. package/dist/src/auth/callback-page.d.ts +0 -1
  40. package/dist/src/auth/callback-page.js +0 -4
  41. package/dist/src/auth/callback.d.ts +0 -25
  42. package/dist/src/auth/callback.js +1 -29
  43. package/dist/src/auth/deployment.d.ts +0 -16
  44. package/dist/src/auth/deployment.js +0 -16
  45. package/dist/src/auth/index.d.ts +0 -11
  46. package/dist/src/auth/index.js +0 -11
  47. package/dist/src/auth/lock.d.ts +0 -1
  48. package/dist/src/auth/lock.js +0 -1
  49. package/dist/src/auth/store.d.ts +7 -108
  50. package/dist/src/auth/store.js +21 -129
  51. package/dist/src/auth/types.d.ts +0 -8
  52. package/dist/src/auth-commands.d.ts +0 -5
  53. package/dist/src/auth-commands.js +0 -1
  54. package/dist/src/automation-runner.d.ts +0 -9
  55. package/dist/src/automation-runner.js +0 -11
  56. package/dist/src/automation-service.d.ts +0 -10
  57. package/dist/src/automation-service.js +3 -17
  58. package/dist/src/automation-tool.js +0 -5
  59. package/dist/src/automation-worker-shutdown.d.ts +0 -1
  60. package/dist/src/automation-worker-shutdown.js +0 -3
  61. package/dist/src/automations.d.ts +23 -2
  62. package/dist/src/automations.js +91 -12
  63. package/dist/src/background-tasks.d.ts +0 -23
  64. package/dist/src/background-tasks.js +0 -11
  65. package/dist/src/boot-trace.d.ts +0 -13
  66. package/dist/src/boot-trace.js +1 -14
  67. package/dist/src/browser-activity.js +0 -2
  68. package/dist/src/browser-bridge.d.ts +0 -2
  69. package/dist/src/browser-bridge.js +0 -27
  70. package/dist/src/browser-captcha-tool.d.ts +0 -13
  71. package/dist/src/browser-captcha-tool.js +3 -35
  72. package/dist/src/browser-extensions-tool.d.ts +24 -0
  73. package/dist/src/browser-extensions-tool.js +137 -0
  74. package/dist/src/browser-extensions.d.ts +127 -0
  75. package/dist/src/browser-extensions.js +729 -0
  76. package/dist/src/browser-observation.d.ts +0 -15
  77. package/dist/src/browser-observation.js +1 -22
  78. package/dist/src/browser-panel-tool.d.ts +10 -25
  79. package/dist/src/browser-panel-tool.js +64 -60
  80. package/dist/src/browser-runner.d.ts +0 -1
  81. package/dist/src/browser-runner.js +7 -46
  82. package/dist/src/browser-server.d.ts +0 -6
  83. package/dist/src/browser-server.js +6 -229
  84. package/dist/src/browser-session.d.ts +0 -25
  85. package/dist/src/browser-session.js +1 -71
  86. package/dist/src/browser-stealth.d.ts +0 -12
  87. package/dist/src/browser-stealth.js +0 -36
  88. package/dist/src/browser-ui/app.js +7 -174
  89. package/dist/src/browser-ui/style.css +0 -68
  90. package/dist/src/browser-ui/tokens.css +11 -93
  91. package/dist/src/browser-view.d.ts +52 -290
  92. package/dist/src/browser-view.js +354 -567
  93. package/dist/src/capabilities.d.ts +0 -8
  94. package/dist/src/capabilities.js +0 -29
  95. package/dist/src/capsolver.d.ts +0 -13
  96. package/dist/src/capsolver.js +0 -15
  97. package/dist/src/clarification-ui.d.ts +0 -7
  98. package/dist/src/clarification-ui.js +0 -8
  99. package/dist/src/claude-executable.d.ts +0 -1
  100. package/dist/src/claude-executable.js +1 -2
  101. package/dist/src/claude-provider.d.ts +6 -19
  102. package/dist/src/claude-provider.js +166 -61
  103. package/dist/src/cli-executable.d.ts +0 -16
  104. package/dist/src/cli-executable.js +0 -7
  105. package/dist/src/cli-process.d.ts +0 -26
  106. package/dist/src/cli-process.js +0 -18
  107. package/dist/src/cli-sign-in.d.ts +0 -20
  108. package/dist/src/cli-sign-in.js +1 -17
  109. package/dist/src/cli.d.ts +3 -56
  110. package/dist/src/cli.js +193 -702
  111. package/dist/src/code-intelligence/CodeIntelligenceManager.d.ts +4 -59
  112. package/dist/src/code-intelligence/CodeIntelligenceManager.js +27 -100
  113. package/dist/src/code-intelligence/ContextLedger.d.ts +0 -4
  114. package/dist/src/code-intelligence/ContextLedger.js +0 -10
  115. package/dist/src/code-intelligence/ContextRouter.d.ts +0 -6
  116. package/dist/src/code-intelligence/ContextRouter.js +0 -8
  117. package/dist/src/code-intelligence/codegraph/CodeGraphProjectSession.d.ts +0 -2
  118. package/dist/src/code-intelligence/codegraph/CodeGraphProjectSession.js +5 -12
  119. package/dist/src/code-intelligence/codegraph/CodeGraphProvider.d.ts +0 -6
  120. package/dist/src/code-intelligence/codegraph/CodeGraphProvider.js +0 -6
  121. package/dist/src/code-intelligence/codegraph/CodeGraphSyncBarrier.d.ts +0 -5
  122. package/dist/src/code-intelligence/codegraph/CodeGraphSyncBarrier.js +1 -17
  123. package/dist/src/code-intelligence/codegraph/repoExploreTool.d.ts +0 -5
  124. package/dist/src/code-intelligence/codegraph/repoExploreTool.js +0 -5
  125. package/dist/src/code-intelligence/repository.d.ts +0 -9
  126. package/dist/src/code-intelligence/repository.js +1 -11
  127. package/dist/src/code-intelligence/types.d.ts +0 -8
  128. package/dist/src/codex-auth.d.ts +0 -44
  129. package/dist/src/codex-auth.js +0 -45
  130. package/dist/src/codex-browser-auth.d.ts +0 -13
  131. package/dist/src/codex-browser-auth.js +3 -20
  132. package/dist/src/codex-errors.js +0 -2
  133. package/dist/src/codex-executable.d.ts +0 -2
  134. package/dist/src/codex-executable.js +1 -7
  135. package/dist/src/codex-provider.d.ts +0 -13
  136. package/dist/src/codex-provider.js +1 -26
  137. package/dist/src/codex-rpc.d.ts +0 -10
  138. package/dist/src/codex-rpc.js +0 -12
  139. package/dist/src/coding-tools.d.ts +6 -27
  140. package/dist/src/coding-tools.js +193 -205
  141. package/dist/src/command-metrics.d.ts +0 -4
  142. package/dist/src/command-output-tap.d.ts +0 -11
  143. package/dist/src/command-output-tap.js +0 -3
  144. package/dist/src/command-screens.d.ts +24 -0
  145. package/dist/src/command-screens.js +132 -100
  146. package/dist/src/commands.d.ts +6 -63
  147. package/dist/src/commands.js +20 -100
  148. package/dist/src/completion-gates.d.ts +0 -47
  149. package/dist/src/completion-gates.js +0 -14
  150. package/dist/src/completion.d.ts +2 -58
  151. package/dist/src/completion.js +90 -186
  152. package/dist/src/config.d.ts +12 -116
  153. package/dist/src/config.js +41 -171
  154. package/dist/src/context-budget.d.ts +0 -22
  155. package/dist/src/context-budget.js +0 -44
  156. package/dist/src/context-compaction.d.ts +29 -6
  157. package/dist/src/context-compaction.js +94 -25
  158. package/dist/src/context-selection.d.ts +0 -24
  159. package/dist/src/context-selection.js +0 -34
  160. package/dist/src/dashboard-view.d.ts +0 -2
  161. package/dist/src/dashboard-view.js +0 -35
  162. package/dist/src/dashboard.d.ts +0 -2
  163. package/dist/src/dashboard.js +0 -9
  164. package/dist/src/deadline.d.ts +0 -45
  165. package/dist/src/deadline.js +0 -54
  166. package/dist/src/decision/ContinuationAdapter.d.ts +0 -5
  167. package/dist/src/decision/ContinuationAdapter.js +0 -5
  168. package/dist/src/decision/DecisionEngine.d.ts +0 -4
  169. package/dist/src/decision/DecisionManager.d.ts +0 -1
  170. package/dist/src/decision/DecisionManager.js +2 -7
  171. package/dist/src/decision/DecisionPolicy.d.ts +0 -1
  172. package/dist/src/decision/DecisionPolicy.js +0 -1
  173. package/dist/src/decision/FactContinuations.d.ts +11 -43
  174. package/dist/src/decision/FactContinuations.js +126 -95
  175. package/dist/src/decision/FactServiceGate.d.ts +0 -1
  176. package/dist/src/decision/FactServiceGate.js +0 -4
  177. package/dist/src/decision/PreparedSteps.d.ts +0 -15
  178. package/dist/src/decision/PreparedSteps.js +1 -11
  179. package/dist/src/decision/applyToolMenu.d.ts +0 -1
  180. package/dist/src/decision/applyToolMenu.js +1 -5
  181. package/dist/src/decision/checks.d.ts +15 -28
  182. package/dist/src/decision/checks.js +118 -49
  183. package/dist/src/decision/facts.d.ts +0 -16
  184. package/dist/src/decision/facts.js +0 -30
  185. package/dist/src/decision/guidancePolicy.js +0 -1
  186. package/dist/src/decision/hostedPolicy.d.ts +0 -6
  187. package/dist/src/decision/hostedPolicy.js +0 -7
  188. package/dist/src/decision/hostedSkills.d.ts +0 -19
  189. package/dist/src/decision/hostedSkills.js +0 -19
  190. package/dist/src/decision/optimizationPolicy.d.ts +0 -3
  191. package/dist/src/decision/optimizationPolicy.js +1 -4
  192. package/dist/src/decision/providers/AndorraDecisionProvider.d.ts +0 -7
  193. package/dist/src/decision/providers/AndorraDecisionProvider.js +4 -27
  194. package/dist/src/decision/providers/HttpDecisionProvider.d.ts +0 -2
  195. package/dist/src/decision/providers/HttpDecisionProvider.js +0 -6
  196. package/dist/src/decision/routeTools.d.ts +0 -8
  197. package/dist/src/decision/routeTools.js +1 -8
  198. package/dist/src/decision/toolFamilies.d.ts +0 -1
  199. package/dist/src/decision/toolFamilies.js +0 -1
  200. package/dist/src/decision/toolMenu.d.ts +0 -3
  201. package/dist/src/decision/toolMenu.js +0 -4
  202. package/dist/src/decision/types.d.ts +0 -6
  203. package/dist/src/default-graph.d.ts +0 -9
  204. package/dist/src/default-graph.js +0 -9
  205. package/dist/src/delegation-changes.d.ts +0 -56
  206. package/dist/src/delegation-changes.js +15 -124
  207. package/dist/src/diff-preview.d.ts +1 -1
  208. package/dist/src/diff-preview.js +4 -12
  209. package/dist/src/duration.d.ts +0 -50
  210. package/dist/src/duration.js +0 -50
  211. package/dist/src/efficiency.d.ts +0 -39
  212. package/dist/src/efficiency.js +0 -16
  213. package/dist/src/engine/protocol.d.ts +0 -2
  214. package/dist/src/engine/protocol.js +0 -2
  215. package/dist/src/engine-streams.d.ts +13 -0
  216. package/dist/src/engine-streams.js +16 -0
  217. package/dist/src/extensions.d.ts +3 -2
  218. package/dist/src/extensions.js +76 -37
  219. package/dist/src/favicons.d.ts +0 -26
  220. package/dist/src/favicons.js +0 -34
  221. package/dist/src/file-diff.d.ts +2 -58
  222. package/dist/src/file-diff.js +0 -32
  223. package/dist/src/file-read-key.d.ts +1 -0
  224. package/dist/src/file-read-key.js +5 -0
  225. package/dist/src/file-read-state.d.ts +0 -1
  226. package/dist/src/file-read-state.js +0 -4
  227. package/dist/src/firefox-cookies.d.ts +0 -34
  228. package/dist/src/firefox-cookies.js +1 -55
  229. package/dist/src/git-identity-audit.d.ts +27 -0
  230. package/dist/src/git-identity-audit.js +286 -0
  231. package/dist/src/git-identity-guard.d.ts +4 -0
  232. package/dist/src/git-identity-guard.js +998 -0
  233. package/dist/src/git-identity-hold.d.ts +12 -0
  234. package/dist/src/git-identity-hold.js +81 -0
  235. package/dist/src/git-summary.d.ts +0 -58
  236. package/dist/src/git-summary.js +2 -212
  237. package/dist/src/goal-completion-verification.d.ts +2 -2
  238. package/dist/src/goal-completion-verification.js +3 -6
  239. package/dist/src/graph-account.d.ts +0 -21
  240. package/dist/src/graph-account.js +0 -17
  241. package/dist/src/graph-editor-session.d.ts +0 -1
  242. package/dist/src/graph-editor-session.js +1 -3
  243. package/dist/src/graph-health.d.ts +7 -49
  244. package/dist/src/graph-health.js +35 -46
  245. package/dist/src/graph-manager-chat.d.ts +27 -128
  246. package/dist/src/graph-manager-chat.js +36 -166
  247. package/dist/src/graph-provider-portability.d.ts +1 -21
  248. package/dist/src/graph-provider-portability.js +6 -45
  249. package/dist/src/graph-quota.d.ts +0 -33
  250. package/dist/src/graph-quota.js +1 -44
  251. package/dist/src/graph-runner.d.ts +0 -1
  252. package/dist/src/graph-runner.js +47 -387
  253. package/dist/src/graph-selection.d.ts +0 -14
  254. package/dist/src/graph-selection.js +0 -14
  255. package/dist/src/graph-server.d.ts +3 -11
  256. package/dist/src/graph-server.js +70 -111
  257. package/dist/src/graph-studio-features.d.ts +0 -5
  258. package/dist/src/graph-studio-features.js +2 -42
  259. package/dist/src/graph-sync.d.ts +0 -78
  260. package/dist/src/graph-sync.js +5 -201
  261. package/dist/src/graph-tool.d.ts +0 -18
  262. package/dist/src/graph-tool.js +0 -27
  263. package/dist/src/graph-ui/app.js +229 -498
  264. package/dist/src/graph-ui/compat.js +0 -7
  265. package/dist/src/graph-ui/controls.css +47 -43
  266. package/dist/src/graph-ui/controls.js +0 -88
  267. package/dist/src/graph-ui/drafts.js +0 -12
  268. package/dist/src/graph-ui/edge-router.js +0 -62
  269. package/dist/src/graph-ui/embed.js +0 -59
  270. package/dist/src/graph-ui/icons.js +2 -7
  271. package/dist/src/graph-ui/index.html +2 -2
  272. package/dist/src/graph-ui/library.css +0 -5
  273. package/dist/src/graph-ui/manager-markdown.js +0 -11
  274. package/dist/src/graph-ui/manager-state.js +110 -49
  275. package/dist/src/graph-ui/manager.css +2 -20
  276. package/dist/src/graph-ui/manager.js +251 -166
  277. package/dist/src/graph-ui/model-picker.js +250 -0
  278. package/dist/src/graph-ui/provider-glyphs/alibaba-cloud.png +0 -0
  279. package/dist/src/graph-ui/provider-glyphs/anthropic.png +0 -0
  280. package/dist/src/graph-ui/provider-glyphs/aws-bedrock.png +0 -0
  281. package/dist/src/graph-ui/provider-glyphs/claude.png +0 -0
  282. package/dist/src/graph-ui/provider-glyphs/deepseek.png +0 -0
  283. package/dist/src/graph-ui/provider-glyphs/fireworks.png +0 -0
  284. package/dist/src/graph-ui/provider-glyphs/kimi.png +0 -0
  285. package/dist/src/graph-ui/provider-glyphs/minimax.png +0 -0
  286. package/dist/src/graph-ui/provider-glyphs/openai.png +0 -0
  287. package/dist/src/graph-ui/provider-glyphs/opencode.png +0 -0
  288. package/dist/src/graph-ui/provider-glyphs/openrouter.png +0 -0
  289. package/dist/src/graph-ui/provider-glyphs/xai.png +0 -0
  290. package/dist/src/graph-ui/provider-glyphs/xiaomi-mimo.png +0 -0
  291. package/dist/src/graph-ui/provider-glyphs/zai.png +0 -0
  292. package/dist/src/graph-ui/sidebar.js +2 -3
  293. package/dist/src/graph-ui/skill-picker.js +373 -39
  294. package/dist/src/graph-ui/studio-canvas.js +3 -38
  295. package/dist/src/graph-ui/studio.css +0 -18
  296. package/dist/src/graph-ui/studio.js +3 -71
  297. package/dist/src/graph-ui/style.css +11 -134
  298. package/dist/src/graph-ui/theme.js +1 -26
  299. package/dist/src/graph-ui/tokens.css +15 -114
  300. package/dist/src/graphs.d.ts +7 -156
  301. package/dist/src/graphs.js +72 -273
  302. package/dist/src/grep-regex-worker.js +0 -2
  303. package/dist/src/grep-regex.d.ts +1 -5
  304. package/dist/src/grep-regex.js +6 -8
  305. package/dist/src/grok-provider.d.ts +0 -17
  306. package/dist/src/grok-provider.js +1 -26
  307. package/dist/src/handoffs.d.ts +0 -11
  308. package/dist/src/handoffs.js +0 -13
  309. package/dist/src/hardened-runtime.d.ts +0 -10
  310. package/dist/src/hardened-runtime.js +8 -46
  311. package/dist/src/harness-home.d.ts +21 -0
  312. package/dist/src/harness-home.js +87 -0
  313. package/dist/src/host-process-guard.d.ts +0 -50
  314. package/dist/src/host-process-guard.js +0 -641
  315. package/dist/src/limits.d.ts +0 -64
  316. package/dist/src/limits.js +0 -62
  317. package/dist/src/managed-sign-in.d.ts +0 -12
  318. package/dist/src/managed-sign-in.js +0 -16
  319. package/dist/src/markdown.d.ts +0 -28
  320. package/dist/src/markdown.js +1 -175
  321. package/dist/src/mcp.d.ts +21 -82
  322. package/dist/src/mcp.js +228 -237
  323. package/dist/src/membership-errors.d.ts +0 -14
  324. package/dist/src/membership-errors.js +0 -13
  325. package/dist/src/membership.d.ts +2 -60
  326. package/dist/src/membership.js +0 -85
  327. package/dist/src/memory/LocalToolMemory.d.ts +0 -1
  328. package/dist/src/memory/LocalToolMemory.js +2 -7
  329. package/dist/src/memory/MemoryDistiller.js +0 -15
  330. package/dist/src/memory/MemoryManager.d.ts +0 -3
  331. package/dist/src/memory/MemoryManager.js +6 -18
  332. package/dist/src/memory/MemoryPolicy.d.ts +0 -2
  333. package/dist/src/memory/MemoryPolicy.js +0 -2
  334. package/dist/src/memory/MemoryProvider.d.ts +0 -6
  335. package/dist/src/memory/MemoryRetriever.js +0 -7
  336. package/dist/src/memory/PermissionMemoryProvider.d.ts +0 -1
  337. package/dist/src/memory/PermissionMemoryProvider.js +0 -1
  338. package/dist/src/memory/ProjectMemory.d.ts +42 -0
  339. package/dist/src/memory/ProjectMemory.js +285 -0
  340. package/dist/src/memory/ToolFailureLessons.d.ts +0 -3
  341. package/dist/src/memory/ToolFailureLessons.js +0 -6
  342. package/dist/src/memory/index.d.ts +1 -0
  343. package/dist/src/memory/index.js +1 -0
  344. package/dist/src/memory/providers/NoOpMemoryProvider.d.ts +0 -1
  345. package/dist/src/memory/providers/NoOpMemoryProvider.js +0 -1
  346. package/dist/src/memory/redaction.d.ts +0 -5
  347. package/dist/src/memory/redaction.js +0 -11
  348. package/dist/src/memory/repository.d.ts +0 -6
  349. package/dist/src/memory/repository.js +2 -12
  350. package/dist/src/memory/types.d.ts +0 -1
  351. package/dist/src/model-health-cache.d.ts +0 -38
  352. package/dist/src/model-health-cache.js +1 -49
  353. package/dist/src/model-metadata.d.ts +0 -18
  354. package/dist/src/model-metadata.js +2 -42
  355. package/dist/src/model-override.d.ts +0 -36
  356. package/dist/src/model-override.js +0 -43
  357. package/dist/src/model-tiers.d.ts +0 -13
  358. package/dist/src/model-tiers.js +0 -23
  359. package/dist/src/opencode-catalog.d.ts +0 -2
  360. package/dist/src/opencode-catalog.js +0 -11
  361. package/dist/src/opencode-cli-provider.d.ts +0 -35
  362. package/dist/src/opencode-cli-provider.js +1 -44
  363. package/dist/src/opencode-provider.d.ts +0 -2
  364. package/dist/src/opencode-provider.js +6 -22
  365. package/dist/src/package-name.d.ts +1 -0
  366. package/dist/src/package-name.js +1 -0
  367. package/dist/src/paged-transcript.d.ts +0 -11
  368. package/dist/src/paged-transcript.js +2 -20
  369. package/dist/src/palette.d.ts +0 -63
  370. package/dist/src/palette.js +0 -63
  371. package/dist/src/permissions.d.ts +0 -15
  372. package/dist/src/permissions.js +0 -17
  373. package/dist/src/powershell-pool.d.ts +0 -9
  374. package/dist/src/powershell-pool.js +3 -26
  375. package/dist/src/process-warnings.d.ts +4 -0
  376. package/dist/src/process-warnings.js +103 -0
  377. package/dist/src/profile.d.ts +0 -43
  378. package/dist/src/profile.js +0 -43
  379. package/dist/src/project-reservation.d.ts +11 -0
  380. package/dist/src/project-reservation.js +143 -0
  381. package/dist/src/provider-connection.d.ts +0 -18
  382. package/dist/src/provider-connection.js +0 -39
  383. package/dist/src/provider-engine.d.ts +0 -21
  384. package/dist/src/provider-engine.js +3 -98
  385. package/dist/src/provider-import.d.ts +0 -49
  386. package/dist/src/provider-import.js +0 -60
  387. package/dist/src/provider-keys.d.ts +1 -36
  388. package/dist/src/provider-keys.js +5 -48
  389. package/dist/src/provider-settings.d.ts +0 -2
  390. package/dist/src/provider-settings.js +0 -32
  391. package/dist/src/providers.d.ts +0 -217
  392. package/dist/src/providers.js +0 -315
  393. package/dist/src/pull-requests.d.ts +0 -173
  394. package/dist/src/pull-requests.js +5 -195
  395. package/dist/src/qa/browser/PlaywrightRunner.d.ts +0 -7
  396. package/dist/src/qa/browser/PlaywrightRunner.js +2 -16
  397. package/dist/src/qa/command-policy.d.ts +0 -23
  398. package/dist/src/qa/command-policy.js +0 -30
  399. package/dist/src/qa/guidance.d.ts +0 -21
  400. package/dist/src/qa/guidance.js +0 -20
  401. package/dist/src/qa/profile/ProjectInspector.d.ts +0 -4
  402. package/dist/src/qa/profile/ProjectInspector.js +0 -6
  403. package/dist/src/qa/profile/QaConfigLoader.d.ts +0 -7
  404. package/dist/src/qa/profile/QaConfigLoader.js +0 -6
  405. package/dist/src/qa/profile/QaProfile.d.ts +0 -7
  406. package/dist/src/qa/profile/QaProfile.js +0 -7
  407. package/dist/src/qa/profile/yaml.d.ts +0 -9
  408. package/dist/src/qa/profile/yaml.js +0 -13
  409. package/dist/src/qa/qa-authorize.d.ts +0 -19
  410. package/dist/src/qa/qa-authorize.js +4 -11
  411. package/dist/src/qa/runtime/ProcessManager.d.ts +0 -31
  412. package/dist/src/qa/runtime/ProcessManager.js +4 -41
  413. package/dist/src/qa/runtime/PtyManager.d.ts +0 -4
  414. package/dist/src/qa/runtime/PtyManager.js +2 -7
  415. package/dist/src/qa/runtime/changes.d.ts +0 -11
  416. package/dist/src/qa/runtime/changes.js +1 -3
  417. package/dist/src/qa/tools.d.ts +0 -10
  418. package/dist/src/qa/tools.js +0 -12
  419. package/dist/src/qa/types.d.ts +0 -13
  420. package/dist/src/react-runtime.d.ts +2 -21
  421. package/dist/src/react-runtime.js +106 -418
  422. package/dist/src/read-document-doc.d.ts +0 -6
  423. package/dist/src/read-document-doc.js +0 -55
  424. package/dist/src/read-document-docx.d.ts +0 -16
  425. package/dist/src/read-document-docx.js +2 -69
  426. package/dist/src/read-document-text.d.ts +0 -17
  427. package/dist/src/read-document-text.js +0 -22
  428. package/dist/src/read-document-tool.d.ts +0 -31
  429. package/dist/src/read-document-tool.js +0 -30
  430. package/dist/src/read-document.d.ts +0 -49
  431. package/dist/src/read-document.js +1 -67
  432. package/dist/src/read-image.d.ts +0 -32
  433. package/dist/src/read-image.js +0 -33
  434. package/dist/src/remote/cert.d.ts +1 -19
  435. package/dist/src/remote/cert.js +2 -51
  436. package/dist/src/remote/client.d.ts +0 -70
  437. package/dist/src/remote/client.js +2 -117
  438. package/dist/src/remote/command.js +0 -17
  439. package/dist/src/remote/engine-pool.d.ts +0 -152
  440. package/dist/src/remote/engine-pool.js +17 -335
  441. package/dist/src/remote/errors.d.ts +0 -15
  442. package/dist/src/remote/errors.js +0 -7
  443. package/dist/src/remote/network.d.ts +0 -26
  444. package/dist/src/remote/network.js +1 -34
  445. package/dist/src/remote/pairing.d.ts +16 -51
  446. package/dist/src/remote/pairing.js +50 -57
  447. package/dist/src/remote/pem.d.ts +2 -0
  448. package/dist/src/remote/pem.js +7 -0
  449. package/dist/src/remote/roots.d.ts +0 -20
  450. package/dist/src/remote/roots.js +0 -8
  451. package/dist/src/remote/serve.d.ts +0 -47
  452. package/dist/src/remote/serve.js +0 -30
  453. package/dist/src/remote/server.d.ts +0 -14
  454. package/dist/src/remote/server.js +1 -79
  455. package/dist/src/remote/types.d.ts +1 -32
  456. package/dist/src/remote/types.js +0 -10
  457. package/dist/src/run-control.d.ts +0 -7
  458. package/dist/src/run-control.js +0 -7
  459. package/dist/src/run-progress.d.ts +0 -170
  460. package/dist/src/run-progress.js +0 -285
  461. package/dist/src/run-records.d.ts +0 -443
  462. package/dist/src/run-records.js +44 -678
  463. package/dist/src/run-temp.d.ts +12 -0
  464. package/dist/src/run-temp.js +50 -0
  465. package/dist/src/runtime.d.ts +11 -227
  466. package/dist/src/runtime.js +0 -127
  467. package/dist/src/sandbox-child.js +0 -1
  468. package/dist/src/save-lock.d.ts +0 -12
  469. package/dist/src/save-lock.js +0 -28
  470. package/dist/src/screen-selection.d.ts +0 -23
  471. package/dist/src/screen-selection.js +0 -22
  472. package/dist/src/secret-redaction.d.ts +0 -12
  473. package/dist/src/secret-redaction.js +0 -45
  474. package/dist/src/session-changes.d.ts +4 -80
  475. package/dist/src/session-changes.js +25 -130
  476. package/dist/src/session-goal.d.ts +129 -0
  477. package/dist/src/session-goal.js +230 -0
  478. package/dist/src/session-monitor.d.ts +1 -10
  479. package/dist/src/session-monitor.js +2 -39
  480. package/dist/src/session-reaper.d.ts +0 -15
  481. package/dist/src/session-reaper.js +0 -14
  482. package/dist/src/session-skills.d.ts +15 -44
  483. package/dist/src/session-skills.js +75 -60
  484. package/dist/src/session-summary.d.ts +0 -26
  485. package/dist/src/session-summary.js +0 -32
  486. package/dist/src/session-title.d.ts +0 -1
  487. package/dist/src/session-title.js +0 -2
  488. package/dist/src/sessions.d.ts +2 -120
  489. package/dist/src/sessions.js +9 -178
  490. package/dist/src/shell.d.ts +3 -20
  491. package/dist/src/shell.js +8 -72
  492. package/dist/src/side-panel.d.ts +5 -99
  493. package/dist/src/side-panel.js +76 -133
  494. package/dist/src/sign-in-detection.d.ts +43 -0
  495. package/dist/src/sign-in-detection.js +154 -0
  496. package/dist/src/sign-in-offer.d.ts +17 -0
  497. package/dist/src/sign-in-offer.js +103 -0
  498. package/dist/src/skill-commands.d.ts +0 -1
  499. package/dist/src/skill-commands.js +1 -4
  500. package/dist/src/skills/bundled/control-browser/SKILL.md +1 -1
  501. package/dist/src/skills/bundled/graph-manager/SKILL.md +22 -4
  502. package/dist/src/skills/bundled/provenance.json +2 -2
  503. package/dist/src/skills/invocable-skills.d.ts +0 -18
  504. package/dist/src/skills/invocable-skills.js +0 -17
  505. package/dist/src/skills/project-skills.d.ts +0 -1
  506. package/dist/src/skills/project-skills.js +0 -3
  507. package/dist/src/skills/prompt-skills.js +0 -14
  508. package/dist/src/skills/runtime-skills.d.ts +1 -8
  509. package/dist/src/skills/runtime-skills.js +8 -32
  510. package/dist/src/stall-recovery.d.ts +0 -25
  511. package/dist/src/stall-recovery.js +0 -38
  512. package/dist/src/stray-files.d.ts +5 -0
  513. package/dist/src/stray-files.js +65 -0
  514. package/dist/src/structured-step.d.ts +0 -22
  515. package/dist/src/structured-step.js +0 -22
  516. package/dist/src/subagent-run.d.ts +0 -163
  517. package/dist/src/subagent-run.js +10 -374
  518. package/dist/src/subagent-tree.d.ts +32 -0
  519. package/dist/src/subagent-tree.js +128 -0
  520. package/dist/src/telemetry/aggregate.d.ts +0 -37
  521. package/dist/src/telemetry/aggregate.js +0 -40
  522. package/dist/src/telemetry/capture-core.d.ts +0 -87
  523. package/dist/src/telemetry/capture-core.js +0 -67
  524. package/dist/src/telemetry/capture.d.ts +0 -1
  525. package/dist/src/telemetry/capture.js +0 -3
  526. package/dist/src/telemetry/classify.d.ts +0 -58
  527. package/dist/src/telemetry/classify.js +0 -60
  528. package/dist/src/telemetry/decision-capture.d.ts +0 -1
  529. package/dist/src/telemetry/emitter.d.ts +0 -30
  530. package/dist/src/telemetry/emitter.js +1 -62
  531. package/dist/src/telemetry/gate.d.ts +0 -29
  532. package/dist/src/telemetry/gate.js +0 -22
  533. package/dist/src/telemetry/identity.d.ts +0 -13
  534. package/dist/src/telemetry/identity.js +0 -29
  535. package/dist/src/telemetry/outbox.d.ts +0 -64
  536. package/dist/src/telemetry/outbox.js +1 -108
  537. package/dist/src/telemetry/schema.d.ts +1 -49
  538. package/dist/src/telemetry/schema.js +1 -82
  539. package/dist/src/telemetry/state.d.ts +0 -53
  540. package/dist/src/telemetry/state.js +0 -64
  541. package/dist/src/telemetry/surface.d.ts +0 -31
  542. package/dist/src/telemetry/surface.js +1 -39
  543. package/dist/src/telemetry/uploader.d.ts +0 -11
  544. package/dist/src/telemetry/uploader.js +0 -46
  545. package/dist/src/telemetry/uploads.d.ts +0 -11
  546. package/dist/src/telemetry/uploads.js +0 -11
  547. package/dist/src/telemetry/version.d.ts +0 -4
  548. package/dist/src/telemetry/version.js +0 -14
  549. package/dist/src/telemetry/workspace.d.ts +0 -7
  550. package/dist/src/telemetry/workspace.js +1 -22
  551. package/dist/src/terminal-ui.d.ts +15 -524
  552. package/dist/src/terminal-ui.js +80 -876
  553. package/dist/src/test-edit-guard.d.ts +74 -0
  554. package/dist/src/test-edit-guard.js +269 -0
  555. package/dist/src/text.d.ts +0 -1
  556. package/dist/src/text.js +0 -1
  557. package/dist/src/time-limit.d.ts +0 -36
  558. package/dist/src/time-limit.js +0 -37
  559. package/dist/src/timers.d.ts +0 -161
  560. package/dist/src/timers.js +0 -126
  561. package/dist/src/todos.d.ts +0 -1
  562. package/dist/src/todos.js +0 -3
  563. package/dist/src/token-rate.d.ts +0 -94
  564. package/dist/src/token-rate.js +0 -154
  565. package/dist/src/tool-activity.d.ts +0 -6
  566. package/dist/src/tool-activity.js +1 -8
  567. package/dist/src/tool-availability.d.ts +0 -13
  568. package/dist/src/tool-availability.js +1 -14
  569. package/dist/src/tool-call-clock.d.ts +1 -4
  570. package/dist/src/tool-call-clock.js +0 -13
  571. package/dist/src/tool-catalog.d.ts +0 -11
  572. package/dist/src/tool-catalog.js +0 -23
  573. package/dist/src/tool-dependencies.d.ts +0 -5
  574. package/dist/src/tool-dependencies.js +0 -4
  575. package/dist/src/tool-diff-stats.d.ts +0 -23
  576. package/dist/src/tool-diff-stats.js +0 -49
  577. package/dist/src/tool-errors.d.ts +9 -7
  578. package/dist/src/tool-errors.js +13 -6
  579. package/dist/src/tool-failure-tracker.d.ts +3 -11
  580. package/dist/src/tool-failure-tracker.js +19 -27
  581. package/dist/src/tool-names.d.ts +0 -16
  582. package/dist/src/tool-names.js +0 -12
  583. package/dist/src/tool-profile.d.ts +6 -0
  584. package/dist/src/tool-profile.js +23 -0
  585. package/dist/src/tool-result-filter.d.ts +0 -19
  586. package/dist/src/tool-result-filter.js +1 -66
  587. package/dist/src/tool-schema-compatibility.d.ts +1 -2
  588. package/dist/src/tool-schema-compatibility.js +2 -7
  589. package/dist/src/tools.d.ts +0 -4
  590. package/dist/src/tools.js +11 -114
  591. package/dist/src/trajectory.d.ts +0 -13
  592. package/dist/src/trajectory.js +23 -79
  593. package/dist/src/transcript.d.ts +1 -37
  594. package/dist/src/transcript.js +11 -61
  595. package/dist/src/types.d.ts +2 -167
  596. package/dist/src/untracked-file-removal.d.ts +3 -0
  597. package/dist/src/untracked-file-removal.js +105 -0
  598. package/dist/src/update-check.d.ts +0 -14
  599. package/dist/src/update-check.js +2 -16
  600. package/dist/src/update.d.ts +4 -29
  601. package/dist/src/update.js +20 -98
  602. package/dist/src/usage-analytics.d.ts +17 -17
  603. package/dist/src/usage-analytics.js +168 -209
  604. package/dist/src/web-search.d.ts +0 -23
  605. package/dist/src/web-search.js +1 -27
  606. package/dist/src/widgets.d.ts +0 -25
  607. package/dist/src/widgets.js +0 -24
  608. package/dist/src/workspace-facts.d.ts +0 -12
  609. package/dist/src/workspace-facts.js +1 -18
  610. package/dist/src/workspace-git-metadata.d.ts +4 -5
  611. package/dist/src/workspace-git-metadata.js +37 -9
  612. package/dist/src/workspace-instructions.d.ts +3 -15
  613. package/dist/src/workspace-instructions.js +7 -20
  614. package/package.json +2 -2
@@ -1,34 +1,9 @@
1
- /**
2
- * Local Usage dashboard accounting from trajectory JSONL.
3
- *
4
- * Model usage is provider-reported and counted once per model.response. A Jev bypass has no
5
- * provider usage, so the call it skipped is estimated: its input is the context the runtime would
6
- * have sent, journaled on the event as `estimatedInputTokens` (anchored to the node's last measured
7
- * request), and its output is the mean output of that run/node's measured calls so far. A bypass
8
- * journaled before the runtime recorded the context falls back to the node's last measured input,
9
- * and one with neither is counted as unestimated. This is a counterfactual estimate, never billed
10
- * or actually saved tokens.
11
- * Measured tokens are also totalled for the run/nodes Jev was enabled on — the ones that journaled
12
- * a `decision.*` event — so the avoidance rate is taken over usage Jev could have affected and a
13
- * node without Jev does not dilute it.
14
- * The reader never sends model prompts, response text, or tool arguments to the desktop —
15
- * neither over the wire nor into the cache file, which holds captured token counts and
16
- * identifiers only.
17
- *
18
- * Records carry the provider id each call ran under, and rows aggregate by that id: it is the
19
- * identity every call shares, and grouping by the display name instead would merge two providers
20
- * a user named the same. The name is resolved at read time for presentation only — the id is
21
- * immutable and the name is what a user renames, so every row a provider contributed shows its
22
- * current name. A provider the configuration no longer holds falls back to the raw journaled id
23
- * — the only name that record still has.
24
- */
25
1
  import { createReadStream } from 'node:fs';
26
2
  import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
27
3
  import { join } from 'node:path';
28
4
  import { loadConfig } from './config.js';
29
5
  import { providerLabel } from './providers.js';
30
6
  const nonnegative = (value) => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : undefined;
31
- /** File facts, unlike token counts, carry sub-millisecond precision on most filesystems. */
32
7
  const finiteNonnegative = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : undefined;
33
8
  const asObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value) ? value : undefined;
34
9
  const dateOf = (time) => {
@@ -40,7 +15,6 @@ const dateOf = (time) => {
40
15
  const keyOf = (providerId, model) => JSON.stringify([providerId, model]);
41
16
  const emptyModel = (providerId, provider, model) => ({ providerId, provider, model, inputTokens: 0, outputTokens: 0, cacheRead: 0, savedInputEstimate: 0, savedOutputEstimate: 0, bypassedTurns: 0 });
42
17
  const emptyDay = (date) => ({ date, inputTokens: 0, outputTokens: 0, cacheRead: 0, savedInputEstimate: 0, savedOutputEstimate: 0, bypassedTurns: 0, models: [] });
43
- /** A streaming collector, also usable with synthetic records in regression tests. */
44
18
  export function createUsageCollector(windowDays, now = new Date(), resolveProvider = provider => provider) {
45
19
  if (![7, 30, 365].includes(windowDays))
46
20
  throw new Error('windowDays must be 7, 30, or 365');
@@ -50,9 +24,7 @@ export function createUsageCollector(windowDays, now = new Date(), resolveProvid
50
24
  const days = new Map();
51
25
  const models = new Map();
52
26
  const baselines = new Map();
53
- /** Every measured call's output per run/node, including those before the window: the output estimate. */
54
27
  const outputs = new Map();
55
- /** In-window measured tokens per run/node, and the run/nodes a `decision.*` event named. */
56
28
  const nodeTokens = new Map();
57
29
  const jevNodes = new Set();
58
30
  let unestimatedTurns = 0;
@@ -67,8 +39,6 @@ export function createUsageCollector(windowDays, now = new Date(), resolveProvid
67
39
  return day;
68
40
  };
69
41
  const modelFor = (collection, providerId, model) => {
70
- // Rows aggregate by the journaled id, never by the resolved name: two providers a user named
71
- // the same must stay two rows, and a row's tokens must not move when a rename lands.
72
42
  const key = keyOf(providerId, model);
73
43
  let value = collection.get(key);
74
44
  if (!value) {
@@ -89,12 +59,11 @@ export function createUsageCollector(windowDays, now = new Date(), resolveProvid
89
59
  const input = nonnegative(usage?.inputTokens);
90
60
  const output = nonnegative(usage?.outputTokens);
91
61
  if (input === undefined || output === undefined)
92
- return; // missing data is not measured zero
62
+ return;
93
63
  const providerId = typeof item.providerId === 'string' ? item.providerId : 'unknown';
94
64
  const model = typeof item.model === 'string' && item.model ? item.model : 'unknown';
95
65
  const cache = Math.min(input, nonnegative(usage?.cacheRead) ?? 0);
96
66
  const identity = JSON.stringify([item.runId, item.nodeId]);
97
- // Keep an observed baseline even when it predates the displayed date range.
98
67
  if (typeof item.runId === 'string' && typeof item.nodeId === 'string') {
99
68
  baselines.set(identity, { providerId, model, inputTokens: input });
100
69
  const history = outputs.get(identity) ?? { tokens: 0, calls: 0 };
@@ -111,7 +80,6 @@ export function createUsageCollector(windowDays, now = new Date(), resolveProvid
111
80
  target.cacheRead += cache;
112
81
  }
113
82
  if (typeof item.runId === 'string' && typeof item.nodeId === 'string') {
114
- // Kept per node rather than added to a Jev total now: the marker may arrive after the tokens.
115
83
  const node = nodeTokens.get(identity) ?? { inputTokens: 0, outputTokens: 0 };
116
84
  node.inputTokens += input;
117
85
  node.outputTokens += output;
@@ -125,12 +93,8 @@ export function createUsageCollector(windowDays, now = new Date(), resolveProvid
125
93
  if (typeof event?.type !== 'string' || !event.type.startsWith('decision.'))
126
94
  return;
127
95
  const data = asObject(event.data);
128
- // Only a node whose decision manager is on journals these, so one of them is the evidence that
129
- // Jev was enabled there. Run-level notices carry no node and mark nothing.
130
96
  if (typeof data?.runId === 'string' && typeof data.nodeId === 'string')
131
97
  jevNodes.add(JSON.stringify([data.runId, data.nodeId]));
132
- // An answered Jev request. Counted apart from the bypasses: a preflight answer picks tools and
133
- // skills for a call that is still made, so most Jev calls skip nothing.
134
98
  if (event.type === 'decision.completed') {
135
99
  if (date >= cutoff)
136
100
  jevCalls++;
@@ -145,7 +109,6 @@ export function createUsageCollector(windowDays, now = new Date(), resolveProvid
145
109
  return;
146
110
  const day = dayFor(date);
147
111
  day.row.bypassedTurns++;
148
- // The context the skipped call would have sent; a zero reading is no reading.
149
112
  const journaled = nonnegative(data?.estimatedInputTokens) || undefined;
150
113
  const input = journaled ?? baseline?.inputTokens;
151
114
  if (input === undefined) {
@@ -155,8 +118,6 @@ export function createUsageCollector(windowDays, now = new Date(), resolveProvid
155
118
  if (journaled !== undefined)
156
119
  contextEstimatedTurns++;
157
120
  const output = history?.calls ? Math.round(history.tokens / history.calls) : 0;
158
- // The call would have gone where the event says; an older event names neither, and the node's
159
- // last measured call is the best record of where it was going.
160
121
  const providerId = typeof data?.providerId === 'string' && data.providerId ? data.providerId : baseline?.providerId ?? 'unknown';
161
122
  const model = typeof data?.model === 'string' && data.model ? data.model : baseline?.model ?? 'unknown';
162
123
  day.row.savedInputEstimate += input;
@@ -204,25 +165,21 @@ const MAX_FILE_BYTES = 256 * 1024 * 1024;
204
165
  const MAX_LINE_BYTES = 8 * 1024 * 1024;
205
166
  const MAX_CACHE_BYTES = 64 * 1024 * 1024;
206
167
  const MAX_CACHE_RECORDS = 500_000;
207
- /** Retained records across the whole home; past this, further files are excluded, not read. */
208
168
  const MAX_CACHE_RECORDS_TOTAL = 250_000;
209
- /**
210
- * 2: files are re-read once to capture the `j` markers version 1 never stored.
211
- * 3: and again for the bypass accounting fields (`i`, `p`, `m` on `b`) version 2 never stored.
212
- * 4: and again for the answered Jev calls (`d`), which earlier versions folded into one `j` marker.
213
- */
214
- const CACHE_VERSION = 4;
169
+ // Run journals keep only answered Jev calls, most of which a trajectory already holds, so they get a smaller budget of
170
+ // their own and can never crowd out the trajectories that carry measured tokens.
171
+ const MAX_RUN_JOURNAL_RECORDS_TOTAL = 50_000;
172
+ const CACHE_VERSION = 5;
215
173
  const NAME_PATTERN = /^[a-zA-Z0-9_-]{1,64}\.jsonl$/;
216
- /** The largest window the collector accepts; files older than its bound are never replayed. */
174
+ const CACHE_KEY_PATTERN = /^(?:runs\/)?[a-zA-Z0-9_-]{1,64}\.jsonl$/;
217
175
  const MAX_WINDOW_DAYS = 365;
218
- /**
219
- * Capture the fields the collector reads from one parsed trajectory record, or undefined when the
220
- * collector would ignore the record entirely. The guards mirror `createUsageCollector`'s early
221
- * returns — the same usage validity and string-typed identifiers — so replaying captured records
222
- * into the collector behaves exactly like feeding it the original lines. The display window is
223
- * deliberately NOT applied here: a record captured today must survive until tomorrow's window
224
- * slides forward, so only the time validity is checked at capture time.
225
- */
176
+ // Every engine build shares one home, and a build only understands its own cache format. Each format has its own file, so
177
+ // an installed release and a development build never discard and rewrite each other's cache.
178
+ const CACHE_FILE = `usage-cache.v${CACHE_VERSION}.json`;
179
+ const LEGACY_CACHE_FILE = 'usage-cache.json';
180
+ const OTHER_CACHE_FILE_PATTERN = /^usage-cache(?:\.v\d{1,6})?\.json$/;
181
+ const UNUSED_CACHE_MS = 30 * 86400000;
182
+ export const usageCacheTiming = { checkpointMs: 5000 };
226
183
  const slimOf = (record) => {
227
184
  const item = asObject(record);
228
185
  if (!item)
@@ -234,7 +191,7 @@ const slimOf = (record) => {
234
191
  const input = nonnegative(usage?.inputTokens);
235
192
  const output = nonnegative(usage?.outputTokens);
236
193
  if (input === undefined || output === undefined)
237
- return undefined; // missing data is not measured zero
194
+ return undefined;
238
195
  const cacheRead = nonnegative(usage?.cacheRead);
239
196
  return {
240
197
  k: 'r', t: item.time,
@@ -253,7 +210,7 @@ const slimOf = (record) => {
253
210
  return undefined;
254
211
  const data = asObject(event.data);
255
212
  if (event.type === 'decision.completed')
256
- return { k: 'd', t: item.time, ...(typeof data?.runId === 'string' ? { r: data.runId } : {}), ...(typeof data?.nodeId === 'string' ? { n: data.nodeId } : {}) };
213
+ return { k: 'd', t: item.time, ...(typeof data?.requestId === 'string' && data.requestId ? { q: data.requestId } : {}), ...(typeof data?.runId === 'string' ? { r: data.runId } : {}), ...(typeof data?.nodeId === 'string' ? { n: data.nodeId } : {}) };
257
214
  if (event.type !== 'decision.turn_bypassed')
258
215
  return typeof data?.runId === 'string' && typeof data.nodeId === 'string'
259
216
  ? { k: 'j', t: item.time, r: data.runId, n: data.nodeId } : undefined;
@@ -267,7 +224,6 @@ const slimOf = (record) => {
267
224
  ...(typeof data?.model === 'string' && data.model ? { m: data.model } : {}),
268
225
  };
269
226
  };
270
- /** Rebuild a collector-shaped record from its slim capture; absent optional fields stay absent. */
271
227
  const replayOf = (slim) => slim.k === 'r'
272
228
  ? {
273
229
  type: 'model.response', time: slim.t, providerId: slim.p, model: slim.m,
@@ -276,7 +232,6 @@ const replayOf = (slim) => slim.k === 'r'
276
232
  }
277
233
  : {
278
234
  type: 'runtime.event', time: slim.t,
279
- // The collector reads a marker's run and node only, so one type stands in for all of them.
280
235
  event: { type: slim.k === 'b' ? 'decision.turn_bypassed' : slim.k === 'd' ? 'decision.completed' : 'decision.ready', data: {
281
236
  ...(slim.r === undefined ? {} : { runId: slim.r }), ...(slim.n === undefined ? {} : { nodeId: slim.n }),
282
237
  ...(slim.k === 'b' && slim.i !== undefined ? { estimatedInputTokens: slim.i } : {}),
@@ -284,11 +239,6 @@ const replayOf = (slim) => slim.k === 'r'
284
239
  ...(slim.k === 'b' && slim.m !== undefined ? { model: slim.m } : {}),
285
240
  } },
286
241
  };
287
- /**
288
- * Validate one loaded slim record. Anything malformed is dropped rather than trusted: the cache is
289
- * an optimization, and a wrong entry would be a wrong dashboard. The collector re-derives and
290
- * re-normalizes every field at replay time, so validation only has to keep the shape honest.
291
- */
292
242
  const validSlim = (value) => {
293
243
  const item = asObject(value);
294
244
  if (!item || typeof item.t !== 'string' || !Number.isFinite(new Date(item.t).getTime()))
@@ -303,7 +253,7 @@ const validSlim = (value) => {
303
253
  if (item.k === 'j')
304
254
  return typeof item.r === 'string' && typeof item.n === 'string';
305
255
  if (item.k === 'd')
306
- return true;
256
+ return item.q === undefined || typeof item.q === 'string';
307
257
  if (item.k !== 'r')
308
258
  return false;
309
259
  if (typeof item.p !== 'string' || typeof item.m !== 'string')
@@ -312,28 +262,12 @@ const validSlim = (value) => {
312
262
  return false;
313
263
  return item.c === undefined || nonnegative(item.c) !== undefined;
314
264
  };
315
- /**
316
- * Read one file region and capture its complete newline-terminated lines.
317
- *
318
- * `limit` caps how many records the region may retain; the caller passes the room its per-file
319
- * and per-home budgets leave. Once a record would exceed it, retention stops and the rest of the
320
- * region is abandoned (`overLimit`, with the stream destroyed early) — the caller excludes or
321
- * defers the file, so there is no reason to materialize records that will be thrown away.
322
- *
323
- * A trailing partial line — a write interrupted before its newline — is left unread: the offset
324
- * stops before it and the next scan reads it once the append completes it, instead of parsing a
325
- * truncated half-record and flagging every future scan incomplete over a line that was never
326
- * whole.
327
- */
328
- async function readRegion(file, start, limit) {
265
+ async function readRegion(file, start, limit, parse = slimOf) {
329
266
  const records = [];
330
267
  let bad = false;
331
268
  let failed = false;
332
269
  let consumed = 0;
333
270
  let overLimit = false;
334
- // Lines are assembled from a chunk list rather than re-copying an accumulated carry buffer, so
335
- // the scan stays linear in file size. A line still unfinished past the record-size bound is
336
- // rejected early and the rest of it is discarded without ever being assembled.
337
271
  const pending = [];
338
272
  let pendingBytes = 0;
339
273
  let skipping = false;
@@ -381,10 +315,9 @@ async function readRegion(file, start, limit) {
381
315
  if (!text.trim())
382
316
  return;
383
317
  try {
384
- const slim = slimOf(JSON.parse(text));
318
+ const slim = parse(JSON.parse(text));
385
319
  if (slim) {
386
320
  if (slim.k === 'j') {
387
- // A node makes a decision per turn; one marker says all the collector needs to know.
388
321
  const identity = JSON.stringify([slim.r, slim.n]);
389
322
  if (marked.has(identity))
390
323
  return;
@@ -402,18 +335,12 @@ async function readRegion(file, start, limit) {
402
335
  bad = true;
403
336
  }
404
337
  };
405
- // Created inside the try: an out-of-range start must degrade this one read to `failed` (the
406
- // entry is dropped and fully re-read next time), not fail the whole request.
407
338
  let stream;
408
339
  try {
409
340
  stream = createReadStream(file, { start });
410
341
  for await (const chunk of stream) {
411
342
  const buffer = chunk;
412
343
  if (skipping) {
413
- // Mid-rejection, a newline-free chunk is discarded as it arrives; only the terminating
414
- // newline ends the line, and searching the whole accumulated pile per chunk would be
415
- // quadratic in the rejected line's length. Whatever follows the newline in the same
416
- // chunk is ordinary content again and falls through to the line processing below.
417
344
  const index = buffer.indexOf(0x0A);
418
345
  if (index === -1) {
419
346
  skippedBytes += buffer.length;
@@ -445,8 +372,6 @@ async function readRegion(file, start, limit) {
445
372
  }
446
373
  break;
447
374
  }
448
- // The newline byte belongs to the consumption, or the next search finds this same
449
- // newline at index 0 and takes nothing — an infinite spin on a single-line file.
450
375
  const line = take(index + 1);
451
376
  consumed += index + 1;
452
377
  absorb(line.subarray(0, line.length - 1));
@@ -462,40 +387,42 @@ async function readRegion(file, start, limit) {
462
387
  }
463
388
  return { records, consumed, bad, failed, overLimit };
464
389
  }
465
- const cachePath = (directory) => join(directory, 'usage-cache.json');
466
- /**
467
- * Load the persisted cache. Every entry is re-validated; anything malformed, and the whole file
468
- * when it is unreadable or from another cache generation, is discarded and rebuilt from the
469
- * trajectories.
470
- */
390
+ const cachePath = (directory) => join(directory, CACHE_FILE);
471
391
  async function loadCache(directory) {
392
+ // The unversioned file is where earlier builds kept this format; it is read once so an upgrade does not rescan.
393
+ for (const name of [CACHE_FILE, LEGACY_CACHE_FILE]) {
394
+ const entries = await loadCacheFile(join(directory, name));
395
+ if (entries)
396
+ return { entries, adopted: name !== CACHE_FILE };
397
+ }
398
+ return { entries: new Map(), adopted: false };
399
+ }
400
+ async function loadCacheFile(file) {
472
401
  const entries = new Map();
473
402
  let stored;
474
403
  try {
475
- stored = await readFile(cachePath(directory), 'utf8');
404
+ stored = await readFile(file, 'utf8');
476
405
  }
477
406
  catch {
478
- return entries;
407
+ return undefined;
479
408
  }
480
409
  let parsed;
481
410
  try {
482
411
  parsed = JSON.parse(stored);
483
412
  }
484
413
  catch {
485
- return entries;
414
+ return undefined;
486
415
  }
487
416
  const root = asObject(parsed);
488
417
  const files = asObject(root?.files);
489
418
  if (root?.version !== CACHE_VERSION || !files)
490
- return entries;
419
+ return undefined;
491
420
  for (const [name, raw] of Object.entries(files)) {
492
- if (!NAME_PATTERN.test(name))
421
+ if (!CACHE_KEY_PATTERN.test(name))
493
422
  continue;
494
423
  const entry = asObject(raw);
495
424
  if (!entry)
496
425
  continue;
497
- // Byte facts (size, offset) index real reads and must stay whole numbers; only the modified
498
- // time legitimately carries the filesystem's sub-millisecond precision.
499
426
  const mtimeMs = finiteNonnegative(entry.m);
500
427
  const size = nonnegative(entry.s);
501
428
  const offset = nonnegative(entry.o);
@@ -509,7 +436,6 @@ async function loadCache(directory) {
509
436
  }
510
437
  return entries;
511
438
  }
512
- /** Publish the cache with one rename, so a reader never sees a truncated file (activity.json's rule). */
513
439
  async function persistCache(directory, entries) {
514
440
  const files = {};
515
441
  for (const [name, entry] of entries) {
@@ -517,7 +443,7 @@ async function persistCache(directory, entries) {
517
443
  }
518
444
  const serialized = JSON.stringify({ version: CACHE_VERSION, files });
519
445
  if (Buffer.byteLength(serialized) > MAX_CACHE_BYTES)
520
- return; // Keep the memory cache; skip the disk copy.
446
+ return;
521
447
  const target = cachePath(directory);
522
448
  const staging = `${target}.tmp-${process.pid}`;
523
449
  try {
@@ -525,75 +451,106 @@ async function persistCache(directory, entries) {
525
451
  await writeFile(staging, serialized, { mode: 0o600 });
526
452
  await rename(staging, target);
527
453
  }
528
- catch { /* Cache persistence is advisory; the next scan rebuilds it. */ }
454
+ catch { }
529
455
  finally {
530
456
  await rm(staging, { force: true }).catch(() => { });
531
457
  }
532
458
  }
459
+ async function removeUnusedCaches(directory) {
460
+ const names = await readdir(directory).catch(() => []);
461
+ const unused = Date.now() - UNUSED_CACHE_MS;
462
+ for (const name of names) {
463
+ if (name === CACHE_FILE || !OTHER_CACHE_FILE_PATTERN.test(name))
464
+ continue;
465
+ const file = join(directory, name);
466
+ const info = await stat(file).catch(() => undefined);
467
+ if (info?.isFile() && info.mtimeMs < unused)
468
+ await rm(file, { force: true }).catch(() => { });
469
+ }
470
+ }
471
+ // Standalone helper runs (subagent-run.ts) journal their Jev calls only to runs/<runId>.jsonl. App-server runs write
472
+ // every event to both runs/ and trajectories/, so from runs/ only answered calls carrying a requestId are kept, and
473
+ // readUsageAnalytics counts each requestId once. Tokens and bypasses stay trajectory-only: bypass estimates need the
474
+ // measured baselines that only trajectories hold.
475
+ const slimRunOf = (record) => {
476
+ const item = asObject(record);
477
+ if (!item || item.type !== 'decision.completed')
478
+ return undefined;
479
+ if (typeof item.time !== 'string' || !Number.isFinite(new Date(item.time).getTime()))
480
+ return undefined;
481
+ const data = asObject(item.data);
482
+ if (typeof data?.requestId !== 'string' || !data.requestId)
483
+ return undefined;
484
+ return {
485
+ k: 'd', t: item.time, q: data.requestId,
486
+ ...(typeof data.runId === 'string' ? { r: data.runId } : {}),
487
+ ...(typeof data.nodeId === 'string' ? { n: data.nodeId } : {}),
488
+ };
489
+ };
490
+ const SOURCES = [
491
+ { folder: 'trajectories', prefix: '', budget: MAX_CACHE_RECORDS_TOTAL, parse: slimOf },
492
+ { folder: 'runs', prefix: 'runs/', budget: MAX_RUN_JOURNAL_RECORDS_TOTAL, parse: slimRunOf },
493
+ ];
494
+ const ownedBy = (source, key) => source.prefix ? key.startsWith(source.prefix) : !key.includes('/');
533
495
  const cacheStates = new Map();
534
496
  const refreshJobs = new Map();
535
- /**
536
- * Bring one directory's cache up to date: stat every trajectory file, reuse captured records for
537
- * unchanged files, read only the appended tail of grown files, and fully re-read anything new,
538
- * shrunk, or previously excluded. Concurrent callers share one refresh.
539
- *
540
- * The refresh is bounded to what a full 365-day scan already spent: files whose modified time
541
- * predates the largest window are neither read nor retained, because no request can replay them,
542
- * and the home's total retained records stop at a budget, past which further files are deferred —
543
- * set aside without being read, reported incomplete, and retried next time the budget has room.
544
- * Files inside those bounds are captured regardless of the request's display window — the window
545
- * is applied per request — so switching between 7, 30 and 365 days never re-reads anything.
546
- */
547
497
  async function refreshUsageCache(directory) {
548
498
  const running = refreshJobs.get(directory);
549
499
  if (running)
550
500
  return running;
551
501
  const job = (async () => {
552
502
  let cache = cacheStates.get(directory);
503
+ let dirty = false;
553
504
  if (!cache) {
554
- cache = { entries: await loadCache(directory), deferred: new Map() };
505
+ const loaded = await loadCache(directory);
506
+ cache = { entries: loaded.entries, deferred: new Map() };
555
507
  cacheStates.set(directory, cache);
508
+ if (loaded.adopted)
509
+ await persistCache(directory, cache.entries);
510
+ void removeUnusedCaches(directory);
556
511
  }
557
512
  const { entries, deferred } = cache;
558
- const folder = join(directory, 'trajectories');
559
- const names = await readdir(folder).catch((error) => {
560
- if (error.code === 'ENOENT')
561
- return null;
562
- throw error;
563
- });
564
- let failedRead = false;
565
- let dirty = false;
566
- if (names === null) {
567
- if (entries.size) {
568
- entries.clear();
569
- dirty = true;
570
- }
571
- deferred.clear();
572
- }
573
- else {
574
- const present = new Set(names.filter(name => NAME_PATTERN.test(name)));
575
- // Entries for files that vanished from the directory outright never reach the stat loop
576
- // below, so they are pruned here; otherwise a deleted session would keep contributing its
577
- // captured records forever.
578
- for (const name of [...entries.keys()]) {
579
- if (!present.has(name)) {
513
+ const failed = new Map();
514
+ let saved = Date.now();
515
+ const agedOut = Date.now() - (MAX_WINDOW_DAYS + 1) * 86400000;
516
+ for (const source of SOURCES) {
517
+ // Each folder has its own record budget, so records cached from one never defer the other's files.
518
+ let retained = 0;
519
+ for (const [name, entry] of entries)
520
+ if (ownedBy(source, name))
521
+ retained += entry.records?.length ?? 0;
522
+ const folder = join(directory, source.folder);
523
+ const names = await readdir(folder).catch((error) => {
524
+ if (error.code === 'ENOENT')
525
+ return [];
526
+ throw error;
527
+ });
528
+ const present = new Set(names.filter(name => NAME_PATTERN.test(name)).map(name => source.prefix + name));
529
+ for (const [name, entry] of [...entries]) {
530
+ if (ownedBy(source, name) && !present.has(name)) {
531
+ retained -= entry.records?.length ?? 0;
580
532
  entries.delete(name);
581
533
  dirty = true;
582
534
  }
583
535
  }
584
536
  for (const name of [...deferred.keys()]) {
585
- if (!present.has(name))
537
+ if (ownedBy(source, name) && !present.has(name))
586
538
  deferred.delete(name);
587
539
  }
588
- let retained = 0;
589
- for (const entry of entries.values())
590
- retained += entry.records?.length ?? 0;
591
- // The aged-out bound is the largest window's, so a file pruned here is one no replay would
592
- // ever visit; pruning it cannot change any answer.
593
- const agedOut = Date.now() - (MAX_WINDOW_DAYS + 1) * 86400000;
594
- for (const name of [...present].sort()) {
595
- const file = join(folder, name);
596
- const info = await stat(file).catch(() => undefined);
540
+ const found = [];
541
+ for (const name of present) {
542
+ const file = join(folder, name.slice(source.prefix.length));
543
+ found.push({ name, file, info: await stat(file).catch(() => undefined) });
544
+ }
545
+ // Newest first, so that when the record budget runs out it is the oldest history that waits.
546
+ found.sort((a, b) => (b.info?.mtimeMs ?? 0) - (a.info?.mtimeMs ?? 0) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
547
+ for (const { name, file, info } of found) {
548
+ // A long scan saves what it has read so far, so closing the app part-way does not start it over.
549
+ if (dirty && Date.now() - saved >= usageCacheTiming.checkpointMs) {
550
+ await persistCache(directory, entries);
551
+ dirty = false;
552
+ saved = Date.now();
553
+ }
597
554
  let entry = entries.get(name);
598
555
  if (!info || !info.isFile() || info.mtimeMs < agedOut) {
599
556
  if (entry) {
@@ -612,7 +569,6 @@ async function refreshUsageCache(directory) {
612
569
  dirty = true;
613
570
  }
614
571
  };
615
- // A deferral holds no records and is retried whenever the budget has room again.
616
572
  const defer = () => {
617
573
  if (entry) {
618
574
  retained -= entry.records?.length ?? 0;
@@ -634,37 +590,24 @@ async function refreshUsageCache(directory) {
634
590
  }
635
591
  if (entry && entry.mtimeMs === info.mtimeMs && entry.size === info.size)
636
592
  continue;
637
- // A file with nothing cached is only read when the budget has room: past it, further
638
- // files are set aside without being read at all, the answer's incomplete flag covers
639
- // them, the heap stays bounded, and the next refresh retries them. A file that already
640
- // has records is settled after the read, when the actual delta is known — refusing it
641
- // here would throw away good history over an append that may add none.
642
- if (entry === undefined && retained >= MAX_CACHE_RECORDS_TOTAL) {
593
+ if (entry === undefined && retained >= source.budget) {
643
594
  defer();
644
595
  continue;
645
596
  }
646
- // Only a strictly grown file is tailed; a same-size rewrite with a fresh mtime gets a full
647
- // read, because bytes before the offset cannot be trusted to match what was captured.
648
597
  const prior = entry && entry.records !== null ? entry.records : null;
649
598
  const priorCount = prior?.length ?? 0;
650
599
  const tail = prior !== null && info.size > entry.size;
651
- // The reader stops retaining at the tighter of the two budgets, so a file far over either
652
- // bound never materializes records that would be discarded. A tail keeps its prior
653
- // records, so they consume both budgets; a full read replaces them, so they are credited
654
- // back — otherwise a truncate could refuse a replacement that fits comfortably.
655
600
  const perFileRoom = tail ? MAX_CACHE_RECORDS - priorCount : MAX_CACHE_RECORDS;
656
- const budgetRoom = MAX_CACHE_RECORDS_TOTAL - retained + (tail ? 0 : priorCount);
657
- const region = await readRegion(file, tail ? entry.offset : 0, Math.max(0, Math.min(perFileRoom, budgetRoom)));
601
+ const budgetRoom = source.budget - retained + (tail ? 0 : priorCount);
602
+ const region = await readRegion(file, tail ? entry.offset : 0, Math.max(0, Math.min(perFileRoom, budgetRoom)), source.parse);
658
603
  if (region.failed) {
659
- failedRead = true;
604
+ failed.set(name, info.mtimeMs);
660
605
  retained -= priorCount;
661
606
  entries.delete(name);
662
607
  dirty = true;
663
608
  continue;
664
609
  }
665
610
  if (region.overLimit) {
666
- // The file exceeded the tighter bound: an over-large file is intrinsically excluded,
667
- // while a budget-bound one is set aside and retried when the budget has room.
668
611
  if (perFileRoom <= budgetRoom)
669
612
  exclude();
670
613
  else
@@ -672,12 +615,11 @@ async function refreshUsageCache(directory) {
672
615
  continue;
673
616
  }
674
617
  const records = tail ? prior.concat(region.records) : region.records;
675
- // The limit makes these unreachable; they stay as the final guard on the retained shape.
676
618
  if (records.length > MAX_CACHE_RECORDS) {
677
619
  exclude();
678
620
  continue;
679
621
  }
680
- if (retained - priorCount + records.length > MAX_CACHE_RECORDS_TOTAL) {
622
+ if (retained - priorCount + records.length > source.budget) {
681
623
  defer();
682
624
  continue;
683
625
  }
@@ -698,22 +640,13 @@ async function refreshUsageCache(directory) {
698
640
  }
699
641
  if (dirty)
700
642
  await persistCache(directory, entries);
701
- return { entries, deferred, failedRead };
643
+ return { entries, deferred, failed };
702
644
  })();
703
645
  refreshJobs.set(directory, job);
704
646
  void job.catch(() => { }).finally(() => { if (refreshJobs.get(directory) === job)
705
647
  refreshJobs.delete(directory); });
706
648
  return job;
707
649
  }
708
- /**
709
- * Map each journaled provider id to the name a person reads for it.
710
- *
711
- * The configuration is the one place the current display names live, and grouping rows by the
712
- * resolved name is what makes a rename cover past runs: the journals keep the immutable id, and
713
- * this is the only translation between the two. A configuration that cannot be read keeps every
714
- * id as itself — usage accounting must not fail over a broken config, and an id the configuration
715
- * no longer holds falls back to the raw journaled string anyway.
716
- */
717
650
  async function providerNameResolver(directory) {
718
651
  try {
719
652
  const config = await loadConfig(directory);
@@ -724,37 +657,63 @@ async function providerNameResolver(directory) {
724
657
  return providerId => providerId;
725
658
  }
726
659
  }
727
- /**
728
- * Local-only read, bounded by modified time, file count and record size, served from a per-file
729
- * cache of the records the collector consumes. The replay visits the captured records in the same
730
- * order a full read would — files sorted by name, lines in file order — so the answer is the one
731
- * the equivalent full scan produces, whatever the cache happened to hold.
732
- */
733
- export async function readUsageAnalytics(directory, windowDays) {
734
- const { entries, deferred, failedRead } = await refreshUsageCache(directory);
735
- const collector = createUsageCollector(windowDays, new Date(), await providerNameResolver(directory));
736
- const earliest = Date.now() - (windowDays + 1) * 86400000;
737
- let scanned = 0;
738
- let incomplete = failedRead;
739
- for (const name of [...entries.keys()].sort()) {
740
- const entry = entries.get(name);
660
+ // Chooses which cached files to replay: in-window, readable, newest first up to maxFiles per folder, replayed in name order.
661
+ // Each folder (trajectories, runs/) has its own maxFiles allowance, so run journals, which mostly mirror Jev calls a
662
+ // trajectory already holds, never push a trajectory and its measured tokens out. A file with no usage records is read but
663
+ // takes no place under the cap. Every in-window file left out is counted under the reason it was not read.
664
+ export function selectUsageFiles(entries, deferred, failed, earliest, maxFiles = MAX_FILES) {
665
+ const unread = { fileCap: 0, oversized: 0, deferred: 0, unreadable: 0 };
666
+ const folders = new Map();
667
+ const read = [];
668
+ for (const [name, entry] of entries) {
741
669
  if (entry.mtimeMs < earliest)
742
670
  continue;
743
- if (entry.records === null || scanned >= MAX_FILES) {
744
- incomplete = true;
671
+ if (entry.records === null) {
672
+ unread.oversized++;
745
673
  continue;
746
674
  }
747
- scanned++;
748
- if (entry.bad)
749
- incomplete = true;
750
- for (const slim of entry.records)
751
- collector.add(replayOf(slim));
675
+ if (entry.records.length === 0) {
676
+ read.push(name);
677
+ continue;
678
+ }
679
+ const folder = name.slice(0, name.lastIndexOf('/') + 1);
680
+ const candidates = folders.get(folder) ?? [];
681
+ candidates.push([name, entry.mtimeMs]);
682
+ folders.set(folder, candidates);
752
683
  }
753
- // Deferred files were never read this round; within a window that would have read them, the
754
- // answer says so rather than presenting their absence as an empty measurement.
755
- for (const mtimeMs of deferred.values()) {
684
+ for (const candidates of folders.values()) {
685
+ candidates.sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
686
+ unread.fileCap += Math.max(0, candidates.length - maxFiles);
687
+ for (const [name] of candidates.slice(0, maxFiles))
688
+ read.push(name);
689
+ }
690
+ for (const mtimeMs of deferred.values())
691
+ if (mtimeMs >= earliest)
692
+ unread.deferred++;
693
+ for (const mtimeMs of failed.values())
756
694
  if (mtimeMs >= earliest)
695
+ unread.unreadable++;
696
+ return { read: read.sort(), unread };
697
+ }
698
+ export async function readUsageAnalytics(directory, windowDays) {
699
+ const { entries, deferred, failed } = await refreshUsageCache(directory);
700
+ const collector = createUsageCollector(windowDays, new Date(), await providerNameResolver(directory));
701
+ const earliest = Date.now() - (windowDays + 1) * 86400000;
702
+ const { read, unread } = selectUsageFiles(entries, deferred, failed, earliest);
703
+ let incomplete = failed.size > 0 || unread.fileCap + unread.oversized + unread.deferred > 0;
704
+ const answered = new Set();
705
+ for (const name of read) {
706
+ const entry = entries.get(name);
707
+ if (entry.bad)
757
708
  incomplete = true;
709
+ for (const slim of entry.records ?? []) {
710
+ if (slim.k === 'd' && slim.q !== undefined) {
711
+ if (answered.has(slim.q))
712
+ continue;
713
+ answered.add(slim.q);
714
+ }
715
+ collector.add(replayOf(slim));
716
+ }
758
717
  }
759
- return collector.finish(scanned, incomplete);
718
+ return { ...collector.finish(read.length, incomplete), unread };
760
719
  }