@draht/coding-agent 2026.6.11 → 2026.7.7-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (332) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +1 -4
  3. package/dist/bun/register-bedrock.d.ts.map +1 -1
  4. package/dist/bun/register-bedrock.js +1 -1
  5. package/dist/bun/register-bedrock.js.map +1 -1
  6. package/dist/bun/restore-sandbox-env.d.ts +4 -0
  7. package/dist/bun/restore-sandbox-env.d.ts.map +1 -1
  8. package/dist/bun/restore-sandbox-env.js +4 -0
  9. package/dist/bun/restore-sandbox-env.js.map +1 -1
  10. package/dist/cli/args.d.ts.map +1 -1
  11. package/dist/cli/args.js +4 -4
  12. package/dist/cli/args.js.map +1 -1
  13. package/dist/cli/config-selector.d.ts +4 -2
  14. package/dist/cli/config-selector.d.ts.map +1 -1
  15. package/dist/cli/config-selector.js +1 -1
  16. package/dist/cli/config-selector.js.map +1 -1
  17. package/dist/cli/file-processor.d.ts.map +1 -1
  18. package/dist/cli/file-processor.js +13 -25
  19. package/dist/cli/file-processor.js.map +1 -1
  20. package/dist/cli/session-picker.d.ts +2 -1
  21. package/dist/cli/session-picker.d.ts.map +1 -1
  22. package/dist/cli/session-picker.js +5 -4
  23. package/dist/cli/session-picker.js.map +1 -1
  24. package/dist/cli/startup-ui.d.ts +5 -1
  25. package/dist/cli/startup-ui.d.ts.map +1 -1
  26. package/dist/cli/startup-ui.js +94 -22
  27. package/dist/cli/startup-ui.js.map +1 -1
  28. package/dist/config.d.ts +6 -2
  29. package/dist/config.d.ts.map +1 -1
  30. package/dist/config.js +35 -16
  31. package/dist/config.js.map +1 -1
  32. package/dist/core/agent-session.d.ts +10 -4
  33. package/dist/core/agent-session.d.ts.map +1 -1
  34. package/dist/core/agent-session.js +124 -83
  35. package/dist/core/agent-session.js.map +1 -1
  36. package/dist/core/auth-storage.d.ts +11 -12
  37. package/dist/core/auth-storage.d.ts.map +1 -1
  38. package/dist/core/auth-storage.js +25 -26
  39. package/dist/core/auth-storage.js.map +1 -1
  40. package/dist/core/compaction/branch-summarization.d.ts +3 -1
  41. package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
  42. package/dist/core/compaction/branch-summarization.js +3 -3
  43. package/dist/core/compaction/branch-summarization.js.map +1 -1
  44. package/dist/core/compaction/compaction.d.ts +5 -4
  45. package/dist/core/compaction/compaction.d.ts.map +1 -1
  46. package/dist/core/compaction/compaction.js +23 -20
  47. package/dist/core/compaction/compaction.js.map +1 -1
  48. package/dist/core/export-html/vendor/marked.min.js +75 -3
  49. package/dist/core/extensions/index.d.ts +1 -1
  50. package/dist/core/extensions/index.d.ts.map +1 -1
  51. package/dist/core/extensions/index.js.map +1 -1
  52. package/dist/core/extensions/loader.d.ts +2 -3
  53. package/dist/core/extensions/loader.d.ts.map +1 -1
  54. package/dist/core/extensions/loader.js +77 -14
  55. package/dist/core/extensions/loader.js.map +1 -1
  56. package/dist/core/extensions/runner.d.ts +2 -1
  57. package/dist/core/extensions/runner.d.ts.map +1 -1
  58. package/dist/core/extensions/runner.js +33 -1
  59. package/dist/core/extensions/runner.js.map +1 -1
  60. package/dist/core/extensions/types.d.ts +29 -2
  61. package/dist/core/extensions/types.d.ts.map +1 -1
  62. package/dist/core/extensions/types.js.map +1 -1
  63. package/dist/core/http-dispatcher.d.ts +1 -0
  64. package/dist/core/http-dispatcher.d.ts.map +1 -1
  65. package/dist/core/http-dispatcher.js +45 -2
  66. package/dist/core/http-dispatcher.js.map +1 -1
  67. package/dist/core/index.d.ts +1 -1
  68. package/dist/core/index.d.ts.map +1 -1
  69. package/dist/core/index.js.map +1 -1
  70. package/dist/core/keybindings.d.ts +1 -1
  71. package/dist/core/keybindings.d.ts.map +1 -1
  72. package/dist/core/keybindings.js +3 -6
  73. package/dist/core/keybindings.js.map +1 -1
  74. package/dist/core/model-registry.d.ts +2 -1
  75. package/dist/core/model-registry.d.ts.map +1 -1
  76. package/dist/core/model-registry.js +32 -67
  77. package/dist/core/model-registry.js.map +1 -1
  78. package/dist/core/model-resolver.d.ts +11 -1
  79. package/dist/core/model-resolver.d.ts.map +1 -1
  80. package/dist/core/model-resolver.js +37 -19
  81. package/dist/core/model-resolver.js.map +1 -1
  82. package/dist/core/package-manager.d.ts +5 -2
  83. package/dist/core/package-manager.d.ts.map +1 -1
  84. package/dist/core/package-manager.js +99 -43
  85. package/dist/core/package-manager.js.map +1 -1
  86. package/dist/core/project-trust.d.ts.map +1 -1
  87. package/dist/core/project-trust.js +4 -3
  88. package/dist/core/project-trust.js.map +1 -1
  89. package/dist/core/provider-attribution.d.ts +2 -2
  90. package/dist/core/provider-attribution.d.ts.map +1 -1
  91. package/dist/core/provider-attribution.js.map +1 -1
  92. package/dist/core/provider-display-names.d.ts.map +1 -1
  93. package/dist/core/provider-display-names.js +1 -1
  94. package/dist/core/provider-display-names.js.map +1 -1
  95. package/dist/core/resolve-config-value.d.ts +7 -8
  96. package/dist/core/resolve-config-value.d.ts.map +1 -1
  97. package/dist/core/resolve-config-value.js +24 -26
  98. package/dist/core/resolve-config-value.js.map +1 -1
  99. package/dist/core/resource-loader.d.ts +3 -2
  100. package/dist/core/resource-loader.d.ts.map +1 -1
  101. package/dist/core/resource-loader.js +16 -6
  102. package/dist/core/resource-loader.js.map +1 -1
  103. package/dist/core/sdk.d.ts +9 -9
  104. package/dist/core/sdk.d.ts.map +1 -1
  105. package/dist/core/sdk.js +3 -1
  106. package/dist/core/sdk.js.map +1 -1
  107. package/dist/core/session-manager.d.ts +21 -2
  108. package/dist/core/session-manager.d.ts.map +1 -1
  109. package/dist/core/session-manager.js +121 -77
  110. package/dist/core/session-manager.js.map +1 -1
  111. package/dist/core/settings-manager.d.ts +10 -1
  112. package/dist/core/settings-manager.d.ts.map +1 -1
  113. package/dist/core/settings-manager.js +29 -3
  114. package/dist/core/settings-manager.js.map +1 -1
  115. package/dist/core/system-prompt.d.ts.map +1 -1
  116. package/dist/core/system-prompt.js +7 -7
  117. package/dist/core/system-prompt.js.map +1 -1
  118. package/dist/core/timings.d.ts +4 -2
  119. package/dist/core/timings.d.ts.map +1 -1
  120. package/dist/core/timings.js +24 -14
  121. package/dist/core/timings.js.map +1 -1
  122. package/dist/core/tools/bash.d.ts.map +1 -1
  123. package/dist/core/tools/bash.js +32 -8
  124. package/dist/core/tools/bash.js.map +1 -1
  125. package/dist/core/tools/edit-diff.d.ts +23 -4
  126. package/dist/core/tools/edit-diff.d.ts.map +1 -1
  127. package/dist/core/tools/edit-diff.js +99 -20
  128. package/dist/core/tools/edit-diff.js.map +1 -1
  129. package/dist/core/tools/edit.d.ts.map +1 -1
  130. package/dist/core/tools/edit.js +2 -2
  131. package/dist/core/tools/edit.js.map +1 -1
  132. package/dist/core/tools/find.d.ts.map +1 -1
  133. package/dist/core/tools/find.js +19 -11
  134. package/dist/core/tools/find.js.map +1 -1
  135. package/dist/core/tools/read.d.ts.map +1 -1
  136. package/dist/core/tools/read.js +12 -25
  137. package/dist/core/tools/read.js.map +1 -1
  138. package/dist/core/trust-manager.d.ts +8 -3
  139. package/dist/core/trust-manager.d.ts.map +1 -1
  140. package/dist/core/trust-manager.js +26 -11
  141. package/dist/core/trust-manager.js.map +1 -1
  142. package/dist/index.d.ts +6 -4
  143. package/dist/index.d.ts.map +1 -1
  144. package/dist/index.js +5 -3
  145. package/dist/index.js.map +1 -1
  146. package/dist/main.d.ts +2 -2
  147. package/dist/main.d.ts.map +1 -1
  148. package/dist/main.js +47 -15
  149. package/dist/main.js.map +1 -1
  150. package/dist/migrations.d.ts.map +1 -1
  151. package/dist/migrations.js +1 -123
  152. package/dist/migrations.js.map +1 -1
  153. package/dist/modes/interactive/assets/clankolas.png +0 -0
  154. package/dist/modes/interactive/components/assistant-message.d.ts +3 -1
  155. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
  156. package/dist/modes/interactive/components/assistant-message.js +23 -15
  157. package/dist/modes/interactive/components/assistant-message.js.map +1 -1
  158. package/dist/modes/interactive/components/config-selector.d.ts +34 -3
  159. package/dist/modes/interactive/components/config-selector.d.ts.map +1 -1
  160. package/dist/modes/interactive/components/config-selector.js +284 -29
  161. package/dist/modes/interactive/components/config-selector.js.map +1 -1
  162. package/dist/modes/interactive/components/custom-entry.d.ts +19 -0
  163. package/dist/modes/interactive/components/custom-entry.d.ts.map +1 -0
  164. package/dist/modes/interactive/components/custom-entry.js +52 -0
  165. package/dist/modes/interactive/components/custom-entry.js.map +1 -0
  166. package/dist/modes/interactive/components/extension-editor.d.ts +3 -1
  167. package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -1
  168. package/dist/modes/interactive/components/extension-editor.js +12 -3
  169. package/dist/modes/interactive/components/extension-editor.js.map +1 -1
  170. package/dist/modes/interactive/components/footer.d.ts +2 -2
  171. package/dist/modes/interactive/components/footer.d.ts.map +1 -1
  172. package/dist/modes/interactive/components/footer.js +4 -0
  173. package/dist/modes/interactive/components/footer.js.map +1 -1
  174. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
  175. package/dist/modes/interactive/components/login-dialog.js +0 -1
  176. package/dist/modes/interactive/components/login-dialog.js.map +1 -1
  177. package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
  178. package/dist/modes/interactive/components/model-selector.js +2 -1
  179. package/dist/modes/interactive/components/model-selector.js.map +1 -1
  180. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
  181. package/dist/modes/interactive/components/scoped-models-selector.js +4 -1
  182. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
  183. package/dist/modes/interactive/components/session-selector.d.ts +0 -1
  184. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
  185. package/dist/modes/interactive/components/session-selector.js +14 -8
  186. package/dist/modes/interactive/components/session-selector.js.map +1 -1
  187. package/dist/modes/interactive/components/settings-selector.d.ts +4 -0
  188. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  189. package/dist/modes/interactive/components/settings-selector.js +188 -16
  190. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  191. package/dist/modes/interactive/components/status-indicator.d.ts +28 -0
  192. package/dist/modes/interactive/components/status-indicator.d.ts.map +1 -0
  193. package/dist/modes/interactive/components/status-indicator.js +60 -0
  194. package/dist/modes/interactive/components/status-indicator.js.map +1 -0
  195. package/dist/modes/interactive/components/tree-selector.d.ts +1 -1
  196. package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -1
  197. package/dist/modes/interactive/components/tree-selector.js +130 -15
  198. package/dist/modes/interactive/components/tree-selector.js.map +1 -1
  199. package/dist/modes/interactive/components/trust-selector.d.ts.map +1 -1
  200. package/dist/modes/interactive/components/trust-selector.js +4 -4
  201. package/dist/modes/interactive/components/trust-selector.js.map +1 -1
  202. package/dist/modes/interactive/components/user-message.d.ts +6 -2
  203. package/dist/modes/interactive/components/user-message.d.ts.map +1 -1
  204. package/dist/modes/interactive/components/user-message.js +19 -6
  205. package/dist/modes/interactive/components/user-message.js.map +1 -1
  206. package/dist/modes/interactive/interactive-mode.d.ts +13 -11
  207. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  208. package/dist/modes/interactive/interactive-mode.js +252 -233
  209. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  210. package/dist/modes/interactive/model-search.d.ts +12 -0
  211. package/dist/modes/interactive/model-search.d.ts.map +1 -0
  212. package/dist/modes/interactive/model-search.js +15 -0
  213. package/dist/modes/interactive/model-search.js.map +1 -0
  214. package/dist/modes/interactive/theme/theme-controller.d.ts +29 -0
  215. package/dist/modes/interactive/theme/theme-controller.d.ts.map +1 -0
  216. package/dist/modes/interactive/theme/theme-controller.js +102 -0
  217. package/dist/modes/interactive/theme/theme-controller.js.map +1 -0
  218. package/dist/modes/interactive/theme/theme-schema.json +2 -1
  219. package/dist/modes/interactive/theme/theme.d.ts +27 -8
  220. package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
  221. package/dist/modes/interactive/theme/theme.js +64 -42
  222. package/dist/modes/interactive/theme/theme.js.map +1 -1
  223. package/dist/modes/rpc/rpc-client.d.ts +19 -4
  224. package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  225. package/dist/modes/rpc/rpc-client.js +14 -0
  226. package/dist/modes/rpc/rpc-client.js.map +1 -1
  227. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  228. package/dist/modes/rpc/rpc-mode.js +17 -1
  229. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  230. package/dist/modes/rpc/rpc-types.d.ts +26 -0
  231. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  232. package/dist/modes/rpc/rpc-types.js.map +1 -1
  233. package/dist/package-manager-cli.d.ts +2 -2
  234. package/dist/package-manager-cli.d.ts.map +1 -1
  235. package/dist/package-manager-cli.js +152 -48
  236. package/dist/package-manager-cli.js.map +1 -1
  237. package/dist/prompts/agents/build.md +38 -0
  238. package/dist/prompts/agents/plan.md +36 -0
  239. package/dist/prompts/agents/verify.md +36 -4
  240. package/dist/prompts/commands/brainstorm.md +162 -0
  241. package/dist/prompts/commands/discuss-phase.md +1 -1
  242. package/dist/prompts/commands/execute-phase.md +4 -1
  243. package/dist/prompts/commands/fix.md +12 -0
  244. package/dist/prompts/commands/init-project.md +1 -0
  245. package/dist/prompts/commands/map-codebase.md +1 -1
  246. package/dist/prompts/commands/new-project.md +1 -0
  247. package/dist/prompts/commands/next-milestone.md +2 -0
  248. package/dist/prompts/commands/orchestrate.md +4 -4
  249. package/dist/prompts/commands/plan-phase.md +5 -0
  250. package/dist/prompts/commands/quick.md +4 -0
  251. package/dist/prompts/commands/review.md +4 -1
  252. package/dist/prompts/commands/verify-work.md +10 -8
  253. package/dist/rpc-entry.d.ts +3 -0
  254. package/dist/rpc-entry.d.ts.map +1 -0
  255. package/dist/rpc-entry.js +10 -0
  256. package/dist/rpc-entry.js.map +1 -0
  257. package/dist/utils/child-process.d.ts +7 -4
  258. package/dist/utils/child-process.d.ts.map +1 -1
  259. package/dist/utils/child-process.js +23 -5
  260. package/dist/utils/child-process.js.map +1 -1
  261. package/dist/utils/image-convert.d.ts +1 -0
  262. package/dist/utils/image-convert.d.ts.map +1 -1
  263. package/dist/utils/image-convert.js +21 -15
  264. package/dist/utils/image-convert.js.map +1 -1
  265. package/dist/utils/image-process.d.ts +18 -0
  266. package/dist/utils/image-process.d.ts.map +1 -0
  267. package/dist/utils/image-process.js +83 -0
  268. package/dist/utils/image-process.js.map +1 -0
  269. package/dist/utils/mime.d.ts.map +1 -1
  270. package/dist/utils/mime.js +41 -0
  271. package/dist/utils/mime.js.map +1 -1
  272. package/dist/utils/shell.d.ts +1 -0
  273. package/dist/utils/shell.d.ts.map +1 -1
  274. package/dist/utils/shell.js +12 -5
  275. package/dist/utils/shell.js.map +1 -1
  276. package/dist/utils/version-check.d.ts.map +1 -1
  277. package/dist/utils/version-check.js +4 -27
  278. package/dist/utils/version-check.js.map +1 -1
  279. package/docs/extensions.md +55 -8
  280. package/docs/keybindings.md +1 -1
  281. package/docs/packages.md +2 -2
  282. package/docs/providers.md +2 -1
  283. package/docs/rpc.md +58 -0
  284. package/docs/sdk.md +50 -0
  285. package/docs/session-format.md +18 -8
  286. package/docs/settings.md +10 -0
  287. package/docs/usage.md +2 -2
  288. package/examples/extensions/README.md +1 -0
  289. package/examples/extensions/custom-compaction.ts +2 -1
  290. package/examples/extensions/custom-provider-anthropic/index.ts +1 -1
  291. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  292. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  293. package/examples/extensions/custom-provider-gitlab-duo/index.ts +9 -5
  294. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  295. package/examples/extensions/custom-provider-gitlab-duo/test.ts +1 -1
  296. package/examples/extensions/entry-renderer.ts +41 -0
  297. package/examples/extensions/gondolin/package-lock.json +2 -2
  298. package/examples/extensions/gondolin/package.json +1 -1
  299. package/examples/extensions/handoff.ts +2 -2
  300. package/examples/extensions/plan-mode/index.ts +87 -37
  301. package/examples/extensions/preset.ts +10 -4
  302. package/examples/extensions/provider-payload.ts +5 -5
  303. package/examples/extensions/qna.ts +2 -2
  304. package/examples/extensions/question.ts +32 -18
  305. package/examples/extensions/questionnaire.ts +41 -28
  306. package/examples/extensions/sandbox/index.ts +2 -2
  307. package/examples/extensions/sandbox/package-lock.json +2 -2
  308. package/examples/extensions/sandbox/package.json +1 -1
  309. package/examples/extensions/subagent/agents.ts +2 -2
  310. package/examples/extensions/subagent/index.ts +9 -3
  311. package/examples/extensions/summarize.ts +2 -1
  312. package/examples/extensions/with-deps/package-lock.json +2 -2
  313. package/examples/extensions/with-deps/package.json +1 -1
  314. package/examples/sdk/02-custom-model.ts +1 -1
  315. package/examples/sdk/12-full-control.ts +2 -2
  316. package/package.json +14 -15
  317. package/prompts/agents/build.md +38 -0
  318. package/prompts/agents/plan.md +36 -0
  319. package/prompts/agents/verify.md +36 -4
  320. package/prompts/commands/brainstorm.md +162 -0
  321. package/prompts/commands/discuss-phase.md +1 -1
  322. package/prompts/commands/execute-phase.md +4 -1
  323. package/prompts/commands/fix.md +12 -0
  324. package/prompts/commands/init-project.md +1 -0
  325. package/prompts/commands/map-codebase.md +1 -1
  326. package/prompts/commands/new-project.md +1 -0
  327. package/prompts/commands/next-milestone.md +2 -0
  328. package/prompts/commands/orchestrate.md +4 -4
  329. package/prompts/commands/plan-phase.md +5 -0
  330. package/prompts/commands/quick.md +4 -0
  331. package/prompts/commands/review.md +4 -1
  332. package/prompts/commands/verify-work.md +10 -8
