@phren/cli 0.3.11 → 0.3.13
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.
- package/dist/bridge/account-usage.d.ts +124 -0
- package/dist/bridge/account-usage.js +258 -0
- package/dist/bridge/agent-hook-stores.d.ts +3 -0
- package/dist/bridge/agent-hook-stores.js +5 -0
- package/dist/bridge/agent-hooks.d.ts +162 -5
- package/dist/bridge/agent-hooks.js +682 -49
- package/dist/bridge/approval-summary.d.ts +16 -0
- package/dist/bridge/approval-summary.js +133 -0
- package/dist/bridge/authority-command.d.ts +20 -0
- package/dist/bridge/authority-command.js +120 -0
- package/dist/bridge/authority.d.ts +223 -0
- package/dist/bridge/authority.js +231 -0
- package/dist/bridge/claude-account-setup.d.ts +14 -0
- package/dist/bridge/claude-account-setup.js +69 -0
- package/dist/bridge/claude-accounts.d.ts +49 -0
- package/dist/bridge/claude-accounts.js +195 -0
- package/dist/bridge/code-routes.js +2 -2
- package/dist/bridge/codex-app-server.d.ts +144 -0
- package/dist/bridge/codex-app-server.js +319 -0
- package/dist/bridge/codex-auth-refresh.d.ts +33 -0
- package/dist/bridge/codex-auth-refresh.js +71 -0
- package/dist/bridge/codex-hook-trust.d.ts +27 -0
- package/dist/bridge/codex-hook-trust.js +237 -0
- package/dist/bridge/codex-question-reply.d.ts +16 -0
- package/dist/bridge/codex-question-reply.js +57 -0
- package/dist/bridge/codex-review-mode.d.ts +10 -0
- package/dist/bridge/codex-review-mode.js +74 -0
- package/dist/bridge/codex-servers.d.ts +217 -0
- package/dist/bridge/codex-servers.js +636 -0
- package/dist/bridge/command.js +97 -4
- package/dist/bridge/computer-identity.d.ts +75 -0
- package/dist/bridge/computer-identity.js +166 -0
- package/dist/bridge/conductor-group.d.ts +91 -8
- package/dist/bridge/conductor-group.js +105 -9
- package/dist/bridge/conductor-role.d.ts +94 -0
- package/dist/bridge/conductor-role.js +176 -0
- package/dist/bridge/conductor-sets.d.ts +20 -0
- package/dist/bridge/conductor-sets.js +59 -0
- package/dist/bridge/dispatch-command.d.ts +3 -0
- package/dist/bridge/dispatch-command.js +82 -12
- package/dist/bridge/dispatch-returns.d.ts +128 -19
- package/dist/bridge/dispatch-returns.js +371 -54
- package/dist/bridge/dispatch.d.ts +128 -3
- package/dist/bridge/dispatch.js +303 -42
- package/dist/bridge/fanouts.d.ts +7 -1
- package/dist/bridge/fanouts.js +39 -4
- package/dist/bridge/folder-trust.d.ts +56 -0
- package/dist/bridge/folder-trust.js +195 -0
- package/dist/bridge/grants.d.ts +22 -1
- package/dist/bridge/grants.js +57 -9
- package/dist/bridge/hand-off-queue.d.ts +74 -0
- package/dist/bridge/hand-off-queue.js +235 -0
- package/dist/bridge/hand-off.d.ts +30 -10
- package/dist/bridge/hand-off.js +79 -46
- package/dist/bridge/harness-status.d.ts +11 -0
- package/dist/bridge/harness-status.js +32 -0
- package/dist/bridge/harnesses.d.ts +39 -0
- package/dist/bridge/harnesses.js +65 -0
- package/dist/bridge/health.d.ts +2 -0
- package/dist/bridge/health.js +1 -1
- package/dist/bridge/herdr.d.ts +11 -2
- package/dist/bridge/herdr.js +98 -23
- package/dist/bridge/hook-fast.d.ts +4 -0
- package/dist/bridge/hook-fast.js +68 -0
- package/dist/bridge/install.d.ts +16 -1
- package/dist/bridge/install.js +102 -21
- package/dist/bridge/launch-brief.d.ts +96 -0
- package/dist/bridge/launch-brief.js +164 -0
- package/dist/bridge/model-switch.d.ts +3 -0
- package/dist/bridge/model-switch.js +17 -1
- package/dist/bridge/models.d.ts +8 -4
- package/dist/bridge/models.js +24 -8
- package/dist/bridge/mux-wire.d.ts +9 -0
- package/dist/bridge/mux-wire.js +19 -0
- package/dist/bridge/opencode-pane-server.d.ts +113 -0
- package/dist/bridge/opencode-pane-server.js +332 -0
- package/dist/bridge/opencode-panes.d.ts +111 -0
- package/dist/bridge/opencode-panes.js +295 -0
- package/dist/bridge/pane-accounts.d.ts +8 -0
- package/dist/bridge/pane-accounts.js +46 -0
- package/dist/bridge/pane-status.d.ts +12 -0
- package/dist/bridge/pane-status.js +50 -1
- package/dist/bridge/prompt-startup.d.ts +5 -0
- package/dist/bridge/prompt-startup.js +24 -0
- package/dist/bridge/protocol.d.ts +5 -0
- package/dist/bridge/protocol.js +4 -0
- package/dist/bridge/push.d.ts +11 -4
- package/dist/bridge/push.js +27 -4
- package/dist/bridge/questions.d.ts +37 -2
- package/dist/bridge/questions.js +158 -15
- package/dist/bridge/resources.d.ts +9 -3
- package/dist/bridge/resources.js +24 -8
- package/dist/bridge/schedule-format.d.ts +2 -0
- package/dist/bridge/schedule-format.js +3 -0
- package/dist/bridge/schedule-launch.d.ts +2 -1
- package/dist/bridge/schedule-launch.js +27 -53
- package/dist/bridge/schedule-watch.d.ts +24 -1
- package/dist/bridge/schedule-watch.js +89 -2
- package/dist/bridge/server-launch.d.ts +22 -5
- package/dist/bridge/server-launch.js +238 -32
- package/dist/bridge/server-overview.d.ts +10 -3
- package/dist/bridge/server-overview.js +32 -15
- package/dist/bridge/server-pane-routes.d.ts +3 -0
- package/dist/bridge/server-pane-routes.js +150 -18
- package/dist/bridge/server-routes.d.ts +11 -0
- package/dist/bridge/server-routes.js +214 -36
- package/dist/bridge/server-stream.d.ts +2 -0
- package/dist/bridge/server-stream.js +22 -8
- package/dist/bridge/server.d.ts +6 -1
- package/dist/bridge/server.js +58 -13
- package/dist/bridge/session-activity.d.ts +93 -0
- package/dist/bridge/session-activity.js +139 -0
- package/dist/bridge/session-rename.d.ts +48 -0
- package/dist/bridge/session-rename.js +190 -0
- package/dist/bridge/session-servers.js +2 -2
- package/dist/bridge/settings-switch.d.ts +60 -0
- package/dist/bridge/settings-switch.js +287 -0
- package/dist/bridge/speech-voice.d.ts +55 -0
- package/dist/bridge/speech-voice.js +132 -0
- package/dist/bridge/speech.d.ts +70 -11
- package/dist/bridge/speech.js +169 -41
- package/dist/bridge/stalls.d.ts +22 -0
- package/dist/bridge/stalls.js +57 -0
- package/dist/bridge/sudo.d.ts +209 -0
- package/dist/bridge/sudo.js +501 -0
- package/dist/bridge/terminal-herdr.js +9 -3
- package/dist/bridge/terminal-tmux.d.ts +15 -3
- package/dist/bridge/terminal-tmux.js +103 -13
- package/dist/bridge/terminal.d.ts +27 -4
- package/dist/bridge/terminal.js +8 -1
- package/dist/bridge/transcript-claude.d.ts +4 -0
- package/dist/bridge/transcript-claude.js +186 -16
- package/dist/bridge/transcript-codex.d.ts +0 -2
- package/dist/bridge/transcript-codex.js +29 -4
- package/dist/bridge/transcript-preview.d.ts +1 -0
- package/dist/bridge/transcript-preview.js +24 -4
- package/dist/bridge/transcripts.d.ts +15 -2
- package/dist/bridge/transcripts.js +47 -16
- package/dist/bridge/turn-records.d.ts +85 -0
- package/dist/bridge/turn-records.js +143 -0
- package/dist/bridge/uploads.d.ts +3 -0
- package/dist/bridge/uploads.js +14 -0
- package/dist/bridge/usage.d.ts +56 -10
- package/dist/bridge/usage.js +190 -45
- package/dist/bridge-hook.mjs +27975 -20636
- package/dist/cli/claude-unmanaged.d.ts +12 -0
- package/dist/cli/claude-unmanaged.js +105 -0
- package/dist/cli/schedules.js +2 -1
- package/dist/cli/session-start.js +10 -1
- package/dist/cli/session-stop.js +9 -1
- package/dist/cli-registry.js +31 -6
- package/dist/data/task-receipts.d.ts +19 -0
- package/dist/data/task-receipts.js +68 -0
- package/dist/data/tasks.js +4 -1
- package/dist/generated/memory-ui-graph.browser.js +63 -63
- package/dist/graph-core/model.js +24 -3
- package/dist/graph-core/types.d.ts +4 -1
- package/dist/graph-core/types.js +1 -0
- package/dist/init/init-preset.js +7 -3
- package/dist/init/init-uninstall.js +2 -11
- package/dist/init/teardown.d.ts +3 -0
- package/dist/init/teardown.js +47 -0
- package/dist/link/context.js +21 -9
- package/dist/mcp/plugin-mode.js +1 -1
- package/dist/memory-ui-graph.runtime.js +63 -63
- package/dist/modules/code-package.d.ts +1 -0
- package/dist/modules/code-package.js +36 -7
- package/dist/modules/registry.js +17 -16
- package/dist/profile-store.d.ts +6 -0
- package/dist/profile-store.js +25 -1
- package/dist/shell/graph/glyphs.js +4 -0
- package/dist/shell/graph/graph-view.js +2 -0
- package/dist/status.js +7 -1
- package/dist/sync/task-claim.js +3 -2
- package/dist/task/lifecycle.js +25 -4
- package/dist/test-global-setup.js +5 -0
- package/dist/tools/dispatch.js +52 -4
- package/dist/tools/tasks.js +10 -1
- package/package.json +1 -1
- package/plugins/opencode/phren-transcript.js +87 -4
- package/starter/global/AGENTS.md +4 -0
- package/starter/global/skills/conductor/SKILL.md +51 -4
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type Json } from "./protocol.js";
|
|
3
|
+
/**
|
|
4
|
+
* Which pane is this computer's conductor, as the Hook keeps it
|
|
5
|
+
* (docs/conductor-sets.md). The role follows the pane: server, pane id and
|
|
6
|
+
* terminal id. When the agent in that pane restarts or logs in again, the
|
|
7
|
+
* role stays and the new session is recorded. The Herdr agent name or tmux
|
|
8
|
+
* `@phren_agent` is only a label, read once to migrate a conductor that
|
|
9
|
+
* predates this file.
|
|
10
|
+
*/
|
|
11
|
+
declare const recordSchema: z.ZodObject<{
|
|
12
|
+
server: z.ZodString;
|
|
13
|
+
pane: z.ZodString;
|
|
14
|
+
terminal: z.ZodOptional<z.ZodString>;
|
|
15
|
+
workspace: z.ZodOptional<z.ZodString>;
|
|
16
|
+
tab: z.ZodOptional<z.ZodString>;
|
|
17
|
+
source: z.ZodOptional<z.ZodEnum<{
|
|
18
|
+
claude: "claude";
|
|
19
|
+
codex: "codex";
|
|
20
|
+
copilot: "copilot";
|
|
21
|
+
opencode: "opencode";
|
|
22
|
+
phren: "phren";
|
|
23
|
+
}>>;
|
|
24
|
+
session: z.ZodOptional<z.ZodString>;
|
|
25
|
+
since: z.ZodString;
|
|
26
|
+
by: z.ZodEnum<{
|
|
27
|
+
launch: "launch";
|
|
28
|
+
migrated: "migrated";
|
|
29
|
+
owner: "owner";
|
|
30
|
+
}>;
|
|
31
|
+
}, z.core.$strip>;
|
|
32
|
+
export type ConductorRecord = z.infer<typeof recordSchema>;
|
|
33
|
+
/** A cleared name keeps its time (`name: null`), so an older name arriving later does not return. */
|
|
34
|
+
declare const setNameSchema: z.ZodObject<{
|
|
35
|
+
name: z.ZodNullable<z.ZodString>;
|
|
36
|
+
namedAt: z.ZodString;
|
|
37
|
+
}, z.core.$strip>;
|
|
38
|
+
export type SetName = z.infer<typeof setNameSchema>;
|
|
39
|
+
/** `conductor` absent is legacy mode: nothing has decided the role yet, so a
|
|
40
|
+
* conductor is still recognized by its mux name. `null` is no conductor. */
|
|
41
|
+
declare const stateSchema: z.ZodObject<{
|
|
42
|
+
version: z.ZodLiteral<1>;
|
|
43
|
+
conductor: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
44
|
+
server: z.ZodString;
|
|
45
|
+
pane: z.ZodString;
|
|
46
|
+
terminal: z.ZodOptional<z.ZodString>;
|
|
47
|
+
workspace: z.ZodOptional<z.ZodString>;
|
|
48
|
+
tab: z.ZodOptional<z.ZodString>;
|
|
49
|
+
source: z.ZodOptional<z.ZodEnum<{
|
|
50
|
+
claude: "claude";
|
|
51
|
+
codex: "codex";
|
|
52
|
+
copilot: "copilot";
|
|
53
|
+
opencode: "opencode";
|
|
54
|
+
phren: "phren";
|
|
55
|
+
}>>;
|
|
56
|
+
session: z.ZodOptional<z.ZodString>;
|
|
57
|
+
since: z.ZodString;
|
|
58
|
+
by: z.ZodEnum<{
|
|
59
|
+
launch: "launch";
|
|
60
|
+
migrated: "migrated";
|
|
61
|
+
owner: "owner";
|
|
62
|
+
}>;
|
|
63
|
+
}, z.core.$strip>>>;
|
|
64
|
+
set: z.ZodOptional<z.ZodObject<{
|
|
65
|
+
name: z.ZodNullable<z.ZodString>;
|
|
66
|
+
namedAt: z.ZodString;
|
|
67
|
+
}, z.core.$strip>>;
|
|
68
|
+
}, z.core.$strip>;
|
|
69
|
+
export type RoleState = z.infer<typeof stateSchema>;
|
|
70
|
+
/** The saved state; undefined before the file exists. */
|
|
71
|
+
export declare function readRoleState(): Promise<RoleState | undefined>;
|
|
72
|
+
/** For tests: forget the cached state. */
|
|
73
|
+
export declare function resetRoleState(): void;
|
|
74
|
+
/** A pane running an agent the Hook can talk to. */
|
|
75
|
+
export declare const runsAgent: (pane: Json | undefined) => pane is Json;
|
|
76
|
+
/**
|
|
77
|
+
* The conductor's pane on `server`, from a snapshot of it, whether or not an
|
|
78
|
+
* agent runs there right now. A recorded pane gone from the snapshot ends the
|
|
79
|
+
* role. In legacy mode a live pane with a conductor name is adopted and saved.
|
|
80
|
+
*/
|
|
81
|
+
export declare function conductorPane(server: string, s: Json): Promise<Json | undefined>;
|
|
82
|
+
/** The recorded conductor, whatever its server; undefined in legacy mode or with none. */
|
|
83
|
+
export declare function recordedConductor(): Promise<ConductorRecord | undefined>;
|
|
84
|
+
/** Makes `pane` on `server` this computer's conductor, replacing any record. */
|
|
85
|
+
export declare function recordConductor(server: string, pane: Json, by: ConductorRecord["by"], session?: string): Promise<ConductorRecord>;
|
|
86
|
+
/** The session now running in the conductor's pane: a restart or new login reattaches here. */
|
|
87
|
+
export declare function noteConductorSession(server: string, pane: string, session: string): Promise<void>;
|
|
88
|
+
/** Ends the role. With `pane`, only when that pane holds it. Returns what was stopped. */
|
|
89
|
+
export declare function clearConductor(pane?: string): Promise<ConductorRecord | undefined>;
|
|
90
|
+
/** The set name this Hook holds. */
|
|
91
|
+
export declare function readSetName(): Promise<SetName | undefined>;
|
|
92
|
+
/** Keeps `name` when it is newer than the one held; `null` clears. Returns whether it changed. */
|
|
93
|
+
export declare function saveSetName(name: string | null, namedAt: string): Promise<boolean>;
|
|
94
|
+
export {};
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { lstatSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { isConductorName, paneAgentName } from "./herdr.js";
|
|
5
|
+
import { atomicInPrivateDir, bridgeRoot, id, objects, provider, serverName } from "./protocol.js";
|
|
6
|
+
/**
|
|
7
|
+
* Which pane is this computer's conductor, as the Hook keeps it
|
|
8
|
+
* (docs/conductor-sets.md). The role follows the pane: server, pane id and
|
|
9
|
+
* terminal id. When the agent in that pane restarts or logs in again, the
|
|
10
|
+
* role stays and the new session is recorded. The Herdr agent name or tmux
|
|
11
|
+
* `@phren_agent` is only a label, read once to migrate a conductor that
|
|
12
|
+
* predates this file.
|
|
13
|
+
*/
|
|
14
|
+
const recordSchema = z.object({
|
|
15
|
+
server: serverName, pane: id, terminal: z.string().max(200).optional(), workspace: id.optional(), tab: id.optional(),
|
|
16
|
+
source: provider.optional(), session: z.string().max(200).optional(),
|
|
17
|
+
since: z.string().datetime(), by: z.enum(["launch", "owner", "migrated"]),
|
|
18
|
+
});
|
|
19
|
+
/** A cleared name keeps its time (`name: null`), so an older name arriving later does not return. */
|
|
20
|
+
const setNameSchema = z.object({ name: z.string().min(1).max(60).nullable(), namedAt: z.string().datetime() });
|
|
21
|
+
/** `conductor` absent is legacy mode: nothing has decided the role yet, so a
|
|
22
|
+
* conductor is still recognized by its mux name. `null` is no conductor. */
|
|
23
|
+
const stateSchema = z.object({ version: z.literal(1), conductor: recordSchema.nullable().optional(), set: setNameSchema.optional() });
|
|
24
|
+
const roleFile = () => path.join(bridgeRoot(), "conductor-role.json");
|
|
25
|
+
/** The Hook is the only writer, so the last state it read or wrote stands in for the file. */
|
|
26
|
+
let cached;
|
|
27
|
+
/** The saved state; undefined before the file exists. */
|
|
28
|
+
export async function readRoleState() {
|
|
29
|
+
const file = roleFile();
|
|
30
|
+
if (cached?.file === file)
|
|
31
|
+
return cached.state;
|
|
32
|
+
// Read once per process and synchronously: the overview's time budget
|
|
33
|
+
// should not wait on disk for a file of a few hundred bytes.
|
|
34
|
+
let text;
|
|
35
|
+
try {
|
|
36
|
+
const info = lstatSync(file);
|
|
37
|
+
// Only a small regular file the Hook wrote is read; anything else counts as damaged.
|
|
38
|
+
text = info.isFile() && info.size <= 65_536 ? readFileSync(file, "utf8") : "";
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
text = undefined;
|
|
42
|
+
}
|
|
43
|
+
let state;
|
|
44
|
+
if (text !== undefined) {
|
|
45
|
+
// A damaged file is a state with no conductor, never a return to names.
|
|
46
|
+
try {
|
|
47
|
+
state = stateSchema.parse(JSON.parse(text));
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
state = { version: 1, conductor: null };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
cached = { file, state };
|
|
54
|
+
return state;
|
|
55
|
+
}
|
|
56
|
+
async function writeRoleState(state) {
|
|
57
|
+
const file = roleFile();
|
|
58
|
+
await atomicInPrivateDir(file, JSON.stringify(state, null, 2) + "\n");
|
|
59
|
+
cached = { file, state };
|
|
60
|
+
}
|
|
61
|
+
/** Every read-modify-write of the state runs one at a time: an overview poll
|
|
62
|
+
* noting a moved pane must not interleave with a make or stop and bring back
|
|
63
|
+
* the record it replaced. */
|
|
64
|
+
let queue = Promise.resolve();
|
|
65
|
+
function serialized(run) {
|
|
66
|
+
const next = queue.then(run, run);
|
|
67
|
+
queue = next.catch(() => undefined);
|
|
68
|
+
return next;
|
|
69
|
+
}
|
|
70
|
+
/** For tests: forget the cached state. */
|
|
71
|
+
export function resetRoleState() { cached = undefined; }
|
|
72
|
+
const liveStatus = (pane) => !["completed", "exited", "failed", "stopped"].includes(String(pane.agent_status));
|
|
73
|
+
/** A pane running an agent the Hook can talk to. */
|
|
74
|
+
export const runsAgent = (pane) => !!pane && provider.safeParse(pane.agent).success && liveStatus(pane);
|
|
75
|
+
function recordFor(server, pane, by, session) {
|
|
76
|
+
return {
|
|
77
|
+
server, pane: String(pane.pane_id), ...(typeof pane.terminal_id === "string" ? { terminal: pane.terminal_id } : {}),
|
|
78
|
+
...(id.safeParse(pane.workspace_id).success ? { workspace: String(pane.workspace_id) } : {}),
|
|
79
|
+
...(id.safeParse(pane.tab_id).success ? { tab: String(pane.tab_id) } : {}),
|
|
80
|
+
...(provider.safeParse(pane.agent).success ? { source: pane.agent } : {}),
|
|
81
|
+
...(session ? { session } : {}), since: new Date().toISOString(), by,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** The recorded pane in a snapshot of its server, when it is still the same terminal. */
|
|
85
|
+
function recordedPane(record, s) {
|
|
86
|
+
const pane = objects(s.panes).find(p => p.pane_id === record.pane);
|
|
87
|
+
if (!pane)
|
|
88
|
+
return undefined;
|
|
89
|
+
// A pane id the multiplexer gave to a new terminal is another pane.
|
|
90
|
+
if (record.terminal && typeof pane.terminal_id === "string" && pane.terminal_id !== record.terminal)
|
|
91
|
+
return undefined;
|
|
92
|
+
return pane;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The conductor's pane on `server`, from a snapshot of it, whether or not an
|
|
96
|
+
* agent runs there right now. A recorded pane gone from the snapshot ends the
|
|
97
|
+
* role. In legacy mode a live pane with a conductor name is adopted and saved.
|
|
98
|
+
*/
|
|
99
|
+
export function conductorPane(server, s) {
|
|
100
|
+
return serialized(async () => {
|
|
101
|
+
const state = await readRoleState();
|
|
102
|
+
if (state?.conductor === undefined) {
|
|
103
|
+
const named = objects(s.panes).find(pane => isConductorName(paneAgentName(s, pane)) && runsAgent(pane));
|
|
104
|
+
if (!named)
|
|
105
|
+
return undefined;
|
|
106
|
+
await writeRoleState({ ...state, version: 1, conductor: recordFor(server, named, "migrated") });
|
|
107
|
+
return named;
|
|
108
|
+
}
|
|
109
|
+
const record = state.conductor;
|
|
110
|
+
if (!record || record.server !== server)
|
|
111
|
+
return undefined;
|
|
112
|
+
const pane = recordedPane(record, s);
|
|
113
|
+
if (!pane) {
|
|
114
|
+
await writeRoleState({ ...state, conductor: null });
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
// Keep the place current: a pane moved to another tab or workspace is the same pane.
|
|
118
|
+
const moved = (id.safeParse(pane.workspace_id).success && pane.workspace_id !== record.workspace) || (id.safeParse(pane.tab_id).success && pane.tab_id !== record.tab)
|
|
119
|
+
|| (provider.safeParse(pane.agent).success && pane.agent !== record.source);
|
|
120
|
+
if (moved)
|
|
121
|
+
await writeRoleState({ ...state, conductor: { ...record, workspace: String(pane.workspace_id), tab: String(pane.tab_id),
|
|
122
|
+
...(provider.safeParse(pane.agent).success ? { source: pane.agent } : {}) } });
|
|
123
|
+
return pane;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/** The recorded conductor, whatever its server; undefined in legacy mode or with none. */
|
|
127
|
+
export async function recordedConductor() {
|
|
128
|
+
return (await readRoleState())?.conductor ?? undefined;
|
|
129
|
+
}
|
|
130
|
+
/** Makes `pane` on `server` this computer's conductor, replacing any record. */
|
|
131
|
+
export function recordConductor(server, pane, by, session) {
|
|
132
|
+
return serialized(async () => {
|
|
133
|
+
const state = await readRoleState() ?? { version: 1 };
|
|
134
|
+
const record = recordFor(server, pane, by, session);
|
|
135
|
+
await writeRoleState({ ...state, conductor: record });
|
|
136
|
+
return record;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/** The session now running in the conductor's pane: a restart or new login reattaches here. */
|
|
140
|
+
export function noteConductorSession(server, pane, session) {
|
|
141
|
+
return serialized(async () => {
|
|
142
|
+
const state = await readRoleState();
|
|
143
|
+
const record = state?.conductor;
|
|
144
|
+
if (!state || !record || record.server !== server || record.pane !== pane || record.session === session)
|
|
145
|
+
return;
|
|
146
|
+
await writeRoleState({ ...state, conductor: { ...record, session } });
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/** Ends the role. With `pane`, only when that pane holds it. Returns what was stopped. */
|
|
150
|
+
export function clearConductor(pane) {
|
|
151
|
+
return serialized(async () => {
|
|
152
|
+
const state = await readRoleState();
|
|
153
|
+
const record = state?.conductor ?? undefined;
|
|
154
|
+
if (record && pane !== undefined && record.pane !== pane)
|
|
155
|
+
return undefined;
|
|
156
|
+
// Written even with nothing to clear: a stop also ends legacy mode, so a
|
|
157
|
+
// leftover conductor name is not adopted again.
|
|
158
|
+
await writeRoleState({ ...state, version: 1, conductor: null });
|
|
159
|
+
return record;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
/** The set name this Hook holds. */
|
|
163
|
+
export async function readSetName() {
|
|
164
|
+
return (await readRoleState())?.set;
|
|
165
|
+
}
|
|
166
|
+
/** Keeps `name` when it is newer than the one held; `null` clears. Returns whether it changed. */
|
|
167
|
+
export function saveSetName(name, namedAt) {
|
|
168
|
+
return serialized(async () => {
|
|
169
|
+
const state = await readRoleState();
|
|
170
|
+
const held = state?.set;
|
|
171
|
+
if (held && Date.parse(held.namedAt) >= Date.parse(namedAt))
|
|
172
|
+
return false;
|
|
173
|
+
await writeRoleState({ ...state, version: 1, set: { name, namedAt } });
|
|
174
|
+
return held?.name !== name;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Json } from "./protocol.js";
|
|
2
|
+
/** The Hook routes for linked computer sets (docs/conductor-sets.md). */
|
|
3
|
+
interface LocalInfo {
|
|
4
|
+
computer: {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
aliases?: string[];
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
/** `GET /v1/conductor`: this computer's live conductor and what a peer needs
|
|
11
|
+
* to place it in a set. Asked with the caller's name or host key, it also
|
|
12
|
+
* says whether this computer lists the caller back. */
|
|
13
|
+
export declare function conductorAnswer(info: LocalInfo, url: URL): Promise<Json>;
|
|
14
|
+
/** `GET /v1/sets`: this computer's set and every set its one-way peers are in, plus unlinked computers. */
|
|
15
|
+
export declare function readSets(info: LocalInfo): Promise<Json>;
|
|
16
|
+
/** `POST /v1/sets/name`: names this computer's set. The owner's call is sent
|
|
17
|
+
* on to every reachable member; a member's relay (with `namedAt`) is kept
|
|
18
|
+
* when newer than the name held, and goes no further. */
|
|
19
|
+
export declare function nameSet(data: Json): Promise<Json>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { askPeers, buildSets, localCaller } from "./conductor-group.js";
|
|
3
|
+
import { readSetName, saveSetName } from "./conductor-role.js";
|
|
4
|
+
import { readComputers } from "./computer-identity.js";
|
|
5
|
+
import { localNames } from "./computer-names.js";
|
|
6
|
+
import { listsCaller } from "./health.js";
|
|
7
|
+
import { optionalHookPeers, peerRequest } from "./peers.js";
|
|
8
|
+
import { localConductor } from "./server-launch.js";
|
|
9
|
+
const callerSchema = z.object({ name: z.string().max(253).optional(), hostKey: z.string().max(512).optional() });
|
|
10
|
+
/** `GET /v1/conductor`: this computer's live conductor and what a peer needs
|
|
11
|
+
* to place it in a set. Asked with the caller's name or host key, it also
|
|
12
|
+
* says whether this computer lists the caller back. */
|
|
13
|
+
export async function conductorAnswer(info, url) {
|
|
14
|
+
const caller = callerSchema.parse({ name: url.searchParams.get("name") ?? undefined, hostKey: url.searchParams.get("hostKey") ?? undefined });
|
|
15
|
+
const [conductor, links, set] = await Promise.all([localConductor(), listsCaller(caller), readSetName()]);
|
|
16
|
+
return { computer: info.computer, conductor: conductor ?? null, peers: links.computers,
|
|
17
|
+
...(caller.name || caller.hostKey ? { knowsCaller: links.knowsCaller } : {}), ...(set ? { set } : {}) };
|
|
18
|
+
}
|
|
19
|
+
/** `GET /v1/sets`: this computer's set and every set its one-way peers are in, plus unlinked computers. */
|
|
20
|
+
export async function readSets(info) {
|
|
21
|
+
const [{ peers, peerError }, conductor, set, { computers }] = await Promise.all([optionalHookPeers(), localConductor(), readSetName(),
|
|
22
|
+
readComputers({ local: { id: info.computer.id } })]);
|
|
23
|
+
const answers = await askPeers(peers, await localCaller());
|
|
24
|
+
const view = buildSets({ local: { name: info.computer.name, id: info.computer.id, ...(conductor ? { conductor } : {}), ...(set ? { set } : {}),
|
|
25
|
+
names: [info.computer.name, ...(info.computer.aliases ?? []), ...localNames()] }, answers, computers });
|
|
26
|
+
return { ...view, ...(peerError ? { peerError } : {}) };
|
|
27
|
+
}
|
|
28
|
+
const setNameValue = z.string().trim().min(1).max(60).refine(t => !/[\x00-\x1f\x7f]/.test(t)).nullable();
|
|
29
|
+
/** `POST /v1/sets/name`: names this computer's set. The owner's call is sent
|
|
30
|
+
* on to every reachable member; a member's relay (with `namedAt`) is kept
|
|
31
|
+
* when newer than the name held, and goes no further. */
|
|
32
|
+
export async function nameSet(data) {
|
|
33
|
+
const input = z.object({ name: setNameValue, namedAt: z.string().datetime().optional() }).strict().parse(data);
|
|
34
|
+
if (input.namedAt)
|
|
35
|
+
return { ok: true, name: input.name, changed: await saveSetName(input.name, input.namedAt) };
|
|
36
|
+
const namedAt = new Date().toISOString();
|
|
37
|
+
await saveSetName(input.name, namedAt);
|
|
38
|
+
const { peers } = await optionalHookPeers();
|
|
39
|
+
const answers = await askPeers(peers, await localCaller());
|
|
40
|
+
const told = [], unreachable = [];
|
|
41
|
+
await Promise.all(answers.map(async (answer) => {
|
|
42
|
+
// A one-way peer is in another set; a peer that could not answer is named so the owner knows it missed the name.
|
|
43
|
+
if (answer.ok && answer.knowsCaller === false)
|
|
44
|
+
return;
|
|
45
|
+
if (!answer.ok) {
|
|
46
|
+
if (!answer.disabled)
|
|
47
|
+
unreachable.push({ computer: answer.peer.name, error: answer.error });
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
await peerRequest(answer.peer, "/v1/sets/name", { name: input.name, namedAt }, 8_000);
|
|
52
|
+
told.push(answer.peer.name);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
unreachable.push({ computer: answer.peer.name, error: error instanceof Error ? error.message.slice(0, 300) : "Unreachable." });
|
|
56
|
+
}
|
|
57
|
+
}));
|
|
58
|
+
return { ok: true, name: input.name, namedAt, told: told.sort(), unreachable: unreachable.sort((a, b) => a.computer.localeCompare(b.computer)) };
|
|
59
|
+
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { type Json } from "./protocol.js";
|
|
1
2
|
export declare function runDispatch(args: string[]): Promise<number>;
|
|
2
3
|
export declare function runHandOff(args: string[]): Promise<number>;
|
|
4
|
+
/** One line per computer: name, reachability, link and conductor. */
|
|
5
|
+
export declare function formatSets(view: Json): string;
|
|
3
6
|
export declare function runConductor(args: string[]): Promise<number>;
|
|
@@ -3,7 +3,7 @@ import { hookRequest } from "./client.js";
|
|
|
3
3
|
import { dispatchSchema } from "./dispatch.js";
|
|
4
4
|
import { handOff } from "./hand-off.js";
|
|
5
5
|
import { terminalPaneFromEnv } from "./terminal.js";
|
|
6
|
-
import { addGrant, grantSchema,
|
|
6
|
+
import { addGrant, grantSchema, listNamedGrants, removeGrant } from "./grants.js";
|
|
7
7
|
import { sessionId } from "./protocol.js";
|
|
8
8
|
export async function runDispatch(args) {
|
|
9
9
|
if (args.length === 1 && args[0] === "status") {
|
|
@@ -14,18 +14,24 @@ export async function runDispatch(args) {
|
|
|
14
14
|
console.log(JSON.stringify(await hookRequest("/v1/dispatch/returns", {}), null, 2));
|
|
15
15
|
return 0;
|
|
16
16
|
}
|
|
17
|
+
if (args[0] === "usage" && args.slice(1).every(arg => arg === "--json")) {
|
|
18
|
+
const { readAccountUsage, formatAccountUsage } = await import("./account-usage.js");
|
|
19
|
+
const view = await readAccountUsage();
|
|
20
|
+
console.log(args.includes("--json") ? JSON.stringify(view, null, 2) : formatAccountUsage(view));
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
17
23
|
if (args.length === 1 && args[0] === "sessions") {
|
|
18
24
|
const { listLiveSessions } = await import("./hand-off.js");
|
|
19
25
|
console.log(JSON.stringify(await listLiveSessions(), null, 2));
|
|
20
26
|
return 0;
|
|
21
27
|
}
|
|
22
28
|
const { values, positionals } = parseArgs({ args, allowPositionals: true, options: {
|
|
23
|
-
harness: { type: "string", default: "codex" }, model: { type: "string" }, prompt: { type: "string" }, label: { type: "string" },
|
|
29
|
+
harness: { type: "string", default: "codex" }, model: { type: "string" }, effort: { type: "string" }, account: { type: "string" }, "permission-mode": { type: "string" }, prompt: { type: "string" }, label: { type: "string" },
|
|
24
30
|
"parent-provider": { type: "string" }, "parent-session": { type: "string" }, "parent-computer": { type: "string" },
|
|
25
31
|
"parent-server": { type: "string" }, "parent-workspace": { type: "string" }, "parent-tab": { type: "string" }, "parent-pane": { type: "string" },
|
|
26
32
|
} });
|
|
27
33
|
if (positionals.length !== 2)
|
|
28
|
-
throw new Error("Usage: phren dispatch <computer|anywhere> <project> --label <label> --prompt <brief> [--harness codex|claude|opencode] [--model <model>] [explicit parent flags]");
|
|
34
|
+
throw new Error("Usage: phren dispatch <computer|anywhere> <project> --label <label> --prompt <brief> [--harness codex|claude|opencode] [--model <model>] [--effort minimal|low|medium|high|xhigh|max] [--account <id>] [--permission-mode supervised|auto-edits|auto|full-access] [explicit parent flags]");
|
|
29
35
|
const parentValues = ["parent-provider", "parent-session", "parent-computer", "parent-server", "parent-workspace", "parent-tab", "parent-pane"];
|
|
30
36
|
const hasParent = parentValues.some(key => values[key] !== undefined);
|
|
31
37
|
const parent = hasParent ? {
|
|
@@ -35,7 +41,8 @@ export async function runDispatch(args) {
|
|
|
35
41
|
server: values["parent-server"], workspace: values["parent-workspace"], tab: values["parent-tab"], pane: values["parent-pane"],
|
|
36
42
|
source: values["parent-provider"], session: values["parent-session"],
|
|
37
43
|
} : undefined;
|
|
38
|
-
const
|
|
44
|
+
const { "permission-mode": permissionMode, ...rest } = values;
|
|
45
|
+
const ordinary = { ...Object.fromEntries(Object.entries(rest).filter(([key]) => !key.startsWith("parent-"))), ...(permissionMode !== undefined ? { permissionMode } : {}) };
|
|
39
46
|
const input = dispatchSchema.parse({ computer: positionals[0], project: positionals[1], ...ordinary,
|
|
40
47
|
...(parent ? { parent, parentTarget } : {}) });
|
|
41
48
|
// Run inside an agent's pane, the dispatch remembers that pane for return notices.
|
|
@@ -46,26 +53,88 @@ export async function runDispatch(args) {
|
|
|
46
53
|
}
|
|
47
54
|
export async function runHandOff(args) {
|
|
48
55
|
const { values, positionals } = parseArgs({ args, allowPositionals: true, options: {
|
|
49
|
-
session: { type: "string" }, text: { type: "string" }, project: { type: "string" },
|
|
56
|
+
"delivery-id": { type: "string" }, status: { type: "boolean" }, session: { type: "string" }, text: { type: "string" }, project: { type: "string" }, account: { type: "string" },
|
|
50
57
|
} });
|
|
51
|
-
if (positionals.length !== 1 || !values.session || !values.text)
|
|
52
|
-
throw new Error("Usage: phren hand-off <computer|local> --session <id> --text <prompt> [--project <slug>]");
|
|
58
|
+
if (positionals.length !== 1 || !values.session || (!values.status && !values.text) || (values.status && !values["delivery-id"]))
|
|
59
|
+
throw new Error("Usage: phren hand-off <computer|local> --session <id> --text <prompt> [--project <slug>] [--account <id>]");
|
|
53
60
|
const computer = positionals[0] === "local" ? undefined : positionals[0];
|
|
54
|
-
const result = await handOff({ ...(computer ? { computer } : {}), project: values.project, session: sessionId.parse(values.session), text: values.text });
|
|
61
|
+
const result = await handOff({ ...(computer ? { computer } : {}), project: values.project, ...(values.account ? { account: values.account } : {}), session: sessionId.parse(values.session), ...(values.status ? { status: true } : { text: values.text }), ...(values["delivery-id"] ? { deliveryId: values["delivery-id"] } : {}) });
|
|
55
62
|
console.log(JSON.stringify(result, null, 2));
|
|
56
63
|
return result.ok ? 0 : 1;
|
|
57
64
|
}
|
|
65
|
+
const CONDUCTOR_USAGE = "Usage: phren conductor status | make [--pane <id>] [--mux herdr:<name>|tmux:<name>] | stop [--pane <id>] | sets [--json] | sets name <name>|--clear | grants [list|add|remove]";
|
|
66
|
+
/** The Hook route for a pane's multiplexer: `--mux`, else the pane the command runs in. */
|
|
67
|
+
function muxQuery(mux, here) {
|
|
68
|
+
if (mux)
|
|
69
|
+
return `?mux=${encodeURIComponent(mux)}`;
|
|
70
|
+
return here ? `?server=${encodeURIComponent(here.server)}` : "";
|
|
71
|
+
}
|
|
72
|
+
/** One line per computer: name, reachability, link and conductor. */
|
|
73
|
+
export function formatSets(view) {
|
|
74
|
+
const lines = [];
|
|
75
|
+
const rows = (value) => Array.isArray(value) ? value.filter((item) => !!item && typeof item === "object") : [];
|
|
76
|
+
for (const set of rows(view.sets)) {
|
|
77
|
+
const conductors = typeof set.conductors === "number" ? set.conductors : 0;
|
|
78
|
+
lines.push(`${typeof set.name === "string" ? set.name : "Unnamed set"}${set.local ? " (this computer)" : ""}${conductors > 1 ? `: ${conductors} conductors, stop all but one` : ""}`);
|
|
79
|
+
for (const computer of rows(set.computers)) {
|
|
80
|
+
const state = computer.link === "self" ? "this computer" : computer.reachable === true ? "reachable" : computer.reachable === false ? "unreachable" : "not asked";
|
|
81
|
+
const link = computer.link === "self" ? "" : `, ${String(computer.link)} link`;
|
|
82
|
+
lines.push(` ${String(computer.name)}: ${state}${link}${computer.conductor ? ", conductor" : ""}${typeof computer.hint === "string" ? `. ${computer.hint}` : typeof computer.error === "string" ? `. ${computer.error}` : ""}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const unlinked = rows(view.unlinked);
|
|
86
|
+
if (unlinked.length) {
|
|
87
|
+
lines.push("Not linked");
|
|
88
|
+
for (const computer of unlinked)
|
|
89
|
+
lines.push(` ${String(computer.name)}. Link it with phren bridge link ${String(computer.name)}.`);
|
|
90
|
+
}
|
|
91
|
+
return lines.join("\n");
|
|
92
|
+
}
|
|
58
93
|
export async function runConductor(args) {
|
|
59
|
-
const [namespace, action = "list", ...rest] = args;
|
|
94
|
+
const [namespace = "status", action = "list", ...rest] = args;
|
|
95
|
+
if (namespace === "status") {
|
|
96
|
+
console.log(JSON.stringify(await hookRequest("/v1/conductor"), null, 2));
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
99
|
+
if (namespace === "make" || namespace === "stop") {
|
|
100
|
+
const { values } = parseArgs({ args: args.slice(1), options: { pane: { type: "string" }, mux: { type: "string" } } });
|
|
101
|
+
const here = await terminalPaneFromEnv();
|
|
102
|
+
if (namespace === "make") {
|
|
103
|
+
const pane = values.pane ?? here?.pane;
|
|
104
|
+
if (!pane)
|
|
105
|
+
throw new Error("Run phren conductor make inside the agent's pane, or name it with --pane <id>.");
|
|
106
|
+
const place = values.pane ? { paneId: values.pane } : { workspaceId: here.workspace, tabId: here.tab, paneId: here.pane };
|
|
107
|
+
const result = await hookRequest(`/v1/conductor/make${muxQuery(values.mux, values.pane ? undefined : here)}`, place);
|
|
108
|
+
console.log(JSON.stringify(result, null, 2));
|
|
109
|
+
return result.ok === true ? 0 : 1;
|
|
110
|
+
}
|
|
111
|
+
const result = await hookRequest("/v1/conductor/stop", values.pane ? { paneId: values.pane } : {});
|
|
112
|
+
console.log(result.stopped ? "This computer has no conductor now." : "This computer had no conductor.");
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
if (namespace === "sets") {
|
|
116
|
+
if (action === "name") {
|
|
117
|
+
const { values, positionals } = parseArgs({ args: rest, allowPositionals: true, options: { clear: { type: "boolean" } } });
|
|
118
|
+
const name = values.clear ? null : positionals.join(" ").trim();
|
|
119
|
+
if (name === "")
|
|
120
|
+
throw new Error("Usage: phren conductor sets name <name>|--clear");
|
|
121
|
+
console.log(JSON.stringify(await hookRequest("/v1/sets/name", { name }), null, 2));
|
|
122
|
+
return 0;
|
|
123
|
+
}
|
|
124
|
+
const view = await hookRequest("/v1/sets", undefined, undefined, 60_000);
|
|
125
|
+
console.log(args.includes("--json") ? JSON.stringify(view, null, 2) : formatSets(view));
|
|
126
|
+
return 0;
|
|
127
|
+
}
|
|
60
128
|
if (namespace !== "grants")
|
|
61
|
-
throw new Error(
|
|
129
|
+
throw new Error(CONDUCTOR_USAGE);
|
|
62
130
|
if (action === "list") {
|
|
63
|
-
console.log(JSON.stringify(await
|
|
131
|
+
console.log(JSON.stringify(await listNamedGrants(), null, 2));
|
|
64
132
|
return 0;
|
|
65
133
|
}
|
|
66
134
|
if (action === "add") {
|
|
67
135
|
const { values } = parseArgs({ args: rest, allowPositionals: true, options: {
|
|
68
136
|
scope: { type: "string" }, actions: { type: "string" }, computers: { type: "string" }, until: { type: "string" },
|
|
137
|
+
"max-permission-mode": { type: "string" },
|
|
69
138
|
} });
|
|
70
139
|
const actions = (values.actions ?? "dispatch,hand_off").split(",").map(value => value.trim()).filter(Boolean);
|
|
71
140
|
const grant = grantSchema.parse({
|
|
@@ -73,6 +142,7 @@ export async function runConductor(args) {
|
|
|
73
142
|
actions,
|
|
74
143
|
...(values.computers ? { computers: values.computers.split(",").map(value => value.trim()).filter(Boolean) } : {}),
|
|
75
144
|
...(values.until ? { until: values.until } : {}),
|
|
145
|
+
...(values["max-permission-mode"] ? { maxPermissionMode: values["max-permission-mode"] } : {}),
|
|
76
146
|
});
|
|
77
147
|
console.log(JSON.stringify(await addGrant(grant), null, 2));
|
|
78
148
|
return 0;
|
|
@@ -89,5 +159,5 @@ export async function runConductor(args) {
|
|
|
89
159
|
console.log(JSON.stringify(await removeGrant(index !== undefined ? { index } : { scope: values.scope }), null, 2));
|
|
90
160
|
return 0;
|
|
91
161
|
}
|
|
92
|
-
throw new Error(
|
|
162
|
+
throw new Error(CONDUCTOR_USAGE);
|
|
93
163
|
}
|