Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

26.4 KB
Raw Blame
import { describe, expect, it } from "vitest";
import type { AgentEventEnvelope, UiMessage } from "@pi-desktop/shared";

import type { LaunchResolver } from "./launch-resolver.js";
import { PlanExecutionDispatcher } from "./plan-dispatch.js";
import { RuntimeService, type RuntimeHostLink, type RuntimeSidecarLink, type TurnEndedInfo } from "./runtime-service.js";

type Call = { method: string; params: Record<string, unknown> };

class FakeHost implements RuntimeHostLink {
  calls: Call[] = [];
  beforeCall?: (method: string) => Promise<void>;
  notify: ((method: string, params: unknown) => void) | null = null;
  available = true;
  private turnCounter = 0;
  messages = new Map<string, UiMessage[]>();
  failAppend = false;
  /** Answer `session.beginTurn` like a Host that predates the turn ceiling. */
  ignoreCeiling = false;
  session: Record<string, unknown> = { id: "s1", mode: "agent", permissionMode: "ask", providerId: "p1", modelId: "m1" };

  async call<T = unknown>(method: string, params: Record<string, unknown> = {}): Promise<T> {
    this.calls.push({ method, params });
    if (this.beforeCall) await this.beforeCall(method);
    switch (method) {
      case "settings.get":
        return {} as T;
      case "session.get":
        return { session: { ...this.session, id: params.id, messages: this.messages.get(String(params.id)) ?? [] } } as T;
      case "session.beginTurn":
        this.turnCounter += 1;
        return {
          turnId: `turn-${this.turnCounter}`,
          ...(params.permissionCeiling && !this.ignoreCeiling ? { permissionCeiling: params.permissionCeiling } : {}),
        } as T;
      case "session.appendMessage": {
        if (this.failAppend) throw Object.assign(new Error("disk full"), { errorCode: "INTERNAL" });
        const list = this.messages.get(String(params.sessionId)) ?? [];
        list.push(params.message as UiMessage);
        this.messages.set(String(params.sessionId), list);
        return {} as T;
      }
      case "session.endTurn":
        return { ok: true } as T;
      case "session.saveInflightMessage":
        return { ok: true } as T;
      case "plans.abort":
      case "plans.claimExecution":
      case "plans.finishExecution":
        return {} as T;
      default:
        throw new Error(`unexpected host call ${method}`);
    }
  }
  isAvailable(): boolean {
    return this.available;
  }
  onNotification(handler: (method: string, params: unknown) => void): () => void {
    this.notify = handler;
    return () => {
      this.notify = null;
    };
  }
  onExit(): () => void {
    return () => undefined;
  }
}

class FakeSidecar implements RuntimeSidecarLink {
  calls: Call[] = [];
  beforeCall?: (method: string) => Promise<void>;
  notify: ((method: string, params: unknown) => void) | null = null;
  exit: ((info: { intentional: boolean; code: number | null; signal: NodeJS.Signals | null }) => void) | null = null;
  roots = new Map<string, string | undefined>();
  rejectPrompt = false;
  running = false;
  /** Error `agent.compact` answers with; `null` accepts the compaction. */
  compactError: Error | null = null;
  /** Runs inside a failing `agent.compact`, e.g. to persist a checkpoint. */
  beforeCompact: (() => void) | null = null;

  async call<T = unknown>(method: string, params: Record<string, unknown> = {}): Promise<T> {
    this.calls.push({ method, params });
    if (this.beforeCall) await this.beforeCall(method);
    switch (method) {
      case "agent.prompt":
      case "agent.executeApprovedPlan":
        if (this.rejectPrompt) throw Object.assign(new Error("model not configured"), { data: { errorCode: "MODEL_NOT_CONFIGURED" } });
        this.running = true;
        return { accepted: true, turnId: params.turnId } as T;
      case "agent.steeringContext":
        return { supportsVision: false } as T;
      case "agent.steer":
        return { accepted: true, turnId: params.expectedTurnId } as T;
      case "agent.compact":
        this.beforeCompact?.();
        if (this.compactError) throw this.compactError;
        return { accepted: true } as T;
      case "agent.abort":
        this.running = false;
        return { ok: true } as T;
      case "agent.stop":
        return { requested: this.running } as T;
      case "agent.getStatus":
        return { status: { sessionId: params.sessionId, isRunning: this.running, pendingToolConfirmations: 0 } } as T;
      case "asktool.resolve":
        return { ok: true } as T;
      default:
        throw new Error(`unexpected sidecar call ${method}`);
    }
  }
  onNotification(handler: (method: string, params: unknown) => void): () => void {
    this.notify = handler;
    return () => {
      this.notify = null;
    };
  }
  onExit(handler: (info: { intentional: boolean; code: number | null; signal: NodeJS.Signals | null }) => void): () => void {
    this.exit = handler;
    return () => {
      this.exit = null;
    };
  }
  setProjectInstructionRoot(sessionId: string, projectPath?: string): void {
    this.roots.set(sessionId, projectPath);
  }
  clearProjectInstructionRoot(): void {}
  clearVendorAuthBindings(): void {}
}

