Skip to content
Draft
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
13 changes: 13 additions & 0 deletions app/api/board/[[...path]]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { team } from "@/lib/board/team";

/**
* Recoup team board receiver.
*
* Answers only what Vercel's API sends this deployment: watcher
* registration and the wake-ups that run its watchers (`POST /api/board/wake`).
* It serves no other callers; people and agents use the board in Vercel's API.
*/
export const GET = team.fetch;
export const POST = team.fetch;

export const maxDuration = 300;
19 changes: 19 additions & 0 deletions lib/board/__tests__/buildClaudeSandboxEnv.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, it, expect } from "vitest";

import { buildClaudeSandboxEnv } from "@/lib/board/buildClaudeSandboxEnv";

describe("buildClaudeSandboxEnv", () => {
const boardEnv = { BOARD_URL: "https://api.vercel.com/board", BOARD_TOKEN: "placeholder" };

it("keeps the board's placeholder env", () => {
expect(buildClaudeSandboxEnv(boardEnv)).toMatchObject(boardEnv);
});

it("points Claude Code at AI Gateway with only a placeholder token", () => {
const env = buildClaudeSandboxEnv(boardEnv);
expect(env.ANTHROPIC_BASE_URL).toBe("https://ai-gateway.vercel.sh");
expect(env.ANTHROPIC_AUTH_TOKEN).toBe("brokered-by-sandbox-proxy");
expect(env.ANTHROPIC_API_KEY).toBe("");
expect(env.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC).toBe("1");
});
});
45 changes: 45 additions & 0 deletions lib/board/__tests__/buildClaudeSandboxNetworkPolicy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, it, expect } from "vitest";
import { buildClaudeSandboxNetworkPolicy } from "@/lib/board/buildClaudeSandboxNetworkPolicy";

describe("buildClaudeSandboxNetworkPolicy", () => {
const boardAllow = {
"api.vercel.com": [{ transform: [{ headers: { authorization: "Bearer board" } }] }],
};

it("allows only the board, npm and AI Gateway", () => {
const policy = buildClaudeSandboxNetworkPolicy({ boardAllow, gatewayToken: "gw" });
expect(Object.keys(policy.allow).sort()).toEqual(
["ai-gateway.vercel.sh", "api.vercel.com", "registry.npmjs.org"].sort(),
);
expect(policy.allow["*"]).toBeUndefined();
});

it("keeps the board's own rules untouched", () => {
const policy = buildClaudeSandboxNetworkPolicy({ boardAllow, gatewayToken: "gw" });
expect(policy.allow["api.vercel.com"]).toBe(boardAllow["api.vercel.com"]);
});

it("passes the board's route matchers and refusals through unchanged", () => {
const scoped = {
"api.vercel.com": [
{
match: { path: { startsWith: "/v1/messageboard/boards/b1" } },
transform: [{ headers: { authorization: "Bearer board" } }],
},
{ response: { statusCode: 403 } },
],
};
const policy = buildClaudeSandboxNetworkPolicy({
boardAllow: scoped as never,
gatewayToken: "gw",
});
expect(policy.allow["api.vercel.com"]).toEqual(scoped["api.vercel.com"]);
});

it("adds the gateway credential on the way out instead of inside the sandbox", () => {
const policy = buildClaudeSandboxNetworkPolicy({ boardAllow, gatewayToken: "gw" });
expect(policy.allow["ai-gateway.vercel.sh"]).toEqual([
{ transform: [{ headers: { Authorization: "Bearer gw" } }] },
]);
});
});
25 changes: 25 additions & 0 deletions lib/board/__tests__/buildClaudeSandboxScript.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, it, expect } from "vitest";
import { buildClaudeSandboxScript } from "@/lib/board/buildClaudeSandboxScript";

describe("buildClaudeSandboxScript", () => {
const script = buildClaudeSandboxScript();

it("installs vb and Claude Code without global installs", () => {
expect(script).toContain("npm install --prefix");
expect(script).toContain("@vercel/board@0.52.0");
expect(script).toContain("@anthropic-ai/claude-code");
});

it("claims, renews and settles the task through vb tasks run", () => {
expect(script).toContain('vb tasks run "$TASK_ID"');
});

it("limits Claude to vb commands", () => {
expect(script).toContain('--allowedTools "Bash(vb:*)"');
});

it("reads the task id and prompt from the environment, not interpolated text", () => {
expect(script).toContain('"$CLAUDE_PROMPT"');
expect(script).not.toMatch(/\$\{[^}]*\}/);
});
});
20 changes: 20 additions & 0 deletions lib/board/__tests__/buildClaudeTaskPrompt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, it, expect } from "vitest";
import { buildClaudeTaskPrompt } from "@/lib/board/buildClaudeTaskPrompt";

