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,564 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { extname, resolve } from "node:path";
3
+ import type { Command } from "commander";
4
+ import * as ts from "typescript";
5
+ import type { EmitContext } from "../commander.ts";
6
+ import { resolveBinName } from "../core/config.ts";
7
+
8
+ /**
9
+ * `harn outline <file>`: print the structural skeleton of a code file (imports
10
+ * + top-level decls + line numbers). TS/JS/TSX/JSX use the TypeScript compiler
11
+ * AST; PHP and Python use a regex pass. Token-efficient substitute for reading
12
+ * a whole large file just to find one symbol.
13
+ *
14
+ * For markdown files, dispatch to `harn toc`.
15
+ */
16
+
17
+ interface OutlineOpts {
18
+ json?: boolean;
19
+ exportsOnly?: boolean;
20
+ imports?: boolean; // Commander sets to false on --no-imports
21
+ members?: boolean; // Commander sets to false on --no-members
22
+ }
23
+
24
+ interface SymbolEntry {
25
+ kind: string;
26
+ name: string;
27
+ signature?: string;
28
+ line: number;
29
+ exported?: boolean;
30
+ members?: SymbolEntry[];
31
+ }
32
+
33
+ interface OutlineResult {
34
+ file: string;
35
+ language: string;
36
+ total_lines: number;
37
+ imports: string[];
38
+ symbols: SymbolEntry[];
39
+ }
40
+
41
+ export function registerOutlineCommand(program: Command, emit: EmitContext): void {
42
+ program
43
+ .command("outline <file>")
44
+ .description(
45
+ `Print the structural skeleton of a code file (imports + top-level decls + line numbers). Supports TS/JS/TSX/JSX (AST), PHP/Python (regex). Use \`${resolveBinName()} toc\` for markdown.`,
46
+ )
47
+ .option("--json", "Structured JSON envelope")
48
+ .option("--exports-only", "Only show exported symbols")
49
+ .option("--no-imports", "Skip the imports summary block")
50
+ .option("--no-members", "Don't expand class/interface members")
51
+ .action(async (file: string, opts: OutlineOpts) => {
52
+ try {
53
+ const result = await runOutline(file, opts);
54
+ if (opts.json) {
55
+ emit.config({ format: "json" });
56
+ emit.data(result);
57
+ return;
58
+ }
59
+ emit.text(`${renderOutline(result, opts)}\n`);
60
+ } catch (err) {
61
+ emit.error({ code: "outline_failed", message: (err as Error).message });
62
+ process.exit(1);
63
+ }
64
+ });
65
+ }
66
+
67
+ async function runOutline(file: string, opts: OutlineOpts): Promise<OutlineResult> {
68
+ const absPath = resolve(file);
69
+ if (!existsSync(absPath)) throw new Error(`no such file: ${file}`);
70
+
71
+ const content = readFileSync(absPath, "utf8");
72
+ const total_lines = content.split("\n").length;
73
+ const ext = extname(absPath).toLowerCase();
74
+
75
+ let language = "";
76
+ let parsed: { imports: string[]; symbols: SymbolEntry[] };
77
+
78
+ if ([".ts", ".tsx", ".js", ".jsx", ".mts", ".cts", ".mjs", ".cjs"].includes(ext)) {
79
+ language = ext.slice(1);
80
+ parsed = outlineTypeScript(content, absPath, ext);
81
+ } else if (ext === ".php") {
82
+ language = "php";
83
+ parsed = outlinePhp(content);
84
+ } else if (ext === ".py") {
85
+ language = "python";
86
+ parsed = outlinePython(content);
87
+ } else if ([".md", ".mdx"].includes(ext)) {
88
+ throw new Error(`use \`${resolveBinName()} toc ${file}\` for markdown files`);
89
+ } else {
90
+ throw new Error(
91
+ `unsupported file type: ${ext || "(no extension)"} (supported: ts/tsx/js/jsx/php/py)`,
92
+ );
93
+ }
94
+
95
+ if (opts.exportsOnly) {
96
+ parsed.symbols = parsed.symbols.filter((s) => s.exported);
97
+ }
98
+
99
+ return {
100
+ file,
101
+ language,
102
+ total_lines,
103
+ imports: parsed.imports,
104
+ symbols: parsed.symbols,
105
+ };
106
+ }
107
+
108
+ function outlineTypeScript(
109
+ content: string,
110
+ path: string,
111
+ ext: string,
112
+ ): { imports: string[]; symbols: SymbolEntry[] } {
113
+ const isTsx = ext === ".tsx" || ext === ".jsx";
114
+ const scriptKind = isTsx ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
115
+ const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true, scriptKind);
116
+
117
+ const imports: string[] = [];
118
+ const symbols: SymbolEntry[] = [];
119
+
120
+ const lineOf = (node: ts.Node) =>
121
+ source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1;
122
+ const text = (node: ts.Node) =>
123
+ content.slice(node.getStart(source), node.getEnd()).replace(/\s+/g, " ").trim();
124
+ const isExported = (node: ts.Node): boolean => {
125
+ const mods = (node as { modifiers?: ReadonlyArray<ts.ModifierLike> }).modifiers;
126
+ return mods?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) ?? false;
127
+ };
128
+
129
+ for (const stmt of source.statements) {
130
+ if (ts.isImportDeclaration(stmt)) {
131
+ const moduleSpec = (stmt.moduleSpecifier as ts.StringLiteral).text;
132
+ const clause = stmt.importClause;
133
+ const isTypeOnly = clause?.isTypeOnly ? "type " : "";
134
+ let what = "";
135
+ if (clause?.name) what = clause.name.text;
136
+ if (clause?.namedBindings) {
137
+ if (ts.isNamespaceImport(clause.namedBindings)) {
138
+ what = `${what ? `${what}, ` : ""}* as ${clause.namedBindings.name.text}`;
139
+ } else {
140
+ const names = clause.namedBindings.elements.map((e) => e.name.text).join(", ");
141
+ what = `${what ? `${what}, ` : ""}{${names}}`;
142
+ }
143
+ }
144
+ imports.push(`${isTypeOnly}${what || "(side-effect)"} from "${moduleSpec}"`);
145
+ continue;
146
+ }
147
+
148
+ const exported = isExported(stmt);
149
+
150
+ if (ts.isFunctionDeclaration(stmt) && stmt.name) {
151
+ symbols.push({
152
+ kind: "function",
153
+ name: stmt.name.text,
154
+ signature: renderFnSig(stmt, content, source),
155
+ line: lineOf(stmt),
156
+ exported,
157
+ });
158
+ } else if (ts.isClassDeclaration(stmt) && stmt.name) {
159
+ const heritage = (stmt.heritageClauses || [])
160
+ .map(
161
+ (h) =>
162
+ (h.token === ts.SyntaxKind.ExtendsKeyword ? "extends " : "implements ") +
163
+ h.types.map((t) => text(t.expression)).join(", "),
164
+ )
165
+ .join(" ");
166
+ const members: SymbolEntry[] = [];
167
+ for (const member of stmt.members) {
168
+ if (ts.isMethodDeclaration(member) && member.name) {
169
+ members.push({
170
+ kind: "method",
171
+ name: (member.name as ts.Identifier).text || text(member.name),
172
+ signature: renderFnSig(member, content, source),
173
+ line: lineOf(member),
174
+ });
175
+ } else if (ts.isConstructorDeclaration(member)) {
176
+ members.push({
177
+ kind: "method",
178
+ name: "constructor",
179
+ signature: renderFnSig(member, content, source),
180
+ line: lineOf(member),
181
+ });
182
+ } else if (ts.isPropertyDeclaration(member) && member.name) {
183
+ members.push({
184
+ kind: "property",
185
+ name: (member.name as ts.Identifier).text || text(member.name),
186
+ signature: member.type ? `: ${text(member.type)}` : "",
187
+ line: lineOf(member),
188
+ });
189
+ }
190
+ }
191
+ symbols.push({
192
+ kind: "class",
193
+ name: stmt.name.text,
194
+ signature: heritage,
195
+ line: lineOf(stmt),
196
+ exported,
197
+ members,
198
+ });
199
+ } else if (ts.isInterfaceDeclaration(stmt)) {
200
+ const heritage = (stmt.heritageClauses || [])
201
+ .flatMap((h) => h.types.map((t) => text(t.expression)))
202
+ .join(", ");
203
+ const members: SymbolEntry[] = [];
204
+ for (const member of stmt.members) {
205
+ if (ts.isPropertySignature(member) && member.name) {
206
+ members.push({
207
+ kind: "property",
208
+ name: (member.name as ts.Identifier).text || text(member.name),
209
+ signature: member.type ? `: ${text(member.type)}` : "",
210
+ line: lineOf(member),
211
+ });
212
+ } else if (ts.isMethodSignature(member) && member.name) {
213
+ members.push({
214
+ kind: "method",
215
+ name: (member.name as ts.Identifier).text || text(member.name),
216
+ signature: renderFnSig(member, content, source),
217
+ line: lineOf(member),
218
+ });
219
+ }
220
+ }
221
+ symbols.push({
222
+ kind: "interface",
223
+ name: stmt.name.text,
224
+ signature: heritage ? `extends ${heritage}` : "",
225
+ line: lineOf(stmt),
226
+ exported,
227
+ members,
228
+ });
229
+ } else if (ts.isTypeAliasDeclaration(stmt)) {
230
+ const sigText = text(stmt.type);
231
+ symbols.push({
232
+ kind: "type",
233
+ name: stmt.name.text,
234
+ signature: `= ${sigText.slice(0, 80)}${sigText.length > 80 ? "…" : ""}`,
235
+ line: lineOf(stmt),
236
+ exported,
237
+ });
238
+ } else if (ts.isEnumDeclaration(stmt)) {
239
+ symbols.push({
240
+ kind: "enum",
241
+ name: stmt.name.text,
242
+ line: lineOf(stmt),
243
+ exported,
244
+ members: stmt.members.map((m) => ({
245
+ kind: "enumMember",
246
+ name: (m.name as ts.Identifier).text || text(m.name),
247
+ line: lineOf(m),
248
+ })),
249
+ });
250
+ } else if (ts.isVariableStatement(stmt)) {
251
+ const flags = stmt.declarationList.flags;
252
+ const isConst = (flags & ts.NodeFlags.Const) !== 0;
253
+ const isLet = (flags & ts.NodeFlags.Let) !== 0;
254
+ const kind = isConst ? "const" : isLet ? "let" : "var";
255
+ const stmtExported = isExported(stmt);
256
+ for (const decl of stmt.declarationList.declarations) {
257
+ if (!ts.isIdentifier(decl.name)) continue;
258
+ let sig = "";
259
+ if (decl.type) sig = `: ${text(decl.type)}`;
260
+ else if (
261
+ decl.initializer &&
262
+ (ts.isArrowFunction(decl.initializer) || ts.isFunctionExpression(decl.initializer))
263
+ ) {
264
+ sig = renderFnSig(decl.initializer, content, source);
265
+ }
266
+ symbols.push({
267
+ kind,
268
+ name: decl.name.text,
269
+ signature: sig,
270
+ line: lineOf(decl),
271
+ exported: stmtExported,
272
+ });
273
+ }
274
+ } else if (ts.isExportDeclaration(stmt)) {
275
+ if (stmt.exportClause && ts.isNamedExports(stmt.exportClause)) {
276
+ const names = stmt.exportClause.elements.map((e) => e.name.text).join(", ");
277
+ const fromText = stmt.moduleSpecifier
278
+ ? ` from "${(stmt.moduleSpecifier as ts.StringLiteral).text}"`
279
+ : "";
280
+ symbols.push({
281
+ kind: "re-export",
282
+ name: `{${names}}${fromText}`,
283
+ line: lineOf(stmt),
284
+ exported: true,
285
+ });
286
+ } else if (stmt.moduleSpecifier) {
287
+ symbols.push({
288
+ kind: "re-export",
289
+ name: `* from "${(stmt.moduleSpecifier as ts.StringLiteral).text}"`,
290
+ line: lineOf(stmt),
291
+ exported: true,
292
+ });
293
+ }
294
+ } else if (ts.isModuleDeclaration(stmt) && stmt.name) {
295
+ symbols.push({
296
+ kind: "namespace",
297
+ name: (stmt.name as ts.Identifier).text || text(stmt.name),
298
+ line: lineOf(stmt),
299
+ exported,
300
+ });
301
+ }
302
+ }
303
+
304
+ return { imports, symbols };
305
+ }
306
+
307
+ function renderFnSig(
308
+ fn: ts.FunctionLikeDeclarationBase | ts.MethodSignature,
309
+ content: string,
310
+ source: ts.SourceFile,
311
+ ): string {
312
+ const params = (fn.parameters || [])
313
+ .map((p) => {
314
+ const name = ts.isIdentifier(p.name)
315
+ ? p.name.text
316
+ : content.slice(p.name.getStart(source), p.name.getEnd());
317
+ const type = p.type
318
+ ? content.slice(p.type.getStart(source), p.type.getEnd()).replace(/\s+/g, " ").trim()
319
+ : "";
320
+ const optional = p.questionToken ? "?" : "";
321
+ return type ? `${name}${optional}: ${type}` : `${name}${optional}`;
322
+ })
323
+ .join(", ");
324
+ const ret = fn.type
325
+ ? `: ${content.slice(fn.type.getStart(source), fn.type.getEnd()).replace(/\s+/g, " ").trim()}`
326
+ : "";
327
+ return `(${params})${ret}`;
328
+ }
329
+
330
+ function outlinePhp(content: string): { imports: string[]; symbols: SymbolEntry[] } {
331
+ const lines = content.split("\n");
332
+ const imports: string[] = [];
333
+ const symbols: SymbolEntry[] = [];
334
+ let currentContainer: SymbolEntry | null = null;
335
+
336
+ for (let i = 0; i < lines.length; i++) {
337
+ const line = lines[i];
338
+ const lineNum = i + 1;
339
+ const trimmed = line.trim();
340
+ const isIndented = /^[ \t]/.test(line);
341
+
342
+ if (
343
+ trimmed === "" ||
344
+ trimmed.startsWith("//") ||
345
+ trimmed.startsWith("#") ||
346
+ trimmed.startsWith("*") ||
347
+ trimmed.startsWith("/*")
348
+ )
349
+ continue;
350
+
351
+ const nsMatch = trimmed.match(/^namespace\s+([\w\\]+)\s*;/);
352
+ if (nsMatch && !isIndented) {
353
+ symbols.push({ kind: "namespace", name: nsMatch[1], line: lineNum });
354
+ continue;
355
+ }
356
+ const useMatch = trimmed.match(/^use\s+([\w\\]+)(?:\s+as\s+(\w+))?\s*;/);
357
+ if (useMatch && !isIndented) {
358
+ imports.push(useMatch[2] ? `${useMatch[1]} as ${useMatch[2]}` : useMatch[1]);
359
+ continue;
360
+ }
361
+ if (!isIndented) {
362
+ const classMatch = trimmed.match(
363
+ /^(?:abstract\s+|final\s+)?class\s+(\w+)(?:\s+extends\s+(\w+))?(?:\s+implements\s+([\w,\s]+))?/,
364
+ );
365
+ if (classMatch) {
366
+ currentContainer = {
367
+ kind: "class",
368
+ name: classMatch[1],
369
+ signature: [
370
+ classMatch[2] ? `extends ${classMatch[2]}` : "",
371
+ classMatch[3] ? `implements ${classMatch[3].trim()}` : "",
372
+ ]
373
+ .filter(Boolean)
374
+ .join(" "),
375
+ line: lineNum,
376
+ members: [],
377
+ };
378
+ symbols.push(currentContainer);
379
+ continue;
380
+ }
381
+ const interfaceMatch = trimmed.match(/^interface\s+(\w+)/);
382
+ if (interfaceMatch) {
383
+ currentContainer = {
384
+ kind: "interface",
385
+ name: interfaceMatch[1],
386
+ line: lineNum,
387
+ members: [],
388
+ };
389
+ symbols.push(currentContainer);
390
+ continue;
391
+ }
392
+ const traitMatch = trimmed.match(/^trait\s+(\w+)/);
393
+ if (traitMatch) {
394
+ currentContainer = { kind: "trait", name: traitMatch[1], line: lineNum, members: [] };
395
+ symbols.push(currentContainer);
396
+ continue;
397
+ }
398
+ }
399
+ const fnMatch = trimmed.match(
400
+ /^(?:public\s+|private\s+|protected\s+)?(?:static\s+)?function\s+(\w+)\s*\(([^)]*)\)/,
401
+ );
402
+ if (fnMatch) {
403
+ const entry: SymbolEntry = {
404
+ kind: isIndented && currentContainer ? "method" : "function",
405
+ name: fnMatch[1],
406
+ signature: `(${fnMatch[2].trim()})`,
407
+ line: lineNum,
408
+ };
409
+ if (isIndented && currentContainer) {
410
+ currentContainer.members!.push(entry);
411
+ } else {
412
+ symbols.push(entry);
413
+ }
414
+ }
415
+ }
416
+
417
+ return { imports, symbols };
418
+ }
419
+
420
+ function outlinePython(content: string): { imports: string[]; symbols: SymbolEntry[] } {
421
+ const lines = content.split("\n");
422
+ const imports: string[] = [];
423
+ const symbols: SymbolEntry[] = [];
424
+ let currentClass: SymbolEntry | null = null;
425
+
426
+ for (let i = 0; i < lines.length; i++) {
427
+ const line = lines[i];
428
+ const lineNum = i + 1;
429
+ const trimmed = line.trim();
430
+ const indent = line.match(/^ */)![0].length;
431
+
432
+ if (trimmed === "" || trimmed.startsWith("#")) continue;
433
+
434
+ const importMatch = trimmed.match(/^import\s+(.+)$/);
435
+ const fromMatch = trimmed.match(/^from\s+(\S+)\s+import\s+(.+)$/);
436
+ if (importMatch && indent === 0) {
437
+ imports.push(importMatch[1]);
438
+ continue;
439
+ }
440
+ if (fromMatch && indent === 0) {
441
+ imports.push(`${fromMatch[2].trim()} from ${fromMatch[1]}`);
442
+ continue;
443
+ }
444
+
445
+ const classMatch = trimmed.match(/^class\s+(\w+)(?:\(([^)]*)\))?:/);
446
+ if (classMatch && indent === 0) {
447
+ currentClass = {
448
+ kind: "class",
449
+ name: classMatch[1],
450
+ signature: classMatch[2] ? `(${classMatch[2]})` : "",
451
+ line: lineNum,
452
+ members: [],
453
+ };
454
+ symbols.push(currentClass);
455
+ continue;
456
+ }
457
+
458
+ const methodMatch = trimmed.match(/^(?:async\s+)?def\s+(\w+)\s*\(([^)]*)\)/);
459
+ if (methodMatch) {
460
+ if (indent === 0) {
461
+ symbols.push({
462
+ kind: "function",
463
+ name: methodMatch[1],
464
+ signature: `(${methodMatch[2].trim()})`,
465
+ line: lineNum,
466
+ });
467
+ currentClass = null;
468
+ } else if (currentClass) {
469
+ currentClass.members!.push({
470
+ kind: "method",
471
+ name: methodMatch[1],
472
+ signature: `(${methodMatch[2].trim()})`,
473
+ line: lineNum,
474
+ });
475
+ }
476
+ continue;
477
+ }
478
+
479
+ if (indent === 0 && trimmed.length > 0 && currentClass) {
480
+ currentClass = null;
481
+ }
482
+ }
483
+
484
+ return { imports, symbols };
485
+ }
486
+
487
+ function renderOutline(r: OutlineResult, opts: OutlineOpts): string {
488
+ const lines: string[] = [];
489
+ const symbolCount = r.symbols.length;
490
+ lines.push(
491
+ `outline · ${r.file} (${r.language}, ${r.total_lines} lines, ${symbolCount} top-level symbol${symbolCount === 1 ? "" : "s"})`,
492
+ );
493
+
494
+ if (opts.imports !== false && r.imports.length > 0) {
495
+ lines.push("");
496
+ if (r.imports.length <= 4) {
497
+ lines.push(`imports: ${r.imports.join(" · ")}`);
498
+ } else {
499
+ lines.push(`imports (${r.imports.length}):`);
500
+ for (const imp of r.imports) lines.push(` ${imp}`);
501
+ }
502
+ }
503
+
504
+ if (r.symbols.length > 0) {
505
+ lines.push("");
506
+ for (const sym of r.symbols) {
507
+ lines.push(renderSymbol(sym, 0, opts));
508
+ }
509
+ } else {
510
+ lines.push("");
511
+ lines.push("(no top-level symbols)");
512
+ }
513
+
514
+ return lines.join("\n");
515
+ }
516
+
517
+ function renderSymbol(s: SymbolEntry, depth: number, opts: OutlineOpts): string {
518
+ const indent = " ".repeat(depth);
519
+ const lineMark = `L${s.line}`.padEnd(6);
520
+ const exp = s.exported ? "★ " : " ";
521
+ const kindAbbr = kindToAbbr(s.kind);
522
+ const sig = s.signature ? ` ${s.signature}` : "";
523
+ const head = `${indent}${exp}${lineMark} ${kindAbbr} ${s.name}${sig}`;
524
+
525
+ if (opts.members !== false && s.members && s.members.length > 0) {
526
+ return [head, ...s.members.map((m) => renderSymbol(m, depth + 1, opts))].join("\n");
527
+ }
528
+ return head;
529
+ }
530
+
531
+ function kindToAbbr(kind: string): string {
532
+ switch (kind) {
533
+ case "function":
534
+ return "fn ";
535
+ case "class":
536
+ return "cl ";
537
+ case "interface":
538
+ return "if ";
539
+ case "type":
540
+ return "ty ";
541
+ case "const":
542
+ return "ct ";
543
+ case "let":
544
+ return "let ";
545
+ case "var":
546
+ return "var ";
547
+ case "enum":
548
+ return "en ";
549
+ case "enumMember":
550
+ return "em ";
551
+ case "method":
552
+ return "mt ";
553
+ case "property":
554
+ return "pr ";
555
+ case "namespace":
556
+ return "ns ";
557
+ case "trait":
558
+ return "tr ";
559
+ case "re-export":
560
+ return "rx ";
561
+ default:
562
+ return kind.slice(0, 4).padEnd(4);
563
+ }
564
+ }
@@ -0,0 +1,152 @@
1
+ import type { Command } from "commander";
2
+ import type { EmitContext } from "../commander.ts";
3
+ import {
4
+ emitCanonical,
5
+ normalizeHarness,
6
+ readHeartbeat,
7
+ resolveOwner,
8
+ } from "../core/agents/index.ts";
9
+ import {
10
+ ageSeconds,
11
+ applyDetection,
12
+ clearPresence,
13
+ formatAge,
14
+ type PresenceState,
15
+ presenceFilePath,
16
+ readPresence,
17
+ writePresence,
18
+ } from "../lib/presence.ts";
19
+
20
+ /**
21
+ * `presence`: mobile/office state for agents (~/.claude/presence).
22
+ *
23
+ * Uses the injected EmitContext so composed and standalone consumers share
24
+ * one code path. The state file path is Claude-Code-specific (lives under
25
+ * ~/.claude/) but the detect/set/clear logic is generic: any agent harness
26
+ * can adopt the same convention.
27
+ */
28
+ export function registerPresenceCommand(program: Command, emit: EmitContext): void {
29
+ const cmd = program
30
+ .command("presence")
31
+ .description("Mobile/office state for agents (~/.claude/presence)")
32
+ .action(() => {
33
+ printStatus(emit);
34
+ });
35
+
36
+ cmd
37
+ .command("get")
38
+ .description('Print just the state ("mobile" / "office"), or full record with --json')
39
+ .option("--json", "Print the full record as JSON")
40
+ .action((opts: { json?: boolean }) => {
41
+ const r = readPresence();
42
+ if (opts.json) {
43
+ emit.config({ format: "json" });
44
+ emit.data({
45
+ state: r.state,
46
+ updated_at: r.updated_at,
47
+ source: r.source,
48
+ is_default: r.is_default,
49
+ path: presenceFilePath(),
50
+ });
51
+ return;
52
+ }
53
+ emit.text(`${r.state}\n`);
54
+ });
55
+
56
+ cmd
57
+ .command("set")
58
+ .argument("<state>", "mobile | office")
59
+ .description("Set state explicitly (source=cli). Hook auto-detection can still overwrite.")
60
+ .action((state: string) => {
61
+ if (state !== "mobile" && state !== "office") {
62
+ emit.error({
63
+ code: "invalid_state",
64
+ message: `state must be "mobile" or "office" (got: ${JSON.stringify(state)})`,
65
+ });
66
+ process.exit(2);
67
+ }
68
+ const before = readPresence();
69
+ writePresence(state as PresenceState, "cli");
70
+ emitPresenceChange(before.state, state as PresenceState, "cli");
71
+ printStatus(emit);
72
+ });
73
+
74
+ cmd
75
+ .command("clear")
76
+ .description('Delete the state file (next read returns the default, "office")')
77
+ .action(() => {
78
+ const removed = clearPresence();
79
+ emit.data({
80
+ ok: true,
81
+ removed,
82
+ path: presenceFilePath(),
83
+ });
84
+ });
85
+
86
+ cmd
87
+ .command("detect")
88
+ .description(
89
+ "Internal: read prompt text from stdin, apply detection rules, update state if signal is clear",
90
+ )
91
+ .option("--from-stdin", "Read prompt from stdin (the only supported input mode today)")
92
+ .option("--verbose", "Print the detection result to stderr")
93
+ .action(async (opts: { fromStdin?: boolean; verbose?: boolean }) => {
94
+ const prompt = await readStdin();
95
+ if (!prompt) {
96
+ if (opts.verbose) emit.log("(no input on stdin; skipping)", "info");
97
+ return;
98
+ }
99
+ const result = applyDetection(prompt);
100
+ if (opts.verbose) {
101
+ emit.log(
102
+ JSON.stringify({
103
+ detected: result.detected,
104
+ before: result.before,
105
+ after: result.after,
106
+ changed: result.changed,
107
+ }),
108
+ "info",
109
+ );
110
+ }
111
+ });
112
+ }
113
+
114
+ function printStatus(emit: EmitContext): void {
115
+ const r = readPresence();
116
+ emit.data({
117
+ state: r.state,
118
+ updated_at: r.updated_at,
119
+ source: r.source,
120
+ is_default: r.is_default,
121
+ age_seconds: r.is_default ? null : ageSeconds(r.updated_at),
122
+ age_human: r.is_default ? "(default: file missing)" : formatAge(ageSeconds(r.updated_at)),
123
+ path: presenceFilePath(),
124
+ });
125
+ }
126
+
127
+ async function readStdin(): Promise<string> {
128
+ if (process.stdin.isTTY) return "";
129
+ const chunks: Buffer[] = [];
130
+ for await (const chunk of process.stdin) {
131
+ chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
132
+ }
133
+ return Buffer.concat(chunks).toString("utf8");
134
+ }
135
+
136
+ function emitPresenceChange(
137
+ from: PresenceState,
138
+ to: PresenceState,
139
+ source: "cli" | "hook" | "user",
140
+ ): void {
141
+ if (from === to) return;
142
+ const owner = resolveOwner();
143
+ if (!owner) return;
144
+ const hb = readHeartbeat(owner);
145
+ emitCanonical({
146
+ type: "state.presence_change",
147
+ owner,
148
+ session: hb?.session_id ?? owner,
149
+ harness: normalizeHarness(hb?.platform),
150
+ data: { from, to, source },
151
+ });
152
+ }