@osovv/vv-opencode 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (296) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/README.md +87 -85
  3. package/dist/commands/completion.js +0 -1
  4. package/dist/commands/completion.js.map +1 -1
  5. package/dist/commands/doctor.js +6 -9
  6. package/dist/commands/doctor.js.map +1 -1
  7. package/dist/commands/init.d.ts +2 -1
  8. package/dist/commands/init.js +7 -12
  9. package/dist/commands/init.js.map +1 -1
  10. package/dist/commands/install.d.ts +4 -0
  11. package/dist/commands/install.js +40 -37
  12. package/dist/commands/install.js.map +1 -1
  13. package/dist/commands/launch.d.ts +2 -1
  14. package/dist/commands/launch.js +18 -14
  15. package/dist/commands/launch.js.map +1 -1
  16. package/dist/commands/patch-provider.d.ts +225 -307
  17. package/dist/commands/patch-provider.js +92 -296
  18. package/dist/commands/patch-provider.js.map +1 -1
  19. package/dist/commands/status.js +7 -10
  20. package/dist/commands/status.js.map +1 -1
  21. package/dist/commands/sync.d.ts +4 -0
  22. package/dist/commands/sync.js +38 -35
  23. package/dist/commands/sync.js.map +1 -1
  24. package/dist/index.d.ts +25 -5
  25. package/dist/index.js +73 -34
  26. package/dist/index.js.map +1 -1
  27. package/dist/lib/agent-tool-catalog.d.ts +1 -1
  28. package/dist/lib/agent-tool-catalog.js +3 -6
  29. package/dist/lib/agent-tool-catalog.js.map +1 -1
  30. package/dist/lib/agent-tool-contract.d.ts +22 -13
  31. package/dist/lib/agent-tool-contract.js +20 -20
  32. package/dist/lib/agent-tool-contract.js.map +1 -1
  33. package/dist/lib/analytics/types.d.ts +7 -1
  34. package/dist/lib/analytics/types.js +2 -2
  35. package/dist/lib/analytics/types.js.map +1 -1
  36. package/dist/lib/config-layers.d.ts +28 -6
  37. package/dist/lib/config-layers.js +268 -73
  38. package/dist/lib/config-layers.js.map +1 -1
  39. package/dist/lib/managed-agents.d.ts +24 -2
  40. package/dist/lib/managed-agents.js +45 -15
  41. package/dist/lib/managed-agents.js.map +1 -1
  42. package/dist/lib/model-roles.d.ts +5 -0
  43. package/dist/lib/model-roles.js +22 -1
  44. package/dist/lib/model-roles.js.map +1 -1
  45. package/dist/lib/opencode/agent-registrations.d.ts +4 -4
  46. package/dist/lib/opencode/agent-registrations.js +386 -197
  47. package/dist/lib/opencode/agent-registrations.js.map +1 -1
  48. package/dist/lib/opencode/inspection.d.ts +13 -13
  49. package/dist/lib/opencode/inspection.js +69 -106
  50. package/dist/lib/opencode/inspection.js.map +1 -1
  51. package/dist/lib/opencode/model-overrides.d.ts +2 -2
  52. package/dist/lib/opencode/model-overrides.js +192 -47
  53. package/dist/lib/opencode/model-overrides.js.map +1 -1
  54. package/dist/lib/opencode/paths.d.ts +6 -2
  55. package/dist/lib/opencode/paths.js +12 -11
  56. package/dist/lib/opencode/paths.js.map +1 -1
  57. package/dist/lib/opencode/plugin-registration.d.ts +10 -11
  58. package/dist/lib/opencode/plugin-registration.js +76 -138
  59. package/dist/lib/opencode/plugin-registration.js.map +1 -1
  60. package/dist/lib/opencode/shared-utils.d.ts +47 -3
  61. package/dist/lib/opencode/shared-utils.js +346 -15
  62. package/dist/lib/opencode/shared-utils.js.map +1 -1
  63. package/dist/lib/opencode.d.ts +5 -6
  64. package/dist/lib/opencode.js +10 -10
  65. package/dist/lib/opencode.js.map +1 -1
  66. package/dist/lib/vvoc-config.js +4 -4
  67. package/dist/lib/vvoc-config.js.map +1 -1
  68. package/dist/lib/vvoc-paths.d.ts +7 -0
  69. package/dist/lib/vvoc-paths.js +15 -1
  70. package/dist/lib/vvoc-paths.js.map +1 -1
  71. package/dist/lib/vvoc-preset-registry.d.ts +38 -50
  72. package/dist/lib/vvoc-preset-registry.js +38 -48
  73. package/dist/lib/vvoc-preset-registry.js.map +1 -1
  74. package/dist/plugins/analytics/index.d.ts +20 -14
  75. package/dist/plugins/analytics/index.js +281 -133
  76. package/dist/plugins/analytics/index.js.map +1 -1
  77. package/dist/plugins/guardian/index.d.ts +63 -42
  78. package/dist/plugins/guardian/index.js +217 -759
  79. package/dist/plugins/guardian/index.js.map +1 -1
  80. package/dist/plugins/hashline-edit/index.d.ts +145 -31
  81. package/dist/plugins/hashline-edit/index.js +679 -244
  82. package/dist/plugins/hashline-edit/index.js.map +1 -1
  83. package/dist/plugins/hashline-edit/schemas.d.ts +1 -1
  84. package/dist/plugins/hashline-edit/schemas.js +3 -3
  85. package/dist/plugins/hashline-edit/schemas.js.map +1 -1
  86. package/dist/plugins/model-roles/index.d.ts +18 -4
  87. package/dist/plugins/model-roles/index.js +44 -181
  88. package/dist/plugins/model-roles/index.js.map +1 -1
  89. package/dist/plugins/peak-hours/index.d.ts +23 -22
  90. package/dist/plugins/peak-hours/index.js +210 -178
  91. package/dist/plugins/peak-hours/index.js.map +1 -1
  92. package/dist/plugins/secrets-redaction/deep.js +35 -51
  93. package/dist/plugins/secrets-redaction/deep.js.map +1 -1
  94. package/dist/plugins/secrets-redaction/index.d.ts +96 -8
  95. package/dist/plugins/secrets-redaction/index.js +290 -102
  96. package/dist/plugins/secrets-redaction/index.js.map +1 -1
  97. package/dist/plugins/secrets-redaction/session.d.ts +9 -0
  98. package/dist/plugins/secrets-redaction/session.js +21 -4
  99. package/dist/plugins/secrets-redaction/session.js.map +1 -1
  100. package/dist/plugins/secrets-redaction/stream.d.ts +92 -0
  101. package/dist/plugins/secrets-redaction/stream.js +772 -0
  102. package/dist/plugins/secrets-redaction/stream.js.map +1 -0
  103. package/dist/plugins/spec-guard/index.d.ts +49 -18
  104. package/dist/plugins/spec-guard/index.js +231 -129
  105. package/dist/plugins/spec-guard/index.js.map +1 -1
  106. package/dist/plugins/system-context-injection/index.d.ts +10 -4
  107. package/dist/plugins/system-context-injection/index.js +271 -78
  108. package/dist/plugins/system-context-injection/index.js.map +1 -1
  109. package/dist/plugins/tool-history-compaction/index.d.ts +26 -4
  110. package/dist/plugins/tool-history-compaction/index.js +170 -32
  111. package/dist/plugins/tool-history-compaction/index.js.map +1 -1
  112. package/dist/plugins/tool-history-compaction/transform.d.ts +47 -16
  113. package/dist/plugins/tool-history-compaction/transform.js +173 -55
  114. package/dist/plugins/tool-history-compaction/transform.js.map +1 -1
  115. package/dist/plugins/web-tools/fetch-service.d.ts +51 -13
  116. package/dist/plugins/web-tools/fetch-service.js +106 -75
  117. package/dist/plugins/web-tools/fetch-service.js.map +1 -1
  118. package/dist/plugins/web-tools/index.d.ts +58 -16
  119. package/dist/plugins/web-tools/index.js +202 -91
  120. package/dist/plugins/web-tools/index.js.map +1 -1
  121. package/dist/plugins/web-tools/media-loader.d.ts +3 -3
  122. package/dist/plugins/web-tools/media-loader.js +3 -3
  123. package/dist/plugins/web-tools/media-loader.js.map +1 -1
  124. package/dist/plugins/web-tools/providers/native-fetch.d.ts +2 -2
  125. package/dist/plugins/web-tools/providers/native-fetch.js +1 -1
  126. package/dist/plugins/web-tools/providers/native-fetch.js.map +1 -1
  127. package/dist/plugins/web-tools/providers/spider.d.ts +2 -2
  128. package/dist/plugins/web-tools/providers/spider.js +1 -1
  129. package/dist/plugins/web-tools/providers/spider.js.map +1 -1
  130. package/dist/plugins/web-tools/providers/zai.d.ts +2 -2
  131. package/dist/plugins/web-tools/providers/zai.js +1 -1
  132. package/dist/plugins/web-tools/providers/zai.js.map +1 -1
  133. package/dist/plugins/web-tools/schemas.d.ts +1 -1
  134. package/dist/plugins/web-tools/schemas.js +3 -3
  135. package/dist/plugins/web-tools/schemas.js.map +1 -1
  136. package/dist/plugins/web-tools/search-service.d.ts +81 -12
  137. package/dist/plugins/web-tools/search-service.js +104 -57
  138. package/dist/plugins/web-tools/search-service.js.map +1 -1
  139. package/dist/plugins/workflow/authorization.d.ts +44 -10
  140. package/dist/plugins/workflow/authorization.js +113 -73
  141. package/dist/plugins/workflow/authorization.js.map +1 -1
  142. package/dist/plugins/workflow/cancellation.d.ts +86 -0
  143. package/dist/plugins/workflow/cancellation.js +202 -0
  144. package/dist/plugins/workflow/cancellation.js.map +1 -0
  145. package/dist/plugins/workflow/checkpoints.d.ts +7 -1
  146. package/dist/plugins/workflow/checkpoints.js +141 -21
  147. package/dist/plugins/workflow/checkpoints.js.map +1 -1
  148. package/dist/plugins/workflow/delegated.d.ts +14 -0
  149. package/dist/plugins/workflow/delegated.js +6 -5
  150. package/dist/plugins/workflow/delegated.js.map +1 -1
  151. package/dist/plugins/workflow/host.d.ts +81 -0
  152. package/dist/plugins/workflow/host.js +189 -0
  153. package/dist/plugins/workflow/host.js.map +1 -0
  154. package/dist/plugins/workflow/index.d.ts +69 -4
  155. package/dist/plugins/workflow/index.js +1391 -1026
  156. package/dist/plugins/workflow/index.js.map +1 -1
  157. package/dist/plugins/workflow/recovery.d.ts +3 -5
  158. package/dist/plugins/workflow/recovery.js +18 -33
  159. package/dist/plugins/workflow/recovery.js.map +1 -1
  160. package/dist/plugins/workflow/repair.d.ts +31 -5
  161. package/dist/plugins/workflow/repair.js +116 -48
  162. package/dist/plugins/workflow/repair.js.map +1 -1
  163. package/dist/plugins/workflow/results.d.ts +31 -31
  164. package/dist/plugins/workflow/results.js +4 -4
  165. package/dist/plugins/workflow/results.js.map +1 -1
  166. package/dist/plugins/workflow/schemas.d.ts +3 -3
  167. package/dist/plugins/workflow/schemas.js +5 -5
  168. package/dist/plugins/workflow/schemas.js.map +1 -1
  169. package/dist/plugins/workflow/system-instruction.md +4 -0
  170. package/dist/plugins/workflow/tooling.d.ts +6 -0
  171. package/dist/plugins/workflow/tooling.js +3 -1
  172. package/dist/plugins/workflow/tooling.js.map +1 -1
  173. package/dist/runtime/auxiliary.d.ts +13 -0
  174. package/dist/runtime/auxiliary.js +108 -0
  175. package/dist/runtime/auxiliary.js.map +1 -0
  176. package/dist/runtime/client.d.ts +88 -0
  177. package/dist/runtime/client.js +174 -0
  178. package/dist/runtime/client.js.map +1 -0
  179. package/dist/runtime/context-inspection-contract.d.ts +397 -0
  180. package/dist/runtime/context-inspection-contract.js +622 -0
  181. package/dist/runtime/context-inspection-contract.js.map +1 -0
  182. package/dist/runtime/context-inspection.d.ts +52 -0
  183. package/dist/runtime/context-inspection.js +324 -0
  184. package/dist/runtime/context-inspection.js.map +1 -0
  185. package/dist/runtime/context.d.ts +192 -0
  186. package/dist/runtime/context.js +1246 -0
  187. package/dist/runtime/context.js.map +1 -0
  188. package/dist/runtime/coordination.d.ts +40 -0
  189. package/dist/runtime/coordination.js +87 -0
  190. package/dist/runtime/coordination.js.map +1 -0
  191. package/dist/runtime/model-registry.d.ts +34 -0
  192. package/dist/runtime/model-registry.js +227 -0
  193. package/dist/runtime/model-registry.js.map +1 -0
  194. package/dist/runtime/permissions.d.ts +20 -0
  195. package/dist/runtime/permissions.js +250 -0
  196. package/dist/runtime/permissions.js.map +1 -0
  197. package/dist/runtime/snapshot-config.d.ts +80 -0
  198. package/dist/runtime/snapshot-config.js +268 -0
  199. package/dist/runtime/snapshot-config.js.map +1 -0
  200. package/dist/runtime/snapshot-store.d.ts +25 -0
  201. package/dist/runtime/snapshot-store.js +519 -0
  202. package/dist/runtime/snapshot-store.js.map +1 -0
  203. package/dist/runtime/snapshots.d.ts +59 -0
  204. package/dist/runtime/snapshots.js +721 -0
  205. package/dist/runtime/snapshots.js.map +1 -0
  206. package/dist/runtime/types.d.ts +713 -0
  207. package/dist/runtime/types.js +190 -0
  208. package/dist/runtime/types.js.map +1 -0
  209. package/dist/tui/analytics/indicator.d.ts +25 -26
  210. package/dist/tui/analytics/indicator.js +66 -74
  211. package/dist/tui/analytics/indicator.js.map +1 -1
  212. package/dist/tui/branding/footer.d.ts +7 -12
  213. package/dist/tui/branding/footer.js +16 -24
  214. package/dist/tui/branding/footer.js.map +1 -1
  215. package/dist/tui/context/analyze.d.ts +20 -9
  216. package/dist/tui/context/analyze.js +295 -257
  217. package/dist/tui/context/analyze.js.map +1 -1
  218. package/dist/tui/context/collect.d.ts +29 -3
  219. package/dist/tui/context/collect.js +450 -66
  220. package/dist/tui/context/collect.js.map +1 -1
  221. package/dist/tui/context/plugin.d.ts +21 -7
  222. package/dist/tui/context/plugin.js +65 -70
  223. package/dist/tui/context/plugin.js.map +1 -1
  224. package/dist/tui/context/types.d.ts +174 -43
  225. package/dist/tui/context/types.js +25 -19
  226. package/dist/tui/context/types.js.map +1 -1
  227. package/dist/tui/context/view.d.ts +33 -7
  228. package/dist/tui/context/view.js +136 -81
  229. package/dist/tui/context/view.js.map +1 -1
  230. package/dist/tui/peak-hours/banner.d.ts +13 -11
  231. package/dist/tui/peak-hours/banner.js +48 -80
  232. package/dist/tui/peak-hours/banner.js.map +1 -1
  233. package/dist/tui/policy.d.ts +69 -0
  234. package/dist/tui/policy.js +169 -0
  235. package/dist/tui/policy.js.map +1 -0
  236. package/dist/tui.d.ts +3 -8
  237. package/dist/tui.js +72 -68
  238. package/dist/tui.js.map +1 -1
  239. package/package.json +19 -15
  240. package/schemas/vvoc/v3.json +1 -1
  241. package/server.js +3 -5
  242. package/templates/agents/enhancer.md +10 -5
  243. package/templates/agents/guardian.md +10 -4
  244. package/templates/agents/investigator.md +4 -2
  245. package/templates/agents/vv-code-reviewer.md +4 -2
  246. package/templates/agents/vv-spec-reviewer.md +4 -2
  247. package/templates/skills/vv-execute/references/tool-contracts.md +1 -1
  248. package/tui.js +3 -5
  249. package/dist/plugins/analytics/v2.d.ts +0 -21
  250. package/dist/plugins/analytics/v2.js +0 -133
  251. package/dist/plugins/analytics/v2.js.map +0 -1
  252. package/dist/plugins/guardian/v2.d.ts +0 -13
  253. package/dist/plugins/guardian/v2.js +0 -161
  254. package/dist/plugins/guardian/v2.js.map +0 -1
  255. package/dist/plugins/hashline-edit/v2.d.ts +0 -12
  256. package/dist/plugins/hashline-edit/v2.js +0 -145
  257. package/dist/plugins/hashline-edit/v2.js.map +0 -1
  258. package/dist/plugins/model-roles/v2.d.ts +0 -24
  259. package/dist/plugins/model-roles/v2.js +0 -274
  260. package/dist/plugins/model-roles/v2.js.map +0 -1
  261. package/dist/plugins/peak-hours/v2.d.ts +0 -12
  262. package/dist/plugins/peak-hours/v2.js +0 -95
  263. package/dist/plugins/peak-hours/v2.js.map +0 -1
  264. package/dist/plugins/secrets-redaction/v2.d.ts +0 -14
  265. package/dist/plugins/secrets-redaction/v2.js +0 -100
  266. package/dist/plugins/secrets-redaction/v2.js.map +0 -1
  267. package/dist/plugins/spec-guard/v2.d.ts +0 -12
  268. package/dist/plugins/spec-guard/v2.js +0 -139
  269. package/dist/plugins/spec-guard/v2.js.map +0 -1
  270. package/dist/plugins/system-context-injection/agent-contexts.d.ts +0 -5
  271. package/dist/plugins/system-context-injection/agent-contexts.js +0 -164
  272. package/dist/plugins/system-context-injection/agent-contexts.js.map +0 -1
  273. package/dist/plugins/system-context-injection/v2.d.ts +0 -13
  274. package/dist/plugins/system-context-injection/v2.js +0 -60
  275. package/dist/plugins/system-context-injection/v2.js.map +0 -1
  276. package/dist/plugins/tool-history-compaction/v2.d.ts +0 -11
  277. package/dist/plugins/tool-history-compaction/v2.js +0 -56
  278. package/dist/plugins/tool-history-compaction/v2.js.map +0 -1
  279. package/dist/plugins/v2-runtime/config-watcher.d.ts +0 -26
  280. package/dist/plugins/v2-runtime/config-watcher.js +0 -111
  281. package/dist/plugins/v2-runtime/config-watcher.js.map +0 -1
  282. package/dist/plugins/v2-runtime/index.d.ts +0 -50
  283. package/dist/plugins/v2-runtime/index.js +0 -114
  284. package/dist/plugins/v2-runtime/index.js.map +0 -1
  285. package/dist/plugins/v2-runtime/location-config.d.ts +0 -51
  286. package/dist/plugins/v2-runtime/location-config.js +0 -91
  287. package/dist/plugins/v2-runtime/location-config.js.map +0 -1
  288. package/dist/plugins/v2-runtime/setup.d.ts +0 -48
  289. package/dist/plugins/v2-runtime/setup.js +0 -174
  290. package/dist/plugins/v2-runtime/setup.js.map +0 -1
  291. package/dist/plugins/web-tools/v2.d.ts +0 -11
  292. package/dist/plugins/web-tools/v2.js +0 -99
  293. package/dist/plugins/web-tools/v2.js.map +0 -1
  294. package/dist/plugins/workflow/v2.d.ts +0 -58
  295. package/dist/plugins/workflow/v2.js +0 -181
  296. package/dist/plugins/workflow/v2.js.map +0 -1
