agent.test.ts 18 KB

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