@namzu/sdk 65.3.0 → 68.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 (638) hide show
  1. package/CHANGELOG.md +167 -0
  2. package/dist/account/lock.d.ts +4 -12
  3. package/dist/account/lock.d.ts.map +1 -1
  4. package/dist/account/lock.js +5 -140
  5. package/dist/account/lock.js.map +1 -1
  6. package/dist/account/registry.d.ts.map +1 -1
  7. package/dist/account/registry.js +50 -6
  8. package/dist/account/registry.js.map +1 -1
  9. package/dist/account/types.d.ts +7 -5
  10. package/dist/account/types.d.ts.map +1 -1
  11. package/dist/account/types.js +6 -7
  12. package/dist/account/types.js.map +1 -1
  13. package/dist/authorization/dangerous-commands.d.ts +64 -0
  14. package/dist/authorization/dangerous-commands.d.ts.map +1 -0
  15. package/dist/authorization/dangerous-commands.js +479 -0
  16. package/dist/authorization/dangerous-commands.js.map +1 -0
  17. package/dist/authorization/gate.d.ts.map +1 -1
  18. package/dist/authorization/gate.js +19 -5
  19. package/dist/authorization/gate.js.map +1 -1
  20. package/dist/authorization/protected-paths.d.ts +11 -6
  21. package/dist/authorization/protected-paths.d.ts.map +1 -1
  22. package/dist/authorization/protected-paths.js +13 -8
  23. package/dist/authorization/protected-paths.js.map +1 -1
  24. package/dist/authorization/readonly-commands.d.ts +6 -1
  25. package/dist/authorization/readonly-commands.d.ts.map +1 -1
  26. package/dist/authorization/readonly-commands.js +35 -1
  27. package/dist/authorization/readonly-commands.js.map +1 -1
  28. package/dist/authorization/removal.d.ts +10 -0
  29. package/dist/authorization/removal.d.ts.map +1 -1
  30. package/dist/authorization/removal.js +7 -7
  31. package/dist/authorization/removal.js.map +1 -1
  32. package/dist/authorization/rules.d.ts +10 -0
  33. package/dist/authorization/rules.d.ts.map +1 -1
  34. package/dist/authorization/rules.js +60 -14
  35. package/dist/authorization/rules.js.map +1 -1
  36. package/dist/authorization/shell-lexer.d.ts +10 -0
  37. package/dist/authorization/shell-lexer.d.ts.map +1 -1
  38. package/dist/authorization/shell-lexer.js +15 -0
  39. package/dist/authorization/shell-lexer.js.map +1 -1
  40. package/dist/bridge/acp/permission.d.ts +5 -0
  41. package/dist/bridge/acp/permission.d.ts.map +1 -1
  42. package/dist/bridge/acp/permission.js.map +1 -1
  43. package/dist/bridge/acp/server.d.ts +8 -1
  44. package/dist/bridge/acp/server.d.ts.map +1 -1
  45. package/dist/bridge/acp/server.js +48 -6
  46. package/dist/bridge/acp/server.js.map +1 -1
  47. package/dist/compaction/context-breakdown.d.ts +25 -6
  48. package/dist/compaction/context-breakdown.d.ts.map +1 -1
  49. package/dist/compaction/context-breakdown.js +27 -7
  50. package/dist/compaction/context-breakdown.js.map +1 -1
  51. package/dist/compaction/dangling.d.ts.map +1 -1
  52. package/dist/compaction/dangling.js +2 -0
  53. package/dist/compaction/dangling.js.map +1 -1
  54. package/dist/compaction/extractor.d.ts +7 -1
  55. package/dist/compaction/extractor.d.ts.map +1 -1
  56. package/dist/compaction/extractor.js +8 -1
  57. package/dist/compaction/extractor.js.map +1 -1
  58. package/dist/compaction/index.d.ts +1 -1
  59. package/dist/compaction/index.d.ts.map +1 -1
  60. package/dist/compaction/index.js +1 -1
  61. package/dist/compaction/index.js.map +1 -1
  62. package/dist/compaction/managers/structured.js +2 -2
  63. package/dist/compaction/managers/structured.js.map +1 -1
  64. package/dist/compaction/manual.d.ts.map +1 -1
  65. package/dist/compaction/manual.js +11 -6
  66. package/dist/compaction/manual.js.map +1 -1
  67. package/dist/compaction/model-summary.d.ts.map +1 -1
  68. package/dist/compaction/model-summary.js +7 -1
  69. package/dist/compaction/model-summary.js.map +1 -1
  70. package/dist/compaction/summary.d.ts +13 -2
  71. package/dist/compaction/summary.d.ts.map +1 -1
  72. package/dist/compaction/summary.js +46 -3
  73. package/dist/compaction/summary.js.map +1 -1
  74. package/dist/constants/tools/index.d.ts +18 -1
  75. package/dist/constants/tools/index.d.ts.map +1 -1
  76. package/dist/constants/tools/index.js +28 -16
  77. package/dist/constants/tools/index.js.map +1 -1
  78. package/dist/manager/session/turn-recorder.d.ts +6 -0
  79. package/dist/manager/session/turn-recorder.d.ts.map +1 -1
  80. package/dist/manager/session/turn-recorder.js +29 -1
  81. package/dist/manager/session/turn-recorder.js.map +1 -1
  82. package/dist/network/audit.d.ts +49 -0
  83. package/dist/network/audit.d.ts.map +1 -0
  84. package/dist/network/audit.js +84 -0
  85. package/dist/network/audit.js.map +1 -0
  86. package/dist/network/clock.d.ts +21 -0
  87. package/dist/network/clock.d.ts.map +1 -0
  88. package/dist/network/clock.js +51 -0
  89. package/dist/network/clock.js.map +1 -0
  90. package/dist/network/discovery-mdns.d.ts +120 -0
  91. package/dist/network/discovery-mdns.d.ts.map +1 -0
  92. package/dist/network/discovery-mdns.js +442 -0
  93. package/dist/network/discovery-mdns.js.map +1 -0
  94. package/dist/network/discovery.d.ts +63 -0
  95. package/dist/network/discovery.d.ts.map +1 -0
  96. package/dist/network/discovery.js +163 -0
  97. package/dist/network/discovery.js.map +1 -0
  98. package/dist/network/fingerprint.d.ts +73 -0
  99. package/dist/network/fingerprint.d.ts.map +1 -0
  100. package/dist/network/fingerprint.js +208 -0
  101. package/dist/network/fingerprint.js.map +1 -0
  102. package/dist/network/identity.d.ts +38 -0
  103. package/dist/network/identity.d.ts.map +1 -0
  104. package/dist/network/identity.js +71 -0
  105. package/dist/network/identity.js.map +1 -0
  106. package/dist/network/index.d.ts +39 -0
  107. package/dist/network/index.d.ts.map +1 -0
  108. package/dist/network/index.js +25 -0
  109. package/dist/network/index.js.map +1 -0
  110. package/dist/network/join.d.ts +69 -0
  111. package/dist/network/join.d.ts.map +1 -0
  112. package/dist/network/join.js +136 -0
  113. package/dist/network/join.js.map +1 -0
  114. package/dist/network/mail-policy.d.ts +97 -0
  115. package/dist/network/mail-policy.d.ts.map +1 -0
  116. package/dist/network/mail-policy.js +147 -0
  117. package/dist/network/mail-policy.js.map +1 -0
  118. package/dist/network/membership.d.ts +76 -0
  119. package/dist/network/membership.d.ts.map +1 -0
  120. package/dist/network/membership.js +225 -0
  121. package/dist/network/membership.js.map +1 -0
  122. package/dist/network/roster.d.ts +59 -0
  123. package/dist/network/roster.d.ts.map +1 -0
  124. package/dist/network/roster.js +120 -0
  125. package/dist/network/roster.js.map +1 -0
  126. package/dist/network/service.d.ts +303 -0
  127. package/dist/network/service.d.ts.map +1 -0
  128. package/dist/network/service.js +1891 -0
  129. package/dist/network/service.js.map +1 -0
  130. package/dist/network/store.d.ts +141 -0
  131. package/dist/network/store.d.ts.map +1 -0
  132. package/dist/network/store.js +198 -0
  133. package/dist/network/store.js.map +1 -0
  134. package/dist/network/transport.d.ts +96 -0
  135. package/dist/network/transport.d.ts.map +1 -0
  136. package/dist/network/transport.js +342 -0
  137. package/dist/network/transport.js.map +1 -0
  138. package/dist/network/wire.d.ts +345 -0
  139. package/dist/network/wire.d.ts.map +1 -0
  140. package/dist/network/wire.js +212 -0
  141. package/dist/network/wire.js.map +1 -0
  142. package/dist/network/x509.d.ts +28 -0
  143. package/dist/network/x509.d.ts.map +1 -0
  144. package/dist/network/x509.js +84 -0
  145. package/dist/network/x509.js.map +1 -0
  146. package/dist/pals/activity/causality.d.ts.map +1 -1
  147. package/dist/pals/activity/causality.js +14 -2
  148. package/dist/pals/activity/causality.js.map +1 -1
  149. package/dist/peers/endpoint.d.ts.map +1 -1
  150. package/dist/peers/endpoint.js +6 -0
  151. package/dist/peers/endpoint.js.map +1 -1
  152. package/dist/peers/envelope.d.ts +18 -0
  153. package/dist/peers/envelope.d.ts.map +1 -1
  154. package/dist/peers/envelope.js +68 -5
  155. package/dist/peers/envelope.js.map +1 -1
  156. package/dist/peers/index.d.ts +5 -4
  157. package/dist/peers/index.d.ts.map +1 -1
  158. package/dist/peers/index.js +3 -2
  159. package/dist/peers/index.js.map +1 -1
  160. package/dist/peers/mode-word.d.ts +10 -0
  161. package/dist/peers/mode-word.d.ts.map +1 -0
  162. package/dist/peers/mode-word.js +19 -0
  163. package/dist/peers/mode-word.js.map +1 -0
  164. package/dist/peers/protocol.d.ts +42 -0
  165. package/dist/peers/protocol.d.ts.map +1 -1
  166. package/dist/peers/protocol.js +22 -1
  167. package/dist/peers/protocol.js.map +1 -1
  168. package/dist/peers/record.d.ts +8 -0
  169. package/dist/peers/record.d.ts.map +1 -1
  170. package/dist/peers/record.js +5 -0
  171. package/dist/peers/record.js.map +1 -1
  172. package/dist/plugin/session-hook-env.d.ts.map +1 -1
  173. package/dist/plugin/session-hook-env.js +22 -2
  174. package/dist/plugin/session-hook-env.js.map +1 -1
  175. package/dist/plugin/shell-hook.d.ts +16 -2
  176. package/dist/plugin/shell-hook.d.ts.map +1 -1
  177. package/dist/plugin/shell-hook.js +183 -14
  178. package/dist/plugin/shell-hook.js.map +1 -1
  179. package/dist/pricing/catalogue.generated.d.ts.map +1 -1
  180. package/dist/pricing/catalogue.generated.js +6 -0
  181. package/dist/pricing/catalogue.generated.js.map +1 -1
  182. package/dist/prompt/coding-agent-doctrine.d.ts +4 -2
  183. package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
  184. package/dist/prompt/coding-agent-doctrine.js +33 -38
  185. package/dist/prompt/coding-agent-doctrine.js.map +1 -1
  186. package/dist/provider/fallback.d.ts.map +1 -1
  187. package/dist/provider/fallback.js +12 -0
  188. package/dist/provider/fallback.js.map +1 -1
  189. package/dist/provider/idle-timeout.d.ts.map +1 -1
  190. package/dist/provider/idle-timeout.js +5 -0
  191. package/dist/provider/idle-timeout.js.map +1 -1
  192. package/dist/provider/model-window-suffix.d.ts.map +1 -1
  193. package/dist/provider/model-window-suffix.js +1 -0
  194. package/dist/provider/model-window-suffix.js.map +1 -1
  195. package/dist/provider/retry.d.ts.map +1 -1
  196. package/dist/provider/retry.js +5 -0
  197. package/dist/provider/retry.js.map +1 -1
  198. package/dist/public-runtime.d.ts +16 -8
  199. package/dist/public-runtime.d.ts.map +1 -1
  200. package/dist/public-runtime.js +24 -7
  201. package/dist/public-runtime.js.map +1 -1
  202. package/dist/public-tools.d.ts +3 -2
  203. package/dist/public-tools.d.ts.map +1 -1
  204. package/dist/public-tools.js +2 -2
  205. package/dist/public-tools.js.map +1 -1
  206. package/dist/public-types.d.ts +10 -5
  207. package/dist/public-types.d.ts.map +1 -1
  208. package/dist/registry/command/kernel-commands.d.ts +11 -1
  209. package/dist/registry/command/kernel-commands.d.ts.map +1 -1
  210. package/dist/registry/command/kernel-commands.js +114 -46
  211. package/dist/registry/command/kernel-commands.js.map +1 -1
  212. package/dist/runtime/jobs/registry.d.ts +29 -1
  213. package/dist/runtime/jobs/registry.d.ts.map +1 -1
  214. package/dist/runtime/jobs/registry.js +64 -0
  215. package/dist/runtime/jobs/registry.js.map +1 -1
  216. package/dist/runtime/query/auto-review/classifier.d.ts +30 -6
  217. package/dist/runtime/query/auto-review/classifier.d.ts.map +1 -1
  218. package/dist/runtime/query/auto-review/classifier.js +40 -12
  219. package/dist/runtime/query/auto-review/classifier.js.map +1 -1
  220. package/dist/runtime/query/auto-review/feedback.d.ts +37 -0
  221. package/dist/runtime/query/auto-review/feedback.d.ts.map +1 -0
  222. package/dist/runtime/query/auto-review/feedback.js +45 -0
  223. package/dist/runtime/query/auto-review/feedback.js.map +1 -0
  224. package/dist/runtime/query/auto-review/git-status.d.ts +59 -0
  225. package/dist/runtime/query/auto-review/git-status.d.ts.map +1 -0
  226. package/dist/runtime/query/auto-review/git-status.js +379 -0
  227. package/dist/runtime/query/auto-review/git-status.js.map +1 -0
  228. package/dist/runtime/query/auto-review/index.d.ts +3 -3
  229. package/dist/runtime/query/auto-review/index.d.ts.map +1 -1
  230. package/dist/runtime/query/auto-review/index.js +2 -2
  231. package/dist/runtime/query/auto-review/index.js.map +1 -1
  232. package/dist/runtime/query/auto-review/prompt.d.ts.map +1 -1
  233. package/dist/runtime/query/auto-review/prompt.js +6 -1
  234. package/dist/runtime/query/auto-review/prompt.js.map +1 -1
  235. package/dist/runtime/query/auto-review/transcript.d.ts +14 -1
  236. package/dist/runtime/query/auto-review/transcript.d.ts.map +1 -1
  237. package/dist/runtime/query/auto-review/transcript.js +48 -2
  238. package/dist/runtime/query/auto-review/transcript.js.map +1 -1
  239. package/dist/runtime/query/executor/tool-call-admission.d.ts.map +1 -1
  240. package/dist/runtime/query/executor/tool-call-admission.js +40 -3
  241. package/dist/runtime/query/executor/tool-call-admission.js.map +1 -1
  242. package/dist/runtime/query/executor.d.ts +32 -10
  243. package/dist/runtime/query/executor.d.ts.map +1 -1
  244. package/dist/runtime/query/executor.js +78 -17
  245. package/dist/runtime/query/executor.js.map +1 -1
  246. package/dist/runtime/query/index.d.ts +9 -0
  247. package/dist/runtime/query/index.d.ts.map +1 -1
  248. package/dist/runtime/query/index.js +54 -17
  249. package/dist/runtime/query/index.js.map +1 -1
  250. package/dist/runtime/query/iteration/index.d.ts +1 -1
  251. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  252. package/dist/runtime/query/iteration/index.js +20 -6
  253. package/dist/runtime/query/iteration/index.js.map +1 -1
  254. package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
  255. package/dist/runtime/query/iteration/phases/compaction.js +5 -2
  256. package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
  257. package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
  258. package/dist/runtime/query/iteration/phases/context.js +5 -0
  259. package/dist/runtime/query/iteration/phases/context.js.map +1 -1
  260. package/dist/runtime/query/iteration/phases/per-call-review.d.ts +1 -2
  261. package/dist/runtime/query/iteration/phases/per-call-review.d.ts.map +1 -1
  262. package/dist/runtime/query/iteration/phases/per-call-review.js +2 -5
  263. package/dist/runtime/query/iteration/phases/per-call-review.js.map +1 -1
  264. package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
  265. package/dist/runtime/query/iteration/phases/tool-review.js +6 -1
  266. package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
  267. package/dist/runtime/query/pasted-frames.d.ts +34 -0
  268. package/dist/runtime/query/pasted-frames.d.ts.map +1 -0
  269. package/dist/runtime/query/pasted-frames.js +108 -0
  270. package/dist/runtime/query/pasted-frames.js.map +1 -0
  271. package/dist/runtime/query/path-escalation.d.ts +6 -0
  272. package/dist/runtime/query/path-escalation.d.ts.map +1 -1
  273. package/dist/runtime/query/path-escalation.js +15 -3
  274. package/dist/runtime/query/path-escalation.js.map +1 -1
  275. package/dist/runtime/query/request-interrupted-turns.d.ts +20 -0
  276. package/dist/runtime/query/request-interrupted-turns.d.ts.map +1 -0
  277. package/dist/runtime/query/request-interrupted-turns.js +43 -0
  278. package/dist/runtime/query/request-interrupted-turns.js.map +1 -0
  279. package/dist/runtime/query/review-policy.d.ts +16 -6
  280. package/dist/runtime/query/review-policy.d.ts.map +1 -1
  281. package/dist/runtime/query/review-policy.js +111 -15
  282. package/dist/runtime/query/review-policy.js.map +1 -1
  283. package/dist/runtime/query/turn/outcome-ledger.d.ts +9 -0
  284. package/dist/runtime/query/turn/outcome-ledger.d.ts.map +1 -1
  285. package/dist/runtime/query/turn/outcome-ledger.js +94 -6
  286. package/dist/runtime/query/turn/outcome-ledger.js.map +1 -1
  287. package/dist/runtime/query/unanswered.d.ts +19 -0
  288. package/dist/runtime/query/unanswered.d.ts.map +1 -0
  289. package/dist/runtime/query/unanswered.js +25 -0
  290. package/dist/runtime/query/unanswered.js.map +1 -0
  291. package/dist/runtime/system-events.d.ts +4 -4
  292. package/dist/runtime/system-events.d.ts.map +1 -1
  293. package/dist/runtime/system-events.js +2 -0
  294. package/dist/runtime/system-events.js.map +1 -1
  295. package/dist/session/workspace/git-worktree.d.ts +8 -0
  296. package/dist/session/workspace/git-worktree.d.ts.map +1 -1
  297. package/dist/session/workspace/git-worktree.js +21 -3
  298. package/dist/session/workspace/git-worktree.js.map +1 -1
  299. package/dist/store/evidence/source-kind.d.ts +1 -1
  300. package/dist/store/evidence/source-kind.d.ts.map +1 -1
  301. package/dist/store/evidence/source-kind.js +1 -1
  302. package/dist/store/evidence/source-kind.js.map +1 -1
  303. package/dist/store/goal/index.d.ts +53 -3
  304. package/dist/store/goal/index.d.ts.map +1 -1
  305. package/dist/store/goal/index.js +245 -9
  306. package/dist/store/goal/index.js.map +1 -1
  307. package/dist/store/session-index/sqlite.d.ts.map +1 -1
  308. package/dist/store/session-index/sqlite.js +42 -3
  309. package/dist/store/session-index/sqlite.js.map +1 -1
  310. package/dist/store/session-log/disk.js +1 -1
  311. package/dist/store/session-log/disk.js.map +1 -1
  312. package/dist/store/session-log/lease.d.ts.map +1 -1
  313. package/dist/store/session-log/lease.js +3 -2
  314. package/dist/store/session-log/lease.js.map +1 -1
  315. package/dist/tools/admit-facts.d.ts.map +1 -1
  316. package/dist/tools/admit-facts.js +2 -0
  317. package/dist/tools/admit-facts.js.map +1 -1
  318. package/dist/tools/builtins/bash.d.ts +10 -2
  319. package/dist/tools/builtins/bash.d.ts.map +1 -1
  320. package/dist/tools/builtins/bash.js +56 -17
  321. package/dist/tools/builtins/bash.js.map +1 -1
  322. package/dist/tools/builtins/browser-local.d.ts +117 -0
  323. package/dist/tools/builtins/browser-local.d.ts.map +1 -0
  324. package/dist/tools/builtins/browser-local.js +437 -0
  325. package/dist/tools/builtins/browser-local.js.map +1 -0
  326. package/dist/tools/builtins/browser.d.ts +18 -1
  327. package/dist/tools/builtins/browser.d.ts.map +1 -1
  328. package/dist/tools/builtins/browser.js +84 -6
  329. package/dist/tools/builtins/browser.js.map +1 -1
  330. package/dist/tools/builtins/computer-use-window-id.d.ts +16 -0
  331. package/dist/tools/builtins/computer-use-window-id.d.ts.map +1 -0
  332. package/dist/tools/builtins/computer-use-window-id.js +52 -0
  333. package/dist/tools/builtins/computer-use-window-id.js.map +1 -0
  334. package/dist/tools/builtins/computer-use.d.ts +65 -7
  335. package/dist/tools/builtins/computer-use.d.ts.map +1 -1
  336. package/dist/tools/builtins/computer-use.js +384 -71
  337. package/dist/tools/builtins/computer-use.js.map +1 -1
  338. package/dist/tools/builtins/content-fingerprint.d.ts +6 -0
  339. package/dist/tools/builtins/content-fingerprint.d.ts.map +1 -1
  340. package/dist/tools/builtins/content-fingerprint.js +7 -1
  341. package/dist/tools/builtins/content-fingerprint.js.map +1 -1
  342. package/dist/tools/builtins/edit.d.ts.map +1 -1
  343. package/dist/tools/builtins/edit.js +89 -7
  344. package/dist/tools/builtins/edit.js.map +1 -1
  345. package/dist/tools/builtins/file-write-result.d.ts.map +1 -1
  346. package/dist/tools/builtins/file-write-result.js +2 -1
  347. package/dist/tools/builtins/file-write-result.js.map +1 -1
  348. package/dist/tools/builtins/grep.js +1 -1
  349. package/dist/tools/builtins/grep.js.map +1 -1
  350. package/dist/tools/builtins/job.d.ts.map +1 -1
  351. package/dist/tools/builtins/job.js +4 -2
  352. package/dist/tools/builtins/job.js.map +1 -1
  353. package/dist/tools/builtins/monitor.d.ts.map +1 -1
  354. package/dist/tools/builtins/monitor.js +6 -1
  355. package/dist/tools/builtins/monitor.js.map +1 -1
  356. package/dist/tools/builtins/search-tools.d.ts +12 -1
  357. package/dist/tools/builtins/search-tools.d.ts.map +1 -1
  358. package/dist/tools/builtins/search-tools.js +166 -20
  359. package/dist/tools/builtins/search-tools.js.map +1 -1
  360. package/dist/tools/builtins/shell-read-credit.d.ts +18 -6
  361. package/dist/tools/builtins/shell-read-credit.d.ts.map +1 -1
  362. package/dist/tools/builtins/shell-read-credit.js +95 -27
  363. package/dist/tools/builtins/shell-read-credit.js.map +1 -1
  364. package/dist/tools/builtins/wait-for-job.d.ts.map +1 -1
  365. package/dist/tools/builtins/wait-for-job.js +14 -3
  366. package/dist/tools/builtins/wait-for-job.js.map +1 -1
  367. package/dist/tools/builtins/web-guidance.d.ts +6 -0
  368. package/dist/tools/builtins/web-guidance.d.ts.map +1 -1
  369. package/dist/tools/builtins/web-guidance.js +6 -0
  370. package/dist/tools/builtins/web-guidance.js.map +1 -1
  371. package/dist/tools/builtins/web.d.ts.map +1 -1
  372. package/dist/tools/builtins/web.js +2 -1
  373. package/dist/tools/builtins/web.js.map +1 -1
  374. package/dist/tools/builtins/write-file.d.ts.map +1 -1
  375. package/dist/tools/builtins/write-file.js +32 -2
  376. package/dist/tools/builtins/write-file.js.map +1 -1
  377. package/dist/tools/command-shell.d.ts +1 -1
  378. package/dist/tools/command-shell.d.ts.map +1 -1
  379. package/dist/tools/command-shell.js +6 -8
  380. package/dist/tools/command-shell.js.map +1 -1
  381. package/dist/tools/defineTool.d.ts +2 -0
  382. package/dist/tools/defineTool.d.ts.map +1 -1
  383. package/dist/tools/defineTool.js +17 -1
  384. package/dist/tools/defineTool.js.map +1 -1
  385. package/dist/tools/goal/confirmation.d.ts +82 -1
  386. package/dist/tools/goal/confirmation.d.ts.map +1 -1
  387. package/dist/tools/goal/confirmation.js +153 -3
  388. package/dist/tools/goal/confirmation.js.map +1 -1
  389. package/dist/tools/goal/index.d.ts.map +1 -1
  390. package/dist/tools/goal/index.js +34 -3
  391. package/dist/tools/goal/index.js.map +1 -1
  392. package/dist/tools/schedules/index.d.ts +1 -0
  393. package/dist/tools/schedules/index.d.ts.map +1 -1
  394. package/dist/tools/schedules/index.js +1 -0
  395. package/dist/tools/schedules/index.js.map +1 -1
  396. package/dist/tools/schedules/loop-tool.d.ts.map +1 -1
  397. package/dist/tools/schedules/loop-tool.js +8 -6
  398. package/dist/tools/schedules/loop-tool.js.map +1 -1
  399. package/dist/tools/schedules/present.js +1 -1
  400. package/dist/tools/schedules/present.js.map +1 -1
  401. package/dist/tools/schedules/timer-tools.d.ts +36 -0
  402. package/dist/tools/schedules/timer-tools.d.ts.map +1 -0
  403. package/dist/tools/schedules/timer-tools.js +234 -0
  404. package/dist/tools/schedules/timer-tools.js.map +1 -0
  405. package/dist/tools/schedules/types.d.ts +68 -3
  406. package/dist/tools/schedules/types.d.ts.map +1 -1
  407. package/dist/tools/task/create.d.ts.map +1 -1
  408. package/dist/tools/task/create.js +2 -0
  409. package/dist/tools/task/create.js.map +1 -1
  410. package/dist/toolsets/manager.d.ts +49 -7
  411. package/dist/toolsets/manager.d.ts.map +1 -1
  412. package/dist/toolsets/manager.js +239 -40
  413. package/dist/toolsets/manager.js.map +1 -1
  414. package/dist/turn/evidence-query.d.ts +18 -1
  415. package/dist/turn/evidence-query.d.ts.map +1 -1
  416. package/dist/turn/evidence-query.js +177 -35
  417. package/dist/turn/evidence-query.js.map +1 -1
  418. package/dist/turn/evidence-recall.d.ts +8 -1
  419. package/dist/turn/evidence-recall.d.ts.map +1 -1
  420. package/dist/turn/evidence-recall.js +82 -22
  421. package/dist/turn/evidence-recall.js.map +1 -1
  422. package/dist/turn/history-shed.d.ts +15 -0
  423. package/dist/turn/history-shed.d.ts.map +1 -0
  424. package/dist/turn/history-shed.js +23 -0
  425. package/dist/turn/history-shed.js.map +1 -0
  426. package/dist/types/acp/index.d.ts +26 -0
  427. package/dist/types/acp/index.d.ts.map +1 -1
  428. package/dist/types/authorization/index.d.ts +9 -1
  429. package/dist/types/authorization/index.d.ts.map +1 -1
  430. package/dist/types/authorization/index.js +11 -2
  431. package/dist/types/authorization/index.js.map +1 -1
  432. package/dist/types/command/index.d.ts +26 -2
  433. package/dist/types/command/index.d.ts.map +1 -1
  434. package/dist/types/command/index.js +5 -2
  435. package/dist/types/command/index.js.map +1 -1
  436. package/dist/types/common/index.d.ts +4 -2
  437. package/dist/types/common/index.d.ts.map +1 -1
  438. package/dist/types/common/index.js.map +1 -1
  439. package/dist/types/goal/index.d.ts +59 -0
  440. package/dist/types/goal/index.d.ts.map +1 -1
  441. package/dist/types/hitl/index.d.ts +10 -0
  442. package/dist/types/hitl/index.d.ts.map +1 -1
  443. package/dist/types/hitl/index.js.map +1 -1
  444. package/dist/types/message/index.d.ts +56 -1
  445. package/dist/types/message/index.d.ts.map +1 -1
  446. package/dist/types/message/index.js +2 -1
  447. package/dist/types/message/index.js.map +1 -1
  448. package/dist/types/permission/index.d.ts +14 -0
  449. package/dist/types/permission/index.d.ts.map +1 -1
  450. package/dist/types/permission/index.js +14 -0
  451. package/dist/types/permission/index.js.map +1 -1
  452. package/dist/types/plugin/index.d.ts +148 -1
  453. package/dist/types/plugin/index.d.ts.map +1 -1
  454. package/dist/types/plugin/index.js +9 -0
  455. package/dist/types/plugin/index.js.map +1 -1
  456. package/dist/types/provider/chat.d.ts +10 -5
  457. package/dist/types/provider/chat.d.ts.map +1 -1
  458. package/dist/types/provider/index.d.ts +1 -0
  459. package/dist/types/provider/index.d.ts.map +1 -1
  460. package/dist/types/provider/interface.d.ts +19 -0
  461. package/dist/types/provider/interface.d.ts.map +1 -1
  462. package/dist/types/provider/limits.d.ts +33 -0
  463. package/dist/types/provider/limits.d.ts.map +1 -0
  464. package/dist/types/provider/limits.js +7 -0
  465. package/dist/types/provider/limits.js.map +1 -0
  466. package/dist/types/provider/model.d.ts +5 -0
  467. package/dist/types/provider/model.d.ts.map +1 -1
  468. package/dist/types/session/config.d.ts +2 -2
  469. package/dist/types/session/outcome.d.ts +5 -2
  470. package/dist/types/session/outcome.d.ts.map +1 -1
  471. package/dist/types/session/outcome.js.map +1 -1
  472. package/dist/types/tool/index.d.ts +30 -0
  473. package/dist/types/tool/index.d.ts.map +1 -1
  474. package/dist/types/tool/index.js.map +1 -1
  475. package/dist/types/tool/presentation.d.ts +4 -0
  476. package/dist/types/tool/presentation.d.ts.map +1 -1
  477. package/dist/utils/cost.d.ts +1 -1
  478. package/dist/utils/cost.d.ts.map +1 -1
  479. package/dist/utils/cost.js +14 -5
  480. package/dist/utils/cost.js.map +1 -1
  481. package/dist/utils/file-lock.d.ts +88 -0
  482. package/dist/utils/file-lock.d.ts.map +1 -0
  483. package/dist/utils/file-lock.js +281 -0
  484. package/dist/utils/file-lock.js.map +1 -0
  485. package/dist/utils/state-backup.d.ts +87 -0
  486. package/dist/utils/state-backup.d.ts.map +1 -0
  487. package/dist/utils/state-backup.js +272 -0
  488. package/dist/utils/state-backup.js.map +1 -0
  489. package/dist/utils/temp-sweep.d.ts +66 -0
  490. package/dist/utils/temp-sweep.d.ts.map +1 -0
  491. package/dist/utils/temp-sweep.js +311 -0
  492. package/dist/utils/temp-sweep.js.map +1 -0
  493. package/dist/utils/temp.d.ts +14 -0
  494. package/dist/utils/temp.d.ts.map +1 -1
  495. package/dist/utils/temp.js +70 -2
  496. package/dist/utils/temp.js.map +1 -1
  497. package/package.json +1 -1
  498. package/src/account/lock.ts +13 -133
  499. package/src/account/registry.ts +75 -21
  500. package/src/account/types.ts +8 -9
  501. package/src/authorization/dangerous-commands.ts +542 -0
  502. package/src/authorization/gate.ts +19 -5
  503. package/src/authorization/protected-paths.ts +13 -8
  504. package/src/authorization/readonly-commands.ts +35 -1
  505. package/src/authorization/removal.ts +7 -7
  506. package/src/authorization/rules.ts +75 -15
  507. package/src/authorization/shell-lexer.ts +19 -0
  508. package/src/bridge/acp/permission.ts +2 -0
  509. package/src/bridge/acp/server.ts +66 -6
  510. package/src/compaction/context-breakdown.ts +53 -11
  511. package/src/compaction/dangling.ts +2 -0
  512. package/src/compaction/extractor.ts +9 -1
  513. package/src/compaction/index.ts +1 -0
  514. package/src/compaction/managers/structured.ts +2 -2
  515. package/src/compaction/manual.ts +11 -3
  516. package/src/compaction/model-summary.ts +8 -1
  517. package/src/compaction/summary.ts +54 -7
  518. package/src/constants/tools/index.ts +30 -16
  519. package/src/manager/session/turn-recorder.ts +37 -1
  520. package/src/network/audit.ts +113 -0
  521. package/src/network/clock.ts +62 -0
  522. package/src/network/discovery-mdns.ts +485 -0
  523. package/src/network/discovery.ts +210 -0
  524. package/src/network/fingerprint.ts +241 -0
  525. package/src/network/identity.ts +98 -0
  526. package/src/network/index.ts +134 -0
  527. package/src/network/join.ts +177 -0
  528. package/src/network/mail-policy.ts +199 -0
  529. package/src/network/membership.ts +290 -0
  530. package/src/network/roster.ts +165 -0
  531. package/src/network/service.ts +2304 -0
  532. package/src/network/store.ts +261 -0
  533. package/src/network/transport.ts +445 -0
  534. package/src/network/wire.ts +250 -0
  535. package/src/network/x509.ts +114 -0
  536. package/src/pals/activity/causality.ts +16 -2
  537. package/src/peers/endpoint.ts +6 -0
  538. package/src/peers/envelope.ts +82 -5
  539. package/src/peers/index.ts +10 -2
  540. package/src/peers/mode-word.ts +18 -0
  541. package/src/peers/protocol.ts +24 -1
  542. package/src/peers/record.ts +5 -0
  543. package/src/plugin/session-hook-env.ts +25 -2
  544. package/src/plugin/shell-hook.ts +208 -14
  545. package/src/pricing/catalogue.generated.ts +6 -0
  546. package/src/pricing/rates.source.json +13 -7
  547. package/src/prompt/coding-agent-doctrine.ts +33 -38
  548. package/src/provider/fallback.ts +11 -0
  549. package/src/provider/idle-timeout.ts +5 -0
  550. package/src/provider/model-window-suffix.ts +1 -0
  551. package/src/provider/retry.ts +5 -0
  552. package/src/public-runtime.ts +116 -1
  553. package/src/public-tools.ts +11 -0
  554. package/src/public-types.ts +74 -1
  555. package/src/registry/command/kernel-commands.ts +136 -49
  556. package/src/runtime/jobs/registry.ts +93 -3
  557. package/src/runtime/query/auto-review/classifier.ts +76 -14
  558. package/src/runtime/query/auto-review/feedback.ts +50 -0
  559. package/src/runtime/query/auto-review/git-status.ts +437 -0
  560. package/src/runtime/query/auto-review/index.ts +7 -2
  561. package/src/runtime/query/auto-review/prompt.ts +6 -1
  562. package/src/runtime/query/auto-review/transcript.ts +67 -4
  563. package/src/runtime/query/executor/tool-call-admission.ts +49 -3
  564. package/src/runtime/query/executor.ts +104 -21
  565. package/src/runtime/query/index.ts +66 -17
  566. package/src/runtime/query/iteration/index.ts +33 -9
  567. package/src/runtime/query/iteration/phases/compaction.ts +4 -0
  568. package/src/runtime/query/iteration/phases/context.ts +5 -0
  569. package/src/runtime/query/iteration/phases/per-call-review.ts +8 -13
  570. package/src/runtime/query/iteration/phases/tool-review.ts +7 -1
  571. package/src/runtime/query/pasted-frames.ts +119 -0
  572. package/src/runtime/query/path-escalation.ts +27 -3
  573. package/src/runtime/query/request-interrupted-turns.ts +47 -0
  574. package/src/runtime/query/review-policy.ts +125 -22
  575. package/src/runtime/query/turn/outcome-ledger.ts +99 -6
  576. package/src/runtime/query/unanswered.ts +35 -0
  577. package/src/runtime/system-events.ts +2 -0
  578. package/src/session/workspace/git-worktree.ts +34 -3
  579. package/src/store/evidence/source-kind.ts +1 -1
  580. package/src/store/goal/index.ts +320 -21
  581. package/src/store/session-index/sqlite.ts +40 -3
  582. package/src/store/session-log/disk.ts +1 -1
  583. package/src/store/session-log/lease.ts +3 -2
  584. package/src/tools/admit-facts.ts +2 -0
  585. package/src/tools/builtins/bash.ts +62 -19
  586. package/src/tools/builtins/browser-local.ts +504 -0
  587. package/src/tools/builtins/browser.ts +108 -6
  588. package/src/tools/builtins/computer-use-window-id.ts +50 -0
  589. package/src/tools/builtins/computer-use.ts +460 -68
  590. package/src/tools/builtins/content-fingerprint.ts +8 -1
  591. package/src/tools/builtins/edit.ts +93 -7
  592. package/src/tools/builtins/file-write-result.ts +2 -1
  593. package/src/tools/builtins/grep.ts +1 -1
  594. package/src/tools/builtins/job.ts +4 -2
  595. package/src/tools/builtins/monitor.ts +6 -1
  596. package/src/tools/builtins/search-tools.ts +196 -21
  597. package/src/tools/builtins/shell-read-credit.ts +97 -33
  598. package/src/tools/builtins/wait-for-job.ts +13 -3
  599. package/src/tools/builtins/web-guidance.ts +6 -0
  600. package/src/tools/builtins/web.ts +2 -1
  601. package/src/tools/builtins/write-file.ts +31 -3
  602. package/src/tools/command-shell.ts +6 -8
  603. package/src/tools/defineTool.ts +20 -1
  604. package/src/tools/goal/confirmation.ts +227 -3
  605. package/src/tools/goal/index.ts +40 -4
  606. package/src/tools/schedules/index.ts +11 -0
  607. package/src/tools/schedules/loop-tool.ts +10 -6
  608. package/src/tools/schedules/present.ts +1 -1
  609. package/src/tools/schedules/timer-tools.ts +258 -0
  610. package/src/tools/schedules/types.ts +72 -3
  611. package/src/tools/task/create.ts +2 -0
  612. package/src/toolsets/manager.ts +262 -46
  613. package/src/turn/evidence-query.ts +183 -42
  614. package/src/turn/evidence-recall.ts +96 -19
  615. package/src/turn/history-shed.ts +30 -0
  616. package/src/types/acp/index.ts +24 -0
  617. package/src/types/authorization/index.ts +20 -3
  618. package/src/types/command/index.ts +27 -2
  619. package/src/types/common/index.ts +4 -2
  620. package/src/types/goal/index.ts +61 -0
  621. package/src/types/hitl/index.ts +10 -0
  622. package/src/types/message/index.ts +65 -0
  623. package/src/types/permission/index.ts +18 -0
  624. package/src/types/plugin/index.ts +205 -26
  625. package/src/types/provider/chat.ts +10 -5
  626. package/src/types/provider/index.ts +1 -0
  627. package/src/types/provider/interface.ts +21 -0
  628. package/src/types/provider/limits.ts +35 -0
  629. package/src/types/provider/model.ts +5 -0
  630. package/src/types/session/config.ts +2 -2
  631. package/src/types/session/outcome.ts +3 -1
  632. package/src/types/tool/index.ts +30 -2
  633. package/src/types/tool/presentation.ts +4 -0
  634. package/src/utils/cost.ts +15 -6
  635. package/src/utils/file-lock.ts +328 -0
  636. package/src/utils/state-backup.ts +318 -0
  637. package/src/utils/temp-sweep.ts +327 -0
  638. package/src/utils/temp.ts +73 -2