@@ -1,8 +1,8 @@
1
1
  // FILE: src/lib/opencode/agent-registrations.ts
2
- // VERSION: 1.0.0
2
+ // VERSION: 2.0.0
3
3
  // START_MODULE_CONTRACT
4
- // PURPOSE: Conservative vvoc-managed OpenCode agent registrations, managed prompts and skills materialization, and managed agent model IO.
5
- // SCOPE: Managed default agent and agent-map config text with role references and tool gating, skills path registration, managed prompt/skill file install and sync with managed-marker and frontmatter guards, the OpenCode skills symlink, and read/write of managed agent model overrides. Generic model/default-model/provider overrides live in model-overrides.ts.
4
+ // PURPOSE: Conservative vvoc-managed native OpenCode agent markdown, managed skills registration, and managed agent model IO.
5
+ // SCOPE: Native `default_agent`/`skills` config registration, native discovered agent markdown generation with native frontmatter, managed prompt/skill file install and sync with managed-marker guards, the OpenCode skills symlink, and read/write of managed agent model overrides. Generic model/default-model/provider overrides live in model-overrides.ts.
6
6
  // DEPENDS: [jsonc-parser, node:fs/promises, node:path, src/lib/managed-agents.ts, src/lib/managed-skills.ts, src/lib/model-roles.ts, src/lib/opencode/shared-utils.ts, src/lib/opencode/paths.ts]
