oma-coding-agent 1.1.4

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 (1298) hide show
  1. package/CHANGELOG.md +12164 -0
  2. package/README.md +35 -0
  3. package/dist/cli.js +18266 -0
  4. package/examples/README.md +21 -0
  5. package/examples/custom-tools/README.md +104 -0
  6. package/examples/custom-tools/hello/index.ts +20 -0
  7. package/examples/extensions/README.md +142 -0
  8. package/examples/extensions/api-demo.ts +79 -0
  9. package/examples/extensions/chalk-logger.ts +25 -0
  10. package/examples/extensions/hello.ts +31 -0
  11. package/examples/extensions/pirate.ts +43 -0
  12. package/examples/extensions/plan-mode.ts +549 -0
  13. package/examples/extensions/reload-runtime.ts +38 -0
  14. package/examples/extensions/thinking-note.ts +13 -0
  15. package/examples/extensions/tools.ts +145 -0
  16. package/examples/extensions/with-deps/index.ts +36 -0
  17. package/examples/extensions/with-deps/package-lock.json +31 -0
  18. package/examples/extensions/with-deps/package.json +17 -0
  19. package/examples/hooks/README.md +56 -0
  20. package/examples/hooks/auto-commit-on-exit.ts +48 -0
  21. package/examples/hooks/confirm-destructive.ts +58 -0
  22. package/examples/hooks/custom-compaction.ts +115 -0
  23. package/examples/hooks/dirty-repo-guard.ts +51 -0
  24. package/examples/hooks/file-trigger.ts +40 -0
  25. package/examples/hooks/git-checkpoint.ts +52 -0
  26. package/examples/hooks/handoff.ts +149 -0
  27. package/examples/hooks/permission-gate.ts +33 -0
  28. package/examples/hooks/protected-paths.ts +29 -0
  29. package/examples/hooks/qna.ts +118 -0
  30. package/examples/hooks/status-line.ts +39 -0
  31. package/examples/sdk/01-minimal.ts +21 -0
  32. package/examples/sdk/02-custom-model.ts +49 -0
  33. package/examples/sdk/03-custom-prompt.ts +46 -0
  34. package/examples/sdk/04-skills.ts +43 -0
  35. package/examples/sdk/06-extensions.ts +82 -0
  36. package/examples/sdk/06-hooks.ts +61 -0
  37. package/examples/sdk/07-context-files.ts +35 -0
  38. package/examples/sdk/08-prompt-templates.ts +41 -0
  39. package/examples/sdk/08-slash-commands.ts +46 -0
  40. package/examples/sdk/09-api-keys-and-oauth.ts +54 -0
  41. package/examples/sdk/11-sessions.ts +47 -0
  42. package/examples/sdk/12-redis-sessions.ts +54 -0
  43. package/examples/sdk/13-sql-sessions.ts +61 -0
  44. package/examples/sdk/README.md +172 -0
  45. package/package.json +573 -0
  46. package/scripts/bench-guard.ts +71 -0
  47. package/scripts/build-binary.ts +108 -0
  48. package/scripts/bundle-dist.ts +110 -0
  49. package/scripts/embed-mupdf-wasm.ts +67 -0
  50. package/scripts/format-prompts.ts +68 -0
  51. package/scripts/generate-docs-index.ts +56 -0
  52. package/scripts/generate-share-viewer.ts +34 -0
  53. package/scripts/measure-prompt-tokens.ts +63 -0
  54. package/scripts/omp +42 -0
  55. package/scripts/omp.ts +19 -0
  56. package/src/advisor/__tests__/advisor.test.ts +915 -0
  57. package/src/advisor/advise-tool.ts +165 -0
  58. package/src/advisor/index.ts +4 -0
  59. package/src/advisor/runtime.ts +270 -0
  60. package/src/advisor/transcript-recorder.ts +136 -0
  61. package/src/advisor/watchdog.ts +83 -0
  62. package/src/async/index.ts +1 -0
  63. package/src/async/job-manager.ts +674 -0
  64. package/src/auto-thinking/classifier.ts +190 -0
  65. package/src/autolearn/controller.ts +139 -0
  66. package/src/autolearn/managed-skills.ts +255 -0
  67. package/src/autoresearch/command-resume.md +14 -0
  68. package/src/autoresearch/dashboard.ts +436 -0
  69. package/src/autoresearch/git.ts +319 -0
  70. package/src/autoresearch/helpers.ts +218 -0
  71. package/src/autoresearch/index.ts +536 -0
  72. package/src/autoresearch/prompt-setup.md +43 -0
  73. package/src/autoresearch/prompt.md +103 -0
  74. package/src/autoresearch/resume-message.md +10 -0
  75. package/src/autoresearch/state.ts +273 -0
  76. package/src/autoresearch/storage.ts +700 -0
  77. package/src/autoresearch/tools/init-experiment.ts +269 -0
  78. package/src/autoresearch/tools/log-experiment.ts +521 -0
  79. package/src/autoresearch/tools/run-experiment.ts +407 -0
  80. package/src/autoresearch/tools/update-notes.ts +109 -0
  81. package/src/autoresearch/types.ts +168 -0
  82. package/src/capability/context-file.ts +44 -0
  83. package/src/capability/extension-module.ts +34 -0
  84. package/src/capability/extension.ts +47 -0
  85. package/src/capability/fs.ts +117 -0
  86. package/src/capability/hook.ts +40 -0
  87. package/src/capability/index.ts +436 -0
  88. package/src/capability/instruction.ts +37 -0
  89. package/src/capability/mcp.ts +76 -0
  90. package/src/capability/prompt.ts +35 -0
  91. package/src/capability/rule-buckets.ts +66 -0
  92. package/src/capability/rule.ts +261 -0
  93. package/src/capability/settings.ts +34 -0
  94. package/src/capability/skill.ts +63 -0
  95. package/src/capability/slash-command.ts +40 -0
  96. package/src/capability/ssh.ts +41 -0
  97. package/src/capability/system-prompt.ts +34 -0
  98. package/src/capability/tool.ts +38 -0
  99. package/src/capability/types.ts +168 -0
  100. package/src/cli/agents-cli.ts +138 -0
  101. package/src/cli/args.ts +361 -0
  102. package/src/cli/auth-broker-cli.ts +893 -0
  103. package/src/cli/auth-gateway-cli.ts +608 -0
  104. package/src/cli/bench-cli.ts +552 -0
  105. package/src/cli/classify-install-target.ts +76 -0
  106. package/src/cli/claude-trace-cli.ts +795 -0
  107. package/src/cli/commands/init-xdg.ts +27 -0
  108. package/src/cli/completion-gen.ts +550 -0
  109. package/src/cli/config-cli.ts +418 -0
  110. package/src/cli/dry-balance-cli.ts +858 -0
  111. package/src/cli/extension-flags.ts +48 -0
  112. package/src/cli/file-processor.ts +133 -0
  113. package/src/cli/flag-tables.ts +280 -0
  114. package/src/cli/gallery-cli.ts +231 -0
  115. package/src/cli/gallery-fixtures/agentic.ts +407 -0
  116. package/src/cli/gallery-fixtures/codeintel.ts +187 -0
  117. package/src/cli/gallery-fixtures/edit.ts +194 -0
  118. package/src/cli/gallery-fixtures/fs.ts +220 -0
  119. package/src/cli/gallery-fixtures/index.ts +40 -0
  120. package/src/cli/gallery-fixtures/interaction.ts +49 -0
  121. package/src/cli/gallery-fixtures/memory.ts +81 -0
  122. package/src/cli/gallery-fixtures/misc.ts +250 -0
  123. package/src/cli/gallery-fixtures/search.ts +213 -0
  124. package/src/cli/gallery-fixtures/shell.ts +167 -0
  125. package/src/cli/gallery-fixtures/types.ts +57 -0
  126. package/src/cli/gallery-fixtures/web.ts +158 -0
  127. package/src/cli/gallery-screenshot.ts +279 -0
  128. package/src/cli/grep-cli.ts +160 -0
  129. package/src/cli/grievances-cli.ts +256 -0
  130. package/src/cli/initial-message.ts +58 -0
  131. package/src/cli/models-cli.ts +427 -0
  132. package/src/cli/plugin-cli.ts +996 -0
  133. package/src/cli/profile-alias.ts +338 -0
  134. package/src/cli/profile-bootstrap.ts +243 -0
  135. package/src/cli/read-cli.ts +57 -0
  136. package/src/cli/session-picker.ts +80 -0
  137. package/src/cli/setup-cli.ts +332 -0
  138. package/src/cli/setup-model-picker.ts +43 -0
  139. package/src/cli/shell-cli.ts +176 -0
  140. package/src/cli/ssh-cli.ts +179 -0
  141. package/src/cli/startup-cwd.ts +58 -0
  142. package/src/cli/stats-cli.ts +229 -0
  143. package/src/cli/tiny-models-cli.ts +127 -0
  144. package/src/cli/ttsr-cli.ts +995 -0
  145. package/src/cli/update-cli.ts +671 -0
  146. package/src/cli/usage-cli.ts +774 -0
  147. package/src/cli/web-search-cli.ts +132 -0
  148. package/src/cli/worktree-cli.ts +291 -0
  149. package/src/cli-commands.ts +85 -0
  150. package/src/cli.ts +326 -0
  151. package/src/collab/crypto.ts +63 -0
  152. package/src/collab/guest.ts +450 -0
  153. package/src/collab/host.ts +577 -0
  154. package/src/collab/protocol.ts +274 -0
  155. package/src/collab/relay-client.ts +216 -0
  156. package/src/commands/acp.ts +24 -0
  157. package/src/commands/agents.ts +57 -0
  158. package/src/commands/auth-broker.ts +99 -0
  159. package/src/commands/auth-gateway.ts +69 -0
  160. package/src/commands/bench.ts +42 -0
  161. package/src/commands/commit.ts +46 -0
  162. package/src/commands/complete.ts +66 -0
  163. package/src/commands/completions.ts +60 -0
  164. package/src/commands/config.ts +51 -0
  165. package/src/commands/dry-balance.ts +43 -0
  166. package/src/commands/gallery.ts +52 -0
  167. package/src/commands/grep.ts +48 -0
  168. package/src/commands/grievances.ts +51 -0
  169. package/src/commands/install.ts +107 -0
  170. package/src/commands/join.ts +39 -0
  171. package/src/commands/launch.ts +182 -0
  172. package/src/commands/models.ts +61 -0
  173. package/src/commands/plugin.ts +78 -0
  174. package/src/commands/read.ts +38 -0
  175. package/src/commands/say.ts +102 -0
  176. package/src/commands/setup.ts +67 -0
  177. package/src/commands/shell.ts +29 -0
  178. package/src/commands/ssh.ts +60 -0
  179. package/src/commands/stats.ts +29 -0
  180. package/src/commands/tiny-models.ts +36 -0
  181. package/src/commands/token.ts +108 -0
  182. package/src/commands/ttsr.ts +125 -0
  183. package/src/commands/update.ts +21 -0
  184. package/src/commands/usage.ts +43 -0
  185. package/src/commands/web-search.ts +42 -0
  186. package/src/commands/worktree.ts +56 -0
  187. package/src/commit/agentic/agent.ts +318 -0
  188. package/src/commit/agentic/fallback.ts +96 -0
  189. package/src/commit/agentic/index.ts +355 -0
  190. package/src/commit/agentic/prompts/analyze-file.md +22 -0
  191. package/src/commit/agentic/prompts/session-user.md +25 -0
  192. package/src/commit/agentic/prompts/split-confirm.md +1 -0
  193. package/src/commit/agentic/prompts/system.md +38 -0
  194. package/src/commit/agentic/state.ts +60 -0
  195. package/src/commit/agentic/tools/analyze-file.ts +149 -0
  196. package/src/commit/agentic/tools/git-file-diff.ts +191 -0
  197. package/src/commit/agentic/tools/git-hunk.ts +52 -0
  198. package/src/commit/agentic/tools/git-overview.ts +81 -0
  199. package/src/commit/agentic/tools/index.ts +54 -0
  200. package/src/commit/agentic/tools/propose-changelog.ts +147 -0
  201. package/src/commit/agentic/tools/propose-commit.ts +109 -0
  202. package/src/commit/agentic/tools/recent-commits.ts +81 -0
  203. package/src/commit/agentic/tools/schemas.ts +11 -0
  204. package/src/commit/agentic/tools/split-commit.ts +241 -0
  205. package/src/commit/agentic/topo-sort.ts +44 -0
  206. package/src/commit/agentic/trivial.ts +51 -0
  207. package/src/commit/agentic/validation.ts +183 -0
  208. package/src/commit/analysis/conventional.ts +64 -0
  209. package/src/commit/analysis/index.ts +4 -0
  210. package/src/commit/analysis/scope.ts +242 -0
  211. package/src/commit/analysis/summary.ts +107 -0
  212. package/src/commit/analysis/validation.ts +66 -0
  213. package/src/commit/changelog/detect.ts +40 -0
  214. package/src/commit/changelog/generate.ts +101 -0
  215. package/src/commit/changelog/index.ts +234 -0
  216. package/src/commit/changelog/parse.ts +44 -0
  217. package/src/commit/cli.ts +85 -0
  218. package/src/commit/git/diff.ts +148 -0
  219. package/src/commit/index.ts +5 -0
  220. package/src/commit/map-reduce/index.ts +69 -0
  221. package/src/commit/map-reduce/map-phase.ts +193 -0
  222. package/src/commit/map-reduce/reduce-phase.ts +49 -0
  223. package/src/commit/map-reduce/utils.ts +9 -0
  224. package/src/commit/message.ts +11 -0
  225. package/src/commit/model-selection.ts +89 -0
  226. package/src/commit/pipeline.ts +243 -0
  227. package/src/commit/prompts/analysis-system.md +148 -0
  228. package/src/commit/prompts/analysis-user.md +38 -0
  229. package/src/commit/prompts/changelog-system.md +50 -0
  230. package/src/commit/prompts/changelog-user.md +18 -0
  231. package/src/commit/prompts/file-observer-system.md +24 -0
  232. package/src/commit/prompts/file-observer-user.md +8 -0
  233. package/src/commit/prompts/reduce-system.md +50 -0
  234. package/src/commit/prompts/reduce-user.md +17 -0
  235. package/src/commit/prompts/summary-retry.md +3 -0
  236. package/src/commit/prompts/summary-system.md +38 -0
  237. package/src/commit/prompts/summary-user.md +13 -0
  238. package/src/commit/prompts/types-description.md +2 -0
  239. package/src/commit/shared-llm.ts +70 -0
  240. package/src/commit/types.ts +118 -0
  241. package/src/commit/utils/exclusions.ts +42 -0
  242. package/src/commit/utils.ts +58 -0
  243. package/src/config/api-key-resolver.ts +67 -0
  244. package/src/config/append-only-context-mode.ts +76 -0
  245. package/src/config/config-file.ts +315 -0
  246. package/src/config/file-lock.ts +164 -0
  247. package/src/config/keybindings.ts +634 -0
  248. package/src/config/mcp-schema.json +238 -0
  249. package/src/config/model-discovery.ts +589 -0
  250. package/src/config/model-registry.ts +2260 -0
  251. package/src/config/model-resolver.ts +1819 -0
  252. package/src/config/model-roles.ts +99 -0
  253. package/src/config/models-config-schema.ts +266 -0
  254. package/src/config/models-config.ts +131 -0
  255. package/src/config/prompt-templates.ts +185 -0
  256. package/src/config/resolve-config-value.ts +94 -0
  257. package/src/config/settings-schema.ts +4740 -0
  258. package/src/config/settings.ts +1243 -0
  259. package/src/config.ts +242 -0
  260. package/src/cursor.ts +340 -0
  261. package/src/dap/client.ts +760 -0
  262. package/src/dap/config.ts +189 -0
  263. package/src/dap/defaults.json +212 -0
  264. package/src/dap/index.ts +4 -0
  265. package/src/dap/session.ts +1441 -0
  266. package/src/dap/types.ts +610 -0
  267. package/src/debug/index.ts +559 -0
  268. package/src/debug/log-formatting.ts +58 -0
  269. package/src/debug/log-viewer.ts +908 -0
  270. package/src/debug/profiler.ts +162 -0
  271. package/src/debug/protocol-probe.ts +267 -0
  272. package/src/debug/raw-sse-buffer.ts +294 -0
  273. package/src/debug/raw-sse.ts +292 -0
  274. package/src/debug/remote-debugger.ts +151 -0
  275. package/src/debug/report-bundle.ts +375 -0
  276. package/src/debug/system-info.ts +111 -0
  277. package/src/debug/terminal-info.ts +124 -0
  278. package/src/discovery/agents-md.ts +67 -0
  279. package/src/discovery/agents.ts +230 -0
  280. package/src/discovery/at-imports.ts +273 -0
  281. package/src/discovery/builtin-defaults.ts +39 -0
  282. package/src/discovery/builtin-rules/index.ts +63 -0
  283. package/src/discovery/builtin-rules/low-end/no-hallucinated-apis.md +14 -0
  284. package/src/discovery/builtin-rules/low-end/no-hallucinated-paths.md +14 -0
  285. package/src/discovery/builtin-rules/low-end/no-premature-completion.md +14 -0
  286. package/src/discovery/builtin-rules/rs-box-leak.md +48 -0
  287. package/src/discovery/builtin-rules/rs-future-prelude.md +23 -0
  288. package/src/discovery/builtin-rules/rs-lazylock.md +51 -0
  289. package/src/discovery/builtin-rules/rs-match-ergonomics.md +67 -0
  290. package/src/discovery/builtin-rules/rs-parking-lot.md +44 -0
  291. package/src/discovery/builtin-rules/rs-result-type.md +19 -0
  292. package/src/discovery/builtin-rules/ts-bare-catch.md +38 -0
  293. package/src/discovery/builtin-rules/ts-import-type.md +42 -0
  294. package/src/discovery/builtin-rules/ts-no-any.md +65 -0
  295. package/src/discovery/builtin-rules/ts-no-deprecated-leftovers.md +44 -0
  296. package/src/discovery/builtin-rules/ts-no-dynamic-import.md +39 -0
  297. package/src/discovery/builtin-rules/ts-no-inline-cast-access.md +55 -0
  298. package/src/discovery/builtin-rules/ts-no-return-type.md +44 -0
  299. package/src/discovery/builtin-rules/ts-no-test-timers.md +55 -0
  300. package/src/discovery/builtin-rules/ts-no-tiny-functions.md +51 -0
  301. package/src/discovery/builtin-rules/ts-promise-with-resolvers.md +65 -0
  302. package/src/discovery/builtin-rules/ts-redundant-clear-guard.md +75 -0
  303. package/src/discovery/builtin-rules/ts-set-map.md +28 -0
  304. package/src/discovery/builtin.ts +934 -0
  305. package/src/discovery/claude-plugins.ts +386 -0
  306. package/src/discovery/claude.ts +584 -0
  307. package/src/discovery/cline.ts +83 -0
  308. package/src/discovery/codex.ts +522 -0
  309. package/src/discovery/cursor.ts +220 -0
  310. package/src/discovery/gemini.ts +383 -0
  311. package/src/discovery/github.ts +337 -0
  312. package/src/discovery/helpers.ts +1092 -0
  313. package/src/discovery/index.ts +81 -0
  314. package/src/discovery/mcp-json.ts +172 -0
  315. package/src/discovery/omp-extension-roots.ts +190 -0
  316. package/src/discovery/omp-plugins.ts +383 -0
  317. package/src/discovery/opencode.ts +398 -0
  318. package/src/discovery/plugin-dir-roots.ts +28 -0
  319. package/src/discovery/ssh.ts +153 -0
  320. package/src/discovery/substitute-plugin-root.ts +29 -0
  321. package/src/discovery/vscode.ts +105 -0
  322. package/src/discovery/windsurf.ts +147 -0
  323. package/src/edit/apply-patch/index.ts +87 -0
  324. package/src/edit/apply-patch/parser.ts +174 -0
  325. package/src/edit/diff.ts +999 -0
  326. package/src/edit/file-snapshot-store.ts +143 -0
  327. package/src/edit/hashline/block-resolver.ts +33 -0
  328. package/src/edit/hashline/diff.ts +290 -0
  329. package/src/edit/hashline/execute.ts +237 -0
  330. package/src/edit/hashline/filesystem.ts +130 -0
  331. package/src/edit/hashline/index.ts +5 -0
  332. package/src/edit/hashline/noop-loop-guard.ts +99 -0
  333. package/src/edit/hashline/params.ts +19 -0
  334. package/src/edit/index.ts +620 -0
  335. package/src/edit/modes/apply-patch.lark +19 -0
  336. package/src/edit/modes/apply-patch.ts +53 -0
  337. package/src/edit/modes/patch.ts +1888 -0
  338. package/src/edit/modes/replace.ts +1133 -0
  339. package/src/edit/normalize.ts +345 -0
  340. package/src/edit/notebook.ts +242 -0
  341. package/src/edit/read-file.ts +25 -0
  342. package/src/edit/renderer.ts +823 -0
  343. package/src/edit/streaming.ts +517 -0
  344. package/src/eval/__tests__/agent-bridge.test.ts +769 -0
  345. package/src/eval/__tests__/bridge-timeout.test.ts +64 -0
  346. package/src/eval/__tests__/budget-bridge.test.ts +69 -0
  347. package/src/eval/__tests__/completion-bridge.test.ts +412 -0
  348. package/src/eval/__tests__/helpers-local-roots.test.ts +58 -0
  349. package/src/eval/__tests__/idle-timeout.test.ts +80 -0
  350. package/src/eval/__tests__/js-context-manager.test.ts +291 -0
  351. package/src/eval/__tests__/kernel-spawn.test.ts +103 -0
  352. package/src/eval/__tests__/prelude-agent.test.ts +73 -0
  353. package/src/eval/agent-bridge.ts +319 -0
  354. package/src/eval/backend.ts +71 -0
  355. package/src/eval/bridge-timeout.ts +44 -0
  356. package/src/eval/budget-bridge.ts +48 -0
  357. package/src/eval/completion-bridge.ts +211 -0
  358. package/src/eval/concurrency-bridge.ts +34 -0
  359. package/src/eval/idle-timeout.ts +91 -0
  360. package/src/eval/index.ts +4 -0
  361. package/src/eval/js/context-manager.ts +621 -0
  362. package/src/eval/js/executor.ts +173 -0
  363. package/src/eval/js/index.ts +51 -0
  364. package/src/eval/js/shared/helpers.ts +283 -0
  365. package/src/eval/js/shared/indirect-eval.ts +30 -0
  366. package/src/eval/js/shared/local-module-loader.ts +342 -0
  367. package/src/eval/js/shared/prelude.ts +2 -0
  368. package/src/eval/js/shared/prelude.txt +307 -0
  369. package/src/eval/js/shared/rewrite-imports.ts +532 -0
  370. package/src/eval/js/shared/runtime.ts +580 -0
  371. package/src/eval/js/shared/types.ts +18 -0
  372. package/src/eval/js/tool-bridge.ts +163 -0
  373. package/src/eval/js/worker-core.ts +151 -0
  374. package/src/eval/js/worker-entry.ts +37 -0
  375. package/src/eval/js/worker-protocol.ts +47 -0
  376. package/src/eval/py/__tests__/prelude.test.ts +19 -0
  377. package/src/eval/py/display.ts +71 -0
  378. package/src/eval/py/executor.ts +742 -0
  379. package/src/eval/py/index.ts +68 -0
  380. package/src/eval/py/kernel.ts +748 -0
  381. package/src/eval/py/prelude.py +683 -0
  382. package/src/eval/py/prelude.ts +3 -0
  383. package/src/eval/py/runner.py +1177 -0
  384. package/src/eval/py/runtime.ts +276 -0
  385. package/src/eval/py/spawn-options.ts +126 -0
  386. package/src/eval/py/tool-bridge.ts +182 -0
  387. package/src/eval/session-id.ts +8 -0
  388. package/src/eval/types.ts +48 -0
  389. package/src/exa/index.ts +2 -0
  390. package/src/exa/mcp-client.ts +370 -0
  391. package/src/exa/types.ts +69 -0
  392. package/src/exec/bash-executor.ts +434 -0
  393. package/src/exec/exec.ts +53 -0
  394. package/src/exec/non-interactive-env.ts +119 -0
  395. package/src/export/custom-share.ts +65 -0
  396. package/src/export/html/index.ts +266 -0
  397. package/src/export/html/share-loader.js +102 -0
  398. package/src/export/html/template.css +1337 -0
  399. package/src/export/html/template.html +49 -0
  400. package/src/export/html/template.js +1626 -0
  401. package/src/export/html/tool-views.generated.js +37 -0
  402. package/src/export/html/vendor/highlight.min.js +1213 -0
  403. package/src/export/html/vendor/marked.min.js +6 -0
  404. package/src/export/share.ts +268 -0
  405. package/src/export/ttsr.ts +583 -0
  406. package/src/extensibility/custom-commands/bundled/ci-green/index.ts +54 -0
  407. package/src/extensibility/custom-commands/bundled/review/index.ts +698 -0
  408. package/src/extensibility/custom-commands/index.ts +2 -0
  409. package/src/extensibility/custom-commands/loader.ts +242 -0
  410. package/src/extensibility/custom-commands/types.ts +119 -0
  411. package/src/extensibility/custom-tools/index.ts +7 -0
  412. package/src/extensibility/custom-tools/loader.ts +268 -0
  413. package/src/extensibility/custom-tools/types.ts +277 -0
  414. package/src/extensibility/custom-tools/wrapper.ts +47 -0
  415. package/src/extensibility/extensions/compact-handler.ts +40 -0
  416. package/src/extensibility/extensions/get-commands-handler.ts +78 -0
  417. package/src/extensibility/extensions/index.ts +16 -0
  418. package/src/extensibility/extensions/loader.ts +587 -0
  419. package/src/extensibility/extensions/model-api.ts +41 -0
  420. package/src/extensibility/extensions/runner.ts +989 -0
  421. package/src/extensibility/extensions/types.ts +1394 -0
  422. package/src/extensibility/extensions/wrapper.ts +259 -0
  423. package/src/extensibility/hooks/index.ts +6 -0
  424. package/src/extensibility/hooks/loader.ts +262 -0
  425. package/src/extensibility/hooks/runner.ts +425 -0
  426. package/src/extensibility/hooks/tool-wrapper.ts +107 -0
  427. package/src/extensibility/hooks/types.ts +613 -0
  428. package/src/extensibility/legacy-pi-ai-shim.ts +61 -0
  429. package/src/extensibility/legacy-pi-coding-agent-shim.ts +128 -0
  430. package/src/extensibility/plugins/doctor.ts +65 -0
  431. package/src/extensibility/plugins/git-url.ts +367 -0
  432. package/src/extensibility/plugins/index.ts +9 -0
  433. package/src/extensibility/plugins/installer.ts +192 -0
  434. package/src/extensibility/plugins/legacy-pi-compat.ts +712 -0
  435. package/src/extensibility/plugins/loader.ts +458 -0
  436. package/src/extensibility/plugins/manager.ts +1026 -0
  437. package/src/extensibility/plugins/marketplace/cache.ts +136 -0
  438. package/src/extensibility/plugins/marketplace/fetcher.ts +315 -0
  439. package/src/extensibility/plugins/marketplace/index.ts +6 -0
  440. package/src/extensibility/plugins/marketplace/manager.ts +770 -0
  441. package/src/extensibility/plugins/marketplace/registry.ts +196 -0
  442. package/src/extensibility/plugins/marketplace/source-resolver.ts +147 -0
  443. package/src/extensibility/plugins/marketplace/types.ts +191 -0
  444. package/src/extensibility/plugins/marketplace-auto-update.ts +49 -0
  445. package/src/extensibility/plugins/parser.ts +105 -0
  446. package/src/extensibility/plugins/runtime-config.ts +9 -0
  447. package/src/extensibility/plugins/types.ts +194 -0
  448. package/src/extensibility/shared-events.ts +367 -0
  449. package/src/extensibility/skills.ts +408 -0
  450. package/src/extensibility/slash-commands.ts +131 -0
  451. package/src/extensibility/tool-proxy.ts +28 -0
  452. package/src/extensibility/typebox.ts +945 -0
  453. package/src/extensibility/utils.ts +44 -0
  454. package/src/goals/guided-setup.ts +142 -0
  455. package/src/goals/index.ts +3 -0
  456. package/src/goals/runtime.ts +521 -0
  457. package/src/goals/state.ts +37 -0
  458. package/src/goals/tools/goal-tool.ts +251 -0
  459. package/src/hindsight/backend.ts +354 -0
  460. package/src/hindsight/bank.ts +156 -0
  461. package/src/hindsight/client.ts +623 -0
  462. package/src/hindsight/config.ts +175 -0
  463. package/src/hindsight/content.ts +210 -0
  464. package/src/hindsight/index.ts +8 -0
  465. package/src/hindsight/mental-models.ts +429 -0
  466. package/src/hindsight/seeds.json +32 -0
  467. package/src/hindsight/state.ts +492 -0
  468. package/src/hindsight/transcript.ts +71 -0
  469. package/src/index.ts +66 -0
  470. package/src/internal-urls/agent-protocol.ts +146 -0
  471. package/src/internal-urls/artifact-protocol.ts +107 -0
  472. package/src/internal-urls/docs-index.generated.txt +2 -0
  473. package/src/internal-urls/docs-index.ts +102 -0
  474. package/src/internal-urls/history-protocol.ts +118 -0
  475. package/src/internal-urls/index.ts +25 -0
  476. package/src/internal-urls/issue-pr-protocol.ts +594 -0
  477. package/src/internal-urls/json-query.ts +126 -0
  478. package/src/internal-urls/local-protocol.ts +309 -0
  479. package/src/internal-urls/mcp-protocol.ts +151 -0
  480. package/src/internal-urls/memory-protocol.ts +169 -0
  481. package/src/internal-urls/omp-protocol.ts +94 -0
  482. package/src/internal-urls/parse.ts +72 -0
  483. package/src/internal-urls/registry-helpers.ts +25 -0
  484. package/src/internal-urls/router.ts +105 -0
  485. package/src/internal-urls/rule-protocol.ts +45 -0
  486. package/src/internal-urls/skill-protocol.ts +96 -0
  487. package/src/internal-urls/types.ts +152 -0
  488. package/src/internal-urls/vault-protocol.ts +936 -0
  489. package/src/irc/bus.ts +311 -0
  490. package/src/lib/xai-http.ts +124 -0
  491. package/src/lsp/client.ts +1217 -0
  492. package/src/lsp/clients/biome-client.ts +264 -0
  493. package/src/lsp/clients/index.ts +50 -0
  494. package/src/lsp/clients/lsp-linter-client.ts +85 -0
  495. package/src/lsp/clients/swiftlint-client.ts +120 -0
  496. package/src/lsp/config.ts +502 -0
  497. package/src/lsp/defaults.json +499 -0
  498. package/src/lsp/diagnostics-ledger.ts +51 -0
  499. package/src/lsp/edits.ts +267 -0
  500. package/src/lsp/format-options.ts +119 -0
  501. package/src/lsp/index.ts +2480 -0
  502. package/src/lsp/lspmux.ts +233 -0
  503. package/src/lsp/render.ts +668 -0
  504. package/src/lsp/startup-events.ts +13 -0
  505. package/src/lsp/types.ts +444 -0
  506. package/src/lsp/utils.ts +718 -0
  507. package/src/main.ts +1421 -0
  508. package/src/markit/NOTICE +32 -0
  509. package/src/markit/converters/docx.ts +56 -0
  510. package/src/markit/converters/epub.ts +136 -0
  511. package/src/markit/converters/mammoth.d.ts +24 -0
  512. package/src/markit/converters/pdf/columns.ts +103 -0
  513. package/src/markit/converters/pdf/extract.ts +574 -0
  514. package/src/markit/converters/pdf/grid.ts +780 -0
  515. package/src/markit/converters/pdf/headers.ts +106 -0
  516. package/src/markit/converters/pdf/index.ts +146 -0
  517. package/src/markit/converters/pdf/render.ts +501 -0
  518. package/src/markit/converters/pdf/types.ts +84 -0
  519. package/src/markit/converters/pptx.ts +325 -0
  520. package/src/markit/converters/xlsx.ts +173 -0
  521. package/src/markit/index.ts +2 -0
  522. package/src/markit/registry.ts +59 -0
  523. package/src/markit/types.ts +35 -0
  524. package/src/mcp/client.ts +509 -0
  525. package/src/mcp/config-writer.ts +229 -0
  526. package/src/mcp/config.ts +365 -0
  527. package/src/mcp/index.ts +29 -0
  528. package/src/mcp/json-rpc.ts +122 -0
  529. package/src/mcp/loader.ts +124 -0
  530. package/src/mcp/manager.ts +1326 -0
  531. package/src/mcp/oauth-credentials.ts +104 -0
  532. package/src/mcp/oauth-discovery.ts +467 -0
  533. package/src/mcp/oauth-flow.ts +555 -0
  534. package/src/mcp/render.ts +155 -0
  535. package/src/mcp/smithery-auth.ts +104 -0
  536. package/src/mcp/smithery-connect.ts +145 -0
  537. package/src/mcp/smithery-registry.ts +477 -0
  538. package/src/mcp/startup-events.ts +21 -0
  539. package/src/mcp/timeout.ts +59 -0
  540. package/src/mcp/tool-bridge.ts +429 -0
  541. package/src/mcp/tool-cache.ts +117 -0
  542. package/src/mcp/transports/http.ts +519 -0
  543. package/src/mcp/transports/index.ts +6 -0
  544. package/src/mcp/transports/stdio.ts +606 -0
  545. package/src/mcp/types.ts +427 -0
  546. package/src/memories/index.ts +1281 -0
  547. package/src/memories/storage.ts +578 -0
  548. package/src/memory-backend/index.ts +18 -0
  549. package/src/memory-backend/local-backend.ts +45 -0
  550. package/src/memory-backend/off-backend.ts +25 -0
  551. package/src/memory-backend/resolve.ts +25 -0
  552. package/src/memory-backend/runtime.ts +66 -0
  553. package/src/memory-backend/types.ts +166 -0
  554. package/src/mnemopi/backend.ts +612 -0
  555. package/src/mnemopi/config.ts +265 -0
  556. package/src/mnemopi/embed-client.ts +401 -0
  557. package/src/mnemopi/embed-protocol.ts +35 -0
  558. package/src/mnemopi/embed-worker.ts +113 -0
  559. package/src/mnemopi/index.ts +3 -0
  560. package/src/mnemopi/state.ts +657 -0
  561. package/src/modes/acp/acp-agent.ts +2362 -0
  562. package/src/modes/acp/acp-client-bridge.ts +154 -0
  563. package/src/modes/acp/acp-event-mapper.ts +933 -0
  564. package/src/modes/acp/acp-mode.ts +23 -0
  565. package/src/modes/acp/index.ts +2 -0
  566. package/src/modes/acp/terminal-auth.ts +37 -0
  567. package/src/modes/components/__tests__/skill-message.test.ts +92 -0
  568. package/src/modes/components/advisor-message.ts +99 -0
  569. package/src/modes/components/agent-dashboard.ts +1206 -0
  570. package/src/modes/components/agent-hub.ts +566 -0
  571. package/src/modes/components/agent-transcript-viewer.ts +461 -0
  572. package/src/modes/components/assistant-message.ts +612 -0
  573. package/src/modes/components/background-tan-message.ts +36 -0
  574. package/src/modes/components/bash-execution.ts +220 -0
  575. package/src/modes/components/bordered-loader.ts +41 -0
  576. package/src/modes/components/btw-panel.ts +112 -0
  577. package/src/modes/components/cache-invalidation-marker.ts +110 -0
  578. package/src/modes/components/chat-block.ts +111 -0
  579. package/src/modes/components/chat-transcript-builder.ts +476 -0
  580. package/src/modes/components/collab-prompt-message.ts +32 -0
  581. package/src/modes/components/compaction-summary-message.ts +215 -0
  582. package/src/modes/components/copy-selector.ts +206 -0
  583. package/src/modes/components/countdown-timer.ts +75 -0
  584. package/src/modes/components/custom-editor.test.ts +142 -0
  585. package/src/modes/components/custom-editor.ts +620 -0
  586. package/src/modes/components/custom-message.ts +67 -0
  587. package/src/modes/components/diff.ts +254 -0
  588. package/src/modes/components/dynamic-border.ts +34 -0
  589. package/src/modes/components/error-banner.ts +33 -0
  590. package/src/modes/components/eval-execution.ts +158 -0
  591. package/src/modes/components/execution-shared.ts +101 -0
  592. package/src/modes/components/extensions/extension-dashboard.ts +399 -0
  593. package/src/modes/components/extensions/extension-list.ts +502 -0
  594. package/src/modes/components/extensions/index.ts +9 -0
  595. package/src/modes/components/extensions/inspector-panel.ts +321 -0
  596. package/src/modes/components/extensions/state-manager.ts +627 -0
  597. package/src/modes/components/extensions/types.ts +186 -0
  598. package/src/modes/components/footer.ts +275 -0
  599. package/src/modes/components/history-search.ts +280 -0
  600. package/src/modes/components/hook-editor.ts +167 -0
  601. package/src/modes/components/hook-input.ts +87 -0
  602. package/src/modes/components/hook-message.ts +67 -0
  603. package/src/modes/components/hook-selector.ts +659 -0
  604. package/src/modes/components/index.ts +38 -0
  605. package/src/modes/components/keybinding-hints.ts +65 -0
  606. package/src/modes/components/late-diagnostics-message.ts +60 -0
  607. package/src/modes/components/login-dialog.ts +164 -0
  608. package/src/modes/components/logout-account-selector.ts +130 -0
  609. package/src/modes/components/mcp-add-wizard.ts +1360 -0
  610. package/src/modes/components/message-frame.ts +92 -0
  611. package/src/modes/components/model-selector.ts +1315 -0
  612. package/src/modes/components/oauth-selector.ts +457 -0
  613. package/src/modes/components/omfg-panel.ts +141 -0
  614. package/src/modes/components/overlay-box.ts +109 -0
  615. package/src/modes/components/plan-review-overlay.ts +847 -0
  616. package/src/modes/components/plan-toc.ts +138 -0
  617. package/src/modes/components/plugin-selector.ts +95 -0
  618. package/src/modes/components/plugin-settings.ts +739 -0
  619. package/src/modes/components/queue-mode-selector.ts +56 -0
  620. package/src/modes/components/read-tool-group.ts +676 -0
  621. package/src/modes/components/reset-usage-selector.ts +161 -0
  622. package/src/modes/components/segment-track.ts +89 -0
  623. package/src/modes/components/session-selector.ts +631 -0
  624. package/src/modes/components/settings-defs.ts +225 -0
  625. package/src/modes/components/settings-selector.ts +1095 -0
  626. package/src/modes/components/show-images-selector.ts +45 -0
  627. package/src/modes/components/skill-message.ts +110 -0
  628. package/src/modes/components/snapcompact-shape-preview-doc.md +18 -0
  629. package/src/modes/components/snapcompact-shape-preview.ts +192 -0
  630. package/src/modes/components/status-line/component.ts +1001 -0
  631. package/src/modes/components/status-line/context-thresholds.ts +78 -0
  632. package/src/modes/components/status-line/git-utils.ts +42 -0
  633. package/src/modes/components/status-line/index.ts +5 -0
  634. package/src/modes/components/status-line/presets.ts +106 -0
  635. package/src/modes/components/status-line/segments.ts +616 -0
  636. package/src/modes/components/status-line/separators.ts +55 -0
  637. package/src/modes/components/status-line/token-rate.ts +66 -0
  638. package/src/modes/components/status-line/types.ts +124 -0
  639. package/src/modes/components/theme-selector.ts +63 -0
  640. package/src/modes/components/thinking-selector.ts +52 -0
  641. package/src/modes/components/tiny-title-download-progress.ts +90 -0
  642. package/src/modes/components/tips.txt +24 -0
  643. package/src/modes/components/todo-reminder.ts +39 -0
  644. package/src/modes/components/tool-execution.ts +1165 -0
  645. package/src/modes/components/transcript-container.ts +806 -0
  646. package/src/modes/components/tree-selector.ts +994 -0
  647. package/src/modes/components/ttsr-notification.ts +123 -0
  648. package/src/modes/components/usage-row.ts +18 -0
  649. package/src/modes/components/user-message-selector.ts +227 -0
  650. package/src/modes/components/user-message.ts +68 -0
  651. package/src/modes/components/visual-truncate.ts +63 -0
  652. package/src/modes/components/welcome.ts +581 -0
  653. package/src/modes/controllers/btw-controller.ts +173 -0
  654. package/src/modes/controllers/command-controller-shared.ts +109 -0
  655. package/src/modes/controllers/command-controller.ts +1653 -0
  656. package/src/modes/controllers/event-controller.ts +1153 -0
  657. package/src/modes/controllers/extension-ui-controller.ts +893 -0
  658. package/src/modes/controllers/input-controller.ts +1627 -0
  659. package/src/modes/controllers/mcp-command-controller.ts +2162 -0
  660. package/src/modes/controllers/omfg-controller.ts +283 -0
  661. package/src/modes/controllers/omfg-rule.ts +647 -0
  662. package/src/modes/controllers/selector-controller.ts +1285 -0
  663. package/src/modes/controllers/session-focus-controller.ts +112 -0
  664. package/src/modes/controllers/ssh-command-controller.ts +384 -0
  665. package/src/modes/controllers/streaming-reveal.ts +295 -0
  666. package/src/modes/controllers/tan-command-controller.ts +190 -0
  667. package/src/modes/controllers/todo-command-controller.ts +485 -0
  668. package/src/modes/controllers/tool-args-reveal.ts +174 -0
  669. package/src/modes/data/emojis.json +1 -0
  670. package/src/modes/emoji-autocomplete.ts +285 -0
  671. package/src/modes/gradient-highlight.ts +99 -0
  672. package/src/modes/image-references.ts +137 -0
  673. package/src/modes/index.ts +17 -0
  674. package/src/modes/interactive-mode.ts +3940 -0
  675. package/src/modes/internal-url-autocomplete.ts +143 -0
  676. package/src/modes/loop-limit.ts +192 -0
  677. package/src/modes/magic-keywords.ts +42 -0
  678. package/src/modes/markdown-prose.ts +247 -0
  679. package/src/modes/oauth-manual-input.ts +69 -0
  680. package/src/modes/orchestrate.ts +42 -0
  681. package/src/modes/print-mode.ts +130 -0
  682. package/src/modes/prompt-action-autocomplete.ts +260 -0
  683. package/src/modes/rpc/host-tools.ts +186 -0
  684. package/src/modes/rpc/host-uris.ts +235 -0
  685. package/src/modes/rpc/rpc-client.ts +995 -0
  686. package/src/modes/rpc/rpc-mode.ts +1156 -0
  687. package/src/modes/rpc/rpc-subagents.ts +265 -0
  688. package/src/modes/rpc/rpc-types.ts +487 -0
  689. package/src/modes/runtime-init.ts +142 -0
  690. package/src/modes/session-observer-registry.ts +215 -0
  691. package/src/modes/setup-version.ts +11 -0
  692. package/src/modes/setup-wizard/index.ts +101 -0
  693. package/src/modes/setup-wizard/lazy.ts +16 -0
  694. package/src/modes/setup-wizard/scenes/glyph.ts +114 -0
  695. package/src/modes/setup-wizard/scenes/outro.ts +35 -0
  696. package/src/modes/setup-wizard/scenes/providers.ts +103 -0
  697. package/src/modes/setup-wizard/scenes/sign-in.ts +286 -0
  698. package/src/modes/setup-wizard/scenes/splash.ts +201 -0
  699. package/src/modes/setup-wizard/scenes/theme.ts +326 -0
  700. package/src/modes/setup-wizard/scenes/types.ts +57 -0
  701. package/src/modes/setup-wizard/scenes/web-search.ts +145 -0
  702. package/src/modes/setup-wizard/startup-splash.ts +107 -0
  703. package/src/modes/setup-wizard/wizard-overlay.ts +334 -0
  704. package/src/modes/shared.ts +49 -0
  705. package/src/modes/theme/dark.json +95 -0
  706. package/src/modes/theme/defaults/alabaster.json +93 -0
  707. package/src/modes/theme/defaults/amethyst.json +96 -0
  708. package/src/modes/theme/defaults/anthracite.json +93 -0
  709. package/src/modes/theme/defaults/basalt.json +91 -0
  710. package/src/modes/theme/defaults/birch.json +95 -0
  711. package/src/modes/theme/defaults/dark-abyss.json +91 -0
  712. package/src/modes/theme/defaults/dark-arctic.json +104 -0
  713. package/src/modes/theme/defaults/dark-aurora.json +95 -0
  714. package/src/modes/theme/defaults/dark-catppuccin.json +107 -0
  715. package/src/modes/theme/defaults/dark-cavern.json +91 -0
  716. package/src/modes/theme/defaults/dark-copper.json +95 -0
  717. package/src/modes/theme/defaults/dark-cosmos.json +90 -0
  718. package/src/modes/theme/defaults/dark-cyberpunk.json +102 -0
  719. package/src/modes/theme/defaults/dark-dracula.json +98 -0
  720. package/src/modes/theme/defaults/dark-eclipse.json +91 -0
  721. package/src/modes/theme/defaults/dark-ember.json +95 -0
  722. package/src/modes/theme/defaults/dark-equinox.json +90 -0
  723. package/src/modes/theme/defaults/dark-forest.json +96 -0
  724. package/src/modes/theme/defaults/dark-github.json +105 -0
  725. package/src/modes/theme/defaults/dark-gruvbox.json +112 -0
  726. package/src/modes/theme/defaults/dark-lavender.json +95 -0
  727. package/src/modes/theme/defaults/dark-lunar.json +89 -0
  728. package/src/modes/theme/defaults/dark-midnight.json +95 -0
  729. package/src/modes/theme/defaults/dark-monochrome.json +94 -0
  730. package/src/modes/theme/defaults/dark-monokai.json +98 -0
  731. package/src/modes/theme/defaults/dark-nebula.json +90 -0
  732. package/src/modes/theme/defaults/dark-nord.json +97 -0
  733. package/src/modes/theme/defaults/dark-ocean.json +101 -0
  734. package/src/modes/theme/defaults/dark-one.json +100 -0
  735. package/src/modes/theme/defaults/dark-poimandres.json +142 -0
  736. package/src/modes/theme/defaults/dark-rainforest.json +91 -0
  737. package/src/modes/theme/defaults/dark-reef.json +91 -0
  738. package/src/modes/theme/defaults/dark-retro.json +92 -0
  739. package/src/modes/theme/defaults/dark-rose-pine.json +96 -0
  740. package/src/modes/theme/defaults/dark-sakura.json +95 -0
  741. package/src/modes/theme/defaults/dark-slate.json +95 -0
  742. package/src/modes/theme/defaults/dark-solarized.json +97 -0
  743. package/src/modes/theme/defaults/dark-solstice.json +90 -0
  744. package/src/modes/theme/defaults/dark-starfall.json +91 -0
  745. package/src/modes/theme/defaults/dark-sunset.json +99 -0
  746. package/src/modes/theme/defaults/dark-swamp.json +90 -0
  747. package/src/modes/theme/defaults/dark-synthwave.json +103 -0
  748. package/src/modes/theme/defaults/dark-taiga.json +91 -0
  749. package/src/modes/theme/defaults/dark-terminal.json +95 -0
  750. package/src/modes/theme/defaults/dark-tokyo-night.json +101 -0
  751. package/src/modes/theme/defaults/dark-tundra.json +91 -0
  752. package/src/modes/theme/defaults/dark-twilight.json +91 -0
  753. package/src/modes/theme/defaults/dark-volcanic.json +91 -0
  754. package/src/modes/theme/defaults/graphite.json +92 -0
  755. package/src/modes/theme/defaults/index.ts +199 -0
  756. package/src/modes/theme/defaults/light-arctic.json +107 -0
  757. package/src/modes/theme/defaults/light-aurora-day.json +91 -0
  758. package/src/modes/theme/defaults/light-canyon.json +91 -0
  759. package/src/modes/theme/defaults/light-catppuccin.json +106 -0
  760. package/src/modes/theme/defaults/light-cirrus.json +90 -0
  761. package/src/modes/theme/defaults/light-coral.json +95 -0
  762. package/src/modes/theme/defaults/light-cyberpunk.json +96 -0
  763. package/src/modes/theme/defaults/light-dawn.json +90 -0
  764. package/src/modes/theme/defaults/light-dunes.json +91 -0
  765. package/src/modes/theme/defaults/light-eucalyptus.json +95 -0
  766. package/src/modes/theme/defaults/light-forest.json +100 -0
  767. package/src/modes/theme/defaults/light-frost.json +95 -0
  768. package/src/modes/theme/defaults/light-github.json +115 -0
  769. package/src/modes/theme/defaults/light-glacier.json +91 -0
  770. package/src/modes/theme/defaults/light-gruvbox.json +108 -0
  771. package/src/modes/theme/defaults/light-haze.json +90 -0
  772. package/src/modes/theme/defaults/light-honeycomb.json +95 -0
  773. package/src/modes/theme/defaults/light-lagoon.json +91 -0
  774. package/src/modes/theme/defaults/light-lavender.json +95 -0
  775. package/src/modes/theme/defaults/light-meadow.json +91 -0
  776. package/src/modes/theme/defaults/light-mint.json +95 -0
  777. package/src/modes/theme/defaults/light-monochrome.json +101 -0
  778. package/src/modes/theme/defaults/light-ocean.json +99 -0
  779. package/src/modes/theme/defaults/light-one.json +99 -0
  780. package/src/modes/theme/defaults/light-opal.json +91 -0
  781. package/src/modes/theme/defaults/light-orchard.json +91 -0
  782. package/src/modes/theme/defaults/light-paper.json +95 -0
  783. package/src/modes/theme/defaults/light-poimandres.json +142 -0
  784. package/src/modes/theme/defaults/light-prism.json +90 -0
  785. package/src/modes/theme/defaults/light-retro.json +98 -0
  786. package/src/modes/theme/defaults/light-sand.json +95 -0
  787. package/src/modes/theme/defaults/light-savanna.json +91 -0
  788. package/src/modes/theme/defaults/light-solarized.json +102 -0
  789. package/src/modes/theme/defaults/light-soleil.json +90 -0
  790. package/src/modes/theme/defaults/light-sunset.json +99 -0
  791. package/src/modes/theme/defaults/light-synthwave.json +98 -0
  792. package/src/modes/theme/defaults/light-tokyo-night.json +111 -0
  793. package/src/modes/theme/defaults/light-wetland.json +91 -0
  794. package/src/modes/theme/defaults/light-zenith.json +89 -0
  795. package/src/modes/theme/defaults/limestone.json +94 -0
  796. package/src/modes/theme/defaults/mahogany.json +97 -0
  797. package/src/modes/theme/defaults/marble.json +93 -0
  798. package/src/modes/theme/defaults/obsidian.json +91 -0
  799. package/src/modes/theme/defaults/onyx.json +91 -0
  800. package/src/modes/theme/defaults/pearl.json +93 -0
  801. package/src/modes/theme/defaults/porcelain.json +91 -0
  802. package/src/modes/theme/defaults/quartz.json +96 -0
  803. package/src/modes/theme/defaults/sandstone.json +95 -0
  804. package/src/modes/theme/defaults/titanium.json +90 -0
  805. package/src/modes/theme/light.json +93 -0
  806. package/src/modes/theme/mermaid-cache.ts +92 -0
  807. package/src/modes/theme/shimmer.ts +235 -0
  808. package/src/modes/theme/theme-schema.json +459 -0
  809. package/src/modes/theme/theme.ts +2915 -0
  810. package/src/modes/turn-budget.ts +31 -0
  811. package/src/modes/types.ts +406 -0
  812. package/src/modes/ultrathink.ts +41 -0
  813. package/src/modes/utils/context-usage.ts +432 -0
  814. package/src/modes/utils/copy-targets.ts +360 -0
  815. package/src/modes/utils/hotkeys-markdown.ts +62 -0
  816. package/src/modes/utils/keybinding-matchers.ts +51 -0
  817. package/src/modes/utils/tools-markdown.ts +27 -0
  818. package/src/modes/utils/ui-helpers.ts +886 -0
  819. package/src/modes/workflow.ts +42 -0
  820. package/src/plan-mode/approved-plan.ts +186 -0
  821. package/src/plan-mode/plan-handoff.ts +37 -0
  822. package/src/plan-mode/plan-protection.ts +31 -0
  823. package/src/plan-mode/state.ts +6 -0
  824. package/src/priority.json +45 -0
  825. package/src/prompts/advisor/advise-tool.md +3 -0
  826. package/src/prompts/advisor/system.md +113 -0
  827. package/src/prompts/agents/designer.md +74 -0
  828. package/src/prompts/agents/explore.md +58 -0
  829. package/src/prompts/agents/frontmatter.md +11 -0
  830. package/src/prompts/agents/init.md +33 -0
  831. package/src/prompts/agents/librarian.md +119 -0
  832. package/src/prompts/agents/oracle.md +54 -0
  833. package/src/prompts/agents/plan.md +48 -0
  834. package/src/prompts/agents/reviewer.md +139 -0
  835. package/src/prompts/agents/task.md +17 -0
  836. package/src/prompts/bench.md +12 -0
  837. package/src/prompts/ci-green-request.md +36 -0
  838. package/src/prompts/dry-balance-bench.md +8 -0
  839. package/src/prompts/goals/goal-budget-limit.md +16 -0
  840. package/src/prompts/goals/goal-continuation.md +28 -0
  841. package/src/prompts/goals/goal-mode-active.md +23 -0
  842. package/src/prompts/goals/guided-goal-interview.md +8 -0
  843. package/src/prompts/goals/guided-goal-system.md +12 -0
  844. package/src/prompts/low-end/system.md +47 -0
  845. package/src/prompts/memories/consolidation.md +30 -0
  846. package/src/prompts/memories/consolidation_system.md +4 -0
  847. package/src/prompts/memories/read-path.md +17 -0
  848. package/src/prompts/memories/stage_one_input.md +6 -0
  849. package/src/prompts/memories/stage_one_system.md +21 -0
  850. package/src/prompts/review-custom-request.md +22 -0
  851. package/src/prompts/review-headless-request.md +16 -0
  852. package/src/prompts/review-request.md +69 -0
  853. package/src/prompts/steering/user-interjection.md +9 -0
  854. package/src/prompts/system/agent-creation-architect.md +50 -0
  855. package/src/prompts/system/agent-creation-user.md +6 -0
  856. package/src/prompts/system/auto-continue.md +1 -0
  857. package/src/prompts/system/auto-thinking-difficulty-local.md +14 -0
  858. package/src/prompts/system/auto-thinking-difficulty.md +12 -0
  859. package/src/prompts/system/autolearn-guidance-learn.md +1 -0
  860. package/src/prompts/system/autolearn-guidance.md +7 -0
  861. package/src/prompts/system/autolearn-nudge.md +3 -0
  862. package/src/prompts/system/background-tan-dispatch.md +8 -0
  863. package/src/prompts/system/btw-user.md +8 -0
  864. package/src/prompts/system/commit-message-system.md +14 -0
  865. package/src/prompts/system/custom-system-prompt.md +64 -0
  866. package/src/prompts/system/eager-task.md +7 -0
  867. package/src/prompts/system/eager-todo.md +18 -0
  868. package/src/prompts/system/empty-stop-retry.md +4 -0
  869. package/src/prompts/system/irc-autoreply.md +6 -0
  870. package/src/prompts/system/irc-incoming.md +7 -0
  871. package/src/prompts/system/manual-continue.md +7 -0
  872. package/src/prompts/system/memory-consolidation-system.md +8 -0
  873. package/src/prompts/system/memory-extraction-system.md +26 -0
  874. package/src/prompts/system/omfg-user.md +50 -0
  875. package/src/prompts/system/orchestrate-notice.md +40 -0
  876. package/src/prompts/system/personalities/default.md +18 -0
  877. package/src/prompts/system/personalities/friendly.md +17 -0
  878. package/src/prompts/system/personalities/pragmatic.md +15 -0
  879. package/src/prompts/system/plan-mode-active.md +109 -0
  880. package/src/prompts/system/plan-mode-approved.md +25 -0
  881. package/src/prompts/system/plan-mode-compact-instructions.md +16 -0
  882. package/src/prompts/system/plan-mode-reference.md +11 -0
  883. package/src/prompts/system/plan-mode-subagent.md +33 -0
  884. package/src/prompts/system/plan-mode-tool-decision-reminder.md +9 -0
  885. package/src/prompts/system/project-prompt.md +52 -0
  886. package/src/prompts/system/snapcompact-context-frames-note.md +1 -0
  887. package/src/prompts/system/snapcompact-context-stub.md +1 -0
  888. package/src/prompts/system/snapcompact-system-frames-note.md +1 -0
  889. package/src/prompts/system/snapcompact-system-stub.md +1 -0
  890. package/src/prompts/system/snapcompact-toolresult-note.md +1 -0
  891. package/src/prompts/system/subagent-system-prompt.md +71 -0
  892. package/src/prompts/system/subagent-user-prompt.md +3 -0
  893. package/src/prompts/system/subagent-yield-reminder.md +12 -0
  894. package/src/prompts/system/system-prompt.md +251 -0
  895. package/src/prompts/system/tiny-title-system.md +8 -0
  896. package/src/prompts/system/title-marker-instruction.md +1 -0
  897. package/src/prompts/system/title-system-marker.md +16 -0
  898. package/src/prompts/system/title-system.md +16 -0
  899. package/src/prompts/system/ttsr-interrupt.md +7 -0
  900. package/src/prompts/system/ttsr-tool-reminder.md +5 -0
  901. package/src/prompts/system/ultrathink-notice.md +3 -0
  902. package/src/prompts/system/unexpected-stop-classifier.md +17 -0
  903. package/src/prompts/system/unexpected-stop-retry.md +4 -0
  904. package/src/prompts/system/web-search.md +25 -0
  905. package/src/prompts/system/workflow-notice.md +70 -0
  906. package/src/prompts/tools/apply-patch.md +65 -0
  907. package/src/prompts/tools/ask.md +22 -0
  908. package/src/prompts/tools/ast-edit.md +22 -0
  909. package/src/prompts/tools/ast-grep.md +25 -0
  910. package/src/prompts/tools/async-result.md +8 -0
  911. package/src/prompts/tools/bash.md +45 -0
  912. package/src/prompts/tools/browser.md +42 -0
  913. package/src/prompts/tools/checkpoint.md +15 -0
  914. package/src/prompts/tools/debug.md +17 -0
  915. package/src/prompts/tools/eval.md +70 -0
  916. package/src/prompts/tools/find.md +19 -0
  917. package/src/prompts/tools/github.md +17 -0
  918. package/src/prompts/tools/goal.md +11 -0
  919. package/src/prompts/tools/image-attachment-describe-system.md +8 -0
  920. package/src/prompts/tools/image-attachment-describe.md +10 -0
  921. package/src/prompts/tools/image-gen.md +7 -0
  922. package/src/prompts/tools/inspect-image-system.md +20 -0
  923. package/src/prompts/tools/inspect-image.md +22 -0
  924. package/src/prompts/tools/irc.md +33 -0
  925. package/src/prompts/tools/job.md +17 -0
  926. package/src/prompts/tools/learn.md +7 -0
  927. package/src/prompts/tools/lsp-late-diagnostic.md +8 -0
  928. package/src/prompts/tools/lsp.md +39 -0
  929. package/src/prompts/tools/manage-skill.md +9 -0
  930. package/src/prompts/tools/memory-edit.md +8 -0
  931. package/src/prompts/tools/patch.md +57 -0
  932. package/src/prompts/tools/read.md +76 -0
  933. package/src/prompts/tools/recall.md +5 -0
  934. package/src/prompts/tools/reflect.md +5 -0
  935. package/src/prompts/tools/replace.md +29 -0
  936. package/src/prompts/tools/resolve.md +4 -0
  937. package/src/prompts/tools/retain.md +6 -0
  938. package/src/prompts/tools/rewind.md +13 -0
  939. package/src/prompts/tools/search-tool-bm25.md +32 -0
  940. package/src/prompts/tools/search.md +22 -0
  941. package/src/prompts/tools/ssh.md +22 -0
  942. package/src/prompts/tools/task-summary.md +17 -0
  943. package/src/prompts/tools/task.md +91 -0
  944. package/src/prompts/tools/todo.md +39 -0
  945. package/src/prompts/tools/web-search.md +6 -0
  946. package/src/prompts/tools/write.md +14 -0
  947. package/src/registry/agent-lifecycle.ts +270 -0
  948. package/src/registry/agent-registry.ts +190 -0
  949. package/src/sdk.ts +2919 -0
  950. package/src/secrets/index.ts +123 -0
  951. package/src/secrets/obfuscator.ts +298 -0
  952. package/src/secrets/regex.ts +21 -0
  953. package/src/session/agent-session.ts +12539 -0
  954. package/src/session/agent-storage.ts +478 -0
  955. package/src/session/artifacts.ts +153 -0
  956. package/src/session/auth-broker-config.ts +92 -0
  957. package/src/session/auth-storage.ts +24 -0
  958. package/src/session/blob-store.ts +255 -0
  959. package/src/session/client-bridge.ts +85 -0
  960. package/src/session/codex-auto-reset.ts +202 -0
  961. package/src/session/compact-modes.ts +105 -0
  962. package/src/session/history-storage.ts +361 -0
  963. package/src/session/indexed-session-storage.ts +427 -0
  964. package/src/session/messages.ts +546 -0
  965. package/src/session/redis-session-storage.ts +170 -0
  966. package/src/session/session-context.ts +399 -0
  967. package/src/session/session-dump-format.ts +216 -0
  968. package/src/session/session-entries.ts +198 -0
  969. package/src/session/session-history-format.ts +308 -0
  970. package/src/session/session-listing.ts +588 -0
  971. package/src/session/session-loader.ts +93 -0
  972. package/src/session/session-manager.ts +1748 -0
  973. package/src/session/session-migrations.ts +78 -0
  974. package/src/session/session-paths.ts +193 -0
  975. package/src/session/session-persistence.ts +147 -0
  976. package/src/session/session-storage.ts +590 -0
  977. package/src/session/shake-types.ts +43 -0
  978. package/src/session/snapcompact-inline.ts +542 -0
  979. package/src/session/snapcompact-savings-journal.ts +113 -0
  980. package/src/session/sql-session-storage.ts +314 -0
  981. package/src/session/streaming-output.ts +1330 -0
  982. package/src/session/tool-choice-queue.ts +290 -0
  983. package/src/session/unexpected-stop-classifier.ts +129 -0
  984. package/src/session/yield-queue.ts +183 -0
  985. package/src/slash-commands/acp-builtins.ts +70 -0
  986. package/src/slash-commands/available-commands.ts +105 -0
  987. package/src/slash-commands/builtin-registry.ts +2332 -0
  988. package/src/slash-commands/helpers/active-oauth-account.ts +44 -0
  989. package/src/slash-commands/helpers/collab-qrcode.ts +28 -0
  990. package/src/slash-commands/helpers/context-report.ts +66 -0
  991. package/src/slash-commands/helpers/format.ts +46 -0
  992. package/src/slash-commands/helpers/logout.ts +88 -0
  993. package/src/slash-commands/helpers/marketplace-manager.ts +25 -0
  994. package/src/slash-commands/helpers/mcp.ts +532 -0
  995. package/src/slash-commands/helpers/parse.ts +85 -0
  996. package/src/slash-commands/helpers/reset-usage.ts +66 -0
  997. package/src/slash-commands/helpers/ssh.ts +195 -0
  998. package/src/slash-commands/helpers/stats-dashboard.ts +85 -0
  999. package/src/slash-commands/helpers/todo.ts +279 -0
  1000. package/src/slash-commands/helpers/usage-report.ts +128 -0
  1001. package/src/slash-commands/marketplace-install-parser.ts +99 -0
  1002. package/src/slash-commands/types.ts +135 -0
  1003. package/src/ssh/config-writer.ts +183 -0
  1004. package/src/ssh/connection-manager.ts +510 -0
  1005. package/src/ssh/ssh-executor.ts +189 -0
  1006. package/src/ssh/sshfs-mount.ts +140 -0
  1007. package/src/ssh/utils.ts +8 -0
  1008. package/src/startup-splash.ts +19 -0
  1009. package/src/stt/asr-client.ts +521 -0
  1010. package/src/stt/asr-protocol.ts +65 -0
  1011. package/src/stt/asr-worker.ts +790 -0
  1012. package/src/stt/downloader.ts +138 -0
  1013. package/src/stt/endpointer.ts +259 -0
  1014. package/src/stt/index.ts +7 -0
  1015. package/src/stt/models.ts +150 -0
  1016. package/src/stt/recorder.ts +538 -0
  1017. package/src/stt/stt-controller.ts +380 -0
  1018. package/src/stt/transcriber.ts +60 -0
  1019. package/src/stt/wav.ts +173 -0
  1020. package/src/system-prompt.ts +709 -0
  1021. package/src/task/agents.ts +166 -0
  1022. package/src/task/commands.ts +132 -0
  1023. package/src/task/discovery.ts +122 -0
  1024. package/src/task/executor.ts +2356 -0
  1025. package/src/task/index.ts +1580 -0
  1026. package/src/task/name-generator.ts +1577 -0
  1027. package/src/task/omp-command.ts +26 -0
  1028. package/src/task/output-manager.ts +93 -0
  1029. package/src/task/parallel.ts +116 -0
  1030. package/src/task/persisted-revive.ts +128 -0
  1031. package/src/task/render.ts +1558 -0
  1032. package/src/task/repair-args.ts +129 -0
  1033. package/src/task/subprocess-tool-registry.ts +88 -0
  1034. package/src/task/types.ts +401 -0
  1035. package/src/task/worktree.ts +514 -0
  1036. package/src/telemetry-export.ts +144 -0
  1037. package/src/thinking.ts +187 -0
  1038. package/src/tiny/device.ts +111 -0
  1039. package/src/tiny/dtype.ts +101 -0
  1040. package/src/tiny/models.ts +252 -0
  1041. package/src/tiny/text.ts +169 -0
  1042. package/src/tiny/title-client.ts +538 -0
  1043. package/src/tiny/title-protocol.ts +56 -0
  1044. package/src/tiny/worker.ts +491 -0
  1045. package/src/tool-discovery/mode.ts +24 -0
  1046. package/src/tool-discovery/tool-index.ts +271 -0
  1047. package/src/tools/__tests__/json-tree.test.ts +35 -0
  1048. package/src/tools/approval.ts +189 -0
  1049. package/src/tools/ask.ts +977 -0
  1050. package/src/tools/ast-edit.ts +700 -0
  1051. package/src/tools/ast-grep.ts +483 -0
  1052. package/src/tools/auto-generated-guard.ts +322 -0
  1053. package/src/tools/bash-command-fixup.ts +37 -0
  1054. package/src/tools/bash-interactive.ts +408 -0
  1055. package/src/tools/bash-interceptor.ts +67 -0
  1056. package/src/tools/bash-pty-selection.ts +14 -0
  1057. package/src/tools/bash-skill-urls.ts +248 -0
  1058. package/src/tools/bash.ts +1405 -0
  1059. package/src/tools/browser/attach.ts +194 -0
  1060. package/src/tools/browser/cmux/cmux-tab.ts +1264 -0
  1061. package/src/tools/browser/cmux/rpc.ts +156 -0
  1062. package/src/tools/browser/cmux/socket-client.ts +309 -0
  1063. package/src/tools/browser/launch.ts +673 -0
  1064. package/src/tools/browser/readable.ts +112 -0
  1065. package/src/tools/browser/registry.ts +241 -0
  1066. package/src/tools/browser/render.ts +221 -0
  1067. package/src/tools/browser/tab-protocol.ts +107 -0
  1068. package/src/tools/browser/tab-supervisor.ts +799 -0
  1069. package/src/tools/browser/tab-worker-entry.ts +29 -0
  1070. package/src/tools/browser/tab-worker.ts +1226 -0
  1071. package/src/tools/browser.ts +403 -0
  1072. package/src/tools/builtin-names.ts +34 -0
  1073. package/src/tools/checkpoint.ts +136 -0
  1074. package/src/tools/conflict-detect.ts +718 -0
  1075. package/src/tools/context.ts +39 -0
  1076. package/src/tools/debug.ts +1087 -0
  1077. package/src/tools/eval-backends.ts +27 -0
  1078. package/src/tools/eval-render.ts +762 -0
  1079. package/src/tools/eval.ts +600 -0
  1080. package/src/tools/fetch.ts +1902 -0
  1081. package/src/tools/file-recorder.ts +35 -0
  1082. package/src/tools/find.ts +629 -0
  1083. package/src/tools/fs-cache-invalidation.ts +28 -0
  1084. package/src/tools/gh-cache-invalidation.ts +255 -0
  1085. package/src/tools/gh-format.ts +12 -0
  1086. package/src/tools/gh-renderer.ts +481 -0
  1087. package/src/tools/gh.ts +3752 -0
  1088. package/src/tools/github-cache.ts +663 -0
  1089. package/src/tools/grouped-file-output.ts +210 -0
  1090. package/src/tools/image-gen.ts +1586 -0
  1091. package/src/tools/index.ts +649 -0
  1092. package/src/tools/inspect-image-renderer.ts +132 -0
  1093. package/src/tools/inspect-image.ts +260 -0
  1094. package/src/tools/irc.ts +788 -0
  1095. package/src/tools/job.ts +612 -0
  1096. package/src/tools/json-tree.ts +260 -0
  1097. package/src/tools/jtd-to-json-schema.ts +219 -0
  1098. package/src/tools/jtd-to-typescript.ts +136 -0
  1099. package/src/tools/jtd-utils.ts +102 -0
  1100. package/src/tools/learn.ts +141 -0
  1101. package/src/tools/list-limit.ts +40 -0
  1102. package/src/tools/manage-skill.ts +100 -0
  1103. package/src/tools/match-line-format.ts +20 -0
  1104. package/src/tools/memory-edit.ts +59 -0
  1105. package/src/tools/memory-recall.ts +102 -0
  1106. package/src/tools/memory-reflect.ts +88 -0
  1107. package/src/tools/memory-render.ts +202 -0
  1108. package/src/tools/memory-retain.ts +89 -0
  1109. package/src/tools/output-meta.ts +768 -0
  1110. package/src/tools/output-schema-validator.ts +132 -0
  1111. package/src/tools/path-utils.ts +1116 -0
  1112. package/src/tools/plan-mode-guard.ts +142 -0
  1113. package/src/tools/puppeteer/00_stealth_tampering.txt +63 -0
  1114. package/src/tools/puppeteer/01_stealth_activity.txt +20 -0
  1115. package/src/tools/puppeteer/02_stealth_hairline.txt +11 -0
  1116. package/src/tools/puppeteer/03_stealth_botd.txt +384 -0
  1117. package/src/tools/puppeteer/04_stealth_iframe.txt +81 -0
  1118. package/src/tools/puppeteer/05_stealth_webgl.txt +75 -0
  1119. package/src/tools/puppeteer/06_stealth_screen.txt +72 -0
  1120. package/src/tools/puppeteer/07_stealth_fonts.txt +97 -0
  1121. package/src/tools/puppeteer/08_stealth_audio.txt +51 -0
  1122. package/src/tools/puppeteer/09_stealth_locale.txt +46 -0
  1123. package/src/tools/puppeteer/10_stealth_plugins.txt +208 -0
  1124. package/src/tools/puppeteer/11_stealth_hardware.txt +8 -0
  1125. package/src/tools/puppeteer/12_stealth_codecs.txt +40 -0
  1126. package/src/tools/puppeteer/13_stealth_worker.txt +74 -0
  1127. package/src/tools/read.ts +3124 -0
  1128. package/src/tools/render-utils.ts +895 -0
  1129. package/src/tools/renderers.ts +86 -0
  1130. package/src/tools/report-tool-issue.ts +530 -0
  1131. package/src/tools/resolve.ts +302 -0
  1132. package/src/tools/review.ts +251 -0
  1133. package/src/tools/search-tool-bm25.ts +351 -0
  1134. package/src/tools/search.ts +1583 -0
  1135. package/src/tools/sqlite-reader.ts +828 -0
  1136. package/src/tools/ssh.ts +369 -0
  1137. package/src/tools/todo.ts +938 -0
  1138. package/src/tools/tool-errors.ts +62 -0
  1139. package/src/tools/tool-result.ts +102 -0
  1140. package/src/tools/tool-timeouts.ts +30 -0
  1141. package/src/tools/tts.ts +265 -0
  1142. package/src/tools/write.ts +1182 -0
  1143. package/src/tools/yield.ts +269 -0
  1144. package/src/tts/downloader.ts +64 -0
  1145. package/src/tts/index.ts +8 -0
  1146. package/src/tts/models.ts +137 -0
  1147. package/src/tts/player.ts +137 -0
  1148. package/src/tts/runtime.ts +21 -0
  1149. package/src/tts/streaming-player.ts +266 -0
  1150. package/src/tts/tts-client.ts +642 -0
  1151. package/src/tts/tts-protocol.ts +60 -0
  1152. package/src/tts/tts-worker.ts +505 -0
  1153. package/src/tts/vocalizer.ts +162 -0
  1154. package/src/tts/wav.ts +58 -0
  1155. package/src/tui/code-cell.ts +257 -0
  1156. package/src/tui/file-list.ts +55 -0
  1157. package/src/tui/hyperlink.ts +178 -0
  1158. package/src/tui/index.ts +13 -0
  1159. package/src/tui/output-block.ts +240 -0
  1160. package/src/tui/status-line.ts +54 -0
  1161. package/src/tui/tree-list.ts +133 -0
  1162. package/src/tui/types.ts +15 -0
  1163. package/src/tui/utils.ts +103 -0
  1164. package/src/tui/width-aware-text.ts +58 -0
  1165. package/src/utils/block-context.ts +312 -0
  1166. package/src/utils/changelog.ts +132 -0
  1167. package/src/utils/clipboard.ts +262 -0
  1168. package/src/utils/command-args.ts +76 -0
  1169. package/src/utils/commit-message-generator.ts +147 -0
  1170. package/src/utils/edit-mode.ts +41 -0
  1171. package/src/utils/enhanced-paste.ts +230 -0
  1172. package/src/utils/event-bus.ts +33 -0
  1173. package/src/utils/external-editor.ts +78 -0
  1174. package/src/utils/file-display-mode.ts +45 -0
  1175. package/src/utils/file-mentions.ts +284 -0
  1176. package/src/utils/git.ts +1838 -0
  1177. package/src/utils/image-loading.ts +231 -0
  1178. package/src/utils/image-resize.ts +309 -0
  1179. package/src/utils/image-vision-fallback.ts +197 -0
  1180. package/src/utils/ipc.ts +38 -0
  1181. package/src/utils/jj.ts +248 -0
  1182. package/src/utils/lang-from-path.ts +244 -0
  1183. package/src/utils/markit.ts +143 -0
  1184. package/src/utils/mupdf-wasm-embed.ts +12 -0
  1185. package/src/utils/open.ts +55 -0
  1186. package/src/utils/qrcode.ts +535 -0
  1187. package/src/utils/session-color.ts +142 -0
  1188. package/src/utils/shell-snapshot.ts +187 -0
  1189. package/src/utils/sixel.ts +69 -0
  1190. package/src/utils/thinking-display.ts +11 -0
  1191. package/src/utils/title-generator.ts +416 -0
  1192. package/src/utils/tool-choice.ts +49 -0
  1193. package/src/utils/tools-manager.ts +372 -0
  1194. package/src/utils/turndown.ts +83 -0
  1195. package/src/utils/zip.ts +1091 -0
  1196. package/src/web/kagi.ts +304 -0
  1197. package/src/web/parallel.ts +353 -0
  1198. package/src/web/scrapers/artifacthub.ts +207 -0
  1199. package/src/web/scrapers/arxiv.ts +83 -0
  1200. package/src/web/scrapers/aur.ts +162 -0
  1201. package/src/web/scrapers/biorxiv.ts +133 -0
  1202. package/src/web/scrapers/bluesky.ts +262 -0
  1203. package/src/web/scrapers/brew.ts +172 -0
  1204. package/src/web/scrapers/cheatsh.ts +68 -0
  1205. package/src/web/scrapers/chocolatey.ts +196 -0
  1206. package/src/web/scrapers/choosealicense.ts +95 -0
  1207. package/src/web/scrapers/cisa-kev.ts +87 -0
  1208. package/src/web/scrapers/clojars.ts +154 -0
  1209. package/src/web/scrapers/coingecko.ts +177 -0
  1210. package/src/web/scrapers/crates-io.ts +97 -0
  1211. package/src/web/scrapers/crossref.ts +136 -0
  1212. package/src/web/scrapers/devto.ts +147 -0
  1213. package/src/web/scrapers/discogs.ts +306 -0
  1214. package/src/web/scrapers/discourse.ts +197 -0
  1215. package/src/web/scrapers/dockerhub.ts +138 -0
  1216. package/src/web/scrapers/docs-rs.ts +652 -0
  1217. package/src/web/scrapers/fdroid.ts +134 -0
  1218. package/src/web/scrapers/firefox-addons.ts +191 -0
  1219. package/src/web/scrapers/flathub.ts +223 -0
  1220. package/src/web/scrapers/github-gist.ts +58 -0
  1221. package/src/web/scrapers/github.ts +800 -0
  1222. package/src/web/scrapers/gitlab.ts +401 -0
  1223. package/src/web/scrapers/go-pkg.ts +266 -0
  1224. package/src/web/scrapers/hackage.ts +140 -0
  1225. package/src/web/scrapers/hackernews.ts +189 -0
  1226. package/src/web/scrapers/hex.ts +105 -0
  1227. package/src/web/scrapers/huggingface.ts +321 -0
  1228. package/src/web/scrapers/iacr.ts +89 -0
  1229. package/src/web/scrapers/index.ts +252 -0
  1230. package/src/web/scrapers/jetbrains-marketplace.ts +159 -0
  1231. package/src/web/scrapers/lemmy.ts +203 -0
  1232. package/src/web/scrapers/lobsters.ts +175 -0
  1233. package/src/web/scrapers/mastodon.ts +292 -0
  1234. package/src/web/scrapers/maven.ts +138 -0
  1235. package/src/web/scrapers/mdn.ts +173 -0
  1236. package/src/web/scrapers/metacpan.ts +222 -0
  1237. package/src/web/scrapers/musicbrainz.ts +250 -0
  1238. package/src/web/scrapers/npm.ts +98 -0
  1239. package/src/web/scrapers/nuget.ts +183 -0
  1240. package/src/web/scrapers/nvd.ts +222 -0
  1241. package/src/web/scrapers/ollama.ts +239 -0
  1242. package/src/web/scrapers/open-vsx.ts +106 -0
  1243. package/src/web/scrapers/opencorporates.ts +292 -0
  1244. package/src/web/scrapers/openlibrary.ts +336 -0
  1245. package/src/web/scrapers/orcid.ts +286 -0
  1246. package/src/web/scrapers/osv.ts +176 -0
  1247. package/src/web/scrapers/packagist.ts +160 -0
  1248. package/src/web/scrapers/pub-dev.ts +143 -0
  1249. package/src/web/scrapers/pubmed.ts +211 -0
  1250. package/src/web/scrapers/pypi.ts +112 -0
  1251. package/src/web/scrapers/rawg.ts +110 -0
  1252. package/src/web/scrapers/readthedocs.ts +120 -0
  1253. package/src/web/scrapers/reddit.ts +95 -0
  1254. package/src/web/scrapers/repology.ts +251 -0
  1255. package/src/web/scrapers/rfc.ts +201 -0
  1256. package/src/web/scrapers/rubygems.ts +103 -0
  1257. package/src/web/scrapers/searchcode.ts +189 -0
  1258. package/src/web/scrapers/sec-edgar.ts +261 -0
  1259. package/src/web/scrapers/semantic-scholar.ts +171 -0
  1260. package/src/web/scrapers/snapcraft.ts +187 -0
  1261. package/src/web/scrapers/sourcegraph.ts +336 -0
  1262. package/src/web/scrapers/spdx.ts +108 -0
  1263. package/src/web/scrapers/spotify.ts +198 -0
  1264. package/src/web/scrapers/stackoverflow.ts +120 -0
  1265. package/src/web/scrapers/terraform.ts +277 -0
  1266. package/src/web/scrapers/tldr.ts +47 -0
  1267. package/src/web/scrapers/twitter.ts +94 -0
  1268. package/src/web/scrapers/types.ts +354 -0
  1269. package/src/web/scrapers/utils.ts +109 -0
  1270. package/src/web/scrapers/vimeo.ts +133 -0
  1271. package/src/web/scrapers/vscode-marketplace.ts +187 -0
  1272. package/src/web/scrapers/w3c.ts +156 -0
  1273. package/src/web/scrapers/wikidata.ts +344 -0
  1274. package/src/web/scrapers/wikipedia.ts +84 -0
  1275. package/src/web/scrapers/youtube.ts +325 -0
  1276. package/src/web/search/index.ts +317 -0
  1277. package/src/web/search/provider.ts +169 -0
  1278. package/src/web/search/providers/anthropic.ts +343 -0
  1279. package/src/web/search/providers/base.ts +90 -0
  1280. package/src/web/search/providers/brave.ts +152 -0
  1281. package/src/web/search/providers/codex.ts +593 -0
  1282. package/src/web/search/providers/exa.ts +400 -0
  1283. package/src/web/search/providers/gemini.ts +518 -0
  1284. package/src/web/search/providers/jina.ts +111 -0
  1285. package/src/web/search/providers/kagi.ts +86 -0
  1286. package/src/web/search/providers/kimi.ts +196 -0
  1287. package/src/web/search/providers/parallel.ts +225 -0
  1288. package/src/web/search/providers/perplexity-auth.ts +133 -0
  1289. package/src/web/search/providers/perplexity.ts +866 -0
  1290. package/src/web/search/providers/searxng.ts +325 -0
  1291. package/src/web/search/providers/synthetic.ts +114 -0
  1292. package/src/web/search/providers/tavily.ts +176 -0
  1293. package/src/web/search/providers/utils.ts +128 -0
  1294. package/src/web/search/providers/zai.ts +333 -0
  1295. package/src/web/search/render.ts +262 -0
  1296. package/src/web/search/types.ts +462 -0
  1297. package/src/web/search/utils.ts +17 -0
  1298. package/src/workspace-tree.ts +326 -0
