@phren/cli 0.2.15 → 0.3.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 (392) hide show
  1. package/README.md +2 -2
  2. package/dist/agent-instructions.d.ts +12 -0
  3. package/dist/agent-instructions.js +52 -0
  4. package/dist/bridge/agent-hook-stores.d.ts +32 -0
  5. package/dist/bridge/agent-hook-stores.js +67 -0
  6. package/dist/bridge/agent-hooks.d.ts +195 -2
  7. package/dist/bridge/agent-hooks.js +1049 -38
  8. package/dist/bridge/canary.d.ts +27 -0
  9. package/dist/bridge/canary.js +189 -0
  10. package/dist/bridge/changes.d.ts +57 -0
  11. package/dist/bridge/changes.js +403 -0
  12. package/dist/bridge/client.d.ts +3 -0
  13. package/dist/bridge/client.js +36 -0
  14. package/dist/bridge/code-note.d.ts +23 -0
  15. package/dist/bridge/code-note.js +51 -0
  16. package/dist/bridge/code-routes.d.ts +151 -0
  17. package/dist/bridge/code-routes.js +442 -0
  18. package/dist/bridge/codex-threads.d.ts +27 -0
  19. package/dist/bridge/codex-threads.js +442 -0
  20. package/dist/bridge/command.d.ts +7 -0
  21. package/dist/bridge/command.js +50 -5
  22. package/dist/bridge/computer-names.d.ts +4 -0
  23. package/dist/bridge/computer-names.js +16 -0
  24. package/dist/bridge/computers.d.ts +9 -0
  25. package/dist/bridge/computers.js +111 -0
  26. package/dist/bridge/context.d.ts +14 -0
  27. package/dist/bridge/context.js +149 -0
  28. package/dist/bridge/dispatch-command.d.ts +3 -0
  29. package/dist/bridge/dispatch-command.js +93 -0
  30. package/dist/bridge/dispatch-hosts.d.ts +23 -0
  31. package/dist/bridge/dispatch-hosts.js +25 -0
  32. package/dist/bridge/dispatch-returns.d.ts +125 -0
  33. package/dist/bridge/dispatch-returns.js +283 -0
  34. package/dist/bridge/dispatch-tree.d.ts +54 -0
  35. package/dist/bridge/dispatch-tree.js +198 -0
  36. package/dist/bridge/dispatch.d.ts +210 -0
  37. package/dist/bridge/dispatch.js +286 -0
  38. package/dist/bridge/enroll.d.ts +31 -0
  39. package/dist/bridge/enroll.js +236 -0
  40. package/dist/bridge/fanout-messages.d.ts +74 -0
  41. package/dist/bridge/fanout-messages.js +230 -0
  42. package/dist/bridge/fanouts.d.ts +160 -0
  43. package/dist/bridge/fanouts.js +702 -0
  44. package/dist/bridge/file-range.d.ts +16 -0
  45. package/dist/bridge/file-range.js +90 -0
  46. package/dist/bridge/files.d.ts +20 -0
  47. package/dist/bridge/files.js +54 -0
  48. package/dist/bridge/git-publish.d.ts +23 -0
  49. package/dist/bridge/git-publish.js +145 -0
  50. package/dist/bridge/git-worktrees.d.ts +21 -0
  51. package/dist/bridge/git-worktrees.js +150 -0
  52. package/dist/bridge/git.d.ts +54 -0
  53. package/dist/bridge/git.js +552 -0
  54. package/dist/bridge/grants.d.ts +31 -0
  55. package/dist/bridge/grants.js +181 -0
  56. package/dist/bridge/hand-off.d.ts +79 -0
  57. package/dist/bridge/hand-off.js +151 -0
  58. package/dist/bridge/health.d.ts +128 -0
  59. package/dist/bridge/health.js +260 -0
  60. package/dist/bridge/herdr.d.ts +79 -6
  61. package/dist/bridge/herdr.js +292 -27
  62. package/dist/bridge/hook-main.js +2 -0
  63. package/dist/bridge/install.d.ts +28 -1
  64. package/dist/bridge/install.js +225 -38
  65. package/dist/bridge/launch-worktree.d.ts +21 -0
  66. package/dist/bridge/launch-worktree.js +70 -0
  67. package/dist/bridge/limits.d.ts +20 -0
  68. package/dist/bridge/limits.js +65 -0
  69. package/dist/bridge/locate.d.ts +16 -0
  70. package/dist/bridge/locate.js +75 -0
  71. package/dist/bridge/metrics.d.ts +29 -0
  72. package/dist/bridge/metrics.js +71 -0
  73. package/dist/bridge/model-switch.d.ts +35 -0
  74. package/dist/bridge/model-switch.js +250 -0
  75. package/dist/bridge/models.d.ts +46 -0
  76. package/dist/bridge/models.js +257 -0
  77. package/dist/bridge/opencode-approvals.d.ts +6 -0
  78. package/dist/bridge/opencode-approvals.js +60 -0
  79. package/dist/bridge/pane-text.d.ts +15 -0
  80. package/dist/bridge/pane-text.js +23 -0
  81. package/dist/bridge/peers.d.ts +27 -0
  82. package/dist/bridge/peers.js +130 -0
  83. package/dist/bridge/projects.d.ts +12 -1
  84. package/dist/bridge/projects.js +268 -20
  85. package/dist/bridge/protocol.d.ts +35 -2
  86. package/dist/bridge/protocol.js +61 -7
  87. package/dist/bridge/push.d.ts +83 -0
  88. package/dist/bridge/push.js +199 -0
  89. package/dist/bridge/questions.d.ts +38 -0
  90. package/dist/bridge/questions.js +205 -0
  91. package/dist/bridge/schedule-format.d.ts +103 -0
  92. package/dist/bridge/schedule-format.js +266 -0
  93. package/dist/bridge/schedule-launch.d.ts +11 -0
  94. package/dist/bridge/schedule-launch.js +169 -0
  95. package/dist/bridge/schedule-watch.d.ts +54 -0
  96. package/dist/bridge/schedule-watch.js +255 -0
  97. package/dist/bridge/schedules.d.ts +48 -0
  98. package/dist/bridge/schedules.js +205 -0
  99. package/dist/bridge/server-launch.d.ts +14 -0
  100. package/dist/bridge/server-launch.js +218 -0
  101. package/dist/bridge/server-overview.d.ts +45 -0
  102. package/dist/bridge/server-overview.js +102 -0
  103. package/dist/bridge/server-pane-routes.d.ts +36 -0
  104. package/dist/bridge/server-pane-routes.js +479 -0
  105. package/dist/bridge/server-routes.d.ts +108 -0
  106. package/dist/bridge/server-routes.js +556 -0
  107. package/dist/bridge/server-stream.d.ts +46 -0
  108. package/dist/bridge/server-stream.js +253 -0
  109. package/dist/bridge/server.d.ts +3 -15
  110. package/dist/bridge/server.js +162 -264
  111. package/dist/bridge/session-servers.d.ts +24 -0
  112. package/dist/bridge/session-servers.js +136 -0
  113. package/dist/bridge/side-questions.d.ts +74 -0
  114. package/dist/bridge/side-questions.js +252 -0
  115. package/dist/bridge/simulators.d.ts +28 -0
  116. package/dist/bridge/simulators.js +198 -0
  117. package/dist/bridge/speech-transcribe.d.ts +24 -0
  118. package/dist/bridge/speech-transcribe.js +124 -0
  119. package/dist/bridge/speech.d.ts +35 -0
  120. package/dist/bridge/speech.js +98 -0
  121. package/dist/bridge/steps.d.ts +13 -0
  122. package/dist/bridge/steps.js +222 -0
  123. package/dist/bridge/tab-activity.d.ts +18 -0
  124. package/dist/bridge/tab-activity.js +99 -0
  125. package/dist/bridge/terminal-choice.d.ts +64 -0
  126. package/dist/bridge/terminal-choice.js +281 -0
  127. package/dist/bridge/transcript-claude.d.ts +22 -0
  128. package/dist/bridge/transcript-claude.js +353 -0
  129. package/dist/bridge/transcript-codex.d.ts +30 -0
  130. package/dist/bridge/transcript-codex.js +282 -0
  131. package/dist/bridge/transcript-copilot.d.ts +4 -0
  132. package/dist/bridge/transcript-copilot.js +14 -0
  133. package/dist/bridge/transcript-opencode.d.ts +5 -0
  134. package/dist/bridge/transcript-opencode.js +26 -0
  135. package/dist/bridge/transcript-preview.d.ts +96 -0
  136. package/dist/bridge/transcript-preview.js +491 -0
  137. package/dist/bridge/transcripts.d.ts +81 -4
  138. package/dist/bridge/transcripts.js +332 -67
  139. package/dist/bridge/transport.d.ts +3 -0
  140. package/dist/bridge/transport.js +96 -15
  141. package/dist/bridge/uploads.d.ts +15 -0
  142. package/dist/bridge/uploads.js +44 -2
  143. package/dist/bridge/usage.d.ts +124 -0
  144. package/dist/bridge/usage.js +859 -0
  145. package/dist/bridge-hook.mjs +46545 -21051
  146. package/dist/cli/actions.js +14 -1
  147. package/dist/cli/config-policy.js +25 -0
  148. package/dist/cli/config-proactivity.d.ts +3 -1
  149. package/dist/cli/config-proactivity.js +35 -3
  150. package/dist/cli/config.js +4 -1
  151. package/dist/cli/extract.d.ts +0 -8
  152. package/dist/cli/extract.js +4 -7
  153. package/dist/cli/govern.js +5 -2
  154. package/dist/cli/hooks-globs.js +1 -1
  155. package/dist/cli/hooks.d.ts +1 -1
  156. package/dist/cli/hooks.js +9 -7
  157. package/dist/cli/namespaces-projects.js +12 -6
  158. package/dist/cli/namespaces-store.js +37 -6
  159. package/dist/cli/namespaces-tasks.js +1 -1
  160. package/dist/cli/schedules.d.ts +1 -0
  161. package/dist/cli/schedules.js +164 -0
  162. package/dist/cli/session-background.js +1 -1
  163. package/dist/cli/session-git.d.ts +8 -5
  164. package/dist/cli/session-git.js +45 -120
  165. package/dist/cli/session-start.js +3 -1
  166. package/dist/cli/session-stop.js +19 -8
  167. package/dist/cli/team.js +13 -10
  168. package/dist/cli-handlers.d.ts +1 -1
  169. package/dist/cli-handlers.js +8 -4
  170. package/dist/cli-help.d.ts +5 -4
  171. package/dist/cli-help.js +15 -15
  172. package/dist/cli-registry.d.ts +4 -1
  173. package/dist/cli-registry.js +119 -6
  174. package/dist/content/archive.js +2 -2
  175. package/dist/content/citation.d.ts +11 -0
  176. package/dist/content/citation.js +18 -0
  177. package/dist/content/learning.js +2 -0
  178. package/dist/content/summarize.d.ts +14 -15
  179. package/dist/content/summarize.js +60 -5
  180. package/dist/content/validate.d.ts +17 -4
  181. package/dist/content/validate.js +200 -122
  182. package/dist/core/project-registry.d.ts +26 -0
  183. package/dist/core/project-registry.js +573 -0
  184. package/dist/core/project.js +2 -3
  185. package/dist/data/access.d.ts +16 -1
  186. package/dist/data/access.js +31 -3
  187. package/dist/data/tasks.d.ts +18 -3
  188. package/dist/data/tasks.js +8 -7
  189. package/dist/entrypoint.js +44 -10
  190. package/dist/fanout/adapters/claude.d.ts +2 -0
  191. package/dist/fanout/adapters/claude.js +7 -0
  192. package/dist/fanout/adapters/codex.d.ts +2 -0
  193. package/dist/fanout/adapters/codex.js +9 -0
  194. package/dist/fanout/adapters/opencode.d.ts +4 -0
  195. package/dist/fanout/adapters/opencode.js +9 -0
  196. package/dist/fanout/adapters/types.d.ts +21 -0
  197. package/dist/fanout/adapters/types.js +3 -0
  198. package/dist/fanout/command.d.ts +2 -0
  199. package/dist/fanout/command.js +78 -0
  200. package/dist/fanout/launcher.d.ts +27 -0
  201. package/dist/fanout/launcher.js +156 -0
  202. package/dist/fanout/opencode-serve.d.ts +24 -0
  203. package/dist/fanout/opencode-serve.js +191 -0
  204. package/dist/fanout/picker.d.ts +91 -0
  205. package/dist/fanout/picker.js +51 -0
  206. package/dist/fanout/usage.d.ts +5 -0
  207. package/dist/fanout/usage.js +51 -0
  208. package/dist/fanout/watchdog.d.ts +10 -0
  209. package/dist/fanout/watchdog.js +27 -0
  210. package/dist/filenames.d.ts +9 -0
  211. package/dist/filenames.js +11 -0
  212. package/dist/finding/context.js +4 -22
  213. package/dist/generated/memory-ui-graph.browser.js +525 -419
  214. package/dist/governance/policy-constants.d.ts +11 -0
  215. package/dist/governance/policy-constants.js +9 -0
  216. package/dist/governance/policy.d.ts +5 -8
  217. package/dist/governance/policy.js +11 -8
  218. package/dist/graph-core/camera.d.ts +24 -0
  219. package/dist/graph-core/camera.js +18 -0
  220. package/dist/graph-core/index.d.ts +2 -0
  221. package/dist/graph-core/index.js +2 -0
  222. package/dist/graph-core/labels.d.ts +77 -0
  223. package/dist/graph-core/labels.js +124 -0
  224. package/dist/graph-core/model.d.ts +13 -0
  225. package/dist/graph-core/model.js +33 -0
  226. package/dist/graph-core/types.d.ts +2 -0
  227. package/dist/home-paths.d.ts +7 -0
  228. package/dist/home-paths.js +21 -0
  229. package/dist/hooks.d.ts +3 -15
  230. package/dist/hooks.js +14 -1
  231. package/dist/index.js +24 -2
  232. package/dist/init/guard-globals.d.ts +1 -1
  233. package/dist/init/guard-globals.js +4 -4
  234. package/dist/init/init-configure.js +3 -3
  235. package/dist/init/init-uninstall.js +15 -5
  236. package/dist/init/init-walkthrough.js +1 -1
  237. package/dist/init/init.js +26 -6
  238. package/dist/init/management-preset.d.ts +1 -1
  239. package/dist/init/management-preset.js +2 -2
  240. package/dist/init/preferences.js +1 -1
  241. package/dist/init/self-wiring.js +1 -1
  242. package/dist/init/setup.d.ts +3 -26
  243. package/dist/init/setup.js +68 -593
  244. package/dist/init/shared.js +2 -0
  245. package/dist/init/store-gitignore.d.ts +1 -6
  246. package/dist/init/store-gitignore.js +2 -8
  247. package/dist/init/teardown.d.ts +1 -1
  248. package/dist/init/teardown.js +2 -2
  249. package/dist/link/context-imports.d.ts +15 -0
  250. package/dist/link/context-imports.js +101 -0
  251. package/dist/link/context.js +1 -1
  252. package/dist/link/doctor.d.ts +22 -1
  253. package/dist/link/doctor.js +128 -58
  254. package/dist/link/link.js +29 -61
  255. package/dist/link/refresh.js +2 -2
  256. package/dist/link/skills.js +26 -5
  257. package/dist/machine-identity.d.ts +3 -0
  258. package/dist/machine-identity.js +6 -0
  259. package/dist/mcp/profile.d.ts +2 -19
  260. package/dist/mcp/profile.js +64 -22
  261. package/dist/memory-ui-graph.runtime.js +525 -419
  262. package/dist/modules/code-package.d.ts +16 -0
  263. package/dist/modules/code-package.js +160 -0
  264. package/dist/modules/command.d.ts +2 -0
  265. package/dist/modules/command.js +67 -0
  266. package/dist/modules/config.d.ts +4 -0
  267. package/dist/modules/config.js +70 -0
  268. package/dist/modules/manifest.d.ts +41 -0
  269. package/dist/modules/manifest.js +1 -0
  270. package/dist/modules/provision.d.ts +3 -0
  271. package/dist/modules/provision.js +84 -0
  272. package/dist/modules/registry.d.ts +16 -0
  273. package/dist/modules/registry.js +250 -0
  274. package/dist/modules/runtime.d.ts +15 -0
  275. package/dist/modules/runtime.js +59 -0
  276. package/dist/package-metadata.js +10 -2
  277. package/dist/phren-dotenv.js +4 -2
  278. package/dist/phren-paths.d.ts +3 -2
  279. package/dist/phren-paths.js +4 -5
  280. package/dist/profile-store.js +43 -8
  281. package/dist/project-config.d.ts +19 -1
  282. package/dist/project-config.js +29 -3
  283. package/dist/project-topics.d.ts +1 -0
  284. package/dist/project-topics.js +13 -10
  285. package/dist/provider-adapters.js +1 -4
  286. package/dist/query-correlation.js +6 -6
  287. package/dist/session/artifacts.js +5 -5
  288. package/dist/session/checkpoints.js +5 -0
  289. package/dist/session/utils.d.ts +2 -2
  290. package/dist/session/utils.js +6 -6
  291. package/dist/shared/content.d.ts +1 -1
  292. package/dist/shared/content.js +1 -1
  293. package/dist/shared/data-utils.js +1 -1
  294. package/dist/shared/fragment-graph.d.ts +1 -1
  295. package/dist/shared/fragment-graph.js +5 -5
  296. package/dist/shared/index.js +48 -7
  297. package/dist/shared/retrieval.d.ts +18 -3
  298. package/dist/shared/retrieval.js +101 -12
  299. package/dist/shared/search-fallback.js +4 -6
  300. package/dist/shared/sqljs.d.ts +2 -0
  301. package/dist/shared/sqljs.js +4 -2
  302. package/dist/shared.d.ts +0 -1
  303. package/dist/shared.js +0 -1
  304. package/dist/shell/input.d.ts +1 -5
  305. package/dist/shell/input.js +16 -10
  306. package/dist/shell/palette.d.ts +2 -2
  307. package/dist/shell/palette.js +3 -7
  308. package/dist/shell/render.d.ts +3 -1
  309. package/dist/shell/render.js +4 -4
  310. package/dist/shell/shell.js +4 -1
  311. package/dist/shell/types.d.ts +1 -0
  312. package/dist/shell/types.js +4 -0
  313. package/dist/shell/view.d.ts +0 -4
  314. package/dist/shell/view.js +19 -27
  315. package/dist/skill/registry.js +25 -17
  316. package/dist/skill/state.d.ts +3 -0
  317. package/dist/skill/state.js +13 -7
  318. package/dist/status.js +38 -17
  319. package/dist/store-registry.d.ts +44 -18
  320. package/dist/store-registry.js +169 -60
  321. package/dist/store-weight.d.ts +1 -6
  322. package/dist/store-weight.js +4 -2
  323. package/dist/sync/auth-doctor.d.ts +8 -0
  324. package/dist/sync/auth-doctor.js +27 -0
  325. package/dist/sync/auth.d.ts +27 -0
  326. package/dist/sync/auth.js +114 -0
  327. package/dist/sync/conflict-resolve.d.ts +9 -0
  328. package/dist/sync/conflict-resolve.js +146 -0
  329. package/dist/sync/git-state.d.ts +4 -0
  330. package/dist/sync/git-state.js +49 -0
  331. package/dist/sync/outcome.d.ts +34 -0
  332. package/dist/sync/outcome.js +57 -0
  333. package/dist/sync/pull.d.ts +15 -7
  334. package/dist/sync/pull.js +84 -35
  335. package/dist/sync/store-merge.d.ts +29 -0
  336. package/dist/sync/store-merge.js +137 -0
  337. package/dist/sync/task-merge.d.ts +22 -0
  338. package/dist/sync/task-merge.js +158 -0
  339. package/dist/task/github.js +1 -1
  340. package/dist/task/lifecycle.d.ts +5 -0
  341. package/dist/task/lifecycle.js +67 -23
  342. package/dist/terminal-text.d.ts +5 -0
  343. package/dist/terminal-text.js +11 -0
  344. package/dist/test-global-setup.js +7 -0
  345. package/dist/tool-registry.d.ts +1 -1
  346. package/dist/tool-registry.js +3 -1
  347. package/dist/tools/code.d.ts +3 -0
  348. package/dist/tools/code.js +186 -0
  349. package/dist/tools/config.js +8 -0
  350. package/dist/tools/data.js +14 -7
  351. package/dist/tools/dispatch.d.ts +2 -0
  352. package/dist/tools/dispatch.js +66 -0
  353. package/dist/tools/finding.d.ts +11 -0
  354. package/dist/tools/finding.js +180 -123
  355. package/dist/tools/ops.js +5 -2
  356. package/dist/tools/search.js +21 -8
  357. package/dist/tools/session.js +21 -22
  358. package/dist/tools/tasks.js +8 -2
  359. package/dist/tools/types.d.ts +2 -2
  360. package/dist/tools/types.js +2 -2
  361. package/dist/ui/assets.js +1 -1
  362. package/dist/ui/data.js +14 -15
  363. package/dist/ui/deep-void.d.ts +1 -1
  364. package/dist/ui/deep-void.js +0 -1
  365. package/dist/ui/graph-chrome.d.ts +1 -1
  366. package/dist/ui/graph-chrome.js +51 -0
  367. package/dist/ui/page.js +5 -5
  368. package/dist/ui/scripts.js +67 -30
  369. package/dist/ui/server.js +52 -7
  370. package/dist/update.js +22 -11
  371. package/dist/utils-fts.js +1 -0
  372. package/dist/utils-helpers.d.ts +7 -0
  373. package/dist/utils-helpers.js +17 -0
  374. package/dist/utils.d.ts +1 -1
  375. package/dist/utils.js +1 -1
  376. package/package.json +49 -7
  377. package/plugins/opencode/phren-transcript.js +340 -0
  378. package/starter/README.md +4 -4
  379. package/starter/global/{CLAUDE.md → AGENTS.md} +2 -2
  380. package/starter/global/skills/conductor/SKILL.md +106 -0
  381. package/starter/global/skills/fanout/SKILL.md +20 -0
  382. package/starter/global/skills/phren-discover/SKILL.md +6 -6
  383. package/starter/global/skills/phren-init/SKILL.md +4 -4
  384. package/starter/global/skills/phren-sync/SKILL.md +3 -3
  385. package/starter/templates/frontend/reference/README.md +1 -1
  386. package/starter/templates/library/reference/README.md +1 -1
  387. package/starter/templates/monorepo/reference/README.md +1 -1
  388. package/starter/templates/python-project/reference/README.md +1 -1
  389. /package/starter/templates/frontend/{CLAUDE.md → AGENTS.md} +0 -0
  390. /package/starter/templates/library/{CLAUDE.md → AGENTS.md} +0 -0
  391. /package/starter/templates/monorepo/{CLAUDE.md → AGENTS.md} +0 -0
  392. /package/starter/templates/python-project/{CLAUDE.md → AGENTS.md} +0 -0
