Skip to content

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

11.5 KB
Raw Blame
import { readFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";

import Ajv2020 from "ajv/dist/2020.js";
import { describe, expect, it } from "vitest";
import { createTaskAction } from "../protocol-actions/create-task.js";
import { createProjectAction } from "../protocol-actions/create-project.js";
import { handoffCaseAction } from "../protocol-actions/handoff-case.js";

import {
  PAPERCLIP_SEMANTIC_ACTION_CATALOG,
  canonicalPaperclipSemanticActionCatalog,
  paperclipSemanticAction,
} from "./semantic-action-catalog.js";

const packageRoot = resolve(
  dirname(fileURLToPath(import.meta.url)),
  "..",
  "..",
);

describe("semantic action catalog", () => {
  it("compiles case handoff identity constraints in strict consumers and rejects malformed IDs", () => {
    const ajv = new Ajv2020({ allErrors: true, allowUnionTypes: true, strict: true });
    for (const schema of [handoffCaseAction.live.descriptor.inputSchema, paperclipSemanticAction("handoff_case")!.inputSchema]) {
      const validate = ajv.compile(schema);
      const input = handoffCaseAction.examples.call.input;
      expect(validate(input), JSON.stringify(validate.errors)).toBe(true);
      expect(validate({ ...input, case: { id: "case-id" } })).toBe(false);
      expect(validate({ ...input, toAgentId: "agent-id" })).toBe(false);
      expect(validate({ ...input, bindIssueId: "task-id" })).toBe(false);
      expect(validate({ ...input, case: { create: { caseType: "work", title: "New case", projectId: null } }, workflow_document_text: "Implement, verify, then finish." })).toBe(true);
      expect(validate({ ...input, case: { create: { caseType: "work", title: "New case", projectId: null } } })).toBe(false);
      expect(validate({ ...input, case: { create: { caseType: "work", title: "New case", projectId: "project-id" } }, workflow_document_text: "Implement, then verify." })).toBe(false);
    }
  });
  it("requires a workflow source for new cases and defers existing-case first-task checks to the service", () => {
    const ajv = new Ajv2020({ allErrors: true, allowUnionTypes: true, strict: true });
    for (const schema of [handoffCaseAction.live.descriptor.inputSchema, paperclipSemanticAction("handoff_case")!.inputSchema]) {
      const validate = ajv.compile(schema);
      const input = handoffCaseAction.examples.call.input;
      const create = { ...input, case: { create: { caseType: "work", title: "New work" } } };
      expect(validate(create)).toBe(false);
      const workflowId = "00000000-0000-4000-8000-000000000004";
      expect(validate({ ...create, workflowId })).toBe(true);
      expect(validate({ ...create, workflowId: null })).toBe(false);
      expect(validate({ ...create, workflowId, workflow_document_text: "Duplicate source" })).toBe(false);
      expect(validate({ ...input, workflowId: null })).toBe(true);
      expect(validate({ ...create, workflow_document_text: "Triage, implement, verify, done." })).toBe(true);
      expect(validate({ ...create, bindIssueId: "00000000-0000-4000-8000-000000000003", workflow_document_text: "Triage, implement, verify, done." })).toBe(true);
      expect(validate({ ...create, bindIssueId: "00000000-0000-4000-8000-000000000003" })).toBe(false);
      // The service decides whether this existing case is still passive.
      expect(validate({ ...input, workflow_document_text: "Triage, implement, verify, done." })).toBe(true);
      for (const text of ["", " \t\n", "x".repeat(200001)]) expect(validate({ ...create, workflow_document_text: text })).toBe(false);
      expect(validate({ ...create, workflow_document_text: "Verify the result.", initialDocuments: [{ key: "workflow", title: "Duplicate", body: "No" }] })).toBe(false);
    }
  });
  it("advertises completion intent without accepting agent routing or bypassing completion", () => {
    const ajv = new Ajv2020({ allErrors: true, allowUnionTypes: true, strict: true });
    const done = { to: "done", idempotencyKey: "finish-case", case: { id: "00000000-0000-4000-8000-000000000001" },
      instructions: "Saved final artifacts; finish the task through its existing completion gates.",
      expectedHandoffVersion: 4, expectedIssueStatusVersion: 7 };
    for (const schema of [handoffCaseAction.live.descriptor.inputSchema, paperclipSemanticAction("handoff_case")!.inputSchema]) {
      const validate = ajv.compile(schema);
      expect(validate(done), JSON.stringify(validate.errors)).toBe(true);
      expect(validate({ ...done, phase: "done" }), JSON.stringify(validate.errors)).toBe(true);
      const { expectedHandoffVersion: _caseVersion, ...withoutCaseVersion } = done;
      const { expectedIssueStatusVersion: _issueVersion, ...withoutIssueVersion } = done;
      for (const input of [withoutCaseVersion, withoutIssueVersion, { ...done, case: { id: "invalid" } },
        { ...done, case: { create: { caseType: "work", title: "New work" } } },
        { ...done, toAgentId: "00000000-0000-4000-8000-000000000002" },
        { ...done, bindIssueId: "00000000-0000-4000-8000-000000000003" },
        { ...done, phase: "verification" }, { ...done, status: "done" }, { ...done, initialDocuments: [] }, { ...done, workflow_document_text: "Already finished." }]) {
        expect(validate(input), JSON.stringify(input)).toBe(false);
      }
    }
    expect(handoffCaseAction.documentation.description).toContain("does not complete the task or yield execution");
    expect(handoffCaseAction.documentation.description).toContain("call finish_task normally with truthful completion evidence");
  });
  it("limits project repository URLs to HTTPS GitHub repository paths on both tool surfaces", () => {
    const ajv = new Ajv2020({ allErrors: true, allowUnionTypes: true, strict: true });
    for (const schema of [createProjectAction.live.descriptor.inputSchema, paperclipSemanticAction("create_project")!.inputSchema]) {
      const validate = ajv.compile(schema);
      const input = { name: "Project", idempotencyKey: "create-project-1" };
      expect(validate({ ...input, repositoryUrls: ["https://github.com/org/repo", "https://github.com/org/other.git/"] })).toBe(true);
      for (const url of [
        "http://github.com/org/repo", "file:///etc/passwd", "data:text/plain,repo",
        "https://localhost/org/repo", "https://127.0.0.1/org/repo", "https://10.0.0.1/org/repo",
        "https://github.com.evil.test/org/repo", "https://[email protected]/org/repo",
        "https://github.com:8443/org/repo", "https://github.com/org/repo?token=secret",
        "https://github.com/org/repo#fragment", "https://github.com/org/repo/tree/main",
        "https://github.com/../repo", "https://github.com/org/..",
      ]) expect(validate({ ...input, repositoryUrls: [url] }), url).toBe(false);
    }
  });

  it("advertises human assignment in both task creation contracts", () => {
    const ajv = new Ajv2020({ allErrors: true, allowUnionTypes: true, strict: true });
    for (const schema of [createTaskAction.live.descriptor.inputSchema, paperclipSemanticAction("create_task")!.inputSchema]) {
      const validate = ajv.compile(schema);
      const input = { title: "Hello", idempotencyKey: "hello", assigneeUserId: "company-owner" };
      expect(validate(input), JSON.stringify(validate.errors)).toBe(true);
      expect(validate({ ...input, assigneeUserId: null })).toBe(true);
      expect(validate({ ...input, assigneeUserId: 42 })).toBe(false);
      expect(validate({ ...input, undeclaredField: true })).toBe(false);
    }
  });

  it("accepts project handoff receipts and preserves ordinary child task receipts", () => {
    const ajv = new Ajv2020({ allErrors: true, allowUnionTypes: true, strict: true });
    const validate = ajv.compile(createTaskAction.live.descriptor.outputSchema);
    const receipt = {
      commandId: "create-task-1", disposition: "applied", stateRevision: 1,
      entityRefs: ["task-1"], scheduledWakeIds: ["wake-1"],
      task: { id: "task-1", identifier: "CHAT-1", parentId: null, projectId: "project-1", status: "todo", assigneeActorId: "agent-1" },
    };
    expect(validate(receipt), JSON.stringify(validate.errors)).toBe(true);
    const { projectId: _projectId, ...childTask } = receipt.task;
    expect(validate({ ...receipt, task: { ...childTask, parentId: "parent-1" } })).toBe(true);
    expect(validate({ ...receipt, task: { ...receipt.task, projectId: 42 } })).toBe(false);
    expect(validate({ ...receipt, task: { ...receipt.task, parentId: "" } })).toBe(false);
  });

  it("defines one immutable v1 declaration for each Codex-spine action", () => {
    const operationIds = PAPERCLIP_SEMANTIC_ACTION_CATALOG.map(
      (action) => action.operationId,
    );

    expect(operationIds).toHaveLength(49);
    expect(operationIds).toContain("set_task_monitor");
    expect(new Set(operationIds).size).toBe(operationIds.length);
    expect(operationIds).not.toContain("generic_api_request");
    expect(Object.isFrozen(PAPERCLIP_SEMANTIC_ACTION_CATALOG)).toBe(true);
    expect(
      Object.isFrozen(paperclipSemanticAction("write_document")?.inputSchema),
    ).toBe(true);
  });

  it("declares hire_agent as a native identity-only mutation", () => {
    const hire = paperclipSemanticAction("hire_agent");
    expect(hire).toMatchObject({
      effect: "write",
      requiredClaims: ["delegation:agents:create"],
      allowedModes: ["standard", "skill_test"],
    });
    expect(hire?.inputSchema.properties).not.toHaveProperty("adapterConfig");
    expect(hire?.inputSchema.properties).not.toHaveProperty("env");
  });

  it("compiles every operation input and output schema", () => {
    const ajv = new Ajv2020({
      allErrors: true,
      allowUnionTypes: true,
      strict: true,
    });

    for (const action of PAPERCLIP_SEMANTIC_ACTION_CATALOG) {
      expect(
        () => ajv.compile(action.inputSchema),
        `${action.operationId} input`,
      ).not.toThrow();
      expect(
        () => ajv.compile(action.outputSchema),
        `${action.operationId} output`,
      ).not.toThrow();
    }
  });

  it("keeps bounded mutation inputs and rejects undeclared fields", () => {
    const ajv = new Ajv2020({
      allErrors: true,
      allowUnionTypes: true,
      strict: true,
    });
    const writeDocument = paperclipSemanticAction("write_document");
    const taskContext = paperclipSemanticAction("get_task_context");
    expect(writeDocument).toBeDefined();
    expect(taskContext).toBeDefined();

    const validateWrite = ajv.compile(writeDocument!.inputSchema);
    expect(
      validateWrite({
        idempotencyKey: "write-1",
        key: "plan",
        title: "Plan",
        body: "A bounded body",
        baseRevisionId: null,
      }),
    ).toBe(true);
    expect(
      validateWrite({
        key: "plan",
        title: "Plan",
        body: "Body",
        baseRevisionId: null,
      }),
    ).toBe(false);

    const validateContext = ajv.compile(taskContext!.inputSchema);
    expect(validateContext({})).toBe(true);
    expect(validateContext({ companyId: "forged-company" })).toBe(false);
  });

  it("matches the checked-in generated inventory byte for byte", async () => {
    const generated = await readFile(
      resolve(packageRoot, "generated/semantic-action-catalog.json"),
      "utf8",
    );

    expect(generated).toBe(canonicalPaperclipSemanticActionCatalog());
  });

  it("does not carry executable authorization or binding hooks", () => {
    for (const action of PAPERCLIP_SEMANTIC_ACTION_CATALOG) {
      const keys = Object.keys(action);
      expect(keys).not.toContain("authorize");
      expect(keys).not.toContain("execute");
      expect(keys).not.toContain("binding");
    }
  });
});