@@ -0,0 +1,3124 @@
1
+ import { Database } from "bun:sqlite";
2
+ import * as fs from "node:fs/promises";
3
+ import * as os from "node:os";
4
+ import * as path from "node:path";
5
+ import { formatHashlineHeader, formatNumberedLine, formatNumberedLines } from "@oh-my-pi/hashline";
6
+ import type { AgentTool, AgentToolContext, AgentToolResult, AgentToolUpdateCallback } from "@oh-my-pi/pi-agent-core";
7
+ import type { ImageContent, TextContent } from "@oh-my-pi/pi-ai";
8
+ import { glob, type SummaryResult, summarizeCode } from "@oh-my-pi/pi-natives";
9
+ import type { Component } from "@oh-my-pi/pi-tui";
10
+ import { Text } from "@oh-my-pi/pi-tui";
11
+ import { getRemoteDir, logger, prompt, readImageMetadata, untilAborted } from "@oh-my-pi/pi-utils";
12
+ import { type } from "arktype";
13
+ import { LRUCache } from "lru-cache/raw";
14
+ import {
15
+ canonicalSnapshotKey,
16
+ getFileSnapshotStore,
17
+ recordFileSnapshot,
18
+ recordSeenLines,
19
+ recordSeenLinesFromBody,
20
+ SNAPSHOT_MAX_BYTES,
21
+ } from "../edit/file-snapshot-store";
22
+ import { normalizeToLF } from "../edit/normalize";
23
+ import { isNotebookPath, readEditableNotebookText } from "../edit/notebook";
24
+ import type { RenderResultOptions } from "../extensibility/custom-tools/types";
25
+ import { InternalUrlRouter } from "../internal-urls";
26
+ import { parseInternalUrl } from "../internal-urls/parse";
27
+ import type { InternalUrl } from "../internal-urls/types";
28
+ import { getLanguageFromPath, type Theme } from "../modes/theme/theme";
29
+ import readDescription from "../prompts/tools/read.md" with { type: "text" };
30
+ import type { ToolSession } from "../sdk";
31
+ import {
32
+ DEFAULT_MAX_BYTES,
33
+ DEFAULT_MAX_LINES,
34
+ noTruncResult,
35
+ type TruncationResult,
36
+ truncateHead,
37
+ truncateHeadBytes,
38
+ truncateLine,
39
+ } from "../session/streaming-output";
40
+ import { fileHyperlink, renderCodeCell, renderMarkdownCell, renderStatusLine, tryResolveInternalUrlSync } from "../tui";
41
+ import { CachedOutputBlock, markFramedBlockComponent } from "../tui/output-block";
42
+ import { buildLineEntriesWithBlockContext, type LineEntry, lineEntriesToPlainText } from "../utils/block-context";
43
+ import { resolveFileDisplayMode } from "../utils/file-display-mode";
44
+ import {
45
+ ImageInputTooLargeError,
46
+ loadImageInput,
47
+ MAX_IMAGE_INPUT_BYTES,
48
+ webpExclusionForModel,
49
+ } from "../utils/image-loading";
50
+ import { convertFileWithMarkit } from "../utils/markit";
51
+ import { type ArchiveReader, formatArchiveEntryLines, openArchive, parseArchivePathCandidates } from "../utils/zip";
52
+ import { buildDirectoryTree, type DirectoryTree } from "../workspace-tree";
53
+ import {
54
+ type ConflictEntry,
55
+ type ConflictScope,
56
+ formatConflictSummary,
57
+ formatConflictWarning,
58
+ getConflictHistory,
59
+ parseConflictUri,
60
+ renderConflictRegion,
61
+ scanConflictLines,
62
+ scanFileForConflicts,
63
+ } from "./conflict-detect";
64
+ import {
65
+ executeReadUrl,
66
+ isReadableUrlPath,
67
+ loadReadUrlCacheEntry,
68
+ parseReadUrlTarget,
69
+ type ReadUrlToolDetails,
70
+ renderReadUrlCall,
71
+ renderReadUrlResult,
72
+ } from "./fetch";
73
+ import { applyListLimit } from "./list-limit";
74
+ import {
75
+ formatFullOutputReference,
76
+ formatStyledTruncationWarning,
77
+ type OutputMeta,
78
+ resolveOutputMaxColumns,
79
+ stripOutputNotice,
80
+ } from "./output-meta";
81
+ import {
82
+ expandPath,
83
+ formatPathRelativeToCwd,
84
+ type LineRange,
85
+ parseLineRanges,
86
+ resolveReadPath,
87
+ splitDelimitedPathEntry,
88
+ splitInternalUrlSel,
89
+ splitPathAndSel,
90
+ } from "./path-utils";
91
+ import { formatBytes, replaceTabs, shortenPath, wrapBrackets } from "./render-utils";
92
+ import {
93
+ executeReadQuery,
94
+ getRowByKey,
95
+ getRowByRowId,
96
+ getTableSchema,
97
+ isSqliteFile,
98
+ listTables,
99
+ MAX_RAW_QUERY_ROWS,
100
+ parseSqlitePathCandidates,
101
+ parseSqliteSelector,
102
+ queryRows,
103
+ renderRow,
104
+ renderSchema,
105
+ renderTable,
106
+ renderTableList,
107
+ resolveTableRowLookup,
108
+ } from "./sqlite-reader";
109
+ import { ToolAbortError, ToolError, throwIfAborted } from "./tool-errors";
110
+ import { toolResult } from "./tool-result";
111
+
112
+ // Per-session memo for tree-sitter summaries. `summarizeCode` is a pure function
113
+ // of (code, path, fold settings) but costs ~12-18ms for a ~1500-line file, and a
114
+ // repeat summary read of the same unchanged file re-parses from scratch. Key on
115
+ // the content hash of the freshly-read bytes (+ path + fold settings): the file
116
+ // is still read fresh on every call, so a hit only reuses the deterministic
117
+ // parse — there is no staleness window and no stat guard is needed. Bounded LRU,
118
+ // aged out with the session via WeakMap.
119
+ // Unusable results (not parsed, or nothing elided) are memoized as `false`: the
120
+ // full SummaryResult embeds the whole source in kept segments, and the caller
121
+ // only ever renders `parsed && elided` summaries — caching the segments would
122
+ // retain up to 48 near-2MiB sources just to remember "no summary".
123
+ const SUMMARY_CACHE_MAX = 48;
124
+ const summaryParseCaches = new WeakMap<object, LRUCache<string, SummaryResult | false>>();
125
+ function getSummaryParseCache(session: object): LRUCache<string, SummaryResult | false> {
126
+ let cache = summaryParseCaches.get(session);
127
+ if (!cache) {
128
+ cache = new LRUCache<string, SummaryResult | false>({ max: SUMMARY_CACHE_MAX });
129
+ summaryParseCaches.set(session, cache);
130
+ }
131
+ return cache;
132
+ }
133
+
134
+ // Document types converted to markdown via markit.
135
+ const CONVERTIBLE_EXTENSIONS = new Set([".pdf", ".doc", ".docx", ".ppt", ".pptx", ".xls", ".xlsx", ".rtf", ".epub"]);
136
+
137
+ const MAX_SUMMARY_BYTES = 2 * 1024 * 1024;
138
+ const MAX_SUMMARY_LINES = 20_000;
139
+ /**
140
+ * Per-line column cap for file reads. Lines wider than the value of
141
+ * `tools.outputMaxColumns` are ellipsis-truncated at display time; the file
142
+ * on disk is unchanged. Shared with the streaming sink path so one setting
143
+ * covers `bash`/`ssh`/`python`/`js eval` and `read` uniformly.
144
+ */
145
+ const PROSE_SUMMARY_EXTENSIONS = new Set([".md", ".txt"]);
146
+ // Remote mount path prefix (sshfs mounts) - skip fuzzy matching to avoid hangs
147
+ const REMOTE_MOUNT_PREFIX = getRemoteDir() + path.sep;
148
+
149
+ async function readBracketContextFullLines(absolutePath: string, fileSize: number): Promise<string[] | undefined> {
150
+ if (fileSize > SNAPSHOT_MAX_BYTES) return undefined;
151
+ try {
152
+ return normalizeToLF(await Bun.file(absolutePath).text()).split("\n");
153
+ } catch {
154
+ return undefined;
155
+ }
156
+ }
157
+
158
+ function isRemoteMountPath(absolutePath: string): boolean {
159
+ return absolutePath.startsWith(REMOTE_MOUNT_PREFIX);
160
+ }
161
+
162
+ function prependLineNumbers(text: string, startNum: number): string {
163
+ const textLines = text.split("\n");
164
+ return textLines.map((line, i) => `${startNum + i}|${line}`).join("\n");
165
+ }
166
+
167
+ interface HashlineHeaderContext {
168
+ header: string;
169
+ tag: string;
170
+ fullText?: string;
171
+ }
172
+
173
+ function recordFullHashlineContext(
174
+ session: ToolSession,
175
+ absolutePath: string | undefined,
176
+ displayPath: string,
177
+ fullText: string,
178
+ ): HashlineHeaderContext | undefined {
179
+ if (!absolutePath || !path.isAbsolute(absolutePath)) return undefined;
180
+ const normalized = normalizeToLF(fullText);
181
+ const tag = getFileSnapshotStore(session).record(canonicalSnapshotKey(absolutePath), normalized);
182
+ return {
183
+ header: formatHashlineHeader(displayPath, tag),
184
+ tag,
185
+ fullText: normalized,
186
+ };
187
+ }
188
+
189
+ async function readHashlineHeaderContext(
190
+ session: ToolSession,
191
+ absolutePath: string,
192
+ cwd: string,
193
+ ): Promise<HashlineHeaderContext> {
194
+ const fullText = await Bun.file(absolutePath).text();
195
+ const context = recordFullHashlineContext(
196
+ session,
197
+ absolutePath,
198
+ formatPathRelativeToCwd(absolutePath, cwd),
199
+ fullText,
200
+ );
201
+ if (!context) throw new ToolError(`Cannot record hashline snapshot for non-absolute path: ${absolutePath}`);
202
+ return context;
203
+ }
204
+
205
+ function hashlineHeaderContext(displayPath: string, tag: string): HashlineHeaderContext {
206
+ return { header: formatHashlineHeader(displayPath, tag), tag };
207
+ }
208
+
209
+ function prependHashlineHeader(text: string, context: HashlineHeaderContext | undefined): string {
210
+ return context ? `${context.header}\n${text}` : text;
211
+ }
212
+
213
+ function formatTextWithMode(
214
+ text: string,
215
+ startNum: number,
216
+ shouldAddHashLines: boolean,
217
+ shouldAddLineNumbers: boolean,
218
+ ): string {
219
+ if (shouldAddHashLines) return formatNumberedLines(text, startNum);
220
+ if (shouldAddLineNumbers) return prependLineNumbers(text, startNum);
221
+ return text;
222
+ }
223
+
224
+ const BRACKET_CONTEXT_ELLIPSIS = "…";
225
+
226
+ function formatLineEntryWithMode(entry: LineEntry, shouldAddHashLines: boolean, shouldAddLineNumbers: boolean): string {
227
+ if (entry.kind === "ellipsis") return BRACKET_CONTEXT_ELLIPSIS;
228
+ return formatSingleLine(entry.lineNumber, entry.text, shouldAddHashLines, shouldAddLineNumbers);
229
+ }
230
+
231
+ function formatLineEntriesWithMode(
232
+ entries: readonly LineEntry[],
233
+ shouldAddHashLines: boolean,
234
+ shouldAddLineNumbers: boolean,
235
+ ): string {
236
+ return entries.map(entry => formatLineEntryWithMode(entry, shouldAddHashLines, shouldAddLineNumbers)).join("\n");
237
+ }
238
+
239
+ const BRACE_PAIRS: Record<string, string> = { "{": "}", "(": ")", "[": "]" };
240
+ const BRACE_TAIL_TRAILING_RE = /^[;,)\]}]*$/;
241
+
242
+ /**
243
+ * Decide whether the kept lines surrounding an elided range collapse to a
244
+ * single brace-pair line in the rendered summary. Returns true when the head
245
+ * line ends with `{` / `(` / `[` and the tail line is the matching closer
246
+ * (optionally followed by terminating punctuation like `;`, `,`, or further
247
+ * closers — e.g. `};`, `})`, `]);`).
248
+ */
249
+ function canMergeBracePair(headLine: string, tailLine: string): boolean {
250
+ const head = headLine.trimEnd();
251
+ const tail = tailLine.trim();
252
+ const opener = head.slice(-1);
253
+ const closer = BRACE_PAIRS[opener];
254
+ if (!closer) return false;
255
+ if (!tail.startsWith(closer)) return false;
256
+ return BRACE_TAIL_TRAILING_RE.test(tail.slice(closer.length));
257
+ }
258
+
259
+ function formatSingleLine(
260
+ line: number,
261
+ text: string,
262
+ shouldAddHashLines: boolean,
263
+ shouldAddLineNumbers: boolean,
264
+ ): string {
265
+ if (shouldAddHashLines) return formatNumberedLine(line, text);
266
+ if (shouldAddLineNumbers) return `${line}|${text}`;
267
+ return text;
268
+ }
269
+
270
+ function formatMergedBraceLine(
271
+ startLine: number,
272
+ endLine: number,
273
+ headText: string,
274
+ tailText: string,
275
+ shouldAddHashLines: boolean,
276
+ shouldAddLineNumbers: boolean,
277
+ ): { model: string; display: string } {
278
+ const merged = `${headText.trimEnd()} … ${tailText.trim()}`;
279
+ if (shouldAddHashLines) {
280
+ return { model: `${startLine}-${endLine}:${merged}`, display: merged };
281
+ }
282
+ if (shouldAddLineNumbers) {
283
+ return { model: `${startLine}-${endLine}|${merged}`, display: merged };
284
+ }
285
+ return { model: merged, display: merged };
286
+ }
287
+
288
+ function countTextLines(text: string): number {
289
+ if (text.length === 0) return 0;
290
+ return text.split("\n").length;
291
+ }
292
+
293
+ function contiguousLineNumbers(startLine: number, count: number): number[] {
294
+ const lines: number[] = [];
295
+ for (let offset = 0; offset < count; offset++) lines.push(startLine + offset);
296
+ return lines;
297
+ }
298
+
299
+ function lineNumbersFromEntries(entries: readonly LineEntry[]): number[] {
300
+ const lines: number[] = [];
301
+ for (const entry of entries) {
302
+ if (entry.kind === "line") lines.push(entry.lineNumber);
303
+ }
304
+ return lines;
305
+ }
306
+
307
+ /** Inclusive line range describing one elided span in a structural summary. */
308
+ interface ElidedRange {
309
+ start: number;
310
+ end: number;
311
+ }
312
+
313
+ /** Sample ranges shown in the footer to demonstrate the multi-range syntax. */
314
+ const FOOTER_RANGE_SAMPLES = 2;
315
+
316
+ /**
317
+ * Footer appended to summarized reads telling the model how to recover the
318
+ * elided body. Without this hint, agents either ignore the `…`/`{ … }`
319
+ * markers or burn a turn guessing the right selector (see issue #1046). The
320
+ * footer demonstrates the multi-range selector syntax with concrete sample
321
+ * ranges drawn from the actual elision so the model re-reads only what it
322
+ * needs instead of falling back to `:raw` or whole-file reads.
323
+ */
324
+ function formatSummaryElisionFooter(
325
+ readPath: string,
326
+ elidedRanges: ReadonlyArray<ElidedRange>,
327
+ elidedLines: number,
328
+ ): string {
329
+ if (elidedRanges.length === 0) return "";
330
+ const sampleCount = Math.min(elidedRanges.length, FOOTER_RANGE_SAMPLES);
331
+ const selector = elidedRanges
332
+ .slice(0, sampleCount)
333
+ .map(r => `${r.start}-${r.end}`)
334
+ .join(",");
335
+ const example = `${readPath}:${selector}`;
336
+ const tail = elidedRanges.length > sampleCount ? `, e.g. ${example}` : ` with ${example}`;
337
+ return `[…${elidedLines}ln elided; re-read needed ranges${tail}]`;
338
+ }
339
+ const READ_CHUNK_SIZE = 8 * 1024;
340
+
341
+ /**
342
+ * Context lines added around an explicit range read. Anchor-stale failures
343
+ * cluster on edits whose anchors land just outside the most recent read
344
+ * window, but the data (`scripts/session-stats/analyze_selector_reads.py`)
345
+ * shows most follow-up reads are disjoint hops, not adjacent extensions —
346
+ * so symmetric padding rarely pays for itself.
347
+ *
348
+ * Leading=1 catches accidental single-line reads where the anchor is the
349
+ * line immediately above the requested start. Trailing=3 buffers the
350
+ * common case where the agent asks for a narrow range and then needs the
351
+ * next few lines to disambiguate an anchor.
352
+ */
353
+ const RANGE_LEADING_CONTEXT_LINES = 1;
354
+ const RANGE_TRAILING_CONTEXT_LINES = 3;
355
+
356
+ /**
357
+ * Expand a [start, end) range with leading/trailing context lines on the
358
+ * sides where the user actually constrained the range. A start of 0 (no
359
+ * explicit offset) does not get leading context — that's already an
360
+ * open-ended read from the top.
361
+ */
362
+ function expandRangeWithContext(
363
+ requestedStart: number,
364
+ requestedEnd: number,
365
+ totalLines: number,
366
+ expandStart: boolean,
367
+ expandEnd: boolean,
368
+ ): { startLine: number; endLine: number } {
369
+ return {
370
+ startLine: expandStart ? Math.max(0, requestedStart - RANGE_LEADING_CONTEXT_LINES) : requestedStart,
371
+ endLine: expandEnd ? Math.min(totalLines, requestedEnd + RANGE_TRAILING_CONTEXT_LINES) : requestedEnd,
372
+ };
373
+ }
374
+
375
+ async function streamLinesFromFile(
376
+ filePath: string,
377
+ startLine: number,
378
+ maxLinesToCollect: number,
379
+ maxBytes: number,
380
+ selectedLineLimit: number | null,
381
+ signal?: AbortSignal,
382
+ stopScanAfterCollect = false,
383
+ ): Promise<{
384
+ lines: string[];
385
+ totalFileLines: number;
386
+ collectedBytes: number;
387
+ stoppedByByteLimit: boolean;
388
+ firstLinePreview?: { text: string; bytes: number };
389
+ firstLineByteLength?: number;
390
+ selectedBytesTotal: number;
391
+ /** False when `stopScanAfterCollect` cut the scan short — `totalFileLines` is then a lower bound. */
392
+ reachedEof: boolean;
393
+ }> {
394
+ const bufferChunk = Buffer.allocUnsafe(READ_CHUNK_SIZE);
395
+ const collectedLines: string[] = [];
396
+ let lineIndex = 0;
397
+ let collectedBytes = 0;
398
+ let stoppedByByteLimit = false;
399
+ let doneCollecting = false;
400
+ let reachedEof = true;
401
+ let fileHandle: fs.FileHandle | null = null;
402
+ let currentLineLength = 0;
403
+ let currentLineChunks: Buffer[] = [];
404
+ let sawAnyByte = false;
405
+ let endedWithNewline = false;
406
+ let firstLinePreviewBytes = 0;
407
+ const firstLinePreviewChunks: Buffer[] = [];
408
+ let firstLineByteLength: number | undefined;
409
+ let selectedBytesTotal = 0;
410
+ let selectedLinesSeen = 0;
411
+ let captureLine = false;
412
+ let discardLineChunks = false;
413
+ let lineCaptureLimit = 0;
414
+
415
+ const setupLineState = () => {
416
+ captureLine = !doneCollecting && lineIndex >= startLine;
417
+ discardLineChunks = !captureLine;
418
+ if (captureLine) {
419
+ const separatorBytes = collectedLines.length > 0 ? 1 : 0;
420
+ lineCaptureLimit = maxBytes - collectedBytes - separatorBytes;
421
+ if (lineCaptureLimit <= 0) {
422
+ discardLineChunks = true;
423
+ }
424
+ } else {
425
+ lineCaptureLimit = 0;
426
+ }
427
+ };
428
+
429
+ const decodeLine = (): string => {
430
+ if (currentLineLength === 0) return "";
431
+ if (currentLineChunks.length === 1 && currentLineChunks[0]?.length === currentLineLength) {
432
+ return currentLineChunks[0].toString("utf-8");
433
+ }
434
+ return Buffer.concat(currentLineChunks, currentLineLength).toString("utf-8");
435
+ };
436
+
437
+ const maybeCapturePreview = (segment: Uint8Array) => {
438
+ if (doneCollecting || lineIndex < startLine || collectedLines.length !== 0) return;
439
+ if (firstLinePreviewBytes >= maxBytes || segment.length === 0) return;
440
+ const remaining = maxBytes - firstLinePreviewBytes;
441
+ const slice = segment.length > remaining ? segment.subarray(0, remaining) : segment;
442
+ if (slice.length === 0) return;
443
+ firstLinePreviewChunks.push(Buffer.from(slice));
444
+ firstLinePreviewBytes += slice.length;
445
+ };
446
+
447
+ const appendSegment = (segment: Uint8Array) => {
448
+ currentLineLength += segment.length;
449
+ maybeCapturePreview(segment);
450
+ if (!captureLine || discardLineChunks || segment.length === 0) return;
451
+ if (currentLineLength <= lineCaptureLimit) {
452
+ currentLineChunks.push(Buffer.from(segment));
453
+ } else {
454
+ discardLineChunks = true;
455
+ }
456
+ };
457
+
458
+ const finalizeLine = () => {
459
+ if (lineIndex >= startLine && (selectedLineLimit === null || selectedLinesSeen < selectedLineLimit)) {
460
+ selectedBytesTotal += currentLineLength + (selectedLinesSeen > 0 ? 1 : 0);
461
+ selectedLinesSeen++;
462
+ }
463
+
464
+ if (!doneCollecting && lineIndex >= startLine) {
465
+ const separatorBytes = collectedLines.length > 0 ? 1 : 0;
466
+ if (collectedLines.length >= maxLinesToCollect) {
467
+ doneCollecting = true;
468
+ } else if (collectedLines.length === 0 && currentLineLength > maxBytes) {
469
+ stoppedByByteLimit = true;
470
+ doneCollecting = true;
471
+ if (firstLineByteLength === undefined) {
472
+ firstLineByteLength = currentLineLength;
473
+ }
474
+ } else if (collectedLines.length > 0 && collectedBytes + separatorBytes + currentLineLength > maxBytes) {
475
+ stoppedByByteLimit = true;
476
+ doneCollecting = true;
477
+ } else {
478
+ const lineText = decodeLine();
479
+ collectedLines.push(lineText);
480
+ collectedBytes += separatorBytes + currentLineLength;
481
+ if (firstLineByteLength === undefined) {
482
+ firstLineByteLength = currentLineLength;
483
+ }
484
+ if (collectedBytes > maxBytes) {
485
+ stoppedByByteLimit = true;
486
+ doneCollecting = true;
487
+ } else if (collectedLines.length >= maxLinesToCollect) {
488
+ doneCollecting = true;
489
+ }
490
+ }
491
+ } else if (lineIndex >= startLine && firstLineByteLength === undefined) {
492
+ firstLineByteLength = currentLineLength;
493
+ }
494
+
495
+ lineIndex++;
496
+ currentLineLength = 0;
497
+ currentLineChunks = [];
498
+ setupLineState();
499
+ };
500
+
501
+ setupLineState();
502
+
503
+ try {
504
+ fileHandle = await fs.open(filePath, "r");
505
+
506
+ while (true) {
507
+ throwIfAborted(signal);
508
+ const { bytesRead } = await fileHandle.read(bufferChunk, 0, bufferChunk.length, null);
509
+ if (bytesRead === 0) break;
510
+
511
+ sawAnyByte = true;
512
+ const chunk = bufferChunk.subarray(0, bytesRead);
513
+ endedWithNewline = chunk[bytesRead - 1] === 0x0a;
514
+
515
+ // Once collection and selected-line accounting are both finished, the
516
+ // remaining scan only computes `totalFileLines` — count newlines with
517
+ // native indexOf instead of the per-byte JS loop (a multi-GB tail
518
+ // otherwise stalls the read for seconds to minutes).
519
+ if (doneCollecting && selectedLineLimit !== null && selectedLinesSeen >= selectedLineLimit) {
520
+ if (stopScanAfterCollect) {
521
+ reachedEof = false;
522
+ break;
523
+ }
524
+ let searchFrom = 0;
525
+ let newlineAt = chunk.indexOf(0x0a);
526
+ while (newlineAt !== -1) {
527
+ lineIndex++;
528
+ searchFrom = newlineAt + 1;
529
+ newlineAt = chunk.indexOf(0x0a, searchFrom);
530
+ }
531
+ if (searchFrom === 0) {
532
+ currentLineLength += chunk.length;
533
+ } else {
534
+ currentLineLength = chunk.length - searchFrom;
535
+ }
536
+ continue;
537
+ }
538
+
539
+ let start = 0;
540
+ for (let i = 0; i < chunk.length; i++) {
541
+ if (chunk[i] === 0x0a) {
542
+ const segment = chunk.subarray(start, i);
543
+ if (segment.length > 0) {
544
+ appendSegment(segment);
545
+ }
546
+ finalizeLine();
547
+ start = i + 1;
548
+ }
549
+ }
550
+
551
+ if (start < chunk.length) {
552
+ appendSegment(chunk.subarray(start));
553
+ }
554
+ }
555
+ } finally {
556
+ if (fileHandle) {
557
+ await fileHandle.close();
558
+ }
559
+ }
560
+
561
+ if (reachedEof && (endedWithNewline || currentLineLength > 0 || !sawAnyByte)) {
562
+ finalizeLine();
563
+ }
564
+
565
+ let firstLinePreview: { text: string; bytes: number } | undefined;
566
+ if (firstLinePreviewBytes > 0) {
567
+ const { text, bytes } = truncateHeadBytes(Buffer.concat(firstLinePreviewChunks, firstLinePreviewBytes), maxBytes);
568
+ firstLinePreview = { text, bytes };
569
+ }
570
+
571
+ return {
572
+ lines: collectedLines,
573
+ totalFileLines: lineIndex,
574
+ collectedBytes,
575
+ stoppedByByteLimit,
576
+ firstLinePreview,
577
+ firstLineByteLength,
578
+ selectedBytesTotal,
579
+ reachedEof,
580
+ };
581
+ }
582
+
583
+ // Maximum image file size (20MB) - larger images will be rejected to prevent OOM during serialization
584
+ const MAX_IMAGE_SIZE = MAX_IMAGE_INPUT_BYTES;
585
+ const GLOB_TIMEOUT_MS = 5000;
586
+
587
+ function isNotFoundError(error: unknown): boolean {
588
+ if (!error || typeof error !== "object") return false;
589
+ const code = (error as { code?: string }).code;
590
+ return code === "ENOENT" || code === "ENOTDIR";
591
+ }
592
+
593
+ /**
594
+ * Escape glob metacharacters so a literal path (e.g. `foo[1].ts`) interpolated
595
+ * into a suffix-glob pattern matches itself. Each metachar is wrapped in a
596
+ * character class (the native glob engine rewrites `\` to `/`, so backslash
597
+ * escaping is unavailable). `]`/`}` need no escaping once their openers are
598
+ * neutralized — unmatched closers are literal.
599
+ */
600
+ function escapeGlobMetachars(value: string): string {
601
+ return value.replace(/[*?[{]/g, "[$&]");
602
+ }
603
+
604
+ /**
605
+ * Attempt to resolve a non-existent path by finding a unique suffix match within the workspace.
606
+ * Uses a glob suffix pattern so the native engine handles matching directly.
607
+ * Returns null when 0 or >1 candidates match (ambiguous = no auto-resolution).
608
+ */
609
+ async function findUniqueSuffixMatch(
610
+ rawPath: string,
611
+ cwd: string,
612
+ signal?: AbortSignal,
613
+ ): Promise<{ absolutePath: string; displayPath: string } | null> {
614
+ const normalized = rawPath.replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/+$/, "");
615
+ if (!normalized) return null;
616
+ const pattern = `**/${escapeGlobMetachars(normalized)}`;
617
+
618
+ const timeoutSignal = AbortSignal.timeout(GLOB_TIMEOUT_MS);
619
+ const combinedSignal = signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal;
620
+
621
+ let matches: string[];
622
+ try {
623
+ const result = await untilAborted(combinedSignal, () =>
624
+ glob({
625
+ pattern,
626
+ path: cwd,
627
+ // No fileType filter: matches both files and directories
628
+ hidden: true,
629
+ }),
630
+ );
631
+ matches = result.matches.map(m => m.path);
632
+ } catch (error) {
633
+ if (error instanceof Error && error.name === "AbortError") {
634
+ if (!signal?.aborted) return null; // timeout — give up silently
635
+ throw new ToolAbortError();
636
+ }
637
+ return null;
638
+ }
639
+
640
+ if (matches.length !== 1) return null;
641
+
642
+ return {
643
+ absolutePath: path.resolve(cwd, matches[0]),
644
+ displayPath: matches[0],
645
+ };
646
+ }
647
+
648
+ function decodeUtf8Text(bytes: Uint8Array): string | null {
649
+ if (bytes.indexOf(0) !== -1) return null;
650
+
651
+ try {
652
+ return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
653
+ } catch {
654
+ return null;
655
+ }
656
+ }
657
+
658
+ function prependSuffixResolutionNotice(text: string, suffixResolution?: { from: string; to: string }): string {
659
+ if (!suffixResolution) return text;
660
+
661
+ const notice = `[Path '${suffixResolution.from}' not found; resolved to '${suffixResolution.to}' via suffix match]`;
662
+ return text ? `${notice}\n${text}` : notice;
663
+ }
664
+ const PDF_IMAGE_PLACEHOLDER_RE = /<!--\s*image:\s*([^\s<>]+)(.*?)-->/g;
665
+ const PDF_IMAGE_MEMBER_RE = /^(.*\.pdf):(.*)$/i;
666
+ const PDF_IMAGE_MEMBER_EXTENSION_RE = /\.png$/i;
667
+
668
+ function pdfImageMemberPath(pdfPath: string, imageId: string): string {
669
+ const member = PDF_IMAGE_MEMBER_EXTENSION_RE.test(imageId) ? imageId : `${imageId}.png`;
670
+ return `${pdfPath}:${member}`;
671
+ }
672
+
673
+ function rewritePdfImagePlaceholders(markdown: string, pdfPath: string): string {
674
+ return markdown.replace(PDF_IMAGE_PLACEHOLDER_RE, (_match: string, imageId: string, metadataText: string) => {
675
+ const metadata = metadataText.trim();
676
+ const suffix = metadata.length > 0 ? ` (${metadata})` : "";
677
+ return `Image ${imageId}${suffix}: read \`${pdfImageMemberPath(pdfPath, imageId)}\``;
678
+ });
679
+ }
680
+
681
+ function splitPdfImageMemberReadPath(readPath: string): { pdfPath: string; member: string } | null {
682
+ const match = PDF_IMAGE_MEMBER_RE.exec(readPath);
683
+ if (!match) return null;
684
+ const pdfPath = match[1];
685
+ const member = match[2];
686
+ if (pdfPath === undefined || member === undefined) return null;
687
+ if (member.length !== 0 && !PDF_IMAGE_MEMBER_EXTENSION_RE.test(member)) return null;
688
+ return { pdfPath, member };
689
+ }
690
+
691
+ const readSchema = type({
692
+ path: type("string").describe(
693
+ 'Local path, internal URI (e.g. "omp://", "issue://123", "pr://123"), or URL; append :<sel> for line ranges or raw mode (e.g. "src/foo.ts:50-100")',
694
+ ),
695
+ });
696
+
697
+ export type ReadToolInput = typeof readSchema.infer;
698
+
699
+ export interface ReadToolDetails {
700
+ kind?: "file" | "url";
701
+ truncation?: TruncationResult;
702
+ isDirectory?: boolean;
703
+ resolvedPath?: string;
704
+ suffixResolution?: { from: string; to: string };
705
+ url?: string;
706
+ finalUrl?: string;
707
+ contentType?: string;
708
+ method?: string;
709
+ notes?: string[];
710
+ meta?: OutputMeta;
711
+ /** Raw text + start line for user-visible TUI rendering, set when content is text-like.
712
+ * Mirrors the same lines the model receives but without hashline/line-number prefixes,
713
+ * so the TUI can render the file content with its own gutter without re-parsing the formatted text. */
714
+ displayContent?: {
715
+ text: string;
716
+ startLine: number;
717
+ lineNumbers?: Array<number | null>;
718
+ };
719
+ summary?: { lines: number; elidedSpans: number; elidedLines: number };
720
+ /** Number of unresolved git conflicts surfaced by this read (TUI uses for inline `⚠ N` badge). */
721
+ conflictCount?: number;
722
+ /** Paths recovered from a delimited read argument; used only by the TUI to render one call as multiple read rows. */
723
+ displayReadTargets?: string[];
724
+ }
725
+
726
+ type ReadParams = ReadToolInput;
727
+
728
+ /** Parsed representation of a path-embedded selector. */
729
+ type ParsedSelector =
730
+ | { kind: "none" }
731
+ | { kind: "raw" }
732
+ | { kind: "conflicts" }
733
+ | { kind: "lines"; ranges: [LineRange, ...LineRange[]]; raw?: boolean };
734
+
735
+ /** Returns true when the selector requested verbatim/raw output (alone or combined with a range). */
736
+ function isRawSelector(parsed: ParsedSelector): boolean {
737
+ return parsed.kind === "raw" || (parsed.kind === "lines" && parsed.raw === true);
738
+ }
739
+
740
+ /** Returns true when the selector requested multiple line ranges. */
741
+ function isMultiRange(parsed: ParsedSelector): boolean {
742
+ return parsed.kind === "lines" && parsed.ranges.length > 1;
743
+ }
744
+
745
+ function parseSel(sel: string | undefined): ParsedSelector {
746
+ if (!sel || sel.length === 0) return { kind: "none" };
747
+
748
+ // Compound selector: `1-50:raw` or `raw:1-50`. Split into chunks and accept
749
+ // any combination of one line range (possibly multi) and the literal `raw`.
750
+ if (sel.includes(":")) {
751
+ const chunks = sel.split(":");
752
+ if (chunks.length === 2) {
753
+ const [a, b] = chunks as [string, string];
754
+ const aIsRaw = a.toLowerCase() === "raw";
755
+ const bIsRaw = b.toLowerCase() === "raw";
756
+ const rangeChunk = aIsRaw ? b : bIsRaw ? a : null;
757
+ const rawChunk = aIsRaw ? a : bIsRaw ? b : null;
758
+ if (rangeChunk !== null && rawChunk !== null) {
759
+ const ranges = parseLineRanges(rangeChunk);
760
+ if (ranges) {
761
+ return { kind: "lines", ranges, raw: true };
762
+ }
763
+ }
764
+ }
765
+ // Unrecognized compound — fall through (sqlite/archive/url consume their own colon syntax).
766
+ return { kind: "none" };
767
+ }
768
+
769
+ if (sel.toLowerCase() === "raw") return { kind: "raw" };
770
+ if (sel.toLowerCase() === "conflicts") return { kind: "conflicts" };
771
+ const ranges = parseLineRanges(sel);
772
+ if (ranges) {
773
+ return { kind: "lines", ranges };
774
+ }
775
+ // Unrecognized selectors fall through; sqlite/archive/url readers consume their own colon syntax.
776
+ return { kind: "none" };
777
+ }
778
+
779
+ /**
780
+ * Convert a single-range selector to the offset/limit pair used by internal pagination.
781
+ * Returns the FIRST range only — multi-range callers MUST branch on `isMultiRange` before
782
+ * calling this helper.
783
+ */
784
+ function selToOffsetLimit(parsed: ParsedSelector): { offset?: number; limit?: number } {
785
+ if (parsed.kind === "lines") {
786
+ const first = parsed.ranges[0];
787
+ const limit = first.endLine !== undefined ? first.endLine - first.startLine + 1 : undefined;
788
+ return { offset: first.startLine, limit };
789
+ }
790
+ return {};
791
+ }
792
+
793
+ interface ResolvedArchiveReadPath {
794
+ absolutePath: string;
795
+ archiveSubPath: string;
796
+ suffixResolution?: { from: string; to: string };
797
+ }
798
+
799
+ interface ResolvedSqliteReadPath {
800
+ absolutePath: string;
801
+ sqliteSubPath: string;
802
+ queryString: string;
803
+ suffixResolution?: { from: string; to: string };
804
+ }
805
+
806
+ /** Per-execute memo of suffix-glob lookups; `null` records a confirmed miss. */
807
+ type SuffixMatchCache = Map<string, { absolutePath: string; displayPath: string } | null>;
808
+
809
+ /**
810
+ * Read tool implementation.
811
+ *
812
+ * Reads files with support for images, converted documents (via markit), and text.
813
+ * Directories return a formatted listing with modification times.
814
+ */
815
+ export class ReadTool implements AgentTool<typeof readSchema, ReadToolDetails> {
816
+ readonly name = "read";
817
+ readonly approval = "read" as const;
818
+ readonly label = "Read";
819
+ readonly loadMode = "essential";
820
+ readonly description: string;
821
+ readonly parameters = readSchema;
822
+ readonly strict = true;
823
+
824
+ readonly #autoResizeImages: boolean;
825
+ readonly #defaultLimit: number;
826
+ readonly #inspectImageEnabled: boolean;
827
+
828
+ constructor(private readonly session: ToolSession) {
829
+ const displayMode = resolveFileDisplayMode(session);
830
+ this.#autoResizeImages = session.settings.get("images.autoResize");
831
+ this.#defaultLimit = Math.max(
832
+ 1,
833
+ Math.min(session.settings.get("read.defaultLimit") ?? DEFAULT_MAX_LINES, DEFAULT_MAX_LINES),
834
+ );
835
+ this.#inspectImageEnabled = session.settings.get("inspect_image.enabled");
836
+ this.description = prompt.render(readDescription, {
837
+ DEFAULT_LIMIT: String(this.#defaultLimit),
838
+ DEFAULT_MAX_LINES: String(DEFAULT_MAX_LINES),
839
+ IS_HL_MODE: displayMode.hashLines,
840
+ IS_LINE_NUMBER_MODE: !displayMode.hashLines && displayMode.lineNumbers,
841
+ INSPECT_IMAGE_ENABLED: this.#inspectImageEnabled,
842
+ });
843
+ }
844
+
845
+ async #tryReadDelimitedPaths(
846
+ readPath: string,
847
+ signal?: AbortSignal,
848
+ ): Promise<AgentToolResult<ReadToolDetails> | null> {
849
+ const parts = await splitDelimitedPathEntry(readPath, this.session.cwd);
850
+ if (!parts) return null;
851
+
852
+ const notice = `Note: interpreted as ${parts.length} paths: ${parts.join(", ")}`;
853
+ const notes = [notice];
854
+ const content: Array<TextContent | ImageContent> = [];
855
+ const displayReadTargets: string[] = [];
856
+ let pendingText = notice;
857
+ const flushText = () => {
858
+ if (pendingText.length === 0) return;
859
+ content.push({ type: "text", text: pendingText });
860
+ pendingText = "";
861
+ };
862
+ const appendText = (text: string) => {
863
+ pendingText = pendingText.length > 0 ? `${pendingText}\n\n${text}` : text;
864
+ };
865
+
866
+ for (const part of parts) {
867
+ try {
868
+ const result = await this.execute("read-delimited-part", { path: part }, signal);
869
+ displayReadTargets.push(result.details?.suffixResolution?.to ?? part);
870
+ for (const block of result.content) {
871
+ if (block.type === "text") {
872
+ appendText(block.text);
873
+ continue;
874
+ }
875
+ flushText();
876
+ content.push(block);
877
+ }
878
+ } catch (error) {
879
+ if (error instanceof ToolAbortError || signal?.aborted) throw error;
880
+ const message = error instanceof Error ? error.message : String(error);
881
+ const errorNote = `Could not read ${part}: ${message}`;
882
+ notes.push(errorNote);
883
+ displayReadTargets.push(part);
884
+ appendText(`[${errorNote}]`);
885
+ }
886
+ }
887
+ flushText();
888
+
889
+ return toolResult<ReadToolDetails>({ notes, displayReadTargets }).content(content).done();
890
+ }
891
+
892
+ /**
893
+ * Memoized {@link findUniqueSuffixMatch} for a single read call. A missing
894
+ * path with archive/sqlite extensions probes the workspace once per stage
895
+ * (archive candidates, sqlite candidates, plain path) — each glob carries a
896
+ * 5s timeout, so repeated lookups of the same string stack into a long
897
+ * stall before erroring. The cache collapses repeats within one execute().
898
+ */
899
+ async #findSuffixMatchCached(
900
+ cache: SuffixMatchCache,
901
+ rawPath: string,
902
+ signal?: AbortSignal,
903
+ ): Promise<{ absolutePath: string; displayPath: string } | null> {
904
+ const hit = cache.get(rawPath);
905
+ if (hit !== undefined) return hit;
906
+ const result = await findUniqueSuffixMatch(rawPath, this.session.cwd, signal);
907
+ cache.set(rawPath, result);
908
+ return result;
909
+ }
910
+
911
+ async #resolveArchiveReadPath(
912
+ readPath: string,
913
+ suffixCache: SuffixMatchCache,
914
+ signal?: AbortSignal,
915
+ ): Promise<ResolvedArchiveReadPath | null> {
916
+ const candidates = parseArchivePathCandidates(readPath);
917
+ for (const candidate of candidates) {
918
+ let absolutePath = resolveReadPath(candidate.archivePath, this.session.cwd);
919
+ let suffixResolution: { from: string; to: string } | undefined;
920
+
921
+ try {
922
+ const stat = await Bun.file(absolutePath).stat();
923
+ if (stat.isDirectory()) continue;
924
+ return {
925
+ absolutePath,
926
+ archiveSubPath: candidate.archivePath === readPath ? "" : candidate.subPath,
927
+ suffixResolution,
928
+ };
929
+ } catch (error) {
930
+ if (!isNotFoundError(error) || isRemoteMountPath(absolutePath)) continue;
931
+
932
+ const suffixMatch = await this.#findSuffixMatchCached(suffixCache, candidate.archivePath, signal);
933
+ if (!suffixMatch) continue;
934
+
935
+ try {
936
+ const retryStat = await Bun.file(suffixMatch.absolutePath).stat();
937
+ if (retryStat.isDirectory()) continue;
938
+
939
+ absolutePath = suffixMatch.absolutePath;
940
+ suffixResolution = { from: candidate.archivePath, to: suffixMatch.displayPath };
941
+ return {
942
+ absolutePath,
943
+ archiveSubPath: candidate.archivePath === readPath ? "" : candidate.subPath,
944
+ suffixResolution,
945
+ };
946
+ } catch (retryError) {
947
+ if (!isNotFoundError(retryError)) {
948
+ throw retryError;
949
+ }
950
+ }
951
+ }
952
+ }
953
+
954
+ return null;
955
+ }
956
+
957
+ async #resolveSqliteReadPath(
958
+ readPath: string,
959
+ suffixCache: SuffixMatchCache,
960
+ signal?: AbortSignal,
961
+ ): Promise<ResolvedSqliteReadPath | null> {
962
+ const candidates = parseSqlitePathCandidates(readPath);
963
+ for (const candidate of candidates) {
964
+ let absolutePath = resolveReadPath(candidate.sqlitePath, this.session.cwd);
965
+ let suffixResolution: { from: string; to: string } | undefined;
966
+
967
+ try {
968
+ const stat = await Bun.file(absolutePath).stat();
969
+ if (stat.isDirectory()) continue;
970
+ if (!(await isSqliteFile(absolutePath))) continue;
971
+
972
+ return {
973
+ absolutePath,
974
+ sqliteSubPath: candidate.subPath,
975
+ queryString: candidate.queryString,
976
+ suffixResolution,
977
+ };
978
+ } catch (error) {
979
+ if (!isNotFoundError(error) || isRemoteMountPath(absolutePath)) continue;
980
+
981
+ const suffixMatch = await this.#findSuffixMatchCached(suffixCache, candidate.sqlitePath, signal);
982
+ if (!suffixMatch) continue;
983
+
984
+ try {
985
+ const retryStat = await Bun.file(suffixMatch.absolutePath).stat();
986
+ if (retryStat.isDirectory()) continue;
987
+ if (!(await isSqliteFile(suffixMatch.absolutePath))) continue;
988
+
989
+ absolutePath = suffixMatch.absolutePath;
990
+ suffixResolution = { from: candidate.sqlitePath, to: suffixMatch.displayPath };
991
+ return {
992
+ absolutePath,
993
+ sqliteSubPath: candidate.subPath,
994
+ queryString: candidate.queryString,
995
+ suffixResolution,
996
+ };
997
+ } catch (retryError) {
998
+ if (!isNotFoundError(retryError)) {
999
+ throw retryError;
1000
+ }
1001
+ }
1002
+ }
1003
+ }
1004
+
1005
+ return null;
1006
+ }
1007
+
1008
+ #pdfImageCacheDir(absolutePdfPath: string): string {
1009
+ const artifactsDir = this.session.getArtifactsDir?.();
1010
+ let root = artifactsDir ?? undefined;
1011
+ if (root === undefined) {
1012
+ const sessionFile = this.session.getSessionFile();
1013
+ root = sessionFile?.endsWith(".jsonl")
1014
+ ? sessionFile.slice(0, -6)
1015
+ : path.join(os.tmpdir(), "omp-read-pdf-images");
1016
+ }
1017
+ const basename = path.basename(absolutePdfPath).replace(/[^A-Za-z0-9._-]/g, "_");
1018
+ return path.join(root, "read-pdf-images", `${basename}-${Bun.hash(absolutePdfPath).toString(36)}`);
1019
+ }
1020
+
1021
+ async #listPdfImageMembers(imageDir: string): Promise<string[]> {
1022
+ try {
1023
+ const entries = await fs.readdir(imageDir, { withFileTypes: true });
1024
+ const members: string[] = [];
1025
+ for (const entry of entries) {
1026
+ if (entry.isFile() && PDF_IMAGE_MEMBER_EXTENSION_RE.test(entry.name)) members.push(entry.name);
1027
+ }
1028
+ return members.sort();
1029
+ } catch (error) {
1030
+ if (isNotFoundError(error)) return [];
1031
+ throw error;
1032
+ }
1033
+ }
1034
+
1035
+ async #ensurePdfImageCache(absolutePdfPath: string, signal?: AbortSignal): Promise<string> {
1036
+ const imageDir = this.#pdfImageCacheDir(absolutePdfPath);
1037
+ const markerPath = path.join(imageDir, ".extracted");
1038
+ try {
1039
+ await fs.stat(markerPath);
1040
+ return imageDir;
1041
+ } catch (error) {
1042
+ if (!isNotFoundError(error)) throw error;
1043
+ }
1044
+
1045
+ await fs.rm(imageDir, { recursive: true, force: true });
1046
+ await fs.mkdir(imageDir, { recursive: true });
1047
+ const result = await convertFileWithMarkit(absolutePdfPath, signal, { imageDir });
1048
+ if (!result.ok) {
1049
+ await fs.rm(imageDir, { recursive: true, force: true });
1050
+ throw new ToolError(`Cannot extract images from PDF: ${result.error ?? "conversion failed"}`);
1051
+ }
1052
+ await Bun.write(markerPath, "ok");
1053
+ return imageDir;
1054
+ }
1055
+
1056
+ async #readPdfImageMember(
1057
+ absolutePdfPath: string,
1058
+ pdfDisplayPath: string,
1059
+ member: string,
1060
+ suffixResolution: { from: string; to: string } | undefined,
1061
+ signal?: AbortSignal,
1062
+ ): Promise<AgentToolResult<ReadToolDetails>> {
1063
+ const imageDir = await this.#ensurePdfImageCache(absolutePdfPath, signal);
1064
+ const members = await this.#listPdfImageMembers(imageDir);
1065
+ if (member.length === 0) {
1066
+ const text =
1067
+ members.length === 0
1068
+ ? "No extractable PDF image members found."
1069
+ : `Extractable PDF image members:\n${members
1070
+ .map(imageMember => `- read \`${pdfDisplayPath}:${imageMember}\``)
1071
+ .join("\n")}`;
1072
+ return toolResult<ReadToolDetails>({ resolvedPath: absolutePdfPath, suffixResolution })
1073
+ .text(prependSuffixResolutionNotice(text, suffixResolution))
1074
+ .sourcePath(absolutePdfPath)
1075
+ .done();
1076
+ }
1077
+
1078
+ if (!members.includes(member)) {
1079
+ const available = members.length === 0 ? "(none)" : members.join(", ");
1080
+ throw new ToolError(`PDF image member '${member}' not found. Available members: ${available}`);
1081
+ }
1082
+
1083
+ const imagePath = path.join(imageDir, member);
1084
+ const imageStat = await Bun.file(imagePath).stat();
1085
+ if (imageStat.size > MAX_IMAGE_SIZE) {
1086
+ const sizeStr = formatBytes(imageStat.size);
1087
+ const maxStr = formatBytes(MAX_IMAGE_SIZE);
1088
+ throw new ToolError(`Image file too large: ${sizeStr} exceeds ${maxStr} limit.`);
1089
+ }
1090
+ const metadata = await readImageMetadata(imagePath);
1091
+ const mimeType = metadata?.mimeType;
1092
+ if (!mimeType) throw new ToolError(`PDF image member '${member}' is not a supported image.`);
1093
+ const imageInput = await loadImageInput({
1094
+ path: `${pdfDisplayPath}:${member}`,
1095
+ cwd: this.session.cwd,
1096
+ autoResize: this.#autoResizeImages,
1097
+ maxBytes: MAX_IMAGE_SIZE,
1098
+ resolvedPath: imagePath,
1099
+ detectedMimeType: mimeType,
1100
+ excludeWebP: webpExclusionForModel(this.session.getActiveModel?.()),
1101
+ });
1102
+ if (!imageInput) {
1103
+ throw new ToolError(`Read image file [${mimeType}] failed: unsupported image format.`);
1104
+ }
1105
+ const textNote = prependSuffixResolutionNotice(imageInput.textNote, suffixResolution);
1106
+ return toolResult<ReadToolDetails>({ resolvedPath: absolutePdfPath, suffixResolution })
1107
+ .content([
1108
+ { type: "text", text: textNote },
1109
+ { type: "image", data: imageInput.data, mimeType: imageInput.mimeType },
1110
+ ])
1111
+ .sourcePath(imageInput.resolvedPath)
1112
+ .done();
1113
+ }
1114
+
1115
+ #buildInMemoryTextResult(
1116
+ text: string,
1117
+ offset: number | undefined,
1118
+ limit: number | undefined,
1119
+ options: {
1120
+ details?: ReadToolDetails;
1121
+ sourcePath?: string;
1122
+ sourceUrl?: string;
1123
+ sourceInternal?: string;
1124
+ entityLabel: string;
1125
+ ignoreResultLimits?: boolean;
1126
+ raw?: boolean;
1127
+ immutable?: boolean;
1128
+ },
1129
+ ): AgentToolResult<ReadToolDetails> {
1130
+ const displayMode = resolveFileDisplayMode(this.session, { raw: options.raw, immutable: options.immutable });
1131
+ const details = options.details ?? {};
1132
+ const allLines = text.split("\n");
1133
+ const totalLines = allLines.length;
1134
+ // User-requested 0-indexed range start. Lines BEFORE this are leading
1135
+ // context (added below if offset is explicit).
1136
+ const requestedStart = offset ? Math.max(0, offset - 1) : 0;
1137
+ const ignoreResultLimits = options.ignoreResultLimits ?? false;
1138
+ const requestedEnd = limit !== undefined ? Math.min(requestedStart + limit, allLines.length) : allLines.length;
1139
+ // Expand only on sides the user actually constrained: leading context
1140
+ // when offset>1, trailing context when a finite limit was set.
1141
+ const expanded = expandRangeWithContext(
1142
+ requestedStart,
1143
+ requestedEnd,
1144
+ allLines.length,
1145
+ offset !== undefined && offset > 1,
1146
+ limit !== undefined,
1147
+ );
1148
+ const startLine = expanded.startLine;
1149
+ const endLineExpanded = expanded.endLine;
1150
+ const startLineDisplay = startLine + 1;
1151
+
1152
+ const resultBuilder = toolResult(details);
1153
+ if (options.sourcePath) {
1154
+ resultBuilder.sourcePath(options.sourcePath);
1155
+ }
1156
+ if (options.sourceUrl) {
1157
+ resultBuilder.sourceUrl(options.sourceUrl);
1158
+ }
1159
+ if (options.sourceInternal) {
1160
+ resultBuilder.sourceInternal(options.sourceInternal);
1161
+ }
1162
+
1163
+ if (requestedStart >= allLines.length) {
1164
+ const suggestion =
1165
+ allLines.length === 0
1166
+ ? `The ${options.entityLabel} is empty.`
1167
+ : `Use :1 to read from the start, or :${allLines.length} to read the last line.`;
1168
+ return resultBuilder
1169
+ .text(
1170
+ `Line ${requestedStart + 1} is beyond end of ${options.entityLabel} (${allLines.length} lines total). ${suggestion}`,
1171
+ )
1172
+ .done();
1173
+ }
1174
+
1175
+ const endLine = endLineExpanded;
1176
+ const selectedContent = allLines.slice(startLine, endLine).join("\n");
1177
+ const userLimitedLines = limit !== undefined ? endLine - startLine : undefined;
1178
+ const truncation = ignoreResultLimits ? noTruncResult(selectedContent) : truncateHead(selectedContent);
1179
+
1180
+ const shouldAddHashLines = displayMode.hashLines;
1181
+ const shouldAddLineNumbers = shouldAddHashLines ? false : displayMode.lineNumbers;
1182
+ const hashContext =
1183
+ shouldAddHashLines && options.sourcePath
1184
+ ? recordFullHashlineContext(
1185
+ this.session,
1186
+ options.sourcePath,
1187
+ formatPathRelativeToCwd(options.sourcePath, this.session.cwd),
1188
+ text,
1189
+ )
1190
+ : undefined;
1191
+ let emittedHashlineHeader = false;
1192
+ let seenLines: number[] | undefined;
1193
+ const formatText = (content: string, startNum: number): string => {
1194
+ const lineCount = countTextLines(content);
1195
+ details.displayContent = {
1196
+ text: content,
1197
+ startLine: startNum,
1198
+ lineNumbers: Array.from({ length: lineCount }, (_, i) => startNum + i),
1199
+ };
1200
+ if (shouldAddHashLines) seenLines = contiguousLineNumbers(startNum, countTextLines(content));
1201
+ const formatted = formatTextWithMode(content, startNum, shouldAddHashLines, shouldAddLineNumbers);
1202
+ if (!hashContext || emittedHashlineHeader) return formatted;
1203
+ emittedHashlineHeader = true;
1204
+ return prependHashlineHeader(formatted, hashContext);
1205
+ };
1206
+ const formatLineEntries = (entries: readonly LineEntry[], startNum: number): string => {
1207
+ const firstLine = entries.find(entry => entry.kind === "line");
1208
+ details.displayContent = {
1209
+ text: lineEntriesToPlainText(entries, BRACKET_CONTEXT_ELLIPSIS),
1210
+ startLine: firstLine?.kind === "line" ? firstLine.lineNumber : startNum,
1211
+ lineNumbers: entries.map(entry => (entry.kind === "line" ? entry.lineNumber : null)),
1212
+ };
1213
+ if (shouldAddHashLines) seenLines = lineNumbersFromEntries(entries);
1214
+ const formatted = formatLineEntriesWithMode(entries, shouldAddHashLines, shouldAddLineNumbers);
1215
+ if (!hashContext || emittedHashlineHeader) return formatted;
1216
+ emittedHashlineHeader = true;
1217
+ return prependHashlineHeader(formatted, hashContext);
1218
+ };
1219
+ const buildLineEntries = (endLineDisplay: number): LineEntry[] =>
1220
+ buildLineEntriesWithBlockContext(allLines, [{ startLine: startLineDisplay, endLine: endLineDisplay }], {
1221
+ path: options.sourcePath,
1222
+ });
1223
+
1224
+ let outputText: string;
1225
+ let truncationInfo:
1226
+ | { result: TruncationResult; options: { direction: "head"; startLine?: number; totalFileLines?: number } }
1227
+ | undefined;
1228
+
1229
+ if (truncation.firstLineExceedsLimit) {
1230
+ const firstLine = allLines[startLine] ?? "";
1231
+ const firstLineBytes = Buffer.byteLength(firstLine, "utf-8");
1232
+ const snippet = truncateHeadBytes(firstLine, DEFAULT_MAX_BYTES);
1233
+
1234
+ if (shouldAddHashLines) {
1235
+ outputText = `[Line ${startLineDisplay} is ${formatBytes(
1236
+ firstLineBytes,
1237
+ )}, exceeds ${formatBytes(DEFAULT_MAX_BYTES)} limit. Hashline output requires full lines; cannot emit an editable numbered preview for a truncated line.]`;
1238
+ } else {
1239
+ outputText = formatText(snippet.text, startLineDisplay);
1240
+ }
1241
+
1242
+ if (snippet.text.length === 0) {
1243
+ outputText = `[Line ${startLineDisplay} is ${formatBytes(
1244
+ firstLineBytes,
1245
+ )}, exceeds ${formatBytes(DEFAULT_MAX_BYTES)} limit. Unable to display a valid UTF-8 snippet.]`;
1246
+ }
1247
+
1248
+ details.truncation = truncation;
1249
+ truncationInfo = {
1250
+ result: truncation,
1251
+ options: { direction: "head", startLine: startLineDisplay, totalFileLines: totalLines },
1252
+ };
1253
+ } else if (truncation.truncated) {
1254
+ const outputLines = truncation.outputLines ?? countTextLines(truncation.content);
1255
+ const endLineDisplay = startLineDisplay + Math.max(0, outputLines - 1);
1256
+ outputText =
1257
+ options.raw === true
1258
+ ? formatText(truncation.content, startLineDisplay)
1259
+ : formatLineEntries(buildLineEntries(endLineDisplay), startLineDisplay);
1260
+ details.truncation = truncation;
1261
+ truncationInfo = {
1262
+ result: truncation,
1263
+ options: { direction: "head", startLine: startLineDisplay, totalFileLines: totalLines },
1264
+ };
1265
+ } else if (userLimitedLines !== undefined && startLine + userLimitedLines < allLines.length) {
1266
+ const remaining = allLines.length - (startLine + userLimitedLines);
1267
+ const nextOffset = startLine + userLimitedLines + 1;
1268
+
1269
+ outputText =
1270
+ options.raw === true
1271
+ ? formatText(selectedContent, startLineDisplay)
1272
+ : formatLineEntries(buildLineEntries(endLine), startLineDisplay);
1273
+ outputText += `\n\n[${remaining} more lines in ${options.entityLabel}. Use :${nextOffset} to continue]`;
1274
+ } else {
1275
+ outputText =
1276
+ options.raw === true
1277
+ ? formatText(truncation.content, startLineDisplay)
1278
+ : formatLineEntries(buildLineEntries(endLine), startLineDisplay);
1279
+ }
1280
+
1281
+ if (hashContext?.tag && options.sourcePath && seenLines) {
1282
+ recordSeenLines(this.session, options.sourcePath, hashContext.tag, seenLines);
1283
+ }
1284
+ resultBuilder.text(outputText);
1285
+ if (truncationInfo) {
1286
+ resultBuilder.truncation(truncationInfo.result, truncationInfo.options);
1287
+ }
1288
+ return resultBuilder.done();
1289
+ }
1290
+
1291
+ /**
1292
+ * Render a multi-range read against in-memory text. Each range emits a
1293
+ * formatted block with its own anchors / line numbers, blocks are joined
1294
+ * with an elision separator, and ranges past EOF surface as `[…]` notices
1295
+ * so the model can correct the next call. No leading/trailing context is
1296
+ * added — multi-range callers always specify exact bounds.
1297
+ */
1298
+ #buildInMemoryMultiRangeResult(
1299
+ text: string,
1300
+ ranges: readonly LineRange[],
1301
+ options: {
1302
+ details?: ReadToolDetails;
1303
+ sourcePath?: string;
1304
+ sourceUrl?: string;
1305
+ sourceInternal?: string;
1306
+ entityLabel: string;
1307
+ raw?: boolean;
1308
+ immutable?: boolean;
1309
+ },
1310
+ ): AgentToolResult<ReadToolDetails> {
1311
+ const displayMode = resolveFileDisplayMode(this.session, { raw: options.raw, immutable: options.immutable });
1312
+ const details = options.details ?? {};
1313
+ const allLines = text.split("\n");
1314
+ const totalLines = allLines.length;
1315
+ const shouldAddHashLines = displayMode.hashLines;
1316
+ const shouldAddLineNumbers = shouldAddHashLines ? false : displayMode.lineNumbers;
1317
+ const hashContext =
1318
+ shouldAddHashLines && options.sourcePath
1319
+ ? recordFullHashlineContext(
1320
+ this.session,
1321
+ options.sourcePath,
1322
+ formatPathRelativeToCwd(options.sourcePath, this.session.cwd),
1323
+ text,
1324
+ )
1325
+ : undefined;
1326
+ let emittedHashlineHeader = false;
1327
+
1328
+ let seenLines: number[] | undefined;
1329
+ const resultBuilder = toolResult(details);
1330
+ if (options.sourcePath) resultBuilder.sourcePath(options.sourcePath);
1331
+ if (options.sourceUrl) resultBuilder.sourceUrl(options.sourceUrl);
1332
+ if (options.sourceInternal) resultBuilder.sourceInternal(options.sourceInternal);
1333
+
1334
+ const outOfBounds: LineRange[] = [];
1335
+ const visibleSpans: Array<{ startLine: number; endLine: number }> = [];
1336
+ const rawParts: string[] = [];
1337
+ for (const range of ranges) {
1338
+ if (range.startLine > totalLines) {
1339
+ outOfBounds.push(range);
1340
+ continue;
1341
+ }
1342
+ const effectiveEnd = Math.min(range.endLine ?? totalLines, totalLines);
1343
+ visibleSpans.push({ startLine: range.startLine, endLine: effectiveEnd });
1344
+ if (options.raw === true) {
1345
+ rawParts.push(allLines.slice(range.startLine - 1, effectiveEnd).join("\n"));
1346
+ }
1347
+ }
1348
+
1349
+ let outputText = "";
1350
+ if (options.raw === true) {
1351
+ outputText = rawParts.length > 0 ? rawParts.join("\n\n…\n\n") : "";
1352
+ } else if (visibleSpans.length > 0) {
1353
+ const entries = buildLineEntriesWithBlockContext(allLines, visibleSpans, { path: options.sourcePath });
1354
+ if (shouldAddHashLines) seenLines = lineNumbersFromEntries(entries);
1355
+ const firstLine = entries.find(entry => entry.kind === "line");
1356
+ if (firstLine?.kind === "line") {
1357
+ details.displayContent = {
1358
+ text: lineEntriesToPlainText(entries, BRACKET_CONTEXT_ELLIPSIS),
1359
+ startLine: firstLine.lineNumber,
1360
+ lineNumbers: entries.map(entry => (entry.kind === "line" ? entry.lineNumber : null)),
1361
+ };
1362
+ }
1363
+ const formatted = formatLineEntriesWithMode(entries, shouldAddHashLines, shouldAddLineNumbers);
1364
+ outputText = hashContext && !emittedHashlineHeader ? prependHashlineHeader(formatted, hashContext) : formatted;
1365
+ if (hashContext) emittedHashlineHeader = true;
1366
+ }
1367
+ const notices: string[] = [];
1368
+ for (const range of outOfBounds) {
1369
+ const bound = range.endLine !== undefined ? `${range.startLine}-${range.endLine}` : `${range.startLine}`;
1370
+ notices.push(`[Range ${bound} is beyond end of ${options.entityLabel} (${totalLines} lines total); skipped]`);
1371
+ }
1372
+ const finalText =
1373
+ notices.length > 0 ? (outputText ? `${outputText}\n${notices.join("\n")}` : notices.join("\n")) : outputText;
1374
+ if (hashContext?.tag && options.sourcePath && seenLines) {
1375
+ recordSeenLines(this.session, options.sourcePath, hashContext.tag, seenLines);
1376
+ }
1377
+ resultBuilder.text(finalText);
1378
+ return resultBuilder.done();
1379
+ }
1380
+
1381
+ /**
1382
+ * Stream multiple non-contiguous ranges from a local file. ACP bridge takes
1383
+ * priority when present (editor buffer is source of truth); otherwise each
1384
+ * range is streamed independently with its own line/byte budget. Out-of-bounds
1385
+ * ranges surface as inline notices rather than aborting the read.
1386
+ */
1387
+ async #readLocalFileMultiRange(
1388
+ absolutePath: string,
1389
+ ranges: readonly LineRange[],
1390
+ fileSize: number,
1391
+ parsed: ParsedSelector,
1392
+ displayMode: { hashLines: boolean; lineNumbers: boolean },
1393
+ suffixResolution: { from: string; to: string } | undefined,
1394
+ signal: AbortSignal | undefined,
1395
+ ): Promise<{
1396
+ outputText: string;
1397
+ columnTruncated: number;
1398
+ displayContent?: { text: string; startLine: number; lineNumbers?: Array<number | null> };
1399
+ bridgeResult?: AgentToolResult<ReadToolDetails>;
1400
+ }> {
1401
+ const rawSelector = isRawSelector(parsed);
1402
+
1403
+ // ACP bridge first — the editor's in-memory buffer is source of truth.
1404
+ const bridgePromise = this.#routeReadThroughBridge(absolutePath);
1405
+ if (bridgePromise !== undefined) {
1406
+ try {
1407
+ const bridgeText = await bridgePromise;
1408
+ const bridgeResult = this.#buildInMemoryMultiRangeResult(bridgeText, ranges, {
1409
+ details: { resolvedPath: absolutePath, suffixResolution },
1410
+ sourcePath: absolutePath,
1411
+ entityLabel: "file",
1412
+ raw: rawSelector,
1413
+ });
1414
+ if (suffixResolution) {
1415
+ const notice = `[Path '${suffixResolution.from}' not found; resolved to '${suffixResolution.to}' via suffix match]`;
1416
+ const firstText = bridgeResult.content.find((c): c is TextContent => c.type === "text");
1417
+ if (firstText) firstText.text = `${notice}\n${firstText.text}`;
1418
+ }
1419
+ return { outputText: "", columnTruncated: 0, bridgeResult };
1420
+ } catch (error) {
1421
+ logger.warn("ACP fs readTextFile failed; falling back to disk", { path: absolutePath, error });
1422
+ }
1423
+ }
1424
+
1425
+ const shouldAddHashLines = !rawSelector && displayMode.hashLines;
1426
+ const shouldAddLineNumbers = rawSelector ? false : shouldAddHashLines ? false : displayMode.lineNumbers;
1427
+ const maxColumns = resolveOutputMaxColumns(this.session.settings);
1428
+
1429
+ const blocks: string[] = [];
1430
+ const notices: string[] = [];
1431
+ const visibleSpans: Array<{ startLine: number; endLine: number }> = [];
1432
+ const displayLineByNumber = new Map<number, string>();
1433
+ const fullLines = rawSelector ? undefined : await readBracketContextFullLines(absolutePath, fileSize);
1434
+ let columnTruncated = 0;
1435
+ let displayContent: { text: string; startLine: number; lineNumbers?: Array<number | null> } | undefined;
1436
+
1437
+ for (const range of ranges) {
1438
+ const rangeStart = range.startLine - 1; // 0-indexed
1439
+ const requestedLength = range.endLine !== undefined ? range.endLine - range.startLine + 1 : this.#defaultLimit;
1440
+ const maxLines = Math.min(requestedLength, DEFAULT_MAX_LINES);
1441
+
1442
+ // When the full file is already in memory (the common case for files
1443
+ // within the snapshot byte cap), slice ranges from it instead of
1444
+ // re-streaming the file once per range.
1445
+ let collectedLines: string[];
1446
+ let totalFileLines: number;
1447
+ if (fullLines) {
1448
+ totalFileLines = fullLines.length;
1449
+ collectedLines = fullLines.slice(rangeStart, rangeStart + maxLines);
1450
+ } else {
1451
+ const maxBytesForRead = Math.max(DEFAULT_MAX_BYTES, maxLines * 512);
1452
+ const streamResult = await streamLinesFromFile(
1453
+ absolutePath,
1454
+ rangeStart,
1455
+ maxLines,
1456
+ maxBytesForRead,
1457
+ maxLines,
1458
+ signal,
1459
+ fileSize > SNAPSHOT_MAX_BYTES, // giant file: collected ranges don't need an exact EOF line count
1460
+ );
1461
+ totalFileLines = streamResult.totalFileLines;
1462
+ collectedLines = streamResult.lines;
1463
+ }
1464
+
1465
+ if (rangeStart >= totalFileLines) {
1466
+ const bound = range.endLine !== undefined ? `${range.startLine}-${range.endLine}` : `${range.startLine}`;
1467
+ notices.push(`[Range ${bound} is beyond end of file (${totalFileLines} lines total); skipped]`);
1468
+ continue;
1469
+ }
1470
+
1471
+ // Column truncation is display-only; clone before stamping ellipsis so
1472
+ // the original on-disk lines stay intact for display reconstruction.
1473
+ let displayLines: string[] = collectedLines;
1474
+ if (!rawSelector && maxColumns > 0) {
1475
+ let cloned: string[] | undefined;
1476
+ for (let i = 0; i < collectedLines.length; i++) {
1477
+ const { text, wasTruncated } = truncateLine(collectedLines[i], maxColumns);
1478
+ if (wasTruncated) {
1479
+ if (!cloned) cloned = collectedLines.slice();
1480
+ cloned[i] = text;
1481
+ columnTruncated = maxColumns;
1482
+ }
1483
+ }
1484
+ if (cloned) displayLines = cloned;
1485
+ }
1486
+ const endLine = range.startLine + Math.max(0, displayLines.length - 1);
1487
+ visibleSpans.push({ startLine: range.startLine, endLine });
1488
+ for (let i = 0; i < displayLines.length; i++) {
1489
+ displayLineByNumber.set(range.startLine + i, displayLines[i] ?? "");
1490
+ }
1491
+ if (!fullLines || rawSelector) {
1492
+ const blockText = displayLines.join("\n");
1493
+ blocks.push(formatTextWithMode(blockText, range.startLine, shouldAddHashLines, shouldAddLineNumbers));
1494
+ }
1495
+ }
1496
+
1497
+ let outputText: string;
1498
+ if (!rawSelector && fullLines && visibleSpans.length > 0) {
1499
+ const entries = buildLineEntriesWithBlockContext(
1500
+ fullLines,
1501
+ visibleSpans,
1502
+ { path: absolutePath },
1503
+ {
1504
+ lineText: (lineNumber, sourceText) => {
1505
+ const visibleText = displayLineByNumber.get(lineNumber);
1506
+ if (visibleText !== undefined) return visibleText;
1507
+ if (maxColumns <= 0) return sourceText;
1508
+ const truncated = truncateLine(sourceText, maxColumns);
1509
+ if (truncated.wasTruncated) columnTruncated = maxColumns;
1510
+ return truncated.text;
1511
+ },
1512
+ },
1513
+ );
1514
+ const firstLine = entries.find(entry => entry.kind === "line");
1515
+ displayContent = {
1516
+ text: lineEntriesToPlainText(entries, BRACKET_CONTEXT_ELLIPSIS),
1517
+ startLine: firstLine?.kind === "line" ? firstLine.lineNumber : (visibleSpans[0]?.startLine ?? 1),
1518
+ lineNumbers: entries.map(entry => (entry.kind === "line" ? entry.lineNumber : null)),
1519
+ };
1520
+ outputText = formatLineEntriesWithMode(entries, shouldAddHashLines, shouldAddLineNumbers);
1521
+ } else {
1522
+ outputText = blocks.join("\n\n…\n\n");
1523
+ }
1524
+ if (shouldAddHashLines && outputText) {
1525
+ const tag = await recordFileSnapshot(this.session, absolutePath);
1526
+ if (tag) {
1527
+ recordSeenLinesFromBody(this.session, absolutePath, tag, outputText);
1528
+ outputText = `${formatHashlineHeader(formatPathRelativeToCwd(absolutePath, this.session.cwd), tag)}\n${outputText}`;
1529
+ }
1530
+ }
1531
+ if (notices.length > 0) {
1532
+ outputText = outputText ? `${outputText}\n${notices.join("\n")}` : notices.join("\n");
1533
+ }
1534
+ return { outputText, columnTruncated, displayContent };
1535
+ }
1536
+
1537
+ async #readArchiveDirectory(
1538
+ archive: ArchiveReader,
1539
+ archivePath: string,
1540
+ subPath: string,
1541
+ offset: number | undefined,
1542
+ limit: number | undefined,
1543
+ details: ReadToolDetails,
1544
+ signal?: AbortSignal,
1545
+ ): Promise<AgentToolResult<ReadToolDetails>> {
1546
+ const DEFAULT_LIMIT = 500;
1547
+ const effectiveLimit = limit ?? DEFAULT_LIMIT;
1548
+ const allEntries = archive.listDirectory(subPath);
1549
+ // `offset` is 1-indexed (line-selector semantics): `a.zip:dir:50` starts
1550
+ // the listing at the 50th entry instead of being silently ignored.
1551
+ const entries = offset !== undefined && offset > 1 ? allEntries.slice(offset - 1) : allEntries;
1552
+
1553
+ const listLimit = applyListLimit(entries, { limit: effectiveLimit });
1554
+ const limitedEntries = listLimit.items;
1555
+ const limitMeta = listLimit.meta;
1556
+
1557
+ for (let index = 0; index < limitedEntries.length; index++) {
1558
+ throwIfAborted(signal);
1559
+ }
1560
+ const results = formatArchiveEntryLines(limitedEntries);
1561
+
1562
+ const output = results.length > 0 ? results.join("\n") : "(empty archive directory)";
1563
+ const text = prependSuffixResolutionNotice(output, details.suffixResolution);
1564
+ const truncation = truncateHead(text, { maxLines: Number.MAX_SAFE_INTEGER });
1565
+ const directoryDetails: ReadToolDetails = { ...details, isDirectory: true };
1566
+ const resultBuilder = toolResult<ReadToolDetails>(directoryDetails).text(truncation.content);
1567
+ resultBuilder.sourcePath(archivePath).limits({ resultLimit: limitMeta.resultLimit?.reached });
1568
+ if (truncation.truncated) {
1569
+ directoryDetails.truncation = truncation;
1570
+ resultBuilder.truncation(truncation, { direction: "head" });
1571
+ }
1572
+ return resultBuilder.done();
1573
+ }
1574
+
1575
+ async #readArchive(
1576
+ readPath: string,
1577
+ parsedSel: ParsedSelector,
1578
+ resolvedArchivePath: ResolvedArchiveReadPath,
1579
+ signal?: AbortSignal,
1580
+ ): Promise<AgentToolResult<ReadToolDetails>> {
1581
+ throwIfAborted(signal);
1582
+ const archive = await openArchive(resolvedArchivePath.absolutePath);
1583
+ throwIfAborted(signal);
1584
+
1585
+ const details: ReadToolDetails = {
1586
+ resolvedPath: resolvedArchivePath.absolutePath,
1587
+ suffixResolution: resolvedArchivePath.suffixResolution,
1588
+ };
1589
+
1590
+ let archiveSubPath = resolvedArchivePath.archiveSubPath;
1591
+ let sel = parsedSel;
1592
+ let node = archive.getNode(archiveSubPath);
1593
+ if (!node && archiveSubPath) {
1594
+ // `archive.zip:500` / `archive.zip:raw`: the whole subPath is a
1595
+ // selector on the archive root, not a member name. Member names take
1596
+ // precedence (getNode above); fall back to root + selector.
1597
+ const wholeSel = parseSel(archiveSubPath);
1598
+ if (wholeSel.kind !== "none") {
1599
+ node = archive.getNode("");
1600
+ archiveSubPath = "";
1601
+ sel = wholeSel;
1602
+ }
1603
+ }
1604
+ if (!node) {
1605
+ throw new ToolError(`Path '${readPath}' not found inside archive`);
1606
+ }
1607
+
1608
+ if (node.isDirectory) {
1609
+ if (isMultiRange(sel)) {
1610
+ throw new ToolError("Multi-range line selectors are not supported for archive directory listings.");
1611
+ }
1612
+ const { offset, limit } = selToOffsetLimit(sel);
1613
+ return this.#readArchiveDirectory(
1614
+ archive,
1615
+ resolvedArchivePath.absolutePath,
1616
+ archiveSubPath,
1617
+ offset,
1618
+ limit,
1619
+ details,
1620
+ signal,
1621
+ );
1622
+ }
1623
+
1624
+ const entry = await archive.readFile(archiveSubPath);
1625
+ const text = decodeUtf8Text(entry.bytes);
1626
+ if (text === null) {
1627
+ return toolResult<ReadToolDetails>(details)
1628
+ .text(
1629
+ prependSuffixResolutionNotice(
1630
+ `[Cannot read binary archive entry '${entry.path}' (${formatBytes(entry.size)})]`,
1631
+ resolvedArchivePath.suffixResolution,
1632
+ ),
1633
+ )
1634
+ .sourcePath(resolvedArchivePath.absolutePath)
1635
+ .done();
1636
+ }
1637
+
1638
+ // Archive members are immutable: there is no edit path for bytes inside
1639
+ // an archive, and a hashline tag keyed to the archive file would invite
1640
+ // (and fail) edits while clobbering sibling members' snapshots.
1641
+ const raw = isRawSelector(sel);
1642
+ const result =
1643
+ isMultiRange(sel) && sel.kind === "lines"
1644
+ ? this.#buildInMemoryMultiRangeResult(text, sel.ranges, {
1645
+ details,
1646
+ sourcePath: resolvedArchivePath.absolutePath,
1647
+ entityLabel: "archive entry",
1648
+ raw,
1649
+ immutable: true,
1650
+ })
1651
+ : this.#buildInMemoryTextResult(text, selToOffsetLimit(sel).offset, selToOffsetLimit(sel).limit, {
1652
+ details,
1653
+ sourcePath: resolvedArchivePath.absolutePath,
1654
+ entityLabel: "archive entry",
1655
+ raw,
1656
+ immutable: true,
1657
+ });
1658
+ const firstText = result.content.find((content): content is TextContent => content.type === "text");
1659
+ if (firstText) {
1660
+ firstText.text = prependSuffixResolutionNotice(firstText.text, resolvedArchivePath.suffixResolution);
1661
+ }
1662
+ return result;
1663
+ }
1664
+
1665
+ async #readSqlite(
1666
+ resolvedSqlitePath: ResolvedSqliteReadPath,
1667
+ signal?: AbortSignal,
1668
+ ): Promise<AgentToolResult<ReadToolDetails>> {
1669
+ throwIfAborted(signal);
1670
+
1671
+ const selectorInput = {
1672
+ subPath: resolvedSqlitePath.sqliteSubPath,
1673
+ queryString: resolvedSqlitePath.queryString,
1674
+ };
1675
+ const selector = parseSqliteSelector(selectorInput.subPath, selectorInput.queryString);
1676
+ const details: ReadToolDetails = {
1677
+ resolvedPath: resolvedSqlitePath.absolutePath,
1678
+ suffixResolution: resolvedSqlitePath.suffixResolution,
1679
+ };
1680
+
1681
+ let db: Database | null = null;
1682
+ try {
1683
+ db = new Database(resolvedSqlitePath.absolutePath, { readonly: true, strict: true });
1684
+ db.run("PRAGMA busy_timeout = 3000");
1685
+ throwIfAborted(signal);
1686
+
1687
+ switch (selector.kind) {
1688
+ case "list": {
1689
+ const listLimit = applyListLimit(listTables(db), { limit: 500 });
1690
+ const output = prependSuffixResolutionNotice(
1691
+ renderTableList(listLimit.items),
1692
+ resolvedSqlitePath.suffixResolution,
1693
+ );
1694
+ const truncation = truncateHead(output, { maxLines: Number.MAX_SAFE_INTEGER });
1695
+ details.truncation = truncation.truncated ? truncation : undefined;
1696
+ const resultBuilder = toolResult<ReadToolDetails>(details)
1697
+ .text(truncation.content)
1698
+ .sourcePath(resolvedSqlitePath.absolutePath)
1699
+ .limits({ resultLimit: listLimit.meta.resultLimit?.reached });
1700
+ if (truncation.truncated) {
1701
+ resultBuilder.truncation(truncation, { direction: "head" });
1702
+ }
1703
+ return resultBuilder.done();
1704
+ }
1705
+ case "schema": {
1706
+ const sampleRows = queryRows(db, selector.table, { limit: selector.sampleLimit, offset: 0 });
1707
+ let output = renderSchema(getTableSchema(db, selector.table), {
1708
+ columns: sampleRows.columns,
1709
+ rows: sampleRows.rows,
1710
+ });
1711
+ if (sampleRows.rows.length < sampleRows.totalCount) {
1712
+ const remaining = sampleRows.totalCount - sampleRows.rows.length;
1713
+ output += `\n[${remaining} more rows; append :${selector.table}?limit=20&offset=${sampleRows.rows.length} to the database path to continue]`;
1714
+ }
1715
+ return toolResult<ReadToolDetails>(details)
1716
+ .text(prependSuffixResolutionNotice(output, resolvedSqlitePath.suffixResolution))
1717
+ .sourcePath(resolvedSqlitePath.absolutePath)
1718
+ .done();
1719
+ }
1720
+ case "row": {
1721
+ const lookup = resolveTableRowLookup(db, selector.table);
1722
+ const row =
1723
+ lookup.kind === "pk"
1724
+ ? getRowByKey(db, selector.table, lookup, selector.key)
1725
+ : getRowByRowId(db, selector.table, selector.key);
1726
+ if (!row) {
1727
+ return toolResult<ReadToolDetails>(details)
1728
+ .text(
1729
+ prependSuffixResolutionNotice(
1730
+ `No row found in table '${selector.table}' for key '${selector.key}'.`,
1731
+ resolvedSqlitePath.suffixResolution,
1732
+ ),
1733
+ )
1734
+ .sourcePath(resolvedSqlitePath.absolutePath)
1735
+ .done();
1736
+ }
1737
+ return toolResult<ReadToolDetails>(details)
1738
+ .text(prependSuffixResolutionNotice(renderRow(row), resolvedSqlitePath.suffixResolution))
1739
+ .sourcePath(resolvedSqlitePath.absolutePath)
1740
+ .done();
1741
+ }
1742
+ case "query": {
1743
+ const page = queryRows(db, selector.table, selector);
1744
+ return toolResult<ReadToolDetails>(details)
1745
+ .text(
1746
+ prependSuffixResolutionNotice(
1747
+ renderTable(page.columns, page.rows, {
1748
+ totalCount: page.totalCount,
1749
+ offset: selector.offset,
1750
+ limit: selector.limit,
1751
+ table: selector.table,
1752
+ dbPath: resolvedSqlitePath.absolutePath,
1753
+ }),
1754
+ resolvedSqlitePath.suffixResolution,
1755
+ ),
1756
+ )
1757
+ .sourcePath(resolvedSqlitePath.absolutePath)
1758
+ .done();
1759
+ }
1760
+ case "raw": {
1761
+ const result = executeReadQuery(db, selector.sql);
1762
+ let output = renderTable(result.columns, result.rows, {
1763
+ totalCount: result.rows.length,
1764
+ offset: 0,
1765
+ limit: result.rows.length || DEFAULT_MAX_LINES,
1766
+ table: "query",
1767
+ dbPath: resolvedSqlitePath.absolutePath,
1768
+ });
1769
+ if (result.truncated) {
1770
+ output += `\n[Output capped at ${MAX_RAW_QUERY_ROWS} rows; add a LIMIT/OFFSET clause to the query to page through more]`;
1771
+ }
1772
+ return toolResult<ReadToolDetails>(details)
1773
+ .text(prependSuffixResolutionNotice(output, resolvedSqlitePath.suffixResolution))
1774
+ .sourcePath(resolvedSqlitePath.absolutePath)
1775
+ .done();
1776
+ }
1777
+ }
1778
+
1779
+ throw new ToolError("Unsupported SQLite selector");
1780
+ } catch (error) {
1781
+ if (error instanceof ToolError) {
1782
+ throw error;
1783
+ }
1784
+ throw new ToolError(error instanceof Error ? error.message : String(error));
1785
+ } finally {
1786
+ db?.close();
1787
+ }
1788
+ }
1789
+
1790
+ #routeReadThroughBridge(
1791
+ absolutePath: string,
1792
+ options?: { line?: number; limit?: number },
1793
+ ): Promise<string> | undefined {
1794
+ const bridge = this.session.getClientBridge?.();
1795
+ if (!bridge?.capabilities.readTextFile || !bridge.readTextFile) return undefined;
1796
+ return bridge.readTextFile({ path: absolutePath, ...options });
1797
+ }
1798
+
1799
+ async #trySummarize(absolutePath: string, fileSize: number, signal?: AbortSignal): Promise<SummaryResult | null> {
1800
+ if (fileSize > MAX_SUMMARY_BYTES) return null;
1801
+
1802
+ try {
1803
+ throwIfAborted(signal);
1804
+ const bridgePromise = this.#routeReadThroughBridge(absolutePath);
1805
+ const code =
1806
+ bridgePromise !== undefined
1807
+ ? await bridgePromise.catch(() => Bun.file(absolutePath).text())
1808
+ : await Bun.file(absolutePath).text();
1809
+ throwIfAborted(signal);
1810
+ const lineCount = countTextLines(code);
1811
+ if (lineCount > MAX_SUMMARY_LINES) return null;
1812
+ if (lineCount < this.session.settings.get("read.summarize.minTotalLines")) return null;
1813
+
1814
+ const minBodyLines = this.session.settings.get("read.summarize.minBodyLines");
1815
+ const minCommentLines = this.session.settings.get("read.summarize.minCommentLines");
1816
+ const unfoldUntilLines = this.session.settings.get("read.summarize.unfoldUntil");
1817
+ const unfoldLimitLines = this.session.settings.get("read.summarize.unfoldLimit");
1818
+ const cache = getSummaryParseCache(this.session);
1819
+ const cacheKey = `${absolutePath}\0${Bun.hash(code)}\0${minBodyLines},${minCommentLines},${unfoldUntilLines},${unfoldLimitLines}`;
1820
+ const memoized = cache.get(cacheKey);
1821
+ if (memoized !== undefined) return memoized || null;
1822
+ const result = summarizeCode({
1823
+ code,
1824
+ path: absolutePath,
1825
+ minBodyLines,
1826
+ minCommentLines,
1827
+ unfoldUntilLines,
1828
+ unfoldLimitLines,
1829
+ });
1830
+ const usable = result.parsed && result.elided ? result : false;
1831
+ cache.set(cacheKey, usable);
1832
+ return usable || null;
1833
+ } catch {
1834
+ return null;
1835
+ }
1836
+ }
1837
+
1838
+ #renderSummary(summary: SummaryResult): {
1839
+ text: string;
1840
+ displayText: string;
1841
+ elidedRanges: ElidedRange[];
1842
+ elidedLines: number;
1843
+ } {
1844
+ const displayMode = resolveFileDisplayMode(this.session);
1845
+ const shouldAddHashLines = displayMode.hashLines;
1846
+ const shouldAddLineNumbers = shouldAddHashLines ? false : displayMode.lineNumbers;
1847
+
1848
+ // Flatten segments into per-line units so we can merge a kept-head /
1849
+ // elided / kept-tail sandwich into a single brace-pair line when the
1850
+ // boundary lines look like `… {` and `}` (or matching variants).
1851
+ type Unit =
1852
+ | { kind: "line"; line: number; text: string }
1853
+ | { kind: "elided"; startLine: number; endLine: number }
1854
+ | {
1855
+ kind: "merged";
1856
+ startLine: number;
1857
+ endLine: number;
1858
+ headText: string;
1859
+ tailText: string;
1860
+ };
1861
+
1862
+ const raw: Unit[] = [];
1863
+ for (const segment of summary.segments) {
1864
+ if (segment.kind === "elided") {
1865
+ raw.push({ kind: "elided", startLine: segment.startLine, endLine: segment.endLine });
1866
+ continue;
1867
+ }
1868
+ const text = segment.text ?? "";
1869
+ if (text.length === 0) continue;
1870
+ const lines = text.split("\n");
1871
+ for (let i = 0; i < lines.length; i++) {
1872
+ raw.push({ kind: "line", line: segment.startLine + i, text: lines[i] });
1873
+ }
1874
+ }
1875
+
1876
+ const units: Unit[] = [];
1877
+ let i = 0;
1878
+ while (i < raw.length) {
1879
+ const cur = raw[i];
1880
+ if (cur.kind === "elided") {
1881
+ const prev = units.length > 0 ? units[units.length - 1] : null;
1882
+ const next = i + 1 < raw.length ? raw[i + 1] : null;
1883
+ if (prev?.kind === "line" && next?.kind === "line" && canMergeBracePair(prev.text, next.text)) {
1884
+ units.pop();
1885
+ units.push({
1886
+ kind: "merged",
1887
+ startLine: prev.line,
1888
+ endLine: next.line,
1889
+ headText: prev.text,
1890
+ tailText: next.text,
1891
+ });
1892
+ i += 2;
1893
+ continue;
1894
+ }
1895
+ }
1896
+ units.push(cur);
1897
+ i++;
1898
+ }
1899
+
1900
+ const modelParts: string[] = [];
1901
+ const displayParts: string[] = [];
1902
+ const elidedRanges: ElidedRange[] = [];
1903
+ let elidedLines = 0;
1904
+ for (const unit of units) {
1905
+ if (unit.kind === "elided") {
1906
+ modelParts.push("…");
1907
+ displayParts.push("…");
1908
+ elidedRanges.push({ start: unit.startLine, end: unit.endLine });
1909
+ elidedLines += unit.endLine - unit.startLine + 1;
1910
+ continue;
1911
+ }
1912
+ if (unit.kind === "merged") {
1913
+ const formatted = formatMergedBraceLine(
1914
+ unit.startLine,
1915
+ unit.endLine,
1916
+ unit.headText,
1917
+ unit.tailText,
1918
+ shouldAddHashLines,
1919
+ shouldAddLineNumbers,
1920
+ );
1921
+ modelParts.push(formatted.model);
1922
+ displayParts.push(formatted.display);
1923
+ // Suggest the full brace range so re-reading shows both braces
1924
+ // plus the elided body in one shot.
1925
+ elidedRanges.push({ start: unit.startLine, end: unit.endLine });
1926
+ // Merged brace pair encloses (start+1)..(end-1) as elided.
1927
+ elidedLines += Math.max(0, unit.endLine - unit.startLine - 1);
1928
+ continue;
1929
+ }
1930
+ modelParts.push(formatSingleLine(unit.line, unit.text, shouldAddHashLines, shouldAddLineNumbers));
1931
+ displayParts.push(unit.text);
1932
+ }
1933
+
1934
+ return { text: modelParts.join("\n"), displayText: displayParts.join("\n"), elidedRanges, elidedLines };
1935
+ }
1936
+
1937
+ async execute(
1938
+ _toolCallId: string,
1939
+ params: ReadParams,
1940
+ signal?: AbortSignal,
1941
+ _onUpdate?: AgentToolUpdateCallback<ReadToolDetails>,
1942
+ _toolContext?: AgentToolContext,
1943
+ ): Promise<AgentToolResult<ReadToolDetails>> {
1944
+ let { path: readPath } = params;
1945
+ if (readPath.startsWith("file://")) {
1946
+ readPath = expandPath(readPath);
1947
+ }
1948
+
1949
+ const conflictUri = parseConflictUri(readPath);
1950
+ if (conflictUri) {
1951
+ if (conflictUri.id === "*") {
1952
+ throw new ToolError(
1953
+ "Reading `conflict://*` is not supported — wildcards are write-only. Use the `<path>:conflicts` read selector for the full list of conflicts in a file, or read `conflict://<N>` to inspect a single block.",
1954
+ );
1955
+ }
1956
+ return this.#readConflictRegion(conflictUri.id, conflictUri.scope);
1957
+ }
1958
+ const displayMode = resolveFileDisplayMode(this.session);
1959
+
1960
+ const parsedUrlTarget = parseReadUrlTarget(readPath);
1961
+ if (parsedUrlTarget) {
1962
+ if (!this.session.settings.get("fetch.enabled")) {
1963
+ throw new ToolError("URL reads are disabled by settings.");
1964
+ }
1965
+ if (parsedUrlTarget.ranges !== undefined) {
1966
+ const cached = await loadReadUrlCacheEntry(
1967
+ this.session,
1968
+ { path: parsedUrlTarget.path, raw: parsedUrlTarget.raw },
1969
+ signal,
1970
+ { ensureArtifact: true, preferCached: true },
1971
+ );
1972
+ return this.#buildInMemoryMultiRangeResult(cached.output, parsedUrlTarget.ranges, {
1973
+ details: { ...cached.details },
1974
+ sourceUrl: cached.details.finalUrl,
1975
+ entityLabel: "URL output",
1976
+ raw: parsedUrlTarget.raw,
1977
+ immutable: true,
1978
+ });
1979
+ }
1980
+ if (parsedUrlTarget.offset !== undefined || parsedUrlTarget.limit !== undefined) {
1981
+ const cached = await loadReadUrlCacheEntry(
1982
+ this.session,
1983
+ { path: parsedUrlTarget.path, raw: parsedUrlTarget.raw },
1984
+ signal,
1985
+ {
1986
+ ensureArtifact: true,
1987
+ preferCached: true,
1988
+ },
1989
+ );
1990
+ return this.#buildInMemoryTextResult(cached.output, parsedUrlTarget.offset, parsedUrlTarget.limit, {
1991
+ details: { ...cached.details },
1992
+ sourceUrl: cached.details.finalUrl,
1993
+ entityLabel: "URL output",
1994
+ raw: parsedUrlTarget.raw,
1995
+ immutable: true,
1996
+ });
1997
+ }
1998
+ return executeReadUrl(this.session, { path: parsedUrlTarget.path, raw: parsedUrlTarget.raw }, signal);
1999
+ }
2000
+
2001
+ // Handle internal URLs (agent://, artifact://, memory://, skill://, rule://, local://, mcp://, omp://, issue://, pr://).
2002
+ // Use the internal-URL-aware splitter so malformed selectors are peeled
2003
+ // off the URL and surfaced via parseSel rather than confusing handlers.
2004
+ const internalRouter = InternalUrlRouter.instance();
2005
+ if (internalRouter.canHandle(readPath)) {
2006
+ const internalTarget = splitInternalUrlSel(readPath);
2007
+ const parsed = parseSel(internalTarget.sel);
2008
+ if (internalTarget.sel !== undefined && parsed.kind === "none") {
2009
+ throw new ToolError(
2010
+ `Invalid selector ':${internalTarget.sel}' on '${internalTarget.path}'. Use :N, :N-M, :N+K, :N- (open-ended), a comma-separated list of ranges, :raw, or a range combined with raw (e.g. :raw:50-100).`,
2011
+ );
2012
+ }
2013
+ return this.#handleInternalUrl(internalTarget.path, parsed, signal);
2014
+ }
2015
+
2016
+ // One suffix-glob memo per read call — archive, sqlite, and plain-path
2017
+ // resolution share misses instead of re-globbing the workspace.
2018
+ const suffixCache: SuffixMatchCache = new Map();
2019
+
2020
+ const archivePath = await this.#resolveArchiveReadPath(readPath, suffixCache, signal);
2021
+ if (archivePath) {
2022
+ const archiveSubPath = splitPathAndSel(archivePath.archiveSubPath);
2023
+ const archiveParsed = parseSel(archiveSubPath.sel);
2024
+ return this.#readArchive(
2025
+ readPath,
2026
+ archiveParsed,
2027
+ { ...archivePath, archiveSubPath: archiveSubPath.path },
2028
+ signal,
2029
+ );
2030
+ }
2031
+
2032
+ const sqlitePath = await this.#resolveSqliteReadPath(readPath, suffixCache, signal);
2033
+ if (sqlitePath) {
2034
+ return this.#readSqlite(sqlitePath, signal);
2035
+ }
2036
+
2037
+ const pdfImageMemberPath = splitPdfImageMemberReadPath(readPath);
2038
+ if (pdfImageMemberPath) {
2039
+ let absolutePdfPath = resolveReadPath(pdfImageMemberPath.pdfPath, this.session.cwd);
2040
+ let suffixResolution: { from: string; to: string } | undefined;
2041
+ try {
2042
+ const stat = await Bun.file(absolutePdfPath).stat();
2043
+ if (stat.isDirectory())
2044
+ throw new ToolError(`Path '${pdfImageMemberPath.pdfPath}' is a directory, not a PDF file`);
2045
+ } catch (error) {
2046
+ if (!isNotFoundError(error) || isRemoteMountPath(absolutePdfPath)) throw error;
2047
+ const suffixMatch = await this.#findSuffixMatchCached(suffixCache, pdfImageMemberPath.pdfPath, signal);
2048
+ if (!suffixMatch) throw new ToolError(`Path '${pdfImageMemberPath.pdfPath}' not found`);
2049
+ absolutePdfPath = suffixMatch.absolutePath;
2050
+ suffixResolution = { from: pdfImageMemberPath.pdfPath, to: suffixMatch.displayPath };
2051
+ }
2052
+ return this.#readPdfImageMember(
2053
+ absolutePdfPath,
2054
+ pdfImageMemberPath.pdfPath,
2055
+ pdfImageMemberPath.member,
2056
+ suffixResolution,
2057
+ signal,
2058
+ );
2059
+ }
2060
+
2061
+ const localTarget = splitPathAndSel(readPath);
2062
+ const localReadPath = localTarget.path;
2063
+ const parsed = parseSel(localTarget.sel);
2064
+
2065
+ let absolutePath = resolveReadPath(localReadPath, this.session.cwd);
2066
+ let suffixResolution: { from: string; to: string } | undefined;
2067
+
2068
+ let isDirectory = false;
2069
+ let fileSize = 0;
2070
+ try {
2071
+ const stat = await Bun.file(absolutePath).stat();
2072
+ fileSize = stat.size;
2073
+ isDirectory = stat.isDirectory();
2074
+ } catch (error) {
2075
+ if (isNotFoundError(error)) {
2076
+ // Attempt unique suffix resolution before falling back to fuzzy suggestions
2077
+ if (!isRemoteMountPath(absolutePath)) {
2078
+ const suffixMatch = await this.#findSuffixMatchCached(suffixCache, localReadPath, signal);
2079
+ if (suffixMatch) {
2080
+ try {
2081
+ const retryStat = await Bun.file(suffixMatch.absolutePath).stat();
2082
+ absolutePath = suffixMatch.absolutePath;
2083
+ fileSize = retryStat.size;
2084
+ isDirectory = retryStat.isDirectory();
2085
+ suffixResolution = { from: localReadPath, to: suffixMatch.displayPath };
2086
+ } catch {
2087
+ // Suffix match candidate no longer stats — fall through to error path
2088
+ }
2089
+ }
2090
+ }
2091
+
2092
+ if (!suffixResolution) {
2093
+ const delimitedResult = await this.#tryReadDelimitedPaths(readPath, signal);
2094
+ if (delimitedResult) return delimitedResult;
2095
+ throw new ToolError(`Path '${localReadPath}' not found`);
2096
+ }
2097
+ } else {
2098
+ throw error;
2099
+ }
2100
+ }
2101
+
2102
+ if (isDirectory) {
2103
+ if (isMultiRange(parsed)) {
2104
+ throw new ToolError("Multi-range line selectors are not supported for directory listings.");
2105
+ }
2106
+ const { offset, limit } = selToOffsetLimit(parsed);
2107
+ // Directory listings are deterministic and fast; never abort them mid-scan
2108
+ // (an interrupt would otherwise surface a misleading "Operation aborted").
2109
+ const dirResult = await this.#readDirectory(absolutePath, offset, limit, undefined);
2110
+ if (suffixResolution) {
2111
+ dirResult.details ??= {};
2112
+ dirResult.details.suffixResolution = suffixResolution;
2113
+ }
2114
+ return dirResult;
2115
+ }
2116
+
2117
+ if (parsed.kind === "conflicts") {
2118
+ return this.#readFileConflicts(absolutePath, suffixResolution, signal);
2119
+ }
2120
+
2121
+ const imageMetadata = await readImageMetadata(absolutePath);
2122
+ const mimeType = imageMetadata?.mimeType;
2123
+ const ext = path.extname(absolutePath).toLowerCase();
2124
+ const shouldConvertWithMarkit = CONVERTIBLE_EXTENSIONS.has(ext);
2125
+ // Read the file based on type
2126
+ let content: Array<TextContent | ImageContent> | undefined;
2127
+ let details: ReadToolDetails = {};
2128
+ let sourcePath: string | undefined;
2129
+ let columnTruncated = 0;
2130
+ let truncationInfo:
2131
+ | { result: TruncationResult; options: { direction: "head"; startLine?: number; totalFileLines?: number } }
2132
+ | undefined;
2133
+
2134
+ if (mimeType) {
2135
+ if (this.#inspectImageEnabled) {
2136
+ const metadata = imageMetadata;
2137
+ const outputMime = metadata?.mimeType ?? mimeType;
2138
+ const outputBytes = fileSize;
2139
+ const metadataLines = [
2140
+ "Image metadata:",
2141
+ `- MIME: ${outputMime}`,
2142
+ `- Bytes: ${outputBytes} (${formatBytes(outputBytes)})`,
2143
+ metadata?.width !== undefined && metadata.height !== undefined
2144
+ ? `- Dimensions: ${metadata.width}x${metadata.height}`
2145
+ : "- Dimensions: unknown",
2146
+ metadata?.channels !== undefined ? `- Channels: ${metadata.channels}` : "- Channels: unknown",
2147
+ metadata?.hasAlpha === true
2148
+ ? "- Alpha: yes"
2149
+ : metadata?.hasAlpha === false
2150
+ ? "- Alpha: no"
2151
+ : "- Alpha: unknown",
2152
+ "",
2153
+ `If you want to analyze the image, call inspect_image with path="${formatPathRelativeToCwd(
2154
+ absolutePath,
2155
+ this.session.cwd,
2156
+ )}" and a question describing what to inspect and the desired output format.`,
2157
+ ];
2158
+ content = [{ type: "text", text: metadataLines.join("\n") }];
2159
+ details = {};
2160
+ sourcePath = absolutePath;
2161
+ } else {
2162
+ if (fileSize > MAX_IMAGE_SIZE) {
2163
+ const sizeStr = formatBytes(fileSize);
2164
+ const maxStr = formatBytes(MAX_IMAGE_SIZE);
2165
+ throw new ToolError(`Image file too large: ${sizeStr} exceeds ${maxStr} limit.`);
2166
+ }
2167
+ try {
2168
+ const imageInput = await loadImageInput({
2169
+ path: readPath,
2170
+ cwd: this.session.cwd,
2171
+ autoResize: this.#autoResizeImages,
2172
+ maxBytes: MAX_IMAGE_SIZE,
2173
+ resolvedPath: absolutePath,
2174
+ detectedMimeType: mimeType,
2175
+ excludeWebP: webpExclusionForModel(this.session.getActiveModel?.()),
2176
+ });
2177
+ if (!imageInput) {
2178
+ throw new ToolError(`Read image file [${mimeType}] failed: unsupported image format.`);
2179
+ }
2180
+ content = [
2181
+ { type: "text", text: imageInput.textNote },
2182
+ { type: "image", data: imageInput.data, mimeType: imageInput.mimeType },
2183
+ ];
2184
+ details = {};
2185
+ sourcePath = imageInput.resolvedPath;
2186
+ } catch (error) {
2187
+ if (error instanceof ImageInputTooLargeError) {
2188
+ throw new ToolError(error.message);
2189
+ }
2190
+ throw error;
2191
+ }
2192
+ }
2193
+ } else if (isNotebookPath(absolutePath) && !isRawSelector(parsed)) {
2194
+ const notebookText = await readEditableNotebookText(absolutePath, localReadPath);
2195
+ if (isMultiRange(parsed) && parsed.kind === "lines") {
2196
+ return this.#buildInMemoryMultiRangeResult(notebookText, parsed.ranges, {
2197
+ details: { resolvedPath: absolutePath },
2198
+ sourcePath: absolutePath,
2199
+ entityLabel: "notebook",
2200
+ });
2201
+ }
2202
+ const { offset, limit } = selToOffsetLimit(parsed);
2203
+ return this.#buildInMemoryTextResult(notebookText, offset, limit, {
2204
+ details: { resolvedPath: absolutePath },
2205
+ sourcePath: absolutePath,
2206
+ entityLabel: "notebook",
2207
+ });
2208
+ } else if (shouldConvertWithMarkit) {
2209
+ // Convert document via markit.
2210
+ const result = await convertFileWithMarkit(absolutePath, signal);
2211
+ if (result.ok) {
2212
+ const renderedContent =
2213
+ ext === ".pdf" ? rewritePdfImagePlaceholders(result.content, localReadPath) : result.content;
2214
+ // Route the converted markdown through the in-memory text builder
2215
+ // so line-range selectors (`file.pdf:50-100`, `:5-16,40-80`) and
2216
+ // raw mode apply against the converted output. Without this,
2217
+ // `file.pdf:50-100` silently returned the head of the document
2218
+ // because only `truncateHead` was being applied.
2219
+ if (isMultiRange(parsed) && parsed.kind === "lines") {
2220
+ return this.#buildInMemoryMultiRangeResult(renderedContent, parsed.ranges, {
2221
+ details: { resolvedPath: absolutePath },
2222
+ sourcePath: absolutePath,
2223
+ entityLabel: "document",
2224
+ });
2225
+ }
2226
+ const { offset, limit } = selToOffsetLimit(parsed);
2227
+ return this.#buildInMemoryTextResult(renderedContent, offset, limit, {
2228
+ details: { resolvedPath: absolutePath },
2229
+ sourcePath: absolutePath,
2230
+ entityLabel: "document",
2231
+ raw: isRawSelector(parsed),
2232
+ });
2233
+ } else if (result.error) {
2234
+ content = [{ type: "text", text: `[Cannot read ${ext} file: ${result.error || "conversion failed"}]` }];
2235
+ } else {
2236
+ content = [{ type: "text", text: `[Cannot read ${ext} file: conversion failed]` }];
2237
+ }
2238
+ } else {
2239
+ if (
2240
+ parsed.kind === "none" &&
2241
+ this.session.settings.get("read.summarize.enabled") &&
2242
+ (this.session.settings.get("read.summarize.prose") || !PROSE_SUMMARY_EXTENSIONS.has(ext))
2243
+ ) {
2244
+ const summary = await this.#trySummarize(absolutePath, fileSize, signal);
2245
+ if (summary?.parsed && summary.elided) {
2246
+ const renderedSummary = this.#renderSummary(summary);
2247
+ const footer = formatSummaryElisionFooter(
2248
+ localReadPath,
2249
+ renderedSummary.elidedRanges,
2250
+ renderedSummary.elidedLines,
2251
+ );
2252
+ const summaryHashContext = displayMode.hashLines
2253
+ ? await readHashlineHeaderContext(this.session, absolutePath, this.session.cwd)
2254
+ : undefined;
2255
+ const bodyText = footer ? `${renderedSummary.text}\n\n${footer}` : renderedSummary.text;
2256
+ const modelText = prependHashlineHeader(bodyText, summaryHashContext);
2257
+ if (summaryHashContext?.tag) {
2258
+ recordSeenLinesFromBody(this.session, absolutePath, summaryHashContext.tag, renderedSummary.text);
2259
+ }
2260
+ details = {
2261
+ displayContent: { text: renderedSummary.displayText, startLine: 1 },
2262
+ summary: {
2263
+ lines: countTextLines(renderedSummary.text),
2264
+ elidedSpans: renderedSummary.elidedRanges.length,
2265
+ elidedLines: renderedSummary.elidedLines,
2266
+ },
2267
+ };
2268
+
2269
+ sourcePath = absolutePath;
2270
+ content = [{ type: "text", text: modelText }];
2271
+ }
2272
+ }
2273
+
2274
+ if (!content) {
2275
+ if (isMultiRange(parsed) && parsed.kind === "lines") {
2276
+ const multiResult = await this.#readLocalFileMultiRange(
2277
+ absolutePath,
2278
+ parsed.ranges,
2279
+ fileSize,
2280
+ parsed,
2281
+ displayMode,
2282
+ suffixResolution,
2283
+ undefined, // plain-file read: deterministic and fast, never abort mid-read
2284
+ );
2285
+ if (multiResult.bridgeResult) return multiResult.bridgeResult;
2286
+ content = [{ type: "text", text: multiResult.outputText }];
2287
+ sourcePath = absolutePath;
2288
+ details = multiResult.displayContent ? { displayContent: multiResult.displayContent } : {};
2289
+ if (multiResult.columnTruncated > 0) {
2290
+ columnTruncated = multiResult.columnTruncated;
2291
+ }
2292
+ } else {
2293
+ // Raw text or line-range mode
2294
+ const { offset, limit } = selToOffsetLimit(parsed);
2295
+ // Try ACP bridge first — editor's in-memory buffer is source of truth.
2296
+ // Request full text so local range rendering keeps normal context and line numbers.
2297
+ const bridgePromise = this.#routeReadThroughBridge(absolutePath);
2298
+ if (bridgePromise !== undefined) {
2299
+ try {
2300
+ const bridgeText = await bridgePromise;
2301
+ const bridgeResult = this.#buildInMemoryTextResult(bridgeText, offset, limit, {
2302
+ details: { resolvedPath: absolutePath, suffixResolution },
2303
+ sourcePath: absolutePath,
2304
+ entityLabel: "file",
2305
+ raw: isRawSelector(parsed),
2306
+ });
2307
+ if (suffixResolution) {
2308
+ const notice = `[Path '${suffixResolution.from}' not found; resolved to '${suffixResolution.to}' via suffix match]`;
2309
+ const firstText = bridgeResult.content.find((c): c is TextContent => c.type === "text");
2310
+ if (firstText) firstText.text = `${notice}\n${firstText.text}`;
2311
+ }
2312
+ return bridgeResult;
2313
+ } catch (error) {
2314
+ logger.warn("ACP fs readTextFile failed; falling back to disk", { path: absolutePath, error });
2315
+ }
2316
+ }
2317
+
2318
+ // User-requested 0-indexed range start. Lines BEFORE this become
2319
+ // leading context (added below if offset is explicit).
2320
+ const requestedStart = offset ? Math.max(0, offset - 1) : 0;
2321
+ const expandStart = offset !== undefined && offset > 1;
2322
+ const expandEnd = limit !== undefined;
2323
+ const leadingContext = expandStart ? Math.min(requestedStart, RANGE_LEADING_CONTEXT_LINES) : 0;
2324
+ const trailingContext = expandEnd ? RANGE_TRAILING_CONTEXT_LINES : 0;
2325
+ const startLine = requestedStart - leadingContext;
2326
+ const startLineDisplay = startLine + 1;
2327
+
2328
+ const DEFAULT_LIMIT = this.#defaultLimit;
2329
+ const effectiveLimit = limit ?? DEFAULT_LIMIT;
2330
+ const maxLinesToCollect = Math.min(effectiveLimit + leadingContext + trailingContext, DEFAULT_MAX_LINES);
2331
+ const selectedLineLimit = effectiveLimit + leadingContext + trailingContext;
2332
+ // Scale byte budget with line limit so the configured line count actually fits.
2333
+ // Assume ~512 bytes/line average; never go below the shared default.
2334
+ const maxBytesForRead = Math.max(DEFAULT_MAX_BYTES, maxLinesToCollect * 512);
2335
+
2336
+ const streamResult = await streamLinesFromFile(
2337
+ absolutePath,
2338
+ startLine,
2339
+ maxLinesToCollect,
2340
+ maxBytesForRead,
2341
+ selectedLineLimit,
2342
+ undefined, // plain-file read: deterministic and fast, never abort mid-read
2343
+ fileSize > SNAPSHOT_MAX_BYTES, // giant file: don't scan to EOF just for an exact line count
2344
+ );
2345
+
2346
+ const {
2347
+ lines: collectedLines,
2348
+ totalFileLines,
2349
+ collectedBytes,
2350
+ stoppedByByteLimit,
2351
+ firstLinePreview,
2352
+ firstLineByteLength,
2353
+ reachedEof,
2354
+ } = streamResult;
2355
+
2356
+ // Check if offset is out of bounds - return graceful message instead of throwing
2357
+ if (requestedStart >= totalFileLines) {
2358
+ const suggestion =
2359
+ totalFileLines === 0
2360
+ ? "The file is empty."
2361
+ : `Use :1 to read from the start, or :${totalFileLines} to read the last line.`;
2362
+ return toolResult<ReadToolDetails>({ resolvedPath: absolutePath, suffixResolution })
2363
+ .text(
2364
+ `Line ${requestedStart + 1} is beyond end of file (${totalFileLines} lines total). ${suggestion}`,
2365
+ )
2366
+ .done();
2367
+ }
2368
+
2369
+ // Per-line column cap. Skipped in raw mode so `:raw` always returns
2370
+ // verbatim bytes for paste-back-into-tool workflows. Total byte/line
2371
+ // counts in `truncation` keep reflecting the source, not the trimmed
2372
+ // view — column truncation surfaces separately via `.limits()`.
2373
+ const rawSelector = isRawSelector(parsed);
2374
+ // Binary sniff: NUL bytes in the collected window mean the file is
2375
+ // not displayable text (binary, or UTF-16 which has NULs in the
2376
+ // ASCII range) — emit a notice instead of mojibake filling the
2377
+ // line budget. `:raw` stays an explicit escape hatch.
2378
+ if (!rawSelector) {
2379
+ for (const line of collectedLines) {
2380
+ if (line.includes("\u0000")) {
2381
+ return toolResult<ReadToolDetails>({ resolvedPath: absolutePath, suffixResolution })
2382
+ .text(
2383
+ prependSuffixResolutionNotice(
2384
+ `[Cannot read binary file '${formatPathRelativeToCwd(absolutePath, this.session.cwd)}' (${formatBytes(fileSize)}); content contains NUL bytes (binary or UTF-16 encoded)]`,
2385
+ suffixResolution,
2386
+ ),
2387
+ )
2388
+ .sourcePath(absolutePath)
2389
+ .done();
2390
+ }
2391
+ }
2392
+ }
2393
+ const maxColumns = resolveOutputMaxColumns(this.session.settings);
2394
+ // Column truncation is display-only. `collectedLines` MUST stay
2395
+ // byte-for-byte with the on-disk content so the snapshot recorded
2396
+ // below can be verified against the live file. Mutating it with
2397
+ // ellipsis-truncated text made every long-line file uneditable on
2398
+ // the next edit attempt.
2399
+ let displayLines: string[] = collectedLines;
2400
+ if (!rawSelector && maxColumns > 0) {
2401
+ let cloned: string[] | undefined;
2402
+ for (let i = 0; i < collectedLines.length; i++) {
2403
+ const { text, wasTruncated } = truncateLine(collectedLines[i], maxColumns);
2404
+ if (wasTruncated) {
2405
+ if (!cloned) cloned = collectedLines.slice();
2406
+ cloned[i] = text;
2407
+ columnTruncated = maxColumns;
2408
+ }
2409
+ }
2410
+ if (cloned) displayLines = cloned;
2411
+ }
2412
+
2413
+ const displayLineByNumber = new Map<number, string>();
2414
+ for (let i = 0; i < displayLines.length; i++) {
2415
+ displayLineByNumber.set(startLineDisplay + i, displayLines[i] ?? "");
2416
+ }
2417
+ const bracketContextFullLines = rawSelector
2418
+ ? undefined
2419
+ : await readBracketContextFullLines(absolutePath, fileSize);
2420
+ const displayedEndLine = startLineDisplay + Math.max(0, displayLines.length - 1);
2421
+
2422
+ const selectedContent = displayLines.join("\n");
2423
+ const userLimitedLines = collectedLines.length;
2424
+
2425
+ const totalSelectedLines = totalFileLines - startLine;
2426
+ const totalSelectedBytes = collectedBytes;
2427
+ const wasTruncated = collectedLines.length < totalSelectedLines || stoppedByByteLimit;
2428
+ const firstLineExceedsLimit = firstLineByteLength !== undefined && firstLineByteLength > maxBytesForRead;
2429
+
2430
+ const truncation: TruncationResult = {
2431
+ content: selectedContent,
2432
+ truncated: wasTruncated,
2433
+ truncatedBy: stoppedByByteLimit ? "bytes" : wasTruncated ? "lines" : undefined,
2434
+ totalLines: totalSelectedLines,
2435
+ totalBytes: totalSelectedBytes,
2436
+ outputLines: collectedLines.length,
2437
+ outputBytes: collectedBytes,
2438
+ lastLinePartial: false,
2439
+ firstLineExceedsLimit,
2440
+ };
2441
+
2442
+ const shouldAddHashLines = !rawSelector && displayMode.hashLines;
2443
+ const shouldAddLineNumbers = rawSelector ? false : shouldAddHashLines ? false : displayMode.lineNumbers;
2444
+ let hashContext: HashlineHeaderContext | undefined;
2445
+ if (shouldAddHashLines && collectedLines.length > 0 && !firstLineExceedsLimit) {
2446
+ // The tag is a content hash of the WHOLE file. A whole-file read
2447
+ // already holds every line in memory; a range read re-reads the
2448
+ // file (bounded by SNAPSHOT_MAX_BYTES) so the tag fingerprints the
2449
+ // full file and any anchor validates while the file is unchanged.
2450
+ const isWholeFile = offset === undefined && limit === undefined && !wasTruncated;
2451
+ const tag = isWholeFile
2452
+ ? getFileSnapshotStore(this.session).record(
2453
+ canonicalSnapshotKey(absolutePath),
2454
+ normalizeToLF(collectedLines.join("\n")),
2455
+ )
2456
+ : await recordFileSnapshot(this.session, absolutePath);
2457
+ if (tag) {
2458
+ hashContext = hashlineHeaderContext(formatPathRelativeToCwd(absolutePath, this.session.cwd), tag);
2459
+ }
2460
+ }
2461
+
2462
+ let capturedDisplayContent:
2463
+ | { text: string; startLine: number; lineNumbers?: Array<number | null> }
2464
+ | undefined;
2465
+ let emittedHashlineHeader = false;
2466
+ const formatText = (text: string, startNum: number): string => {
2467
+ const lineCount = countTextLines(text);
2468
+ capturedDisplayContent = {
2469
+ text,
2470
+ startLine: startNum,
2471
+ lineNumbers: Array.from({ length: lineCount }, (_, i) => startNum + i),
2472
+ };
2473
+ const formatted = formatTextWithMode(text, startNum, shouldAddHashLines, shouldAddLineNumbers);
2474
+ if (!hashContext || emittedHashlineHeader) return formatted;
2475
+ emittedHashlineHeader = true;
2476
+ return prependHashlineHeader(formatted, hashContext);
2477
+ };
2478
+ const formatBracketAwareText = (): string | undefined => {
2479
+ if (!bracketContextFullLines) return undefined;
2480
+ const entries = buildLineEntriesWithBlockContext(
2481
+ bracketContextFullLines,
2482
+ [{ startLine: startLineDisplay, endLine: displayedEndLine }],
2483
+ { path: absolutePath },
2484
+ {
2485
+ lineText: (lineNumber, sourceText) => {
2486
+ const visibleText = displayLineByNumber.get(lineNumber);
2487
+ if (visibleText !== undefined) return visibleText;
2488
+ if (maxColumns <= 0) return sourceText;
2489
+ const truncated = truncateLine(sourceText, maxColumns);
2490
+ if (truncated.wasTruncated) columnTruncated = maxColumns;
2491
+ return truncated.text;
2492
+ },
2493
+ },
2494
+ );
2495
+ const firstLine = entries.find(entry => entry.kind === "line");
2496
+ capturedDisplayContent = {
2497
+ text: lineEntriesToPlainText(entries, BRACKET_CONTEXT_ELLIPSIS),
2498
+ startLine: firstLine?.kind === "line" ? firstLine.lineNumber : startLineDisplay,
2499
+ lineNumbers: entries.map(entry => (entry.kind === "line" ? entry.lineNumber : null)),
2500
+ };
2501
+ const formatted = formatLineEntriesWithMode(entries, shouldAddHashLines, shouldAddLineNumbers);
2502
+ if (!hashContext || emittedHashlineHeader) return formatted;
2503
+ emittedHashlineHeader = true;
2504
+ return prependHashlineHeader(formatted, hashContext);
2505
+ };
2506
+
2507
+ let outputText: string;
2508
+
2509
+ if (truncation.firstLineExceedsLimit) {
2510
+ const firstLineBytes = firstLineByteLength ?? 0;
2511
+ const snippet = firstLinePreview ?? { text: "", bytes: 0 };
2512
+
2513
+ if (shouldAddHashLines) {
2514
+ outputText = `[Line ${startLineDisplay} is ${formatBytes(
2515
+ firstLineBytes,
2516
+ )}, exceeds ${formatBytes(maxBytesForRead)} limit. Hashline output requires full lines; cannot emit an editable numbered preview for a truncated line.]`;
2517
+ } else {
2518
+ outputText = formatText(snippet.text, startLineDisplay);
2519
+ }
2520
+ if (snippet.text.length === 0) {
2521
+ outputText = `[Line ${startLineDisplay} is ${formatBytes(
2522
+ firstLineBytes,
2523
+ )}, exceeds ${formatBytes(maxBytesForRead)} limit. Unable to display a valid UTF-8 snippet.]`;
2524
+ }
2525
+ details = { truncation };
2526
+ sourcePath = absolutePath;
2527
+ truncationInfo = {
2528
+ result: truncation,
2529
+ options: {
2530
+ direction: "head",
2531
+ startLine: startLineDisplay,
2532
+ totalFileLines: reachedEof ? totalFileLines : undefined,
2533
+ },
2534
+ };
2535
+ } else if (truncation.truncated) {
2536
+ outputText = formatBracketAwareText() ?? formatText(truncation.content, startLineDisplay);
2537
+ details = { truncation };
2538
+ sourcePath = absolutePath;
2539
+ truncationInfo = {
2540
+ result: truncation,
2541
+ options: {
2542
+ direction: "head",
2543
+ startLine: startLineDisplay,
2544
+ totalFileLines: reachedEof ? totalFileLines : undefined,
2545
+ },
2546
+ };
2547
+ } else if (startLine + userLimitedLines < totalFileLines || !reachedEof) {
2548
+ const nextOffset = startLine + userLimitedLines + 1;
2549
+
2550
+ outputText = formatBracketAwareText() ?? formatText(truncation.content, startLineDisplay);
2551
+ outputText += reachedEof
2552
+ ? `\n\n[${totalFileLines - (startLine + userLimitedLines)} more lines in file. Use :${nextOffset} to continue]`
2553
+ : `\n\n[More lines in file (${formatBytes(fileSize)} total; not scanned to EOF). Use :${nextOffset} to continue]`;
2554
+ details = {};
2555
+ sourcePath = absolutePath;
2556
+ } else {
2557
+ // No truncation, no user limit exceeded
2558
+ outputText = formatBracketAwareText() ?? formatText(truncation.content, startLineDisplay);
2559
+ details = {};
2560
+ sourcePath = absolutePath;
2561
+ }
2562
+
2563
+ if (hashContext?.tag) {
2564
+ recordSeenLinesFromBody(this.session, absolutePath, hashContext.tag, outputText);
2565
+ }
2566
+
2567
+ if (capturedDisplayContent) {
2568
+ details.displayContent = capturedDisplayContent;
2569
+ }
2570
+
2571
+ if (!firstLineExceedsLimit && collectedLines.length > 0) {
2572
+ const blocks = scanConflictLines(collectedLines, startLineDisplay);
2573
+ if (blocks.length > 0) {
2574
+ const history = getConflictHistory(this.session);
2575
+ const displayPathForWarning = formatPathRelativeToCwd(absolutePath, this.session.cwd);
2576
+ const entries = blocks.map(block =>
2577
+ history.register({
2578
+ absolutePath,
2579
+ displayPath: displayPathForWarning,
2580
+ ...block,
2581
+ }),
2582
+ );
2583
+ // Cheap full-file scan only when the window already showed
2584
+ // at least one conflict — otherwise pay nothing on clean files.
2585
+ let totalInFile = entries.length;
2586
+ let scanTruncated = false;
2587
+ try {
2588
+ const fileScan = await scanFileForConflicts(absolutePath);
2589
+ totalInFile = Math.max(entries.length, fileScan.blocks.length);
2590
+ scanTruncated = fileScan.scanTruncated;
2591
+ } catch {
2592
+ // Best-effort enrichment; fall back to window-only count.
2593
+ }
2594
+ outputText += formatConflictWarning(entries, {
2595
+ totalInFile,
2596
+ displayPath: displayPathForWarning,
2597
+ scanTruncated,
2598
+ });
2599
+ details.conflictCount = entries.length;
2600
+ }
2601
+ }
2602
+
2603
+ content = [{ type: "text", text: outputText }];
2604
+ }
2605
+ }
2606
+ }
2607
+
2608
+ if (suffixResolution) {
2609
+ details.suffixResolution = suffixResolution;
2610
+ // Inline resolution notice into first text block so the model sees the actual path
2611
+ const notice = `[Path '${suffixResolution.from}' not found; resolved to '${suffixResolution.to}' via suffix match]`;
2612
+ const firstText = content.find((c): c is TextContent => c.type === "text");
2613
+ if (firstText) {
2614
+ firstText.text = `${notice}\n${firstText.text}`;
2615
+ } else {
2616
+ content = [{ type: "text", text: notice }, ...content];
2617
+ }
2618
+ }
2619
+ const resultBuilder = toolResult(details).content(content);
2620
+ if (sourcePath) {
2621
+ resultBuilder.sourcePath(sourcePath);
2622
+ }
2623
+ if (truncationInfo) {
2624
+ resultBuilder.truncation(truncationInfo.result, truncationInfo.options);
2625
+ }
2626
+ if (columnTruncated > 0) {
2627
+ resultBuilder.limits({ columnMax: columnTruncated });
2628
+ }
2629
+ return resultBuilder.done();
2630
+ }
2631
+
2632
+ /**
2633
+ * Render a `conflict://<N>` (or `conflict://<N>/<scope>`) region as
2634
+ * regular file content. The lines are emitted with their original
2635
+ * file line numbers so hashline anchors line up with the source
2636
+ * file, and no truncation footer is appended.
2637
+ */
2638
+ async #readConflictRegion(id: number, scope: ConflictScope | undefined): Promise<AgentToolResult<ReadToolDetails>> {
2639
+ const entry: ConflictEntry | undefined = getConflictHistory(this.session).get(id);
2640
+ if (!entry) {
2641
+ throw new ToolError(
2642
+ `Conflict #${id} not found. Conflict ids are registered when \`read\` surfaces a marker block; re-read the file to get a current id.`,
2643
+ );
2644
+ }
2645
+
2646
+ const region = renderConflictRegion(entry, scope);
2647
+ const displayMode = resolveFileDisplayMode(this.session);
2648
+ const shouldAddHashLines = displayMode.hashLines;
2649
+ const shouldAddLineNumbers = shouldAddHashLines ? false : displayMode.lineNumbers;
2650
+
2651
+ const rawText = region.lines.join("\n");
2652
+ const tag = shouldAddHashLines ? await recordFileSnapshot(this.session, entry.absolutePath) : undefined;
2653
+ const hashContext = tag
2654
+ ? hashlineHeaderContext(formatPathRelativeToCwd(entry.absolutePath, this.session.cwd), tag)
2655
+ : undefined;
2656
+ const formattedBody = formatTextWithMode(rawText, region.startLine, shouldAddHashLines, shouldAddLineNumbers);
2657
+ const formattedText = prependHashlineHeader(formattedBody, hashContext);
2658
+
2659
+ const details: ReadToolDetails = {
2660
+ resolvedPath: entry.absolutePath,
2661
+ displayContent: { text: rawText, startLine: region.startLine },
2662
+ };
2663
+ return toolResult<ReadToolDetails>(details).text(formattedText).sourcePath(entry.absolutePath).done();
2664
+ }
2665
+
2666
+ /**
2667
+ * Implement the `<path>:conflicts` read selector: scan the whole file once, register
2668
+ * every block in the session's conflict history, and return a compact
2669
+ * `#N L_a-L_b` index instead of file content. Designed for heavily
2670
+ * conflicted files where dumping every body would be wasteful.
2671
+ */
2672
+ async #readFileConflicts(
2673
+ absolutePath: string,
2674
+ suffixResolution: { from: string; to: string } | undefined,
2675
+ signal: AbortSignal | undefined,
2676
+ ): Promise<AgentToolResult<ReadToolDetails>> {
2677
+ throwIfAborted(signal);
2678
+ const scan = await scanFileForConflicts(absolutePath);
2679
+ const displayPath = formatPathRelativeToCwd(absolutePath, this.session.cwd);
2680
+ const history = getConflictHistory(this.session);
2681
+ const entries = scan.blocks.map(block =>
2682
+ history.register({
2683
+ absolutePath,
2684
+ displayPath,
2685
+ ...block,
2686
+ }),
2687
+ );
2688
+
2689
+ const summary =
2690
+ entries.length === 0
2691
+ ? `No unresolved git merge conflicts in ${displayPath}.`
2692
+ : formatConflictSummary(entries, { displayPath, scanTruncated: scan.scanTruncated });
2693
+
2694
+ const details: ReadToolDetails = {
2695
+ resolvedPath: absolutePath,
2696
+ suffixResolution,
2697
+ conflictCount: entries.length,
2698
+ };
2699
+ return toolResult<ReadToolDetails>(details).text(summary).sourcePath(absolutePath).done();
2700
+ }
2701
+
2702
+ /**
2703
+ * Handle internal URLs (agent://, artifact://, memory://, skill://, rule://, local://, mcp://).
2704
+ * Supports pagination via offset/limit but rejects them when query extraction is used.
2705
+ */
2706
+ async #handleInternalUrl(
2707
+ url: string,
2708
+ parsedSel: ParsedSelector,
2709
+ signal?: AbortSignal,
2710
+ ): Promise<AgentToolResult<ReadToolDetails>> {
2711
+ const internalRouter = InternalUrlRouter.instance();
2712
+
2713
+ // Check if URL has query extraction (agent:// only).
2714
+ // Use parseInternalUrl which handles colons in host (namespaced skills).
2715
+ let urlMeta: InternalUrl;
2716
+ try {
2717
+ urlMeta = parseInternalUrl(url);
2718
+ } catch (e) {
2719
+ throw new ToolError(e instanceof Error ? e.message : String(e));
2720
+ }
2721
+ const scheme = urlMeta.protocol.replace(/:$/, "").toLowerCase();
2722
+ let hasExtraction = false;
2723
+ if (scheme === "agent") {
2724
+ const hasPathExtraction = urlMeta.pathname && urlMeta.pathname !== "/" && urlMeta.pathname !== "";
2725
+ const queryParam = urlMeta.searchParams.get("q");
2726
+ const hasQueryExtraction = queryParam !== null && queryParam !== "";
2727
+ hasExtraction = hasPathExtraction || hasQueryExtraction;
2728
+ }
2729
+
2730
+ // Reject line selectors when query extraction is used
2731
+ if (hasExtraction && parsedSel.kind !== "none" && parsedSel.kind !== "raw") {
2732
+ throw new ToolError("Cannot combine query extraction with line selectors");
2733
+ }
2734
+
2735
+ // Resolve the internal URL
2736
+ const resource = await internalRouter.resolve(url, {
2737
+ cwd: this.session.cwd,
2738
+ settings: this.session.settings,
2739
+ signal,
2740
+ localProtocolOptions: this.session.localProtocolOptions,
2741
+ });
2742
+ const details: ReadToolDetails = { resolvedPath: resource.sourcePath, contentType: resource.contentType };
2743
+
2744
+ // If extraction was used, return directly (no pagination)
2745
+ if (hasExtraction) {
2746
+ return toolResult(details).text(resource.content).sourceInternal(url).done();
2747
+ }
2748
+
2749
+ const raw = isRawSelector(parsedSel);
2750
+ if (isMultiRange(parsedSel) && parsedSel.kind === "lines") {
2751
+ return this.#buildInMemoryMultiRangeResult(resource.content, parsedSel.ranges, {
2752
+ details,
2753
+ sourcePath: resource.sourcePath,
2754
+ sourceInternal: url,
2755
+ entityLabel: "resource",
2756
+ immutable: resource.immutable,
2757
+ raw,
2758
+ });
2759
+ }
2760
+
2761
+ const { offset, limit } = selToOffsetLimit(parsedSel);
2762
+ return this.#buildInMemoryTextResult(resource.content, offset, limit, {
2763
+ details,
2764
+ sourcePath: resource.sourcePath,
2765
+ sourceInternal: url,
2766
+ entityLabel: "resource",
2767
+ ignoreResultLimits: scheme === "skill",
2768
+ immutable: resource.immutable,
2769
+ raw,
2770
+ });
2771
+ }
2772
+
2773
+ /** Read directory contents as a formatted listing */
2774
+ async #readDirectory(
2775
+ absolutePath: string,
2776
+ offset: number | undefined,
2777
+ limit: number | undefined,
2778
+ signal?: AbortSignal,
2779
+ ): Promise<AgentToolResult<ReadToolDetails>> {
2780
+ const READ_DIRECTORY_MAX_DEPTH = 2;
2781
+ const READ_DIRECTORY_CHILD_LIMIT = 12;
2782
+
2783
+ throwIfAborted(signal);
2784
+ let tree: DirectoryTree;
2785
+ try {
2786
+ tree = await buildDirectoryTree(absolutePath, {
2787
+ maxDepth: READ_DIRECTORY_MAX_DEPTH,
2788
+ perDirLimit: READ_DIRECTORY_CHILD_LIMIT,
2789
+ rootLimit: null,
2790
+ // `lineCap` truncates the rendered tree itself, so apply it only when the caller
2791
+ // did not request an offset — otherwise we'd cap the first N lines before slicing.
2792
+ lineCap: offset === undefined && limit !== undefined ? limit : null,
2793
+ });
2794
+ } catch (error) {
2795
+ const message = error instanceof Error ? error.message : String(error);
2796
+ throw new ToolError(`Cannot read directory: ${message}`);
2797
+ }
2798
+ throwIfAborted(signal);
2799
+
2800
+ const output = tree.totalLines <= 1 ? "(empty directory)" : tree.rendered;
2801
+ const details: ReadToolDetails = {
2802
+ isDirectory: true,
2803
+ resolvedPath: tree.rootPath,
2804
+ };
2805
+
2806
+ // Slice the rendered listing when the caller passed an offset/limit. We do this
2807
+ // instead of passing the selector down to `buildDirectoryTree` because the tree
2808
+ // builder lays out entries hierarchically (per-dir caps, recent-then-elided
2809
+ // summaries); line-based slicing operates on the formatted text and matches what
2810
+ // users expect from `:N-M` on long listings.
2811
+ const wantsSlice = offset !== undefined || limit !== undefined;
2812
+ if (wantsSlice) {
2813
+ const allLines = output.split("\n");
2814
+ const start = offset ? Math.max(0, offset - 1) : 0;
2815
+ if (start >= allLines.length) {
2816
+ const suggestion =
2817
+ allLines.length === 0
2818
+ ? "The listing is empty."
2819
+ : `Use :1 to read from the start, or :${allLines.length} to read the last line.`;
2820
+ return toolResult(details)
2821
+ .text(`Line ${start + 1} is beyond end of listing (${allLines.length} lines total). ${suggestion}`)
2822
+ .sourcePath(tree.rootPath)
2823
+ .done();
2824
+ }
2825
+ const end = limit !== undefined ? Math.min(start + limit, allLines.length) : allLines.length;
2826
+ const sliced = allLines.slice(start, end).join("\n");
2827
+ const resultBuilder = toolResult(details).sourcePath(tree.rootPath);
2828
+ let text = sliced;
2829
+ if (end < allLines.length) {
2830
+ const remaining = allLines.length - end;
2831
+ text += `\n\n[${remaining} more lines in listing. Use :${end + 1} to continue]`;
2832
+ }
2833
+ resultBuilder.text(text);
2834
+ if (tree.truncated) {
2835
+ resultBuilder.limits({ resultLimit: 1 });
2836
+ }
2837
+ return resultBuilder.done();
2838
+ }
2839
+
2840
+ const truncation = truncateHead(output, { maxLines: Number.MAX_SAFE_INTEGER });
2841
+ const resultBuilder = toolResult(details).text(truncation.content).sourcePath(tree.rootPath);
2842
+ if (tree.truncated) {
2843
+ resultBuilder.limits({ resultLimit: 1 });
2844
+ }
2845
+ if (truncation.truncated) {
2846
+ resultBuilder.truncation(truncation, { direction: "head" });
2847
+ details.truncation = truncation;
2848
+ }
2849
+
2850
+ return resultBuilder.done();
2851
+ }
2852
+ }
2853
+
2854
+ // =============================================================================
2855
+ // TUI Renderer
2856
+ // =============================================================================
2857
+
2858
+ interface ReadRenderArgs {
2859
+ path?: string;
2860
+ file_path?: string;
2861
+ sel?: string;
2862
+ // Legacy fields from old schema — tolerated for in-flight tool calls during transition
2863
+ offset?: number;
2864
+ limit?: number;
2865
+ raw?: boolean;
2866
+ }
2867
+
2868
+ const INTERNAL_URL_LIKE_RE = /^[a-z][a-z0-9+.-]*:\/\//i;
2869
+
2870
+ function splitReadRenderPath(rawPath: string): { path: string; sel?: string } {
2871
+ if (INTERNAL_URL_LIKE_RE.test(rawPath)) {
2872
+ const internal = splitInternalUrlSel(rawPath);
2873
+ if (internal.sel) return internal;
2874
+ }
2875
+ return splitPathAndSel(rawPath);
2876
+ }
2877
+
2878
+ function firstReadSelectorLine(sel: string | undefined): number | undefined {
2879
+ if (!sel) return undefined;
2880
+ try {
2881
+ const parsed = parseSel(sel);
2882
+ if (parsed.kind !== "lines") return undefined;
2883
+ return parsed.ranges[0].startLine;
2884
+ } catch {
2885
+ return undefined;
2886
+ }
2887
+ }
2888
+
2889
+ /** Absolute fs path the read result actually resolved to, used as the OSC 8 link
2890
+ * target when the structured `resolvedPath` isn't set (the common plain-file and
2891
+ * image reads only record the path in `meta.source`). URL/internal sources are
2892
+ * not fs paths, so only `type: "path"` qualifies. */
2893
+ function readSourceFsPath(details: ReadToolDetails | undefined): string | undefined {
2894
+ const source = details?.meta?.source;
2895
+ return source?.type === "path" ? source.value : undefined;
2896
+ }
2897
+
2898
+ function formatReadPathLink(
2899
+ rawPath: string,
2900
+ options: {
2901
+ resolvedPath?: string;
2902
+ sourcePath?: string;
2903
+ suffixResolution?: { from: string; to: string };
2904
+ offset?: number;
2905
+ fallbackLabel?: string;
2906
+ },
2907
+ ): string {
2908
+ const split = splitReadRenderPath(rawPath);
2909
+ const basePath = split.path || rawPath;
2910
+ const selectorSuffix = split.sel ? `:${split.sel}` : "";
2911
+ const plainDisplayPath = options.suffixResolution
2912
+ ? shortenPath(options.suffixResolution.to)
2913
+ : shortenPath(basePath || options.resolvedPath || options.fallbackLabel || rawPath);
2914
+ const absoluteInputPath = path.isAbsolute(basePath) ? basePath : undefined;
2915
+ const target =
2916
+ options.resolvedPath ?? options.sourcePath ?? tryResolveInternalUrlSync(basePath) ?? absoluteInputPath;
2917
+ const line = firstReadSelectorLine(split.sel) ?? options.offset;
2918
+ const linkOptions = line !== undefined ? { line } : undefined;
2919
+ const linkedPath = target ? fileHyperlink(target, plainDisplayPath, linkOptions) : plainDisplayPath;
2920
+ return `${linkedPath}${selectorSuffix}`;
2921
+ }
2922
+
2923
+ export const readToolRenderer = {
2924
+ renderCall(args: ReadRenderArgs, _options: RenderResultOptions, uiTheme: Theme): Component {
2925
+ if (isReadableUrlPath(args.file_path || args.path || "")) {
2926
+ return renderReadUrlCall(args, _options, uiTheme);
2927
+ }
2928
+
2929
+ const rawPath = args.file_path || args.path || "";
2930
+ const offset = args.offset;
2931
+ const limit = args.limit;
2932
+
2933
+ let pathDisplay = formatReadPathLink(rawPath, { offset, fallbackLabel: "…" }) || "…";
2934
+ if (offset !== undefined || limit !== undefined) {
2935
+ const startLine = offset ?? 1;
2936
+ const endLine = limit !== undefined ? startLine + limit - 1 : "";
2937
+ pathDisplay += `:${startLine}${endLine ? `-${endLine}` : ""}`;
2938
+ }
2939
+
2940
+ const text = renderStatusLine({ icon: "pending", title: "Read", description: pathDisplay }, uiTheme);
2941
+ return new Text(text, 0, 0);
2942
+ },
2943
+
2944
+ renderResult(
2945
+ result: { content: Array<{ type: string; text?: string }>; details?: ReadToolDetails; isError?: boolean },
2946
+ options: RenderResultOptions,
2947
+ uiTheme: Theme,
2948
+ args?: ReadRenderArgs,
2949
+ ): Component {
2950
+ const urlDetails = result.details as ReadUrlToolDetails | undefined;
2951
+ if (urlDetails?.kind === "url" || isReadableUrlPath(args?.file_path || args?.path || "")) {
2952
+ return renderReadUrlResult(
2953
+ result as {
2954
+ content: Array<{ type: string; text?: string }>;
2955
+ details?: ReadUrlToolDetails;
2956
+ isError?: boolean;
2957
+ },
2958
+ options,
2959
+ uiTheme,
2960
+ );
2961
+ }
2962
+
2963
+ if (result.isError) {
2964
+ const rawErrorText = result.content?.find(c => c.type === "text")?.text ?? "";
2965
+ const errorText = (rawErrorText || "Unknown error").replace(/^Error:\s*/, "");
2966
+ const rawPath = args?.file_path || args?.path || "";
2967
+ const filePath =
2968
+ formatReadPathLink(rawPath, { offset: args?.offset, sourcePath: readSourceFsPath(result.details) }) ||
2969
+ shortenPath(rawPath);
2970
+ let title = filePath ? `Read ${filePath}` : "Read";
2971
+ if (args?.offset !== undefined || args?.limit !== undefined) {
2972
+ const startLine = args.offset ?? 1;
2973
+ const endLine = args.limit !== undefined ? startLine + args.limit - 1 : "";
2974
+ title += `:${startLine}${endLine ? `-${endLine}` : ""}`;
2975
+ }
2976
+ const header = renderStatusLine({ icon: "error", title }, uiTheme);
2977
+ const errorLines = errorText.split("\n").map(line => uiTheme.fg("error", replaceTabs(line)));
2978
+ const outputBlock = new CachedOutputBlock();
2979
+ return markFramedBlockComponent({
2980
+ render: (width: number) =>
2981
+ outputBlock.render({ header, state: "error", sections: [{ lines: errorLines }], width }, uiTheme),
2982
+ invalidate: () => outputBlock.invalidate(),
2983
+ });
2984
+ }
2985
+ const details = result.details;
2986
+ const rawText = result.content?.find(c => c.type === "text")?.text ?? "";
2987
+ // Prefer structured `displayContent` from details when available so the TUI
2988
+ // shows clean file content (no model-only hashline anchors) without parsing the formatted text.
2989
+ // Fall back to the raw text, but strip the LLM-facing notice so it doesn't
2990
+ // echo next to the styled warning line below.
2991
+ const contentText = details?.displayContent?.text ?? stripOutputNotice(rawText, details?.meta);
2992
+ const imageContent = result.content?.find(c => c.type === "image");
2993
+ const rawPath = args?.file_path || args?.path || "";
2994
+ const renderPath = splitReadRenderPath(rawPath);
2995
+ const lang = getLanguageFromPath(renderPath.path);
2996
+
2997
+ const warningLines: string[] = [];
2998
+ const truncation = details?.meta?.truncation;
2999
+ const fallback = details?.truncation;
3000
+ if (details?.resolvedPath) {
3001
+ warningLines.push(uiTheme.fg("dim", wrapBrackets(`Resolved path: ${details.resolvedPath}`, uiTheme)));
3002
+ }
3003
+ if (truncation) {
3004
+ if (fallback?.firstLineExceedsLimit) {
3005
+ let warning = `First line exceeds ${formatBytes(fallback.outputBytes ?? fallback.totalBytes)} limit`;
3006
+ if (truncation.artifactId) {
3007
+ warning += `. ${formatFullOutputReference(truncation.artifactId)}`;
3008
+ }
3009
+ warningLines.push(uiTheme.fg("warning", wrapBrackets(warning, uiTheme)));
3010
+ } else {
3011
+ const warning = formatStyledTruncationWarning(details?.meta, uiTheme);
3012
+ if (warning) warningLines.push(warning);
3013
+ }
3014
+ }
3015
+
3016
+ if (imageContent) {
3017
+ const suffix = details?.suffixResolution;
3018
+ const displayPath = formatReadPathLink(rawPath, {
3019
+ resolvedPath: details?.resolvedPath,
3020
+ sourcePath: readSourceFsPath(details),
3021
+ suffixResolution: suffix,
3022
+ fallbackLabel: "image",
3023
+ });
3024
+ const correction = suffix ? ` ${uiTheme.fg("dim", `(corrected from ${shortenPath(suffix.from)})`)}` : "";
3025
+ const header = renderStatusLine(
3026
+ { icon: suffix ? "warning" : "success", title: "Read", description: `${displayPath}${correction}` },
3027
+ uiTheme,
3028
+ );
3029
+ const detailLines = contentText ? contentText.split("\n").map(line => uiTheme.fg("toolOutput", line)) : [];
3030
+ const lines = [...detailLines, ...warningLines];
3031
+ const outputBlock = new CachedOutputBlock();
3032
+ return markFramedBlockComponent({
3033
+ render: (width: number) =>
3034
+ outputBlock.render(
3035
+ {
3036
+ header,
3037
+ state: "success",
3038
+ sections: [
3039
+ {
3040
+ label: uiTheme.fg("toolTitle", "Details"),
3041
+ lines: lines.length > 0 ? lines : [uiTheme.fg("dim", "(image)")],
3042
+ },
3043
+ ],
3044
+ width,
3045
+ },
3046
+ uiTheme,
3047
+ ),
3048
+ invalidate: () => outputBlock.invalidate(),
3049
+ });
3050
+ }
3051
+
3052
+ const suffix = details?.suffixResolution;
3053
+ // resolvedPath is the absolute fs path when a read resolved/corrected the
3054
+ // input (suffix match, internal URL, archive/sqlite/notebook); plain file
3055
+ // reads only record the absolute path in meta.source, so fall back to that
3056
+ // (and then to a sync internal-URL resolver) to keep the title clickable.
3057
+ const displayPath = formatReadPathLink(rawPath, {
3058
+ resolvedPath: details?.resolvedPath,
3059
+ sourcePath: readSourceFsPath(details),
3060
+ suffixResolution: suffix,
3061
+ offset: args?.offset,
3062
+ });
3063
+ const correction = suffix ? ` ${uiTheme.fg("dim", `(corrected from ${shortenPath(suffix.from)})`)}` : "";
3064
+ let title = displayPath ? `Read ${displayPath}${correction}` : "Read";
3065
+ if (args?.offset !== undefined || args?.limit !== undefined) {
3066
+ const startLine = args.offset ?? 1;
3067
+ const endLine = args.limit !== undefined ? startLine + args.limit - 1 : "";
3068
+ title += `:${startLine}${endLine ? `-${endLine}` : ""}`;
3069
+ }
3070
+ if (details?.summary) {
3071
+ title += ` (summary: ${details.summary.elidedSpans} elided span${details.summary.elidedSpans === 1 ? "" : "s"})`;
3072
+ }
3073
+ if (details?.conflictCount && details.conflictCount > 0) {
3074
+ const n = details.conflictCount;
3075
+ title += ` ${uiTheme.fg("warning", `(⚠ ${n} conflict${n === 1 ? "" : "s"})`)}`;
3076
+ }
3077
+ const rawRequested = args?.raw === true || isRawSelector(parseSel(renderPath.sel));
3078
+ const isMarkdown = details?.contentType === "text/markdown" && !rawRequested;
3079
+ let cachedWidth: number | undefined;
3080
+ let cachedExpanded: boolean | undefined;
3081
+ let cachedLines: string[] | undefined;
3082
+ return markFramedBlockComponent({
3083
+ render: (width: number) => {
3084
+ const expanded = options.expanded;
3085
+ if (cachedLines && cachedWidth === width && cachedExpanded === expanded) return cachedLines;
3086
+ cachedLines = isMarkdown
3087
+ ? renderMarkdownCell(
3088
+ {
3089
+ content: contentText,
3090
+ title,
3091
+ status: "complete",
3092
+ output: warningLines.length > 0 ? warningLines.join("\n") : undefined,
3093
+ expanded,
3094
+ width,
3095
+ },
3096
+ uiTheme,
3097
+ )
3098
+ : renderCodeCell(
3099
+ {
3100
+ code: contentText,
3101
+ language: lang,
3102
+ title,
3103
+ status: "complete",
3104
+ output: warningLines.length > 0 ? warningLines.join("\n") : undefined,
3105
+ expanded,
3106
+ codeStartLine: details?.displayContent?.startLine,
3107
+ codeLineNumbers: details?.displayContent?.lineNumbers,
3108
+ width,
3109
+ },
3110
+ uiTheme,
3111
+ );
3112
+ cachedWidth = width;
3113
+ cachedExpanded = expanded;
3114
+ return cachedLines;
3115
+ },
3116
+ invalidate: () => {
3117
+ cachedWidth = undefined;
3118
+ cachedExpanded = undefined;
3119
+ cachedLines = undefined;
3120
+ },
3121
+ });
3122
+ },
3123
+ mergeCallAndResult: true,
3124
+ };