@phren/cli 0.3.44 → 0.3.46

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 (67) hide show
  1. package/dist/bridge/agent-hook-asks.d.ts +34 -0
  2. package/dist/bridge/agent-hook-asks.js +72 -0
  3. package/dist/bridge/agent-hook-callback.d.ts +22 -0
  4. package/dist/bridge/agent-hook-callback.js +114 -0
  5. package/dist/bridge/agent-hooks.d.ts +7 -74
  6. package/dist/bridge/agent-hooks.js +22 -186
  7. package/dist/bridge/claude-question-dialog.d.ts +6 -0
  8. package/dist/bridge/claude-question-dialog.js +29 -7
  9. package/dist/bridge/codex-app-server.d.ts +5 -0
  10. package/dist/bridge/codex-app-server.js +15 -0
  11. package/dist/bridge/codex-auth-refresh.js +7 -0
  12. package/dist/bridge/dispatch-command.js +7 -1
  13. package/dist/bridge/file-write.js +3 -1
  14. package/dist/bridge/git-host-auth.d.ts +6 -0
  15. package/dist/bridge/git-host-auth.js +45 -0
  16. package/dist/bridge/git-publish.d.ts +1 -1
  17. package/dist/bridge/git-publish.js +75 -8
  18. package/dist/bridge/git-review.js +23 -2
  19. package/dist/bridge/git.d.ts +5 -1
  20. package/dist/bridge/git.js +103 -8
  21. package/dist/bridge/health.d.ts +1 -0
  22. package/dist/bridge/health.js +8 -5
  23. package/dist/bridge/lookup-cache.d.ts +17 -0
  24. package/dist/bridge/lookup-cache.js +70 -0
  25. package/dist/bridge/memory-store.js +40 -14
  26. package/dist/bridge/pair.js +4 -0
  27. package/dist/bridge/peers.js +36 -0
  28. package/dist/bridge/projects.d.ts +14 -0
  29. package/dist/bridge/projects.js +48 -1
  30. package/dist/bridge/prompt-deliveries.d.ts +38 -3
  31. package/dist/bridge/prompt-deliveries.js +196 -36
  32. package/dist/bridge/schedule-format.d.ts +4 -0
  33. package/dist/bridge/schedule-format.js +34 -0
  34. package/dist/bridge/schedule-problems.js +4 -0
  35. package/dist/bridge/scoped-gateway.d.ts +1 -0
  36. package/dist/bridge/scoped-gateway.js +10 -1
  37. package/dist/bridge/server-launch.js +6 -2
  38. package/dist/bridge/server-routes.js +5 -1
  39. package/dist/bridge/session-activity.d.ts +10 -0
  40. package/dist/bridge/session-activity.js +25 -0
  41. package/dist/bridge/session-servers.js +3 -0
  42. package/dist/bridge/{codex-thread-origin.d.ts → thread-origin.d.ts} +8 -0
  43. package/dist/bridge/{codex-thread-origin.js → thread-origin.js} +24 -6
  44. package/dist/bridge/usage.d.ts +4 -0
  45. package/dist/bridge/usage.js +22 -8
  46. package/dist/bridge-hook.mjs +1149 -469
  47. package/dist/cli/namespaces-tasks.js +19 -5
  48. package/dist/cli/ops.d.ts +1 -1
  49. package/dist/cli/ops.js +2 -2
  50. package/dist/cli/schedules.js +16 -9
  51. package/dist/content/summarize.js +46 -6
  52. package/dist/content/validate.d.ts +3 -1
  53. package/dist/content/validate.js +33 -4
  54. package/dist/data/access.d.ts +8 -1
  55. package/dist/data/access.js +27 -9
  56. package/dist/data/notes.d.ts +2 -0
  57. package/dist/data/notes.js +2 -1
  58. package/dist/entrypoint.js +2 -1
  59. package/dist/generated/memory-ui-graph.browser.js +1 -1
  60. package/dist/governance/locks.js +17 -1
  61. package/dist/governance/policy.js +37 -23
  62. package/dist/link/link.js +6 -1
  63. package/dist/memory-ui-graph.runtime.js +1 -1
  64. package/dist/sync/conflict-resolve.d.ts +1 -1
  65. package/dist/sync/conflict-resolve.js +91 -8
  66. package/dist/task/lifecycle.js +2 -1
  67. package/package.json +1 -1