const launch: LaunchResolver = {
  async resolve(sessionId, session) {
    return {
      providerId: String(session.providerId ?? "p1"),
      modelId: String(session.modelId ?? "m1"),
      projectPath: "/work/project",
      sidecarParams: {
        sessionId,
        mode: "agent",
        provider: { id: "p1", name: "P1", modelId: "m1", apiKey: "k", supportsReasoning: false, supportedThinkingLevels: ["off"] },
      },
    };
  },
};

function build() {
  const host = new FakeHost();
  const sidecar = new FakeSidecar();
  const events: AgentEventEnvelope[] = [];
  const ended: TurnEndedInfo[] = [];
  const logs: Array<{ level: string; message: string }> = [];
  const service = new RuntimeService({
    getHost: () => host,
    getSidecar: () => sidecar,
    launch,
    log: (level, message) => logs.push({ level, message }),
    now: () => Date.parse("2026-09-18T00:00:00.000Z"),
  });
  service.attachHost(host);
  service.attachSidecar(sidecar);
  service.onEvent((envelope) => events.push(envelope));
  service.onTurnEnded((info) => ended.push(info));
  return { host, sidecar, service, events, ended, logs };
}

describe("RuntimeService manual compaction against a lost reply (#795)", () => {
  const timeout = () => new Error("sidecar RPC timeout: agent.compact");
  const record = (id: string) => ({ id, summary: "s", throughMessageId: "m1", tokensBefore: 1, createdAt: "2026-09-18T00:00:00.000Z" });

  it("reports the durable outcome when the checkpoint landed after a transport timeout", async () => {
    const { host, sidecar, service, logs } = build();
    host.session = { ...host.session, compaction: record("c-old") };
    sidecar.compactError = timeout();
    // The sidecar keeps summarizing and persists through host-core, so the
    // host's own deadline is not evidence that the compaction failed.
    sidecar.beforeCompact = () => {
      host.session = { ...host.session, compaction: record("c-new") };
    };
    await expect(service.compact("s1")).resolves.toEqual({ accepted: true });
    expect(host.calls.filter((call) => call.method === "session.get")).toHaveLength(2);
    expect(logs).toContainEqual({
      level: "warn",
      message: "manual compaction outlived its transport deadline; the checkpoint landed",
    });
  });

  it("keeps reporting a timeout when no checkpoint landed", async () => {
    const { host, sidecar, service } = build();
    host.session = { ...host.session, compaction: record("c-old") };
    sidecar.compactError = timeout();
    await expect(service.compact("s1")).rejects.toThrow("sidecar RPC timeout: agent.compact");
    expect(host.calls.filter((call) => call.method === "session.get")).toHaveLength(2);
  });

  it("does not reconcile a sidecar verdict with a checkpoint from another attempt", async () => {
    const { host, sidecar, service } = build();
    host.session = { ...host.session, compaction: record("c-old") };
    // A real compaction failure is the sidecar's own verdict: an older or
    // unrelated durable checkpoint must not turn it into a success.
    sidecar.beforeCompact = () => {
      host.session = { ...host.session, compaction: record("c-new") };
    };
    sidecar.compactError = Object.assign(new Error("context compaction failed"), {
      errorCode: "CONTEXT_COMPACTION_FAILED",
    });
    await expect(service.compact("s1")).rejects.toThrow("context compaction failed");
    expect(host.calls.filter((call) => call.method === "session.get")).toHaveLength(1);
  });

  it("accepts an acknowledged compaction and never re-reads the session", async () => {
    const { host, service } = build();
    await expect(service.compact("s1")).resolves.toEqual({ accepted: true });
    expect(host.calls.filter((call) => call.method === "session.get")).toHaveLength(1);
  });
});