@@ -0,0 +1,318 @@
1
+ import {
2
+ chmodSync,
3
+ closeSync,
4
+ fsyncSync,
5
+ mkdirSync,
6
+ openSync,
7
+ readFileSync,
8
+ readdirSync,
9
+ renameSync,
10
+ rmSync,
11
+ statSync,
12
+ utimesSync,
13
+ writeSync,
14
+ } from 'node:fs'
15
+ import { basename, dirname, join } from 'node:path'
16
+
17
+ import { temporaryPathFor } from './atomic-write.js'
18
+
19
+ /**
20
+ * Recovery copies of the small state files in the Namzu home whose loss hurts
21
+ * (settings, folder trust, server approvals, accounts, the provider chain).
22
+ *
23
+ * - `<home>/backups/<file>.backup.<epoch ms>`: the file as it was just before
24
+ * a write. At most one per {@link STATE_BACKUP_MIN_INTERVAL_MS} per file, the
25
+ * newest {@link STATE_BACKUP_KEEP} kept. A copy identical to the newest one is
26
+ * not taken again, so the kept copies are distinct states.
27
+ * - `<home>/backups/<file>.corrupted.<epoch ms>`: a file that could not be
28
+ * read, kept before anything is allowed to replace it.
29
+ *
30
+ * The number in a name only ever grows for a file: a copy is named at least
31
+ * one more than the newest of its kind, also when the clock went back, so the
32
+ * highest number is always the copy written last. The copy's modification
33
+ * time is set to when it was taken, and the one-a-minute limit is measured
34
+ * from that, not from the name.
35
+ *
36
+ * Every copy is written private (0600) whatever the source's mode, because
37
+ * some of these files hold keys. Nothing is restored automatically: restoring
38
+ * is copying one of these back over the file.
39
+ */
40
+ export const STATE_BACKUPS_DIR = 'backups'
41
+ export const STATE_BACKUP_KEEP = 5
42
+ export const STATE_BACKUP_MIN_INTERVAL_MS = 60_000
43
+
44
+ const DIR_MODE = 0o700
45
+ const FILE_MODE = 0o600
46
+
47
+ export interface StateBackupOptions {
48
+ /** The Namzu home; copies go to `<home>/backups/`. */
49
+ readonly home: string
50
+ /** Epoch milliseconds; injected so a test never depends on the clock. */
51
+ readonly now?: () => number
52
+ }
53
+
54
+ export interface StateBackupFileOptions extends StateBackupOptions {
55
+ /**
56
+ * Applied to the file's text before it is copied, for a secret the coming
57
+ * write removes and that must not reach a copy either.
58
+ */
59
+ readonly redact?: (text: string) => string
60
+ }
61
+
62
+ export interface StateFileCopy {
63
+ readonly path: string
64
+ /** The base name of the file this copies, such as `trust.json`. */
65
+ readonly file: string
66
+ readonly kind: 'backup' | 'corrupted'
67
+ /** Epoch milliseconds from the copy's name; the highest is the copy written last. */
68
+ readonly at: number
69
+ /** When the copy was taken (its modification time), for saying so to a person. */
70
+ readonly takenAt: number
71
+ }
72
+
73
+ export function stateBackupsDir(home: string): string {
74
+ return join(home, STATE_BACKUPS_DIR)
75
+ }
76
+
77
+ const COPY_NAME = /^(.+?)\.(backup|corrupted)\.(\d{1,16})$/
78
+
79
+ function parseCopy(dir: string, entry: string): StateFileCopy | undefined {
80
+ const match = COPY_NAME.exec(entry)
81
+ if (!match) return undefined
82
+ const at = Number(match[3])
83
+ if (!Number.isSafeInteger(at)) return undefined
84
+ const path = join(dir, entry)
85
+ let takenAt = at
86
+ try {
87
+ takenAt = statSync(path).mtimeMs
88
+ } catch {
89
+ // Listed a moment ago; the name's number is the best left.
90
+ }
91
+ const kind = match[2] === 'backup' ? 'backup' : 'corrupted'
92
+ return { path, file: match[1] as string, kind, at, takenAt }
93
+ }
94
+
95
+ /** The copies in `<home>/backups/`, newest first; only those of `file` when it is given. */
96
+ export function listStateFileCopies(home: string, file?: string): StateFileCopy[] {
97
+ const dir = stateBackupsDir(home)
98
+ let entries: string[]
99
+ try {
100
+ entries = readdirSync(dir)
101
+ } catch {
102
+ return []
103
+ }
104
+ const copies: StateFileCopy[] = []
105
+ for (const entry of entries) {
106
+ const copy = parseCopy(dir, entry)
107
+ if (copy && (file === undefined || copy.file === file)) copies.push(copy)
108
+ }
109
+ return copies.sort((a, b) => b.at - a.at || (a.path < b.path ? 1 : -1))
110
+ }
111
+
112
+ /**
113
+ * Write `content` to `file` through a private temporary beside it: the body is
114
+ * flushed to disk before the rename, so a reader sees the old file or the
115
+ * whole new one, also after a power loss. The directory is flushed too where
116
+ * the platform allows it.
117
+ */
118
+ export function writeFileAtomicSync(
119
+ file: string,
120
+ content: string | Uint8Array,
121
+ options: { readonly mode?: number } = {},
122
+ ): void {
123
+ const temporary = temporaryPathFor(file)
124
+ let fd: number | undefined
125
+ try {
126
+ fd = openSync(temporary, 'wx', options.mode ?? FILE_MODE)
127
+ const bytes = typeof content === 'string' ? Buffer.from(content, 'utf8') : content
128
+ let written = 0
129
+ while (written < bytes.length) written += writeSync(fd, bytes, written)
130
+ fsyncSync(fd)
131
+ closeSync(fd)
132
+ fd = undefined
133
+ renameSync(temporary, file)
134
+ } catch (error) {
135
+ if (fd !== undefined) {
136
+ try {
137
+ closeSync(fd)
138
+ } catch {
139
+ // The write already failed; that is the error to report.
140
+ }
141
+ }
142
+ rmSync(temporary, { force: true })
143
+ throw error
144
+ }
145
+ syncDirectoryQuietly(dirname(file))
146
+ }
147
+
148
+ function syncDirectoryQuietly(directory: string): void {
149
+ if (process.platform === 'win32') return
150
+ let fd: number | undefined
151
+ try {
152
+ fd = openSync(directory, 'r')
153
+ fsyncSync(fd)
154
+ } catch {
155
+ // The file itself is already flushed; the directory entry is the filesystem's.
156
+ } finally {
157
+ if (fd !== undefined) closeSync(fd)
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Write a private copy and prove it: a copy that keeps a wide mode, or does not
163
+ * read back as `bytes`, is removed. `takenAt` becomes its modification time.
164
+ */
165
+ function writePrivateCopy(path: string, bytes: Uint8Array, takenAt: number): void {
166
+ writeFileAtomicSync(path, bytes, { mode: FILE_MODE })
167
+ try {
168
+ if (process.platform !== 'win32') {
169
+ if ((statSync(path).mode & 0o077) !== 0) chmodSync(path, FILE_MODE)
170
+ if ((statSync(path).mode & 0o077) !== 0) throw new Error(`${path} could not be made private`)
171
+ }
172
+ if (!sameBytes(path, bytes)) throw new Error(`${path} does not read back as what was written`)
173
+ const seconds = takenAt / 1000
174
+ utimesSync(path, seconds, seconds)
175
+ } catch (error) {
176
+ rmSync(path, { force: true })
177
+ throw error
178
+ }
179
+ }
180
+
181
+ function sameBytes(path: string, bytes: Uint8Array): boolean {
182
+ try {
183
+ if (statSync(path).size !== bytes.length) return false
184
+ return Buffer.compare(readFileSync(path), bytes) === 0
185
+ } catch {
186
+ return false
187
+ }
188
+ }
189
+
190
+ function prune(copies: readonly StateFileCopy[], keep: number): void {
191
+ for (const old of copies.slice(keep)) rmSync(old.path, { force: true })
192
+ }
193
+
194
+ /**
195
+ * A name not yet taken, numbered at least one past the newest copy of its kind,
196
+ * so the order of the names is the order the copies were written in.
197
+ */
198
+ function freshName(
199
+ dir: string,
200
+ file: string,
201
+ kind: StateFileCopy['kind'],
202
+ at: number,
203
+ newest: StateFileCopy | undefined,
204
+ ): string {
205
+ let stamp = newest ? Math.max(at, newest.at + 1) : at
206
+ for (;;) {
207
+ const candidate = join(dir, `${file}.${kind}.${stamp}`)
208
+ try {
209
+ statSync(candidate)
210
+ stamp += 1
211
+ } catch {
212
+ return candidate
213
+ }
214
+ }
215
+ }
216
+
217
+ function ensureDir(home: string): string {
218
+ const dir = stateBackupsDir(home)
219
+ mkdirSync(dir, { recursive: true, mode: DIR_MODE })
220
+ if (process.platform !== 'win32') {
221
+ try {
222
+ if ((statSync(dir).mode & 0o077) !== 0) chmodSync(dir, DIR_MODE)
223
+ } catch {
224
+ // The copies themselves are private; a wide folder is reported by doctor.
225
+ }
226
+ }
227
+ return dir
228
+ }
229
+
230
+ /**
231
+ * Take a recovery copy of `file` before it is replaced. Skipped when the file
232
+ * does not exist or is empty, when the newest copy was taken less than a minute ago, or
233
+ * when it is the same as the newest copy. A newest copy taken "later" than now
234
+ * (the clock went back) does not hold the next one off. Never throws: a copy
235
+ * that could not be taken must not stop the write it was protecting. Returns
236
+ * the copy written, if any.
237
+ */
238
+ export function backupStateFile(file: string, options: StateBackupFileOptions): string | undefined {
239
+ try {
240
+ const now = options.now?.() ?? Date.now()
241
+ const name = basename(file)
242
+ const existing = listStateFileCopies(options.home, name).filter((c) => c.kind === 'backup')
243
+ const newest = existing[0]
244
+ const since = newest ? now - newest.takenAt : Number.POSITIVE_INFINITY
245
+ if (since >= 0 && since < STATE_BACKUP_MIN_INTERVAL_MS) return undefined
246
+ let bytes: Buffer
247
+ try {
248
+ bytes = readFileSync(file)
249
+ } catch {
250
+ return undefined
251
+ }
252
+ // An empty file holds nothing to go back to, and must not become "the newest copy".
253
+ if (bytes.toString('utf8').trim() === '') return undefined
254
+ if (options.redact) bytes = Buffer.from(options.redact(bytes.toString('utf8')), 'utf8')
255
+ if (newest && sameBytes(newest.path, bytes)) return undefined
256
+ const dir = ensureDir(options.home)
257
+ const path = freshName(dir, name, 'backup', now, newest)
258
+ writePrivateCopy(path, bytes, now)
259
+ prune(
260
+ listStateFileCopies(options.home, name).filter((c) => c.kind === 'backup'),
261
+ STATE_BACKUP_KEEP,
262
+ )
263
+ return path
264
+ } catch {
265
+ return undefined
266
+ }
267
+ }
268
+
269
+ /**
270
+ * Keep the bytes of a file that could not be read as `<file>.corrupted.<ms>`
271
+ * before anything may replace it. A file already kept with the same bytes is
272
+ * not copied again, so a damaged file is reported once, not on every write.
273
+ * The copy is read back and compared before this returns. Throws when no copy
274
+ * can be made; the caller must then refuse its write.
275
+ */
276
+ export function preserveUnreadableStateFile(
277
+ file: string,
278
+ options: StateBackupOptions,
279
+ ): { readonly path: string; readonly fresh: boolean } {
280
+ const bytes = readFileSync(file)
281
+ const name = basename(file)
282
+ const kept = listStateFileCopies(options.home, name).filter((c) => c.kind === 'corrupted')
283
+ const same = kept.find((copy) => sameBytes(copy.path, bytes))
284
+ if (same) return { path: same.path, fresh: false }
285
+ const dir = ensureDir(options.home)
286
+ const now = options.now?.() ?? Date.now()
287
+ const path = freshName(dir, name, 'corrupted', now, kept[0])
288
+ writePrivateCopy(path, bytes, now)
289
+ prune(
290
+ listStateFileCopies(options.home, name).filter((c) => c.kind === 'corrupted'),
291
+ STATE_BACKUP_KEEP,
292
+ )
293
+ return { path, fresh: true }
294
+ }
295
+
296
+ /**
297
+ * Change every recovery copy of `file`, for a secret that must not outlive its
298
+ * removal in an old copy. `edit` gets a copy's text and returns its new text,
299
+ * or `undefined` to remove that copy. A copy that cannot be rewritten is
300
+ * removed rather than left holding what was asked to go.
301
+ */
302
+ export function rewriteStateFileBackups(
303
+ home: string,
304
+ file: string,
305
+ edit: (text: string) => string | undefined,
306
+ ): void {
307
+ for (const copy of listStateFileCopies(home, file)) {
308
+ try {
309
+ const text = readFileSync(copy.path, 'utf8')
310
+ const next = edit(text)
311
+ if (next === undefined) rmSync(copy.path, { force: true })
312
+ // Rewritten in place, it keeps the time it was taken.
313
+ else if (next !== text) writePrivateCopy(copy.path, Buffer.from(next, 'utf8'), copy.takenAt)
314
+ } catch {
315
+ rmSync(copy.path, { force: true })
316
+ }
317
+ }
318
+ }
@@ -0,0 +1,327 @@
1
+ import { readFileSync, writeFileSync } from 'node:fs'
2
+ import {
3
+ chmod,
4
+ lstat,
5
+ readFile,
6
+ readdir,
7
+ readlink,
8
+ rm,
9
+ rmdir,
10
+ stat,
11
+ unlink,
12
+ } from 'node:fs/promises'
13
+ import { isAbsolute, join, resolve } from 'node:path'
14
+ import { isEntityId } from './id.js'
15
+ import { privateTempRootPath } from './temp.js'
16
+
17
+ /**
18
+ * Clearing what sessions left in Namzu's private temporary folder
19
+ * (`<temp base>/namzu-<user>`, see `temp.ts`).
20
+ *
21
+ * Nothing removed a session's scratch folder when the session ended, and the
22
+ * operating system does not always clear its temporary folder: on a machine
23
+ * that is never restarted, or where `/tmp` is a plain folder on the main disk,
24
+ * the folders stay until the disk is full. This sweep removes three kinds of
25
+ * leftovers, and nothing else in the folder:
26
+ *
27
+ * - `<slug>/<session-id>/` folders (the scratch folder and its `jobs/`) in
28
+ * which nothing has changed for {@link TEMP_SWEEP_MAX_AGE_MS}, unless the
29
+ * caller says the session is running. The age is the newest change of
30
+ * anything inside, not the folder's own time: one folder is shared by every
31
+ * home on the machine, so a session of another home that this process
32
+ * cannot ask about is protected by its own recent writes.
33
+ * - `hook-env*` folders whose owner process has exited. A folder made before
34
+ * owners were recorded is removed once nothing in it changed for the same age.
35
+ * - `s-<hex>` short names whose folder is gone.
36
+ *
37
+ * A link is never followed, an entry of another user is never touched, and a
38
+ * folder too large to read in full counts as recent. Errors are counted and
39
+ * left: this is housekeeping, and it never stops a session from starting.
40
+ */
41
+
42
+ /** A scratch folder in which nothing changed for this long belongs to no running session. */
43
+ export const TEMP_SWEEP_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000
44
+ /** The sweep runs at most this often; the stamp file records when it last did. */
45
+ export const TEMP_SWEEP_EVERY_MS = 24 * 60 * 60 * 1000
46
+ /** The stamp file in the private folder. */
47
+ export const TEMP_SWEEP_STAMP = '.last-cleanup'
48
+ /** The file a hook-env folder's owner writes into it (`session-hook-env.ts`). */
49
+ export const HOOK_ENV_OWNER_FILE = 'owner.json'
50
+ /** The most entries read inside one folder to learn its age; a larger folder counts as recent. */
51
+ const MAX_ENTRIES_PER_FOLDER = 5000
52
+ const ALIAS = /^s-[0-9a-f]{8}$/
53
+ const HOOK_ENV = /^hook-env/
54
+
55
+ export interface TempSweepOptions {
56
+ /** The private folder. Default {@link privateTempRootPath}. */
57
+ readonly root?: string
58
+ /** Default `Date.now()`. */
59
+ readonly now?: number
60
+ /** Default {@link TEMP_SWEEP_MAX_AGE_MS}. */
61
+ readonly maxAgeMs?: number
62
+ /** Default {@link TEMP_SWEEP_EVERY_MS}; 0 runs every time. */
63
+ readonly everyMs?: number
64
+ /** Session ids never to touch: the ones this process runs. */
65
+ readonly keep?: ReadonlySet<string>
66
+ /** Whether a session is running (its writer lease is live). A throw counts as running. */
67
+ readonly isSessionLive?: (slug: string, sessionId: string) => boolean | Promise<boolean>
68
+ /** Default `process.kill(pid, 0)`. */
69
+ readonly isPidAlive?: (pid: number) => boolean
70
+ /** Default `process.getuid`; `undefined` skips the owner check (win32). */
71
+ readonly getuid?: (() => number) | undefined
72
+ }
73
+
74
+ export interface TempSweepReport {
75
+ /** False when the sweep was not due (or the folder does not exist). */
76
+ readonly ran: boolean
77
+ readonly removedSessions: number
78
+ readonly removedHookEnv: number
79
+ readonly removedAliases: number
80
+ /** Folders it could not remove, or could not read in full. */
81
+ readonly failed: number
82
+ }
83
+
84
+ const NOT_RUN: TempSweepReport = {
85
+ ran: false,
86
+ removedSessions: 0,
87
+ removedHookEnv: 0,
88
+ removedAliases: 0,
89
+ failed: 0,
90
+ }
91
+
92
+ function defaultPidAlive(pid: number): boolean {
93
+ try {
94
+ process.kill(pid, 0)
95
+ return true
96
+ } catch (error) {
97
+ // EPERM: the process exists and belongs to someone else.
98
+ return (error as NodeJS.ErrnoException).code === 'EPERM'
99
+ }
100
+ }
101
+
102
+ /**
103
+ * The newest change inside `dir` (its own time included), walking real folders
104
+ * only. `undefined` when the folder holds more than the bound: it is then
105
+ * treated as recent, so a folder that cannot be read in full is never removed.
106
+ */
107
+ async function newestChange(dir: string): Promise<number | undefined> {
108
+ let newest = 0
109
+ let seen = 0
110
+ const pending = [dir]
111
+ while (pending.length > 0) {
112
+ const current = pending.pop() as string
113
+ const entry = await lstat(current)
114
+ newest = Math.max(newest, entry.mtimeMs, entry.ctimeMs)
115
+ if (!entry.isDirectory()) continue
116
+ const names = await readdir(current)
117
+ seen += names.length
118
+ if (seen > MAX_ENTRIES_PER_FOLDER) return undefined
119
+ for (const name of names) pending.push(join(current, name))
120
+ }
121
+ return newest
122
+ }
123
+
124
+ function ownedBy(entry: { uid: number }, getuid: (() => number) | undefined): boolean {
125
+ return getuid === undefined || entry.uid === getuid()
126
+ }
127
+
128
+ /** Make a folder writable again before removing it: a test or a tool may leave read-only folders. */
129
+ async function removeTree(path: string): Promise<void> {
130
+ try {
131
+ await rm(path, { recursive: true, force: true })
132
+ } catch (error) {
133
+ const code = (error as NodeJS.ErrnoException).code
134
+ if (code !== 'EACCES' && code !== 'EPERM') throw error
135
+ const pending = [path]
136
+ while (pending.length > 0) {
137
+ const current = pending.pop() as string
138
+ const entry = await lstat(current).catch(() => undefined)
139
+ if (!entry?.isDirectory()) continue
140
+ await chmod(current, 0o700).catch(() => undefined)
141
+ for (const name of await readdir(current).catch(() => [] as string[]))
142
+ pending.push(join(current, name))
143
+ }
144
+ await rm(path, { recursive: true, force: true })
145
+ }
146
+ }
147
+
148
+ /** Whether the stamp says the sweep ran within `everyMs`; writes a new stamp when it is due. */
149
+ function claimDue(root: string, now: number, everyMs: number): boolean {
150
+ if (everyMs <= 0) return true
151
+ const stamp = join(root, TEMP_SWEEP_STAMP)
152
+ try {
153
+ const last = Date.parse(readFileSync(stamp, 'utf8').trim())
154
+ if (Number.isFinite(last) && last <= now && now - last < everyMs) return false
155
+ } catch {
156
+ // No stamp yet, or one that cannot be read: the sweep is due.
157
+ }
158
+ try {
159
+ writeFileSync(stamp, `${new Date(now).toISOString()}\n`, { mode: 0o600 })
160
+ } catch {
161
+ // A stamp that cannot be written means the next start sweeps again; that is all.
162
+ }
163
+ return true
164
+ }
165
+
166
+ /** A hook-env folder whose owner reads as dead is removed only once nothing in it changed for this long. */
167
+ const HOOK_ENV_IDLE_MS = 10 * 60 * 1000
168
+
169
+ /** The owner a hook-env folder recorded, if it recorded one. */
170
+ async function hookEnvOwner(dir: string): Promise<number | undefined> {
171
+ try {
172
+ const raw = JSON.parse(await readFile(join(dir, HOOK_ENV_OWNER_FILE), 'utf8')) as {
173
+ pid?: unknown
174
+ }
175
+ return typeof raw.pid === 'number' && Number.isSafeInteger(raw.pid) && raw.pid > 0
176
+ ? raw.pid
177
+ : undefined
178
+ } catch {
179
+ return undefined
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Remove old session scratch folders, hook-env folders of exited processes and
185
+ * short names that point nowhere, in Namzu's private temporary folder. See the
186
+ * module header for exactly what is and is not removed.
187
+ */
188
+ export async function sweepPrivateTempRoot(
189
+ options: TempSweepOptions = {},
190
+ ): Promise<TempSweepReport> {
191
+ const root = resolve(options.root ?? privateTempRootPath())
192
+ if (!isAbsolute(root)) return NOT_RUN
193
+ const now = options.now ?? Date.now()
194
+ const maxAgeMs = options.maxAgeMs ?? TEMP_SWEEP_MAX_AGE_MS
195
+ const getuid = 'getuid' in options ? options.getuid : process.getuid?.bind(process)
196
+ const isPidAlive = options.isPidAlive ?? defaultPidAlive
197
+ const keep = options.keep ?? new Set<string>()
198
+ try {
199
+ const top = await lstat(root)
200
+ if (!top.isDirectory() || top.isSymbolicLink() || !ownedBy(top, getuid)) return NOT_RUN
201
+ } catch {
202
+ return NOT_RUN
203
+ }
204
+ if (!claimDue(root, now, options.everyMs ?? TEMP_SWEEP_EVERY_MS)) return NOT_RUN
205
+
206
+ let removedSessions = 0
207
+ let removedHookEnv = 0
208
+ let removedAliases = 0
209
+ let failed = 0
210
+ const old = async (dir: string): Promise<boolean> => {
211
+ try {
212
+ const newest = await newestChange(dir)
213
+ if (newest === undefined) {
214
+ failed += 1
215
+ return false
216
+ }
217
+ return now - newest >= maxAgeMs
218
+ } catch {
219
+ failed += 1
220
+ return false
221
+ }
222
+ }
223
+ const idleFor = async (dir: string, ms: number): Promise<boolean> => {
224
+ try {
225
+ const newest = await newestChange(dir)
226
+ return newest !== undefined && now - newest >= ms
227
+ } catch {
228
+ return false
229
+ }
230
+ }
231
+ const running = async (slug: string, sessionId: string): Promise<boolean> => {
232
+ if (keep.has(sessionId)) return true
233
+ if (!options.isSessionLive) return false
234
+ try {
235
+ return await options.isSessionLive(slug, sessionId)
236
+ } catch {
237
+ return true
238
+ }
239
+ }
240
+
241
+ let names: string[]
242
+ try {
243
+ names = await readdir(root)
244
+ } catch {
245
+ return { ...NOT_RUN, ran: true, failed: 1 }
246
+ }
247
+ for (const name of names) {
248
+ if (ALIAS.test(name)) continue // after the folders, so a link to one removed now goes too
249
+ const path = join(root, name)
250
+ let entry: Awaited<ReturnType<typeof lstat>>
251
+ try {
252
+ entry = await lstat(path)
253
+ } catch {
254
+ continue
255
+ }
256
+ if (!entry.isDirectory() || entry.isSymbolicLink() || !ownedBy(entry, getuid)) continue
257
+ if (HOOK_ENV.test(name)) {
258
+ const pid = await hookEnvOwner(path)
259
+ // A pid this process cannot see (another pid namespace sharing the folder) reads as dead, so
260
+ // a dead owner alone is not enough: the folder must also have been idle for a while.
261
+ const gone =
262
+ pid === undefined
263
+ ? await old(path)
264
+ : !isPidAlive(pid) && (await idleFor(path, HOOK_ENV_IDLE_MS))
265
+ if (!gone) continue
266
+ try {
267
+ await removeTree(path)
268
+ removedHookEnv += 1
269
+ } catch {
270
+ failed += 1
271
+ }
272
+ continue
273
+ }
274
+ // A project's folder holds one folder per session, named by its id.
275
+ let children: string[]
276
+ try {
277
+ children = await readdir(path)
278
+ } catch {
279
+ continue
280
+ }
281
+ let removedHere = 0
282
+ for (const child of children) {
283
+ if (!isEntityId(child, 'session')) continue
284
+ const sessionDir = join(path, child)
285
+ try {
286
+ const inner = await lstat(sessionDir)
287
+ if (!inner.isDirectory() || inner.isSymbolicLink() || !ownedBy(inner, getuid)) continue
288
+ } catch {
289
+ continue
290
+ }
291
+ if (await running(name, child)) continue
292
+ if (!(await old(sessionDir))) continue
293
+ try {
294
+ await removeTree(sessionDir)
295
+ removedSessions += 1
296
+ removedHere += 1
297
+ } catch {
298
+ failed += 1
299
+ }
300
+ }
301
+ if (removedHere > 0) {
302
+ // Only an empty folder goes (rmdir refuses one that is not); one that holds anything stays.
303
+ await rmdir(path).catch(() => undefined)
304
+ }
305
+ }
306
+ for (const name of names) {
307
+ if (!ALIAS.test(name)) continue
308
+ const link = join(root, name)
309
+ try {
310
+ if (!(await lstat(link)).isSymbolicLink()) continue
311
+ const target = await readlink(link)
312
+ try {
313
+ await stat(link)
314
+ continue // the folder it names is still there
315
+ } catch (error) {
316
+ if ((error as NodeJS.ErrnoException).code !== 'ENOENT') continue
317
+ }
318
+ // Re-read before removing: a session may have just pointed the name at a new folder.
319
+ if ((await readlink(link)) !== target) continue
320
+ await unlink(link)
321
+ removedAliases += 1
322
+ } catch {
323
+ // Removed by someone else first, or not ours to read.
324
+ }
325
+ }
326
+ return { ran: true, removedSessions, removedHookEnv, removedAliases, failed }
327
+ }