Skip to content

The open-source app everyone uses to manage agents at work

7.9 KB
Raw Blame
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";

import Ajv2020 from "ajv/dist/2020.js";

import { readAcpxSidecarProtocolVersion } from "../scripts/acpx-sidecar-contract.mjs";

const schema = JSON.parse(
  await readFile(
    new URL(
      "../protocol/provider-schemas/acpx-sidecar.schema.json",
      import.meta.url,
    ),
    "utf8",
  ),
);
const validate = new Ajv2020({ allErrors: true, strict: true }).compile(schema);
const protocolVersion = readAcpxSidecarProtocolVersion(schema);

const messages = [
  {
    protocolVersion,
    id: 1,
    command: "initialize",
    params: {},
  },
  {
    protocolVersion,
    id: 1,
    ok: true,
    result: {},
  },
  {
    protocolVersion,
    sequence: 1,
    eventType: "runtime.event",
    runId: "run-1",
    turnId: "turn-1",
    payload: {},
  },
];

test("the ACPX sidecar schema accepts each versioned message family", () => {
  for (const message of messages) {
    assert.equal(validate(message), true, JSON.stringify(validate.errors));
  }
});

test("Pi session admission requires an explicit supported thinking level including recovery", () => {
  const open = (params) => ({ protocolVersion, id: 1, command: "session.open", params });
  for (const piThinkingLevel of ["off", "low", "high", "max"]) {
    assert.equal(validate(open({ agent: "pi", piThinkingLevel })), true, JSON.stringify(validate.errors));
    assert.equal(validate(open({ agent: "pi", piThinkingLevel, expectedIdentity: null })), true);
    assert.equal(validate(open({ agent: "pi", piThinkingLevel, expectedIdentity: { piThinkingLevel } })), true);
  }
  for (const piThinkingLevel of [undefined, null, "medium", "minimal", "xhigh", " LOW ", 1]) {
    assert.equal(validate(open({ agent: "pi", piThinkingLevel })), false);
    assert.equal(validate(open({ agent: "pi", piThinkingLevel: "low", expectedIdentity: { piThinkingLevel } })), false);
  }
  for (const agent of ["claude", "codex", "grok", "cursor", "copilot"]) {
    assert.equal(validate(open({ agent })), true);
    assert.equal(validate(open({ agent, expectedIdentity: null })), true);
    assert.equal(validate(open({ agent, piThinkingLevel: "low" })), false);
    assert.equal(validate(open({ agent, expectedIdentity: { piThinkingLevel: "low" } })), false);
  }
});

test("public Pi descriptors preserve effective thinking levels without invalidating historical replay", async () => {
  const descriptorSchema = JSON.parse(await readFile(new URL("../protocol/schemas/provider-descriptor.schema.json", import.meta.url), "utf8"));
  const validateDescriptor = new Ajv2020({ allErrors: true, strict: true, strictRequired: false }).compile(descriptorSchema);
  const historical = {
    provider: "acpx", driver: "acpx_runtime", model: "model", executionKind: "local_process",
    providerVersion: "0.13.1", agent: "pi", requestedModel: "model", acpProtocolVersion: 1,
    agentServerPackage: "pi-acp", agentServerVersion: "0.0.33", acpxRecordId: null, agentProcessId: null,
  };
  assert.equal(validateDescriptor(historical), true, JSON.stringify(validateDescriptor.errors));
  for (const piThinkingLevel of ["off", "low", "high", "max"]) {
    assert.equal(validateDescriptor({ ...historical, piThinkingLevel }), true);
  }
  for (const piThinkingLevel of ["medium", null, "xhigh", 1]) {
    assert.equal(validateDescriptor({ ...historical, piThinkingLevel }), false);
  }
  assert.equal(validateDescriptor({ ...historical, agent: "copilot", piThinkingLevel: "low" }), false);
  assert.equal(validateDescriptor({ ...historical, provider: "codex", driver: "codex_app_server", piThinkingLevel: "low" }), false);
});

