From bf469e231f315dc87e33b85e857be2678fba64c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=A1a=20M=D0=B0kar=C3=B6v?= Date: Fri, 4 Dec 2026 15:39:12 -0800 Subject: [PATCH] control: `deliver own`, a machine takes every message as a prompt of its own MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A message handed to a live Claude Code session by its Stop hook, a plain waiter or its inbox is not read as the person's prompt, and Claude Code tells the model not to take it as their approval: a session asked that way to merge pull requests declined. A machine set to `deliver own` passes those rungs over for a Claude Code session it can copy, so the message is the prompt of a turn the daemon runs (resume, or a copy of a held session). The default, `any`, is the ladder as it was. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Miša Mаkaröv --- .changeset/deliver-own.md | 5 ++++ packages/cli/src/control/cli.ts | 23 +++++++++++++++++++ packages/cli/src/control/daemon.ts | 16 +++++++++++-- packages/cli/src/control/route.ts | 12 ++++++++++ packages/cli/src/control/store.ts | 9 ++++++++ packages/cli/src/usage.ts | 1 + packages/cli/test/control-daemon.test.ts | 29 ++++++++++++++++++++++++ spec/CONTROL.md | 17 ++++++++++++++ 8 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 .changeset/deliver-own.md diff --git a/.changeset/deliver-own.md b/.changeset/deliver-own.md new file mode 100644 index 00000000..d389f51c --- /dev/null +++ b/.changeset/deliver-own.md @@ -0,0 +1,5 @@ +--- +"sessionpipe": minor +--- + +`sessionpipe control deliver own`: a machine can take every message as a prompt of its own (`spec/CONTROL.md` §6). A message for a Claude Code session that is open or mid-turn then runs in a copy instead of being handed to the session as a Stop hook's block, a waiter's output or an inbox request, which Claude Code tells its model not to take as the person's approval: a session asked that way to merge pull requests declined. `any` (the default) is the ladder as before. diff --git a/packages/cli/src/control/cli.ts b/packages/cli/src/control/cli.ts index ab83117b..96ad93fa 100644 --- a/packages/cli/src/control/cli.ts +++ b/packages/cli/src/control/cli.ts @@ -51,6 +51,7 @@ import { type ControlMode, controlFile, controlState, + deliverOf, parseMode, readControl, writeControl, @@ -60,6 +61,7 @@ import { waitForMessage, waitSession } from "./wait.js"; export const CONTROL_HELP = ` sessionpipe control pair [--folder DIR]… [--mode safe|auto|inherit] [--tier 0-3] [--token T] [--name N] [--no-service] sessionpipe control mode auto|safe|inherit (how sessions run when you message them) + sessionpipe control deliver own|any (own: a message is always a prompt of its own, never read beside a turn) sessionpipe control folders [add|remove DIR…] (where sessions you message may run) sessionpipe control status | keys [remove ] | off [] | run sessionpipe wait [--session :] (a session runs this in the background) @@ -286,6 +288,27 @@ export async function controlMain( // The running daemon reads control.json again within seconds; the next run uses it. return out(` ✓ Sessions you message now run in ${want} mode (${MODE_NOW[want]}).`); } + case "deliver": { + // The machine's own choice, made at its keyboard like its mode (CONTROL.md §6). + const want = argv[2]; + const c = readControl(); + if (want !== "own" && want !== "any") { + out(` deliver: ${c ? deliverOf(c) : "not set up"}\n usage: sessionpipe control deliver own|any`); + out(" any: a message for a session that is open or mid-turn is handed to it where it is (the default)."); + out(" own: a message always runs as a prompt of its own; an open Claude Code session answers in a copy."); + return; + } + if (!c) return out(" control isn't set up on this machine: run `sessionpipe control pair ` first"); + if (want === "own") c.deliver = "own"; + else delete c.deliver; + writeControl(c); + // The running daemon reads control.json again within seconds; the next message uses it. + return out( + want === "own" + ? " ✓ A message now always runs as a prompt of its own. An open Claude Code session answers in a copy; its window is not told." + : " ✓ A message for an open session is handed to it where it is again.", + ); + } case "folders": { // The folders are the machine's own list, so they change here, at its keyboard, as // at pairing: no new pairing and no passkey. A message to a session in any other diff --git a/packages/cli/src/control/daemon.ts b/packages/cli/src/control/daemon.ts index 204e00b0..b9a8d27d 100644 --- a/packages/cli/src/control/daemon.ts +++ b/packages/cli/src/control/daemon.ts @@ -138,6 +138,7 @@ import { kindPath, messageMark, onItsWord, + ownTurnView, routeCancel, routePrompt, stillRecent, @@ -153,6 +154,7 @@ import { type ControlConfig, type ControlMode, type DaemonState, + deliverOf, HOLD_TTL_MS, NonceStore, type PairedReceiver, @@ -3628,9 +3630,19 @@ export class ControlDaemon { // What a session writes for a message handed to it in place is found by the message's // mark, which is made of its id and only of a whole ULID (core's `messageMark`). const marked = messageMark(id) !== null; - const bare = this.view(cmd.session, place, null, marked, o.held); + // A machine set to `deliver own` (§6): a receiver's message is only ever a prompt of + // its own, so the rungs that hand it to a live session beside its turn are left out. + // Where that leaves the message nowhere to go (a copy that is itself held), it takes + // the ladder as it stands: delivered beside a turn is better than not delivered. + const seenBare = this.view(cmd.session, place, null, marked, o.held); + const ownBare = + !isSink(r) && deliverOf(this.cfg) === "own" + ? ownTurnView(seenBare, this.live.get(cmd.session)?.channel != null) + : null; + const own = !!ownBare && "mode" in routePrompt(ownBare); + const bare = own && ownBare ? ownBare : seenBare; const first = routePrompt(bare); - const over = o.noInbox || isSink(r) || ("mode" in first && earlier(first.mode, "inbox")); + const over = own || o.noInbox || isSink(r) || ("mode" in first && earlier(first.mode, "inbox")); const look = over ? null : this.inboxLook(cmd.session, place, { iat: cmd.iat, body: framed, id }); const seen = over ? bare : this.view(cmd.session, place, look, marked, o.held); const route = over ? first : routePrompt(seen); diff --git a/packages/cli/src/control/route.ts b/packages/cli/src/control/route.ts index 8733aae4..1a4c0ff4 100644 --- a/packages/cli/src/control/route.ts +++ b/packages/cli/src/control/route.ts @@ -199,6 +199,18 @@ const FITS: Record boolean> = { fork: (s) => HEADLESS.has(s.harness) && s.known && !!s.held && s.canFork && !s.copied, }; +/** A session as a machine set to `deliver own` routes a message for it (§6, `deliver`): + * the rungs that hand a message to a live session as something beside its own prompt + * (a plain waiter's output, a Stop hook's block, the inbox's teammate frame) do not fit, + * so the ladder goes on to a turn the daemon runs, where the message is the prompt. A + * channel's waiter stays (`channel`): Claude Code takes what it hands over as the + * person's own words (§8). Only Claude Code: no other harness can copy a held session, + * and a message for one would have nowhere to go. */ +export function ownTurnView(s: SessionView, channel: boolean): SessionView { + if (s.harness !== "claude-code" || !s.canFork) return s; + return { ...s, waiter: s.waiter && channel, midTurn: false, inbox: false }; +} + /** Said to a `permission.answer` for a Codex session. */ export const CODEX_NO_ANSWER = "A Codex permission prompt is answered in Codex itself for now: sessionpipe does not answer it from here"; diff --git a/packages/cli/src/control/store.ts b/packages/cli/src/control/store.ts index 4c9ca800..a8965aae 100644 --- a/packages/cli/src/control/store.ts +++ b/packages/cli/src/control/store.ts @@ -86,11 +86,19 @@ export interface ControlConfig { /** Whether a session here may ask another agent through the daemon (CONTROL.md §13). * On unless `sessionpipe ask off` set it false; the daemon says which in its hello. */ ask?: boolean; + /** Whether a message may be handed to a live session as something it reads beside its + * own turn (CONTROL.md §6, `deliver`). `own`: a message only ever arrives as the + * session's prompt, in a turn the daemon runs or a channel hands over. Unsaid: `any`. */ + deliver?: Deliver; } /** `ask` as the config means it: on when unsaid. */ export const askOn = (c: Pick): boolean => c.ask !== false; +export type Deliver = "any" | "own"; +/** `deliver` as the config means it: `any` when unsaid or unreadable. */ +export const deliverOf = (c: Pick): Deliver => (c.deliver === "own" ? "own" : "any"); + /** The real folder `folder` names (absolute, or `~` / `~/…` under this user's home), when it is * one of `folders` or inside one, compared on whole path segments of real paths; else null. */ export function allowedIn(folder: string, folders: readonly string[]): string | null { @@ -142,6 +150,7 @@ export function readControl(env: NodeJS.ProcessEnv = process.env, run: Runner = ), ...(j.limits ? { limits: j.limits } : {}), ...(typeof j.ask === "boolean" ? { ask: j.ask } : {}), + ...(j.deliver === "own" ? { deliver: "own" as const } : {}), }; } catch { return null; diff --git a/packages/cli/src/usage.ts b/packages/cli/src/usage.ts index 11f1d465..4d425694 100644 --- a/packages/cli/src/usage.ts +++ b/packages/cli/src/usage.ts @@ -46,6 +46,7 @@ export const GROUPS: readonly { title: string; rows: readonly (readonly [string, ["control pair ", "Trust a device's key, at this computer's terminal"], ["control status", "The background service: what it hears and holds"], ["control mode ", "What a session you message may do"], + ["control deliver own|any", "Whether a message is always a prompt of its own"], ["control folders", "Where sessions you message may run"], ["control keys", "The keys this computer trusts"], ["control off", "Stop taking messages"], diff --git a/packages/cli/test/control-daemon.test.ts b/packages/cli/test/control-daemon.test.ts index b931ce6b..ff18b892 100644 --- a/packages/cli/test/control-daemon.test.ts +++ b/packages/cli/test/control-daemon.test.ts @@ -34,6 +34,7 @@ import { HOOKS_WORD_MS, holderOf, onItsWord, + ownTurnView, routePrompt, SEND_AGAIN_AFTER_TURN, type SessionView, @@ -119,12 +120,15 @@ async function startDaemon( extra?: Partial; /** More keys in the store, as control.json has them. */ keys?: ControlConfig["receivers"][number]["keys"]; + /** The machine's `deliver` setting (§6). */ + deliver?: ControlConfig["deliver"]; } = {}, ) { const c = cfg(o.folders); if (o.keys) c.receivers = c.receivers.map((r) => ({ ...r, keys: [...r.keys, ...(o.keys ?? [])] })); if (o.token !== undefined) c.receivers = c.receivers.map((r) => ({ ...r, token: o.token as string, token_in: "keychain" as const })); + if (o.deliver) c.deliver = o.deliver; d = new ControlDaemon({ ...c, ...(o.limits ? { limits: o.limits } : {}) }, path.join(tmp, "state", "control"), sock, { now: Date.now, fetch, @@ -216,6 +220,23 @@ describe("routing (CONTROL.md §6)", () => { copied: false, api: false, }; + it("deliver own: a live Claude Code session is never handed a message beside its turn", () => { + const own = (s: SessionView, channel = false) => routePrompt(ownTurnView(s, channel)); + // Mid-turn in a window: a copy, where `any` waits for the Stop. + expect(own({ ...base, midTurn: true, held: "open" })).toEqual({ mode: "fork" }); + // A parked plain waiter and an idle window's inbox are passed over too. + expect(own({ ...base, waiter: true, held: "open" })).toEqual({ mode: "fork" }); + expect(own({ ...base, inbox: true, held: "open" })).toEqual({ mode: "fork" }); + // A channel hands the message over as the person's own words: it stays. + expect(own({ ...base, waiter: true, held: "open" }, true)).toEqual({ mode: "waiter" }); + // A session the daemon holds through the SDK, and one nothing holds, are as before. + expect(own({ ...base, sdk: true, midTurn: true })).toEqual({ mode: "sdk" }); + expect(own({ ...base })).toEqual({ mode: "resume" }); + // No copy can be made: the view is left alone, so the message still has its Stop. + expect(own({ ...base, canFork: false, midTurn: true, held: "open" })).toEqual({ mode: "turn" }); + expect(own({ ...base, harness: "codex", midTurn: true })).toEqual({ mode: "turn" }); + }); + it("takes the first mode that fits", () => { expect(routePrompt({ ...base, sdk: true, waiter: true })).toEqual({ mode: "sdk" }); expect(routePrompt({ ...base, waiter: true, midTurn: true })).toEqual({ mode: "waiter" }); @@ -676,6 +697,14 @@ describe.runIf(unix)("delivery", () => { expect(await parked).toBeNull(); }); + it("deliver own: a message for a session mid-turn runs in a copy, and its Stop is handed nothing", async () => { + await startDaemon({ deliver: "own" }); + await ask(sock, { op: "event", session: SESSION, event: "UserPromptSubmit" }, 500); + const q = await rx.send({ session: SESSION, text: "also write pong" }); + expect(await rx.waitAck(q.id, 15_000)).toMatchObject({ outcome: "delivered", mode: "fork" }); + expect(await ask(sock, { op: "stop", session: SESSION, active: false }, 500)).not.toMatchObject({ op: "block" }); + }, 30_000); + it("turn: a message for a session mid-turn is the Stop hook's block, once", async () => { await startDaemon(); await ask(sock, { op: "event", session: SESSION, event: "UserPromptSubmit" }, 500); diff --git a/spec/CONTROL.md b/spec/CONTROL.md index 4f82b0dc..aa1ff53e 100644 --- a/spec/CONTROL.md +++ b/spec/CONTROL.md @@ -576,6 +576,23 @@ Two rules hold across the in-place modes: MUST NOT fall through to another mode after writing, and MUST NOT retry. A second attempt needs a new signed command from the person, with a new nonce. +**A machine may take every message as a prompt of its own (`deliver own`).** In three +of the in-place modes the session does not read the message as its person's prompt: a +plain waiter's output and a Stop hook's block are a command's and a hook's output, and +the inbox frames it as a teammate's request (§6.3). A harness may tell its model that +none of those carries the person's approval, and a model so told declines what needs +one (a merge, a push, a send) however the message is worded. So the person MAY set +their machine, at its own terminal, to `deliver own` (`sessionpipe control deliver +own`; `any` is the default and the ladder above). For a Claude Code session that can be +copied, a machine so set MUST NOT use `turn`, `inbox`, or a `waiter` that is not a +channel (§8): the ladder goes on to `resume` or `fork`, where the message is the prompt +of a turn the daemon runs, in the machine's mode. `sdk` and a channel's waiter are +unchanged, since there the message already arrives as the person's own words. Where +leaving those modes out would leave the message no delivery at all (the session's copy +is itself held), the ladder applies as it stands. The setting is the machine's alone: +no command names it and no hello carries it. What it costs is said to the person when +they set it: a session open in a window answers in a copy, and its window is not told. + ### 6.1 Homes A session id can be filed in more than one of a harness's accounts (Claude Code: one