Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/deliver-own.md
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 23 additions & 0 deletions packages/cli/src/control/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
type ControlMode,
controlFile,
controlState,
deliverOf,
parseMode,
readControl,
writeControl,
Expand All @@ -60,6 +61,7 @@ import { waitForMessage, waitSession } from "./wait.js";

export const CONTROL_HELP = ` sessionpipe control pair <receiver> [--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 <id>] | off [<receiver>] | run
sessionpipe wait [--session <harness>:<id>] (a session runs this in the background)
Expand Down Expand Up @@ -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 <receiver>` 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
Expand Down
16 changes: 14 additions & 2 deletions packages/cli/src/control/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ import {
kindPath,
messageMark,
onItsWord,
ownTurnView,
routeCancel,
routePrompt,
stillRecent,
Expand All @@ -153,6 +154,7 @@ import {
type ControlConfig,
type ControlMode,
type DaemonState,
deliverOf,
HOLD_TTL_MS,
NonceStore,
type PairedReceiver,
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/src/control/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ const FITS: Record<DeliveryMode, (s: SessionView) => 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";
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/control/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ControlConfig, "ask">): 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<ControlConfig, "deliver">): 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 {
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const GROUPS: readonly { title: string; rows: readonly (readonly [string,
["control pair <receiver>", "Trust a device's key, at this computer's terminal"],
["control status", "The background service: what it hears and holds"],
["control mode <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"],
Expand Down
29 changes: 29 additions & 0 deletions packages/cli/test/control-daemon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
HOOKS_WORD_MS,
holderOf,
onItsWord,
ownTurnView,
routePrompt,
SEND_AGAIN_AFTER_TURN,
type SessionView,
Expand Down Expand Up @@ -119,12 +120,15 @@ async function startDaemon(
extra?: Partial<DaemonDeps>;
/** 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,
Expand Down Expand Up @@ -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" });
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions spec/CONTROL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading