@namzu/sdk 71.0.1 → 72.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (268) hide show
  1. package/CHANGELOG.md +126 -0
  2. package/dist/agents/QueryAgent.d.ts.map +1 -1
  3. package/dist/agents/QueryAgent.js +1 -0
  4. package/dist/agents/QueryAgent.js.map +1 -1
  5. package/dist/authorization/readonly-commands.d.ts +9 -1
  6. package/dist/authorization/readonly-commands.d.ts.map +1 -1
  7. package/dist/authorization/readonly-commands.js +70 -4
  8. package/dist/authorization/readonly-commands.js.map +1 -1
  9. package/dist/authorization/shell-paths.d.ts.map +1 -1
  10. package/dist/authorization/shell-paths.js +68 -16
  11. package/dist/authorization/shell-paths.js.map +1 -1
  12. package/dist/compaction/index.d.ts +1 -1
  13. package/dist/compaction/index.d.ts.map +1 -1
  14. package/dist/compaction/index.js +1 -1
  15. package/dist/compaction/index.js.map +1 -1
  16. package/dist/compaction/manual.d.ts +8 -0
  17. package/dist/compaction/manual.d.ts.map +1 -1
  18. package/dist/compaction/manual.js +18 -14
  19. package/dist/compaction/manual.js.map +1 -1
  20. package/dist/compaction/model-summary.d.ts +24 -6
  21. package/dist/compaction/model-summary.d.ts.map +1 -1
  22. package/dist/compaction/model-summary.js +152 -28
  23. package/dist/compaction/model-summary.js.map +1 -1
  24. package/dist/compaction/restore.d.ts +23 -0
  25. package/dist/compaction/restore.d.ts.map +1 -1
  26. package/dist/compaction/restore.js +59 -1
  27. package/dist/compaction/restore.js.map +1 -1
  28. package/dist/compaction/summary.d.ts +37 -2
  29. package/dist/compaction/summary.d.ts.map +1 -1
  30. package/dist/compaction/summary.js +46 -6
  31. package/dist/compaction/summary.js.map +1 -1
  32. package/dist/config/registry.d.ts +2 -0
  33. package/dist/config/registry.d.ts.map +1 -1
  34. package/dist/config/registry.js +76 -5
  35. package/dist/config/registry.js.map +1 -1
  36. package/dist/config/runtime.js +3 -3
  37. package/dist/config/runtime.js.map +1 -1
  38. package/dist/connector/index.d.ts +2 -2
  39. package/dist/connector/index.d.ts.map +1 -1
  40. package/dist/connector/index.js +1 -1
  41. package/dist/connector/index.js.map +1 -1
  42. package/dist/connector/mcp/adapter.d.ts +8 -0
  43. package/dist/connector/mcp/adapter.d.ts.map +1 -1
  44. package/dist/connector/mcp/adapter.js +131 -1
  45. package/dist/connector/mcp/adapter.js.map +1 -1
  46. package/dist/connector/mcp/client.d.ts +26 -6
  47. package/dist/connector/mcp/client.d.ts.map +1 -1
  48. package/dist/connector/mcp/client.js +49 -7
  49. package/dist/connector/mcp/client.js.map +1 -1
  50. package/dist/connector/mcp/index.d.ts +2 -0
  51. package/dist/connector/mcp/index.d.ts.map +1 -1
  52. package/dist/connector/mcp/index.js +1 -0
  53. package/dist/connector/mcp/index.js.map +1 -1
  54. package/dist/connector/mcp/sandbox-stdio.d.ts +67 -0
  55. package/dist/connector/mcp/sandbox-stdio.d.ts.map +1 -0
  56. package/dist/connector/mcp/sandbox-stdio.js +178 -0
  57. package/dist/connector/mcp/sandbox-stdio.js.map +1 -0
  58. package/dist/connector/mcp/stdio-program.d.ts +34 -0
  59. package/dist/connector/mcp/stdio-program.d.ts.map +1 -0
  60. package/dist/connector/mcp/stdio-program.js +47 -0
  61. package/dist/connector/mcp/stdio-program.js.map +1 -0
  62. package/dist/connector/mcp/stdio.d.ts +12 -0
  63. package/dist/connector/mcp/stdio.d.ts.map +1 -1
  64. package/dist/connector/mcp/stdio.js +54 -7
  65. package/dist/connector/mcp/stdio.js.map +1 -1
  66. package/dist/connector/web/guarded-fetch.d.ts +51 -2
  67. package/dist/connector/web/guarded-fetch.d.ts.map +1 -1
  68. package/dist/connector/web/guarded-fetch.js +140 -19
  69. package/dist/connector/web/guarded-fetch.js.map +1 -1
  70. package/dist/connector/web/index.d.ts +2 -2
  71. package/dist/connector/web/index.d.ts.map +1 -1
  72. package/dist/connector/web/index.js +1 -1
  73. package/dist/connector/web/index.js.map +1 -1
  74. package/dist/constants/mcp/index.d.ts +29 -0
  75. package/dist/constants/mcp/index.d.ts.map +1 -1
  76. package/dist/constants/mcp/index.js +29 -0
  77. package/dist/constants/mcp/index.js.map +1 -1
  78. package/dist/manager/agent/lifecycle.d.ts +18 -1
  79. package/dist/manager/agent/lifecycle.d.ts.map +1 -1
  80. package/dist/manager/agent/lifecycle.js +53 -8
  81. package/dist/manager/agent/lifecycle.js.map +1 -1
  82. package/dist/plugin/hook-module/capabilities.d.ts.map +1 -1
  83. package/dist/plugin/hook-module/capabilities.js +24 -13
  84. package/dist/plugin/hook-module/capabilities.js.map +1 -1
  85. package/dist/plugin/hook-module/host.d.ts +31 -0
  86. package/dist/plugin/hook-module/host.d.ts.map +1 -1
  87. package/dist/plugin/hook-module/host.js +56 -13
  88. package/dist/plugin/hook-module/host.js.map +1 -1
  89. package/dist/plugin/hook-module/index.d.ts +3 -2
  90. package/dist/plugin/hook-module/index.d.ts.map +1 -1
  91. package/dist/plugin/hook-module/index.js +2 -1
  92. package/dist/plugin/hook-module/index.js.map +1 -1
  93. package/dist/plugin/index.d.ts +2 -1
  94. package/dist/plugin/index.d.ts.map +1 -1
  95. package/dist/plugin/index.js +1 -0
  96. package/dist/plugin/index.js.map +1 -1
  97. package/dist/plugin/shell-hook-execute.d.ts +55 -0
  98. package/dist/plugin/shell-hook-execute.d.ts.map +1 -0
  99. package/dist/plugin/shell-hook-execute.js +73 -0
  100. package/dist/plugin/shell-hook-execute.js.map +1 -0
  101. package/dist/plugin/shell-hook-restrict.d.ts +31 -0
  102. package/dist/plugin/shell-hook-restrict.d.ts.map +1 -0
  103. package/dist/plugin/shell-hook-restrict.js +69 -0
  104. package/dist/plugin/shell-hook-restrict.js.map +1 -0
  105. package/dist/plugin/shell-hook.d.ts +73 -4
  106. package/dist/plugin/shell-hook.d.ts.map +1 -1
  107. package/dist/plugin/shell-hook.js +118 -17
  108. package/dist/plugin/shell-hook.js.map +1 -1
  109. package/dist/public-runtime.d.ts +9 -7
  110. package/dist/public-runtime.d.ts.map +1 -1
  111. package/dist/public-runtime.js +6 -5
  112. package/dist/public-runtime.js.map +1 -1
  113. package/dist/public-types.d.ts +4 -2
  114. package/dist/public-types.d.ts.map +1 -1
  115. package/dist/runtime/jobs/websocket-source.d.ts +2 -22
  116. package/dist/runtime/jobs/websocket-source.d.ts.map +1 -1
  117. package/dist/runtime/jobs/websocket-source.js +2 -33
  118. package/dist/runtime/jobs/websocket-source.js.map +1 -1
  119. package/dist/runtime/query/compact-session.d.ts.map +1 -1
  120. package/dist/runtime/query/compact-session.js +5 -0
  121. package/dist/runtime/query/compact-session.js.map +1 -1
  122. package/dist/runtime/query/events.d.ts +7 -0
  123. package/dist/runtime/query/events.d.ts.map +1 -1
  124. package/dist/runtime/query/events.js +29 -11
  125. package/dist/runtime/query/events.js.map +1 -1
  126. package/dist/runtime/query/executor/tool-call-admission.d.ts.map +1 -1
  127. package/dist/runtime/query/executor/tool-call-admission.js +51 -10
  128. package/dist/runtime/query/executor/tool-call-admission.js.map +1 -1
  129. package/dist/runtime/query/index.d.ts +6 -0
  130. package/dist/runtime/query/index.d.ts.map +1 -1
  131. package/dist/runtime/query/index.js +3 -0
  132. package/dist/runtime/query/index.js.map +1 -1
  133. package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
  134. package/dist/runtime/query/iteration/phases/compaction.js +21 -3
  135. package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
  136. package/dist/runtime/query/iteration/phases/context.d.ts +4 -0
  137. package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
  138. package/dist/runtime/query/iteration/phases/context.js.map +1 -1
  139. package/dist/runtime/query/path-escalation.d.ts.map +1 -1
  140. package/dist/runtime/query/path-escalation.js +90 -17
  141. package/dist/runtime/query/path-escalation.js.map +1 -1
  142. package/dist/runtime/query/steering.d.ts +15 -0
  143. package/dist/runtime/query/steering.d.ts.map +1 -1
  144. package/dist/runtime/query/steering.js +27 -3
  145. package/dist/runtime/query/steering.js.map +1 -1
  146. package/dist/scheduler/completion-inbox.d.ts.map +1 -1
  147. package/dist/scheduler/completion-inbox.js +15 -0
  148. package/dist/scheduler/completion-inbox.js.map +1 -1
  149. package/dist/session/workspace/driver.d.ts +12 -0
  150. package/dist/session/workspace/driver.d.ts.map +1 -1
  151. package/dist/session/workspace/git-worktree.d.ts +16 -0
  152. package/dist/session/workspace/git-worktree.d.ts.map +1 -1
  153. package/dist/session/workspace/git-worktree.js +35 -0
  154. package/dist/session/workspace/git-worktree.js.map +1 -1
  155. package/dist/session/workspace/holds.d.ts +78 -0
  156. package/dist/session/workspace/holds.d.ts.map +1 -0
  157. package/dist/session/workspace/holds.js +150 -0
  158. package/dist/session/workspace/holds.js.map +1 -0
  159. package/dist/session/workspace/index.d.ts +3 -1
  160. package/dist/session/workspace/index.d.ts.map +1 -1
  161. package/dist/session/workspace/index.js +2 -1
  162. package/dist/session/workspace/index.js.map +1 -1
  163. package/dist/store/config/index.d.ts +3 -0
  164. package/dist/store/config/index.d.ts.map +1 -1
  165. package/dist/store/config/index.js +3 -0
  166. package/dist/store/config/index.js.map +1 -1
  167. package/dist/store/session-index/index.d.ts +13 -0
  168. package/dist/store/session-index/index.d.ts.map +1 -1
  169. package/dist/store/session-index/index.js +1 -0
  170. package/dist/store/session-index/index.js.map +1 -1
  171. package/dist/store/session-index/rebuild.d.ts +12 -0
  172. package/dist/store/session-index/rebuild.d.ts.map +1 -1
  173. package/dist/store/session-index/rebuild.js +20 -4
  174. package/dist/store/session-index/rebuild.js.map +1 -1
  175. package/dist/store/session-index/sqlite.d.ts +20 -0
  176. package/dist/store/session-index/sqlite.d.ts.map +1 -1
  177. package/dist/store/session-index/sqlite.js +50 -12
  178. package/dist/store/session-index/sqlite.js.map +1 -1
  179. package/dist/tools/admit-facts.d.ts.map +1 -1
  180. package/dist/tools/admit-facts.js +59 -0
  181. package/dist/tools/admit-facts.js.map +1 -1
  182. package/dist/tools/builtins/grep.d.ts.map +1 -1
  183. package/dist/tools/builtins/grep.js +57 -0
  184. package/dist/tools/builtins/grep.js.map +1 -1
  185. package/dist/tools/builtins/lsp.d.ts.map +1 -1
  186. package/dist/tools/builtins/lsp.js +41 -4
  187. package/dist/tools/builtins/lsp.js.map +1 -1
  188. package/dist/tools/coordinator/ask-user-question.d.ts.map +1 -1
  189. package/dist/tools/coordinator/ask-user-question.js +67 -3
  190. package/dist/tools/coordinator/ask-user-question.js.map +1 -1
  191. package/dist/turn/memory-promoter.d.ts.map +1 -1
  192. package/dist/turn/memory-promoter.js +20 -4
  193. package/dist/turn/memory-promoter.js.map +1 -1
  194. package/dist/types/agent/base.d.ts +6 -0
  195. package/dist/types/agent/base.d.ts.map +1 -1
  196. package/dist/types/agent/task.d.ts +5 -0
  197. package/dist/types/agent/task.d.ts.map +1 -1
  198. package/dist/types/agent/task.js.map +1 -1
  199. package/dist/types/code-navigation/index.d.ts +34 -4
  200. package/dist/types/code-navigation/index.d.ts.map +1 -1
  201. package/dist/types/hitl/index.d.ts +8 -0
  202. package/dist/types/hitl/index.d.ts.map +1 -1
  203. package/dist/types/hitl/index.js.map +1 -1
  204. package/dist/types/message/index.d.ts +7 -0
  205. package/dist/types/message/index.d.ts.map +1 -1
  206. package/dist/types/message/index.js.map +1 -1
  207. package/dist/types/tool/presentation.d.ts +21 -0
  208. package/dist/types/tool/presentation.d.ts.map +1 -1
  209. package/package.json +6 -6
  210. package/src/agents/QueryAgent.ts +1 -0
  211. package/src/authorization/readonly-commands.ts +72 -4
  212. package/src/authorization/shell-paths.ts +75 -18
  213. package/src/compaction/index.ts +1 -0
  214. package/src/compaction/manual.ts +48 -12
  215. package/src/compaction/model-summary.ts +163 -28
  216. package/src/compaction/restore.ts +74 -1
  217. package/src/compaction/summary.ts +69 -3
  218. package/src/config/registry.ts +87 -5
  219. package/src/config/runtime.ts +3 -3
  220. package/src/connector/index.ts +3 -0
  221. package/src/connector/mcp/adapter.ts +137 -1
  222. package/src/connector/mcp/client.ts +49 -7
  223. package/src/connector/mcp/index.ts +5 -0
  224. package/src/connector/mcp/sandbox-stdio.ts +214 -0
  225. package/src/connector/mcp/stdio-program.ts +68 -0
  226. package/src/connector/mcp/stdio.ts +61 -7
  227. package/src/connector/web/guarded-fetch.ts +197 -30
  228. package/src/connector/web/index.ts +4 -1
  229. package/src/constants/mcp/index.ts +31 -0
  230. package/src/manager/agent/lifecycle.ts +58 -13
  231. package/src/plugin/hook-module/capabilities.ts +36 -14
  232. package/src/plugin/hook-module/host.ts +86 -13
  233. package/src/plugin/hook-module/index.ts +3 -0
  234. package/src/plugin/index.ts +4 -0
  235. package/src/plugin/shell-hook-execute.ts +126 -0
  236. package/src/plugin/shell-hook-restrict.ts +66 -0
  237. package/src/plugin/shell-hook.ts +209 -16
  238. package/src/public-runtime.ts +19 -0
  239. package/src/public-types.ts +20 -0
  240. package/src/runtime/jobs/websocket-source.ts +2 -46
  241. package/src/runtime/query/compact-session.ts +5 -0
  242. package/src/runtime/query/events.ts +29 -11
  243. package/src/runtime/query/executor/tool-call-admission.ts +62 -17
  244. package/src/runtime/query/index.ts +10 -0
  245. package/src/runtime/query/iteration/phases/compaction.ts +26 -2
  246. package/src/runtime/query/iteration/phases/context.ts +6 -0
  247. package/src/runtime/query/path-escalation.ts +102 -16
  248. package/src/runtime/query/steering.ts +38 -3
  249. package/src/scheduler/completion-inbox.ts +14 -0
  250. package/src/session/workspace/driver.ts +12 -0
  251. package/src/session/workspace/git-worktree.ts +49 -0
  252. package/src/session/workspace/holds.ts +201 -0
  253. package/src/session/workspace/index.ts +13 -1
  254. package/src/store/config/index.ts +6 -0
  255. package/src/store/session-index/index.ts +14 -0
  256. package/src/store/session-index/rebuild.ts +22 -4
  257. package/src/store/session-index/sqlite.ts +63 -14
  258. package/src/tools/admit-facts.ts +64 -0
  259. package/src/tools/builtins/grep.ts +52 -0
  260. package/src/tools/builtins/lsp.ts +47 -4
  261. package/src/tools/coordinator/ask-user-question.ts +88 -4
  262. package/src/turn/memory-promoter.ts +24 -3
  263. package/src/types/agent/base.ts +7 -0
  264. package/src/types/agent/task.ts +5 -0
  265. package/src/types/code-navigation/index.ts +48 -4
  266. package/src/types/hitl/index.ts +8 -0
  267. package/src/types/message/index.ts +7 -0
  268. package/src/types/tool/presentation.ts +22 -0
