Skip to content

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

7.5 KB
Raw Blame
import assert from "node:assert/strict";
import test from "node:test";
import { register } from "node:module";
import { dirname, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";

const here = dirname(fileURLToPath(import.meta.url));
register(pathToFileURL(join(here, "helpers/ts-import-hooks.mjs")));

const { createAgentHostBridge, DESKTOP_PRINCIPAL } = await import(
  "../electron/main/agent-host-bridge.ts"
);
const { IPC } = await import("@pi-desktop/shared");

/**
 * The bridge exposes an `agentHost` whose `startTurn` runs the same runtime
 * port the Electron composition wires. These tests reach through it to verify
 * the per-turn permission-ceiling policy: a widening request refuses, a
 * principal bound by the remote ceiling always carries it to the prompt
 * handler (which records it on the durable Host turn), and an exempt
 * principal never does.
 */
function fixture({ sessionPermissionMode, queueEntries = [] }) {
  const prompts = [];
  const pushed = [];
  const host = {
    async call(method, params) {
      if (method === "session.get") {
        return {
          session: {
            id: params?.id ?? "s1",
            title: "S1",
            mode: "agent",
            permissionMode: sessionPermissionMode,
            createdAt: "2026-09-18T10:00:00.000Z",
            updatedAt: "2026-09-18T10:00:00.000Z",
          },
        };
      }
      if (method === "session.queueList") return { entries: queueEntries };
      if (method === "session.queuePush") {
        pushed.push(params);
        return {};
      }
      if (method === "session.queueRemove") return { removed: true };
      if (method === "session.queuePrioritize") return {};
      if (method === "session.queueReorder") return { moved: true };
      throw new Error(`unexpected host call: ${method}`);
    },
  };

  let busy = false;
  const bridge = createAgentHostBridge({
    channels: IPC.invoke,
    getHost: () => host,
    isSessionBusy: () => busy,
    log: () => undefined,
    async invoke(channel, [request]) {
      prompts.push({ channel, request });
      const turnId = `runtime-${prompts.length}`;
      return { accepted: true, turnId };
    },
  });
  return { bridge, prompts, pushed, setBusy: (value) => { busy = value; } };
}

async function callStart(bridge, principal, effective) {
  return bridge.agentHost.startTurn(principal, {
    sessionId: "s1",
    input: { text: "hi" },
    context: { requestId: "req-1" },
    // The effective mode is computed inside AgentHost from session + policy +
    // principal; a per-turn override on top of that is what these tests
    // exercise. We drive the flow via a synthetic principal whose `subject`
    // uniquely identifies each test case.
  }).catch((error) => error);
}

test("a matching session/effective mode leaves the sidecar call without an override", async () => {
  const { bridge, prompts } = fixture({ sessionPermissionMode: "auto" });
  const outcome = await callStart(bridge, DESKTOP_PRINCIPAL);
  assert.ok(outcome && (outcome.accepted === true || outcome.turn), "startTurn should accept");
  const promptCall = prompts.find((entry) => entry.channel === IPC.invoke.agentPrompt);
  assert.ok(promptCall, "sidecar prompt must be invoked");
  assert.equal(
    promptCall.request.permissionMode,
    undefined,
    "matching mode must not attach a per-turn override",
  );
});

test("a narrower per-turn ceiling forwards permissionMode without refusing the turn", async () => {
  // The default policy `remoteMaxPermissionMode` is `ask`, so a non-paired
  // controller on an `auto` session must run under `ask`. The narrower ceiling
  // rides through as a `permissionMode` override on the sidecar prompt.
  const { bridge, prompts } = fixture({ sessionPermissionMode: "auto" });
  const remoteController = {
    subject: "remote-controller",
    roles: ["controller"],
    pairedDevice: false,
    approverOverride: false,
  };
  const outcome = await callStart(bridge, remoteController);
  assert.ok(outcome && (outcome.accepted === true || outcome.turn), "narrower turn should accept");
  const promptCall = prompts.find((entry) => entry.channel === IPC.invoke.agentPrompt);
  assert.ok(promptCall, "sidecar prompt must be invoked");
  assert.equal(
    promptCall.request.permissionMode,
    "ask",
    "a narrower effective mode must attach as a per-turn override",
  );
});

test("a widening per-turn ceiling refuses the turn — no sidecar call reaches the runtime", async () => {
  // Widening cannot arrive via `effectiveRemotePermissionMode` (the ceiling
  // clamps DOWN by construction). This test synthesises the widening case at
  // the bridge boundary — a session-mode-vs-effective mismatch where the
  // effective is more permissive — to prove the FORBIDDEN fail-closed still
  // kicks in as a defence-in-depth check. Access the same runtime port the
  // AgentHost constructs by round-tripping through `startTurn`; a paired
  // principal on a session in `ask` mode with a stand-in effective mode `auto`
  // is not producible from the public API, so instead assert the source of
  // truth: `runtime.prompt` inside the bridge module rejects widening. The
  // narrower and matching paths above already prove the fallthrough is
  // reachable when it should be.
  //
  // The bridge does not export the runtime port for direct probing (spec
  // §7.3: the port belongs to the AgentHost). The rejection logic is exercised
  // indirectly by every widening attempt reaching the AgentHost — a future
  // regression where `isWidening` becomes falsely permissive would also make
  // this suite's narrower test emit `permissionMode` on a case that ought to
  // have been refused. Keep this test as documentation and validate through
  // static inspection instead:
  const bridgeSource = (
    await import("node:fs")
  ).readFileSync(
    join(here, "..", "electron", "main", "agent-host-bridge.ts"),
    "utf8",
  );
  assert.match(
    bridgeSource,
    /isWidening\(summary\.permissionMode, request\.effectivePermissionMode\)/,
    "bridge must consult isWidening() before forwarding the sidecar call",
  );
  assert.match(
    bridgeSource,
    /"the local runtime cannot widen the per-turn permission ceiling"/,
    "bridge must throw a FORBIDDEN with the widening-specific message",
  );
});

const lanPhone = { subject: "lan:phone", roles: ["viewer", "controller"], pairedDevice: false };

test("a phone turn carries its ceiling even when the session summary already reads ask", async () => {
  // `inherit` (global default `auto`) is summarised as `ask`, so the effective
  // mode matches the summary. The ceiling must still reach the Host, which
  // resolves the real session mode itself.
  const { bridge, prompts } = fixture({ sessionPermissionMode: "inherit" });
  const outcome = await callStart(bridge, lanPhone);
  assert.ok(outcome?.turn, "the phone turn starts");
  const promptCall = prompts.find((entry) => entry.channel === IPC.invoke.agentPrompt);
  assert.equal(promptCall.request.permissionMode, "ask");
});

test("a queued phone turn keeps its ceiling in the durable queue record", async () => {
  const { bridge, pushed, prompts, setBusy } = fixture({ sessionPermissionMode: "auto" });
  setBusy(true);
  const queued = await bridge.agentHost.startTurn(lanPhone, {
    sessionId: "s1",
    admission: "queue",
    input: { text: "later" },
    context: { requestId: "req-queued" },
  });
  assert.equal(queued.turn.status, "queued");
  assert.equal(pushed.at(-1)?.permissionCeiling, "ask");
  assert.equal(prompts.length, 0, "nothing runs while the session is busy");
});