agent.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. import { test, expect } from "bun:test"
  2. import path from "path"
  3. import { tmpdir } from "../fixture/fixture"
  4. import { Instance } from "../../src/project/instance"
  5. import { Agent } from "../../src/agent/agent"
  6. import { PermissionNext } from "../../src/permission/next"
  7. // Helper to evaluate permission for a tool with wildcard pattern
  8. function evalPerm(agent: Agent.Info | undefined, permission: string): PermissionNext.Action | undefined {
  9. if (!agent) return undefined
  10. return PermissionNext.evaluate(permission, "*", agent.permission).action
  11. }
  12. test("returns default native agents when no config", async () => {
  13. await using tmp = await tmpdir()
  14. await Instance.provide({
  15. directory: tmp.path,
  16. fn: async () => {
  17. const agents = await Agent.list()
  18. const names = agents.map((a) => a.name)
  19. expect(names).toContain("build")
  20. expect(names).toContain("plan")
  21. expect(names).toContain("general")
  22. expect(names).toContain("explore")
  23. expect(names).toContain("compaction")
  24. expect(names).toContain("title")
  25. expect(names).toContain("summary")
  26. },
  27. })
  28. })
  29. test("build agent has correct default properties", async () => {
  30. await using tmp = await tmpdir()
  31. await Instance.provide({
  32. directory: tmp.path,
  33. fn: async () => {
  34. const build = await Agent.get("build")
  35. expect(build).toBeDefined()
  36. expect(build?.mode).toBe("primary")
  37. expect(build?.native).toBe(true)
  38. expect(evalPerm(build, "edit")).toBe("allow")
  39. expect(evalPerm(build, "bash")).toBe("allow")
  40. },
  41. })
  42. })
  43. test("plan agent denies edits except .opencode/plans/*", async () => {
  44. await using tmp = await tmpdir()
  45. await Instance.provide({
  46. directory: tmp.path,
  47. fn: async () => {
  48. const plan = await Agent.get("plan")
  49. expect(plan).toBeDefined()
  50. // Wildcard is denied
  51. expect(evalPerm(plan, "edit")).toBe("deny")
  52. // But specific path is allowed
  53. expect(PermissionNext.evaluate("edit", ".opencode/plans/foo.md", plan!.permission).action).toBe("allow")
  54. },
  55. })
  56. })
  57. test("explore agent denies edit and write", async () => {
  58. await using tmp = await tmpdir()
  59. await Instance.provide({
  60. directory: tmp.path,
  61. fn: async () => {
  62. const explore = await Agent.get("explore")
  63. expect(explore).toBeDefined()
  64. expect(explore?.mode).toBe("subagent")
  65. expect(evalPerm(explore, "edit")).toBe("deny")
  66. expect(evalPerm(explore, "write")).toBe("deny")
  67. expect(evalPerm(explore, "todoread")).toBe("deny")
  68. expect(evalPerm(explore, "todowrite")).toBe("deny")
  69. },
  70. })
  71. })
  72. test("explore agent asks for external directories and allows Truncate.GLOB", async () => {
  73. const { Truncate } = await import("../../src/tool/truncation")
  74. await using tmp = await tmpdir()
  75. await Instance.provide({
  76. directory: tmp.path,
  77. fn: async () => {
  78. const explore = await Agent.get("explore")
  79. expect(explore).toBeDefined()
  80. expect(PermissionNext.evaluate("external_directory", "/some/other/path", explore!.permission).action).toBe("ask")
  81. expect(PermissionNext.evaluate("external_directory", Truncate.GLOB, explore!.permission).action).toBe("allow")
  82. },
  83. })
  84. })
  85. test("general agent denies todo tools", async () => {
  86. await using tmp = await tmpdir()
  87. await Instance.provide({
  88. directory: tmp.path,
  89. fn: async () => {
  90. const general = await Agent.get("general")
  91. expect(general).toBeDefined()
  92. expect(general?.mode).toBe("subagent")
  93. expect(general?.hidden).toBeUndefined()
  94. expect(evalPerm(general, "todoread")).toBe("deny")
  95. expect(evalPerm(general, "todowrite")).toBe("deny")
  96. },
  97. })
  98. })
  99. test("compaction agent denies all permissions", async () => {
  100. await using tmp = await tmpdir()
  101. await Instance.provide({
  102. directory: tmp.path,
  103. fn: async () => {
  104. const compaction = await Agent.get("compaction")
  105. expect(compaction).toBeDefined()
  106. expect(compaction?.hidden).toBe(true)
  107. expect(evalPerm(compaction, "bash")).toBe("deny")
  108. expect(evalPerm(compaction, "edit")).toBe("deny")
  109. expect(evalPerm(compaction, "read")).toBe("deny")
  110. },
  111. })
  112. })
  113. test("custom agent from config creates new agent", async () => {
  114. await using tmp = await tmpdir({
  115. config: {
  116. agent: {
  117. my_custom_agent: {
  118. model: "openai/gpt-4",
  119. description: "My custom agent",
  120. temperature: 0.5,
  121. top_p: 0.9,
  122. },
  123. },
  124. },
  125. })
  126. await Instance.provide({
  127. directory: tmp.path,
  128. fn: async () => {
  129. const custom = await Agent.get("my_custom_agent")
  130. expect(custom).toBeDefined()
  131. expect(custom?.model?.providerID).toBe("openai")
  132. expect(custom?.model?.modelID).toBe("gpt-4")
  133. expect(custom?.description).toBe("My custom agent")
  134. expect(custom?.temperature).toBe(0.5)
  135. expect(custom?.topP).toBe(0.9)
  136. expect(custom?.native).toBe(false)
  137. expect(custom?.mode).toBe("all")
  138. },
  139. })
  140. })
  141. test("custom agent config overrides native agent properties", async () => {
  142. await using tmp = await tmpdir({
  143. config: {
  144. agent: {
  145. build: {
  146. model: "anthropic/claude-3",
  147. description: "Custom build agent",
  148. temperature: 0.7,
  149. color: "#FF0000",
  150. },
  151. },
  152. },
  153. })
  154. await Instance.provide({
  155. directory: tmp.path,
  156. fn: async () => {
  157. const build = await Agent.get("build")
  158. expect(build).toBeDefined()
  159. expect(build?.model?.providerID).toBe("anthropic")
  160. expect(build?.model?.modelID).toBe("claude-3")
  161. expect(build?.description).toBe("Custom build agent")
  162. expect(build?.temperature).toBe(0.7)
  163. expect(build?.color).toBe("#FF0000")
  164. expect(build?.native).toBe(true)
  165. },
  166. })
  167. })
  168. test("agent disable removes agent from list", async () => {
  169. await using tmp = await tmpdir({
  170. config: {
  171. agent: {
  172. explore: { disable: true },
  173. },
  174. },
  175. })
  176. await Instance.provide({
  177. directory: tmp.path,
  178. fn: async () => {
  179. const explore = await Agent.get("explore")
  180. expect(explore).toBeUndefined()
  181. const agents = await Agent.list()
  182. const names = agents.map((a) => a.name)
  183. expect(names).not.toContain("explore")
  184. },
  185. })
  186. })
  187. test("agent permission config merges with defaults", async () => {
  188. await using tmp = await tmpdir({
  189. config: {
  190. agent: {
  191. build: {
  192. permission: {
  193. bash: {
  194. "rm -rf *": "deny",
  195. },
  196. },
  197. },
  198. },
  199. },
  200. })
  201. await Instance.provide({
  202. directory: tmp.path,
  203. fn: async () => {
  204. const build = await Agent.get("build")
  205. expect(build).toBeDefined()
  206. // Specific pattern is denied
  207. expect(PermissionNext.evaluate("bash", "rm -rf *", build!.permission).action).toBe("deny")
  208. // Edit still allowed
  209. expect(evalPerm(build, "edit")).toBe("allow")
  210. },
  211. })
  212. })
  213. test("global permission config applies to all agents", async () => {
  214. await using tmp = await tmpdir({
  215. config: {
  216. permission: {
  217. bash: "deny",
  218. },
  219. },
  220. })
  221. await Instance.provide({
  222. directory: tmp.path,
  223. fn: async () => {
  224. const build = await Agent.get("build")
  225. expect(build).toBeDefined()
  226. expect(evalPerm(build, "bash")).toBe("deny")
  227. },
  228. })
  229. })
  230. test("agent steps/maxSteps config sets steps property", async () => {
  231. await using tmp = await tmpdir({
  232. config: {
  233. agent: {
  234. build: { steps: 50 },
  235. plan: { maxSteps: 100 },
  236. },
  237. },
  238. })
  239. await Instance.provide({
  240. directory: tmp.path,
  241. fn: async () => {
  242. const build = await Agent.get("build")
  243. const plan = await Agent.get("plan")
  244. expect(build?.steps).toBe(50)
  245. expect(plan?.steps).toBe(100)
  246. },
  247. })
  248. })
  249. test("agent mode can be overridden", async () => {
  250. await using tmp = await tmpdir({
  251. config: {
  252. agent: {
  253. explore: { mode: "primary" },
  254. },
  255. },
  256. })
  257. await Instance.provide({
  258. directory: tmp.path,
  259. fn: async () => {
  260. const explore = await Agent.get("explore")
  261. expect(explore?.mode).toBe("primary")
  262. },
  263. })
  264. })
  265. test("agent name can be overridden", async () => {
  266. await using tmp = await tmpdir({
  267. config: {
  268. agent: {
  269. build: { name: "Builder" },
  270. },
  271. },
  272. })
  273. await Instance.provide({
  274. directory: tmp.path,
  275. fn: async () => {
  276. const build = await Agent.get("build")
  277. expect(build?.name).toBe("Builder")
  278. },
  279. })
  280. })
  281. test("agent prompt can be set from config", async () => {
  282. await using tmp = await tmpdir({
  283. config: {
  284. agent: {
  285. build: { prompt: "Custom system prompt" },
  286. },
  287. },
  288. })
  289. await Instance.provide({
  290. directory: tmp.path,
  291. fn: async () => {
  292. const build = await Agent.get("build")
  293. expect(build?.prompt).toBe("Custom system prompt")
  294. },
  295. })
  296. })
  297. test("unknown agent properties are placed into options", async () => {
  298. await using tmp = await tmpdir({
  299. config: {
  300. agent: {
  301. build: {
  302. random_property: "hello",
  303. another_random: 123,
  304. },
  305. },
  306. },
  307. })
  308. await Instance.provide({
  309. directory: tmp.path,
  310. fn: async () => {
  311. const build = await Agent.get("build")
  312. expect(build?.options.random_property).toBe("hello")
  313. expect(build?.options.another_random).toBe(123)
  314. },
  315. })
  316. })
  317. test("agent options merge correctly", async () => {
  318. await using tmp = await tmpdir({
  319. config: {
  320. agent: {
  321. build: {
  322. options: {
  323. custom_option: true,
  324. another_option: "value",
  325. },
  326. },
  327. },
  328. },
  329. })
  330. await Instance.provide({
  331. directory: tmp.path,
  332. fn: async () => {
  333. const build = await Agent.get("build")
  334. expect(build?.options.custom_option).toBe(true)
  335. expect(build?.options.another_option).toBe("value")
  336. },
  337. })
  338. })
  339. test("multiple custom agents can be defined", async () => {
  340. await using tmp = await tmpdir({
  341. config: {
  342. agent: {
  343. agent_a: {
  344. description: "Agent A",
  345. mode: "subagent",
  346. },
  347. agent_b: {
  348. description: "Agent B",
  349. mode: "primary",
  350. },
  351. },
  352. },
  353. })
  354. await Instance.provide({
  355. directory: tmp.path,
  356. fn: async () => {
  357. const agentA = await Agent.get("agent_a")
  358. const agentB = await Agent.get("agent_b")
  359. expect(agentA?.description).toBe("Agent A")
  360. expect(agentA?.mode).toBe("subagent")
  361. expect(agentB?.description).toBe("Agent B")
  362. expect(agentB?.mode).toBe("primary")
  363. },
  364. })
  365. })
  366. test("Agent.get returns undefined for non-existent agent", async () => {
  367. await using tmp = await tmpdir()
  368. await Instance.provide({
  369. directory: tmp.path,
  370. fn: async () => {
  371. const nonExistent = await Agent.get("does_not_exist")
  372. expect(nonExistent).toBeUndefined()
  373. },
  374. })
  375. })
  376. test("default permission includes doom_loop and external_directory as ask", async () => {
  377. await using tmp = await tmpdir()
  378. await Instance.provide({
  379. directory: tmp.path,
  380. fn: async () => {
  381. const build = await Agent.get("build")
  382. expect(evalPerm(build, "doom_loop")).toBe("ask")
  383. expect(evalPerm(build, "external_directory")).toBe("ask")
  384. },
  385. })
  386. })
  387. test("webfetch is allowed by default", async () => {
  388. await using tmp = await tmpdir()
  389. await Instance.provide({
  390. directory: tmp.path,
  391. fn: async () => {
  392. const build = await Agent.get("build")
  393. expect(evalPerm(build, "webfetch")).toBe("allow")
  394. },
  395. })
  396. })
  397. test("legacy tools config converts to permissions", async () => {
  398. await using tmp = await tmpdir({
  399. config: {
  400. agent: {
  401. build: {
  402. tools: {
  403. bash: false,
  404. read: false,
  405. },
  406. },
  407. },
  408. },
  409. })
  410. await Instance.provide({
  411. directory: tmp.path,
  412. fn: async () => {
  413. const build = await Agent.get("build")
  414. expect(evalPerm(build, "bash")).toBe("deny")
  415. expect(evalPerm(build, "read")).toBe("deny")
  416. },
  417. })
  418. })
  419. test("legacy tools config maps write/edit/patch/multiedit to edit permission", async () => {
  420. await using tmp = await tmpdir({
  421. config: {
  422. agent: {
  423. build: {
  424. tools: {
  425. write: false,
  426. },
  427. },
  428. },
  429. },
  430. })
  431. await Instance.provide({
  432. directory: tmp.path,
  433. fn: async () => {
  434. const build = await Agent.get("build")
  435. expect(evalPerm(build, "edit")).toBe("deny")
  436. },
  437. })
  438. })
  439. test("Truncate.GLOB is allowed even when user denies external_directory globally", async () => {
  440. const { Truncate } = await import("../../src/tool/truncation")
  441. await using tmp = await tmpdir({
  442. config: {
  443. permission: {
  444. external_directory: "deny",
  445. },
  446. },
  447. })
  448. await Instance.provide({
  449. directory: tmp.path,
  450. fn: async () => {
  451. const build = await Agent.get("build")
  452. expect(PermissionNext.evaluate("external_directory", Truncate.GLOB, build!.permission).action).toBe("allow")
  453. expect(PermissionNext.evaluate("external_directory", Truncate.DIR, build!.permission).action).toBe("deny")
  454. expect(PermissionNext.evaluate("external_directory", "/some/other/path", build!.permission).action).toBe("deny")
  455. },
  456. })
  457. })
  458. test("Truncate.GLOB is allowed even when user denies external_directory per-agent", async () => {
  459. const { Truncate } = await import("../../src/tool/truncation")
  460. await using tmp = await tmpdir({
  461. config: {
  462. agent: {
  463. build: {
  464. permission: {
  465. external_directory: "deny",
  466. },
  467. },
  468. },
  469. },
  470. })
  471. await Instance.provide({
  472. directory: tmp.path,
  473. fn: async () => {
  474. const build = await Agent.get("build")
  475. expect(PermissionNext.evaluate("external_directory", Truncate.GLOB, build!.permission).action).toBe("allow")
  476. expect(PermissionNext.evaluate("external_directory", Truncate.DIR, build!.permission).action).toBe("deny")
  477. expect(PermissionNext.evaluate("external_directory", "/some/other/path", build!.permission).action).toBe("deny")
  478. },
  479. })
  480. })
  481. test("explicit Truncate.GLOB deny is respected", async () => {
  482. const { Truncate } = await import("../../src/tool/truncation")
  483. await using tmp = await tmpdir({
  484. config: {
  485. permission: {
  486. external_directory: {
  487. "*": "deny",
  488. [Truncate.GLOB]: "deny",
  489. },
  490. },
  491. },
  492. })
  493. await Instance.provide({
  494. directory: tmp.path,
  495. fn: async () => {
  496. const build = await Agent.get("build")
  497. expect(PermissionNext.evaluate("external_directory", Truncate.GLOB, build!.permission).action).toBe("deny")
  498. expect(PermissionNext.evaluate("external_directory", Truncate.DIR, build!.permission).action).toBe("deny")
  499. },
  500. })
  501. })
  502. test("skill directories are allowed for external_directory", async () => {
  503. await using tmp = await tmpdir({
  504. git: true,
  505. init: async (dir) => {
  506. const skillDir = path.join(dir, ".opencode", "skill", "perm-skill")
  507. await Bun.write(
  508. path.join(skillDir, "SKILL.md"),
  509. `---
  510. name: perm-skill
  511. description: Permission skill.
  512. ---
  513. # Permission Skill
  514. `,
  515. )
  516. },
  517. })
  518. const home = process.env.OPENCODE_TEST_HOME
  519. process.env.OPENCODE_TEST_HOME = tmp.path
  520. try {
  521. await Instance.provide({
  522. directory: tmp.path,
  523. fn: async () => {
  524. const build = await Agent.get("build")
  525. const skillDir = path.join(tmp.path, ".opencode", "skill", "perm-skill")
  526. const target = path.join(skillDir, "reference", "notes.md")
  527. expect(PermissionNext.evaluate("external_directory", target, build!.permission).action).toBe("allow")
  528. },
  529. })
  530. } finally {
  531. process.env.OPENCODE_TEST_HOME = home
  532. }
  533. })
  534. test("defaultAgent returns build when no default_agent config", async () => {
  535. await using tmp = await tmpdir()
  536. await Instance.provide({
  537. directory: tmp.path,
  538. fn: async () => {
  539. const agent = await Agent.defaultAgent()
  540. expect(agent).toBe("build")
  541. },
  542. })
  543. })
  544. test("defaultAgent respects default_agent config set to plan", async () => {
  545. await using tmp = await tmpdir({
  546. config: {
  547. default_agent: "plan",
  548. },
  549. })
  550. await Instance.provide({
  551. directory: tmp.path,
  552. fn: async () => {
  553. const agent = await Agent.defaultAgent()
  554. expect(agent).toBe("plan")
  555. },
  556. })
  557. })
  558. test("defaultAgent respects default_agent config set to custom agent with mode all", async () => {
  559. await using tmp = await tmpdir({
  560. config: {
  561. default_agent: "my_custom",
  562. agent: {
  563. my_custom: {
  564. description: "My custom agent",
  565. },
  566. },
  567. },
  568. })
  569. await Instance.provide({
  570. directory: tmp.path,
  571. fn: async () => {
  572. const agent = await Agent.defaultAgent()
  573. expect(agent).toBe("my_custom")
  574. },
  575. })
  576. })
  577. test("defaultAgent throws when default_agent points to subagent", async () => {
  578. await using tmp = await tmpdir({
  579. config: {
  580. default_agent: "explore",
  581. },
  582. })
  583. await Instance.provide({
  584. directory: tmp.path,
  585. fn: async () => {
  586. await expect(Agent.defaultAgent()).rejects.toThrow('default agent "explore" is a subagent')
  587. },
  588. })
  589. })
  590. test("defaultAgent throws when default_agent points to hidden agent", async () => {
  591. await using tmp = await tmpdir({
  592. config: {
  593. default_agent: "compaction",
  594. },
  595. })
  596. await Instance.provide({
  597. directory: tmp.path,
  598. fn: async () => {
  599. await expect(Agent.defaultAgent()).rejects.toThrow('default agent "compaction" is hidden')
  600. },
  601. })
  602. })
  603. test("defaultAgent throws when default_agent points to non-existent agent", async () => {
  604. await using tmp = await tmpdir({
  605. config: {
  606. default_agent: "does_not_exist",
  607. },
  608. })
  609. await Instance.provide({
  610. directory: tmp.path,
  611. fn: async () => {
  612. await expect(Agent.defaultAgent()).rejects.toThrow('default agent "does_not_exist" not found')
  613. },
  614. })
  615. })
  616. test("defaultAgent returns plan when build is disabled and default_agent not set", async () => {
  617. await using tmp = await tmpdir({
  618. config: {
  619. agent: {
  620. build: { disable: true },
  621. },
  622. },
  623. })
  624. await Instance.provide({
  625. directory: tmp.path,
  626. fn: async () => {
  627. const agent = await Agent.defaultAgent()
  628. // build is disabled, so it should return plan (next primary agent)
  629. expect(agent).toBe("plan")
  630. },
  631. })
  632. })
  633. test("defaultAgent throws when all primary agents are disabled", async () => {
  634. await using tmp = await tmpdir({
  635. config: {
  636. agent: {
  637. build: { disable: true },
  638. plan: { disable: true },
  639. },
  640. },
  641. })
  642. await Instance.provide({
  643. directory: tmp.path,
  644. fn: async () => {
  645. // build and plan are disabled, no primary-capable agents remain
  646. await expect(Agent.defaultAgent()).rejects.toThrow("no primary visible agent found")
  647. },
  648. })
  649. })