@namzu/sdk 65.3.0 → 67.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 (568) hide show
  1. package/CHANGELOG.md +153 -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/prompt/coding-agent-doctrine.d.ts +4 -2
  180. package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
  181. package/dist/prompt/coding-agent-doctrine.js +33 -38
  182. package/dist/prompt/coding-agent-doctrine.js.map +1 -1
  183. package/dist/public-runtime.d.ts +14 -6
  184. package/dist/public-runtime.d.ts.map +1 -1
  185. package/dist/public-runtime.js +22 -5
  186. package/dist/public-runtime.js.map +1 -1
  187. package/dist/public-tools.d.ts +3 -2
  188. package/dist/public-tools.d.ts.map +1 -1
  189. package/dist/public-tools.js +2 -2
  190. package/dist/public-tools.js.map +1 -1
  191. package/dist/public-types.d.ts +7 -2
  192. package/dist/public-types.d.ts.map +1 -1
  193. package/dist/runtime/jobs/registry.d.ts +29 -1
  194. package/dist/runtime/jobs/registry.d.ts.map +1 -1
  195. package/dist/runtime/jobs/registry.js +64 -0
  196. package/dist/runtime/jobs/registry.js.map +1 -1
  197. package/dist/runtime/query/auto-review/classifier.d.ts +30 -6
  198. package/dist/runtime/query/auto-review/classifier.d.ts.map +1 -1
  199. package/dist/runtime/query/auto-review/classifier.js +40 -12
  200. package/dist/runtime/query/auto-review/classifier.js.map +1 -1
  201. package/dist/runtime/query/auto-review/feedback.d.ts +37 -0
  202. package/dist/runtime/query/auto-review/feedback.d.ts.map +1 -0
  203. package/dist/runtime/query/auto-review/feedback.js +45 -0
  204. package/dist/runtime/query/auto-review/feedback.js.map +1 -0
  205. package/dist/runtime/query/auto-review/git-status.d.ts +59 -0
  206. package/dist/runtime/query/auto-review/git-status.d.ts.map +1 -0
  207. package/dist/runtime/query/auto-review/git-status.js +379 -0
  208. package/dist/runtime/query/auto-review/git-status.js.map +1 -0
  209. package/dist/runtime/query/auto-review/index.d.ts +3 -3
  210. package/dist/runtime/query/auto-review/index.d.ts.map +1 -1
  211. package/dist/runtime/query/auto-review/index.js +2 -2
  212. package/dist/runtime/query/auto-review/index.js.map +1 -1
  213. package/dist/runtime/query/auto-review/prompt.d.ts.map +1 -1
  214. package/dist/runtime/query/auto-review/prompt.js +6 -1
  215. package/dist/runtime/query/auto-review/prompt.js.map +1 -1
  216. package/dist/runtime/query/auto-review/transcript.d.ts +14 -1
  217. package/dist/runtime/query/auto-review/transcript.d.ts.map +1 -1
  218. package/dist/runtime/query/auto-review/transcript.js +48 -2
  219. package/dist/runtime/query/auto-review/transcript.js.map +1 -1
  220. package/dist/runtime/query/executor/tool-call-admission.d.ts.map +1 -1
  221. package/dist/runtime/query/executor/tool-call-admission.js +40 -3
  222. package/dist/runtime/query/executor/tool-call-admission.js.map +1 -1
  223. package/dist/runtime/query/executor.d.ts +32 -10
  224. package/dist/runtime/query/executor.d.ts.map +1 -1
  225. package/dist/runtime/query/executor.js +78 -17
  226. package/dist/runtime/query/executor.js.map +1 -1
  227. package/dist/runtime/query/index.d.ts +9 -0
  228. package/dist/runtime/query/index.d.ts.map +1 -1
  229. package/dist/runtime/query/index.js +54 -17
  230. package/dist/runtime/query/index.js.map +1 -1
  231. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  232. package/dist/runtime/query/iteration/index.js +17 -3
  233. package/dist/runtime/query/iteration/index.js.map +1 -1
  234. package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
  235. package/dist/runtime/query/iteration/phases/compaction.js +5 -2
  236. package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
  237. package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
  238. package/dist/runtime/query/iteration/phases/context.js +5 -0
  239. package/dist/runtime/query/iteration/phases/context.js.map +1 -1
  240. package/dist/runtime/query/iteration/phases/per-call-review.d.ts +1 -2
  241. package/dist/runtime/query/iteration/phases/per-call-review.d.ts.map +1 -1
  242. package/dist/runtime/query/iteration/phases/per-call-review.js +2 -5
  243. package/dist/runtime/query/iteration/phases/per-call-review.js.map +1 -1
  244. package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
  245. package/dist/runtime/query/iteration/phases/tool-review.js +6 -1
  246. package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
  247. package/dist/runtime/query/pasted-frames.d.ts +34 -0
  248. package/dist/runtime/query/pasted-frames.d.ts.map +1 -0
  249. package/dist/runtime/query/pasted-frames.js +108 -0
  250. package/dist/runtime/query/pasted-frames.js.map +1 -0
  251. package/dist/runtime/query/path-escalation.d.ts +6 -0
  252. package/dist/runtime/query/path-escalation.d.ts.map +1 -1
  253. package/dist/runtime/query/path-escalation.js +15 -3
  254. package/dist/runtime/query/path-escalation.js.map +1 -1
  255. package/dist/runtime/query/request-interrupted-turns.d.ts +20 -0
  256. package/dist/runtime/query/request-interrupted-turns.d.ts.map +1 -0
  257. package/dist/runtime/query/request-interrupted-turns.js +43 -0
  258. package/dist/runtime/query/request-interrupted-turns.js.map +1 -0
  259. package/dist/runtime/query/review-policy.d.ts +16 -6
  260. package/dist/runtime/query/review-policy.d.ts.map +1 -1
  261. package/dist/runtime/query/review-policy.js +111 -15
  262. package/dist/runtime/query/review-policy.js.map +1 -1
  263. package/dist/runtime/query/turn/outcome-ledger.d.ts +9 -0
  264. package/dist/runtime/query/turn/outcome-ledger.d.ts.map +1 -1
  265. package/dist/runtime/query/turn/outcome-ledger.js +94 -6
  266. package/dist/runtime/query/turn/outcome-ledger.js.map +1 -1
  267. package/dist/runtime/query/unanswered.d.ts +19 -0
  268. package/dist/runtime/query/unanswered.d.ts.map +1 -0
  269. package/dist/runtime/query/unanswered.js +25 -0
  270. package/dist/runtime/query/unanswered.js.map +1 -0
  271. package/dist/runtime/system-events.d.ts +4 -4
  272. package/dist/runtime/system-events.d.ts.map +1 -1
  273. package/dist/runtime/system-events.js +2 -0
  274. package/dist/runtime/system-events.js.map +1 -1
  275. package/dist/session/workspace/git-worktree.d.ts +8 -0
  276. package/dist/session/workspace/git-worktree.d.ts.map +1 -1
  277. package/dist/session/workspace/git-worktree.js +21 -3
  278. package/dist/session/workspace/git-worktree.js.map +1 -1
  279. package/dist/store/evidence/source-kind.d.ts +1 -1
  280. package/dist/store/evidence/source-kind.d.ts.map +1 -1
  281. package/dist/store/evidence/source-kind.js +1 -1
  282. package/dist/store/evidence/source-kind.js.map +1 -1
  283. package/dist/store/session-index/sqlite.d.ts.map +1 -1
  284. package/dist/store/session-index/sqlite.js +42 -3
  285. package/dist/store/session-index/sqlite.js.map +1 -1
  286. package/dist/store/session-log/disk.js +1 -1
  287. package/dist/store/session-log/disk.js.map +1 -1
  288. package/dist/store/session-log/lease.d.ts.map +1 -1
  289. package/dist/store/session-log/lease.js +3 -2
  290. package/dist/store/session-log/lease.js.map +1 -1
  291. package/dist/tools/admit-facts.d.ts.map +1 -1
  292. package/dist/tools/admit-facts.js +2 -0
  293. package/dist/tools/admit-facts.js.map +1 -1
  294. package/dist/tools/builtins/bash.d.ts +10 -2
  295. package/dist/tools/builtins/bash.d.ts.map +1 -1
  296. package/dist/tools/builtins/bash.js +56 -17
  297. package/dist/tools/builtins/bash.js.map +1 -1
  298. package/dist/tools/builtins/browser-local.d.ts +117 -0
  299. package/dist/tools/builtins/browser-local.d.ts.map +1 -0
  300. package/dist/tools/builtins/browser-local.js +437 -0
  301. package/dist/tools/builtins/browser-local.js.map +1 -0
  302. package/dist/tools/builtins/browser.d.ts +18 -1
  303. package/dist/tools/builtins/browser.d.ts.map +1 -1
  304. package/dist/tools/builtins/browser.js +84 -6
  305. package/dist/tools/builtins/browser.js.map +1 -1
  306. package/dist/tools/builtins/computer-use-window-id.d.ts +16 -0
  307. package/dist/tools/builtins/computer-use-window-id.d.ts.map +1 -0
  308. package/dist/tools/builtins/computer-use-window-id.js +52 -0
  309. package/dist/tools/builtins/computer-use-window-id.js.map +1 -0
  310. package/dist/tools/builtins/computer-use.d.ts +65 -7
  311. package/dist/tools/builtins/computer-use.d.ts.map +1 -1
  312. package/dist/tools/builtins/computer-use.js +384 -71
  313. package/dist/tools/builtins/computer-use.js.map +1 -1
  314. package/dist/tools/builtins/content-fingerprint.d.ts +6 -0
  315. package/dist/tools/builtins/content-fingerprint.d.ts.map +1 -1
  316. package/dist/tools/builtins/content-fingerprint.js +7 -1
  317. package/dist/tools/builtins/content-fingerprint.js.map +1 -1
  318. package/dist/tools/builtins/edit.d.ts.map +1 -1
  319. package/dist/tools/builtins/edit.js +89 -7
  320. package/dist/tools/builtins/edit.js.map +1 -1
  321. package/dist/tools/builtins/file-write-result.d.ts.map +1 -1
  322. package/dist/tools/builtins/file-write-result.js +2 -1
  323. package/dist/tools/builtins/file-write-result.js.map +1 -1
  324. package/dist/tools/builtins/grep.js +1 -1
  325. package/dist/tools/builtins/grep.js.map +1 -1
  326. package/dist/tools/builtins/job.d.ts.map +1 -1
  327. package/dist/tools/builtins/job.js +4 -2
  328. package/dist/tools/builtins/job.js.map +1 -1
  329. package/dist/tools/builtins/monitor.d.ts.map +1 -1
  330. package/dist/tools/builtins/monitor.js +6 -1
  331. package/dist/tools/builtins/monitor.js.map +1 -1
  332. package/dist/tools/builtins/search-tools.d.ts +12 -1
  333. package/dist/tools/builtins/search-tools.d.ts.map +1 -1
  334. package/dist/tools/builtins/search-tools.js +166 -20
  335. package/dist/tools/builtins/search-tools.js.map +1 -1
  336. package/dist/tools/builtins/shell-read-credit.d.ts +18 -6
  337. package/dist/tools/builtins/shell-read-credit.d.ts.map +1 -1
  338. package/dist/tools/builtins/shell-read-credit.js +95 -27
  339. package/dist/tools/builtins/shell-read-credit.js.map +1 -1
  340. package/dist/tools/builtins/wait-for-job.d.ts.map +1 -1
  341. package/dist/tools/builtins/wait-for-job.js +14 -3
  342. package/dist/tools/builtins/wait-for-job.js.map +1 -1
  343. package/dist/tools/builtins/web-guidance.d.ts +6 -0
  344. package/dist/tools/builtins/web-guidance.d.ts.map +1 -1
  345. package/dist/tools/builtins/web-guidance.js +6 -0
  346. package/dist/tools/builtins/web-guidance.js.map +1 -1
  347. package/dist/tools/builtins/web.d.ts.map +1 -1
  348. package/dist/tools/builtins/web.js +2 -1
  349. package/dist/tools/builtins/web.js.map +1 -1
  350. package/dist/tools/builtins/write-file.d.ts.map +1 -1
  351. package/dist/tools/builtins/write-file.js +32 -2
  352. package/dist/tools/builtins/write-file.js.map +1 -1
  353. package/dist/tools/command-shell.d.ts +1 -1
  354. package/dist/tools/command-shell.d.ts.map +1 -1
  355. package/dist/tools/command-shell.js +6 -8
  356. package/dist/tools/command-shell.js.map +1 -1
  357. package/dist/tools/defineTool.d.ts +2 -0
  358. package/dist/tools/defineTool.d.ts.map +1 -1
  359. package/dist/tools/defineTool.js +17 -1
  360. package/dist/tools/defineTool.js.map +1 -1
  361. package/dist/tools/schedules/index.d.ts +1 -0
  362. package/dist/tools/schedules/index.d.ts.map +1 -1
  363. package/dist/tools/schedules/index.js +1 -0
  364. package/dist/tools/schedules/index.js.map +1 -1
  365. package/dist/tools/schedules/loop-tool.d.ts.map +1 -1
  366. package/dist/tools/schedules/loop-tool.js +8 -6
  367. package/dist/tools/schedules/loop-tool.js.map +1 -1
  368. package/dist/tools/schedules/present.js +1 -1
  369. package/dist/tools/schedules/present.js.map +1 -1
  370. package/dist/tools/schedules/timer-tools.d.ts +36 -0
  371. package/dist/tools/schedules/timer-tools.d.ts.map +1 -0
  372. package/dist/tools/schedules/timer-tools.js +234 -0
  373. package/dist/tools/schedules/timer-tools.js.map +1 -0
  374. package/dist/tools/schedules/types.d.ts +68 -3
  375. package/dist/tools/schedules/types.d.ts.map +1 -1
  376. package/dist/tools/task/create.d.ts.map +1 -1
  377. package/dist/tools/task/create.js +2 -0
  378. package/dist/tools/task/create.js.map +1 -1
  379. package/dist/toolsets/manager.d.ts +49 -7
  380. package/dist/toolsets/manager.d.ts.map +1 -1
  381. package/dist/toolsets/manager.js +239 -40
  382. package/dist/toolsets/manager.js.map +1 -1
  383. package/dist/turn/evidence-query.d.ts +18 -1
  384. package/dist/turn/evidence-query.d.ts.map +1 -1
  385. package/dist/turn/evidence-query.js +177 -35
  386. package/dist/turn/evidence-query.js.map +1 -1
  387. package/dist/turn/evidence-recall.d.ts +8 -1
  388. package/dist/turn/evidence-recall.d.ts.map +1 -1
  389. package/dist/turn/evidence-recall.js +82 -22
  390. package/dist/turn/evidence-recall.js.map +1 -1
  391. package/dist/turn/history-shed.d.ts +15 -0
  392. package/dist/turn/history-shed.d.ts.map +1 -0
  393. package/dist/turn/history-shed.js +23 -0
  394. package/dist/turn/history-shed.js.map +1 -0
  395. package/dist/types/acp/index.d.ts +26 -0
  396. package/dist/types/acp/index.d.ts.map +1 -1
  397. package/dist/types/authorization/index.d.ts +9 -1
  398. package/dist/types/authorization/index.d.ts.map +1 -1
  399. package/dist/types/authorization/index.js +11 -2
  400. package/dist/types/authorization/index.js.map +1 -1
  401. package/dist/types/hitl/index.d.ts +10 -0
  402. package/dist/types/hitl/index.d.ts.map +1 -1
  403. package/dist/types/hitl/index.js.map +1 -1
  404. package/dist/types/message/index.d.ts +56 -1
  405. package/dist/types/message/index.d.ts.map +1 -1
  406. package/dist/types/message/index.js +2 -1
  407. package/dist/types/message/index.js.map +1 -1
  408. package/dist/types/permission/index.d.ts +14 -0
  409. package/dist/types/permission/index.d.ts.map +1 -1
  410. package/dist/types/permission/index.js +14 -0
  411. package/dist/types/permission/index.js.map +1 -1
  412. package/dist/types/plugin/index.d.ts +148 -1
  413. package/dist/types/plugin/index.d.ts.map +1 -1
  414. package/dist/types/plugin/index.js +9 -0
  415. package/dist/types/plugin/index.js.map +1 -1
  416. package/dist/types/provider/index.d.ts +1 -0
  417. package/dist/types/provider/index.d.ts.map +1 -1
  418. package/dist/types/provider/interface.d.ts +12 -0
  419. package/dist/types/provider/interface.d.ts.map +1 -1
  420. package/dist/types/provider/limits.d.ts +33 -0
  421. package/dist/types/provider/limits.d.ts.map +1 -0
  422. package/dist/types/provider/limits.js +7 -0
  423. package/dist/types/provider/limits.js.map +1 -0
  424. package/dist/types/tool/index.d.ts +30 -0
  425. package/dist/types/tool/index.d.ts.map +1 -1
  426. package/dist/types/tool/index.js.map +1 -1
  427. package/dist/types/tool/presentation.d.ts +4 -0
  428. package/dist/types/tool/presentation.d.ts.map +1 -1
  429. package/dist/utils/file-lock.d.ts +88 -0
  430. package/dist/utils/file-lock.d.ts.map +1 -0
  431. package/dist/utils/file-lock.js +281 -0
  432. package/dist/utils/file-lock.js.map +1 -0
  433. package/dist/utils/state-backup.d.ts +87 -0
  434. package/dist/utils/state-backup.d.ts.map +1 -0
  435. package/dist/utils/state-backup.js +272 -0
  436. package/dist/utils/state-backup.js.map +1 -0
  437. package/dist/utils/temp-sweep.d.ts +66 -0
  438. package/dist/utils/temp-sweep.d.ts.map +1 -0
  439. package/dist/utils/temp-sweep.js +311 -0
  440. package/dist/utils/temp-sweep.js.map +1 -0
  441. package/dist/utils/temp.d.ts +14 -0
  442. package/dist/utils/temp.d.ts.map +1 -1
  443. package/dist/utils/temp.js +70 -2
  444. package/dist/utils/temp.js.map +1 -1
  445. package/package.json +1 -1
  446. package/src/account/lock.ts +13 -133
  447. package/src/account/registry.ts +75 -21
  448. package/src/account/types.ts +8 -9
  449. package/src/authorization/dangerous-commands.ts +542 -0
  450. package/src/authorization/gate.ts +19 -5
  451. package/src/authorization/protected-paths.ts +13 -8
  452. package/src/authorization/readonly-commands.ts +35 -1
  453. package/src/authorization/removal.ts +7 -7
  454. package/src/authorization/rules.ts +75 -15
  455. package/src/authorization/shell-lexer.ts +19 -0
  456. package/src/bridge/acp/permission.ts +2 -0
  457. package/src/bridge/acp/server.ts +66 -6
  458. package/src/compaction/context-breakdown.ts +53 -11
  459. package/src/compaction/dangling.ts +2 -0
  460. package/src/compaction/extractor.ts +9 -1
  461. package/src/compaction/index.ts +1 -0
  462. package/src/compaction/managers/structured.ts +2 -2
  463. package/src/compaction/manual.ts +11 -3
  464. package/src/compaction/model-summary.ts +8 -1
  465. package/src/compaction/summary.ts +54 -7
  466. package/src/constants/tools/index.ts +30 -16
  467. package/src/manager/session/turn-recorder.ts +37 -1
  468. package/src/network/audit.ts +113 -0
  469. package/src/network/clock.ts +62 -0
  470. package/src/network/discovery-mdns.ts +485 -0
  471. package/src/network/discovery.ts +210 -0
  472. package/src/network/fingerprint.ts +241 -0
  473. package/src/network/identity.ts +98 -0
  474. package/src/network/index.ts +134 -0
  475. package/src/network/join.ts +177 -0
  476. package/src/network/mail-policy.ts +199 -0
  477. package/src/network/membership.ts +290 -0
  478. package/src/network/roster.ts +165 -0
  479. package/src/network/service.ts +2304 -0
  480. package/src/network/store.ts +261 -0
  481. package/src/network/transport.ts +445 -0
  482. package/src/network/wire.ts +250 -0
  483. package/src/network/x509.ts +114 -0
  484. package/src/pals/activity/causality.ts +16 -2
  485. package/src/peers/endpoint.ts +6 -0
  486. package/src/peers/envelope.ts +82 -5
  487. package/src/peers/index.ts +10 -2
  488. package/src/peers/mode-word.ts +18 -0
  489. package/src/peers/protocol.ts +24 -1
  490. package/src/peers/record.ts +5 -0
  491. package/src/plugin/session-hook-env.ts +25 -2
  492. package/src/plugin/shell-hook.ts +208 -14
  493. package/src/prompt/coding-agent-doctrine.ts +33 -38
  494. package/src/public-runtime.ts +107 -1
  495. package/src/public-tools.ts +11 -0
  496. package/src/public-types.ts +67 -1
  497. package/src/runtime/jobs/registry.ts +93 -3
  498. package/src/runtime/query/auto-review/classifier.ts +76 -14
  499. package/src/runtime/query/auto-review/feedback.ts +50 -0
  500. package/src/runtime/query/auto-review/git-status.ts +437 -0
  501. package/src/runtime/query/auto-review/index.ts +7 -2
  502. package/src/runtime/query/auto-review/prompt.ts +6 -1
  503. package/src/runtime/query/auto-review/transcript.ts +67 -4
  504. package/src/runtime/query/executor/tool-call-admission.ts +49 -3
  505. package/src/runtime/query/executor.ts +104 -21
  506. package/src/runtime/query/index.ts +66 -17
  507. package/src/runtime/query/iteration/index.ts +24 -3
  508. package/src/runtime/query/iteration/phases/compaction.ts +4 -0
  509. package/src/runtime/query/iteration/phases/context.ts +5 -0
  510. package/src/runtime/query/iteration/phases/per-call-review.ts +8 -13
  511. package/src/runtime/query/iteration/phases/tool-review.ts +7 -1
  512. package/src/runtime/query/pasted-frames.ts +119 -0
  513. package/src/runtime/query/path-escalation.ts +27 -3
  514. package/src/runtime/query/request-interrupted-turns.ts +47 -0
  515. package/src/runtime/query/review-policy.ts +125 -22
  516. package/src/runtime/query/turn/outcome-ledger.ts +99 -6
  517. package/src/runtime/query/unanswered.ts +35 -0
  518. package/src/runtime/system-events.ts +2 -0
  519. package/src/session/workspace/git-worktree.ts +34 -3
  520. package/src/store/evidence/source-kind.ts +1 -1
  521. package/src/store/session-index/sqlite.ts +40 -3
  522. package/src/store/session-log/disk.ts +1 -1
  523. package/src/store/session-log/lease.ts +3 -2
  524. package/src/tools/admit-facts.ts +2 -0
  525. package/src/tools/builtins/bash.ts +62 -19
  526. package/src/tools/builtins/browser-local.ts +504 -0
  527. package/src/tools/builtins/browser.ts +108 -6
  528. package/src/tools/builtins/computer-use-window-id.ts +50 -0
  529. package/src/tools/builtins/computer-use.ts +460 -68
  530. package/src/tools/builtins/content-fingerprint.ts +8 -1
  531. package/src/tools/builtins/edit.ts +93 -7
  532. package/src/tools/builtins/file-write-result.ts +2 -1
  533. package/src/tools/builtins/grep.ts +1 -1
  534. package/src/tools/builtins/job.ts +4 -2
  535. package/src/tools/builtins/monitor.ts +6 -1
  536. package/src/tools/builtins/search-tools.ts +196 -21
  537. package/src/tools/builtins/shell-read-credit.ts +97 -33
  538. package/src/tools/builtins/wait-for-job.ts +13 -3
  539. package/src/tools/builtins/web-guidance.ts +6 -0
  540. package/src/tools/builtins/web.ts +2 -1
  541. package/src/tools/builtins/write-file.ts +31 -3
  542. package/src/tools/command-shell.ts +6 -8
  543. package/src/tools/defineTool.ts +20 -1
  544. package/src/tools/schedules/index.ts +11 -0
  545. package/src/tools/schedules/loop-tool.ts +10 -6
  546. package/src/tools/schedules/present.ts +1 -1
  547. package/src/tools/schedules/timer-tools.ts +258 -0
  548. package/src/tools/schedules/types.ts +72 -3
  549. package/src/tools/task/create.ts +2 -0
  550. package/src/toolsets/manager.ts +262 -46
  551. package/src/turn/evidence-query.ts +183 -42
  552. package/src/turn/evidence-recall.ts +96 -19
  553. package/src/turn/history-shed.ts +30 -0
  554. package/src/types/acp/index.ts +24 -0
  555. package/src/types/authorization/index.ts +20 -3
  556. package/src/types/hitl/index.ts +10 -0
  557. package/src/types/message/index.ts +65 -0
  558. package/src/types/permission/index.ts +18 -0
  559. package/src/types/plugin/index.ts +205 -26
  560. package/src/types/provider/index.ts +1 -0
  561. package/src/types/provider/interface.ts +13 -0
  562. package/src/types/provider/limits.ts +35 -0
  563. package/src/types/tool/index.ts +30 -2
  564. package/src/types/tool/presentation.ts +4 -0
  565. package/src/utils/file-lock.ts +328 -0
  566. package/src/utils/state-backup.ts +318 -0
  567. package/src/utils/temp-sweep.ts +327 -0
  568. package/src/utils/temp.ts +73 -2
