mixdog 0.7.1

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 (404) hide show
  1. package/.claude-plugin/marketplace.json +31 -0
  2. package/.claude-plugin/plugin.json +20 -0
  3. package/.gitattributes +34 -0
  4. package/.mcp.json +14 -0
  5. package/ARCHITECTURE.md +77 -0
  6. package/CHANGELOG.md +7 -0
  7. package/CONTRIBUTING.md +45 -0
  8. package/DATA-FLOW.md +79 -0
  9. package/LICENSE +21 -0
  10. package/README.md +389 -0
  11. package/SECURITY.md +138 -0
  12. package/UNINSTALL.md +112 -0
  13. package/agents/maintenance.md +5 -0
  14. package/agents/memory-classification.md +30 -0
  15. package/agents/scheduler-task.md +18 -0
  16. package/agents/webhook-handler.md +27 -0
  17. package/agents/worker.md +24 -0
  18. package/bin/bridge +133 -0
  19. package/bin/statusline-launcher.mjs +78 -0
  20. package/bin/statusline-lib.mjs +550 -0
  21. package/bin/statusline.mjs +607 -0
  22. package/bun.lock +802 -0
  23. package/commands/config.md +16 -0
  24. package/commands/doctor.md +13 -0
  25. package/commands/setup.md +17 -0
  26. package/defaults/cycle3-review-prompt.md +90 -0
  27. package/defaults/hidden-roles.json +65 -0
  28. package/defaults/memory-chunk-prompt.md +63 -0
  29. package/defaults/memory-promote-prompt.md +135 -0
  30. package/defaults/mixdog-config.template.json +27 -0
  31. package/defaults/user-workflow.json +8 -0
  32. package/defaults/user-workflow.md +12 -0
  33. package/hooks/hooks.json +73 -0
  34. package/hooks/lib/active-instance.cjs +77 -0
  35. package/hooks/lib/permission-evaluator.cjs +411 -0
  36. package/hooks/lib/permission-route.cjs +63 -0
  37. package/hooks/lib/permission-rules.cjs +170 -0
  38. package/hooks/lib/settings-loader.cjs +116 -0
  39. package/hooks/post-tool-use.cjs +84 -0
  40. package/hooks/pre-mcp-sandbox.cjs +158 -0
  41. package/hooks/pre-tool-subagent.cjs +253 -0
  42. package/hooks/session-start.cjs +1372 -0
  43. package/hooks/turn-timer.cjs +82 -0
  44. package/lib/claude-md-writer.cjs +386 -0
  45. package/lib/config-cjs.cjs +61 -0
  46. package/lib/hook-pipe-path.cjs +10 -0
  47. package/lib/keychain-cjs.cjs +263 -0
  48. package/lib/plugin-paths.cjs +61 -0
  49. package/lib/rules-builder.cjs +241 -0
  50. package/lib/text-utils.cjs +61 -0
  51. package/native/README.md +117 -0
  52. package/native/prebuilt/linux-aarch64/mixdog-shim +0 -0
  53. package/native/prebuilt/linux-x86_64/mixdog-shim +0 -0
  54. package/native/prebuilt/macos-aarch64/mixdog-shim +0 -0
  55. package/native/prebuilt/macos-x86_64/mixdog-shim +0 -0
  56. package/native/prebuilt/windows-x86_64/mixdog-shim.exe +0 -0
  57. package/package.json +107 -0
  58. package/prompts/code-review.txt +16 -0
  59. package/prompts/security-audit.txt +17 -0
  60. package/rules/bridge/00-common.md +39 -0
  61. package/rules/bridge/20-skip-protocol.md +18 -0
  62. package/rules/bridge/30-explorer.md +33 -0
  63. package/rules/bridge/40-cycle1-agent.md +52 -0
  64. package/rules/bridge/41-cycle2-agent.md +62 -0
  65. package/rules/bridge/42-cycle3-agent.md +44 -0
  66. package/rules/lead/00-tool-lead.md +61 -0
  67. package/rules/lead/01-general.md +23 -0
  68. package/rules/lead/02-channels.md +49 -0
  69. package/rules/lead/03-team.md +27 -0
  70. package/rules/lead/04-workflow.md +20 -0
  71. package/rules/shared/00-language.md +14 -0
  72. package/rules/shared/01-tool.md +138 -0
  73. package/scripts/bootstrap.mjs +184 -0
  74. package/scripts/bridge-unify-smoke.mjs +308 -0
  75. package/scripts/build-runtime-linux.sh +348 -0
  76. package/scripts/build-runtime-macos.sh +217 -0
  77. package/scripts/build-runtime-windows.ps1 +242 -0
  78. package/scripts/builtin-utils-smoke.mjs +392 -0
  79. package/scripts/check-json.mjs +45 -0
  80. package/scripts/check-syntax-changed.mjs +102 -0
  81. package/scripts/check-syntax.mjs +58 -0
  82. package/scripts/code-graph-batch.test.mjs +33 -0
  83. package/scripts/config-preserve-smoke.mjs +180 -0
  84. package/scripts/doctor.mjs +484 -0
  85. package/scripts/edit-normalize-fuzz.mjs +130 -0
  86. package/scripts/edit-normalize-smoke.mjs +401 -0
  87. package/scripts/edit-operation-smoke.mjs +369 -0
  88. package/scripts/edit2-smoke.mjs +63 -0
  89. package/scripts/fuzzy-e2e.mjs +28 -0
  90. package/scripts/fuzzy-smoke.mjs +26 -0
  91. package/scripts/generate-runtime-manifest.mjs +166 -0
  92. package/scripts/guard-smoke.mjs +66 -0
  93. package/scripts/hidden-role-schema-smoke.mjs +162 -0
  94. package/scripts/hook-routing-smoke.mjs +29 -0
  95. package/scripts/inject-input.ps1 +204 -0
  96. package/scripts/io-complex-smoke.mjs +667 -0
  97. package/scripts/io-explore-bench.mjs +424 -0
  98. package/scripts/io-guardrails-smoke.mjs +205 -0
  99. package/scripts/io-mini-bench-baseline.json +11 -0
  100. package/scripts/io-mini-bench.mjs +216 -0
  101. package/scripts/io-route-harness.mjs +933 -0
  102. package/scripts/io-telemetry-report.mjs +691 -0
  103. package/scripts/mutation-bench.mjs +564 -0
  104. package/scripts/mutation-io-smoke.mjs +1081 -0
  105. package/scripts/native-patch-bridge-smoke.mjs +288 -0
  106. package/scripts/native-patch-smoke.mjs +304 -0
  107. package/scripts/patch-interior-context-smoke.mjs +49 -0
  108. package/scripts/patch-newline-utf8-smoke.mjs +157 -0
  109. package/scripts/perf-hook-smoke.mjs +71 -0
  110. package/scripts/permission-eval-smoke.mjs +426 -0
  111. package/scripts/prep-patch.mjs +53 -0
  112. package/scripts/prep-shim.mjs +96 -0
  113. package/scripts/provider-cache-smoke.mjs +687 -0
  114. package/scripts/report-runtime-health.mjs +132 -0
  115. package/scripts/run-mcp.mjs +1547 -0
  116. package/scripts/salvage-v4a-shatter.test.mjs +58 -0
  117. package/scripts/scoped-cache-io-smoke.mjs +103 -0
  118. package/scripts/shell-policy-round3-smoke.mjs +46 -0
  119. package/scripts/smoke-runtime-negative.ps1 +100 -0
  120. package/scripts/smoke-runtime-negative.sh +95 -0
  121. package/scripts/stall-policy-smoke.mjs +50 -0
  122. package/scripts/start-memory-worker.mjs +23 -0
  123. package/scripts/statusline-launcher-smoke.mjs +82 -0
  124. package/scripts/stress-atomic-write.mjs +1028 -0
  125. package/scripts/test-config-rmw-restore.mjs +122 -0
  126. package/scripts/test-fault-inject.mjs +164 -0
  127. package/scripts/test-large-file.mjs +174 -0
  128. package/scripts/tool-edge-smoke.mjs +209 -0
  129. package/scripts/uninstall.mjs +201 -0
  130. package/scripts/webhook-selfheal-smoke.mjs +29 -0
  131. package/scripts/write-overwrite-guard-smoke.mjs +56 -0
  132. package/server-main.mjs +3055 -0
  133. package/server.mjs +468 -0
  134. package/setup/config-merge.mjs +254 -0
  135. package/setup/install.mjs +120 -0
  136. package/setup/launch-core.mjs +507 -0
  137. package/setup/launch.mjs +101 -0
  138. package/setup/setup-server.mjs +3206 -0
  139. package/setup/setup.html +3693 -0
  140. package/skills/retro-skill-proposer/SKILL.md +92 -0
  141. package/skills/schedule-add/SKILL.md +77 -0
  142. package/skills/setup/SKILL.md +346 -0
  143. package/skills/webhook-add/SKILL.md +81 -0
  144. package/src/agent/bridge-stall-watchdog.mjs +337 -0
  145. package/src/agent/index.mjs +2138 -0
  146. package/src/agent/orchestrator/activity-bus.mjs +38 -0
  147. package/src/agent/orchestrator/ai-wrapped-dispatch.mjs +1010 -0
  148. package/src/agent/orchestrator/bridge-retry.mjs +220 -0
  149. package/src/agent/orchestrator/bridge-trace.mjs +583 -0
  150. package/src/agent/orchestrator/cache-mtime.mjs +58 -0
  151. package/src/agent/orchestrator/config.mjs +358 -0
  152. package/src/agent/orchestrator/context/collect.mjs +651 -0
  153. package/src/agent/orchestrator/dispatch-persist.mjs +549 -0
  154. package/src/agent/orchestrator/drain-registry.mjs +50 -0
  155. package/src/agent/orchestrator/explore-validator.mjs +8 -0
  156. package/src/agent/orchestrator/internal-roles.mjs +118 -0
  157. package/src/agent/orchestrator/internal-tools.mjs +88 -0
  158. package/src/agent/orchestrator/jobs.mjs +116 -0
  159. package/src/agent/orchestrator/mcp/client.mjs +364 -0
  160. package/src/agent/orchestrator/providers/anthropic-betas.mjs +21 -0
  161. package/src/agent/orchestrator/providers/anthropic-oauth.mjs +1745 -0
  162. package/src/agent/orchestrator/providers/anthropic.mjs +437 -0
  163. package/src/agent/orchestrator/providers/gemini.mjs +1175 -0
  164. package/src/agent/orchestrator/providers/grok-oauth.mjs +782 -0
  165. package/src/agent/orchestrator/providers/model-catalog.mjs +241 -0
  166. package/src/agent/orchestrator/providers/openai-compat.mjs +1467 -0
  167. package/src/agent/orchestrator/providers/openai-oauth-ws.mjs +1890 -0
  168. package/src/agent/orchestrator/providers/openai-oauth.mjs +1307 -0
  169. package/src/agent/orchestrator/providers/openai-ws.mjs +104 -0
  170. package/src/agent/orchestrator/providers/registry.mjs +192 -0
  171. package/src/agent/orchestrator/providers/retry-classifier.mjs +325 -0
  172. package/src/agent/orchestrator/session/abort-lookup.mjs +13 -0
  173. package/src/agent/orchestrator/session/cache/post-edit-marks.mjs +42 -0
  174. package/src/agent/orchestrator/session/cache/prefetch-cache.mjs +142 -0
  175. package/src/agent/orchestrator/session/cache/read-cache.mjs +319 -0
  176. package/src/agent/orchestrator/session/cache/scoped-cache-outcome.mjs +11 -0
  177. package/src/agent/orchestrator/session/cache/scoped-cache.mjs +361 -0
  178. package/src/agent/orchestrator/session/cache/util.mjs +49 -0
  179. package/src/agent/orchestrator/session/loop.mjs +1478 -0
  180. package/src/agent/orchestrator/session/manager.mjs +1975 -0
  181. package/src/agent/orchestrator/session/read-dedup.mjs +6 -0
  182. package/src/agent/orchestrator/session/result-classification.mjs +65 -0
  183. package/src/agent/orchestrator/session/save-session-worker.mjs +18 -0
  184. package/src/agent/orchestrator/session/store.mjs +624 -0
  185. package/src/agent/orchestrator/session/stream-watchdog.mjs +130 -0
  186. package/src/agent/orchestrator/session/tool-result-offload.mjs +166 -0
  187. package/src/agent/orchestrator/session/trim.mjs +491 -0
  188. package/src/agent/orchestrator/smart-bridge/CACHE-SHARD.md +115 -0
  189. package/src/agent/orchestrator/smart-bridge/bridge-llm.mjs +327 -0
  190. package/src/agent/orchestrator/smart-bridge/cache-obs.mjs +150 -0
  191. package/src/agent/orchestrator/smart-bridge/cache-strategy.mjs +228 -0
  192. package/src/agent/orchestrator/smart-bridge/index.mjs +215 -0
  193. package/src/agent/orchestrator/smart-bridge/profiles.mjs +37 -0
  194. package/src/agent/orchestrator/smart-bridge/registry.mjs +348 -0
  195. package/src/agent/orchestrator/smart-bridge/session-builder.mjs +116 -0
  196. package/src/agent/orchestrator/stall-policy.mjs +195 -0
  197. package/src/agent/orchestrator/tool-loop-guard.mjs +75 -0
  198. package/src/agent/orchestrator/tools/bash-policy-scan.mjs +77 -0
  199. package/src/agent/orchestrator/tools/bash-session.mjs +721 -0
  200. package/src/agent/orchestrator/tools/builtin/advisory-lock.mjs +171 -0
  201. package/src/agent/orchestrator/tools/builtin/arg-guard.mjs +455 -0
  202. package/src/agent/orchestrator/tools/builtin/atomic-write.mjs +236 -0
  203. package/src/agent/orchestrator/tools/builtin/bash-tool.mjs +480 -0
  204. package/src/agent/orchestrator/tools/builtin/binary-file.mjs +76 -0
  205. package/src/agent/orchestrator/tools/builtin/builtin-tools.mjs +256 -0
  206. package/src/agent/orchestrator/tools/builtin/cache-layers.mjs +386 -0
  207. package/src/agent/orchestrator/tools/builtin/cwd-utils.mjs +37 -0
  208. package/src/agent/orchestrator/tools/builtin/device-paths.mjs +154 -0
  209. package/src/agent/orchestrator/tools/builtin/diagnostics-tool.mjs +292 -0
  210. package/src/agent/orchestrator/tools/builtin/diff-utils.mjs +109 -0
  211. package/src/agent/orchestrator/tools/builtin/edit-base-guard.mjs +58 -0
  212. package/src/agent/orchestrator/tools/builtin/edit-byte-plan.mjs +240 -0
  213. package/src/agent/orchestrator/tools/builtin/edit-byte-utils.mjs +113 -0
  214. package/src/agent/orchestrator/tools/builtin/edit-commit.mjs +74 -0
  215. package/src/agent/orchestrator/tools/builtin/edit-context-utils.mjs +242 -0
  216. package/src/agent/orchestrator/tools/builtin/edit-diagnostics.mjs +211 -0
  217. package/src/agent/orchestrator/tools/builtin/edit-engine.mjs +1364 -0
  218. package/src/agent/orchestrator/tools/builtin/edit-failure-context.mjs +126 -0
  219. package/src/agent/orchestrator/tools/builtin/edit-hint.mjs +141 -0
  220. package/src/agent/orchestrator/tools/builtin/edit-match-utils.mjs +194 -0
  221. package/src/agent/orchestrator/tools/builtin/edit-partial-write.mjs +60 -0
  222. package/src/agent/orchestrator/tools/builtin/edit-stale-refresh.mjs +168 -0
  223. package/src/agent/orchestrator/tools/builtin/edit-tool.mjs +173 -0
  224. package/src/agent/orchestrator/tools/builtin/edit-utf8-guard.mjs +48 -0
  225. package/src/agent/orchestrator/tools/builtin/fs-reachability.mjs +48 -0
  226. package/src/agent/orchestrator/tools/builtin/fuzzy-match.mjs +99 -0
  227. package/src/agent/orchestrator/tools/builtin/glob-walk.mjs +170 -0
  228. package/src/agent/orchestrator/tools/builtin/grep-formatting.mjs +113 -0
  229. package/src/agent/orchestrator/tools/builtin/hash-utils.mjs +6 -0
  230. package/src/agent/orchestrator/tools/builtin/list-formatting.mjs +7 -0
  231. package/src/agent/orchestrator/tools/builtin/list-tool.mjs +593 -0
  232. package/src/agent/orchestrator/tools/builtin/native-edit-runner.mjs +89 -0
  233. package/src/agent/orchestrator/tools/builtin/notebook-edit-tool.mjs +300 -0
  234. package/src/agent/orchestrator/tools/builtin/open-config-tool.mjs +26 -0
  235. package/src/agent/orchestrator/tools/builtin/path-diagnostics.mjs +152 -0
  236. package/src/agent/orchestrator/tools/builtin/path-locks.mjs +35 -0
  237. package/src/agent/orchestrator/tools/builtin/path-utils.mjs +201 -0
  238. package/src/agent/orchestrator/tools/builtin/read-args.mjs +103 -0
  239. package/src/agent/orchestrator/tools/builtin/read-batch.mjs +172 -0
  240. package/src/agent/orchestrator/tools/builtin/read-constants.mjs +40 -0
  241. package/src/agent/orchestrator/tools/builtin/read-formatting.mjs +118 -0
  242. package/src/agent/orchestrator/tools/builtin/read-image-resize.mjs +189 -0
  243. package/src/agent/orchestrator/tools/builtin/read-image.mjs +88 -0
  244. package/src/agent/orchestrator/tools/builtin/read-lines.mjs +12 -0
  245. package/src/agent/orchestrator/tools/builtin/read-mode-tool.mjs +455 -0
  246. package/src/agent/orchestrator/tools/builtin/read-open.mjs +190 -0
  247. package/src/agent/orchestrator/tools/builtin/read-range-index.mjs +271 -0
  248. package/src/agent/orchestrator/tools/builtin/read-ranges.mjs +26 -0
  249. package/src/agent/orchestrator/tools/builtin/read-single-tool.mjs +728 -0
  250. package/src/agent/orchestrator/tools/builtin/read-snapshot-runtime.mjs +173 -0
  251. package/src/agent/orchestrator/tools/builtin/read-special-files.mjs +268 -0
  252. package/src/agent/orchestrator/tools/builtin/read-streaming.mjs +602 -0
  253. package/src/agent/orchestrator/tools/builtin/read-tool.mjs +530 -0
  254. package/src/agent/orchestrator/tools/builtin/read-windows.mjs +107 -0
  255. package/src/agent/orchestrator/tools/builtin/rename-tool.mjs +196 -0
  256. package/src/agent/orchestrator/tools/builtin/rg-runner.mjs +422 -0
  257. package/src/agent/orchestrator/tools/builtin/search-builders.mjs +158 -0
  258. package/src/agent/orchestrator/tools/builtin/search-tool.mjs +869 -0
  259. package/src/agent/orchestrator/tools/builtin/shell-analysis.mjs +653 -0
  260. package/src/agent/orchestrator/tools/builtin/shell-jobs.mjs +936 -0
  261. package/src/agent/orchestrator/tools/builtin/shell-output.mjs +36 -0
  262. package/src/agent/orchestrator/tools/builtin/shell-runtime.mjs +214 -0
  263. package/src/agent/orchestrator/tools/builtin/snapshot-helpers.mjs +143 -0
  264. package/src/agent/orchestrator/tools/builtin/snapshot-store.mjs +206 -0
  265. package/src/agent/orchestrator/tools/builtin/snapshot-validation.mjs +98 -0
  266. package/src/agent/orchestrator/tools/builtin/text-stats.mjs +69 -0
  267. package/src/agent/orchestrator/tools/builtin/windows-roots.mjs +23 -0
  268. package/src/agent/orchestrator/tools/builtin/write-tool.mjs +401 -0
  269. package/src/agent/orchestrator/tools/builtin.mjs +500 -0
  270. package/src/agent/orchestrator/tools/code-graph-prewarm-worker.mjs +39 -0
  271. package/src/agent/orchestrator/tools/code-graph-tool-defs.mjs +24 -0
  272. package/src/agent/orchestrator/tools/code-graph.mjs +4095 -0
  273. package/src/agent/orchestrator/tools/cwd-tool.mjs +298 -0
  274. package/src/agent/orchestrator/tools/destructive-warning.mjs +323 -0
  275. package/src/agent/orchestrator/tools/edit-normalize.mjs +603 -0
  276. package/src/agent/orchestrator/tools/env-scrub.mjs +100 -0
  277. package/src/agent/orchestrator/tools/graph-binary-fetcher.mjs +144 -0
  278. package/src/agent/orchestrator/tools/graph-manifest.json +26 -0
  279. package/src/agent/orchestrator/tools/host-input.mjs +204 -0
  280. package/src/agent/orchestrator/tools/mutation-content-cache.mjs +67 -0
  281. package/src/agent/orchestrator/tools/mutation-planner.mjs +75 -0
  282. package/src/agent/orchestrator/tools/next-call-utils.mjs +48 -0
  283. package/src/agent/orchestrator/tools/patch-binary-fetcher.mjs +133 -0
  284. package/src/agent/orchestrator/tools/patch-manifest.json +26 -0
  285. package/src/agent/orchestrator/tools/patch-tool-defs.mjs +20 -0
  286. package/src/agent/orchestrator/tools/patch.mjs +2754 -0
  287. package/src/agent/orchestrator/tools/progress-message.mjs +118 -0
  288. package/src/agent/orchestrator/tools/result-compression.mjs +279 -0
  289. package/src/agent/orchestrator/tools/shell-command.mjs +865 -0
  290. package/src/agent/orchestrator/tools/shell-exec-policy.mjs +89 -0
  291. package/src/agent/orchestrator/tools/shell-policy-danger-target.mjs +27 -0
  292. package/src/agent/orchestrator/tools/shell-policy-imports.mjs +7 -0
  293. package/src/agent/orchestrator/tools/shell-policy.mjs +345 -0
  294. package/src/agent/orchestrator/tools/shell-snapshot.mjs +313 -0
  295. package/src/agent/orchestrator/workflow-store.mjs +93 -0
  296. package/src/agent/tool-defs.mjs +103 -0
  297. package/src/channels/backends/discord.mjs +784 -0
  298. package/src/channels/data/voice-runtime-manifest.json +138 -0
  299. package/src/channels/index.mjs +3229 -0
  300. package/src/channels/lib/cli-worker-host.mjs +12 -0
  301. package/src/channels/lib/config-lock.mjs +13 -0
  302. package/src/channels/lib/config.mjs +292 -0
  303. package/src/channels/lib/drop-trace.mjs +71 -0
  304. package/src/channels/lib/event-pipeline.mjs +81 -0
  305. package/src/channels/lib/event-queue.mjs +345 -0
  306. package/src/channels/lib/executor.mjs +168 -0
  307. package/src/channels/lib/format.mjs +188 -0
  308. package/src/channels/lib/holidays.mjs +138 -0
  309. package/src/channels/lib/hook-pipe-server.mjs +802 -0
  310. package/src/channels/lib/interaction-workflows.mjs +184 -0
  311. package/src/channels/lib/memory-client.mjs +149 -0
  312. package/src/channels/lib/output-forwarder.mjs +765 -0
  313. package/src/channels/lib/runtime-paths.mjs +479 -0
  314. package/src/channels/lib/scheduler.mjs +723 -0
  315. package/src/channels/lib/session-control.mjs +36 -0
  316. package/src/channels/lib/session-discovery.mjs +103 -0
  317. package/src/channels/lib/settings.mjs +11 -0
  318. package/src/channels/lib/state-file.mjs +68 -0
  319. package/src/channels/lib/status-snapshot.mjs +219 -0
  320. package/src/channels/lib/tool-format.mjs +140 -0
  321. package/src/channels/lib/transcript-discovery.mjs +195 -0
  322. package/src/channels/lib/voice-runtime-fetcher.mjs +734 -0
  323. package/src/channels/lib/webhook.mjs +1179 -0
  324. package/src/channels/lib/whisper-server.mjs +477 -0
  325. package/src/channels/tool-defs.mjs +170 -0
  326. package/src/daemon/host.mjs +118 -0
  327. package/src/daemon/mcp-transport.mjs +47 -0
  328. package/src/daemon/session.mjs +100 -0
  329. package/src/daemon/thin-client.mjs +71 -0
  330. package/src/daemon/transport.mjs +163 -0
  331. package/src/memory/data/runtime-manifest.json +40 -0
  332. package/src/memory/index.mjs +3305 -0
  333. package/src/memory/lib/agent-ipc.mjs +93 -0
  334. package/src/memory/lib/bridge-trace-queries.mjs +120 -0
  335. package/src/memory/lib/core-memory-store.mjs +330 -0
  336. package/src/memory/lib/embedding-provider.mjs +269 -0
  337. package/src/memory/lib/embedding-worker.mjs +323 -0
  338. package/src/memory/lib/llm-worker-host.mjs +17 -0
  339. package/src/memory/lib/memory-cycle.mjs +11 -0
  340. package/src/memory/lib/memory-cycle1.mjs +641 -0
  341. package/src/memory/lib/memory-cycle2.mjs +1284 -0
  342. package/src/memory/lib/memory-cycle3.mjs +540 -0
  343. package/src/memory/lib/memory-embed.mjs +299 -0
  344. package/src/memory/lib/memory-extraction.mjs +5 -0
  345. package/src/memory/lib/memory-maintenance-store.mjs +32 -0
  346. package/src/memory/lib/memory-ops-policy.mjs +190 -0
  347. package/src/memory/lib/memory-recall-id-patch.mjs +15 -0
  348. package/src/memory/lib/memory-recall-read-query.mjs +7 -0
  349. package/src/memory/lib/memory-recall-scope-filter.mjs +63 -0
  350. package/src/memory/lib/memory-recall-store.mjs +621 -0
  351. package/src/memory/lib/memory-retrievers.mjs +112 -0
  352. package/src/memory/lib/memory-score.mjs +71 -0
  353. package/src/memory/lib/memory-text-utils.mjs +58 -0
  354. package/src/memory/lib/memory.mjs +412 -0
  355. package/src/memory/lib/model-profile.mjs +85 -0
  356. package/src/memory/lib/pg/adapter.mjs +308 -0
  357. package/src/memory/lib/pg/process.mjs +360 -0
  358. package/src/memory/lib/pg/supervisor.mjs +396 -0
  359. package/src/memory/lib/project-id-resolver.mjs +86 -0
  360. package/src/memory/lib/runtime-fetcher.mjs +442 -0
  361. package/src/memory/lib/trace-store.mjs +728 -0
  362. package/src/memory/tool-defs.mjs +79 -0
  363. package/src/search/index.mjs +1173 -0
  364. package/src/search/lib/backends/anthropic-oauth.mjs +98 -0
  365. package/src/search/lib/backends/exa.mjs +50 -0
  366. package/src/search/lib/backends/firecrawl.mjs +61 -0
  367. package/src/search/lib/backends/gemini-api.mjs +83 -0
  368. package/src/search/lib/backends/grok-oauth.mjs +86 -0
  369. package/src/search/lib/backends/index.mjs +150 -0
  370. package/src/search/lib/backends/openai-api.mjs +144 -0
  371. package/src/search/lib/backends/openai-oauth.mjs +98 -0
  372. package/src/search/lib/backends/openai-web-search.mjs +76 -0
  373. package/src/search/lib/backends/tavily.mjs +55 -0
  374. package/src/search/lib/backends/xai-api.mjs +113 -0
  375. package/src/search/lib/cache.mjs +131 -0
  376. package/src/search/lib/config.mjs +192 -0
  377. package/src/search/lib/formatter.mjs +115 -0
  378. package/src/search/lib/provider-usage.mjs +67 -0
  379. package/src/search/lib/providers.mjs +47 -0
  380. package/src/search/lib/search-intent.mjs +109 -0
  381. package/src/search/lib/setup-handler.mjs +261 -0
  382. package/src/search/lib/state.mjs +201 -0
  383. package/src/search/lib/web-tools.mjs +1207 -0
  384. package/src/search/tool-defs.mjs +83 -0
  385. package/src/setup/defender-exclusion.mjs +183 -0
  386. package/src/shared/abort-controller.mjs +15 -0
  387. package/src/shared/atomic-file.mjs +420 -0
  388. package/src/shared/config.mjs +350 -0
  389. package/src/shared/daemon-recycle.mjs +108 -0
  390. package/src/shared/disable-claude-builtins.mjs +88 -0
  391. package/src/shared/err-text.mjs +12 -0
  392. package/src/shared/llm/cost.mjs +66 -0
  393. package/src/shared/llm/http-agent.mjs +123 -0
  394. package/src/shared/llm/index.mjs +41 -0
  395. package/src/shared/llm/pid-cleanup.mjs +27 -0
  396. package/src/shared/llm/usage-log.mjs +47 -0
  397. package/src/shared/plugin-paths.mjs +58 -0
  398. package/src/shared/schedules-store.mjs +70 -0
  399. package/src/shared/seed.mjs +119 -0
  400. package/src/shared/user-cwd.mjs +213 -0
  401. package/src/shared/user-data-guard.mjs +238 -0
  402. package/src/status/aggregator.mjs +584 -0
  403. package/src/status/server.mjs +413 -0
  404. package/tools.json +1653 -0