@@ -72,6 +72,16 @@ export declare function liveBackground(record: TurnRecord | undefined, readFinal
72
72
  * sits on a tab with a live turn.
73
73
  */
74
74
  export declare function markBackground(tab: Json, count: number | undefined): void;
75
+ /** How much of a turn's last reply a session row carries. */
76
+ export declare const REPLY_EXCERPT_CHARS = 240;
77
+ /**
78
+ * The ended turn's last reply as one short line for a session row
79
+ * (`lastReply`): what the agent said before it stopped, so a waiting row can
80
+ * say what it waits for. Markdown emphasis, headings and code fences are
81
+ * dropped and whitespace collapsed. Undefined while a turn runs (its prompt
82
+ * is newer than the Stop) or when the Stop carried no reply.
83
+ */
84
+ export declare function replyExcerpt(record: TurnRecord | undefined): string | undefined;
75
85
  /** A tab or workspace label worth showing as a name: not empty, not Herdr's bare numbering, not an id. */
76
86
  export declare function meaningfulLabel(value: unknown): string | undefined;
77
87
  /** A terminal title without the spinner or status glyphs around it, nor the
@@ -122,6 +122,31 @@ export function markBackground(tab, count) {
122
122
  else if (typeof tab.backgroundTasks === "number")
123
123
  tab.backgroundTasks = Math.max(tab.backgroundTasks, count);
124
124
  }
125
+ /** How much of a turn's last reply a session row carries. */
126
+ export const REPLY_EXCERPT_CHARS = 240;
127
+ /**
128
+ * The ended turn's last reply as one short line for a session row
129
+ * (`lastReply`): what the agent said before it stopped, so a waiting row can
130
+ * say what it waits for. Markdown emphasis, headings and code fences are
131
+ * dropped and whitespace collapsed. Undefined while a turn runs (its prompt
132
+ * is newer than the Stop) or when the Stop carried no reply.
133
+ */
134
+ export function replyExcerpt(record) {
135
+ if (!record)
136
+ return undefined;
137
+ const phase = turnPhase(record);
138
+ if (phase.phase !== "ended" || !phase.reply)
139
+ return undefined;
140
+ const line = phase.reply
141
+ .replace(/```[\s\S]*?(```|$)/g, " ")
142
+ .replace(/(^|\n)\s*(#{1,6}|>|[-*+]|\d+\.)\s+/g, "$1")
143
+ .replace(/(\*\*|__|\*|`)/g, "")
144
+ .replace(/\s+/g, " ")
145
+ .trim();
146
+ if (!line)
147
+ return undefined;
148
+ return line.length > REPLY_EXCERPT_CHARS ? `${line.slice(0, REPLY_EXCERPT_CHARS - 1).trimEnd()}…` : line;
149
+ }
125
150
  /** A tab or workspace label worth showing as a name: not empty, not Herdr's bare numbering, not an id. */
126
151
  export function meaningfulLabel(value) {
127
152
  if (typeof value !== "string")
@@ -53,6 +53,9 @@ export function descendsFrom(pid, roots, parents) {
53
53
  export function attributeServers(input) {
54
54
  const result = [];
55
55
  for (const server of input.servers) {
56
+ // A listing whose owner could not be verified may be shown, but it belongs to no session.
57
+ if (server.unverified)
58
+ continue;
56
59
  if (descendsFrom(server.pid, input.ownRoots, input.parents))
57
60
  result.push({ ...server, source: "started" });
58
61
  else if (input.mentioned.has(server.port) && !descendsFrom(server.pid, input.otherRoots, input.parents)) {
@@ -10,3 +10,11 @@ export declare function codexForeignThread(transcriptPath: unknown): Promise<boo
10
10
  /** The session_meta fields come before its long instructions, so the head of
11
11
  * the line is enough; no full parse. */
12
12
  export declare function foreignMeta(head: string): boolean;
13
+ /** Whether a Claude hook callback comes from a headless run (`claude -p`, the
14
+ * Agent SDK) started inside the pane: it inherits HERDR_* and
15
+ * PHREN_DISPATCH_ID, so its callbacks name the pane and overwrite the parent's
16
+ * turn record. Interactive transcripts say `"entrypoint":"cli"`, headless ones
17
+ * `sdk-cli`/`sdk-ts`/`sdk-py`, on the first user row. A transcript that cannot
18
+ * be read, or has a long first prompt, counts as the pane's own. */
19
+ export declare function claudeHeadlessThread(transcriptPath: unknown): Promise<boolean>;
20
+ export declare function headlessEntrypoint(head: string): boolean;
@@ -11,15 +11,19 @@ import path from "node:path";
11
11
  export async function codexForeignThread(transcriptPath) {
12
12
  if (typeof transcriptPath !== "string" || !path.isAbsolute(transcriptPath) || !/^rollout-.*\.jsonl$/.test(path.basename(transcriptPath)))
13
13
  return false;
14
- const file = await open(transcriptPath, "r").catch(() => undefined);
15
- if (!file)
16
- return false;
14
+ return foreignMeta(await readHead(transcriptPath, 4096));
15
+ }
16
+ /** The first `bytes` of a transcript; "" when it cannot be read. */
17
+ async function readHead(file, bytes) {
18
+ const handle = await open(file, "r").catch(() => undefined);
19
+ if (!handle)
20
+ return "";
17
21
  try {
18
- const buffer = Buffer.alloc(4096), { bytesRead } = await file.read(buffer, 0, buffer.length, 0);
19
- return foreignMeta(buffer.subarray(0, bytesRead).toString("utf8"));
22
+ const buffer = Buffer.alloc(bytes), { bytesRead } = await handle.read(buffer, 0, bytes, 0);
23
+ return buffer.subarray(0, bytesRead).toString("utf8");
20
24
  }
21
25
  finally {
22
- await file.close();
26
+ await handle.close();
23
27
  }
24
28
  }
25
29
  /** The session_meta fields come before its long instructions, so the head of
@@ -29,3 +33,17 @@ export function foreignMeta(head) {
29
33
  return false;
30
34
  return /"originator"\s*:\s*"codex_exec"/.test(head) || /"source"\s*:\s*"exec"/.test(head) || /"source"\s*:\s*\{\s*"subagent"/.test(head);
31
35
  }
36
+ /** Whether a Claude hook callback comes from a headless run (`claude -p`, the
37
+ * Agent SDK) started inside the pane: it inherits HERDR_* and
38
+ * PHREN_DISPATCH_ID, so its callbacks name the pane and overwrite the parent's
39
+ * turn record. Interactive transcripts say `"entrypoint":"cli"`, headless ones
40
+ * `sdk-cli`/`sdk-ts`/`sdk-py`, on the first user row. A transcript that cannot
41
+ * be read, or has a long first prompt, counts as the pane's own. */
42
+ export async function claudeHeadlessThread(transcriptPath) {
43
+ if (typeof transcriptPath !== "string" || !path.isAbsolute(transcriptPath) || !transcriptPath.endsWith(".jsonl"))
44
+ return false;
45
+ return headlessEntrypoint(await readHead(transcriptPath, 32_768));
46
+ }
47
+ export function headlessEntrypoint(head) {
48
+ return /"entrypoint"\s*:\s*"sdk-[a-z]+"/.test(head);
49
+ }
@@ -175,6 +175,10 @@ export declare function settleClaudeUsage(usage: AccountUsage, now: number): Acc
175
175
  /** `opencode stats` scans OpenCode's whole database, several CPU-seconds on a
176
176
  * busy machine, for a rolling seven-day cost that barely moves in a minute. */
177
177
  export declare const OPENCODE_STATS_REUSE_MS: number;
178
+ /** Longest one reader may hold its shared slot. A read that never settles (a
179
+ * child whose grandchild keeps stdout open, say) would otherwise block every
180
+ * later poll until the Hook restarts. */
181
+ export declare const USAGE_READ_LIMIT_MS = 30000;
178
182
  export declare class AccountUsageReader {
179
183
  private readCodex;
180
184
  private now;
@@ -771,6 +771,16 @@ export function settleClaudeUsage(usage, now) {
771
771
  /** `opencode stats` scans OpenCode's whole database, several CPU-seconds on a
772
772
  * busy machine, for a rolling seven-day cost that barely moves in a minute. */
773
773
  export const OPENCODE_STATS_REUSE_MS = 10 * 60_000;
774
+ /** Longest one reader may hold its shared slot. A read that never settles (a
775
+ * child whose grandchild keeps stdout open, say) would otherwise block every
776
+ * later poll until the Hook restarts. */
777
+ export const USAGE_READ_LIMIT_MS = 30_000;
778
+ function bounded(read, fallback, ms = USAGE_READ_LIMIT_MS) {
779
+ let timer;
780
+ const late = new Promise(resolve => { timer = setTimeout(() => resolve(fallback()), ms); timer.unref?.(); });
781
+ return Promise.race([read, late]).finally(() => clearTimeout(timer));
782
+ }
783
+ const unanswered = (source, name) => ({ source, windows: [], message: `${name} did not answer in time on this computer. Phren will ask again shortly.` });
774
784
  export class AccountUsageReader {
775
785
  readCodex;
776
786
  now;
@@ -812,7 +822,7 @@ export class AccountUsageReader {
812
822
  /** Codex and every Claude home, without the spend readers: what a dispatch capacity probe asks. */
813
823
  async limits(allAccounts = true) {
814
824
  if (!this.cached || this.now() - this.cached.at >= 60_000) {
815
- this.pending ??= this.readCodex().then(value => { this.cached = { at: this.now(), value }; return value; }).finally(() => { this.pending = undefined; });
825
+ this.pending ??= bounded(this.readCodex(), () => unanswered("codex", "Codex")).then(value => { this.cached = { at: this.now(), value }; return value; }).finally(() => { this.pending = undefined; });
816
826
  }
817
827
  const codex = this.pending ?? Promise.resolve(this.cached.value);
818
828
  // Every home, default first; the labels and keys are read once per poll.
@@ -822,7 +832,7 @@ export class AccountUsageReader {
822
832
  const email = claudeAccountEmail(home);
823
833
  const credits = claudeCredits(home.id);
824
834
  // Claude Code's own answer only when the profile can't name the plan, or a credit asks whether this is a Console login.
825
- const auth = credits.length || claudePlanUnknown(home) ? await this.readAuth(home).catch(() => undefined) : undefined;
835
+ const auth = credits.length || claudePlanUnknown(home) ? await bounded(this.readAuth(home).catch(() => undefined), () => undefined) : undefined;
826
836
  const fallback = auth?.console ? "Console" : auth?.plan ? claudePlan({ subscriptionType: auth.plan }) : undefined;
827
837
  const subscription = claudeAccountSubscription(home, now, fallback);
828
838
  const usage = settleClaudeUsage(await this.claude(home), this.now());
@@ -840,9 +850,10 @@ export class AccountUsageReader {
840
850
  if (!cached || this.now() - cached.at >= 60_000) {
841
851
  const at = this.now();
842
852
  if (!this.spendingPending.has(key)) {
843
- const pending = Promise.all([this.openCode(at), this.readOpenCodeGo(new Date(at)), this.readOpenRouter(new Date(at)),
844
- includeCopilot ? this.readCopilot(new Date(at)) : Promise.resolve(undefined),
845
- includeElevenLabs ? this.readElevenLabs(new Date(at)).catch(() => undefined) : Promise.resolve(undefined)])
853
+ const pending = Promise.all([this.openCode(at), bounded(this.readOpenCodeGo(new Date(at)), () => unanswered("opencode-go", "OpenCode Go")),
854
+ bounded(this.readOpenRouter(new Date(at)), () => undefined),
855
+ includeCopilot ? bounded(this.readCopilot(new Date(at)), () => unanswered("copilot", "GitHub Copilot")) : Promise.resolve(undefined),
856
+ includeElevenLabs ? bounded(this.readElevenLabs(new Date(at)).catch(() => undefined), () => undefined) : Promise.resolve(undefined)])
846
857
  .then(([openCode, openCodeGo, openRouter, copilot, elevenLabs]) => [openCode, openCodeGo, ...(openRouter ? [openRouter] : []), ...(copilot ? [copilot] : []), ...(elevenLabs ? [elevenLabs] : [])])
847
858
  .then(value => { this.spendingCached.set(key, { at, value }); return value; })
848
859
  .finally(() => { this.spendingPending.delete(key); });
@@ -855,8 +866,11 @@ export class AccountUsageReader {
855
866
  openCode(at) {
856
867
  if (this.openCodeCached && at - this.openCodeCached.at < OPENCODE_STATS_REUSE_MS)
857
868
  return Promise.resolve(this.openCodeCached.value);
858
- this.openCodePending ??= this.readOpenCode(new Date(at))
859
- .then(value => { this.openCodeCached = { at, value }; return value; })
869
+ let late = false;
870
+ this.openCodePending ??= bounded(this.readOpenCode(new Date(at)), () => { late = true; return unanswered("opencode", "opencode stats"); })
871
+ // A late answer is not kept for the ten minutes a real one is.
872
+ .then(value => { if (!late)
873
+ this.openCodeCached = { at, value }; return value; })
860
874
  .finally(() => { this.openCodePending = undefined; });
861
875
  return this.openCodePending;
862
876
  }
@@ -870,7 +884,7 @@ export class AccountUsageReader {
870
884
  if (!cached || this.now() - cached.at >= 60_000) {
871
885
  const at = this.now();
872
886
  if (!this.claudePending.has(home.dir))
873
- this.claudePending.set(home.dir, this.readClaudeLive(new Date(at), home)
887
+ this.claudePending.set(home.dir, bounded(this.readClaudeLive(new Date(at), home), () => undefined)
874
888
  .then(value => { this.claudeCached.set(home.dir, { at, value }); return value; })
875
889
  .catch(() => { this.claudeCached.set(home.dir, { at, value: undefined }); return undefined; })
876
890
  .finally(() => { this.claudePending.delete(home.dir); }));