import { conductorOperation, OPERATIONAL_PERSON_GUIDANCE } from "../hub/operational-authority.ts"; import { nativeCliMarkerPeer } from "../adapters/definitions/native-identity-defaults.ts"; import { recoveryPresentationFlag } from "./recovery-presentation-options.ts"; import { definitionOf, kindOf, type KindMap, type ProjectAdapterCatalog } from "../adapters/registry.ts"; /** Recovery reads this declaration before delegating to another package's CLI. */ export const LIVE_TEST_SUPPORTED = true; /** Presentation prefixes cannot hide a person's recovery action from authorization. */ function recoveryAction(args: readonly string[]): string | undefined { return args.find(arg => !recoveryPresentationFlag(arg)); } export const LIVE_TEST_PERSON_REFUSAL = "live-test cannot write person-attributed content; use peer tools or a person's terminal"; /** Person verdicts and user-authored workflow content are never delegated. */ export function liveTestRefusal(command: string, args: readonly string[], context: { repairedFresh?: boolean } = {}): boolean { if (command === "roadmap" && ![undefined, "list", "show", "--json"].includes(args[0])) return true; if (command === "roadmap" && args[0] === "--json" && ![undefined, "list", "show"].includes(args[1])) return true; if (command === "sdlc" && args[0] === "configure") return true; // #1540: retargeting a stopped project's runtime or every stopped one is the person's choice of which code runs next; an agent shell must not reach it through `--live-test`. if (command === "upgrade" && args.includes("--all-stopped")) return true; if (command === "lineage" && args[0] === "link") return true; if (command === "guidance" && ["create", "withdraw"].includes(args[0] ?? "")) return true; if (command === "sdlc" && args[0] === "survey" && args[1] !== "show") return true; // #1403 A4: starting a security audit files a task; only `show` stays a live-test read. if (command === "sdlc" && args[0] === "audit" && args[1] !== "show") return true; if (command === "model") return true; // #1317: even listing runtime overrides is person-only, never live-test delegated. if (command === "sdlc" && (args[0] === "authorize-release" || args[0] === "plan-review" || args[0] === "release-assign" || args[0] === "merge" || args[0] === "train" && args[1] !== "report")) return true; if (args.some(arg => arg === "--as-user" || arg.startsWith("--as-user="))) return true; if (command === "pet" && (args.includes("--window") || args.includes("--palette"))) return false; const sub = command === "recovery" ? recoveryAction(args) : args.find(arg => arg !== "--json"); return ["task", "role", "goal", "deliverables", "source", "broker", "schedule", "intake"].includes(command) || (command === "room" && !["list", "status"].includes(sub ?? "")) || (command === "backlog" && sub !== "list") || (command === "release" && !["list", "show"].includes(sub ?? "")) || (command === "memory" && sub !== "graph") // #601: a campaign's draft, revise, approve and close are the person's verdicts; only `show` is a read. Without this an agent shell could run them as the person through `--live-test`. || (command === "study" && sub !== "show" && sub !== "experiments") || (command === "recovery" && (sub === "reattach" || sub === "dispose" && context.repairedFresh === true)) || (command === "board" && args[0] === "import") || (command === "sdlc" && (args[0] === "record" || args[0] === "accept" || args[0] === "accept-departure" || args[0] === "adopt-merge")) || (command === "bench" && args.find(arg => arg !== "--json") === "run") || command === "pet" // #691: trusting an untested agent CLI version is the person's consent, never a delegated live-test operation. || command === "autonomy" || command === "title" // #1064 T3: a stamp is a person's record that they reviewed a document; no delegation, live-test or otherwise, may write one. || (command === "specs" && args[0] === "stamp") // #1057: removing a worktree and its branch is the person's own act, in their own terminal; no delegation, live-test or otherwise, may confirm a tidy. || (command === "worktrees" && args.some(arg => arg === "--confirm" || arg.startsWith("--confirm="))) || (command === "agents" && !args.every(arg => arg === "--json")) // #1053: an answer is the person's choice, recorded as the person's; an agent that could run it through `--live-test` would answer its own hub_ask as the person. Any operand answers (the set, a pick, --other); a bare listing stays allowed. || (command === "answer" && args.some(arg => arg !== "--json" && arg !== "--full" && !arg.startsWith("--color="))) // #1054: a receipt is the person's record that they read a result and clears their unread need; no delegation, live-test or otherwise, may write one. A plain listing stays allowed. || (command === "results" && args.some(arg => arg === "--read" || arg.startsWith("--read=") || arg === "--read-all" || arg === "--unread-last")) // A takeover is a person-authored lifecycle decision; never let an agent delegate it through --live-test. || command === "peer"; } export type CliIdentity = | { role: "console" } | { role: "tools"; peer: string } | { role: "invalid"; reason: string }; /** Honest defaults for native shells, not authentication against an agent that can edit its environment. */ export function detectCliIdentity(env: Readonly>, instances?: KindMap): CliIdentity { const hub = env.AGENTHUB_PEER_ID; const claude = env.CLAUDECODE; const session = env.CLAUDE_CODE_SESSION_ID; const codex = env.CODEX_THREAD_ID; const invalid = (reason: string): CliIdentity => ({ role: "invalid", reason }); if (hub !== undefined && (!/^[a-z][a-z0-9-]{0,31}$/.test(hub) || /\s/.test(hub) || ["user", "hub", "digest"].includes(hub))) return invalid("invalid AGENTHUB_PEER_ID marker"); if (claude !== undefined && claude !== "1") return invalid("invalid CLAUDECODE marker"); if (session !== undefined && (!session || /\s/.test(session))) return invalid("invalid CLAUDE_CODE_SESSION_ID marker"); if (codex !== undefined && (!codex || /\s/.test(codex))) return invalid("invalid CODEX_THREAD_ID marker"); if (session !== undefined && claude === undefined) return invalid("CLAUDE_CODE_SESSION_ID without CLAUDECODE marker"); if (claude !== undefined && codex !== undefined) return invalid("conflicting native agent markers"); const native = nativeCliMarkerPeer(claude !== undefined, codex !== undefined); if (hub !== undefined && native !== undefined && (kindOf(hub, instances) ?? hub) !== native) return invalid("AGENTHUB_PEER_ID conflicts with native agent marker"); const peer = hub ?? native; return peer === undefined ? { role: "console" } : { role: "tools", peer }; } export type PeerCommandAccess = "allowed" | "conductor" | "console"; /** #1522: the `ahub memory` subcommands an agent shell runs (reads, and the changes amem records under the agent's own name). Anything else in the family stays the person's. */ export const AGENT_MEMORY_SUBS: readonly string[] = ["list", "search", "show", "suggestions", "machine", "sync", "import", "dedupe", "promote", "demote", "forget", "supersede", "rebuild", "decay", "declined"]; /** The ones that change memory and record an actor: `memoryCommand` runs these under the agent's attribution. Reads and the two review previews run unchanged. */ export const AGENT_MEMORY_RECORDING_SUBS: readonly string[] = ["sync", "import", "dedupe", "promote", "demote", "forget", "supersede", "rebuild"]; /** Flags the read-only forms of #812 accept: display options that change no behaviour. */ const peerReadFlags = (args: readonly string[]): boolean => args.every((arg) => ["--json", "--full", "--verbose", "--color=always", "--color=never", "--color=auto"].includes(arg) || arg.startsWith("--color=")); /** * The read-only command forms #812 makes Allowed: command -> subcommand -> what the rest of argv may hold. * positional: bare words naming what to read (an operation id, a task, a release, a repo); * valueFlags: options consuming one value an agent may name (--peer, --to, --since). * A subcommand of "" is the bare form: display flags only. dryRun is checked separately for upgrade and restart. */ interface PeerReadForm { positional?: number; minimum?: number; valueFlags?: readonly string[]; displayFlags?: readonly string[]; } const publicReadDisplayFlags = ["--json", "--full", "--color=auto", "--color=always", "--color=never"] as const; const sdlcReadDisplayFlags = ["--json", "--color=auto", "--color=always", "--color=never"] as const; const peerReadForms: Readonly>>> = { // #1057: listing the shelf and previewing a tidy are reads an agent may run; `tidy --confirm` and any other form fall to `console` below, and live-test refuses them. worktrees: { "": { displayFlags: ["--json", "--full", "--color=auto", "--color=always", "--color=never"] }, tidy: { displayFlags: ["--json", "--color=auto", "--color=always", "--color=never"] } }, specs: { list: { displayFlags: ["--json"] }, check: { valueFlags: ["--base"], displayFlags: ["--json", "--require-fresh"] }, covers: { positional: 2000, minimum: 1, displayFlags: [] } }, room: { list: { displayFlags: ["--json"] }, status: { positional: 1, displayFlags: ["--json"] } }, queue: { list: { valueFlags: ["--peer"], displayFlags: [...publicReadDisplayFlags, "--all"] } }, roster: { "": { displayFlags: publicReadDisplayFlags } }, // #1411 T2: who holds which flow role, read from the running hub; nothing else takes this command. roles: { "": { displayFlags: sdlcReadDisplayFlags } }, settings: { "": { displayFlags: ["--json"] }, list: { displayFlags: ["--json"] }, get: { positional: 1, minimum: 1, displayFlags: ["--json"] } }, // #814 renders these reads for people and keeps JSON as an explicit data format. sdlc: { status: { positional: 1, displayFlags: sdlcReadDisplayFlags }, explain: { positional: 1, displayFlags: sdlcReadDisplayFlags }, queue: { positional: 1, minimum: 1, displayFlags: sdlcReadDisplayFlags }, release: { positional: 33, minimum: 1, displayFlags: sdlcReadDisplayFlags } }, release: { show: { positional: 1, minimum: 1, displayFlags: [] } }, // #601: reading a campaign is harmless for any agent (the hub stubs a private one); draft, revise, approve and close fall to `console`. study: { evidence: { positional: 1, minimum: 1, valueFlags: ["--revision"], displayFlags: sdlcReadDisplayFlags }, show: { positional: 1, valueFlags: ["--after"], displayFlags: sdlcReadDisplayFlags }, experiments: { valueFlags: ["--campaign", "--attempt", "--after", "--events-after", "--plan", "--baseline", "--variant"], displayFlags: sdlcReadDisplayFlags } }, projects: { "": {} }, models: { status: {} }, recovery: { status: { positional: 1 } }, }; /** Whether argv matches one Allowed read-form of this command: display flags, the subcommand shape's values only. */ const readFormArgs = (args: readonly string[], { valueFlags = [], positional = 0, minimum = 0, displayFlags }: PeerReadForm = {}): boolean => { let positionals = 0; let expectValue: string | undefined; for (const arg of args) { if (expectValue !== undefined) { if (arg.startsWith("-")) return false; expectValue = undefined; } else if (valueFlags.includes(arg)) { expectValue = arg; } else if (arg.startsWith("--")) { if (displayFlags === undefined ? !peerReadFlags([arg]) : !displayFlags.includes(arg)) return false; } else if (positionals >= positional) { return false; } else { positionals++; } } return expectValue === undefined && positionals >= minimum; }; const isPeerReadForm = (command: string, args: readonly string[]): boolean => { const subs = peerReadForms[command]; if (subs === undefined) return false; // The bare form ("") carries no subcommand: args empty or all display flags; otherwise args[0] is the subcommand. const first = args[0]; const bare = first === undefined || first.startsWith("--"); const sub = bare ? "" : first; if (!Object.hasOwn(subs, sub)) return false; return readFormArgs(bare ? args : args.slice(1), subs[sub]!); }; /** * A closed list: unknown commands cannot acquire console authority through an agent shell. * #812: the read-only forms matched by the branches just above the catch-alls are Allowed for any agent. * The branches above them are the older authority rules and stay unchanged. */ export function classifyPeerCommand(command: string, args: readonly string[], catalog?: ProjectAdapterCatalog): PeerCommandAccess { if (command === "model") return "console"; if (command === "recovery" && recoveryAction(args) === "reattach") return "console"; const native = catalog?.definitionOf(command) ?? definitionOf(command); if (native?.cli.personOnlyFlags?.some(flag => args.includes(flag))) return "console"; if (command === "permission" || command === "watch") return "console"; if (args.some((arg) => arg === "--as-user" || arg.startsWith("--as-user="))) return "console"; if (command === "roadmap") { const sub = args[0] === "--json" ? args[1] : args[0]; if (sub === "state" && ["done", "dropped"].includes(args[2] ?? "")) return "console"; // R3 (#1481): importing GitHub milestones is the person's alone, a dry run included; triage, accept and reject are the planner's own tools. if (sub === "import-github") return "console"; if ((sub === "sync" || sub === "export") && args.includes("--apply")) return "conductor"; return "allowed"; } if (command === "answer" && args.includes("--instructed")) return "conductor"; if (command === "pet" && (args.includes("--window") || args.includes("--palette"))) return "allowed"; if (conductorOperation(command,args)) return "conductor"; if (command === "stop") return "console"; if (command === "sdlc" && ["release-guard", "release-guard-hook"].includes(args[0] ?? "")) return "allowed"; if (command === "instruction-hook" || command === "instructed" && args[0] === "run") return "allowed"; // #786, #1063, #1328: guarded merge, merge adoption and explicitly instructed release consent admit the person or unique configured conductor; the daemon checks the role. if (command === "sdlc" && ["merge", "authorize-release", "adopt-merge", "configure"].includes(args[0] ?? "")) return "conductor"; if (command === "role") return ["list", "show"].includes(args[0] ?? "") ? "allowed" : "console"; // The original tools identity reaches Tasks for read visibility or task-scoped link admission. if (command === "lineage") return ["show", "link"].includes(args[0] ?? "") ? "allowed" : "console"; if (command === "guidance") return ["create", "list", "show", "withdraw"].includes(args[0] ?? "") ? "allowed" : "console"; if (command === "task") { // #933: cancel, label and role act on the caller's own task only; the daemon refuses another peer's task by role (Tasks.cancel, task_catalog_role, task_label_check). if (["propose", "show", "cancel", "label", "role"].includes(args[0] ?? "")) return "allowed"; if (["assign", "escalate"].includes(args[0] ?? "")) return "conductor"; return "console"; } // #933: an agent's deliverable list holds only the rows hub_task_show would show it, through the same public screening (#317). if (command === "deliverables") return "allowed"; // #933 lists `route` beyond `explain` as conductor, but the command has no other form: anything else is a usage error and stays closed. if (command === "route") return args[0] === "explain" ? "allowed" : "console"; // #691: reading agent CLI versions is harmless; trusting an untested version on this machine is a person's. if (command === "agents") return args.every(arg => arg === "--json") ? "allowed" : "console"; // #247: reading the research measures and records is harmless (ids and counts only); #933 made backfill a conductor operation (above). if (command === "research") return args[0] === "backfill" ? "console" : "allowed"; // #1522 (hub task 969): an agent shell runs the memory family as itself. The CLI wraps the writers in amem's agent attribution (`memoryCommand`), so the recorded and displayed actor is the agent, never the person. // Left to the person: `graph` (its labels are unscreened note text), `confirm`/`reject` (judgments of suggested links), `profile-check` and every command outside `memory`; hub verdicts are not memory commands. if (command === "memory") { const sub = args.find((arg) => arg !== "--json"); return sub === undefined || sub.startsWith("--") || AGENT_MEMORY_SUBS.includes(sub) ? "allowed" : "console"; } if (command === "goal") { const sub = args.find((arg) => arg !== "--json"); return sub === undefined || sub.startsWith("--") || ["list", "show"].includes(sub) ? "allowed" : "console"; } // #251: a run resets the bench project's tree and proposes work (#933: a conductor operation, above); reading results is harmless. // The subcommand is the first argument that is not --json, which the command accepts anywhere. if (command === "bench") return args.find((arg) => arg !== "--json") === "run" ? "console" : "allowed"; if (command === "budget") return !args.length || args.every((arg) => arg === "--json" || arg === "--full" || arg.startsWith("--color=")) ? "allowed" : "console"; // #812 Allowed: the read-only forms below print what the person sees and change nothing. Each command's // other forms fall to the existing rules underneath, so every older branch above and below keeps its class. if (command === "doctor") { if (peerReadFlags(args)) return "allowed"; // no args, or only --json, --full, --color=... if (args[0] === "--stale-anchors" && peerReadFlags(args.slice(1))) return "allowed"; // a bounded read-only anchor report // --orphans (it can kill) and anything else stay as today. } if ((command === "upgrade" || command === "restart") && !args.includes("--yes") && args.includes("--dry-run") && readFormArgs(args.filter((arg) => arg !== "--dry-run"), { valueFlags: ["--to"] })) return "allowed"; // a preview that applies nothing; --yes stays console if (isPeerReadForm(command, args)) return "allowed"; // exact read forms; every mutating neighbour remains below its existing authority gate if (native || ["pause", "resume"].includes(command)) return "conductor"; if (command === "board" && args[0] === "import") return "console"; if (["help", "version", "--version", "completion", "say", "status", "pet", "board", "report", "usage", "turns", "review", "remember", "facts", "check-path"].includes(command)) return "allowed"; return "console"; } /** No argument text is included: notices and refusal logs must not copy task text or credentials. */ export function cliCommandLabel(command: string, args: readonly string[], catalog?: ProjectAdapterCatalog): string { if (command === "roadmap") { const sub = args[0] === "--json" ? args[1] : args[0]; return ["list", "show", "new", "edit", "link", "unlink", "order", "state", "unplaced", "triage", "decompose", "accept", "reject", "import-github", "check", "sync", "export"].includes(sub ?? "") ? `roadmap ${sub}` : "roadmap"; } if (command === "sdlc" && args[0] === "configure") return "sdlc configure"; if (command === "sdlc" && args[0] === "survey") return ["start", "show"].includes(args[1] ?? "") ? `sdlc survey ${args[1]}` : "sdlc survey"; if (command === "sdlc" && args[0] === "audit") return ["start", "show"].includes(args[1] ?? "") ? `sdlc audit ${args[1]}` : "sdlc audit"; if (command === "lineage") return ["show", "link"].includes(args[0] ?? "") ? `lineage ${args[0]}` : "lineage"; if (command === "guidance") return ["create", "list", "show", "withdraw"].includes(args[0] ?? "") ? `guidance ${args[0]}` : "guidance"; if (command === "model") return ["list", "set", "clear"].includes(args[0] ?? "") ? `model ${args[0]}` : "model"; if (command === "peer") return args[0] === "takeover" ? "peer takeover" : "peer"; if (command === "backlog") return ["list", "add", "pull", "pull-ready", "split", "return", "plan", "pin", "undo"].includes(args[0] ?? "") ? `backlog ${args[0]}` : "backlog"; if (command === "board" && args[0] === "import") return "board import"; // #508: the registry's two forms are named in the audit and in a refusal, never their operands (a path or a URL can carry a secret). if (command === "smoke") return args[0] === "run" ? "smoke run" : "smoke"; if (command === "broker") return args[0] === "serve" ? "broker serve" : args[0] === "room" && args[1] === "drop" ? "broker room drop" : "broker"; if (command === "room" && args[0] === "inbox") return ["show", "done", "discard"].includes(args[1] ?? "") && args[2] && args[2] !== "--json" ? `room inbox ${args[1]}` : "room inbox list"; if (command === "room") return ["create", "invite", "join", "leave", "revoke", "expose", "resolve", "list", "status"].includes(args[0] ?? "") ? `room ${args[0]}` : "room"; // #335: a schedule id is config text, so the audit names the subcommand only. if (command === "schedule") return ["list", "run", "pause", "resume"].includes(args.find(arg => arg !== "--json") ?? "") ? `schedule ${args.find(arg => arg !== "--json")}` : "schedule"; // #334: intake names its two subcommands in the audit and a refusal, never the request text (it can carry a secret). if (command === "intake") return ["approve", "discard"].includes(args.find(arg => arg !== "--json") ?? "") ? `intake ${args.find(arg => arg !== "--json")}` : "intake"; // #514b: the audit and a refusal name the subcommand only, never the card id or any text. if (command === "capabilities") return "capabilities"; // #445 C1: no argument is ever echoed into the audit if (command === "learn") return ["list", "accept", "reject"].includes(args.find(arg => arg !== "--json") ?? "") ? `learn ${args.find(arg => arg !== "--json")}` : "learn"; if (command === "source") return ["add", "list"].includes(args.find(arg => arg !== "--json") ?? "") ? `source ${args.find(arg => arg !== "--json")}` : "source"; // #601: a campaign id and a contract path are text the person chose, so the audit names the subcommand only. if (command === "study") return ["draft", "revise", "approve", "close", "show", "experiments", "evidence", "check"].includes(args[0] ?? "") ? `study ${args[0]}` : "study"; if (command === "release") return ["new", "list", "show", "plan", "activate", "freeze", "ship", "drop"].includes(args[0] ?? "") ? `release ${args[0]}` : "release"; // #1084: `autonomy`, `deliverables` and `usage` were missing from the table below, so a refusal and its audit line named them "unknown". Operands (a flag value, a task id) never reach the label. if (command === "autonomy") { const sub = args.find(arg => !arg.startsWith("--")); return sub !== undefined && ["on", "off", "status"].includes(sub) ? `autonomy ${sub}` : "autonomy"; } if (command === "usage") return args[0] === "limits" || args[0] === "total" ? `usage ${args[0]}` : "usage"; if (command === "deliverables") return "deliverables"; if (command === "title") return "title"; // #1054: a result id is the hub's, but the audit names the action only, never the id. if (command === "results") return ["--read-all", "--unread-last"].find(flag => args.includes(flag))?.slice(2) ? `results ${["--read-all", "--unread-last"].find(flag => args.includes(flag))!.slice(2)}` : args.includes("--read") || args.some(arg => arg.startsWith("--read=")) ? "results read" : "results"; if (command === "specs") return ["list", "check", "covers", "stamp"].includes(args[0] ?? "") ? `specs ${args[0]}` : "specs"; // #1057: the audit names the subcommand and whether it confirms, never a path, a branch or a fingerprint. if (command === "worktrees") return args[0] === "tidy" ? (args.some(arg => arg === "--confirm" || arg.startsWith("--confirm=")) ? "worktrees tidy confirm" : "worktrees tidy") : "worktrees"; if (!(catalog?.definitionOf(command) ?? definitionOf(command)) && !["upgrade", "restart", "recovery", "recovery-run", "help", "--help", "--version", "version", "completion", "projects", "ui", "manager", "setup", "init", "daemon", "up", "console", "stop", "roles", "models", "say", "queue", "tail", "watch", "budget", "board", "task", "review", "remember", "ask", "route", "pause", "resume", "permit", "answer", "permission", "results", "settings", "roster", "status", "pet", "logs", "export", "report", "research", "memory", "goal", "bench", "sdlc", "role", "facts", "instruction-hook", "instructed", "check-path", "turns", "undo", "kill", "down", "reset", "doctor", "agents", "source", "broker", "schedule", "intake", "learn", "study"].includes(command)) return "unknown"; if (["task", "queue", "budget", "route", "recovery", "models", "projects", "research", "memory", "goal", "bench", "agents", "roster", "settings"].includes(command)) { const sub = command === "bench" || command === "memory" || command === "goal" ? args.find((arg) => arg !== "--json") : args[0]; if (sub && ["propose", "show", "assign", "unassign", "escalate", "cancel", "claim", "label", "list", "add", "resolve", "resume", "set", "execution", "explain", "status", "abort", "dispose", "reattach", "setup", "start", "stop", "remove", "discard", "export", "backfill", "addons", "run", "report", "compare", "search", "graph", "promote", "demote", "forget", "supersede", "dedupe", "decay", "rebuild", "sync", "machine", "import", "suggestions", "confirm", "reject", "new", "link", "state", "done", "take", "trust"].includes(sub)) return `${command} ${sub}`; } return (/^[a-z][a-z0-9-]*$/.test(command) && !/\s/.test(command)) || ["--help", "--version"].includes(command) ? command : "unknown"; } /** * #711: a hub refusal that only the person or the task's own reviewer could have avoided ("only its reviewer (user) or the console user can do that", * "only the person may ..."). The wording is the hub's, so this reads it and never invents it: anything else gets no extra line. */ export const personOnlyRefusal = (error: string): boolean => /\bonly\b[^.]*\b(?:console user|the person)\b/i.test(error); /** * #711: the extra stderr line for an agent shell that was refused something only the person may do. The refusal itself and the authority are unchanged: this only * says why the shell was treated as an agent and where to act as the person. `undefined` for the console identity and for any refusal that is not person-only. */ export function agentShellCause(identity: CliIdentity, error: string, env: Readonly> = process.env): string | undefined { if (identity.role !== "tools" || !personOnlyRefusal(error)) return undefined; const markers = ["AGENTHUB_PEER_ID", "CLAUDECODE", "CLAUDE_CODE_SESSION_ID", "CODEX_THREAD_ID"].filter(name => env[name] !== undefined); const set = markers.length ? `${markers.join(" and ")} ${markers.length === 1 ? "is" : "are"} set` : "an agent marker is set"; const task = /\btask #(\d+)\b/.exec(error)?.[1]; return `This shell runs as agent "${identity.peer}" (${set}), not as you. Run the command in your own terminal, or use the ahub console: ${task ? `Tasks, select #${task}, r` : "Tasks, select the task, r"}.`; } export function peerCommandRefusal(peer: string, command: string, env: Readonly> = process.env): string { const markers = ["CLAUDECODE", "CLAUDE_CODE_SESSION_ID", "AGENTHUB_PEER_ID", "CODEX_THREAD_ID"].filter(name => env[name] !== undefined); return `${peer} cannot run ahub ${command}; ${OPERATIONAL_PERSON_GUIDANCE}. Refused because inherited agent markers${markers.length ? ` (${markers.join(", ")})` : ""} identify this shell as an agent. A ! command in an agent session runs as that agent; use a separate terminal outside the agent session`; }