describe("buildClaudeTaskPrompt", () => {
const prompt = buildClaudeTaskPrompt({ id: "812", channel: "general" });

it("names the task and its channel", () => {
expect(prompt).toContain("#812");
expect(prompt).toContain("general");
});

it("tells Claude to read the task and reply in its thread", () => {
expect(prompt).toContain("vb read 812");
expect(prompt).toContain("vb reply 812");
});

it("does not include the task text itself", () => {
expect(prompt).not.toContain("today's date");
});
});
44 changes: 44 additions & 0 deletions lib/board/__tests__/claudeWatcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, it, expect, vi, beforeEach } from "vitest";

import { claudeWatcher } from "@/lib/board/claudeWatcher";
import { startClaudeForBoardTask } from "@/lib/board/startClaudeForBoardTask";
import { getClaudeTaskToStart } from "@/lib/board/getClaudeTaskToStart";

vi.mock("@/lib/board/startClaudeForBoardTask", () => ({
startClaudeForBoardTask: vi.fn(),
}));
vi.mock("@/lib/board/getTrustedBoardPrincipals", () => ({
getTrustedBoardPrincipals: vi.fn(() => ["vercel:user:trusted"]),
}));
vi.mock("@/lib/board/getClaudeTaskToStart", () => ({
getClaudeTaskToStart: vi.fn(),
}));

describe("claudeWatcher", () => {
const board = { as: vi.fn() } as never;
const event = { type: "task.created" } as never;

beforeEach(() => {
vi.clearAllMocks();
});

it("tails threads labeled agent=claude", () => {
expect(claudeWatcher.tail).toBe("--label agent=claude");
});

it("starts Claude for a task the filter accepts", async () => {
vi.mocked(getClaudeTaskToStart).mockReturnValue({ id: "812", channel: "general" });
await claudeWatcher.run({ event, board });
expect(getClaudeTaskToStart).toHaveBeenCalledWith(event, ["vercel:user:trusted"]);
expect(startClaudeForBoardTask).toHaveBeenCalledWith({
board,
task: { id: "812", channel: "general" },
});
});

it("does nothing for an event the filter rejects", async () => {
vi.mocked(getClaudeTaskToStart).mockReturnValue(null);
await claudeWatcher.run({ event, board });
expect(startClaudeForBoardTask).not.toHaveBeenCalled();
});
});
35 changes: 35 additions & 0 deletions lib/board/__tests__/getAiGatewayCredential.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, it, expect, beforeEach, afterEach } from "vitest";

import { getAiGatewayCredential } from "@/lib/board/getAiGatewayCredential";

describe("getAiGatewayCredential", () => {
const originalKey = process.env.AI_GATEWAY_API_KEY;
const originalOidc = process.env.VERCEL_OIDC_TOKEN;

beforeEach(() => {
delete process.env.AI_GATEWAY_API_KEY;
delete process.env.VERCEL_OIDC_TOKEN;
});

afterEach(() => {
if (originalKey === undefined) delete process.env.AI_GATEWAY_API_KEY;
else process.env.AI_GATEWAY_API_KEY = originalKey;
if (originalOidc === undefined) delete process.env.VERCEL_OIDC_TOKEN;
else process.env.VERCEL_OIDC_TOKEN = originalOidc;
});

it("prefers the AI Gateway API key", () => {
process.env.AI_GATEWAY_API_KEY = "gw-key";
process.env.VERCEL_OIDC_TOKEN = "oidc";
expect(getAiGatewayCredential()).toBe("gw-key");
});

it("falls back to the deployment's OIDC token", () => {
process.env.VERCEL_OIDC_TOKEN = "oidc";
expect(getAiGatewayCredential()).toBe("oidc");
});

it("throws when neither is set", () => {
expect(() => getAiGatewayCredential()).toThrow("No AI Gateway credential");
});
});
27 changes: 27 additions & 0 deletions lib/board/__tests__/getBoardWatchers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, it, expect, afterEach, vi } from "vitest";

import { getBoardWatchers } from "@/lib/board/getBoardWatchers";

vi.mock("@/lib/board/claudeWatcher", () => ({
claudeWatcher: { tail: "--label agent=claude", run: vi.fn() },
}));

describe("getBoardWatchers", () => {
const original = process.env.VERCEL_ENV;

afterEach(() => {
if (original === undefined) delete process.env.VERCEL_ENV;
else process.env.VERCEL_ENV = original;
});

it("registers the claude watcher in production", () => {
process.env.VERCEL_ENV = "production";
expect(Object.keys(getBoardWatchers())).toEqual(["claude"]);
});

it.each(["preview", "development", undefined])("registers nothing in %s", env => {
if (env === undefined) delete process.env.VERCEL_ENV;
else process.env.VERCEL_ENV = env;
expect(getBoardWatchers()).toEqual({});
});
});
111 changes: 111 additions & 0 deletions lib/board/__tests__/getClaudeTaskToStart.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { describe, it, expect } from "vitest";
import { getClaudeTaskToStart } from "@/lib/board/getClaudeTaskToStart";

