Files
coucou/brain/tests/cloud.test.ts
T
Claude 5a3dce5121 cloud: the Coucou Cloud engine, V0 (your own Anthropic key)
A Cloudflare Worker (cloud/) that runs the PR Reviewer while the Mac is
off: a GitHub App webhook for pull_request starts a Claude Managed Agents
session on the subscriber's own Anthropic key, on the PR's head commit,
with the diff in the prompt and a spend ceiling; a cron collects the
finished sessions every minute; the review waits until the click posts
it as a comment. D1 keeps the GitHub id and login, the hash of the Mac's
token, the Anthropic key sealed with AES-GCM, the Cloud Mochis and each
run's result. Never the code.

The prompt and the Managed Agents requests move to two modules with no
Node API, shared by the brain and the Worker: brain/src/prompt-core.ts
(buildRepoPrompt, languageRule, repoSystem) and brain/src/cloud-core.ts
(the client, the diff, agent/environment/session params, summarize,
looksLikeReport, withoutNotes). brain cloud try now uses them too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LJm3SRL9k3gBPaVtfPhwcT
2026-11-30 01:47:49 +00:00

95 lines
5.4 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "vitest";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import type { BrainConfig } from "../src/config.js";
import { addPreset } from "../src/mochis.js";
import type { Sh } from "../src/agents.js";
import { filterDiff, summarize, tryOnCloud, type Api } from "../src/cloud.js";
let dir: string;
let repo: string;
let config: BrainConfig;
beforeEach(() => {
dir = fs.mkdtempSync(path.join(os.tmpdir(), "cloud"));
repo = path.join(dir, "repo");
fs.mkdirSync(path.join(repo, ".git"), { recursive: true });
config = {
brainPath: path.join(dir, "brain"), claudeProjectsPath: path.join(dir, "none"), repoPaths: [],
chunkModel: "sonnet", mergeModel: "opus", maxChunkTokens: 100000, language: "fr", repoAliases: {},
};
});
afterEach(() => fs.rmSync(dir, { recursive: true, force: true }));
const PATCH = [
"diff --git a/app/Main.kt b/app/Main.kt\n--- a/app/Main.kt\n+++ b/app/Main.kt\n@@ -1 +1 @@\n-val a = 1\n+val a = 2\n",
"diff --git a/package-lock.json b/package-lock.json\n--- a/package-lock.json\n+++ b/package-lock.json\n@@ -1 +1 @@\n-{}\n+{\"big\":true}\n",
"diff --git a/NotchBuddy/Resources/Localizable.xcstrings b/NotchBuddy/Resources/Localizable.xcstrings\n@@ -1 +1 @@\n-x\n+y\n",
].join("");
describe("Coucou Cloud prototype", () => {
it("keeps the code in the diff and leaves generated files out", () => {
const d = filterDiff(PATCH);
expect(d).toContain("+val a = 2");
expect(d).not.toContain("package-lock.json");
expect(d).not.toContain("xcstrings");
});
it("reads a finished session: last message, cost in cents, model calls and tools", () => {
const r = summarize(
{ status: "idle", usage: { list_cost: { amount: "37", currency: "USD" }, active_seconds: 140 }, stats: { duration_seconds: 160 } },
[
{ type: "agent.tool_use", name: "bash" }, { type: "span.model_request_end" },
{ type: "agent.message", content: [{ type: "text", text: "Je regarde." }] }, { type: "span.model_request_end" },
{ type: "agent.message", content: [{ type: "text", text: "## Summary\n" }, { type: "text", text: "Fine." }] },
{ type: "session.status_idle", stop_reason: { type: "end_turn" } },
],
);
expect(r).toMatchObject({ status: "idle", stopReason: "end_turn", text: "## Summary\nFine.", costUsd: 0.37, activeSeconds: 140, turns: 2, tools: ["bash"] });
});
it("starts a session on the PR's commit, with the diff in the first message and a budget", async () => {
const m = addPreset(config, "pr-reviewer", { repo });
const sh: Sh = (cmd, args) => {
const line = [cmd, ...args].join(" ");
if (line.startsWith("git remote -v")) return { status: 0, stdout: "origin\thttps://github.com/Louis-CFM/coucou.git (fetch)\n", stderr: "" };
if (line.startsWith("gh pr view")) return { status: 0, stderr: "", stdout: JSON.stringify({ number: 1148, title: "Android notif", body: "", url: "u", headRefOid: "abc123def4567890", baseRefName: "main" }) };
if (line.includes("--name-only")) return { status: 0, stdout: "app/Main.kt\npackage-lock.json\n", stderr: "" };
if (line.startsWith("gh pr diff")) return { status: 0, stdout: PATCH, stderr: "" };
return { status: 0, stdout: "", stderr: "" };
};
const calls: { method: string; route: string; body?: any }[] = [];
let polls = 0;
const api: Api = async (method, route, body) => {
calls.push({ method, route, body });
if (route === "/v1/agents") return { id: "agent_1" };
if (route === "/v1/environments") return { id: "env_1" };
if (route === "/v1/sessions") return { id: "sesn_1", status: "running" };
if (route === "/v1/sessions/sesn_1") return ++polls < 2 ? { status: "running" } : { status: "idle", usage: { list_cost: { amount: "42" }, active_seconds: 90 } };
if (route.startsWith("/v1/sessions/sesn_1/events")) return { data: [{ type: "agent.message", content: [{ type: "text", text: "## Summary\nOk." }] }], next_page: null };
return {};
};
const r = await tryOnCloud(config, m.id, { pr: 1148, api, sh, pollMs: 1 });
expect(r).toMatchObject({ sessionId: "sesn_1", text: "## Summary\nOk.", costUsd: 0.42 });
const create = calls.find(c => c.route === "/v1/sessions")!.body;
expect(create.budget).toEqual({ type: "limit", max_list_cost: { amount: "100", currency: "USD" } });
expect(create.resources[0]).toEqual({ type: "github_repository", url: "https://github.com/Louis-CFM/coucou", mount_path: "/workspace/repo", checkout: { type: "commit", sha: "abc123def4567890" } });
const prompt: string = create.initial_events[0].content[0].text;
expect(prompt).toContain("+val a = 2");
expect(prompt).not.toContain('"big":true');
expect(prompt).toContain("- package-lock.json");
expect(prompt).toContain("git fetch origin pull/1148/head");
const agent = calls.find(c => c.route === "/v1/agents")!.body;
expect(agent.model).toBe("claude-sonnet-5-5");
expect(agent.tools[0].default_config.permission_policy).toEqual({ type: "always_allow" });
expect(calls.some(c => c.route === "/v1/sessions/sesn_1/archive")).toBe(true);
// the agent and the environment are made once, then reused
calls.length = 0; polls = 0;
await tryOnCloud(config, m.id, { pr: 1148, api, sh, pollMs: 1 });
expect(calls.some(c => c.route === "/v1/agents" || c.route === "/v1/environments")).toBe(false);
}, 30_000);
});