643 lines · 17.2 KB · TypeScript
RawDownload
import { test, expect } from "bun:test"import { tmpdir } from "../fixture/fixture"import { Instance } from "../../src/project/instance"import { Agent } from "../../src/agent/agent"import { PermissionNext } from "../../src/permission/next" // Helper to evaluate permission for a tool with wildcard patternfunction evalPerm(agent: Agent.Info | undefined, permission: string): PermissionNext.Action | undefined {  if (!agent) return undefined  return PermissionNext.evaluate(permission, "*", agent.permission).action} test("returns default native agents when no config", async () => {  await using tmp = await tmpdir()  await Instance.provide({    directory: tmp.path,    fn: async () => {      const agents = await Agent.list()      const names = agents.map((a) => a.name)      expect(names).toContain("research")      expect(names).toContain("plan")      expect(names).toContain("task")      expect(names).toContain("explore")      expect(names).toContain("compaction")      expect(names).toContain("title")    },  })}) test("research agent has correct default properties", async () => {  await using tmp = await tmpdir()  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(research).toBeDefined()      expect(research?.mode).toBe("primary")      expect(research?.native).toBe(true)      expect(evalPerm(research, "edit")).toBe("allow")      expect(evalPerm(research, "bash")).toBe("allow")    },  })}) test("plan agent denies edits except .openscience/plans/*", async () => {  await using tmp = await tmpdir()  await Instance.provide({    directory: tmp.path,    fn: async () => {      const plan = await Agent.get("plan")      expect(plan).toBeDefined()      // Wildcard is denied      expect(evalPerm(plan, "edit")).toBe("deny")      // But specific path is allowed      expect(PermissionNext.evaluate("edit", ".openscience/plans/foo.md", plan!.permission).action).toBe("allow")    },  })}) test("explore agent denies edit and write", async () => {  await using tmp = await tmpdir()  await Instance.provide({    directory: tmp.path,    fn: async () => {      const explore = await Agent.get("explore")      expect(explore).toBeDefined()      expect(explore?.mode).toBe("subagent")      expect(evalPerm(explore, "edit")).toBe("deny")      expect(evalPerm(explore, "write")).toBe("deny")      expect(evalPerm(explore, "todoread")).toBe("deny")      expect(evalPerm(explore, "todowrite")).toBe("deny")    },  })}) test("task agent denies todo tools", async () => {  await using tmp = await tmpdir()  await Instance.provide({    directory: tmp.path,    fn: async () => {      const task = await Agent.get("task")      expect(task).toBeDefined()      expect(task?.mode).toBe("subagent")      expect(task?.hidden).toBeUndefined()      expect(evalPerm(task, "todoread")).toBe("deny")      expect(evalPerm(task, "todowrite")).toBe("deny")    },  })}) test("compaction agent denies all permissions", async () => {  await using tmp = await tmpdir()  await Instance.provide({    directory: tmp.path,    fn: async () => {      const compaction = await Agent.get("compaction")      expect(compaction).toBeDefined()      expect(compaction?.hidden).toBe(true)      expect(evalPerm(compaction, "bash")).toBe("deny")      expect(evalPerm(compaction, "edit")).toBe("deny")      expect(evalPerm(compaction, "read")).toBe("deny")    },  })}) test("custom agent from config creates new agent", async () => {  await using tmp = await tmpdir({    config: {      agent: {        my_custom_agent: {          model: "openai/gpt-4",          description: "My custom agent",          temperature: 0.5,          top_p: 0.9,        },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const custom = await Agent.get("my_custom_agent")      expect(custom).toBeDefined()      expect(custom?.model?.providerID).toBe("openai")      expect(custom?.model?.modelID).toBe("gpt-4")      expect(custom?.description).toBe("My custom agent")      expect(custom?.temperature).toBe(0.5)      expect(custom?.topP).toBe(0.9)      expect(custom?.native).toBe(false)      expect(custom?.mode).toBe("all")    },  })}) test("custom agent config overrides native agent properties", async () => {  await using tmp = await tmpdir({    config: {      agent: {        research: {          model: "anthropic/claude-3",          description: "Custom research agent",          temperature: 0.7,          color: "#FF0000",        },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(research).toBeDefined()      expect(research?.model?.providerID).toBe("anthropic")      expect(research?.model?.modelID).toBe("claude-3")      expect(research?.description).toBe("Custom research agent")      expect(research?.temperature).toBe(0.7)      expect(research?.color).toBe("#FF0000")      expect(research?.native).toBe(true)    },  })}) test("agent disable removes agent from list", async () => {  await using tmp = await tmpdir({    config: {      agent: {        explore: { disable: true },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const explore = await Agent.get("explore")      expect(explore).toBeUndefined()      const agents = await Agent.list()      const names = agents.map((a) => a.name)      expect(names).not.toContain("explore")    },  })}) test("agent permission config merges with defaults", async () => {  await using tmp = await tmpdir({    config: {      agent: {        research: {          permission: {            bash: {              "rm -rf *": "deny",            },          },        },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(research).toBeDefined()      // Specific pattern is denied      expect(PermissionNext.evaluate("bash", "rm -rf *", research!.permission).action).toBe("deny")      // Edit still allowed      expect(evalPerm(research, "edit")).toBe("allow")    },  })}) test("global permission config applies to all agents", async () => {  await using tmp = await tmpdir({    config: {      permission: {        bash: "deny",      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(research).toBeDefined()      expect(evalPerm(research, "bash")).toBe("deny")    },  })}) test("agent steps/maxSteps config sets steps property", async () => {  await using tmp = await tmpdir({    config: {      agent: {        research: { steps: 50 },        plan: { maxSteps: 100 },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      const plan = await Agent.get("plan")      expect(research?.steps).toBe(50)      expect(plan?.steps).toBe(100)    },  })}) test("agent mode can be overridden", async () => {  await using tmp = await tmpdir({    config: {      agent: {        explore: { mode: "primary" },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const explore = await Agent.get("explore")      expect(explore?.mode).toBe("primary")    },  })}) test("agent name can be overridden", async () => {  await using tmp = await tmpdir({    config: {      agent: {        research: { name: "Builder" },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(research?.name).toBe("Builder")    },  })}) test("agent prompt can be set from config", async () => {  await using tmp = await tmpdir({    config: {      agent: {        research: { prompt: "Custom system prompt" },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(research?.prompt).toBe("Custom system prompt")    },  })}) test("unknown agent properties are placed into options", async () => {  await using tmp = await tmpdir({    config: {      agent: {        research: {          random_property: "hello",          another_random: 123,        },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(research?.options.random_property).toBe("hello")      expect(research?.options.another_random).toBe(123)    },  })}) test("agent options merge correctly", async () => {  await using tmp = await tmpdir({    config: {      agent: {        research: {          options: {            custom_option: true,            another_option: "value",          },        },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(research?.options.custom_option).toBe(true)      expect(research?.options.another_option).toBe("value")    },  })}) test("multiple custom agents can be defined", async () => {  await using tmp = await tmpdir({    config: {      agent: {        agent_a: {          description: "Agent A",          mode: "subagent",        },        agent_b: {          description: "Agent B",          mode: "primary",        },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const agentA = await Agent.get("agent_a")      const agentB = await Agent.get("agent_b")      expect(agentA?.description).toBe("Agent A")      expect(agentA?.mode).toBe("subagent")      expect(agentB?.description).toBe("Agent B")      expect(agentB?.mode).toBe("primary")    },  })}) test("Agent.get returns undefined for non-existent agent", async () => {  await using tmp = await tmpdir()  await Instance.provide({    directory: tmp.path,    fn: async () => {      const nonExistent = await Agent.get("does_not_exist")      expect(nonExistent).toBeUndefined()    },  })}) test("default permission includes doom_loop and external_directory as ask", async () => {  await using tmp = await tmpdir()  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(evalPerm(research, "doom_loop")).toBe("ask")      expect(evalPerm(research, "external_directory")).toBe("ask")    },  })}) test("webfetch is allowed by default", async () => {  await using tmp = await tmpdir()  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(evalPerm(research, "webfetch")).toBe("allow")    },  })}) test("legacy tools config converts to permissions", async () => {  await using tmp = await tmpdir({    config: {      agent: {        research: {          tools: {            bash: false,            read: false,          },        },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(evalPerm(research, "bash")).toBe("deny")      expect(evalPerm(research, "read")).toBe("deny")    },  })}) test("legacy tools config maps write/edit/patch/multiedit to edit permission", async () => {  await using tmp = await tmpdir({    config: {      agent: {        research: {          tools: {            write: false,          },        },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(evalPerm(research, "edit")).toBe("deny")    },  })}) test("Truncate.DIR is allowed even when user denies external_directory globally", async () => {  const { Truncate } = await import("../../src/tool/truncation")  await using tmp = await tmpdir({    config: {      permission: {        external_directory: "deny",      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(PermissionNext.evaluate("external_directory", Truncate.DIR, research!.permission).action).toBe("allow")      expect(PermissionNext.evaluate("external_directory", Truncate.GLOB, research!.permission).action).toBe("allow")      expect(PermissionNext.evaluate("external_directory", "/some/other/path", research!.permission).action).toBe(        "deny",      )    },  })}) test("Truncate.DIR is allowed even when user denies external_directory per-agent", async () => {  const { Truncate } = await import("../../src/tool/truncation")  await using tmp = await tmpdir({    config: {      agent: {        research: {          permission: {            external_directory: "deny",          },        },      },    },  })  await Instance.provide({    directory: tmp.path,    fn: async () => {      const research = await Agent.get("research")      expect(PermissionNext.evaluate("external_directory", Truncate.DIR, research!.permission).action).toBe("allow")      expect(PermissionNext.evaluate("external_directory", Truncate.GLOB, research!.permission).action).toBe("allow")      expect(PermissionNext.evaluate("external_directory", "/some/other/path", research!.permission).action).toBe(        "deny",      )    },  })}) test("explicit Truncate.DIR deny is respected", async () => {  const { Truncate } = await import("../../src/tool/truncation")  await using tmp = await tmpdir({    config: {

Lines 1–500 of 643.