test("the ACPX sidecar schema shares the durable stable-identity boundary", () => {
  const longestTurnId = "t".repeat(240);
  assert.equal(validate({ ...messages[2], turnId: longestTurnId }), true);
  assert.equal(validate({ ...messages[2], turnId: "t".repeat(241) }), false);
  for (const turnId of ["turn 1", "réturn-1", "turn/1", "_turn-1"]) {
    assert.equal(validate({ ...messages[2], turnId }), false, turnId);
  }
  assert.equal(validate({ ...messages[2], runId: "r".repeat(161) }), false);
  for (const runId of ["run 1", "rún-1", "run/1", "_run-1"]) {
    assert.equal(validate({ ...messages[2], runId }), false, runId);
  }
});

test("rich activity has a closed display-only envelope and explicit run/turn scope", () => {
  const message = {
    ...messages[2],
    eventType: "runtime.rich_event",
    payload: { eventType: "plan.updated", itemId: "display-1", payload: {} },
  };
  assert.equal(validate(message), true, JSON.stringify(validate.errors));
  for (const field of ["runId", "turnId"]) {
    assert.equal(validate({ ...message, [field]: null }), false);
  }
  for (const eventType of ["turn.completed", "run.result.proposed", "semantic_tool.input", "unknown"]) {
    assert.equal(validate({ ...message, payload: { ...message.payload, eventType } }), false);
  }
  for (const field of ["sourceRef", "priority", "runId"]) {
    assert.equal(validate({ ...message, payload: { ...message.payload, [field]: "forged" } }), false);
  }
});

test("the ACPX sidecar schema fails closed on drift", () => {
  for (const message of [
    { ...messages[0], protocolVersion: protocolVersion + 1 },
    { ...messages[0], command: "session.destroy" },
    { protocolVersion, id: 1, ok: true, result: {}, error: error() },
    { protocolVersion, id: 1, ok: true },
    { protocolVersion, id: 1, ok: false },
    { protocolVersion, id: 1, ok: false, result: {}, error: error() },
    { ...messages[2], unexpected: true },
  ]) {
    assert.equal(validate(message), false);
  }
});

test("every ACPX sidecar message family uses the shared version", () => {
  for (const family of ["request", "response", "event"]) {
    assert.deepEqual(schema.$defs[family].properties.protocolVersion, {
      $ref: "#/$defs/protocolVersion",
    });
  }
});

test("the ACPX sidecar schema id carries the declared protocol version", () => {
  assert.equal(
    schema.$id,
    `https://paperclip.dev/schemas/acpx-sidecar/v${protocolVersion}/message.schema.json`,
  );
});

test("a coordinated family-version upgrade cannot outpace the schema id", () => {
  const driftedSchema = structuredClone(schema);
  driftedSchema.$defs.protocolVersion.const = protocolVersion + 1;

  assert.throws(
    () => readAcpxSidecarProtocolVersion(driftedSchema),
    /must match its authoritative schema \$id/,
  );
});

function error() {
  return {
    code: "runtime_failed",
    message: "The runtime failed.",
    retryable: false,
  };
}

test("turn control schema preserves explicit modes and rejects open-ended dispatch", () => {
  const message = { protocolVersion, id: 1, command: "turn.steer", params: {
    turnId: "turn-1", controlId: "control-1", mode: "follow_up", message: "Then validate",
  } };
  assert.equal(validate(message), true, JSON.stringify(validate.errors));
  for (const params of [ { ...message.params, mode: "cancel" }, { ...message.params, method: "arbitrary" },
    { ...message.params, controlId: "" }, { ...message.params, turnId: "wrong turn" }, { ...message.params, mode: undefined } ]) {
    assert.equal(validate({ ...message, params }), false);
  }
});

// Runtime payloads are provider-owned; the sidecar envelope stays closed.
// Pi validates native provenance before creating these boundary/history fields.
test("Pi native empty message boundaries and replay history fit the strict sidecar envelope", () => {
  for (const payload of [
    { type: "text_delta", stream: "output", text: "", messageId: "message-1", piMessageBoundary: { phase: "start" } },
    { type: "text_delta", stream: "output", text: "", messageId: "message-1", piMessageBoundary: { phase: "end", stopReason: "toolUse" } },
    { type: "text_delta", stream: "output", text: "Prior assistant reply", messageId: "history-1", piMessageHistory: true },
  ]) {
    assert.equal(validate({ ...messages[2], payload }), true, JSON.stringify(validate.errors));
    assert.equal(validate({ ...messages[2], payload, piMessageBoundary: { phase: "start" } }), false);
  }
});