harnery 0.0.1 → 0.2.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 (445) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +84 -2
  3. package/bin/agent-coord +42 -0
  4. package/bin/agent-hook +44 -0
  5. package/bin/harn +40 -0
  6. package/dist/cli.d.ts +9 -0
  7. package/dist/cli.d.ts.map +1 -0
  8. package/dist/cli.js +18 -0
  9. package/dist/commander.d.ts +128 -0
  10. package/dist/commander.d.ts.map +1 -0
  11. package/dist/commander.js +126 -0
  12. package/dist/commands/agents.d.ts +18 -0
  13. package/dist/commands/agents.d.ts.map +1 -0
  14. package/dist/commands/agents.js +3946 -0
  15. package/dist/commands/backup.d.ts +22 -0
  16. package/dist/commands/backup.d.ts.map +1 -0
  17. package/dist/commands/backup.js +262 -0
  18. package/dist/commands/browse-ai.d.ts +4 -0
  19. package/dist/commands/browse-ai.d.ts.map +1 -0
  20. package/dist/commands/browse-ai.js +156 -0
  21. package/dist/commands/browse.d.ts +4 -0
  22. package/dist/commands/browse.d.ts.map +1 -0
  23. package/dist/commands/browse.js +590 -0
  24. package/dist/commands/callers.d.ts +4 -0
  25. package/dist/commands/callers.d.ts.map +1 -0
  26. package/dist/commands/callers.js +276 -0
  27. package/dist/commands/completion.d.ts +17 -0
  28. package/dist/commands/completion.d.ts.map +1 -0
  29. package/dist/commands/completion.js +158 -0
  30. package/dist/commands/config-get.d.ts +4 -0
  31. package/dist/commands/config-get.d.ts.map +1 -0
  32. package/dist/commands/config-get.js +131 -0
  33. package/dist/commands/context.d.ts +11 -0
  34. package/dist/commands/context.d.ts.map +1 -0
  35. package/dist/commands/context.js +185 -0
  36. package/dist/commands/cookies.d.ts +4 -0
  37. package/dist/commands/cookies.d.ts.map +1 -0
  38. package/dist/commands/cookies.js +140 -0
  39. package/dist/commands/docs.d.ts +4 -0
  40. package/dist/commands/docs.d.ts.map +1 -0
  41. package/dist/commands/docs.js +137 -0
  42. package/dist/commands/doctor.d.ts +25 -0
  43. package/dist/commands/doctor.d.ts.map +1 -0
  44. package/dist/commands/doctor.js +200 -0
  45. package/dist/commands/edit-batch.d.ts +18 -0
  46. package/dist/commands/edit-batch.d.ts.map +1 -0
  47. package/dist/commands/edit-batch.js +172 -0
  48. package/dist/commands/eml.d.ts +4 -0
  49. package/dist/commands/eml.d.ts.map +1 -0
  50. package/dist/commands/eml.js +428 -0
  51. package/dist/commands/env.d.ts +4 -0
  52. package/dist/commands/env.d.ts.map +1 -0
  53. package/dist/commands/env.js +201 -0
  54. package/dist/commands/fetch.d.ts +4 -0
  55. package/dist/commands/fetch.d.ts.map +1 -0
  56. package/dist/commands/fetch.js +99 -0
  57. package/dist/commands/file-history.d.ts +4 -0
  58. package/dist/commands/file-history.d.ts.map +1 -0
  59. package/dist/commands/file-history.js +152 -0
  60. package/dist/commands/grep.d.ts +4 -0
  61. package/dist/commands/grep.d.ts.map +1 -0
  62. package/dist/commands/grep.js +317 -0
  63. package/dist/commands/init.d.ts +82 -0
  64. package/dist/commands/init.d.ts.map +1 -0
  65. package/dist/commands/init.js +288 -0
  66. package/dist/commands/outline.d.ts +4 -0
  67. package/dist/commands/outline.d.ts.map +1 -0
  68. package/dist/commands/outline.js +494 -0
  69. package/dist/commands/presence.d.ts +12 -0
  70. package/dist/commands/presence.d.ts.map +1 -0
  71. package/dist/commands/presence.js +123 -0
  72. package/dist/commands/read.d.ts +7 -0
  73. package/dist/commands/read.d.ts.map +1 -0
  74. package/dist/commands/read.js +46 -0
  75. package/dist/commands/scratch.d.ts +4 -0
  76. package/dist/commands/scratch.d.ts.map +1 -0
  77. package/dist/commands/scratch.js +426 -0
  78. package/dist/commands/session.d.ts +4 -0
  79. package/dist/commands/session.d.ts.map +1 -0
  80. package/dist/commands/session.js +162 -0
  81. package/dist/commands/sync.d.ts +24 -0
  82. package/dist/commands/sync.d.ts.map +1 -0
  83. package/dist/commands/sync.js +275 -0
  84. package/dist/commands/toc.d.ts +5 -0
  85. package/dist/commands/toc.d.ts.map +1 -0
  86. package/dist/commands/toc.js +153 -0
  87. package/dist/commands/tokens.d.ts +4 -0
  88. package/dist/commands/tokens.d.ts.map +1 -0
  89. package/dist/commands/tokens.js +48 -0
  90. package/dist/commands/tunnel.d.ts +4 -0
  91. package/dist/commands/tunnel.d.ts.map +1 -0
  92. package/dist/commands/tunnel.js +513 -0
  93. package/dist/commands/uninstall.d.ts +22 -0
  94. package/dist/commands/uninstall.d.ts.map +1 -0
  95. package/dist/commands/uninstall.js +126 -0
  96. package/dist/commands/web.d.ts +4 -0
  97. package/dist/commands/web.d.ts.map +1 -0
  98. package/dist/commands/web.js +165 -0
  99. package/dist/core/agents/canonical-emit.d.ts +27 -0
  100. package/dist/core/agents/canonical-emit.d.ts.map +1 -0
  101. package/dist/core/agents/canonical-emit.js +72 -0
  102. package/dist/core/agents/cli-emit.d.ts +27 -0
  103. package/dist/core/agents/cli-emit.d.ts.map +1 -0
  104. package/dist/core/agents/cli-emit.js +57 -0
  105. package/dist/core/agents/cli.d.ts +10 -0
  106. package/dist/core/agents/cli.d.ts.map +1 -0
  107. package/dist/core/agents/cli.js +757 -0
  108. package/dist/core/agents/codex-replay.d.ts +29 -0
  109. package/dist/core/agents/codex-replay.d.ts.map +1 -0
  110. package/dist/core/agents/codex-replay.js +138 -0
  111. package/dist/core/agents/coord-client.d.ts +98 -0
  112. package/dist/core/agents/coord-client.d.ts.map +1 -0
  113. package/dist/core/agents/coord-client.js +212 -0
  114. package/dist/core/agents/events/consume.d.ts +59 -0
  115. package/dist/core/agents/events/consume.d.ts.map +1 -0
  116. package/dist/core/agents/events/consume.js +147 -0
  117. package/dist/core/agents/events/emit.d.ts +42 -0
  118. package/dist/core/agents/events/emit.d.ts.map +1 -0
  119. package/dist/core/agents/events/emit.js +70 -0
  120. package/dist/core/agents/events/ulid.d.ts +11 -0
  121. package/dist/core/agents/events/ulid.d.ts.map +1 -0
  122. package/dist/core/agents/events/ulid.js +47 -0
  123. package/dist/core/agents/index.d.ts +14 -0
  124. package/dist/core/agents/index.d.ts.map +1 -0
  125. package/dist/core/agents/index.js +13 -0
  126. package/dist/core/agents/paths.d.ts +6 -0
  127. package/dist/core/agents/paths.d.ts.map +1 -0
  128. package/dist/core/agents/paths.js +17 -0
  129. package/dist/core/agents/render/prompt-context.d.ts +43 -0
  130. package/dist/core/agents/render/prompt-context.d.ts.map +1 -0
  131. package/dist/core/agents/render/prompt-context.js +335 -0
  132. package/dist/core/agents/render/session-context.d.ts +39 -0
  133. package/dist/core/agents/render/session-context.d.ts.map +1 -0
  134. package/dist/core/agents/render/session-context.js +283 -0
  135. package/dist/core/agents/rules/claim-conflict.d.ts +35 -0
  136. package/dist/core/agents/rules/claim-conflict.d.ts.map +1 -0
  137. package/dist/core/agents/rules/claim-conflict.js +244 -0
  138. package/dist/core/agents/rules/commit-conflict.d.ts +59 -0
  139. package/dist/core/agents/rules/commit-conflict.d.ts.map +1 -0
  140. package/dist/core/agents/rules/commit-conflict.js +244 -0
  141. package/dist/core/agents/rules/stop-hook.d.ts +44 -0
  142. package/dist/core/agents/rules/stop-hook.d.ts.map +1 -0
  143. package/dist/core/agents/rules/stop-hook.js +161 -0
  144. package/dist/core/agents/session-events.d.ts +41 -0
  145. package/dist/core/agents/session-events.d.ts.map +1 -0
  146. package/dist/core/agents/session-events.js +205 -0
  147. package/dist/core/agents/state/activity-log.d.ts +18 -0
  148. package/dist/core/agents/state/activity-log.d.ts.map +1 -0
  149. package/dist/core/agents/state/activity-log.js +34 -0
  150. package/dist/core/agents/state/council.d.ts +39 -0
  151. package/dist/core/agents/state/council.d.ts.map +1 -0
  152. package/dist/core/agents/state/council.js +216 -0
  153. package/dist/core/agents/state/heartbeat-projector.d.ts +59 -0
  154. package/dist/core/agents/state/heartbeat-projector.d.ts.map +1 -0
  155. package/dist/core/agents/state/heartbeat-projector.js +436 -0
  156. package/dist/core/agents/state/heartbeat-writer.d.ts +64 -0
  157. package/dist/core/agents/state/heartbeat-writer.d.ts.map +1 -0
  158. package/dist/core/agents/state/heartbeat-writer.js +271 -0
  159. package/dist/core/agents/state/names.d.ts +35 -0
  160. package/dist/core/agents/state/names.d.ts.map +1 -0
  161. package/dist/core/agents/state/names.js +376 -0
  162. package/dist/core/agents/state/pidmap.d.ts +11 -0
  163. package/dist/core/agents/state/pidmap.d.ts.map +1 -0
  164. package/dist/core/agents/state/pidmap.js +32 -0
  165. package/dist/core/agents/state/scratch.d.ts +27 -0
  166. package/dist/core/agents/state/scratch.d.ts.map +1 -0
  167. package/dist/core/agents/state/scratch.js +90 -0
  168. package/dist/core/agents/state/shell-mutation.d.ts +17 -0
  169. package/dist/core/agents/state/shell-mutation.d.ts.map +1 -0
  170. package/dist/core/agents/state/shell-mutation.js +41 -0
  171. package/dist/core/agents/state/stale-sweep.d.ts +16 -0
  172. package/dist/core/agents/state/stale-sweep.d.ts.map +1 -0
  173. package/dist/core/agents/state/stale-sweep.js +166 -0
  174. package/dist/core/config.d.ts +29 -0
  175. package/dist/core/config.d.ts.map +1 -0
  176. package/dist/core/config.js +108 -0
  177. package/dist/core/hooks/cli.d.ts +21 -0
  178. package/dist/core/hooks/cli.d.ts.map +1 -0
  179. package/dist/core/hooks/cli.js +1123 -0
  180. package/dist/core/hooks/effects/image-capture.d.ts +43 -0
  181. package/dist/core/hooks/effects/image-capture.d.ts.map +1 -0
  182. package/dist/core/hooks/effects/image-capture.js +288 -0
  183. package/dist/core/hooks/effects/index.d.ts +64 -0
  184. package/dist/core/hooks/effects/index.d.ts.map +1 -0
  185. package/dist/core/hooks/effects/index.js +197 -0
  186. package/dist/core/hooks/events/emit.d.ts +31 -0
  187. package/dist/core/hooks/events/emit.d.ts.map +1 -0
  188. package/dist/core/hooks/events/emit.js +89 -0
  189. package/dist/core/hooks/events/schema.d.ts +235 -0
  190. package/dist/core/hooks/events/schema.d.ts.map +1 -0
  191. package/dist/core/hooks/events/schema.js +12 -0
  192. package/dist/core/hooks/events/ulid.d.ts +10 -0
  193. package/dist/core/hooks/events/ulid.d.ts.map +1 -0
  194. package/dist/core/hooks/events/ulid.js +47 -0
  195. package/dist/core/hooks/harness/detect.d.ts +9 -0
  196. package/dist/core/hooks/harness/detect.d.ts.map +1 -0
  197. package/dist/core/hooks/harness/detect.js +29 -0
  198. package/dist/core/hooks/harness/events.d.ts +45 -0
  199. package/dist/core/hooks/harness/events.d.ts.map +1 -0
  200. package/dist/core/hooks/harness/events.js +71 -0
  201. package/dist/core/hooks/harness/output.d.ts +46 -0
  202. package/dist/core/hooks/harness/output.d.ts.map +1 -0
  203. package/dist/core/hooks/harness/output.js +87 -0
  204. package/dist/core/hooks/harness/parse.d.ts +67 -0
  205. package/dist/core/hooks/harness/parse.d.ts.map +1 -0
  206. package/dist/core/hooks/harness/parse.js +132 -0
  207. package/dist/core/hooks/index.d.ts +8 -0
  208. package/dist/core/hooks/index.d.ts.map +1 -0
  209. package/dist/core/hooks/index.js +7 -0
  210. package/dist/core/hooks/resolve/anchor.d.ts +37 -0
  211. package/dist/core/hooks/resolve/anchor.d.ts.map +1 -0
  212. package/dist/core/hooks/resolve/anchor.js +48 -0
  213. package/dist/core/hooks/resolve/coord-root.d.ts +6 -0
  214. package/dist/core/hooks/resolve/coord-root.d.ts.map +1 -0
  215. package/dist/core/hooks/resolve/coord-root.js +27 -0
  216. package/dist/core/hooks/resolve/intent.d.ts +33 -0
  217. package/dist/core/hooks/resolve/intent.d.ts.map +1 -0
  218. package/dist/core/hooks/resolve/intent.js +79 -0
  219. package/dist/core/hooks/resolve/owner.d.ts +42 -0
  220. package/dist/core/hooks/resolve/owner.d.ts.map +1 -0
  221. package/dist/core/hooks/resolve/owner.js +140 -0
  222. package/dist/core/hooks/resolve/transcript.d.ts +26 -0
  223. package/dist/core/hooks/resolve/transcript.d.ts.map +1 -0
  224. package/dist/core/hooks/resolve/transcript.js +73 -0
  225. package/dist/index.d.ts +15 -0
  226. package/dist/index.d.ts.map +1 -0
  227. package/dist/index.js +13 -0
  228. package/dist/lib/agent-browser/client.d.ts +99 -0
  229. package/dist/lib/agent-browser/client.d.ts.map +1 -0
  230. package/dist/lib/agent-browser/client.js +177 -0
  231. package/dist/lib/agent-browser/index.d.ts +2 -0
  232. package/dist/lib/agent-browser/index.d.ts.map +1 -0
  233. package/dist/lib/agent-browser/index.js +1 -0
  234. package/dist/lib/browser/client.d.ts +193 -0
  235. package/dist/lib/browser/client.d.ts.map +1 -0
  236. package/dist/lib/browser/client.js +325 -0
  237. package/dist/lib/browser/dev-overlay.d.ts +23 -0
  238. package/dist/lib/browser/dev-overlay.d.ts.map +1 -0
  239. package/dist/lib/browser/dev-overlay.js +153 -0
  240. package/dist/lib/browser/index.d.ts +5 -0
  241. package/dist/lib/browser/index.d.ts.map +1 -0
  242. package/dist/lib/browser/index.js +2 -0
  243. package/dist/lib/browser/layout.d.ts +79 -0
  244. package/dist/lib/browser/layout.d.ts.map +1 -0
  245. package/dist/lib/browser/layout.js +220 -0
  246. package/dist/lib/browser/visibility.d.ts +86 -0
  247. package/dist/lib/browser/visibility.d.ts.map +1 -0
  248. package/dist/lib/browser/visibility.js +333 -0
  249. package/dist/lib/browser/visual-diff.d.ts +38 -0
  250. package/dist/lib/browser/visual-diff.d.ts.map +1 -0
  251. package/dist/lib/browser/visual-diff.js +107 -0
  252. package/dist/lib/completion/bash.d.ts +25 -0
  253. package/dist/lib/completion/bash.d.ts.map +1 -0
  254. package/dist/lib/completion/bash.js +284 -0
  255. package/dist/lib/completion/fish.d.ts +16 -0
  256. package/dist/lib/completion/fish.d.ts.map +1 -0
  257. package/dist/lib/completion/fish.js +118 -0
  258. package/dist/lib/completion/index.d.ts +5 -0
  259. package/dist/lib/completion/index.d.ts.map +1 -0
  260. package/dist/lib/completion/index.js +4 -0
  261. package/dist/lib/completion/walk.d.ts +68 -0
  262. package/dist/lib/completion/walk.d.ts.map +1 -0
  263. package/dist/lib/completion/walk.js +102 -0
  264. package/dist/lib/completion/zsh.d.ts +13 -0
  265. package/dist/lib/completion/zsh.d.ts.map +1 -0
  266. package/dist/lib/completion/zsh.js +249 -0
  267. package/dist/lib/context/index.d.ts +107 -0
  268. package/dist/lib/context/index.d.ts.map +1 -0
  269. package/dist/lib/context/index.js +275 -0
  270. package/dist/lib/cookies/client.d.ts +131 -0
  271. package/dist/lib/cookies/client.d.ts.map +1 -0
  272. package/dist/lib/cookies/client.js +239 -0
  273. package/dist/lib/cookies/index.d.ts +2 -0
  274. package/dist/lib/cookies/index.d.ts.map +1 -0
  275. package/dist/lib/cookies/index.js +1 -0
  276. package/dist/lib/council/index.d.ts +266 -0
  277. package/dist/lib/council/index.d.ts.map +1 -0
  278. package/dist/lib/council/index.js +674 -0
  279. package/dist/lib/docs-index.d.ts +28 -0
  280. package/dist/lib/docs-index.d.ts.map +1 -0
  281. package/dist/lib/docs-index.js +169 -0
  282. package/dist/lib/docs-lint.d.ts +26 -0
  283. package/dist/lib/docs-lint.d.ts.map +1 -0
  284. package/dist/lib/docs-lint.js +378 -0
  285. package/dist/lib/docs-sweep.d.ts +34 -0
  286. package/dist/lib/docs-sweep.d.ts.map +1 -0
  287. package/dist/lib/docs-sweep.js +304 -0
  288. package/dist/lib/docs.d.ts +27 -0
  289. package/dist/lib/docs.d.ts.map +1 -0
  290. package/dist/lib/docs.js +142 -0
  291. package/dist/lib/env.d.ts +11 -0
  292. package/dist/lib/env.d.ts.map +1 -0
  293. package/dist/lib/env.js +12 -0
  294. package/dist/lib/exec.d.ts +32 -0
  295. package/dist/lib/exec.d.ts.map +1 -0
  296. package/dist/lib/exec.js +54 -0
  297. package/dist/lib/format.d.ts +29 -0
  298. package/dist/lib/format.d.ts.map +1 -0
  299. package/dist/lib/format.js +139 -0
  300. package/dist/lib/http/client.d.ts +56 -0
  301. package/dist/lib/http/client.d.ts.map +1 -0
  302. package/dist/lib/http/client.js +160 -0
  303. package/dist/lib/http/index.d.ts +2 -0
  304. package/dist/lib/http/index.d.ts.map +1 -0
  305. package/dist/lib/http/index.js +1 -0
  306. package/dist/lib/identities/index.d.ts +77 -0
  307. package/dist/lib/identities/index.d.ts.map +1 -0
  308. package/dist/lib/identities/index.js +190 -0
  309. package/dist/lib/machine.d.ts +19 -0
  310. package/dist/lib/machine.d.ts.map +1 -0
  311. package/dist/lib/machine.js +61 -0
  312. package/dist/lib/presence.d.ts +48 -0
  313. package/dist/lib/presence.d.ts.map +1 -0
  314. package/dist/lib/presence.js +123 -0
  315. package/dist/lib/readability/client.d.ts +32 -0
  316. package/dist/lib/readability/client.d.ts.map +1 -0
  317. package/dist/lib/readability/client.js +119 -0
  318. package/dist/lib/readability/index.d.ts +2 -0
  319. package/dist/lib/readability/index.d.ts.map +1 -0
  320. package/dist/lib/readability/index.js +1 -0
  321. package/dist/lib/scratch/index.d.ts +74 -0
  322. package/dist/lib/scratch/index.d.ts.map +1 -0
  323. package/dist/lib/scratch/index.js +393 -0
  324. package/dist/lib/tunnel/gate.d.ts +12 -0
  325. package/dist/lib/tunnel/gate.d.ts.map +1 -0
  326. package/dist/lib/tunnel/gate.js +101 -0
  327. package/dist/lib/tunnel/state.d.ts +34 -0
  328. package/dist/lib/tunnel/state.d.ts.map +1 -0
  329. package/dist/lib/tunnel/state.js +132 -0
  330. package/package.json +160 -8
  331. package/schemas/.gitkeep +0 -0
  332. package/schemas/config.schema.json +109 -0
  333. package/src/cli.ts +22 -0
  334. package/src/commander.ts +242 -0
  335. package/src/commands/.gitkeep +0 -0
  336. package/src/commands/agents.ts +4567 -0
  337. package/src/commands/backup.ts +305 -0
  338. package/src/commands/browse-ai.ts +198 -0
  339. package/src/commands/browse.ts +849 -0
  340. package/src/commands/callers.ts +363 -0
  341. package/src/commands/completion.ts +193 -0
  342. package/src/commands/config-get.ts +161 -0
  343. package/src/commands/context.ts +209 -0
  344. package/src/commands/cookies.ts +198 -0
  345. package/src/commands/docs.ts +174 -0
  346. package/src/commands/doctor.ts +231 -0
  347. package/src/commands/edit-batch.ts +233 -0
  348. package/src/commands/eml.ts +519 -0
  349. package/src/commands/env.ts +254 -0
  350. package/src/commands/fetch.ts +136 -0
  351. package/src/commands/file-history.ts +202 -0
  352. package/src/commands/grep.ts +371 -0
  353. package/src/commands/init.ts +335 -0
  354. package/src/commands/outline.ts +564 -0
  355. package/src/commands/presence.ts +152 -0
  356. package/src/commands/read.ts +64 -0
  357. package/src/commands/scratch.ts +445 -0
  358. package/src/commands/session.ts +187 -0
  359. package/src/commands/sync.ts +306 -0
  360. package/src/commands/toc.ts +218 -0
  361. package/src/commands/tokens.ts +79 -0
  362. package/src/commands/tunnel.ts +633 -0
  363. package/src/commands/uninstall.ts +144 -0
  364. package/src/commands/web.ts +193 -0
  365. package/src/core/agents/canonical-emit.ts +77 -0
  366. package/src/core/agents/cli-emit.ts +64 -0
  367. package/src/core/agents/cli.ts +838 -0
  368. package/src/core/agents/codex-replay.ts +163 -0
  369. package/src/core/agents/coord-client.ts +249 -0
  370. package/src/core/agents/events/consume.ts +196 -0
  371. package/src/core/agents/events/emit.ts +108 -0
  372. package/src/core/agents/events/ulid.ts +51 -0
  373. package/src/core/agents/index.ts +14 -0
  374. package/src/core/agents/paths.ts +16 -0
  375. package/src/core/agents/render/prompt-context.ts +401 -0
  376. package/src/core/agents/render/session-context.ts +341 -0
  377. package/src/core/agents/rules/claim-conflict.ts +282 -0
  378. package/src/core/agents/rules/commit-conflict.ts +303 -0
  379. package/src/core/agents/rules/stop-hook.ts +229 -0
  380. package/src/core/agents/session-events.ts +228 -0
  381. package/src/core/agents/state/activity-log.ts +33 -0
  382. package/src/core/agents/state/council.ts +265 -0
  383. package/src/core/agents/state/heartbeat-projector.ts +488 -0
  384. package/src/core/agents/state/heartbeat-writer.ts +333 -0
  385. package/src/core/agents/state/names.ts +399 -0
  386. package/src/core/agents/state/pidmap.ts +38 -0
  387. package/src/core/agents/state/scratch.ts +121 -0
  388. package/src/core/agents/state/shell-mutation.ts +44 -0
  389. package/src/core/agents/state/stale-sweep.ts +190 -0
  390. package/src/core/config.ts +111 -0
  391. package/src/core/hooks/cli.ts +1247 -0
  392. package/src/core/hooks/effects/image-capture.ts +330 -0
  393. package/src/core/hooks/effects/index.ts +210 -0
  394. package/src/core/hooks/events/emit.ts +120 -0
  395. package/src/core/hooks/events/schema.ts +430 -0
  396. package/src/core/hooks/events/ulid.ts +51 -0
  397. package/src/core/hooks/harness/detect.ts +30 -0
  398. package/src/core/hooks/harness/events.ts +102 -0
  399. package/src/core/hooks/harness/output.ts +100 -0
  400. package/src/core/hooks/harness/parse.ts +180 -0
  401. package/src/core/hooks/index.ts +16 -0
  402. package/src/core/hooks/resolve/anchor.ts +51 -0
  403. package/src/core/hooks/resolve/coord-root.ts +25 -0
  404. package/src/core/hooks/resolve/intent.ts +89 -0
  405. package/src/core/hooks/resolve/owner.ts +140 -0
  406. package/src/core/hooks/resolve/transcript.ts +72 -0
  407. package/src/hooks/.gitkeep +0 -0
  408. package/src/index.ts +15 -0
  409. package/src/lib/agent-browser/client.ts +239 -0
  410. package/src/lib/agent-browser/index.ts +1 -0
  411. package/src/lib/browser/client.ts +449 -0
  412. package/src/lib/browser/dev-overlay.ts +207 -0
  413. package/src/lib/browser/index.ts +24 -0
  414. package/src/lib/browser/layout.ts +288 -0
  415. package/src/lib/browser/visibility.ts +419 -0
  416. package/src/lib/browser/visual-diff.ts +150 -0
  417. package/src/lib/completion/bash.ts +291 -0
  418. package/src/lib/completion/fish.ts +134 -0
  419. package/src/lib/completion/index.ts +10 -0
  420. package/src/lib/completion/walk.ts +184 -0
  421. package/src/lib/completion/zsh.ts +262 -0
  422. package/src/lib/context/index.ts +386 -0
  423. package/src/lib/cookies/client.ts +301 -0
  424. package/src/lib/cookies/index.ts +13 -0
  425. package/src/lib/council/index.ts +803 -0
  426. package/src/lib/docs-index.ts +216 -0
  427. package/src/lib/docs-lint.ts +413 -0
  428. package/src/lib/docs-sweep.ts +348 -0
  429. package/src/lib/docs.ts +199 -0
  430. package/src/lib/env.ts +12 -0
  431. package/src/lib/exec.ts +74 -0
  432. package/src/lib/format.ts +147 -0
  433. package/src/lib/http/client.ts +211 -0
  434. package/src/lib/http/index.ts +1 -0
  435. package/src/lib/identities/index.ts +210 -0
  436. package/src/lib/machine.ts +61 -0
  437. package/src/lib/presence.ts +154 -0
  438. package/src/lib/readability/client.ts +156 -0
  439. package/src/lib/readability/index.ts +5 -0
  440. package/src/lib/readability/turndown-plugin-gfm.d.ts +10 -0
  441. package/src/lib/scratch/index.ts +470 -0
  442. package/src/lib/tunnel/gate.ts +113 -0
  443. package/src/lib/tunnel/state.ts +167 -0
  444. package/src/web/.gitkeep +0 -0
  445. package/index.js +0 -1