@@ -8,7 +8,11 @@
8
8
  * - every command of the line is on the list, the line parses completely, and
9
9
  * nothing in it is read at run time: no `$var` outside single quotes, no
10
10
  * substitution, no subshell or group, no background `&`, no here-document,
11
- * no unquoted glob except for a short set of commands that only list or show;
11
+ * no unquoted glob except for a short set of commands that only list or show.
12
+ * `$PWD` and `${PWD}` are the one expansion the line may carry, because the
13
+ * directory the line starts in is fixed before it runs: they are read as the
14
+ * start directory spelled out, so `grep -r foo "$PWD/src"` is decided like
15
+ * `grep -r foo /the/start/dir/src`;
12
16
  * - a redirection is allowed only to `/dev/null`, as a descriptor copy, or as
13
17
  * an input from a plain file; a `~user` path and a UNC path are refused;
14
18
  * - a leading assignment is allowed only for a variable that cannot change
@@ -32,6 +36,10 @@
32
36
  * are in no table, so they are refused; a repository argument naming a
33
37
  * host, a URL or `user@host` is refused.
34
38
  *
39
+ * The expansion may not name another variable: `$PWDfoo` is a variable of its
40
+ * own, `${PWD:-.}` decides its value at run time, and a `~` or a brace list
41
+ * beside it is refused with every other expansion.
42
+ *
35
43
  * Not ported, and so never read-only here: `ps`, `lsof`, `ss`, `date`,
