@gajae-code/coding-agent 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1775) hide show
  1. package/CHANGELOG.md +8662 -0
  2. package/README.md +37 -0
  3. package/dist/types/async/index.d.ts +2 -0
  4. package/dist/types/async/job-manager.d.ts +101 -0
  5. package/dist/types/async/support.d.ts +2 -0
  6. package/dist/types/autoresearch/dashboard.d.ts +4 -0
  7. package/dist/types/autoresearch/git.d.ts +36 -0
  8. package/dist/types/autoresearch/helpers.d.ts +24 -0
  9. package/dist/types/autoresearch/index.d.ts +2 -0
  10. package/dist/types/autoresearch/state.d.ts +17 -0
  11. package/dist/types/autoresearch/storage.d.ts +142 -0
  12. package/dist/types/autoresearch/tools/init-experiment.d.ts +31 -0
  13. package/dist/types/autoresearch/tools/log-experiment.d.ts +23 -0
  14. package/dist/types/autoresearch/tools/run-experiment.d.ts +8 -0
  15. package/dist/types/autoresearch/tools/update-notes.d.ts +12 -0
  16. package/dist/types/autoresearch/types.d.ts +154 -0
  17. package/dist/types/capability/context-file.d.ts +17 -0
  18. package/dist/types/capability/extension-module.d.ts +15 -0
  19. package/dist/types/capability/extension.d.ts +28 -0
  20. package/dist/types/capability/fs.d.ts +20 -0
  21. package/dist/types/capability/hook.d.ts +19 -0
  22. package/dist/types/capability/index.d.ts +80 -0
  23. package/dist/types/capability/instruction.d.ts +17 -0
  24. package/dist/types/capability/mcp.d.ts +45 -0
  25. package/dist/types/capability/prompt.d.ts +15 -0
  26. package/dist/types/capability/rule.d.ts +61 -0
  27. package/dist/types/capability/settings.d.ts +15 -0
  28. package/dist/types/capability/skill.d.ts +36 -0
  29. package/dist/types/capability/slash-command.d.ts +17 -0
  30. package/dist/types/capability/ssh.d.ts +23 -0
  31. package/dist/types/capability/system-prompt.d.ts +15 -0
  32. package/dist/types/capability/tool.d.ts +19 -0
  33. package/dist/types/capability/types.d.ts +154 -0
  34. package/dist/types/cli/agents-cli.d.ts +12 -0
  35. package/dist/types/cli/args.d.ts +53 -0
  36. package/dist/types/cli/auth-broker-cli.d.ts +25 -0
  37. package/dist/types/cli/auth-gateway-cli.d.ts +18 -0
  38. package/dist/types/cli/classify-install-target.d.ts +18 -0
  39. package/dist/types/cli/commands/init-xdg.d.ts +1 -0
  40. package/dist/types/cli/config-cli.d.ts +22 -0
  41. package/dist/types/cli/file-processor.d.ts +11 -0
  42. package/dist/types/cli/grep-cli.d.ts +17 -0
  43. package/dist/types/cli/initial-message.d.ts +17 -0
  44. package/dist/types/cli/list-models.d.ts +30 -0
  45. package/dist/types/cli/plugin-cli.d.ts +32 -0
  46. package/dist/types/cli/read-cli.d.ts +4 -0
  47. package/dist/types/cli/session-picker.d.ts +3 -0
  48. package/dist/types/cli/setup-cli.d.ts +31 -0
  49. package/dist/types/cli/shell-cli.d.ts +8 -0
  50. package/dist/types/cli/ssh-cli.d.ts +21 -0
  51. package/dist/types/cli/stats-cli.d.ts +17 -0
  52. package/dist/types/cli/update-cli.d.ts +38 -0
  53. package/dist/types/cli/web-search-cli.d.ts +20 -0
  54. package/dist/types/cli/worktree-cli.d.ts +26 -0
  55. package/dist/types/cli.d.ts +3 -0
  56. package/dist/types/commands/acp.d.ts +12 -0
  57. package/dist/types/commands/agents.d.ts +34 -0
  58. package/dist/types/commands/auth-broker.d.ts +54 -0
  59. package/dist/types/commands/auth-gateway.d.ts +32 -0
  60. package/dist/types/commands/codex-native-hook.d.ts +6 -0
  61. package/dist/types/commands/commit.d.ts +27 -0
  62. package/dist/types/commands/config.d.ts +30 -0
  63. package/dist/types/commands/deep-interview.d.ts +7 -0
  64. package/dist/types/commands/gjc-runtime-bridge.d.ts +6 -0
  65. package/dist/types/commands/grep.d.ts +42 -0
  66. package/dist/types/commands/launch.d.ts +121 -0
  67. package/dist/types/commands/plugin.d.ts +52 -0
  68. package/dist/types/commands/question.d.ts +7 -0
  69. package/dist/types/commands/ralplan.d.ts +7 -0
  70. package/dist/types/commands/read.d.ts +15 -0
  71. package/dist/types/commands/setup.d.ts +48 -0
  72. package/dist/types/commands/shell.d.ts +21 -0
  73. package/dist/types/commands/ssh.d.ts +48 -0
  74. package/dist/types/commands/state.d.ts +7 -0
  75. package/dist/types/commands/stats.d.ts +25 -0
  76. package/dist/types/commands/team.d.ts +24 -0
  77. package/dist/types/commands/ultragoal.d.ts +7 -0
  78. package/dist/types/commands/update.d.ts +20 -0
  79. package/dist/types/commands/web-search.d.ts +33 -0
  80. package/dist/types/commands/worktree.d.ts +34 -0
  81. package/dist/types/commit/agentic/agent.d.ts +31 -0
  82. package/dist/types/commit/agentic/fallback.d.ts +5 -0
  83. package/dist/types/commit/agentic/index.d.ts +2 -0
  84. package/dist/types/commit/agentic/state.d.ts +58 -0
  85. package/dist/types/commit/agentic/tools/analyze-file.d.ts +19 -0
  86. package/dist/types/commit/agentic/tools/git-file-diff.d.ts +10 -0
  87. package/dist/types/commit/agentic/tools/git-hunk.d.ts +9 -0
  88. package/dist/types/commit/agentic/tools/git-overview.d.ts +9 -0
  89. package/dist/types/commit/agentic/tools/index.d.ts +16 -0
  90. package/dist/types/commit/agentic/tools/propose-changelog.d.ts +28 -0
  91. package/dist/types/commit/agentic/tools/propose-commit.d.ts +36 -0
  92. package/dist/types/commit/agentic/tools/recent-commits.d.ts +7 -0
  93. package/dist/types/commit/agentic/tools/schemas.d.ts +27 -0
  94. package/dist/types/commit/agentic/tools/split-commit.d.ts +53 -0
  95. package/dist/types/commit/agentic/topo-sort.d.ts +4 -0
  96. package/dist/types/commit/agentic/trivial.d.ts +7 -0
  97. package/dist/types/commit/agentic/validation.d.ts +20 -0
  98. package/dist/types/commit/analysis/conventional.d.ts +22 -0
  99. package/dist/types/commit/analysis/index.d.ts +4 -0
  100. package/dist/types/commit/analysis/scope.d.ts +6 -0
  101. package/dist/types/commit/analysis/summary.d.ts +19 -0
  102. package/dist/types/commit/analysis/validation.d.ts +8 -0
  103. package/dist/types/commit/changelog/detect.d.ts +2 -0
  104. package/dist/types/commit/changelog/generate.d.ts +30 -0
  105. package/dist/types/commit/changelog/index.d.ts +30 -0
  106. package/dist/types/commit/changelog/parse.d.ts +2 -0
  107. package/dist/types/commit/cli.d.ts +3 -0
  108. package/dist/types/commit/git/diff.d.ts +5 -0
  109. package/dist/types/commit/index.d.ts +4 -0
  110. package/dist/types/commit/map-reduce/index.d.ts +28 -0
  111. package/dist/types/commit/map-reduce/map-phase.d.ts +17 -0
  112. package/dist/types/commit/map-reduce/reduce-phase.d.ts +13 -0
  113. package/dist/types/commit/map-reduce/utils.d.ts +2 -0
  114. package/dist/types/commit/message.d.ts +2 -0
  115. package/dist/types/commit/model-selection.d.ts +15 -0
  116. package/dist/types/commit/pipeline.d.ts +5 -0
  117. package/dist/types/commit/shared-llm.d.ts +54 -0
  118. package/dist/types/commit/types.d.ts +78 -0
  119. package/dist/types/commit/utils/exclusions.d.ts +4 -0
  120. package/dist/types/commit/utils.d.ts +20 -0
  121. package/dist/types/config/config-file.d.ts +58 -0
  122. package/dist/types/config/file-lock.d.ts +6 -0
  123. package/dist/types/config/keybindings.d.ts +334 -0
  124. package/dist/types/config/model-equivalence.d.ts +24 -0
  125. package/dist/types/config/model-registry.d.ts +390 -0
  126. package/dist/types/config/model-resolver.d.ts +230 -0
  127. package/dist/types/config/models-config-schema.d.ts +504 -0
  128. package/dist/types/config/prompt-templates.d.ts +32 -0
  129. package/dist/types/config/resolve-config-value.d.ts +17 -0
  130. package/dist/types/config/settings-schema.d.ts +3337 -0
  131. package/dist/types/config/settings.d.ts +132 -0
  132. package/dist/types/config/skill-settings-defaults.d.ts +14 -0
  133. package/dist/types/config.d.ts +66 -0
  134. package/dist/types/cursor.d.ts +24 -0
  135. package/dist/types/dap/client.d.ts +38 -0
  136. package/dist/types/dap/config.d.ts +6 -0
  137. package/dist/types/dap/index.d.ts +4 -0
  138. package/dist/types/dap/session.d.ts +108 -0
  139. package/dist/types/dap/types.d.ts +524 -0
  140. package/dist/types/debug/index.d.ts +15 -0
  141. package/dist/types/debug/log-formatting.d.ts +4 -0
  142. package/dist/types/debug/log-viewer.d.ts +68 -0
  143. package/dist/types/debug/profiler.d.ts +24 -0
  144. package/dist/types/debug/raw-sse-buffer.d.ts +44 -0
  145. package/dist/types/debug/raw-sse.d.ts +16 -0
  146. package/dist/types/debug/report-bundle.d.ts +55 -0
  147. package/dist/types/debug/system-info.d.ts +26 -0
  148. package/dist/types/defaults/gjc-defaults.d.ts +44 -0
  149. package/dist/types/discovery/agents-md.d.ts +1 -0
  150. package/dist/types/discovery/agents.d.ts +12 -0
  151. package/dist/types/discovery/builtin.d.ts +1 -0
  152. package/dist/types/discovery/claude-plugins.d.ts +1 -0
  153. package/dist/types/discovery/claude.d.ts +1 -0
  154. package/dist/types/discovery/cline.d.ts +1 -0
  155. package/dist/types/discovery/codex.d.ts +1 -0
  156. package/dist/types/discovery/cursor.d.ts +16 -0
  157. package/dist/types/discovery/gemini.d.ts +1 -0
  158. package/dist/types/discovery/github.d.ts +1 -0
  159. package/dist/types/discovery/helpers.d.ts +268 -0
  160. package/dist/types/discovery/index.d.ts +48 -0
  161. package/dist/types/discovery/mcp-json.d.ts +1 -0
  162. package/dist/types/discovery/opencode.d.ts +1 -0
  163. package/dist/types/discovery/plugin-dir-roots.d.ts +15 -0
  164. package/dist/types/discovery/ssh.d.ts +1 -0
  165. package/dist/types/discovery/substitute-plugin-root.d.ts +5 -0
  166. package/dist/types/discovery/vscode.d.ts +1 -0
  167. package/dist/types/discovery/windsurf.d.ts +13 -0
  168. package/dist/types/edit/apply-patch/index.d.ts +35 -0
  169. package/dist/types/edit/apply-patch/parser.d.ts +34 -0
  170. package/dist/types/edit/diff.d.ts +59 -0
  171. package/dist/types/edit/file-read-cache.d.ts +25 -0
  172. package/dist/types/edit/index.d.ts +58 -0
  173. package/dist/types/edit/modes/apply-patch.d.ts +24 -0
  174. package/dist/types/edit/modes/patch.d.ts +99 -0
  175. package/dist/types/edit/modes/replace.d.ts +142 -0
  176. package/dist/types/edit/normalize.d.ts +43 -0
  177. package/dist/types/edit/notebook.d.ts +23 -0
  178. package/dist/types/edit/read-file.d.ts +2 -0
  179. package/dist/types/edit/renderer.d.ts +110 -0
  180. package/dist/types/edit/streaming.d.ts +66 -0
  181. package/dist/types/eval/backend.d.ts +40 -0
  182. package/dist/types/eval/index.d.ts +4 -0
  183. package/dist/types/eval/js/context-manager.d.ts +24 -0
  184. package/dist/types/eval/js/executor.d.ts +28 -0
  185. package/dist/types/eval/js/index.d.ts +10 -0
  186. package/dist/types/eval/js/shared/helpers.d.ts +38 -0
  187. package/dist/types/eval/js/shared/indirect-eval.d.ts +14 -0
  188. package/dist/types/eval/js/shared/prelude.d.ts +1 -0
  189. package/dist/types/eval/js/shared/rewrite-imports.d.ts +6 -0
  190. package/dist/types/eval/js/shared/runtime.d.ts +47 -0
  191. package/dist/types/eval/js/shared/types.d.ts +24 -0
  192. package/dist/types/eval/js/tool-bridge.d.ts +18 -0
  193. package/dist/types/eval/js/worker-core.d.ts +5 -0
  194. package/dist/types/eval/js/worker-entry.d.ts +1 -0
  195. package/dist/types/eval/js/worker-protocol.d.ts +77 -0
  196. package/dist/types/eval/py/display.d.ts +25 -0
  197. package/dist/types/eval/py/executor.d.ts +83 -0
  198. package/dist/types/eval/py/index.d.ts +10 -0
  199. package/dist/types/eval/py/kernel.d.ts +61 -0
  200. package/dist/types/eval/py/prelude.d.ts +1 -0
  201. package/dist/types/eval/py/runtime.d.ts +21 -0
  202. package/dist/types/eval/py/tool-bridge.d.ts +20 -0
  203. package/dist/types/eval/types.d.ts +52 -0
  204. package/dist/types/exa/factory.d.ts +13 -0
  205. package/dist/types/exa/index.d.ts +20 -0
  206. package/dist/types/exa/mcp-client.d.ts +45 -0
  207. package/dist/types/exa/render.d.ts +19 -0
  208. package/dist/types/exa/researcher.d.ts +9 -0
  209. package/dist/types/exa/search.d.ts +9 -0
  210. package/dist/types/exa/types.d.ts +155 -0
  211. package/dist/types/exa/websets.d.ts +9 -0
  212. package/dist/types/exec/bash-executor.d.ts +41 -0
  213. package/dist/types/exec/exec.d.ts +25 -0
  214. package/dist/types/exec/idle-timeout-watchdog.d.ts +18 -0
  215. package/dist/types/exec/non-interactive-env.d.ts +1 -0
  216. package/dist/types/export/custom-share.d.ts +20 -0
  217. package/dist/types/export/html/index.d.ts +10 -0
  218. package/dist/types/export/html/template.generated.d.ts +1 -0
  219. package/dist/types/export/html/template.macro.d.ts +5 -0
  220. package/dist/types/export/ttsr.d.ts +44 -0
  221. package/dist/types/extensibility/custom-commands/bundled/ci-green/index.d.ts +9 -0
  222. package/dist/types/extensibility/custom-commands/bundled/review/index.d.ts +10 -0
  223. package/dist/types/extensibility/custom-commands/index.d.ts +2 -0
  224. package/dist/types/extensibility/custom-commands/loader.d.ts +29 -0
  225. package/dist/types/extensibility/custom-commands/types.d.ts +106 -0
  226. package/dist/types/extensibility/custom-tools/index.d.ts +6 -0
  227. package/dist/types/extensibility/custom-tools/loader.d.ts +69 -0
  228. package/dist/types/extensibility/custom-tools/types.d.ts +226 -0
  229. package/dist/types/extensibility/custom-tools/wrapper.d.ts +23 -0
  230. package/dist/types/extensibility/extensions/compact-handler.d.ts +26 -0
  231. package/dist/types/extensibility/extensions/get-commands-handler.d.ts +29 -0
  232. package/dist/types/extensibility/extensions/index.d.ts +8 -0
  233. package/dist/types/extensibility/extensions/loader.d.ts +43 -0
  234. package/dist/types/extensibility/extensions/runner.d.ts +134 -0
  235. package/dist/types/extensibility/extensions/types.d.ts +864 -0
  236. package/dist/types/extensibility/extensions/wrapper.d.ts +54 -0
  237. package/dist/types/extensibility/hooks/index.d.ts +5 -0
  238. package/dist/types/extensibility/hooks/loader.d.ts +89 -0
  239. package/dist/types/extensibility/hooks/runner.d.ts +126 -0
  240. package/dist/types/extensibility/hooks/tool-wrapper.d.ts +25 -0
  241. package/dist/types/extensibility/hooks/types.d.ts +431 -0
  242. package/dist/types/extensibility/plugins/doctor.d.ts +3 -0
  243. package/dist/types/extensibility/plugins/git-url.d.ts +34 -0
  244. package/dist/types/extensibility/plugins/index.d.ts +7 -0
  245. package/dist/types/extensibility/plugins/installer.d.ts +5 -0
  246. package/dist/types/extensibility/plugins/legacy-pi-compat.d.ts +2 -0
  247. package/dist/types/extensibility/plugins/loader.d.ts +31 -0
  248. package/dist/types/extensibility/plugins/manager.d.ts +65 -0
  249. package/dist/types/extensibility/plugins/marketplace/cache.d.ts +41 -0
  250. package/dist/types/extensibility/plugins/marketplace/fetcher.d.ts +48 -0
  251. package/dist/types/extensibility/plugins/marketplace/index.d.ts +6 -0
  252. package/dist/types/extensibility/plugins/marketplace/manager.d.ts +57 -0
  253. package/dist/types/extensibility/plugins/marketplace/registry.d.ts +34 -0
  254. package/dist/types/extensibility/plugins/marketplace/source-resolver.d.ts +28 -0
  255. package/dist/types/extensibility/plugins/marketplace/types.d.ts +130 -0
  256. package/dist/types/extensibility/plugins/parser.d.ts +52 -0
  257. package/dist/types/extensibility/plugins/types.d.ts +149 -0
  258. package/dist/types/extensibility/shared-events.d.ts +279 -0
  259. package/dist/types/extensibility/skills.d.ts +74 -0
  260. package/dist/types/extensibility/slash-commands.d.ts +48 -0
  261. package/dist/types/extensibility/tool-proxy.d.ts +4 -0
  262. package/dist/types/extensibility/typebox.d.ts +155 -0
  263. package/dist/types/extensibility/utils.d.ts +12 -0
  264. package/dist/types/gjc-runtime/goal-mode-request.d.ts +46 -0
  265. package/dist/types/gjc-runtime/launch-tmux.d.ts +45 -0
  266. package/dist/types/gjc-runtime/launch-worktree.d.ts +46 -0
  267. package/dist/types/gjc-runtime/team-runtime.d.ts +204 -0
  268. package/dist/types/gjc-runtime/ultragoal-runtime.d.ts +85 -0
  269. package/dist/types/goals/index.d.ts +3 -0
  270. package/dist/types/goals/runtime.d.ts +68 -0
  271. package/dist/types/goals/state.d.ts +35 -0
  272. package/dist/types/goals/tools/goal-tool.d.ts +128 -0
  273. package/dist/types/hashline/anchors.d.ts +20 -0
  274. package/dist/types/hashline/apply.d.ts +14 -0
  275. package/dist/types/hashline/constants.d.ts +19 -0
  276. package/dist/types/hashline/diff-preview.d.ts +2 -0
  277. package/dist/types/hashline/diff.d.ts +17 -0
  278. package/dist/types/hashline/execute.d.ts +4 -0
  279. package/dist/types/hashline/hash.d.ts +123 -0
  280. package/dist/types/hashline/index.d.ts +13 -0
  281. package/dist/types/hashline/input.d.ts +11 -0
  282. package/dist/types/hashline/parser.d.ts +7 -0
  283. package/dist/types/hashline/prefixes.d.ts +7 -0
  284. package/dist/types/hashline/recovery.d.ts +27 -0
  285. package/dist/types/hashline/stream.d.ts +2 -0
  286. package/dist/types/hashline/types.d.ts +77 -0
  287. package/dist/types/hindsight/backend.d.ts +13 -0
  288. package/dist/types/hindsight/bank.d.ts +54 -0
  289. package/dist/types/hindsight/client.d.ts +224 -0
  290. package/dist/types/hindsight/config.d.ts +51 -0
  291. package/dist/types/hindsight/content.d.ts +70 -0
  292. package/dist/types/hindsight/index.d.ts +8 -0
  293. package/dist/types/hindsight/mental-models.d.ts +125 -0
  294. package/dist/types/hindsight/state.d.ts +99 -0
  295. package/dist/types/hindsight/transcript.d.ts +28 -0
  296. package/dist/types/hooks/codex-native-hooks-config.d.ts +29 -0
  297. package/dist/types/hooks/native-skill-hook.d.ts +16 -0
  298. package/dist/types/hooks/skill-keywords.d.ts +17 -0
  299. package/dist/types/hooks/skill-state.d.ts +80 -0
  300. package/dist/types/index.d.ts +33 -0
  301. package/dist/types/internal-urls/agent-protocol.d.ts +12 -0
  302. package/dist/types/internal-urls/artifact-protocol.d.ts +6 -0
  303. package/dist/types/internal-urls/docs-index.generated.d.ts +2 -0
  304. package/dist/types/internal-urls/gjc-protocol.d.ts +12 -0
  305. package/dist/types/internal-urls/index.d.ts +20 -0
  306. package/dist/types/internal-urls/issue-pr-protocol.d.ts +17 -0
  307. package/dist/types/internal-urls/json-query.d.ts +30 -0
  308. package/dist/types/internal-urls/local-protocol.d.ts +39 -0
  309. package/dist/types/internal-urls/mcp-protocol.d.ts +12 -0
  310. package/dist/types/internal-urls/memory-protocol.d.ts +23 -0
  311. package/dist/types/internal-urls/parse.d.ts +19 -0
  312. package/dist/types/internal-urls/registry-helpers.d.ts +10 -0
  313. package/dist/types/internal-urls/router.d.ts +14 -0
  314. package/dist/types/internal-urls/rule-protocol.d.ts +6 -0
  315. package/dist/types/internal-urls/skill-protocol.d.ts +13 -0
  316. package/dist/types/internal-urls/types.d.ts +105 -0
  317. package/dist/types/lsp/client.d.ts +65 -0
  318. package/dist/types/lsp/clients/biome-client.d.ts +16 -0
  319. package/dist/types/lsp/clients/index.d.ts +19 -0
  320. package/dist/types/lsp/clients/lsp-linter-client.d.ts +16 -0
  321. package/dist/types/lsp/clients/swiftlint-client.d.ts +20 -0
  322. package/dist/types/lsp/config.d.ts +65 -0
  323. package/dist/types/lsp/edits.d.ts +23 -0
  324. package/dist/types/lsp/index.d.ts +130 -0
  325. package/dist/types/lsp/lspmux.d.ts +58 -0
  326. package/dist/types/lsp/render.d.ts +32 -0
  327. package/dist/types/lsp/startup-events.d.ts +11 -0
  328. package/dist/types/lsp/types.d.ts +302 -0
  329. package/dist/types/lsp/utils.d.ts +108 -0
  330. package/dist/types/main.d.ts +50 -0
  331. package/dist/types/memories/index.d.ts +28 -0
  332. package/dist/types/memories/storage.d.ts +110 -0
  333. package/dist/types/memory-backend/index.d.ts +4 -0
  334. package/dist/types/memory-backend/local-backend.d.ts +9 -0
  335. package/dist/types/memory-backend/off-backend.d.ts +7 -0
  336. package/dist/types/memory-backend/resolve.d.ts +15 -0
  337. package/dist/types/memory-backend/types.d.ts +61 -0
  338. package/dist/types/modes/acp/acp-agent.d.ts +61 -0
  339. package/dist/types/modes/acp/acp-client-bridge.d.ts +9 -0
  340. package/dist/types/modes/acp/acp-event-mapper.d.ts +32 -0
  341. package/dist/types/modes/acp/acp-mode.d.ts +5 -0
  342. package/dist/types/modes/acp/index.d.ts +2 -0
  343. package/dist/types/modes/acp/terminal-auth.d.ts +6 -0
  344. package/dist/types/modes/components/agent-dashboard.d.ts +21 -0
  345. package/dist/types/modes/components/assistant-message.d.ts +16 -0
  346. package/dist/types/modes/components/bash-execution.d.ts +29 -0
  347. package/dist/types/modes/components/bordered-loader.d.ts +11 -0
  348. package/dist/types/modes/components/branch-summary-message.d.ts +13 -0
  349. package/dist/types/modes/components/btw-panel.d.ts +16 -0
  350. package/dist/types/modes/components/compaction-summary-message.d.ts +13 -0
  351. package/dist/types/modes/components/countdown-timer.d.ts +14 -0
  352. package/dist/types/modes/components/custom-editor.d.ts +47 -0
  353. package/dist/types/modes/components/custom-message.d.ts +15 -0
  354. package/dist/types/modes/components/diff.d.ts +11 -0
  355. package/dist/types/modes/components/dynamic-border.d.ts +14 -0
  356. package/dist/types/modes/components/eval-execution.d.ts +23 -0
  357. package/dist/types/modes/components/execution-shared.d.ts +46 -0
  358. package/dist/types/modes/components/extensions/extension-dashboard.d.ts +26 -0
  359. package/dist/types/modes/components/extensions/extension-list.d.ts +39 -0
  360. package/dist/types/modes/components/extensions/index.d.ts +8 -0
  361. package/dist/types/modes/components/extensions/inspector-panel.d.ts +8 -0
  362. package/dist/types/modes/components/extensions/state-manager.d.ts +50 -0
  363. package/dist/types/modes/components/extensions/types.d.ts +151 -0
  364. package/dist/types/modes/components/footer.d.ts +30 -0
  365. package/dist/types/modes/components/history-search.d.ts +7 -0
  366. package/dist/types/modes/components/hook-editor.d.ts +18 -0
  367. package/dist/types/modes/components/hook-input.d.ts +15 -0
  368. package/dist/types/modes/components/hook-message.d.ts +15 -0
  369. package/dist/types/modes/components/hook-selector.d.ts +23 -0
  370. package/dist/types/modes/components/index.d.ts +35 -0
  371. package/dist/types/modes/components/keybinding-hints.d.ts +40 -0
  372. package/dist/types/modes/components/login-dialog.d.ts +32 -0
  373. package/dist/types/modes/components/message-frame.d.ts +42 -0
  374. package/dist/types/modes/components/model-selector.d.ts +26 -0
  375. package/dist/types/modes/components/oauth-selector.d.ts +14 -0
  376. package/dist/types/modes/components/plugin-selector.d.ts +26 -0
  377. package/dist/types/modes/components/plugin-settings.d.ts +66 -0
  378. package/dist/types/modes/components/provider-onboarding-selector.d.ts +7 -0
  379. package/dist/types/modes/components/queue-mode-selector.d.ts +9 -0
  380. package/dist/types/modes/components/read-tool-group.d.ts +30 -0
  381. package/dist/types/modes/components/runtime-mcp-add-wizard.d.ts +25 -0
  382. package/dist/types/modes/components/session-observer-overlay.d.ts +11 -0
  383. package/dist/types/modes/components/session-selector.d.ts +30 -0
  384. package/dist/types/modes/components/settings-defs.d.ts +50 -0
  385. package/dist/types/modes/components/settings-selector.d.ts +54 -0
  386. package/dist/types/modes/components/show-images-selector.d.ts +9 -0
  387. package/dist/types/modes/components/skill-hud/render.d.ts +2 -0
  388. package/dist/types/modes/components/skill-message.d.ts +9 -0
  389. package/dist/types/modes/components/status-line/context-thresholds.d.ts +4 -0
  390. package/dist/types/modes/components/status-line/git-utils.d.ts +22 -0
  391. package/dist/types/modes/components/status-line/index.d.ts +4 -0
  392. package/dist/types/modes/components/status-line/presets.d.ts +3 -0
  393. package/dist/types/modes/components/status-line/segments.d.ts +5 -0
  394. package/dist/types/modes/components/status-line/separators.d.ts +3 -0
  395. package/dist/types/modes/components/status-line/token-rate.d.ts +10 -0
  396. package/dist/types/modes/components/status-line/types.d.ts +80 -0
  397. package/dist/types/modes/components/status-line.d.ts +80 -0
  398. package/dist/types/modes/components/theme-selector.d.ts +10 -0
  399. package/dist/types/modes/components/thinking-selector.d.ts +10 -0
  400. package/dist/types/modes/components/todo-reminder.d.ts +13 -0
  401. package/dist/types/modes/components/tool-execution.d.ts +53 -0
  402. package/dist/types/modes/components/tree-selector.d.ts +31 -0
  403. package/dist/types/modes/components/ttsr-notification.d.ts +13 -0
  404. package/dist/types/modes/components/user-message-selector.d.ts +28 -0
  405. package/dist/types/modes/components/user-message.d.ts +7 -0
  406. package/dist/types/modes/components/visual-truncate.d.ts +19 -0
  407. package/dist/types/modes/components/welcome.d.ts +34 -0
  408. package/dist/types/modes/controllers/btw-controller.d.ts +10 -0
  409. package/dist/types/modes/controllers/command-controller-shared.d.ts +47 -0
  410. package/dist/types/modes/controllers/command-controller.d.ts +36 -0
  411. package/dist/types/modes/controllers/event-controller.d.ts +12 -0
  412. package/dist/types/modes/controllers/extension-ui-controller.d.ts +80 -0
  413. package/dist/types/modes/controllers/input-controller.d.ts +35 -0
  414. package/dist/types/modes/controllers/runtime-mcp-command-controller.d.ts +10 -0
  415. package/dist/types/modes/controllers/selector-controller.d.ts +46 -0
  416. package/dist/types/modes/controllers/ssh-command-controller.d.ts +10 -0
  417. package/dist/types/modes/controllers/todo-command-controller.d.ts +7 -0
  418. package/dist/types/modes/emoji-autocomplete.d.ts +16 -0
  419. package/dist/types/modes/index.d.ts +9 -0
  420. package/dist/types/modes/interactive-mode.d.ts +271 -0
  421. package/dist/types/modes/loop-limit.d.ts +22 -0
  422. package/dist/types/modes/oauth-manual-input.d.ts +8 -0
  423. package/dist/types/modes/print-mode.d.ts +27 -0
  424. package/dist/types/modes/prompt-action-autocomplete.d.ts +46 -0
  425. package/dist/types/modes/rpc/host-tools.d.ts +16 -0
  426. package/dist/types/modes/rpc/host-uris.d.ts +38 -0
  427. package/dist/types/modes/rpc/rpc-client.d.ts +249 -0
  428. package/dist/types/modes/rpc/rpc-mode.d.ts +17 -0
  429. package/dist/types/modes/rpc/rpc-types.d.ts +586 -0
  430. package/dist/types/modes/runtime-init.d.ts +21 -0
  431. package/dist/types/modes/session-observer-registry.d.ts +26 -0
  432. package/dist/types/modes/shared.d.ts +15 -0
  433. package/dist/types/modes/theme/defaults/index.d.ts +9559 -0
  434. package/dist/types/modes/theme/mermaid-cache.d.ts +9 -0
  435. package/dist/types/modes/theme/shimmer.d.ts +38 -0
  436. package/dist/types/modes/theme/theme.d.ts +273 -0
  437. package/dist/types/modes/types.d.ts +278 -0
  438. package/dist/types/modes/utils/context-usage.d.ts +47 -0
  439. package/dist/types/modes/utils/hotkeys-markdown.d.ts +5 -0
  440. package/dist/types/modes/utils/keybinding-matchers.d.ts +10 -0
  441. package/dist/types/modes/utils/tools-markdown.d.ts +5 -0
  442. package/dist/types/modes/utils/ui-helpers.d.ts +53 -0
  443. package/dist/types/plan-mode/approved-plan.d.ts +49 -0
  444. package/dist/types/plan-mode/state.d.ts +6 -0
  445. package/dist/types/registry/agent-registry.d.ts +62 -0
  446. package/dist/types/runtime-mcp/client.d.ts +74 -0
  447. package/dist/types/runtime-mcp/config-writer.d.ts +53 -0
  448. package/dist/types/runtime-mcp/config.d.ts +75 -0
  449. package/dist/types/runtime-mcp/discoverable-tool-metadata.d.ts +7 -0
  450. package/dist/types/runtime-mcp/index.d.ts +18 -0
  451. package/dist/types/runtime-mcp/json-rpc.d.ts +22 -0
  452. package/dist/types/runtime-mcp/loader.d.ts +43 -0
  453. package/dist/types/runtime-mcp/manager.d.ts +193 -0
  454. package/dist/types/runtime-mcp/oauth-discovery.d.ts +40 -0
  455. package/dist/types/runtime-mcp/oauth-flow.d.ts +59 -0
  456. package/dist/types/runtime-mcp/render.d.ts +25 -0
  457. package/dist/types/runtime-mcp/smithery-auth.d.ts +16 -0
  458. package/dist/types/runtime-mcp/smithery-connect.d.ts +38 -0
  459. package/dist/types/runtime-mcp/smithery-registry.d.ts +51 -0
  460. package/dist/types/runtime-mcp/tool-bridge.d.ts +86 -0
  461. package/dist/types/runtime-mcp/tool-cache.d.ts +8 -0
  462. package/dist/types/runtime-mcp/transports/http.d.ts +36 -0
  463. package/dist/types/runtime-mcp/transports/index.d.ts +5 -0
  464. package/dist/types/runtime-mcp/transports/stdio.d.ts +32 -0
  465. package/dist/types/runtime-mcp/types.d.ts +333 -0
  466. package/dist/types/sdk.d.ts +216 -0
  467. package/dist/types/secrets/index.d.ts +9 -0
  468. package/dist/types/secrets/obfuscator.d.ts +23 -0
  469. package/dist/types/secrets/regex.d.ts +2 -0
  470. package/dist/types/session/agent-session.d.ts +930 -0
  471. package/dist/types/session/agent-storage.d.ts +100 -0
  472. package/dist/types/session/artifacts.d.ts +61 -0
  473. package/dist/types/session/auth-broker-config.d.ts +13 -0
  474. package/dist/types/session/auth-storage.d.ts +6 -0
  475. package/dist/types/session/blob-store.d.ts +63 -0
  476. package/dist/types/session/client-bridge.d.ts +88 -0
  477. package/dist/types/session/history-storage.d.ts +16 -0
  478. package/dist/types/session/messages.d.ts +162 -0
  479. package/dist/types/session/session-dump-format.d.ts +22 -0
  480. package/dist/types/session/session-manager.d.ts +549 -0
  481. package/dist/types/session/session-storage.d.ts +82 -0
  482. package/dist/types/session/streaming-output.d.ts +185 -0
  483. package/dist/types/session/tool-choice-queue.d.ts +78 -0
  484. package/dist/types/session/yield-queue.d.ts +24 -0
  485. package/dist/types/setup/model-onboarding-guidance.d.ts +8 -0
  486. package/dist/types/setup/provider-onboarding.d.ts +28 -0
  487. package/dist/types/skill-state/active-state.d.ts +50 -0
  488. package/dist/types/slash-commands/acp-builtins.d.ts +18 -0
  489. package/dist/types/slash-commands/builtin-registry.d.ts +23 -0
  490. package/dist/types/slash-commands/helpers/context-report.d.ts +7 -0
  491. package/dist/types/slash-commands/helpers/format.d.ts +10 -0
  492. package/dist/types/slash-commands/helpers/mcp.d.ts +3 -0
  493. package/dist/types/slash-commands/helpers/parse.d.ts +33 -0
  494. package/dist/types/slash-commands/helpers/ssh.d.ts +3 -0
  495. package/dist/types/slash-commands/helpers/todo.d.ts +3 -0
  496. package/dist/types/slash-commands/helpers/usage-report.d.ts +7 -0
  497. package/dist/types/slash-commands/types.d.ts +118 -0
  498. package/dist/types/ssh/config-writer.d.ts +49 -0
  499. package/dist/types/ssh/connection-manager.d.ts +33 -0
  500. package/dist/types/ssh/ssh-executor.d.ts +37 -0
  501. package/dist/types/ssh/sshfs-mount.d.ts +6 -0
  502. package/dist/types/ssh/utils.d.ts +2 -0
  503. package/dist/types/stt/downloader.d.ts +9 -0
  504. package/dist/types/stt/index.d.ts +3 -0
  505. package/dist/types/stt/recorder.d.ts +13 -0
  506. package/dist/types/stt/setup.d.ts +18 -0
  507. package/dist/types/stt/stt-controller.d.ts +16 -0
  508. package/dist/types/stt/transcriber.d.ts +16 -0
  509. package/dist/types/system-prompt.d.ts +93 -0
  510. package/dist/types/task/agents.d.ts +28 -0
  511. package/dist/types/task/commands.d.ts +35 -0
  512. package/dist/types/task/discovery.d.ts +19 -0
  513. package/dist/types/task/executor.d.ts +106 -0
  514. package/dist/types/task/gjc-command.d.ts +7 -0
  515. package/dist/types/task/index.d.ts +38 -0
  516. package/dist/types/task/name-generator.d.ts +16 -0
  517. package/dist/types/task/output-manager.d.ts +36 -0
  518. package/dist/types/task/parallel.d.ts +34 -0
  519. package/dist/types/task/render.d.ts +22 -0
  520. package/dist/types/task/simple-mode.d.ts +8 -0
  521. package/dist/types/task/subprocess-tool-registry.d.ts +72 -0
  522. package/dist/types/task/types.d.ts +295 -0
  523. package/dist/types/task/worktree.d.ts +94 -0
  524. package/dist/types/thinking.d.ts +30 -0
  525. package/dist/types/tool-discovery/tool-index.d.ts +112 -0
  526. package/dist/types/tools/archive-reader.d.ts +40 -0
  527. package/dist/types/tools/ask.d.ts +109 -0
  528. package/dist/types/tools/ast-edit.d.ts +77 -0
  529. package/dist/types/tools/ast-grep.d.ts +69 -0
  530. package/dist/types/tools/auto-generated-guard.d.ts +17 -0
  531. package/dist/types/tools/bash-command-fixup.d.ts +11 -0
  532. package/dist/types/tools/bash-interactive.d.ts +16 -0
  533. package/dist/types/tools/bash-interceptor.d.ts +24 -0
  534. package/dist/types/tools/bash-pty-selection.d.ts +7 -0
  535. package/dist/types/tools/bash-skill-urls.d.ts +31 -0
  536. package/dist/types/tools/bash.d.ts +130 -0
  537. package/dist/types/tools/browser/attach.d.ts +34 -0
  538. package/dist/types/tools/browser/launch.d.ts +62 -0
  539. package/dist/types/tools/browser/readable.d.ts +17 -0
  540. package/dist/types/tools/browser/registry.d.ts +42 -0
  541. package/dist/types/tools/browser/render.d.ts +50 -0
  542. package/dist/types/tools/browser/tab-protocol.d.ts +144 -0
  543. package/dist/types/tools/browser/tab-supervisor.d.ts +63 -0
  544. package/dist/types/tools/browser/tab-worker-entry.d.ts +1 -0
  545. package/dist/types/tools/browser/tab-worker.d.ts +19 -0
  546. package/dist/types/tools/browser.d.ts +114 -0
  547. package/dist/types/tools/calculator.d.ts +76 -0
  548. package/dist/types/tools/checkpoint.d.ts +63 -0
  549. package/dist/types/tools/conflict-detect.d.ts +205 -0
  550. package/dist/types/tools/context.d.ts +19 -0
  551. package/dist/types/tools/debug.d.ts +209 -0
  552. package/dist/types/tools/eval.d.ts +108 -0
  553. package/dist/types/tools/fetch.d.ts +61 -0
  554. package/dist/types/tools/file-recorder.d.ts +13 -0
  555. package/dist/types/tools/find.d.ts +95 -0
  556. package/dist/types/tools/fs-cache-invalidation.d.ts +15 -0
  557. package/dist/types/tools/gh-format.d.ts +6 -0
  558. package/dist/types/tools/gh-renderer.d.ts +25 -0
  559. package/dist/types/tools/gh.d.ts +356 -0
  560. package/dist/types/tools/github-cache.d.ts +103 -0
  561. package/dist/types/tools/grouped-file-output.d.ts +36 -0
  562. package/dist/types/tools/hindsight-recall.d.ts +23 -0
  563. package/dist/types/tools/hindsight-reflect.d.ts +25 -0
  564. package/dist/types/tools/hindsight-retain.d.ts +29 -0
  565. package/dist/types/tools/image-gen.d.ts +78 -0
  566. package/dist/types/tools/index.d.ts +242 -0
  567. package/dist/types/tools/inspect-image-renderer.d.ts +26 -0
  568. package/dist/types/tools/inspect-image.d.ts +31 -0
  569. package/dist/types/tools/irc.d.ts +79 -0
  570. package/dist/types/tools/job.d.ts +65 -0
  571. package/dist/types/tools/json-tree.d.ts +23 -0
  572. package/dist/types/tools/jtd-to-json-schema.d.ts +29 -0
  573. package/dist/types/tools/jtd-to-typescript.d.ts +26 -0
  574. package/dist/types/tools/jtd-utils.d.ts +42 -0
  575. package/dist/types/tools/list-limit.d.ts +12 -0
  576. package/dist/types/tools/match-line-format.d.ts +11 -0
  577. package/dist/types/tools/output-meta.d.ts +204 -0
  578. package/dist/types/tools/path-utils.d.ts +146 -0
  579. package/dist/types/tools/plan-mode-guard.d.ts +18 -0
  580. package/dist/types/tools/read.d.ts +85 -0
  581. package/dist/types/tools/recipe/index.d.ts +45 -0
  582. package/dist/types/tools/recipe/render.d.ts +36 -0
  583. package/dist/types/tools/recipe/runner.d.ts +60 -0
  584. package/dist/types/tools/recipe/runners/cargo.d.ts +16 -0
  585. package/dist/types/tools/recipe/runners/index.d.ts +2 -0
  586. package/dist/types/tools/recipe/runners/just.d.ts +2 -0
  587. package/dist/types/tools/recipe/runners/make.d.ts +2 -0
  588. package/dist/types/tools/recipe/runners/pkg.d.ts +2 -0
  589. package/dist/types/tools/recipe/runners/task.d.ts +2 -0
  590. package/dist/types/tools/render-mermaid.d.ts +37 -0
  591. package/dist/types/tools/render-utils.d.ts +158 -0
  592. package/dist/types/tools/renderers.d.ts +26 -0
  593. package/dist/types/tools/resolve.d.ts +90 -0
  594. package/dist/types/tools/review.d.ts +63 -0
  595. package/dist/types/tools/search-tool-bm25.d.ts +65 -0
  596. package/dist/types/tools/search.d.ts +93 -0
  597. package/dist/types/tools/sqlite-reader.d.ts +93 -0
  598. package/dist/types/tools/ssh.d.ts +67 -0
  599. package/dist/types/tools/subagent.d.ts +58 -0
  600. package/dist/types/tools/todo-write.d.ts +120 -0
  601. package/dist/types/tools/tool-errors.d.ts +33 -0
  602. package/dist/types/tools/tool-result.d.ts +30 -0
  603. package/dist/types/tools/tool-timeouts.d.ts +51 -0
  604. package/dist/types/tools/vim.d.ts +58 -0
  605. package/dist/types/tools/write.d.ts +59 -0
  606. package/dist/types/tools/yield.d.ts +25 -0
  607. package/dist/types/tui/code-cell.d.ts +32 -0
  608. package/dist/types/tui/file-list.d.ts +22 -0
  609. package/dist/types/tui/hyperlink.d.ts +42 -0
  610. package/dist/types/tui/index.d.ts +11 -0
  611. package/dist/types/tui/output-block.d.ts +28 -0
  612. package/dist/types/tui/status-line.d.ts +18 -0
  613. package/dist/types/tui/tree-list.d.ts +19 -0
  614. package/dist/types/tui/types.d.ts +13 -0
  615. package/dist/types/tui/utils.d.ts +36 -0
  616. package/dist/types/utils/changelog.d.ts +20 -0
  617. package/dist/types/utils/clipboard.d.ts +22 -0
  618. package/dist/types/utils/command-args.d.ts +9 -0
  619. package/dist/types/utils/commit-message-generator.d.ts +7 -0
  620. package/dist/types/utils/edit-mode.d.ts +13 -0
  621. package/dist/types/utils/event-bus.d.ts +6 -0
  622. package/dist/types/utils/external-editor.d.ts +17 -0
  623. package/dist/types/utils/file-display-mode.d.ts +27 -0
  624. package/dist/types/utils/file-mentions.d.ts +11 -0
  625. package/dist/types/utils/git.d.ts +361 -0
  626. package/dist/types/utils/image-loading.d.ts +26 -0
  627. package/dist/types/utils/image-resize.d.ts +39 -0
  628. package/dist/types/utils/lang-from-path.d.ts +8 -0
  629. package/dist/types/utils/markit.d.ts +7 -0
  630. package/dist/types/utils/open.d.ts +2 -0
  631. package/dist/types/utils/session-color.d.ts +10 -0
  632. package/dist/types/utils/shell-snapshot.d.ts +5 -0
  633. package/dist/types/utils/sixel.d.ts +21 -0
  634. package/dist/types/utils/title-generator.d.ts +31 -0
  635. package/dist/types/utils/tool-choice.d.ts +7 -0
  636. package/dist/types/utils/tools-manager.d.ts +9 -0
  637. package/dist/types/vim/buffer.d.ts +41 -0
  638. package/dist/types/vim/commands.d.ts +6 -0
  639. package/dist/types/vim/engine.d.ts +47 -0
  640. package/dist/types/vim/parser.d.ts +3 -0
  641. package/dist/types/vim/render.d.ts +25 -0
  642. package/dist/types/vim/types.d.ts +182 -0
  643. package/dist/types/web/kagi.d.ts +23 -0
  644. package/dist/types/web/parallel.d.ts +58 -0
  645. package/dist/types/web/scrapers/artifacthub.d.ts +6 -0
  646. package/dist/types/web/scrapers/arxiv.d.ts +5 -0
  647. package/dist/types/web/scrapers/aur.d.ts +5 -0
  648. package/dist/types/web/scrapers/biorxiv.d.ts +5 -0
  649. package/dist/types/web/scrapers/bluesky.d.ts +5 -0
  650. package/dist/types/web/scrapers/brew.d.ts +5 -0
  651. package/dist/types/web/scrapers/cheatsh.d.ts +8 -0
  652. package/dist/types/web/scrapers/chocolatey.d.ts +5 -0
  653. package/dist/types/web/scrapers/choosealicense.d.ts +2 -0
  654. package/dist/types/web/scrapers/cisa-kev.d.ts +5 -0
  655. package/dist/types/web/scrapers/clojars.d.ts +5 -0
  656. package/dist/types/web/scrapers/coingecko.d.ts +5 -0
  657. package/dist/types/web/scrapers/crates-io.d.ts +5 -0
  658. package/dist/types/web/scrapers/crossref.d.ts +2 -0
  659. package/dist/types/web/scrapers/devto.d.ts +5 -0
  660. package/dist/types/web/scrapers/discogs.d.ts +8 -0
  661. package/dist/types/web/scrapers/discourse.d.ts +5 -0
  662. package/dist/types/web/scrapers/dockerhub.d.ts +5 -0
  663. package/dist/types/web/scrapers/docs-rs.d.ts +2 -0
  664. package/dist/types/web/scrapers/fdroid.d.ts +5 -0
  665. package/dist/types/web/scrapers/firefox-addons.d.ts +2 -0
  666. package/dist/types/web/scrapers/flathub.d.ts +2 -0
  667. package/dist/types/web/scrapers/github-gist.d.ts +5 -0
  668. package/dist/types/web/scrapers/github.d.ts +12 -0
  669. package/dist/types/web/scrapers/gitlab.d.ts +5 -0
  670. package/dist/types/web/scrapers/go-pkg.d.ts +5 -0
  671. package/dist/types/web/scrapers/hackage.d.ts +5 -0
  672. package/dist/types/web/scrapers/hackernews.d.ts +2 -0
  673. package/dist/types/web/scrapers/hex.d.ts +5 -0
  674. package/dist/types/web/scrapers/huggingface.d.ts +2 -0
  675. package/dist/types/web/scrapers/iacr.d.ts +5 -0
  676. package/dist/types/web/scrapers/index.d.ts +84 -0
  677. package/dist/types/web/scrapers/jetbrains-marketplace.d.ts +2 -0
  678. package/dist/types/web/scrapers/lemmy.d.ts +2 -0
  679. package/dist/types/web/scrapers/lobsters.d.ts +5 -0
  680. package/dist/types/web/scrapers/mastodon.d.ts +5 -0
  681. package/dist/types/web/scrapers/maven.d.ts +6 -0
  682. package/dist/types/web/scrapers/mdn.d.ts +2 -0
  683. package/dist/types/web/scrapers/metacpan.d.ts +5 -0
  684. package/dist/types/web/scrapers/musicbrainz.d.ts +5 -0
  685. package/dist/types/web/scrapers/npm.d.ts +5 -0
  686. package/dist/types/web/scrapers/nuget.d.ts +5 -0
  687. package/dist/types/web/scrapers/nvd.d.ts +5 -0
  688. package/dist/types/web/scrapers/ollama.d.ts +2 -0
  689. package/dist/types/web/scrapers/open-vsx.d.ts +5 -0
  690. package/dist/types/web/scrapers/opencorporates.d.ts +5 -0
  691. package/dist/types/web/scrapers/openlibrary.d.ts +5 -0
  692. package/dist/types/web/scrapers/orcid.d.ts +5 -0
  693. package/dist/types/web/scrapers/osv.d.ts +5 -0
  694. package/dist/types/web/scrapers/packagist.d.ts +5 -0
  695. package/dist/types/web/scrapers/pub-dev.d.ts +5 -0
  696. package/dist/types/web/scrapers/pubmed.d.ts +5 -0
  697. package/dist/types/web/scrapers/pypi.d.ts +5 -0
  698. package/dist/types/web/scrapers/rawg.d.ts +2 -0
  699. package/dist/types/web/scrapers/readthedocs.d.ts +2 -0
  700. package/dist/types/web/scrapers/reddit.d.ts +5 -0
  701. package/dist/types/web/scrapers/repology.d.ts +5 -0
  702. package/dist/types/web/scrapers/rfc.d.ts +5 -0
  703. package/dist/types/web/scrapers/rubygems.d.ts +5 -0
  704. package/dist/types/web/scrapers/searchcode.d.ts +2 -0
  705. package/dist/types/web/scrapers/sec-edgar.d.ts +5 -0
  706. package/dist/types/web/scrapers/semantic-scholar.d.ts +2 -0
  707. package/dist/types/web/scrapers/snapcraft.d.ts +2 -0
  708. package/dist/types/web/scrapers/sourcegraph.d.ts +2 -0
  709. package/dist/types/web/scrapers/spdx.d.ts +5 -0
  710. package/dist/types/web/scrapers/spotify.d.ts +8 -0
  711. package/dist/types/web/scrapers/stackoverflow.d.ts +6 -0
  712. package/dist/types/web/scrapers/terraform.d.ts +5 -0
  713. package/dist/types/web/scrapers/tldr.d.ts +7 -0
  714. package/dist/types/web/scrapers/twitter.d.ts +5 -0
  715. package/dist/types/web/scrapers/types.d.ts +78 -0
  716. package/dist/types/web/scrapers/utils.d.ts +26 -0
  717. package/dist/types/web/scrapers/vimeo.d.ts +5 -0
  718. package/dist/types/web/scrapers/vscode-marketplace.d.ts +5 -0
  719. package/dist/types/web/scrapers/w3c.d.ts +2 -0
  720. package/dist/types/web/scrapers/wikidata.d.ts +5 -0
  721. package/dist/types/web/scrapers/wikipedia.d.ts +5 -0
  722. package/dist/types/web/scrapers/youtube.d.ts +5 -0
  723. package/dist/types/web/search/index.d.ts +84 -0
  724. package/dist/types/web/search/provider.d.ts +21 -0
  725. package/dist/types/web/search/providers/anthropic.d.ts +32 -0
  726. package/dist/types/web/search/providers/base.d.ts +67 -0
  727. package/dist/types/web/search/providers/brave.d.ts +27 -0
  728. package/dist/types/web/search/providers/codex.d.ts +35 -0
  729. package/dist/types/web/search/providers/exa.d.ts +52 -0
  730. package/dist/types/web/search/providers/gemini.d.ts +57 -0
  731. package/dist/types/web/search/providers/jina.d.ts +26 -0
  732. package/dist/types/web/search/providers/kagi.d.ts +24 -0
  733. package/dist/types/web/search/providers/kimi.d.ts +27 -0
  734. package/dist/types/web/search/providers/parallel.d.ts +15 -0
  735. package/dist/types/web/search/providers/perplexity.d.ts +38 -0
  736. package/dist/types/web/search/providers/searxng.d.ts +44 -0
  737. package/dist/types/web/search/providers/synthetic.d.ts +21 -0
  738. package/dist/types/web/search/providers/tavily.d.ts +29 -0
  739. package/dist/types/web/search/providers/utils.d.ts +52 -0
  740. package/dist/types/web/search/providers/zai.d.ts +28 -0
  741. package/dist/types/web/search/render.d.ts +35 -0
  742. package/dist/types/web/search/types.d.ts +344 -0
  743. package/dist/types/web/search/utils.d.ts +4 -0
  744. package/dist/types/workspace-tree.d.ts +42 -0
  745. package/examples/README.md +21 -0
  746. package/examples/custom-tools/README.md +104 -0
  747. package/examples/custom-tools/hello/index.ts +20 -0
  748. package/examples/extensions/README.md +141 -0
  749. package/examples/extensions/api-demo.ts +79 -0
  750. package/examples/extensions/chalk-logger.ts +25 -0
  751. package/examples/extensions/hello.ts +31 -0
  752. package/examples/extensions/pirate.ts +43 -0
  753. package/examples/extensions/plan-mode.ts +549 -0
  754. package/examples/extensions/reload-runtime.ts +38 -0
  755. package/examples/extensions/tools.ts +144 -0
  756. package/examples/extensions/with-deps/index.ts +36 -0
  757. package/examples/extensions/with-deps/package-lock.json +31 -0
  758. package/examples/extensions/with-deps/package.json +16 -0
  759. package/examples/hooks/README.md +56 -0
  760. package/examples/hooks/auto-commit-on-exit.ts +48 -0
  761. package/examples/hooks/confirm-destructive.ts +58 -0
  762. package/examples/hooks/custom-compaction.ts +115 -0
  763. package/examples/hooks/dirty-repo-guard.ts +51 -0
  764. package/examples/hooks/file-trigger.ts +40 -0
  765. package/examples/hooks/git-checkpoint.ts +52 -0
  766. package/examples/hooks/handoff.ts +149 -0
  767. package/examples/hooks/permission-gate.ts +33 -0
  768. package/examples/hooks/protected-paths.ts +29 -0
  769. package/examples/hooks/qna.ts +118 -0
  770. package/examples/hooks/status-line.ts +39 -0
  771. package/examples/sdk/01-minimal.ts +21 -0
  772. package/examples/sdk/02-custom-model.ts +49 -0
  773. package/examples/sdk/03-custom-prompt.ts +46 -0
  774. package/examples/sdk/04-skills.ts +43 -0
  775. package/examples/sdk/06-extensions.ts +82 -0
  776. package/examples/sdk/06-hooks.ts +61 -0
  777. package/examples/sdk/07-context-files.ts +35 -0
  778. package/examples/sdk/08-prompt-templates.ts +41 -0
  779. package/examples/sdk/08-slash-commands.ts +46 -0
  780. package/examples/sdk/09-api-keys-and-oauth.ts +54 -0
  781. package/examples/sdk/11-sessions.ts +47 -0
  782. package/examples/sdk/README.md +172 -0
  783. package/package.json +533 -0
  784. package/scripts/build-binary.ts +77 -0
  785. package/scripts/format-prompts.ts +68 -0
  786. package/scripts/generate-docs-index.ts +71 -0
  787. package/scripts/generate-template.ts +33 -0
  788. package/src/async/index.ts +2 -0
  789. package/src/async/job-manager.ts +586 -0
  790. package/src/async/support.ts +6 -0
  791. package/src/autoresearch/command-resume.md +14 -0
  792. package/src/autoresearch/dashboard.ts +446 -0
  793. package/src/autoresearch/git.ts +319 -0
  794. package/src/autoresearch/helpers.ts +218 -0
  795. package/src/autoresearch/index.ts +536 -0
  796. package/src/autoresearch/prompt-setup.md +43 -0
  797. package/src/autoresearch/prompt.md +103 -0
  798. package/src/autoresearch/resume-message.md +10 -0
  799. package/src/autoresearch/state.ts +273 -0
  800. package/src/autoresearch/storage.ts +699 -0
  801. package/src/autoresearch/tools/init-experiment.ts +272 -0
  802. package/src/autoresearch/tools/log-experiment.ts +524 -0
  803. package/src/autoresearch/tools/run-experiment.ts +475 -0
  804. package/src/autoresearch/tools/update-notes.ts +109 -0
  805. package/src/autoresearch/types.ts +168 -0
  806. package/src/bun-imports.d.ts +28 -0
  807. package/src/capability/context-file.ts +44 -0
  808. package/src/capability/extension-module.ts +34 -0
  809. package/src/capability/extension.ts +47 -0
  810. package/src/capability/fs.ts +107 -0
  811. package/src/capability/hook.ts +40 -0
  812. package/src/capability/index.ts +436 -0
  813. package/src/capability/instruction.ts +37 -0
  814. package/src/capability/mcp.ts +74 -0
  815. package/src/capability/prompt.ts +35 -0
  816. package/src/capability/rule.ts +244 -0
  817. package/src/capability/settings.ts +34 -0
  818. package/src/capability/skill.ts +56 -0
  819. package/src/capability/slash-command.ts +40 -0
  820. package/src/capability/ssh.ts +41 -0
  821. package/src/capability/system-prompt.ts +34 -0
  822. package/src/capability/tool.ts +38 -0
  823. package/src/capability/types.ts +168 -0
  824. package/src/cli/agents-cli.ts +138 -0
  825. package/src/cli/args.ts +269 -0
  826. package/src/cli/auth-broker-cli.ts +746 -0
  827. package/src/cli/auth-gateway-cli.ts +411 -0
  828. package/src/cli/classify-install-target.ts +50 -0
  829. package/src/cli/commands/init-xdg.ts +27 -0
  830. package/src/cli/config-cli.ts +418 -0
  831. package/src/cli/file-processor.ts +123 -0
  832. package/src/cli/grep-cli.ts +160 -0
  833. package/src/cli/initial-message.ts +58 -0
  834. package/src/cli/list-models.ts +194 -0
  835. package/src/cli/plugin-cli.ts +942 -0
  836. package/src/cli/read-cli.ts +57 -0
  837. package/src/cli/session-picker.ts +52 -0
  838. package/src/cli/setup-cli.ts +433 -0
  839. package/src/cli/shell-cli.ts +176 -0
  840. package/src/cli/ssh-cli.ts +179 -0
  841. package/src/cli/stats-cli.ts +238 -0
  842. package/src/cli/update-cli.ts +408 -0
  843. package/src/cli/web-search-cli.ts +133 -0
  844. package/src/cli/worktree-cli.ts +291 -0
  845. package/src/cli.ts +99 -0
  846. package/src/commands/acp.ts +24 -0
  847. package/src/commands/agents.ts +57 -0
  848. package/src/commands/auth-broker.ts +96 -0
  849. package/src/commands/auth-gateway.ts +63 -0
  850. package/src/commands/codex-native-hook.ts +11 -0
  851. package/src/commands/commit.ts +46 -0
  852. package/src/commands/config.ts +51 -0
  853. package/src/commands/deep-interview.ts +12 -0
  854. package/src/commands/gjc-runtime-bridge.ts +81 -0
  855. package/src/commands/grep.ts +48 -0
  856. package/src/commands/launch.ts +161 -0
  857. package/src/commands/plugin.ts +78 -0
  858. package/src/commands/question.ts +12 -0
  859. package/src/commands/ralplan.ts +12 -0
  860. package/src/commands/read.ts +35 -0
  861. package/src/commands/setup.ts +52 -0
  862. package/src/commands/shell.ts +29 -0
  863. package/src/commands/ssh.ts +60 -0
  864. package/src/commands/state.ts +12 -0
  865. package/src/commands/stats.ts +29 -0
  866. package/src/commands/team.ts +130 -0
  867. package/src/commands/ultragoal.ts +32 -0
  868. package/src/commands/update.ts +21 -0
  869. package/src/commands/web-search.ts +42 -0
  870. package/src/commands/worktree.ts +56 -0
  871. package/src/commit/agentic/agent.ts +316 -0
  872. package/src/commit/agentic/fallback.ts +96 -0
  873. package/src/commit/agentic/index.ts +355 -0
  874. package/src/commit/agentic/prompts/analyze-file.md +22 -0
  875. package/src/commit/agentic/prompts/session-user.md +25 -0
  876. package/src/commit/agentic/prompts/split-confirm.md +1 -0
  877. package/src/commit/agentic/prompts/system.md +38 -0
  878. package/src/commit/agentic/state.ts +60 -0
  879. package/src/commit/agentic/tools/analyze-file.ts +127 -0
  880. package/src/commit/agentic/tools/git-file-diff.ts +191 -0
  881. package/src/commit/agentic/tools/git-hunk.ts +50 -0
  882. package/src/commit/agentic/tools/git-overview.ts +81 -0
  883. package/src/commit/agentic/tools/index.ts +54 -0
  884. package/src/commit/agentic/tools/propose-changelog.ts +144 -0
  885. package/src/commit/agentic/tools/propose-commit.ts +109 -0
  886. package/src/commit/agentic/tools/recent-commits.ts +81 -0
  887. package/src/commit/agentic/tools/schemas.ts +23 -0
  888. package/src/commit/agentic/tools/split-commit.ts +238 -0
  889. package/src/commit/agentic/topo-sort.ts +44 -0
  890. package/src/commit/agentic/trivial.ts +51 -0
  891. package/src/commit/agentic/validation.ts +183 -0
  892. package/src/commit/analysis/conventional.ts +64 -0
  893. package/src/commit/analysis/index.ts +4 -0
  894. package/src/commit/analysis/scope.ts +242 -0
  895. package/src/commit/analysis/summary.ts +105 -0
  896. package/src/commit/analysis/validation.ts +66 -0
  897. package/src/commit/changelog/detect.ts +40 -0
  898. package/src/commit/changelog/generate.ts +97 -0
  899. package/src/commit/changelog/index.ts +234 -0
  900. package/src/commit/changelog/parse.ts +44 -0
  901. package/src/commit/cli.ts +85 -0
  902. package/src/commit/git/diff.ts +148 -0
  903. package/src/commit/index.ts +5 -0
  904. package/src/commit/map-reduce/index.ts +69 -0
  905. package/src/commit/map-reduce/map-phase.ts +193 -0
  906. package/src/commit/map-reduce/reduce-phase.ts +49 -0
  907. package/src/commit/map-reduce/utils.ts +9 -0
  908. package/src/commit/message.ts +11 -0
  909. package/src/commit/model-selection.ts +66 -0
  910. package/src/commit/pipeline.ts +243 -0
  911. package/src/commit/prompts/analysis-system.md +148 -0
  912. package/src/commit/prompts/analysis-user.md +38 -0
  913. package/src/commit/prompts/changelog-system.md +50 -0
  914. package/src/commit/prompts/changelog-user.md +18 -0
  915. package/src/commit/prompts/file-observer-system.md +24 -0
  916. package/src/commit/prompts/file-observer-user.md +8 -0
  917. package/src/commit/prompts/reduce-system.md +50 -0
  918. package/src/commit/prompts/reduce-user.md +17 -0
  919. package/src/commit/prompts/summary-retry.md +3 -0
  920. package/src/commit/prompts/summary-system.md +38 -0
  921. package/src/commit/prompts/summary-user.md +13 -0
  922. package/src/commit/prompts/types-description.md +2 -0
  923. package/src/commit/shared-llm.ts +77 -0
  924. package/src/commit/types.ts +118 -0
  925. package/src/commit/utils/exclusions.ts +42 -0
  926. package/src/commit/utils.ts +58 -0
  927. package/src/config/config-file.ts +232 -0
  928. package/src/config/file-lock.ts +121 -0
  929. package/src/config/keybindings.ts +493 -0
  930. package/src/config/mcp-schema.json +230 -0
  931. package/src/config/model-equivalence.ts +811 -0
  932. package/src/config/model-registry.ts +2384 -0
  933. package/src/config/model-resolver.ts +1398 -0
  934. package/src/config/models-config-schema.ts +175 -0
  935. package/src/config/prompt-templates.ts +310 -0
  936. package/src/config/resolve-config-value.ts +94 -0
  937. package/src/config/settings-schema.ts +2939 -0
  938. package/src/config/settings.ts +919 -0
  939. package/src/config/skill-settings-defaults.ts +27 -0
  940. package/src/config.ts +212 -0
  941. package/src/cursor.ts +350 -0
  942. package/src/dap/client.ts +675 -0
  943. package/src/dap/config.ts +150 -0
  944. package/src/dap/defaults.json +211 -0
  945. package/src/dap/index.ts +4 -0
  946. package/src/dap/session.ts +1306 -0
  947. package/src/dap/types.ts +600 -0
  948. package/src/debug/index.ts +459 -0
  949. package/src/debug/log-formatting.ts +58 -0
  950. package/src/debug/log-viewer.ts +908 -0
  951. package/src/debug/profiler.ts +162 -0
  952. package/src/debug/raw-sse-buffer.ts +270 -0
  953. package/src/debug/raw-sse.ts +213 -0
  954. package/src/debug/report-bundle.ts +365 -0
  955. package/src/debug/system-info.ts +111 -0
  956. package/src/defaults/gjc/skills/deep-interview/SKILL.md +795 -0
  957. package/src/defaults/gjc/skills/ralplan/SKILL.md +142 -0
  958. package/src/defaults/gjc/skills/team/SKILL.md +373 -0
  959. package/src/defaults/gjc/skills/ultragoal/SKILL.md +176 -0
  960. package/src/defaults/gjc-defaults.ts +151 -0
  961. package/src/discovery/agents-md.ts +67 -0
  962. package/src/discovery/agents.ts +230 -0
  963. package/src/discovery/builtin.ts +932 -0
  964. package/src/discovery/claude-plugins.ts +387 -0
  965. package/src/discovery/claude.ts +313 -0
  966. package/src/discovery/cline.ts +83 -0
  967. package/src/discovery/codex.ts +330 -0
  968. package/src/discovery/cursor.ts +220 -0
  969. package/src/discovery/gemini.ts +383 -0
  970. package/src/discovery/github.ts +118 -0
  971. package/src/discovery/helpers.ts +964 -0
  972. package/src/discovery/index.ts +76 -0
  973. package/src/discovery/mcp-json.ts +171 -0
  974. package/src/discovery/opencode.ts +398 -0
  975. package/src/discovery/plugin-dir-roots.ts +28 -0
  976. package/src/discovery/ssh.ts +153 -0
  977. package/src/discovery/substitute-plugin-root.ts +29 -0
  978. package/src/discovery/vscode.ts +105 -0
  979. package/src/discovery/windsurf.ts +147 -0
  980. package/src/edit/apply-patch/index.ts +87 -0
  981. package/src/edit/apply-patch/parser.ts +174 -0
  982. package/src/edit/diff.ts +810 -0
  983. package/src/edit/file-read-cache.ts +95 -0
  984. package/src/edit/index.ts +528 -0
  985. package/src/edit/modes/apply-patch.lark +19 -0
  986. package/src/edit/modes/apply-patch.ts +53 -0
  987. package/src/edit/modes/patch.ts +1835 -0
  988. package/src/edit/modes/replace.ts +1103 -0
  989. package/src/edit/normalize.ts +375 -0
  990. package/src/edit/notebook.ts +222 -0
  991. package/src/edit/read-file.ts +25 -0
  992. package/src/edit/renderer.ts +683 -0
  993. package/src/edit/streaming.ts +561 -0
  994. package/src/eval/backend.ts +43 -0
  995. package/src/eval/index.ts +4 -0
  996. package/src/eval/js/context-manager.ts +414 -0
  997. package/src/eval/js/executor.ts +134 -0
  998. package/src/eval/js/index.ts +46 -0
  999. package/src/eval/js/shared/helpers.ts +237 -0
  1000. package/src/eval/js/shared/indirect-eval.ts +30 -0
  1001. package/src/eval/js/shared/prelude.ts +2 -0
  1002. package/src/eval/js/shared/prelude.txt +70 -0
  1003. package/src/eval/js/shared/rewrite-imports.ts +416 -0
  1004. package/src/eval/js/shared/runtime.ts +301 -0
  1005. package/src/eval/js/shared/types.ts +18 -0
  1006. package/src/eval/js/tool-bridge.ts +144 -0
  1007. package/src/eval/js/worker-core.ts +146 -0
  1008. package/src/eval/js/worker-entry.ts +24 -0
  1009. package/src/eval/js/worker-protocol.ts +41 -0
  1010. package/src/eval/py/display.ts +71 -0
  1011. package/src/eval/py/executor.ts +602 -0
  1012. package/src/eval/py/index.ts +58 -0
  1013. package/src/eval/py/kernel.ts +668 -0
  1014. package/src/eval/py/prelude.py +462 -0
  1015. package/src/eval/py/prelude.ts +3 -0
  1016. package/src/eval/py/runner.py +910 -0
  1017. package/src/eval/py/runtime.ts +210 -0
  1018. package/src/eval/py/tool-bridge.ts +137 -0
  1019. package/src/eval/types.ts +48 -0
  1020. package/src/exa/factory.ts +60 -0
  1021. package/src/exa/index.ts +27 -0
  1022. package/src/exa/mcp-client.ts +364 -0
  1023. package/src/exa/render.ts +244 -0
  1024. package/src/exa/researcher.ts +36 -0
  1025. package/src/exa/search.ts +47 -0
  1026. package/src/exa/types.ts +166 -0
  1027. package/src/exa/websets.ts +248 -0
  1028. package/src/exec/bash-executor.ts +309 -0
  1029. package/src/exec/exec.ts +53 -0
  1030. package/src/exec/idle-timeout-watchdog.ts +126 -0
  1031. package/src/exec/non-interactive-env.ts +48 -0
  1032. package/src/export/custom-share.ts +65 -0
  1033. package/src/export/html/index.ts +164 -0
  1034. package/src/export/html/template.css +1060 -0
  1035. package/src/export/html/template.generated.ts +2 -0
  1036. package/src/export/html/template.html +47 -0
  1037. package/src/export/html/template.js +2268 -0
  1038. package/src/export/html/template.macro.ts +25 -0
  1039. package/src/export/html/vendor/highlight.min.js +1213 -0
  1040. package/src/export/html/vendor/marked.min.js +6 -0
  1041. package/src/export/ttsr.ts +434 -0
  1042. package/src/extensibility/custom-commands/bundled/ci-green/index.ts +25 -0
  1043. package/src/extensibility/custom-commands/bundled/review/index.ts +456 -0
  1044. package/src/extensibility/custom-commands/index.ts +2 -0
  1045. package/src/extensibility/custom-commands/loader.ts +238 -0
  1046. package/src/extensibility/custom-commands/types.ts +113 -0
  1047. package/src/extensibility/custom-tools/index.ts +7 -0
  1048. package/src/extensibility/custom-tools/loader.ts +245 -0
  1049. package/src/extensibility/custom-tools/types.ts +254 -0
  1050. package/src/extensibility/custom-tools/wrapper.ts +47 -0
  1051. package/src/extensibility/extensions/compact-handler.ts +40 -0
  1052. package/src/extensibility/extensions/get-commands-handler.ts +80 -0
  1053. package/src/extensibility/extensions/index.ts +15 -0
  1054. package/src/extensibility/extensions/loader.ts +545 -0
  1055. package/src/extensibility/extensions/runner.ts +900 -0
  1056. package/src/extensibility/extensions/types.ts +1259 -0
  1057. package/src/extensibility/extensions/wrapper.ts +189 -0
  1058. package/src/extensibility/hooks/index.ts +5 -0
  1059. package/src/extensibility/hooks/loader.ts +257 -0
  1060. package/src/extensibility/hooks/runner.ts +425 -0
  1061. package/src/extensibility/hooks/tool-wrapper.ts +107 -0
  1062. package/src/extensibility/hooks/types.ts +599 -0
  1063. package/src/extensibility/plugins/doctor.ts +66 -0
  1064. package/src/extensibility/plugins/git-url.ts +281 -0
  1065. package/src/extensibility/plugins/index.ts +9 -0
  1066. package/src/extensibility/plugins/installer.ts +192 -0
  1067. package/src/extensibility/plugins/legacy-pi-compat.ts +336 -0
  1068. package/src/extensibility/plugins/loader.ts +288 -0
  1069. package/src/extensibility/plugins/manager.ts +731 -0
  1070. package/src/extensibility/plugins/marketplace/cache.ts +136 -0
  1071. package/src/extensibility/plugins/marketplace/fetcher.ts +317 -0
  1072. package/src/extensibility/plugins/marketplace/index.ts +6 -0
  1073. package/src/extensibility/plugins/marketplace/manager.ts +770 -0
  1074. package/src/extensibility/plugins/marketplace/registry.ts +196 -0
  1075. package/src/extensibility/plugins/marketplace/source-resolver.ts +147 -0
  1076. package/src/extensibility/plugins/marketplace/types.ts +191 -0
  1077. package/src/extensibility/plugins/parser.ts +105 -0
  1078. package/src/extensibility/plugins/types.ts +194 -0
  1079. package/src/extensibility/shared-events.ts +343 -0
  1080. package/src/extensibility/skills.ts +391 -0
  1081. package/src/extensibility/slash-commands.ts +227 -0
  1082. package/src/extensibility/tool-proxy.ts +25 -0
  1083. package/src/extensibility/typebox.ts +418 -0
  1084. package/src/extensibility/utils.ts +44 -0
  1085. package/src/gjc-runtime/goal-mode-request.ts +212 -0
  1086. package/src/gjc-runtime/launch-tmux.ts +162 -0
  1087. package/src/gjc-runtime/launch-worktree.ts +291 -0
  1088. package/src/gjc-runtime/team-runtime.ts +2025 -0
  1089. package/src/gjc-runtime/ultragoal-runtime.ts +564 -0
  1090. package/src/goals/index.ts +3 -0
  1091. package/src/goals/runtime.ts +524 -0
  1092. package/src/goals/state.ts +37 -0
  1093. package/src/goals/tools/goal-tool.ts +371 -0
  1094. package/src/hashline/anchors.ts +113 -0
  1095. package/src/hashline/apply.ts +737 -0
  1096. package/src/hashline/bigrams.json +649 -0
  1097. package/src/hashline/constants.ts +25 -0
  1098. package/src/hashline/diff-preview.ts +43 -0
  1099. package/src/hashline/diff.ts +56 -0
  1100. package/src/hashline/execute.ts +267 -0
  1101. package/src/hashline/grammar.lark +21 -0
  1102. package/src/hashline/hash.ts +173 -0
  1103. package/src/hashline/index.ts +13 -0
  1104. package/src/hashline/input.ts +130 -0
  1105. package/src/hashline/parser.ts +246 -0
  1106. package/src/hashline/prefixes.ts +101 -0
  1107. package/src/hashline/recovery.ts +113 -0
  1108. package/src/hashline/stream.ts +123 -0
  1109. package/src/hashline/types.ts +68 -0
  1110. package/src/hindsight/backend.ts +205 -0
  1111. package/src/hindsight/bank.ts +131 -0
  1112. package/src/hindsight/client.ts +598 -0
  1113. package/src/hindsight/config.ts +175 -0
  1114. package/src/hindsight/content.ts +210 -0
  1115. package/src/hindsight/index.ts +8 -0
  1116. package/src/hindsight/mental-models.ts +382 -0
  1117. package/src/hindsight/seeds.json +32 -0
  1118. package/src/hindsight/state.ts +469 -0
  1119. package/src/hindsight/transcript.ts +71 -0
  1120. package/src/hooks/codex-native-hooks-config.ts +143 -0
  1121. package/src/hooks/native-skill-hook.ts +279 -0
  1122. package/src/hooks/skill-keywords.ts +86 -0
  1123. package/src/hooks/skill-state.ts +416 -0
  1124. package/src/index.ts +56 -0
  1125. package/src/internal-urls/agent-protocol.ts +129 -0
  1126. package/src/internal-urls/artifact-protocol.ts +80 -0
  1127. package/src/internal-urls/docs-index.generated.ts +81 -0
  1128. package/src/internal-urls/gjc-protocol.ts +83 -0
  1129. package/src/internal-urls/index.ts +21 -0
  1130. package/src/internal-urls/issue-pr-protocol.ts +577 -0
  1131. package/src/internal-urls/json-query.ts +126 -0
  1132. package/src/internal-urls/local-protocol.ts +249 -0
  1133. package/src/internal-urls/mcp-protocol.ts +151 -0
  1134. package/src/internal-urls/memory-protocol.ts +164 -0
  1135. package/src/internal-urls/parse.ts +72 -0
  1136. package/src/internal-urls/registry-helpers.ts +25 -0
  1137. package/src/internal-urls/router.ts +78 -0
  1138. package/src/internal-urls/rule-protocol.ts +38 -0
  1139. package/src/internal-urls/skill-protocol.ts +103 -0
  1140. package/src/internal-urls/types.ts +110 -0
  1141. package/src/lsp/client.ts +951 -0
  1142. package/src/lsp/clients/biome-client.ts +202 -0
  1143. package/src/lsp/clients/index.ts +50 -0
  1144. package/src/lsp/clients/lsp-linter-client.ts +93 -0
  1145. package/src/lsp/clients/swiftlint-client.ts +120 -0
  1146. package/src/lsp/config.ts +492 -0
  1147. package/src/lsp/defaults.json +493 -0
  1148. package/src/lsp/edits.ts +166 -0
  1149. package/src/lsp/index.ts +2282 -0
  1150. package/src/lsp/lspmux.ts +233 -0
  1151. package/src/lsp/render.ts +692 -0
  1152. package/src/lsp/startup-events.ts +13 -0
  1153. package/src/lsp/types.ts +443 -0
  1154. package/src/lsp/utils.ts +678 -0
  1155. package/src/main.ts +961 -0
  1156. package/src/memories/index.ts +1135 -0
  1157. package/src/memories/storage.ts +577 -0
  1158. package/src/memory-backend/index.ts +4 -0
  1159. package/src/memory-backend/local-backend.ts +30 -0
  1160. package/src/memory-backend/off-backend.ts +16 -0
  1161. package/src/memory-backend/resolve.ts +24 -0
  1162. package/src/memory-backend/types.ts +79 -0
  1163. package/src/modes/acp/acp-agent.ts +2170 -0
  1164. package/src/modes/acp/acp-client-bridge.ts +154 -0
  1165. package/src/modes/acp/acp-event-mapper.ts +879 -0
  1166. package/src/modes/acp/acp-mode.ts +23 -0
  1167. package/src/modes/acp/index.ts +2 -0
  1168. package/src/modes/acp/terminal-auth.ts +37 -0
  1169. package/src/modes/components/agent-dashboard.ts +1120 -0
  1170. package/src/modes/components/assistant-message.ts +231 -0
  1171. package/src/modes/components/bash-execution.ts +228 -0
  1172. package/src/modes/components/bordered-loader.ts +41 -0
  1173. package/src/modes/components/branch-summary-message.ts +45 -0
  1174. package/src/modes/components/btw-panel.ts +104 -0
  1175. package/src/modes/components/compaction-summary-message.ts +51 -0
  1176. package/src/modes/components/countdown-timer.ts +75 -0
  1177. package/src/modes/components/custom-editor.ts +237 -0
  1178. package/src/modes/components/custom-message.ts +65 -0
  1179. package/src/modes/components/diff.ts +266 -0
  1180. package/src/modes/components/dynamic-border.ts +25 -0
  1181. package/src/modes/components/eval-execution.ts +164 -0
  1182. package/src/modes/components/execution-shared.ts +102 -0
  1183. package/src/modes/components/extensions/extension-dashboard.ts +350 -0
  1184. package/src/modes/components/extensions/extension-list.ts +492 -0
  1185. package/src/modes/components/extensions/index.ts +9 -0
  1186. package/src/modes/components/extensions/inspector-panel.ts +317 -0
  1187. package/src/modes/components/extensions/state-manager.ts +628 -0
  1188. package/src/modes/components/extensions/types.ts +191 -0
  1189. package/src/modes/components/footer.ts +270 -0
  1190. package/src/modes/components/history-search.ts +158 -0
  1191. package/src/modes/components/hook-editor.ts +151 -0
  1192. package/src/modes/components/hook-input.ts +79 -0
  1193. package/src/modes/components/hook-message.ts +68 -0
  1194. package/src/modes/components/hook-selector.ts +190 -0
  1195. package/src/modes/components/index.ts +36 -0
  1196. package/src/modes/components/keybinding-hints.ts +65 -0
  1197. package/src/modes/components/login-dialog.ts +164 -0
  1198. package/src/modes/components/message-frame.ts +88 -0
  1199. package/src/modes/components/model-selector.ts +794 -0
  1200. package/src/modes/components/oauth-selector.ts +248 -0
  1201. package/src/modes/components/plugin-selector.ts +95 -0
  1202. package/src/modes/components/plugin-settings.ts +488 -0
  1203. package/src/modes/components/provider-onboarding-selector.ts +88 -0
  1204. package/src/modes/components/queue-mode-selector.ts +56 -0
  1205. package/src/modes/components/read-tool-group.ts +267 -0
  1206. package/src/modes/components/runtime-mcp-add-wizard.ts +1341 -0
  1207. package/src/modes/components/session-observer-overlay.ts +839 -0
  1208. package/src/modes/components/session-selector.ts +343 -0
  1209. package/src/modes/components/settings-defs.ts +175 -0
  1210. package/src/modes/components/settings-selector.ts +635 -0
  1211. package/src/modes/components/show-images-selector.ts +45 -0
  1212. package/src/modes/components/skill-hud/render.ts +47 -0
  1213. package/src/modes/components/skill-message.ts +90 -0
  1214. package/src/modes/components/status-line/context-thresholds.ts +68 -0
  1215. package/src/modes/components/status-line/git-utils.ts +42 -0
  1216. package/src/modes/components/status-line/index.ts +4 -0
  1217. package/src/modes/components/status-line/presets.ts +105 -0
  1218. package/src/modes/components/status-line/segments.ts +560 -0
  1219. package/src/modes/components/status-line/separators.ts +55 -0
  1220. package/src/modes/components/status-line/token-rate.ts +66 -0
  1221. package/src/modes/components/status-line/types.ts +93 -0
  1222. package/src/modes/components/status-line.ts +839 -0
  1223. package/src/modes/components/theme-selector.ts +63 -0
  1224. package/src/modes/components/thinking-selector.ts +52 -0
  1225. package/src/modes/components/todo-reminder.ts +40 -0
  1226. package/src/modes/components/tool-execution.ts +830 -0
  1227. package/src/modes/components/tree-selector.ts +930 -0
  1228. package/src/modes/components/ttsr-notification.ts +80 -0
  1229. package/src/modes/components/user-message-selector.ts +141 -0
  1230. package/src/modes/components/user-message.ts +51 -0
  1231. package/src/modes/components/visual-truncate.ts +63 -0
  1232. package/src/modes/components/welcome.ts +389 -0
  1233. package/src/modes/controllers/btw-controller.ts +105 -0
  1234. package/src/modes/controllers/command-controller-shared.ts +108 -0
  1235. package/src/modes/controllers/command-controller.ts +1630 -0
  1236. package/src/modes/controllers/event-controller.ts +786 -0
  1237. package/src/modes/controllers/extension-ui-controller.ts +927 -0
  1238. package/src/modes/controllers/input-controller.ts +858 -0
  1239. package/src/modes/controllers/runtime-mcp-command-controller.ts +1934 -0
  1240. package/src/modes/controllers/selector-controller.ts +1039 -0
  1241. package/src/modes/controllers/ssh-command-controller.ts +384 -0
  1242. package/src/modes/controllers/todo-command-controller.ts +485 -0
  1243. package/src/modes/data/emojis.json +1 -0
  1244. package/src/modes/emoji-autocomplete.ts +285 -0
  1245. package/src/modes/index.ts +34 -0
  1246. package/src/modes/interactive-mode.ts +2756 -0
  1247. package/src/modes/loop-limit.ts +140 -0
  1248. package/src/modes/oauth-manual-input.ts +42 -0
  1249. package/src/modes/print-mode.ts +121 -0
  1250. package/src/modes/prompt-action-autocomplete.ts +345 -0
  1251. package/src/modes/rpc/host-tools.ts +186 -0
  1252. package/src/modes/rpc/host-uris.ts +235 -0
  1253. package/src/modes/rpc/rpc-client.ts +810 -0
  1254. package/src/modes/rpc/rpc-mode.ts +851 -0
  1255. package/src/modes/rpc/rpc-types.ts +378 -0
  1256. package/src/modes/runtime-init.ts +116 -0
  1257. package/src/modes/session-observer-registry.ts +146 -0
  1258. package/src/modes/shared.ts +55 -0
  1259. package/src/modes/theme/dark.json +95 -0
  1260. package/src/modes/theme/defaults/alabaster.json +93 -0
  1261. package/src/modes/theme/defaults/amethyst.json +96 -0
  1262. package/src/modes/theme/defaults/anthracite.json +93 -0
  1263. package/src/modes/theme/defaults/basalt.json +91 -0
  1264. package/src/modes/theme/defaults/birch.json +95 -0
  1265. package/src/modes/theme/defaults/dark-abyss.json +91 -0
  1266. package/src/modes/theme/defaults/dark-arctic.json +104 -0
  1267. package/src/modes/theme/defaults/dark-aurora.json +95 -0
  1268. package/src/modes/theme/defaults/dark-catppuccin.json +107 -0
  1269. package/src/modes/theme/defaults/dark-cavern.json +91 -0
  1270. package/src/modes/theme/defaults/dark-copper.json +95 -0
  1271. package/src/modes/theme/defaults/dark-cosmos.json +90 -0
  1272. package/src/modes/theme/defaults/dark-cyberpunk.json +102 -0
  1273. package/src/modes/theme/defaults/dark-dracula.json +98 -0
  1274. package/src/modes/theme/defaults/dark-eclipse.json +91 -0
  1275. package/src/modes/theme/defaults/dark-ember.json +95 -0
  1276. package/src/modes/theme/defaults/dark-equinox.json +90 -0
  1277. package/src/modes/theme/defaults/dark-forest.json +96 -0
  1278. package/src/modes/theme/defaults/dark-github.json +105 -0
  1279. package/src/modes/theme/defaults/dark-gruvbox.json +112 -0
  1280. package/src/modes/theme/defaults/dark-lavender.json +95 -0
  1281. package/src/modes/theme/defaults/dark-lunar.json +89 -0
  1282. package/src/modes/theme/defaults/dark-midnight.json +95 -0
  1283. package/src/modes/theme/defaults/dark-monochrome.json +94 -0
  1284. package/src/modes/theme/defaults/dark-monokai.json +98 -0
  1285. package/src/modes/theme/defaults/dark-nebula.json +90 -0
  1286. package/src/modes/theme/defaults/dark-nord.json +97 -0
  1287. package/src/modes/theme/defaults/dark-ocean.json +101 -0
  1288. package/src/modes/theme/defaults/dark-one.json +100 -0
  1289. package/src/modes/theme/defaults/dark-poimandres.json +142 -0
  1290. package/src/modes/theme/defaults/dark-rainforest.json +91 -0
  1291. package/src/modes/theme/defaults/dark-reef.json +91 -0
  1292. package/src/modes/theme/defaults/dark-retro.json +92 -0
  1293. package/src/modes/theme/defaults/dark-rose-pine.json +96 -0
  1294. package/src/modes/theme/defaults/dark-sakura.json +95 -0
  1295. package/src/modes/theme/defaults/dark-slate.json +95 -0
  1296. package/src/modes/theme/defaults/dark-solarized.json +97 -0
  1297. package/src/modes/theme/defaults/dark-solstice.json +90 -0
  1298. package/src/modes/theme/defaults/dark-starfall.json +91 -0
  1299. package/src/modes/theme/defaults/dark-sunset.json +99 -0
  1300. package/src/modes/theme/defaults/dark-swamp.json +90 -0
  1301. package/src/modes/theme/defaults/dark-synthwave.json +103 -0
  1302. package/src/modes/theme/defaults/dark-taiga.json +91 -0
  1303. package/src/modes/theme/defaults/dark-terminal.json +95 -0
  1304. package/src/modes/theme/defaults/dark-tokyo-night.json +101 -0
  1305. package/src/modes/theme/defaults/dark-tundra.json +91 -0
  1306. package/src/modes/theme/defaults/dark-twilight.json +91 -0
  1307. package/src/modes/theme/defaults/dark-volcanic.json +91 -0
  1308. package/src/modes/theme/defaults/graphite.json +92 -0
  1309. package/src/modes/theme/defaults/index.ts +201 -0
  1310. package/src/modes/theme/defaults/light-arctic.json +107 -0
  1311. package/src/modes/theme/defaults/light-aurora-day.json +91 -0
  1312. package/src/modes/theme/defaults/light-canyon.json +91 -0
  1313. package/src/modes/theme/defaults/light-catppuccin.json +106 -0
  1314. package/src/modes/theme/defaults/light-cirrus.json +90 -0
  1315. package/src/modes/theme/defaults/light-coral.json +95 -0
  1316. package/src/modes/theme/defaults/light-cyberpunk.json +96 -0
  1317. package/src/modes/theme/defaults/light-dawn.json +90 -0
  1318. package/src/modes/theme/defaults/light-dunes.json +91 -0
  1319. package/src/modes/theme/defaults/light-eucalyptus.json +95 -0
  1320. package/src/modes/theme/defaults/light-forest.json +100 -0
  1321. package/src/modes/theme/defaults/light-frost.json +95 -0
  1322. package/src/modes/theme/defaults/light-github.json +115 -0
  1323. package/src/modes/theme/defaults/light-glacier.json +91 -0
  1324. package/src/modes/theme/defaults/light-gruvbox.json +108 -0
  1325. package/src/modes/theme/defaults/light-haze.json +90 -0
  1326. package/src/modes/theme/defaults/light-honeycomb.json +95 -0
  1327. package/src/modes/theme/defaults/light-lagoon.json +91 -0
  1328. package/src/modes/theme/defaults/light-lavender.json +95 -0
  1329. package/src/modes/theme/defaults/light-meadow.json +91 -0
  1330. package/src/modes/theme/defaults/light-mint.json +95 -0
  1331. package/src/modes/theme/defaults/light-monochrome.json +101 -0
  1332. package/src/modes/theme/defaults/light-ocean.json +99 -0
  1333. package/src/modes/theme/defaults/light-one.json +99 -0
  1334. package/src/modes/theme/defaults/light-opal.json +91 -0
  1335. package/src/modes/theme/defaults/light-orchard.json +91 -0
  1336. package/src/modes/theme/defaults/light-paper.json +95 -0
  1337. package/src/modes/theme/defaults/light-poimandres.json +142 -0
  1338. package/src/modes/theme/defaults/light-prism.json +90 -0
  1339. package/src/modes/theme/defaults/light-retro.json +98 -0
  1340. package/src/modes/theme/defaults/light-sand.json +95 -0
  1341. package/src/modes/theme/defaults/light-savanna.json +91 -0
  1342. package/src/modes/theme/defaults/light-solarized.json +102 -0
  1343. package/src/modes/theme/defaults/light-soleil.json +90 -0
  1344. package/src/modes/theme/defaults/light-sunset.json +99 -0
  1345. package/src/modes/theme/defaults/light-synthwave.json +98 -0
  1346. package/src/modes/theme/defaults/light-tokyo-night.json +111 -0
  1347. package/src/modes/theme/defaults/light-wetland.json +91 -0
  1348. package/src/modes/theme/defaults/light-zenith.json +89 -0
  1349. package/src/modes/theme/defaults/limestone.json +94 -0
  1350. package/src/modes/theme/defaults/mahogany.json +97 -0
  1351. package/src/modes/theme/defaults/marble.json +93 -0
  1352. package/src/modes/theme/defaults/obsidian.json +91 -0
  1353. package/src/modes/theme/defaults/onyx.json +91 -0
  1354. package/src/modes/theme/defaults/pearl.json +93 -0
  1355. package/src/modes/theme/defaults/porcelain.json +91 -0
  1356. package/src/modes/theme/defaults/quartz.json +96 -0
  1357. package/src/modes/theme/defaults/red-claw.json +123 -0
  1358. package/src/modes/theme/defaults/sandstone.json +95 -0
  1359. package/src/modes/theme/defaults/titanium.json +90 -0
  1360. package/src/modes/theme/light.json +93 -0
  1361. package/src/modes/theme/mermaid-cache.ts +29 -0
  1362. package/src/modes/theme/shimmer.ts +219 -0
  1363. package/src/modes/theme/theme-schema.json +429 -0
  1364. package/src/modes/theme/theme.ts +2413 -0
  1365. package/src/modes/types.ts +313 -0
  1366. package/src/modes/utils/context-usage.ts +335 -0
  1367. package/src/modes/utils/hotkeys-markdown.ts +59 -0
  1368. package/src/modes/utils/keybinding-matchers.ts +30 -0
  1369. package/src/modes/utils/tools-markdown.ts +27 -0
  1370. package/src/modes/utils/ui-helpers.ts +741 -0
  1371. package/src/plan-mode/approved-plan.ts +163 -0
  1372. package/src/plan-mode/state.ts +6 -0
  1373. package/src/priority.json +37 -0
  1374. package/src/prompts/agents/architect.md +79 -0
  1375. package/src/prompts/agents/critic.md +55 -0
  1376. package/src/prompts/agents/executor.md +45 -0
  1377. package/src/prompts/agents/explore.md +58 -0
  1378. package/src/prompts/agents/frontmatter.md +12 -0
  1379. package/src/prompts/agents/init.md +34 -0
  1380. package/src/prompts/agents/plan.md +49 -0
  1381. package/src/prompts/agents/planner.md +49 -0
  1382. package/src/prompts/agents/reviewer.md +141 -0
  1383. package/src/prompts/agents/task.md +16 -0
  1384. package/src/prompts/ci-green-request.md +36 -0
  1385. package/src/prompts/commands/orchestrate.md +49 -0
  1386. package/src/prompts/goals/goal-budget-limit.md +16 -0
  1387. package/src/prompts/goals/goal-continuation.md +28 -0
  1388. package/src/prompts/goals/goal-mode-active.md +23 -0
  1389. package/src/prompts/memories/consolidation.md +30 -0
  1390. package/src/prompts/memories/read-path.md +11 -0
  1391. package/src/prompts/memories/stage_one_input.md +6 -0
  1392. package/src/prompts/memories/stage_one_system.md +21 -0
  1393. package/src/prompts/review-request.md +70 -0
  1394. package/src/prompts/system/agent-creation-architect.md +75 -0
  1395. package/src/prompts/system/agent-creation-user.md +6 -0
  1396. package/src/prompts/system/auto-continue.md +1 -0
  1397. package/src/prompts/system/btw-user.md +8 -0
  1398. package/src/prompts/system/commit-message-system.md +2 -0
  1399. package/src/prompts/system/custom-system-prompt.md +64 -0
  1400. package/src/prompts/system/eager-todo.md +13 -0
  1401. package/src/prompts/system/irc-incoming.md +8 -0
  1402. package/src/prompts/system/plan-mode-active.md +116 -0
  1403. package/src/prompts/system/plan-mode-approved.md +28 -0
  1404. package/src/prompts/system/plan-mode-compact-instructions.md +16 -0
  1405. package/src/prompts/system/plan-mode-reference.md +14 -0
  1406. package/src/prompts/system/plan-mode-subagent.md +34 -0
  1407. package/src/prompts/system/plan-mode-tool-decision-reminder.md +9 -0
  1408. package/src/prompts/system/project-prompt.md +46 -0
  1409. package/src/prompts/system/subagent-system-prompt.md +54 -0
  1410. package/src/prompts/system/subagent-user-prompt.md +3 -0
  1411. package/src/prompts/system/subagent-yield-reminder.md +12 -0
  1412. package/src/prompts/system/system-prompt.md +267 -0
  1413. package/src/prompts/system/title-system.md +2 -0
  1414. package/src/prompts/system/ttsr-interrupt.md +7 -0
  1415. package/src/prompts/system/ttsr-tool-reminder.md +5 -0
  1416. package/src/prompts/system/web-search.md +25 -0
  1417. package/src/prompts/tools/apply-patch.md +65 -0
  1418. package/src/prompts/tools/ask.md +29 -0
  1419. package/src/prompts/tools/ast-edit.md +39 -0
  1420. package/src/prompts/tools/ast-grep.md +42 -0
  1421. package/src/prompts/tools/async-result.md +8 -0
  1422. package/src/prompts/tools/bash.md +39 -0
  1423. package/src/prompts/tools/browser.md +70 -0
  1424. package/src/prompts/tools/calculator.md +10 -0
  1425. package/src/prompts/tools/checkpoint.md +16 -0
  1426. package/src/prompts/tools/create-goal.md +3 -0
  1427. package/src/prompts/tools/debug.md +33 -0
  1428. package/src/prompts/tools/eval.md +75 -0
  1429. package/src/prompts/tools/find.md +34 -0
  1430. package/src/prompts/tools/get-goal.md +3 -0
  1431. package/src/prompts/tools/github.md +20 -0
  1432. package/src/prompts/tools/goal.md +18 -0
  1433. package/src/prompts/tools/hashline.md +130 -0
  1434. package/src/prompts/tools/image-gen.md +7 -0
  1435. package/src/prompts/tools/inspect-image-system.md +20 -0
  1436. package/src/prompts/tools/inspect-image.md +32 -0
  1437. package/src/prompts/tools/irc.md +49 -0
  1438. package/src/prompts/tools/job.md +19 -0
  1439. package/src/prompts/tools/lsp.md +42 -0
  1440. package/src/prompts/tools/patch.md +70 -0
  1441. package/src/prompts/tools/read.md +82 -0
  1442. package/src/prompts/tools/recall.md +5 -0
  1443. package/src/prompts/tools/recipe.md +16 -0
  1444. package/src/prompts/tools/reflect.md +5 -0
  1445. package/src/prompts/tools/render-mermaid.md +9 -0
  1446. package/src/prompts/tools/replace.md +36 -0
  1447. package/src/prompts/tools/resolve.md +9 -0
  1448. package/src/prompts/tools/retain.md +6 -0
  1449. package/src/prompts/tools/rewind.md +13 -0
  1450. package/src/prompts/tools/search-tool-bm25.md +33 -0
  1451. package/src/prompts/tools/search.md +25 -0
  1452. package/src/prompts/tools/ssh.md +35 -0
  1453. package/src/prompts/tools/subagent.md +21 -0
  1454. package/src/prompts/tools/task-summary.md +28 -0
  1455. package/src/prompts/tools/task.md +79 -0
  1456. package/src/prompts/tools/todo-write.md +50 -0
  1457. package/src/prompts/tools/update-goal.md +3 -0
  1458. package/src/prompts/tools/vim.md +98 -0
  1459. package/src/prompts/tools/web-search.md +10 -0
  1460. package/src/prompts/tools/write.md +14 -0
  1461. package/src/registry/agent-registry.ts +140 -0
  1462. package/src/runtime-mcp/client.ts +482 -0
  1463. package/src/runtime-mcp/config-writer.ts +225 -0
  1464. package/src/runtime-mcp/config.ts +365 -0
  1465. package/src/runtime-mcp/discoverable-tool-metadata.ts +25 -0
  1466. package/src/runtime-mcp/index.ts +29 -0
  1467. package/src/runtime-mcp/json-rpc.ts +84 -0
  1468. package/src/runtime-mcp/loader.ts +124 -0
  1469. package/src/runtime-mcp/manager.ts +1172 -0
  1470. package/src/runtime-mcp/oauth-discovery.ts +349 -0
  1471. package/src/runtime-mcp/oauth-flow.ts +407 -0
  1472. package/src/runtime-mcp/render.ts +123 -0
  1473. package/src/runtime-mcp/smithery-auth.ts +104 -0
  1474. package/src/runtime-mcp/smithery-connect.ts +145 -0
  1475. package/src/runtime-mcp/smithery-registry.ts +477 -0
  1476. package/src/runtime-mcp/tool-bridge.ts +416 -0
  1477. package/src/runtime-mcp/tool-cache.ts +117 -0
  1478. package/src/runtime-mcp/transports/http.ts +477 -0
  1479. package/src/runtime-mcp/transports/index.ts +6 -0
  1480. package/src/runtime-mcp/transports/stdio.ts +325 -0
  1481. package/src/runtime-mcp/types.ts +423 -0
  1482. package/src/sdk.ts +2064 -0
  1483. package/src/secrets/index.ts +116 -0
  1484. package/src/secrets/obfuscator.ts +277 -0
  1485. package/src/secrets/regex.ts +21 -0
  1486. package/src/session/agent-session.ts +8794 -0
  1487. package/src/session/agent-storage.ts +466 -0
  1488. package/src/session/artifacts.ts +135 -0
  1489. package/src/session/auth-broker-config.ts +102 -0
  1490. package/src/session/auth-storage.ts +23 -0
  1491. package/src/session/blob-store.ts +168 -0
  1492. package/src/session/client-bridge.ts +85 -0
  1493. package/src/session/history-storage.ts +311 -0
  1494. package/src/session/messages.ts +403 -0
  1495. package/src/session/session-dump-format.ts +209 -0
  1496. package/src/session/session-manager.ts +3333 -0
  1497. package/src/session/session-storage.ts +389 -0
  1498. package/src/session/streaming-output.ts +1093 -0
  1499. package/src/session/tool-choice-queue.ts +213 -0
  1500. package/src/session/yield-queue.ts +155 -0
  1501. package/src/setup/model-onboarding-guidance.ts +36 -0
  1502. package/src/setup/provider-onboarding.ts +195 -0
  1503. package/src/skill-state/active-state.ts +272 -0
  1504. package/src/slash-commands/acp-builtins.ts +46 -0
  1505. package/src/slash-commands/builtin-registry.ts +955 -0
  1506. package/src/slash-commands/helpers/context-report.ts +39 -0
  1507. package/src/slash-commands/helpers/format.ts +46 -0
  1508. package/src/slash-commands/helpers/mcp.ts +532 -0
  1509. package/src/slash-commands/helpers/parse.ts +85 -0
  1510. package/src/slash-commands/helpers/ssh.ts +195 -0
  1511. package/src/slash-commands/helpers/todo.ts +279 -0
  1512. package/src/slash-commands/helpers/usage-report.ts +91 -0
  1513. package/src/slash-commands/types.ts +125 -0
  1514. package/src/ssh/config-writer.ts +183 -0
  1515. package/src/ssh/connection-manager.ts +482 -0
  1516. package/src/ssh/ssh-executor.ts +133 -0
  1517. package/src/ssh/sshfs-mount.ts +140 -0
  1518. package/src/ssh/utils.ts +8 -0
  1519. package/src/stt/downloader.ts +71 -0
  1520. package/src/stt/index.ts +3 -0
  1521. package/src/stt/recorder.ts +351 -0
  1522. package/src/stt/setup.ts +52 -0
  1523. package/src/stt/stt-controller.ts +160 -0
  1524. package/src/stt/transcribe.py +70 -0
  1525. package/src/stt/transcriber.ts +91 -0
  1526. package/src/system-prompt.ts +581 -0
  1527. package/src/task/agents.ts +162 -0
  1528. package/src/task/commands.ts +135 -0
  1529. package/src/task/discovery.ts +130 -0
  1530. package/src/task/executor.ts +1564 -0
  1531. package/src/task/gjc-command.ts +26 -0
  1532. package/src/task/index.ts +1366 -0
  1533. package/src/task/name-generator.ts +1577 -0
  1534. package/src/task/output-manager.ts +107 -0
  1535. package/src/task/parallel.ts +116 -0
  1536. package/src/task/render.ts +1136 -0
  1537. package/src/task/simple-mode.ts +27 -0
  1538. package/src/task/subprocess-tool-registry.ts +88 -0
  1539. package/src/task/types.ts +315 -0
  1540. package/src/task/worktree.ts +506 -0
  1541. package/src/thinking.ts +87 -0
  1542. package/src/tool-discovery/tool-index.ts +400 -0
  1543. package/src/tools/archive-reader.ts +321 -0
  1544. package/src/tools/ask.ts +790 -0
  1545. package/src/tools/ast-edit.ts +542 -0
  1546. package/src/tools/ast-grep.ts +423 -0
  1547. package/src/tools/auto-generated-guard.ts +305 -0
  1548. package/src/tools/bash-command-fixup.ts +37 -0
  1549. package/src/tools/bash-interactive.ts +388 -0
  1550. package/src/tools/bash-interceptor.ts +67 -0
  1551. package/src/tools/bash-pty-selection.ts +14 -0
  1552. package/src/tools/bash-skill-urls.ts +248 -0
  1553. package/src/tools/bash.ts +1061 -0
  1554. package/src/tools/browser/attach.ts +175 -0
  1555. package/src/tools/browser/launch.ts +651 -0
  1556. package/src/tools/browser/readable.ts +95 -0
  1557. package/src/tools/browser/registry.ts +194 -0
  1558. package/src/tools/browser/render.ts +212 -0
  1559. package/src/tools/browser/tab-protocol.ts +105 -0
  1560. package/src/tools/browser/tab-supervisor.ts +577 -0
  1561. package/src/tools/browser/tab-worker-entry.ts +21 -0
  1562. package/src/tools/browser/tab-worker.ts +1054 -0
  1563. package/src/tools/browser.ts +301 -0
  1564. package/src/tools/calculator.ts +540 -0
  1565. package/src/tools/checkpoint.ts +134 -0
  1566. package/src/tools/conflict-detect.ts +672 -0
  1567. package/src/tools/context.ts +39 -0
  1568. package/src/tools/debug.ts +1014 -0
  1569. package/src/tools/eval.ts +1101 -0
  1570. package/src/tools/fetch.ts +1482 -0
  1571. package/src/tools/file-recorder.ts +35 -0
  1572. package/src/tools/find.ts +540 -0
  1573. package/src/tools/fs-cache-invalidation.ts +28 -0
  1574. package/src/tools/gh-format.ts +12 -0
  1575. package/src/tools/gh-renderer.ts +428 -0
  1576. package/src/tools/gh.ts +3499 -0
  1577. package/src/tools/github-cache.ts +548 -0
  1578. package/src/tools/grouped-file-output.ts +96 -0
  1579. package/src/tools/hindsight-recall.ts +68 -0
  1580. package/src/tools/hindsight-reflect.ts +57 -0
  1581. package/src/tools/hindsight-retain.ts +56 -0
  1582. package/src/tools/image-gen.ts +1248 -0
  1583. package/src/tools/index.ts +519 -0
  1584. package/src/tools/inspect-image-renderer.ts +103 -0
  1585. package/src/tools/inspect-image.ts +165 -0
  1586. package/src/tools/irc.ts +239 -0
  1587. package/src/tools/job.ts +520 -0
  1588. package/src/tools/json-tree.ts +243 -0
  1589. package/src/tools/jtd-to-json-schema.ts +219 -0
  1590. package/src/tools/jtd-to-typescript.ts +136 -0
  1591. package/src/tools/jtd-utils.ts +102 -0
  1592. package/src/tools/list-limit.ts +40 -0
  1593. package/src/tools/match-line-format.ts +22 -0
  1594. package/src/tools/output-meta.ts +754 -0
  1595. package/src/tools/path-utils.ts +739 -0
  1596. package/src/tools/plan-mode-guard.ts +68 -0
  1597. package/src/tools/puppeteer/00_stealth_tampering.txt +63 -0
  1598. package/src/tools/puppeteer/01_stealth_activity.txt +20 -0
  1599. package/src/tools/puppeteer/02_stealth_hairline.txt +11 -0
  1600. package/src/tools/puppeteer/03_stealth_botd.txt +384 -0
  1601. package/src/tools/puppeteer/04_stealth_iframe.txt +81 -0
  1602. package/src/tools/puppeteer/05_stealth_webgl.txt +75 -0
  1603. package/src/tools/puppeteer/06_stealth_screen.txt +72 -0
  1604. package/src/tools/puppeteer/07_stealth_fonts.txt +97 -0
  1605. package/src/tools/puppeteer/08_stealth_audio.txt +51 -0
  1606. package/src/tools/puppeteer/09_stealth_locale.txt +46 -0
  1607. package/src/tools/puppeteer/10_stealth_plugins.txt +206 -0
  1608. package/src/tools/puppeteer/11_stealth_hardware.txt +8 -0
  1609. package/src/tools/puppeteer/12_stealth_codecs.txt +40 -0
  1610. package/src/tools/puppeteer/13_stealth_worker.txt +74 -0
  1611. package/src/tools/read.ts +2332 -0
  1612. package/src/tools/recipe/index.ts +80 -0
  1613. package/src/tools/recipe/render.ts +19 -0
  1614. package/src/tools/recipe/runner.ts +219 -0
  1615. package/src/tools/recipe/runners/cargo.ts +131 -0
  1616. package/src/tools/recipe/runners/index.ts +8 -0
  1617. package/src/tools/recipe/runners/just.ts +73 -0
  1618. package/src/tools/recipe/runners/make.ts +101 -0
  1619. package/src/tools/recipe/runners/pkg.ts +167 -0
  1620. package/src/tools/recipe/runners/task.ts +72 -0
  1621. package/src/tools/render-mermaid.ts +68 -0
  1622. package/src/tools/render-utils.ts +774 -0
  1623. package/src/tools/renderers.ts +75 -0
  1624. package/src/tools/resolve.ts +258 -0
  1625. package/src/tools/review.ts +252 -0
  1626. package/src/tools/search-tool-bm25.ts +360 -0
  1627. package/src/tools/search.ts +786 -0
  1628. package/src/tools/sqlite-reader.ts +736 -0
  1629. package/src/tools/ssh.ts +310 -0
  1630. package/src/tools/subagent.ts +312 -0
  1631. package/src/tools/todo-write.ts +695 -0
  1632. package/src/tools/tool-errors.ts +62 -0
  1633. package/src/tools/tool-result.ts +86 -0
  1634. package/src/tools/tool-timeouts.ts +30 -0
  1635. package/src/tools/vim.ts +949 -0
  1636. package/src/tools/write.ts +953 -0
  1637. package/src/tools/yield.ts +268 -0
  1638. package/src/tui/code-cell.ts +201 -0
  1639. package/src/tui/file-list.ts +55 -0
  1640. package/src/tui/hyperlink.ts +126 -0
  1641. package/src/tui/index.ts +12 -0
  1642. package/src/tui/output-block.ts +150 -0
  1643. package/src/tui/status-line.ts +39 -0
  1644. package/src/tui/tree-list.ts +84 -0
  1645. package/src/tui/types.ts +15 -0
  1646. package/src/tui/utils.ts +103 -0
  1647. package/src/utils/changelog.ts +98 -0
  1648. package/src/utils/clipboard.ts +156 -0
  1649. package/src/utils/command-args.ts +76 -0
  1650. package/src/utils/commit-message-generator.ts +142 -0
  1651. package/src/utils/edit-mode.ts +42 -0
  1652. package/src/utils/event-bus.ts +33 -0
  1653. package/src/utils/external-editor.ts +65 -0
  1654. package/src/utils/file-display-mode.ts +45 -0
  1655. package/src/utils/file-mentions.ts +376 -0
  1656. package/src/utils/git.ts +1536 -0
  1657. package/src/utils/image-loading.ts +102 -0
  1658. package/src/utils/image-resize.ts +309 -0
  1659. package/src/utils/lang-from-path.ts +239 -0
  1660. package/src/utils/markit.ts +89 -0
  1661. package/src/utils/open.ts +20 -0
  1662. package/src/utils/session-color.ts +43 -0
  1663. package/src/utils/shell-snapshot.ts +187 -0
  1664. package/src/utils/sixel.ts +69 -0
  1665. package/src/utils/title-generator.ts +223 -0
  1666. package/src/utils/tool-choice.ts +33 -0
  1667. package/src/utils/tools-manager.ts +363 -0
  1668. package/src/vim/buffer.ts +309 -0
  1669. package/src/vim/commands.ts +382 -0
  1670. package/src/vim/engine.ts +2409 -0
  1671. package/src/vim/parser.ts +134 -0
  1672. package/src/vim/render.ts +252 -0
  1673. package/src/vim/types.ts +197 -0
  1674. package/src/web/kagi.ts +183 -0
  1675. package/src/web/parallel.ts +349 -0
  1676. package/src/web/scrapers/artifacthub.ts +207 -0
  1677. package/src/web/scrapers/arxiv.ts +83 -0
  1678. package/src/web/scrapers/aur.ts +162 -0
  1679. package/src/web/scrapers/biorxiv.ts +133 -0
  1680. package/src/web/scrapers/bluesky.ts +262 -0
  1681. package/src/web/scrapers/brew.ts +172 -0
  1682. package/src/web/scrapers/cheatsh.ts +68 -0
  1683. package/src/web/scrapers/chocolatey.ts +196 -0
  1684. package/src/web/scrapers/choosealicense.ts +95 -0
  1685. package/src/web/scrapers/cisa-kev.ts +87 -0
  1686. package/src/web/scrapers/clojars.ts +154 -0
  1687. package/src/web/scrapers/coingecko.ts +177 -0
  1688. package/src/web/scrapers/crates-io.ts +97 -0
  1689. package/src/web/scrapers/crossref.ts +136 -0
  1690. package/src/web/scrapers/devto.ts +147 -0
  1691. package/src/web/scrapers/discogs.ts +306 -0
  1692. package/src/web/scrapers/discourse.ts +197 -0
  1693. package/src/web/scrapers/dockerhub.ts +138 -0
  1694. package/src/web/scrapers/docs-rs.ts +653 -0
  1695. package/src/web/scrapers/fdroid.ts +134 -0
  1696. package/src/web/scrapers/firefox-addons.ts +191 -0
  1697. package/src/web/scrapers/flathub.ts +223 -0
  1698. package/src/web/scrapers/github-gist.ts +58 -0
  1699. package/src/web/scrapers/github.ts +452 -0
  1700. package/src/web/scrapers/gitlab.ts +401 -0
  1701. package/src/web/scrapers/go-pkg.ts +266 -0
  1702. package/src/web/scrapers/hackage.ts +140 -0
  1703. package/src/web/scrapers/hackernews.ts +189 -0
  1704. package/src/web/scrapers/hex.ts +105 -0
  1705. package/src/web/scrapers/huggingface.ts +321 -0
  1706. package/src/web/scrapers/iacr.ts +89 -0
  1707. package/src/web/scrapers/index.ts +252 -0
  1708. package/src/web/scrapers/jetbrains-marketplace.ts +159 -0
  1709. package/src/web/scrapers/lemmy.ts +203 -0
  1710. package/src/web/scrapers/lobsters.ts +175 -0
  1711. package/src/web/scrapers/mastodon.ts +292 -0
  1712. package/src/web/scrapers/maven.ts +138 -0
  1713. package/src/web/scrapers/mdn.ts +173 -0
  1714. package/src/web/scrapers/metacpan.ts +222 -0
  1715. package/src/web/scrapers/musicbrainz.ts +250 -0
  1716. package/src/web/scrapers/npm.ts +98 -0
  1717. package/src/web/scrapers/nuget.ts +183 -0
  1718. package/src/web/scrapers/nvd.ts +222 -0
  1719. package/src/web/scrapers/ollama.ts +239 -0
  1720. package/src/web/scrapers/open-vsx.ts +106 -0
  1721. package/src/web/scrapers/opencorporates.ts +292 -0
  1722. package/src/web/scrapers/openlibrary.ts +336 -0
  1723. package/src/web/scrapers/orcid.ts +286 -0
  1724. package/src/web/scrapers/osv.ts +176 -0
  1725. package/src/web/scrapers/packagist.ts +160 -0
  1726. package/src/web/scrapers/pub-dev.ts +143 -0
  1727. package/src/web/scrapers/pubmed.ts +211 -0
  1728. package/src/web/scrapers/pypi.ts +112 -0
  1729. package/src/web/scrapers/rawg.ts +110 -0
  1730. package/src/web/scrapers/readthedocs.ts +120 -0
  1731. package/src/web/scrapers/reddit.ts +95 -0
  1732. package/src/web/scrapers/repology.ts +251 -0
  1733. package/src/web/scrapers/rfc.ts +201 -0
  1734. package/src/web/scrapers/rubygems.ts +103 -0
  1735. package/src/web/scrapers/searchcode.ts +189 -0
  1736. package/src/web/scrapers/sec-edgar.ts +261 -0
  1737. package/src/web/scrapers/semantic-scholar.ts +171 -0
  1738. package/src/web/scrapers/snapcraft.ts +187 -0
  1739. package/src/web/scrapers/sourcegraph.ts +336 -0
  1740. package/src/web/scrapers/spdx.ts +108 -0
  1741. package/src/web/scrapers/spotify.ts +198 -0
  1742. package/src/web/scrapers/stackoverflow.ts +120 -0
  1743. package/src/web/scrapers/terraform.ts +277 -0
  1744. package/src/web/scrapers/tldr.ts +47 -0
  1745. package/src/web/scrapers/twitter.ts +93 -0
  1746. package/src/web/scrapers/types.ts +318 -0
  1747. package/src/web/scrapers/utils.ts +109 -0
  1748. package/src/web/scrapers/vimeo.ts +133 -0
  1749. package/src/web/scrapers/vscode-marketplace.ts +187 -0
  1750. package/src/web/scrapers/w3c.ts +156 -0
  1751. package/src/web/scrapers/wikidata.ts +344 -0
  1752. package/src/web/scrapers/wikipedia.ts +84 -0
  1753. package/src/web/scrapers/youtube.ts +319 -0
  1754. package/src/web/search/index.ts +288 -0
  1755. package/src/web/search/provider.ts +173 -0
  1756. package/src/web/search/providers/anthropic.ts +302 -0
  1757. package/src/web/search/providers/base.ts +71 -0
  1758. package/src/web/search/providers/brave.ts +149 -0
  1759. package/src/web/search/providers/codex.ts +556 -0
  1760. package/src/web/search/providers/exa.ts +193 -0
  1761. package/src/web/search/providers/gemini.ts +455 -0
  1762. package/src/web/search/providers/jina.ts +101 -0
  1763. package/src/web/search/providers/kagi.ts +75 -0
  1764. package/src/web/search/providers/kimi.ts +171 -0
  1765. package/src/web/search/providers/parallel.ts +210 -0
  1766. package/src/web/search/providers/perplexity.ts +575 -0
  1767. package/src/web/search/providers/searxng.ts +309 -0
  1768. package/src/web/search/providers/synthetic.ts +108 -0
  1769. package/src/web/search/providers/tavily.ts +173 -0
  1770. package/src/web/search/providers/utils.ts +128 -0
  1771. package/src/web/search/providers/zai.ts +320 -0
  1772. package/src/web/search/render.ts +299 -0
  1773. package/src/web/search/types.ts +436 -0
  1774. package/src/web/search/utils.ts +17 -0
  1775. package/src/workspace-tree.ts +286 -0