@@ -0,0 +1,353 @@
1
+ import { readdir, readFile, realpath, stat } from "node:fs/promises";
2
+ import { createReadStream } from "node:fs";
3
+ import { createInterface } from "node:readline";
4
+ import { createHash } from "node:crypto";
5
+ import path from "node:path";
6
+ import { object, objects } from "./protocol.js";
7
+ /** Claude Code's transcript reader: Task sidechains and named teammates as
8
+ * child agents, and the public rows of a conversation. */
9
+ const claudeRelationCache = new Map();
10
+ const claudeChildModelCache = new Map();
11
+ function cacheClaudeChildModel(file, entry) {
12
+ claudeChildModelCache.set(file, entry);
13
+ while (claudeChildModelCache.size > 128)
14
+ claudeChildModelCache.delete(claudeChildModelCache.keys().next().value);
15
+ }
16
+ /** The model is available on the first assistant turn of a Claude sidechain.
17
+ * A growing transcript without that turn is retried; a known model is final. */
18
+ async function claudeChildModel(file) {
19
+ const metadata = await stat(file), cached = claudeChildModelCache.get(file);
20
+ if (cached && cached.dev === metadata.dev && cached.ino === metadata.ino
21
+ && (cached.model !== undefined || metadata.size <= cached.size))
22
+ return cached.model;
23
+ let model;
24
+ if (metadata.size > 0) {
25
+ const bytes = Math.min(metadata.size, 65_536);
26
+ const input = createInterface({ input: createReadStream(file, { start: 0, end: bytes - 1 }), crlfDelay: Infinity });
27
+ let lines = 0;
28
+ for await (const line of input) {
29
+ ++lines;
30
+ try {
31
+ const raw = object(JSON.parse(line));
32
+ if (raw.type === "assistant") {
33
+ const value = object(raw.message).model;
34
+ if (typeof value === "string" && value.length > 0 && value.length <= 200)
35
+ model = value;
36
+ break;
37
+ }
38
+ }
39
+ catch { /* Ignore malformed rows while looking for the first assistant turn. */ }
40
+ if (lines === 200)
41
+ break;
42
+ }
43
+ }
44
+ cacheClaudeChildModel(file, { dev: metadata.dev, ino: metadata.ino, size: metadata.size, ...(model !== undefined ? { model } : {}) });
45
+ return model;
46
+ }
47
+ /** The checkout a Claude sub-agent edits in. Claude Code writes the isolated
48
+ * worktree into the child's `.meta.json`; an older child falls back to the
49
+ * `cwd` its own first rows record when that folder is a linked worktree (its
50
+ * `.git` is a file). A child working in the parent's checkout gets nothing,
51
+ * and a worktree already removed is not offered. */
52
+ export async function claudeChildCheckout(file) {
53
+ const meta = await readFile(file.slice(0, -".jsonl".length) + ".meta.json", "utf8").then(v => object(JSON.parse(v))).catch(() => ({}));
54
+ let cwd = typeof meta.worktreePath === "string" && path.isAbsolute(meta.worktreePath) ? meta.worktreePath : undefined;
55
+ let branch = typeof meta.worktreeBranch === "string" && meta.worktreeBranch ? meta.worktreeBranch.slice(0, 200) : undefined;
56
+ if (!cwd) {
57
+ const input = createInterface({ input: createReadStream(file, { start: 0, end: 65_535 }), crlfDelay: Infinity });
58
+ let lines = 0;
59
+ for await (const line of input) {
60
+ try {
61
+ const value = object(JSON.parse(line)).cwd;
62
+ if (typeof value === "string" && path.isAbsolute(value)) {
63
+ if ((await stat(path.join(value, ".git")).catch(() => undefined))?.isFile())
64
+ cwd = value;
65
+ break;
66
+ }
67
+ }
68
+ catch { /* Keep looking past a malformed row. */ }
69
+ if (++lines >= 40)
70
+ break;
71
+ }
72
+ input.close();
73
+ branch = undefined;
74
+ }
75
+ if (!cwd || !(await stat(cwd).catch(() => undefined))?.isDirectory())
76
+ return {};
77
+ return { cwd, worktreeName: path.basename(cwd).slice(0, 200), ...(branch ? { branch } : {}) };
78
+ }
79
+ async function withClaudeChildModels(relations) {
80
+ return Promise.all(relations.map(async (relation) => {
81
+ if (relation.model !== undefined || relation.transcript === undefined)
82
+ return relation;
83
+ const model = await claudeChildModel(relation.transcript).catch(() => undefined);
84
+ return model === undefined ? relation : { ...relation, model };
85
+ }));
86
+ }
87
+ export async function claudeChildAgents(file, session) {
88
+ const metadata = await stat(file), signature = `${metadata.dev}:${metadata.ino}:${metadata.size}:${metadata.mtimeMs}`;
89
+ const cached = claudeRelationCache.get(file);
90
+ if (cached?.signature === signature && !(cached.recheckAt !== undefined && Date.now() >= cached.recheckAt)) {
91
+ cached.relations = await withClaudeChildModels(cached.relations);
92
+ return cached.relations;
93
+ }
94
+ const launches = new Map();
95
+ // Named teammates (the Agent tool with a `name`) run as their own session
96
+ // and never post a task-notification: they announce themselves idle in a
97
+ // teammate-message instead, and may be woken again later. Their file is
98
+ // `agent-a<name>-<hex>.jsonl` beside the Task sidechains.
99
+ const teammates = new Map();
100
+ const lines = createInterface({ input: createReadStream(file, { encoding: "utf8" }), crlfDelay: Infinity });
101
+ for await (const line of lines) {
102
+ try {
103
+ const raw = object(JSON.parse(line)), result = object(raw.toolUseResult);
104
+ const agentId = String(result.agentId ?? ""), status = String(result.status ?? "");
105
+ if (/^[A-Za-z0-9._-]{1,128}$/.test(agentId) && ["async_launched", "running"].includes(status)) {
106
+ const blocks = objects(object(raw.message).content), callId = String(blocks.find(b => b.type === "tool_result")?.tool_use_id ?? "");
107
+ if (callId)
108
+ launches.set(agentId, { path: String(result.description || result.name || "Agent").slice(0, 200), callId, state: "running" });
109
+ }
110
+ if (raw.type === "assistant") {
111
+ for (const block of objects(object(raw.message).content)) {
112
+ if (block.type !== "tool_use" || block.name !== "Agent")
113
+ continue;
114
+ const input = object(block.input), name = String(input.name ?? "");
115
+ if (!/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(name) || typeof block.id !== "string")
116
+ continue;
117
+ teammates.set(name, { path: String(input.description || name).slice(0, 200), callId: block.id.slice(0, 200), state: "running" });
118
+ }
119
+ }
120
+ const content = typeof raw.content === "string" ? raw.content : typeof object(raw.message).content === "string" ? String(object(raw.message).content) : "";
121
+ if (content.includes("<task-notification>")) {
122
+ const child = /<task-id>([^<>]{1,128})<\/task-id>/.exec(content)?.[1], taskStatus = /<status>([^<>]+)<\/status>/.exec(content)?.[1];
123
+ // A stopped agent's notification says "killed"; it is finished as
124
+ // much as a completed one and must leave the running count.
125
+ const previous = child && launches.get(child);
126
+ if (previous && ["completed", "failed", "cancelled", "killed"].includes(taskStatus ?? ""))
127
+ previous.state = "completed";
128
+ }
129
+ if (content.includes("<teammate-message")) {
130
+ const from = /<teammate-message teammate_id="([A-Za-z0-9][A-Za-z0-9_-]{0,63})"/.exec(content)?.[1];
131
+ const teammate = from && teammates.get(from);
132
+ if (teammate)
133
+ teammate.state = content.includes("\"type\":\"idle_notification\"") ? "completed" : "running";
134
+ }
135
+ }
136
+ catch { /* Ignore unrelated/malformed rows. */ }
137
+ }
138
+ const relations = [];
139
+ // Claude Code records the launch in the parent before the child's own
140
+ // file exists. A launch without a transcript yet is looked for again
141
+ // shortly, rather than being missed until the parent next changes.
142
+ let awaiting = false;
143
+ const root = await realpath(path.join(path.dirname(file), session, "subagents")).catch(() => undefined);
144
+ for (const [agentId, launch] of launches) {
145
+ const childFile = root && await realpath(path.join(root, `agent-${agentId}.jsonl`)).catch(() => undefined);
146
+ if (!childFile || !childFile.startsWith(root + path.sep) || !await claudeChildBelongsTo(childFile, session, agentId)) {
147
+ if (launch.state === "running")
148
+ awaiting = true;
149
+ continue;
150
+ }
151
+ const model = await claudeChildModel(childFile).catch(() => undefined);
152
+ const checkout = await claudeChildCheckout(childFile).catch(() => ({}));
153
+ relations.push({ id: createHash("sha256").update(`claude\0${session}\0${agentId}`).digest("hex").slice(0, 32),
154
+ session: agentId, transcript: childFile, provider: "claude", ...launch,
155
+ ...(model !== undefined ? { model } : {}), ...checkout, children: [] });
156
+ }
157
+ if (teammates.size && root) {
158
+ const names = (await readdir(root).catch(() => [])).filter(n => /^agent-a[A-Za-z0-9][A-Za-z0-9_-]{0,63}-[0-9a-f]{8,32}\.jsonl$/.test(n));
159
+ for (const [name, launch] of teammates) {
160
+ const fileName = names.find(n => n.startsWith(`agent-a${name}-`));
161
+ const agentId = fileName?.slice("agent-".length, -".jsonl".length);
162
+ const childFile = agentId && await realpath(path.join(root, fileName)).catch(() => undefined);
163
+ if (!agentId || !childFile || !childFile.startsWith(root + path.sep) || !await claudeChildBelongsTo(childFile, session, agentId)) {
164
+ if (launch.state === "running")
165
+ awaiting = true;
166
+ continue;
167
+ }
168
+ // The meta file names the model as the launcher chose it ("sonnet");
169
+ // the transcript's first assistant turn carries the full id and wins.
170
+ const meta = await readFile(childFile.slice(0, -".jsonl".length) + ".meta.json", "utf8").then(v => object(JSON.parse(v))).catch(() => ({}));
171
+ const model = await claudeChildModel(childFile).catch(() => undefined) ?? (typeof meta.model === "string" && meta.model ? meta.model.slice(0, 200) : undefined);
172
+ const checkout = await claudeChildCheckout(childFile).catch(() => ({}));
173
+ relations.push({ id: createHash("sha256").update(`claude\0${session}\0${agentId}`).digest("hex").slice(0, 32),
174
+ session: agentId, transcript: childFile, provider: "claude", ...launch,
175
+ ...(model !== undefined ? { model } : {}), ...checkout, children: [] });
176
+ }
177
+ }
178
+ claudeRelationCache.set(file, { signature, relations, ...(awaiting ? { recheckAt: Date.now() + 2_000 } : {}) });
179
+ while (claudeRelationCache.size > 64)
180
+ claudeRelationCache.delete(claudeRelationCache.keys().next().value);
181
+ return relations;
182
+ }
183
+ async function claudeChildBelongsTo(file, parent, agentId) {
184
+ let bytes = Buffer.alloc(0);
185
+ for await (const chunk of createReadStream(file, { start: 0, end: 1_048_575 })) {
186
+ bytes = Buffer.concat([bytes, Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)]);
187
+ const newline = bytes.indexOf(0x0a);
188
+ if (newline >= 0) {
189
+ bytes = bytes.subarray(0, newline);
190
+ break;
191
+ }
192
+ }
193
+ try {
194
+ const raw = object(JSON.parse(bytes.toString("utf8")));
195
+ return (raw.isSidechain === true && raw.sessionId === parent && raw.agentId === agentId)
196
+ || (raw.type === "fork-context-ref" && raw.parentSessionId === parent && raw.agentId === agentId);
197
+ }
198
+ catch {
199
+ return false;
200
+ }
201
+ }
202
+ const CLAUDE_KEYS = new Set(["type", "uuid", "parentUuid", "timestamp", "message", "gitBranch", "cwd", "requestId", "isMeta", "isSidechain", "isCompactSummary", "phrenQueued", "phrenQueueKey", "phrenBackground", "phrenCompacted"]);
203
+ export const harnessPreamble = (text) => /^<(?:environment_context>|user_instructions>|permission_profile|system-reminder>|turn_context>)/.test(text.trimStart());
204
+ function taskNotification(content) {
205
+ const envelope = /<task-notification>([\s\S]*?)<\/task-notification>/.exec(content)?.[1];
206
+ if (!envelope)
207
+ return;
208
+ // Only plain tag values are public. Never copy nested output envelopes.
209
+ const values = new Map();
210
+ for (const match of envelope.matchAll(/<([a-z-]+)(?:\s[^<>]*)?>([\s\S]*?)<\/\1>/g)) {
211
+ if (["task-id", "tool-use-id", "status", "summary"].includes(match[1]) && !match[2].includes("<") && !values.has(match[1]))
212
+ values.set(match[1], match[2]);
213
+ }
214
+ const tags = ["task-id", "tool-use-id", "status", "summary"].flatMap(tag => {
215
+ const value = values.get(tag);
216
+ return value === undefined ? [] : [`<${tag}>${value.slice(0, tag === "summary" ? 500 : 200)}</${tag}>`];
217
+ });
218
+ return tags.some(tag => tag.startsWith("<tool-use-id>")) ? `<task-notification>\n${tags.join("\n")}\n</task-notification>` : undefined;
219
+ }
220
+ /** Claude Code files bracketed-paste input — which is how Herdr's
221
+ * `agent.prompt` delivers every message the phone sends — as
222
+ * `<pasted_content id="…">…</pasted_content id="…">`. The wrapper is the
223
+ * terminal's bookkeeping, not what the user wrote. */
224
+ const PASTED_CONTENT = /<pasted_content\b[^>]*>\n?([\s\S]*?)\n?<\/pasted_content\b[^>]*>/g;
225
+ export function unwrapPastedContent(text) {
226
+ return text.includes("<pasted_content") ? text.replace(PASTED_CONTENT, "$1").trim() : text;
227
+ }
228
+ function unwrapUserText(message) {
229
+ if (message.role !== "user")
230
+ return message;
231
+ if (typeof message.content === "string")
232
+ return { ...message, content: unwrapPastedContent(message.content) };
233
+ if (!Array.isArray(message.content))
234
+ return message;
235
+ return { ...message, content: message.content.map(block => {
236
+ const b = object(block);
237
+ return b.type === "text" && typeof b.text === "string" ? { ...b, text: unwrapPastedContent(b.text) } : block;
238
+ }) };
239
+ }
240
+ /** A thinking block Claude marks as narration for the person watching: its
241
+ * signature is a length-prefixed field reading "narration" (private
242
+ * reasoning reads "thinking" and is stored without text). */
243
+ export function isNarration(block) {
244
+ if (block.type !== "thinking" || typeof block.thinking !== "string" || !block.thinking.trim())
245
+ return false;
246
+ if (typeof block.signature !== "string" || block.signature.length > 16_384)
247
+ return false;
248
+ let bytes;
249
+ try {
250
+ bytes = Buffer.from(block.signature, "base64");
251
+ }
252
+ catch {
253
+ return false;
254
+ }
255
+ return bytes.subarray(0, 96).includes(Buffer.from([0x42, 0x09, ...Buffer.from("narration")]));
256
+ }
257
+ /** The largest injection the phone is sent; the hook's own budget keeps real ones far smaller. */
258
+ const HOOK_CONTEXT_LIMIT = 16_384;
259
+ /** A phren UserPromptSubmit injection, as `{ type: "phren_hook_context",
260
+ * parentUuid, content }`, or undefined for any other row. */
261
+ export function phrenHookContext(raw, includeSidechain = false) {
262
+ if (raw.type !== "attachment" || (raw.isSidechain && !includeSidechain))
263
+ return undefined;
264
+ const attachment = object(raw.attachment);
265
+ // Input Claude takes mid-turn arrives as a queued_command row right before
266
+ // its hook output. Only whether it was a background notification crosses,
267
+ // so the phone can leave a notification's injection out.
268
+ if (attachment.type === "queued_command") {
269
+ return { type: "phren_turn_input", timestamp: raw.timestamp, notification: attachment.commandMode === "task-notification" };
270
+ }
271
+ if (attachment.type !== "hook_success" || attachment.hookEvent !== "UserPromptSubmit")
272
+ return undefined;
273
+ const content = typeof attachment.content === "string" ? attachment.content : "";
274
+ if (!/^\s*◆ phren\b/.test(content) && !content.includes("<phren-context>"))
275
+ return undefined;
276
+ return { type: "phren_hook_context", timestamp: raw.timestamp, uuid: raw.uuid, parentUuid: raw.parentUuid,
277
+ content: content.length > HOOK_CONTEXT_LIMIT ? content.slice(0, HOOK_CONTEXT_LIMIT) + "\n…" : content };
278
+ }
279
+ /** Claude Code rows the phone may see: user, assistant and system turns with
280
+ * private reasoning redacted, queued phone messages, background task
281
+ * notifications and compaction markers. */
282
+ export function visibleClaudeEvent(raw, includeSidechain = false) {
283
+ // A queued phone message carries the same wrapper; unwrap before the
284
+ // digest so enqueue and remove keep matching keys.
285
+ if (raw.type === "queue-operation" && typeof raw.content === "string")
286
+ raw = { ...raw, content: unwrapPastedContent(raw.content) };
287
+ if (raw.type === "queue-operation" && raw.operation === "remove" && typeof raw.content === "string") {
288
+ return { type: "phren_queue_consumed", key: createHash("sha256").update(raw.content).digest("hex"), timestamp: raw.timestamp };
289
+ }
290
+ // Claude Code records background completion as an internal queue row,
291
+ // outside the ordinary user/assistant transcript. Export only the small
292
+ // task-notification envelope; other internal events remain private.
293
+ if (raw.type === "queue-operation" && typeof raw.content === "string"
294
+ && raw.content.length <= 65_536 && raw.content.includes("<task-notification>")
295
+ && raw.content.includes("<tool-use-id>")) {
296
+ const content = taskNotification(raw.content);
297
+ return content ? { type: "system", phrenBackground: true, timestamp: raw.timestamp,
298
+ message: { role: "user", content } } : undefined;
299
+ }
300
+ // A message sent while the agent was mid-turn is only ever a queue row:
301
+ // Claude Code hands it to the model inside a later tool result and never
302
+ // writes a user turn for it. Export the enqueue as the person's message so
303
+ // the phone can draw the bubble it sent. Consumption exposes only a digest.
304
+ if (raw.type === "queue-operation" && ["enqueue", "remove"].includes(String(raw.operation))
305
+ && !raw.isMeta && !raw.isSidechain && typeof raw.content === "string"
306
+ && raw.content.length <= 65_536 && !raw.content.includes("<task-notification>")) {
307
+ // The paste wrapper was removed above; any envelope still starting
308
+ // with "<" is the harness's own and stays private.
309
+ if (raw.content.trimStart().startsWith("<"))
310
+ return undefined;
311
+ const text = raw.content;
312
+ const key = createHash("sha256").update(raw.content).digest("hex");
313
+ // Only the identity crosses the wire on consumption: no queue payload,
314
+ // tool envelope, private metadata, or reasoning is exported.
315
+ if (raw.operation === "remove")
316
+ return { type: "phren_queue_consumed", key, timestamp: raw.timestamp };
317
+ return { type: "user", phrenQueued: true, phrenQueueKey: key, timestamp: raw.timestamp,
318
+ message: { role: "user", content: text } };
319
+ }
320
+ // Claude Code appends a boundary marker and then the summary it hands the
321
+ // model as a user turn. The phone shows the marker and a bounded preview,
322
+ // never the full summary as a bubble.
323
+ if (raw.type === "system" && raw.subtype === "compact_boundary") {
324
+ return { type: "system", phrenCompacted: true, timestamp: raw.timestamp };
325
+ }
326
+ if (raw.type === "user" && raw.isCompactSummary === true) {
327
+ const content = object(raw.message).content;
328
+ return { type: "user", isCompactSummary: true, timestamp: raw.timestamp,
329
+ message: { role: "user", content: (typeof content === "string" ? content : "").slice(0, 4_000) } };
330
+ }
331
+ // What phren's prompt hook injected into a user turn: Claude Code records
332
+ // the hook's output as an attachment row pointing at that turn. Only
333
+ // phren's own output crosses; other hooks' output stays private.
334
+ const hookContext = phrenHookContext(raw, includeSidechain);
335
+ if (hookContext)
336
+ return hookContext;
337
+ if (raw.isMeta || (raw.isSidechain && !includeSidechain) || !["user", "assistant", "system"].includes(String(raw.type)))
338
+ return undefined;
339
+ raw = Object.fromEntries(Object.entries(raw).filter(([key]) => CLAUDE_KEYS.has(key)));
340
+ const message = unwrapUserText(object(raw.message));
341
+ // Keep indexes for historical images while removing thinking contents.
342
+ // The one exception is narration: short progress notes the model writes
343
+ // for the person watching (the lines Claude Code's terminal shows between
344
+ // tool calls). They arrive as thinking blocks whose signature declares
345
+ // them narration and whose text is present; private reasoning is stored
346
+ // with empty text and a "thinking" signature and stays redacted.
347
+ if (typeof message.content === "string")
348
+ return raw.type === "user" && harnessPreamble(message.content) ? undefined : { ...raw, message };
349
+ if (Array.isArray(message.content))
350
+ return { ...raw, message: { ...message, content: objects(message.content).map(b => ["text", "image", "tool_use", "tool_result"].includes(String(b.type)) ? b
351
+ : isNarration(b) ? { type: "text", text: String(b.thinking), narration: true } : { type: "redacted" }) } };
352
+ return undefined;
353
+ }
@@ -0,0 +1,30 @@
1
+ import { type Json } from "./protocol.js";
2
+ import type { Entry, LocalChildAgentRelation } from "./transcripts.js";
3
+ /** Codex's transcript reader: SubAgentActivity child links, the public rows
4
+ * of a rollout, and code-mode calls projected into ordinary tool calls. */
5
+ export type DirectRelation = Pick<LocalChildAgentRelation, "session" | "path" | "callId" | "state">;
6
+ export declare function directChildAgents(file: string): Promise<DirectRelation[]>;
7
+ export declare function childTranscriptBelongsTo(file: string, parent: string): Promise<boolean>;
8
+ /** Codex rows the phone may see: public messages, tool calls and their
9
+ * outputs, the answering model, and turn/usage events. */
10
+ export declare function visibleCodexEvent(raw: Json): Json | undefined;
11
+ /** Codex 0.155 code mode: the model calls one generic tool whose input is
12
+ * JavaScript, and that source invokes `tools.apply_patch`/`tools.shell`/
13
+ * `tools.read`. The Hook resolves each invocation into the ordinary call the
14
+ * phone already draws, so a patch shows a diff instead of an opaque source. */
15
+ export interface CodeToolCall {
16
+ name: "apply_patch" | "shell" | "read";
17
+ input: Json;
18
+ }
19
+ /** Every recognized invocation in a code-mode source, in the order written. */
20
+ export declare function codeToolCalls(source: string): CodeToolCall[] | undefined;
21
+ /** Expand one Codex row: a code-mode call becomes one ordinary call per
22
+ * invocation, each carrying the original source under `input.source`, and the
23
+ * matching result follows the first. Other rows pass through unchanged. */
24
+ export declare function projectCodexRow(raw: Json): {
25
+ rows: Json[];
26
+ base?: string;
27
+ };
28
+ /** The output row for a projected call is read before its call (newest row
29
+ * first), so an output already collected in this page follows the first. */
30
+ export declare function rewriteProjectedOutput(entries: Entry[], base: string): void;