36
44
  * `man`, `xargs`, `fd`, `docker logs|inspect`, `printf`, `[[`, `test`
37
45
  * and the shell builtins other than `cd`.
@@ -280,6 +288,62 @@ function hasDynamicSyntax(line: string): boolean {
280
288
  return false
281
289
  }
282
290
 
291
+ /**
292
+ * The working-directory variable: `$PWD`, or `${PWD}` written out. It is the
293
+ * directory the line starts in, which the shell fixes before the line runs,
294
+ * so a word carrying it is as literal as the same word with the path typed
295
+ * out. Only this exact spelling matches: `$PWDfoo` is another variable and
296
+ * `${PWD:-x}` decides its own value.
297
+ */
298
+ const WORKING_DIRECTORY_VARIABLE = /^(?:\$PWD(?![A-Za-z0-9_])|\$\{PWD\})/
299
+
300
+ /** The directory a `$PWD` word is read as, so that both spellings match. */
301
+ const WORKING_DIRECTORY_PATH = '/namzu-start-directory'
302
+
303
+ /**
304
+ * The line with every `$PWD`/`${PWD}` the shell would expand replaced by the
305
+ * literal start directory, so the rest of the check sees the spelling with the
306
+ * path typed out. A `$` inside single quotes is text the shell passes on, and
307
+ * an escaped `\$PWD` is likewise passed on, so neither is touched.
308
+ */
309
+ function spellOutWorkingDirectory(line: string): string {
310
+ let out = ''
311
+ let single = false
312
+ let double = false
313
+ for (let i = 0; i < line.length; i += 1) {
314
+ const c = line[i] as string
315
+ if (c === '\\' && !single) {
316
+ out += c
317
+ const escaped = line[i + 1]
318
+ if (escaped !== undefined) {
319
+ out += escaped
320
+ i += 1
321
+ }
322
+ continue
323
+ }
324
+ if (c === "'" && !double) {
325
+ single = !single
326
+ out += c
327
+ continue
328
+ }
329
+ if (c === '"' && !single) {
330
+ double = !double
331
+ out += c
332
+ continue
333
+ }
334
+ if (!single && c === '$') {
335
+ const match = WORKING_DIRECTORY_VARIABLE.exec(line.slice(i))
336
+ if (match !== null) {
337
+ out += WORKING_DIRECTORY_PATH
338
+ i += match[0].length - 1
339
+ continue
340
+ }
341
+ }
342
+ out += c
343
+ }
344
+ return out
345
+ }
346
+
283
347
  /** An unquoted `*`, `?` or `[` in a word's source text. */