const owner = { subject: "desktop", roles: ["owner" as const], pairedDevice: true };

async function settle(): Promise<void> {
  for (let index = 0; index < 5; index += 1) await new Promise((resolve) => setImmediate(resolve));
}

function holdNextCall(link: FakeHost | FakeSidecar, method: string) {
  let entered!: () => void;
  let release!: () => void;
  const reached = new Promise<void>((resolve) => { entered = resolve; });
  const released = new Promise<void>((resolve) => { release = resolve; });
  link.beforeCall = async (calledMethod) => {
    if (calledMethod !== method) return;
    link.beforeCall = undefined;
    entered();
    await released;
  };
  return { reached, release };
}

describe("RuntimeService per-turn permission ceiling", () => {
  const phone = { subject: "lan:phone", roles: ["controller" as const], pairedDevice: false };

  it("records the ceiling on the durable Host turn instead of the runtime prompt", async () => {
    const { host, sidecar, service } = build();
    host.session = { ...host.session, permissionMode: "auto" };
    await service.prompt({ sessionId: "s1", content: "from phone", effectivePermissionMode: "ask", permissionCeiling: "ask", principal: phone });
    expect(host.calls.find((call) => call.method === "session.beginTurn")?.params.permissionCeiling).toBe("ask");
    expect(sidecar.calls.find((call) => call.method === "agent.prompt")?.params).not.toHaveProperty("permissionMode");
    await service.prompt({ sessionId: "s2", content: "from desktop", effectivePermissionMode: "auto", principal: owner });
    expect(host.calls.filter((call) => call.method === "session.beginTurn")[1]?.params).not.toHaveProperty("permissionCeiling");
    await service.dispose();
  });

  it("starts nothing when the Host does not confirm the ceiling", async () => {
    const { host, sidecar, service, ended } = build();
    host.ignoreCeiling = true;
    await expect(service.prompt({ sessionId: "s1", content: "from phone", effectivePermissionMode: "ask", permissionCeiling: "ask", principal: phone }))
      .rejects.toMatchObject({ errorCode: "PERMISSION_CEILING_UNAVAILABLE" });
    expect(sidecar.calls.some((call) => call.method === "agent.prompt")).toBe(false);
    expect(host.calls.some((call) => call.method === "session.appendMessage")).toBe(false);
    expect(host.calls.find((call) => call.method === "session.endTurn")?.params)
      .toMatchObject({ turnId: "turn-1", status: "error", errorCode: "PERMISSION_CEILING_UNAVAILABLE" });
    expect(ended).toEqual([expect.objectContaining({ turnId: "turn-1" })]);
    expect(service.isBusy("s1")).toBe(false);
    await service.dispose();
  });
});

