mixdog 1.4.3 → 1.4.5

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 (281) hide show
  1. package/package.json +1 -2
  2. package/src/cli.mjs +5 -1
  3. package/src/defaults/shipped-history.json +30 -2
  4. package/src/defaults/skills/browser-use/SKILL.md +148 -273
  5. package/src/defaults/skills/browser-use/references/devtools.md +14 -0
  6. package/src/defaults/skills/browser-use/references/page-flows.md +66 -0
  7. package/src/defaults/skills/browser-use/references/sign-in.md +70 -0
  8. package/src/defaults/skills/code-tidy/SKILL.md +60 -84
  9. package/src/defaults/skills/code-tidy/references/agent-cleanup.md +27 -2
  10. package/src/defaults/skills/computer-use/SKILL.md +145 -229
  11. package/src/defaults/skills/computer-use/references/flows.md +35 -0
  12. package/src/defaults/skills/computer-use/references/operations.md +22 -0
  13. package/src/defaults/skills/computer-use/references/platforms.md +12 -0
  14. package/src/defaults/skills/docx/SKILL.md +23 -31
  15. package/src/defaults/skills/docx/references/native-authoring.md +26 -3
  16. package/src/defaults/skills/docx/references/redlining.md +15 -0
  17. package/src/defaults/skills/image/SKILL.md +37 -77
  18. package/src/defaults/skills/image/references/prompt-forms.md +51 -0
  19. package/src/defaults/skills/image/references/text.md +6 -0
  20. package/src/defaults/skills/local-provider/SKILL.md +75 -105
  21. package/src/defaults/skills/local-provider/references/lifecycle.md +14 -0
  22. package/src/defaults/skills/local-provider/references/maintenance.md +19 -0
  23. package/src/defaults/skills/local-provider/references/recovery.md +30 -0
  24. package/src/defaults/skills/memory-management/SKILL.md +22 -21
  25. package/src/defaults/skills/pdf/SKILL.md +21 -23
  26. package/src/defaults/skills/pdf/references/blocks.md +20 -0
  27. package/src/defaults/skills/pdf/references/forms.md +12 -0
  28. package/src/defaults/skills/pdf/references/html.md +2 -2
  29. package/src/defaults/skills/pdf/references/ocr.md +7 -0
  30. package/src/defaults/skills/pdf/references/security.md +8 -0
  31. package/src/defaults/skills/pptx/SKILL.md +30 -109
  32. package/src/defaults/skills/pptx/references/audit.md +45 -0
  33. package/src/defaults/skills/pptx/references/brief-critique.md +47 -0
  34. package/src/defaults/skills/pptx/references/editing.md +24 -0
  35. package/src/defaults/skills/pptx/references/html.md +1 -1
  36. package/src/defaults/skills/pptx/references/script.md +33 -0
  37. package/src/defaults/skills/setup/SKILL.md +34 -102
  38. package/src/defaults/skills/setup/references/actions.md +73 -14
  39. package/src/defaults/skills/setup/references/surfaces.md +23 -8
  40. package/src/defaults/skills/setup-guide/SKILL.md +28 -115
  41. package/src/defaults/skills/setup-guide/references/features.md +36 -0
  42. package/src/defaults/skills/setup-guide/references/glossary.md +38 -0
  43. package/src/defaults/skills/setup-guide/references/workflow-models.md +62 -0
  44. package/src/defaults/skills/test-suite/SKILL.md +15 -127
  45. package/src/defaults/skills/test-suite/references/case-management.md +17 -9
  46. package/src/defaults/skills/test-suite/references/diagnose.md +23 -0
  47. package/src/defaults/skills/test-suite/references/prune.md +27 -0
  48. package/src/defaults/skills/test-suite/references/write.md +44 -0
  49. package/src/defaults/skills/video/SKILL.md +28 -42
  50. package/src/defaults/skills/video/references/prompting.md +31 -0
  51. package/src/defaults/skills/visualize/SKILL.md +73 -107
  52. package/src/defaults/skills/visualize/references/craft.md +1 -2
  53. package/src/defaults/skills/visualize/references/feedback.md +36 -0
  54. package/src/defaults/skills/visualize/references/showing.md +13 -0
  55. package/src/defaults/skills/xlsx/SKILL.md +23 -50
  56. package/src/defaults/skills/xlsx/references/editing.md +15 -0
  57. package/src/defaults/skills/xlsx/references/model-conventions.md +1 -1
  58. package/src/defaults/skills/xlsx/references/recalculation.md +37 -0
  59. package/src/defaults/skills/xlsx/references/report-design.md +18 -2
  60. package/src/lib/keychain-cjs.cjs +22 -1
  61. package/src/lib/rules-builder.cjs +1 -1
  62. package/src/rules/agent/AGENT.md +3 -0
  63. package/src/rules/agent-mode/MIXY.md +5 -3
  64. package/src/rules/lead/LEAD.md +3 -1
  65. package/src/runtime/agent/orchestrator/agent-runtime/cache-strategy.mjs +8 -30
  66. package/src/runtime/agent/orchestrator/agent-runtime/title-completion.mjs +5 -0
  67. package/src/runtime/agent/orchestrator/mcp/oauth-request.mjs +21 -2
  68. package/src/runtime/agent/orchestrator/providers/account-pool.mjs +3 -0
  69. package/src/runtime/agent/orchestrator/providers/admission-scheduler.mjs +4 -3
  70. package/src/runtime/agent/orchestrator/providers/anthropic-api-request.mjs +11 -1
  71. package/src/runtime/agent/orchestrator/providers/anthropic-api-transport.mjs +2 -0
  72. package/src/runtime/agent/orchestrator/providers/anthropic-message-threads.mjs +583 -0
  73. package/src/runtime/agent/orchestrator/providers/anthropic-oauth-client-version.mjs +1 -1
  74. package/src/runtime/agent/orchestrator/providers/anthropic-oauth.mjs +81 -19
  75. package/src/runtime/agent/orchestrator/providers/anthropic-sse-turn/turn-events/content-block-start.mjs +4 -0
  76. package/src/runtime/agent/orchestrator/providers/anthropic-sse-turn/turn-outcome.mjs +2 -0
  77. package/src/runtime/agent/orchestrator/providers/anthropic-sse.mjs +180 -13
  78. package/src/runtime/agent/orchestrator/providers/anthropic-turn-result.mjs +2 -0
  79. package/src/runtime/agent/orchestrator/providers/anthropic.mjs +58 -6
  80. package/src/runtime/agent/orchestrator/providers/cursor-wire.mjs +4 -2
  81. package/src/runtime/agent/orchestrator/providers/custom-provider-config.mjs +121 -9
  82. package/src/runtime/agent/orchestrator/providers/custom-provider.mjs +4 -1
  83. package/src/runtime/agent/orchestrator/providers/gemini-error-contract.mjs +6 -6
  84. package/src/runtime/agent/orchestrator/providers/gemini-model-redirect.mjs +44 -0
  85. package/src/runtime/agent/orchestrator/providers/gemini-reasoning-relay.mjs +5 -0
  86. package/src/runtime/agent/orchestrator/providers/gemini-request-body.mjs +34 -5
  87. package/src/runtime/agent/orchestrator/providers/gemini-response.mjs +10 -0
  88. package/src/runtime/agent/orchestrator/providers/gemini-schema.mjs +11 -6
  89. package/src/runtime/agent/orchestrator/providers/gemini-sdk-request.mjs +4 -100
  90. package/src/runtime/agent/orchestrator/providers/gemini-stream.mjs +3 -227
  91. package/src/runtime/agent/orchestrator/providers/gemini.mjs +226 -118
  92. package/src/runtime/agent/orchestrator/providers/lib/anthropic-request-utils.mjs +11 -1
  93. package/src/runtime/agent/orchestrator/providers/lib/claude-bridge-schema.mjs +15 -3
  94. package/src/runtime/agent/orchestrator/providers/lib/gemini-model-catalog.mjs +2 -1
  95. package/src/runtime/agent/orchestrator/providers/lib/generation-window.mjs +9 -0
  96. package/src/runtime/agent/orchestrator/providers/openai-compat-chat-send.mjs +9 -1
  97. package/src/runtime/agent/orchestrator/providers/openai-compat-chat-stream/stream-result.mjs +2 -0
  98. package/src/runtime/agent/orchestrator/providers/openai-compat-chat-stream/stream-state.mjs +5 -0
  99. package/src/runtime/agent/orchestrator/providers/openai-compat-options.mjs +3 -0
  100. package/src/runtime/agent/orchestrator/providers/openai-compat-response-normalization.mjs +2 -0
  101. package/src/runtime/agent/orchestrator/providers/openai-compat-responses.mjs +10 -1
  102. package/src/runtime/agent/orchestrator/providers/openai-direct-request.mjs +5 -28
  103. package/src/runtime/agent/orchestrator/providers/openai-http-sse-response-state/response-events.mjs +3 -0
  104. package/src/runtime/agent/orchestrator/providers/openai-http-sse-response-state/response-outcome.mjs +2 -0
  105. package/src/runtime/agent/orchestrator/providers/openai-ws-pool.mjs +50 -28
  106. package/src/runtime/agent/orchestrator/providers/openai-ws-response-state.mjs +9 -2
  107. package/src/runtime/agent/orchestrator/providers/openai-ws-stream/socket-handlers.mjs +14 -10
  108. package/src/runtime/agent/orchestrator/providers/openai-ws.mjs +1 -3
  109. package/src/runtime/agent/orchestrator/providers/openrouter-provider-routing.mjs +28 -0
  110. package/src/runtime/agent/orchestrator/providers/openrouter.mjs +4 -1
  111. package/src/runtime/agent/orchestrator/providers/registry.mjs +30 -25
  112. package/src/runtime/agent/orchestrator/providers/stream-json-pool/worker-slots/slot-spawn.mjs +8 -3
  113. package/src/runtime/agent/orchestrator/providers/vertex-ai-maas.mjs +2 -2
  114. package/src/runtime/agent/orchestrator/runtime-core/ask-user-result.mjs +66 -0
  115. package/src/runtime/agent/orchestrator/runtime-core/config-helpers.mjs +4 -5
  116. package/src/runtime/agent/orchestrator/runtime-core/goal-tool-defs.mjs +2 -2
  117. package/src/runtime/agent/orchestrator/runtime-core/provider-request-tools.mjs +15 -8
  118. package/src/runtime/agent/orchestrator/runtime-core/session-title.mjs +1 -0
  119. package/src/runtime/agent/orchestrator/runtime-core/todo-list.mjs +9 -32
  120. package/src/runtime/agent/orchestrator/runtime-core/todo-tool-defs.mjs +4 -5
  121. package/src/runtime/agent/orchestrator/session/compact/summary-route.mjs +1 -3
  122. package/src/runtime/agent/orchestrator/session/loop/compact-policy.mjs +5 -2
  123. package/src/runtime/agent/orchestrator/session/loop/no-tool-turn/todo-check.mjs +2 -17
  124. package/src/runtime/agent/orchestrator/session/loop/usage.mjs +7 -3
  125. package/src/runtime/agent/orchestrator/session/manager/ask-turn-settle.mjs +9 -2
  126. package/src/runtime/agent/orchestrator/session/manager/runtime-liveness.mjs +8 -1
  127. package/src/runtime/agent/orchestrator/session/manager/session-close.mjs +26 -13
  128. package/src/runtime/agent/orchestrator/session/manager/session-lifecycle.mjs +4 -5
  129. package/src/runtime/agent/orchestrator/session/manager/usage-metrics.mjs +16 -5
  130. package/src/runtime/agent/orchestrator/session/store/lifecycle-barriers.mjs +22 -1
  131. package/src/runtime/agent/orchestrator/session/store/live-state.mjs +4 -0
  132. package/src/runtime/agent/orchestrator/session/store.mjs +12 -6
  133. package/src/runtime/agent/orchestrator/tools/builtin/cache-layers.mjs +11 -9
  134. package/src/runtime/agent/orchestrator/tools/builtin/grep-formatting.mjs +2 -8
  135. package/src/runtime/agent/orchestrator/tools/builtin/lib/search-input-helpers.mjs +1 -9
  136. package/src/runtime/agent/orchestrator/tools/builtin/lib/shell-output-silence.mjs +64 -0
  137. package/src/runtime/agent/orchestrator/tools/builtin/lib/shell-task-output.mjs +20 -6
  138. package/src/runtime/agent/orchestrator/tools/builtin/local-search-telemetry.mjs +2 -2
  139. package/src/runtime/agent/orchestrator/tools/builtin/native-search-client.mjs +1 -1
  140. package/src/runtime/agent/orchestrator/tools/builtin/runtime-capabilities.mjs +12 -3
  141. package/src/runtime/agent/orchestrator/tools/builtin/task-tool.mjs +12 -1
  142. package/src/runtime/agent/orchestrator/tools/code-graph/ast-calls.mjs +9 -2
  143. package/src/runtime/agent/orchestrator/tools/code-graph/build.mjs +137 -7
  144. package/src/runtime/agent/orchestrator/tools/code-graph/calls-cache.mjs +43 -14
  145. package/src/runtime/agent/orchestrator/tools/code-graph/calls-sidecar-worker.mjs +33 -0
  146. package/src/runtime/agent/orchestrator/tools/code-graph/disk-cache.mjs +146 -11
  147. package/src/runtime/agent/orchestrator/tools/code-graph/dispatch.mjs +31 -24
  148. package/src/runtime/agent/orchestrator/tools/code-graph/graph-binary/spawn-jsonl.mjs +172 -25
  149. package/src/runtime/agent/orchestrator/tools/code-graph/graph-model.mjs +100 -2
  150. package/src/runtime/agent/orchestrator/tools/code-graph/main-thread-slice.mjs +18 -0
  151. package/src/runtime/agent/orchestrator/tools/code-graph/mask-pool.mjs +219 -0
  152. package/src/runtime/agent/orchestrator/tools/code-graph/mask-worker.mjs +12 -0
  153. package/src/runtime/agent/orchestrator/tools/code-graph/memory-cache.mjs +6 -1
  154. package/src/runtime/agent/orchestrator/tools/code-graph/modes/calls.mjs +9 -2
  155. package/src/runtime/agent/orchestrator/tools/code-graph/modes/dependents.mjs +5 -1
  156. package/src/runtime/agent/orchestrator/tools/code-graph/modes/structure.mjs +8 -2
  157. package/src/runtime/agent/orchestrator/tools/code-graph/modes/symbols.mjs +14 -11
  158. package/src/runtime/agent/orchestrator/tools/code-graph/search-references.mjs +41 -18
  159. package/src/runtime/agent/orchestrator/tools/code-graph/search.mjs +391 -83
  160. package/src/runtime/agent/orchestrator/tools/code-graph/symbol-index.mjs +109 -9
  161. package/src/runtime/agent/orchestrator/tools/code-graph/text-mask.mjs +73 -29
  162. package/src/runtime/agent/orchestrator/tools/code-graph-prewarm-worker.mjs +6 -5
  163. package/src/runtime/agent/orchestrator/tools/lib/native-spawn-client.mjs +7 -3
  164. package/src/runtime/channels/lib/crash-log.mjs +21 -1
  165. package/src/runtime/channels/lib/routines-db.mjs +35 -0
  166. package/src/runtime/channels/lib/schedules-db.mjs +4 -0
  167. package/src/runtime/channels/lib/webhooks-db.mjs +4 -0
  168. package/src/runtime/channels/lib/worker-main.mjs +8 -25
  169. package/src/runtime/computer-bridge/action-schema.mjs +2 -2
  170. package/src/runtime/computer-bridge/core-actions.mjs +16 -2
  171. package/src/runtime/computer-bridge/result-canonical.mjs +6 -0
  172. package/src/runtime/media/jobs.mjs +5 -0
  173. package/src/runtime/media/store.mjs +146 -3
  174. package/src/runtime/memory/tool-defs.mjs +1 -1
  175. package/src/runtime/office/core/office-write-ledger.mjs +2 -2
  176. package/src/runtime/shared/agent-reap-state.mjs +19 -0
  177. package/src/runtime/shared/desktop-notice-host.d.mts +13 -0
  178. package/src/runtime/shared/desktop-notice-host.mjs +87 -0
  179. package/src/runtime/shared/file-lock.mjs +2 -2
  180. package/src/runtime/shared/heap-cap.mjs +9 -11
  181. package/src/runtime/shared/llm/http-agent.mjs +11 -1
  182. package/src/runtime/shared/llm/system-proxy.mjs +100 -25
  183. package/src/runtime/shared/llm/usage-accounting.mjs +121 -19
  184. package/src/runtime/shared/llm/usage-context.mjs +3 -3
  185. package/src/runtime/shared/llm/usage-ledger-quota.mjs +2 -0
  186. package/src/runtime/shared/llm/usage-ledger-requests.mjs +190 -0
  187. package/src/runtime/shared/llm/usage-ledger-rollup.mjs +7 -3
  188. package/src/runtime/shared/llm/usage-ledger-worker.mjs +2 -0
  189. package/src/runtime/shared/llm/usage-ledger.mjs +69 -1
  190. package/src/runtime/shared/llm/usage-request-status.mjs +48 -0
  191. package/src/runtime/shared/provider-accounts.mjs +22 -0
  192. package/src/runtime/shared/provider-auth-binding.mjs +2 -2
  193. package/src/runtime/shared/resource-admission.mjs +2 -2
  194. package/src/runtime/shared/root-async-context.mjs +51 -0
  195. package/src/runtime/shared/run-child.mjs +18 -1
  196. package/src/runtime/shared/slow-span.mjs +2 -2
  197. package/src/runtime/shared/tool-execution-owner.mjs +2 -2
  198. package/src/runtime/shared/tool-result-summary.mjs +1 -1
  199. package/src/runtime/shared/tool-work-units.mjs +2 -0
  200. package/src/runtime/shared/turn-snapshot-store.mjs +75 -18
  201. package/src/runtime/shared/turn-worktree-snapshot.mjs +38 -8
  202. package/src/runtime/shared/user-cwd.mjs +2 -2
  203. package/src/runtime/shared/worker-requests.mjs +3 -1
  204. package/src/runtime/shared/worktrees/lock.mjs +2 -2
  205. package/src/runtime/visualize-bridge/client.mjs +26 -4
  206. package/src/runtime/visualize-bridge/tool-defs.mjs +46 -5
  207. package/src/session-runtime/agent-home-delivery.mjs +76 -8
  208. package/src/session-runtime/agent-task-sessions.mjs +63 -22
  209. package/src/session-runtime/boot/apis.mjs +16 -0
  210. package/src/session-runtime/boot/begin.mjs +2 -0
  211. package/src/session-runtime/boot/tools.mjs +8 -2
  212. package/src/session-runtime/internal-tool-executor/ask-user-tool.mjs +3 -58
  213. package/src/session-runtime/media-api.mjs +15 -0
  214. package/src/session-runtime/model-route/route-persist.mjs +12 -4
  215. package/src/session-runtime/provider-models/row-hydration.mjs +2 -0
  216. package/src/session-runtime/provider-readiness.mjs +10 -2
  217. package/src/session-runtime/routine-owner.mjs +9 -3
  218. package/src/session-runtime/runtime-core.mjs +35 -8
  219. package/src/session-runtime/runtime-review-api.mjs +3 -1
  220. package/src/session-runtime/services/agent-task-status.mjs +29 -1
  221. package/src/session-runtime/services/agent-tool/close-flow.mjs +6 -1
  222. package/src/session-runtime/services/agent-tool/execute-resume.mjs +94 -0
  223. package/src/session-runtime/services/agent-tool/execute-send.mjs +77 -27
  224. package/src/session-runtime/services/agent-tool/execute-spawn.mjs +28 -7
  225. package/src/session-runtime/services/agent-tool/execute.mjs +6 -1
  226. package/src/session-runtime/services/agent-tool/helpers.mjs +11 -0
  227. package/src/session-runtime/services/agent-tool/job-views/job-render.mjs +13 -10
  228. package/src/session-runtime/services/agent-tool/job-views/session-progress.mjs +3 -0
  229. package/src/session-runtime/services/agent-tool/render.mjs +8 -1
  230. package/src/session-runtime/services/agent-tool/send-flow.mjs +1 -0
  231. package/src/session-runtime/services/agent-tool/spawn-flow/spawn-prep.mjs +6 -0
  232. package/src/session-runtime/services/agent-tool/spawn-flow/spawn-run.mjs +2 -1
  233. package/src/session-runtime/services/agent-tool/tag-registry/reaped-sessions.mjs +82 -0
  234. package/src/session-runtime/services/agent-tool/tag-registry/record-facts-worker.mjs +36 -0
  235. package/src/session-runtime/services/agent-tool/tag-registry.mjs +4 -0
  236. package/src/session-runtime/services/agent-tool/terminal-reaper/row-helpers.mjs +29 -0
  237. package/src/session-runtime/services/agent-tool/terminal-reaper/scan-admission.mjs +11 -3
  238. package/src/session-runtime/services/agent-tool/terminal-reaper/stale-rows.mjs +19 -8
  239. package/src/session-runtime/services/agent-tool/terminal-reaper.mjs +5 -2
  240. package/src/session-runtime/services/agent-tool/worker-index.mjs +1 -0
  241. package/src/session-runtime/services/channel-admin.mjs +44 -3
  242. package/src/session-runtime/services/daemon-crash-capture/daemon-exit-record.mjs +2 -2
  243. package/src/session-runtime/services/plugin-admin.mjs +46 -7
  244. package/src/session-runtime/services/provider-admin.mjs +5 -0
  245. package/src/session-runtime/session-create.mjs +5 -0
  246. package/src/session-runtime/setup-tool/agent-settings-actions.mjs +1 -1
  247. package/src/session-runtime/setup-tool/executor.mjs +1 -1
  248. package/src/session-runtime/setup-tool/extended-actions.mjs +1 -1
  249. package/src/session-runtime/setup-tool/settings-contract.mjs +4 -2
  250. package/src/session-runtime/usage-stats-api.mjs +120 -1
  251. package/src/session-runtime/workflow-agents-api/agent-connectors.mjs +36 -29
  252. package/src/session-runtime/workflow-agents-api/agent-editor.mjs +183 -23
  253. package/src/session-runtime/workspace/admission-gate.mjs +25 -7
  254. package/src/session-runtime/workspace/transitions.mjs +4 -0
  255. package/src/standalone/daemon-log.mjs +28 -4
  256. package/src/standalone/daemon-owner.mjs +41 -7
  257. package/src/standalone/daemon-work-gate.mjs +139 -0
  258. package/src/standalone/daemon.mjs +80 -11
  259. package/src/standalone/desktop-service-registry.mjs +11 -4
  260. package/src/standalone/session-client.mjs +57 -5
  261. package/src/standalone/session-protocol.mjs +6 -0
  262. package/src/standalone/session-service/agent-tree/agent-cancel.mjs +17 -1
  263. package/src/standalone/session-service/agent-tree.mjs +7 -0
  264. package/src/standalone/session-service/retention/busy.mjs +3 -2
  265. package/src/standalone/session-service/session-calls/session-turns.mjs +12 -5
  266. package/src/standalone/session-transport/call-route.mjs +10 -4
  267. package/src/standalone/session-transport/routes.mjs +21 -0
  268. package/src/standalone/session-transport/stack.mjs +3 -1
  269. package/src/standalone/session-transport.mjs +5 -0
  270. package/src/tui/app/model-picker/model-footer.mjs +1 -1
  271. package/src/tui/app/model-picker/provider-models-list.mjs +7 -3
  272. package/src/tui/app/model-picker/route-selection/fast-parameters.mjs +23 -3
  273. package/src/tui/app/model-picker/route-selection.mjs +1 -0
  274. package/src/tui/dist/index.mjs +536 -484
  275. package/src/tui/session/session-api/integrations.mjs +3 -0
  276. package/src/tui/session/session-api/media.mjs +5 -0
  277. package/src/tui/session/tool-approval-settle.mjs +24 -4
  278. package/src/tui/session/tool-approval.mjs +35 -10
  279. package/src/tui/session/turn.mjs +13 -0
  280. package/src/runtime/agent/orchestrator/providers/gemini-sdk-stream/cancellation.mjs +0 -71
  281. package/src/runtime/agent/orchestrator/providers/gemini-sdk-stream/reader.mjs +0 -108
@@ -1,6 +1,7 @@
1
1
  import { randomUUID } from 'node:crypto';
2
2
  import { getUsageLedger, makeUsageRecord } from './usage-ledger.mjs';
3
- import { isNotedUsage, withUsageContext } from './usage-context.mjs';
3
+ import { currentUsageContext, isNotedUsage, withUsageContext } from './usage-context.mjs';
4
+ import { servedModelSubstituted, usageRequestStatus } from './usage-request-status.mjs';
4
5
  import { ACCOUNT_PROVIDERS } from '../provider-accounts.mjs';
5
6
  import { currentProviderAccountId } from '../provider-auth-binding.mjs';
6
7
 
@@ -8,7 +9,31 @@ import { currentProviderAccountId } from '../provider-auth-binding.mjs';
8
9
  // fallback, catalog retry) runs its own accounting and its result or error
9
10
  // then returns through the enclosing send, which must not write it again.
10
11
  const recorded = new WeakSet();
12
+ // The accounting identity of the send whose options carry it; object spread
13
+ // keeps it on every option set a provider derives for its own re-send.
14
+ const SEND_IDENTITY = Symbol('usageSendIdentity');
15
+ // Providers whose answering stream's generation window (`generationMs`,
16
+ // providers/lib/generation-window.mjs) starts when generation does, read
17
+ // from the stream code: Anthropic opens every content block, a thinking
18
+ // block included even when its text is omitted; OpenAI's Responses streams
19
+ // add every output item, an encrypted reasoning item included, as it starts;
20
+ // the local llama.cpp server streams every generated token, reasoning
21
+ // included. Any other provider may reason before its first streamed token
22
+ // (hidden reasoning on chat-compat gateways, Gemini thought summaries,
23
+ // Cursor, Devin, compat Responses gateways), so a speed from it could be
24
+ // inflated: it records no first-output time.
25
+ const TIMED_PROVIDERS = new Set(['anthropic', 'anthropic-oauth', 'openai', 'openai-oauth', 'mixdog-local']);
26
+
27
+ /** The answering stream's own first-output time within the request, when its
28
+ * provider is timed: never an abandoned attempt's or a retry backoff's. */
29
+ function firstOutputMs(provider, result, streamMs) {
30
+ const generationMs = Number(result?.generationMs);
31
+ if (!TIMED_PROVIDERS.has(provider) || !Number.isFinite(generationMs)) return null;
32
+ if (generationMs < 0 || generationMs > streamMs) return null;
33
+ return streamMs - generationMs;
34
+ }
11
35
  const UNMEASURED_USAGE = Object.freeze({ inputTokens: null, inputTokensKnown: false });