284
348
  function hasUnquotedGlob(text: string): boolean {
285
349
  let single = false
@@ -850,10 +914,11 @@ export function isReadOnlyCommandLine(
850
914
  ): boolean {
851
915
  if (dialect === 'cmd') return false
852
916
  if (line.trim() === '' || line.length > MAX_LINE_LENGTH) return false
853
- if (hasDynamicSyntax(line)) return false
854
- if (looksLikeNetworkPath(line.trim())) return false
917
+ const spelled = spellOutWorkingDirectory(line)
918
+ if (hasDynamicSyntax(spelled)) return false
919
+ if (looksLikeNetworkPath(spelled.trim())) return false
855
920
 
856
- const lexed = lexShellCommandLine(line, { dialect })
921
+ const lexed = lexShellCommandLine(spelled, { dialect })
857
922
  if (!lexed.complete || lexed.opaque || lexed.commands.length === 0) return false
858
923
  if (lexed.compoundWords.length > 0) return false
859
924
  if (!redirectionsAreHarmless(lexed.redirections)) return false
@@ -890,6 +955,9 @@ export function isReadOnlyCommandLine(
890
955
  if (head.value === 'cd') changesDirectory = true
891
956
  }
892
957
  if (runsGit && changesDirectory) return false
958
+ // `$PWD` is read as the directory the line starts in; after a `cd` the shell
959
+ // holds another one, so the two spellings no longer name the same path.
960
+ if (spelled !== line && changesDirectory) return false
893
961
  // Git trusts the folder it starts in. The directory of the session carries
894
962
  // over between calls, so `cd vendor/cache` and `git status` are two lines
895
963
  // that are only dangerous together. A folder that looks like a bare
@@ -176,20 +176,49 @@ function derived(from: ShellWord, value: string): ShellWord {
176
176
  return { ...from, text: value, value }
177
177
  }
178
178
 
179
+ /** An operand of `cp` or `mv`, and whether it is the destination. */
180
+ interface CopyOperand {
181
+ readonly word: ShellWord
182
+ readonly destination: boolean
183
+ }
184
+
179
185
  /**
180
- * Operands of `cp` and `mv`. GNU lets a flag follow an operand, and the
181
- * destination can be a flag's value: `--target-directory=D`, `--target-directory D`,
182
- * `-tD`, `-t D`, also inside a cluster (`-at D`, `-rtD`). Reading the operands
183
- * alone would miss the attached and the trailing forms.
186
+ * Operands of `cp` and `mv`, each marked with whether it is the destination.
187
+ * GNU lets a flag follow an operand, and the destination can be a flag's value:
188
+ * `--target-directory=D`, `--target-directory D`, `-tD`, `-t D`, also inside a
189
+ * cluster (`-at D`, `-rtD`). Reading the operands alone would miss the attached
190
+ * and the trailing forms. A target directory makes every other operand a
191
+ * source; without one the last operand is the destination. The callers differ
192
+ * on what a source costs: `mv` removes it, `cp` only reads it.
184
193
  */
185
- function copyOperands(args: Args): ShellWord[] {
186
- const out: ShellWord[] = []
194
+ function copyOperands(args: Args): CopyOperand[] {
195
+ return readCopyArguments(args).operands
196
+ }
197
+
198
+ /** True when `name` is a spelling GNU accepts for `full`: `minimum` or more of its leading characters. */
199
+ function isLongOption(name: string, full: string, minimum: number): boolean {
200
+ return name.length >= minimum && full.startsWith(name)
201
+ }
202
+
203
+ /**
204
+ * The operands of `cp`, `mv` or `install`, and whether the flags make the
205
+ * "copy" a link to its source (`cp -l`, `--link`, `-s`, `--symbolic-link`,
206
+ * alone or in a cluster such as `-al`, `-rs`): a later edit through the link
207
+ * changes the source. GNU takes any unique prefix of a long option, so the
208
+ * prefixes below are the shortest that no other option of `cp`, `mv` or
209
+ * `install` shares: `--t` for `--target-directory`, `--su` for `--suffix`,
210
+ * `--l` for `--link`, `--sy` for `--symbolic-link`. `--reflink` is a copy.
211
+ */
212
+ function readCopyArguments(args: Args): { operands: CopyOperand[]; links: boolean } {
213
+ const out: CopyOperand[] = []
214
+ let links = false
215
+ let target: ShellWord | undefined
187
216
  let afterDashes = false
188
217
  for (let i = 0; i < args.length; i += 1) {
189
218
  const arg = args[i] as ShellWord
190
219
  const text = arg.value
191
220
  if (afterDashes || text === '-' || !text.startsWith('-')) {
192
- out.push(arg)
221
+ out.push({ word: arg, destination: false })
193
222
  continue
194
223
  }
195
224
  if (text === '--') {
@@ -199,30 +228,39 @@ function copyOperands(args: Args): ShellWord[] {
199
228
  if (text.startsWith('--')) {
200
229
  const eq = text.indexOf('=')
201
230
  const name = eq >= 0 ? text.slice(0, eq) : text
202
- if (name === '--target-directory') {
203
- if (eq >= 0) out.push(derived(arg, text.slice(eq + 1)))
231
+ if (isLongOption(name, '--target-directory', 3)) {
232
+ if (eq >= 0) target = derived(arg, text.slice(eq + 1))
204
233
  else if (args[i + 1] !== undefined) {
205
- out.push(args[i + 1] as ShellWord)
234
+ target = args[i + 1] as ShellWord
206
235
  i += 1
207
236
  }
208
- } else if (name === '--suffix' && eq < 0) i += 1
237
+ } else if (isLongOption(name, '--suffix', 4) && eq < 0) i += 1
238
+ else if (isLongOption(name, '--link', 3) || isLongOption(name, '--symbolic-link', 4))
239
+ links = true
209
240
  continue
210
241
  }
211
242
  for (let j = 1; j < text.length; j += 1) {
212
243
  const letter = text[j] as string
244
+ if (letter === 'l' || letter === 's') links = true
213
245
  if (letter !== 't' && letter !== 'S') continue
214
246
  const rest = text.slice(j + 1)
215
247
  if (letter === 't') {
216
- if (rest !== '') out.push(derived(arg, rest))
248
+ if (rest !== '') target = derived(arg, rest)
217
249
  else if (args[i + 1] !== undefined) {
218
- out.push(args[i + 1] as ShellWord)
250
+ target = args[i + 1] as ShellWord
219
251
  i += 1
220
252
  }
221
253
  } else if (rest === '') i += 1
222
254
  break
223
255
  }
224
256
  }
225
- return out
257
+ if (target !== undefined) {
258
+ out.push({ word: target, destination: true })
259
+ return { operands: out, links }
260
+ }
261
+ const last = out[out.length - 1]
262
+ if (last !== undefined) out[out.length - 1] = { word: last.word, destination: true }
263
+ return { operands: out, links }
226
264
  }
227
265
 
228
266
  /**
@@ -804,8 +842,6 @@ const EXTRACTORS: Readonly<Record<string, (args: Args) => ShellWord[]>> = {
804
842
  touch: touchOperands,
805
843
  rm: operands,
806
844
  rmdir: operands,
807
- mv: copyOperands,
808
- cp: copyOperands,
809
845
  cat: operands,
810
846
  head: operands,
811
847
  tail: operands,
@@ -1175,7 +1211,10 @@ function outputTargets(program: string, args: Args): ShellWord[] {
1175
1211
  case 'dd':
1176
1212
  return args.filter((a) => a.value.startsWith('of=')).map((a) => derived(a, a.value.slice(3)))
1177
1213
  case 'install':
1178
- return [...copyOperands(args), ...(hasShort(args, 'd') ? operands(args) : [])]
1214
+ return [
1215
+ ...copyOperands(args).map((operand) => operand.word),
1216
+ ...(hasShort(args, 'd') ? operands(args) : []),
1217
+ ]
1179
1218
  case 'rsync': {
1180
1219
  const rest = operandsSkippingValues(args, RSYNC_VALUED, 'eTBMfgp'.replace(/[gp]/g, ''))
1181
1220
  return rest.length > 1 ? [rest[rest.length - 1] as ShellWord] : []
@@ -1374,7 +1413,7 @@ function outputTargets(program: string, args: Args): ShellWord[] {
1374
1413
  }
1375
1414
 
1376
1415
  /** Commands that create or change what their operands name. `sed` is added per call when it edits in place. */
1377
- const WRITERS = new Set(['mkdir', 'touch', 'rm', 'rmdir', 'mv', 'cp', 'tee'])
1416
+ const WRITERS = new Set(['mkdir', 'touch', 'rm', 'rmdir', 'tee'])
1378
1417
 
1379
1418
  const CD_PROGRAMS = new Set(['cd', 'pushd', 'popd', 'chdir'])
1380
1419
 
@@ -1449,6 +1488,24 @@ export function shellPathAccess(reading: ShellReading): ShellPathAccess {
1449
1488
  ...(directory !== undefined ? { directory } : {}),
1450
1489
  })
1451
1490
  }
1491
+ if (program === 'mv' || program === 'cp') {
1492
+ // `mv` removes its sources, so each is a write; `cp` leaves its sources
1493
+ // alone, so only the destination is, unless it links to them (`-l`,
1494
+ // `-s`): an edit through the link changes the source, so a source is
1495
+ // a write then as well.
1496
+ const copy = readCopyArguments(args)
1497
+ const sourcesChange = program === 'mv' || copy.links
1498
+ for (const operand of copy.operands) {
1499
+ if (operand.word.value === '' || isHarmlessTarget(operand.word.value)) continue
1500
+ uses.push({
1501
+ path: operand.word.value,
1502
+ access: sourcesChange || operand.destination ? 'write' : 'read',
1503
+ via: program,
1504
+ unresolved: expandsAtRuntime(operand.word),
1505
+ ...(directory !== undefined ? { directory } : {}),
1506
+ })
1507
+ }
1508
+ }
1452
1509
  if (program === 'touch') {
1453
1510
  for (const arg of touchReferences(args)) {
1454
1511
  if (arg.value === '' || isHarmlessTarget(arg.value)) continue
@@ -80,6 +80,7 @@ export {
80
80
  COMPACTION_HEADER,
81
81
  COMPACTION_RECOVERY_NOTE,
82
82
  COMPACTION_RESUME_NOTE,
83
+ COMPACTION_RESUME_NOTE_MAY_ASK,
83
84
  buildCompactionMessage,
84
85
  collectRevealedTools,
85
86
  isCompactionMessage,
@@ -31,7 +31,13 @@ import {
31
31
  summarizeWithModel,
32
32
  } from './model-summary.js'
33
33
  import { planCompaction } from './plan.js'
34
- import { carriedWorkingCommands, recordRestorable, renderRestoredContext } from './restore.js'
34
+ import {
35
+ carriedWorkingCommands,
36
+ collectToolsUsed,
37
+ keptInstructionFiles,
38
+ recordRestorable,
39
+ renderRestoredContext,
40
+ } from './restore.js'
35
41
  import { findRetainedIndices } from './retention.js'
36
42
  import { buildCompactionMessage, collectRevealedTools, isCompactionMessage } from './summary.js'
37
43
  import { estimateMessagesTokens } from './token-estimate.js'
@@ -85,6 +91,14 @@ export interface CompactNowInput extends CompactionVerificationOptions {
85
91
  * model at the originals ({@link COMPACTION_RECOVERY_NOTE}). Absent adds none.
86
92
  */
87
93
  readonly recoveryNote?: string
94
+ /** The file the full conversation is stored in, when the host has one; the summary says where exact text can be found. */
95
+ readonly sessionFile?: string
96
+ /**
97
+ * Close the summary with the wording that lets the model ask when something
98
+ * is unclear, instead of telling it to continue directly. Absent or false,
99
+ * it continues directly. See {@link COMPACTION_RESUME_NOTE_MAY_ASK}.
100
+ */
101
+ readonly askWhenUnclear?: boolean
88
102
  /** Text the host wants carried across, such as where its plan file is; added to the restored context. */
89
103
  readonly restoreNote?: string
90
104
  /**
@@ -231,6 +245,8 @@ function splice(
231
245
  recentMessages: readonly Message[],
232
246
  recoveryNote?: string,
233
247
  cut: readonly Message[] = [],
248
+ sessionFile?: string,
249
+ askWhenUnclear?: boolean,
234
250
  ): { messages: Message[]; summary: Message } {
235
251
  // A host-triggered pass has no turn-scoped WorkingStateManager to carry the
236
252
  // summary into a later query. Pin this summary itself: until a future turn
@@ -239,7 +255,12 @@ function splice(
239
255
  // `cut` is every message the pass read: the tools it loaded are still loaded
240
256
  // once the messages that loaded them are gone.
241
257
  const summary = {
242
- ...buildCompactionMessage(summaryBody, recoveryNote, collectRevealedTools(cut)),
258
+ ...buildCompactionMessage(summaryBody, recoveryNote, collectRevealedTools(cut), {
259
+ keptTail: retainedOlder.length + recentMessages.length > 0,
260
+ toolsUsed: collectToolsUsed(cut),
261
+ ...(sessionFile ? { sessionFile } : {}),
262
+ ...(askWhenUnclear ? { askWhenUnclear } : {}),
263
+ }),
243
264
  retain: true,
244
265
  }
245
266
  return { messages: [...preservedSystem, summary, ...retainedOlder, ...recentMessages], summary }
@@ -251,6 +272,7 @@ function withRestoredContext(
251
272
  manager: WorkingStateManager,
252
273
  input: CompactNowInput,
253
274
  kept: readonly Message[],
275
+ cut: readonly Message[],
254
276
  ): string {
255
277
  const window = resolveContextWindow(
256
278
  input.contextWindowTokens ?? input.config.contextWindowTokens,
@@ -260,6 +282,8 @@ function withRestoredContext(
260
282
  contextWindowTokens: window.tokens,
261
283
  // Instructions already whole in what the pass keeps are not put back twice.
262
284
  wholeSkills: handledSkillNames(kept),
285
+ toolsUsed: collectToolsUsed(cut),
286
+ instructionFiles: keptInstructionFiles(kept),
263
287
  ...(input.restoreNote ? { hostNote: input.restoreNote } : {}),
264
288
  })
265
289
  return restored ? `${body}\n\n${restored}` : body
@@ -403,15 +427,19 @@ export async function compactNow(input: CompactNowInput): Promise<CompactionResu
403
427
 
404
428
  const { messages, summary } = splice(
405
429
  preservedSystem,
406
- withRestoredContext(body, manager, input, [
407
- ...preservedSystem,
408
- ...retainedOlder,
409
- ...plan.recentMessages,
410
- ]),
430
+ withRestoredContext(
431
+ body,
432
+ manager,
433
+ input,
434
+ [...preservedSystem, ...retainedOlder, ...plan.recentMessages],
435
+ input.messages,
436
+ ),
411
437
  retainedOlder,
412
438
  plan.recentMessages,
413
439
  input.recoveryNote,
414
440
  input.messages,
441
+ input.sessionFile,
442
+ input.askWhenUnclear,
415
443
  )
416
444
  const published = await publishManualCompaction(input, {
417
445
  messages,
@@ -523,13 +551,21 @@ export async function compactRegion(input: CompactRegionInput): Promise<Compacti
523
551
  // replaced outside a turn, so its only surviving account is pinned.
524
552
  const summary = {
525
553
  ...buildCompactionMessage(
526
- withRestoredContext(body, manager, input, [
527
- ...messages.slice(0, start),
528
- ...retainedSelected,
529
- ...messages.slice(end),
530
- ]),
554
+ withRestoredContext(
555
+ body,
556
+ manager,
557
+ input,
558
+ [...messages.slice(0, start), ...retainedSelected, ...messages.slice(end)],
559
+ messages,
560
+ ),
531
561
  input.recoveryNote,
532
562
  collectRevealedTools(messages),
563
+ {
564
+ keptTail: retainedSelected.length + (messages.length - end) > 0,
565
+ toolsUsed: collectToolsUsed(messages),
566
+ ...(input.sessionFile ? { sessionFile: input.sessionFile } : {}),
567
+ ...(input.askWhenUnclear ? { askWhenUnclear: true } : {}),
568
+ },
533
569
  ),
534
570
  retain: true,
535
571
  }