7
7
  // LINKS: [M-CLI-CONFIG, M-CLI-MANAGED-AGENTS, M-CLI-MANAGED-SKILLS]
8
8
  // ROLE: RUNTIME
@@ -12,30 +12,31 @@
12
12
  // START_MODULE_MAP
13
13
  // ManagedAgentModelMap - Map of managed agent names to model selections.
14
14
  // ManagedAgentOverrideMap - Map of agent override configs.
15
- // ensureManagedAgentRegistrationsConfigText - Ensures OpenCode config contains the vvoc-managed default agent, agent registrations, and tool gating.
16
- // syncManagedAgentRegistrations - Syncs the canonical vvoc-managed OpenCode agent registrations and tool gating into OpenCode config.
17
- // installManagedAgentPrompts - Creates managed vvoc prompt files for the bundled Guardian and managed OpenCode agents when missing.
18
- // syncManagedAgentPrompts - Rewrites managed vvoc prompt files for the bundled Guardian and managed OpenCode agents.
15
+ // ensureManagedAgentRegistrationsConfigText - Ensures native OpenCode config contains the managed default agent and skills registration.
16
+ // syncManagedAgentRegistrations - Syncs the canonical native default agent and managed skills registration into OpenCode config.
17
+ // installManagedAgentPrompts - Creates managed native agent markdown and the guardian prompt body when missing.
18
+ // syncManagedAgentPrompts - Rewrites managed native agent markdown and the guardian prompt body.
19
19
  // installManagedSkillFiles - Creates managed vvoc skill files from bundled templates.
20
20
  // syncManagedSkillFiles - Rewrites managed vvoc skill files from bundled templates.
21
- // ensureManagedSkillSymlink - Creates symlink from OpenCode skills dir to vvoc skills dir for skill discovery.
22
- // readManagedAgentModels - Reads model overrides for the bundled vvoc-managed OpenCode agents from OpenCode config.
21
+ // ensureManagedSkillSymlink - Creates symlink from the native OpenCode skills dir to the vvoc skills dir for skill discovery.
22
+ // readManagedAgentModels - Reads model overrides for the bundled vvoc-managed OpenCode agents from native config.
23
23
  // readManagedAgentOverrides - Reads model overrides for the bundled vvoc-managed OpenCode agents.