@@ -112,46 +112,40 @@ export function getLatestCompactionEntry(entries) {
112
112
  }
113
113
  return null;
114
114
  }
115
- /**
116
- * Build the session context from entries using tree traversal.
117
- * If leafId is provided, walks from that entry to root.
118
- * Handles compaction and branch summaries along the path.
119
- */
120
- export function buildSessionContext(entries, leafId, byId) {
121
- // Build uuid index if not available
122
- if (!byId) {
123
- byId = new Map();
124
- for (const entry of entries) {
125
- byId.set(entry.id, entry);
126
- }
115
+ function buildEntryIndex(entries, byId) {
116
+ if (byId)
117
+ return byId;
118
+ const index = new Map();
119
+ for (const entry of entries) {
120
+ index.set(entry.id, entry);
127
121
  }
128
- // Find leaf
122
+ return index;
123
+ }
124
+ function buildSessionPath(entries, leafId, byId) {
125
+ const index = buildEntryIndex(entries, byId);
129
126
  let leaf;
130
127
  if (leafId === null) {
131
- // Explicitly null - return no messages (navigated to before first entry)
132
- return { messages: [], thinkingLevel: "off", model: null };
128
+ return [];
133
129
  }
134
130
  if (leafId) {
135
- leaf = byId.get(leafId);
136
- }
137
- if (!leaf) {
138
- // Fallback to last entry (when leafId is undefined)
139
- leaf = entries[entries.length - 1];
131
+ leaf = index.get(leafId);
140
132
  }
133
+ leaf ??= entries[entries.length - 1];
141
134
  if (!leaf) {
142
- return { messages: [], thinkingLevel: "off", model: null };
135
+ return [];
143
136
  }
144
- // Walk from leaf to root, collecting path
145
137
  const path = [];
146
138
  let current = leaf;
147
139
  while (current) {
148
- path.unshift(current);
149
- current = current.parentId ? byId.get(current.parentId) : undefined;
140
+ path.push(current);
141
+ current = current.parentId ? index.get(current.parentId) : undefined;
150
142
  }
151
- // Extract settings and find compaction
143
+ path.reverse();
144
+ return path;
145
+ }
146
+ function getSessionContextSettings(path) {
152
147
  let thinkingLevel = "off";
153
148
  let model = null;
154
- let compaction = null;
155
149
  for (const entry of path) {
156
150
  if (entry.type === "thinking_level_change") {
157
151
  thinkingLevel = entry.thinkingLevel;
@@ -162,55 +156,83 @@ export function buildSessionContext(entries, leafId, byId) {
162
156
  else if (entry.type === "message" && entry.message.role === "assistant") {
163
157
  model = { provider: entry.message.provider, modelId: entry.message.model };
164
158
  }
165
- else if (entry.type === "compaction") {
166
- compaction = entry;
167
- }
168
159
  }
169
- // Build messages and collect corresponding entries
170
- // When there's a compaction, we need to:
171
- // 1. Emit summary first (entry = compaction)
172
- // 2. Emit kept messages (from firstKeptEntryId up to compaction)
173
- // 3. Emit messages after compaction
174
- const messages = [];
175
- const appendMessage = (entry) => {
176
- if (entry.type === "message") {
177
- messages.push(entry.message);
178
- }
179
- else if (entry.type === "custom_message") {
180
- messages.push(createCustomMessage(entry.customType, entry.content, entry.display, entry.details, entry.timestamp));
181
- }
182
- else if (entry.type === "branch_summary" && entry.summary) {
183
- messages.push(createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp));
184
- }
185
- };
186
- if (compaction) {
187
- // Emit summary first
188
- messages.push(createCompactionSummaryMessage(compaction.summary, compaction.tokensBefore, compaction.timestamp));
189
- // Find compaction index in path
190
- const compactionIdx = path.findIndex((e) => e.type === "compaction" && e.id === compaction.id);
191
- // Emit kept messages (before compaction, starting from firstKeptEntryId)
192
- let foundFirstKept = false;
193
- for (let i = 0; i < compactionIdx; i++) {
194
- const entry = path[i];
195
- if (entry.id === compaction.firstKeptEntryId) {
196
- foundFirstKept = true;
197
- }
198
- if (foundFirstKept) {
199
- appendMessage(entry);
200
- }
160
+ return { thinkingLevel, model };
161
+ }
162
+ /**
163
+ * Project one selected session entry into LLM/runtime messages.
164
+ * Plain custom entries are display/state entries and do not participate in context.
165
+ */
166
+ export function sessionEntryToContextMessages(entry) {
167
+ if (entry.type === "message") {
168
+ const message = entry.message;
169
+ // Session files are parsed without validation; old versions, forks, or
170
+ // hand-edited files can contain messages with null/missing content.
171
+ if ((message.role === "user" || message.role === "assistant" || message.role === "toolResult") &&
172
+ message.content == null) {
173
+ return [{ ...message, content: [] }];
201
174
  }
202
- // Emit messages after compaction
203
- for (let i = compactionIdx + 1; i < path.length; i++) {
204
- const entry = path[i];
205
- appendMessage(entry);
175
+ return [message];
176
+ }
177
+ if (entry.type === "custom_message") {
178
+ return [
179
+ createCustomMessage(entry.customType, entry.content ?? [], entry.display, entry.details, entry.timestamp),
180
+ ];
181
+ }
182
+ if (entry.type === "branch_summary" && entry.summary) {
183
+ return [createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp)];
184
+ }
185
+ if (entry.type === "compaction") {
186
+ return [createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp)];
187
+ }
188
+ return [];
189
+ }
190
+ /**
191
+ * Build the active, compaction-aware session entry list.
192
+ *
193
+ * This follows the current leaf path. If the path contains compaction entries,
194
+ * the latest compaction is represented by the compaction entry itself, followed
195
+ * by the kept entries starting at firstKeptEntryId and all entries after the
196
+ * compaction entry. Older summarized entries are omitted.
197
+ */
198
+ export function buildContextEntries(entries, leafId, byId) {
199
+ const path = buildSessionPath(entries, leafId, byId);
200
+ let compaction = null;
201
+ for (const entry of path) {
202
+ if (entry.type === "compaction") {
203
+ compaction = entry;
206
204
  }
207
205
  }
208
- else {
209
- // No compaction - emit all messages, handle branch summaries and custom messages
210
- for (const entry of path) {
211
- appendMessage(entry);
206
+ if (!compaction) {
207
+ return path;
208
+ }
209
+ const compactionIdx = path.findIndex((entry) => entry.id === compaction.id);
210
+ if (compactionIdx < 0) {
211
+ return path;
212
+ }
213
+ const contextEntries = [compaction];
214
+ let foundFirstKept = false;
215
+ for (let i = 0; i < compactionIdx; i++) {
216
+ const entry = path[i];
217
+ if (entry.id === compaction.firstKeptEntryId) {
218
+ foundFirstKept = true;
219
+ }
220
+ if (foundFirstKept) {
221
+ contextEntries.push(entry);
212
222
  }
213
223
  }
224
+ contextEntries.push(...path.slice(compactionIdx + 1));
225
+ return contextEntries;
226
+ }
227
+ /**
228
+ * Build the session context from entries using tree traversal.
229
+ * If leafId is provided, walks from that entry to root.
230
+ * Handles compaction and branch summaries along the path.
231
+ */
232
+ export function buildSessionContext(entries, leafId, byId) {
233
+ const path = buildSessionPath(entries, leafId, byId);
234
+ const { thinkingLevel, model } = getSessionContextSettings(path);
235
+ const messages = buildContextEntries(entries, leafId, byId).flatMap(sessionEntryToContextMessages);
214
236
  return { messages, thinkingLevel, model };
215
237
  }
216
238
  /**
@@ -532,10 +554,13 @@ export class SessionManager {
532
554
  this.sessionFile = resolvePath(sessionFile);
533
555
  if (existsSync(this.sessionFile)) {
534
556
  this.fileEntries = loadEntriesFromFile(this.sessionFile);
535
- // If file was empty or corrupted (no valid header), truncate and start fresh
536
- // to avoid appending messages without a session header (which breaks the session)
557
+ // If file was empty, initialize it with a valid session header. If it was
558
+ // non-empty but did not parse as a pi session, fail without modifying it.
537
559
  if (this.fileEntries.length === 0) {
538
560
  const explicitPath = this.sessionFile;
561
+ if (statSync(explicitPath).size > 0) {
562
+ throw new Error(`Session file is not a valid pi session: ${explicitPath}`);
563
+ }
539
564
  this.newSession();
540
565
  this.sessionFile = explicitPath;
541
566
  this._rewriteFile();
@@ -573,6 +598,7 @@ export class SessionManager {
573
598
  this.fileEntries = [header];
574
599
  this.byId.clear();
575
600
  this.labelsById.clear();
601
+ this.labelTimestampsById.clear();
576
602
  this.leafId = null;
577
603
  this.flushed = false;
578
604
  if (this.persist) {
@@ -743,12 +769,13 @@ export class SessionManager {
743
769
  }
744
770
  /** Append a session info entry (e.g., display name). Returns entry id. */
745
771
  appendSessionInfo(name) {
772
+ const sanitizedName = name.replace(/[\r\n]+/g, " ").trim();
746
773
  const entry = {
747
774
  type: "session_info",
748
775
  id: generateId(this.byId),
749
776
  parentId: this.leafId,
750
777
  timestamp: new Date().toISOString(),
751
- name: name.trim(),
778
+ name: sanitizedName,
752
779
  };
753
780
  this._appendEntry(entry);
754
781
  return entry.id;
@@ -856,11 +883,19 @@ export class SessionManager {
856
883
  const startId = fromId ?? this.leafId;
857
884
  let current = startId ? this.byId.get(startId) : undefined;
858
885
  while (current) {
859
- path.unshift(current);
886
+ path.push(current);
860
887
  current = current.parentId ? this.byId.get(current.parentId) : undefined;
861
888
  }
889
+ path.reverse();
862
890
  return path;
863
891
  }
892
+ /**
893
+ * Build the active, compaction-aware entry list for context/rendering.
894
+ * Uses tree traversal from current leaf.
895
+ */
896
+ buildContextEntries() {
897
+ return buildContextEntries(this.getEntries(), this.leafId, this.byId);
898
+ }
864
899
  /**
865
900
  * Build the session context (what gets sent to the LLM).
866
901
  * Uses tree traversal from current leaf.
@@ -982,8 +1017,17 @@ export class SessionManager {
982
1017
  if (path.length === 0) {
983
1018
  throw new Error(`Entry ${leafId} not found`);
984
1019
  }
985
- // Filter out LabelEntry from path - we'll recreate them from the resolved map
986
- const pathWithoutLabels = path.filter((e) => e.type !== "label");
1020
+ // Filter out LabelEntry from path - we'll recreate them from the resolved map.
1021
+ // Because labels are real tree entries, later entries can be children of labels;
1022
+ // removing labels requires re-chaining the retained path to avoid orphaned subtrees.
1023
+ const pathWithoutLabels = [];
1024
+ let pathParentId = null;
1025
+ for (const entry of path) {
1026
+ if (entry.type === "label")
1027
+ continue;
1028
+ pathWithoutLabels.push({ ...entry, parentId: pathParentId });
1029
+ pathParentId = entry.id;
1030
+ }
987
1031
  const newSessionId = createSessionId();
988
1032
  const timestamp = new Date().toISOString();
989
1033
  const fileTimestamp = timestamp.replace(/[:.]/g, "-");
@@ -1101,8 +1145,8 @@ export class SessionManager {
1101
1145
  return new SessionManager(cwd, dir, undefined, true);
1102
1146
  }
1103
1147
  /** Create an in-memory session (no file persistence) */
1104
- static inMemory(cwd = process.cwd()) {
1105
- return new SessionManager(cwd, "", undefined, false);
1148
+ static inMemory(cwd = process.cwd(), options) {
1149
+ return new SessionManager(cwd, "", undefined, false, options);
1106
1150
  }
1107
1151
  /**
1108
1152
  * Fork a session from another project directory into the current project.