@@ -0,0 +1,3333 @@
1
+ import * as fs from "node:fs";
2
+ import * as os from "node:os";
3
+ import * as path from "node:path";
4
+ import type { AgentMessage } from "@gajae-code/agent-core";
5
+ import type {
6
+ ImageContent,
7
+ Message,
8
+ MessageAttribution,
9
+ ProviderPayload,
10
+ ServiceTier,
11
+ TextContent,
12
+ Usage,
13
+ } from "@gajae-code/ai";
14
+ import { getTerminalId } from "@gajae-code/tui";
15
+ import {
16
+ getBlobsDir,
17
+ getAgentDir as getDefaultAgentDir,
18
+ getProjectDir,
19
+ getSessionsDir,
20
+ getTerminalSessionsDir,
21
+ hasFsCode,
22
+ isEnoent,
23
+ logger,
24
+ parseJsonlLenient,
25
+ pathIsWithin,
26
+ resolveEquivalentPath,
27
+ Snowflake,
28
+ toError,
29
+ } from "@gajae-code/utils";
30
+ import { ArtifactManager } from "./artifacts";
31
+ import {
32
+ type BlobPutResult,
33
+ BlobStore,
34
+ externalizeImageData,
35
+ externalizeImageDataSync,
36
+ externalizeImageDataUrl,
37
+ externalizeImageDataUrlSync,
38
+ isBlobRef,
39
+ isImageDataUrl,
40
+ resolveImageData,
41
+ resolveImageDataUrl,
42
+ } from "./blob-store";
43
+ import {
44
+ type BashExecutionMessage,
45
+ type CustomMessage,
46
+ createBranchSummaryMessage,
47
+ createCompactionSummaryMessage,
48
+ createCustomMessage,
49
+ type FileMentionMessage,
50
+ type HookMessage,
51
+ type PythonExecutionMessage,
52
+ sanitizeRehydratedOpenAIResponsesAssistantMessage,
53
+ stripInternalDetailsFields,
54
+ } from "./messages";
55
+ import type { SessionStorage, SessionStorageWriter } from "./session-storage";
56
+ import { FileSessionStorage, MemorySessionStorage } from "./session-storage";
57
+
58
+ export const CURRENT_SESSION_VERSION = 3;
59
+
60
+ export interface SessionHeader {
61
+ type: "session";
62
+ version?: number; // v1 sessions don't have this
63
+ id: string;
64
+ title?: string; // Auto-generated title from first message
65
+ titleSource?: "auto" | "user";
66
+ timestamp: string;
67
+ cwd: string;
68
+ parentSession?: string;
69
+ }
70
+
71
+ export interface NewSessionOptions {
72
+ parentSession?: string;
73
+ /** Skip flushing the current session and delete it instead of saving. */
74
+ drop?: boolean;
75
+ }
76
+
77
+ export interface SessionEntryBase {
78
+ type: string;
79
+ id: string;
80
+ parentId: string | null;
81
+ timestamp: string;
82
+ }
83
+
84
+ export interface SessionMessageEntry extends SessionEntryBase {
85
+ type: "message";
86
+ message: AgentMessage;
87
+ }
88
+
89
+ export interface ThinkingLevelChangeEntry extends SessionEntryBase {
90
+ type: "thinking_level_change";
91
+ thinkingLevel?: string | null;
92
+ }
93
+
94
+ export interface ModelChangeEntry extends SessionEntryBase {
95
+ type: "model_change";
96
+ /** Model in "provider/modelId" format */
97
+ model: string;
98
+ /** Role: "default", "smol", "slow", etc. Undefined treated as "default" */
99
+ role?: string;
100
+ }
101
+
102
+ export interface ServiceTierChangeEntry extends SessionEntryBase {
103
+ type: "service_tier_change";
104
+ serviceTier: ServiceTier | null;
105
+ }
106
+
107
+ export interface CompactionEntry<T = unknown> extends SessionEntryBase {
108
+ type: "compaction";
109
+ summary: string;
110
+ shortSummary?: string;
111
+ firstKeptEntryId: string;
112
+ tokensBefore: number;
113
+ /** Extension-specific data (e.g., ArtifactIndex, version markers for structured compaction) */
114
+ details?: T;
115
+ /** Hook-provided data to persist across compaction */
116
+ preserveData?: Record<string, unknown>;
117
+ /** True if generated by an extension, undefined/false if pi-generated (backward compatible) */
118
+ fromExtension?: boolean;
119
+ }
120
+
121
+ export interface BranchSummaryEntry<T = unknown> extends SessionEntryBase {
122
+ type: "branch_summary";
123
+ fromId: string;
124
+ summary: string;
125
+ /** Extension-specific data (not sent to LLM) */
126
+ details?: T;
127
+ /** True if generated by an extension, false if pi-generated */
128
+ fromExtension?: boolean;
129
+ }
130
+
131
+ /**
132
+ * Custom entry for extensions to store extension-specific data in the session.
133
+ * Use customType to identify your extension's entries.
134
+ *
135
+ * Purpose: Persist extension state across session reloads. On reload, extensions can
136
+ * scan entries for their customType and reconstruct internal state.
137
+ *
138
+ * Does NOT participate in LLM context (ignored by buildSessionContext).
139
+ * For injecting content into context, see CustomMessageEntry.
140
+ */
141
+ export interface CustomEntry<T = unknown> extends SessionEntryBase {
142
+ type: "custom";
143
+ customType: string;
144
+ data?: T;
145
+ }
146
+
147
+ /** Label entry for user-defined bookmarks/markers on entries. */
148
+ export interface LabelEntry extends SessionEntryBase {
149
+ type: "label";
150
+ targetId: string;
151
+ label: string | undefined;
152
+ }
153
+
154
+ /** TTSR injection entry - tracks which time-traveling rules have been injected this session. */
155
+ export interface TtsrInjectionEntry extends SessionEntryBase {
156
+ type: "ttsr_injection";
157
+ /** Names of rules that were injected */
158
+ injectedRules: string[];
159
+ }
160
+
161
+ /** Persisted MCP discovery selection state for a session branch. */
162
+ export interface MCPToolSelectionEntry extends SessionEntryBase {
163
+ type: "mcp_tool_selection";
164
+ /** MCP tool names selected for visibility in discovery mode. */
165
+ selectedToolNames: string[];
166
+ }
167
+
168
+ /** Session init entry - captures initial context for subagent sessions (debugging/replay). */
169
+ export interface SessionInitEntry extends SessionEntryBase {
170
+ type: "session_init";
171
+ /** Full system prompt sent to the model */
172
+ systemPrompt: string;
173
+ /** Initial task/user message */
174
+ task: string;
175
+ /** Tools available to the agent */
176
+ tools: string[];
177
+ /** Output schema if structured output was requested */
178
+ outputSchema?: unknown;
179
+ }
180
+
181
+ /** Mode change entry - tracks agent mode transitions (e.g. plan mode). */
182
+ export interface ModeChangeEntry extends SessionEntryBase {
183
+ type: "mode_change";
184
+ /** Current mode name, or "none" when exiting a mode */
185
+ mode: string;
186
+ /** Optional mode-specific data (e.g. plan file path) */
187
+ data?: Record<string, unknown>;
188
+ }
189
+
190
+ /**
191
+ * Custom message entry for extensions to inject messages into LLM context.
192
+ * Use customType to identify your extension's entries.
193
+ *
194
+ * Unlike CustomEntry, this DOES participate in LLM context.
195
+ * The content participates in LLM context through convertToLlm().
196
+ * Use details for extension-specific metadata (not sent to LLM).
197
+ *
198
+ * display controls TUI rendering:
199
+ * - false: hidden entirely
200
+ * - true: rendered with distinct styling (different from user messages)
201
+ */
202
+ export interface CustomMessageEntry<T = unknown> extends SessionEntryBase {
203
+ type: "custom_message";
204
+ customType: string;
205
+ content: string | (TextContent | ImageContent)[];
206
+ details?: T;
207
+ display: boolean;
208
+ /** Who initiated this message for billing/attribution semantics. */
209
+ attribution?: MessageAttribution;
210
+ }
211
+
212
+ /** Session entry - has id/parentId for tree structure (returned by "read" methods in SessionManager) */
213
+ export type SessionEntry =
214
+ | SessionMessageEntry
215
+ | ThinkingLevelChangeEntry
216
+ | ModelChangeEntry
217
+ | ServiceTierChangeEntry
218
+ | CompactionEntry
219
+ | BranchSummaryEntry
220
+ | CustomEntry
221
+ | CustomMessageEntry
222
+ | LabelEntry
223
+ | TtsrInjectionEntry
224
+ | MCPToolSelectionEntry
225
+ | SessionInitEntry
226
+ | ModeChangeEntry;
227
+
228
+ /** Raw file entry (includes header) */
229
+ export type FileEntry = SessionHeader | SessionEntry;
230
+
231
+ /** Tree node for getTree() - defensive copy of session structure */
232
+ export interface SessionTreeNode {
233
+ entry: SessionEntry;
234
+ children: SessionTreeNode[];
235
+ /** Resolved label for this entry, if any */
236
+ label?: string;
237
+ }
238
+
239
+ export interface SessionContext {
240
+ messages: AgentMessage[];
241
+ thinkingLevel?: string;
242
+ serviceTier?: ServiceTier;
243
+ /** Model roles: { default: "provider/modelId", small: "provider/modelId", ... } */
244
+ models: Record<string, string>;
245
+ /** Names of TTSR rules that have been injected this session */
246
+ injectedTtsrRules: string[];
247
+ /** MCP tool names selected through discovery for this session branch. */
248
+ selectedMCPToolNames: string[];
249
+ /** Whether this branch contains an explicit persisted MCP selection entry. */
250
+ hasPersistedMCPToolSelection: boolean;
251
+ /** Active mode (e.g. "plan") or "none" if no special mode is active */
252
+ mode: string;
253
+ /** Mode-specific data from the last mode_change entry */
254
+ modeData?: Record<string, unknown>;
255
+ }
256
+
257
+ export interface SessionInfo {
258
+ path: string;
259
+ id: string;
260
+ /** Working directory where the session was started. Empty string for old sessions. */
261
+ cwd: string;
262
+ title?: string;
263
+ /** Path to the parent session (if this session was forked). */
264
+ parentSessionPath?: string;
265
+ created: Date;
266
+ modified: Date;
267
+ messageCount: number;
268
+ /** File size in bytes on disk; used for compact list rendering. */
269
+ size: number;
270
+ firstMessage: string;
271
+ allMessagesText: string;
272
+ }
273
+
274
+ export type ReadonlySessionManager = Pick<
275
+ SessionManager,
276
+ | "getCwd"
277
+ | "getSessionDir"
278
+ | "getSessionId"
279
+ | "getSessionFile"
280
+ | "getSessionName"
281
+ | "getArtifactsDir"
282
+ | "getArtifactManager"
283
+ | "allocateArtifactPath"
284
+ | "saveArtifact"
285
+ | "getArtifactPath"
286
+ | "getLeafId"
287
+ | "getLeafEntry"
288
+ | "getEntry"
289
+ | "getLabel"
290
+ | "getBranch"
291
+ | "getHeader"
292
+ | "getEntries"
293
+ | "getTree"
294
+ | "getUsageStatistics"
295
+ | "putBlob"
296
+ >;
297
+
298
+ function createSessionId(): string {
299
+ return Bun.randomUUIDv7();
300
+ }
301
+
302
+ /** Generate a unique short ID (8 hex chars, collision-checked) */
303
+ function generateId(byId: { has(id: string): boolean }): string {
304
+ for (let i = 0; i < 100; i++) {
305
+ const id = crypto.randomUUID().slice(-8);
306
+ if (!byId.has(id)) return id;
307
+ }
308
+ return Snowflake.next(); // fallback to full snowflake id
309
+ }
310
+
311
+ /** Migrate v1 → v2: add id/parentId tree structure. Mutates in place. */
312
+ function migrateV1ToV2(entries: FileEntry[]): void {
313
+ const ids = new Set<string>();
314
+ let prevId: string | null = null;
315
+
316
+ for (const entry of entries) {
317
+ if (entry.type === "session") {
318
+ entry.version = 2;
319
+ continue;
320
+ }
321
+
322
+ entry.id = generateId(ids);
323
+ entry.parentId = prevId;
324
+ prevId = entry.id;
325
+
326
+ // Convert firstKeptEntryIndex to firstKeptEntryId for compaction
327
+ if (entry.type === "compaction") {
328
+ const comp = entry as CompactionEntry & { firstKeptEntryIndex?: number };
329
+ if (typeof comp.firstKeptEntryIndex === "number") {
330
+ const targetEntry = entries[comp.firstKeptEntryIndex];
331
+ if (targetEntry && targetEntry.type !== "session") {
332
+ comp.firstKeptEntryId = targetEntry.id;
333
+ }
334
+ delete comp.firstKeptEntryIndex;
335
+ }
336
+ }
337
+ }
338
+ }
339
+
340
+ /** Migrate v2 → v3: rename hookMessage role to custom. Mutates in place. */
341
+ function migrateV2ToV3(entries: FileEntry[]): void {
342
+ for (const entry of entries) {
343
+ if (entry.type === "session") {
344
+ entry.version = 3;
345
+ continue;
346
+ }
347
+
348
+ if (entry.type === "message") {
349
+ const msg = entry.message as { role?: string };
350
+ if (msg.role === "hookMessage") {
351
+ (entry.message as { role: string }).role = "custom";
352
+ }
353
+ }
354
+ }
355
+ }
356
+
357
+ /**
358
+ * Run all necessary migrations to bring entries to current version.
359
+ * Mutates entries in place. Returns true if any migration was applied.
360
+ */
361
+ function migrateToCurrentVersion(entries: FileEntry[]): boolean {
362
+ const header = entries.find(e => e.type === "session") as SessionHeader | undefined;
363
+ const version = header?.version ?? 1;
364
+
365
+ if (version >= CURRENT_SESSION_VERSION) return false;
366
+
367
+ if (version < 2) migrateV1ToV2(entries);
368
+ if (version < 3) migrateV2ToV3(entries);
369
+
370
+ return true;
371
+ }
372
+
373
+ /** Exported for testing */
374
+ export function migrateSessionEntries(entries: FileEntry[]): void {
375
+ migrateToCurrentVersion(entries);
376
+ }
377
+
378
+ const migratedSessionRoots = new Set<string>();
379
+
380
+ /**
381
+ * Merge or rename a legacy session directory into its canonical target.
382
+ * Best effort: callers decide whether migration failures should surface.
383
+ */
384
+ function migrateSessionDirPath(oldPath: string, newPath: string): void {
385
+ const existing = fs.statSync(newPath, { throwIfNoEntry: false });
386
+ if (existing?.isDirectory()) {
387
+ for (const file of fs.readdirSync(oldPath)) {
388
+ const src = path.join(oldPath, file);
389
+ const dst = path.join(newPath, file);
390
+ if (!fs.existsSync(dst)) {
391
+ fs.renameSync(src, dst);
392
+ }
393
+ }
394
+ fs.rmSync(oldPath, { recursive: true, force: true });
395
+ return;
396
+ }
397
+ if (existing) {
398
+ fs.rmSync(newPath, { recursive: true, force: true });
399
+ }
400
+ fs.renameSync(oldPath, newPath);
401
+ }
402
+
403
+ function encodeLegacyAbsoluteSessionDirName(cwd: string): string {
404
+ const resolvedCwd = path.resolve(cwd);
405
+ return `--${resolvedCwd.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-")}--`;
406
+ }
407
+
408
+ function encodeRelativeSessionDirName(prefix: string, root: string, cwd: string): string {
409
+ const relative = path.relative(root, cwd).replace(/[/\\:]/g, "-");
410
+ return relative ? (prefix.endsWith("-") ? `${prefix}${relative}` : `${prefix}-${relative}`) : prefix;
411
+ }
412
+
413
+ function getDefaultSessionDirName(cwd: string): { encodedDirName: string; resolvedCwd: string } {
414
+ const resolvedCwd = path.resolve(cwd);
415
+ const canonicalCwd = resolveEquivalentPath(resolvedCwd);
416
+ const home = resolveEquivalentPath(os.homedir());
417
+ const tempRoot = resolveEquivalentPath(os.tmpdir());
418
+ const encodedDirName = pathIsWithin(home, canonicalCwd)
419
+ ? encodeRelativeSessionDirName("-", home, canonicalCwd)
420
+ : pathIsWithin(tempRoot, canonicalCwd)
421
+ ? encodeRelativeSessionDirName("-tmp", tempRoot, canonicalCwd)
422
+ : encodeLegacyAbsoluteSessionDirName(canonicalCwd);
423
+ return { encodedDirName, resolvedCwd };
424
+ }
425
+
426
+ /**
427
+ * Migrate old `--<home-encoded>-*--` session dirs to the new `-*` format.
428
+ * Runs once per sessions root on first access, best-effort.
429
+ */
430
+ function migrateHomeSessionDirs(sessionsRoot: string): void {
431
+ if (migratedSessionRoots.has(sessionsRoot)) return;
432
+ migratedSessionRoots.add(sessionsRoot);
433
+
434
+ const home = os.homedir();
435
+ const homeEncoded = home.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-");
436
+ const oldPrefix = `--${homeEncoded}-`;
437
+ const oldExact = `--${homeEncoded}--`;
438
+
439
+ let entries: string[];
440
+ try {
441
+ entries = fs.readdirSync(sessionsRoot);
442
+ } catch {
443
+ return;
444
+ }
445
+
446
+ for (const entry of entries) {
447
+ let remainder: string;
448
+ if (entry === oldExact) {
449
+ remainder = "";
450
+ } else if (entry.startsWith(oldPrefix) && entry.endsWith("--")) {
451
+ remainder = entry.slice(oldPrefix.length, -2);
452
+ } else {
453
+ continue;
454
+ }
455
+
456
+ const newName = remainder ? `-${remainder}` : "-";
457
+ const oldPath = path.join(sessionsRoot, entry);
458
+ const newPath = path.join(sessionsRoot, newName);
459
+
460
+ try {
461
+ migrateSessionDirPath(oldPath, newPath);
462
+ } catch {
463
+ // Best effort
464
+ }
465
+ }
466
+ }
467
+
468
+ function migrateLegacyAbsoluteSessionDir(cwd: string, sessionDir: string, sessionsRoot: string): void {
469
+ const legacyDir = path.join(sessionsRoot, encodeLegacyAbsoluteSessionDirName(cwd));
470
+ if (legacyDir === sessionDir || !fs.existsSync(legacyDir)) return;
471
+
472
+ try {
473
+ migrateSessionDirPath(legacyDir, sessionDir);
474
+ } catch {
475
+ // Best effort
476
+ }
477
+ }
478
+
479
+ function resolveManagedSessionRoot(sessionDir: string, cwd: string): string | undefined {
480
+ const currentDirName = path.basename(sessionDir);
481
+ const { encodedDirName } = getDefaultSessionDirName(cwd);
482
+ if (currentDirName !== encodedDirName && currentDirName !== encodeLegacyAbsoluteSessionDirName(cwd)) {
483
+ return undefined;
484
+ }
485
+ return path.dirname(sessionDir);
486
+ }
487
+
488
+ /** Exported for compaction.test.ts */
489
+ export function parseSessionEntries(content: string): FileEntry[] {
490
+ return parseJsonlLenient<FileEntry>(content);
491
+ }
492
+
493
+ export function getLatestCompactionEntry(entries: SessionEntry[]): CompactionEntry | null {
494
+ for (let i = entries.length - 1; i >= 0; i--) {
495
+ if (entries[i].type === "compaction") {
496
+ return entries[i] as CompactionEntry;
497
+ }
498
+ }
499
+ return null;
500
+ }
501
+
502
+ /**
503
+ * Build the session context from entries using tree traversal.
504
+ * If leafId is provided, walks from that entry to root.
505
+ * Handles compaction and branch summaries along the path.
506
+ */
507
+ export function buildSessionContext(
508
+ entries: SessionEntry[],
509
+ leafId?: string | null,
510
+ byId?: Map<string, SessionEntry>,
511
+ ): SessionContext {
512
+ // Build uuid index if not available
513
+ if (!byId) {
514
+ byId = new Map<string, SessionEntry>();
515
+ for (const entry of entries) {
516
+ byId.set(entry.id, entry);
517
+ }
518
+ }
519
+
520
+ // Find leaf
521
+ let leaf: SessionEntry | undefined;
522
+ if (leafId === null) {
523
+ // Explicitly null - return no messages (navigated to before first entry)
524
+ return {
525
+ messages: [],
526
+ thinkingLevel: "off",
527
+ serviceTier: undefined,
528
+ models: {},
529
+ injectedTtsrRules: [],
530
+ selectedMCPToolNames: [],
531
+ hasPersistedMCPToolSelection: false,
532
+ mode: "none",
533
+ };
534
+ }
535
+ if (leafId) {
536
+ leaf = byId.get(leafId);
537
+ }
538
+ if (!leaf) {
539
+ // Fallback to last entry (when leafId is undefined)
540
+ leaf = entries[entries.length - 1];
541
+ }
542
+
543
+ if (!leaf) {
544
+ return {
545
+ messages: [],
546
+ thinkingLevel: "off",
547
+ serviceTier: undefined,
548
+ models: {},
549
+ injectedTtsrRules: [],
550
+ selectedMCPToolNames: [],
551
+ hasPersistedMCPToolSelection: false,
552
+ mode: "none",
553
+ };
554
+ }
555
+
556
+ // Walk from leaf to root, collecting path
557
+ const path: SessionEntry[] = [];
558
+ let current: SessionEntry | undefined = leaf;
559
+ while (current) {
560
+ path.unshift(current);
561
+ current = current.parentId ? byId.get(current.parentId) : undefined;
562
+ }
563
+
564
+ // Extract settings and find compaction
565
+ let thinkingLevel: string | undefined = "off";
566
+ let serviceTier: ServiceTier | undefined;
567
+ const models: Record<string, string> = {};
568
+ let compaction: CompactionEntry | null = null;
569
+ const injectedTtsrRulesSet = new Set<string>();
570
+ let selectedMCPToolNames: string[] = [];
571
+ let hasPersistedMCPToolSelection = false;
572
+ let mode = "none";
573
+ let modeData: Record<string, unknown> | undefined;
574
+ // Track whether an explicit `model_change` with role="default" has been
575
+ // seen on this path. Once a user (or the agent itself) records an
576
+ // explicit default, later assistant-message inference must NOT overwrite
577
+ // it: temporary fallbacks (retry fallback, context promotion) and
578
+ // server-side model downgrades both produce assistant messages tagged
579
+ // with the wrong model id, which previously clobbered the user's pick on
580
+ // resume (issue #849).
581
+ let hasExplicitDefaultModel = false;
582
+
583
+ for (const entry of path) {
584
+ if (entry.type === "thinking_level_change") {
585
+ thinkingLevel = entry.thinkingLevel ?? "off";
586
+ } else if (entry.type === "model_change") {
587
+ // New format: { model: "provider/id", role?: string }
588
+ if (entry.model) {
589
+ const role = entry.role ?? "default";
590
+ models[role] = entry.model;
591
+ if (role === "default") {
592
+ hasExplicitDefaultModel = true;
593
+ }
594
+ }
595
+ } else if (entry.type === "service_tier_change") {
596
+ serviceTier = entry.serviceTier ?? undefined;
597
+ } else if (entry.type === "message" && entry.message.role === "assistant") {
598
+ // Legacy fallback: infer default model from assistant messages only
599
+ // when no explicit `model_change` (role=default) entry has been
600
+ // recorded yet. Newer sessions always record an explicit default
601
+ // model_change at the start of the conversation, so this branch is
602
+ // only used to keep pre-model_change sessions working.
603
+ if (!hasExplicitDefaultModel) {
604
+ models.default = `${entry.message.provider}/${entry.message.model}`;
605
+ }
606
+ } else if (entry.type === "compaction") {
607
+ compaction = entry;
608
+ } else if (entry.type === "ttsr_injection") {
609
+ // Collect injected TTSR rule names
610
+ for (const ruleName of entry.injectedRules) {
611
+ injectedTtsrRulesSet.add(ruleName);
612
+ }
613
+ } else if (entry.type === "mcp_tool_selection") {
614
+ selectedMCPToolNames = [...entry.selectedToolNames];
615
+ hasPersistedMCPToolSelection = true;
616
+ } else if (entry.type === "mode_change") {
617
+ mode = entry.mode;
618
+ modeData = entry.data;
619
+ }
620
+ }
621
+
622
+ const injectedTtsrRules = Array.from(injectedTtsrRulesSet);
623
+
624
+ // Build messages and collect corresponding entries
625
+ // When there's a compaction, we need to:
626
+ // 1. Emit summary first (entry = compaction)
627
+ // 2. Emit kept messages (from firstKeptEntryId up to compaction)
628
+ // 3. Emit messages after compaction
629
+ const messages: AgentMessage[] = [];
630
+
631
+ const appendMessage = (entry: SessionEntry) => {
632
+ if (entry.type === "message") {
633
+ messages.push(entry.message);
634
+ } else if (entry.type === "custom_message") {
635
+ messages.push(
636
+ createCustomMessage(
637
+ entry.customType,
638
+ entry.content,
639
+ entry.display,
640
+ entry.details,
641
+ entry.timestamp,
642
+ entry.attribution,
643
+ ),
644
+ );
645
+ } else if (entry.type === "branch_summary" && entry.summary) {
646
+ messages.push(createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp));
647
+ }
648
+ };
649
+
650
+ if (compaction) {
651
+ const providerPayload: ProviderPayload | undefined = (() => {
652
+ const candidate = compaction.preserveData?.openaiRemoteCompaction;
653
+ if (!candidate || typeof candidate !== "object") return undefined;
654
+ const remote = candidate as { provider?: unknown; replacementHistory?: unknown };
655
+ if (typeof remote.provider !== "string" || remote.provider.length === 0) return undefined;
656
+ if (!Array.isArray(remote.replacementHistory)) return undefined;
657
+ return {
658
+ type: "openaiResponsesHistory",
659
+ provider: remote.provider,
660
+ items: remote.replacementHistory as Array<Record<string, unknown>>,
661
+ };
662
+ })();
663
+ const remoteReplacementHistory = providerPayload?.items;
664
+
665
+ // Emit summary first
666
+ messages.push(
667
+ createCompactionSummaryMessage(
668
+ compaction.summary,
669
+ compaction.tokensBefore,
670
+ compaction.timestamp,
671
+ compaction.shortSummary,
672
+ providerPayload,
673
+ ),
674
+ );
675
+
676
+ // Find compaction index in path
677
+ const compactionIdx = path.findIndex(e => e.type === "compaction" && e.id === compaction.id);
678
+
679
+ if (!remoteReplacementHistory) {
680
+ // Emit kept messages (before compaction, starting from firstKeptEntryId)
681
+ let foundFirstKept = false;
682
+ for (let i = 0; i < compactionIdx; i++) {
683
+ const entry = path[i];
684
+ if (entry.id === compaction.firstKeptEntryId) {
685
+ foundFirstKept = true;
686
+ }
687
+ if (foundFirstKept) {
688
+ appendMessage(entry);
689
+ }
690
+ }
691
+ }
692
+
693
+ // Emit messages after compaction
694
+ for (let i = compactionIdx + 1; i < path.length; i++) {
695
+ const entry = path[i];
696
+ appendMessage(entry);
697
+ }
698
+ } else {
699
+ // No compaction - emit all messages, handle branch summaries and custom messages
700
+ for (const entry of path) {
701
+ appendMessage(entry);
702
+ }
703
+ }
704
+
705
+ return {
706
+ messages,
707
+ thinkingLevel,
708
+ serviceTier,
709
+ models,
710
+ injectedTtsrRules,
711
+ selectedMCPToolNames,
712
+ hasPersistedMCPToolSelection,
713
+ mode,
714
+ modeData,
715
+ };
716
+ }
717
+
718
+ /**
719
+ * Compute the default session directory for a cwd.
720
+ * Classifies cwd by canonical location so symlink/alias paths resolve to the
721
+ * same home-relative or temp-root directory names as their real targets.
722
+ */
723
+ function computeDefaultSessionDir(
724
+ cwd: string,
725
+ storage: SessionStorage,
726
+ sessionsRoot: string = getSessionsDir(),
727
+ ): string {
728
+ const { encodedDirName, resolvedCwd } = getDefaultSessionDirName(cwd);
729
+ migrateHomeSessionDirs(sessionsRoot);
730
+ const sessionDir = path.join(sessionsRoot, encodedDirName);
731
+ migrateLegacyAbsoluteSessionDir(resolvedCwd, sessionDir, sessionsRoot);
732
+ storage.ensureDirSync(sessionDir);
733
+ return sessionDir;
734
+ }
735
+
736
+ // =============================================================================
737
+ // Terminal breadcrumbs: maps terminal (TTY) -> last session file for --continue
738
+ // =============================================================================
739
+
740
+ /**
741
+ * Write a breadcrumb linking the current terminal to a session file.
742
+ * The breadcrumb contains the cwd and session path so --continue can
743
+ * find "this terminal's last session" even when running concurrent instances.
744
+ */
745
+ function writeTerminalBreadcrumb(cwd: string, sessionFile: string): void {
746
+ const terminalId = getTerminalId();
747
+ if (!terminalId) return;
748
+
749
+ const breadcrumbDir = getTerminalSessionsDir();
750
+ const breadcrumbFile = path.join(breadcrumbDir, terminalId);
751
+ const content = `${cwd}\n${sessionFile}\n`;
752
+ // Best-effort — don't break session creation if breadcrumb fails
753
+ Bun.write(breadcrumbFile, content).catch(() => {});
754
+ }
755
+
756
+ /**
757
+ * Read the terminal breadcrumb for the current terminal, scoped to a cwd.
758
+ * Returns the session file path if it exists and matches the cwd, null otherwise.
759
+ */
760
+ async function readTerminalBreadcrumb(cwd: string): Promise<string | null> {
761
+ const terminalId = getTerminalId();
762
+ if (!terminalId) return null;
763
+
764
+ try {
765
+ const breadcrumbFile = path.join(getTerminalSessionsDir(), terminalId);
766
+ const content = await Bun.file(breadcrumbFile).text();
767
+ const lines = content.trim().split("\n");
768
+ if (lines.length < 2) return null;
769
+
770
+ const breadcrumbCwd = lines[0];
771
+ const sessionFile = lines[1];
772
+
773
+ // Only return if cwd matches (user might have cd'd)
774
+ if (path.resolve(breadcrumbCwd) !== path.resolve(cwd)) return null;
775
+
776
+ // Verify the session file still exists
777
+ const stat = fs.statSync(sessionFile, { throwIfNoEntry: false });
778
+ if (stat?.isFile()) return sessionFile;
779
+ } catch (err) {
780
+ if (!isEnoent(err)) logger.debug("Terminal breadcrumb read failed", { err });
781
+ // Breadcrumb doesn't exist or is corrupt — fall through
782
+ }
783
+ return null;
784
+ }
785
+
786
+ /** Exported for testing */
787
+ export async function loadEntriesFromFile(
788
+ filePath: string,
789
+ storage: SessionStorage = new FileSessionStorage(),
790
+ ): Promise<FileEntry[]> {
791
+ let content: string;
792
+ try {
793
+ content = await storage.readText(filePath);
794
+ } catch (err) {
795
+ if (isEnoent(err)) return [];
796
+ throw err;
797
+ }
798
+ const entries = parseJsonlLenient<FileEntry>(content);
799
+
800
+ // Validate session header
801
+ if (entries.length === 0) return entries;
802
+ const header = entries[0] as SessionHeader;
803
+ if (header.type !== "session" || typeof header.id !== "string") {
804
+ return [];
805
+ }
806
+
807
+ return entries;
808
+ }
809
+
810
+ /**
811
+ * Resolve blob references in loaded entries, restoring both session image blocks and persisted
812
+ * provider image URLs back to the inline data expected by downstream transports. Mutates entries in place.
813
+ */
814
+ function hasImageUrl(value: unknown): value is { image_url: string } {
815
+ return typeof value === "object" && value !== null && "image_url" in value && typeof value.image_url === "string";
816
+ }
817
+
818
+ async function resolvePersistedImageUrlRefs(value: unknown, blobStore: BlobStore): Promise<void> {
819
+ if (Array.isArray(value)) {
820
+ await Promise.all(value.map(item => resolvePersistedImageUrlRefs(item, blobStore)));
821
+ return;
822
+ }
823
+
824
+ if (typeof value !== "object" || value === null) return;
825
+
826
+ if (hasImageUrl(value) && isBlobRef(value.image_url)) {
827
+ value.image_url = await resolveImageDataUrl(blobStore, value.image_url);
828
+ }
829
+
830
+ await Promise.all(Object.values(value).map(item => resolvePersistedImageUrlRefs(item, blobStore)));
831
+ }
832
+
833
+ async function resolveBlobRefsInEntries(entries: FileEntry[], blobStore: BlobStore): Promise<void> {
834
+ const promises: Promise<void>[] = [];
835
+
836
+ for (const entry of entries) {
837
+ if (entry.type === "session") continue;
838
+
839
+ let contentArray: unknown[] | undefined;
840
+ if (entry.type === "message" && "content" in entry.message && Array.isArray(entry.message.content)) {
841
+ contentArray = entry.message.content;
842
+ } else if (entry.type === "custom_message" && Array.isArray(entry.content)) {
843
+ contentArray = entry.content;
844
+ }
845
+
846
+ if (contentArray) {
847
+ for (const block of contentArray) {
848
+ if (isImageBlock(block) && isBlobRef(block.data)) {
849
+ promises.push(
850
+ resolveImageData(blobStore, block.data).then(resolved => {
851
+ block.data = resolved;
852
+ }),
853
+ );
854
+ }
855
+ }
856
+ }
857
+
858
+ promises.push(resolvePersistedImageUrlRefs(entry, blobStore));
859
+ }
860
+
861
+ await Promise.all(promises);
862
+ }
863
+
864
+ /**
865
+ * Lightweight metadata for a session file, used in session picker UI.
866
+ * Uses lazy getters to defer string formatting until actually displayed.
867
+ */
868
+ function sanitizeSessionName(value: string | undefined): string | undefined {
869
+ if (!value) return undefined;
870
+ const firstLine = value.split(/\r?\n/)[0] ?? "";
871
+ const stripped = firstLine.replace(/[\x00-\x1F\x7F]/g, "");
872
+ const trimmed = stripped.trim();
873
+ return trimmed.length > 0 ? trimmed : undefined;
874
+ }
875
+
876
+ class RecentSessionInfo {
877
+ #fullName: string | undefined;
878
+ #timeAgo: string | undefined;
879
+ readonly #headerTimestamp: string | undefined;
880
+
881
+ constructor(
882
+ readonly path: string,
883
+ readonly mtime: number,
884
+ header: Record<string, unknown>,
885
+ firstPrompt?: string,
886
+ ) {
887
+ // Prefer an explicit title, then the first user prompt. The raw UUID `id` is
888
+ // intentionally not used as a fallback: showing it as a "name" is unfriendly and
889
+ // indistinguishable from neighboring sessions in the UI. The friendly fallback is
890
+ // derived lazily in `fullName` from the session timestamp.
891
+ const trystr = (v: unknown) => (typeof v === "string" ? v : undefined);
892
+ this.#fullName = sanitizeSessionName(trystr(header.title)) ?? sanitizeSessionName(firstPrompt);
893
+ this.#headerTimestamp = trystr(header.timestamp);
894
+ }
895
+
896
+ /** Display name. Falls back to a timestamp-based label, never the raw UUID. */
897
+ get fullName(): string {
898
+ if (this.#fullName) return this.#fullName;
899
+ const ts = this.#headerTimestamp ? Date.parse(this.#headerTimestamp) : Number.NaN;
900
+ const date = new Date(Number.isFinite(ts) ? ts : this.mtime);
901
+ const time = date.toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" });
902
+ this.#fullName = `Untitled · ${time}`;
903
+ return this.#fullName;
904
+ }
905
+
906
+ /**
907
+ * Display name without an arbitrary length cap. The renderer is responsible for
908
+ * width-aware truncation so adjacent fields (e.g. the relative time) stay visible.
909
+ */
910
+ get name(): string {
911
+ return this.fullName;
912
+ }
913
+
914
+ /** Human-readable relative time (e.g., "2 hours ago") */
915
+ get timeAgo(): string {
916
+ if (this.#timeAgo) return this.#timeAgo;
917
+ this.#timeAgo = formatTimeAgo(new Date(this.mtime));
918
+ return this.#timeAgo;
919
+ }
920
+ }
921
+
922
+ /**
923
+ * Extracts the text content from a user message entry.
924
+ * Returns undefined if the entry is not a user message or has no text.
925
+ */
926
+ function extractFirstUserPrompt(entries: Array<Record<string, unknown>>): string | undefined {
927
+ for (const entry of entries) {
928
+ if (entry.type !== "message") continue;
929
+ const message = entry.message as Record<string, unknown> | undefined;
930
+ if (message?.role !== "user") continue;
931
+ const content = message.content;
932
+ if (typeof content === "string") return content;
933
+ if (Array.isArray(content)) {
934
+ for (const block of content) {
935
+ if (typeof block === "object" && block !== null && "text" in block) {
936
+ const text = (block as { text: unknown }).text;
937
+ if (typeof text === "string") return text;
938
+ }
939
+ }
940
+ }
941
+ }
942
+ return undefined;
943
+ }
944
+
945
+ /**
946
+ * Promote orphaned `<basename>.jsonl.<snowflake>.bak` backups created by
947
+ * `#replaceSessionFileAfterEperm` back to their primary path when the primary
948
+ * is missing. This runs once per session-dir scan, before the main `*.jsonl`
949
+ * glob, so a crash between the two renames in the EPERM-rewrite path does not
950
+ * leave the user's last good state stranded outside the loader's view.
951
+ *
952
+ * Exported for testing.
953
+ */
954
+ export async function recoverOrphanedBackups(sessionDir: string, storage: SessionStorage): Promise<void> {
955
+ let backups: string[];
956
+ try {
957
+ backups = storage.listFilesSync(sessionDir, "*.bak");
958
+ } catch {
959
+ return;
960
+ }
961
+ if (backups.length === 0) return;
962
+ // For each primary path, pick the newest backup (highest mtime) as the recovery source.
963
+ const candidates = new Map<string, { backup: string; mtimeMs: number }>();
964
+ for (const backup of backups) {
965
+ const name = path.basename(backup);
966
+ // Expect "<primary>.<snowflake>.bak" where <primary> ends in ".jsonl".
967
+ if (!name.endsWith(".bak")) continue;
968
+ const trimmed = name.slice(0, -".bak".length);
969
+ const dotIdx = trimmed.lastIndexOf(".");
970
+ if (dotIdx <= 0) continue;
971
+ const primaryName = trimmed.slice(0, dotIdx);
972
+ if (!primaryName.endsWith(".jsonl")) continue;
973
+ const primaryPath = path.join(sessionDir, primaryName);
974
+ let mtimeMs = 0;
975
+ try {
976
+ mtimeMs = storage.statSync(backup).mtimeMs;
977
+ } catch {
978
+ continue;
979
+ }
980
+ const existing = candidates.get(primaryPath);
981
+ if (!existing || mtimeMs > existing.mtimeMs) {
982
+ candidates.set(primaryPath, { backup, mtimeMs });
983
+ }
984
+ }
985
+ for (const [primaryPath, { backup }] of candidates) {
986
+ if (storage.existsSync(primaryPath)) continue;
987
+ try {
988
+ await storage.rename(backup, primaryPath);
989
+ logger.warn("Recovered orphaned session backup", {
990
+ sessionFile: primaryPath,
991
+ backupPath: backup,
992
+ });
993
+ } catch (err) {
994
+ logger.warn("Failed to recover orphaned session backup", {
995
+ sessionFile: primaryPath,
996
+ backupPath: backup,
997
+ error: toError(err).message,
998
+ });
999
+ }
1000
+ }
1001
+ }
1002
+
1003
+ /**
1004
+ * Reads all session files from the directory and returns them sorted by mtime (newest first).
1005
+ * Uses low-level file I/O to efficiently read only the first 4KB of each file
1006
+ * to extract the JSON header and first user message without loading entire session logs into memory.
1007
+ */
1008
+ async function getSortedSessions(sessionDir: string, storage: SessionStorage): Promise<RecentSessionInfo[]> {
1009
+ await recoverOrphanedBackups(sessionDir, storage);
1010
+ try {
1011
+ const files: string[] = storage.listFilesSync(sessionDir, "*.jsonl");
1012
+ const sessions: RecentSessionInfo[] = [];
1013
+ await Promise.all(
1014
+ files.map(async (path: string) => {
1015
+ try {
1016
+ const content = await storage.readTextPrefix(path, 4096);
1017
+ const entries = parseJsonlLenient<Record<string, unknown>>(content);
1018
+ if (entries.length === 0) return;
1019
+ const header = entries[0] as Record<string, unknown>;
1020
+ if (header.type !== "session" || typeof header.id !== "string") return;
1021
+ const mtime = storage.statSync(path).mtimeMs;
1022
+ const firstPrompt = header.title ? undefined : extractFirstUserPrompt(entries);
1023
+ sessions.push(new RecentSessionInfo(path, mtime, header, firstPrompt));
1024
+ } catch {}
1025
+ }),
1026
+ );
1027
+ return sessions.sort((a, b) => b.mtime - a.mtime);
1028
+ } catch {
1029
+ return [];
1030
+ }
1031
+ }
1032
+
1033
+ /** Exported for testing */
1034
+ export async function findMostRecentSession(
1035
+ sessionDir: string,
1036
+ storage: SessionStorage = new FileSessionStorage(),
1037
+ ): Promise<string | null> {
1038
+ const sessions = await getSortedSessions(sessionDir, storage);
1039
+ return sessions[0]?.path || null;
1040
+ }
1041
+
1042
+ /** Format a time difference as a human-readable string */
1043
+ function formatTimeAgo(date: Date): string {
1044
+ const now = Date.now();
1045
+ const diffMs = now - date.getTime();
1046
+ const diffMins = Math.floor(diffMs / 60000);
1047
+ const diffHours = Math.floor(diffMs / 3600000);
1048
+ const diffDays = Math.floor(diffMs / 86400000);
1049
+
1050
+ if (diffMins < 1) return "just now";
1051
+ if (diffMins < 60) return `${diffMins}m ago`;
1052
+ if (diffHours < 24) return `${diffHours}h ago`;
1053
+ if (diffDays < 7) return `${diffDays}d ago`;
1054
+ return date.toLocaleDateString();
1055
+ }
1056
+
1057
+ const MAX_PERSIST_CHARS = 500_000;
1058
+ const TRUNCATION_NOTICE = "\n\n[Session persistence truncated large content]";
1059
+ /** Minimum base64 length to externalize to blob store (skip tiny inline images) */
1060
+ const BLOB_EXTERNALIZE_THRESHOLD = 1024;
1061
+ const TEXT_CONTENT_KEY = "content";
1062
+
1063
+ /**
1064
+ * Recursively truncate large strings in an object for session persistence.
1065
+ * - Truncates any oversized string fields (key-agnostic)
1066
+ * - Replaces oversized image blocks with text notices
1067
+ * - Updates lineCount when content is truncated
1068
+ * - Returns original object if no changes needed (structural sharing)
1069
+ */
1070
+ function truncateString(value: string, maxLength: number): string {
1071
+ if (value.length <= maxLength) return value;
1072
+ let truncated = value.slice(0, maxLength);
1073
+ if (truncated.length > 0) {
1074
+ const last = truncated.charCodeAt(truncated.length - 1);
1075
+ if (last >= 0xd800 && last <= 0xdbff) {
1076
+ truncated = truncated.slice(0, -1);
1077
+ }
1078
+ }
1079
+ return truncated;
1080
+ }
1081
+
1082
+ function isImageBlock(value: unknown): value is { type: "image"; data: string; mimeType?: string } {
1083
+ return (
1084
+ typeof value === "object" &&
1085
+ value !== null &&
1086
+ "type" in value &&
1087
+ (value as { type?: string }).type === "image" &&
1088
+ "data" in value &&
1089
+ typeof (value as { data?: string }).data === "string"
1090
+ );
1091
+ }
1092
+
1093
+ async function truncateForPersistence(obj: FileEntry, blobStore: BlobStore, key?: string): Promise<FileEntry>;
1094
+ async function truncateForPersistence(obj: string, blobStore: BlobStore, key?: string): Promise<string>;
1095
+ async function truncateForPersistence(obj: unknown[], blobStore: BlobStore, key?: string): Promise<unknown[]>;
1096
+ async function truncateForPersistence(obj: object, blobStore: BlobStore, key?: string): Promise<object>;
1097
+ async function truncateForPersistence(
1098
+ obj: null | undefined,
1099
+ blobStore: BlobStore,
1100
+ key?: string,
1101
+ ): Promise<null | undefined>;
1102
+ async function truncateForPersistence(obj: unknown, blobStore: BlobStore, key?: string): Promise<unknown> {
1103
+ if (obj === null || obj === undefined) return obj;
1104
+
1105
+ if (typeof obj === "string") {
1106
+ if (key === "image_url" && isImageDataUrl(obj)) {
1107
+ return externalizeImageDataUrl(blobStore, obj);
1108
+ }
1109
+
1110
+ if (obj.length > MAX_PERSIST_CHARS) {
1111
+ // Cryptographic signatures must be preserved exactly or cleared entirely — never truncated.
1112
+ // Truncation would produce an invalid signature that the API rejects.
1113
+ if (key === "thinkingSignature" || key === "thoughtSignature" || key === "textSignature") {
1114
+ return "";
1115
+ }
1116
+
1117
+ const limit = Math.max(0, MAX_PERSIST_CHARS - TRUNCATION_NOTICE.length);
1118
+ return `${truncateString(obj, limit)}${TRUNCATION_NOTICE}`;
1119
+ }
1120
+
1121
+ return obj;
1122
+ }
1123
+
1124
+ if (Array.isArray(obj)) {
1125
+ let changed = false;
1126
+ const result = await Promise.all(
1127
+ obj.map(async item => {
1128
+ // Special handling: compress oversized images while preserving shape
1129
+ if (key === TEXT_CONTENT_KEY && isImageBlock(item)) {
1130
+ if (!isBlobRef(item.data) && item.data.length >= BLOB_EXTERNALIZE_THRESHOLD) {
1131
+ changed = true;
1132
+ const blobRef = await externalizeImageData(blobStore, item.data);
1133
+ return { ...item, data: blobRef };
1134
+ }
1135
+ }
1136
+
1137
+ const newItem = await truncateForPersistence(item, blobStore, key);
1138
+ if (newItem !== item) changed = true;
1139
+ return newItem;
1140
+ }),
1141
+ );
1142
+ return changed ? result : obj;
1143
+ }
1144
+
1145
+ if (typeof obj === "object") {
1146
+ let changed = false;
1147
+ const entries: Array<readonly [string, unknown]> = await Promise.all(
1148
+ Object.entries(obj).flatMap(([childKey, value]) => {
1149
+ // Strip transient/redundant properties that shouldn't be persisted.
1150
+ // - partialJson: streaming accumulator for tool call JSON parsing
1151
+ // - jsonlEvents: raw subprocess streaming events (already saved to artifact files)
1152
+ if (childKey === "partialJson" || childKey === "jsonlEvents") {
1153
+ changed = true;
1154
+ return [];
1155
+ }
1156
+
1157
+ return [
1158
+ (async () => {
1159
+ const newValue = await truncateForPersistence(value, blobStore, childKey);
1160
+ if (newValue !== value) changed = true;
1161
+ return [childKey, newValue] as const;
1162
+ })(),
1163
+ ];
1164
+ }),
1165
+ );
1166
+
1167
+ if (!changed) return obj;
1168
+
1169
+ const contentEntry = entries.find(([childKey]) => childKey === "content");
1170
+ const lineCountEntry = entries.find(([childKey]) => childKey === "lineCount");
1171
+ if (
1172
+ contentEntry &&
1173
+ typeof contentEntry[1] === "string" &&
1174
+ lineCountEntry &&
1175
+ typeof lineCountEntry[1] === "number"
1176
+ ) {
1177
+ const content = contentEntry[1];
1178
+ const updatedEntries = entries.map(([childKey, value]) =>
1179
+ childKey === "lineCount" ? ([childKey, content.split("\n").length] as const) : ([childKey, value] as const),
1180
+ );
1181
+ return Object.fromEntries(updatedEntries);
1182
+ }
1183
+ return Object.fromEntries(entries);
1184
+ }
1185
+
1186
+ return obj;
1187
+ }
1188
+
1189
+ async function prepareEntryForPersistence(entry: FileEntry, blobStore: BlobStore): Promise<FileEntry> {
1190
+ return truncateForPersistence(entry, blobStore);
1191
+ }
1192
+
1193
+ /**
1194
+ * Synchronous variant of {@link truncateForPersistence}.
1195
+ *
1196
+ * The async version's overhead — `Promise.all` over `Object.entries`/`Array.prototype.map`,
1197
+ * one microtask hop per nested node — is pure waste for entries without image blobs
1198
+ * (the vast majority). The fast path runs in one synchronous tick so an OOM/SIGKILL
1199
+ * landing right after `_persist` returns cannot lose the entry. Image externalization
1200
+ * still happens, but via the synchronous blob-store path (`fs.writeFileSync`), so the
1201
+ * blob bytes are in the kernel page cache before the JSONL line referencing them is
1202
+ * written.
1203
+ */
1204
+ function truncateForPersistenceSync(obj: unknown, blobStore: BlobStore, key?: string): unknown {
1205
+ if (obj === null || obj === undefined) return obj;
1206
+
1207
+ if (typeof obj === "string") {
1208
+ if (key === "image_url" && isImageDataUrl(obj)) {
1209
+ return externalizeImageDataUrlSync(blobStore, obj);
1210
+ }
1211
+ if (obj.length > MAX_PERSIST_CHARS) {
1212
+ if (key === "thinkingSignature" || key === "thoughtSignature" || key === "textSignature") {
1213
+ return "";
1214
+ }
1215
+ const limit = Math.max(0, MAX_PERSIST_CHARS - TRUNCATION_NOTICE.length);
1216
+ return `${truncateString(obj, limit)}${TRUNCATION_NOTICE}`;
1217
+ }
1218
+ return obj;
1219
+ }
1220
+
1221
+ if (Array.isArray(obj)) {
1222
+ let changed = false;
1223
+ const result: unknown[] = new Array(obj.length);
1224
+ for (let i = 0; i < obj.length; i++) {
1225
+ const item = obj[i];
1226
+ if (key === TEXT_CONTENT_KEY && isImageBlock(item)) {
1227
+ if (!isBlobRef(item.data) && item.data.length >= BLOB_EXTERNALIZE_THRESHOLD) {
1228
+ changed = true;
1229
+ const blobRef = externalizeImageDataSync(blobStore, item.data);
1230
+ result[i] = { ...item, data: blobRef };
1231
+ continue;
1232
+ }
1233
+ }
1234
+ const newItem = truncateForPersistenceSync(item, blobStore, key);
1235
+ if (newItem !== item) changed = true;
1236
+ result[i] = newItem;
1237
+ }
1238
+ return changed ? result : obj;
1239
+ }
1240
+
1241
+ if (typeof obj === "object") {
1242
+ let changed = false;
1243
+ const entries: Array<readonly [string, unknown]> = [];
1244
+ for (const [childKey, value] of Object.entries(obj)) {
1245
+ if (childKey === "partialJson" || childKey === "jsonlEvents") {
1246
+ changed = true;
1247
+ continue;
1248
+ }
1249
+ const newValue = truncateForPersistenceSync(value, blobStore, childKey);
1250
+ if (newValue !== value) changed = true;
1251
+ entries.push([childKey, newValue]);
1252
+ }
1253
+ if (!changed) return obj;
1254
+
1255
+ const contentEntry = entries.find(([childKey]) => childKey === "content");
1256
+ const lineCountEntry = entries.find(([childKey]) => childKey === "lineCount");
1257
+ if (
1258
+ contentEntry &&
1259
+ typeof contentEntry[1] === "string" &&
1260
+ lineCountEntry &&
1261
+ typeof lineCountEntry[1] === "number"
1262
+ ) {
1263
+ const content = contentEntry[1];
1264
+ const updatedEntries = entries.map(([childKey, value]) =>
1265
+ childKey === "lineCount" ? ([childKey, content.split("\n").length] as const) : ([childKey, value] as const),
1266
+ );
1267
+ return Object.fromEntries(updatedEntries);
1268
+ }
1269
+ return Object.fromEntries(entries);
1270
+ }
1271
+
1272
+ return obj;
1273
+ }
1274
+
1275
+ function prepareEntryForPersistenceSync(entry: FileEntry, blobStore: BlobStore): FileEntry {
1276
+ return truncateForPersistenceSync(entry, blobStore) as FileEntry;
1277
+ }
1278
+
1279
+ class NdjsonFileWriter {
1280
+ #writer: SessionStorageWriter;
1281
+ #closed = false;
1282
+ #closing = false;
1283
+ #error: Error | undefined;
1284
+ #pendingWrites: Promise<void> = Promise.resolve();
1285
+ #onError: ((err: Error) => void) | undefined;
1286
+
1287
+ constructor(storage: SessionStorage, path: string, options?: { flags?: "a" | "w"; onError?: (err: Error) => void }) {
1288
+ this.#onError = options?.onError;
1289
+ this.#writer = storage.openWriter(path, {
1290
+ flags: options?.flags ?? "a",
1291
+ onError: (err: Error) => this.#recordError(err),
1292
+ });
1293
+ }
1294
+
1295
+ #recordError(err: unknown): Error {
1296
+ const writeErr = toError(err);
1297
+ if (!this.#error) this.#error = writeErr;
1298
+ this.#onError?.(writeErr);
1299
+ return writeErr;
1300
+ }
1301
+
1302
+ #enqueue(task: () => Promise<void>): Promise<void> {
1303
+ const run = async () => {
1304
+ if (this.#error) throw this.#error;
1305
+ await task();
1306
+ };
1307
+ const next = this.#pendingWrites.then(run);
1308
+ void next.catch((err: unknown) => {
1309
+ if (!this.#error) this.#error = toError(err);
1310
+ });
1311
+ this.#pendingWrites = next;
1312
+ return next;
1313
+ }
1314
+
1315
+ async #writeLine(line: string): Promise<void> {
1316
+ if (this.#error) throw this.#error;
1317
+ try {
1318
+ await this.#writer.writeLine(line);
1319
+ } catch (err) {
1320
+ throw this.#recordError(err);
1321
+ }
1322
+ }
1323
+
1324
+ /** Queue a write. Returns a promise so callers can await if needed. */
1325
+ write(entry: FileEntry): Promise<void> {
1326
+ if (this.#closed || this.#closing) throw new Error("Writer closed");
1327
+ if (this.#error) throw this.#error;
1328
+ const line = `${JSON.stringify(entry)}\n`;
1329
+ return this.#enqueue(() => this.#writeLine(line));
1330
+ }
1331
+
1332
+ /**
1333
+ * Synchronously serialize and append the entry. Returns once `fs.writeSync` has handed
1334
+ * the bytes to the kernel page cache — durable across OOM/SIGKILL even before fsync.
1335
+ *
1336
+ * Callers MUST NOT mix this with pending async `write()` calls on the same writer:
1337
+ * the async path is queued through `#pendingWrites`, but this method bypasses the
1338
+ * queue. Use only when no concurrent async write is in flight (the session-manager
1339
+ * persist path enforces this via `#flushed`/`#needsFullRewriteOnNextPersist`).
1340
+ */
1341
+ writeSync(entry: FileEntry): void {
1342
+ if (this.#closed || this.#closing) throw new Error("Writer closed");
1343
+ if (this.#error) throw this.#error;
1344
+ const line = `${JSON.stringify(entry)}\n`;
1345
+ try {
1346
+ this.#writer.writeLineSync(line);
1347
+ } catch (err) {
1348
+ throw this.#recordError(err);
1349
+ }
1350
+ }
1351
+
1352
+ /** Flush all buffered data to disk. Waits for all queued writes. */
1353
+ async flush(): Promise<void> {
1354
+ if (this.#closed) return;
1355
+ if (this.#error) throw this.#error;
1356
+
1357
+ await this.#enqueue(async () => {});
1358
+
1359
+ if (this.#error) throw this.#error;
1360
+
1361
+ try {
1362
+ await this.#writer.flush();
1363
+ } catch (err) {
1364
+ throw this.#recordError(err);
1365
+ }
1366
+ }
1367
+
1368
+ /** Sync data to persistent storage. */
1369
+ async fsync(): Promise<void> {
1370
+ if (this.#closed) return;
1371
+ if (this.#error) throw this.#error;
1372
+ try {
1373
+ await this.#writer.fsync();
1374
+ } catch (err) {
1375
+ throw this.#recordError(err);
1376
+ }
1377
+ }
1378
+
1379
+ /** Close the writer, flushing all data. */
1380
+ async close(): Promise<void> {
1381
+ if (this.#closed || this.#closing) return;
1382
+ this.#closing = true;
1383
+
1384
+ let closeError: Error | undefined;
1385
+ try {
1386
+ await this.flush();
1387
+ } catch (err) {
1388
+ closeError = toError(err);
1389
+ }
1390
+
1391
+ try {
1392
+ await this.#pendingWrites;
1393
+ } catch (err) {
1394
+ if (!closeError) closeError = toError(err);
1395
+ }
1396
+
1397
+ try {
1398
+ await this.#writer.close();
1399
+ } catch (err) {
1400
+ const endErr = this.#recordError(err);
1401
+ if (!closeError) closeError = endErr;
1402
+ }
1403
+
1404
+ this.#closed = true;
1405
+
1406
+ if (!closeError && this.#error) closeError = this.#error;
1407
+ if (closeError) throw closeError;
1408
+ }
1409
+
1410
+ /** Check if there's a stored error. */
1411
+ getError(): Error | undefined {
1412
+ return this.#error;
1413
+ }
1414
+
1415
+ /** True while the writer accepts new writes (not closing or closed). */
1416
+ isOpen(): boolean {
1417
+ return !this.#closed && !this.#closing;
1418
+ }
1419
+ }
1420
+
1421
+ /** Get recent sessions for display in welcome screen */
1422
+ export async function getRecentSessions(
1423
+ sessionDir: string,
1424
+ limit = 3,
1425
+ storage: SessionStorage = new FileSessionStorage(),
1426
+ ): Promise<RecentSessionInfo[]> {
1427
+ const sessions = await getSortedSessions(sessionDir, storage);
1428
+ return sessions.slice(0, limit);
1429
+ }
1430
+
1431
+ /**
1432
+ * Manages conversation sessions as append-only trees stored in JSONL files.
1433
+ *
1434
+ * Each session entry has an id and parentId forming a tree structure. The "leaf"
1435
+ * pointer tracks the current position. Appending creates a child of the current leaf.
1436
+ * Branching moves the leaf to an earlier entry, allowing new branches without
1437
+ * modifying history.
1438
+ *
1439
+ * Use buildSessionContext() to get the resolved message list for the LLM, which
1440
+ * handles compaction summaries and follows the path from root to current leaf.
1441
+ */
1442
+ export interface UsageStatistics {
1443
+ input: number;
1444
+ output: number;
1445
+ cacheRead: number;
1446
+ cacheWrite: number;
1447
+ premiumRequests: number;
1448
+ cost: number;
1449
+ }
1450
+
1451
+ function getTaskToolUsage(details: unknown): Usage | undefined {
1452
+ if (!details || typeof details !== "object") return undefined;
1453
+ const record = details as Record<string, unknown>;
1454
+ const usage = record.usage;
1455
+ if (!usage || typeof usage !== "object") return undefined;
1456
+ return usage as Usage;
1457
+ }
1458
+
1459
+ function extractTextFromContent(content: Message["content"]): string {
1460
+ if (typeof content === "string") return content;
1461
+ return content
1462
+ .filter((block): block is TextContent => block.type === "text")
1463
+ .map(block => block.text)
1464
+ .join(" ");
1465
+ }
1466
+
1467
+ const SESSION_LIST_PREFIX_BYTES = 4096;
1468
+ const SESSION_LIST_PARALLEL_THRESHOLD = 64;
1469
+ const SESSION_LIST_MAX_WORKERS = 16;
1470
+ const sessionListPrefixDecoder = new TextDecoder("utf-8", { fatal: false });
1471
+
1472
+ async function readSessionListPrefix(file: string, storage: SessionStorage, buffer: Buffer): Promise<string> {
1473
+ if (!(storage instanceof FileSessionStorage)) {
1474
+ return storage.readTextPrefix(file, buffer.byteLength);
1475
+ }
1476
+
1477
+ const handle = await fs.promises.open(file, "r");
1478
+ try {
1479
+ const { bytesRead } = await handle.read(buffer, 0, buffer.byteLength, 0);
1480
+ return sessionListPrefixDecoder.decode(buffer.subarray(0, bytesRead));
1481
+ } finally {
1482
+ await handle.close();
1483
+ }
1484
+ }
1485
+
1486
+ function decodeJsonStringFragment(value: string): string {
1487
+ const safeValue = value.endsWith("\\") ? value.slice(0, -1) : value;
1488
+ try {
1489
+ return JSON.parse(`"${safeValue}"`) as string;
1490
+ } catch {
1491
+ return safeValue
1492
+ .replace(/\\n/g, "\n")
1493
+ .replace(/\\r/g, "\r")
1494
+ .replace(/\\t/g, "\t")
1495
+ .replace(/\\"/g, '"')
1496
+ .replace(/\\\\/g, "\\");
1497
+ }
1498
+ }
1499
+
1500
+ function extractStringProperty(source: string, name: string, startIndex = 0): string | undefined {
1501
+ const propertyIndex = source.indexOf(`"${name}"`, startIndex);
1502
+ if (propertyIndex === -1) return undefined;
1503
+
1504
+ const colonIndex = source.indexOf(":", propertyIndex + name.length + 2);
1505
+ if (colonIndex === -1) return undefined;
1506
+
1507
+ let valueIndex = colonIndex + 1;
1508
+ while (valueIndex < source.length) {
1509
+ const char = source.charCodeAt(valueIndex);
1510
+ if (char !== 32 && char !== 9 && char !== 10 && char !== 13) break;
1511
+ valueIndex++;
1512
+ }
1513
+ if (source.charCodeAt(valueIndex) !== 34) return undefined;
1514
+
1515
+ const valueStart = valueIndex + 1;
1516
+ let escaped = false;
1517
+ for (let i = valueStart; i < source.length; i++) {
1518
+ const char = source.charCodeAt(i);
1519
+ if (escaped) {
1520
+ escaped = false;
1521
+ continue;
1522
+ }
1523
+ if (char === 92) {
1524
+ escaped = true;
1525
+ continue;
1526
+ }
1527
+ if (char === 34) {
1528
+ return decodeJsonStringFragment(source.slice(valueStart, i));
1529
+ }
1530
+ }
1531
+
1532
+ return decodeJsonStringFragment(source.slice(valueStart));
1533
+ }
1534
+
1535
+ function countMessageMarkers(content: string): number {
1536
+ let count = 0;
1537
+ let index = 0;
1538
+ while (index < content.length) {
1539
+ const typeIndex = content.indexOf('"type"', index);
1540
+ if (typeIndex === -1) break;
1541
+ const colonIndex = content.indexOf(":", typeIndex + 6);
1542
+ if (colonIndex === -1) break;
1543
+ const type = extractStringProperty(content, "type", typeIndex);
1544
+ if (type === "message") count++;
1545
+ index = colonIndex + 1;
1546
+ }
1547
+ return count;
1548
+ }
1549
+
1550
+ function extractFirstUserMessageFromPrefix(content: string): string | undefined {
1551
+ const roleIndex = content.indexOf('"role"');
1552
+ if (roleIndex === -1) return undefined;
1553
+
1554
+ let index = roleIndex;
1555
+ while (index !== -1) {
1556
+ const role = extractStringProperty(content, "role", index);
1557
+ if (role === "user") {
1558
+ return extractStringProperty(content, "content", index) ?? extractStringProperty(content, "text", index);
1559
+ }
1560
+ index = content.indexOf('"role"', index + 6);
1561
+ }
1562
+
1563
+ return undefined;
1564
+ }
1565
+
1566
+ interface SessionListHeader {
1567
+ type: "session";
1568
+ id: string;
1569
+ cwd?: string;
1570
+ title?: string;
1571
+ parentSession?: string;
1572
+ timestamp?: string;
1573
+ }
1574
+
1575
+ function parseSessionListHeader(
1576
+ content: string,
1577
+ entries: Array<Record<string, unknown>>,
1578
+ ): SessionListHeader | undefined {
1579
+ const parsedHeader = entries[0];
1580
+ if (parsedHeader?.type === "session" && typeof parsedHeader.id === "string") {
1581
+ return {
1582
+ type: "session",
1583
+ id: parsedHeader.id,
1584
+ cwd: typeof parsedHeader.cwd === "string" ? parsedHeader.cwd : undefined,
1585
+ title: typeof parsedHeader.title === "string" ? parsedHeader.title : undefined,
1586
+ parentSession: typeof parsedHeader.parentSession === "string" ? parsedHeader.parentSession : undefined,
1587
+ timestamp: typeof parsedHeader.timestamp === "string" ? parsedHeader.timestamp : undefined,
1588
+ };
1589
+ }
1590
+
1591
+ const firstLineEnd = content.indexOf("\n");
1592
+ const firstLine = firstLineEnd === -1 ? content : content.slice(0, firstLineEnd);
1593
+ if (extractStringProperty(firstLine, "type") !== "session") return undefined;
1594
+
1595
+ const id = extractStringProperty(firstLine, "id");
1596
+ if (!id) return undefined;
1597
+
1598
+ return {
1599
+ type: "session",
1600
+ id,
1601
+ cwd: extractStringProperty(firstLine, "cwd"),
1602
+ title: extractStringProperty(firstLine, "title"),
1603
+ parentSession: extractStringProperty(firstLine, "parentSession"),
1604
+ timestamp: extractStringProperty(firstLine, "timestamp"),
1605
+ };
1606
+ }
1607
+
1608
+ function getSessionListWorkerCount(fileCount: number): number {
1609
+ if (fileCount <= SESSION_LIST_PARALLEL_THRESHOLD) return 1;
1610
+ return Math.min(
1611
+ SESSION_LIST_MAX_WORKERS,
1612
+ os.availableParallelism(),
1613
+ Math.ceil(fileCount / SESSION_LIST_PARALLEL_THRESHOLD),
1614
+ );
1615
+ }
1616
+
1617
+ async function collectSessionFromFile(
1618
+ file: string,
1619
+ storage: SessionStorage,
1620
+ buffer: Buffer,
1621
+ ): Promise<SessionInfo | undefined> {
1622
+ try {
1623
+ const content = await readSessionListPrefix(file, storage, buffer);
1624
+ const entries = parseJsonlLenient<Record<string, unknown>>(content);
1625
+ const header = parseSessionListHeader(content, entries);
1626
+ if (!header) return undefined;
1627
+
1628
+ let parsedMessageCount = 0;
1629
+ let firstMessage = "";
1630
+ const allMessages: string[] = [];
1631
+ let shortSummary: string | undefined;
1632
+
1633
+ for (let i = 1; i < entries.length; i++) {
1634
+ const entry = entries[i] as { type?: string; message?: Message; shortSummary?: string };
1635
+
1636
+ if (entry.type === "compaction" && typeof entry.shortSummary === "string") {
1637
+ shortSummary = entry.shortSummary;
1638
+ }
1639
+
1640
+ if (entry.type === "message" && entry.message) {
1641
+ parsedMessageCount++;
1642
+
1643
+ if (entry.message.role === "user" || entry.message.role === "assistant") {
1644
+ const textContent = extractTextFromContent(entry.message.content);
1645
+
1646
+ if (textContent) {
1647
+ allMessages.push(textContent);
1648
+
1649
+ if (!firstMessage && entry.message.role === "user") {
1650
+ firstMessage = textContent;
1651
+ }
1652
+ }
1653
+ }
1654
+ }
1655
+ }
1656
+
1657
+ firstMessage ||= extractFirstUserMessageFromPrefix(content) ?? "";
1658
+ const messageCount = Math.max(parsedMessageCount, countMessageMarkers(content));
1659
+ const stats = storage.statSync(file);
1660
+ return {
1661
+ path: file,
1662
+ id: header.id,
1663
+ cwd: header.cwd ?? "",
1664
+ title: header.title ?? shortSummary,
1665
+ parentSessionPath: header.parentSession,
1666
+ created: new Date(header.timestamp ?? ""),
1667
+ modified: stats.mtime,
1668
+ messageCount,
1669
+ size: stats.size,
1670
+ firstMessage: firstMessage || "(no messages)",
1671
+ allMessagesText: allMessages.length > 0 ? allMessages.join(" ") : firstMessage,
1672
+ };
1673
+ } catch {
1674
+ return undefined;
1675
+ }
1676
+ }
1677
+
1678
+ async function collectSessionsFromFileStride(
1679
+ files: string[],
1680
+ storage: SessionStorage,
1681
+ startIndex: number,
1682
+ stride: number,
1683
+ ): Promise<SessionInfo[]> {
1684
+ const sessions: SessionInfo[] = [];
1685
+ const buffer = Buffer.allocUnsafe(SESSION_LIST_PREFIX_BYTES);
1686
+
1687
+ for (let i = startIndex; i < files.length; i += stride) {
1688
+ const session = await collectSessionFromFile(files[i], storage, buffer);
1689
+ if (session) sessions.push(session);
1690
+ }
1691
+
1692
+ return sessions;
1693
+ }
1694
+
1695
+ async function collectSessionsFromFiles(files: string[], storage: SessionStorage): Promise<SessionInfo[]> {
1696
+ const workerCount = getSessionListWorkerCount(files.length);
1697
+ const sessions =
1698
+ workerCount === 1
1699
+ ? await collectSessionsFromFileStride(files, storage, 0, 1)
1700
+ : (
1701
+ await Promise.all(
1702
+ Array.from({ length: workerCount }, (_, workerIndex) =>
1703
+ collectSessionsFromFileStride(files, storage, workerIndex, workerCount),
1704
+ ),
1705
+ )
1706
+ ).flat();
1707
+
1708
+ sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
1709
+ return sessions;
1710
+ }
1711
+
1712
+ export interface ResolvedSessionMatch {
1713
+ session: SessionInfo;
1714
+ scope: "local" | "global";
1715
+ }
1716
+
1717
+ function sessionMatchesResumeArg(session: SessionInfo, sessionArg: string): boolean {
1718
+ const normalizedArg = sessionArg.toLowerCase();
1719
+ const normalizedId = session.id.toLowerCase();
1720
+ if (normalizedId.startsWith(normalizedArg)) {
1721
+ return true;
1722
+ }
1723
+
1724
+ const fileName = path.basename(session.path, ".jsonl").toLowerCase();
1725
+ if (fileName.startsWith(normalizedArg)) {
1726
+ return true;
1727
+ }
1728
+
1729
+ const separator = fileName.lastIndexOf("_");
1730
+ if (separator < 0) {
1731
+ return false;
1732
+ }
1733
+
1734
+ const fileSessionId = fileName.slice(separator + 1);
1735
+ return fileSessionId.startsWith(normalizedArg);
1736
+ }
1737
+
1738
+ export async function resolveResumableSession(
1739
+ sessionArg: string,
1740
+ cwd: string,
1741
+ sessionDir?: string,
1742
+ storage: SessionStorage = new FileSessionStorage(),
1743
+ ): Promise<ResolvedSessionMatch | undefined> {
1744
+ const localSessionDir = sessionDir ?? SessionManager.getDefaultSessionDir(cwd, undefined, storage);
1745
+ const localSessions = await SessionManager.list(cwd, localSessionDir, storage);
1746
+ const localMatch = localSessions.find(session => sessionMatchesResumeArg(session, sessionArg));
1747
+ if (localMatch) {
1748
+ return { session: localMatch, scope: "local" };
1749
+ }
1750
+
1751
+ if (sessionDir) {
1752
+ return undefined;
1753
+ }
1754
+
1755
+ const globalSessions = await SessionManager.listAll(storage);
1756
+ const globalMatch = globalSessions.find(session => sessionMatchesResumeArg(session, sessionArg));
1757
+ if (!globalMatch) {
1758
+ return undefined;
1759
+ }
1760
+
1761
+ return { session: globalMatch, scope: "global" };
1762
+ }
1763
+ interface SessionManagerStateSnapshot {
1764
+ sessionId: string;
1765
+ sessionName: string | undefined;
1766
+ titleSource: "auto" | "user" | undefined;
1767
+ sessionFile: string | undefined;
1768
+ flushed: boolean;
1769
+ needsFullRewriteOnNextPersist: boolean;
1770
+ fileEntries: FileEntry[];
1771
+ }
1772
+
1773
+ export class SessionManager {
1774
+ #sessionId: string = "";
1775
+ #sessionName: string | undefined;
1776
+ #titleSource: "auto" | "user" | undefined;
1777
+ #sessionFile: string | undefined;
1778
+ #flushed: boolean = false;
1779
+ #needsFullRewriteOnNextPersist: boolean = false;
1780
+ #ensuredOnDisk: boolean = false;
1781
+ #fileEntries: FileEntry[] = [];
1782
+ #byId: Map<string, SessionEntry> = new Map();
1783
+ #labelsById: Map<string, string> = new Map();
1784
+ #leafId: string | null = null;
1785
+ #usageStatistics = {
1786
+ input: 0,
1787
+ output: 0,
1788
+ cacheRead: 0,
1789
+ cacheWrite: 0,
1790
+ premiumRequests: 0,
1791
+ cost: 0,
1792
+ } satisfies UsageStatistics;
1793
+ #persistWriter: NdjsonFileWriter | undefined;
1794
+ #persistWriterPath: string | undefined;
1795
+ #persistChain: Promise<void> = Promise.resolve();
1796
+ #persistError: Error | undefined;
1797
+ #persistErrorReported = false;
1798
+ #artifactManager: ArtifactManager | null = null;
1799
+ #artifactManagerSessionFile: string | null = null;
1800
+ // When set, take precedence over the lazily-derived per-session manager.
1801
+ // Subagents adopt the parent's manager so artifact IDs are unique across the
1802
+ // whole agent tree and all files land in the parent's artifacts dir.
1803
+ #adoptedArtifactManager: ArtifactManager | null = null;
1804
+ // In-memory artifact fallback for non-persistent sessions (persist=false).
1805
+ // Keyed by sequential numeric ID string; mirrors the file-based ArtifactManager ID scheme.
1806
+ #inMemoryArtifacts: Map<string, string> | null = null;
1807
+ #inMemoryArtifactCounter = 0;
1808
+ readonly #blobStore: BlobStore;
1809
+
1810
+ private constructor(
1811
+ private cwd: string,
1812
+ private sessionDir: string,
1813
+ private readonly persist: boolean,
1814
+ private readonly storage: SessionStorage,
1815
+ ) {
1816
+ this.#blobStore = new BlobStore(getBlobsDir());
1817
+ if (persist && sessionDir) {
1818
+ this.storage.ensureDirSync(sessionDir);
1819
+ }
1820
+ // Note: call _initSession() or _initSessionFile() after construction
1821
+ }
1822
+
1823
+ /** Puts a binary blob into the blob store and returns the blob reference */
1824
+ async putBlob(data: Buffer): Promise<BlobPutResult> {
1825
+ return this.#blobStore.put(data);
1826
+ }
1827
+
1828
+ captureState(): SessionManagerStateSnapshot {
1829
+ return {
1830
+ sessionId: this.#sessionId,
1831
+ sessionName: this.#sessionName,
1832
+ titleSource: this.#titleSource,
1833
+ sessionFile: this.#sessionFile,
1834
+ flushed: this.#flushed,
1835
+ needsFullRewriteOnNextPersist: this.#needsFullRewriteOnNextPersist,
1836
+ // Snapshot entry objects by reference: switch/reload replaces the active entry array,
1837
+ // so rollback does not need structured cloning of extension/custom details.
1838
+ fileEntries: [...this.#fileEntries],
1839
+ };
1840
+ }
1841
+
1842
+ restoreState(snapshot: SessionManagerStateSnapshot): void {
1843
+ this.#sessionId = snapshot.sessionId;
1844
+ this.#sessionName = snapshot.sessionName;
1845
+ this.#titleSource = snapshot.titleSource;
1846
+ this.#sessionFile = snapshot.sessionFile;
1847
+ this.#flushed = snapshot.flushed;
1848
+ this.#needsFullRewriteOnNextPersist = snapshot.needsFullRewriteOnNextPersist;
1849
+ this.#fileEntries = [...snapshot.fileEntries];
1850
+ this.#persistWriter = undefined;
1851
+ this.#persistWriterPath = undefined;
1852
+ this.#persistChain = Promise.resolve();
1853
+ this.#persistError = undefined;
1854
+ this.#persistErrorReported = false;
1855
+ this.#artifactManager = null;
1856
+ this.#artifactManagerSessionFile = null;
1857
+ this.#adoptedArtifactManager = null;
1858
+ this.#buildIndex();
1859
+ if (this.#sessionFile) {
1860
+ writeTerminalBreadcrumb(this.cwd, this.#sessionFile);
1861
+ }
1862
+ }
1863
+
1864
+ /** Initialize with a specific session file (used by factory methods) */
1865
+ async #initSessionFile(sessionFile: string): Promise<void> {
1866
+ await this.setSessionFile(sessionFile);
1867
+ }
1868
+
1869
+ /** Initialize with a new session (used by factory methods) */
1870
+ #initNewSession(): void {
1871
+ this.#newSessionSync();
1872
+ }
1873
+
1874
+ /** Switch to a different session file (used for resume and branching) */
1875
+ async setSessionFile(sessionFile: string): Promise<void> {
1876
+ await this.#closePersistWriter();
1877
+ this.#persistError = undefined;
1878
+ this.#persistErrorReported = false;
1879
+ this.#sessionFile = path.resolve(sessionFile);
1880
+ writeTerminalBreadcrumb(this.cwd, this.#sessionFile);
1881
+ this.#fileEntries = await loadEntriesFromFile(this.#sessionFile, this.storage);
1882
+ if (this.#fileEntries.length > 0) {
1883
+ const header = this.#fileEntries.find(e => e.type === "session") as SessionHeader | undefined;
1884
+ this.#sessionId = header?.id ?? createSessionId();
1885
+ this.#sessionName = header?.title;
1886
+ this.#titleSource = header?.titleSource;
1887
+
1888
+ this.#needsFullRewriteOnNextPersist = migrateToCurrentVersion(this.#fileEntries);
1889
+
1890
+ await resolveBlobRefsInEntries(this.#fileEntries, this.#blobStore);
1891
+ this.sanitizeLoadedOpenAIResponsesReplayMetadata();
1892
+
1893
+ this.#buildIndex();
1894
+ this.#flushed = true;
1895
+ this.#ensuredOnDisk = true;
1896
+ } else {
1897
+ const explicitPath = this.#sessionFile;
1898
+ this.#newSessionSync();
1899
+ this.#sessionFile = explicitPath; // preserve explicit path from --session flag
1900
+ await this.#rewriteFile();
1901
+ this.#flushed = true;
1902
+ this.#ensuredOnDisk = true;
1903
+ return;
1904
+ }
1905
+ }
1906
+
1907
+ /** Start a new session. Closes any existing writer first. */
1908
+ async newSession(options?: NewSessionOptions): Promise<string | undefined> {
1909
+ await this.#closePersistWriter();
1910
+ return this.#newSessionSync(options);
1911
+ }
1912
+
1913
+ /** Delete a session file and its artifacts. Drains the persist writer first to avoid EPERM on Windows. ENOENT is treated as success. */
1914
+ async dropSession(sessionPath: string): Promise<void> {
1915
+ await this.#closePersistWriter();
1916
+ try {
1917
+ await this.storage.deleteSessionWithArtifacts(sessionPath);
1918
+ } catch (err) {
1919
+ if (isEnoent(err)) return;
1920
+ throw err;
1921
+ }
1922
+ }
1923
+
1924
+ /**
1925
+ * Fork the current session, creating a new session file with the same entries.
1926
+ * Returns both the old and new session file paths for artifact copying.
1927
+ * @returns { oldSessionFile, newSessionFile } or undefined if not persisting
1928
+ */
1929
+ async fork(): Promise<{ oldSessionFile: string; newSessionFile: string } | undefined> {
1930
+ if (!this.persist || !this.#sessionFile) {
1931
+ return undefined;
1932
+ }
1933
+
1934
+ const oldSessionFile = this.#sessionFile;
1935
+ const oldSessionId = this.#sessionId;
1936
+
1937
+ // Close the current writer
1938
+ await this.#closePersistWriter();
1939
+ this.#persistChain = Promise.resolve();
1940
+ this.#persistError = undefined;
1941
+ this.#persistErrorReported = false;
1942
+
1943
+ // Create new session ID and header
1944
+ this.#sessionId = createSessionId();
1945
+ const timestamp = new Date().toISOString();
1946
+ const fileTimestamp = timestamp.replace(/[:.]/g, "-");
1947
+ this.#sessionFile = path.join(this.getSessionDir(), `${fileTimestamp}_${this.#sessionId}.jsonl`);
1948
+
1949
+ // Update the header with new ID but keep all entries
1950
+ const oldHeader = this.#fileEntries.find(e => e.type === "session") as SessionHeader | undefined;
1951
+ const newHeader: SessionHeader = {
1952
+ type: "session",
1953
+ version: CURRENT_SESSION_VERSION,
1954
+ id: this.#sessionId,
1955
+ title: oldHeader?.title ?? this.#sessionName,
1956
+ titleSource: oldHeader?.titleSource ?? this.#titleSource,
1957
+ timestamp,
1958
+ cwd: this.cwd,
1959
+ parentSession: oldSessionId,
1960
+ };
1961
+ this.#sessionName = newHeader.title;
1962
+ this.#titleSource = newHeader.titleSource;
1963
+
1964
+ // Replace the header in fileEntries
1965
+ const entries = this.#fileEntries.filter((e): e is SessionEntry => e.type !== "session");
1966
+ this.#fileEntries = [newHeader, ...entries];
1967
+
1968
+ // Write the new session file
1969
+ this.#flushed = false;
1970
+ await this.#rewriteFile();
1971
+
1972
+ return { oldSessionFile, newSessionFile: this.#sessionFile };
1973
+ }
1974
+
1975
+ /**
1976
+ * Move the session to a new working directory.
1977
+ * Moves session files and artifacts on disk, updates all internal references,
1978
+ * and rewrites the session header with the new cwd.
1979
+ */
1980
+ async moveTo(newCwd: string): Promise<void> {
1981
+ const resolvedCwd = path.resolve(newCwd);
1982
+ if (resolvedCwd === this.cwd) return;
1983
+
1984
+ const managedSessionsRoot = resolveManagedSessionRoot(this.sessionDir, this.cwd);
1985
+ const newSessionDir = managedSessionsRoot
1986
+ ? computeDefaultSessionDir(resolvedCwd, this.storage, managedSessionsRoot)
1987
+ : computeDefaultSessionDir(resolvedCwd, this.storage);
1988
+ let hadSessionFile = false;
1989
+
1990
+ if (this.persist && this.#sessionFile) {
1991
+ // Close the persist writer before moving files
1992
+ await this.#closePersistWriter();
1993
+ this.#persistChain = Promise.resolve();
1994
+ this.#persistError = undefined;
1995
+ this.#persistErrorReported = false;
1996
+
1997
+ const oldSessionFile = this.#sessionFile;
1998
+ const newSessionFile = path.join(newSessionDir, path.basename(oldSessionFile));
1999
+ const oldArtifactDir = oldSessionFile.slice(0, -6); // strip .jsonl
2000
+ const newArtifactDir = newSessionFile.slice(0, -6);
2001
+ hadSessionFile = this.storage.existsSync(oldSessionFile);
2002
+ let movedSessionFile = false;
2003
+ let movedArtifactDir = false;
2004
+
2005
+ try {
2006
+ // Guard: session file may not exist yet (no assistant messages persisted)
2007
+ if (hadSessionFile) {
2008
+ await fs.promises.rename(oldSessionFile, newSessionFile);
2009
+ movedSessionFile = true;
2010
+ }
2011
+
2012
+ try {
2013
+ const stat = await fs.promises.stat(oldArtifactDir);
2014
+ if (stat.isDirectory()) {
2015
+ await fs.promises.rename(oldArtifactDir, newArtifactDir);
2016
+ movedArtifactDir = true;
2017
+ }
2018
+ } catch (err) {
2019
+ if (!isEnoent(err)) throw err;
2020
+ }
2021
+ } catch (err) {
2022
+ if (movedArtifactDir) {
2023
+ try {
2024
+ await fs.promises.rename(newArtifactDir, oldArtifactDir);
2025
+ } catch (rollbackErr) {
2026
+ throw new Error(
2027
+ `Failed to move artifacts and rollback: ${rollbackErr instanceof Error ? rollbackErr.message : String(rollbackErr)}`,
2028
+ );
2029
+ }
2030
+ }
2031
+ if (movedSessionFile) {
2032
+ try {
2033
+ await fs.promises.rename(newSessionFile, oldSessionFile);
2034
+ } catch (rollbackErr) {
2035
+ throw new Error(
2036
+ `Failed to move session file and rollback: ${rollbackErr instanceof Error ? rollbackErr.message : String(rollbackErr)}`,
2037
+ );
2038
+ }
2039
+ }
2040
+ throw err;
2041
+ }
2042
+ this.#sessionFile = newSessionFile;
2043
+ }
2044
+
2045
+ // Update cwd and sessionDir after the move succeeds.
2046
+ this.cwd = resolvedCwd;
2047
+ this.sessionDir = newSessionDir;
2048
+
2049
+ // Update the session header in fileEntries
2050
+ const header = this.#fileEntries.find(e => e.type === "session") as SessionHeader | undefined;
2051
+ if (header) {
2052
+ header.cwd = resolvedCwd;
2053
+ }
2054
+
2055
+ // Rewrite the session file at its new location with updated header.
2056
+ // hadSessionFile: file existed before move → must rewrite to update cwd
2057
+ // hasAssistant: assistant messages in memory but file missing → recreate from memory
2058
+ // Neither true → fresh session, never written → preserve lazy-persist
2059
+ const hasAssistant = this.#fileEntries.some(e => e.type === "message" && e.message.role === "assistant");
2060
+ if (this.persist && this.#sessionFile && (hadSessionFile || hasAssistant)) {
2061
+ await this.#rewriteFile();
2062
+ }
2063
+
2064
+ // Update terminal breadcrumb
2065
+ if (this.#sessionFile) {
2066
+ writeTerminalBreadcrumb(resolvedCwd, this.#sessionFile);
2067
+ }
2068
+ }
2069
+
2070
+ /** Sync version for initial creation (no existing writer to close) */
2071
+ #newSessionSync(options?: NewSessionOptions): string | undefined {
2072
+ this.#persistChain = Promise.resolve();
2073
+ this.#persistError = undefined;
2074
+ this.#persistErrorReported = false;
2075
+ this.#sessionId = createSessionId();
2076
+ this.#sessionName = undefined;
2077
+ this.#titleSource = undefined;
2078
+ const timestamp = new Date().toISOString();
2079
+ const header: SessionHeader = {
2080
+ type: "session",
2081
+ version: CURRENT_SESSION_VERSION,
2082
+ id: this.#sessionId,
2083
+ timestamp,
2084
+ cwd: this.cwd,
2085
+ parentSession: options?.parentSession,
2086
+ };
2087
+ this.#fileEntries = [header];
2088
+ this.#byId.clear();
2089
+ this.#labelsById.clear();
2090
+ this.#leafId = null;
2091
+ this.#flushed = false;
2092
+ this.#needsFullRewriteOnNextPersist = false;
2093
+ this.#ensuredOnDisk = false;
2094
+ this.#usageStatistics = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, premiumRequests: 0, cost: 0 };
2095
+ this.#inMemoryArtifacts = null;
2096
+ this.#inMemoryArtifactCounter = 0;
2097
+
2098
+ if (this.persist) {
2099
+ const fileTimestamp = timestamp.replace(/[:.]/g, "-");
2100
+ this.#sessionFile = path.join(this.getSessionDir(), `${fileTimestamp}_${this.#sessionId}.jsonl`);
2101
+ writeTerminalBreadcrumb(this.cwd, this.#sessionFile);
2102
+ }
2103
+ return this.#sessionFile;
2104
+ }
2105
+
2106
+ #buildIndex(): void {
2107
+ this.#byId.clear();
2108
+ this.#labelsById.clear();
2109
+ this.#leafId = null;
2110
+ this.#usageStatistics = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, premiumRequests: 0, cost: 0 };
2111
+ for (const entry of this.#fileEntries) {
2112
+ if (entry.type === "session") continue;
2113
+ this.#byId.set(entry.id, entry);
2114
+ this.#leafId = entry.id;
2115
+ if (entry.type === "label") {
2116
+ if (entry.label) {
2117
+ this.#labelsById.set(entry.targetId, entry.label);
2118
+ } else {
2119
+ this.#labelsById.delete(entry.targetId);
2120
+ }
2121
+ }
2122
+ if (entry.type === "message" && entry.message.role === "assistant") {
2123
+ const usage = entry.message.usage;
2124
+ this.#usageStatistics.input += usage.input;
2125
+ this.#usageStatistics.output += usage.output;
2126
+ this.#usageStatistics.cacheRead += usage.cacheRead;
2127
+ this.#usageStatistics.cacheWrite += usage.cacheWrite;
2128
+ this.#usageStatistics.premiumRequests += usage.premiumRequests ?? 0;
2129
+ this.#usageStatistics.cost += usage.cost.total;
2130
+ }
2131
+
2132
+ if (entry.type === "message" && entry.message.role === "toolResult" && entry.message.toolName === "task") {
2133
+ const usage = getTaskToolUsage(entry.message.details);
2134
+ if (usage) {
2135
+ this.#usageStatistics.input += usage.input;
2136
+ this.#usageStatistics.output += usage.output;
2137
+ this.#usageStatistics.cacheRead += usage.cacheRead;
2138
+ this.#usageStatistics.cacheWrite += usage.cacheWrite;
2139
+ this.#usageStatistics.premiumRequests += usage.premiumRequests ?? 0;
2140
+ this.#usageStatistics.cost += usage.cost.total;
2141
+ }
2142
+ }
2143
+ }
2144
+ }
2145
+
2146
+ #recordPersistError(err: unknown): Error {
2147
+ const normalized = toError(err);
2148
+ if (!this.#persistError) this.#persistError = normalized;
2149
+ if (!this.#persistErrorReported) {
2150
+ this.#persistErrorReported = true;
2151
+ logger.error("Session persistence error.", {
2152
+ sessionFile: this.#sessionFile,
2153
+ error: normalized.message,
2154
+ stack: normalized.stack,
2155
+ });
2156
+ }
2157
+ return normalized;
2158
+ }
2159
+
2160
+ #queuePersistTask(task: () => Promise<void>, options?: { ignoreError?: boolean }): Promise<void> {
2161
+ const next = this.#persistChain.then(async () => {
2162
+ if (this.#persistError && !options?.ignoreError) throw this.#persistError;
2163
+ await task();
2164
+ });
2165
+ this.#persistChain = next.catch(err => {
2166
+ this.#recordPersistError(err);
2167
+ });
2168
+ return next;
2169
+ }
2170
+
2171
+ #ensurePersistWriter(): NdjsonFileWriter | undefined {
2172
+ if (!this.persist || !this.#sessionFile) return undefined;
2173
+ if (this.#persistError) throw this.#persistError;
2174
+ if (this.#persistWriter && this.#persistWriterPath === this.#sessionFile) {
2175
+ if (this.#persistWriter.isOpen()) return this.#persistWriter;
2176
+ // Cached writer for the current file is mid-close (queued
2177
+ // `#closePersistWriterInternal` has flipped `#closing` but not yet
2178
+ // cleared `#persistWriter`). Returning it would make `writeSync`
2179
+ // throw "Writer closed". Defer to the caller — `_persist` routes
2180
+ // the entry through the async rewrite path so it still lands on disk.
2181
+ return undefined;
2182
+ }
2183
+ // Note: caller must await _closePersistWriter() before calling this if switching files
2184
+ this.#persistWriter = new NdjsonFileWriter(this.storage, this.#sessionFile, {
2185
+ onError: err => {
2186
+ this.#recordPersistError(err);
2187
+ },
2188
+ });
2189
+ this.#persistWriterPath = this.#sessionFile;
2190
+ return this.#persistWriter;
2191
+ }
2192
+
2193
+ async #closePersistWriterInternal(): Promise<void> {
2194
+ if (this.#persistWriter) {
2195
+ await this.#persistWriter.close();
2196
+ this.#persistWriter = undefined;
2197
+ }
2198
+ this.#persistWriterPath = undefined;
2199
+ }
2200
+
2201
+ async #closePersistWriter(): Promise<void> {
2202
+ await this.#queuePersistTask(
2203
+ async () => {
2204
+ await this.#closePersistWriterInternal();
2205
+ },
2206
+ { ignoreError: true },
2207
+ );
2208
+ }
2209
+ // Windows can reject overwrite-style rename with EPERM even after our own writer is closed.
2210
+ // Move the old session file aside first so a failed retry can roll back to the last good file.
2211
+ // The backup uses a plain `<basename>.<snowflake>.bak` name (no leading dot) so that if the
2212
+ // process crashes between the two renames, `recoverOrphanedBackups` can find it via the
2213
+ // shared `*.bak` glob on both real and in-memory storage backends and promote it back to
2214
+ // the primary on the next session-dir scan.
2215
+
2216
+ async #replaceSessionFileAfterEperm(tempPath: string, targetPath: string, renameError: unknown): Promise<void> {
2217
+ const dir = path.resolve(targetPath, "..");
2218
+ const backupPath = path.join(dir, `${path.basename(targetPath)}.${Snowflake.next()}.bak`);
2219
+ try {
2220
+ await this.storage.rename(targetPath, backupPath);
2221
+ } catch (err) {
2222
+ if (isEnoent(err)) {
2223
+ await this.storage.rename(tempPath, targetPath);
2224
+ return;
2225
+ }
2226
+ throw toError(renameError);
2227
+ }
2228
+
2229
+ try {
2230
+ await this.storage.rename(tempPath, targetPath);
2231
+ } catch (err) {
2232
+ const replaceError = toError(err);
2233
+ const originalError = toError(renameError);
2234
+ try {
2235
+ await this.storage.rename(backupPath, targetPath);
2236
+ } catch (rollbackErr) {
2237
+ const rollbackError = toError(rollbackErr);
2238
+ throw new Error(
2239
+ `Failed to replace session file after EPERM (original: ${originalError.message}; retry: ${replaceError.message}); rollback from ${backupPath} also failed: ${rollbackError.message}`,
2240
+ { cause: originalError },
2241
+ );
2242
+ }
2243
+ throw replaceError;
2244
+ }
2245
+
2246
+ try {
2247
+ await this.storage.unlink(backupPath);
2248
+ } catch (err) {
2249
+ if (!isEnoent(err)) {
2250
+ logger.warn("Failed to remove session rewrite backup", {
2251
+ sessionFile: targetPath,
2252
+ backupPath,
2253
+ error: toError(err).message,
2254
+ });
2255
+ }
2256
+ }
2257
+ }
2258
+
2259
+ async #replaceSessionFile(tempPath: string, targetPath: string): Promise<void> {
2260
+ try {
2261
+ await this.storage.rename(tempPath, targetPath);
2262
+ } catch (err) {
2263
+ if (!hasFsCode(err, "EPERM")) throw toError(err);
2264
+ await this.#replaceSessionFileAfterEperm(tempPath, targetPath, err);
2265
+ }
2266
+ }
2267
+ async #writeEntriesAtomically(entries: FileEntry[]): Promise<void> {
2268
+ if (!this.#sessionFile) return;
2269
+ const dir = path.resolve(this.#sessionFile, "..");
2270
+ const tempPath = path.join(dir, `.${path.basename(this.#sessionFile)}.${Snowflake.next()}.tmp`);
2271
+ const writer = new NdjsonFileWriter(this.storage, tempPath, { flags: "w" });
2272
+ try {
2273
+ for (const entry of entries) {
2274
+ await writer.write(entry);
2275
+ }
2276
+ await writer.flush();
2277
+ await writer.fsync();
2278
+ await writer.close();
2279
+ await this.#replaceSessionFile(tempPath, this.#sessionFile);
2280
+ } catch (err) {
2281
+ try {
2282
+ await writer.close();
2283
+ } catch {
2284
+ // Ignore cleanup errors
2285
+ }
2286
+ try {
2287
+ await this.storage.unlink(tempPath);
2288
+ } catch {
2289
+ // Ignore cleanup errors
2290
+ }
2291
+ throw toError(err);
2292
+ }
2293
+ }
2294
+
2295
+ async #rewriteFile(): Promise<void> {
2296
+ if (!this.persist || !this.#sessionFile) return;
2297
+ await this.#queuePersistTask(async () => {
2298
+ await this.#closePersistWriterInternal();
2299
+ const entries = await Promise.all(
2300
+ this.#fileEntries.map(entry => prepareEntryForPersistence(entry, this.#blobStore)),
2301
+ );
2302
+ await this.#writeEntriesAtomically(entries);
2303
+ this.#needsFullRewriteOnNextPersist = false;
2304
+ this.#flushed = true;
2305
+ });
2306
+ }
2307
+
2308
+ isPersisted(): boolean {
2309
+ return this.persist;
2310
+ }
2311
+
2312
+ /**
2313
+ * Force-persist all current entries to disk, even when no assistant message exists yet.
2314
+ * Used by ACP mode where session/new must create a discoverable session immediately.
2315
+ */
2316
+ async ensureOnDisk(): Promise<void> {
2317
+ if (!this.persist || !this.#sessionFile) return;
2318
+ if (this.#flushed && !this.#needsFullRewriteOnNextPersist) return;
2319
+ await this.#rewriteFile();
2320
+ this.#ensuredOnDisk = true;
2321
+ }
2322
+
2323
+ /** Flush pending writes to disk. Call before switching sessions or on shutdown. */
2324
+ async flush(): Promise<void> {
2325
+ await this.#queuePersistTask(async () => {
2326
+ if (this.#persistWriter) {
2327
+ await this.#persistWriter.flush();
2328
+ await this.#persistWriter.fsync();
2329
+ }
2330
+ });
2331
+ if (this.#persistError) throw this.#persistError;
2332
+ }
2333
+
2334
+ /** Close the persistent writer after flushing all pending data. */
2335
+ async close(): Promise<void> {
2336
+ if (!this.#persistWriter) return;
2337
+ await this.#queuePersistTask(async () => {
2338
+ await this.#closePersistWriterInternal();
2339
+ this.#flushed = true;
2340
+ });
2341
+ if (this.#persistError) throw this.#persistError;
2342
+ }
2343
+
2344
+ getCwd(): string {
2345
+ return this.cwd;
2346
+ }
2347
+
2348
+ /** Get usage statistics across all assistant messages in the session. */
2349
+ getUsageStatistics(): UsageStatistics {
2350
+ return this.#usageStatistics;
2351
+ }
2352
+
2353
+ getSessionDir(): string {
2354
+ return this.sessionDir;
2355
+ }
2356
+
2357
+ getSessionId(): string {
2358
+ return this.#sessionId;
2359
+ }
2360
+
2361
+ getSessionFile(): string | undefined {
2362
+ return this.#sessionFile;
2363
+ }
2364
+
2365
+ /**
2366
+ * Returns the session artifacts directory path (session file path without .jsonl).
2367
+ * Returns null when the session is not persisted to a file.
2368
+ * When this session has adopted an external ArtifactManager (subagent case),
2369
+ * returns that manager's directory so reads/writes land in the shared parent
2370
+ * dir instead of a private (non-existent) subdir.
2371
+ */
2372
+ getArtifactsDir(): string | null {
2373
+ if (this.#adoptedArtifactManager) return this.#adoptedArtifactManager.dir;
2374
+ const sessionFile = this.#sessionFile;
2375
+ return sessionFile ? sessionFile.slice(0, -6) : null;
2376
+ }
2377
+
2378
+ /**
2379
+ * Adopt an externally-owned ArtifactManager. Used by subagents to share
2380
+ * the parent session's artifact directory and ID counter.
2381
+ */
2382
+ adoptArtifactManager(manager: ArtifactManager): void {
2383
+ this.#adoptedArtifactManager = manager;
2384
+ }
2385
+
2386
+ /**
2387
+ * Returns the ArtifactManager this session writes through. Lazily creates
2388
+ * one bound to the current session file unless an external manager was
2389
+ * adopted via `adoptArtifactManager`. Returns null only for non-persistent
2390
+ * sessions with no adopted manager.
2391
+ */
2392
+ getArtifactManager(): ArtifactManager | null {
2393
+ return this.#getOrCreateArtifactManager();
2394
+ }
2395
+
2396
+ /**
2397
+ * Returns an artifact manager bound to the current session file.
2398
+ * Recreates the manager when the active session file changes.
2399
+ */
2400
+ #getOrCreateArtifactManager(): ArtifactManager | null {
2401
+ if (this.#adoptedArtifactManager) return this.#adoptedArtifactManager;
2402
+ const sessionFile = this.#sessionFile;
2403
+ if (!sessionFile) {
2404
+ this.#artifactManager = null;
2405
+ this.#artifactManagerSessionFile = null;
2406
+ return null;
2407
+ }
2408
+
2409
+ if (this.#artifactManager && this.#artifactManagerSessionFile === sessionFile) {
2410
+ return this.#artifactManager;
2411
+ }
2412
+
2413
+ const manager = new ArtifactManager(sessionFile.slice(0, -6));
2414
+ this.#artifactManager = manager;
2415
+ this.#artifactManagerSessionFile = sessionFile;
2416
+ return manager;
2417
+ }
2418
+
2419
+ /**
2420
+ * Allocate a new artifact path and ID for the current session.
2421
+ * Returns an empty object when the session is not persisted.
2422
+ */
2423
+ async allocateArtifactPath(toolType: string): Promise<{ id?: string; path?: string }> {
2424
+ const manager = this.#getOrCreateArtifactManager();
2425
+ if (!manager) return {};
2426
+ return manager.allocatePath(toolType);
2427
+ }
2428
+
2429
+ /**
2430
+ * Save artifact content under the current session and return artifact ID.
2431
+ * Returns an artifact ID for all sessions (file-backed for persistent, in-memory fallback otherwise).
2432
+ */
2433
+ async saveArtifact(content: string, toolType: string): Promise<string | undefined> {
2434
+ const manager = this.#getOrCreateArtifactManager();
2435
+ if (manager) return manager.save(content, toolType);
2436
+ // Non-persistent session: store in memory so spill truncation can proceed.
2437
+ if (!this.#inMemoryArtifacts) this.#inMemoryArtifacts = new Map();
2438
+ const id = String(this.#inMemoryArtifactCounter++);
2439
+ this.#inMemoryArtifacts.set(id, content);
2440
+ return id;
2441
+ }
2442
+
2443
+ /**
2444
+ * Resolve an artifact ID to an on-disk path for the current session.
2445
+ * Returns null when missing or when the session is not persisted.
2446
+ */
2447
+ async getArtifactPath(id: string): Promise<string | null> {
2448
+ const manager = this.#getOrCreateArtifactManager();
2449
+ if (!manager) return null;
2450
+ return manager.getPath(id);
2451
+ }
2452
+
2453
+ /**
2454
+ * Path to the unsent-input draft sidecar for the current session. Lives inside
2455
+ * the artifacts directory so it is removed together with the session on
2456
+ * `dropSession`. Returns null when the session has no on-disk identity.
2457
+ */
2458
+ #getDraftPath(): string | null {
2459
+ const dir = this.getArtifactsDir();
2460
+ return dir ? path.join(dir, "draft.txt") : null;
2461
+ }
2462
+
2463
+ /**
2464
+ * Persist (or clear) the current editor draft so the next resume of this
2465
+ * session can restore it. Empty text deletes any stale draft. No-op when the
2466
+ * session is not persisted.
2467
+ */
2468
+ async saveDraft(text: string): Promise<void> {
2469
+ const draftPath = this.#getDraftPath();
2470
+ if (!draftPath || !this.persist) return;
2471
+ if (text.length === 0) {
2472
+ try {
2473
+ await this.storage.unlink(draftPath);
2474
+ } catch (err) {
2475
+ if (!isEnoent(err)) throw err;
2476
+ }
2477
+ return;
2478
+ }
2479
+ // Force the session header onto disk so resume can find the file we are
2480
+ // attaching this draft to. Without this, a session whose first message
2481
+ // never produced an assistant reply would persist a draft next to a
2482
+ // session file that does not exist on disk.
2483
+ await this.ensureOnDisk();
2484
+ await this.storage.writeText(draftPath, text);
2485
+ }
2486
+
2487
+ /**
2488
+ * Read and remove the saved draft. Returns the previously-saved text, or
2489
+ * null when no draft is pending. Single-shot: a successful read removes the
2490
+ * sidecar so a subsequent resume does not re-restore the same text.
2491
+ */
2492
+ async consumeDraft(): Promise<string | null> {
2493
+ const draftPath = this.#getDraftPath();
2494
+ if (!draftPath) return null;
2495
+ let text: string;
2496
+ try {
2497
+ text = await this.storage.readText(draftPath);
2498
+ } catch (err) {
2499
+ if (isEnoent(err)) return null;
2500
+ throw err;
2501
+ }
2502
+ try {
2503
+ await this.storage.unlink(draftPath);
2504
+ } catch (err) {
2505
+ if (!isEnoent(err)) throw err;
2506
+ }
2507
+ return text;
2508
+ }
2509
+
2510
+ /** The source that set the session name: "user" (manual /rename or RPC) or "auto" (generated title). */
2511
+ get titleSource(): "auto" | "user" | undefined {
2512
+ return this.#titleSource;
2513
+ }
2514
+
2515
+ getSessionName(): string | undefined {
2516
+ return this.#sessionName;
2517
+ }
2518
+
2519
+ /** Strip C0/C1 control characters (includes ESC, so removes ANSI sequences) and collapse whitespace. */
2520
+ static #sanitizeName(name: string): string {
2521
+ return name
2522
+ .replace(/[\u0000-\u001f\u007f-\u009f]/g, " ")
2523
+ .replace(/ +/g, " ")
2524
+ .trim();
2525
+ }
2526
+
2527
+ /**
2528
+ * Set the session display name.
2529
+ * @param source - "user" for explicit renames (/rename command, RPC); "auto" for generated titles.
2530
+ * Auto-generated titles are silently ignored when the user has already set a name.
2531
+ */
2532
+ async setSessionName(name: string, source: "auto" | "user" = "auto"): Promise<boolean> {
2533
+ // User-set names take permanent precedence over auto-generated ones.
2534
+ if (this.#titleSource === "user" && source === "auto") return false;
2535
+
2536
+ const sanitized = SessionManager.#sanitizeName(name);
2537
+ if (!sanitized) return false;
2538
+
2539
+ this.#sessionName = sanitized;
2540
+ this.#titleSource = source;
2541
+
2542
+ // Update the in-memory header (so first flush includes title)
2543
+ const header = this.#fileEntries.find(e => e.type === "session") as SessionHeader | undefined;
2544
+ if (header) {
2545
+ header.title = sanitized;
2546
+ header.titleSource = source;
2547
+ }
2548
+
2549
+ // Update the session file header with the title (if already flushed)
2550
+ const sessionFile = this.#sessionFile;
2551
+ if (this.persist && sessionFile && this.storage.existsSync(sessionFile)) {
2552
+ await this.#rewriteFile();
2553
+ }
2554
+ return true;
2555
+ }
2556
+
2557
+ _persist(entry: SessionEntry): void {
2558
+ if (!this.persist || !this.#sessionFile) return;
2559
+ if (this.#persistError) throw this.#persistError;
2560
+
2561
+ // Normally we wait for the first assistant message before persisting to avoid
2562
+ // creating files for sessions that never produce output. Once ensureOnDisk() has
2563
+ // been called, the session is already on disk and every entry must be flushed.
2564
+ if (!this.#ensuredOnDisk) {
2565
+ const hasAssistant = this.#fileEntries.some(e => e.type === "message" && e.message.role === "assistant");
2566
+ if (!hasAssistant) {
2567
+ // Mark as not flushed so when assistant arrives, all entries get written.
2568
+ this.#flushed = false;
2569
+ return;
2570
+ }
2571
+ }
2572
+
2573
+ if (this.#needsFullRewriteOnNextPersist || !this.#flushed) {
2574
+ // Cold path: rewrite the whole file atomically. Async — the writer is
2575
+ // closed/reopened and every entry is re-prepared. Errors flow through
2576
+ // `#persistChain` → `#recordPersistError`; we swallow the rejection
2577
+ // here to avoid an unhandled rejection when the persist dir races with
2578
+ // test-level tempDir cleanup.
2579
+ this.#rewriteFile().catch(() => {});
2580
+ return;
2581
+ }
2582
+
2583
+ // Hot path: synchronously truncate + append. `fs.writeSync` returns once the
2584
+ // bytes are in the kernel page cache, so the entry survives an OOM/SIGKILL
2585
+ // landing immediately after this call. Image externalization (rare) runs via
2586
+ // the synchronous blob-store path so blob bytes are durable before the JSONL
2587
+ // line referencing them is written.
2588
+ try {
2589
+ const writer = this.#ensurePersistWriter();
2590
+ if (!writer) {
2591
+ // `#ensurePersistWriter` returns undefined here only when the cached
2592
+ // writer is mid-close (the `!persist`/`!sessionFile` cases are
2593
+ // rejected above). Route through `#rewriteFile` so the entry — which
2594
+ // is already in `#fileEntries` — persists once the close drains.
2595
+ this.#rewriteFile().catch(() => {});
2596
+ return;
2597
+ }
2598
+ const persistedEntry = prepareEntryForPersistenceSync(entry, this.#blobStore);
2599
+ writer.writeSync(persistedEntry);
2600
+ } catch (err) {
2601
+ this.#recordPersistError(err);
2602
+ }
2603
+ }
2604
+
2605
+ #appendEntry(entry: SessionEntry): void {
2606
+ this.#fileEntries.push(entry);
2607
+ this.#byId.set(entry.id, entry);
2608
+ this.#leafId = entry.id;
2609
+ this._persist(entry);
2610
+ if (entry.type === "message" && entry.message.role === "assistant") {
2611
+ const usage = entry.message.usage;
2612
+ this.#usageStatistics.input += usage.input;
2613
+ this.#usageStatistics.output += usage.output;
2614
+ this.#usageStatistics.cacheRead += usage.cacheRead;
2615
+ this.#usageStatistics.cacheWrite += usage.cacheWrite;
2616
+ this.#usageStatistics.premiumRequests += usage.premiumRequests ?? 0;
2617
+ this.#usageStatistics.cost += usage.cost.total;
2618
+ }
2619
+
2620
+ if (entry.type === "message" && entry.message.role === "toolResult" && entry.message.toolName === "task") {
2621
+ const usage = getTaskToolUsage(entry.message.details);
2622
+ if (usage) {
2623
+ this.#usageStatistics.input += usage.input;
2624
+ this.#usageStatistics.output += usage.output;
2625
+ this.#usageStatistics.cacheRead += usage.cacheRead;
2626
+ this.#usageStatistics.cacheWrite += usage.cacheWrite;
2627
+ this.#usageStatistics.premiumRequests += usage.premiumRequests ?? 0;
2628
+ this.#usageStatistics.cost += usage.cost.total;
2629
+ }
2630
+ }
2631
+ }
2632
+
2633
+ /** Append a message as child of current leaf, then advance leaf. Returns entry id.
2634
+ * Does not allow writing CompactionSummaryMessage and BranchSummaryMessage directly.
2635
+ * Reason: we want these to be top-level entries in the session, not message session entries,
2636
+ * so it is easier to find them.
2637
+ * These need to be appended via appendCompaction() and appendBranchSummary() methods.
2638
+ */
2639
+ appendMessage(
2640
+ message:
2641
+ | Message
2642
+ | CustomMessage
2643
+ | HookMessage
2644
+ | BashExecutionMessage
2645
+ | PythonExecutionMessage
2646
+ | FileMentionMessage,
2647
+ ): string {
2648
+ const entry: SessionMessageEntry = {
2649
+ type: "message",
2650
+ id: generateId(this.#byId),
2651
+ parentId: this.#leafId,
2652
+ timestamp: new Date().toISOString(),
2653
+ message,
2654
+ };
2655
+ this.#appendEntry(entry);
2656
+ return entry.id;
2657
+ }
2658
+
2659
+ /** Append a thinking level change as child of current leaf, then advance leaf. Returns entry id. */
2660
+ appendThinkingLevelChange(thinkingLevel?: string): string {
2661
+ const entry: ThinkingLevelChangeEntry = {
2662
+ type: "thinking_level_change",
2663
+ id: generateId(this.#byId),
2664
+ parentId: this.#leafId,
2665
+ timestamp: new Date().toISOString(),
2666
+ thinkingLevel: thinkingLevel ?? null,
2667
+ };
2668
+ this.#appendEntry(entry);
2669
+ return entry.id;
2670
+ }
2671
+
2672
+ appendServiceTierChange(serviceTier: ServiceTier | null): string {
2673
+ const entry: ServiceTierChangeEntry = {
2674
+ type: "service_tier_change",
2675
+ id: generateId(this.#byId),
2676
+ parentId: this.#leafId,
2677
+ timestamp: new Date().toISOString(),
2678
+ serviceTier,
2679
+ };
2680
+ this.#appendEntry(entry);
2681
+ return entry.id;
2682
+ }
2683
+
2684
+ /** Append a mode change as child of current leaf, then advance leaf. Returns entry id. */
2685
+ appendModeChange(mode: string, data?: Record<string, unknown>): string {
2686
+ const entry: ModeChangeEntry = {
2687
+ type: "mode_change",
2688
+ id: generateId(this.#byId),
2689
+ parentId: this.#leafId,
2690
+ timestamp: new Date().toISOString(),
2691
+ mode,
2692
+ data,
2693
+ };
2694
+ this.#appendEntry(entry);
2695
+ return entry.id;
2696
+ }
2697
+
2698
+ /**
2699
+ * Append a model change as child of current leaf, then advance leaf. Returns entry id.
2700
+ * @param model Model in "provider/modelId" format
2701
+ * @param role Optional role (default: "default")
2702
+ */
2703
+ appendModelChange(model: string, role?: string): string {
2704
+ const entry: ModelChangeEntry = {
2705
+ type: "model_change",
2706
+ id: generateId(this.#byId),
2707
+ parentId: this.#leafId,
2708
+ timestamp: new Date().toISOString(),
2709
+ model,
2710
+ role,
2711
+ };
2712
+ this.#appendEntry(entry);
2713
+ return entry.id;
2714
+ }
2715
+
2716
+ /** Append session init metadata (for subagent debugging/replay). Returns entry id. */
2717
+ appendSessionInit(init: { systemPrompt: string; task: string; tools: string[]; outputSchema?: unknown }): string {
2718
+ const entry: SessionInitEntry = {
2719
+ type: "session_init",
2720
+ id: generateId(this.#byId),
2721
+ parentId: this.#leafId,
2722
+ timestamp: new Date().toISOString(),
2723
+ ...init,
2724
+ };
2725
+ this.#appendEntry(entry);
2726
+ return entry.id;
2727
+ }
2728
+
2729
+ /** Append a compaction summary as child of current leaf, then advance leaf. Returns entry id. */
2730
+ appendCompaction<T = unknown>(
2731
+ summary: string,
2732
+ shortSummary: string | undefined,
2733
+ firstKeptEntryId: string,
2734
+ tokensBefore: number,
2735
+ details?: T,
2736
+ fromExtension?: boolean,
2737
+ preserveData?: Record<string, unknown>,
2738
+ ): string {
2739
+ const entry: CompactionEntry<T> = {
2740
+ type: "compaction",
2741
+ id: generateId(this.#byId),
2742
+ parentId: this.#leafId,
2743
+ timestamp: new Date().toISOString(),
2744
+ summary,
2745
+ shortSummary,
2746
+ firstKeptEntryId,
2747
+ tokensBefore,
2748
+ details,
2749
+ fromExtension,
2750
+ preserveData,
2751
+ };
2752
+ this.#appendEntry(entry);
2753
+ return entry.id;
2754
+ }
2755
+
2756
+ /** Append a custom entry (for extensions) as child of current leaf, then advance leaf. Returns entry id. */
2757
+ appendCustomEntry(customType: string, data?: unknown): string {
2758
+ const entry: CustomEntry = {
2759
+ type: "custom",
2760
+ customType,
2761
+ data,
2762
+ id: generateId(this.#byId),
2763
+ parentId: this.#leafId,
2764
+ timestamp: new Date().toISOString(),
2765
+ };
2766
+ this.#appendEntry(entry);
2767
+ return entry.id;
2768
+ }
2769
+
2770
+ /**
2771
+ * Rewrite the session file after in-place entry updates.
2772
+ * Use sparingly (e.g., pruning old tool outputs).
2773
+ */
2774
+ async rewriteEntries(): Promise<void> {
2775
+ if (!this.persist || !this.#sessionFile) return;
2776
+ await this.#rewriteFile();
2777
+ }
2778
+
2779
+ /**
2780
+ * Append a custom message entry (for extensions) that participates in LLM context.
2781
+ * @param customType Hook identifier for filtering on reload
2782
+ * @param content Message content (string or TextContent/ImageContent array)
2783
+ * @param display Whether to show in TUI (true = styled display, false = hidden)
2784
+ * @param details Optional extension-specific metadata (not sent to LLM)
2785
+ * @param attribution Who initiated this message for billing/attribution semantics
2786
+ * @returns Entry id
2787
+ */
2788
+ appendCustomMessageEntry<T = unknown>(
2789
+ customType: string,
2790
+ content: string | (TextContent | ImageContent)[],
2791
+ display: boolean,
2792
+ details?: T,
2793
+ attribution: MessageAttribution = "agent",
2794
+ ): string {
2795
+ const entry: CustomMessageEntry<T> = {
2796
+ type: "custom_message",
2797
+ customType,
2798
+ content,
2799
+ display,
2800
+ // Drop AgentSession-internal transient fields (allowlist in
2801
+ // `INTERNAL_DETAILS_FIELDS`) before disk persistence. Single
2802
+ // chokepoint covers every CustomMessage write path.
2803
+ details: stripInternalDetailsFields(details),
2804
+ attribution,
2805
+ id: generateId(this.#byId),
2806
+ parentId: this.#leafId,
2807
+ timestamp: new Date().toISOString(),
2808
+ };
2809
+ this.#appendEntry(entry);
2810
+ return entry.id;
2811
+ }
2812
+
2813
+ // =========================================================================
2814
+ // TTSR (Time Traveling Stream Rules)
2815
+ // =========================================================================
2816
+
2817
+ /**
2818
+ * Append an MCP tool selection entry recording the discovery-selected MCP tools.
2819
+ * @param selectedToolNames MCP tool names selected for this branch
2820
+ * @returns Entry id
2821
+ */
2822
+ appendMCPToolSelection(selectedToolNames: string[]): string {
2823
+ const entry: MCPToolSelectionEntry = {
2824
+ type: "mcp_tool_selection",
2825
+ id: generateId(this.#byId),
2826
+ parentId: this.#leafId,
2827
+ timestamp: new Date().toISOString(),
2828
+ selectedToolNames: [...selectedToolNames],
2829
+ };
2830
+ this.#appendEntry(entry);
2831
+ return entry.id;
2832
+ }
2833
+
2834
+ /**
2835
+ * Append a TTSR injection entry recording which rules were injected.
2836
+ * @param ruleNames Names of rules that were injected
2837
+ * @returns Entry id
2838
+ */
2839
+ appendTtsrInjection(ruleNames: string[]): string {
2840
+ const entry: TtsrInjectionEntry = {
2841
+ type: "ttsr_injection",
2842
+ id: generateId(this.#byId),
2843
+ parentId: this.#leafId,
2844
+ timestamp: new Date().toISOString(),
2845
+ injectedRules: ruleNames,
2846
+ };
2847
+ this.#appendEntry(entry);
2848
+ return entry.id;
2849
+ }
2850
+
2851
+ /**
2852
+ * Get all unique TTSR rule names that have been injected in the current branch.
2853
+ * Scans from root to current leaf for ttsr_injection entries.
2854
+ */
2855
+ getInjectedTtsrRules(): string[] {
2856
+ const path = this.getBranch();
2857
+ const ruleNames = new Set<string>();
2858
+ for (const entry of path) {
2859
+ if (entry.type === "ttsr_injection") {
2860
+ for (const name of entry.injectedRules) {
2861
+ ruleNames.add(name);
2862
+ }
2863
+ }
2864
+ }
2865
+ return Array.from(ruleNames);
2866
+ }
2867
+
2868
+ // =========================================================================
2869
+ // Tree Traversal
2870
+ // =========================================================================
2871
+
2872
+ getLeafId(): string | null {
2873
+ return this.#leafId;
2874
+ }
2875
+
2876
+ getLeafEntry(): SessionEntry | undefined {
2877
+ return this.#leafId ? this.#byId.get(this.#leafId) : undefined;
2878
+ }
2879
+
2880
+ /**
2881
+ * Get the most recent model role from the current session path.
2882
+ * Returns undefined if no model change has been recorded.
2883
+ */
2884
+ getLastModelChangeRole(): string | undefined {
2885
+ let current = this.getLeafEntry();
2886
+ while (current) {
2887
+ if (current.type === "model_change") {
2888
+ return current.role ?? "default";
2889
+ }
2890
+ current = current.parentId ? this.#byId.get(current.parentId) : undefined;
2891
+ }
2892
+ return undefined;
2893
+ }
2894
+
2895
+ getEntry(id: string): SessionEntry | undefined {
2896
+ return this.#byId.get(id);
2897
+ }
2898
+
2899
+ /**
2900
+ * Get all direct children of an entry.
2901
+ */
2902
+ getChildren(parentId: string): SessionEntry[] {
2903
+ const children: SessionEntry[] = [];
2904
+ for (const entry of this.#byId.values()) {
2905
+ if (entry.parentId === parentId) {
2906
+ children.push(entry);
2907
+ }
2908
+ }
2909
+ return children;
2910
+ }
2911
+
2912
+ /**
2913
+ * Get the label for an entry, if any.
2914
+ */
2915
+ getLabel(id: string): string | undefined {
2916
+ return this.#labelsById.get(id);
2917
+ }
2918
+
2919
+ /**
2920
+ * Set or clear a label on an entry.
2921
+ * Labels are user-defined markers for bookmarking/navigation.
2922
+ * Pass undefined or empty string to clear the label.
2923
+ */
2924
+ appendLabelChange(targetId: string, label: string | undefined): string {
2925
+ if (!this.#byId.has(targetId)) {
2926
+ throw new Error(`Entry ${targetId} not found`);
2927
+ }
2928
+ const entry: LabelEntry = {
2929
+ type: "label",
2930
+ id: generateId(this.#byId),
2931
+ parentId: this.#leafId,
2932
+ timestamp: new Date().toISOString(),
2933
+ targetId,
2934
+ label,
2935
+ };
2936
+ this.#appendEntry(entry);
2937
+ if (label) {
2938
+ this.#labelsById.set(targetId, label);
2939
+ } else {
2940
+ this.#labelsById.delete(targetId);
2941
+ }
2942
+ return entry.id;
2943
+ }
2944
+
2945
+ /**
2946
+ * Walk from entry to root, returning all entries in path order.
2947
+ * Includes all entry types (messages, compaction, model changes, etc.).
2948
+ * Use buildSessionContext() to get the resolved messages for the LLM.
2949
+ */
2950
+ getBranch(fromId?: string): SessionEntry[] {
2951
+ const path: SessionEntry[] = [];
2952
+ const startId = fromId ?? this.#leafId;
2953
+ let current = startId ? this.#byId.get(startId) : undefined;
2954
+ while (current) {
2955
+ path.unshift(current);
2956
+ current = current.parentId ? this.#byId.get(current.parentId) : undefined;
2957
+ }
2958
+ return path;
2959
+ }
2960
+
2961
+ /**
2962
+ * Build the session context (what gets sent to the LLM).
2963
+ * Uses tree traversal from current leaf.
2964
+ */
2965
+ buildSessionContext(): SessionContext {
2966
+ return buildSessionContext(this.getEntries(), this.#leafId, this.#byId);
2967
+ }
2968
+
2969
+ /** Strip stale OpenAI Responses assistant replay metadata from loaded in-memory entries. */
2970
+ sanitizeLoadedOpenAIResponsesReplayMetadata(): boolean {
2971
+ let didSanitize = false;
2972
+ for (const entry of this.#fileEntries) {
2973
+ if (entry.type !== "message" || entry.message.role !== "assistant") {
2974
+ continue;
2975
+ }
2976
+
2977
+ const sanitizedMessage = sanitizeRehydratedOpenAIResponsesAssistantMessage(entry.message);
2978
+ if (sanitizedMessage === entry.message) {
2979
+ continue;
2980
+ }
2981
+
2982
+ entry.message = sanitizedMessage;
2983
+ didSanitize = true;
2984
+ }
2985
+
2986
+ return didSanitize;
2987
+ }
2988
+
2989
+ /**
2990
+ * Get session header.
2991
+ */
2992
+ getHeader(): SessionHeader | null {
2993
+ const h = this.#fileEntries.find(e => e.type === "session");
2994
+ return h ? (h as SessionHeader) : null;
2995
+ }
2996
+
2997
+ /**
2998
+ * Get all session entries (excludes header). Returns a shallow copy.
2999
+ * The session is append-only: use appendXXX() to add entries, branch() to
3000
+ * change the leaf pointer. Entries cannot be modified or deleted.
3001
+ */
3002
+ getEntries(): SessionEntry[] {
3003
+ return this.#fileEntries.filter((e): e is SessionEntry => e.type !== "session");
3004
+ }
3005
+
3006
+ /**
3007
+ * Get the session as a tree structure. Returns a shallow defensive copy of all entries.
3008
+ * A well-formed session has exactly one root (first entry with parentId === null).
3009
+ * Orphaned entries (broken parent chain) are also returned as roots.
3010
+ */
3011
+ getTree(): SessionTreeNode[] {
3012
+ const entries = this.getEntries();
3013
+ const nodeMap = new Map<string, SessionTreeNode>();
3014
+ const roots: SessionTreeNode[] = [];
3015
+
3016
+ // Create nodes with resolved labels
3017
+ for (const entry of entries) {
3018
+ const label = this.#labelsById.get(entry.id);
3019
+ nodeMap.set(entry.id, { entry, children: [], label });
3020
+ }
3021
+
3022
+ // Build tree
3023
+ for (const entry of entries) {
3024
+ const node = nodeMap.get(entry.id)!;
3025
+ if (entry.parentId === null || entry.parentId === entry.id) {
3026
+ roots.push(node);
3027
+ } else {
3028
+ const parent = nodeMap.get(entry.parentId);
3029
+ if (parent) {
3030
+ parent.children.push(node);
3031
+ } else {
3032
+ // Orphan - treat as root
3033
+ roots.push(node);
3034
+ }
3035
+ }
3036
+ }
3037
+
3038
+ // Sort children by timestamp (oldest first, newest at bottom)
3039
+ // Use iterative approach to avoid stack overflow on deep trees
3040
+ const stack: SessionTreeNode[] = [...roots];
3041
+ while (stack.length > 0) {
3042
+ const node = stack.pop()!;
3043
+ node.children.sort((a, b) => new Date(a.entry.timestamp).getTime() - new Date(b.entry.timestamp).getTime());
3044
+ stack.push(...node.children);
3045
+ }
3046
+
3047
+ return roots;
3048
+ }
3049
+
3050
+ // =========================================================================
3051
+ // Branching
3052
+ // =========================================================================
3053
+
3054
+ /**
3055
+ * Start a new branch from an earlier entry.
3056
+ * Moves the leaf pointer to the specified entry. The next appendXXX() call
3057
+ * will create a child of that entry, forming a new branch. Existing entries
3058
+ * are not modified or deleted.
3059
+ */
3060
+ branch(branchFromId: string): void {
3061
+ if (!this.#byId.has(branchFromId)) {
3062
+ throw new Error(`Entry ${branchFromId} not found`);
3063
+ }
3064
+ this.#leafId = branchFromId;
3065
+ }
3066
+
3067
+ /**
3068
+ * Reset the leaf pointer to null (before any entries).
3069
+ * The next appendXXX() call will create a new root entry (parentId = null).
3070
+ * Use this when navigating to re-edit the first user message.
3071
+ */
3072
+ resetLeaf(): void {
3073
+ this.#leafId = null;
3074
+ }
3075
+
3076
+ /**
3077
+ * Start a new branch with a summary of the abandoned path.
3078
+ * Same as branch(), but also appends a branch_summary entry that captures
3079
+ * context from the abandoned conversation path.
3080
+ */
3081
+ branchWithSummary(branchFromId: string | null, summary: string, details?: unknown, fromExtension?: boolean): string {
3082
+ if (branchFromId !== null && !this.#byId.has(branchFromId)) {
3083
+ throw new Error(`Entry ${branchFromId} not found`);
3084
+ }
3085
+ this.#leafId = branchFromId;
3086
+ const entry: BranchSummaryEntry = {
3087
+ type: "branch_summary",
3088
+ id: generateId(this.#byId),
3089
+ parentId: branchFromId,
3090
+ timestamp: new Date().toISOString(),
3091
+ fromId: branchFromId ?? "root",
3092
+ summary,
3093
+ details,
3094
+ fromExtension,
3095
+ };
3096
+ this.#appendEntry(entry);
3097
+ return entry.id;
3098
+ }
3099
+
3100
+ /**
3101
+ * Create a new session file containing only the path from root to the specified leaf.
3102
+ * Useful for extracting a single conversation path from a branched session.
3103
+ * Returns the new session file path, or undefined if not persisting.
3104
+ */
3105
+ createBranchedSession(leafId: string): string | undefined {
3106
+ const previousSessionFile = this.#sessionFile;
3107
+ const branchPath = this.getBranch(leafId);
3108
+ if (branchPath.length === 0) {
3109
+ throw new Error(`Entry ${leafId} not found`);
3110
+ }
3111
+
3112
+ // Filter out LabelEntry from path - we'll recreate them from the resolved map
3113
+ const pathWithoutLabels = branchPath.filter(e => e.type !== "label");
3114
+
3115
+ const newSessionId = createSessionId();
3116
+ const timestamp = new Date().toISOString();
3117
+ const fileTimestamp = timestamp.replace(/[:.]/g, "-");
3118
+ const newSessionFile = path.join(this.getSessionDir(), `${fileTimestamp}_${newSessionId}.jsonl`);
3119
+
3120
+ const header: SessionHeader = {
3121
+ type: "session",
3122
+ version: CURRENT_SESSION_VERSION,
3123
+ id: newSessionId,
3124
+ timestamp,
3125
+ cwd: this.cwd,
3126
+ parentSession: this.persist ? previousSessionFile : undefined,
3127
+ };
3128
+
3129
+ // Collect labels for entries in the path
3130
+ const pathEntryIds = new Set(pathWithoutLabels.map(e => e.id));
3131
+ const labelsToWrite: Array<{ targetId: string; label: string }> = [];
3132
+ for (const [targetId, label] of this.#labelsById) {
3133
+ if (pathEntryIds.has(targetId)) {
3134
+ labelsToWrite.push({ targetId, label });
3135
+ }
3136
+ }
3137
+
3138
+ if (this.persist) {
3139
+ const lines: string[] = [];
3140
+ lines.push(JSON.stringify(header));
3141
+ for (const entry of pathWithoutLabels) {
3142
+ lines.push(JSON.stringify(entry));
3143
+ }
3144
+ // Write fresh label entries at the end
3145
+ const lastEntryId = pathWithoutLabels[pathWithoutLabels.length - 1]?.id || null;
3146
+ let parentId = lastEntryId;
3147
+ const labelEntries: LabelEntry[] = [];
3148
+ for (const { targetId, label } of labelsToWrite) {
3149
+ const labelEntry: LabelEntry = {
3150
+ type: "label",
3151
+ id: generateId(new Set(pathEntryIds)),
3152
+ parentId,
3153
+ timestamp: new Date().toISOString(),
3154
+ targetId,
3155
+ label,
3156
+ };
3157
+ lines.push(JSON.stringify(labelEntry));
3158
+ pathEntryIds.add(labelEntry.id);
3159
+ labelEntries.push(labelEntry);
3160
+ parentId = labelEntry.id;
3161
+ }
3162
+ this.storage.writeTextSync(newSessionFile, `${lines.join("\n")}\n`);
3163
+ this.#fileEntries = [header, ...pathWithoutLabels, ...labelEntries];
3164
+ this.#sessionId = newSessionId;
3165
+ this.#sessionFile = newSessionFile;
3166
+ this.#flushed = true;
3167
+ this.#buildIndex();
3168
+ return newSessionFile;
3169
+ }
3170
+
3171
+ // In-memory mode: replace current session with the path + labels
3172
+ const labelEntries: LabelEntry[] = [];
3173
+ let parentId = pathWithoutLabels[pathWithoutLabels.length - 1]?.id || null;
3174
+ for (const { targetId, label } of labelsToWrite) {
3175
+ const labelEntry: LabelEntry = {
3176
+ type: "label",
3177
+ id: generateId(new Set([...pathEntryIds, ...labelEntries.map(e => e.id)])),
3178
+ parentId,
3179
+ timestamp: new Date().toISOString(),
3180
+ targetId,
3181
+ label,
3182
+ };
3183
+ labelEntries.push(labelEntry);
3184
+ parentId = labelEntry.id;
3185
+ }
3186
+ this.#fileEntries = [header, ...pathWithoutLabels, ...labelEntries];
3187
+ this.#sessionId = newSessionId;
3188
+ this.#buildIndex();
3189
+ return undefined;
3190
+ }
3191
+
3192
+ /**
3193
+ * Resolve the canonical default session directory for a cwd.
3194
+ */
3195
+ static getDefaultSessionDir(
3196
+ cwd: string,
3197
+ agentDir?: string,
3198
+ storage: SessionStorage = new FileSessionStorage(),
3199
+ ): string {
3200
+ return computeDefaultSessionDir(cwd, storage, getSessionsDir(agentDir));
3201
+ }
3202
+
3203
+ /**
3204
+ * Create a new session.
3205
+ * @param cwd Working directory (stored in session header)
3206
+ * @param sessionDir Optional session directory. If omitted, uses default (~/.gjc/agent/sessions/<encoded-cwd>/).
3207
+ */
3208
+ static create(cwd: string, sessionDir?: string, storage: SessionStorage = new FileSessionStorage()): SessionManager {
3209
+ const dir = sessionDir ?? SessionManager.getDefaultSessionDir(cwd, undefined, storage);
3210
+ const manager = new SessionManager(cwd, dir, true, storage);
3211
+ manager.#initNewSession();
3212
+ return manager;
3213
+ }
3214
+
3215
+ /**
3216
+ * Fork a session into the current project directory.
3217
+ * Copies history from another session file while creating a new session file in the current sessionDir.
3218
+ */
3219
+ static async forkFrom(
3220
+ sourcePath: string,
3221
+ cwd: string,
3222
+ sessionDir?: string,
3223
+ storage: SessionStorage = new FileSessionStorage(),
3224
+ ): Promise<SessionManager> {
3225
+ const dir = sessionDir ?? SessionManager.getDefaultSessionDir(cwd, undefined, storage);
3226
+ const manager = new SessionManager(cwd, dir, true, storage);
3227
+ const forkEntries = structuredClone(await loadEntriesFromFile(sourcePath, storage)) as FileEntry[];
3228
+ migrateToCurrentVersion(forkEntries);
3229
+ await resolveBlobRefsInEntries(forkEntries, manager.#blobStore);
3230
+ const sourceHeader = forkEntries.find(e => e.type === "session") as SessionHeader | undefined;
3231
+ const historyEntries = forkEntries.filter(entry => entry.type !== "session") as SessionEntry[];
3232
+ manager.#newSessionSync({ parentSession: sourceHeader?.id });
3233
+ const newHeader = manager.#fileEntries[0] as SessionHeader;
3234
+ newHeader.title = sourceHeader?.title;
3235
+ newHeader.titleSource = sourceHeader?.titleSource;
3236
+ manager.#fileEntries = [newHeader, ...historyEntries];
3237
+ manager.#sessionName = newHeader.title;
3238
+ manager.#titleSource = newHeader.titleSource;
3239
+ manager.sanitizeLoadedOpenAIResponsesReplayMetadata();
3240
+ manager.#buildIndex();
3241
+ await manager.#rewriteFile();
3242
+ return manager;
3243
+ }
3244
+
3245
+ /**
3246
+ * Open a specific session file.
3247
+ * @param path Path to session file
3248
+ * @param sessionDir Optional session directory for /new or /branch. If omitted, derives from file's parent.
3249
+ */
3250
+ static async open(
3251
+ filePath: string,
3252
+ sessionDir?: string,
3253
+ storage: SessionStorage = new FileSessionStorage(),
3254
+ ): Promise<SessionManager> {
3255
+ // Extract cwd from session header if possible, otherwise use getProjectDir()
3256
+ const entries = await loadEntriesFromFile(filePath, storage);
3257
+ const header = entries.find(e => e.type === "session") as SessionHeader | undefined;
3258
+ const cwd = header?.cwd ?? getProjectDir();
3259
+ // If no sessionDir provided, derive from file's parent directory
3260
+ const dir = sessionDir ?? path.resolve(filePath, "..");
3261
+ const manager = new SessionManager(cwd, dir, true, storage);
3262
+ await manager.#initSessionFile(filePath);
3263
+ return manager;
3264
+ }
3265
+
3266
+ /**
3267
+ * Continue the most recent session, or create new if none.
3268
+ * @param cwd Working directory
3269
+ * @param sessionDir Optional session directory. If omitted, uses default (~/.gjc/agent/sessions/<encoded-cwd>/).
3270
+ */
3271
+ static async continueRecent(
3272
+ cwd: string,
3273
+ sessionDir?: string,
3274
+ storage: SessionStorage = new FileSessionStorage(),
3275
+ ): Promise<SessionManager> {
3276
+ const dir = sessionDir ?? SessionManager.getDefaultSessionDir(cwd, undefined, storage);
3277
+ // Prefer terminal-scoped breadcrumb (handles concurrent sessions correctly)
3278
+ const terminalSession = await readTerminalBreadcrumb(cwd);
3279
+ const mostRecent = terminalSession ?? (await findMostRecentSession(dir, storage));
3280
+ const manager = new SessionManager(cwd, dir, true, storage);
3281
+ if (mostRecent) {
3282
+ await manager.#initSessionFile(mostRecent);
3283
+ } else {
3284
+ manager.#initNewSession();
3285
+ }
3286
+ return manager;
3287
+ }
3288
+
3289
+ /** Create an in-memory session (no file persistence) */
3290
+ static inMemory(
3291
+ cwd: string = getProjectDir(),
3292
+ storage: SessionStorage = new MemorySessionStorage(),
3293
+ ): SessionManager {
3294
+ const manager = new SessionManager(cwd, "", false, storage);
3295
+ manager.#initNewSession();
3296
+ return manager;
3297
+ }
3298
+
3299
+ /**
3300
+ * List all sessions.
3301
+ * @param cwd Working directory (used to compute default session directory)
3302
+ * @param sessionDir Optional session directory. If omitted, uses default (~/.gjc/agent/sessions/<encoded-cwd>/).
3303
+ */
3304
+ static async list(
3305
+ cwd: string,
3306
+ sessionDir?: string,
3307
+ storage: SessionStorage = new FileSessionStorage(),
3308
+ ): Promise<SessionInfo[]> {
3309
+ const dir = sessionDir ?? SessionManager.getDefaultSessionDir(cwd, undefined, storage);
3310
+ try {
3311
+ await recoverOrphanedBackups(dir, storage);
3312
+ const files = storage.listFilesSync(dir, "*.jsonl");
3313
+ return await collectSessionsFromFiles(files, storage);
3314
+ } catch {
3315
+ return [];
3316
+ }
3317
+ }
3318
+
3319
+ /**
3320
+ * List all sessions across all project directories.
3321
+ */
3322
+ static async listAll(storage: SessionStorage = new FileSessionStorage()): Promise<SessionInfo[]> {
3323
+ const sessionsRoot = path.join(getDefaultAgentDir(), "sessions");
3324
+ try {
3325
+ const files = await Array.fromAsync(new Bun.Glob("*/*.jsonl").scan(sessionsRoot), name =>
3326
+ path.join(sessionsRoot, name),
3327
+ );
3328
+ return await collectSessionsFromFiles(files, storage);
3329
+ } catch {
3330
+ return [];
3331
+ }
3332
+ }
3333
+ }