36
+ const FAILED_USAGE = Object.freeze({ inputTokens: 0, outputTokens: 0, costUsd: 0 });
12
37
  const hasTokens = (usage) =>
13
38
  ['inputTokens', 'outputTokens', 'cachedTokens', 'cacheWriteTokens'].some((key) => Number(usage?.[key]) > 0);
14
39
  // One rule for every reported usage object: tokens, or a provider-reported
@@ -41,17 +66,32 @@ export async function accountProviderSend(provider, instance, send, model, opts
41
66
  } catch (error) {
42
67
  openingError = error;
43
68
  }
69
+ // A provider's own re-send on another model (an overload or retired-model
70
+ // fallback) still answers the model the outer send was asked for. Only a
71
+ // re-send built from the outer send's own options is one: any other send
72
+ // that merely runs inside that async context (a tool or agent started
73
+ // while the outer stream was live) asks for its own model.
74
+ const outer = currentUsageContext();
75
+ // A session on a configured route-fallback hop (route-fallback.mjs) asks
76
+ // for the route the user configured, the primary one kept at the first hop,
77
+ // however many hops the chain has taken: that is what its answer replaces.
78
+ const primary = opts.session?.provider === provider ? opts.session.routeFallbackPrimary : null;
79
+ const routeHop = Boolean(primary?.provider && (primary.provider !== provider || primary.model !== model));
44
80
  const identity = {
45
81
  provider,
46
- requestedModel: model,
82
+ requestedProvider: routeHop ? primary.provider : provider,
83
+ requestedModel: routeHop
84
+ ? primary.model
85
+ : (outer && opts[SEND_IDENTITY] === outer && outer.requestedModel) || model,
47
86
  sessionId,
48
87
  sourceType,
49
88
  inputTokensInclusive,
50
89
  };