24
- // writeManagedAgentModel - Writes or removes a bundled vvoc-managed OpenCode agent model override in OpenCode config.
25
- // readAgentMap - Reads the agent object map from a parsed OpenCode config.
26
- // ensureAgentConfigText - Ensures an OpenCode config document with an agent object exists.
24
+ // writeManagedAgentModel - Writes or removes a bundled vvoc-managed OpenCode agent model override in native config.
25
+ // readAgentMap - Reads the native `agents` object map from a parsed OpenCode config.
26
+ // ensureAgentConfigText - Ensures an OpenCode config document with a native `agents` object exists.
27
27
  // END_MODULE_MAP
28
28
  //
29
29
  // START_CHANGE_SUMMARY
30
- // LAST_CHANGE: [C-OPENCODE-MODULE-SPLIT - Extracted managed agent registrations, prompts, skills, symlink, and managed model IO from the former src/lib/opencode.ts monolith into this zone module.]
30
+ // LAST_CHANGE: [C-OPENCODE-V2-NATIVE T-007 - Replaced V1 inline agent prompt/permission registration with native discovered agent markdown plus native default_agent and skills registration.]
31
31
  // END_CHANGE_SUMMARY
32
32
  import { applyEdits, format, modify } from "jsonc-parser";
33
33
  import { lstat, mkdir, readlink, symlink, unlink } from "node:fs/promises";
34
34
  import { dirname, join, relative, resolve } from "node:path";
35
- import { MANAGED_AGENT_PROMPT_NAMES, MANAGED_OPENCODE_AGENTS, getManagedAgentPromptPath, getManagedOpenCodeAgentDefinition, loadManagedAgentPromptTemplate, } from "../managed-agents.js";
35
+ import { MANAGED_NATIVE_AGENT_NAMES, MANAGED_OPENCODE_AGENTS, getManagedAgentPromptPath, getManagedNativeAgentFrontmatter, loadManagedAgentPromptTemplate, } from "../managed-agents.js";
36
36
  import { MANAGED_SKILL_NAMES, getManagedSkillFilePath, listManagedSkillReferenceNames, loadManagedSkillReference, loadManagedSkillTemplate, } from "../managed-skills.js";
37
- import { getBuiltInRoleBindings, ROLE_REFERENCE_PREFIX } from "../model-roles.js";
38
- import { ensureOpenCodeConfigText, ensureTrailingNewline, hasYamlFrontmatter, isManagedFile, OPENCODE_SCHEMA_URL, parseObjectDocument, readAgentOverride, readOptionalObject, readOptionalText, readStringArray, renderJson, stripMarkdownFrontmatter, updateAgentEntryText, writeText, } from "./shared-utils.js";
37
+ import { isRoleReference } from "../model-roles.js";
38
+ import { getPinnedPackageSpecifier } from "../package.js";
39
+ import { applyModelIntent, assertNativeOpenCodeDocument, ensureOpenCodeConfigText, ensureTrailingNewline, hasYamlFrontmatter, isManagedFile, OPENCODE_SCHEMA_URL, parseObjectDocument, readAgentOverride, readModelIntentEnvelope, readNativeAgents, readOptionalText, readSkillsArray, removeTextFile, renderJson, stripMarkdownFrontmatter, updateAgentEntryText, writeText, } from "./shared-utils.js";
39
40
  import { getGlobalOpencodeSkillsDir, getGlobalVvocDir, getVvocSkillsDir } from "../vvoc-paths.js";
40
41
  const MANAGED_DEFAULT_AGENT = "vv-controller";
