@phren/cli 0.3.11 → 0.3.12

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 (168) hide show
  1. package/dist/bridge/agent-hook-stores.d.ts +3 -0
  2. package/dist/bridge/agent-hook-stores.js +5 -0
  3. package/dist/bridge/agent-hooks.d.ts +159 -5
  4. package/dist/bridge/agent-hooks.js +663 -49
  5. package/dist/bridge/approval-summary.d.ts +16 -0
  6. package/dist/bridge/approval-summary.js +133 -0
  7. package/dist/bridge/authority-command.d.ts +20 -0
  8. package/dist/bridge/authority-command.js +120 -0
  9. package/dist/bridge/authority.d.ts +223 -0
  10. package/dist/bridge/authority.js +231 -0
  11. package/dist/bridge/claude-account-setup.d.ts +14 -0
  12. package/dist/bridge/claude-account-setup.js +69 -0
  13. package/dist/bridge/claude-accounts.d.ts +49 -0
  14. package/dist/bridge/claude-accounts.js +195 -0
  15. package/dist/bridge/code-routes.js +2 -2
  16. package/dist/bridge/codex-app-server.d.ts +144 -0
  17. package/dist/bridge/codex-app-server.js +319 -0
  18. package/dist/bridge/codex-auth-refresh.d.ts +33 -0
  19. package/dist/bridge/codex-auth-refresh.js +71 -0
  20. package/dist/bridge/codex-question-reply.d.ts +16 -0
  21. package/dist/bridge/codex-question-reply.js +57 -0
  22. package/dist/bridge/codex-review-mode.d.ts +10 -0
  23. package/dist/bridge/codex-review-mode.js +74 -0
  24. package/dist/bridge/codex-servers.d.ts +217 -0
  25. package/dist/bridge/codex-servers.js +636 -0
  26. package/dist/bridge/command.js +94 -4
  27. package/dist/bridge/computer-identity.d.ts +75 -0
  28. package/dist/bridge/computer-identity.js +166 -0
  29. package/dist/bridge/conductor-group.d.ts +91 -8
  30. package/dist/bridge/conductor-group.js +105 -9
  31. package/dist/bridge/conductor-role.d.ts +94 -0
  32. package/dist/bridge/conductor-role.js +176 -0
  33. package/dist/bridge/conductor-sets.d.ts +20 -0
  34. package/dist/bridge/conductor-sets.js +59 -0
  35. package/dist/bridge/dispatch-command.d.ts +3 -0
  36. package/dist/bridge/dispatch-command.js +75 -11
  37. package/dist/bridge/dispatch-returns.d.ts +123 -19
  38. package/dist/bridge/dispatch-returns.js +357 -50
  39. package/dist/bridge/dispatch.d.ts +103 -2
  40. package/dist/bridge/dispatch.js +272 -40
  41. package/dist/bridge/fanouts.d.ts +7 -1
  42. package/dist/bridge/fanouts.js +39 -4
  43. package/dist/bridge/folder-trust.d.ts +56 -0
  44. package/dist/bridge/folder-trust.js +195 -0
  45. package/dist/bridge/grants.d.ts +22 -1
  46. package/dist/bridge/grants.js +57 -9
  47. package/dist/bridge/hand-off.d.ts +21 -9
  48. package/dist/bridge/hand-off.js +58 -45
  49. package/dist/bridge/harness-status.d.ts +11 -0
  50. package/dist/bridge/harness-status.js +32 -0
  51. package/dist/bridge/harnesses.d.ts +39 -0
  52. package/dist/bridge/harnesses.js +65 -0
  53. package/dist/bridge/health.d.ts +2 -0
  54. package/dist/bridge/health.js +1 -1
  55. package/dist/bridge/herdr.d.ts +11 -2
  56. package/dist/bridge/herdr.js +98 -23
  57. package/dist/bridge/hook-fast.d.ts +4 -0
  58. package/dist/bridge/hook-fast.js +68 -0
  59. package/dist/bridge/install.d.ts +16 -1
  60. package/dist/bridge/install.js +88 -21
  61. package/dist/bridge/launch-brief.d.ts +96 -0
  62. package/dist/bridge/launch-brief.js +164 -0
  63. package/dist/bridge/model-switch.d.ts +3 -0
  64. package/dist/bridge/model-switch.js +17 -1
  65. package/dist/bridge/models.d.ts +8 -4
  66. package/dist/bridge/models.js +24 -8
  67. package/dist/bridge/mux-wire.d.ts +9 -0
  68. package/dist/bridge/mux-wire.js +19 -0
  69. package/dist/bridge/opencode-pane-server.d.ts +113 -0
  70. package/dist/bridge/opencode-pane-server.js +332 -0
  71. package/dist/bridge/opencode-panes.d.ts +111 -0
  72. package/dist/bridge/opencode-panes.js +295 -0
  73. package/dist/bridge/pane-accounts.d.ts +8 -0
  74. package/dist/bridge/pane-accounts.js +46 -0
  75. package/dist/bridge/prompt-startup.d.ts +5 -0
  76. package/dist/bridge/prompt-startup.js +24 -0
  77. package/dist/bridge/protocol.d.ts +5 -0
  78. package/dist/bridge/protocol.js +4 -0
  79. package/dist/bridge/push.d.ts +5 -4
  80. package/dist/bridge/push.js +6 -4
  81. package/dist/bridge/questions.d.ts +37 -2
  82. package/dist/bridge/questions.js +158 -15
  83. package/dist/bridge/resources.d.ts +9 -3
  84. package/dist/bridge/resources.js +24 -8
  85. package/dist/bridge/schedule-format.d.ts +2 -0
  86. package/dist/bridge/schedule-format.js +3 -0
  87. package/dist/bridge/schedule-launch.d.ts +2 -1
  88. package/dist/bridge/schedule-launch.js +23 -53
  89. package/dist/bridge/schedule-watch.d.ts +10 -1
  90. package/dist/bridge/schedule-watch.js +54 -2
  91. package/dist/bridge/server-launch.d.ts +22 -5
  92. package/dist/bridge/server-launch.js +225 -29
  93. package/dist/bridge/server-overview.d.ts +3 -3
  94. package/dist/bridge/server-overview.js +22 -14
  95. package/dist/bridge/server-pane-routes.d.ts +2 -0
  96. package/dist/bridge/server-pane-routes.js +145 -15
  97. package/dist/bridge/server-routes.d.ts +7 -0
  98. package/dist/bridge/server-routes.js +163 -35
  99. package/dist/bridge/server-stream.d.ts +2 -0
  100. package/dist/bridge/server-stream.js +22 -8
  101. package/dist/bridge/server.d.ts +6 -1
  102. package/dist/bridge/server.js +44 -12
  103. package/dist/bridge/session-activity.d.ts +81 -0
  104. package/dist/bridge/session-activity.js +117 -0
  105. package/dist/bridge/session-rename.d.ts +48 -0
  106. package/dist/bridge/session-rename.js +190 -0
  107. package/dist/bridge/session-servers.js +2 -2
  108. package/dist/bridge/settings-switch.d.ts +60 -0
  109. package/dist/bridge/settings-switch.js +287 -0
  110. package/dist/bridge/speech-voice.d.ts +55 -0
  111. package/dist/bridge/speech-voice.js +132 -0
  112. package/dist/bridge/speech.d.ts +70 -11
  113. package/dist/bridge/speech.js +169 -41
  114. package/dist/bridge/terminal-herdr.js +9 -3
  115. package/dist/bridge/terminal-tmux.d.ts +3 -3
  116. package/dist/bridge/terminal-tmux.js +24 -9
  117. package/dist/bridge/terminal.d.ts +27 -4
  118. package/dist/bridge/terminal.js +8 -1
  119. package/dist/bridge/transcript-claude.js +57 -12
  120. package/dist/bridge/transcript-codex.d.ts +0 -2
  121. package/dist/bridge/transcript-codex.js +29 -4
  122. package/dist/bridge/transcript-preview.d.ts +1 -0
  123. package/dist/bridge/transcript-preview.js +24 -4
  124. package/dist/bridge/transcripts.d.ts +15 -2
  125. package/dist/bridge/transcripts.js +47 -16
  126. package/dist/bridge/turn-records.d.ts +85 -0
  127. package/dist/bridge/turn-records.js +143 -0
  128. package/dist/bridge/uploads.d.ts +3 -0
  129. package/dist/bridge/uploads.js +14 -0
  130. package/dist/bridge/usage.d.ts +39 -10
  131. package/dist/bridge/usage.js +155 -44
  132. package/dist/bridge-hook.mjs +19169 -13176
  133. package/dist/cli/claude-unmanaged.d.ts +12 -0
  134. package/dist/cli/claude-unmanaged.js +105 -0
  135. package/dist/cli/schedules.js +2 -1
  136. package/dist/cli/session-start.js +10 -1
  137. package/dist/cli/session-stop.js +9 -1
  138. package/dist/cli-registry.js +24 -5
  139. package/dist/data/task-receipts.d.ts +19 -0
  140. package/dist/data/task-receipts.js +68 -0
  141. package/dist/data/tasks.js +4 -1
  142. package/dist/generated/memory-ui-graph.browser.js +63 -63
  143. package/dist/graph-core/model.js +24 -3
  144. package/dist/graph-core/types.d.ts +4 -1
  145. package/dist/graph-core/types.js +1 -0
  146. package/dist/init/init-preset.js +7 -3
  147. package/dist/init/init-uninstall.js +2 -11
  148. package/dist/init/teardown.d.ts +3 -0
  149. package/dist/init/teardown.js +47 -0
  150. package/dist/link/context.js +21 -9
  151. package/dist/mcp/plugin-mode.js +1 -1
  152. package/dist/memory-ui-graph.runtime.js +63 -63
  153. package/dist/modules/code-package.d.ts +1 -0
  154. package/dist/modules/code-package.js +36 -7
  155. package/dist/modules/registry.js +14 -13
  156. package/dist/profile-store.d.ts +6 -0
  157. package/dist/profile-store.js +25 -1
  158. package/dist/shell/graph/glyphs.js +4 -0
  159. package/dist/shell/graph/graph-view.js +2 -0
  160. package/dist/status.js +7 -1
  161. package/dist/sync/task-claim.js +3 -2
  162. package/dist/task/lifecycle.js +25 -4
  163. package/dist/test-global-setup.js +5 -0
  164. package/dist/tools/dispatch.js +35 -2
  165. package/dist/tools/tasks.js +10 -1
  166. package/package.json +1 -1
  167. package/plugins/opencode/phren-transcript.js +87 -4
  168. package/starter/global/skills/conductor/SKILL.md +17 -2