@@ -0,0 +1,1745 @@
1
+ /**
2
+ * Anthropic OAuth provider — uses Claude Code's OAuth credentials
3
+ * (~/.claude/.credentials.json) for Claude Max subscription access.
4
+ *
5
+ * Raw HTTP + SSE streaming, reuses message/tool conversion patterns
6
+ * from anthropic.mjs. Bridge-trace instrumented.
7
+ */
8
+ import { readFileSync, existsSync, statSync } from 'fs';
9
+ import { join } from 'path';
10
+ import { homedir } from 'os';
11
+ import {
12
+ traceBridgeFetch,
13
+ traceBridgeSse,
14
+ traceBridgeUsage,
15
+ } from '../bridge-trace.mjs';
16
+ import { createAbortController } from '../../../shared/abort-controller.mjs';
17
+ import { writeJsonAtomicSync } from '../../../shared/atomic-file.mjs';
18
+ import { getPluginData } from '../config.mjs';
19
+ import { enrichModels } from './model-catalog.mjs';
20
+ import { sanitizeToolPairs, sanitizeAnthropicContentPairs } from '../session/trim.mjs';
21
+ import {
22
+ PROVIDER_GENERATE_TOTAL_TIMEOUT_MS,
23
+ PROVIDER_HTTP_RESPONSE_TIMEOUT_MS,
24
+ PROVIDER_RETRY_BACKOFF_MS,
25
+ PROVIDER_RETRY_MAX_ATTEMPTS,
26
+ PROVIDER_SSE_IDLE_TIMEOUT_MS,
27
+ PROVIDER_SSE_IDLE_WATCHDOG_ENABLED,
28
+ createTimeoutSignal,
29
+ } from '../stall-policy.mjs';
30
+ import {
31
+ classifyError,
32
+ retryAfterMsFromError,
33
+ withRetry,
34
+ } from './retry-classifier.mjs';
35
+ import { buildAnthropicBetaHeaders, supportsAnthropicFastMode } from './anthropic-betas.mjs';
36
+ import { getLlmDispatcher, preconnect } from '../../../shared/llm/http-agent.mjs';
37
+
38
+ // --- Model catalog cache helpers ---
39
+ // Disk-backed cache so repeated process starts (cron, tool calls) don't
40
+ // hammer /v1/models. 24h TTL is the same cadence Claude Code itself uses
41
+ // for its internal model discovery.
42
+ const MODEL_CACHE_TTL_MS = 24 * 60 * 60_000;
43
+ // SSE progress emits (per-request "Response …" and "Done:" lines). Off by default.
44
+ const SSE_VERBOSE = process.env.MIXDOG_SSE_VERBOSE === '1';
45
+
46
+ function _modelCachePath() {
47
+ return join(getPluginData(), 'anthropic-oauth-models.json');
48
+ }
49
+
50
+ async function _loadModelCache() {
51
+ const path = _modelCachePath();
52
+ if (!existsSync(path)) return null;
53
+ try {
54
+ const raw = JSON.parse(readFileSync(path, 'utf-8'));
55
+ if (!raw?.fetchedAt || !Array.isArray(raw.models)) return null;
56
+ if (Date.now() - raw.fetchedAt > MODEL_CACHE_TTL_MS) return null;
57
+ return raw.models;
58
+ } catch { return null; }
59
+ }
60
+
61
+ async function _saveModelCache(models) {
62
+ try {
63
+ writeJsonAtomicSync(_modelCachePath(), {
64
+ fetchedAt: Date.now(),
65
+ models,
66
+ }, { lock: true, fsyncDir: true });
67
+ _inMemoryCatalog = Array.isArray(models) ? models.slice() : null;
68
+ } catch { /* cache is best-effort */ }
69
+ }
70
+
71
+ // In-memory mirror of the disk catalog — populated on first listModels() and
72
+ // refreshed after every _saveModelCache. Used by _catalogHas and _displayModel
73
+ // so hot paths don't hit disk on every response.
74
+ let _inMemoryCatalog = null;
75
+ let _modelRefreshInFlight = null;
76
+ let _oauthRefreshInFlight = null;
77
+ // No in-memory credential cache: the canonical credentials file is the
78
+ // single source of truth. Cross-process refresh_token rotation by host
79
+ // Claude Code (or another concurrent reader) would invalidate any cached
80
+ // copy here and produce invalid_grant on the next refresh. Reading from
81
+ // disk on demand is cheap (one stat + one small JSON parse) and removes
82
+ // the cache-vs-disk skew entirely.
83
+
84
+
85
+ function _catalogHas(id) {
86
+ if (!id || !Array.isArray(_inMemoryCatalog)) return false;
87
+ return _inMemoryCatalog.some(m => m.id === id);
88
+ }
89
+
90
+ // Display-name normalization for trace / usage. Turns dated or version-alias
91
+ // ids into the version alias form: claude-opus-4-7 → claude-opus-4.7,
92
+ // claude-haiku-4-5-20251001 → claude-haiku-4.5. Falls back to the raw id.
93
+ function _displayModel(id) {
94
+ if (!id || typeof id !== 'string') return id;
95
+ const m = id.match(/^claude-(opus|sonnet|haiku)-(\d+)-(\d+)(?:-\d{8})?$/i);
96
+ if (!m) return id;
97
+ return `claude-${m[1].toLowerCase()}-${m[2]}.${m[3]}`;
98
+ }
99
+
100
+ // Classify a model id into our common tier/family shape. Anthropic's catalog
101
+ // mixes dated ids (claude-opus-4-5-20251101), versioned aliases
102
+ // (claude-opus-4-6), and the raw family tokens resolved via env vars.
103
+ function _normalizeAnthropicModel(raw) {
104
+ const id = raw?.id || raw?.name;
105
+ if (!id) return null;
106
+ const familyMatch = id.match(/^claude-(opus|sonnet|haiku)/i);
107
+ const family = familyMatch ? familyMatch[1].toLowerCase() : 'other';
108
+ // Dated: trailing -YYYYMMDD (8 digits).
109
+ const dated = /-\d{8}$/.test(id);
110
+ // Versioned alias: claude-<family>-<major>-<minor>[-...] with no dated suffix.
111
+ const versioned = !dated && /-\d+-\d+/.test(id);
112
+ const tier = dated ? 'dated' : versioned ? 'version' : 'family';
113
+ const releaseDate = dated
114
+ ? id.match(/-(\d{4})(\d{2})(\d{2})$/)
115
+ : null;
116
+ return {
117
+ id,
118
+ display: raw?.display_name || _prettyName(id, family),
119
+ family,
120
+ provider: 'anthropic-oauth',
121
+ contextWindow: raw?.context_window || raw?.max_context_window || _defaultContextForModel(id, family),
122
+ tier,
123
+ latest: false, // assigned in a second pass once full list is known
124
+ releaseDate: releaseDate ? `${releaseDate[1]}-${releaseDate[2]}-${releaseDate[3]}` : null,
125
+ };
126
+ }
127
+
128
+ function _prettyName(id, family) {
129
+ const v = id.match(/-(\d+)-(\d+)/);
130
+ const base = family[0].toUpperCase() + family.slice(1);
131
+ return v ? `${base} ${v[1]}.${v[2]}` : base;
132
+ }
133
+
134
+ function _defaultContextForModel(id, family) {
135
+ if (/^claude-(opus|sonnet)-4-(6|7|8)(?:$|-)/i.test(String(id || ''))) return 1000000;
136
+ if (family === 'opus') return 200000;
137
+ if (family === 'sonnet') return 200000;
138
+ if (family === 'haiku') return 200000;
139
+ return 200000;
140
+ }
141
+
142
+ // Mark the highest-numbered version per family as `latest: true`. Uses a simple
143
+ // lexicographic comparison on the numeric parts embedded in the id.
144
+ function _markLatestByFamily(models) {
145
+ const byFamily = new Map();
146
+ for (const m of models) {
147
+ if (m.tier !== 'version') continue;
148
+ const cur = byFamily.get(m.family);
149
+ if (!cur || _compareVersion(m.id, cur.id) > 0) {
150
+ byFamily.set(m.family, m);
151
+ }
152
+ }
153
+ for (const m of byFamily.values()) m.latest = true;
154
+ }
155
+
156
+ function _compareVersion(a, b) {
157
+ const na = (a.match(/-(\d+)-(\d+)/) || []).slice(1).map(Number);
158
+ const nb = (b.match(/-(\d+)-(\d+)/) || []).slice(1).map(Number);
159
+ for (let i = 0; i < Math.max(na.length, nb.length); i++) {
160
+ if ((na[i] || 0) !== (nb[i] || 0)) return (na[i] || 0) - (nb[i] || 0);
161
+ }
162
+ return a.localeCompare(b);
163
+ }
164
+
165
+ // Newest HIGH-TIER chat model by version, read from the SYNC in-memory catalog
166
+ // mirror. Symmetric with resolveLatestGrokModel / resolveLatestCodexModel.
167
+ // Anthropic ships three families: opus / sonnet / haiku. "Latest" is the
168
+ // highest version across opus + sonnet only — haiku is the cheap tier and is
169
+ // never the flagship default. Returns null until listModels() populates the
170
+ // mirror; callers must warm the catalog (ensureLatestAnthropicModel) when null.
171
+ export function resolveLatestAnthropicModel() {
172
+ if (!Array.isArray(_inMemoryCatalog)) return null;
173
+ let best = null;
174
+ for (const m of _inMemoryCatalog) {
175
+ if (!m?.id || (m.family !== 'opus' && m.family !== 'sonnet')) continue;
176
+ if (!best || _compareVersion(m.id, best.id) > 0) best = m;
177
+ }
178
+ return best?.id || null;
179
+ }
180
+
181
+ export async function ensureLatestAnthropicModel(provider) {
182
+ let m = resolveLatestAnthropicModel();
183
+ if (m) return m;
184
+ await provider._refreshModelCache();
185
+ m = resolveLatestAnthropicModel();
186
+ if (m) return m;
187
+ throw new Error('[anthropic-oauth] model catalog unavailable after warmup — cannot resolve default model');
188
+ }
189
+
190
+ const API_URL = 'https://api.anthropic.com/v1/messages';
191
+ // SSRF guard for the OAuth token endpoint override. Env-supplied URLs must be
192
+ // https with a valid http(s) URL shape; reject file:/data:/ftp:/etc. and any
193
+ // http override so a hostile env cannot redirect refresh-token requests.
194
+ function assertSafeTokenURL(rawURL) {
195
+ let parsed;
196
+ try {
197
+ parsed = new URL(String(rawURL));
198
+ } catch {
199
+ throw new Error(`[anthropic-oauth] invalid ANTHROPIC_OAUTH_TOKEN_URL: ${rawURL}`);
200
+ }
201
+ if (parsed.protocol.toLowerCase() !== 'https:') {
202
+ throw new Error(`[anthropic-oauth] ANTHROPIC_OAUTH_TOKEN_URL must use https (got ${parsed.protocol})`);
203
+ }
204
+ return rawURL;
205
+ }
206
+ const TOKEN_URL = assertSafeTokenURL(process.env.ANTHROPIC_OAUTH_TOKEN_URL || 'https://console.anthropic.com/v1/oauth/token');
207
+ const ANTHROPIC_VERSION = '2023-06-01';
208
+ const DEFAULT_CREDENTIALS_PATH = join(homedir(), '.claude', '.credentials.json');
209
+ const CLAUDE_CODE_CLIENT_ID = process.env.ANTHROPIC_OAUTH_CLIENT_ID || '9d1c250a-e61b-44d9-88ed-5944d1962f5e';
210
+ const TOKEN_REFRESH_SKEW_MS = 5 * 60_000;
211
+
212
+ // Anthropic OAuth contract for first-party Claude Code clients.
213
+ // Opus/Sonnet requests are gated on a specific system-prompt prefix.
214
+ // Our plugin ONLY runs inside Claude Code (marketplace-distributed),
215
+ // so declaring ourselves as Claude Code is literally accurate — not
216
+ // impersonation. Haiku is not gated and ignores this prefix.
217
+ const CLAUDE_CODE_SYSTEM_PREFIX = "You are Claude Code, Anthropic's official CLI for Claude.";
218
+ const OAUTH_BETA_HEADERS = 'oauth-2025-04-20,interleaved-thinking-2025-05-14,context-management-2025-06-27,extended-cache-ttl-2025-04-11';
219
+ const DEFAULT_CLI_VERSION = '2.1.77';
220
+
221
+ function resolveCliVersion() {
222
+ // Claude Code sets CLAUDE_CODE_VERSION in the plugin subprocess env.
223
+ // Fallback exists so unit tests and older Claude Code versions still work.
224
+ return process.env.CLAUDE_CODE_VERSION
225
+ || process.env.CLAUDE_CODE_EXECPATH_VERSION
226
+ || DEFAULT_CLI_VERSION;
227
+ }
228
+
229
+ function requiresSystemPrefix(model) {
230
+ // Opus / Sonnet require the Claude Code system prefix when authenticated
231
+ // via OAuth. Haiku does not.
232
+ return /^claude-(opus|sonnet)/i.test(String(model || ''));
233
+ }
234
+
235
+ // OAuth rate-limit pool routing is gated by the server inspecting the first
236
+ // system block. When it reads exactly "You are Claude Code, Anthropic's
237
+ // official CLI for Claude." it routes into the Claude Code pool; any other
238
+ // content (even the prefix concatenated with extra text in the same block)
239
+ // falls into the standard pool and Opus/Sonnet return 429. Splitting into
240
+ // two blocks — [prefix, rest] — keeps both routing and user instructions.
241
+ function buildSystemBlocks(systemText, model, cacheControl) {
242
+ // systemText is an array of strings — each element becomes its own Anthropic
243
+ // content block with its own cache_control breakpoint (BP1 + BP2).
244
+ // Invariant: callers must pass an array; scalar strings are not accepted.
245
+ const texts = Array.isArray(systemText)
246
+ ? systemText.map(s => typeof s === 'string' ? s.trim() : '').filter(Boolean)
247
+ : [];
248
+ const gated = requiresSystemPrefix(model);
249
+
250
+ const blocks = [];
251
+ if (gated) {
252
+ blocks.push({ type: 'text', text: CLAUDE_CODE_SYSTEM_PREFIX });
253
+ }
254
+ for (let i = 0; i < texts.length; i++) {
255
+ let body = texts[i];
256
+ // Strip a duplicated Claude Code prefix from the first block if present.
257
+ if (gated && i === 0 && body.startsWith(CLAUDE_CODE_SYSTEM_PREFIX)) {
258
+ body = body.slice(CLAUDE_CODE_SYSTEM_PREFIX.length).trim();
259
+ if (!body) continue;
260
+ }
261
+ const block = { type: 'text', text: body };
262
+ if (cacheControl) block.cache_control = cacheControl;
263
+ blocks.push(block);
264
+ }
265
+ return blocks;
266
+ }
267
+
268
+ const MODELS = [
269
+ { id: 'claude-opus-4-8', name: 'Claude Opus 4.8', provider: 'anthropic-oauth', contextWindow: 1000000 },
270
+ { id: 'claude-opus-4-7', name: 'Claude Opus 4.7', provider: 'anthropic-oauth', contextWindow: 1000000 },
271
+ { id: 'claude-opus-4-6', name: 'Claude Opus 4.6', provider: 'anthropic-oauth', contextWindow: 1000000 },
272
+ { id: 'claude-sonnet-4-6', name: 'Claude Sonnet 4.6', provider: 'anthropic-oauth', contextWindow: 1000000 },
273
+ { id: 'claude-haiku-4-5-20251001', name: 'Claude Haiku 4.5', provider: 'anthropic-oauth', contextWindow: 200000 },
274
+ ];
275
+
276
+ // Per-model max_tokens when the model id is explicitly listed. New models
277
+ // (e.g., Sonnet 4.7) won't match a specific entry and fall through to the
278
+ // family-based heuristic below. Conservative defaults — model may support
279
+ // more but we'd rather stay within safe bounds.
280
+ const MAX_TOKENS = {
281
+ 'claude-opus-4-8': 65536,
282
+ 'claude-opus-4-7': 65536,
283
+ 'claude-opus-4-6': 65536,
284
+ 'claude-sonnet-4-6': 16384,
285
+ 'claude-haiku-4-5-20251001': 8192,
286
+ };
287
+
288
+ function resolveMaxTokens(model) {
289
+ if (MAX_TOKENS[model]) return MAX_TOKENS[model];
290
+ const id = String(model || '').toLowerCase();
291
+ if (id.includes('opus')) return 65536;
292
+ if (id.includes('sonnet')) return 16384;
293
+ if (id.includes('haiku')) return 8192;
294
+ return 8192;
295
+ }
296
+
297
+ const EFFORT_BUDGET = {
298
+ low: 1024,
299
+ medium: 4096,
300
+ high: 16384,
301
+ xhigh: 32768,
302
+ max: 32768,
303
+ };
304
+
305
+ // Tracks which unknown effort labels we've already logged so a repeated
306
+ // session-level misconfig doesn't flood stderr with the same warning.
307
+ const _LOGGED_UNKNOWN_EFFORT = new Set();
308
+
309
+ // Layered cache TTLs — stable layers get 1h, volatile layers get 5m.
310
+ // Anthropic requires 1h entries to appear before 5m entries in the request.
311
+ const CACHE_TTL_STABLE = { type: 'ephemeral', ttl: '1h' }; // tools, system
312
+ const CACHE_TTL_VOLATILE = { type: 'ephemeral' }; // messages (5m default)
313
+
314
+ // --- Credential helpers ---
315
+
316
+ function _pushUnique(list, value) {
317
+ if (!value || typeof value !== 'string') return;
318
+ if (!list.includes(value)) list.push(value);
319
+ }
320
+
321
+ function _claudeCredentialsFromPluginRoot(root) {
322
+ const clean = String(root || '').replace(/\\/g, '/');
323
+ const marker = '/.claude/plugins/';
324
+ const idx = clean.indexOf(marker);
325
+ if (idx < 0) return null;
326
+ return `${clean.slice(0, idx)}/.claude/.credentials.json`;
327
+ }
328
+
329
+ function credentialCandidates() {
330
+ const paths = [];
331
+ _pushUnique(paths, process.env.CLAUDE_CODE_CREDENTIALS_PATH);
332
+ _pushUnique(paths, process.env.CLAUDE_CREDENTIALS_PATH);
333
+ _pushUnique(paths, _claudeCredentialsFromPluginRoot(process.env.CLAUDE_PLUGIN_ROOT));
334
+ _pushUnique(paths, DEFAULT_CREDENTIALS_PATH);
335
+ return paths;
336
+ }
337
+
338
+ // Fallback expiry from the access_token's JWT `exp` claim (epoch ms) when the
339
+ // credentials file carries no explicit expiresAt — without it expiresAt stays 0,
340
+ // which ensureAuth reads as "never expires", disabling proactive refresh. Claude
341
+ // OAuth tokens are opaque so this returns 0 and the file's expiresAt governs; kept
342
+ // for parity with the other OAuth providers. JWT `exp` is epoch SECONDS (RFC 7519).
343
+ function _expiryFromAccessToken(token) {
344
+ try {
345
+ const parts = String(token || '').split('.');
346
+ if (parts.length !== 3) return 0;
347
+ const payload = JSON.parse(Buffer.from(parts[1], 'base64').toString('utf-8'));
348
+ const exp = Number(payload?.exp);
349
+ return Number.isFinite(exp) && exp > 0 ? exp * 1000 : 0;
350
+ } catch { return 0; }
351
+ }
352
+
353
+ function _loadCredentialsFile(path) {
354
+ if (!existsSync(path)) return null;
355
+ try {
356
+ const stat = statSync(path);
357
+ const raw = JSON.parse(readFileSync(path, 'utf-8'));
358
+ const oauth = raw?.claudeAiOauth;
359
+ if (!oauth?.accessToken) return null;
360
+ return {
361
+ path,
362
+ mtimeMs: stat.mtimeMs,
363
+ accessToken: oauth.accessToken,
364
+ refreshToken: oauth.refreshToken || null,
365
+ expiresAt: _normalizeExpiresAt(oauth.expiresAt ?? oauth.expires_at) || _expiryFromAccessToken(oauth.accessToken),
366
+ scopes: Array.isArray(oauth.scopes) ? oauth.scopes : [],
367
+ subscriptionType: oauth.subscriptionType || null,
368
+ };
369
+ } catch {
370
+ return null;
371
+ }
372
+ }
373
+
374
+ // Cross-process safe write-back. Lockfile (O_EXCL) prevents two refreshers
375
+ // from clobbering each other; atomic rename guarantees readers see either
376
+ // the old or new file, never a half-written one. Used so refresh_token
377
+ // rotation propagates to host Claude Code (and any other reader of the
378
+ // same credentials file) instead of leaving them stuck on the previous
379
+ // refresh_token. Mirrors openai-oauth.mjs:saveTokens.
380
+ function _saveCredentialsFile(path, raw) {
381
+ // No `secret: true`: this is the HOST-owned credentials file (~/.claude/
382
+ // .credentials.json) — mixdog only writes back the rotated refresh_token,
383
+ // it must not re-permission a file Claude Code owns. (Forcing an owner-
384
+ // only ACL here also used to clamp the parent ~/.claude and wipe the
385
+ // whole tree's DACLs — see atomic-file.mjs secret-write note.)
386
+ writeJsonAtomicSync(path, raw, { lock: true, fsyncDir: true, mode: 0o600 });
387
+ }
388
+
389
+ // Cheap stat-only probe so ensureAuth can detect host-rotated credentials
390
+ // (claude login, logout/relogin) without paying a full JSON read every call.
391
+ function _credentialsMaxMtime() {
392
+ let max = 0;
393
+ for (const p of credentialCandidates()) {
394
+ try {
395
+ const s = statSync(p);
396
+ if (s.mtimeMs > max) max = s.mtimeMs;
397
+ } catch { /* not present — skip */ }
398
+ }
399
+ return max;
400
+ }
401
+
402
+ function loadCredentials() {
403
+ const loaded = credentialCandidates()
404
+ .map(_loadCredentialsFile)
405
+ .filter(Boolean);
406
+ if (!loaded.length) return null;
407
+ loaded.sort((a, b) => (Number(b.expiresAt) || 0) - (Number(a.expiresAt) || 0));
408
+ return loaded[0];
409
+ }
410
+
411
+ // Public predicate used by config.buildDefaultConfig — provider is enabled
412
+ // when on-disk credentials exist AND carry the inference scope. Single
413
+ // truth: same loader the runtime uses, no parallel hard-coded path probe.
414
+ export function hasAnthropicOAuthCredentials() {
415
+ const creds = loadCredentials();
416
+ if (!creds?.accessToken) return false;
417
+ return Array.isArray(creds.scopes) && creds.scopes.includes('user:inference');
418
+ }
419
+
420
+ function _normalizeExpiresAt(value) {
421
+ if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) return 0;
422
+ return value < 1e12 ? value * 1000 : value;
423
+ }
424
+
425
+ function _scrubTokens(text) {
426
+ return String(text || '')
427
+ .replace(/Bearer [A-Za-z0-9._\-]+/g, 'Bearer [REDACTED]')
428
+ .replace(/sk-ant-[A-Za-z0-9._\-]+/g, '[REDACTED]')
429
+ .replace(/"access[Tt]oken"\s*:\s*"[^"]+"/g, '"accessToken":"[REDACTED]"')
430
+ .replace(/"refresh[Tt]oken"\s*:\s*"[^"]+"/g, '"refreshToken":"[REDACTED]"')
431
+ .replace(/"access_token"\s*:\s*"[^"]+"/g, '"access_token":"[REDACTED]"')
432
+ .replace(/"refresh_token"\s*:\s*"[^"]+"/g, '"refresh_token":"[REDACTED]"');
433
+ }
434
+
435
+ async function refreshOAuthCredentials(creds) {
436
+ if (!creds?.refreshToken) {
437
+ throw new Error('Anthropic OAuth refresh token not available. Run "claude login" to re-authenticate.');
438
+ }
439
+
440
+ const controller = new AbortController();
441
+ const timeout = setTimeout(() => controller.abort(), 30_000);
442
+ try {
443
+ const res = await fetch(TOKEN_URL, {
444
+ method: 'POST',
445
+ headers: {
446
+ 'Content-Type': 'application/json',
447
+ 'anthropic-dangerous-direct-browser-access': 'true',
448
+ 'user-agent': `claude-cli/${resolveCliVersion()} (external, sdk-cli)`,
449
+ },
450
+ body: JSON.stringify({
451
+ grant_type: 'refresh_token',
452
+ refresh_token: creds.refreshToken,
453
+ client_id: CLAUDE_CODE_CLIENT_ID,
454
+ }),
455
+ // Never follow a redirect on a secret-bearing request: a token
456
+ // endpoint that 307/308-redirects would replay the refresh_token to
457
+ // the redirect target. Fail loud instead.
458
+ redirect: 'error',
459
+ signal: controller.signal,
460
+ dispatcher: getLlmDispatcher(),
461
+ });
462
+
463
+ const text = await res.text();
464
+ let json = null;
465
+ try { json = text ? JSON.parse(text) : null; } catch { /* handled below */ }
466
+ if (!res.ok) {
467
+ const isInvalidGrant = text.includes('invalid_grant') || json?.error === 'invalid_grant';
468
+ throw Object.assign(new Error(`token refresh ${res.status}: ${_scrubTokens(text).slice(0, 200)}`), { isInvalidGrant });
469
+ }
470
+
471
+ const accessToken = json?.access_token || json?.accessToken;
472
+ if (!accessToken) throw new Error('token refresh returned no access token');
473
+ const expiresAt = _normalizeExpiresAt(json?.expires_at ?? json?.expiresAt)
474
+ || (typeof json?.expires_in === 'number' ? Date.now() + json.expires_in * 1000 : 0);
475
+ const refreshed = {
476
+ path: creds.path,
477
+ accessToken,
478
+ refreshToken: json?.refresh_token || json?.refreshToken || creds.refreshToken,
479
+ expiresAt,
480
+ scopes: Array.isArray(json?.scope) ? json.scope : creds.scopes,
481
+ subscriptionType: creds.subscriptionType,
482
+ };
483
+ // Persist rotated tokens back so host Claude Code and any other
484
+ // reader of the same credentials file pick up the new refresh_token.
485
+ // Without this, host's next refresh invalidates our copy and we
486
+ // loop on invalid_grant.
487
+ if (creds.path && existsSync(creds.path)) {
488
+ try {
489
+ const raw = JSON.parse(readFileSync(creds.path, 'utf-8'));
490
+ raw.claudeAiOauth = {
491
+ ...(raw.claudeAiOauth || {}),
492
+ accessToken: refreshed.accessToken,
493
+ refreshToken: refreshed.refreshToken,
494
+ expiresAt: refreshed.expiresAt,
495
+ scopes: refreshed.scopes,
496
+ };
497
+ _saveCredentialsFile(creds.path, raw);
498
+ } catch (err) {
499
+ process.stderr.write(`[anthropic-oauth] credential write-back failed: ${_scrubTokens(err?.message || String(err)).slice(0, 200)}\n`);
500
+ throw new Error(`[oauth] credentials write-back failed: ${err?.message ?? String(err)}`);
501
+ }
502
+ }
503
+ return refreshed;
504
+ } catch (err) {
505
+ if (err?.name === 'AbortError') {
506
+ throw new Error('Anthropic OAuth token refresh timed out after 30000ms');
507
+ }
508
+ throw err;
509
+ } finally {
510
+ clearTimeout(timeout);
511
+ }
512
+ }
513
+
514
+ async function refreshOAuthCredentialsWithFallback(creds) {
515
+ try {
516
+ return await refreshOAuthCredentials(creds);
517
+ } catch (firstErr) {
518
+ if (!firstErr.isInvalidGrant) throw firstErr;
519
+ // invalid_grant: another writer rotated the refresh_token between
520
+ // our read and refresh. Re-read disk to pick up the rotation and
521
+ // retry once. If the on-disk creds still match what we just failed
522
+ // with, the user must re-auth via host Claude Code.
523
+ process.stderr.write(`[anthropic-oauth] invalid_grant — re-reading disk, retrying refresh\n`);
524
+ const fresh = loadCredentials();
525
+ if (!fresh?.refreshToken || fresh.refreshToken === creds.refreshToken) throw new ReauthRequired(firstErr.message);
526
+ try {
527
+ return await refreshOAuthCredentials(fresh);
528
+ } catch (secondErr) {
529
+ if (secondErr.isInvalidGrant) throw new ReauthRequired(secondErr.message);
530
+ throw secondErr;
531
+ }
532
+ }
533
+ }
534
+
535
+ // Exported so callers can detect re-auth-required scenarios and prompt the user.
536
+ export class ReauthRequired extends Error {
537
+ constructor(message) {
538
+ super(message);
539
+ this.name = 'ReauthRequired';
540
+ }
541
+ }
542
+
543
+ // --- Message conversion (mirrors anthropic.mjs) ---
544
+
545
+ function withCacheControl(block, ttl = CACHE_TTL_VOLATILE) {
546
+ if (!block || typeof block !== 'object' || block.cache_control) return block;
547
+ return { ...block, cache_control: ttl };
548
+ }
549
+
550
+ function appendCacheControl(content, ttl = CACHE_TTL_VOLATILE) {
551
+ if (Array.isArray(content)) {
552
+ if (content.length === 0) return content;
553
+ const next = [...content];
554
+ next[next.length - 1] = withCacheControl(next[next.length - 1], ttl);
555
+ return next;
556
+ }
557
+ if (typeof content === 'string') {
558
+ return [withCacheControl({ type: 'text', text: content }, ttl)];
559
+ }
560
+ return content;
561
+ }
562
+
563
+ function collectRecentCacheableIndexes(messages, availableSlots = 2) {
564
+ // Anthropic enforces a 4-breakpoint max per request. Callers reserve slots
565
+ // for tools[-1] and system breakpoints (typically 2); whatever remains is
566
+ // spread across messages as 5m breakpoints.
567
+ //
568
+ // Anchor strategy when only ONE message slot is available — pin the
569
+ // single marker to the FIRST chat message (typically the locked task
570
+ // brief) instead of the sliding tail. Reason: a tail marker shifts
571
+ // position every iter (messages.length grows as tool turns accumulate),
572
+ // and Anthropic caches by prefix-bytes-up-to-marker, so a moving tail
573
+ // creates a NEW prefix every iter — which means cache_creation fires
574
+ // every loop on first-time-seen prefixes (no prior 1h slot warmed up,
575
+ // 1h indexing latency blocks intra-call read). Pinning the marker to
576
+ // a stable position keeps the prefix bytes identical across iters so
577
+ // 5m cache can read on the second iter onward, dramatically cutting
578
+ // first-call cost when the loop runs N>1 turns.
579
+ //
580
+ // Multi-slot path (slots>=2) still uses the sliding tail for the
581
+ // remaining slots so the most-recent message also gets cached for the
582
+ // benefit of cross-call hits within the 5m window.
583
+ const slots = Math.max(0, Math.min(4, availableSlots));
584
+ if (slots === 0) return new Set();
585
+ const marked = new Set();
586
+ let firstChat = -1;
587
+ for (let i = 0; i < messages.length; i++) {
588
+ if (messages[i]?.role !== 'system') { firstChat = i; break; }
589
+ }
590
+ if (firstChat < 0) return marked;
591
+ marked.add(firstChat);
592
+ if (slots === 1) return marked;
593
+ for (let i = messages.length - 1; i >= 0 && marked.size < slots; i--) {
594
+ if (messages[i]?.role !== 'system') marked.add(i);
595
+ }
596
+ return marked;
597
+ }
598
+
599
+ // Anthropic's tool spec forbids oneOf / allOf / anyOf at the TOP level of
600
+ // input_schema (nested usage inside properties is allowed). External MCP
601
+ // servers (e.g. Claude Code's built-in tools) sometimes emit such schemas.
602
+ // Convert them to a flat object schema so the API never sees a 400.
603
+ function _sanitizeInputSchema(schema, toolName) {
604
+ if (!schema || typeof schema !== 'object') {
605
+ return { type: 'object', properties: {} };
606
+ }
607
+ const compound = schema.oneOf || schema.anyOf || schema.allOf;
608
+ if (!compound) return structuredClone(schema);
609
+ // Merge all branch properties into one permissive object schema.
610
+ // None of the branches' required lists are hoisted — callers that relied
611
+ // on discriminated-union semantics will still function; the model simply
612
+ // receives a union of the property surface with no hard-required constraint.
613
+ const mergedProps = {};
614
+ const branchDescs = [];
615
+ for (const branch of Array.isArray(compound) ? compound : []) {
616
+ if (branch && typeof branch === 'object' && branch.properties) {
617
+ Object.assign(mergedProps, branch.properties);
618
+ }
619
+ if (branch && typeof branch === 'object') {
620
+ const parts = [];
621
+ if (branch.description) parts.push(branch.description);
622
+ else if (branch.type) parts.push(`type:${branch.type}`);
623
+ if (parts.length) branchDescs.push(parts.join(' '));
624
+ }
625
+ }
626
+ const compoundKey = schema.oneOf ? 'oneOf' : schema.anyOf ? 'anyOf' : 'allOf';
627
+ let description = schema.description || '';
628
+ if (branchDescs.length) {
629
+ const parts = [];
630
+ let used = 0;
631
+ for (let i = 0; i < branchDescs.length; i++) {
632
+ const v = `(variant ${i + 1}: ${branchDescs[i]})`;
633
+ if (used + v.length + (parts.length ? 1 : 0) > 500) break;
634
+ parts.push(v);
635
+ used += v.length + (parts.length > 1 ? 1 : 0);
636
+ }
637
+ const addition = parts.join(' ');
638
+ if (addition) description = description ? `${description} ${addition}` : addition;
639
+ }
640
+ const mergedPropsCount = Object.keys(mergedProps).length;
641
+ process.stderr.write(
642
+ `[anthropic-oauth-sanitizer] tool="${toolName ?? ''}" compound="${compoundKey}" branches=${Array.isArray(compound) ? compound.length : 0} mergedProps=${mergedPropsCount}\n`
643
+ );
644
+ return {
645
+ type: 'object',
646
+ ...(description ? { description } : {}),
647
+ properties: mergedProps,
648
+ };
649
+ }
650
+
651
+ function toAnthropicTools(tools) {
652
+ return tools.map(t => ({
653
+ name: t.name,
654
+ description: t.description,
655
+ input_schema: _sanitizeInputSchema(t.inputSchema, t.name),
656
+ }));
657
+ }
658
+
659
+ function toAnthropicMessages(
660
+ messages,
661
+ cacheableIndexes = new Set(),
662
+ messageTtl = CACHE_TTL_VOLATILE,
663
+ tier3Idx = -1,
664
+ tier3Ttl = null,
665
+ ) {
666
+ // messageTtl === null disables message-tail caching.
667
+ // tier3Ttl === null disables the dedicated Tier 3 breakpoint.
668
+ const applyMsgTtl = messageTtl || CACHE_TTL_VOLATILE;
669
+ const shouldCacheMsg = (idx) => messageTtl !== null && cacheableIndexes.has(idx);
670
+ const shouldCacheTier3 = (idx) => tier3Ttl !== null && idx === tier3Idx;
671
+ const pickTtl = (idx) => shouldCacheTier3(idx) ? tier3Ttl : applyMsgTtl;
672
+ const anyCache = (idx) => shouldCacheMsg(idx) || shouldCacheTier3(idx);
673
+
674
+ const result = [];
675
+ for (let idx = 0; idx < messages.length; idx++) {
676
+ const m = messages[idx];
677
+ if (m.role === 'system') continue;
678
+
679
+ if (m.role === 'assistant' && m.toolCalls?.length) {
680
+ let content = [];
681
+ if (m.content) content.push({ type: 'text', text: m.content });
682
+ for (const tc of m.toolCalls) {
683
+ content.push({
684
+ type: 'tool_use',
685
+ id: tc.id,
686
+ name: tc.name,
687
+ input: tc.arguments,
688
+ });
689
+ }
690
+ if (anyCache(idx)) content = appendCacheControl(content, pickTtl(idx));
691
+ result.push({ role: 'assistant', content });
692
+ continue;
693
+ }
694
+
695
+ if (m.role === 'tool') {
696
+ const last = result[result.length - 1];
697
+ const block = {
698
+ type: 'tool_result',
699
+ tool_use_id: m.toolCallId || '',
700
+ content: m.content,
701
+ };
702
+ if (last?.role === 'user' && Array.isArray(last.content)) {
703
+ last.content.push(block);
704
+ if (anyCache(idx)) {
705
+ last.content = appendCacheControl(last.content, pickTtl(idx));
706
+ }
707
+ } else {
708
+ let content = [block];
709
+ if (anyCache(idx)) content = appendCacheControl(content, pickTtl(idx));
710
+ result.push({ role: 'user', content });
711
+ }
712
+ continue;
713
+ }
714
+
715
+ const content = anyCache(idx)
716
+ ? appendCacheControl(m.content, pickTtl(idx))
717
+ : m.content;
718
+ result.push({ role: m.role, content });
719
+ }
720
+ return sanitizeAnthropicContentPairs(result);
721
+ }
722
+
723
+ // --- SSE parser ---
724
+
725
+ function _captureMidstreamAbort(state, reason) {
726
+ if (!state) return;
727
+ const reasonName = reason?.name || '';
728
+ if (reasonName === 'BridgeStallAbortError' || reasonName === 'StreamStalledAbortError') {
729
+ state.watchdogAbort = reasonName;
730
+ } else {
731
+ state.userAbort = true;
732
+ }
733
+ }
734
+
735
+ async function parseSSEStream(response, signal, abortStream, onStreamDelta, onToolCall, state) {
736
+ const reader = response.body.getReader();
737
+ const decoder = new TextDecoder();
738
+ const SSE_IDLE_TIMEOUT_MS = PROVIDER_SSE_IDLE_TIMEOUT_MS;
739
+ let content = '';
740
+ let hasThinkingContent = false;
741
+ const contentBlockTypes = new Set();
742
+ let model = '';
743
+ let toolCalls = [];
744
+ let usage = { inputTokens: 0, outputTokens: 0, cachedTokens: 0, cacheWriteTokens: 0, raw: null };
745
+ let stopReason = null;
746
+ let buffer = '';
747
+ let idleTimedOut = false;
748
+ let idleTimer = null;
749
+ let currentEvent = '';
750
+
751
+ const pendingToolInputs = new Map();
752
+
753
+ // Holds the in-flight reader.read() race rejector so the idle timer can
754
+ // force-unblock the loop even when reader.cancel() fails to settle the
755
+ // pending read (undici half-open socket). See resetIdleTimer below.
756
+ let idleReject = null;
757
+
758
+ const resetIdleTimer = () => {
759
+ // OFF by default (matches Claude Code native gate). When disabled the
760
+ // idle timer never arms, so the stream is never killed on inactivity;
761
+ // the bridge stall watchdog (600s) remains the dead-stream backstop.
762
+ if (!PROVIDER_SSE_IDLE_WATCHDOG_ENABLED) return;
763
+ if (idleTimer) clearTimeout(idleTimer);
764
+ idleTimer = setTimeout(() => {
765
+ idleTimedOut = true;
766
+ try { abortStream?.(); } catch (err) {
767
+ try { process.stderr.write(`[anthropic-oauth] sse idle abortStream failed: ${err?.message ?? String(err)}\n`); } catch {}
768
+ }
769
+ try {
770
+ const _c = reader.cancel('SSE idle timeout');
771
+ if (_c && typeof _c.catch === 'function') _c.catch(() => {});
772
+ } catch (err) {
773
+ try { process.stderr.write(`[anthropic-oauth] sse idle cancel failed: ${err?.message ?? String(err)}\n`); } catch {}
774
+ }
775
+ // Force-reject the in-flight reader.read() race even when reader.cancel()
776
+ // fails to settle the pending read: without this the await below stays
777
+ // pending forever and the SSE idle timeout never unblocks the loop —
778
+ // the 391s-hang root cause.
779
+ if (idleReject) {
780
+ const e = new Error(`Anthropic OAuth SSE stream timed out after ${SSE_IDLE_TIMEOUT_MS}ms of inactivity`);
781
+ e.code = 'ETIMEDOUT';
782
+ const r = idleReject; idleReject = null; r(e);
783
+ }
784
+ // Shared provider policy: short inter-chunk inactivity catches the
785
+ // sess_9cfd11-class stuck pattern where SSE starts but then goes silent.
786
+ }, SSE_IDLE_TIMEOUT_MS);
787
+ };
788
+
789
+ const onAbort = () => {
790
+ try {
791
+ const _c = reader.cancel('SSE aborted');
792
+ if (_c && typeof _c.catch === 'function') _c.catch(() => {});
793
+ } catch {}
794
+ };
795
+ if (signal) {
796
+ if (signal.aborted) {
797
+ _captureMidstreamAbort(state, signal.reason);
798
+ throw signal.reason instanceof Error
799
+ ? signal.reason
800
+ : new Error('Anthropic OAuth SSE stream aborted');
801
+ }
802
+ signal.addEventListener('abort', onAbort, { once: true });
803
+ }
804
+
805
+ try {
806
+ resetIdleTimer();
807
+ streamLoop: while (true) {
808
+ let chunk;
809
+ try {
810
+ // Race the read against the idle timer's rejector so a stuck
811
+ // reader.read() (cancel did not settle it) still unblocks here.
812
+ chunk = await new Promise((resolve, reject) => {
813
+ idleReject = reject;
814
+ reader.read().then(resolve, reject);
815
+ });
816
+ } catch (err) {
817
+ if (idleTimedOut) {
818
+ const idleErr = new Error(`Anthropic OAuth SSE stream timed out after ${SSE_IDLE_TIMEOUT_MS}ms of inactivity`);
819
+ idleErr.code = 'ETIMEDOUT';
820
+ throw idleErr;
821
+ }
822
+ if (signal?.aborted) {
823
+ _captureMidstreamAbort(state, signal.reason);
824
+ throw signal.reason instanceof Error
825
+ ? signal.reason
826
+ : new Error('Anthropic OAuth SSE stream aborted');
827
+ }
828
+ throw err;
829
+ }
830
+ const { done, value } = chunk;
831
+ if (done) break;
832
+
833
+ resetIdleTimer();
834
+ buffer += decoder.decode(value, { stream: true });
835
+ const lines = buffer.split('\n');
836
+ buffer = lines.pop() || '';
837
+
838
+ for (const line of lines) {
839
+ if (line.startsWith(':')) {
840
+ // SSE comment frame (Anthropic `:ping` keepalive). The HTML Standard SSE
841
+ // spec says comments are silently ignored, but we surface them here so
842
+ // the bridge-stall-watchdog sees the stream is still alive during Opus
843
+ // extended-thinking pauses. No content is emitted — this only refreshes
844
+ // the runtime's lastStreamDeltaAt timestamp.
845
+ try { onStreamDelta?.(); } catch {}
846
+ continue;
847
+ }
848
+ if (line.startsWith('event: ')) {
849
+ currentEvent = line.slice(7).trim();
850
+ continue;
851
+ }
852
+ if (!line.startsWith('data: ')) continue;
853
+ const data = line.slice(6).trim();
854
+ if (!data) continue;
855
+
856
+ try {
857
+ const event = JSON.parse(data);
858
+
859
+ if (event.type === 'message_start' && event.message) {
860
+ if (state) state.sawMessageStart = true;
861
+ if (event.message.model) model = event.message.model;
862
+ if (event.message.usage) {
863
+ usage.inputTokens = event.message.usage.input_tokens || 0;
864
+ usage.cachedTokens = event.message.usage.cache_read_input_tokens || 0;
865
+ usage.cacheWriteTokens = event.message.usage.cache_creation_input_tokens || 0;
866
+ usage.raw = { ...event.message.usage };
867
+ }
868
+ }
869
+
870
+ if (event.type === 'content_block_start') {
871
+ const block = event.content_block;
872
+ if (block?.type === 'tool_use') {
873
+ pendingToolInputs.set(event.index, {
874
+ id: block.id || '',
875
+ name: block.name || '',
876
+ inputJson: '',
877
+ });
878
+ }
879
+ }
880
+
881
+ if (event.type === 'content_block_delta') {
882
+ const delta = event.delta;
883
+ if (delta?.type) contentBlockTypes.add(delta.type);
884
+ if (delta?.type === 'text_delta') {
885
+ content += delta.text || '';
886
+ try { onStreamDelta?.(); } catch {}
887
+ }
888
+ if (delta?.type === 'thinking_delta' || delta?.type === 'signature_delta') {
889
+ // Extended-thinking block: provider reasoning without
890
+ // user-visible text. Track presence so a final turn
891
+ // that emitted ONLY thinking (no text_delta, no
892
+ // tool_use) can be classified by the loop as
893
+ // synthesis-stalled rather than silent empty.
894
+ hasThinkingContent = true;
895
+ try { onStreamDelta?.(); } catch {}
896
+ }
897
+ if (delta?.type === 'input_json_delta') {
898
+ const pending = pendingToolInputs.get(event.index);
899
+ if (pending) {
900
+ pending.inputJson += delta.partial_json || '';
901
+ }
902
+ try { onStreamDelta?.(); } catch {}
903
+ }
904
+ }
905
+
906
+ if (event.type === 'content_block_stop') {
907
+ const pending = pendingToolInputs.get(event.index);
908
+ if (pending) {
909
+ // Bare JSON.parse threw straight up into the
910
+ // surrounding broad catch, which swallowed the
911
+ // whole tool_call — the loop never saw it and
912
+ // the assistant turn ended with an unmatched
913
+ // tool_use id. Wrap the parse so a malformed
914
+ // input still produces a tool_call (with empty
915
+ // arguments and a logged error) instead of a
916
+ // silent drop.
917
+ let parsedArgs = {};
918
+ if (pending.inputJson) {
919
+ try { parsedArgs = JSON.parse(pending.inputJson); }
920
+ catch (parseErr) {
921
+ process.stderr.write(`[anthropic-oauth] tool args JSON.parse failed (id=${pending.id}, name=${pending.name}): ${parseErr?.message || parseErr}\n`);
922
+ parsedArgs = {};
923
+ }
924
+ }
925
+ const call = {
926
+ id: pending.id,
927
+ name: pending.name,
928
+ arguments: parsedArgs,
929
+ };
930
+ toolCalls.push(call);
931
+ pendingToolInputs.delete(event.index);
932
+ if (state) state.emittedToolCall = true;
933
+ // Eager dispatch: let the loop start this tool
934
+ // before message_stop arrives. The loop keys
935
+ // pending promises by call.id so order is safe.
936
+ try { onToolCall?.(call); } catch {}
937
+ try { onStreamDelta?.(); } catch {}
938
+ }
939
+ }
940
+
941
+ if (event.type === 'message_delta') {
942
+ if (event.delta?.stop_reason) {
943
+ stopReason = event.delta.stop_reason;
944
+ }
945
+ if (event.usage) {
946
+ usage.outputTokens = event.usage.output_tokens || 0;
947
+ usage.raw = { ...(usage.raw || {}), ...event.usage };
948
+ }
949
+ if (stopReason === 'tool_use' && toolCalls.length > 0 && pendingToolInputs.size === 0) {
950
+ if (state) state.sawCompleted = true;
951
+ break streamLoop;
952
+ }
953
+ }
954
+ if (event.type === 'message_stop') {
955
+ if (state) state.sawCompleted = true;
956
+ // Anthropic streams can keep emitting `:ping` keepalive
957
+ // frames after `message_stop`; if we wait for EOF the
958
+ // outer reader.read() loop hangs indefinitely. Break
959
+ // out of streamLoop the moment the message ends.
960
+ break streamLoop;
961
+ }
962
+ // Unified prompt volume — what the model actually ingested.
963
+ // Anthropic splits input into three billable slots (uncached
964
+ // input + cache_read + cache_create); keep them separate for
965
+ // cost math but also expose the sum so cross-provider logs
966
+ // have a consistent `promptTokens` meaning.
967
+ usage.promptTokens = (usage.inputTokens || 0)
968
+ + (usage.cachedTokens || 0)
969
+ + (usage.cacheWriteTokens || 0);
970
+ } catch { /* skip malformed events */ }
971
+ }
972
+ }
973
+
974
+ // Truncated-stream guard: if the reader loop exited (EOF or break)
975
+ // after message_start but without seeing message_stop / a tool_use
976
+ // stop_reason, the assistant turn was cut off mid-flight. Returning
977
+ // success here would silently surface partial content (or a partially
978
+ // streamed tool_use whose input_json never completed) as final.
979
+ // Throw a typed truncated-stream error so the loop can decide whether
980
+ // to retry, surface, or escalate instead of accepting the partial.
981
+ if (state?.sawMessageStart && !state?.sawCompleted) {
982
+ const pendingToolUse = pendingToolInputs.size > 0;
983
+ const err = Object.assign(
984
+ new Error(
985
+ `Anthropic OAuth SSE stream truncated: message_start without message_stop`
986
+ + (pendingToolUse ? ` (pending tool_use input)` : ''),
987
+ ),
988
+ {
989
+ name: 'TruncatedStreamError',
990
+ code: 'TRUNCATED_STREAM',
991
+ truncatedStream: true,
992
+ pendingToolUse,
993
+ stopReason,
994
+ },
995
+ );
996
+ throw err;
997
+ }
998
+
999
+ return {
1000
+ content,
1001
+ model,
1002
+ toolCalls: toolCalls.length ? toolCalls : undefined,
1003
+ usage,
1004
+ stopReason,
1005
+ hasThinkingContent,
1006
+ contentBlockTypes: Array.from(contentBlockTypes),
1007
+ };
1008
+ } finally {
1009
+ if (idleTimer) clearTimeout(idleTimer);
1010
+ if (signal) signal.removeEventListener('abort', onAbort);
1011
+ try { reader.releaseLock(); } catch (err) {
1012
+ try { process.stderr.write(`[anthropic-oauth] reader releaseLock failed: ${err?.message ?? String(err)}\n`); } catch {}
1013
+ }
1014
+ }
1015
+ }
1016
+
1017
+ /**
1018
+ * Classify an Anthropic SSE failure for single-shot mid-stream retry.
1019
+ *
1020
+ * Retry is allowed only after `message_start` and before `message_stop`,
1021
+ * and only when no tool call has already been surfaced to the loop.
1022
+ * That keeps recovery limited to transport/stream stalls without risking
1023
+ * duplicate eager tool execution.
1024
+ */
1025
+ export function _classifyMidstreamError(err, state) {
1026
+ if (!state) return null;
1027
+ if ((state.attemptIndex | 0) >= 1) return null;
1028
+ if (state.sawCompleted) return null;
1029
+ if (!state.sawMessageStart) return null;
1030
+ if (state.userAbort) return null;
1031
+ if (state.emittedToolCall) return null;
1032
+
1033
+ if (!err) return null;
1034
+ const status = Number(err?.httpStatus || 0);
1035
+ if (status === 401 || status === 403 || status === 429) return null;
1036
+
1037
+ const name = err?.name || '';
1038
+ if (name === 'BridgeStallAbortError') return 'bridge_stall';
1039
+ if (name === 'StreamStalledAbortError') return 'stream_stalled';
1040
+ if (state.watchdogAbort === 'BridgeStallAbortError') return 'bridge_stall';
1041
+ if (state.watchdogAbort === 'StreamStalledAbortError') return 'stream_stalled';
1042
+
1043
+ const code = err?.code || err?.cause?.code || '';
1044
+ if (code === 'ECONNRESET') return 'reset';
1045
+ if (code === 'ETIMEDOUT' || code === 'ESOCKETTIMEDOUT') return 'timeout';
1046
+ if (code === 'ENOTFOUND' || code === 'EAI_AGAIN' || code === 'EAI_NODATA') return 'dns';
1047
+
1048
+ const msg = String(err?.message || '').toLowerCase();
1049
+ if (msg.includes('stream timed out after') && msg.includes('of inactivity')) return 'sse_idle_timeout';
1050
+ if (msg.includes('body stream') && msg.includes('terminated')) return 'stream_terminated';
1051
+ if (msg.includes('fetch failed')) return 'fetch_failed';
1052
+
1053
+ return null;
1054
+ }
1055
+
1056
+ // --- Build request body ---
1057
+
1058
+ function resolveCacheTtls(opts) {
1059
+ // Layered cache strategy — caller may override per-layer via opts.cacheStrategy.
1060
+ // Anthropic enforces: 1h entries must appear before 5m entries in the request.
1061
+ const strategy = opts.cacheStrategy || {};
1062
+ const pick = (layer, fallback) => {
1063
+ const v = strategy[layer];
1064
+ if (v === '1h') return CACHE_TTL_STABLE;
1065
+ if (v === '5m') return CACHE_TTL_VOLATILE;
1066
+ if (v === 'none') return null;
1067
+ return fallback;
1068
+ };
1069
+ // BP budget (4 total):
1070
+ // BP1 baseRules — 1h (shared across ALL roles)
1071
+ // BP2 roleCatalog — 1h (shared across ALL roles)
1072
+ // BP3 tier3 — 1h (sessionMarker: role + permission + project)
1073
+ // BP4 messages — 5m sliding tail (tool_result cache across iter)
1074
+ // tools BP is dropped — system BP covers the tools prefix via
1075
+ // Anthropic's prompt cache prefix semantics (order: tools → system
1076
+ // → messages).
1077
+ // tier3 defaults to 1h (stable) — sessionMarker content is stable per
1078
+ // (role, permission, project) tuple and Anthropic only spends the BP
1079
+ // slot when findTier3Index() actually finds a <system-reminder> block,
1080
+ // so this default is free for sessions that don't carry one. Previously
1081
+ // null here meant any caller that skipped smart bridge resolve (CLI,
1082
+ // raw bridge spawn) silently lost the tier3 cache layer even
1083
+ // though their message layout supported it.
1084
+ return {
1085
+ tools: pick('tools', CACHE_TTL_STABLE),
1086
+ system: pick('system', CACHE_TTL_STABLE),
1087
+ tier3: pick('tier3', CACHE_TTL_STABLE),
1088
+ messages: pick('messages', CACHE_TTL_VOLATILE),
1089
+ };
1090
+ }
1091
+
1092
+ // Tier 3 is injected by session/manager as a user message wrapped in
1093
+ // `<system-reminder>` whose body starts with the explicit sentinel
1094
+ // `<!-- bp3-sentinel -->` (emitted by collect.mjs:composeSystemPrompt only
1095
+ // when a stable projectContext is present). The sentinel is mandatory:
1096
+ // volatileTail (role/permission/taskBrief/memoryRecap) is also wrapped in
1097
+ // `<system-reminder>` but varies per-call, so a plain prefix match would
1098
+ // pin per-call data to the 1h BP3 slot and explode the cache.
1099
+ const BP3_SENTINEL = '<!-- bp3-sentinel -->';
1100
+ function findTier3Index(chatMsgs) {
1101
+ for (let i = 0; i < chatMsgs.length; i++) {
1102
+ const m = chatMsgs[i];
1103
+ if (m?.role === 'user' && typeof m.content === 'string'
1104
+ && m.content.startsWith('<system-reminder>')
1105
+ && m.content.includes(BP3_SENTINEL)) {
1106
+ return i;
1107
+ }
1108
+ }
1109
+ return -1;
1110
+ }
1111
+
1112
+ function buildRequestBody(messages, model, tools, sendOpts) {
1113
+ const systemMsgs = messages.filter(m => m.role === 'system');
1114
+ const chatMsgs = messages.filter(m => m.role !== 'system');
1115
+ // Pass each system message text as its own entry so the Anthropic body
1116
+ // gets N separate content blocks — each can have its own BP
1117
+ // independent of the others.
1118
+ const systemTexts = systemMsgs.map(m => m.content);
1119
+ const maxTokens = resolveMaxTokens(model);
1120
+ const opts = sendOpts || {};
1121
+ const ttls = resolveCacheTtls(opts);
1122
+ const systemBlocks = buildSystemBlocks(systemTexts, model, ttls?.system);
1123
+
1124
+ // 4-BP budget layout. tools BP is dropped — system BP covers the
1125
+ // tools prefix via Anthropic's prompt cache prefix semantics
1126
+ // (order: tools → system → messages). That frees slots for
1127
+ // tier3 + messages-tail.
1128
+ const systemBpUsed = ttls.system ? systemBlocks.filter(b => b.cache_control).length : 0;
1129
+ const toolsBpUsed = 0;
1130
+ const tier3Idx = ttls.tier3 ? findTier3Index(chatMsgs) : -1;
1131
+ const tier3BpUsed = tier3Idx >= 0 ? 1 : 0;
1132
+ const usedSlots = toolsBpUsed + systemBpUsed + tier3BpUsed;
1133
+ // Env override for smoke-testing BP-count strategies. ANTHROPIC_MSG_SLOTS
1134
+ // caps how many sliding message-tail breakpoints we burn per request
1135
+ // (default: fill whatever's left of the 4-BP budget). Set to 1 to reduce
1136
+ // BP-position churn across iterations; set to 0 to disable messages-tail
1137
+ // caching entirely and rely on the tools+system+tier3 prefix.
1138
+ const msgSlotsCap = Number.parseInt(process.env.ANTHROPIC_MSG_SLOTS, 10);
1139
+ const defaultMsgSlots = Math.max(0, 4 - usedSlots);
1140
+ const msgSlots = ttls.messages
1141
+ ? (Number.isFinite(msgSlotsCap) && msgSlotsCap >= 0 ? Math.min(msgSlotsCap, defaultMsgSlots) : defaultMsgSlots)
1142
+ : 0;
1143
+ const cacheableIndexes = collectRecentCacheableIndexes(chatMsgs, msgSlots);
1144
+ // If the tail slot landed on the Tier 3 index, drop it from the sliding
1145
+ // set — Tier 3 already owns its own BP and we don't want to double-mark.
1146
+ if (tier3Idx >= 0) cacheableIndexes.delete(tier3Idx);
1147
+ const anthropicMessages = toAnthropicMessages(
1148
+ chatMsgs,
1149
+ cacheableIndexes,
1150
+ ttls.messages,
1151
+ tier3Idx,
1152
+ ttls.tier3,
1153
+ );
1154
+
1155
+ const body = {
1156
+ model,
1157
+ max_tokens: maxTokens,
1158
+ messages: anthropicMessages,
1159
+ stream: true,
1160
+ };
1161
+
1162
+ if (systemBlocks.length) body.system = systemBlocks;
1163
+
1164
+ if (tools?.length) {
1165
+ // No cache_control on tools — the systemBase BP already covers the
1166
+ // tools prefix via Anthropic's prompt cache prefix semantics (order:
1167
+ // tools → system → messages). Placing a separate BP here would waste
1168
+ // a slot that's better spent on messages tail.
1169
+ body.tools = toAnthropicTools(tools);
1170
+ }
1171
+
1172
+ if (opts.effort) {
1173
+ if (EFFORT_BUDGET[opts.effort]) {
1174
+ body.thinking = { type: 'enabled', budget_tokens: EFFORT_BUDGET[opts.effort] };
1175
+ } else if (!_LOGGED_UNKNOWN_EFFORT.has(opts.effort)) {
1176
+ _LOGGED_UNKNOWN_EFFORT.add(opts.effort);
1177
+ try {
1178
+ process.stderr.write(`[anthropic-oauth] unknown effort=${opts.effort} ignored (known: ${Object.keys(EFFORT_BUDGET).join(',')})\n`);
1179
+ } catch {}
1180
+ }
1181
+ }
1182
+
1183
+ if (opts.fast === true && supportsAnthropicFastMode(model)) {
1184
+ body.speed = 'fast';
1185
+ }
1186
+
1187
+ return body;
1188
+ }
1189
+
1190
+ // --- Provider ---
1191
+
1192
+ export class AnthropicOAuthProvider {
1193
+ // input_tokens EXCLUDES cache_read_input_tokens (separate field) — add the
1194
+ // cache back for the real context footprint. See registry.mjs.
1195
+ static inputExcludesCache = true;
1196
+ name = 'anthropic-oauth';
1197
+ credentials = null;
1198
+ config;
1199
+ fastModeBetaHeaderLatched = false;
1200
+
1201
+ constructor(config) {
1202
+ this.config = config || {};
1203
+ this.credentials = loadCredentials();
1204
+ // Warm a kept-alive socket to the messages API so the first request
1205
+ // skips the cold TLS handshake. Best-effort; never throws.
1206
+ preconnect('https://api.anthropic.com');
1207
+ }
1208
+
1209
+ async ensureAuth({ forceRefresh = false, reason = 'preemptive' } = {}) {
1210
+ if (!this.credentials) {
1211
+ this.credentials = loadCredentials();
1212
+ }
1213
+ if (!this.credentials) {
1214
+ throw new Error('Anthropic OAuth credentials not found. Run "claude login" to authenticate.');
1215
+ }
1216
+
1217
+ // Pick up host-rotated tokens the moment the credentials file is
1218
+ // rewritten — without this, a fresh `claude login` is ignored until
1219
+ // the in-memory token's expiry skew triggers a refresh.
1220
+ const diskMtime = _credentialsMaxMtime();
1221
+ if (diskMtime > 0 && diskMtime > (this.credentials.mtimeMs || 0)) {
1222
+ const fresh = loadCredentials();
1223
+ if (fresh?.accessToken) {
1224
+ this.credentials = fresh;
1225
+ process.stderr.write(`[anthropic-oauth] Credentials reloaded from disk (mtime change)\n`);
1226
+ }
1227
+ }
1228
+
1229
+ const expiring = this.credentials.expiresAt
1230
+ && this.credentials.expiresAt < Date.now() + TOKEN_REFRESH_SKEW_MS;
1231
+ if (forceRefresh || expiring) {
1232
+ this.credentials = await this._refreshCredentials({ force: forceRefresh, reason });
1233
+ }
1234
+
1235
+ return this.credentials;
1236
+ }
1237
+
1238
+ async _refreshCredentials({ force = false, reason = 'preemptive' } = {}) {
1239
+ const currentToken = this.credentials?.accessToken || null;
1240
+ const disk = loadCredentials();
1241
+ const validAfter = Date.now() + (force ? 0 : TOKEN_REFRESH_SKEW_MS);
1242
+ if (disk?.accessToken && disk.accessToken !== currentToken
1243
+ && (!disk.expiresAt || disk.expiresAt >= validAfter)) {
1244
+ this.credentials = disk;
1245
+ process.stderr.write(`[anthropic-oauth] Credentials reloaded from disk\n`);
1246
+ return disk;
1247
+ }
1248
+ if (!this.credentials && disk) this.credentials = disk;
1249
+
1250
+ if (_oauthRefreshInFlight) {
1251
+ const shared = await _oauthRefreshInFlight;
1252
+ this.credentials = shared;
1253
+ if (!force || shared?.accessToken !== currentToken) return this.credentials;
1254
+ }
1255
+
1256
+ const startingCreds = this.credentials || disk;
1257
+ _oauthRefreshInFlight = (async () => {
1258
+ const latest = loadCredentials() || startingCreds;
1259
+ const latestValidAfter = Date.now() + (force ? 0 : TOKEN_REFRESH_SKEW_MS);
1260
+ if (latest?.accessToken && latest.accessToken !== currentToken
1261
+ && (!latest.expiresAt || latest.expiresAt >= latestValidAfter)) {
1262
+ process.stderr.write(`[anthropic-oauth] Credentials reloaded from disk\n`);
1263
+ return latest;
1264
+ }
1265
+
1266
+ if (!latest?.refreshToken) {
1267
+ if (!force && latest?.accessToken && (!latest.expiresAt || latest.expiresAt > Date.now())) {
1268
+ process.stderr.write(`[anthropic-oauth] WARNING: token expiring but no refresh token; using current token until expiry\n`);
1269
+ return latest;
1270
+ }
1271
+ throw new Error('Anthropic OAuth refresh token not available. Run "claude login" to re-authenticate.');
1272
+ }
1273
+
1274
+ try {
1275
+ process.stderr.write(`[anthropic-oauth] Token ${reason}, refreshing...\n`);
1276
+ const refreshed = await refreshOAuthCredentials(latest);
1277
+ process.stderr.write(`[anthropic-oauth] Token refreshed, expires in ${Math.round(((refreshed.expiresAt || Date.now()) - Date.now()) / 1000)}s\n`);
1278
+ return refreshed;
1279
+ } catch (err) {
1280
+ if (!force && latest?.accessToken && (!latest.expiresAt || latest.expiresAt > Date.now())) {
1281
+ const msg = err instanceof Error ? err.message : String(err);
1282
+ process.stderr.write(`[anthropic-oauth] Refresh failed (${msg}); using still-valid current token\n`);
1283
+ return latest;
1284
+ }
1285
+ throw err;
1286
+ }
1287
+ })().finally(() => { _oauthRefreshInFlight = null; });
1288
+
1289
+ this.credentials = await _oauthRefreshInFlight;
1290
+ return this.credentials;
1291
+ }
1292
+
1293
+ scrubTokens(text) {
1294
+ return _scrubTokens(text);
1295
+ }
1296
+
1297
+ async send(messages, model, tools, sendOpts) {
1298
+ // Defense-in-depth: enforce tool_use / tool_result pairing before
1299
+ // the Anthropic API call. The trim.mjs sanitize pass is normally
1300
+ // invoked by the budget trimmer in loop.mjs, but dispatches under
1301
+ // budget skip it — a tool that aborted mid-flight then leaves an
1302
+ // unmatched tool_use in messages, which the provider rejects with
1303
+ // a hard 400. Pairing here closes the gap regardless of caller.
1304
+ messages = sanitizeToolPairs(messages);
1305
+ const opts = sendOpts || {};
1306
+ const onStageChange = typeof opts.onStageChange === 'function' ? opts.onStageChange : null;
1307
+ const onStreamDelta = typeof opts.onStreamDelta === 'function' ? opts.onStreamDelta : null;
1308
+ const onToolCall = typeof opts.onToolCall === 'function' ? opts.onToolCall : null;
1309
+ const externalSignal = opts.signal || null;
1310
+ // Test seam: lets the retry harness drive stream outcomes without a
1311
+ // live OAuth session.
1312
+ const parseSSEFn = typeof opts._parseSSEFn === 'function' ? opts._parseSSEFn : parseSSEStream;
1313
+
1314
+ let creds = await this.ensureAuth();
1315
+ // Default when the caller doesn't pin a model: newest high-tier chat
1316
+ // model from the live catalog (one warmup round-trip if cache is cold).
1317
+ const useModel = model || await ensureLatestAnthropicModel(this);
1318
+ const body = buildRequestBody(messages, useModel, tools, sendOpts);
1319
+ if (body.speed === 'fast') {
1320
+ this.fastModeBetaHeaderLatched = true;
1321
+ }
1322
+ const sessionId = opts.sessionId || null;
1323
+ const iteration = Number.isFinite(Number(opts.iteration)) ? Number(opts.iteration) : null;
1324
+ const totalTimeout = createTimeoutSignal(
1325
+ externalSignal,
1326
+ PROVIDER_GENERATE_TOTAL_TIMEOUT_MS,
1327
+ 'Anthropic OAuth total request',
1328
+ );
1329
+ const totalSignal = totalTimeout.signal;
1330
+
1331
+ const cleanupCancelHandler = (handler) => {
1332
+ if (!handler) return;
1333
+ try { totalSignal.removeEventListener('abort', handler); } catch {}
1334
+ };
1335
+
1336
+ const doRequest = async (accessToken, requestSignal = null) => {
1337
+ const controller = createAbortController();
1338
+ const fetchStartedAt = Date.now();
1339
+
1340
+ let cancelHandler = null;
1341
+ let attemptCancelHandler = null;
1342
+ if (totalSignal) {
1343
+ if (totalSignal.aborted) {
1344
+ controller.abort(totalSignal.reason);
1345
+ throw totalSignal.reason instanceof Error
1346
+ ? totalSignal.reason
1347
+ : new Error('Anthropic OAuth request aborted by session close');
1348
+ }
1349
+ cancelHandler = () => { try { controller.abort(totalSignal.reason); } catch {} };
1350
+ totalSignal.addEventListener('abort', cancelHandler, { once: true });
1351
+ }
1352
+ if (requestSignal && requestSignal !== totalSignal) {
1353
+ if (requestSignal.aborted) {
1354
+ cleanupCancelHandler(cancelHandler);
1355
+ controller.abort(requestSignal.reason);
1356
+ throw requestSignal.reason instanceof Error
1357
+ ? requestSignal.reason
1358
+ : new Error('Anthropic OAuth request attempt aborted');
1359
+ }
1360
+ attemptCancelHandler = () => { try { controller.abort(requestSignal.reason); } catch {} };
1361
+ requestSignal.addEventListener('abort', attemptCancelHandler, { once: true });
1362
+ }
1363
+
1364
+ try {
1365
+ try { onStageChange?.('requesting'); } catch {}
1366
+ body.messages = sanitizeAnthropicContentPairs(body.messages);
1367
+
1368
+ const response = await fetch(API_URL, {
1369
+ method: 'POST',
1370
+ headers: {
1371
+ 'Authorization': `Bearer ${accessToken}`,
1372
+ 'anthropic-version': ANTHROPIC_VERSION,
1373
+ 'anthropic-beta': buildAnthropicBetaHeaders({
1374
+ base: OAUTH_BETA_HEADERS,
1375
+ fastMode: this.fastModeBetaHeaderLatched,
1376
+ }),
1377
+ 'anthropic-dangerous-direct-browser-access': 'true',
1378
+ 'user-agent': `claude-cli/${resolveCliVersion()} (external, sdk-cli)`,
1379
+ 'x-app': 'cli',
1380
+ 'Content-Type': 'application/json',
1381
+ },
1382
+ body: JSON.stringify(body),
1383
+ signal: controller.signal,
1384
+ dispatcher: getLlmDispatcher(),
1385
+ });
1386
+
1387
+ traceBridgeFetch({
1388
+ sessionId,
1389
+ headersMs: Date.now() - fetchStartedAt,
1390
+ httpStatus: response.status,
1391
+ provider: 'anthropic-oauth',
1392
+ model: useModel,
1393
+ transport: 'sse',
1394
+ });
1395
+
1396
+ if (attemptCancelHandler) {
1397
+ try { requestSignal.removeEventListener('abort', attemptCancelHandler); } catch {}
1398
+ }
1399
+ return { response, controller, cancelHandler };
1400
+ } catch (err) {
1401
+ if (attemptCancelHandler) {
1402
+ try { requestSignal.removeEventListener('abort', attemptCancelHandler); } catch {}
1403
+ }
1404
+ cleanupCancelHandler(cancelHandler);
1405
+ if (requestSignal?.aborted) {
1406
+ const reason = requestSignal.reason;
1407
+ throw reason instanceof Error ? reason : new Error('Anthropic OAuth request attempt aborted');
1408
+ }
1409
+ if (totalSignal?.aborted) {
1410
+ const reason = totalSignal.reason;
1411
+ throw reason instanceof Error ? reason : new Error('Anthropic OAuth request aborted by session close');
1412
+ }
1413
+ if (err?.name === 'AbortError') {
1414
+ const timeoutErr = new Error(`Anthropic OAuth API initial response timed out after ${PROVIDER_HTTP_RESPONSE_TIMEOUT_MS}ms`);
1415
+ timeoutErr.code = 'EPROVIDERTIMEOUT';
1416
+ throw timeoutErr;
1417
+ }
1418
+ throw err;
1419
+ }
1420
+ };
1421
+ // Test seam: injectable request factory for retry-path tests.
1422
+ const doRequestImpl = typeof opts._doRequestFn === 'function' ? opts._doRequestFn : doRequest;
1423
+
1424
+ const requestWithRetry = async (accessToken) => withRetry(async ({ signal: attemptSignal }) => {
1425
+ const result = await doRequestImpl(accessToken, attemptSignal);
1426
+ const status = Number(result?.response?.status || 0);
1427
+ const transientStatus = classifyError({ httpStatus: status }) === 'transient';
1428
+ if (transientStatus || status === 429) {
1429
+ const err = new Error(`Anthropic OAuth API ${status}`);
1430
+ err.httpStatus = status;
1431
+ err.status = status;
1432
+ err.headers = result?.response?.headers;
1433
+ err.response = { status, headers: result?.response?.headers };
1434
+ const retryAfterMs = retryAfterMsFromError(err);
1435
+ if (transientStatus || retryAfterMs != null) {
1436
+ try { await result.response.text(); } catch {}
1437
+ cleanupCancelHandler(result.cancelHandler);
1438
+ try { result.controller?.abort?.(); } catch {}
1439
+ throw err;
1440
+ }
1441
+ }
1442
+ return result;
1443
+ }, {
1444
+ signal: totalSignal,
1445
+ maxAttempts: PROVIDER_RETRY_MAX_ATTEMPTS,
1446
+ backoffMs: PROVIDER_RETRY_BACKOFF_MS,
1447
+ perAttemptTimeoutMs: PROVIDER_HTTP_RESPONSE_TIMEOUT_MS,
1448
+ perAttemptLabel: 'Anthropic OAuth initial response',
1449
+ onRetry: ({ attempt, lastErr, delayMs, delayReason }) => {
1450
+ const status = Number(lastErr?.httpStatus || lastErr?.status || lastErr?.response?.status || 0) || null;
1451
+ const reason = status || lastErr?.code || lastErr?.message || 'network error';
1452
+ const suffix = delayReason ? ` (${delayReason})` : '';
1453
+ try {
1454
+ process.stderr.write(
1455
+ `[anthropic-oauth] retry attempt ${attempt + 1}/${PROVIDER_RETRY_MAX_ATTEMPTS} after ${reason}, backoff ${delayMs}ms${suffix}\n`,
1456
+ );
1457
+ } catch {}
1458
+ },
1459
+ });
1460
+ // One retry only: enough to recover transient stream loss without
1461
+ // quietly replaying long-running work multiple times.
1462
+ const MAX_MIDSTREAM_RETRIES = 1;
1463
+ let firstAttemptError = null;
1464
+ let firstAttemptClassifier = null;
1465
+
1466
+ try {
1467
+ for (let attemptIndex = 0; attemptIndex <= MAX_MIDSTREAM_RETRIES; attemptIndex++) {
1468
+ let response, controller, cancelHandler;
1469
+ ({ response, controller, cancelHandler } = await requestWithRetry(creds.accessToken));
1470
+
1471
+ // 401: token expired/revoked. 403: organization permission flipped
1472
+ // (e.g. relogin into a different org). Both: force a shared refresh
1473
+ // and retry once with the new token.
1474
+ if (response.status === 401 || response.status === 403) {
1475
+ process.stderr.write(`[anthropic-oauth] ${response.status} — forcing refresh and retrying once\n`);
1476
+ cleanupCancelHandler(cancelHandler);
1477
+ creds = await this.ensureAuth({ forceRefresh: true, reason: String(response.status) });
1478
+ ({ response, controller, cancelHandler } = await requestWithRetry(creds.accessToken));
1479
+ }
1480
+
1481
+ if (!response.ok) {
1482
+ cleanupCancelHandler(cancelHandler);
1483
+ const text = await response.text().catch(() => '');
1484
+ const safeText = this.scrubTokens(text).slice(0, 200);
1485
+ process.stderr.write(`[anthropic-oauth] API error ${response.status}: ${safeText}\n`);
1486
+
1487
+ // Phase I: on unknown/404 model errors, force a catalog refresh and
1488
+ // retry once. Protects against a silently-rotated model id.
1489
+ const isUnknownModel = response.status === 404
1490
+ || /unknown[_\s-]?model|model[_\s-]?not[_\s-]?found/i.test(safeText);
1491
+ if (isUnknownModel && !opts._modelRetry) {
1492
+ process.stderr.write(`[anthropic-oauth] unknown model — refreshing catalog + 1 retry\n`);
1493
+ await this._refreshModelCache();
1494
+ return this.send(messages, model, tools, { ...opts, _modelRetry: true });
1495
+ }
1496
+ throw new Error(`Anthropic OAuth API ${response.status}: ${safeText}`);
1497
+ }
1498
+
1499
+ if (SSE_VERBOSE) process.stderr.write(`[anthropic-oauth] Response ${response.status}, parsing SSE...\n`);
1500
+ try { onStageChange?.('streaming'); } catch {}
1501
+
1502
+ const midState = {
1503
+ attemptIndex,
1504
+ sawMessageStart: false,
1505
+ sawCompleted: false,
1506
+ emittedToolCall: false,
1507
+ userAbort: false,
1508
+ watchdogAbort: null,
1509
+ ttftAt: null,
1510
+ };
1511
+
1512
+ try {
1513
+ const sseStartedAt = Date.now();
1514
+ const result = await parseSSEFn(
1515
+ response,
1516
+ controller.signal,
1517
+ () => controller.abort(),
1518
+ onStreamDelta,
1519
+ onToolCall,
1520
+ midState,
1521
+ );
1522
+
1523
+ const ttftMs = midState.ttftAt ? midState.ttftAt - sseStartedAt : null;
1524
+ const liveModel = result.model || useModel;
1525
+ traceBridgeSse({
1526
+ sessionId,
1527
+ sseParseMs: Date.now() - sseStartedAt,
1528
+ ttftMs,
1529
+ provider: 'anthropic-oauth',
1530
+ model: liveModel,
1531
+ transport: 'sse',
1532
+ });
1533
+
1534
+ traceBridgeUsage({
1535
+ sessionId,
1536
+ iteration,
1537
+ inputTokens: result.usage?.inputTokens || 0,
1538
+ outputTokens: result.usage?.outputTokens || 0,
1539
+ cachedTokens: result.usage?.cachedTokens || 0,
1540
+ cacheWriteTokens: result.usage?.cacheWriteTokens || 0,
1541
+ promptTokens: result.usage?.promptTokens || 0,
1542
+ model: liveModel,
1543
+ modelDisplay: _displayModel(liveModel),
1544
+ rawUsage: result.usage?.raw || null,
1545
+ provider: 'anthropic-oauth',
1546
+ });
1547
+
1548
+ // Phase I: if the live response surfaced a model id we don't know
1549
+ // about yet, kick off a background catalog refresh. Fire-and-forget
1550
+ // — do not await, do not surface errors.
1551
+ if (result.model && !_catalogHas(result.model)) {
1552
+ void this._refreshModelCache();
1553
+ }
1554
+
1555
+ if (SSE_VERBOSE) process.stderr.write(`[anthropic-oauth] Done: ${result.content.length} chars, ${result.toolCalls?.length || 0} tool calls\n`);
1556
+ // Empty-stream guard. Invariant: a valid Anthropic SSE response
1557
+ // ALWAYS opens with message_start (which carries usage.input_tokens).
1558
+ // A 200 whose body produced no message_start delivered nothing —
1559
+ // no usage, no content, no tool calls — i.e. a dropped/empty stream
1560
+ // (transient, often rate-limit-adjacent under concurrent load), NOT
1561
+ // a valid terminal turn. Returning it surfaces upstream as a silent
1562
+ // empty turn (0 tokens, no content) that masks the cause. Throw a
1563
+ // marked error: retry is provably safe here (no message_start ⇒
1564
+ // nothing was emitted ⇒ no duplicate-tool risk), and once retries
1565
+ // are exhausted the error is surfaced instead of swallowed.
1566
+ if (!midState.sawMessageStart
1567
+ && !midState.userAbort
1568
+ && !midState.watchdogAbort
1569
+ && !result.content
1570
+ && !(result.toolCalls && result.toolCalls.length)
1571
+ && !(result.usage && result.usage.inputTokens > 0)) {
1572
+ const emptyErr = new Error('Anthropic OAuth SSE stream produced no message_start (empty/dropped stream — likely transient or rate-limited)');
1573
+ emptyErr.code = 'EEMPTYSTREAM';
1574
+ emptyErr.isEmptyStream = true;
1575
+ throw emptyErr;
1576
+ }
1577
+ try {
1578
+ Object.defineProperty(result, '__midstreamRetries', { value: attemptIndex, enumerable: false });
1579
+ } catch { /* ignore non-extensible result */ }
1580
+ return result;
1581
+ } catch (err) {
1582
+ // Empty/dropped stream (no message_start): safe to retry once —
1583
+ // nothing was emitted, so there is no duplicate-tool risk. This
1584
+ // is intentionally NOT routed through _classifyMidstreamError,
1585
+ // which requires sawMessageStart and would reject it.
1586
+ if (err?.isEmptyStream && attemptIndex < MAX_MIDSTREAM_RETRIES) {
1587
+ firstAttemptError = err;
1588
+ firstAttemptClassifier = 'empty_stream';
1589
+ try { controller?.abort?.(err); } catch { /* best-effort teardown */ }
1590
+ try { process.stderr.write(`[anthropic-oauth] empty stream (no message_start) — retry ${attemptIndex + 1}/${MAX_MIDSTREAM_RETRIES}\n`); } catch {}
1591
+ continue;
1592
+ }
1593
+ // Truncated stream (message_start without message_stop): the
1594
+ // partial result is discarded and re-requesting is safe (a
1595
+ // pendingToolUse means the tool_use input JSON never completed).
1596
+ // _classifyMidstreamError does not cover this; route it through
1597
+ // the shared classifier so it inherits the cross-provider
1598
+ // transient policy instead of escaping and killing the worker.
1599
+ // Guard: parseSSEStream eagerly fires onToolCall and sets
1600
+ // emittedToolCall=true at content_block_stop, BEFORE message_stop.
1601
+ // If the stream truncates after that, retrying would
1602
+ // double-execute the tool. Only retry when nothing was emitted
1603
+ // yet; otherwise let the error surface.
1604
+ if ((err?.truncatedStream === true || err?.code === 'TRUNCATED_STREAM')
1605
+ && classifyError(err) === 'transient'
1606
+ && !midState.emittedToolCall
1607
+ && attemptIndex < MAX_MIDSTREAM_RETRIES) {
1608
+ firstAttemptError = err;
1609
+ firstAttemptClassifier = 'truncated_stream';
1610
+ try { controller?.abort?.(err); } catch { /* best-effort teardown */ }
1611
+ try { process.stderr.write(`[anthropic-oauth] truncated stream — retry ${attemptIndex + 1}/${MAX_MIDSTREAM_RETRIES}\n`); } catch {}
1612
+ continue;
1613
+ }
1614
+ const classifier = _classifyMidstreamError(err, midState);
1615
+ if (classifier && attemptIndex < MAX_MIDSTREAM_RETRIES) {
1616
+ firstAttemptError = err;
1617
+ firstAttemptClassifier = classifier;
1618
+ try { controller?.abort?.(err); } catch (abortErr) {
1619
+ /* best-effort stream teardown */
1620
+ try { process.stderr.write(`[anthropic-oauth] abort on stream error failed: ${abortErr?.message ?? String(abortErr)}\n`); } catch {}
1621
+ }
1622
+ try {
1623
+ process.stderr.write(`[anthropic-oauth] mid-stream recovered: retry ${attemptIndex + 1}/${MAX_MIDSTREAM_RETRIES} (cause: ${classifier})\n`);
1624
+ } catch {}
1625
+ continue;
1626
+ }
1627
+ if (attemptIndex > 0 && firstAttemptError) {
1628
+ try { firstAttemptError.midstreamRetries = attemptIndex; } catch {}
1629
+ try { firstAttemptError.midstreamClassifier = firstAttemptClassifier; } catch {}
1630
+ throw firstAttemptError;
1631
+ }
1632
+ throw err;
1633
+ } finally {
1634
+ cleanupCancelHandler(cancelHandler);
1635
+ }
1636
+ }
1637
+ throw firstAttemptError || new Error('Anthropic OAuth mid-stream retry: unreachable');
1638
+ } finally {
1639
+ totalTimeout.cleanup();
1640
+ }
1641
+ }
1642
+
1643
+ async listModels() {
1644
+ // Dynamic lookup via /v1/models — returns whatever Anthropic currently
1645
+ // exposes for this OAuth account. Cached on disk with 24h TTL; falls
1646
+ // back to the static MODELS list on any failure so the plugin still
1647
+ // works offline or when Anthropic's /v1/models is momentarily down.
1648
+ const cached = await _loadModelCache();
1649
+ if (cached) {
1650
+ _inMemoryCatalog = cached.slice();
1651
+ return cached;
1652
+ }
1653
+ try {
1654
+ const creds = await this.ensureAuth();
1655
+ const res = await fetch('https://api.anthropic.com/v1/models', {
1656
+ signal: AbortSignal.timeout(10_000),
1657
+ method: 'GET',
1658
+ headers: {
1659
+ 'Authorization': `Bearer ${creds.accessToken}`,
1660
+ 'anthropic-version': ANTHROPIC_VERSION,
1661
+ 'anthropic-beta': OAUTH_BETA_HEADERS,
1662
+ 'anthropic-dangerous-direct-browser-access': 'true',
1663
+ 'user-agent': `claude-cli/${resolveCliVersion()} (external, sdk-cli)`,
1664
+ 'x-app': 'cli',
1665
+ },
1666
+ dispatcher: getLlmDispatcher(),
1667
+ });
1668
+ if (!res.ok) throw new Error(`list_models ${res.status}`);
1669
+ const data = await res.json();
1670
+ const items = Array.isArray(data?.data) ? data.data : [];
1671
+ const normalized = items
1672
+ .map(m => _normalizeAnthropicModel(m))
1673
+ .filter(Boolean);
1674
+ _markLatestByFamily(normalized);
1675
+ // Enrich with LiteLLM catalog metadata (context, pricing, capabilities)
1676
+ const enriched = await enrichModels(normalized);
1677
+ await _saveModelCache(enriched);
1678
+ return enriched;
1679
+ } catch (err) {
1680
+ process.stderr.write(`[anthropic-oauth] listModels fetch failed (${err.message})\n`);
1681
+ // Fallback with full API model IDs. Short family tokens leaked
1682
+ // through here would be accepted by setup and reintroduce the
1683
+ // legacy shape. Env var override keeps this tracking defaults.
1684
+ const opusId = process.env.ANTHROPIC_DEFAULT_OPUS_MODEL || 'claude-opus-4-8';
1685
+ const sonnetId = process.env.ANTHROPIC_DEFAULT_SONNET_MODEL || 'claude-sonnet-4-6';
1686
+ const haikuId = process.env.ANTHROPIC_DEFAULT_HAIKU_MODEL || 'claude-haiku-4-5-20251001';
1687
+ return [
1688
+ { id: opusId, display: 'Opus (auto)', family: 'opus', provider: 'anthropic-oauth', tier: 'family', latest: true, contextWindow: 1000000 },
1689
+ { id: sonnetId, display: 'Sonnet (auto)', family: 'sonnet', provider: 'anthropic-oauth', tier: 'family', latest: true, contextWindow: 1000000 },
1690
+ { id: haikuId, display: 'Haiku (auto)', family: 'haiku', provider: 'anthropic-oauth', tier: 'family', latest: true, contextWindow: 200000 },
1691
+ ];
1692
+ }
1693
+ }
1694
+
1695
+ // Force a catalog refresh (ignores the 24h TTL). De-duped via
1696
+ // _modelRefreshInFlight so concurrent callers share one HTTP round-trip.
1697
+ // Returns the new catalog on success, null on failure.
1698
+ async _refreshModelCache() {
1699
+ if (_modelRefreshInFlight) return _modelRefreshInFlight;
1700
+ _modelRefreshInFlight = (async () => {
1701
+ try {
1702
+ const creds = await this.ensureAuth();
1703
+ const res = await fetch('https://api.anthropic.com/v1/models', {
1704
+ signal: AbortSignal.timeout(10_000),
1705
+ method: 'GET',
1706
+ headers: {
1707
+ 'Authorization': `Bearer ${creds.accessToken}`,
1708
+ 'anthropic-version': ANTHROPIC_VERSION,
1709
+ 'anthropic-beta': OAUTH_BETA_HEADERS,
1710
+ 'anthropic-dangerous-direct-browser-access': 'true',
1711
+ 'user-agent': `claude-cli/${resolveCliVersion()} (external, sdk-cli)`,
1712
+ 'x-app': 'cli',
1713
+ },
1714
+ dispatcher: getLlmDispatcher(),
1715
+ });
1716
+ if (!res.ok) throw new Error(`list_models ${res.status}`);
1717
+ const data = await res.json();
1718
+ const items = Array.isArray(data?.data) ? data.data : [];
1719
+ const normalized = items
1720
+ .map(m => _normalizeAnthropicModel(m))
1721
+ .filter(Boolean);
1722
+ _markLatestByFamily(normalized);
1723
+ const enriched = await enrichModels(normalized);
1724
+ await _saveModelCache(enriched);
1725
+ process.stderr.write(`[anthropic-oauth] catalog refreshed (${enriched.length} models)\n`);
1726
+ return enriched;
1727
+ } catch (err) {
1728
+ process.stderr.write(`[anthropic-oauth] catalog refresh failed (${err.message})\n`);
1729
+ return null;
1730
+ } finally {
1731
+ _modelRefreshInFlight = null;
1732
+ }
1733
+ })();
1734
+ return _modelRefreshInFlight;
1735
+ }
1736
+
1737
+ async isAvailable() {
1738
+ return this.credentials !== null || loadCredentials() !== null;
1739
+ }
1740
+ }
1741
+
1742
+ // Additive exports for test harnesses.
1743
+ // Lets the SSE parser be exercised in isolation against a synthetic
1744
+ // ReadableStream without needing a live OAuth session.
1745
+ export { parseSSEStream };