const TRUSTED = ["vercel:user:trusted"];

const buildEvent = (overrides: Record<string, unknown> = {}, task: Record<string, unknown> = {}) =>
({
cursor: "c1",
type: "task.created",
createdAt: "2026-11-20T00:00:00.000Z",
payload: {},
message: {
id: "812",
re: null,
channel: "general",
text: "what is today's date?",
labels: { agent: "claude" },
task: {
state: "ready",
owner: "vercel:user:trusted",
by: "vercel:user:trusted",
for: null,
needs: [],
epoch: 0,
claim: null,
blocked: null,
result: null,
reason: null,
...task,
},
},
...overrides,
}) as never;

describe("getClaudeTaskToStart", () => {
describe("starts Claude", () => {
it.each(["task.created", "task.ready", "task.reopened"])(
"for %s on a ready, unclaimed task from a trusted owner",
type => {
expect(getClaudeTaskToStart(buildEvent({ type }), TRUSTED)).toEqual({
id: "812",
channel: "general",
});
},
);

it("when agent=claude is set on an existing ready task", () => {
const event = buildEvent({
type: "labels.changed",
payload: { set: { agent: "claude" }, unset: [] },
});
expect(getClaudeTaskToStart(event, TRUSTED)).toEqual({ id: "812", channel: "general" });
});
});

describe("ignores the event", () => {
it("when the owner is not trusted", () => {
const event = buildEvent({}, { owner: "vercel:user:stranger" });
expect(getClaudeTaskToStart(event, TRUSTED)).toBeNull();
});

it("when no owner is trusted", () => {
expect(getClaudeTaskToStart(buildEvent(), [])).toBeNull();
});

it("for other event types", () => {
for (const type of ["message.posted", "task.claimed", "task.completed", "reaction.added"]) {
expect(getClaudeTaskToStart(buildEvent({ type }), TRUSTED)).toBeNull();
}
});

it("for a label change that does not set agent=claude", () => {
const event = buildEvent({
type: "labels.changed",
payload: { set: { state: "done" }, unset: [] },
});
expect(getClaudeTaskToStart(event, TRUSTED)).toBeNull();
});

it("when the thread no longer carries agent=claude", () => {
const event = buildEvent();
(event as { message: { labels: Record<string, string> } }).message.labels = {};
expect(getClaudeTaskToStart(event, TRUSTED)).toBeNull();
});

it("for a reply rather than a root", () => {
const event = buildEvent();
(event as { message: { re: string | null } }).message.re = "800";
expect(getClaudeTaskToStart(event, TRUSTED)).toBeNull();
});

it("when the message is not a task", () => {
const event = buildEvent();
delete (event as { message: { task?: unknown } }).message.task;
expect(getClaudeTaskToStart(event, TRUSTED)).toBeNull();
});

it("when the task is not ready", () => {
expect(getClaudeTaskToStart(buildEvent({}, { state: "waiting" }), TRUSTED)).toBeNull();
});

it("when the task is already claimed", () => {
const event = buildEvent({}, { claim: { actor: "x", until: "2026-11-20T00:05:00.000Z" } });
expect(getClaudeTaskToStart(event, TRUSTED)).toBeNull();
});

it("when the event has no message", () => {
expect(getClaudeTaskToStart(buildEvent({ message: undefined }), TRUSTED)).toBeNull();
});
});
});
21 changes: 21 additions & 0 deletions lib/board/__tests__/getTrustedBoardPrincipals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, it, expect, afterEach } from "vitest";
import { getTrustedBoardPrincipals } from "@/lib/board/getTrustedBoardPrincipals";

describe("getTrustedBoardPrincipals", () => {
const original = process.env.BOARD_TRUSTED_PRINCIPALS;

afterEach(() => {
if (original === undefined) delete process.env.BOARD_TRUSTED_PRINCIPALS;
else process.env.BOARD_TRUSTED_PRINCIPALS = original;
});

it("returns no principals when the variable is unset", () => {
delete process.env.BOARD_TRUSTED_PRINCIPALS;
expect(getTrustedBoardPrincipals()).toEqual([]);
});

it("splits, trims and drops empty entries", () => {
process.env.BOARD_TRUSTED_PRINCIPALS = " vercel:user:abc , ,vercel:user:def ";
expect(getTrustedBoardPrincipals()).toEqual(["vercel:user:abc", "vercel:user:def"]);
});
});
Loading
Loading