@@ -0,0 +1,81 @@
1
+ import { type Json } from "./protocol.js";
2
+ import type { FinalTurn } from "./schedule-watch.js";
3
+ import { type TurnRecord } from "./turn-records.js";
4
+ /**
5
+ * What a live session's row says about itself beyond Herdr's own status and
6
+ * title, from the pane's turn record (turn-records.ts) and the dispatch label
7
+ * (launch-brief.ts):
8
+ *
9
+ * - A Claude turn that ended while background shells or subagents still run
10
+ * (its Stop's `background` count, less the tasks its transcript shows
11
+ * finishing since, for at most BACKGROUND_STALE_MS), or a Codex session with
12
+ * running subagents or fanout jobs, is still working. Herdr reports it idle, so the
13
+ * phone listed it under IDLE with a moon badge. The tab keeps
14
+ * `agentStatus: "working"` and gains `backgroundTasks`, the count.
15
+ * - A dispatched worker is named by its dispatch label. Claude and Codex title
16
+ * a session after its first prompt, which for a brief launch is
17
+ * "Read and follow the brief in <path>" (or "Brief d35c6189" once they
18
+ * shorten it), so those titles are never shown.
19
+ */
20
+ /** The record, only when it belongs to this pane's terminal and conversation
21
+ * (the guard dispatch returns use): a leftover record of an earlier terminal
22
+ * or session in the same pane must not speak for this one. */
23
+ export declare function ownRecord(record: TurnRecord | undefined, pane: Json, source: unknown, session: string | undefined): TurnRecord | undefined;
24
+ /** The pane's own turn record. OpenCode has no Stop hook to report background work or a dispatch id here. */
25
+ export declare function paneRecord(server: string, pane: Json, session: string | undefined): Promise<TurnRecord | undefined>;
26
+ /** How long after its Stop a background count may keep a row working. A task
27
+ * whose finish the transcript never shows (a dev server left running, a
28
+ * persistent monitor, a notification the tail no longer holds) would
29
+ * otherwise keep it working forever. Matches dispatch returns' wait. */
30
+ export declare const BACKGROUND_STALE_MS: number;
31
+ /** Background tasks the recorded turn ended with that are still running:
32
+ * the Stop's count less those `finishedTasks` (FinalTurn's) says finished
33
+ * after it. Undefined once none are left or the Stop is BACKGROUND_STALE_MS old. */
34
+ export declare function recordedBackground(record: TurnRecord | undefined, finishedTasks?: readonly string[], now?: number): number | undefined;
35
+ /** `recordedBackground` for a pane, reading the transcript (`readFinalTurn`,
36
+ * passed in: schedule-watch imports herdr, which imports this) only when the
37
+ * Stop left background work. Only Claude's Stop reports a count. */
38
+ export declare function liveBackground(record: TurnRecord | undefined, readFinal: (source: "claude", session: string) => Promise<FinalTurn | undefined>, now?: number): Promise<number | undefined>;
39
+ /**
40
+ * Marks a tab whose main turn ended but whose background work continues:
41
+ * an idle or done tab becomes `working` with `backgroundTasks: count`. Called
42
+ * again with a second source (the transcript's running children), it keeps the
43
+ * larger count, never the sum: Claude's own Stop count already includes its
44
+ * subagents. A tab that is working, blocked or waiting on its own is left
45
+ * alone, so `backgroundTasks` never sits on a tab with a live turn.
46
+ */
47
+ export declare function markBackground(tab: Json, count: number | undefined): void;
48
+ /** A tab or workspace label worth showing as a name: not empty, not Herdr's bare numbering, not an id. */
49
+ export declare function meaningfulLabel(value: unknown): string | undefined;
50
+ /** A harness title made from the brief launch's first prompt rather than the work: the prompt itself, the brief path, or "Brief <id>". */
51
+ export declare function isBriefTitle(title: string): boolean;
52
+ /**
53
+ * The name a session is shown under, first that applies:
54
+ * 1. the agent pane's own label, when meaningful (a rename from the phone or
55
+ * from Herdr or tmux itself; a pane has none until someone sets one);
56
+ * 2. the dispatch label its dispatcher gave it;
57
+ * 3. for a dispatch with no stored label (sent before labels were kept), the
58
+ * tab label, else the workspace label, when meaningful;
59
+ * 4. the harness's own title, unless it is brief boilerplate, which is
60
+ * replaced by a meaningful tab or workspace label or dropped (the phone
61
+ * then falls back to the label).
62
+ * Never leaves a row blank: when none applies and `fallbackLabel` (passed, even
63
+ * if undefined) is the label the phone shows without a title and is blank, the raw harness title stands.
64
+ */
65
+ export declare function sessionTitle(input: {
66
+ dispatched: boolean;
67
+ paneLabel?: unknown;
68
+ dispatchLabel?: string;
69
+ harnessTitle?: unknown;
70
+ tabLabel?: unknown;
71
+ workspaceLabel?: unknown;
72
+ fallbackLabel?: unknown;
73
+ }): string | undefined;
74
+ /** `sessionTitle` for a pane, given its own turn record (the guarded one from `paneRecord`). */
75
+ export declare function recordTitle(record: TurnRecord | undefined, input: {
76
+ paneLabel?: unknown;
77
+ harnessTitle?: unknown;
78
+ tabLabel?: unknown;
79
+ workspaceLabel?: unknown;
80
+ fallbackLabel?: unknown;
81
+ }): Promise<string | undefined>;
@@ -0,0 +1,117 @@
1
+ import { briefLabel } from "./launch-brief.js";
2
+ import { provider } from "./protocol.js";
3
+ import { backgroundLeft, readTurn, turnPhase } from "./turn-records.js";
4
+ /**
5
+ * What a live session's row says about itself beyond Herdr's own status and
6
+ * title, from the pane's turn record (turn-records.ts) and the dispatch label
7
+ * (launch-brief.ts):
8
+ *
9
+ * - A Claude turn that ended while background shells or subagents still run
10
+ * (its Stop's `background` count, less the tasks its transcript shows
11
+ * finishing since, for at most BACKGROUND_STALE_MS), or a Codex session with
12
+ * running subagents or fanout jobs, is still working. Herdr reports it idle, so the
13
+ * phone listed it under IDLE with a moon badge. The tab keeps
14
+ * `agentStatus: "working"` and gains `backgroundTasks`, the count.
15
+ * - A dispatched worker is named by its dispatch label. Claude and Codex title
16
+ * a session after its first prompt, which for a brief launch is
17
+ * "Read and follow the brief in <path>" (or "Brief d35c6189" once they
18
+ * shorten it), so those titles are never shown.
19
+ */
20
+ /** The record, only when it belongs to this pane's terminal and conversation
21
+ * (the guard dispatch returns use): a leftover record of an earlier terminal
22
+ * or session in the same pane must not speak for this one. */
23
+ export function ownRecord(record, pane, source, session) {
24
+ return record && session && record.terminal === pane.terminal_id && record.source === source && record.session === session ? record : undefined;
25
+ }
26
+ /** The pane's own turn record. OpenCode has no Stop hook to report background work or a dispatch id here. */
27
+ export async function paneRecord(server, pane, session) {
28
+ if (!session || pane.agent === "opencode" || !provider.safeParse(pane.agent).success)
29
+ return undefined;
30
+ return ownRecord(await readTurn(server, String(pane.pane_id)).catch(() => undefined), pane, pane.agent, session);
31
+ }
32
+ /** How long after its Stop a background count may keep a row working. A task
33
+ * whose finish the transcript never shows (a dev server left running, a
34
+ * persistent monitor, a notification the tail no longer holds) would
35
+ * otherwise keep it working forever. Matches dispatch returns' wait. */
36
+ export const BACKGROUND_STALE_MS = 2 * 60 * 60 * 1000;
37
+ /** Background tasks the recorded turn ended with that are still running:
38
+ * the Stop's count less those `finishedTasks` (FinalTurn's) says finished
39
+ * after it. Undefined once none are left or the Stop is BACKGROUND_STALE_MS old. */
40
+ export function recordedBackground(record, finishedTasks, now = Date.now()) {
41
+ if (!record)
42
+ return undefined;
43
+ const phase = turnPhase(record);
44
+ if (phase.phase !== "ended" || !(now - Date.parse(phase.at) < BACKGROUND_STALE_MS))
45
+ return undefined;
46
+ return backgroundLeft(phase.background, phase.at, finishedTasks) || undefined;
47
+ }
48
+ /** `recordedBackground` for a pane, reading the transcript (`readFinalTurn`,
49
+ * passed in: schedule-watch imports herdr, which imports this) only when the
50
+ * Stop left background work. Only Claude's Stop reports a count. */
51
+ export async function liveBackground(record, readFinal, now = Date.now()) {
52
+ if (!record || record.source !== "claude" || !recordedBackground(record, [], now))
53
+ return undefined;
54
+ const final = await readFinal("claude", record.session).catch(() => undefined);
55
+ return recordedBackground(record, final?.finishedTasks, now);
56
+ }
57
+ /**
58
+ * Marks a tab whose main turn ended but whose background work continues:
59
+ * an idle or done tab becomes `working` with `backgroundTasks: count`. Called
60
+ * again with a second source (the transcript's running children), it keeps the
61
+ * larger count, never the sum: Claude's own Stop count already includes its
62
+ * subagents. A tab that is working, blocked or waiting on its own is left
63
+ * alone, so `backgroundTasks` never sits on a tab with a live turn.
64
+ */
65
+ export function markBackground(tab, count) {
66
+ if (!count || count < 1)
67
+ return;
68
+ if (tab.agentStatus === "idle" || tab.agentStatus === "done") {
69
+ tab.agentStatus = "working";
70
+ tab.backgroundTasks = count;
71
+ }
72
+ else if (typeof tab.backgroundTasks === "number")
73
+ tab.backgroundTasks = Math.max(tab.backgroundTasks, count);
74
+ }
75
+ /** A tab or workspace label worth showing as a name: not empty, not Herdr's bare numbering, not an id. */
76
+ export function meaningfulLabel(value) {
77
+ if (typeof value !== "string")
78
+ return undefined;
79
+ const text = value.trim();
80
+ return text && !/^\d+$/.test(text) && !/^\S*:\S*$/.test(text) && !/^[A-Za-z]{1,2}[_-]?\d+[A-Za-z]?$/.test(text) ? text : undefined;
81
+ }
82
+ /** A harness title made from the brief launch's first prompt rather than the work: the prompt itself, the brief path, or "Brief <id>". */
83
+ export function isBriefTitle(title) {
84
+ return /^\s*read and follow the brief/i.test(title) || title.includes("/briefs/")
85
+ || /^\s*brief\s+[0-9a-f]{8}[0-9a-f-]*\s*(…|\.\.\.)?\s*$/i.test(title);
86
+ }
87
+ /**
88
+ * The name a session is shown under, first that applies:
89
+ * 1. the agent pane's own label, when meaningful (a rename from the phone or
90
+ * from Herdr or tmux itself; a pane has none until someone sets one);
91
+ * 2. the dispatch label its dispatcher gave it;
92
+ * 3. for a dispatch with no stored label (sent before labels were kept), the
93
+ * tab label, else the workspace label, when meaningful;
94
+ * 4. the harness's own title, unless it is brief boilerplate, which is
95
+ * replaced by a meaningful tab or workspace label or dropped (the phone
96
+ * then falls back to the label).
97
+ * Never leaves a row blank: when none applies and `fallbackLabel` (passed, even
98
+ * if undefined) is the label the phone shows without a title and is blank, the raw harness title stands.
99
+ */
100
+ export function sessionTitle(input) {
101
+ const renamed = meaningfulLabel(input.paneLabel);
102
+ if (renamed)
103
+ return renamed;
104
+ if (input.dispatchLabel)
105
+ return input.dispatchLabel;
106
+ const named = meaningfulLabel(input.tabLabel) ?? meaningfulLabel(input.workspaceLabel);
107
+ const title = typeof input.harnessTitle === "string" && input.harnessTitle.trim() ? input.harnessTitle : undefined;
108
+ const chosen = input.dispatched ? named : !title ? undefined : isBriefTitle(title) ? named : title;
109
+ const label = input.fallbackLabel;
110
+ const blank = "fallbackLabel" in input && (typeof label !== "string" || !label.trim());
111
+ return chosen ?? (blank ? title : undefined);
112
+ }
113
+ /** `sessionTitle` for a pane, given its own turn record (the guarded one from `paneRecord`). */
114
+ export async function recordTitle(record, input) {
115
+ const dispatch = record?.dispatch;
116
+ return sessionTitle({ ...input, dispatched: !!dispatch, dispatchLabel: dispatch ? await briefLabel(dispatch) : undefined });
117
+ }
@@ -0,0 +1,48 @@
1
+ import { z } from "zod";
2
+ import type { PaneClient } from "./opencode-pane-server.js";
3
+ import { type Json, type Target } from "./protocol.js";
4
+ import { type TerminalProvider } from "./terminal.js";
5
+ /** Renaming a session from the phone: the pane's and tab's terminal labels,
6
+ * then the harness's own rename where one can be driven remotely. */
7
+ /** What the phone may send as a session name: 1 to 80 characters, no control characters. */
8
+ export declare const renameLabel: z.ZodString;
9
+ export declare const renameRequest: z.ZodObject<{
10
+ workspaceId: z.ZodString;
11
+ tabId: z.ZodString;
12
+ paneId: z.ZodOptional<z.ZodString>;
13
+ label: z.ZodString;
14
+ }, z.core.$strip>;
15
+ export type NativeMechanism = "slash-command" | "app-server" | "opencode-api" | "none";
16
+ export interface NativeResult {
17
+ harness: string;
18
+ mechanism: NativeMechanism;
19
+ applied: boolean;
20
+ reason?: string;
21
+ }
22
+ /** Everything the rename touches outside this file; tests pass their own. */
23
+ export interface RenameDeps {
24
+ snapshot(server: string): Promise<Json>;
25
+ identity(server: string, pane: Json): Promise<string | undefined>;
26
+ terminal(): TerminalProvider;
27
+ codexRename(target: Target, label: string): Promise<boolean | undefined>;
28
+ opencodeClient(server: string, pane: string): PaneClient | undefined;
29
+ /** The name Claude's transcript for `target` last carries as its `custom-title`. */
30
+ claudeTitle(target: Target): Promise<string | undefined>;
31
+ /** The name Codex's `session_index.jsonl` last holds for a thread. */
32
+ codexIndexTitle(thread: string): Promise<string | undefined>;
33
+ sleep(ms: number): Promise<void>;
34
+ /** How long a typed rename may take to show in the harness's own records. */
35
+ verifyMs: number;
36
+ }
37
+ /** The last `custom-title` in the tail of a Claude transcript. */
38
+ export declare function lastCustomTitle(text: string): string | undefined;
39
+ /** The last `thread_name` written for `thread` in a Codex `session_index.jsonl`. */
40
+ export declare function lastIndexedName(text: string, thread: string): string | undefined;
41
+ export declare const defaultRenameDeps: RenameDeps;
42
+ /** The agent pane a rename addresses: the named pane, else the tab's only agent pane, else its only pane. */
43
+ export declare function renamePane(panes: Json[], paneId: string | undefined): {
44
+ pane: Json;
45
+ renameTab: boolean;
46
+ };
47
+ /** `POST /v1/sessions/rename`: terminal labels first, then the harness's own name. */
48
+ export declare function renameSession(server: string, data: Json, deps?: RenameDeps): Promise<Json>;
@@ -0,0 +1,190 @@
1
+ import { open } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { z } from "zod";
4
+ import { codexHome } from "../home-paths.js";
5
+ import { stripTerminal } from "../terminal-text.js";
6
+ import { codexServers } from "./codex-servers.js";
7
+ import { paneIdentity, snapshot } from "./herdr.js";
8
+ import { emptyComposer } from "./model-switch.js";
9
+ import { paneClient, servedPane } from "./opencode-panes.js";
10
+ import { BridgeError, id, objects } from "./protocol.js";
11
+ import { terminalProvider } from "./terminal.js";
12
+ import { visibleTerminalChoice } from "./terminal-choice.js";
13
+ import { targetTranscriptPath } from "./transcripts.js";
14
+ /** Renaming a session from the phone: the pane's and tab's terminal labels,
15
+ * then the harness's own rename where one can be driven remotely. */
16
+ /** What the phone may send as a session name: 1 to 80 characters, no control characters. */
17
+ export const renameLabel = z.string().trim().min(1).max(80).refine(t => !/[\x00-\x1f\x7f]/.test(t));
18
+ export const renameRequest = z.object({ workspaceId: id, tabId: id, paneId: id.optional(), label: renameLabel });
19
+ const TAIL_BYTES = 262_144;
20
+ /** The last `custom-title` in the tail of a Claude transcript. */
21
+ export function lastCustomTitle(text) {
22
+ for (const line of text.split("\n").reverse()) {
23
+ if (!line.includes('"custom-title"'))
24
+ continue;
25
+ try {
26
+ const row = JSON.parse(line);
27
+ if (row.type === "custom-title" && typeof row.customTitle === "string")
28
+ return row.customTitle;
29
+ }
30
+ catch { /* a line cut by the tail window */ }
31
+ }
32
+ return undefined;
33
+ }
34
+ /** The last `thread_name` written for `thread` in a Codex `session_index.jsonl`. */
35
+ export function lastIndexedName(text, thread) {
36
+ for (const line of text.split("\n").reverse()) {
37
+ if (!line.includes(thread))
38
+ continue;
39
+ try {
40
+ const row = JSON.parse(line);
41
+ if (row.id === thread && typeof row.thread_name === "string")
42
+ return row.thread_name;
43
+ }
44
+ catch { /* a line cut by the tail window */ }
45
+ }
46
+ return undefined;
47
+ }
48
+ async function tail(file) {
49
+ const handle = await open(file, "r").catch(() => undefined);
50
+ if (!handle)
51
+ return undefined;
52
+ try {
53
+ const { size } = await handle.stat();
54
+ const length = Math.min(size, TAIL_BYTES), buffer = Buffer.alloc(length);
55
+ await handle.read(buffer, 0, length, size - length);
56
+ return buffer.toString("utf8");
57
+ }
58
+ finally {
59
+ await handle.close();
60
+ }
61
+ }
62
+ export const defaultRenameDeps = {
63
+ snapshot,
64
+ identity: (server, pane) => paneIdentity(server, pane, true),
65
+ terminal: terminalProvider,
66
+ async codexRename(target, label) {
67
+ const entry = codexServers.forTarget(target);
68
+ if (!entry)
69
+ return undefined;
70
+ await codexServers.renameThread(entry, label);
71
+ return true;
72
+ },
73
+ opencodeClient(server, pane) {
74
+ const entry = servedPane(server, pane);
75
+ return entry ? paneClient(entry) : undefined;
76
+ },
77
+ async claudeTitle(target) {
78
+ const text = await tail(await targetTranscriptPath({ ...target, source: "claude" }));
79
+ return text === undefined ? undefined : lastCustomTitle(text);
80
+ },
81
+ async codexIndexTitle(thread) {
82
+ const text = await tail(path.join(codexHome(), "session_index.jsonl"));
83
+ return text === undefined ? undefined : lastIndexedName(text, thread);
84
+ },
85
+ sleep: ms => new Promise(resolve => setTimeout(resolve, ms)),
86
+ verifyMs: 4_000,
87
+ };
88
+ /** The agent pane a rename addresses: the named pane, else the tab's only agent pane, else its only pane. */
89
+ export function renamePane(panes, paneId) {
90
+ if (!panes.length)
91
+ throw new BridgeError(409, "The tab changed.");
92
+ const agents = panes.filter(p => p.agent);
93
+ if (paneId) {
94
+ const pane = panes.find(p => p.pane_id === paneId);
95
+ if (!pane)
96
+ throw new BridgeError(409, "The pane changed.");
97
+ return { pane, renameTab: agents.length ? agents.length === 1 && !!pane.agent : panes.length === 1 };
98
+ }
99
+ if (agents.length === 1)
100
+ return { pane: agents[0], renameTab: true };
101
+ if (!agents.length && panes.length === 1)
102
+ return { pane: panes[0], renameTab: true };
103
+ throw new BridgeError(400, "Choose a pane to rename.");
104
+ }
105
+ /** Types a slash command into an agent pane that is showing an empty composer. */
106
+ async function typeCommand(deps, server, pane, text) {
107
+ const terminal = deps.terminal();
108
+ const before = await terminal.readScreen(server, String(pane.pane_id), { scope: "pane", source: "visible", lines: 40, format: "ansi", timeoutMs: 2_000 }).catch(() => "");
109
+ const composer = before.split(/\r?\n/).reverse().find(line => /^\s*[›❯>]/.test(stripTerminal(line)));
110
+ if (!composer || !emptyComposer(composer) || visibleTerminalChoice(stripTerminal(before)))
111
+ return "the terminal has a draft or an open menu";
112
+ await terminal.prompt(server, String(pane.pane_id), text);
113
+ return undefined;
114
+ }
115
+ /** Polls `seen` until it holds or the deadline passes. */
116
+ async function confirmed(deps, seen) {
117
+ const deadline = Date.now() + deps.verifyMs;
118
+ do {
119
+ if (await seen().catch(() => false))
120
+ return true;
121
+ await deps.sleep(150);
122
+ } while (Date.now() < deadline);
123
+ return false;
124
+ }
125
+ /** The harness's own rename for the pane's session, never failing the request: a refusal is reported as `reason`. */
126
+ async function nativeRename(deps, server, workspace, tab, pane, label) {
127
+ const harness = typeof pane.agent === "string" && pane.agent ? pane.agent : "none";
128
+ const skip = (mechanism, reason) => ({ harness, mechanism, applied: false, reason });
129
+ if (!["claude", "codex", "opencode"].includes(harness))
130
+ return skip("none", "This harness has no remote rename.");
131
+ const paneId = String(pane.pane_id), status = String(pane.agent_status);
132
+ let session;
133
+ try {
134
+ session = await deps.identity(server, pane);
135
+ }
136
+ catch {
137
+ session = undefined;
138
+ }
139
+ if (!session && harness !== "opencode")
140
+ return skip("none", "The session is not identified yet.");
141
+ const target = { server, workspace, tab, pane: paneId, source: harness, session: session ?? "" };
142
+ let mechanism = harness === "opencode" ? "opencode-api" : "slash-command";
143
+ try {
144
+ if (harness === "opencode") {
145
+ const client = deps.opencodeClient(server, paneId);
146
+ if (!client)
147
+ return skip("none", "This OpenCode was not started by the Hook.");
148
+ const chosen = session ?? (await client.currentSession())?.id;
149
+ if (!chosen)
150
+ return skip("opencode-api", "OpenCode has no session yet.");
151
+ const title = await client.setTitle(chosen, label);
152
+ return title === label ? { harness, mechanism: "opencode-api", applied: true } : skip("opencode-api", "OpenCode did not confirm the new name.");
153
+ }
154
+ if (harness === "codex") {
155
+ mechanism = "app-server";
156
+ const served = await deps.codexRename(target, label);
157
+ if (served)
158
+ return { harness, mechanism, applied: true };
159
+ mechanism = "slash-command";
160
+ }
161
+ // A typed command runs only where the harness reads it as one: never over a dialog.
162
+ if (["blocked", "waiting", "unknown"].includes(status))
163
+ return skip("slash-command", "The agent needs input in the terminal first.");
164
+ if (harness === "codex" && status === "working")
165
+ return skip("slash-command", "The agent is working.");
166
+ const refused = await typeCommand(deps, server, pane, `/rename ${label}`);
167
+ if (refused)
168
+ return skip("slash-command", `Not typed: ${refused}.`);
169
+ const applied = await confirmed(deps, harness === "claude" ? async () => (await deps.claudeTitle(target)) === label
170
+ : async () => (await deps.codexIndexTitle(session)) === label);
171
+ return applied ? { harness, mechanism: "slash-command", applied: true } : skip("slash-command", "The harness did not confirm the new name.");
172
+ }
173
+ catch (error) {
174
+ return skip(mechanism, error instanceof Error ? error.message.slice(0, 200) : "The rename failed.");
175
+ }
176
+ }
177
+ /** `POST /v1/sessions/rename`: terminal labels first, then the harness's own name. */
178
+ export async function renameSession(server, data, deps = defaultRenameDeps) {
179
+ const { workspaceId, tabId, paneId, label } = renameRequest.parse(data);
180
+ const s = await deps.snapshot(server);
181
+ if (!objects(s.tabs).some(t => t.tab_id === tabId && t.workspace_id === workspaceId))
182
+ throw new BridgeError(409, "The tab changed.");
183
+ const { pane, renameTab } = renamePane(objects(s.panes).filter(p => p.tab_id === tabId && p.workspace_id === workspaceId), paneId);
184
+ const terminal = deps.terminal();
185
+ await terminal.renamePane(server, String(pane.pane_id), label);
186
+ if (renameTab)
187
+ await terminal.groupAction(server, "rename", { workspace: workspaceId, tab: tabId }, label);
188
+ const native = await nativeRename(deps, server, workspaceId, tabId, pane, label);
189
+ return { ok: true, label, pane: pane.pane_id, native };
190
+ }
@@ -5,7 +5,7 @@ import { objects } from "./protocol.js";
5
5
  import { snapshot } from "./herdr.js";
6
6
  import { terminalProvider } from "./terminal.js";
7
7
  import { webServers } from "./projects.js";
8
- import { transcriptPath } from "./transcripts.js";
8
+ import { targetTranscriptPath } from "./transcripts.js";
9
9
  /**
10
10
  * The web servers one agent session owns, for the chat's ••• menu. Never a
11
11
  * machine-wide list: a server belongs to a session only when
@@ -110,7 +110,7 @@ async function transcriptTail(file) {
110
110
  export async function sessionWebServers(target, pane) {
111
111
  const [servers, table, own, file] = await Promise.all([
112
112
  webServers(), processTable(target.session), paneRoots(target.server, pane.pane_id),
113
- transcriptPath(target.source, target.session).catch(() => undefined),
113
+ targetTranscriptPath(target).catch(() => undefined),
114
114
  ]);
115
115
  const parents = table.parents;
116
116
  const ownRoots = new Set([...own, ...table.runners]);
@@ -0,0 +1,60 @@
1
+ import { AgentHooks } from "./agent-hooks.js";
2
+ import { type CodexNextTurn } from "./codex-servers.js";
3
+ import { type Json, type PermissionMode, type Target } from "./protocol.js";
4
+ /** T3's CodexSessionRuntime mapping. The reviewer is always sent: leaving it
5
+ * out would keep `auto_review` from an earlier turn. */
6
+ export declare const CODEX_MODES: Record<PermissionMode, Pick<CodexNextTurn, "approvalPolicy" | "approvalsReviewer" | "sandboxPolicy">>;
7
+ export declare const CLAUDE_NAMES: Record<string, string>;
8
+ /** The same Codex mode as command-line flags, for a Codex started with no thread yet. */
9
+ export declare function codexModeFlags(mode: PermissionMode): string[];
10
+ export interface SettingsCapabilities {
11
+ permissionModes: string[];
12
+ plan: boolean;
13
+ fast: boolean;
14
+ }
15
+ /** What this pane can change from the phone; undefined for harnesses that
16
+ * offer nothing. A hand-started Codex TUI is empty: its state is still read
17
+ * from its transcript, but switching is not offered. Claude's cycle holds
18
+ * `full-access` only when it was launched allowing bypass. */
19
+ export declare function settingsCapabilities(source: string, codexServed: boolean, claudeBypass?: boolean): SettingsCapabilities | undefined;
20
+ /** Claude's permission mode from the footer line its TUI draws under the
21
+ * composer. The transcript lags it: a `permission-mode` row is written only
22
+ * when a prompt is submitted, and a fresh session has no file yet. The line may
23
+ * trail hints ("(shift+tab to cycle) · 1 agent"), so only its start counts. */
24
+ export declare function claudeFooterMode(screen: string): string | undefined;
25
+ export interface SettingsState {
26
+ permissionMode?: string;
27
+ plan?: boolean;
28
+ fast?: boolean;
29
+ }
30
+ /** A Claude pane's settings as its footer shows them, read at most once per
31
+ * `PHREN_DIALOG_THROTTLE_MS` like the dialog check, and whether bypass was
32
+ * ever seen in it (which is when full access is offered). */
33
+ export declare class ClaudeSettingsReader {
34
+ private readonly hooks;
35
+ private readonly every;
36
+ private cache;
37
+ constructor(hooks: AgentHooks, every?: number);
38
+ read(target: Target, terminal: unknown): Promise<{
39
+ state?: SettingsState;
40
+ bypass: boolean;
41
+ }>;
42
+ }
43
+ /** One settings transaction per pane owns its terminal input until it ends. */
44
+ export declare class SettingsSwitcher {
45
+ private readonly hooks;
46
+ private readonly wait;
47
+ private active;
48
+ private readonly reader;
49
+ constructor(hooks: AgentHooks, wait?: number);
50
+ /** A Claude pane's footer-read settings and capabilities for the stream. */
51
+ streamSettings(target: Target, terminal: unknown, codexServed: boolean): Promise<{
52
+ settings?: SettingsCapabilities;
53
+ settingsState?: SettingsState;
54
+ }>;
55
+ private key;
56
+ assertAvailable(target: Target): void;
57
+ switch(target: Target, data: Json): Promise<Json>;
58
+ private hold;
59
+ private claude;
60
+ }