@@ -0,0 +1,199 @@
1
+ /**
2
+ * What a terminal does with a message from another session: accept it, hold it
3
+ * for the person, or refuse it. One pure decision for mail from another machine
4
+ * and mail from another terminal of this machine, plus the counters and the
5
+ * hold queue that decision depends on.
6
+ *
7
+ * A held message is not in the model's context and approves nothing. It waits
8
+ * for the person for ten minutes; if nobody answers it expires and the sender
9
+ * is told. Allowing is "allow once": nothing is remembered.
10
+ *
11
+ * @experimental
12
+ */
13
+
14
+ import { permissionModeWord } from '../peers/mode-word.js'
15
+ import type { NetworkClock } from './clock.js'
16
+ import { MAX_HOPS, MAX_NET_TEXT_BYTES } from './wire.js'
17
+
18
+ export type MailSetting = 'members' | 'hold' | 'off'
19
+
20
+ export interface MailDecisionInput {
21
+ readonly scope: 'remote' | 'local'
22
+ readonly text: string
23
+ readonly hops: number
24
+ readonly receiver: {
25
+ readonly mode: string | undefined
26
+ readonly bypass: boolean
27
+ /** The terminal takes messages at all (`enabled`, ready). */
28
+ readonly accepting: boolean
29
+ readonly setting: MailSetting
30
+ }
31
+ readonly sender: {
32
+ /** What the sender says its mode is. */
33
+ readonly claimedMode: string
34
+ /** What its machine's own roster says. Absent when it could not be vouched for. */
35
+ readonly vouchedMode: string | undefined
36
+ }
37
+ /** False when the sending machine's bucket is empty. */
38
+ readonly rateOk: boolean
39
+ /** Messages between this pair of sessions in the last 60 seconds, counting this one. */
40
+ readonly pairCount: number
41
+ }
42
+
43
+ export type MailDecision =
44
+ | { readonly action: 'accept' }
45
+ | { readonly action: 'hold'; readonly reason: string }
46
+ | { readonly action: 'refuse'; readonly reason: string }
47
+
48
+ export const LOOP_THRESHOLD = 8
49
+
50
+ export function decideMail(input: MailDecisionInput): MailDecision {
51
+ const { receiver, sender } = input
52
+ if (input.scope === 'remote' && receiver.setting === 'off') {
53
+ return { action: 'refuse', reason: 'This machine does not take messages from other machines.' }
54
+ }
55
+ if (!receiver.accepting)
56
+ return { action: 'refuse', reason: 'That terminal is not taking messages.' }
57
+ if (input.text.trim().length === 0) return { action: 'refuse', reason: 'The message is empty.' }
58
+ if (Buffer.byteLength(input.text, 'utf8') > MAX_NET_TEXT_BYTES) {
59
+ return { action: 'refuse', reason: 'The message is too long.' }
60
+ }
61
+ if (input.scope === 'remote' && !input.rateOk) {
62
+ return {
63
+ action: 'refuse',
64
+ reason: 'Too many messages from that machine; try again in a moment.',
65
+ }
66
+ }
67
+ if (input.scope === 'remote' && input.hops > MAX_HOPS) {
68
+ return { action: 'refuse', reason: 'This conversation between sessions is going in circles.' }
69
+ }
70
+ if (receiver.mode === undefined)
71
+ return { action: 'hold', reason: 'its permission mode is not known' }
72
+ if (receiver.bypass) return { action: 'hold', reason: 'this terminal is in bypass mode' }
73
+ if (sender.vouchedMode === undefined || sender.claimedMode !== sender.vouchedMode) {
74
+ return { action: 'hold', reason: 'the sender’s permission mode could not be confirmed' }
75
+ }
76
+ if (sender.vouchedMode !== receiver.mode) {
77
+ return {
78
+ action: 'hold',
79
+ reason: `its permission mode differs (it is in ${permissionModeWord(sender.vouchedMode)}, you are in ${permissionModeWord(receiver.mode)})`,
80
+ }
81
+ }
82
+ if (input.pairCount > LOOP_THRESHOLD)
83
+ return { action: 'hold', reason: 'these two sessions are messaging each other very fast' }
84
+ if (input.scope === 'remote' && receiver.setting === 'hold') {
85
+ return {
86
+ action: 'hold',
87
+ reason: 'you asked to see every message from your other machines first',
88
+ }
89
+ }
90
+ return { action: 'accept' }
91
+ }
92
+
93
+ export interface TokenBucketOptions {
94
+ readonly capacity: number
95
+ /** Tokens added per second. */
96
+ readonly refillPerSecond: number
97
+ readonly clock: NetworkClock
98
+ }
99
+
100
+ /** A token bucket per key. Empty means refuse until the clock refills it. */
101
+ export class TokenBuckets {
102
+ private readonly state = new Map<string, { tokens: number; at: number }>()
103
+ constructor(private readonly options: TokenBucketOptions) {}
104
+
105
+ take(key: string): boolean {
106
+ const now = this.options.clock.now()
107
+ const prior = this.state.get(key) ?? { tokens: this.options.capacity, at: now }
108
+ const tokens = Math.min(
109
+ this.options.capacity,
110
+ prior.tokens + ((now - prior.at) / 1000) * this.options.refillPerSecond,
111
+ )
112
+ if (tokens < 1) {
113
+ this.state.set(key, { tokens, at: now })
114
+ return false
115
+ }
116
+ this.state.set(key, { tokens: tokens - 1, at: now })
117
+ return true
118
+ }
119
+ }
120
+
121
+ /** A sliding window counter per key, for "more than N in the last W ms". */
122
+ export class SlidingCounter {
123
+ private readonly hits = new Map<string, number[]>()
124
+ constructor(
125
+ private readonly clock: NetworkClock,
126
+ private readonly windowMs: number,
127
+ ) {}
128
+
129
+ /** Record one hit and return how many fall in the window, this one included. */
130
+ record(key: string): number {
131
+ const now = this.clock.now()
132
+ const recent = (this.hits.get(key) ?? []).filter((t) => now - t < this.windowMs)
133
+ recent.push(now)
134
+ this.hits.set(key, recent)
135
+ if (this.hits.size > 512) {
136
+ for (const [k, v] of this.hits)
137
+ if (v.every((t) => now - t >= this.windowMs)) this.hits.delete(k)
138
+ }
139
+ return recent.length
140
+ }
141
+ }
142
+
143
+ export const HOLD_EXPIRY_MS = 10 * 60 * 1000
144
+ export const MAX_HELD_PER_TERMINAL = 8
145
+
146
+ export interface HoldQueueOptions<T> {
147
+ readonly clock: NetworkClock
148
+ readonly expiryMs?: number
149
+ readonly capacity?: number
150
+ readonly onExpire: (id: string, item: T) => void
151
+ }
152
+
153
+ export interface HeldEntry<T> {
154
+ readonly id: string
155
+ readonly item: T
156
+ readonly heldAt: number
157
+ readonly expiresAt: number
158
+ }
159
+
160
+ /** Mail waiting for the person. Holds nothing past its expiry. */
161
+ export class HoldQueue<T> {
162
+ private readonly entries = new Map<string, HeldEntry<T> & { cancel: () => void }>()
163
+ constructor(private readonly options: HoldQueueOptions<T>) {}
164
+
165
+ /** False when the queue is full; the caller refuses instead. */
166
+ hold(id: string, item: T): boolean {
167
+ if (this.entries.has(id)) return true
168
+ if (this.entries.size >= (this.options.capacity ?? MAX_HELD_PER_TERMINAL)) return false
169
+ const heldAt = this.options.clock.now()
170
+ const expiryMs = this.options.expiryMs ?? HOLD_EXPIRY_MS
171
+ const cancel = this.options.clock.setTimer(() => {
172
+ if (this.entries.delete(id)) this.options.onExpire(id, item)
173
+ }, expiryMs)
174
+ this.entries.set(id, { id, item, heldAt, expiresAt: heldAt + expiryMs, cancel })
175
+ return true
176
+ }
177
+
178
+ /** Take an entry out of the queue (allow or deny). */
179
+ release(id: string): HeldEntry<T> | undefined {
180
+ const entry = this.entries.get(id)
181
+ if (!entry) return undefined
182
+ entry.cancel()
183
+ this.entries.delete(id)
184
+ return entry
185
+ }
186
+
187
+ list(): readonly HeldEntry<T>[] {
188
+ return [...this.entries.values()]
189
+ }
190
+
191
+ get size(): number {
192
+ return this.entries.size
193
+ }
194
+
195
+ clear(): void {
196
+ for (const e of this.entries.values()) e.cancel()
197
+ this.entries.clear()
198
+ }
199
+ }
@@ -0,0 +1,290 @@
1
+ /**
2
+ * Membership of a Namzu network as signed statements, not a shared secret.
3
+ *
4
+ * The founder signs a statement admitting itself; every other machine is in
5
+ * because an existing member signed an admission for it. A revocation is a
6
+ * statement signed by a member. There is no network-wide key, so a stolen
7
+ * machine can only vouch for machines until it is removed.
8
+ *
9
+ * This module is pure: no I/O, no clock. Every function takes the statements
10
+ * and returns a verdict. Merging two sets of statements is a union followed by
11
+ * re-validation, which is independent of the order the statements arrive in.
12
+ *
13
+ * Canonical bytes are the UTF-8 of a JSON array with a fixed field order,
14
+ * prefixed by a domain string, so a signature for one kind of statement cannot
15
+ * be replayed as another.
16
+ *
17
+ * @experimental
18
+ */
19
+
20
+ import { z } from 'zod'
21
+ import { type MachineKey, machineIdOf, netIdOf, signBytes, verifyBytes } from './identity.js'
22
+
23
+ const HEX = /^[0-9a-f]+$/
24
+
25
+ const MachineIdSchema = z.string().length(64).regex(HEX)
26
+
27
+ export const AdmissionSchema = z
28
+ .object({
29
+ netId: z.string().length(16).regex(HEX),
30
+ machineId: MachineIdSchema,
31
+ /** SPKI DER, base64. */
32
+ publicKey: z.string().min(1).max(256),
33
+ admittedBy: MachineIdSchema,
34
+ admittedAt: z.number().int().nonnegative(),
35
+ signature: z.string().min(1).max(256),
36
+ })
37
+ .strict()
38
+ export type Admission = z.infer<typeof AdmissionSchema>
39
+
40
+ export const RevocationSchema = z
41
+ .object({
42
+ netId: z.string().length(16).regex(HEX),
43
+ machineId: MachineIdSchema,
44
+ revokedBy: MachineIdSchema,
45
+ revokedAt: z.number().int().nonnegative(),
46
+ cascade: z.boolean(),
47
+ signature: z.string().min(1).max(256),
48
+ })
49
+ .strict()
50
+ export type Revocation = z.infer<typeof RevocationSchema>
51
+
52
+ export const ADMIT_DOMAIN = 'namzu-net/admit/1'
53
+ export const REVOKE_DOMAIN = 'namzu-net/revoke/1'
54
+
55
+ export function canonicalAdmission(a: Omit<Admission, 'signature'>): Buffer {
56
+ return Buffer.from(
57
+ `${ADMIT_DOMAIN}\n${JSON.stringify([a.netId, a.machineId, a.publicKey, a.admittedBy, a.admittedAt])}`,
58
+ 'utf8',
59
+ )
60
+ }
61
+
62
+ export function canonicalRevocation(r: Omit<Revocation, 'signature'>): Buffer {
63
+ return Buffer.from(
64
+ `${REVOKE_DOMAIN}\n${JSON.stringify([r.netId, r.machineId, r.revokedBy, r.revokedAt, r.cascade])}`,
65
+ 'utf8',
66
+ )
67
+ }
68
+
69
+ /** The founding statement: self-signed, and the root every chain must reach. */
70
+ export function createFounding(key: MachineKey, now: number): Admission {
71
+ const body = {
72
+ netId: netIdOf(key.publicKeyDer),
73
+ machineId: key.machineId,
74
+ publicKey: key.publicKeyDer.toString('base64'),
75
+ admittedBy: key.machineId,
76
+ admittedAt: now,
77
+ }
78
+ return {
79
+ ...body,
80
+ signature: signBytes(key, canonicalAdmission(body)).toString('base64'),
81
+ }
82
+ }
83
+
84
+ export function signAdmission(
85
+ signer: MachineKey,
86
+ netId: string,
87
+ joiner: { readonly machineId: string; readonly publicKey: string },
88
+ now: number,
89
+ ): Admission {
90
+ const body = {
91
+ netId,
92
+ machineId: joiner.machineId,
93
+ publicKey: joiner.publicKey,
94
+ admittedBy: signer.machineId,
95
+ admittedAt: now,
96
+ }
97
+ return {
98
+ ...body,
99
+ signature: signBytes(signer, canonicalAdmission(body)).toString('base64'),
100
+ }
101
+ }
102
+
103
+ export function signRevocation(
104
+ signer: MachineKey,
105
+ netId: string,
106
+ machineId: string,
107
+ cascade: boolean,
108
+ now: number,
109
+ ): Revocation {
110
+ const body = {
111
+ netId,
112
+ machineId,
113
+ revokedBy: signer.machineId,
114
+ revokedAt: now,
115
+ cascade,
116
+ }
117
+ return {
118
+ ...body,
119
+ signature: signBytes(signer, canonicalRevocation(body)).toString('base64'),
120
+ }
121
+ }
122
+
123
+ function admissionIntact(a: Admission, signerKeyB64: string): boolean {
124
+ const publicKey = Buffer.from(a.publicKey, 'base64')
125
+ if (machineIdOf(publicKey) !== a.machineId) return false
126
+ return verifyBytes(
127
+ Buffer.from(signerKeyB64, 'base64'),
128
+ canonicalAdmission(a),
129
+ Buffer.from(a.signature, 'base64'),
130
+ )
131
+ }
132
+
133
+ function isFounding(a: Admission): boolean {
134
+ if (a.admittedBy !== a.machineId) return false
135
+ return netIdOf(Buffer.from(a.publicKey, 'base64')) === a.netId
136
+ }
137
+
138
+ export interface MembershipState {
139
+ readonly admissions: readonly Admission[]
140
+ readonly revocations: readonly Revocation[]
141
+ }
142
+
143
+ export interface MembershipView {
144
+ readonly netId: string | undefined
145
+ /** Current members: valid admission, not revoked. */
146
+ readonly members: ReadonlyMap<string, Admission>
147
+ /** Every machine with a valid revocation or removed by a cascade; never to be re-admitted. */
148
+ readonly removed: ReadonlySet<string>
149
+ /** The statements that survived validation, ready to store or send. */
150
+ readonly admissions: readonly Admission[]
151
+ readonly revocations: readonly Revocation[]
152
+ }
153
+
154
+ /**
155
+ * Validate a set of statements for one network (the one whose founding
156
+ * statement is in the set). Invalid and orphan statements are dropped.
157
+ */
158
+ export function evaluateMembership(state: MembershipState, expectedNetId?: string): MembershipView {
159
+ const founding = [...state.admissions]
160
+ .filter((a) => isFounding(a) && (expectedNetId === undefined || a.netId === expectedNetId))
161
+ .filter((a) => admissionIntact(a, a.publicKey))
162
+ .sort((a, b) => a.machineId.localeCompare(b.machineId))[0]
163
+ if (!founding) {
164
+ return {
165
+ netId: undefined,
166
+ members: new Map(),
167
+ removed: new Set(),
168
+ admissions: [],
169
+ revocations: [],
170
+ }
171
+ }
172
+ const netId = founding.netId
173
+ const sameNet = state.admissions.filter((a) => a.netId === netId && a !== founding)
174
+ const revs = state.revocations.filter((r) => r.netId === netId)
175
+
176
+ // Structure first, from signatures alone: who admitted whom. No time in a statement is
177
+ // trusted for ordering, because the signer chooses it (a revoked machine could back-date).
178
+ const valid = new Map<string, Admission>([[founding.machineId, founding]])
179
+ for (let changed = true; changed; ) {
180
+ changed = false
181
+ for (const a of [...sameNet].sort(
182
+ (x, y) => x.admittedAt - y.admittedAt || x.signature.localeCompare(y.signature),
183
+ )) {
184
+ if (valid.has(a.machineId)) continue
185
+ const signer = valid.get(a.admittedBy)
186
+ if (!signer || !admissionIntact(a, signer.publicKey)) continue
187
+ valid.set(a.machineId, a)
188
+ changed = true
189
+ }
190
+ }
191
+ const childrenOf = new Map<string, string[]>()
192
+ for (const [id, adm] of valid)
193
+ if (id !== adm.admittedBy)
194
+ childrenOf.set(adm.admittedBy, [...(childrenOf.get(adm.admittedBy) ?? []), id])
195
+ const ancestorsOf = (id: string): Set<string> => {
196
+ const out = new Set<string>()
197
+ for (
198
+ let cur = valid.get(id);
199
+ cur && cur.admittedBy !== cur.machineId;
200
+ cur = valid.get(cur.admittedBy)
201
+ )
202
+ out.add(cur.admittedBy)
203
+ return out
204
+ }
205
+ const descendantsOf = (id: string): string[] => {
206
+ const out: string[] = []
207
+ const queue = [...(childrenOf.get(id) ?? [])]
208
+ while (queue.length > 0) {
209
+ const next = queue.pop() as string
210
+ out.push(next)
211
+ queue.push(...(childrenOf.get(next) ?? []))
212
+ }
213
+ return out
214
+ }
215
+
216
+ // A revocation always takes along everything the machine admitted (a stolen machine can
217
+ // not keep vouching for machines), and a machine can never remove one above it in its own
218
+ // admission chain. Revocations from above are applied first; lateral ones follow in a
219
+ // fixed order and only from a signer that is still a member.
220
+ const removed = new Set<string>()
221
+ const validRevs = new Map<string, Revocation>()
222
+ const sigOk = (r: Revocation) => {
223
+ const signer = valid.get(r.revokedBy)
224
+ return (
225
+ signer !== undefined &&
226
+ valid.has(r.machineId) &&
227
+ verifyBytes(
228
+ Buffer.from(signer.publicKey, 'base64'),
229
+ canonicalRevocation(r),
230
+ Buffer.from(r.signature, 'base64'),
231
+ )
232
+ )
233
+ }
234
+ const apply = (r: Revocation) => {
235
+ if (!validRevs.has(r.machineId)) validRevs.set(r.machineId, r)
236
+ removed.add(r.machineId)
237
+ for (const d of descendantsOf(r.machineId)) removed.add(d)
238
+ }
239
+ const candidates = revs
240
+ .filter(sigOk)
241
+ .filter((r) => !ancestorsOf(r.revokedBy).has(r.machineId))
242
+ .sort((x, y) => x.revokedAt - y.revokedAt || x.signature.localeCompare(y.signature))
243
+ const above = candidates.filter((r) => ancestorsOf(r.machineId).has(r.revokedBy))
244
+ for (const r of above) if (!removed.has(r.revokedBy)) apply(r)
245
+ for (const r of candidates) {
246
+ if (above.includes(r) || removed.has(r.revokedBy)) continue
247
+ apply(r)
248
+ }
249
+ const members = new Map([...valid].filter(([id]) => !removed.has(id)))
250
+ return {
251
+ netId,
252
+ members,
253
+ removed,
254
+ admissions: [...valid.values()],
255
+ revocations: [...validRevs.values()],
256
+ }
257
+ }
258
+
259
+ /** Union of two sets of statements, re-validated. Order of arguments does not matter. */
260
+ export function mergeMembership(
261
+ a: MembershipState,
262
+ b: MembershipState,
263
+ expectedNetId?: string,
264
+ ): MembershipView {
265
+ const adm = new Map<string, Admission>()
266
+ for (const x of [...a.admissions, ...b.admissions]) adm.set(x.signature, x)
267
+ const rev = new Map<string, Revocation>()
268
+ for (const x of [...a.revocations, ...b.revocations]) rev.set(x.signature, x)
269
+ return evaluateMembership(
270
+ { admissions: [...adm.values()], revocations: [...rev.values()] },
271
+ expectedNetId,
272
+ )
273
+ }
274
+
275
+ /** The machines `machineId` admitted that are still members: what `remove` lists before a cascade. */
276
+ export function admittedBy(view: MembershipView, machineId: string): string[] {
277
+ return [...view.members.values()]
278
+ .filter((m) => m.admittedBy === machineId && m.machineId !== machineId)
279
+ .map((m) => m.machineId)
280
+ }
281
+
282
+ /** Whether `candidate` is a statement that would be dropped; used to reject bad input at the wire. */
283
+ export function parseAdmission(value: unknown): Admission | undefined {
284
+ const r = AdmissionSchema.safeParse(value)
285
+ return r.success ? r.data : undefined
286
+ }
287
+ export function parseRevocation(value: unknown): Revocation | undefined {
288
+ const r = RevocationSchema.safeParse(value)
289
+ return r.success ? r.data : undefined
290
+ }
@@ -0,0 +1,165 @@
1
+ /**
2
+ * What a person sees of their machines: a roster of machines, each with its
3
+ * live sessions. The name is the address: a session is reached by its name,
4
+ * with a short ref added to a row only when two rows share a name.
5
+ *
6
+ * Session names are the session's title, or a readable `<adjective>-<noun>`
7
+ * derived from its id so the name is stable for the life of the session.
8
+ *
9
+ * @experimental
10
+ */
11
+
12
+ import { createHash } from 'node:crypto'
13
+ import type { RosterSession } from './wire.js'
14
+
15
+ const ADJECTIVES = [
16
+ 'calm',
17
+ 'quiet',
18
+ 'swift',
19
+ 'brisk',
20
+ 'bright',
21
+ 'gentle',
22
+ 'steady',
23
+ 'keen',
24
+ 'merry',
25
+ 'nimble',
26
+ 'proud',
27
+ 'rustic',
28
+ 'sunny',
29
+ 'tidy',
30
+ 'warm',
31
+ 'witty',
32
+ ] as const
33
+ const NOUNS = [
34
+ 'heron',
35
+ 'otter',
36
+ 'finch',
37
+ 'marten',
38
+ 'badger',
39
+ 'falcon',
40
+ 'lynx',
41
+ 'wren',
42
+ 'maple',
43
+ 'cedar',
44
+ 'harbor',
45
+ 'meadow',
46
+ 'comet',
47
+ 'ember',
48
+ 'river',
49
+ 'summit',
50
+ ] as const
51
+
52
+ /** A readable name chosen deterministically from the session id. */
53
+ export function generateSessionName(sessionId: string): string {
54
+ const digest = createHash('sha256').update(sessionId, 'utf8').digest()
55
+ return `${ADJECTIVES[(digest[0] as number) % ADJECTIVES.length]}-${NOUNS[(digest[1] as number) % NOUNS.length]}`
56
+ }
57
+
58
+ export function sessionDisplayName(session: {
59
+ readonly sessionId: string
60
+ readonly title?: string
61
+ }): string {
62
+ const title = session.title?.trim()
63
+ return title ? title : generateSessionName(session.sessionId)
64
+ }
65
+
66
+ /** A live session of this machine, as a host reports it. `conversationId` never goes on the wire. */
67
+ export interface LocalSession extends RosterSession {
68
+ readonly conversationId?: string
69
+ /** The terminal is in a mode that skips permission prompts; every message to it is held. Never on the wire. */
70
+ readonly bypass?: boolean
71
+ }
72
+
73
+ export type MachineStatus = 'self' | 'online' | 'offline'
74
+
75
+ export interface RosterMachineView {
76
+ readonly machineId: string
77
+ readonly name: string
78
+ readonly status: MachineStatus
79
+ /** Epoch ms of the last answer; absent for this machine. */
80
+ readonly seenAt?: number
81
+ readonly role?: string
82
+ readonly sessions: readonly RosterSession[]
83
+ }
84
+
85
+ export interface RosterView {
86
+ readonly machines: readonly RosterMachineView[]
87
+ }
88
+
89
+ /** The name to show for a machine: its own name, plus six hex of its id when another machine has the same name. */
90
+ export function machineLabel(view: RosterView, machine: RosterMachineView): string {
91
+ const same = view.machines.filter((m) => m.name === machine.name).length > 1
92
+ return same ? `${machine.name} (${machine.machineId.slice(0, 6)})` : machine.name
93
+ }
94
+
95
+ /** The name to show for a session: its name, plus `[ref]` only when two sessions in the roster share it. */
96
+ export function sessionLabel(
97
+ view: RosterView,
98
+ machine: RosterMachineView,
99
+ session: RosterSession,
100
+ ): string {
101
+ let count = 0
102
+ for (const m of view.machines) for (const s of m.sessions) if (s.name === session.name) count++
103
+ const sameMachine = machine.sessions.filter((s) => s.name === session.name).length > 1
104
+ return count > 1 && (sameMachine || view.machines.length > 1)
105
+ ? `${session.name} [${session.ref}]`
106
+ : session.name
107
+ }
108
+
109
+ export type TargetResolution =
110
+ | { readonly ok: true; readonly machine: RosterMachineView; readonly session: RosterSession }
111
+ | {
112
+ readonly ok: false
113
+ readonly reason: 'none' | 'ambiguous'
114
+ readonly candidates: readonly string[]
115
+ }
116
+
117
+ /**
118
+ * Resolve what a person typed. Accepts `machine/session`, a session name, or a
119
+ * `[ref]` / bare ref. Offline machines are not targets.
120
+ */
121
+ export function resolveSessionTarget(view: RosterView, text: string): TargetResolution {
122
+ const wanted = text.trim()
123
+ const slash = wanted.indexOf('/')
124
+ const machinePart = slash >= 0 ? wanted.slice(0, slash).trim().toLowerCase() : undefined
125
+ const sessionPart = (slash >= 0 ? wanted.slice(slash + 1) : wanted).trim()
126
+ const bracket = /^(.*?)\s*\[([0-9a-f]{6})\]$/.exec(sessionPart)
127
+ const wantedName = (bracket ? (bracket[1] as string) : sessionPart).trim().toLowerCase()
128
+ const wantedRef = bracket
129
+ ? (bracket[2] as string)
130
+ : /^[0-9a-f]{6}$/.test(sessionPart)
131
+ ? sessionPart
132
+ : undefined
133
+ const hits: { machine: RosterMachineView; session: RosterSession }[] = []
134
+ for (const machine of view.machines) {
135
+ if (machine.status === 'offline') continue
136
+ if (machinePart !== undefined && machine.name.toLowerCase() !== machinePart) continue
137
+ for (const session of machine.sessions) {
138
+ const nameOk = wantedName.length > 0 && session.name.toLowerCase() === wantedName
139
+ const refOk = wantedRef !== undefined && session.ref === wantedRef
140
+ if (bracket ? refOk && (wantedName.length === 0 || nameOk) : nameOk || refOk)
141
+ hits.push({ machine, session })
142
+ }
143
+ }
144
+ const unique = [
145
+ ...new Map(hits.map((h) => [`${h.machine.machineId}/${h.session.ref}`, h])).values(),
146
+ ]
147
+ if (unique.length === 1) return { ok: true, ...(unique[0] as (typeof unique)[number]) }
148
+ return {
149
+ ok: false,
150
+ reason: unique.length === 0 ? 'none' : 'ambiguous',
151
+ candidates: unique.map((h) => `${h.machine.name}/${h.session.name} [${h.session.ref}]`),
152
+ }
153
+ }
154
+
155
+ /** The most a member's last roster is believed once it stops answering. */
156
+ export const ROSTER_CACHE_MS = 24 * 60 * 60 * 1000
157
+
158
+ /** Apply the offline rule: a member that did not answer keeps its cached sessions for 24 hours, shown offline. */
159
+ export function offlineSessions(
160
+ sessions: readonly RosterSession[],
161
+ seenAt: number,
162
+ now: number,
163
+ ): readonly RosterSession[] {
164
+ return now - seenAt > ROSTER_CACHE_MS ? [] : sessions
165
+ }