@wahack/pi-coding-agent 15.11.0

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