agent.test.ts 21 KB

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