describe("RuntimeService startup cancellation", () => {
  for (const targeted of [false, true]) {
    it.each([
      ["host", "settings.get"],
      ["host", "session.beginTurn"],
      ["host", "session.appendMessage"],
      ["sidecar", "agent.prompt"],
    ] as const)(`settles a ${targeted ? "named" : "session"} abort after %s %s admission`, async (link, method) => {
      const fixture = build();
      const { host, sidecar, service, ended } = fixture;
      const gate = holdNextCall(fixture[link], method);
      const starting = service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner });
      await gate.reached;
      const stopping = service.abort("s1", targeted ? "turn-1" : undefined);
      try {
        await settle();
      } finally {
        gate.release();
      }
      await Promise.all([starting, stopping]);
      expect(sidecar.running).toBe(false);
      expect(service.isBusy("s1")).toBe(false);
      expect(ended).toEqual([expect.objectContaining({ turnId: "turn-1", reason: "aborted" })]);
      expect(host.calls.filter((call) => call.method === "session.endTurn")).toHaveLength(1);

      // The abort acknowledgement is the boundary after which a new input may
      // start; it must not inherit the canceled admission or its terminal event.
      await expect(service.prompt({ sessionId: "s1", content: "follow-up", effectivePermissionMode: "ask", principal: owner }))
        .resolves.toEqual({ turnId: "turn-2" });
      expect(sidecar.running).toBe(true);
      await service.dispose();
    });
  }

  it("preserves an admission failure and releases the session for the next input", async () => {
    const { host, sidecar, service, ended } = build();
    host.failAppend = true;
    const gate = holdNextCall(host, "session.appendMessage");
    const starting = service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner });
    const failedStart = expect(starting).rejects.toThrow("disk full");
    await gate.reached;
    const stopping = service.abort("s1");
    await settle();
    gate.release();
    await Promise.all([failedStart, stopping]);
    expect(sidecar.calls.some((call) => call.method === "agent.prompt")).toBe(false);
    expect(service.isBusy("s1")).toBe(false);
    expect(ended).toHaveLength(1);
    host.failAppend = false;
    await expect(service.prompt({ sessionId: "s1", content: "retry", effectivePermissionMode: "ask", principal: owner }))
      .resolves.toEqual({ turnId: "turn-2" });
    await service.dispose();
  });

  it.each([
    ["host", "plans.claimExecution"],
    ["sidecar", "agent.executeApprovedPlan"],
  ] as const)("cancels an approved plan still being admitted at %s %s", async (link, method) => {
    const fixture = build();
    const { host, sidecar, service, ended } = fixture;
    const dispatcher = new PlanExecutionDispatcher({
      getHost: () => host, getSidecar: () => sidecar, launch, runtime: service, log: () => {},
    });
    const gate = holdNextCall(fixture[link], method);
    const starting = dispatcher.dispatchApprovedPlan({
      id: "execution-1", proposalId: "proposal-1", sessionId: "s1", state: "queued",
      title: "Approved work", plan: "Work", question: "Proceed?", targetPermissionMode: "ask",
      artifact: { relativePath: ".pi/plan/approved.md", sha256: "a".repeat(64), sizeBytes: 4 },
    });
    await gate.reached;
    const stopping = service.abort("s1");
    await settle();
    gate.release();
    await Promise.all([starting, stopping]);
    await settle();
    expect(sidecar.running).toBe(false);
    expect(ended).toEqual([expect.objectContaining({ turnId: "turn-1", reason: "aborted" })]);
    expect(host.calls.filter((call) => call.method === "plans.finishExecution")).toEqual([
      { method: "plans.finishExecution", params: { executionId: "execution-1", status: "interrupted", errorCode: "TURN_ABORTED" } },
    ]);
    dispatcher.dispose();
    await service.dispose();
  });

  it("locks an active turn's abort reason before a racing terminal event", async () => {
    const { sidecar, service, ended } = build();
    const { turnId } = await service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner });
    const stopping = service.abort("s1", turnId);
    sidecar.notify?.("agent.event", { sessionId: "s1", turnId, ts: 1, event: { type: "agent_end", messageIds: [] } });
    await stopping;
    await settle();
    expect(ended).toEqual([expect.objectContaining({ turnId, reason: "aborted" })]);
    await service.dispose();
  });

  it("keeps a pending abort ahead of the next input without blocking other sessions", async () => {
    const { host, sidecar, service } = build();
    await service.prompt({ sessionId: "s1", content: "first", effectivePermissionMode: "ask", principal: owner });
    const gate = holdNextCall(sidecar, "agent.abort");
    const stopping = service.abort("s1", "turn-1");
    await gate.reached;
    const next = service.prompt({ sessionId: "s1", content: "next", effectivePermissionMode: "ask", principal: owner });
    const staleStop = service.abort("s1", "turn-1");
    try {
      await expect(service.prompt({ sessionId: "s2", content: "independent", effectivePermissionMode: "ask", principal: owner }))
        .resolves.toEqual({ turnId: "turn-2" });
    } finally {
      gate.release();
    }
    await stopping;
    await expect(next).resolves.toEqual({ turnId: "turn-3" });
    await staleStop;
    expect(service.activeTurnId("s1")).toBe("turn-3");
    expect(service.activeTurnId("s2")).toBe("turn-2");
    expect(host.calls.filter((call) => call.method === "session.endTurn")).toHaveLength(1);
    await service.dispose();
  });
});

