@phren/cli 0.3.71 → 0.3.73
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/agent-hooks.js +11 -1
- package/dist/bridge/browser-env.d.ts +5 -0
- package/dist/bridge/browser-env.js +110 -0
- package/dist/bridge/browser-protocol.d.ts +120 -0
- package/dist/bridge/browser-protocol.js +70 -0
- package/dist/bridge/browser-recordings.d.ts +63 -0
- package/dist/bridge/browser-recordings.js +148 -0
- package/dist/bridge/browser-registry.d.ts +64 -0
- package/dist/bridge/browser-registry.js +262 -0
- package/dist/bridge/browser-routes.d.ts +4 -0
- package/dist/bridge/browser-routes.js +39 -0
- package/dist/bridge/browser-shim/phren-playwright.cjs +184 -0
- package/dist/bridge/browser-socket.d.ts +2 -0
- package/dist/bridge/browser-socket.js +10 -0
- package/dist/bridge/browser-stream.d.ts +3 -0
- package/dist/bridge/browser-stream.js +129 -0
- package/dist/bridge/browser-watch.d.ts +12 -0
- package/dist/bridge/browser-watch.js +83 -0
- package/dist/bridge/claude-account-setup.js +3 -1
- package/dist/bridge/claude-accounts.d.ts +5 -1
- package/dist/bridge/claude-accounts.js +36 -6
- package/dist/bridge/codex-servers.d.ts +2 -0
- package/dist/bridge/codex-servers.js +1 -1
- package/dist/bridge/command.js +24 -5
- package/dist/bridge/dispatch-clone.d.ts +33 -0
- package/dist/bridge/dispatch-clone.js +176 -0
- package/dist/bridge/dispatch-command.d.ts +14 -0
- package/dist/bridge/dispatch-command.js +98 -13
- package/dist/bridge/dispatch-idempotency.d.ts +85 -0
- package/dist/bridge/dispatch-idempotency.js +235 -0
- package/dist/bridge/dispatch-returns.d.ts +6 -0
- package/dist/bridge/dispatch-returns.js +131 -1
- package/dist/bridge/dispatch-wait.d.ts +32 -0
- package/dist/bridge/dispatch-wait.js +177 -0
- package/dist/bridge/dispatch.d.ts +78 -2
- package/dist/bridge/dispatch.js +841 -25
- package/dist/bridge/dream-schedule.d.ts +133 -0
- package/dist/bridge/dream-schedule.js +322 -0
- package/dist/bridge/fanouts.js +20 -3
- package/dist/bridge/files.d.ts +1 -1
- package/dist/bridge/git-credential.d.ts +44 -0
- package/dist/bridge/git-credential.js +113 -0
- package/dist/bridge/git-host-auth.d.ts +9 -1
- package/dist/bridge/git-host-auth.js +18 -5
- package/dist/bridge/git-host-gitboy.js +8 -1
- package/dist/bridge/git-hosts.d.ts +12 -1
- package/dist/bridge/git-publish.js +3 -2
- package/dist/bridge/herdr.d.ts +4 -0
- package/dist/bridge/herdr.js +44 -9
- package/dist/bridge/install.d.ts +1 -1
- package/dist/bridge/install.js +12 -4
- package/dist/bridge/launch-brief.d.ts +11 -0
- package/dist/bridge/launch-brief.js +22 -0
- package/dist/bridge/launch-worktree.d.ts +2 -0
- package/dist/bridge/launch-worktree.js +5 -1
- package/dist/bridge/pane-status.js +1 -1
- package/dist/bridge/protocol.d.ts +7 -0
- package/dist/bridge/schedule-watch.js +13 -2
- package/dist/bridge/server-launch.d.ts +2 -0
- package/dist/bridge/server-launch.js +251 -145
- package/dist/bridge/server-routes.d.ts +3 -0
- package/dist/bridge/server-routes.js +33 -17
- package/dist/bridge/server-stream.d.ts +4 -0
- package/dist/bridge/server-stream.js +24 -5
- package/dist/bridge/server.js +35 -4
- package/dist/bridge/session-move.d.ts +10 -4
- package/dist/bridge/session-move.js +65 -34
- package/dist/bridge/simulator-stream.d.ts +2 -0
- package/dist/bridge/simulator-stream.js +2 -0
- package/dist/bridge/status-line-fast.d.ts +9 -0
- package/dist/bridge/status-line-fast.js +56 -0
- package/dist/bridge/tab-activity.d.ts +2 -0
- package/dist/bridge/tab-activity.js +15 -2
- package/dist/bridge/task-routes.d.ts +20 -16
- package/dist/bridge/task-routes.js +1 -1
- package/dist/bridge/terminal-herdr.d.ts +5 -1
- package/dist/bridge/terminal-herdr.js +67 -2
- package/dist/bridge/terminal-tmux.d.ts +2 -0
- package/dist/bridge/terminal-tmux.js +29 -7
- package/dist/bridge/terminal.d.ts +5 -1
- package/dist/bridge/transcript-claude.js +192 -112
- package/dist/bridge/transcript-preview.d.ts +2 -1
- package/dist/bridge/transcript-preview.js +85 -3
- package/dist/bridge/transcripts.js +17 -11
- package/dist/bridge/turn-records.js +16 -4
- package/dist/bridge/usage.d.ts +7 -1
- package/dist/bridge/usage.js +47 -31
- package/dist/bridge/worker-close.d.ts +15 -0
- package/dist/bridge/worker-close.js +304 -3
- package/dist/bridge/worker-identity.d.ts +51 -0
- package/dist/bridge/worker-identity.js +289 -0
- package/dist/bridge/worker-reports.js +15 -3
- package/dist/bridge-hook.mjs +39990 -36152
- package/dist/capabilities/mcp.js +1 -1
- package/dist/cli/browser-hint.d.ts +2 -0
- package/dist/cli/browser-hint.js +21 -0
- package/dist/cli/config-dreaming.d.ts +7 -0
- package/dist/cli/config-dreaming.js +72 -0
- package/dist/cli/config.js +6 -0
- package/dist/cli/dream.d.ts +59 -0
- package/dist/cli/dream.js +315 -0
- package/dist/cli/hooks-context.d.ts +2 -2
- package/dist/cli/hooks-context.js +3 -3
- package/dist/cli/hooks.js +20 -11
- package/dist/cli/namespaces-tasks.js +14 -0
- package/dist/cli-registry.js +28 -3
- package/dist/config/dreaming.d.ts +82 -0
- package/dist/config/dreaming.js +195 -0
- package/dist/config/resolve.js +11 -0
- package/dist/config/schema.d.ts +4 -4
- package/dist/config/schema.js +157 -1
- package/dist/content/citation-freshness.js +6 -0
- package/dist/content/learning.d.ts +1 -0
- package/dist/content/learning.js +2 -0
- package/dist/data/access.d.ts +1 -1
- package/dist/data/access.js +3 -1
- package/dist/data/task-contract.d.ts +5 -4
- package/dist/data/tasks.d.ts +5 -0
- package/dist/data/tasks.js +28 -2
- package/dist/dream/apply.d.ts +69 -0
- package/dist/dream/apply.js +372 -0
- package/dist/dream/budget.d.ts +78 -0
- package/dist/dream/budget.js +180 -0
- package/dist/dream/collect.d.ts +86 -0
- package/dist/dream/collect.js +449 -0
- package/dist/dream/eval/dream-adapter.d.ts +16 -0
- package/dist/dream/eval/dream-adapter.js +21 -0
- package/dist/dream/eval/fixture.d.ts +53 -0
- package/dist/dream/eval/fixture.js +172 -0
- package/dist/dream/eval/index.d.ts +40 -0
- package/dist/dream/eval/index.js +78 -0
- package/dist/dream/eval/score.d.ts +34 -0
- package/dist/dream/eval/score.js +99 -0
- package/dist/dream/eval/scripted-model.d.ts +19 -0
- package/dist/dream/eval/scripted-model.js +70 -0
- package/dist/dream/pages.d.ts +68 -0
- package/dist/dream/pages.js +220 -0
- package/dist/dream/privacy.d.ts +3 -0
- package/dist/dream/privacy.js +18 -0
- package/dist/dream/prompt.d.ts +13 -0
- package/dist/dream/prompt.js +84 -0
- package/dist/dream/run.d.ts +98 -0
- package/dist/dream/run.js +317 -0
- package/dist/dream/types.d.ts +74 -0
- package/dist/dream/types.js +4 -0
- package/dist/finding/lifecycle.d.ts +2 -2
- package/dist/finding/lifecycle.js +5 -2
- package/dist/finding/versions.d.ts +4 -0
- package/dist/finding/versions.js +28 -0
- package/dist/index.js +1 -0
- package/dist/modules/registry.js +10 -8
- package/dist/project-config.d.ts +3 -0
- package/dist/task/merge-links.d.ts +38 -0
- package/dist/task/merge-links.js +45 -0
- package/dist/task/reconcile.d.ts +39 -0
- package/dist/task/reconcile.js +201 -0
- package/dist/task/report-links.d.ts +10 -0
- package/dist/task/report-links.js +47 -0
- package/dist/tools/config.js +46 -4
- package/dist/tools/dispatch.js +2 -1
- package/dist/tools/dream.d.ts +3 -0
- package/dist/tools/dream.js +123 -0
- package/dist/tools/tasks.js +4 -1
- package/package.json +1 -1
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { noteBrowserLaunch } from "./browser-watch.js";
|
|
1
3
|
import { markPaneClosed } from "./worker-close.js";
|
|
2
4
|
import { mkdir, mkdtemp, readFile, realpath, rmdir } from "node:fs/promises";
|
|
3
5
|
import { phrenStoreRoot } from "./transcripts.js";
|
|
@@ -5,7 +7,7 @@ import path from "node:path";
|
|
|
5
7
|
import { z } from "zod";
|
|
6
8
|
import { homeDir } from "../home-paths.js";
|
|
7
9
|
import { defaultPhrenPath } from "../phren-paths.js";
|
|
8
|
-
import { agentNames, findPane, isConductorName, paneChatState, paneIdentity, servers, snapshot } from "./herdr.js";
|
|
10
|
+
import { agentNames, findPane, isConductorName, paneChatState, paneIdentity, seedStartingToken, servers, snapshot } from "./herdr.js";
|
|
9
11
|
import { agentNotReady, terminalKind, terminalName, terminalProvider } from "./terminal.js";
|
|
10
12
|
import { tmuxScroll } from "./terminal-tmux.js";
|
|
11
13
|
import { intervalFromEnv } from "./limits.js";
|
|
@@ -18,6 +20,9 @@ import { groupConductor } from "./conductor-group.js";
|
|
|
18
20
|
import { clearConductor, conductorPane, noteConductorSession, readRoleState, recordConductor, runsAgent } from "./conductor-role.js";
|
|
19
21
|
import { localNames } from "./computer-names.js";
|
|
20
22
|
import { pretrustFolder } from "./folder-trust.js";
|
|
23
|
+
import { keyedExplicitEnv, prepareKeyedGitboy, removeAgentTokenFile } from "./git-credential.js";
|
|
24
|
+
import { createDispatchClone, dispatchClonePaths } from "./dispatch-clone.js";
|
|
25
|
+
import { acquireLaunchLock, markSubmitted, releaseLaunchLock } from "./worker-identity.js";
|
|
21
26
|
import { claudeHome, claudeLaunchEnv, isAccountSlug, DEFAULT_ACCOUNT } from "./claude-accounts.js";
|
|
22
27
|
import { pickClaudeAccount } from "./account-choice.js";
|
|
23
28
|
import { harnessInventoryWithin, hasUsable, launchCheckOff } from "./harnesses.js";
|
|
@@ -49,6 +54,17 @@ function effortArgs(kind, effort) {
|
|
|
49
54
|
return ["--reasoning", effort];
|
|
50
55
|
return [];
|
|
51
56
|
}
|
|
57
|
+
/** Refuse a keyed launch whose resolved arguments would skip approvals or allow everything. */
|
|
58
|
+
export function assertKeyedLaunchArgs(kind, args) {
|
|
59
|
+
const line = args.join(" ");
|
|
60
|
+
const unsafe = /--dangerously|--allow-all|--yolo|--full-auto|bypassPermissions/.test(line)
|
|
61
|
+
|| /(?:^|\s)-a\s+never(?:\s|$)/.test(line)
|
|
62
|
+
|| /(?:^|\s)-s\s+danger-full-access(?:\s|$)/.test(line)
|
|
63
|
+
|| /--ask-for-approval(?:=|\s+)never/.test(line)
|
|
64
|
+
|| /--sandbox(?:=|\s+)danger-full-access/.test(line);
|
|
65
|
+
if (unsafe)
|
|
66
|
+
throw new BridgeError(400, `A keyed ${kind} launch may not use skip-approval or allow-all flags.`);
|
|
67
|
+
}
|
|
52
68
|
/** phren's own agent runs as `phren agent`: the subcommand and its interactive TUI lead its arguments. */
|
|
53
69
|
const PHREN_AGENT_ARGS = ["agent", "-i"];
|
|
54
70
|
/**
|
|
@@ -229,6 +245,14 @@ async function startWhenShellReady(server, pane, agent) {
|
|
|
229
245
|
for (;;) {
|
|
230
246
|
try {
|
|
231
247
|
await terminalProvider().startAgent(server, pane, agent);
|
|
248
|
+
if (agent.env?.PHREN_BROWSER_SOCKET) {
|
|
249
|
+
const processes = await terminalProvider().processes(server, pane).catch(() => undefined);
|
|
250
|
+
if (processes) {
|
|
251
|
+
const s = await terminalProvider().snapshot(server).catch(() => undefined);
|
|
252
|
+
const current = s ? objects(s.panes).find(p => p.pane_id === pane) : undefined;
|
|
253
|
+
noteBrowserLaunch(processes.foregroundPids, agent.env, current ? { server, pane: current } : undefined);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
232
256
|
return;
|
|
233
257
|
}
|
|
234
258
|
catch (error) {
|
|
@@ -389,7 +413,16 @@ async function startSession(server, data, options) {
|
|
|
389
413
|
const kind = z.enum(launchKinds).parse(data.kind);
|
|
390
414
|
const effort = z.enum(launchEfforts).default("medium").parse(data.effort);
|
|
391
415
|
const named = data.account === undefined || data.account === null ? undefined : z.string().refine(isAccountSlug, "Account must be default or a lowercase slug.").parse(data.account);
|
|
392
|
-
|
|
416
|
+
let permissionMode = z.enum(PERMISSION_MODES).optional().parse(data.permissionMode ?? undefined);
|
|
417
|
+
if (data.idempotencyKey) {
|
|
418
|
+
if (kind !== "claude" && kind !== "codex") {
|
|
419
|
+
throw new BridgeError(400, "A dispatch with an idempotency key may run only claude or codex; this harness is refused.");
|
|
420
|
+
}
|
|
421
|
+
if (permissionMode && permissionMode !== "supervised") {
|
|
422
|
+
throw new BridgeError(400, `A dispatch with an idempotency key runs supervised; --permission-mode ${permissionMode} is refused.`);
|
|
423
|
+
}
|
|
424
|
+
permissionMode = "supervised";
|
|
425
|
+
}
|
|
393
426
|
if (permissionMode && role === "conductor")
|
|
394
427
|
throw new BridgeError(400, "A conductor starts with its own permissions; permissionMode is for workers.");
|
|
395
428
|
if (permissionMode && kind === "opencode")
|
|
@@ -409,6 +442,9 @@ async function startSession(server, data, options) {
|
|
|
409
442
|
const brief = data.brief === undefined || data.brief === null ? undefined : launchBriefSchema.parse(data.brief);
|
|
410
443
|
if (brief && role === "conductor")
|
|
411
444
|
throw new BridgeError(400, "A conductor starts with its own brief.");
|
|
445
|
+
// A launch's own token, so an ambiguous outcome can later be resolved by
|
|
446
|
+
// looking for the pane that carries it.
|
|
447
|
+
const suppliedStartingToken = data.startingToken === undefined || data.startingToken === null ? undefined : z.string().regex(/^[a-f0-9]{64}$/).parse(data.startingToken);
|
|
412
448
|
// Herdr's agent name is a slug (lowercase, digits, - or _, 1 to 32 chars);
|
|
413
449
|
// the label a person typed is not, so derive one from it.
|
|
414
450
|
const baseName = herdrAgentName(data.name === undefined ? label : plainText(200).parse(data.name));
|
|
@@ -451,10 +487,14 @@ async function startSession(server, data, options) {
|
|
|
451
487
|
const args = role === "conductor" ? [...await prepareConductor(kind, effort, model), ...(kind === "phren" ? phrenArgs : [])]
|
|
452
488
|
: [...(kind === "phren" ? [...PHREN_AGENT_ARGS, ...phrenArgs] : []), ...(model && kind === "phren" ? phrenModelArgs(model) : model && modelFlag[kind] ? [modelFlag[kind], model] : []), ...(data.effort === undefined ? [] : effortArgs(kind, effort)),
|
|
453
489
|
...(permissionMode && kind === "claude" ? ["--permission-mode", CLAUDE_NAMES[permissionMode]] : []), ...(resumeClaude ? ["--resume", resumeClaude] : []), ...(permissionMode && kind === "codex" ? codexModeFlags(permissionMode) : []), ...(permissionMode && kind === "copilot" ? copilotModeFlags(permissionMode) : [])];
|
|
490
|
+
if (data.idempotencyKey)
|
|
491
|
+
assertKeyedLaunchArgs(kind, args);
|
|
454
492
|
// A Codex worker runs on a Phren-owned app-server (codex-servers.ts): the
|
|
455
493
|
// pane joins the thread the Hook started, and the brief is that thread's
|
|
456
494
|
// first turn. The typed arguments below stay the fallback.
|
|
457
|
-
|
|
495
|
+
// A keyed Codex worker uses the typed explicit command so its durable
|
|
496
|
+
// worker identity file is written before the harness execs.
|
|
497
|
+
const structured = kind === "codex" && role === "agent" && !options.canary && !data.idempotencyKey && codexAppServerEnabled();
|
|
458
498
|
// OpenCode serves its own API on a port of its own (opencode-panes.ts): the
|
|
459
499
|
// Hook sends its prompts, answers its asks and interrupts it over HTTP, and
|
|
460
500
|
// the pane stays the owner's view. A brief goes the same way once it
|
|
@@ -466,164 +506,230 @@ async function startSession(server, data, options) {
|
|
|
466
506
|
const briefLaunch = brief && briefFile && launchesWithBrief(kind) ? briefArgs(kind, briefFile) : undefined;
|
|
467
507
|
// sudo -A in the new agent asks the phone for the password (sudo.ts).
|
|
468
508
|
const dispatchId = options.dispatchId ?? brief?.id;
|
|
469
|
-
const variables = { ...askpassEnv(), ...(dispatchId ? { [DISPATCH_ID_ENV]: dispatchId } : {}),
|
|
470
|
-
|
|
509
|
+
const variables = { ...askpassEnv(), ...(dispatchId ? { [DISPATCH_ID_ENV]: dispatchId } : {}),
|
|
510
|
+
...(suppliedStartingToken ? { PHREN_STARTING_TOKEN: suppliedStartingToken } : {}), ...served?.env, ...(home ? claudeLaunchEnv(home) : {}) };
|
|
471
511
|
if (workspace && !objects(before.workspaces).some(w => w.workspace_id === workspace))
|
|
472
512
|
throw new BridgeError(409, "The workspace changed.");
|
|
473
513
|
const knownWorkspaces = new Set(objects(before.workspaces).map(w => w.workspace_id));
|
|
474
514
|
const knownTabs = new Set(objects(before.tabs).map(t => t.tab_id));
|
|
475
515
|
// Created last, once nothing else can refuse the launch, so a refusal
|
|
476
|
-
// never leaves a
|
|
477
|
-
|
|
478
|
-
//
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
if (await realpath(agents) !== agents)
|
|
494
|
-
throw new BridgeError(403, "The agents folder must be inside the Phren directory.");
|
|
495
|
-
scratch = await mkdtemp(path.join(agents, `${new Date().toISOString().slice(0, 10)}-${herdrAgentName(label)}-`));
|
|
496
|
-
}
|
|
497
|
-
const cwd = scratch ?? worktree?.cwd ?? projectDirectory;
|
|
498
|
-
// Claude's folder-trust screen defaults to "No, exit" and Codex's holds the
|
|
499
|
-
// agent too; a folder the Hook picked or just created is trusted up front.
|
|
500
|
-
if (scratch || worktree || options.trustFolder)
|
|
501
|
-
await pretrustFolder(kind, cwd, scratch ? "new agent folder" : worktree ? `new worktree for ${worktree.branch}` : "project folder", home ? { ...process.env, ...claudeLaunchEnv(home) } : process.env);
|
|
502
|
-
if (!options.into) {
|
|
503
|
-
try {
|
|
504
|
-
await terminalProvider().create(server, { workspace, label, cwd, ...(env ? { env } : {}) });
|
|
516
|
+
// never leaves a checkout or branch behind. A keyed dispatch gets a fresh
|
|
517
|
+
// clone with its own managed global config; an unkeyed launch keeps the
|
|
518
|
+
// ordinary worktree of the user's checkout.
|
|
519
|
+
const keyed = role === "agent" && !!data.idempotencyKey;
|
|
520
|
+
if (keyed && !worktreeRequest)
|
|
521
|
+
throw new BridgeError(400, "A dispatch with an idempotency key requires --worktree: keyed workers run only in a Phren-managed clone.");
|
|
522
|
+
const keyedDispatchId = keyed ? z.string().uuid().parse(data.dispatchId ?? brief?.id ?? randomUUID()) : undefined;
|
|
523
|
+
const keyedWorkerFile = keyed ? dispatchClonePaths(keyedDispatchId).workerFile : undefined;
|
|
524
|
+
let launchLock;
|
|
525
|
+
if (keyed)
|
|
526
|
+
launchLock = await acquireLaunchLock(keyedDispatchId);
|
|
527
|
+
try {
|
|
528
|
+
let worktree;
|
|
529
|
+
if (worktreeRequest) {
|
|
530
|
+
worktree = keyed
|
|
531
|
+
? await createDispatchClone(projectDirectory, { branch: worktreeRequest.branch, dispatchId: keyedDispatchId, paths: dispatchClonePaths(keyedDispatchId) })
|
|
532
|
+
: await createLaunchWorktree(projectDirectory, worktreeRequest);
|
|
505
533
|
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
534
|
+
// The same root as the conductor, chosen by this computer's PHREN_PATH.
|
|
535
|
+
// mkdtemp keeps simultaneous chats with the same title isolated.
|
|
536
|
+
let scratch;
|
|
537
|
+
if (agentFolder) {
|
|
538
|
+
// Runtime folders are excluded from project discovery and store sync.
|
|
539
|
+
// Created before resolving the store: it may not exist yet, on a
|
|
540
|
+
// computer without one or before the Hook's own startup made it.
|
|
541
|
+
await mkdir(path.join(defaultPhrenPath(), ".runtime"), { recursive: true, mode: 0o700 });
|
|
542
|
+
const store = await realpath(defaultPhrenPath());
|
|
543
|
+
const runtime = path.join(store, ".runtime");
|
|
544
|
+
if (await realpath(runtime) !== runtime)
|
|
545
|
+
throw new BridgeError(403, "The runtime folder must be inside the Phren directory.");
|
|
546
|
+
const agents = path.join(runtime, "agents");
|
|
547
|
+
await mkdir(agents, { recursive: true, mode: 0o700 });
|
|
548
|
+
// A pre-existing symlink must not redirect the new folder outside the store.
|
|
549
|
+
if (await realpath(agents) !== agents)
|
|
550
|
+
throw new BridgeError(403, "The agents folder must be inside the Phren directory.");
|
|
551
|
+
scratch = await mkdtemp(path.join(agents, `${new Date().toISOString().slice(0, 10)}-${herdrAgentName(label)}-`));
|
|
511
552
|
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
553
|
+
const cwd = scratch ?? worktree?.cwd ?? projectDirectory;
|
|
554
|
+
// A keyed dispatch uses only the stored agent token and the clone's managed
|
|
555
|
+
// config. Its environment is an explicit allowlist, never an overlay on the
|
|
556
|
+
// Hook's own environment.
|
|
557
|
+
let keyedGitboy;
|
|
558
|
+
let agentTokenFile;
|
|
559
|
+
let keyedEnv;
|
|
560
|
+
if (keyed) {
|
|
561
|
+
try {
|
|
562
|
+
keyedGitboy = await prepareKeyedGitboy(cwd, { tokenFile: worktree.agentTokenFile, gitConfigFile: worktree.gitConfigFile });
|
|
563
|
+
}
|
|
564
|
+
catch (error) {
|
|
565
|
+
await worktree?.discard();
|
|
566
|
+
throw error;
|
|
567
|
+
}
|
|
568
|
+
agentTokenFile = keyedGitboy?.tokenFile;
|
|
569
|
+
keyedEnv = keyedExplicitEnv(process.env, {
|
|
570
|
+
...(keyedGitboy?.env ?? {}),
|
|
571
|
+
...(dispatchId ? { [DISPATCH_ID_ENV]: dispatchId } : {}),
|
|
572
|
+
...(keyedWorkerFile ? { PHREN_WORKER_ID_FILE: keyedWorkerFile } : {}),
|
|
573
|
+
...(home ? claudeLaunchEnv(home) : {}),
|
|
574
|
+
});
|
|
524
575
|
}
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
576
|
+
const envVariables = keyedEnv ?? { ...variables };
|
|
577
|
+
const env = Object.keys(envVariables).length ? envVariables : undefined;
|
|
578
|
+
// Claude's folder-trust screen defaults to "No, exit" and Codex's holds the
|
|
579
|
+
// agent too; a folder the Hook picked or just created is trusted up front.
|
|
580
|
+
if (scratch || worktree || options.trustFolder)
|
|
581
|
+
await pretrustFolder(kind, cwd, scratch ? "new agent folder" : worktree ? `new worktree for ${worktree.branch}` : "project folder", home ? { ...process.env, ...claudeLaunchEnv(home) } : process.env);
|
|
582
|
+
if (!options.into) {
|
|
583
|
+
try {
|
|
584
|
+
await terminalProvider().create(server, { workspace, label, cwd, ...(env ? { env } : {}) });
|
|
585
|
+
}
|
|
586
|
+
catch (error) {
|
|
587
|
+
await removeAgentTokenFile(agentTokenFile);
|
|
588
|
+
await worktree?.discard();
|
|
589
|
+
if (scratch)
|
|
590
|
+
await rmdir(scratch).catch(() => undefined);
|
|
591
|
+
throw error;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
let created = options.into;
|
|
595
|
+
for (let attempt = 0; attempt < 25 && !created; attempt++) {
|
|
596
|
+
const s = await snapshot(server);
|
|
597
|
+
const fresh = objects(s.tabs).filter(t => !knownTabs.has(t.tab_id)
|
|
598
|
+
&& (workspace ? t.workspace_id === workspace : !knownWorkspaces.has(t.workspace_id)));
|
|
599
|
+
const tab = fresh.find(t => t.label === label)
|
|
600
|
+
?? fresh.find(t => objects(s.workspaces).some(w => w.workspace_id === t.workspace_id && w.label === label))
|
|
601
|
+
?? fresh[0];
|
|
602
|
+
const pane = tab && objects(s.panes).find(p => p.tab_id === tab.tab_id && p.workspace_id === tab.workspace_id && !p.agent);
|
|
603
|
+
if (tab && pane && id.safeParse(tab.workspace_id).success && id.safeParse(tab.tab_id).success && id.safeParse(pane.pane_id).success) {
|
|
604
|
+
created = { workspaceId: String(tab.workspace_id), tabId: String(tab.tab_id), paneId: String(pane.pane_id) };
|
|
605
|
+
}
|
|
606
|
+
else
|
|
607
|
+
await new Promise(resolve => setTimeout(resolve, 200));
|
|
608
|
+
}
|
|
609
|
+
if (!created) {
|
|
610
|
+
await removeAgentTokenFile(agentTokenFile);
|
|
611
|
+
throw new BridgeError(409, `${terminalName(server)} created "${label}" but its pane did not appear. Check ${terminalName(server)} on the computer.`);
|
|
612
|
+
}
|
|
613
|
+
if (suppliedStartingToken)
|
|
614
|
+
seedStartingToken(server, { workspace_id: created.workspaceId, tab_id: created.tabId, pane_id: created.paneId }, suppliedStartingToken);
|
|
615
|
+
const place = { workspace: created.workspaceId, tab: created.tabId, pane: created.paneId };
|
|
616
|
+
await options.onPlaced?.({ server, ...place, source: kind });
|
|
617
|
+
// The pane identity variables are ours (Herdr/tmux), not the caller's, and
|
|
618
|
+
// the worker's hooks need them under the explicit keyed environment too.
|
|
619
|
+
const paneEnv = terminalProvider().paneEnv?.(server, place) ?? {};
|
|
620
|
+
const identityEnv = Object.fromEntries(Object.entries(paneEnv)
|
|
621
|
+
.filter(([key]) => /^(?:HERDR_(?:ENV|SOCKET_PATH|WORKSPACE_ID|TAB_ID|PANE_ID)|TMUX|TMUX_PANE)$/.test(key)));
|
|
622
|
+
const launchEnv = keyedEnv ? { ...keyedEnv, ...identityEnv } : env;
|
|
623
|
+
const structuredLaunch = structured ? await codexServers.launch({ server, ...place }, { cwd, ...(model ? { model } : {}),
|
|
624
|
+
...(data.effort === undefined ? {} : { effort }),
|
|
625
|
+
env: keyedEnv ? { ...(launchEnv ?? {}) } : { ...paneEnv, ...(env ?? {}) }, envExact: !!keyedEnv,
|
|
626
|
+
...(brief ? { dispatchId: brief.id } : {}), startThread: !!brief, ...(permissionMode ? { remoteArgs: codexModeFlags(permissionMode) } : {}) }).catch(error => {
|
|
627
|
+
const note = `Codex app-server unavailable, typing instead: ${error instanceof Error ? error.message : String(error)}`;
|
|
628
|
+
if (error instanceof AppServerUnsupportedError)
|
|
629
|
+
logger.debug("launch", note);
|
|
630
|
+
else
|
|
631
|
+
logger.warn("launch", note);
|
|
632
|
+
return undefined;
|
|
633
|
+
}) : undefined;
|
|
634
|
+
const appServer = structuredLaunch?.entry;
|
|
635
|
+
const binding = { server, workspace: created.workspaceId, tab: created.tabId, pane: created.paneId, source: kind };
|
|
636
|
+
// The brief is the thread's first turn, acknowledged with its id before
|
|
637
|
+
// the pane even starts; the pane's TUI shows it running when it joins.
|
|
638
|
+
// Refused outright, nothing was sent and the caller types it; a lost
|
|
639
|
+
// reply may have started it, so it is not offered for typing again.
|
|
640
|
+
let briefTurn;
|
|
641
|
+
if (appServer?.threadId && brief) {
|
|
642
|
+
try {
|
|
643
|
+
// The thread's first turn carries the mode; the TUI joining it has no flags to set.
|
|
644
|
+
if (permissionMode)
|
|
645
|
+
codexServers.holdSettings(appServer, { ...CODEX_MODES[permissionMode] });
|
|
646
|
+
await codexServers.prompt(appServer, brief.text);
|
|
647
|
+
briefTurn = "sent";
|
|
648
|
+
await recordBriefArrival(brief.id, "UserPromptSubmit", { ...binding, session: appServer.threadId }).catch(() => undefined);
|
|
649
|
+
}
|
|
650
|
+
catch (error) {
|
|
651
|
+
briefTurn = error instanceof AppServerRpcError ? undefined : "uncertain";
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
const agentArgs = [...(structuredLaunch ? structuredLaunch.args : [...args, ...(briefLaunch ?? [])]),
|
|
655
|
+
...(kind === "codex" ? ["-c", "check_for_update_on_startup=false"] : [])];
|
|
656
|
+
if (keyedDispatchId)
|
|
657
|
+
await markSubmitted(keyedDispatchId);
|
|
550
658
|
try {
|
|
551
|
-
|
|
552
|
-
if (permissionMode)
|
|
553
|
-
codexServers.holdSettings(appServer, { ...CODEX_MODES[permissionMode] });
|
|
554
|
-
await codexServers.prompt(appServer, brief.text);
|
|
555
|
-
briefTurn = "sent";
|
|
556
|
-
await recordBriefArrival(brief.id, "UserPromptSubmit", { ...binding, session: appServer.threadId }).catch(() => undefined);
|
|
659
|
+
await (kind === "codex" ? startCodex : startWhenShellReady)(server, created.paneId, { name, kind, args: agentArgs, timeoutMs: timeout, ...(launchEnv ? { env: launchEnv } : {}), ...(keyedEnv ? { envExact: true, freshPane: !options.into } : {}) });
|
|
557
660
|
}
|
|
558
661
|
catch (error) {
|
|
559
|
-
|
|
662
|
+
if (appServer && !agentNotReady(error))
|
|
663
|
+
await codexServers.stop(appServer).catch(() => undefined);
|
|
664
|
+
if (error instanceof BridgeError && error.message.startsWith("Codex exited at start:"))
|
|
665
|
+
throw error;
|
|
666
|
+
// A first-run screen (Claude's folder trust, a login notice) holds the
|
|
667
|
+
// agent at startup. It did start: hand the pane back so the owner answers
|
|
668
|
+
// that screen from the chat instead of stranding the workspace.
|
|
669
|
+
const blocked = agentNotReady(error);
|
|
670
|
+
if (!blocked) {
|
|
671
|
+
// A keyed worker may have started before the error (a snapshot read can
|
|
672
|
+
// fail after the harness runs): its token stays for the confirmed-
|
|
673
|
+
// termination cleanup, which knows the path from the receipt.
|
|
674
|
+
if (!keyedEnv)
|
|
675
|
+
await removeAgentTokenFile(agentTokenFile);
|
|
676
|
+
const host = terminalName(server);
|
|
677
|
+
const reason = error instanceof BridgeError && error.status === 504 ? "it did not become ready in time"
|
|
678
|
+
: error instanceof BridgeError && error.message.startsWith(`${host}: `) ? error.message.slice(host.length + 2) : `${host} reported an error`;
|
|
679
|
+
if (options.into)
|
|
680
|
+
throw new BridgeError(409, `${host} couldn't start ${kind} in the "${label}" pane (${reason}). The pane is still open at its shell prompt.`);
|
|
681
|
+
throw new BridgeError(409, `${host} couldn't start ${kind} in the new "${label}" pane (${reason}). The workspace was created and is still open on the computer — open it from ${host === "Herdr" ? "Herdr workspaces" : "its tmux session"}.`);
|
|
682
|
+
}
|
|
560
683
|
}
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
const reason = error instanceof BridgeError && error.status === 504 ? "it did not become ready in time"
|
|
579
|
-
: error instanceof BridgeError && error.message.startsWith(`${host}: `) ? error.message.slice(host.length + 2) : `${host} reported an error`;
|
|
580
|
-
if (options.into)
|
|
581
|
-
throw new BridgeError(409, `${host} couldn't start ${kind} in the "${label}" pane (${reason}). The pane is still open at its shell prompt.`);
|
|
582
|
-
throw new BridgeError(409, `${host} couldn't start ${kind} in the new "${label}" pane (${reason}). The workspace was created and is still open on the computer — open it from ${host === "Herdr" ? "Herdr workspaces" : "its tmux session"}.`);
|
|
684
|
+
// Without a brief the pane's TUI started the thread; the Hook heard it.
|
|
685
|
+
if (appServer && !appServer.threadId)
|
|
686
|
+
await codexServers.awaitThread(appServer);
|
|
687
|
+
let servedBrief;
|
|
688
|
+
if (served) {
|
|
689
|
+
const variant = role === "conductor" || data.effort !== undefined ? effort : undefined;
|
|
690
|
+
const entry = await registerServedPane(server, created.paneId, cwd, served, { defaults: { ...(role === "conductor" ? { agent: "conductor" } : {}), ...(model ? { model } : {}), ...(variant ? { variant } : {}) } }).catch(() => undefined);
|
|
691
|
+
const sent = entry && brief ? await sendServedBrief(entry, brief.text) : undefined;
|
|
692
|
+
if (brief && sent?.sent) {
|
|
693
|
+
// Sent is enough to never type it again; the user turn appearing in
|
|
694
|
+
// the session is the acceptance the dispatch receipt waits for.
|
|
695
|
+
servedBrief = { session: sent.session, delivered: sent.delivered };
|
|
696
|
+
const at = { ...binding, session: sent.session };
|
|
697
|
+
await recordBriefArrival(brief.id, "SessionStart", at).catch(() => { });
|
|
698
|
+
if (sent.delivered)
|
|
699
|
+
await recordBriefArrival(brief.id, "UserPromptSubmit", at).catch(() => { });
|
|
700
|
+
}
|
|
583
701
|
}
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
702
|
+
const after = await snapshot(server);
|
|
703
|
+
const pane = findPane(after, { workspace: created.workspaceId, tab: created.tabId, pane: created.paneId });
|
|
704
|
+
const agentStatus = typeof pane?.agent_status === "string" ? pane.agent_status : undefined;
|
|
705
|
+
if (kind === "claude" && account)
|
|
706
|
+
recordPaneAccount(paneAccountKey(server, created.paneId), account, typeof pane?.terminal_id === "string" ? pane.terminal_id : undefined);
|
|
707
|
+
const sessionId = appServer?.threadId ?? (pane && pane.agent === kind ? await paneIdentity(server, pane) : undefined) ?? servedBrief?.session;
|
|
708
|
+
// The Hook, not the agent name, holds the role from here on.
|
|
709
|
+
if (role === "conductor" && !options.canary)
|
|
710
|
+
await recordConductor(server, pane ?? { pane_id: created.paneId, workspace_id: created.workspaceId, tab_id: created.tabId, agent: kind }, "launch", sessionId);
|
|
711
|
+
const chat = !sessionId && pane && pane.agent === kind ? await paneChatState(server, pane).catch(() => ({})) : {};
|
|
712
|
+
const target = sessionId ? { ...binding, session: sessionId }
|
|
713
|
+
: chat.starting === true ? { ...binding, starting: true, startingToken: chat.startingToken } : undefined;
|
|
714
|
+
// Register a dispatched or scheduled worker's job, so the resources report
|
|
715
|
+
// can name its agent and a sweep can end a leftover process group after its
|
|
716
|
+
// pane is gone. The process group is resolved on the next read.
|
|
717
|
+
if (brief && role === "agent") {
|
|
718
|
+
await new JobRegistry().register({ pane: { server, pane: created.paneId, ...(created.workspaceId ? { workspace: created.workspaceId } : {}), agent: kind, label },
|
|
719
|
+
...(sessionId ? { session: sessionId } : {}), agent: kind, label, command: kind }).catch(() => undefined);
|
|
601
720
|
}
|
|
721
|
+
return { ok: true, ...created, cwd, agent: kind, agentStatus, role, sessionId, target, ...(account ? { account } : {}), ...(choice ? { accountChoice: choice.reason } : {}), ...(permissionMode ? { permissionMode } : {}), ...(unchecked.length ? { unchecked } : {}),
|
|
722
|
+
// The caller types the brief itself unless it went with the launch.
|
|
723
|
+
...(brief ? { briefLaunched: appServer ? briefTurn !== undefined : !!briefLaunch || !!servedBrief,
|
|
724
|
+
briefState: appServer ? briefTurn ?? "unconfirmed" : served ? (servedBrief?.delivered ? "sent" : "uncertain") : briefLaunch ? "sent" : "unconfirmed" } : {}),
|
|
725
|
+
...(worktree ? { worktree: { path: worktree.path, branch: worktree.branch } } : {}), ...(agentTokenFile ? { agentTokenFile } : {}),
|
|
726
|
+
...(worktree?.cloneDir ? { cloneDir: worktree.cloneDir, gitConfigFile: worktree.gitConfigFile } : {}),
|
|
727
|
+
...(worktree?.workerFile ? { workerFile: worktree.workerFile } : {}) };
|
|
602
728
|
}
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
if (kind === "claude" && account)
|
|
607
|
-
recordPaneAccount(paneAccountKey(server, created.paneId), account, typeof pane?.terminal_id === "string" ? pane.terminal_id : undefined);
|
|
608
|
-
const sessionId = appServer?.threadId ?? (pane && pane.agent === kind ? await paneIdentity(server, pane) : undefined) ?? servedBrief?.session;
|
|
609
|
-
// The Hook, not the agent name, holds the role from here on.
|
|
610
|
-
if (role === "conductor" && !options.canary)
|
|
611
|
-
await recordConductor(server, pane ?? { pane_id: created.paneId, workspace_id: created.workspaceId, tab_id: created.tabId, agent: kind }, "launch", sessionId);
|
|
612
|
-
const chat = !sessionId && pane && pane.agent === kind ? await paneChatState(server, pane).catch(() => ({})) : {};
|
|
613
|
-
const target = sessionId ? { ...binding, session: sessionId }
|
|
614
|
-
: chat.starting === true ? { ...binding, starting: true, startingToken: chat.startingToken } : undefined;
|
|
615
|
-
// Register a dispatched or scheduled worker's job, so the resources report
|
|
616
|
-
// can name its agent and a sweep can end a leftover process group after its
|
|
617
|
-
// pane is gone. The process group is resolved on the next read.
|
|
618
|
-
if (brief && role === "agent") {
|
|
619
|
-
await new JobRegistry().register({ pane: { server, pane: created.paneId, ...(created.workspaceId ? { workspace: created.workspaceId } : {}), agent: kind, label },
|
|
620
|
-
...(sessionId ? { session: sessionId } : {}), agent: kind, label, command: kind }).catch(() => undefined);
|
|
729
|
+
finally {
|
|
730
|
+
if (launchLock)
|
|
731
|
+
await releaseLaunchLock(launchLock);
|
|
621
732
|
}
|
|
622
|
-
return { ok: true, ...created, cwd, agent: kind, agentStatus, role, sessionId, target, ...(account ? { account } : {}), ...(choice ? { accountChoice: choice.reason } : {}), ...(permissionMode ? { permissionMode } : {}), ...(unchecked.length ? { unchecked } : {}),
|
|
623
|
-
// The caller types the brief itself unless it went with the launch.
|
|
624
|
-
...(brief ? { briefLaunched: appServer ? briefTurn !== undefined : !!briefLaunch || !!servedBrief,
|
|
625
|
-
briefState: appServer ? briefTurn ?? "unconfirmed" : served ? (servedBrief?.delivered ? "sent" : "uncertain") : briefLaunch ? "sent" : "unconfirmed" } : {}),
|
|
626
|
-
...(worktree ? { worktree: { path: worktree.path, branch: worktree.branch } } : {}) };
|
|
627
733
|
}
|
|
628
734
|
export async function workspaceAction(server, operation, data) {
|
|
629
735
|
if (!["focus", "rename", "create", "close", "scroll"].includes(operation))
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { BrowserRegistry } from "./browser-registry.js";
|
|
1
2
|
import type { TurnQueue } from "./turn-queue.js";
|
|
2
3
|
import type { ConductorSuccessionService } from "./conductor-succession.js";
|
|
3
4
|
import type { StorePulls } from "./store-pull.js";
|
|
@@ -50,6 +51,7 @@ export interface HookInfo {
|
|
|
50
51
|
readonly gatewayMs: number | undefined;
|
|
51
52
|
}
|
|
52
53
|
export interface RouteContext {
|
|
54
|
+
browsers?: BrowserRegistry;
|
|
53
55
|
turnQueue?: TurnQueue;
|
|
54
56
|
version: string;
|
|
55
57
|
modules: ModuleSnapshot;
|
|
@@ -100,6 +102,7 @@ export declare const STATUS_ENRICH_BUDGET_MS = 2000;
|
|
|
100
102
|
* finish inside `budgetMs` is left out, which preserves its last known status. */
|
|
101
103
|
export declare function effectiveStatusRows(server: string, s: Json, budgetMs?: number, hooks?: AgentHooks): Promise<StatusRow[]>;
|
|
102
104
|
export declare const capabilities: {
|
|
105
|
+
browsers: boolean;
|
|
103
106
|
turnCheckpoints: boolean;
|
|
104
107
|
transcript: boolean;
|
|
105
108
|
progress: boolean;
|