41
42
  const JSON_FORMAT = {
@@ -45,40 +46,16 @@ const JSON_FORMAT = {
45
46
  };
46
47
  // START_BLOCK_ENSURE_MANAGED_AGENT_CONFIG
47
48
  export function ensureManagedAgentRegistrationsConfigText(text, paths) {
48
- const builtInRoleBindings = getBuiltInRoleBindings();
49
- const rootRoleRefs = {
50
- model: createRoleReference(builtInRoleBindings.opencodeDefaults.model),
51
- small_model: createRoleReference(builtInRoleBindings.opencodeDefaults.smallModel),
52
- };
53
- const builtInAgentModelRefs = {
54
- explore: createRoleReference(builtInRoleBindings.opencodeAgents.explore),
55
- };
49
+ const managedSkillsPath = getManagedSkillsPathReference(paths);
56
50
  if (!text?.trim()) {
57
- const managedRegistrations = Object.fromEntries(MANAGED_OPENCODE_AGENTS.map((definition) => [
58
- definition.name,
59
- getManagedOpenCodeAgentRegistration(paths, definition.name),
60
- ]));
61
51
  return renderJson({
62
52
  $schema: OPENCODE_SCHEMA_URL,
63
- model: rootRoleRefs.model,
64
- small_model: rootRoleRefs.small_model,
65
53
  default_agent: MANAGED_DEFAULT_AGENT,
66
- tools: {
67
- apply_patch: false,
68
- },
69
- agent: {
70
- ...Object.fromEntries(Object.entries(builtInAgentModelRefs).map(([name, model]) => [name, { model }])),
71
- ...managedRegistrations,
72
- },
73
- skills: {
74
- paths: [getManagedSkillsPathReference(paths)],
75
- },
76
- command: {},
54
+ skills: [managedSkillsPath],
77
55
  });
78
56
  }
79
57
  const document = parseObjectDocument(text, "OpenCode config");
80
- const currentAgents = readAgentMap(document, "OpenCode config");
81
- const currentTools = readOptionalObject(document, "tools", "OpenCode config");
58
+ assertNativeOpenCodeDocument(document, "OpenCode config", assetValidationContext(paths));
82
59
  let nextText = text;
83
60
  if (!Object.hasOwn(document, "$schema")) {
84
61
  nextText = applyEdits(nextText, modify(nextText, ["$schema"], OPENCODE_SCHEMA_URL, {
@@ -86,106 +63,333 @@ export function ensureManagedAgentRegistrationsConfigText(text, paths) {
86
63
  getInsertionIndex: () => 0,
87
64
  }));
88
65
  }
89
- if (document.model !== rootRoleRefs.model) {
90
- nextText = applyEdits(nextText, modify(nextText, ["model"], rootRoleRefs.model, {
91
- formattingOptions: JSON_FORMAT,
92
- }));
93
- }
94
- if (document.small_model !== rootRoleRefs.small_model) {
95
- nextText = applyEdits(nextText, modify(nextText, ["small_model"], rootRoleRefs.small_model, {
96
- formattingOptions: JSON_FORMAT,
97
- }));
98
- }
99
66
  if (document.default_agent !== MANAGED_DEFAULT_AGENT) {
100
67
  nextText = applyEdits(nextText, modify(nextText, ["default_agent"], MANAGED_DEFAULT_AGENT, {
101
68
  formattingOptions: JSON_FORMAT,
102
69
  }));
103
70
  }
104
- if (currentTools?.apply_patch !== false) {
105
- nextText = applyEdits(nextText, modify(nextText, ["tools", "apply_patch"], false, {
106
- formattingOptions: JSON_FORMAT,
107
- }));
71
+ nextText = ensureManagedSkillsPathConfigText(nextText, paths);
72
+ return ensureTrailingNewline(applyEdits(nextText, format(nextText, undefined, JSON_FORMAT)));
73
+ }
74
+ export async function syncManagedAgentRegistrations(paths) {
75
+ const currentText = await readOptionalText(paths.opencodeConfigPath);
76
+ const nextText = ensureManagedAgentRegistrationsConfigText(currentText, paths);
77
+ if (currentText === nextText) {
78
+ return { path: paths.opencodeConfigPath, changed: false };
108
79
  }
109
- for (const [agentName, modelRef] of Object.entries(builtInAgentModelRefs)) {
110
- const currentEntry = currentAgents[agentName];
111
- if (!currentEntry) {
112
- nextText = applyEdits(nextText, modify(nextText, ["agent", agentName], { model: modelRef }, {
113
- formattingOptions: JSON_FORMAT,
114
- }));
80
+ await writeText(paths.opencodeConfigPath, nextText);
81
+ return { path: paths.opencodeConfigPath, changed: true };
82
+ }
83
+ // END_BLOCK_ENSURE_MANAGED_AGENT_CONFIG
84
+ // START_BLOCK_MANAGED_AGENT_MARKDOWN
85
+ /** Managed frontmatter keys vvoc generates by default; users may override them. */
86
+ const MANAGED_AGENT_FRONTMATTER_KEYS = new Set([
87
+ "description",
88
+ "mode",
89
+ "hidden",
90
+ "steps",
91
+ "permissions",
92
+ ]);
93
+ /**
94
+ * Renders a native discovered agent markdown file. Native frontmatter keys the
95
+ * user defined inline are omitted so the inline registration stays effective,
96
+ * and an existing managed file's frontmatter is preserved verbatim as
97
+ * user-owned while only the managed body is refreshed. The
98
+ * `<!-- Managed by vvoc ... -->` header stays in the body so `isManagedFile`
99
+ * can distinguish vvoc-managed bodies.
100
+ */
101
+ function renderNativeAgentMarkdown(agentName, template, options = {}) {
102
+ const inner = options.preservedFrontmatter !== undefined && options.preservedFrontmatter !== ""
103
+ ? stripFrontmatterKeys(options.preservedFrontmatter.replace(/\n+$/, ""), options.omitKeys)
104
+ : renderNativeAgentFrontmatterInner(getManagedNativeAgentFrontmatter(agentName), options.omitKeys);
105
+ const body = stripMarkdownFrontmatter(template).trim();
106
+ return `---\n${inner}\n---\n${MANAGED_FILE_HEADER}${body}\n`;
107
+ }
108
+ /**
109
+ * Removes only the named top-level frontmatter keys (and their indented
110
+ * continuation lines) so an inline override wins without wiping the user's
111
+ * other preserved keys.
112
+ */
113
+ function stripFrontmatterKeys(inner, omitKeys) {
114
+ if (omitKeys === undefined || omitKeys.size === 0)
115
+ return inner;
116
+ const out = [];
117
+ let skipping = false;
118
+ for (const line of inner.split("\n")) {
119
+ const topKey = /^([A-Za-z_][A-Za-z0-9_-]*)\s*:/.exec(line);
120
+ if (topKey !== null) {
121
+ skipping = omitKeys.has(topKey[1]);
122
+ if (skipping)
123
+ continue;
124
+ out.push(line);
115
125
  continue;
116
126
  }
117
- if (currentEntry.model !== modelRef) {
118
- nextText = applyEdits(nextText, modify(nextText, ["agent", agentName, "model"], modelRef, {
119
- formattingOptions: JSON_FORMAT,
120
- }));
127
+ if (skipping && (line.startsWith(" ") || line.startsWith("\t") || line.trim() === "")) {
128
+ continue;
121
129
  }
130
+ skipping = false;
131
+ out.push(line);
122
132
  }
123
- for (const definition of MANAGED_OPENCODE_AGENTS) {
124
- const currentEntry = currentAgents[definition.name];
125
- const registration = getManagedOpenCodeAgentRegistration(paths, definition.name);
126
- if (!currentEntry) {
127
- nextText = applyEdits(nextText, modify(nextText, ["agent", definition.name], registration, {
128
- formattingOptions: JSON_FORMAT,
129
- }));
133
+ return out.join("\n").replace(/\n+$/, "");
134
+ }
135
+ /** Returns the inner frontmatter text of a markdown file, without the `---` fences. */
136
+ function extractFrontmatterBlock(text) {
137
+ const match = text.replaceAll("\r\n", "\n").match(/^---\n([\s\S]*?)\n---(?:\n|$)/);
138
+ return match?.[1];
139
+ }
140
+ const MANAGED_FILE_HEADER = [
141
+ "<!-- Managed by vvoc.",
142
+ "`vvoc sync` rewrites files with this marker while preserving agent registration and model settings elsewhere.",
143
+ "Remove this comment if you want to manage the file manually.",
144
+ "-->",
145
+ "",
146
+ ].join("\n");
147
+ function renderNativeAgentFrontmatterInner(frontmatter, omitKeys) {
148
+ const omit = (key) => omitKeys?.has(key) === true;
149
+ const lines = [];
150
+ if (!omit("description"))
151
+ lines.push(`description: ${quoteYaml(frontmatter.description)}`);
152
+ if (!omit("mode"))
153
+ lines.push(`mode: ${frontmatter.mode}`);
154
+ if (frontmatter.hidden !== undefined && !omit("hidden")) {
155
+ lines.push(`hidden: ${frontmatter.hidden ? "true" : "false"}`);
156
+ }
157
+ if (frontmatter.steps !== undefined && !omit("steps")) {
158
+ lines.push(`steps: ${frontmatter.steps}`);
159
+ }
160
+ if (frontmatter.permissions && frontmatter.permissions.length > 0 && !omit("permissions")) {
161
+ lines.push("permissions:");
162
+ for (const rule of frontmatter.permissions) {
163
+ lines.push(` - action: ${quoteYaml(rule.action)}`);
164
+ lines.push(` resource: ${quoteYaml(rule.resource)}`);
165
+ lines.push(` effect: ${quoteYaml(rule.effect)}`);
166
+ }
167
+ }
168
+ return lines.join("\n");
169
+ }
170
+ /** Canonical inner frontmatter for one managed agent, used to detect user edits. */
171
+ function canonicalFrontmatterInner(agentName) {
172
+ return renderNativeAgentFrontmatterInner(getManagedNativeAgentFrontmatter(agentName));
173
+ }
174
+ function isCanonicalManagedFrontmatter(text, agentName) {
175
+ const inner = extractFrontmatterBlock(text);
176
+ if (inner === undefined)
177
+ return false;
178
+ return inner.replace(/\n+$/, "") === canonicalFrontmatterInner(agentName).replace(/\n+$/, "");
179
+ }
180
+ function quoteYaml(value) {
181
+ return JSON.stringify(value);
182
+ }
183
+ /** Reads native inline `agents.<id>` overrides that must stay effective over generated markdown. */
184
+ function assetValidationContext(paths) {
185
+ return typeof paths.opencodeConfigPath === "string" && paths.opencodeConfigPath
186
+ ? { configDir: dirname(paths.opencodeConfigPath) }
187
+ : {};
188
+ }
189
+ /** Fail closed on a malformed existing config before any asset write. */
190
+ async function assertOpencodeConfigForAssets(paths) {
191
+ if (typeof paths.opencodeConfigPath !== "string" || !paths.opencodeConfigPath)
192
+ return;
193
+ const text = await readOptionalText(paths.opencodeConfigPath);
194
+ if (!text)
195
+ return;
196
+ const document = parseObjectDocument(text, paths.opencodeConfigPath);
197
+ assertNativeOpenCodeDocument(document, paths.opencodeConfigPath, assetValidationContext(paths));
198
+ }
199
+ async function readInlineManagedAgentOverrides(paths) {
200
+ const overrides = new Map();
201
+ if (typeof paths.opencodeConfigPath !== "string" || !paths.opencodeConfigPath)
202
+ return overrides;
203
+ const text = await readOptionalText(paths.opencodeConfigPath);
204
+ if (!text)
205
+ return overrides;
206
+ const document = parseObjectDocument(text, paths.opencodeConfigPath);
207
+ assertNativeOpenCodeDocument(document, paths.opencodeConfigPath);
208
+ const agents = readNativeAgents(document, paths.opencodeConfigPath);
209
+ for (const agentName of MANAGED_NATIVE_AGENT_NAMES) {
210
+ const entry = agents[agentName];
211
+ if (!entry)
212
+ continue;
213
+ overrides.set(agentName, {
214
+ definesSystem: Object.hasOwn(entry, "system"),
215
+ managedKeys: new Set(Object.keys(entry).filter((key) => MANAGED_AGENT_FRONTMATTER_KEYS.has(key))),
216
+ });
217
+ }
218
+ return overrides;
219
+ }
220
+ /**
221
+ * Reconciles inline `agents.<id>.system` overrides with generated markdown.
222
+ * Native markdown loads after inline JSON and would shadow the inline system,
223
+ * so a canonical vvoc-managed file is removed (vvoc-owned and regenerable) and
224
+ * a user-customized file fails closed before any write instead of guessing.
225
+ */
226
+ async function resolveInlineSystemActions(paths, inlineOverrides) {
227
+ const actions = new Map();
228
+ for (const [agentName, override] of inlineOverrides) {
229
+ if (!override.definesSystem)
230
+ continue;
231
+ const agentPath = getManagedAgentPromptPath(paths.managedAgentsDirPath, agentName);
232
+ const text = await readOptionalText(agentPath);
233
+ if (!text || !isManagedFile(text)) {
234
+ actions.set(agentName, "skip");
130
235
  continue;
131
236
  }
132
- for (const [field, nextValue] of Object.entries(registration)) {
133
- if (JSON.stringify(currentEntry[field]) === JSON.stringify(nextValue)) {
134
- continue;
135
- }
136
- nextText = applyEdits(nextText, modify(nextText, ["agent", definition.name, field], nextValue, {
137
- formattingOptions: JSON_FORMAT,
138
- }));
237
+ if (isCanonicalManagedFrontmatter(text, agentName)) {
238
+ actions.set(agentName, "delete");
239
+ continue;
139
240
  }
241
+ throw new Error(`inline agents.${agentName}.system conflicts with the user-customized managed markdown at ${agentPath}; remove one source before syncing`);
140
242
  }
141
- return ensureManagedSkillsPathConfigText(ensureTrailingNewline(applyEdits(nextText, format(nextText, undefined, JSON_FORMAT))), paths);
243
+ return actions;
142
244
  }
143
- export async function syncManagedAgentRegistrations(paths) {
144
- const currentText = await readOptionalText(paths.opencodeConfigPath);
145
- const nextText = ensureManagedAgentRegistrationsConfigText(currentText, paths);
146
- if (currentText === nextText) {
147
- return { path: paths.opencodeConfigPath, changed: false };
245
+ async function applyInlineSystemAction(agentPath, action) {
246
+ if (action === "delete") {
247
+ const removed = await removeTextFile(agentPath);
248
+ return [
249
+ {
250
+ action: removed ? "deleted" : "kept",
251
+ path: agentPath,
252
+ reason: "inline agent registration owns the system prompt",
253
+ },
254
+ ];
148
255
  }
149
- await writeText(paths.opencodeConfigPath, nextText);
150
- return { path: paths.opencodeConfigPath, changed: true };
256
+ return [
257
+ {
258
+ action: "skipped",
259
+ path: agentPath,
260
+ reason: "inline agent registration owns the system prompt",
261
+ },
262
+ ];
151
263
  }
152
- // END_BLOCK_ENSURE_MANAGED_AGENT_CONFIG
153
264
  export async function installManagedAgentPrompts(paths, options) {
265
+ await assertOpencodeConfigForAssets(paths);
154
266
  const results = [];
155
- for (const agentName of MANAGED_AGENT_PROMPT_NAMES) {
156
- const promptPath = getManagedPromptPath(paths, agentName);
157
- const currentText = await readOptionalText(promptPath);
267
+ const inlineOverrides = await readInlineManagedAgentOverrides(paths);
268
+ const inlineSystem = await resolveInlineSystemActions(paths, inlineOverrides);
269
+ for (const agentName of MANAGED_NATIVE_AGENT_NAMES) {
270
+ const agentPath = getManagedAgentPromptPath(paths.managedAgentsDirPath, agentName);
271
+ const systemAction = inlineSystem.get(agentName);
272
+ if (systemAction !== undefined) {
273
+ results.push(...(await applyInlineSystemAction(agentPath, systemAction)));
274
+ continue;
275
+ }
276
+ const override = inlineOverrides.get(agentName);
277
+ const currentText = await readOptionalText(agentPath);
158
278
  if (!currentText) {
159
- await writeText(promptPath, await renderManagedPrompt(agentName));
160
- results.push({ action: "created", path: promptPath });
279
+ await writeText(agentPath, await renderNativeAgentMarkdownForName(agentName, {
280
+ omitKeys: override?.managedKeys,
281
+ }));
282
+ results.push({ action: "created", path: agentPath });
161
283
  continue;
162
284
  }
163
285
  if (!options.force) {
164
286
  if (!isManagedFile(currentText)) {
165
287
  results.push({
166
288
  action: "skipped",
167
- path: promptPath,
289
+ path: agentPath,
168
290
  reason: "existing file is not managed by vvoc",
169
291
  });
170
292
  }
171
293
  else {
172
- results.push({ action: "kept", path: promptPath });
294
+ results.push({ action: "kept", path: agentPath });
173
295
  }
174
296
  continue;
175
297
  }
176
- results.push(await syncManagedPrompt(paths, agentName, options));
298
+ results.push(await syncManagedPrompt(paths, agentName, options, override));
177
299
  }
300
+ results.push(...(await syncGuardianPromptBody(paths, { force: options.force, installOnly: true })));
178
301
  return results;
179
302
  }
180
303
  export async function syncManagedAgentPrompts(paths, options) {
304
+ await assertOpencodeConfigForAssets(paths);
181
305
  const results = [];
182
- for (const agentName of MANAGED_AGENT_PROMPT_NAMES) {
183
- results.push(await syncManagedPrompt(paths, agentName, options));
306
+ const inlineOverrides = await readInlineManagedAgentOverrides(paths);
307
+ const inlineSystem = await resolveInlineSystemActions(paths, inlineOverrides);
308
+ for (const agentName of MANAGED_NATIVE_AGENT_NAMES) {
309
+ const agentPath = getManagedAgentPromptPath(paths.managedAgentsDirPath, agentName);
310
+ const systemAction = inlineSystem.get(agentName);
311
+ if (systemAction !== undefined) {
312
+ results.push(...(await applyInlineSystemAction(agentPath, systemAction)));
313
+ continue;
314
+ }
315
+ const override = inlineOverrides.get(agentName);
316
+ results.push(await syncManagedPrompt(paths, agentName, options, override));
184
317
  }
318
+ results.push(...(await syncGuardianPromptBody(paths, { force: options.force, installOnly: false })));
185
319
  return results;
186
320
  }
321
+ async function renderNativeAgentMarkdownForName(agentName, options = {}) {
322
+ return renderNativeAgentMarkdown(agentName, await loadManagedAgentPromptTemplate(agentName), options);
323
+ }
324
+ async function syncManagedPrompt(paths, agentName, options, inlineOverride) {
325
+ const agentPath = getManagedAgentPromptPath(paths.managedAgentsDirPath, agentName);
326
+ const currentText = await readOptionalText(agentPath);
327
+ if (!currentText) {
328
+ await writeText(agentPath, await renderNativeAgentMarkdownForName(agentName, {
329
+ omitKeys: inlineOverride?.managedKeys,
330
+ }));
331
+ return { action: "created", path: agentPath };
332
+ }
333
+ if (!options.force && !isManagedFile(currentText)) {
334
+ return {
335
+ action: "skipped",
336
+ path: agentPath,
337
+ reason: "existing file is not managed by vvoc",
338
+ };
339
+ }
340
+ // Preserve user-owned frontmatter while refreshing the managed body.
341
+ const preservedFrontmatter = isManagedFile(currentText)
342
+ ? extractFrontmatterBlock(currentText)
343
+ : undefined;
344
+ const nextText = await renderNativeAgentMarkdownForName(agentName, {
345
+ preservedFrontmatter,
346
+ omitKeys: inlineOverride?.managedKeys,
347
+ });
348
+ if (currentText === nextText) {
349
+ return { action: "kept", path: agentPath };
350
+ }
351
+ await writeText(agentPath, nextText);
352
+ return { action: "updated", path: agentPath };
353
+ }
354
+ /**
355
+ * The Guardian plugin loads `.vvoc/agents/guardian.md` as raw prompt text, so
356
+ * the prompt body is materialized there separately from the native discovered
357
+ * agent markdown.
358
+ */
359
+ async function syncGuardianPromptBody(paths, options) {
360
+ const guardianPath = getManagedAgentPromptPath(paths.vvocAgentsDirPath, "guardian");
361
+ const currentText = await readOptionalText(guardianPath);
362
+ if (!currentText) {
363
+ await writeText(guardianPath, await renderManagedPrompt("guardian"));
364
+ return [{ action: "created", path: guardianPath }];
365
+ }
366
+ if (!options.force && !isManagedFile(currentText)) {
367
+ return [
368
+ {
369
+ action: "skipped",
370
+ path: guardianPath,
371
+ reason: "existing file is not managed by vvoc",
372
+ },
373
+ ];
374
+ }
375
+ if (options.installOnly) {
376
+ return [{ action: isManagedFile(currentText) ? "kept" : "skipped", path: guardianPath }];
377
+ }
378
+ const nextText = await renderManagedPrompt("guardian");
379
+ if (currentText === nextText) {
380
+ return [{ action: "kept", path: guardianPath }];
381
+ }
382
+ await writeText(guardianPath, nextText);
383
+ return [{ action: "updated", path: guardianPath }];
384
+ }
385
+ async function renderManagedPrompt(agentName) {
386
+ const template = stripMarkdownFrontmatter(await loadManagedAgentPromptTemplate(agentName)).trim();
387
+ return `${MANAGED_FILE_HEADER}${template}\n`;
388
+ }
389
+ // END_BLOCK_MANAGED_AGENT_MARKDOWN
187
390
  // START_BLOCK_MANAGED_SKILL_FUNCTIONS
188
391
  export async function installManagedSkillFiles(paths, options) {
392
+ await assertOpencodeConfigForAssets(paths);
189
393
  const results = [];
190
394
  for (const skillName of MANAGED_SKILL_NAMES) {
191
395
  const skillPath = getManagedSkillFilePath(paths.managedSkillsDirPath, skillName);
@@ -216,6 +420,7 @@ export async function installManagedSkillFiles(paths, options) {
216
420
  return results;
217
421
  }
218
422
  export async function syncManagedSkillFiles(paths, options) {
423
+ await assertOpencodeConfigForAssets(paths);
219
424
  const results = [];
220
425
  for (const skillName of MANAGED_SKILL_NAMES) {
221
426
  const skillResult = await syncManagedSkill(paths, skillName, options);
@@ -272,8 +477,21 @@ export async function readManagedAgentOverrides(paths) {
272
477
  }
273
478
  const document = parseObjectDocument(currentText, paths.opencodeConfigPath);
274
479
  const agentMap = readAgentMap(document, paths.opencodeConfigPath);
480
+ const envelope = readModelIntentEnvelope(document);
481
+ const envelopeAgents = envelope?.agents;
275
482
  for (const definition of MANAGED_OPENCODE_AGENTS) {
276
- overrides[definition.name] = readAgentOverride(agentMap[definition.name], definition.name);
483
+ const literal = readAgentOverride(agentMap[definition.name], definition.name);
484
+ if (literal.model !== undefined) {
485
+ overrides[definition.name] = literal;
486
+ continue;
487
+ }
488
+ const envelopeModel = envelopeAgents && typeof envelopeAgents === "object" && !Array.isArray(envelopeAgents)
489
+ ? envelopeAgents[definition.name]
490
+ : undefined;
491
+ overrides[definition.name] =
492
+ typeof envelopeModel === "string" && envelopeModel.trim()
493
+ ? { model: envelopeModel.trim() }
494
+ : {};
277
495
  }
278
496
  return overrides;
279
497
  }
@@ -289,23 +507,23 @@ export async function writeManagedAgentModel(paths, agentName, options) {
289
507
  if (!baseText) {
290
508
  return { action: "kept", path: paths.opencodeConfigPath };
291
509
  }
292
- const document = parseObjectDocument(baseText, paths.opencodeConfigPath);
293
- const agentMap = readAgentMap(document, paths.opencodeConfigPath);
294
- const currentEntry = agentMap[agentName];
295
- if (!currentEntry && !options.ensureEntry) {
296
- return { action: "kept", path: paths.opencodeConfigPath };
297
- }
298
- const nextEntry = {
299
- ...getManagedOpenCodeAgentRegistration(paths, agentName),
300
- ...currentEntry,
301
- };
302
- if (options.model) {
303
- nextEntry.model = options.model;
510
+ const specifier = await getPinnedPackageSpecifier();
511
+ const model = options.model?.trim() || undefined;
512
+ let nextText;
513
+ if (model !== undefined && isRoleReference(model)) {
514
+ // Role intent never becomes a native model literal; keep it in the envelope.
515
+ nextText = applyModelIntent(baseText, specifier, paths.opencodeConfigPath, {
516
+ agents: { [agentName]: model },
517
+ }, assetValidationContext(paths));
518
+ nextText = clearManagedAgentLiteralModel(nextText, paths.opencodeConfigPath, agentName);
304
519
  }
305
520
  else {
306
- delete nextEntry.model;
521
+ nextText = applyModelIntent(baseText, specifier, paths.opencodeConfigPath, {
522
+ agents: { [agentName]: null },
523
+ }, assetValidationContext(paths));
524
+ nextText = updateManagedAgentLiteralModel(nextText, paths.opencodeConfigPath, agentName, model);
307
525
  }
308
- const nextText = updateAgentEntryText(baseText, agentName, nextEntry);
526
+ assertNativeOpenCodeDocument(parseObjectDocument(nextText, paths.opencodeConfigPath), paths.opencodeConfigPath, assetValidationContext(paths));
309
527
  if ((currentText ?? "") === nextText) {
310
528
  return { action: "kept", path: paths.opencodeConfigPath };
311
529
  }
@@ -315,113 +533,84 @@ export async function writeManagedAgentModel(paths, agentName, options) {
315
533
  path: paths.opencodeConfigPath,
316
534
  };
317
535
  }
536
+ function updateManagedAgentLiteralModel(text, label, agentName, model) {
537
+ const document = parseObjectDocument(text, label);
538
+ const agentMap = readAgentMap(document, label);
539
+ const nextEntry = { ...agentMap[agentName] };
540
+ if (model === undefined)
541
+ delete nextEntry.model;
542
+ else
543
+ nextEntry.model = model;
544
+ return Object.keys(nextEntry).length === 0
545
+ ? removeAgentEntryText(text, agentName)
546
+ : updateAgentEntryText(text, agentName, nextEntry);
547
+ }
548
+ function clearManagedAgentLiteralModel(text, label, agentName) {
549
+ const document = parseObjectDocument(text, label);
550
+ const agentMap = readAgentMap(document, label);
551
+ const current = agentMap[agentName];
552
+ if (current === undefined || current.model === undefined)
553
+ return text;
554
+ const nextEntry = { ...current };
555
+ delete nextEntry.model;
556
+ return Object.keys(nextEntry).length === 0
557
+ ? removeAgentEntryText(text, agentName)
558
+ : updateAgentEntryText(text, agentName, nextEntry);
559
+ }
318
560
  // END_BLOCK_MANAGED_AGENT_MODEL_IO
319
561
  // START_BLOCK_MANAGED_AGENT_HELPERS
320
562
  export function readAgentMap(document, label) {
321
- const raw = document.agent;
322
- if (raw === undefined) {
323
- return {};
324
- }
325
- if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
326
- throw new Error(`${label}: expected "agent" to be an object`);
327
- }
328
- const entries = {};
329
- for (const [name, value] of Object.entries(raw)) {
330
- if (!value || typeof value !== "object" || Array.isArray(value)) {
331
- throw new Error(`${label}: expected "agent.${name}" to be an object`);
332
- }
333
- entries[name] = value;
334
- }
335
- return entries;
563
+ return readNativeAgents(document, label);
564
+ }
565
+ export function ensureAgentConfigText(text, context = {}) {
566
+ return ensureAgentConfigTextInternal(text, context);
336
567
  }
337
- export function ensureAgentConfigText(text) {
338
- const nextText = ensureOpenCodeConfigText(text);
568
+ function ensureAgentConfigTextInternal(text, context) {
569
+ const nextText = ensureOpenCodeConfigText(text, context);
339
570
  const document = parseObjectDocument(nextText, "OpenCode config");
340
571
  const currentAgents = readAgentMap(document, "OpenCode config");
341
572
  let nextAgentText = nextText;
342
- if (!Object.hasOwn(document, "agent")) {
343
- nextAgentText = applyEdits(nextAgentText, modify(nextAgentText, ["agent"], currentAgents, {
573
+ if (!Object.hasOwn(document, "agents")) {
574
+ nextAgentText = applyEdits(nextAgentText, modify(nextAgentText, ["agents"], currentAgents, {
344
575
  formattingOptions: JSON_FORMAT,
345
576
  }));
346
577
  }
347
578
  return ensureTrailingNewline(applyEdits(nextAgentText, format(nextAgentText, undefined, JSON_FORMAT)));
348
579
  }
349
- function getManagedPromptPath(paths, agentName) {
350
- return getManagedAgentPromptPath(paths.managedAgentsDirPath, agentName);
351
- }
352
- function getManagedOpenCodeAgentPromptReference(paths, agentName) {
353
- const promptPath = getManagedPromptPath(paths, agentName);
354
- const promptRef = relative(dirname(paths.opencodeConfigPath), promptPath).replaceAll("\\", "/");
355
- return `{file:${promptRef.startsWith(".") ? promptRef : `./${promptRef}`}}`;
580
+ function removeAgentEntryText(text, agentName) {
581
+ const nextText = applyEdits(text, modify(text, ["agents", agentName], undefined, {
582
+ formattingOptions: JSON_FORMAT,
583
+ }));
584
+ return ensureTrailingNewline(applyEdits(nextText, format(nextText, undefined, JSON_FORMAT)));
356
585
  }
357
586
  function getManagedSkillsPathReference(paths) {
358
- const skillsRef = relative(dirname(paths.opencodeConfigPath), paths.managedSkillsDirPath).replaceAll("\\", "/");
587
+ // Native resolves relative `skills` entries against the location directory
588
+ // (the project root), not the config file's directory. A global skills
589
+ // directory lives outside the location, so it must be registered absolutely;
590
+ // a project one is registered relative to the project root without leaking
591
+ // the global layer.
592
+ const baseDir = paths.scope === "project"
593
+ ? (paths.projectRoot ?? dirname(paths.opencodeConfigPath))
594
+ : undefined;
595
+ if (baseDir === undefined) {
596
+ return paths.managedSkillsDirPath;
597
+ }
598
+ const skillsRef = relative(resolve(baseDir), paths.managedSkillsDirPath).replaceAll("\\", "/");
359
599
  return skillsRef.startsWith(".") ? skillsRef : `./${skillsRef}`;
360
600
  }
361
601
  function ensureManagedSkillsPathConfigText(text, paths) {
362
602
  const document = parseObjectDocument(text, "OpenCode config");
363
- const skills = readOptionalObject(document, "skills", "OpenCode config");
364
- const rawPaths = skills?.paths;
365
- const currentPaths = rawPaths === undefined ? [] : readStringArray(rawPaths, "OpenCode config: skills.paths");
603
+ const currentPaths = readSkillsArray(document, "OpenCode config");
366
604
  const managedSkillsPath = getManagedSkillsPathReference(paths);
367
605
  if (currentPaths.includes(managedSkillsPath)) {
368
606
  return text;
369
607
  }
370
608
  const nextPaths = [...currentPaths, managedSkillsPath];
371
- const nextText = applyEdits(text, modify(text, ["skills", "paths"], nextPaths, {
609
+ const nextText = applyEdits(text, modify(text, ["skills"], nextPaths, {
372
610
  formattingOptions: JSON_FORMAT,
373
611
  }));
374
612
  return ensureTrailingNewline(applyEdits(nextText, format(nextText, undefined, JSON_FORMAT)));
375
613
  }
376
- function getManagedOpenCodeAgentRegistration(paths, agentName) {
377
- const definition = getManagedOpenCodeAgentDefinition(agentName);
378
- const builtInBindings = getBuiltInRoleBindings();
379
- const registration = {
380
- description: definition.description,
381
- mode: definition.mode,
382
- prompt: getManagedOpenCodeAgentPromptReference(paths, agentName),
383
- model: createRoleReference(builtInBindings.managedAgents[agentName]),
384
- };
385
- if (definition.permission) {
386
- registration.permission = definition.permission;
387
- }
388
- return registration;
389
- }
390
- async function renderManagedPrompt(agentName) {
391
- const template = stripMarkdownFrontmatter(await loadManagedAgentPromptTemplate(agentName)).trim();
392
- const header = [
393
- "<!-- Managed by vvoc.",
394
- "`vvoc sync` rewrites files with this marker while preserving agent registration and model settings elsewhere.",
395
- "Remove this comment if you want to manage the file manually.",
396
- "-->",
397
- "",
398
- ].join("\n");
399
- return `${header}${template}\n`;
400
- }
401
- async function syncManagedPrompt(paths, agentName, options) {
402
- const promptPath = getManagedPromptPath(paths, agentName);
403
- const currentText = await readOptionalText(promptPath);
404
- if (!currentText) {
405
- await writeText(promptPath, await renderManagedPrompt(agentName));
406
- return { action: "created", path: promptPath };
407
- }
408
- if (!options.force && !isManagedFile(currentText)) {
409
- return {
410
- action: "skipped",
411
- path: promptPath,
412
- reason: "existing file is not managed by vvoc",
413
- };
414
- }
415
- const nextText = await renderManagedPrompt(agentName);
416
- if (currentText === nextText) {
417
- return { action: "kept", path: promptPath };
418
- }
419
- await writeText(promptPath, nextText);
420
- return { action: "updated", path: promptPath };
421
- }
422
- async function renderManagedSkill(skillName) {
423
- return loadManagedSkillTemplate(skillName);
424
- }
425
614
  async function syncManagedSkillReferences(skillsDirPath, skillName) {
426
615
  const results = [];
427
616
  const referenceNames = await listManagedSkillReferenceNames(skillName);
@@ -462,8 +651,8 @@ async function syncManagedSkill(paths, skillName, options) {
462
651
  await writeText(skillPath, nextText);
463
652
  return { action: "updated", path: skillPath };
464
653
  }
465
- function createRoleReference(roleId) {
466
- return `${ROLE_REFERENCE_PREFIX}${roleId}`;
654
+ async function renderManagedSkill(skillName) {
655
+ return loadManagedSkillTemplate(skillName);
467
656
  }
468
657
  // END_BLOCK_MANAGED_AGENT_HELPERS
469
658
  //# sourceMappingURL=agent-registrations.js.map