describe("RuntimeService prompt lifecycle", () => {
  it("opens a durable turn, persists the user row, then starts the runtime under that turn id", async () => {
    const { host, sidecar, service, events } = build();
    const { turnId } = await service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner });
    expect(turnId).toBe("turn-1");
    const methods = host.calls.map((call) => call.method);
    expect(methods.indexOf("session.beginTurn")).toBeLessThan(methods.indexOf("session.appendMessage"));
    const prompt = sidecar.calls.find((call) => call.method === "agent.prompt");
    expect(prompt?.params.turnId).toBe("turn-1");
    expect(prompt?.params.content).toBe("hello");
    expect(sidecar.roots.get("s1")).toBe("/work/project");
    expect(service.isBusy("s1")).toBe(true);
    expect(service.activeTurnId("s1")).toBe("turn-1");
    // The user row is announced to subscribers before the runtime answers.
    expect(events.map((event) => event.event.type)).toEqual(["message_start", "message_end"]);
    const userRow = (events[0]!.event as { message: UiMessage }).message;
    expect(userRow.role).toBe("user");
    expect(host.messages.get("s1")?.[0]?.id).toBe(userRow.id);
  });

  it("keeps a client-chosen UUID as the durable user row id", async () => {
    const { host, service } = build();
    const id = "6f1c1e2a-3b4d-4c5e-8f6a-7b8c9d0e1f2a";
    await service.prompt({ sessionId: "s1", content: "x", userMessageId: id, effectivePermissionMode: "ask", principal: owner });
    expect(host.messages.get("s1")?.[0]?.id).toBe(id);
  });

  it("refuses a second prompt while the turn runs and settles the turn on agent_end", async () => {
    const { host, sidecar, service, ended } = build();
    await service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner });
    await expect(
      service.prompt({ sessionId: "s1", content: "again", effectivePermissionMode: "ask", principal: owner }),
    ).rejects.toMatchObject({ errorCode: "AGENT_BUSY" });
    sidecar.notify?.("agent.event", { sessionId: "s1", turnId: "turn-1", ts: 1, event: { type: "agent_end", messageIds: [] } });
    await settle();
    const end = host.calls.find((call) => call.method === "session.endTurn");
    expect(end?.params).toMatchObject({ turnId: "turn-1", status: "completed" });
    expect(ended).toEqual([{ sessionId: "s1", turnId: "turn-1", reason: "completed", settled: true }]);
    expect(service.isBusy("s1")).toBe(false);
  });

  it("closes the turn as failed when the runtime rejects the prompt", async () => {
    const { host, sidecar, service, ended } = build();
    sidecar.rejectPrompt = true;
    await expect(
      service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner }),
    ).rejects.toThrow("model not configured");
    const end = host.calls.find((call) => call.method === "session.endTurn");
    expect(end?.params).toMatchObject({ turnId: "turn-1", status: "error", errorCode: "MODEL_NOT_CONFIGURED" });
    expect(ended[0]).toMatchObject({ reason: "error", errorCode: "MODEL_NOT_CONFIGURED" });
    expect(service.isBusy("s1")).toBe(false);
  });

  it("never starts a runtime turn when the user row could not be appended", async () => {
    const { host, sidecar, service } = build();
    host.failAppend = true;
    await expect(
      service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner }),
    ).rejects.toThrow("disk full");
    expect(sidecar.calls.some((call) => call.method === "agent.prompt")).toBe(false);
    expect(host.calls.find((call) => call.method === "session.endTurn")?.params).toMatchObject({ status: "error" });
  });

  it("records an abort before the cancel request so a late agent_end cannot restate it as completed", async () => {
    const { host, sidecar, service, ended } = build();
    await service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner });
    await service.abort("s1");
    expect(sidecar.calls.some((call) => call.method === "agent.abort")).toBe(true);
    sidecar.notify?.("agent.event", { sessionId: "s1", turnId: "turn-1", ts: 1, event: { type: "agent_end", messageIds: [] } });
    await settle();
    const ends = host.calls.filter((call) => call.method === "session.endTurn");
    expect(ends).toHaveLength(1);
    expect(ends[0]?.params).toMatchObject({ status: "aborted", errorCode: "TURN_ABORTED" });
    expect(ended).toHaveLength(1);
    expect(ended[0]?.reason).toBe("aborted");
  });

  it("ignores a terminal event that names a turn which no longer owns the session", async () => {
    const { host, sidecar, service, events } = build();
    await service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner });
    events.length = 0;
    sidecar.notify?.("agent.event", { sessionId: "s1", turnId: "turn-0", ts: 1, event: { type: "agent_end", messageIds: [] } });
    sidecar.notify?.("agent.event", { sessionId: "s1", ts: 1, event: { type: "agent_end", messageIds: [] } });
    await settle();
    expect(events).toHaveLength(0);
    expect(host.calls.some((call) => call.method === "session.endTurn")).toBe(false);
    expect(service.isBusy("s1")).toBe(true);
  });

  it("persists completed assistant and tool rows under the owning turn", async () => {
    const { host, sidecar, service } = build();
    await service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner });
    const assistant: UiMessage = { id: "a1", role: "assistant", content: "done", createdAt: "2026-09-18T00:00:00.000Z", status: "complete" };
    sidecar.notify?.("agent.event", { sessionId: "s1", turnId: "turn-1", ts: 1, event: { type: "tool_start", toolCallId: "c1", toolName: "Read", args: { path: "a" } } });
    sidecar.notify?.("agent.event", { sessionId: "s1", turnId: "turn-1", ts: 2, event: { type: "tool_end", toolCallId: "c1", result: "ok" } });
    sidecar.notify?.("agent.event", { sessionId: "s1", turnId: "turn-1", ts: 3, event: { type: "message_end", message: assistant } });
    await settle();
    const appended = host.calls.filter((call) => call.method === "session.appendMessage").map((call) => call.params);
    expect(appended.map((params) => (params.message as UiMessage).id)).toEqual([expect.any(String), "c1", "a1"]);
    expect(appended[1]).toMatchObject({ turnId: "turn-1" });
    expect((appended[1]!.message as UiMessage).toolName).toBe("Read");
    expect(appended[2]).toMatchObject({ turnId: "turn-1" });
  });

  it("turns a host permission request into an agent event that names the asking delegate", async () => {
    const { host, sidecar, service, events } = build();
    await service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner });
    sidecar.notify?.("agent.event", {
      sessionId: "s1",
      turnId: "turn-1",
      ts: 1,
      parentToolCallId: "task-1",
      agentName: "reviewer",
      event: { type: "tool_start", toolCallId: "c9", toolName: "Bash", args: {} },
    });
    events.length = 0;
    host.notify?.("permissions.request", {
      requestId: "req-1",
      sessionId: "s1",
      toolCallId: "c9",
      toolName: "Bash",
      argsPreview: "rm",
      risk: "high",
      reason: "shell",
    });
    expect(events).toHaveLength(1);
    expect(events[0]?.turnId).toBe("turn-1");
    expect(events[0]?.event).toMatchObject({
      type: "tool_permission_request",
      request: { requestId: "req-1", agentName: "reviewer", parentToolCallId: "task-1" },
    });
  });

  it("settles every running turn as aborted with the honest crash code when the sidecar dies", async () => {
    const { host, sidecar, service, ended } = build();
    await service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner });
    sidecar.exit?.({ intentional: false, code: 1, signal: null });
    await settle();
    expect(host.calls.find((call) => call.method === "session.endTurn")?.params).toMatchObject({
      status: "aborted",
      errorCode: "AGENT_SIDECAR_CRASHED",
      recoverInflight: true,
    });
    expect(ended[0]?.reason).toBe("aborted");
    expect(ended[0]?.errorCode).toBe("AGENT_SIDECAR_CRASHED");
    expect(service.isBusy("s1")).toBe(false);
  });

  it("steers only the live turn and refuses once it ended", async () => {
    const { sidecar, service } = build();
    await service.prompt({ sessionId: "s1", content: "hello", effectivePermissionMode: "ask", principal: owner });
    expect(await service.steer({ sessionId: "s1", turnId: "turn-1", content: "also", principal: owner })).toEqual({ accepted: true });
    expect(sidecar.calls.at(-1)?.params).toMatchObject({ expectedTurnId: "turn-1", content: "also" });
    expect(await service.steer({ sessionId: "s1", turnId: "turn-9", content: "no", principal: owner })).toEqual({ accepted: false });
  });

  it("refuses native Pi sessions and attachments the headless runtime cannot serve", async () => {
    const { service } = build();
    await expect(
      service.prompt({ sessionId: "native-pi:x", content: "hi", effectivePermissionMode: "ask", principal: owner }),
    ).rejects.toMatchObject({ errorCode: "NATIVE_PI_UNSUPPORTED" });
    await expect(
      service.prompt({
        sessionId: "s1",
        content: "hi",
        attachments: [{ path: "/tmp/a.png", name: "a.png", kind: "image" }],
        effectivePermissionMode: "ask",
        principal: owner,
      }),
    ).rejects.toMatchObject({ errorCode: "INVALID_ARGUMENT" });
  });
});