90
+ const sendOpts = { ...opts, [SEND_IDENTITY]: identity };
51
91
  // `timed`: only a send's completed result owns the request's wall time. An
52
92
  // abandoned attempt or a failure shares that clock with the final attempt,
53
93
  // so timing it too would count the same seconds twice and understate speed.
54
- const record = async (result, id, owner, timed) => {
94
+ const record = async (result, id, owner, timed, outcome = null) => {
55
95
  // A completed call that reported no usage is still a call: it is recorded
56
96
  // as unmeasured (unknown input, unpriced) instead of vanishing from totals.
57
97
  if (!result?.usage && !(timed && result && typeof result === 'object')) return;
@@ -64,6 +104,23 @@ export async function accountProviderSend(provider, instance, send, model, opts
64
104
  recorded.add(owner);
65
105
  const usage = result.usage || UNMEASURED_USAGE;
66
106
  const durationMs = timed ? Date.now() - startedAt : 0;
107
+ const servedModel = result.model || model;
108
+ const attempts = outcome ? outcome.attempts : 1;
109
+ let status = 'completed';
110
+ if (outcome?.cancelled) status = 'cancelled';
111
+ else if (outcome)
112
+ status = usageRequestStatus({
113
+ failed: outcome.failed,
114
+ attempts,
115
+ substituted:
116
+ routeHop ||
117
+ servedModelSubstituted({
118
+ provider,
119
+ requestedModel: identity.requestedModel,
120
+ servedModel,
121
+ fallback: Boolean(result.providerMetadata?.anthropicFallbacks?.length),
122
+ }),
123
+ });
67
124
  // The one measured request time: the loop's speed figures read it, so
68
125
  // the composer and the ledger show the same span.
69
126
  if (timed && result && typeof result === 'object') {
@@ -73,8 +130,16 @@ export async function accountProviderSend(provider, instance, send, model, opts
73
130
  id: result.responseId ? undefined : id,
74
131
  ts: Date.now(),
75
132
  provider,
76
- model: result.model || model,
77
- requestedModel: model,
133
+ model: servedModel,
134
+ requestedModel: outcome ? identity.requestedModel : model,
135
+ requestedProvider: outcome ? identity.requestedProvider : provider,
136
+ status,
137
+ attempts,
138
+ // Measured to the moment the stream returned, not to this write: the
139
+ // abandoned attempts' writes awaited before it are not first-output wait.
140
+ ttftMs: timed ? firstOutputMs(provider, result, streamEndedAt - startedAt) : null,
141
+ // The request log shows how long a failure took; statistics never time it.
142
+ elapsedMs: outcome?.failed ? Date.now() - startedAt : 0,
78
143
  pricingModel: result.pricingModel,
79
144
  sessionId,
80
145
  // The session this one runs for (an agent's Lead), kept with its spend.
@@ -101,34 +166,71 @@ export async function accountProviderSend(provider, instance, send, model, opts
101
166
  // SQLite write no longer runs on the event loop.
102
167
  await ledger.recordQueued(row);
103
168
  };
104
- const save = async (result, id = requestId, owner = result, timed = false) => {
169
+ const save = async (result, id = requestId, owner = result, timed = false, outcome = null) => {
105
170
  try {
106
- await record(result, id, owner, timed);
171
+ await record(result, id, owner, timed, outcome);
107
172
  } catch (error) {
108
173
  result.usageAccountingError = String(error?.message || error);
109
174
  process.stderr.write(`[usage-ledger] RECORD NOT SAVED: ${result.usageAccountingError}\n`);
110
175
  }
111
176
  };
112
- const saveAbandoned = async () => {
177
+ const saveAbandoned = async (skip = -1) => {
113
178
  for (const [index, attempt] of (identity.abandonedUsage || []).entries()) {
114
- if (recordable(attempt.usage)) await save(attempt, `${requestId}:abandoned:${index}`);
179
+ if (index !== skip && recordable(attempt.usage)) await save(attempt, `${requestId}:abandoned:${index}`);
115
180
  }
116
181
  };
117
182
  let result;
183
+ let streamEndedAt;
118
184
  try {
119
- result = await withUsageContext(identity, send);
185
+ result = await withUsageContext(identity, () => send(sendOpts));
186
+ streamEndedAt = Date.now();
120
187
  } catch (error) {
121
- await saveAbandoned();
122
- // Only provider-reported partial usage is recordable; never invent
123
- // tokens for a failed request or reinterpret an error as a success.
124
- // Usage already noted as an abandoned attempt was just recorded above.
125
- if (error?.usage) {
126
- if (!isNotedUsage(error.usage)) await save(error);
127
- } else if (recordable(error?.partialUsage) && !isNotedUsage(error.partialUsage))
128
- await save({ usage: error.partialUsage, model: error.partialModel }, requestId, error);
188
+ const abandoned = identity.abandonedUsage || [];
189
+ const cancelled = opts.signal?.aborted === true || error?.name === 'AbortError';
190
+ // Only provider-reported usage is recordable; never invent tokens for a
191
+ // failed request or reinterpret an error as a success.
192
+ const reported = error?.usage || (recordable(error?.partialUsage) ? error.partialUsage : null);
193
+ if (cancelled) {
194
+ // A user's cancel is not a request outcome: it records exactly what it
195
+ // always did, under the same ids (noted attempts as abandoned rows, the
196
+ // cancel's own unnoted usage as the request's row, response-keyed when
197
+ // it names a response), and that row is marked `cancelled` so the
198
+ // request log leaves it out.
199
+ await saveAbandoned();
200
+ const outcome = { cancelled: true, attempts: abandoned.length + 1 };
201
+ if (error?.usage) {
202
+ if (!isNotedUsage(error.usage)) await save(error, requestId, error, false, outcome);
203
+ } else if (reported && !isNotedUsage(reported)) {
204
+ await save({ usage: reported, model: error.partialModel }, requestId, error, false, outcome);
205
+ }
206
+ throw error;
207
+ }
208
+ // The failing attempt's usage may already be noted as abandoned (the
209
+ // error carries that same usage): that attempt is the request's own row,
210
+ // not an abandoned one. Any other failure without usage is an attempt of
211
+ // its own.
212
+ const last = reported
213
+ ? abandoned.findLastIndex((attempt) => attempt.source === reported || attempt.usage === reported)
214
+ : -1;
215
+ await saveAbandoned(last);
216
+ const failure = { failed: true, attempts: last >= 0 ? abandoned.length : abandoned.length + 1 };
217
+ const owner = error && typeof error === 'object' ? error : {};
218
+ if (last >= 0) {
219
+ const attempt = abandoned[last];
220
+ // Owned by the attempt, as when it was stored as abandoned: one error
221
+ // may end several concurrent sends (a shared cancel reason).
222
+ await save({ usage: attempt.usage, model: attempt.model || error?.model }, requestId, attempt, false, failure);
223
+ } else if (reported) {
224
+ // Usage noted by a nested send was recorded there.
225
+ const failed = error.usage ? error : { usage: reported, model: error.partialModel };
226
+ if (!isNotedUsage(reported)) await save(failed, requestId, owner, false, failure);
227
+ } else {
228
+ // A failure that reported nothing is still a request: zero tokens.
229
+ await save({ usage: FAILED_USAGE, model: error?.model }, requestId, owner, false, failure);
230
+ }
129
231
  throw error;
130
232
  }
131
233
  await saveAbandoned();
132
- await save(result, requestId, result, true);
234
+ await save(result, requestId, result, true, { failed: false, attempts: (identity.abandonedUsage?.length || 0) + 1 });
133
235
  return result;
134
236
  }
@@ -1,7 +1,7 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
1
+ import { runtimeAsyncLocalStorage } from '../root-async-context.mjs';
2
2
 
3
3
  // The selected route owns accounting identity, not an inner transport adapter.
4
- const context = new AsyncLocalStorage();
4
+ const context = runtimeAsyncLocalStorage();
5
5
  export const withUsageContext = (identity, send) => context.run(identity, send);
6
6
  export const currentUsageContext = () => context.getStore();
7
7
  // Each wire attempt stamps the tier it actually sent on its own send's
@@ -25,5 +25,5 @@ export const noteAbandonedUsage = (usage, model) => {
25
25
  notedUsage.add(usage);
26
26
  notedUsage.add(stored);
27
27
  identity.abandonedUsage ||= [];
28
- identity.abandonedUsage.push({ usage: stored, model });
28
+ identity.abandonedUsage.push({ usage: stored, model, source: usage });
29
29
  };
@@ -13,6 +13,7 @@
13
13
  * is an estimate: providers do not publish what a request costs the meter.
14
14
  */
15
15
  import { normalizeUsageMeasurement } from './usage-measurement.mjs';
16
+ import { NO_USAGE_COST_SOURCE } from './usage-ledger-requests.mjs';
16
17
  import { FAST_KEY, fastRouteSql, sortFastBesideModel, usageRollupDayKey } from './usage-rollup.mjs';
17
18
  import { estimateOutsideQuota, estimateQuotaValue, outsideSpans } from './quota-value-estimate.mjs';
18
19
 
@@ -401,6 +402,7 @@ function readQuotaEvents(db, { provider, account, label = '', fromMs, toMs }) {
401
402
  ${fastRouteSql('signature')} AS fast
402
403
  FROM usage_routes
403
404
  WHERE json_extract(signature,'$[0]')=? AND COALESCE(json_extract(signature,'$[8]'),'') IN (?,?)
405
+ AND json_extract(signature,'$[4]')<>'${NO_USAGE_COST_SOURCE}'
404
406
  )
405
407
  SELECT e.ts,e.day,r.model,r.rank,COUNT(*) AS turns,
406
408
  SUM(e.input) AS input,SUM(e.output) AS output,SUM(e.cache_read) AS cacheRead,
@@ -0,0 +1,190 @@
1
+ /**
2
+ * The request log: individual requests, newest first, a page at a time. The
3
+ * ledger keeps every record forever, so each page is read through an index
4
+ * ordered by time (all requests, one status, or one route) and stops after
5
+ * the page: a page of "all history" costs what a page of today does.
6
+ *
7
+ * The request facts (requested model, status, attempts) sit in
8
+ * `usage_requests`, the first-output time in `usage_request_first_output` and
9
+ * the provider asked for, when a configured fallback hop made another provider
10
+ * answer, in `usage_request_requested_provider`, beside `usage_events` and
11
+ * keyed by the record id, so the record table keeps
12
+ * the exact shape every earlier build writes: an older app still opens and
13
+ * writes a ledger this one touched. A record without a request row is a
14
+ * completed single attempt (every record from before).
15
+ */
16
+
17
+ export const REQUEST_STATUSES = ['completed', 'substituted', 'retried', 'failed'];
18
+ // `cancelled`: usage of a user's cancel, kept under its own record id (and so
19
+ // response-deduplicated) but never listed: a cancel is not a request outcome.
20
+ export const STORED_REQUEST_STATUSES = [...REQUEST_STATUSES, 'cancelled'];
21
+
22
+ // Almost every request completes: the status index holds only the others,
23
+ // and "completed" is read off the time index, where nearly every row matches.
24
+ // The model index lets a model filter find its routes without a scan.
25
+ export const REQUEST_LOG_SCHEMA = `
26
+ CREATE TABLE IF NOT EXISTS usage_requests (
27
+ id BLOB PRIMARY KEY, ts INTEGER NOT NULL, requested_model TEXT, status TEXT NOT NULL,
28
+ attempts INTEGER NOT NULL, ttft_ms REAL, elapsed_ms REAL
29
+ ) WITHOUT ROWID;
30
+ CREATE TABLE IF NOT EXISTS usage_request_first_output (
31
+ id BLOB PRIMARY KEY, ttft_ms REAL NOT NULL
32
+ ) WITHOUT ROWID;
33
+ CREATE TABLE IF NOT EXISTS usage_request_requested_provider (
34
+ id BLOB PRIMARY KEY, provider TEXT NOT NULL
35
+ ) WITHOUT ROWID;
36
+ CREATE INDEX IF NOT EXISTS usage_requests_status_time ON usage_requests(status, ts) WHERE status<>'completed';
37
+ CREATE INDEX IF NOT EXISTS usage_events_route_time ON usage_events(route, ts);
38
+ CREATE INDEX IF NOT EXISTS usage_routes_model ON usage_routes(json_extract(signature,'$[1]'));
39
+ `;
40
+
41
+ // The cost source of a failure that reported no usage: the request log lists
42
+ // it, but statistics and quota totals add no turn for it.
43
+ export const NO_USAGE_COST_SOURCE = 'none';
44
+
45
+ const DEFAULT_PAGE = 50;
46
+ const MAX_PAGE = 200;
47
+ // A provider-local retry's abandoned attempts are stored as
48
+ // `<requestId>:abandoned:N` (UTF-8 ids carry tag 0x01). They are part of
49
+ // their request, counted in its attempts, never a request of their own.
50
+ const NOT_ABANDONED = `NOT (substr(e.id,1,1)=X'01' AND instr(e.id, CAST(':abandoned:' AS BLOB))>0)`;
51
+ // The first-output time is read only from usage_request_first_output, which
52
+ // only a build that times the answering stream's first output of any kind
53
+ // writes. `usage_requests.ttft_ms` is never read: the build that added it
54
+ // (e544d62a5) timed visible text only, so a thinking or tool turn read as
55
+ // generated in a few milliseconds, and an older process may still write it.
56
+ const COLUMNS = `e.id,e.ts,e.route,e.session,e.input,e.output,e.cache_read,e.cache_write,e.cost_usd,e.duration_ms,
57
+ r.requested_model,r.status,r.attempts,r.elapsed_ms,
58
+ (SELECT f.ttft_ms FROM usage_request_first_output f WHERE f.id=e.id) AS first_output_ms,
59
+ (SELECT p.provider FROM usage_request_requested_provider p WHERE p.id=e.id) AS requested_provider`;
60
+ const COMPLETED = `(r.status IS NULL OR r.status='completed')`;
61
+ // Checked on the row the time-ordered read already joined: no other index.
62
+ const LISTED = `(r.status IS NULL OR r.status<>'cancelled')`;
63
+
64
+ function decodeCursor(cursor) {
65
+ const match = typeof cursor === 'string' ? /^(\d+):([0-9a-f]+)$/.exec(cursor) : null;
66
+ return match ? { ts: Number(match[1]), id: Buffer.from(match[2], 'hex') } : null;
67
+ }
68
+
69
+ const encodeCursor = (row) => `${row.ts}:${Buffer.from(row.id).toString('hex')}`;
70
+
71
+ const newestFirst = (a, b) => b.ts - a.ts || Buffer.compare(Buffer.from(b.id), Buffer.from(a.id));
72
+
73
+ /** The SQL of one page; `source` is the table whose index orders the read. */
74
+ export function requestPageQuery({ source, cursor = false, completed = false, routes = 0 }) {
75
+ const alias = source === 'status' ? 'r' : 'e';
76
+ const from =
77
+ source === 'status'
78
+ ? 'usage_requests r INDEXED BY usage_requests_status_time CROSS JOIN usage_events e ON e.id=r.id'
79
+ : `usage_events e INDEXED BY ${source === 'route' ? 'usage_events_route_time' : 'usage_events_time'}
80
+ LEFT JOIN usage_requests r ON r.id=e.id`;
81
+ const where = [
82
+ ...(source === 'status' ? [`r.status=? AND r.status<>'completed'`] : []),
83
+ ...(source === 'route' ? ['e.route=?'] : []),
84
+ `${alias}.ts>=?`,
85
+ `${alias}.ts<=?`,
86
+ ...(cursor ? [`(${alias}.ts<? OR ${alias}.id<?)`] : []),
87
+ NOT_ABANDONED,
88
+ ...(source !== 'status' ? [completed ? COMPLETED : LISTED] : []),
89
+ ...(routes ? [`e.route IN (${Array(routes).fill('?').join(',')})`] : []),
90
+ ];
91
+ return `SELECT ${COLUMNS} FROM ${from} WHERE ${where.join(' AND ')}
92
+ ORDER BY ${alias}.ts DESC, ${alias}.id DESC LIMIT ?`;
93
+ }
94
+
95
+ /**
96
+ * `{ fromMs, toMs, status?, model?, cursor?, limit? }` → `{ requests,
97
+ * nextCursor, models }`. `model` is the model that answered (the route's).
98
+ * `models`, every model that answered within the period, comes with the
99
+ * first page only (no cursor): it is the one read that visits every route.
100
+ */
101
+ export function readUsageRequests(db, options = {}) {
102
+ const fromMs = Number(options.fromMs) || 0;
103
+ const toMs = Number.isFinite(Number(options.toMs)) ? Number(options.toMs) : Date.now();
104
+ const requested = Math.floor(Number(options.limit));
105
+ const limit = requested > 0 ? Math.min(requested, MAX_PAGE) : DEFAULT_PAGE;
106
+ const status = REQUEST_STATUSES.includes(options.status) ? options.status : '';
107
+ const cursor = decodeCursor(options.cursor);
108
+ const range = [fromMs, cursor ? Math.min(toMs, cursor.ts) : toMs, ...(cursor ? [cursor.ts, cursor.id] : [])];
109
+ const modelRoutes = options.model
110
+ ? db
111
+ .prepare(`SELECT id FROM usage_routes WHERE json_extract(signature,'$[1]')=?`)
112
+ .all(String(options.model))
113
+ .map((route) => route.id)
114
+ : null;
115
+ const run = (shape, params) =>
116
+ db.prepare(requestPageQuery({ cursor: Boolean(cursor), ...shape })).all(...params, limit + 1);
117
+ let rows;
118
+ if (modelRoutes && !modelRoutes.length) rows = [];
119
+ else if (status && status !== 'completed') {
120
+ // Rare statuses: their own index, the model's routes filtered within it.
121
+ rows = run({ source: 'status', routes: modelRoutes?.length || 0 }, [status, ...range, ...(modelRoutes || [])]);
122
+ } else if (modelRoutes) {
123
+ // One ordered read per route of that model, merged: never every record of it.
124
+ rows = modelRoutes
125
+ .flatMap((route) => run({ source: 'route', completed: Boolean(status) }, [route, ...range]))
126
+ .sort(newestFirst)
127
+ .slice(0, limit + 1);
128
+ } else {
129
+ rows = run({ source: 'time', completed: Boolean(status) }, range);
130
+ }
131
+ const more = rows.length > limit;
132
+ rows = rows.slice(0, limit);
133
+
134
+ const routeOf = db.prepare(
135
+ `SELECT json_extract(signature,'$[0]') AS provider,json_extract(signature,'$[1]') AS model,
136
+ json_extract(signature,'$[2]') AS kind,json_extract(signature,'$[3]') AS source_type,
137
+ json_extract(signature,'$[4]') AS cost_source
138
+ FROM usage_routes WHERE id=?`
139
+ );
140
+ const sessionOf = db.prepare('SELECT value FROM usage_sessions WHERE id=?');
141
+ const ownerOf = db.prepare(
142
+ 'SELECT s.value FROM usage_session_owners o JOIN usage_sessions s ON s.id=o.owner WHERE o.session=?'
143
+ );
144
+ const routes = new Map();
145
+ const requests = rows.map((row) => {
146
+ if (!routes.has(row.route)) routes.set(row.route, routeOf.get(row.route) || {});
147
+ const route = routes.get(row.route);
148
+ const rowStatus = row.status || 'completed';
149
+ const durationMs = row.duration_ms || row.elapsed_ms || 0;
150
+ const ttftMs = row.first_output_ms;
151
+ return {
152
+ id: Buffer.from(row.id).toString('hex'),
153
+ ts: row.ts,
154
+ provider: route.provider || '',
155
+ model: route.model || '',
156
+ requestedModel: row.requested_model || route.model || '',
157
+ requestedProvider: row.requested_provider || '',
158
+ kind: route.kind || '',
159
+ sourceType: route.source_type || '',
160
+ costSource: route.cost_source || '',
161
+ sessionId: sessionOf.get(row.session)?.value || '',
162
+ ownerSessionId: ownerOf.get(row.session)?.value || '',
163
+ input: row.input,
164
+ output: row.output,
165
+ cacheRead: row.cache_read,
166
+ cacheWrite: row.cache_write,
167
+ tokens: row.input + row.output + row.cache_read + row.cache_write,
168
+ costUsd: row.cost_usd,
169
+ durationMs,
170
+ ttftMs: ttftMs ?? null,
171
+ attempts: row.attempts || 1,
172
+ status: rowStatus,
173
+ };
174
+ });
175
+
176
+ let models = null;
177
+ if (!cursor) {
178
+ // One indexed probe per route: does any request of it fall in the period?
179
+ const probe = db.prepare(
180
+ `SELECT 1 FROM usage_events e INDEXED BY usage_events_route_time LEFT JOIN usage_requests r ON r.id=e.id
181
+ WHERE e.route=? AND e.ts>=? AND e.ts<=? AND ${NOT_ABANDONED} AND ${LISTED} LIMIT 1`
182
+ );
183
+ const found = new Set();
184
+ for (const route of db.prepare(`SELECT id,json_extract(signature,'$[1]') AS model FROM usage_routes`).all()) {
185
+ if (route.model && !found.has(route.model) && probe.get(route.id, fromMs, toMs)) found.add(route.model);
186
+ }
187
+ models = [...found].sort((a, b) => a.localeCompare(b));
188
+ }
189
+ return { requests, nextCursor: more && rows.length ? encodeCursor(rows.at(-1)) : null, models };
190
+ }
@@ -23,6 +23,7 @@ import {
23
23
  usageRollupDayKey,
24
24
  } from './usage-rollup.mjs';
25
25
  import { normalizeUsageMeasurement, normalizeLegacyUsageDay } from './usage-measurement.mjs';
26
+ import { NO_USAGE_COST_SOURCE } from './usage-ledger-requests.mjs';
26
27
 
27
28
  // A detail and its terminal summary can overlap on the SAME route.
28
29
  // Another provider/model on that day is independent and must survive.
@@ -97,7 +98,7 @@ function readHourlyRows(db, { hourlyDay, fromDay, toDay, fromMs, toMs, fromTs, t
97
98
  .prepare(`
98
99
  SELECT e.ts,e.provider,e.source_type,e.input,e.output,e.cache_read,e.cache_write,e.cost_usd
99
100
  FROM events e JOIN (${best}) b USING(day,provider,model,rank)
100
- WHERE e.ts>=? AND e.ts<? ORDER BY e.ts
101
+ WHERE e.ts>=? AND e.ts<? AND e.cost_source<>'${NO_USAGE_COST_SOURCE}' ORDER BY e.ts
101
102
  `)
102
103
  .all(fromDay, toDay, fromMs == null ? start.getTime() : fromTs, toMs == null ? end.getTime() : toTs)
103
104
  .map((row) => {
@@ -132,7 +133,7 @@ function readDailyAmounts(db, { fromDay, toDay, fromMs, fromTs, toTs }) {
132
133
  SUM(e.cost_usd) AS cost_usd,SUM(e.duration_ms) AS duration_ms,
133
134
  MAX(e.origin!='live') AS imported
134
135
  FROM events e JOIN (${best}) b USING(day,provider,model,rank)
135
- WHERE e.ts>=? AND e.ts<?
136
+ WHERE e.ts>=? AND e.ts<? AND e.cost_source<>'${NO_USAGE_COST_SOURCE}'
136
137
  GROUP BY e.day,e.rank,e.provider,e.model,e.kind,e.cost_source,e.source_type
137
138
  `)
138
139
  .all(fromDay, toDay, fromTs, toTs)
@@ -178,7 +179,8 @@ function readFastAmounts(db, { fromDay, toDay, fromTs, toTs }) {
178
179
  json_extract(r.signature,'$[3]') AS source_type,
179
180
  json_extract(r.signature,'$[4]') AS cost_source,
180
181
  json_extract(r.signature,'$[7]') AS rank
181
- FROM usage_routes r WHERE ${fastRouteSql('r.signature')}`;
182
+ FROM usage_routes r WHERE ${fastRouteSql('r.signature')}
183
+ AND json_extract(r.signature,'$[4]')<>'${NO_USAGE_COST_SOURCE}'`;
182
184
  if (!db.prepare(`${fastRoutes} LIMIT 1`).get()) return [];
183
185
  return db
184
186
  .prepare(`
@@ -258,6 +260,7 @@ function readSessionAttribution(db, { fromDay, toDay, fromTs, toTs }) {
258
260
  s.value AS session_id,e.input,e.output,e.cacheRead,e.cacheWrite
259
261
  FROM grouped e JOIN usage_routes r ON r.id=e.route
260
262
  JOIN usage_sessions s ON s.id=e.session
263
+ WHERE json_extract(r.signature,'$[4]')<>'${NO_USAGE_COST_SOURCE}'
261
264
  )
262
265
  SELECT e.* FROM attributed e JOIN (${best}) b USING(day,provider,model,rank)
263
266
  `)
@@ -399,6 +402,7 @@ export function readSessionUsage(db, sessionIds, ownerIds = []) {
399
402
  m.turns,m.input,m.output,m.cache_read,m.cache_write,m.cost_usd,m.duration_ms,
400
403
  m.speed_output,m.speed_ms
401
404
  FROM mine m JOIN usage_routes r ON r.id=m.route
405
+ WHERE json_extract(r.signature,'$[4]')<>'${NO_USAGE_COST_SOURCE}'
402
406
  )
403
407
  SELECT a.* FROM attributed a JOIN (${best}) b USING(day,provider,model,rank)
404
408
  `)
@@ -9,6 +9,7 @@ import { serveWorkerRequests } from '../worker-requests.mjs';
9
9
  import { UsageLedger } from './usage-ledger.mjs';
10
10
  import { readSessionUsage, rollupDelta, rollupUsage } from './usage-ledger-rollup.mjs';
11
11
  import { readQuotaHistory, readQuotaWindows } from './usage-ledger-quota.mjs';
12
+ import { readUsageRequests } from './usage-ledger-requests.mjs';
12
13
  import { applyUsageRepairIfUnchanged, selectUsageRepairCandidates } from './usage-ledger-repair.mjs';
13
14
  import { unpricedCountAfter, unpricedPendingCounts } from './usage-pricing-refresh.mjs';
14
15
 
@@ -61,6 +62,7 @@ const operations = {
61
62
  rollup: ({ path, options }) => read(path, (db) => rollupUsage(db, options)),
62
63
  rollupDelta: ({ path, options, known, now }) => read(path, (db) => rollupDelta(db, options, known, now)),
63
64
  sessionUsage: ({ path, sessionIds, ownerIds }) => read(path, (db) => readSessionUsage(db, sessionIds, ownerIds)),
65
+ requests: ({ path, options }) => read(path, (db) => readUsageRequests(db, options)),
64
66
  record: ({ path, rows }) => write(path, (ledger) => recordBatch(ledger, rows)),
65
67
  recordQuota: ({ path, samples }) => write(path, (ledger) => ledger.recordQuota(samples)),
66
68
  quotaHistory: ({ path, options }) => read(path, (db) => readQuotaHistory(db, options)),
@@ -20,6 +20,12 @@ import {
20
20
  rollupUsage,
21
21
  } from './usage-ledger-rollup.mjs';
22
22
  import { QUOTA_SCHEMA, readQuotaHistory, readQuotaWindows, recordQuotaSamples } from './usage-ledger-quota.mjs';
23
+ import {
24
+ NO_USAGE_COST_SOURCE,
25
+ REQUEST_LOG_SCHEMA,
26
+ STORED_REQUEST_STATUSES,
27
+ readUsageRequests,
28
+ } from './usage-ledger-requests.mjs';
23
29
  import { SHARE_ENV } from 'node:worker_threads';
24
30
  import { createWorkerRequestClient } from '../worker-requests.mjs';
25
31
 
@@ -116,6 +122,9 @@ function compactEventWriter(db) {
116
122
  const insertSession = db.prepare('INSERT OR IGNORE INTO usage_sessions(value) VALUES (?)');
117
123
  const findSession = db.prepare('SELECT id FROM usage_sessions WHERE value=?');
118
124
  const insert = db.prepare('INSERT INTO usage_events VALUES (?,?,?,?,?,?,?,?,?,?,?)');
125
+ const insertRequest = db.prepare('INSERT INTO usage_requests VALUES (?,?,?,?,?,?,?)');
126
+ const insertFirstOutput = db.prepare('INSERT INTO usage_request_first_output VALUES (?,?)');
127
+ const insertRequestedProvider = db.prepare('INSERT INTO usage_request_requested_provider VALUES (?,?)');
119
128
  const insertOwner = db.prepare('INSERT OR IGNORE INTO usage_session_owners VALUES (?,?)');
120
129
  return (row) => {
121
130
  for (const key of ['id', 'provider', 'model', 'kind', 'source_type', 'cost_source', 'origin', 'session_id']) {
@@ -159,6 +168,18 @@ function compactEventWriter(db) {
159
168
  row.cost_usd,
160
169
  row.duration_ms
161
170
  );
171
+ // Request-log facts live beside the record (usage-ledger-requests.mjs);
172
+ // a completed single attempt with nothing else to say needs no row.
173
+ const status = row.status || 'completed';
174
+ const attempts = row.attempts || 1;
175
+ if (status !== 'completed' || attempts > 1 || row.requested_model || row.elapsed_ms) {
176
+ insertRequest.run(id, row.ts, row.requested_model || null, status, attempts, null, row.elapsed_ms || null);
177
+ }
178
+ // The first-output time has its own table: only this build writes it, so
179
+ // a row there is this build's measurement (usage-ledger-requests.mjs).
180
+ if (row.ttft_ms != null) insertFirstOutput.run(id, row.ttft_ms);
181
+ // Likewise the provider a fallback hop was asked on: older builds never see it.
182
+ if (row.requested_provider) insertRequestedProvider.run(id, row.requested_provider);
162
183
  return true;
163
184
  };
164
185
  }
@@ -187,6 +208,7 @@ function migrateUsageLedger(db, path) {
187
208
  }
188
209
 
189
210
  db.exec(COMPACT_SCHEMA);
211
+ db.exec(REQUEST_LOG_SCHEMA);
190
212
  const insert = compactEventWriter(db);
191
213
  for (const row of db.prepare('SELECT * FROM events').iterate()) insert(row);
192
214
  db.exec(`CREATE VIEW usage_events_migration_check AS ${EVENT_SELECT}`);
@@ -251,6 +273,8 @@ export function usageCostSource({ kind, costUsd, subscription, reported = false
251
273
  export function makeUsageRecord(args) {
252
274
  const provider = text(args.provider);
253
275
  const model = text(args.model);
276
+ const requestedModel = text(args.requestedModel);
277
+ const requestedProvider = text(args.requestedProvider);
254
278
  if (!provider || !model) throw new Error('Usage requires a provider and model');
255
279
  const ts = Number(args.ts ?? Date.now());
256
280
  if (!Number.isSafeInteger(ts) || ts <= 0) throw new Error('Usage requires a valid timestamp');
@@ -269,6 +293,26 @@ export function makeUsageRecord(args) {
269
293
  let costUsd = priced.costUsd;
270
294
  if (kind === 'local') costUsd = 0;
271
295
  else if (reported) costUsd = Number(args.costUsd);
296
+ // A request that measured no tokens at all (a failure that reported
297
+ // nothing) cost nothing, priced model or not: it must not read as unpriced.
298
+ else if (
299
+ costUsd === null &&
300
+ args.status === 'failed' &&
301
+ args.inputTokensKnown !== false &&
302
+ priced.input === 0 &&
303
+ !num(args.outputTokens) &&
304
+ !num(args.cacheReadTokens) &&
305
+ !num(args.cacheWriteTokens)
306
+ )
307
+ costUsd = 0;
308
+ const noUsage =
309
+ args.status === 'failed' &&
310
+ args.inputTokensKnown !== false &&
311
+ priced.input === 0 &&
312
+ !num(args.outputTokens) &&
313
+ !num(args.cacheReadTokens) &&
314
+ !num(args.cacheWriteTokens) &&
315
+ !costUsd;
272
316
  let rates = priced.rates;
273
317
  if (reported || kind === 'local') {
274
318
  rates = {
@@ -295,12 +339,24 @@ export function makeUsageRecord(args) {
295
339
  cacheRead: num(args.cacheReadTokens),
296
340
  cacheWrite: num(args.cacheWriteTokens),
297
341
  costUsd,
298
- costSource: usageCostSource({ kind, costUsd, subscription, reported }),
342
+ costSource: noUsage ? NO_USAGE_COST_SOURCE : usageCostSource({ kind, costUsd, subscription, reported }),
299
343
  rates,
300
344
  responseId: text(args.responseId),
301
345
  account: text(args.account),
302
346
  origin: args.origin || 'live',
303
347
  durationMs: num(args.durationMs),
348
+ // The request log (usage-ledger-requests.mjs): kept only when the
349
+ // answering model differs from the one asked for.
350
+ requestedModel: requestedModel !== model ? requestedModel : '',
351
+ // Only a route-fallback hop asks another provider.
352
+ requestedProvider: requestedProvider !== provider ? requestedProvider : '',
353
+ status: STORED_REQUEST_STATUSES.includes(args.status) ? args.status : 'completed',
354
+ attempts: Math.max(1, Math.floor(num(args.attempts)) || 1),
355
+ ttftMs: args.ttftMs == null || !Number.isFinite(Number(args.ttftMs)) ? null : Math.max(0, Number(args.ttftMs)),
356
+ // A failed request's wall time, shown in the request log only: the
357
+ // record's own duration stays 0 so statistics speed reads completed
358
+ // requests alone, as before.
359
+ elapsedMs: num(args.elapsedMs),
304
360
  };
305
361
  row.id = args.id || (row.origin === 'live' && !row.responseId ? randomUUID() : usageRecordId(row));
306
362
  return row;
@@ -369,6 +425,7 @@ export class UsageLedger {
369
425
  CREATE TABLE IF NOT EXISTS legacy_days (day TEXT PRIMARY KEY, document TEXT NOT NULL);
370
426
  ${QUOTA_SCHEMA}
371
427
  ${DAY_REVISION_SCHEMA}
428
+ ${REQUEST_LOG_SCHEMA}
372
429
  PRAGMA user_version=2;
373
430
  `);
374
431
  this.prepareStatements();
@@ -417,6 +474,7 @@ export class UsageLedger {
417
474
  }
418
475
 
419
476
  indexRecord(row, rank) {
477
+ if (row.costSource === NO_USAGE_COST_SOURCE) return;
420
478
  const conversation = isConversationUsageSource(row.sourceType);
421
479
  this.daily.run(
422
480
  row.day,
@@ -477,6 +535,10 @@ export class UsageLedger {
477
535
  cost_source: row.costSource,
478
536
  rates: row.rates ? JSON.stringify(row.rates) : null,
479
537
  duration_ms: row.durationMs,
538
+ requested_model: row.requestedModel,
539
+ requested_provider: row.requestedProvider,
540
+ ttft_ms: row.ttftMs,
541
+ elapsed_ms: row.elapsedMs,
480
542
  rank,
481
543
  })
482
544
  )
@@ -582,6 +644,12 @@ export class UsageLedger {
582
644
  return this.workerRequest('sessionUsage', { sessionIds, ownerIds });
583
645
  }
584
646
 
647
+ /** One page of the request log (usage-ledger-requests.mjs), read on the ledger worker. */
648
+ requestsAsync(options) {
649
+ if (this.path === ':memory:') return Promise.resolve().then(() => readUsageRequests(this.db, options));
650
+ return this.workerRequest('requests', { options });
651
+ }
652
+
585
653
  preserveLegacyDays(days) {
586
654
  const insert = this.db.prepare('INSERT OR IGNORE INTO legacy_days VALUES (?,?)');
587
655
  this.db.exec('BEGIN IMMEDIATE');