@@ -0,0 +1,488 @@
1
+ /**
2
+ * Heartbeat projector. Reads canonical events from the consumer and projects
3
+ * them into per-owner state files under `.harnery/active/<id>.json`, the same
4
+ * canonical location every reader (this library, hooks, the web UI, etc.)
5
+ * expects.
6
+ *
7
+ * Projection writes a single file, additively merged with any existing body
8
+ * so writes from sibling tools (e.g. `agent-coord set-task` that doesn't go
9
+ * through the canonical event stream) survive each projector run.
10
+ */
11
+
12
+ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
13
+ import { dirname, join } from "node:path";
14
+ import type { CanonicalEvent } from "../events/consume.ts";
15
+
16
+ export interface V2Heartbeat {
17
+ instance_id: string;
18
+ session_id: string;
19
+ harness: string;
20
+ agent_id?: string;
21
+ name?: string;
22
+ kind?: "session" | "subagent" | "transient";
23
+ model?: string;
24
+ platform?: string;
25
+ subagent_call_id?: string;
26
+ parent_session_id?: string;
27
+ started_at?: string;
28
+ last_heartbeat: string;
29
+ last_tool?: string;
30
+ last_tool_target?: string;
31
+ last_tool_at?: string;
32
+ task?: string;
33
+ task_updated_at?: string;
34
+ last_status_at?: string;
35
+ presence?: "mobile" | "office";
36
+ last_intent?: string;
37
+ last_intent_source?: string;
38
+ last_turn_id?: string;
39
+ last_user_prompt_at?: string;
40
+ last_turn_stop_at?: string;
41
+ last_turn_status_box_present?: boolean;
42
+ ended_at?: string;
43
+ clean_exit?: boolean;
44
+ files_touched?: string[];
45
+ turn_summary?: string;
46
+ turn_summary_updated_at?: string;
47
+ /** ULID of the last event applied for this owner; idempotency anchor. */
48
+ last_event_id: string;
49
+ /** Count of events applied for this owner since the projector first saw it. */
50
+ events_applied: number;
51
+ /** Internal projection metadata. */
52
+ v2_meta: {
53
+ schema_version: 1;
54
+ first_seen: string;
55
+ last_projected: string;
56
+ };
57
+ }
58
+
59
+ export function projectHeartbeats(
60
+ coordRoot: string,
61
+ events: readonly CanonicalEvent[],
62
+ ): { written: string[]; perOwner: Record<string, V2Heartbeat> } {
63
+ const perOwner: Record<string, V2Heartbeat> = {};
64
+
65
+ // Terminal events for an owner we've never seen must NOT seed a new heartbeat:
66
+ // that resurrects a dead agent as a nameless, started_at-less zombie (the
67
+ // `agent-unknown (20608d ago)` ghost). It happens when a subagent.stop /
68
+ // session.end drains without (or after) its matching start: seed() then
69
+ // apply(stop) writes a bare tombstone the sweep+readers then choke on. If
70
+ // there's no existing heartbeat and the first event we see for an owner is
71
+ // terminal, skip it entirely.
72
+ //
73
+ // `health.heartbeat_swept` is terminal for the same reason, and was the
74
+ // sharper bug: stale-sweep deletes a dead heartbeat then emits this event,
75
+ // which the projector replayed to RE-CREATE the very file the sweep just
76
+ // removed (minus files_touched, since no start event ever ran for it). The
77
+ // reader then flagged it "missing required fields", and the resurrected file,
78
+ // carrying a fresh last_heartbeat = the swept-event ts, survived one
79
+ // freshness window before the next sweep deleted-and-resurrected it again. A
80
+ // self-perpetuating zombie loop (same instance swept 18×). A swept event must
81
+ // never seed a heartbeat.
82
+ const TERMINAL = new Set(["session.end", "subagent.stop", "health.heartbeat_swept"]);
83
+
84
+ // Seed from any existing v2 files so a partial replay doesn't reset state.
85
+ for (const ev of events) {
86
+ if (!perOwner[ev.instance_id]) {
87
+ const existing = readExisting(coordRoot, ev.instance_id);
88
+ if (!existing && TERMINAL.has(ev.event_type)) continue;
89
+ perOwner[ev.instance_id] = existing ?? seed(ev, coordRoot);
90
+ }
91
+ apply(perOwner[ev.instance_id]!, ev);
92
+ }
93
+
94
+ const written: string[] = [];
95
+ for (const [instance_id, hb] of Object.entries(perOwner)) {
96
+ // Mid-batch terminal guard: the replay variant of the seed-time TERMINAL
97
+ // skip above. A drain that replays a COMPLETED run end-to-end (shared
98
+ // cursor lagging another consumer, replayAll) seeds from the start event,
99
+ // applies the whole history INCLUDING the terminal stop, then lands here
100
+ // and would re-create the heartbeat the end-hook already unlinked, a
101
+ // zombie that reads as a live agent for a full staleness window (observed:
102
+ // a finished subagent's heartbeat resurrected 4m after its stop by a
103
+ // sibling's spawn drain). `ended_at` is only ever set by apply() in this
104
+ // batch, it is not in writeHeartbeat's persisted allowlist, so it can't
105
+ // arrive from disk. If the batch saw the owner end and no heartbeat file
106
+ // exists now, there is nothing live to update: skip. An EXISTING file
107
+ // still gets the terminal write (tombstone semantics, locked by the
108
+ // "session.end on an EXISTING heartbeat still applies" test).
109
+ if (hb.ended_at && !existsSync(heartbeatPath(coordRoot, instance_id))) continue;
110
+ writeHeartbeat(coordRoot, instance_id, hb);
111
+ written.push(instance_id);
112
+ }
113
+ return { written, perOwner };
114
+ }
115
+
116
+ function seed(ev: CanonicalEvent, coordRoot: string): V2Heartbeat {
117
+ const nowIso = new Date().toISOString();
118
+ const hb: V2Heartbeat = {
119
+ instance_id: ev.instance_id,
120
+ session_id: ev.session_id,
121
+ harness: ev.harness,
122
+ last_heartbeat: ev.ts,
123
+ last_event_id: ev.event_id,
124
+ events_applied: 0,
125
+ v2_meta: {
126
+ schema_version: 1,
127
+ first_seen: nowIso,
128
+ last_projected: nowIso,
129
+ },
130
+ };
131
+
132
+ // Recover identity from the durable `.name-history`. That file is written
133
+ // in-process at session.start / subagent.start time, BEFORE any projection,
134
+ // keyed by instance_id, surviving sweeps. Without this, seeding from a
135
+ // non-start event (a tool/turn whose start was never in a projected batch,
136
+ // e.g. the owner id resolved differently at start than later) produced a
137
+ // nameless `agent-unknown` heartbeat. Mirrors heartbeat-writer.healHeartbeat
138
+ // so BOTH heartbeat producers resolve identity the same way. Best-effort: a
139
+ // names.ts failure must never break projection (a past
140
+ // stop-projection crash that stalled the whole drain).
141
+ try {
142
+ const { resolveName } = require("./names.ts") as typeof import("./names.ts");
143
+ const resolved = resolveName(coordRoot, ev.instance_id, ev.session_id);
144
+ if (resolved) {
145
+ hb.name = resolved.name;
146
+ hb.kind = resolved.kind;
147
+ if (resolved.kind === "subagent") hb.agent_id = ev.instance_id;
148
+ }
149
+ } catch {
150
+ /* name-history unavailable: seed stays nameless; sweep + render guards cope */
151
+ }
152
+
153
+ return hb;
154
+ }
155
+
156
+ function apply(hb: V2Heartbeat, ev: CanonicalEvent): void {
157
+ hb.last_heartbeat = ev.ts;
158
+ hb.last_event_id = ev.event_id;
159
+ hb.events_applied += 1;
160
+ hb.v2_meta.last_projected = new Date().toISOString();
161
+ if (ev.turn_id) hb.last_turn_id = ev.turn_id;
162
+
163
+ const d = ev.data;
164
+ switch (ev.event_type) {
165
+ case "session.start":
166
+ hb.started_at = pickStr(d, "started_at") ?? ev.ts;
167
+ hb.harness = ev.harness;
168
+ {
169
+ const model = pickStr(d, "model");
170
+ if (model) hb.model = model;
171
+ const platform = pickStr(d, "platform") ?? harnessToPlatform(ev.harness);
172
+ hb.platform = platform;
173
+ const name = pickStr(d, "name");
174
+ if (name) hb.name = name;
175
+ const kind = pickStr(d, "kind");
176
+ if (kind === "session" || kind === "subagent" || kind === "transient") {
177
+ hb.kind = kind;
178
+ } else if (!hb.kind) {
179
+ hb.kind = "session";
180
+ }
181
+ const agentId = pickStr(d, "agent_id");
182
+ if (agentId) hb.agent_id = agentId;
183
+ const subagentCallId = pickStr(d, "subagent_call_id");
184
+ if (subagentCallId) hb.subagent_call_id = subagentCallId;
185
+ const parentSession = pickStr(d, "parent_session_id");
186
+ if (parentSession) hb.parent_session_id = parentSession;
187
+ if (!hb.files_touched) hb.files_touched = [];
188
+ }
189
+ break;
190
+
191
+ case "session.end":
192
+ hb.ended_at = pickStr(d, "ended_at") ?? ev.ts;
193
+ hb.clean_exit = pickBool(d, "clean_exit");
194
+ break;
195
+
196
+ case "subagent.start": {
197
+ const name = pickStr(d, "name");
198
+ if (name) hb.name = name;
199
+ hb.kind = "subagent";
200
+ const parentSession = pickStr(d, "parent_session_id");
201
+ if (parentSession) hb.parent_session_id = parentSession;
202
+ const subagentCallId = pickStr(d, "subagent_call_id");
203
+ if (subagentCallId) hb.subagent_call_id = subagentCallId;
204
+ hb.agent_id = ev.instance_id;
205
+ hb.started_at = ev.ts;
206
+ if (!hb.files_touched) hb.files_touched = [];
207
+ hb.platform = harnessToPlatform(ev.harness);
208
+ break;
209
+ }
210
+
211
+ case "subagent.stop":
212
+ hb.ended_at = pickStr(d, "ended_at") ?? ev.ts;
213
+ hb.clean_exit = pickBool(d, "clean_exit") ?? true;
214
+ break;
215
+
216
+ case "user_prompt.submit":
217
+ hb.last_user_prompt_at = ev.ts;
218
+ break;
219
+
220
+ case "turn.stop":
221
+ hb.last_turn_stop_at = ev.ts;
222
+ hb.last_turn_status_box_present = pickBool(d, "status_box_present");
223
+ {
224
+ const summary = pickStr(d, "turn_summary");
225
+ if (summary) {
226
+ hb.turn_summary = summary;
227
+ hb.turn_summary_updated_at = ev.ts;
228
+ }
229
+ // Backfill model for harnesses that omit it at session.start (Claude
230
+ // Code). The Stop hook resolves it from the transcript by this point;
231
+ // only set when present so we never clobber a known model.
232
+ const model = pickStr(d, "model");
233
+ if (model) hb.model = model;
234
+ }
235
+ break;
236
+
237
+ case "tool.pre_use": {
238
+ const toolName = pickStr(d, "tool_name");
239
+ hb.last_tool = toolName;
240
+ hb.last_tool_target = extractTarget(d);
241
+ hb.last_tool_at = ev.ts;
242
+ const intent = pickStr(d, "intent");
243
+ if (intent && intent !== "(no intent)") {
244
+ hb.last_intent = intent;
245
+ hb.last_intent_source = pickStr(d, "intent_source");
246
+ }
247
+ // Project files_touched: Edit / Write / NotebookEdit add their target.
248
+ if (toolName === "Edit" || toolName === "Write" || toolName === "NotebookEdit") {
249
+ const target = extractFilePath(d);
250
+ if (target) {
251
+ if (!hb.files_touched) hb.files_touched = [];
252
+ if (!hb.files_touched.includes(target)) hb.files_touched.push(target);
253
+ }
254
+ }
255
+ break;
256
+ }
257
+
258
+ case "tool.post_use":
259
+ case "tool.post_use_failure":
260
+ hb.last_tool_at = ev.ts;
261
+ break;
262
+
263
+ case "state.task_set": {
264
+ const cleared = pickBool(d, "cleared");
265
+ const task = pickStr(d, "task");
266
+ if (cleared || !task) {
267
+ hb.task = undefined;
268
+ } else {
269
+ hb.task = task;
270
+ }
271
+ hb.task_updated_at = ev.ts;
272
+ break;
273
+ }
274
+
275
+ case "state.status_checked":
276
+ hb.last_status_at = ev.ts;
277
+ break;
278
+
279
+ case "state.presence_change": {
280
+ const to = pickStr(d, "to");
281
+ if (to === "mobile" || to === "office") hb.presence = to;
282
+ break;
283
+ }
284
+
285
+ case "claim.release": {
286
+ const path = pickStr(d, "path");
287
+ if (path && hb.files_touched) {
288
+ hb.files_touched = hb.files_touched.filter((p) => p !== path);
289
+ }
290
+ break;
291
+ }
292
+ }
293
+ }
294
+
295
+ function harnessToPlatform(harness: string): string {
296
+ if (harness === "claude-code") return "claude_code";
297
+ if (harness === "cursor") return "cursor";
298
+ if (harness === "codex") return "codex";
299
+ return harness;
300
+ }
301
+
302
+ function extractFilePath(data: Record<string, unknown>): string | undefined {
303
+ const raw = data.tool_input;
304
+ if (typeof raw !== "string") return undefined;
305
+ try {
306
+ const parsed = JSON.parse(raw) as Record<string, unknown>;
307
+ return (
308
+ pickStr(parsed, "file_path") ??
309
+ pickStr(parsed, "path") ??
310
+ pickStr(parsed, "notebook_path") ??
311
+ undefined
312
+ );
313
+ } catch {
314
+ return undefined;
315
+ }
316
+ }
317
+
318
+ function extractTarget(data: Record<string, unknown>): string | undefined {
319
+ // tool_input is stringified JSON in our envelope; try to parse and pull a
320
+ // common target field (file_path, path, command).
321
+ const raw = data.tool_input;
322
+ if (typeof raw !== "string") return undefined;
323
+ try {
324
+ const parsed = JSON.parse(raw) as Record<string, unknown>;
325
+ return (
326
+ pickStr(parsed, "file_path") ??
327
+ pickStr(parsed, "path") ??
328
+ pickStr(parsed, "notebook_path") ??
329
+ cleanCommand(pickStr(parsed, "command")) ??
330
+ undefined
331
+ );
332
+ } catch {
333
+ return undefined;
334
+ }
335
+ }
336
+
337
+ /**
338
+ * The repo mandates a `# intent: …` first-line comment on every Bash command,
339
+ * so a raw `command` payload starts with the intent prose, not the command.
340
+ * Stamping that into `last_tool_target` leaked the intent into the peer table
341
+ * and pushed the real command past the 60-char render slice. Skip leading
342
+ * comment-only lines so the target reflects what the agent is actually running.
343
+ */
344
+ function cleanCommand(command: string | undefined): string | undefined {
345
+ if (command === undefined) return undefined;
346
+ for (const line of command.split("\n")) {
347
+ const trimmed = line.trim();
348
+ if (!trimmed || trimmed.startsWith("#")) continue;
349
+ return trimmed;
350
+ }
351
+ // All-comment / degenerate: fall back to the trimmed whole.
352
+ return command.trim() || undefined;
353
+ }
354
+
355
+ function readExisting(coordRoot: string, instanceId: string): V2Heartbeat | null {
356
+ const path = heartbeatPath(coordRoot, instanceId);
357
+ if (!existsSync(path)) return null;
358
+ try {
359
+ const raw = JSON.parse(readFileSync(path, "utf8")) as Partial<V2Heartbeat>;
360
+ return coerceV2Heartbeat(raw, instanceId);
361
+ } catch {
362
+ return null;
363
+ }
364
+ }
365
+
366
+ /**
367
+ * Restore the projector-owned invariant fields on a heartbeat read from disk.
368
+ *
369
+ * The active-heartbeat file has multiple producers: the projector (seed/apply,
370
+ * which set `v2_meta` + `events_applied`) AND the writer layer
371
+ * (heartbeat-writer.ts: healHeartbeat, setTask, stampToolActivity, …), which
372
+ * only knows the v1 shape and omits both. readExisting previously `as`-cast the
373
+ * raw JSON straight to V2Heartbeat, so a body recreated by `healHeartbeat`
374
+ * (e.g. a pruned Cursor session) reached apply() without `v2_meta` →
375
+ * `hb.v2_meta.last_projected = …` threw (caught + logged ~200×/day, phase
376
+ * "stop-projection"), and without `events_applied` → `events_applied += 1`
377
+ * silently went NaN. The read boundary is where untyped JSON becomes a typed
378
+ * V2Heartbeat, so it's where the type's required-field invariant must be
379
+ * re-established, covering every malformed producer, not just one symptom.
380
+ *
381
+ * Note `v2_meta` is NOT in writeHeartbeat's persisted allowlist, so it never
382
+ * lands on disk; it's ephemeral per-drain bookkeeping, which means readExisting
383
+ * must re-coerce it on EVERY read of an already-seen owner (not only for
384
+ * heal-written bodies). `events_applied` IS persisted, so coercing it to 0 only
385
+ * matters for bodies a writer produced without the field (e.g. healHeartbeat).
386
+ */
387
+ function coerceV2Heartbeat(raw: Partial<V2Heartbeat>, instanceId: string): V2Heartbeat {
388
+ const hb = raw as V2Heartbeat;
389
+ if (!hb.instance_id) hb.instance_id = instanceId;
390
+ if (typeof hb.events_applied !== "number" || Number.isNaN(hb.events_applied)) {
391
+ hb.events_applied = 0;
392
+ }
393
+ if (!hb.v2_meta) {
394
+ const nowIso = new Date().toISOString();
395
+ hb.v2_meta = {
396
+ schema_version: 1,
397
+ first_seen: hb.last_heartbeat ?? nowIso,
398
+ last_projected: nowIso,
399
+ };
400
+ }
401
+ return hb;
402
+ }
403
+
404
+ function writeHeartbeat(coordRoot: string, instanceId: string, hb: V2Heartbeat): void {
405
+ const path = heartbeatPath(coordRoot, instanceId);
406
+ try {
407
+ mkdirSync(dirname(path), { recursive: true });
408
+ // Additive merge with existing body so writes from sibling tools (e.g.
409
+ // `agent-coord set-task` that doesn't go through the canonical event
410
+ // stream) survive each projector run. Projected fields win on conflict.
411
+ let existing: Record<string, unknown> = {};
412
+ if (existsSync(path)) {
413
+ try {
414
+ existing = JSON.parse(readFileSync(path, "utf8"));
415
+ } catch {
416
+ /* skip merge */
417
+ }
418
+ }
419
+ const merged: Record<string, unknown> = {
420
+ schema_version: 1,
421
+ ...existing,
422
+ instance_id: hb.instance_id,
423
+ session_id: hb.session_id,
424
+ last_heartbeat: hb.last_heartbeat,
425
+ last_event_id: hb.last_event_id,
426
+ events_applied: hb.events_applied,
427
+ };
428
+ setIfDefined(merged, "name", hb.name);
429
+ setIfDefined(merged, "kind", hb.kind);
430
+ setIfDefined(merged, "agent_id", hb.agent_id);
431
+ setIfDefined(merged, "subagent_call_id", hb.subagent_call_id);
432
+ setIfDefined(merged, "model", hb.model);
433
+ setIfDefined(merged, "platform", hb.platform);
434
+ setIfDefined(merged, "started_at", hb.started_at);
435
+ // files_touched is a required-array invariant for every reader
436
+ // (coord-reader.isHeartbeatShape, the web UI, stale-sweep). Seed paths that
437
+ // never hit a start event leave it undefined; default to [] so the writer
438
+ // can never emit a file that fails the reader's shape check. Belt to the
439
+ // TERMINAL guard's suspenders.
440
+ merged.files_touched = hb.files_touched ?? [];
441
+ setIfDefined(merged, "last_tool", hb.last_tool);
442
+ setIfDefined(merged, "last_tool_target", hb.last_tool_target);
443
+ setIfDefined(merged, "last_tool_at", hb.last_tool_at);
444
+ setIfDefined(merged, "task", hb.task);
445
+ setIfDefined(merged, "task_updated_at", hb.task_updated_at);
446
+ setIfDefined(merged, "last_status_at", hb.last_status_at);
447
+ setIfDefined(merged, "turn_summary", hb.turn_summary);
448
+ setIfDefined(merged, "turn_summary_updated_at", hb.turn_summary_updated_at);
449
+ setIfDefined(merged, "current_turn_id", hb.last_turn_id);
450
+ setIfDefined(merged, "parent_instance_id", hb.parent_session_id);
451
+ // Atomic temp+rename (same primitive as heartbeat-writer.ts:atomicWrite) so
452
+ // a concurrent reader (stale-sweep, `harn agents`, the web UI) never sees a
453
+ // half-written file. A plain in-place writeFileSync truncates-then-writes,
454
+ // exposing a partial-read window; stale-sweep deletes any heartbeat it
455
+ // fails to JSON.parse, so a partial read there would delete a live agent.
456
+ const tmp = `${path}.tmp.${process.pid}`;
457
+ writeFileSync(tmp, JSON.stringify(merged, null, 2), "utf8");
458
+ renameSync(tmp, path);
459
+ } catch {
460
+ /* surfaced by caller via missing heartbeat file */
461
+ }
462
+ }
463
+
464
+ export function heartbeatPath(coordRoot: string, instanceId: string): string {
465
+ return join(coordRoot, ".harnery", "active", `${instanceId}.json`);
466
+ }
467
+
468
+ /** Set a field only when value is defined (not null/undefined). Used by the
469
+ * additive merge so non-projected writes survive projector runs. */
470
+ function setIfDefined<T>(
471
+ target: Record<string, unknown>,
472
+ key: string,
473
+ value: T | undefined | null,
474
+ ): void {
475
+ if (value !== undefined && value !== null) {
476
+ target[key] = value;
477
+ }
478
+ }
479
+
480
+ function pickStr(o: Record<string, unknown>, k: string): string | undefined {
481
+ const v = o[k];
482
+ return typeof v === "string" && v.length > 0 ? v : undefined;
483
+ }
484
+
485
+ function pickBool(o: Record<string, unknown>, k: string): boolean | undefined {
486
+ const v = o[k];
487
+ return typeof v === "boolean" ? v : undefined;
488
+ }