Auto-approve all (全部自动同意) ships in agent-console 1.3.1, split out of #727 (qingnian108).
The mode is off by default. In it, Codex runs with its sandbox off and Claude Code skips its checks. Cursor, OpenCode and dsh requests are answered allow-once by the console, never allow_always or acceptForSession. Each auto answer is saved with `auto: true` before the Agent goes on, and a banner shows while the mode is on. Plan confirmations and questions still ask. A 1.2 默认 of 全部放行 is not silently upgraded to real auto-approve. Pi keeps the `default` mode id ("Pi never asks"), so switching Agents before the first message never carries auto-approve into an Agent the user didn't pick.
Reviewed at 7e55c87ca merged onto main 20b89389:
- Linux (WSL2 Ubuntu 24.04): root `bun test packages/agent-console` 6/6; `bun scripts/check.mjs` OK; `npm run check` OK; vitest 461 passed / 17 skipped; `verify:build` 5/5 identical.
- Windows 11 (Edge): `e2e:v1` 29/29.
- Linux e2e stops at scenario 5 (steer timeout). It does the same on main 1.3.0, so it was filed as #753.
- Broken by hand:
- Pi's modeMap back to bypassPermissions: 2 tests red.
- The keeps line shown for every Agent: 1 test red.
- Pi's label branch removed: 1 test red.
- An earlier round at 049449612 on macOS checked the daemon behaviour with probes. This version is published from the dist branch; the package isn't on npm.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
// 1.3 全部自动同意 upgrade (#727 review 3): a 1.2 全部放行 never becomes real auto-approval silently for the Agents it
|
||||
// did nothing for in 1.2 (Cursor / OpenCode / dsh); Claude / Codex keep it (it meant the same there).
|
||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { ThreadsStore } from "../../daemon/src/store/threads.js";
|
||||
import { SettingsStore, parseSettingsPatch } from "../../daemon/src/store/settings.js";
|
||||
import {
|
||||
migrateAutoApprove13, guardLegacyMode, endLegacyDefault, LEGACY_NO_AUTO, MIGRATED_NOTICE_CODE, upgradeNotice, UPGRADE_NOTICE_CODE,
|
||||
} from "../../daemon/src/migrate/autoApprove13.js";
|
||||
|
||||
let dir: string;
|
||||
beforeEach(() => { dir = mkdtempSync(join(tmpdir(), "ac-up-")); });
|
||||
afterEach(() => rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 }));
|
||||
|
||||
const stores = () => ({ settings: new SettingsStore(dir), threads: new ThreadsStore(dir) });
|
||||
|
||||
describe("migrateAutoApprove13", () => {
|
||||
it("an upgraded 1.2 user: Cursor/OpenCode/dsh conversations in bypass go back to 每次询问 with a notice; others keep it", async () => {
|
||||
writeFileSync(join(dir, "settings.json"), JSON.stringify({ defaultMode: "bypassPermissions", v1Migrated: true }));
|
||||
const s = stores();
|
||||
const mk = (provider: string, mode: "bypassPermissions" | "default") => s.threads.create({ projectId: "p", provider, mode });
|
||||
const cur = await mk("cursor", "bypassPermissions");
|
||||
const oc = await mk("opencode", "bypassPermissions");
|
||||
const dsh = await mk("dsh", "bypassPermissions");
|
||||
const cl = await mk("claude", "bypassPermissions");
|
||||
const cx = await mk("codex", "bypassPermissions");
|
||||
const curDef = await mk("cursor", "default");
|
||||
const r = await migrateAutoApprove13(s);
|
||||
expect(r.skipped).toBe(false);
|
||||
expect(r.legacyDefault).toBe(true);
|
||||
expect(r.threads.sort()).toEqual([cur.id, oc.id, dsh.id].sort());
|
||||
for (const t of [cur, oc, dsh]) {
|
||||
expect((await s.threads.get(t.id))?.mode).toBe("default");
|
||||
const evs = await s.threads.readEvents(t.id, 0, 10);
|
||||
expect(evs.map((e) => (e as { code?: string }).code)).toEqual([MIGRATED_NOTICE_CODE]);
|
||||
}
|
||||
for (const t of [cl, cx]) expect((await s.threads.get(t.id))?.mode).toBe("bypassPermissions");
|
||||
expect((await s.threads.readEvents(curDef.id, 0, 10)).length).toBe(0);
|
||||
// runs once
|
||||
const again = await migrateAutoApprove13(stores());
|
||||
expect(again.skipped).toBe(true);
|
||||
});
|
||||
|
||||
it("a fresh install (or a default that is not 全部放行) has nothing to guard", async () => {
|
||||
const s = stores();
|
||||
const r = await migrateAutoApprove13(s);
|
||||
expect(r.legacyDefault).toBe(false);
|
||||
expect(await guardLegacyMode(s.settings, "cursor", "bypassPermissions")).toEqual({ mode: "bypassPermissions", notice: false });
|
||||
});
|
||||
});
|
||||
|
||||
describe("guardLegacyMode", () => {
|
||||
it("new Cursor/OpenCode/dsh conversations from a 1.2 全部放行 default start in 每次询问; told once; Claude unaffected", async () => {
|
||||
writeFileSync(join(dir, "settings.json"), JSON.stringify({ defaultMode: "bypassPermissions" }));
|
||||
const s = stores();
|
||||
await migrateAutoApprove13(s);
|
||||
expect(await guardLegacyMode(s.settings, "claude", "bypassPermissions")).toEqual({ mode: "bypassPermissions", notice: false });
|
||||
expect(await guardLegacyMode(s.settings, "codex", "bypassPermissions")).toEqual({ mode: "bypassPermissions", notice: false });
|
||||
expect(await guardLegacyMode(s.settings, "cursor", "bypassPermissions")).toEqual({ mode: "default", notice: true });
|
||||
expect(await guardLegacyMode(s.settings, "opencode", "bypassPermissions")).toEqual({ mode: "default", notice: false });
|
||||
expect(await guardLegacyMode(s.settings, "dsh", "bypassPermissions")).toEqual({ mode: "default", notice: false });
|
||||
expect(await guardLegacyMode(s.settings, "cursor", "plan")).toEqual({ mode: "plan", notice: false });
|
||||
// survives a daemon restart
|
||||
expect(await guardLegacyMode(new SettingsStore(dir), "cursor", "bypassPermissions")).toEqual({ mode: "default", notice: false });
|
||||
// the user picks the default again in Settings (a 1.3 decision): from then on it is honoured
|
||||
await endLegacyDefault(s.settings);
|
||||
expect(await guardLegacyMode(s.settings, "cursor", "bypassPermissions")).toEqual({ mode: "bypassPermissions", notice: false });
|
||||
});
|
||||
|
||||
it("the guard state can't be set from the page, and the notice has its code", () => {
|
||||
expect(parseSettingsPatch({ autoApproveUpgrade: { done: true, legacyDefault: false, noticed: true } }).ok).toBe(false);
|
||||
expect(upgradeNotice().code).toBe(UPGRADE_NOTICE_CODE);
|
||||
expect([...LEGACY_NO_AUTO].sort()).toEqual(["cursor", "dsh", "opencode"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,249 @@
|
||||
// 1.3 全部自动同意: per-Agent mode mapping, the allow-once pick, and the daemon auto-approve path (never when the mode is off).
|
||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { AgentManager } from "../../daemon/src/agents/manager.js";
|
||||
import { ThreadsStore } from "../../daemon/src/store/threads.js";
|
||||
import { ProjectsStore } from "../../daemon/src/store/projects.js";
|
||||
import { SettingsStore, parseSettingsPatch } from "../../daemon/src/store/settings.js";
|
||||
import { AttachmentStore } from "../../daemon/src/store/attachments.js";
|
||||
import { Gateway } from "../../daemon/src/ws/gateway.js";
|
||||
import { initProviders, getProvider } from "../../daemon/src/providers/registry.js";
|
||||
import { autoApproveOption, pickAllowOnce, FULL_AUTO_NATIVE } from "../../daemon/src/providers/autoApprove.js";
|
||||
import { acpConfigForEntry } from "../../daemon/src/providers/acp/specs.js";
|
||||
import { createAcpProvider, cursorAcpCapabilities } from "../../daemon/src/providers/acp/provider.js";
|
||||
import { catalogById } from "../../daemon/src/providers/catalog.js";
|
||||
import { clampMode } from "../../daemon/src/providers/modes.js";
|
||||
import { clearProviderCache } from "../../daemon/src/providers/infoCache.js";
|
||||
import { claudeArgs } from "../../daemon/src/providers/claude/cli.js";
|
||||
import type { Config } from "../../daemon/src/config.js";
|
||||
import type { Mode } from "../../daemon/src/providers/types.js";
|
||||
|
||||
process.env.AGENT_CONSOLE_MOCK = "1";
|
||||
|
||||
const ACP_OPTS = [
|
||||
{ id: "allow-once", label: "Allow once", kind: "allow_once" },
|
||||
{ id: "allow-always", label: "Allow always", kind: "allow_always" },
|
||||
{ id: "reject-once", label: "Reject", kind: "reject_once" },
|
||||
];
|
||||
|
||||
describe("pickAllowOnce", () => {
|
||||
it("prefers allow_once over allow_always (no permanent rule in the user's config)", () => {
|
||||
expect(pickAllowOnce(ACP_OPTS)?.id).toBe("allow-once");
|
||||
expect(pickAllowOnce([...ACP_OPTS].reverse())?.id).toBe("allow-once");
|
||||
});
|
||||
it("Codex / Claude / OpenCode option sets", () => {
|
||||
expect(pickAllowOnce([{ id: "accept", kind: "allow_once" }, { id: "acceptForSession", kind: "allow_always" }, { id: "decline", kind: "reject_once" }])?.id).toBe("accept");
|
||||
expect(pickAllowOnce([{ id: "allow", kind: "allow_once" }, { id: "allow_always", kind: "allow_always" }, { id: "deny", kind: "reject_once" }])?.id).toBe("allow");
|
||||
expect(pickAllowOnce([{ id: "once", kind: "allow_once" }, { id: "always", kind: "allow_always" }, { id: "reject", kind: "reject_once" }])?.id).toBe("once");
|
||||
});
|
||||
it("without kinds: an allow-looking option that is not 'always', never a reject", () => {
|
||||
expect(pickAllowOnce([{ id: "deny" }, { id: "always_allow" }, { id: "approve" }])?.id).toBe("approve");
|
||||
expect(pickAllowOnce([{ id: "reject" }, { id: "cancel" }])).toBeNull();
|
||||
});
|
||||
it("never allow_always: with nothing narrower the card stays for the user", () => {
|
||||
expect(pickAllowOnce([{ id: "a", kind: "allow_always" }, { id: "r", kind: "reject_once" }])).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("autoApproveOption", () => {
|
||||
const req = { kind: "execute", options: ACP_OPTS };
|
||||
it("never fires when the mode is not full-auto", () => {
|
||||
for (const m of ["default", "plan", "acceptEdits", "ask", null, undefined] as (Mode | null | undefined)[]) {
|
||||
for (const p of Object.keys(FULL_AUTO_NATIVE)) expect(autoApproveOption(m, p, req)).toBeNull();
|
||||
}
|
||||
});
|
||||
it("fires for every Agent that lets the daemon answer", () => {
|
||||
for (const p of ["claude", "codex", "cursor", "copilot", "opencode", "devin", "dsh", "mock"]) {
|
||||
expect(autoApproveOption("bypassPermissions", p, req)?.id).toBe("allow-once");
|
||||
}
|
||||
});
|
||||
it("leaves Pi extension dialogs, plan approvals and mode switches to the user", () => {
|
||||
expect(autoApproveOption("bypassPermissions", "pi", req)).toBeNull();
|
||||
expect(autoApproveOption("bypassPermissions", "claude", { kind: "plan", options: [{ id: "implement", kind: "allow_once" }] })).toBeNull();
|
||||
expect(autoApproveOption("bypassPermissions", "cursor", { kind: "switch_mode", options: ACP_OPTS })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("mode mapping per Agent", () => {
|
||||
const acp = (id: string) => {
|
||||
const entry = catalogById(id);
|
||||
expect(entry).toBeTruthy();
|
||||
return createAcpProvider(acpConfigForEntry(entry!, "0.0.0")!);
|
||||
};
|
||||
it("every Agent that has a full-auto choice offers 全部自动同意 (bypassPermissions)", () => {
|
||||
for (const id of ["cursor", "devin", "opencode", "copilot", "dsh"]) {
|
||||
expect(acp(id).capabilities.modes, id).toContain("bypassPermissions");
|
||||
}
|
||||
});
|
||||
it("Cursor: Agent / 全部自动同意 / Plan / Ask", () => {
|
||||
expect(cursorAcpCapabilities.modes).toEqual(["default", "bypassPermissions", "plan", "ask"]);
|
||||
});
|
||||
it("OpenCode: full-auto runs in build; Copilot: allow-all; Devin: bypass", () => {
|
||||
const oc = acpConfigForEntry(catalogById("opencode")!, "0")!;
|
||||
expect(oc.modeMap?.bypassPermissions).toBe("build");
|
||||
expect(acpConfigForEntry(catalogById("copilot")!, "0")!.modeMap?.bypassPermissions).toBe("allow-all");
|
||||
expect(acpConfigForEntry(catalogById("devin")!, "0")!.modeMap?.bypassPermissions).toBe("bypass");
|
||||
});
|
||||
it("Pi persists its single mode as default and sends no ACP mode override", () => {
|
||||
const pi = acp("pi");
|
||||
const config = acpConfigForEntry(catalogById("pi")!, "0")!;
|
||||
expect(pi.capabilities.modes).toEqual(["default"]);
|
||||
expect(config.modeMap).toEqual({ default: "" });
|
||||
expect(clampMode(pi.capabilities.modes, "default")).toBe("default");
|
||||
// Full-auto from another Agent cannot become Pi's persisted mode either.
|
||||
expect(clampMode(pi.capabilities.modes, "bypassPermissions")).toBe("default");
|
||||
});
|
||||
it("Claude: bypassPermissions goes to --permission-mode and is allowed at launch", () => {
|
||||
const a = claudeArgs({ model: null, permissionMode: "bypassPermissions", effort: null, resume: null });
|
||||
expect(a.join(" ")).toContain("--permission-mode bypassPermissions");
|
||||
expect(a).toContain("--allow-dangerously-skip-permissions");
|
||||
expect(claudeArgs({ model: null, permissionMode: "default", effort: null, resume: null })).not.toContain("--allow-dangerously-skip-permissions");
|
||||
});
|
||||
it("the unused warning flag is refused; the real default-mode setting is still accepted", () => {
|
||||
expect(parseSettingsPatch({ autoApproveWarned: true }).ok).toBe(false);
|
||||
expect(parseSettingsPatch({ defaultMode: "bypassPermissions" }).ok).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// ---- the daemon path, with the mock Agent (/perm asks once for "npm test") ----
|
||||
const cfg: Config = { port: 0, dataDir: "", token: "t", version: "0.0.0", idleExitMinutes: 60, buildStamp: "0" };
|
||||
let dir: string, threads: ThreadsStore, manager: AgentManager, threadId: string, projectId: string;
|
||||
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
||||
const waitFor = async (fn: () => boolean | Promise<boolean>, ms = 10000) => {
|
||||
const t0 = Date.now();
|
||||
while (Date.now() - t0 < ms) { if (await fn()) return; await sleep(30); }
|
||||
throw new Error("waitFor timeout");
|
||||
};
|
||||
const events = () => threads.readEvents(threadId, 0, 50000);
|
||||
const idle = () => waitFor(async () => (await threads.get(threadId))?.status === "idle", 15000);
|
||||
|
||||
describe("AgentManager provider switching", () => {
|
||||
beforeEach(async () => {
|
||||
dir = mkdtempSync(join(tmpdir(), "ac-auto-switch-"));
|
||||
threads = new ThreadsStore(dir);
|
||||
const projects = new ProjectsStore(dir);
|
||||
manager = new AgentManager(new Gateway(cfg), threads, projects, new SettingsStore(dir), new AttachmentStore(join(dir, "att")));
|
||||
projectId = (await projects.create(dir)).id;
|
||||
});
|
||||
afterEach(async () => {
|
||||
await manager.shutdown();
|
||||
rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 });
|
||||
});
|
||||
|
||||
it("Pi's default id does not arm another Agent, including on a round trip", async () => {
|
||||
initProviders();
|
||||
const agents = ["pi", "codex", "claude", "cursor", "opencode", "dsh", "copilot", "devin"];
|
||||
for (const id of agents) {
|
||||
getProvider(id)!.detect = async () => ({ available: true, bin: "test-double" });
|
||||
clearProviderCache(id);
|
||||
}
|
||||
const pi = getProvider("pi")!;
|
||||
threadId = (await threads.create({
|
||||
projectId,
|
||||
provider: "pi",
|
||||
mode: clampMode(pi.capabilities.modes, "default"),
|
||||
})).id;
|
||||
expect((await threads.get(threadId))?.mode).toBe("default");
|
||||
|
||||
for (const id of agents.slice(1)) {
|
||||
await manager.setProvider(threadId, "pi");
|
||||
await manager.setProvider(threadId, id);
|
||||
expect((await threads.get(threadId))?.mode, `Pi → ${id}`).toBe("default");
|
||||
}
|
||||
|
||||
await manager.setProvider(threadId, "pi");
|
||||
expect((await threads.get(threadId))?.mode).toBe("default");
|
||||
await manager.setProvider(threadId, "codex");
|
||||
expect((await threads.get(threadId))?.mode).toBe("default");
|
||||
});
|
||||
});
|
||||
|
||||
describe("AgentManager auto-approve", () => {
|
||||
beforeEach(async () => {
|
||||
dir = mkdtempSync(join(tmpdir(), "ac-auto-"));
|
||||
threads = new ThreadsStore(dir);
|
||||
const projects = new ProjectsStore(dir);
|
||||
initProviders();
|
||||
manager = new AgentManager(new Gateway(cfg), threads, projects, new SettingsStore(dir), new AttachmentStore(join(dir, "att")));
|
||||
const proj = await projects.create(dir);
|
||||
threadId = (await threads.create({ projectId: proj.id, provider: "mock" })).id;
|
||||
});
|
||||
afterEach(async () => { await manager.shutdown(); await sleep(50); rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 }); });
|
||||
|
||||
it("mode on: the request is answered allow-once by the daemon, and the approval is persisted (auto) in the timeline", async () => {
|
||||
await manager.setMode(threadId, "bypassPermissions");
|
||||
await manager.send(threadId, { text: "/perm" });
|
||||
await idle();
|
||||
const evs = await events();
|
||||
// #727 review: no silent approvals. The request (what was approved) and its answer are both in the timeline, marked auto
|
||||
const iReq = evs.findIndex((e) => e.type === "permission_request");
|
||||
const iRes = evs.findIndex((e) => e.type === "permission_resolved");
|
||||
const iOk = evs.findIndex((e) => e.type === "tool_call" && (e as { status?: string }).status === "ok");
|
||||
expect(iReq).toBeGreaterThanOrEqual(0);
|
||||
const req = evs[iReq] as { auto?: boolean; title?: string; requestId?: string; detail?: { command?: string } };
|
||||
expect(req.auto).toBe(true);
|
||||
expect(req.title).toBeTruthy();
|
||||
const res = evs[iRes] as { auto?: boolean; optionId?: string; requestId?: string };
|
||||
expect(res).toMatchObject({ auto: true, optionId: "accept", requestId: req.requestId });
|
||||
// written before the Agent was told to go on
|
||||
expect(iRes).toBeGreaterThan(iReq);
|
||||
expect(iOk).toBeGreaterThan(iRes);
|
||||
expect(manager.runtimeInfo(threadId)?.pending ?? []).toHaveLength(0);
|
||||
}, 20000);
|
||||
|
||||
it("mode off: nothing is answered for the user, the card waits", async () => {
|
||||
await manager.send(threadId, { text: "/perm" });
|
||||
await waitFor(async () => (await events()).some((e) => e.type === "permission_request"));
|
||||
await sleep(400);
|
||||
const evs = await events();
|
||||
expect(evs.some((e) => e.type === "permission_resolved")).toBe(false);
|
||||
expect(evs.some((e) => e.type === "tool_call" && (e as { status?: string }).status === "ok")).toBe(false);
|
||||
expect((await threads.get(threadId))?.status).toBe("running");
|
||||
await manager.interrupt(threadId);
|
||||
await idle();
|
||||
}, 20000);
|
||||
|
||||
it("acceptEdits / plan are not full-auto either", async () => {
|
||||
await manager.setMode(threadId, "acceptEdits");
|
||||
await manager.send(threadId, { text: "/perm" });
|
||||
await waitFor(async () => (await events()).some((e) => e.type === "permission_request"));
|
||||
await sleep(300);
|
||||
expect((await events()).some((e) => e.type === "permission_resolved")).toBe(false);
|
||||
await manager.interrupt(threadId);
|
||||
await idle();
|
||||
}, 20000);
|
||||
|
||||
it("a card already waiting is approved (allow once, marked auto) when the mode is switched on", async () => {
|
||||
await manager.send(threadId, { text: "/perm" });
|
||||
await waitFor(async () => (await events()).some((e) => e.type === "permission_request"));
|
||||
await manager.setMode(threadId, "bypassPermissions");
|
||||
await idle();
|
||||
const evs = await events();
|
||||
const res = evs.find((e) => e.type === "permission_resolved") as { optionId?: string; auto?: boolean } | undefined;
|
||||
expect(res?.optionId).toBe("accept");
|
||||
expect(res?.auto).toBe(true);
|
||||
expect(evs.some((e) => e.type === "tool_call" && (e as { status?: string }).status === "ok")).toBe(true);
|
||||
}, 20000);
|
||||
|
||||
it("switching to another mode does not approve a waiting card", async () => {
|
||||
await manager.send(threadId, { text: "/perm" });
|
||||
await waitFor(async () => (await events()).some((e) => e.type === "permission_request"));
|
||||
await manager.setMode(threadId, "acceptEdits");
|
||||
await sleep(300);
|
||||
expect((await events()).some((e) => e.type === "permission_resolved")).toBe(false);
|
||||
await manager.interrupt(threadId);
|
||||
await idle();
|
||||
}, 20000);
|
||||
|
||||
it("a plan approval is never auto-answered", async () => {
|
||||
await manager.setMode(threadId, "bypassPermissions");
|
||||
await manager.send(threadId, { text: "/plan" });
|
||||
await waitFor(async () => (await events()).some((e) => e.type === "permission_request"));
|
||||
await sleep(300);
|
||||
expect((await events()).some((e) => e.type === "permission_resolved")).toBe(false);
|
||||
await manager.interrupt(threadId);
|
||||
await idle();
|
||||
}, 20000);
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
// 1.3 全部自动同意 (#727 review item 2): auto-approvals are in the timeline and shown as one quiet foldable row per run
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { foldTimeline } from "../../web/src/timeline/fold.js";
|
||||
import { collectAutoApproved, approvalTarget, approvalChoice } from "../../web/src/timeline/autoApproved.js";
|
||||
import { reduceTimeline, type DisplayItem } from "../../web/src/timeline/reducer.js";
|
||||
|
||||
type Ev = Record<string, unknown>;
|
||||
let seq = 0;
|
||||
const tl = (evs: Ev[]) => evs.map((e) => ({ seq: ++seq, ts: 0, threadId: "t", turnId: "u1", ...e })) as never[];
|
||||
const tool = (itemId: string, kind: string, status: string): Ev => ({ type: "tool_call", itemId, kind, title: `${kind} ${itemId}`, status });
|
||||
const opts = [{ id: "once", label: "允许一次", kind: "allow_once" }, { id: "always", label: "总是允许", kind: "allow_always" }, { id: "no", label: "拒绝", kind: "reject_once" }];
|
||||
const autoReq = (id: string, command: string): Ev[] => [
|
||||
{ type: "permission_request", requestId: id, title: `运行 ${command}`, kind: "tool", detail: { command }, options: opts, auto: true },
|
||||
{ type: "permission_resolved", requestId: id, optionId: "once", auto: true },
|
||||
];
|
||||
const show = (evs: Ev[], status = "idle") => foldTimeline(collectAutoApproved(reduceTimeline(tl(evs), { runtimeStatus: status }).items));
|
||||
const kinds = (items: DisplayItem[]) => items.map((i) => i.kind);
|
||||
|
||||
describe("auto-approved trace", () => {
|
||||
it("a run's auto approvals become one row after the run, and don't break the tool summary", () => {
|
||||
const items = show([
|
||||
{ type: "user_message", text: "跑测试" },
|
||||
tool("a", "bash", "running"), ...autoReq("r1", "npm test"), tool("a", "bash", "ok"),
|
||||
tool("b", "bash", "running"), ...autoReq("r2", "git status"), tool("b", "bash", "ok"),
|
||||
{ type: "assistant_message", itemId: "m", text: "好了" },
|
||||
]);
|
||||
expect(kinds(items)).toEqual(["user", "toolgroup", "autoperm", "assistant"]);
|
||||
const g = items[1] as Extract<DisplayItem, { kind: "toolgroup" }>;
|
||||
expect(g.steps.map((s) => s.itemId)).toEqual(["a", "b"]);
|
||||
const a = items[2] as Extract<DisplayItem, { kind: "autoperm" }>;
|
||||
expect(a.items.map(approvalTarget)).toEqual(["npm test", "git status"]);
|
||||
expect(a.items.map(approvalChoice)).toEqual(["允许一次", "允许一次"]);
|
||||
expect(a.items.every((p) => p.status === "resolved" && p.auto)).toBe(true);
|
||||
});
|
||||
|
||||
it("a card the user answered stays a normal card; a card approved when the mode was switched on joins the row", () => {
|
||||
const items = show([
|
||||
tool("a", "bash", "running"),
|
||||
{ type: "permission_request", requestId: "u1", title: "运行 rm x", kind: "tool", detail: { command: "rm x" }, options: opts },
|
||||
{ type: "permission_resolved", requestId: "u1", optionId: "once" },
|
||||
tool("b", "bash", "running"),
|
||||
{ type: "permission_request", requestId: "w1", title: "运行 ls", kind: "tool", detail: { command: "ls" }, options: opts },
|
||||
{ type: "permission_resolved", requestId: "w1", optionId: "once", auto: true },
|
||||
]);
|
||||
expect(kinds(items)).toEqual(["toolgroup", "permission", "toolgroup", "autoperm"]);
|
||||
expect((items[3] as Extract<DisplayItem, { kind: "autoperm" }>).items[0]!.requestId).toBe("w1");
|
||||
});
|
||||
|
||||
it("an auto request is never a pending card with buttons, even before its answer arrives", () => {
|
||||
const items = show([tool("a", "bash", "running"), autoReq("r1", "make")[0]!], "running");
|
||||
expect(kinds(items)).toEqual(["toolgroup", "autoperm"]);
|
||||
});
|
||||
|
||||
it("nothing auto: the items are passed through untouched", () => {
|
||||
const base = reduceTimeline(tl([{ type: "user_message", text: "x" }, tool("a", "read", "ok")]), { runtimeStatus: "idle" }).items;
|
||||
expect(collectAutoApproved(base)).toBe(base);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user