@lesliechan721/aw 0.1.0-beta.10 → 0.1.0-beta.100

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 (820) hide show
  1. package/dist/authoring/assets/aw-config.d.mts +1 -50
  2. package/dist/authoring/assets/aw-config.mjs +58 -928
  3. package/dist/authoring/assets/aw-config.mjs.map +1 -1
  4. package/dist/authoring/assets/aw-runtime-config.d.mts +13 -0
  5. package/dist/authoring/assets/aw-runtime-config.mjs +221 -0
  6. package/dist/authoring/assets/aw-runtime-config.mjs.map +1 -0
  7. package/dist/authoring/assets/command-runtime-config.d.mts +9 -0
  8. package/dist/authoring/assets/command-runtime-config.mjs +134 -0
  9. package/dist/authoring/assets/command-runtime-config.mjs.map +1 -0
  10. package/dist/authoring/scripts/add-skill.mjs +16 -39
  11. package/dist/authoring/scripts/add-skill.mjs.map +1 -1
  12. package/dist/authoring/scripts/init-plugin.mjs +43 -71
  13. package/dist/authoring/scripts/init-plugin.mjs.map +1 -1
  14. package/dist/authoring/scripts/lib/plugin-root.mjs +4 -4
  15. package/dist/authoring/scripts/lib/plugin-root.mjs.map +1 -1
  16. package/dist/authoring/scripts/lib/plugin-version-check.d.mts +1 -0
  17. package/dist/authoring/scripts/lib/plugin-version-check.mjs +230 -9
  18. package/dist/authoring/scripts/lib/plugin-version-check.mjs.map +1 -1
  19. package/dist/authoring/scripts/lib/skill-scaffold.d.mts +3 -3
  20. package/dist/authoring/scripts/lib/skill-scaffold.mjs +5 -8
  21. package/dist/authoring/scripts/lib/skill-scaffold.mjs.map +1 -1
  22. package/dist/authoring/scripts/remove-plugin.mjs +1 -1
  23. package/dist/authoring/scripts/remove-plugin.mjs.map +1 -1
  24. package/dist/authoring/scripts/validate-plugin.mjs +408 -1748
  25. package/dist/authoring/scripts/validate-plugin.mjs.map +1 -1
  26. package/dist/authoring/scripts/validate-skill.mjs +1 -1
  27. package/dist/index.js +2 -6
  28. package/dist/index.js.map +1 -1
  29. package/dist/native-runtime/reader-runtime.mjs +34 -0
  30. package/dist/native-runtime/reader.d.mts +6 -0
  31. package/dist/native-runtime/reader.mjs +11 -0
  32. package/dist/scripts/aw-skill-eval-materializer.d.ts +11 -1
  33. package/dist/scripts/aw-skill-eval-materializer.js +119 -114
  34. package/dist/scripts/aw-skill-eval-materializer.js.map +1 -1
  35. package/dist/scripts/bundle-plugins.js +32 -7
  36. package/dist/scripts/bundle-plugins.js.map +1 -1
  37. package/dist/scripts/plugin-staging.d.ts +1 -1
  38. package/dist/scripts/plugin-staging.js +5 -2
  39. package/dist/scripts/plugin-staging.js.map +1 -1
  40. package/dist/scripts/plugin-test-context-guard.d.mts +1 -0
  41. package/dist/scripts/plugin-test-context-guard.mjs +59 -0
  42. package/dist/scripts/plugin-test-context-guard.mjs.map +1 -0
  43. package/dist/scripts/plugin-test-reporter.d.mts +2 -0
  44. package/dist/scripts/plugin-test-reporter.mjs +21 -0
  45. package/dist/scripts/plugin-test-reporter.mjs.map +1 -0
  46. package/dist/scripts/test-plugin-scripts.d.ts +5 -0
  47. package/dist/scripts/test-plugin-scripts.js +343 -58
  48. package/dist/scripts/test-plugin-scripts.js.map +1 -1
  49. package/dist/scripts/test-workers.d.mts +24 -0
  50. package/dist/scripts/test-workers.mjs +59 -0
  51. package/dist/scripts/test-workers.mjs.map +1 -0
  52. package/dist/src/adapters/helpers.d.ts +7 -0
  53. package/dist/src/adapters/helpers.js.map +1 -1
  54. package/dist/src/authoring/workspace-commands.d.ts +3 -0
  55. package/dist/src/authoring/workspace-commands.js +7 -2
  56. package/dist/src/authoring/workspace-commands.js.map +1 -1
  57. package/dist/src/commands/aw-capabilities.d.ts +1 -1
  58. package/dist/src/commands/aw-capabilities.js +48 -19
  59. package/dist/src/commands/aw-capabilities.js.map +1 -1
  60. package/dist/src/commands/catalog-dev-source.d.ts +40 -3
  61. package/dist/src/commands/catalog-dev-source.js +333 -42
  62. package/dist/src/commands/catalog-dev-source.js.map +1 -1
  63. package/dist/src/commands/catalog-generation.d.ts +5 -0
  64. package/dist/src/commands/catalog-generation.js.map +1 -1
  65. package/dist/src/commands/catalog-publication-inspection.d.ts +11 -3
  66. package/dist/src/commands/catalog-publication-inspection.js +211 -59
  67. package/dist/src/commands/catalog-publication-inspection.js.map +1 -1
  68. package/dist/src/commands/catalog-publication-journal.d.ts +12 -0
  69. package/dist/src/commands/catalog-publication-journal.js +78 -1
  70. package/dist/src/commands/catalog-publication-journal.js.map +1 -1
  71. package/dist/src/commands/catalog-publish.d.ts +21 -1
  72. package/dist/src/commands/catalog-publish.js +133 -92
  73. package/dist/src/commands/catalog-publish.js.map +1 -1
  74. package/dist/src/commands/catalog-source.d.ts +25 -5
  75. package/dist/src/commands/catalog-source.js +153 -27
  76. package/dist/src/commands/catalog-source.js.map +1 -1
  77. package/dist/src/commands/catalog-status.js +2 -2
  78. package/dist/src/commands/catalog-status.js.map +1 -1
  79. package/dist/src/commands/catalog-store.d.ts +31 -7
  80. package/dist/src/commands/catalog-store.js +379 -42
  81. package/dist/src/commands/catalog-store.js.map +1 -1
  82. package/dist/src/commands/catalog-workspace-resolution.d.ts +4 -0
  83. package/dist/src/commands/catalog-workspace-resolution.js +18 -3
  84. package/dist/src/commands/catalog-workspace-resolution.js.map +1 -1
  85. package/dist/src/commands/console.js +33 -20
  86. package/dist/src/commands/console.js.map +1 -1
  87. package/dist/src/commands/dependency-operation-plan.d.ts +7 -0
  88. package/dist/src/commands/dependency-operation-plan.js +66 -30
  89. package/dist/src/commands/dependency-operation-plan.js.map +1 -1
  90. package/dist/src/commands/deployment-confirmation.d.ts +11 -0
  91. package/dist/src/commands/deployment-confirmation.js +101 -0
  92. package/dist/src/commands/deployment-confirmation.js.map +1 -0
  93. package/dist/src/commands/init.js +1 -1
  94. package/dist/src/commands/init.js.map +1 -1
  95. package/dist/src/commands/intent-command.d.ts +34 -0
  96. package/dist/src/commands/intent-command.js +174 -0
  97. package/dist/src/commands/intent-command.js.map +1 -0
  98. package/dist/src/commands/lifecycle-command-package.d.ts +45 -0
  99. package/dist/src/commands/lifecycle-command-package.js +199 -0
  100. package/dist/src/commands/lifecycle-command-package.js.map +1 -0
  101. package/dist/src/commands/managed-commands.d.ts +41 -0
  102. package/dist/src/commands/managed-commands.js +404 -0
  103. package/dist/src/commands/managed-commands.js.map +1 -0
  104. package/dist/src/commands/offline-cleanup-evidence.d.ts +12 -2
  105. package/dist/src/commands/offline-cleanup-evidence.js +57 -35
  106. package/dist/src/commands/offline-cleanup-evidence.js.map +1 -1
  107. package/dist/src/commands/operation-plan.d.ts +72 -2
  108. package/dist/src/commands/operation-plan.js +54 -3
  109. package/dist/src/commands/operation-plan.js.map +1 -1
  110. package/dist/src/commands/operation-planner.d.ts +6 -5
  111. package/dist/src/commands/operation-planner.js +23 -20
  112. package/dist/src/commands/operation-planner.js.map +1 -1
  113. package/dist/src/commands/options.d.ts +11 -16
  114. package/dist/src/commands/options.js +31 -25
  115. package/dist/src/commands/options.js.map +1 -1
  116. package/dist/src/commands/permission-batch.d.ts +5 -0
  117. package/dist/src/commands/permission-batch.js +83 -0
  118. package/dist/src/commands/permission-batch.js.map +1 -0
  119. package/dist/src/commands/plugin-capabilities.js +128 -58
  120. package/dist/src/commands/plugin-capabilities.js.map +1 -1
  121. package/dist/src/commands/public-config-confirmation.d.ts +14 -0
  122. package/dist/src/commands/public-config-confirmation.js +73 -0
  123. package/dist/src/commands/public-config-confirmation.js.map +1 -0
  124. package/dist/src/commands/public-interruption.d.ts +9 -0
  125. package/dist/src/commands/public-interruption.js +23 -0
  126. package/dist/src/commands/public-interruption.js.map +1 -0
  127. package/dist/src/commands/public-lifecycle-result.d.ts +106 -0
  128. package/dist/src/commands/public-lifecycle-result.js +536 -0
  129. package/dist/src/commands/public-lifecycle-result.js.map +1 -0
  130. package/dist/src/commands/public-lifecycle.d.ts +27 -0
  131. package/dist/src/commands/public-lifecycle.js +569 -0
  132. package/dist/src/commands/public-lifecycle.js.map +1 -0
  133. package/dist/src/commands/public-plugin-query.d.ts +16 -0
  134. package/dist/src/commands/public-plugin-query.js +231 -0
  135. package/dist/src/commands/public-plugin-query.js.map +1 -0
  136. package/dist/src/commands/rebuild-command.d.ts +177 -16
  137. package/dist/src/commands/rebuild-command.js +3370 -1559
  138. package/dist/src/commands/rebuild-command.js.map +1 -1
  139. package/dist/src/commands/rebuild-integration.d.ts +100 -55
  140. package/dist/src/commands/rebuild-integration.js +866 -3275
  141. package/dist/src/commands/rebuild-integration.js.map +1 -1
  142. package/dist/src/commands/rebuild-operations.d.ts +68 -13
  143. package/dist/src/commands/rebuild-operations.js +309 -120
  144. package/dist/src/commands/rebuild-operations.js.map +1 -1
  145. package/dist/src/commands/rebuild-platform-integration.d.ts +99 -0
  146. package/dist/src/commands/rebuild-platform-integration.js +2329 -0
  147. package/dist/src/commands/rebuild-platform-integration.js.map +1 -0
  148. package/dist/src/commands/reconcile-operation-plan.d.ts +14 -9
  149. package/dist/src/commands/reconcile-operation-plan.js +26 -6
  150. package/dist/src/commands/reconcile-operation-plan.js.map +1 -1
  151. package/dist/src/commands/review-inspection.d.ts +60 -0
  152. package/dist/src/commands/review-inspection.js +437 -0
  153. package/dist/src/commands/review-inspection.js.map +1 -0
  154. package/dist/src/commands/review-preparation.d.ts +51 -0
  155. package/dist/src/commands/review-preparation.js +400 -0
  156. package/dist/src/commands/review-preparation.js.map +1 -0
  157. package/dist/src/commands/runtime.d.ts +5 -1
  158. package/dist/src/commands/runtime.js +41 -8
  159. package/dist/src/commands/runtime.js.map +1 -1
  160. package/dist/src/commands/skill-eval.d.ts +8 -0
  161. package/dist/src/commands/skill-eval.js +126 -73
  162. package/dist/src/commands/skill-eval.js.map +1 -1
  163. package/dist/src/commands/status-v6.d.ts +148 -35
  164. package/dist/src/commands/status-v6.js +1166 -307
  165. package/dist/src/commands/status-v6.js.map +1 -1
  166. package/dist/src/commands/targets-command.d.ts +38 -0
  167. package/dist/src/commands/targets-command.js +182 -0
  168. package/dist/src/commands/targets-command.js.map +1 -0
  169. package/dist/src/commands/wizard.d.ts +7 -3
  170. package/dist/src/commands/wizard.js +7 -4
  171. package/dist/src/commands/wizard.js.map +1 -1
  172. package/dist/src/commands/write-commands.d.ts +7 -1
  173. package/dist/src/commands/write-commands.js +129 -38
  174. package/dist/src/commands/write-commands.js.map +1 -1
  175. package/dist/src/constants.d.ts +2 -3
  176. package/dist/src/constants.js +2 -3
  177. package/dist/src/constants.js.map +1 -1
  178. package/dist/src/eval/manifest-v1.d.ts +6 -3
  179. package/dist/src/eval/manifest-v1.js +44 -33
  180. package/dist/src/eval/manifest-v1.js.map +1 -1
  181. package/dist/src/generators/local-plugin.d.ts +4 -2
  182. package/dist/src/generators/local-plugin.js +319 -353
  183. package/dist/src/generators/local-plugin.js.map +1 -1
  184. package/dist/src/generators/permissions.d.ts +20 -2
  185. package/dist/src/generators/permissions.js +82 -15
  186. package/dist/src/generators/permissions.js.map +1 -1
  187. package/dist/src/index.d.ts +2 -33
  188. package/dist/src/index.js +1575 -1919
  189. package/dist/src/index.js.map +1 -1
  190. package/dist/src/intent/deployment-preferences.d.ts +63 -0
  191. package/dist/src/intent/deployment-preferences.js +170 -0
  192. package/dist/src/intent/deployment-preferences.js.map +1 -0
  193. package/dist/src/intent/diagnostics.d.ts +11 -0
  194. package/dist/src/intent/diagnostics.js +14 -0
  195. package/dist/src/intent/diagnostics.js.map +1 -0
  196. package/dist/src/intent/intent-codec.d.ts +19 -0
  197. package/dist/src/intent/intent-codec.js +117 -0
  198. package/dist/src/intent/intent-codec.js.map +1 -0
  199. package/dist/src/intent/intent-store.d.ts +43 -0
  200. package/dist/src/intent/intent-store.js +118 -0
  201. package/dist/src/intent/intent-store.js.map +1 -0
  202. package/dist/src/intent/scope-context.d.ts +35 -0
  203. package/dist/src/intent/scope-context.js +179 -0
  204. package/dist/src/intent/scope-context.js.map +1 -0
  205. package/dist/src/intent/work-context.d.ts +13 -0
  206. package/dist/src/intent/work-context.js +22 -0
  207. package/dist/src/intent/work-context.js.map +1 -0
  208. package/dist/src/output/command-receipt.d.ts +6 -2
  209. package/dist/src/output/command-receipt.js +67 -32
  210. package/dist/src/output/command-receipt.js.map +1 -1
  211. package/dist/src/output/component-delivery.d.ts +28 -0
  212. package/dist/src/output/component-delivery.js +141 -0
  213. package/dist/src/output/component-delivery.js.map +1 -0
  214. package/dist/src/output/public-result.d.ts +69 -0
  215. package/dist/src/output/public-result.js +403 -0
  216. package/dist/src/output/public-result.js.map +1 -0
  217. package/dist/src/output/rebuild-progress.d.ts +3 -0
  218. package/dist/src/output/rebuild-progress.js +34 -9
  219. package/dist/src/output/rebuild-progress.js.map +1 -1
  220. package/dist/src/output/receipt-projection.d.ts +7 -0
  221. package/dist/src/output/receipt-projection.js +664 -0
  222. package/dist/src/output/receipt-projection.js.map +1 -0
  223. package/dist/src/output/skill-invocation-notices.d.ts +18 -0
  224. package/dist/src/output/skill-invocation-notices.js +84 -0
  225. package/dist/src/output/skill-invocation-notices.js.map +1 -0
  226. package/dist/src/platforms/antigravity-permissions.d.ts +50 -0
  227. package/dist/src/platforms/antigravity-permissions.js +388 -0
  228. package/dist/src/platforms/antigravity-permissions.js.map +1 -0
  229. package/dist/src/platforms/capabilities.d.ts +9 -0
  230. package/dist/src/platforms/capabilities.js +125 -0
  231. package/dist/src/platforms/capabilities.js.map +1 -0
  232. package/dist/src/platforms/contracts.d.ts +394 -0
  233. package/dist/src/platforms/contracts.js +2 -0
  234. package/dist/src/platforms/contracts.js.map +1 -0
  235. package/dist/src/platforms/cursor-workspace-hook.d.ts +28 -0
  236. package/dist/src/platforms/cursor-workspace-hook.js +198 -0
  237. package/dist/src/platforms/cursor-workspace-hook.js.map +1 -0
  238. package/dist/src/platforms/discovery.d.ts +100 -0
  239. package/dist/src/platforms/discovery.js +779 -0
  240. package/dist/src/platforms/discovery.js.map +1 -0
  241. package/dist/src/platforms/grok-permission-config.d.ts +23 -0
  242. package/dist/src/platforms/grok-permission-config.js +184 -0
  243. package/dist/src/platforms/grok-permission-config.js.map +1 -0
  244. package/dist/src/platforms/grok-permission-document.d.ts +4 -0
  245. package/dist/src/platforms/grok-permission-document.js +145 -0
  246. package/dist/src/platforms/grok-permission-document.js.map +1 -0
  247. package/dist/src/platforms/grok-permissions.d.ts +17 -0
  248. package/dist/src/platforms/grok-permissions.js +311 -0
  249. package/dist/src/platforms/grok-permissions.js.map +1 -0
  250. package/dist/src/platforms/host-permission-policy.d.ts +38 -0
  251. package/dist/src/platforms/host-permission-policy.js +86 -0
  252. package/dist/src/platforms/host-permission-policy.js.map +1 -0
  253. package/dist/src/platforms/identity.d.ts +4 -0
  254. package/dist/src/platforms/identity.js +22 -0
  255. package/dist/src/platforms/identity.js.map +1 -0
  256. package/dist/src/platforms/inspection-grok.d.ts +4 -0
  257. package/dist/src/platforms/inspection-grok.js +380 -0
  258. package/dist/src/platforms/inspection-grok.js.map +1 -0
  259. package/dist/src/platforms/inspection-native.d.ts +8 -0
  260. package/dist/src/platforms/inspection-native.js +584 -0
  261. package/dist/src/platforms/inspection-native.js.map +1 -0
  262. package/dist/src/platforms/inspection-opencode.d.ts +4 -0
  263. package/dist/src/platforms/inspection-opencode.js +98 -0
  264. package/dist/src/platforms/inspection-opencode.js.map +1 -0
  265. package/dist/src/platforms/inspection-pi.d.ts +9 -0
  266. package/dist/src/platforms/inspection-pi.js +490 -0
  267. package/dist/src/platforms/inspection-pi.js.map +1 -0
  268. package/dist/src/platforms/inspection-query.d.ts +26 -0
  269. package/dist/src/platforms/inspection-query.js +118 -0
  270. package/dist/src/platforms/inspection-query.js.map +1 -0
  271. package/dist/src/platforms/inspection.d.ts +20 -0
  272. package/dist/src/platforms/inspection.js +259 -0
  273. package/dist/src/platforms/inspection.js.map +1 -0
  274. package/dist/src/platforms/layout.d.ts +35 -0
  275. package/dist/src/platforms/layout.js +332 -0
  276. package/dist/src/platforms/layout.js.map +1 -0
  277. package/dist/src/platforms/lifecycle-native-services.d.ts +3 -0
  278. package/dist/src/platforms/lifecycle-native-services.js +213 -0
  279. package/dist/src/platforms/lifecycle-native-services.js.map +1 -0
  280. package/dist/src/platforms/lifecycle-native.d.ts +14 -0
  281. package/dist/src/platforms/lifecycle-native.js +234 -0
  282. package/dist/src/platforms/lifecycle-native.js.map +1 -0
  283. package/dist/src/platforms/lifecycle.d.ts +29 -0
  284. package/dist/src/platforms/lifecycle.js +688 -0
  285. package/dist/src/platforms/lifecycle.js.map +1 -0
  286. package/dist/src/platforms/opencode-permission-config.d.ts +13 -0
  287. package/dist/src/platforms/opencode-permission-config.js +253 -0
  288. package/dist/src/platforms/opencode-permission-config.js.map +1 -0
  289. package/dist/src/platforms/opencode-permissions.d.ts +39 -0
  290. package/dist/src/platforms/opencode-permissions.js +212 -0
  291. package/dist/src/platforms/opencode-permissions.js.map +1 -0
  292. package/dist/src/platforms/opencode-related-permissions.d.ts +40 -0
  293. package/dist/src/platforms/opencode-related-permissions.js +177 -0
  294. package/dist/src/platforms/opencode-related-permissions.js.map +1 -0
  295. package/dist/src/platforms/permission-ownership.d.ts +89 -0
  296. package/dist/src/platforms/permission-ownership.js +543 -0
  297. package/dist/src/platforms/permission-ownership.js.map +1 -0
  298. package/dist/src/platforms/permission-sidecar.d.ts +70 -0
  299. package/dist/src/platforms/permission-sidecar.js +215 -0
  300. package/dist/src/platforms/permission-sidecar.js.map +1 -0
  301. package/dist/src/platforms/projection-code-extension.d.ts +8 -0
  302. package/dist/src/platforms/projection-code-extension.js +126 -0
  303. package/dist/src/platforms/projection-code-extension.js.map +1 -0
  304. package/dist/src/platforms/projection-command-resources.d.ts +7 -0
  305. package/dist/src/platforms/projection-command-resources.js +74 -0
  306. package/dist/src/platforms/projection-command-resources.js.map +1 -0
  307. package/dist/src/platforms/projection-files.d.ts +28 -0
  308. package/dist/src/platforms/projection-files.js +113 -0
  309. package/dist/src/platforms/projection-files.js.map +1 -0
  310. package/dist/src/platforms/projection-marketplace.d.ts +19 -0
  311. package/dist/src/platforms/projection-marketplace.js +42 -0
  312. package/dist/src/platforms/projection-marketplace.js.map +1 -0
  313. package/dist/src/platforms/projection.d.ts +13 -0
  314. package/dist/src/platforms/projection.js +578 -0
  315. package/dist/src/platforms/projection.js.map +1 -0
  316. package/dist/src/platforms/read-only-retry-policy.d.ts +34 -0
  317. package/dist/src/platforms/read-only-retry-policy.js +70 -0
  318. package/dist/src/platforms/read-only-retry-policy.js.map +1 -0
  319. package/dist/src/platforms/registry.d.ts +4 -0
  320. package/dist/src/platforms/registry.js +30 -0
  321. package/dist/src/platforms/registry.js.map +1 -0
  322. package/dist/src/platforms/retirement-native.d.ts +6 -0
  323. package/dist/src/platforms/retirement-native.js +95 -0
  324. package/dist/src/platforms/retirement-native.js.map +1 -0
  325. package/dist/src/platforms/supplement-layout.d.ts +20 -0
  326. package/dist/src/platforms/supplement-layout.js +54 -0
  327. package/dist/src/platforms/supplement-layout.js.map +1 -0
  328. package/dist/src/platforms/supplement-state.d.ts +13 -0
  329. package/dist/src/platforms/supplement-state.js +93 -0
  330. package/dist/src/platforms/supplement-state.js.map +1 -0
  331. package/dist/src/portable/commands.d.ts +60 -11
  332. package/dist/src/portable/commands.js +88 -346
  333. package/dist/src/portable/commands.js.map +1 -1
  334. package/dist/src/portable/historical-materialization.d.ts +25 -0
  335. package/dist/src/portable/historical-materialization.js +148 -0
  336. package/dist/src/portable/historical-materialization.js.map +1 -0
  337. package/dist/src/portable/materializer.d.ts +46 -2
  338. package/dist/src/portable/materializer.js +262 -259
  339. package/dist/src/portable/materializer.js.map +1 -1
  340. package/dist/src/portable/recovery-layout.d.ts +17 -0
  341. package/dist/src/portable/recovery-layout.js +42 -0
  342. package/dist/src/portable/recovery-layout.js.map +1 -0
  343. package/dist/src/portable/recovery.d.ts +177 -0
  344. package/dist/src/portable/recovery.js +597 -0
  345. package/dist/src/portable/recovery.js.map +1 -0
  346. package/dist/src/portable/writer.d.ts +67 -0
  347. package/dist/src/portable/writer.js +321 -0
  348. package/dist/src/portable/writer.js.map +1 -0
  349. package/dist/src/recovery/coordinator.d.ts +175 -0
  350. package/dist/src/recovery/coordinator.js +1624 -0
  351. package/dist/src/recovery/coordinator.js.map +1 -0
  352. package/dist/src/recovery/demand.d.ts +53 -0
  353. package/dist/src/recovery/demand.js +189 -0
  354. package/dist/src/recovery/demand.js.map +1 -0
  355. package/dist/src/recovery/install-concurrency.d.ts +48 -0
  356. package/dist/src/recovery/install-concurrency.js +196 -0
  357. package/dist/src/recovery/install-concurrency.js.map +1 -0
  358. package/dist/src/recovery/interruption.d.ts +19 -0
  359. package/dist/src/recovery/interruption.js +43 -0
  360. package/dist/src/recovery/interruption.js.map +1 -0
  361. package/dist/src/recovery/joint-coordinator.d.ts +46 -0
  362. package/dist/src/recovery/joint-coordinator.js +1723 -0
  363. package/dist/src/recovery/joint-coordinator.js.map +1 -0
  364. package/dist/src/recovery/joint-demand.d.ts +72 -0
  365. package/dist/src/recovery/joint-demand.js +332 -0
  366. package/dist/src/recovery/joint-demand.js.map +1 -0
  367. package/dist/src/recovery/joint-install-execution.d.ts +19 -0
  368. package/dist/src/recovery/joint-install-execution.js +308 -0
  369. package/dist/src/recovery/joint-install-execution.js.map +1 -0
  370. package/dist/src/recovery/joint-install-native-rebase.d.ts +4 -0
  371. package/dist/src/recovery/joint-install-native-rebase.js +67 -0
  372. package/dist/src/recovery/joint-install-native-rebase.js.map +1 -0
  373. package/dist/src/recovery/performance.d.ts +4 -0
  374. package/dist/src/recovery/performance.js +26 -0
  375. package/dist/src/recovery/performance.js.map +1 -0
  376. package/dist/src/recovery/receipt.d.ts +131 -0
  377. package/dist/src/recovery/receipt.js +91 -0
  378. package/dist/src/recovery/receipt.js.map +1 -0
  379. package/dist/src/recovery/retirement.d.ts +47 -0
  380. package/dist/src/recovery/retirement.js +499 -0
  381. package/dist/src/recovery/retirement.js.map +1 -0
  382. package/dist/src/recovery/status.d.ts +143 -0
  383. package/dist/src/recovery/status.js +1528 -0
  384. package/dist/src/recovery/status.js.map +1 -0
  385. package/dist/src/recovery/types.d.ts +85 -0
  386. package/dist/src/recovery/types.js +2 -0
  387. package/dist/src/recovery/types.js.map +1 -0
  388. package/dist/src/review/eval-source-input.d.ts +44 -0
  389. package/dist/src/review/eval-source-input.js +213 -0
  390. package/dist/src/review/eval-source-input.js.map +1 -0
  391. package/dist/src/review/platform-profile.d.ts +5 -0
  392. package/dist/src/review/platform-profile.js +13 -0
  393. package/dist/src/review/platform-profile.js.map +1 -0
  394. package/dist/src/review/review-profile.d.ts +20 -0
  395. package/dist/src/review/review-profile.js +286 -0
  396. package/dist/src/review/review-profile.js.map +1 -0
  397. package/dist/src/review/review-selection.d.ts +20 -0
  398. package/dist/src/review/review-selection.js +502 -0
  399. package/dist/src/review/review-selection.js.map +1 -0
  400. package/dist/src/review/review-types.d.ts +71 -0
  401. package/dist/src/review/review-types.js +16 -0
  402. package/dist/src/review/review-types.js.map +1 -0
  403. package/dist/src/review/skill-materialization.d.ts +67 -0
  404. package/dist/src/review/skill-materialization.js +235 -0
  405. package/dist/src/review/skill-materialization.js.map +1 -0
  406. package/dist/src/review/source-binding.d.ts +203 -0
  407. package/dist/src/review/source-binding.js +791 -0
  408. package/dist/src/review/source-binding.js.map +1 -0
  409. package/dist/src/tui/agent-management.d.ts +23 -0
  410. package/dist/src/tui/agent-management.js +319 -0
  411. package/dist/src/tui/agent-management.js.map +1 -0
  412. package/dist/src/tui/agent-settings.d.ts +47 -0
  413. package/dist/src/tui/agent-settings.js +97 -0
  414. package/dist/src/tui/agent-settings.js.map +1 -0
  415. package/dist/src/tui/catalog-generation.d.ts +9 -0
  416. package/dist/src/tui/catalog-generation.js +96 -6
  417. package/dist/src/tui/catalog-generation.js.map +1 -1
  418. package/dist/src/tui/catalog-sources/projection.d.ts +2 -2
  419. package/dist/src/tui/catalog-sources/screens.js +19 -12
  420. package/dist/src/tui/catalog-sources/screens.js.map +1 -1
  421. package/dist/src/tui/catalog-sources/session.d.ts +11 -1
  422. package/dist/src/tui/catalog-sources/session.js +80 -5
  423. package/dist/src/tui/catalog-sources/session.js.map +1 -1
  424. package/dist/src/tui/catalog-sources/types.d.ts +15 -2
  425. package/dist/src/tui/catalog-store-task.d.ts +16 -0
  426. package/dist/src/tui/catalog-store-task.js +19 -0
  427. package/dist/src/tui/catalog-store-task.js.map +1 -0
  428. package/dist/src/tui/clipboard.d.ts +13 -0
  429. package/dist/src/tui/clipboard.js +95 -0
  430. package/dist/src/tui/clipboard.js.map +1 -0
  431. package/dist/src/tui/components.d.ts +113 -3
  432. package/dist/src/tui/components.js +201 -34
  433. package/dist/src/tui/components.js.map +1 -1
  434. package/dist/src/tui/config-array-draft.d.ts +30 -0
  435. package/dist/src/tui/config-array-draft.js +51 -0
  436. package/dist/src/tui/config-array-draft.js.map +1 -0
  437. package/dist/src/tui/config-leaf-editor.d.ts +44 -0
  438. package/dist/src/tui/config-leaf-editor.js +227 -0
  439. package/dist/src/tui/config-leaf-editor.js.map +1 -0
  440. package/dist/src/tui/config-object-suggestions.d.ts +15 -0
  441. package/dist/src/tui/config-object-suggestions.js +71 -0
  442. package/dist/src/tui/config-object-suggestions.js.map +1 -0
  443. package/dist/src/tui/config-references.d.ts +38 -0
  444. package/dist/src/tui/config-references.js +82 -0
  445. package/dist/src/tui/config-references.js.map +1 -0
  446. package/dist/src/tui/config-suggestions.d.ts +27 -0
  447. package/dist/src/tui/config-suggestions.js +62 -0
  448. package/dist/src/tui/config-suggestions.js.map +1 -0
  449. package/dist/src/tui/config-value-presentation.d.ts +23 -0
  450. package/dist/src/tui/config-value-presentation.js +151 -0
  451. package/dist/src/tui/config-value-presentation.js.map +1 -0
  452. package/dist/src/tui/console-app-v4.d.ts +14 -5
  453. package/dist/src/tui/console-app-v4.js +9779 -2488
  454. package/dist/src/tui/console-app-v4.js.map +1 -1
  455. package/dist/src/tui/content-item-card.d.ts +13 -0
  456. package/dist/src/tui/content-item-card.js +51 -0
  457. package/dist/src/tui/content-item-card.js.map +1 -0
  458. package/dist/src/tui/controller-runtime.d.ts +4 -3
  459. package/dist/src/tui/controller-runtime.js +1610 -590
  460. package/dist/src/tui/controller-runtime.js.map +1 -1
  461. package/dist/src/tui/controller.d.ts +583 -80
  462. package/dist/src/tui/controller.js +3362 -263
  463. package/dist/src/tui/controller.js.map +1 -1
  464. package/dist/src/tui/dependency-graph-view.d.ts +39 -0
  465. package/dist/src/tui/dependency-graph-view.js +121 -0
  466. package/dist/src/tui/dependency-graph-view.js.map +1 -0
  467. package/dist/src/tui/dependency-graph.d.ts +81 -0
  468. package/dist/src/tui/dependency-graph.js +452 -0
  469. package/dist/src/tui/dependency-graph.js.map +1 -0
  470. package/dist/src/tui/deployment-view.d.ts +14 -0
  471. package/dist/src/tui/deployment-view.js +175 -0
  472. package/dist/src/tui/deployment-view.js.map +1 -0
  473. package/dist/src/tui/diagnostic-projection.d.ts +31 -0
  474. package/dist/src/tui/diagnostic-projection.js +681 -0
  475. package/dist/src/tui/diagnostic-projection.js.map +1 -0
  476. package/dist/src/tui/diagnostics.d.ts +6 -2
  477. package/dist/src/tui/diagnostics.js +14 -2
  478. package/dist/src/tui/diagnostics.js.map +1 -1
  479. package/dist/src/tui/entity-cards.d.ts +56 -0
  480. package/dist/src/tui/entity-cards.js +327 -0
  481. package/dist/src/tui/entity-cards.js.map +1 -0
  482. package/dist/src/tui/glossary.d.ts +25 -0
  483. package/dist/src/tui/glossary.js +78 -0
  484. package/dist/src/tui/glossary.js.map +1 -0
  485. package/dist/src/tui/guided-setup.d.ts +25 -0
  486. package/dist/src/tui/guided-setup.js +101 -0
  487. package/dist/src/tui/guided-setup.js.map +1 -0
  488. package/dist/src/tui/index.d.ts +4 -1
  489. package/dist/src/tui/index.js +4 -1
  490. package/dist/src/tui/index.js.map +1 -1
  491. package/dist/src/tui/input-display.d.ts +1 -0
  492. package/dist/src/tui/input-display.js +23 -1
  493. package/dist/src/tui/input-display.js.map +1 -1
  494. package/dist/src/tui/keys.d.ts +44 -0
  495. package/dist/src/tui/keys.js +43 -15
  496. package/dist/src/tui/keys.js.map +1 -1
  497. package/dist/src/tui/lifecycle-action-view.d.ts +6 -0
  498. package/dist/src/tui/lifecycle-action-view.js +36 -0
  499. package/dist/src/tui/lifecycle-action-view.js.map +1 -0
  500. package/dist/src/tui/model.d.ts +141 -28
  501. package/dist/src/tui/model.js +968 -285
  502. package/dist/src/tui/model.js.map +1 -1
  503. package/dist/src/tui/operation-diagnostic-screens.d.ts +8 -0
  504. package/dist/src/tui/operation-diagnostic-screens.js +76 -0
  505. package/dist/src/tui/operation-diagnostic-screens.js.map +1 -0
  506. package/dist/src/tui/operation-history.d.ts +91 -0
  507. package/dist/src/tui/operation-history.js +167 -0
  508. package/dist/src/tui/operation-history.js.map +1 -0
  509. package/dist/src/tui/operation-review-planner.d.ts +7 -0
  510. package/dist/src/tui/operation-review-planner.js +980 -146
  511. package/dist/src/tui/operation-review-planner.js.map +1 -1
  512. package/dist/src/tui/plugin-content.d.ts +33 -0
  513. package/dist/src/tui/plugin-content.js +245 -0
  514. package/dist/src/tui/plugin-content.js.map +1 -0
  515. package/dist/src/tui/plugin-detail.d.ts +75 -0
  516. package/dist/src/tui/plugin-detail.js +267 -0
  517. package/dist/src/tui/plugin-detail.js.map +1 -0
  518. package/dist/src/tui/plugin-write-task.d.ts +69 -0
  519. package/dist/src/tui/plugin-write-task.js +286 -0
  520. package/dist/src/tui/plugin-write-task.js.map +1 -0
  521. package/dist/src/tui/query-header.d.ts +30 -0
  522. package/dist/src/tui/query-header.js +40 -0
  523. package/dist/src/tui/query-header.js.map +1 -0
  524. package/dist/src/tui/reader.d.ts +38 -0
  525. package/dist/src/tui/reader.js +49 -0
  526. package/dist/src/tui/reader.js.map +1 -0
  527. package/dist/src/tui/router.d.ts +170 -39
  528. package/dist/src/tui/router.js +338 -50
  529. package/dist/src/tui/router.js.map +1 -1
  530. package/dist/src/tui/scoped-flow.d.ts +49 -0
  531. package/dist/src/tui/scoped-flow.js +212 -0
  532. package/dist/src/tui/scoped-flow.js.map +1 -0
  533. package/dist/src/tui/scoped-inventory.d.ts +139 -0
  534. package/dist/src/tui/scoped-inventory.js +295 -0
  535. package/dist/src/tui/scoped-inventory.js.map +1 -0
  536. package/dist/src/tui/screen-registry.d.ts +696 -0
  537. package/dist/src/tui/screen-registry.js +226 -0
  538. package/dist/src/tui/screen-registry.js.map +1 -0
  539. package/dist/src/tui/session-console-app.d.ts +8 -0
  540. package/dist/src/tui/session-console-app.js +24 -0
  541. package/dist/src/tui/session-console-app.js.map +1 -0
  542. package/dist/src/tui/session-host.d.ts +45 -0
  543. package/dist/src/tui/session-host.js +219 -0
  544. package/dist/src/tui/session-host.js.map +1 -0
  545. package/dist/src/tui/source-detail-card.d.ts +29 -0
  546. package/dist/src/tui/source-detail-card.js +45 -0
  547. package/dist/src/tui/source-detail-card.js.map +1 -0
  548. package/dist/src/tui/theme.d.ts +12 -0
  549. package/dist/src/tui/theme.js +12 -0
  550. package/dist/src/tui/theme.js.map +1 -1
  551. package/dist/src/tui/viewport.d.ts +1 -0
  552. package/dist/src/tui/viewport.js +23 -0
  553. package/dist/src/tui/viewport.js.map +1 -1
  554. package/dist/src/tui/wizard.js +26 -4
  555. package/dist/src/tui/wizard.js.map +1 -1
  556. package/dist/src/types.d.ts +105 -83
  557. package/dist/src/types.js +1 -1
  558. package/dist/src/types.js.map +1 -1
  559. package/dist/src/utils/agent-version.d.ts +10 -4
  560. package/dist/src/utils/agent-version.js +6 -8
  561. package/dist/src/utils/agent-version.js.map +1 -1
  562. package/dist/src/utils/artifact-plan.d.ts +18 -25
  563. package/dist/src/utils/artifact-plan.js +59 -445
  564. package/dist/src/utils/artifact-plan.js.map +1 -1
  565. package/dist/src/utils/artifact-renderer.d.ts +64 -90
  566. package/dist/src/utils/artifact-renderer.js +202 -303
  567. package/dist/src/utils/artifact-renderer.js.map +1 -1
  568. package/dist/src/utils/artifact-request.d.ts +19 -14
  569. package/dist/src/utils/artifact-request.js +51 -36
  570. package/dist/src/utils/artifact-request.js.map +1 -1
  571. package/dist/src/utils/artifact-sources.d.ts +4 -8
  572. package/dist/src/utils/artifact-sources.js +15 -6
  573. package/dist/src/utils/artifact-sources.js.map +1 -1
  574. package/dist/src/utils/aw-home-mutation-lock.js +2 -1
  575. package/dist/src/utils/aw-home-mutation-lock.js.map +1 -1
  576. package/dist/src/utils/bundle-layout.d.ts +2 -0
  577. package/dist/src/utils/bundle-layout.js +88 -158
  578. package/dist/src/utils/bundle-layout.js.map +1 -1
  579. package/dist/src/utils/catalog-capability-projection.d.ts +8 -0
  580. package/dist/src/utils/catalog-capability-projection.js +14 -0
  581. package/dist/src/utils/catalog-capability-projection.js.map +1 -0
  582. package/dist/src/utils/catalog-digests.d.ts +2 -12
  583. package/dist/src/utils/catalog-digests.js +7 -55
  584. package/dist/src/utils/catalog-digests.js.map +1 -1
  585. package/dist/src/utils/catalog-generation-resolver.d.ts +28 -4
  586. package/dist/src/utils/catalog-generation-resolver.js +146 -40
  587. package/dist/src/utils/catalog-generation-resolver.js.map +1 -1
  588. package/dist/src/utils/catalog-generation-service.d.ts +24 -2
  589. package/dist/src/utils/catalog-generation-service.js +92 -48
  590. package/dist/src/utils/catalog-generation-service.js.map +1 -1
  591. package/dist/src/utils/catalog-generation-state.js +1 -1
  592. package/dist/src/utils/catalog-generation-state.js.map +1 -1
  593. package/dist/src/utils/catalog-generation-store.d.ts +4 -0
  594. package/dist/src/utils/catalog-generation-store.js +68 -10
  595. package/dist/src/utils/catalog-generation-store.js.map +1 -1
  596. package/dist/src/utils/catalog-operation-context.d.ts +61 -4
  597. package/dist/src/utils/catalog-operation-context.js +225 -4
  598. package/dist/src/utils/catalog-operation-context.js.map +1 -1
  599. package/dist/src/utils/catalog-public-error.js +4 -0
  600. package/dist/src/utils/catalog-public-error.js.map +1 -1
  601. package/dist/src/utils/catalog-runtime-projection.d.ts +4 -3
  602. package/dist/src/utils/catalog-runtime-projection.js.map +1 -1
  603. package/dist/src/utils/catalog-snapshot.d.ts +3 -0
  604. package/dist/src/utils/catalog-snapshot.js +4 -1
  605. package/dist/src/utils/catalog-snapshot.js.map +1 -1
  606. package/dist/src/utils/catalog-source-impact.d.ts +3 -2
  607. package/dist/src/utils/catalog-source-impact.js +142 -20
  608. package/dist/src/utils/catalog-source-impact.js.map +1 -1
  609. package/dist/src/utils/catalog-source-references.d.ts +57 -1
  610. package/dist/src/utils/catalog-source-references.js +755 -162
  611. package/dist/src/utils/catalog-source-references.js.map +1 -1
  612. package/dist/src/utils/catalog-sources.d.ts +1 -1
  613. package/dist/src/utils/catalog-sources.js +21 -5
  614. package/dist/src/utils/catalog-sources.js.map +1 -1
  615. package/dist/src/utils/codex-config.d.ts +3 -0
  616. package/dist/src/utils/codex-config.js +7 -0
  617. package/dist/src/utils/codex-config.js.map +1 -1
  618. package/dist/src/utils/codex-executable-discovery.d.ts +28 -0
  619. package/dist/src/utils/codex-executable-discovery.js +431 -0
  620. package/dist/src/utils/codex-executable-discovery.js.map +1 -0
  621. package/dist/src/utils/codex-manager.d.ts +23 -42
  622. package/dist/src/utils/codex-manager.js +79 -767
  623. package/dist/src/utils/codex-manager.js.map +1 -1
  624. package/dist/src/utils/codex-native-observation.d.ts +40 -1
  625. package/dist/src/utils/codex-native-observation.js +172 -0
  626. package/dist/src/utils/codex-native-observation.js.map +1 -1
  627. package/dist/src/utils/codex-reconciler.d.ts +2 -7
  628. package/dist/src/utils/codex-reconciler.js +33 -5
  629. package/dist/src/utils/codex-reconciler.js.map +1 -1
  630. package/dist/src/utils/codex-shared-native.d.ts +8 -26
  631. package/dist/src/utils/codex-shared-native.js +63 -83
  632. package/dist/src/utils/codex-shared-native.js.map +1 -1
  633. package/dist/src/utils/codex-skill-staging.d.ts +4 -3
  634. package/dist/src/utils/codex-skill-staging.js.map +1 -1
  635. package/dist/src/utils/codex-skills-root.d.ts +4 -0
  636. package/dist/src/utils/codex-skills-root.js +29 -0
  637. package/dist/src/utils/codex-skills-root.js.map +1 -0
  638. package/dist/src/utils/codex-surface-analysis.d.ts +6 -2
  639. package/dist/src/utils/codex-surface-analysis.js +31 -56
  640. package/dist/src/utils/codex-surface-analysis.js.map +1 -1
  641. package/dist/src/utils/component-agent-binding.d.ts +17 -0
  642. package/dist/src/utils/component-agent-binding.js +119 -0
  643. package/dist/src/utils/component-agent-binding.js.map +1 -0
  644. package/dist/src/utils/component-command-preparation.d.ts +33 -0
  645. package/dist/src/utils/component-command-preparation.js +129 -0
  646. package/dist/src/utils/component-command-preparation.js.map +1 -0
  647. package/dist/src/utils/component-gap.d.ts +4 -0
  648. package/dist/src/utils/component-gap.js +16 -0
  649. package/dist/src/utils/component-gap.js.map +1 -0
  650. package/dist/src/utils/component-resources.d.ts +53 -0
  651. package/dist/src/utils/component-resources.js +340 -0
  652. package/dist/src/utils/component-resources.js.map +1 -0
  653. package/dist/src/utils/config-platforms.d.ts +21 -0
  654. package/dist/src/utils/config-platforms.js +51 -0
  655. package/dist/src/utils/config-platforms.js.map +1 -0
  656. package/dist/src/utils/dynamic-skill-tree.d.ts +18 -6
  657. package/dist/src/utils/dynamic-skill-tree.js +33 -40
  658. package/dist/src/utils/dynamic-skill-tree.js.map +1 -1
  659. package/dist/src/utils/git-catalog-provider.d.ts +3 -0
  660. package/dist/src/utils/git-catalog-provider.js +49 -3
  661. package/dist/src/utils/git-catalog-provider.js.map +1 -1
  662. package/dist/src/utils/inventory-recovery-diagnostic.d.ts +4 -0
  663. package/dist/src/utils/inventory-recovery-diagnostic.js +15 -0
  664. package/dist/src/utils/inventory-recovery-diagnostic.js.map +1 -0
  665. package/dist/src/utils/local-marketplace.d.ts +19 -12
  666. package/dist/src/utils/local-marketplace.js +97 -194
  667. package/dist/src/utils/local-marketplace.js.map +1 -1
  668. package/dist/src/utils/managed-command-cli-review.d.ts +53 -0
  669. package/dist/src/utils/managed-command-cli-review.js +431 -0
  670. package/dist/src/utils/managed-command-cli-review.js.map +1 -0
  671. package/dist/src/utils/managed-command-closure.d.ts +14 -0
  672. package/dist/src/utils/managed-command-closure.js +156 -0
  673. package/dist/src/utils/managed-command-closure.js.map +1 -0
  674. package/dist/src/utils/managed-command-errors.d.ts +5 -0
  675. package/dist/src/utils/managed-command-errors.js +10 -0
  676. package/dist/src/utils/managed-command-errors.js.map +1 -0
  677. package/dist/src/utils/managed-command-materialization.d.ts +90 -0
  678. package/dist/src/utils/managed-command-materialization.js +375 -0
  679. package/dist/src/utils/managed-command-materialization.js.map +1 -0
  680. package/dist/src/utils/managed-command-preparation.d.ts +30 -0
  681. package/dist/src/utils/managed-command-preparation.js +39 -0
  682. package/dist/src/utils/managed-command-preparation.js.map +1 -0
  683. package/dist/src/utils/managed-command-review.d.ts +33 -0
  684. package/dist/src/utils/managed-command-review.js +80 -0
  685. package/dist/src/utils/managed-command-review.js.map +1 -0
  686. package/dist/src/utils/managed-command-runtime.d.ts +36 -0
  687. package/dist/src/utils/managed-command-runtime.js +126 -0
  688. package/dist/src/utils/managed-command-runtime.js.map +1 -0
  689. package/dist/src/utils/managed-command-snapshots.d.ts +24 -0
  690. package/dist/src/utils/managed-command-snapshots.js +89 -0
  691. package/dist/src/utils/managed-command-snapshots.js.map +1 -0
  692. package/dist/src/utils/managed-command-store.d.ts +141 -0
  693. package/dist/src/utils/managed-command-store.js +839 -0
  694. package/dist/src/utils/managed-command-store.js.map +1 -0
  695. package/dist/src/utils/multi-source-catalog.d.ts +6 -1
  696. package/dist/src/utils/multi-source-catalog.js +9 -4
  697. package/dist/src/utils/multi-source-catalog.js.map +1 -1
  698. package/dist/src/utils/native-cli-interruption.d.ts +8 -0
  699. package/dist/src/utils/native-cli-interruption.js +65 -0
  700. package/dist/src/utils/native-cli-interruption.js.map +1 -0
  701. package/dist/src/utils/native-cli.d.ts +37 -6
  702. package/dist/src/utils/native-cli.js +523 -60
  703. package/dist/src/utils/native-cli.js.map +1 -1
  704. package/dist/src/utils/native-demand.d.ts +29 -0
  705. package/dist/src/utils/native-demand.js +4 -0
  706. package/dist/src/utils/native-demand.js.map +1 -0
  707. package/dist/src/utils/native-tool-context.d.ts +17 -0
  708. package/dist/src/utils/native-tool-context.js +40 -0
  709. package/dist/src/utils/native-tool-context.js.map +1 -0
  710. package/dist/src/utils/native-tool-review.d.ts +8 -0
  711. package/dist/src/utils/native-tool-review.js +14 -0
  712. package/dist/src/utils/native-tool-review.js.map +1 -0
  713. package/dist/src/utils/ownership.d.ts +38 -3
  714. package/dist/src/utils/ownership.js +118 -11
  715. package/dist/src/utils/ownership.js.map +1 -1
  716. package/dist/src/utils/package-content.d.ts +7 -0
  717. package/dist/src/utils/package-content.js +70 -0
  718. package/dist/src/utils/package-content.js.map +1 -0
  719. package/dist/src/utils/path-catalog-provider.js +2 -0
  720. package/dist/src/utils/path-catalog-provider.js.map +1 -1
  721. package/dist/src/utils/paths.d.ts +1 -1
  722. package/dist/src/utils/paths.js +5 -2
  723. package/dist/src/utils/paths.js.map +1 -1
  724. package/dist/src/utils/platform-empty-state.d.ts +69 -0
  725. package/dist/src/utils/platform-empty-state.js +249 -0
  726. package/dist/src/utils/platform-empty-state.js.map +1 -0
  727. package/dist/src/utils/platform-support.d.ts +1 -1
  728. package/dist/src/utils/platform-support.js +1 -1
  729. package/dist/src/utils/platform-support.js.map +1 -1
  730. package/dist/src/utils/plugin-config-command-apply.d.ts +33 -0
  731. package/dist/src/utils/plugin-config-command-apply.js +142 -0
  732. package/dist/src/utils/plugin-config-command-apply.js.map +1 -0
  733. package/dist/src/utils/plugin-config-diagnostics.d.ts +16 -0
  734. package/dist/src/utils/plugin-config-diagnostics.js +44 -0
  735. package/dist/src/utils/plugin-config-diagnostics.js.map +1 -0
  736. package/dist/src/utils/plugin-config-impact.d.ts +76 -0
  737. package/dist/src/utils/plugin-config-impact.js +359 -0
  738. package/dist/src/utils/plugin-config-impact.js.map +1 -0
  739. package/dist/src/utils/plugin-config-references.d.ts +116 -0
  740. package/dist/src/utils/plugin-config-references.js +379 -0
  741. package/dist/src/utils/plugin-config-references.js.map +1 -0
  742. package/dist/src/utils/plugin-config-suggestion-runner.d.ts +83 -0
  743. package/dist/src/utils/plugin-config-suggestion-runner.js +308 -0
  744. package/dist/src/utils/plugin-config-suggestion-runner.js.map +1 -0
  745. package/dist/src/utils/plugin-config-suggestions.d.ts +50 -0
  746. package/dist/src/utils/plugin-config-suggestions.js +322 -0
  747. package/dist/src/utils/plugin-config-suggestions.js.map +1 -0
  748. package/dist/src/utils/plugin-config-write.d.ts +108 -19
  749. package/dist/src/utils/plugin-config-write.js +781 -568
  750. package/dist/src/utils/plugin-config-write.js.map +1 -1
  751. package/dist/src/utils/plugin-config.d.ts +122 -43
  752. package/dist/src/utils/plugin-config.js +574 -610
  753. package/dist/src/utils/plugin-config.js.map +1 -1
  754. package/dist/src/utils/plugin-dependencies.d.ts +1 -1
  755. package/dist/src/utils/plugin-dependencies.js.map +1 -1
  756. package/dist/src/utils/plugin-scripts.d.ts +41 -17
  757. package/dist/src/utils/plugin-scripts.js +586 -123
  758. package/dist/src/utils/plugin-scripts.js.map +1 -1
  759. package/dist/src/utils/process-lock.js +14 -5
  760. package/dist/src/utils/process-lock.js.map +1 -1
  761. package/dist/src/utils/process-tree.d.ts +2 -2
  762. package/dist/src/utils/process-tree.js +1 -1
  763. package/dist/src/utils/process-tree.js.map +1 -1
  764. package/dist/src/utils/project-boundary.d.ts +11 -1
  765. package/dist/src/utils/project-boundary.js +33 -11
  766. package/dist/src/utils/project-boundary.js.map +1 -1
  767. package/dist/src/utils/rendered-contract.js +24 -1
  768. package/dist/src/utils/rendered-contract.js.map +1 -1
  769. package/dist/src/utils/runtime-config.d.ts +18 -0
  770. package/dist/src/utils/runtime-config.js +64 -0
  771. package/dist/src/utils/runtime-config.js.map +1 -0
  772. package/dist/src/utils/runtime-skill-closure.d.ts +2 -2
  773. package/dist/src/utils/runtime-skill-closure.js +50 -7
  774. package/dist/src/utils/runtime-skill-closure.js.map +1 -1
  775. package/dist/src/utils/safe-operation-diagnostic.d.ts +15 -0
  776. package/dist/src/utils/safe-operation-diagnostic.js +33 -0
  777. package/dist/src/utils/safe-operation-diagnostic.js.map +1 -0
  778. package/dist/src/utils/sensitive-content.d.ts +4 -0
  779. package/dist/src/utils/sensitive-content.js +36 -0
  780. package/dist/src/utils/sensitive-content.js.map +1 -0
  781. package/dist/src/utils/skill-invocation-warning.d.ts +19 -0
  782. package/dist/src/utils/skill-invocation-warning.js +43 -0
  783. package/dist/src/utils/skill-invocation-warning.js.map +1 -0
  784. package/dist/src/utils/skill-validation.js +0 -5
  785. package/dist/src/utils/skill-validation.js.map +1 -1
  786. package/dist/src/utils/state-paths.d.ts +8 -3
  787. package/dist/src/utils/state-paths.js +53 -16
  788. package/dist/src/utils/state-paths.js.map +1 -1
  789. package/dist/src/utils/target-eligibility.d.ts +2 -2
  790. package/dist/src/utils/target-eligibility.js.map +1 -1
  791. package/dist/src/utils/windows-claude-shim.d.ts +7 -0
  792. package/dist/src/utils/windows-claude-shim.js +10 -0
  793. package/dist/src/utils/windows-claude-shim.js.map +1 -0
  794. package/dist/src/utils/windows-npm-shim.d.ts +16 -0
  795. package/dist/src/utils/windows-npm-shim.js +155 -0
  796. package/dist/src/utils/windows-npm-shim.js.map +1 -0
  797. package/dist/vitest.config.js +109 -9
  798. package/dist/vitest.config.js.map +1 -1
  799. package/package.json +20 -12
  800. package/dist/authoring/templates/aw-config-runtime.d.mts +0 -2
  801. package/dist/authoring/templates/aw-config-runtime.mjs +0 -407
  802. package/dist/authoring/templates/aw-config-runtime.mjs.map +0 -1
  803. package/dist/src/commands/review-publication.d.ts +0 -99
  804. package/dist/src/commands/review-publication.js +0 -904
  805. package/dist/src/commands/review-publication.js.map +0 -1
  806. package/dist/src/commands/sync-command.d.ts +0 -23
  807. package/dist/src/commands/sync-command.js +0 -242
  808. package/dist/src/commands/sync-command.js.map +0 -1
  809. package/dist/src/portable/skills-cli.d.ts +0 -43
  810. package/dist/src/portable/skills-cli.js +0 -148
  811. package/dist/src/portable/skills-cli.js.map +0 -1
  812. package/dist/src/utils/codex-native-render-receipt.d.ts +0 -40
  813. package/dist/src/utils/codex-native-render-receipt.js +0 -205
  814. package/dist/src/utils/codex-native-render-receipt.js.map +0 -1
  815. package/dist/src/utils/plugin-intent-state.d.ts +0 -34
  816. package/dist/src/utils/plugin-intent-state.js +0 -289
  817. package/dist/src/utils/plugin-intent-state.js.map +0 -1
  818. package/dist/src/utils/slash-commands.d.ts +0 -1
  819. package/dist/src/utils/slash-commands.js +0 -2
  820. package/dist/src/utils/slash-commands.js.map +0 -1
@@ -1,43 +1,76 @@
1
1
  import crypto from 'node:crypto';
2
- import os from 'node:os';
3
2
  import path from 'node:path';
3
+ import { catalogFileMode } from '@lesliechan721/aw-plugin-core';
4
4
  import fs from 'fs-extra';
5
- import { generateAgentArtifacts } from '../generators/agents.js';
6
- import { generateInstructionArtifacts, renderCodexManagedInstructionBlock, } from '../generators/instructions.js';
7
- import { generateCodexPermissionArtifacts, mergeClaudePermissions, } from '../generators/permissions.js';
5
+ import { ManagedCommandError } from '../utils/managed-command-store.js';
6
+ import { prepareLifecycleCommandPackage, publishLifecycleCommandPackages, retainedManagedCommandPackageResult } from './lifecycle-command-package.js';
7
+ import { readClaudeSettings, writeClaudeSettings } from '../adapters/helpers.js';
8
8
  import { buildGeneratedName } from '../generators/shared.js';
9
- import { applyConditionalCodexConfigTransactionFile, applyCodexConfigTransactionFile, composeManagedDeveloperInstructionsBlock, readCodexConfigFile, } from '../utils/codex-config.js';
10
- import { canonicalizePathThroughExistingAncestor } from '../utils/state-paths.js';
11
- import { createCodexSkillExpectedTree, createCodexSkillStagingTreeContentDigest, } from '../utils/codex-skill-staging.js';
12
- import { createDynamicSkillExpectedTree, createDynamicSkillTreeContentDigest, readDynamicSkillTree, validateSkillDirectory, writeDynamicSkillTree, } from '../utils/dynamic-skill-tree.js';
13
- import { parseCodexSharedHooksFile, serializeTrackedHookGroupIdentity, } from '../utils/hooks-contract.js';
14
- import { buildCodexMarketplaceAddInvocation, buildCodexMarketplaceListInvocation, buildCodexPluginAddInvocation, buildCodexPluginListInvocation, buildCodexPluginListJsonInvocation, buildCodexProjectPluginListInvocations, buildCodexPluginRemoveInvocation, CodexProjectConfigNotEffectiveError, parseCodexPluginListJson, postcheckCodexProjectPluginActivation, runNativeCli, runNativeCliChecked, SYSTEM_CODEX_MANAGER, } from '../utils/native-cli.js';
15
- import { OWNED_TEXT_HEADER_PREFIX, createSharedOwnerToken, createSkillOwnerMarkerContent, parseSharedOwnerToken, parseOwnedTextHeader, readSkillOwnerMarker, renderOwnedTextFile, inspectOwnedComponents, writeOwnedTextFile, } from '../utils/ownership.js';
16
- import { resolveArtifactRenderConfig, resolveArtifactRendererTarget, runArtifactRenderer, } from '../utils/artifact-renderer.js';
17
- import { createArtifactConfigDigest, validateArtifactPlan, } from '../utils/artifact-plan.js';
18
- import { resolveAwDeliveredArtifactIds } from '../utils/artifact-request.js';
19
- import { createAwPluginRequirementsReceipt, } from '../utils/manifest.js';
20
- import { resolvePlatformEntries } from '../utils/platform-entries.js';
21
- import { resolveAwHome, resolveClaudePaths, resolveCodexPaths, resolveRepoRoot, getCodexHome, } from '../utils/paths.js';
22
- import { runPostApplyScript } from '../utils/plugin-scripts.js';
23
- import { atomicRemoveConfigFile, atomicReplaceConfigFile, withConfigFileMutation, } from '../utils/plugin-config.js';
24
9
  import { readCatalogSourcesConfigSnapshot } from '../utils/catalog-sources.js';
25
- import { getNamespacedMarketplaceName, projectMarketplaceManifestForPlatform, readStrictNamespacedMarketplaceRoot, replayNamespacedCodexSkillOverlays, } from '../utils/local-marketplace.js';
26
- import { commitMarketplaceGenerationBackup } from '../utils/orphan-marketplace-cleanup.js';
27
- import { readClaudeSettings, writeClaudeSettings } from '../adapters/helpers.js';
28
- import { analyzeCodexSurface } from '../utils/codex-surface-analysis.js';
29
- import { CODEX_NATIVE_RENDER_RECEIPT_PATH, codexNativeRenderProjectionsEqual, createCodexNativeRenderProjection, createCodexNativeRenderReceipt, parseCodexNativeRenderReceipt, serializeCodexNativeRenderReceipt, } from '../utils/codex-native-render-receipt.js';
30
- import { inspectCodexSharedNative, } from '../utils/codex-shared-native.js';
31
- import { calculateCodexNativeRenderPayloadDigest, calculateCodexSharedNativePayloadDigest, } from '../utils/catalog-digests.js';
32
- import { combineCodexNativePluginObservations, observeCodexNativePlugin, } from '../utils/codex-native-observation.js';
10
+ import { applyCodexConfigTransactionFile, applyConditionalCodexConfigTransactionFile, readCodexConfigFile } from '../utils/codex-config.js';
33
11
  import { isSafeCodexMarketplaceRoot } from '../utils/codex-marketplace-path.js';
12
+ import { resolveCodexSkillsRelativeRoot } from '../utils/codex-skills-root.js';
13
+ import { createCodexNativeObservationSession, } from '../utils/codex-native-observation.js';
14
+ import { inspectCodexSharedNative, isCanonicalCodexMarketplaceRegistration, } from '../utils/codex-shared-native.js';
15
+ import { validateSkillDirectory } from '../utils/dynamic-skill-tree.js';
16
+ import { parseCodexSharedHooksFile, serializeTrackedHookGroupIdentity, } from '../utils/hooks-contract.js';
17
+ import { assertSafeNamespacedMarketplacePluginRoot, readStrictNamespacedMarketplaceRoot } from '../utils/local-marketplace.js';
18
+ import { buildCodexMarketplaceRemoveInvocation, buildCodexPluginRemoveInvocation, CodexProjectConfigNotEffectiveError, runNativeCliChecked, SYSTEM_CODEX_MANAGER } from '../utils/native-cli.js';
19
+ import { inspectOwnedComponents, OWNED_TEXT_HEADER_PREFIX, parseOwnedTextHeader, parseSharedOwnerToken, readSkillOwnerMarker } from '../utils/ownership.js';
20
+ import { resolveAwHome, resolveClaudePaths, resolveCodexPaths, resolveRepoRoot } from '../utils/paths.js';
21
+ import { atomicRemoveConfigFile, atomicReplaceConfigFile, withConfigFileMutation, } from '../utils/plugin-config.js';
22
+ import { runPreApplyScript } from '../utils/plugin-scripts.js';
34
23
  import { assertCanonicalProjectManagedRoots } from '../utils/project-boundary.js';
35
- import { findCodexRoleNameConflict, resolveCodexRoleNamespace, } from '../utils/codex-roles.js';
24
+ import { attachSafeOperationError, readSafeOperationError } from '../utils/safe-operation-diagnostic.js';
25
+ import { createProjectLocationKey, resolveRequiredCanonicalRepoRoot, resolveClaudeProjectKey, resolveCodexProjectKey } from '../utils/state-paths.js';
26
+ import { hasPermissionOwner, updateHostPermissions } from '../platforms/permission-ownership.js';
27
+ import { resolvePlatformConfigRoot } from '../platforms/discovery.js';
28
+ import { canonicalNamespaceRoot } from '../platforms/identity.js';
29
+ import { getManagedPlatformRoot } from '../platforms/layout.js';
30
+ import { platformOwnership, PlatformRebuildSession } from './rebuild-platform-integration.js';
36
31
  const MARKETPLACE_NAME = 'aw';
37
32
  const LEGACY_MCP_OWNER_ENV = 'AW_AGENT_WORKFLOWS_OWNER';
38
- function requirementsReceipt(plugin) {
39
- const receipt = createAwPluginRequirementsReceipt({ name: plugin.manifest.name, version: plugin.manifest.version }, plugin.manifest.requires?.aw.capabilities ?? []);
40
- return Buffer.from(`${JSON.stringify(receipt, null, 2)}\n`, 'utf8');
33
+ const platformRebuildSessions = new WeakMap();
34
+ const commandPackageCandidates = new WeakMap();
35
+ export function finalizePreparedPlatformRebuildSession(results) {
36
+ for (const session of platformRebuildSessions.get(results)?.values() ?? [])
37
+ session.freezeMarketplaceCandidates();
38
+ }
39
+ export function recordPreparedGrokProjectRetirement(results, ...retirement) {
40
+ const sessions = platformRebuildSessions.get(results);
41
+ sessions?.get('grok')?.recordGrokProjectRetirement(...retirement);
42
+ sessions?.get(undefined)?.recordGrokProjectRetirement(...retirement);
43
+ }
44
+ export function recordPreparedGrokOfflineRuleRemoval(results, removal) {
45
+ const sessions = platformRebuildSessions.get(results);
46
+ sessions?.get('grok')?.recordGrokOfflineRuleRemoval(removal);
47
+ sessions?.get(undefined)?.recordGrokOfflineRuleRemoval(removal);
48
+ }
49
+ export function recordPreparedPermissionWrite(results, ...args) {
50
+ for (const session of platformRebuildSessions.get(results)?.values() ?? []) {
51
+ session.recordPermissionWrite(...args);
52
+ }
53
+ }
54
+ export function recordPreparedOfflinePermissionRemoval(results, settingsPath, ownerKey) {
55
+ for (const session of platformRebuildSessions.get(results)?.values() ?? []) {
56
+ session.recordOfflinePermissionRemoval(settingsPath, ownerKey);
57
+ }
58
+ }
59
+ export async function disposePreparedPlatformRebuildSession(results) {
60
+ const sessions = platformRebuildSessions.get(results);
61
+ platformRebuildSessions.delete(results);
62
+ const packages = commandPackageCandidates.get(results);
63
+ commandPackageCandidates.delete(results);
64
+ try {
65
+ await Promise.all([...(sessions?.values() ?? [])].map((session) => session.dispose()));
66
+ }
67
+ finally {
68
+ if (packages)
69
+ await Promise.all([...packages.values()].map(async (candidate) => {
70
+ const prepared = await candidate.catch(() => undefined);
71
+ await prepared?.cleanup();
72
+ }));
73
+ }
41
74
  }
42
75
  export class CodexActivationPostcheckError extends Error {
43
76
  code = 'codexActivationPostcheckFailed';
@@ -48,13 +81,34 @@ export class CodexActivationPostcheckError extends Error {
48
81
  this.codexPluginIds = [...new Set(typeof codexPluginIds === 'string' ? [codexPluginIds] : codexPluginIds)];
49
82
  }
50
83
  }
84
+ function codexObservationScope(context, marketplaceName, fallbackScope) {
85
+ return context.codexObservationScopeByMarketplace?.get(marketplaceName)
86
+ ?? (fallbackScope === 'user' ? 'user' : 'both');
87
+ }
88
+ function codexMarketplaceFromPluginId(pluginId) {
89
+ return pluginId.slice(pluginId.lastIndexOf('@') + 1);
90
+ }
91
+ function requireCodexObservationCoverage(registration, scope, operation) {
92
+ if (!registration.observations.user
93
+ || (scope === 'both' && !registration.observations.project)) {
94
+ throw new Error(`codexNativeObservationUnavailable: ${operation} requires ${scope === 'user' ? 'neutral' : 'exact and neutral'} Codex plugin list evidence.`);
95
+ }
96
+ }
51
97
  function requireCodexManager(manager) {
52
98
  if (!manager) {
53
99
  throw new Error('codexManagerRequired: Codex native operation has no resolved manager.');
54
100
  }
55
101
  return manager;
56
102
  }
57
- export async function executeCodexReconcileActivations(options) {
103
+ async function runCodexMutationWithInvalidation(options) {
104
+ try {
105
+ return await options.mutation();
106
+ }
107
+ finally {
108
+ options.observationSession?.invalidate(options.marketplaceName);
109
+ }
110
+ }
111
+ async function executeCodexReconcileActivationsInSession(options) {
58
112
  const byScope = {
59
113
  user: { upserts: {}, removals: new Set() },
60
114
  project: { upserts: {}, removals: new Set() },
@@ -87,23 +141,35 @@ export async function executeCodexReconcileActivations(options) {
87
141
  }
88
142
  }
89
143
  }
144
+ const recordActivationMutations = (pluginIds) => {
145
+ if (!options.mutationMarketplaces)
146
+ return;
147
+ for (const pluginId of pluginIds) {
148
+ options.mutationMarketplaces.add(pluginId.slice(pluginId.lastIndexOf('@') + 1));
149
+ }
150
+ };
90
151
  const observedActivationPluginIds = [...new Set(options.plans.flatMap((plan) => plan.postcheck.support === 'observed'
91
152
  ? plan.postcheck.plugins.map((plugin) => plugin.codexPluginId)
92
153
  : []))];
93
- if (observedActivationPluginIds.length > 0) {
154
+ if (!options.preflightCompleted && observedActivationPluginIds.length > 0) {
94
155
  const manager = requireCodexManager(options.manager);
95
156
  for (const pluginId of observedActivationPluginIds) {
157
+ const marketplaceName = codexMarketplaceFromPluginId(pluginId);
158
+ const observationScope = options.observationScopeByMarketplace?.get(marketplaceName)
159
+ ?? (options.plans.some((plan) => plan.requirement.scope === 'project')
160
+ ? 'both'
161
+ : 'user');
96
162
  const registration = await inspectCodexPluginRegistrationLive({
97
163
  pluginId,
98
164
  cwd: options.cwd,
99
165
  env: options.env,
100
166
  nativeCliRunner: options.runner,
101
167
  manager,
168
+ scope: observationScope,
169
+ observationSession: options.observationSession,
102
170
  });
103
171
  const observedState = requireObservedCodexNativeState(registration.observation, 'activation precheck');
104
- if (!registration.observations.user || !registration.observations.project) {
105
- throw new Error('codexNativeObservationUnavailable: activation precheck requires exact and neutral Codex plugin list evidence.');
106
- }
172
+ requireCodexObservationCoverage(registration, observationScope, 'activation precheck');
107
173
  if (observedState !== 'installed') {
108
174
  throw new Error('codexNativeObservationFailed: activation precheck did not observe the installed Codex plugin.');
109
175
  }
@@ -115,7 +181,9 @@ export async function executeCodexReconcileActivations(options) {
115
181
  if (Object.keys(enables).length === 0)
116
182
  continue;
117
183
  const configPath = resolveCodexPaths(options.cwd, scope, options.env).configTomlPath;
118
- await applyCodexConfigTransactionFile(configPath, { upsertPluginActivations: enables });
184
+ const update = await applyCodexConfigTransactionFile(configPath, { upsertPluginActivations: enables });
185
+ if (update.wrote)
186
+ recordActivationMutations(Object.keys(enables));
119
187
  const enabledConfig = await readCodexConfigFile(configPath);
120
188
  const missingEnables = Object.keys(enables).filter((pluginId) => enabledConfig.snapshot.plugins[pluginId]?.enabled !== true);
121
189
  if (missingEnables.length > 0) {
@@ -128,7 +196,7 @@ export async function executeCodexReconcileActivations(options) {
128
196
  .filter(([, desired]) => !desired));
129
197
  if (Object.keys(disables).length === 0 && transaction.removals.size === 0)
130
198
  continue;
131
- await applyCodexConfigTransactionFile(resolveCodexPaths(options.cwd, scope, options.env).configTomlPath, {
199
+ const update = await applyCodexConfigTransactionFile(resolveCodexPaths(options.cwd, scope, options.env).configTomlPath, {
132
200
  ...(transaction.removals.size > 0
133
201
  ? { removePluginActivations: [...transaction.removals].sort() }
134
202
  : {}),
@@ -136,6 +204,12 @@ export async function executeCodexReconcileActivations(options) {
136
204
  ? { upsertPluginActivations: disables }
137
205
  : {}),
138
206
  });
207
+ if (update.wrote) {
208
+ recordActivationMutations([
209
+ ...Object.keys(disables),
210
+ ...transaction.removals,
211
+ ]);
212
+ }
139
213
  }
140
214
  const [userConfig, projectConfig] = await Promise.all([
141
215
  readCodexConfigFile(resolveCodexPaths(options.cwd, 'user', options.env).configTomlPath),
@@ -172,184 +246,101 @@ export async function executeCodexReconcileActivations(options) {
172
246
  : []);
173
247
  if (observedActivationChecks.length === 0)
174
248
  return;
175
- const manager = requireCodexManager(options.manager);
176
- const neutralCwd = await fs.mkdtemp(path.join(os.tmpdir(), 'aw-codex-postcheck-'));
249
+ const ineffectivePlugins = [];
250
+ const ineffectiveDetails = [];
251
+ for (const plugin of observedActivationChecks) {
252
+ const userActive = userConfig.snapshot.plugins[plugin.codexPluginId]?.enabled === true;
253
+ const projectActive = projectConfig.snapshot.plugins[plugin.codexPluginId]?.enabled === true;
254
+ if ((userActive || projectActive) !== (plugin.expectedUserEnabled || plugin.expectedProjectEnabled)
255
+ || userActive !== plugin.expectedUserEnabled) {
256
+ ineffectivePlugins.push(plugin.codexPluginId);
257
+ ineffectiveDetails.push(new CodexProjectConfigNotEffectiveError(plugin.codexPluginId).message);
258
+ }
259
+ }
260
+ if (ineffectivePlugins.length > 0) {
261
+ throw new CodexActivationPostcheckError(ineffectivePlugins, ineffectiveDetails.join(' '));
262
+ }
263
+ }
264
+ export async function executeCodexReconcileActivations(options) {
265
+ const ownedSession = options.observationSession ? undefined
266
+ : await createCodexNativeObservationSession({
267
+ exactCwd: options.cwd,
268
+ env: options.env,
269
+ runner: options.runner,
270
+ manager: options.manager,
271
+ });
177
272
  try {
178
- const invocations = buildCodexProjectPluginListInvocations(manager, {
273
+ await executeCodexReconcileActivationsInSession({
274
+ ...options,
275
+ observationSession: options.observationSession ?? ownedSession,
276
+ });
277
+ }
278
+ finally {
279
+ await ownedSession?.dispose();
280
+ }
281
+ }
282
+ export async function postcheckCodexReconcileActivations(options) {
283
+ const checks = options.plans.flatMap((plan) => plan.postcheck.support === 'observed' ? plan.postcheck.plugins : []);
284
+ if (checks.length === 0)
285
+ return;
286
+ const ownedSession = options.observationSession ? undefined
287
+ : await createCodexNativeObservationSession({
179
288
  exactCwd: options.cwd,
180
- neutralCwd,
181
289
  env: options.env,
290
+ runner: options.runner,
291
+ manager: options.manager,
182
292
  });
183
- const [exactResult, neutralResult] = await Promise.all([
184
- runNativeCliChecked(invocations.exact, options.runner),
185
- runNativeCliChecked(invocations.neutral, options.runner),
186
- ]);
187
- const ineffectivePlugins = [];
188
- const ineffectiveDetails = [];
189
- for (const plugin of observedActivationChecks) {
293
+ const session = options.observationSession ?? ownedSession;
294
+ try {
295
+ const failedPluginIds = [];
296
+ const failureDetails = [];
297
+ for (const check of checks) {
190
298
  try {
191
- postcheckCodexProjectPluginActivation({
192
- pluginId: plugin.codexPluginId,
193
- userEnabled: plugin.expectedUserEnabled,
194
- projectEnabled: plugin.expectedProjectEnabled,
195
- exactResult,
196
- neutralResult,
299
+ const marketplaceName = codexMarketplaceFromPluginId(check.codexPluginId);
300
+ const observationScope = options.observationScopeByMarketplace?.get(marketplaceName)
301
+ ?? (options.plans.some((plan) => plan.requirement.scope === 'project')
302
+ ? 'both'
303
+ : 'user');
304
+ const registration = await inspectCodexPluginRegistrationLive({
305
+ pluginId: check.codexPluginId,
306
+ cwd: options.cwd,
307
+ env: options.env,
308
+ nativeCliRunner: options.runner,
309
+ manager: options.manager,
310
+ scope: observationScope,
311
+ observationSession: session,
197
312
  });
313
+ const state = requireObservedCodexNativeState(registration.observation, 'activation postcheck');
314
+ requireCodexObservationCoverage(registration, observationScope, 'activation postcheck');
315
+ if (state !== 'installed') {
316
+ throw new Error(`codexNativeObservationFailed: ${check.codexPluginId} was not observed after Codex activation mutation.`);
317
+ }
198
318
  }
199
319
  catch (error) {
200
- if (error instanceof CodexProjectConfigNotEffectiveError) {
201
- ineffectivePlugins.push(plugin.codexPluginId);
202
- ineffectiveDetails.push(error.message);
203
- continue;
204
- }
205
- throw error;
320
+ failedPluginIds.push(check.codexPluginId);
321
+ failureDetails.push(error instanceof Error ? error.message : String(error));
206
322
  }
207
323
  }
208
- if (ineffectivePlugins.length > 0) {
209
- throw new CodexActivationPostcheckError(ineffectivePlugins, ineffectiveDetails.join(' '));
324
+ if (failedPluginIds.length > 0) {
325
+ throw new CodexActivationPostcheckError(failedPluginIds, failureDetails.join(' '));
210
326
  }
211
327
  }
212
328
  finally {
213
- await fs.remove(neutralCwd);
329
+ await ownedSession?.dispose();
214
330
  }
215
331
  }
216
332
  const CODEX_REMOVE_RECOVERY = 'Codex residue was retained. Retry cleanup or uninstall, upgrade AW, or install a compatible official Codex CLI.';
217
- function ownerIdentity(plugin, artifactKey, platform, scope) {
218
- return { owner: 'agent-workflows', plugin, artifactKey, platform, scope };
219
- }
220
- function pluginOwnerIdentity(plugin, artifactKey, platform, scope) {
221
- return {
222
- ...ownerIdentity(plugin.manifest.name, artifactKey, platform, scope),
223
- ...(plugin.sourceId ? {
224
- sourceId: plugin.sourceId,
225
- pluginId: plugin.pluginId,
226
- } : {}),
227
- };
228
- }
229
- function bindNamespacedOwnerIdentity(components, source) {
230
- if (!source.sourceId)
231
- return;
232
- for (const component of components) {
233
- component.identity.sourceId = source.sourceId;
234
- component.identity.pluginId = source.pluginId;
235
- }
236
- }
237
- function internalArtifactKey(kind, value) {
238
- return `${kind}-${crypto.createHash('sha256').update(value).digest('hex').slice(0, 20)}`;
239
- }
240
- function pathComponent(options) {
241
- if (options.kind !== 'skill-directory')
242
- return options;
243
- return {
244
- ...options,
245
- validateHealth: true,
246
- };
247
- }
248
- function pristineTreeDescriptor(tree) {
249
- return Object.entries(tree.files)
250
- .sort(([left], [right]) => left.localeCompare(right))
251
- .map(([relativePath, content]) => ({
252
- relativePath,
253
- contentDigest: `sha256:${crypto.createHash('sha256').update(content).digest('hex')}`,
254
- }));
255
- }
256
333
  function pluginActionId(plugin) {
257
334
  return plugin.pluginId ?? plugin.manifest.name;
258
335
  }
259
- function skillWriteAction(options) {
260
- const files = [
261
- ...Object.entries(options.tree.files).map(([relativePath, content]) => ({
262
- relativePath,
263
- contentBase64: content.toString('base64'),
264
- contentDigest: `sha256:${crypto.createHash('sha256').update(content).digest('hex')}`,
265
- })),
266
- {
267
- relativePath: '.agent-workflows-owner.json',
268
- contentBase64: Buffer.from(createSkillOwnerMarkerContent(options.identity)).toString('base64'),
269
- contentDigest: `sha256:${crypto.createHash('sha256')
270
- .update(createSkillOwnerMarkerContent(options.identity)).digest('hex')}`,
271
- },
272
- ].sort((left, right) => left.relativePath.localeCompare(right.relativePath));
273
- const contentDigest = `sha256:${crypto.createHash('sha256').update(JSON.stringify(files)).digest('hex')}`;
274
- return {
275
- kind: 'writeOwnedArtifact',
276
- pluginId: pluginActionId(options.plugin),
277
- platform: options.platform,
278
- scope: options.scope,
279
- path: options.path,
280
- allowedRoot: options.allowedRoot,
281
- owner: options.identity,
282
- artifactKind: 'skill-directory',
283
- files,
284
- contentDigest,
285
- allowPristineUnowned: options.allowPristineUnowned,
286
- };
287
- }
288
- function finalizeOwnedArtifactActions(actions) {
289
- for (const action of actions) {
290
- if (action.kind !== 'writeOwnedArtifact' || !action.owner)
291
- continue;
292
- if (action.artifactKind === 'owned-text-file') {
293
- if (action.sourceContent === undefined)
294
- throw new Error('Owned text action is missing source content.');
295
- action.content = renderOwnedTextFile(action.sourceContent, action.owner);
296
- action.contentDigest = `sha256:${crypto.createHash('sha256').update(action.content).digest('hex')}`;
297
- continue;
298
- }
299
- if (action.artifactKind === 'skill-directory') {
300
- const markerContent = createSkillOwnerMarkerContent(action.owner);
301
- const marker = {
302
- relativePath: '.agent-workflows-owner.json',
303
- contentBase64: Buffer.from(markerContent).toString('base64'),
304
- contentDigest: `sha256:${crypto.createHash('sha256').update(markerContent).digest('hex')}`,
305
- };
306
- action.files = [
307
- ...(action.files ?? []).filter((file) => file.relativePath !== marker.relativePath),
308
- marker,
309
- ].sort((left, right) => left.relativePath.localeCompare(right.relativePath));
310
- action.contentDigest = `sha256:${crypto.createHash('sha256')
311
- .update(JSON.stringify(action.files)).digest('hex')}`;
312
- }
313
- }
314
- }
315
- function generatedTextWrite(options) {
316
- const artifactPath = path.join(options.configDir, options.relativePath);
317
- const identity = ownerIdentity(options.plugin.manifest.name, options.artifactKey, options.platform, options.scope);
318
- const component = pathComponent({
319
- kind: 'owned-text-file',
320
- path: artifactPath,
321
- allowedRoot: options.configDir,
322
- identity,
323
- });
324
- const content = options.content;
325
- return {
326
- artifact: {
327
- path: artifactPath,
328
- kind: options.kind,
329
- platform: options.platform,
330
- artifactKey: options.artifactKey,
331
- },
332
- component,
333
- action: {
334
- kind: 'writeOwnedArtifact',
335
- pluginId: pluginActionId(options.plugin),
336
- platform: options.platform,
337
- scope: options.scope,
338
- path: artifactPath,
339
- allowedRoot: options.configDir,
340
- owner: identity,
341
- artifactKind: 'owned-text-file',
342
- sourceContent: content,
343
- content,
344
- contentDigest: `sha256:${crypto.createHash('sha256').update(content).digest('hex')}`,
345
- },
346
- };
347
- }
348
336
  function mergePermissionStates(values) {
349
337
  return {
350
338
  allow: [...new Set(values.flatMap((value) => value?.allow ?? []))],
351
339
  ask: [...new Set(values.flatMap((value) => value?.ask ?? []))],
352
340
  deny: [...new Set(values.flatMap((value) => value?.deny ?? []))],
341
+ ...(values.some((value) => value?.excludedCommands !== undefined) ? {
342
+ excludedCommands: [...new Set(values.flatMap((value) => value?.excludedCommands ?? []))],
343
+ } : {}),
353
344
  };
354
345
  }
355
346
  function subtractPermissionState(value, generated) {
@@ -362,6 +353,9 @@ function subtractPermissionState(value, generated) {
362
353
  allow: (value?.allow ?? []).filter((entry) => !remove.allow.has(entry)),
363
354
  ask: (value?.ask ?? []).filter((entry) => !remove.ask.has(entry)),
364
355
  deny: (value?.deny ?? []).filter((entry) => !remove.deny.has(entry)),
356
+ ...(value?.excludedCommands ? {
357
+ excludedCommands: value.excludedCommands.filter((entry) => !generated.excludedCommands?.includes(entry)),
358
+ } : {}),
365
359
  };
366
360
  }
367
361
  function intersectPermissionState(value, generated) {
@@ -374,11 +368,17 @@ function intersectPermissionState(value, generated) {
374
368
  allow: (value?.allow ?? []).filter((entry) => keep.allow.has(entry)),
375
369
  ask: (value?.ask ?? []).filter((entry) => keep.ask.has(entry)),
376
370
  deny: (value?.deny ?? []).filter((entry) => keep.deny.has(entry)),
371
+ ...(value?.excludedCommands ? {
372
+ excludedCommands: value.excludedCommands.filter((entry) => generated.excludedCommands?.includes(entry)),
373
+ } : {}),
377
374
  };
378
375
  }
379
376
  function rebuildClaudePermissions(settings, ownerKey, desired) {
380
377
  const previousGenerated = mergePermissionStates(Object.values(settings.awGenerated?.permissions ?? {}));
381
- const currentPermissions = mergePermissionStates([settings.permissions]);
378
+ const currentPermissions = mergePermissionStates([{
379
+ ...settings.permissions,
380
+ ...(settings.sandbox?.excludedCommands ? { excludedCommands: settings.sandbox.excludedCommands } : {}),
381
+ }]);
382
382
  const manual = mergePermissionStates([
383
383
  subtractPermissionState(currentPermissions, previousGenerated),
384
384
  intersectPermissionState(intersectPermissionState(settings.awGenerated?.manualPermissions, previousGenerated), currentPermissions),
@@ -392,16 +392,22 @@ function rebuildClaudePermissions(settings, ownerKey, desired) {
392
392
  if (Object.keys(settings.awGenerated.permissions).length === 0) {
393
393
  delete settings.awGenerated.permissions;
394
394
  }
395
- if (manual.allow.length || manual.ask.length || manual.deny.length) {
395
+ if (manual.allow.length || manual.ask.length || manual.deny.length || manual.excludedCommands?.length) {
396
396
  settings.awGenerated.manualPermissions = manual;
397
397
  }
398
398
  else {
399
399
  delete settings.awGenerated.manualPermissions;
400
400
  }
401
- settings.permissions = mergePermissionStates([
401
+ const combined = mergePermissionStates([
402
402
  manual,
403
403
  ...Object.values(settings.awGenerated.permissions ?? {}),
404
404
  ]);
405
+ settings.permissions = {
406
+ ...settings.permissions, allow: combined.allow, ask: combined.ask, deny: combined.deny,
407
+ };
408
+ if (combined.excludedCommands !== undefined) {
409
+ settings.sandbox = { ...settings.sandbox, excludedCommands: combined.excludedCommands };
410
+ }
405
411
  if (settings.awGenerated.permissions === undefined
406
412
  && settings.awGenerated.manualPermissions === undefined
407
413
  && settings.awGenerated.hooks === undefined
@@ -409,51 +415,6 @@ function rebuildClaudePermissions(settings, ownerKey, desired) {
409
415
  delete settings.awGenerated;
410
416
  }
411
417
  }
412
- function createClaudePermissionsComponent(options) {
413
- const ownerKey = createSharedOwnerToken(options.identity);
414
- return {
415
- kind: 'shared-config',
416
- identity: options.identity,
417
- label: `${options.settingsPath}#awGenerated.permissions.${ownerKey}`,
418
- locator: { kind: 'claude-permissions', settingsPath: options.settingsPath, ownerKey },
419
- };
420
- }
421
- function createCodexInstructionComponent(options) {
422
- const ownerKey = createSharedOwnerToken(options.identity);
423
- return {
424
- kind: 'managed-block',
425
- identity: options.identity,
426
- label: `${options.configPath}#developer_instructions:${ownerKey}`,
427
- locator: { kind: 'codex-instruction', configPath: options.configPath, ownerKey },
428
- };
429
- }
430
- function createCodexActivationComponent(options) {
431
- return {
432
- kind: 'shared-config',
433
- identity: options.identity,
434
- label: `${options.configPath}#plugins.${options.codexPluginId}.enabled`,
435
- locator: {
436
- kind: 'codex-activation',
437
- configPath: options.configPath,
438
- codexPluginId: options.codexPluginId,
439
- desired: options.desired,
440
- },
441
- };
442
- }
443
- function createCodexActivationCleanupComponent(options) {
444
- return {
445
- kind: 'shared-config',
446
- identity: options.identity,
447
- label: `${options.configPath}#plugins.${options.codexPluginId}.enabled`,
448
- locator: {
449
- kind: 'codex-activation',
450
- configPath: options.configPath,
451
- codexPluginId: options.codexPluginId,
452
- desired: options.disposition === 'remove' ? false : true,
453
- disposition: options.disposition,
454
- },
455
- };
456
- }
457
418
  function legacyCodexMcpOwner(actual) {
458
419
  const envValue = actual?.value?.env;
459
420
  return envValue
@@ -463,20 +424,6 @@ function legacyCodexMcpOwner(actual) {
463
424
  ? envValue[LEGACY_MCP_OWNER_ENV]
464
425
  : null;
465
426
  }
466
- function createLegacyCodexMcpCleanupComponent(options) {
467
- const ownerKey = createSharedOwnerToken(options.identity);
468
- return {
469
- kind: 'shared-config',
470
- identity: options.identity,
471
- label: `${options.configPath}#mcp_servers.${options.serverKey}`,
472
- locator: {
473
- kind: 'legacy-codex-mcp',
474
- configPath: options.configPath,
475
- serverKey: options.serverKey,
476
- ownerKey,
477
- },
478
- };
479
- }
480
427
  async function readLegacyCodexHooks(hooksPath) {
481
428
  if (!await fs.pathExists(hooksPath))
482
429
  return parseCodexSharedHooksFile({ hooks: {} });
@@ -516,19 +463,6 @@ async function applyLegacyCodexHookCleanupTransactionFile(hooksPath, update) {
516
463
  },
517
464
  });
518
465
  }
519
- function createLegacyCodexHookCleanupComponent(options) {
520
- const serialized = serializeTrackedHookGroupIdentity(options.identity);
521
- return {
522
- kind: 'shared-config',
523
- identity: ownerIdentity(options.pluginName, internalArtifactKey('hook', serialized), 'codex', options.scope),
524
- label: `${options.hooksPath}#${serialized}`,
525
- locator: {
526
- kind: 'legacy-codex-hook',
527
- hooksPath: options.hooksPath,
528
- serializedIdentity: serialized,
529
- },
530
- };
531
- }
532
466
  async function codexRoleRegistrationOwned(options) {
533
467
  if (!options.registration?.configFile)
534
468
  return false;
@@ -544,1613 +478,188 @@ async function codexRoleRegistrationOwned(options) {
544
478
  return false;
545
479
  }
546
480
  }
547
- function createCodexRoleRegistrationComponent(options) {
548
- return {
549
- kind: 'shared-config',
550
- identity: options.identity,
551
- label: `${options.configPath}#agents.${options.roleKey}`,
552
- locator: {
553
- kind: 'codex-role-registration',
554
- configPath: options.configPath,
555
- roleKey: options.roleKey,
556
- rolePath: options.rolePath,
557
- fileIdentity: options.fileIdentity,
558
- },
559
- };
560
- }
561
- function sameOwnerIdentity(left, right) {
562
- return left.owner === right.owner
563
- && left.sourceId === right.sourceId
564
- && left.pluginId === right.pluginId
565
- && left.plugin === right.plugin
566
- && left.artifactKey === right.artifactKey
567
- && left.platform === right.platform
568
- && left.scope === right.scope;
481
+ export async function removeForceCleanupCodexRoleRegistration(options) {
482
+ const component = options.component;
483
+ if (component.kind !== 'owned-text-file' || component.identity.platform !== 'codex')
484
+ return;
485
+ const paths = resolveCodexPaths(options.cwd, component.identity.scope, options.env);
486
+ const roleKey = component.identity.artifactKey;
487
+ const expectedPath = path.join(paths.agentsDir, `${buildGeneratedName(component.identity.plugin, roleKey)}.toml`);
488
+ if (path.resolve(component.path) !== path.resolve(expectedPath))
489
+ return;
490
+ await applyConditionalCodexConfigTransactionFile(paths.configTomlPath, async (snapshot) => {
491
+ const registration = snapshot.agents[roleKey];
492
+ if (!registration?.configFile
493
+ || path.resolve(path.dirname(paths.configTomlPath), registration.configFile)
494
+ !== path.resolve(component.path))
495
+ return null;
496
+ // Force authorizes the existing exclusive file. The matching registration is
497
+ // removed before that evidence disappears; missing or foreign files grant no authority.
498
+ const inspected = await inspectOwnedComponents({ components: [component], forceExclusive: true });
499
+ return inspected.components[0]?.state === 'present'
500
+ ? { removeAgentRegistrations: [roleKey] }
501
+ : null;
502
+ });
569
503
  }
570
504
  function sharedOwnerPluginName(value) {
571
505
  return parseSharedOwnerToken(value)?.plugin ?? value;
572
506
  }
573
- async function scanCodexSharedComponents(options) {
574
- const output = [];
575
- const expectedIdentity = (artifactKey) => ({
576
- ...ownerIdentity(options.pluginName, artifactKey, 'codex', options.scope),
577
- ...(options.sourceId ? {
578
- sourceId: options.sourceId,
579
- pluginId: options.pluginId,
580
- } : {}),
581
- });
582
- const corruptedComponent = (filePath, error) => ({
583
- kind: 'shared-config',
584
- identity: ownerIdentity(options.pluginName, internalArtifactKey('corrupted-shared-config', filePath), 'codex', options.scope),
585
- label: filePath,
586
- locator: {
587
- kind: 'corrupted',
588
- message: `包含 ${options.pluginName} 的 AW ownership 证据,但共享配置无法解析:${error instanceof Error ? error.message : String(error)}`,
589
- },
590
- });
591
- const containsOwnedEvidence = (raw) => raw.includes(options.pluginName) && (raw.includes('[[aw-managed:start plugin=')
592
- || raw.includes(OWNED_TEXT_HEADER_PREFIX)
593
- || raw.includes('agent-workflows'));
594
- if (options.scanConfig) {
595
- const config = await readCodexConfigFile(options.paths.configTomlPath).catch(async (error) => {
596
- if (!options.ignoreReadErrors)
597
- throw error;
598
- const raw = await fs.readFile(options.paths.configTomlPath, 'utf8').catch(() => '');
599
- if (containsOwnedEvidence(raw)) {
600
- output.push(corruptedComponent(options.paths.configTomlPath, error));
601
- }
602
- return null;
603
- });
604
- const instructionIdentity = expectedIdentity('developer-instructions');
605
- const instructionOwnerKey = createSharedOwnerToken(instructionIdentity);
606
- if (config?.snapshot.developerInstructions.managedBlocks[instructionOwnerKey]) {
607
- output.push(createCodexInstructionComponent({
608
- identity: instructionIdentity,
609
- scope: options.scope,
610
- configPath: options.paths.configTomlPath,
611
- }));
612
- }
613
- for (const [roleKey, registration] of Object.entries(config?.snapshot.agents ?? {})) {
614
- if (!registration.configFile)
615
- continue;
616
- const rolePath = path.resolve(path.dirname(options.paths.configTomlPath), registration.configFile);
617
- const raw = await fs.readFile(rolePath, 'utf8').catch(() => '');
618
- const parsed = (() => {
619
- try {
620
- return parseOwnedTextHeader(raw);
621
- }
622
- catch {
623
- return null;
624
- }
625
- })();
626
- if (!parsed || !sameOwnerIdentity(parsed.identity, expectedIdentity(roleKey)))
627
- continue;
628
- output.push(createCodexRoleRegistrationComponent({
629
- identity: expectedIdentity(internalArtifactKey('role', roleKey)),
630
- fileIdentity: expectedIdentity(roleKey),
631
- configPath: options.paths.configTomlPath,
632
- roleKey,
633
- rolePath,
634
- }));
635
- }
507
+ function observedCodexNativeState(observation, operation) {
508
+ if (observation.state === 'observed') {
509
+ return observation.value === 'installed' ? 'present' : 'absent';
636
510
  }
637
- return output;
511
+ if (observation.state === 'unobserved')
512
+ return 'partial';
513
+ throw new Error(`codexNativeObservationFailed: ${operation} requires Codex plugin list evidence.`);
638
514
  }
639
- async function scanClaudeSharedComponents(options) {
640
- const settings = await readClaudeSettings(options.settingsPath);
641
- const identity = {
642
- ...ownerIdentity(options.pluginName, 'permissions', 'claude', options.scope),
643
- ...(options.sourceId ? {
644
- sourceId: options.sourceId,
645
- pluginId: options.pluginId,
646
- } : {}),
647
- };
648
- return settings.awGenerated?.permissions?.[createSharedOwnerToken(identity)]
649
- ? [createClaudePermissionsComponent({ ...options, identity })]
650
- : [];
515
+ function requireObservedCodexNativeState(observation, operation) {
516
+ if (observation.state === 'observed')
517
+ return observation.value;
518
+ const code = observation.state === 'failed'
519
+ ? 'codexNativeObservationFailed'
520
+ : 'codexNativeObservationUnavailable';
521
+ throw new Error(`${code}: ${operation} requires Codex plugin list evidence.`);
651
522
  }
652
- async function scanOwnedTextFiles(options) {
653
- if (!await fs.pathExists(options.root))
654
- return [];
655
- const output = [];
656
- const pending = [options.root];
657
- while (pending.length > 0) {
658
- const current = pending.pop();
659
- for (const entry of await fs.readdir(current, { withFileTypes: true })) {
660
- const entryPath = path.join(current, entry.name);
661
- if (entry.isSymbolicLink())
662
- continue;
663
- if (entry.isDirectory()) {
664
- pending.push(entryPath);
665
- continue;
666
- }
667
- if (!entry.isFile())
668
- continue;
669
- const raw = await fs.readFile(entryPath, 'utf8').catch(() => '');
670
- if (!raw.includes(OWNED_TEXT_HEADER_PREFIX))
671
- continue;
672
- let parsed;
673
- try {
674
- parsed = parseOwnedTextHeader(raw);
675
- }
676
- catch {
677
- continue;
678
- }
679
- if (parsed?.identity.plugin !== options.pluginName
680
- || (options.sourceId !== undefined && parsed.identity.sourceId !== options.sourceId)
681
- || parsed.identity.platform !== options.platform
682
- || parsed.identity.scope !== options.scope)
683
- continue;
684
- output.push(pathComponent({
685
- kind: 'owned-text-file',
686
- path: entryPath,
687
- allowedRoot: options.root,
688
- identity: parsed.identity,
689
- }));
690
- }
523
+ export async function inspectCodexPluginRegistrationLive(options) {
524
+ if (!options.nativeCliRunner) {
525
+ return {
526
+ observation: { state: 'unobserved' },
527
+ observations: { user: false, project: false },
528
+ activations: {},
529
+ };
691
530
  }
692
- return output;
693
- }
694
- async function scanOwnedSkills(options) {
695
- if (!await fs.pathExists(options.root))
696
- return [];
697
- const output = [];
698
- const pending = [options.root];
699
- while (pending.length > 0) {
700
- const current = pending.pop();
701
- for (const entry of await fs.readdir(current, { withFileTypes: true })) {
702
- if (!entry.isDirectory() || entry.isSymbolicLink())
703
- continue;
704
- const skillRoot = path.join(current, entry.name);
705
- const marker = await readSkillOwnerMarker({
706
- skillRoot,
707
- allowedRoot: current,
708
- }).catch(() => null);
709
- if (marker?.plugin === options.pluginName
710
- && (options.sourceId === undefined || marker.sourceId === options.sourceId)
711
- && marker.platform === options.platform
712
- && marker.scope === options.scope) {
713
- output.push(pathComponent({
714
- kind: 'skill-directory',
715
- path: skillRoot,
716
- allowedRoot: current,
717
- identity: marker,
718
- }));
719
- continue;
720
- }
721
- pending.push(skillRoot);
722
- }
531
+ const ownedSession = options.observationSession ? undefined
532
+ : await createCodexNativeObservationSession({
533
+ exactCwd: options.cwd,
534
+ env: options.env,
535
+ runner: options.nativeCliRunner,
536
+ manager: options.manager ?? SYSTEM_CODEX_MANAGER,
537
+ });
538
+ const session = options.observationSession ?? ownedSession;
539
+ try {
540
+ const inspected = await session.inspectPlugin({
541
+ pluginId: options.pluginId,
542
+ scope: options.scope ?? 'both',
543
+ });
544
+ return {
545
+ observation: inspected.observation,
546
+ observations: inspected.observations,
547
+ activations: inspected.activations,
548
+ };
549
+ }
550
+ finally {
551
+ await ownedSession?.dispose();
723
552
  }
724
- return output;
725
553
  }
726
- function componentIdentityKey(component) {
727
- return [
728
- component.kind,
729
- component.identity.artifactKey,
730
- 'path' in component ? path.resolve(component.path) : 'label' in component ? component.label : component.pluginId,
731
- ].join('\0');
554
+ export async function removeCodexPluginActivation(options) {
555
+ await applyConditionalCodexConfigTransactionFile(options.configPath, (snapshot) => Object.hasOwn(snapshot.plugins, options.codexPluginId)
556
+ ? { removePluginActivations: [options.codexPluginId] }
557
+ : null);
732
558
  }
733
- function dedupeComponents(components) {
734
- const seen = new Set();
735
- return components.filter((component) => {
736
- const key = componentIdentityKey(component);
737
- if (seen.has(key))
738
- return false;
739
- seen.add(key);
740
- return true;
559
+ export async function removeNamespacedMarketplacePluginProjection(options) {
560
+ if (!await isSafeCodexMarketplaceRoot({
561
+ awHome: resolveAwHome(options.env),
562
+ sourceId: options.sourceId,
563
+ marketplaceRoot: options.rootDir,
564
+ })) {
565
+ throw new Error('marketplaceProjectionUnsafe: marketplace root is outside the canonical AW source path.');
566
+ }
567
+ const root = await readStrictNamespacedMarketplaceRoot({
568
+ rootDir: options.rootDir,
569
+ sourceId: options.sourceId,
570
+ sourceIdentity: options.sourceIdentity,
741
571
  });
742
- }
743
- async function preflightCodexWriteSafety(options) {
744
- const pluginName = options.plugin.manifest.name;
745
- const userConfigPath = resolveCodexPaths(options.cwd, 'user', options.env).configTomlPath;
746
- const projectConfigPath = resolveCodexPaths(options.cwd, 'project', options.env).configTomlPath;
747
- const nativeSurface = analyzeCodexSurface(options.plugin.manifest).nativeKinds.length > 0;
748
- const needsUserConfig = options.scope === 'user'
749
- || nativeSurface;
750
- let userConfig;
751
- if (needsUserConfig) {
752
- try {
753
- userConfig = await readCodexConfigFile(userConfigPath);
754
- }
755
- catch (error) {
756
- throw new Error(`codexProjectUserConfigUnreadable: 无法解析用户级 Codex 配置 ${userConfigPath}:${error instanceof Error ? error.message : String(error)}`);
757
- }
572
+ if (!root)
573
+ return;
574
+ const pluginRoot = await assertSafeNamespacedMarketplacePluginRoot(options.rootDir, options.pluginName);
575
+ const pluginStats = await fs.lstat(pluginRoot).catch((error) => {
576
+ if (error.code === 'ENOENT')
577
+ return null;
578
+ throw error;
579
+ });
580
+ if (pluginStats && (!pluginStats.isDirectory() || pluginStats.isSymbolicLink())) {
581
+ throw new Error('marketplaceProjectionUnsafe: plugin projection is not a regular directory.');
758
582
  }
759
- if (options.scope === 'project' && userConfig) {
760
- const expectedPluginId = `${pluginName}@${options.plugin.marketplaceName ?? MARKETPLACE_NAME}`;
761
- for (const [key, plugin] of Object.entries(userConfig.snapshot.plugins)) {
762
- if (key !== expectedPluginId
763
- && (key === pluginName || key.startsWith(`${pluginName}@`) || plugin.name === pluginName)) {
764
- throw new Error(`externalOwnershipPresent: 用户级 Codex 已存在同名外部插件 ${key}。`);
583
+ const manifests = [
584
+ { value: root.agentsManifest, path: path.join(options.rootDir, '.agents', 'plugins', 'marketplace.json') },
585
+ ...(root.claudeManifest
586
+ ? [{ value: root.claudeManifest, path: path.join(options.rootDir, '.claude-plugin', 'marketplace.json') }]
587
+ : []),
588
+ ];
589
+ const hasEntry = manifests.some(({ value }) => Array.isArray(value.plugins)
590
+ && value.plugins.some((entry) => entry !== null && typeof entry === 'object'
591
+ && entry.name === options.pluginName));
592
+ let hasIdentityManifest = false;
593
+ if (pluginStats) {
594
+ for (const manifestName of ['.agents-plugin/plugin.json', '.codex-plugin/plugin.json']) {
595
+ const manifestPath = path.join(pluginRoot, manifestName);
596
+ const manifest = await fs.readJson(manifestPath).catch((error) => {
597
+ if (error.code === 'ENOENT')
598
+ return null;
599
+ throw error;
600
+ });
601
+ if (manifest && manifest.name !== options.pluginName) {
602
+ throw new Error('marketplaceProjectionIdentityMismatch: plugin projection has a foreign name.');
765
603
  }
604
+ hasIdentityManifest ||= manifest?.name === options.pluginName;
766
605
  }
767
- }
768
- const roleKeys = new Set((options.plugin.manifest.artifacts?.targets ?? [])
769
- .filter((target) => target.platform === 'codex'
770
- && target.scopes.includes(options.scope)
771
- && target.target === 'codex.agentRole'
772
- && target.roleKey)
773
- .map((target) => target.roleKey));
774
- const needsProjectConfig = options.scope === 'project' && Boolean(nativeSurface
775
- || resolvePlatformEntries(options.plugin.manifest.extensions?.instructions, 'codex').length > 0
776
- || (options.plugin.manifest.artifacts?.targets ?? []).some((target) => target.platform === 'codex'
777
- && target.scopes.includes('project')
778
- && (target.target === 'codex.agentRole'
779
- || target.target === 'codex.developerInstructionsBlock')));
780
- if (needsProjectConfig) {
781
- try {
782
- await readCodexConfigFile(projectConfigPath);
783
- }
784
- catch (error) {
785
- throw new Error(`projectConfigUnreadable: 无法解析项目级 Codex 配置 ${projectConfigPath}:${error instanceof Error ? error.message : String(error)}`);
606
+ if (!hasEntry && !hasIdentityManifest) {
607
+ throw new Error('marketplaceProjectionIdentityUnavailable: plugin directory has no matching marketplace entry or plugin manifest.');
786
608
  }
787
609
  }
788
- if (roleKeys.size === 0)
789
- return;
790
- for (const configPath of options.scope === 'project'
791
- ? [projectConfigPath]
792
- : [userConfigPath]) {
793
- const configScope = configPath === userConfigPath ? 'user' : 'project';
794
- const ownedRolePaths = [];
795
- const ownedRoleKeys = [];
796
- for (const roleKey of roleKeys) {
797
- const rolePath = path.join(path.dirname(configPath), 'agents', `${buildGeneratedName(pluginName, roleKey)}.toml`);
798
- const raw = await fs.readFile(rolePath, 'utf8').catch(() => '');
799
- try {
800
- if (parseOwnedTextHeader(raw, pluginOwnerIdentity(options.plugin, roleKey, 'codex', configScope))) {
801
- ownedRolePaths.push(rolePath);
802
- ownedRoleKeys.push(roleKey);
610
+ if (pluginStats) {
611
+ const inspectTree = async (directory) => {
612
+ for (const entry of await fs.readdir(directory, { withFileTypes: true })) {
613
+ if (entry.isSymbolicLink() || (!entry.isDirectory() && !entry.isFile())) {
614
+ throw new Error('marketplaceProjectionUnsafe: plugin projection contains an unsafe node.');
803
615
  }
616
+ if (entry.isDirectory())
617
+ await inspectTree(path.join(directory, entry.name));
804
618
  }
805
- catch {
806
- // A foreign file remains external below.
807
- }
808
- }
809
- let namespace;
810
- try {
811
- namespace = await resolveCodexRoleNamespace(configPath, {
812
- strict: true,
813
- awManagedTableKeys: ownedRoleKeys,
814
- awManagedRoleLayerPaths: ownedRolePaths,
619
+ };
620
+ await inspectTree(pluginRoot);
621
+ await fs.remove(pluginRoot);
622
+ }
623
+ for (const manifest of manifests) {
624
+ if (!Array.isArray(manifest.value.plugins)) {
625
+ throw new Error('marketplaceProjectionInvalid: marketplace plugins are not an array.');
626
+ }
627
+ const plugins = manifest.value.plugins.filter((entry) => entry === null
628
+ || typeof entry !== 'object'
629
+ || entry.name !== options.pluginName);
630
+ if (plugins.length !== manifest.value.plugins.length) {
631
+ await atomicReplaceConfigFile({
632
+ filePath: manifest.path,
633
+ allowedRoot: options.rootDir,
634
+ content: `${JSON.stringify({ ...manifest.value, plugins }, null, 2)}\n`,
815
635
  });
816
636
  }
817
- catch (error) {
818
- throw new Error(`projectConfigUnreadable: 无法读取 Codex role namespace ${configPath}:${error instanceof Error ? error.message : String(error)}`);
819
- }
820
- for (const roleKey of roleKeys) {
821
- const tableCollision = namespace.roles.find((role) => role.source === 'registration' && role.tableKey === roleKey && !role.awManaged);
822
- const nameConflict = findCodexRoleNameConflict(namespace, roleKey);
823
- if (tableCollision || nameConflict?.conflict) {
824
- throw new Error(`roleNameConflict: Codex role "${roleKey}" 已由外部配置占用(${configPath})。`);
825
- }
826
- }
827
637
  }
828
638
  }
829
- function renderCodexRole(options) {
830
- const stringValue = (value) => JSON.stringify(value.trim());
831
- return [
832
- `name = ${stringValue(options.roleKey)}`,
833
- `description = ${stringValue(options.description)}`,
834
- `developer_instructions = ${stringValue(options.developerInstructions)}`,
835
- ...(options.model ? [`model = ${stringValue(options.model)}`] : []),
836
- ...(options.modelReasoningEffort
837
- ? [`model_reasoning_effort = ${stringValue(options.modelReasoningEffort)}`]
838
- : []),
839
- '',
840
- ].join('\n');
841
- }
842
- async function resolveDynamicArtifacts(options) {
843
- const requestedArtifactIds = resolveAwDeliveredArtifactIds({
844
- manifest: options.plugin.manifest,
845
- platform: options.platform,
846
- scope: options.scope,
639
+ /**
640
+ * Read-only shared-native inspection. Official Codex list evidence owns installation
641
+ * state. Filesystem validation is limited to the AW-owned marketplace source.
642
+ */
643
+ export async function inspectCodexSharedNativeLive(options) {
644
+ const marketplaceName = options.marketplaceName ?? MARKETPLACE_NAME;
645
+ const marketplaceRoot = options.marketplaceRoot ?? path.join(resolveAwHome(options.env), 'marketplaces', options.sourceId);
646
+ const marketplacePathSafe = await isSafeCodexMarketplaceRoot({
647
+ awHome: resolveAwHome(options.env),
648
+ sourceId: options.sourceId,
649
+ marketplaceRoot,
847
650
  });
848
- if (requestedArtifactIds.length === 0)
849
- return { artifacts: [], sourceDirs: () => [] };
850
- const render = await runArtifactRenderer({
851
- event: options.event,
852
- plugin: options.plugin,
853
- platform: options.platform,
854
- scope: options.scope,
855
- cwd: options.cwd,
856
- commandEnv: options.env,
857
- previousArtifacts: [...options.previousArtifacts],
858
- requestedArtifactIds,
859
- scriptTimeoutMs: options.scriptTimeoutMs,
860
- });
861
- const config = await resolveArtifactRenderConfig({
862
- sourceId: options.plugin.sourceId,
863
- sourceIdentity: options.plugin.sourceIdentity,
864
- pluginName: options.plugin.manifest.name,
865
- manifest: options.plugin.manifest,
866
- scope: options.scope,
867
- cwd: options.cwd,
868
- env: options.env,
869
- });
870
- const artifacts = validateArtifactPlan({
871
- manifest: options.plugin.manifest,
872
- plan: render.plan,
873
- platform: options.platform,
874
- scope: options.scope,
875
- requestedArtifactIds: render.context.requestedArtifactIds,
876
- renderConfig: config,
877
- rendererDigest: render.target.rendererDigest,
878
- manifestDigest: render.target.manifestDigest,
879
- }).artifacts;
880
- return {
881
- artifacts,
882
- sourceDirs: (artifact) => render.context.artifactSources.find((entry) => entry.id === artifact.id && entry.target === artifact.target)?.sourceDirs ?? [],
883
- };
884
- }
885
- function dynamicSkillWrite(options) {
886
- const skillKey = 'skillKey' in options.artifact ? options.artifact.skillKey : '';
887
- const identity = ownerIdentity(options.plugin.manifest.name, skillKey, options.platform, options.scope);
888
- const component = pathComponent({
889
- kind: 'skill-directory',
890
- path: options.skillPath,
891
- allowedRoot: options.allowedRoot,
892
- identity,
893
- ...(options.platform === 'codex' && options.scope === 'user'
894
- ? {
895
- pristineTree: pristineTreeDescriptor(options.tree),
896
- }
897
- : {}),
898
- });
899
- return {
900
- artifact: {
901
- path: options.skillPath,
902
- paths: Object.keys(options.tree.files).map((relativePath) => path.join(options.skillPath, relativePath.split('/').join(path.sep))),
903
- kind: 'skill',
904
- platform: options.platform,
905
- artifactKey: skillKey,
906
- id: options.artifact.id,
907
- target: options.artifact.target,
908
- state: options.artifact.state,
909
- ...(options.artifact.disabledReason
910
- ? { disabledReason: options.artifact.disabledReason }
911
- : {}),
912
- refreshKeys: [...options.artifact.refreshKeys],
913
- configDigest: options.artifact.configDigest,
914
- contentDigest: options.platform === 'codex'
915
- ? createCodexSkillStagingTreeContentDigest(options.tree)
916
- : createDynamicSkillTreeContentDigest(options.tree),
917
- },
918
- ...(options.artifact.state === 'present' ? { component } : {}),
919
- ...(options.artifact.state === 'present' ? {
920
- action: skillWriteAction({
921
- plugin: options.plugin,
922
- platform: options.platform,
923
- scope: options.scope,
924
- path: options.skillPath,
925
- allowedRoot: options.allowedRoot,
926
- identity,
927
- tree: options.tree,
928
- allowPristineUnowned: options.platform === 'codex' && options.scope === 'user',
929
- }),
930
- } : {}),
931
- };
932
- }
933
- async function resolveCodexSkillsRelativeRoot(plugin, marketplaceFallbackRoot) {
934
- const manifestPaths = [
935
- path.join(plugin.rootDir, '.codex-plugin', 'plugin.json'),
936
- ...(marketplaceFallbackRoot
937
- ? [path.join(marketplaceFallbackRoot, '.codex-plugin', 'plugin.json')]
938
- : []),
939
- ];
940
- let manifest = null;
941
- for (const manifestPath of manifestPaths) {
942
- manifest = await fs.readJson(manifestPath).catch(() => null);
943
- if (manifest)
944
- break;
945
- }
946
- const raw = typeof manifest?.skills === 'string' && manifest.skills.trim() !== ''
947
- ? manifest.skills
948
- : plugin.manifest.skills ?? './codex/skills';
949
- const normalized = path.posix.normalize(raw.replace(/^\.\//u, ''));
950
- if (normalized === '..' || normalized.startsWith('../') || path.isAbsolute(normalized)) {
951
- throw new Error(`Codex plugin skills root 越界:${raw}`);
952
- }
953
- return normalized;
954
- }
955
- async function buildBundledCodexSkillWrites(options) {
956
- const relativeRoot = await resolveCodexSkillsRelativeRoot(options.plugin);
957
- const sourceRoot = path.join(options.plugin.rootDir, ...relativeRoot.split('/'));
958
- if (!await fs.pathExists(sourceRoot))
959
- return [];
960
- const dynamicKeys = new Set((options.plugin.manifest.artifacts?.targets ?? [])
961
- .filter((target) => target.target === 'codex.skill' && target.skillKey)
962
- .map((target) => target.skillKey));
963
- const targetRoot = await canonicalizePathThroughExistingAncestor(path.join(options.marketplaceRoot ?? resolveAwHome(options.env), 'plugins', options.plugin.manifest.name, ...relativeRoot.split('/')));
964
- const writes = [];
965
- for (const entry of await fs.readdir(sourceRoot, { withFileTypes: true })) {
966
- if (entry.isSymbolicLink()) {
967
- throw new Error(`Codex skills source 不能包含符号链接:${entry.name}`);
968
- }
969
- if (!entry.isDirectory() || dynamicKeys.has(entry.name))
970
- continue;
971
- const skillSource = path.join(sourceRoot, entry.name);
972
- if (!await fs.pathExists(path.join(skillSource, 'SKILL.md')))
973
- continue;
974
- const skillPath = path.join(targetRoot, entry.name);
975
- const identity = ownerIdentity(options.plugin.manifest.name, entry.name, 'codex', 'user');
976
- if (options.shallow) {
977
- writes.push({
978
- artifact: {
979
- path: skillPath,
980
- kind: 'skill',
981
- platform: 'codex',
982
- artifactKey: entry.name,
983
- },
984
- component: pathComponent({
985
- kind: 'skill-directory',
986
- path: skillPath,
987
- allowedRoot: targetRoot,
988
- identity,
989
- }),
990
- });
991
- continue;
992
- }
993
- const tree = await createCodexSkillExpectedTree({
994
- artifact: {
995
- target: 'codex.skill',
996
- skillKey: entry.name,
997
- state: 'present',
998
- content: { files: {} },
999
- },
1000
- sourceDirs: [skillSource],
1001
- sourceRequirementsReceiptPolicy: 'allow',
1002
- });
1003
- const canRecoverPristine = await canonicalizePathThroughExistingAncestor(skillSource)
1004
- !== path.resolve(skillPath);
1005
- const component = pathComponent({
1006
- kind: 'skill-directory',
1007
- path: skillPath,
1008
- allowedRoot: targetRoot,
1009
- identity,
1010
- ...(canRecoverPristine
1011
- ? {
1012
- pristineTree: pristineTreeDescriptor(tree),
1013
- }
1014
- : {}),
1015
- });
1016
- writes.push({
1017
- artifact: {
1018
- path: skillPath,
1019
- paths: Object.keys(tree.files).map((relativePath) => path.join(skillPath, relativePath)),
1020
- kind: 'skill',
1021
- platform: 'codex',
1022
- artifactKey: entry.name,
1023
- },
1024
- component,
1025
- action: skillWriteAction({
1026
- plugin: options.plugin,
1027
- platform: 'codex',
1028
- scope: 'user',
1029
- path: skillPath,
1030
- allowedRoot: targetRoot,
1031
- identity,
1032
- tree,
1033
- allowPristineUnowned: canRecoverPristine,
1034
- }),
1035
- });
1036
- }
1037
- return writes;
1038
- }
1039
- async function buildClaudeWrites(options) {
1040
- const paths = resolveClaudePaths(options.cwd, options.scope, options.env);
1041
- const writes = [];
1042
- const dynamic = await resolveDynamicArtifacts({
1043
- ...options,
1044
- platform: 'claude',
1045
- previousArtifacts: options.historicalArtifacts,
1046
- });
1047
- for (const artifact of dynamic.artifacts.filter((entry) => entry.target === 'claude.skill')) {
1048
- const skillKey = artifact.skillKey;
1049
- const tree = await createDynamicSkillExpectedTree({
1050
- artifact,
1051
- sourceDirs: dynamic.sourceDirs(artifact),
1052
- requireReadme: true,
1053
- requirementsReceipt: requirementsReceipt(options.plugin),
1054
- sourceRequirementsReceiptPolicy: 'allow',
1055
- });
1056
- writes.push(dynamicSkillWrite({
1057
- ...options,
1058
- platform: 'claude',
1059
- artifact,
1060
- skillPath: path.join(paths.configDir, 'skills', skillKey),
1061
- allowedRoot: path.join(paths.configDir, 'skills'),
1062
- tree,
1063
- }));
1064
- }
1065
- for (const artifact of generateInstructionArtifacts(options.plugin.manifest.name, resolvePlatformEntries(options.plugin.manifest.extensions?.instructions, 'claude'), 'claude')) {
1066
- writes.push(generatedTextWrite({
1067
- ...options,
1068
- platform: 'claude',
1069
- configDir: paths.configDir,
1070
- relativePath: artifact.relativePath,
1071
- artifactKey: artifact.artifactKey,
1072
- content: artifact.content,
1073
- kind: 'instruction',
1074
- }));
1075
- }
1076
- const permissions = resolvePlatformEntries(options.plugin.manifest.extensions?.permissions, 'claude');
1077
- if (permissions.length > 0) {
1078
- const component = createClaudePermissionsComponent({
1079
- identity: pluginOwnerIdentity(options.plugin, 'permissions', 'claude', options.scope),
1080
- scope: options.scope,
1081
- settingsPath: paths.settingsPath,
1082
- });
1083
- const desiredPermissions = mergeClaudePermissions(permissions);
1084
- const mutation = { kind: 'claude-permissions', permissions: desiredPermissions };
1085
- writes.push({
1086
- artifact: { path: paths.settingsPath, kind: 'permission', platform: 'claude' },
1087
- component,
1088
- action: {
1089
- kind: 'updateSharedConfig',
1090
- pluginId: pluginActionId(options.plugin),
1091
- platform: 'claude',
1092
- scope: options.scope,
1093
- owner: component.identity,
1094
- locator: component.locator,
1095
- mutation,
1096
- contentDigest: `sha256:${crypto.createHash('sha256').update(JSON.stringify(mutation)).digest('hex')}`,
1097
- },
1098
- });
1099
- }
1100
- return writes;
1101
- }
1102
- async function buildCodexWrites(options) {
1103
- const paths = resolveCodexPaths(options.cwd, options.scope, options.env);
1104
- const writes = [];
1105
- const dynamic = await resolveDynamicArtifacts({
1106
- ...options,
1107
- platform: 'codex',
1108
- previousArtifacts: options.historicalArtifacts,
1109
- });
1110
- const dynamicInstructions = dynamic.artifacts.filter((entry) => entry.target === 'codex.developerInstructionsBlock');
1111
- const dynamicRoles = dynamic.artifacts.filter((entry) => entry.target === 'codex.agentRole');
1112
- const dynamicSkills = dynamic.artifacts.filter((entry) => entry.target === 'codex.skill');
1113
- const repoRoot = await resolveRepoRoot(options.cwd);
1114
- if (options.scope === 'user') {
1115
- writes.push(...await buildBundledCodexSkillWrites({
1116
- plugin: options.plugin,
1117
- env: options.env,
1118
- marketplaceRoot: options.marketplaceRoot,
1119
- }));
1120
- }
1121
- const skillRoot = options.scope === 'user'
1122
- ? path.join(getCodexHome(options.env), 'skills')
1123
- : path.join(repoRoot, '.agents', 'skills');
1124
- for (const artifact of dynamicSkills) {
1125
- const tree = await createCodexSkillExpectedTree({
1126
- artifact,
1127
- sourceDirs: dynamic.sourceDirs(artifact),
1128
- requireReadme: true,
1129
- requirementsReceipt: requirementsReceipt(options.plugin),
1130
- sourceRequirementsReceiptPolicy: 'allow',
1131
- });
1132
- writes.push(dynamicSkillWrite({
1133
- ...options,
1134
- platform: 'codex',
1135
- artifact,
1136
- skillPath: path.join(skillRoot, artifact.skillKey),
1137
- allowedRoot: skillRoot,
1138
- tree,
1139
- }));
1140
- }
1141
- const staticInstructions = dynamicInstructions.length > 0
1142
- ? []
1143
- : resolvePlatformEntries(options.plugin.manifest.extensions?.instructions, 'codex');
1144
- const hasInstructionOutput = dynamicInstructions.length > 0 || staticInstructions.length > 0;
1145
- if (hasInstructionOutput) {
1146
- const component = createCodexInstructionComponent({
1147
- identity: pluginOwnerIdentity(options.plugin, 'developer-instructions', 'codex', options.scope),
1148
- scope: options.scope,
1149
- configPath: paths.configTomlPath,
1150
- });
1151
- const dynamicBlock = dynamicInstructions.length > 0
1152
- ? composeManagedDeveloperInstructionsBlock(options.plugin.manifest.name, dynamicInstructions
1153
- .filter((entry) => entry.state === 'present')
1154
- .map((entry) => ({ artifactId: entry.id, value: entry.content })))
1155
- : undefined;
1156
- const block = dynamicBlock?.value
1157
- ? { ...dynamicBlock, pluginName: createSharedOwnerToken(component.identity) }
1158
- : staticInstructions.length > 0
1159
- ? {
1160
- pluginName: createSharedOwnerToken(component.identity),
1161
- value: renderCodexManagedInstructionBlock(options.plugin.manifest.name, staticInstructions),
1162
- }
1163
- : undefined;
1164
- const instructionMutation = { kind: 'codex-instruction', ...(block ? { block } : {}) };
1165
- for (const artifact of dynamicInstructions.length > 0 ? dynamicInstructions : [{
1166
- id: undefined,
1167
- target: undefined,
1168
- state: 'present',
1169
- refreshKeys: [],
1170
- configDigest: undefined,
1171
- disabledReason: undefined,
1172
- }]) {
1173
- writes.push({
1174
- artifact: {
1175
- path: paths.configTomlPath,
1176
- kind: 'instruction',
1177
- platform: 'codex',
1178
- ...(artifact.id ? { id: artifact.id } : {}),
1179
- ...(artifact.target ? { target: artifact.target } : {}),
1180
- state: artifact.state,
1181
- refreshKeys: [...artifact.refreshKeys],
1182
- ...(artifact.configDigest ? { configDigest: artifact.configDigest } : {}),
1183
- ...(artifact.disabledReason ? { disabledReason: artifact.disabledReason } : {}),
1184
- },
1185
- ...(block ? { component } : {}),
1186
- action: {
1187
- kind: 'updateSharedConfig',
1188
- pluginId: pluginActionId(options.plugin),
1189
- platform: 'codex',
1190
- scope: options.scope,
1191
- owner: component.identity,
1192
- locator: component.locator,
1193
- mutation: instructionMutation,
1194
- contentDigest: `sha256:${crypto.createHash('sha256')
1195
- .update(JSON.stringify(instructionMutation)).digest('hex')}`,
1196
- },
1197
- });
1198
- }
1199
- }
1200
- for (const artifact of dynamicRoles) {
1201
- const roleKey = artifact.roleKey;
1202
- const rolePath = path.join(paths.configDir, 'agents', `${buildGeneratedName(options.plugin.manifest.name, roleKey)}.toml`);
1203
- const roleComponent = pathComponent({
1204
- kind: 'owned-text-file',
1205
- path: rolePath,
1206
- allowedRoot: paths.configDir,
1207
- identity: pluginOwnerIdentity(options.plugin, roleKey, 'codex', options.scope),
1208
- });
1209
- const registrationComponent = createCodexRoleRegistrationComponent({
1210
- identity: pluginOwnerIdentity(options.plugin, internalArtifactKey('role', roleKey), 'codex', options.scope),
1211
- fileIdentity: roleComponent.identity,
1212
- configPath: paths.configTomlPath,
1213
- roleKey,
1214
- rolePath,
1215
- });
1216
- const roleFile = artifact.content?.roleFile;
1217
- const description = artifact.content?.registration?.description
1218
- ?? roleFile?.description;
1219
- const present = artifact.state === 'present';
1220
- if (present && (!description || !roleFile?.developer_instructions)) {
1221
- throw new Error(`Artifact "${artifact.id}" 的 codex.agentRole 缺少 description 或 developer_instructions。`);
1222
- }
1223
- writes.push({
1224
- artifact: {
1225
- path: rolePath,
1226
- paths: [paths.configTomlPath, rolePath],
1227
- kind: 'agent',
1228
- platform: 'codex',
1229
- artifactKey: roleKey,
1230
- roleKey,
1231
- id: artifact.id,
1232
- target: artifact.target,
1233
- state: artifact.state,
1234
- refreshKeys: [...artifact.refreshKeys],
1235
- configDigest: artifact.configDigest,
1236
- ...(artifact.disabledReason ? { disabledReason: artifact.disabledReason } : {}),
1237
- },
1238
- ...(present ? { component: roleComponent } : {}),
1239
- ...(present ? { additionalComponents: [registrationComponent] } : {}),
1240
- ...(present ? {
1241
- action: (() => {
1242
- const content = renderCodexRole({
1243
- roleKey,
1244
- description: description,
1245
- developerInstructions: roleFile.developer_instructions,
1246
- model: roleFile.model,
1247
- modelReasoningEffort: roleFile.model_reasoning_effort,
1248
- });
1249
- return {
1250
- kind: 'writeOwnedArtifact',
1251
- pluginId: pluginActionId(options.plugin),
1252
- platform: 'codex',
1253
- scope: options.scope,
1254
- path: rolePath,
1255
- allowedRoot: paths.configDir,
1256
- owner: roleComponent.identity,
1257
- artifactKind: 'owned-text-file',
1258
- sourceContent: content,
1259
- content,
1260
- contentDigest: `sha256:${crypto.createHash('sha256').update(content).digest('hex')}`,
1261
- };
1262
- })(),
1263
- } : {}),
1264
- ...(present ? {
1265
- additionalActions: [(() => {
1266
- const mutation = {
1267
- kind: 'codex-role-registration',
1268
- roleKey,
1269
- configFile: path.relative(path.dirname(paths.configTomlPath), rolePath),
1270
- description: description,
1271
- };
1272
- return {
1273
- kind: 'updateSharedConfig',
1274
- pluginId: pluginActionId(options.plugin),
1275
- platform: 'codex',
1276
- scope: options.scope,
1277
- owner: registrationComponent.identity,
1278
- locator: registrationComponent.locator,
1279
- mutation,
1280
- contentDigest: `sha256:${crypto.createHash('sha256')
1281
- .update(JSON.stringify(mutation)).digest('hex')}`,
1282
- };
1283
- })()],
1284
- } : {}),
1285
- });
1286
- }
1287
- for (const artifact of generateCodexPermissionArtifacts(options.plugin.manifest.name, resolvePlatformEntries(options.plugin.manifest.extensions?.permissions, 'codex'))) {
1288
- writes.push(generatedTextWrite({
1289
- ...options,
1290
- platform: 'codex',
1291
- configDir: paths.configDir,
1292
- relativePath: artifact.relativePath,
1293
- artifactKey: artifact.artifactKey,
1294
- content: artifact.content,
1295
- kind: 'permission',
1296
- }));
1297
- }
1298
- for (const artifact of generateAgentArtifacts(options.plugin.manifest.name, resolvePlatformEntries(options.plugin.manifest.extensions?.agents, 'codex'), 'codex')) {
1299
- writes.push(generatedTextWrite({
1300
- ...options,
1301
- platform: 'codex',
1302
- configDir: paths.configDir,
1303
- relativePath: artifact.relativePath,
1304
- artifactKey: artifact.artifactKey,
1305
- content: artifact.content,
1306
- kind: 'agent',
1307
- }));
1308
- }
1309
- const codexPluginId = `${options.plugin.manifest.name}@${options.marketplaceName ?? options.plugin.marketplaceName ?? MARKETPLACE_NAME}`;
1310
- const nativeSurface = analyzeCodexSurface(options.plugin.manifest).nativeKinds.length > 0;
1311
- const userActivationDesired = options.scope === 'user'
1312
- || options.userActivationDesired?.has(codexPluginId) === true;
1313
- for (const activation of !nativeSurface ? [] : options.scope === 'project'
1314
- ? [
1315
- {
1316
- scope: 'user',
1317
- desired: userActivationDesired,
1318
- configPath: resolveCodexPaths(options.cwd, 'user', options.env).configTomlPath,
1319
- },
1320
- { scope: 'project', desired: true, configPath: paths.configTomlPath },
1321
- ]
1322
- : [{ scope: 'user', desired: true, configPath: paths.configTomlPath }]) {
1323
- const component = createCodexActivationComponent({
1324
- identity: pluginOwnerIdentity(options.plugin, internalArtifactKey('activation', activation.scope), 'codex', options.scope),
1325
- configPath: activation.configPath,
1326
- codexPluginId,
1327
- desired: activation.desired,
1328
- });
1329
- writes.push({
1330
- artifact: {
1331
- path: activation.configPath,
1332
- kind: 'instruction',
1333
- platform: 'codex',
1334
- artifactKey: internalArtifactKey('activation', activation.scope),
1335
- },
1336
- component,
1337
- action: {
1338
- kind: 'setActivation',
1339
- pluginId: pluginActionId(options.plugin),
1340
- platform: 'codex',
1341
- scope: activation.scope,
1342
- enabled: activation.desired,
1343
- configPath: activation.configPath,
1344
- registration: codexPluginId,
1345
- },
1346
- });
1347
- }
1348
- return writes;
1349
- }
1350
- async function buildInspectionWrites(options) {
1351
- const writes = [];
1352
- if (options.platform === 'claude') {
1353
- const paths = resolveClaudePaths(options.cwd, options.scope, options.env);
1354
- for (const artifact of generateInstructionArtifacts(options.plugin.manifest.name, resolvePlatformEntries(options.plugin.manifest.extensions?.instructions, 'claude'), 'claude')) {
1355
- writes.push(generatedTextWrite({
1356
- ...options,
1357
- configDir: paths.configDir,
1358
- relativePath: artifact.relativePath,
1359
- artifactKey: artifact.artifactKey,
1360
- content: artifact.content,
1361
- kind: 'instruction',
1362
- }));
1363
- }
1364
- if (resolvePlatformEntries(options.plugin.manifest.extensions?.permissions, 'claude').length > 0) {
1365
- writes.push({
1366
- artifact: { path: paths.settingsPath, kind: 'permission', platform: 'claude' },
1367
- component: createClaudePermissionsComponent({
1368
- identity: pluginOwnerIdentity(options.plugin, 'permissions', 'claude', options.scope),
1369
- scope: options.scope,
1370
- settingsPath: paths.settingsPath,
1371
- }),
1372
- });
1373
- }
1374
- for (const target of options.plugin.manifest.artifacts?.targets ?? []) {
1375
- if (target.platform !== 'claude'
1376
- || !target.scopes.includes(options.scope)
1377
- || target.target !== 'claude.skill'
1378
- || !target.skillKey)
1379
- continue;
1380
- const state = target.states.includes('present') ? 'present' : 'disabled';
1381
- const skillPath = path.join(paths.configDir, 'skills', target.skillKey);
1382
- const component = pathComponent({
1383
- kind: 'skill-directory',
1384
- path: skillPath,
1385
- allowedRoot: path.dirname(skillPath),
1386
- identity: ownerIdentity(options.plugin.manifest.name, target.skillKey, 'claude', options.scope),
1387
- });
1388
- writes.push({
1389
- artifact: {
1390
- path: skillPath,
1391
- kind: 'skill',
1392
- platform: 'claude',
1393
- artifactKey: target.skillKey,
1394
- id: target.id,
1395
- target: target.target,
1396
- state,
1397
- },
1398
- ...(state === 'present' ? { component } : {}),
1399
- ...(target.states.includes('present') && target.states.includes('disabled')
1400
- ? { optional: true }
1401
- : {}),
1402
- });
1403
- }
1404
- return writes;
1405
- }
1406
- const paths = resolveCodexPaths(options.cwd, options.scope, options.env);
1407
- const repoRoot = await resolveRepoRoot(options.cwd);
1408
- if (options.scope === 'user' && options.shallow !== true) {
1409
- writes.push(...await buildBundledCodexSkillWrites({
1410
- plugin: options.plugin,
1411
- env: options.env,
1412
- marketplaceRoot: options.marketplaceRoot,
1413
- }));
1414
- }
1415
- const instructions = resolvePlatformEntries(options.plugin.manifest.extensions?.instructions, 'codex');
1416
- const targets = (options.plugin.manifest.artifacts?.targets ?? []).filter((target) => target.platform === 'codex' && target.scopes.includes(options.scope));
1417
- const instructionTargets = targets.filter((target) => target.target === 'codex.developerInstructionsBlock');
1418
- const presentCapableInstructionTargets = instructionTargets.filter((target) => target.states.includes('present'));
1419
- const instructionDesired = presentCapableInstructionTargets.length > 0;
1420
- const instructionOptional = presentCapableInstructionTargets.length > 0
1421
- && presentCapableInstructionTargets.every((target) => target.states.includes('disabled'));
1422
- if (instructions.length > 0 || instructionDesired) {
1423
- writes.push({
1424
- artifact: { path: paths.configTomlPath, kind: 'instruction', platform: 'codex' },
1425
- component: createCodexInstructionComponent({
1426
- identity: pluginOwnerIdentity(options.plugin, 'developer-instructions', 'codex', options.scope),
1427
- scope: options.scope,
1428
- configPath: paths.configTomlPath,
1429
- }),
1430
- ...(instructions.length === 0 && instructionOptional ? { optional: true } : {}),
1431
- });
1432
- }
1433
- const dynamicSkillRoot = options.scope === 'user'
1434
- ? path.join(getCodexHome(options.env), 'skills')
1435
- : path.join(repoRoot, '.agents', 'skills');
1436
- for (const target of targets) {
1437
- const state = target.states.includes('present') ? 'present' : 'disabled';
1438
- if (target.target === 'codex.skill'
1439
- && target.delivery?.mode === 'aw-extension'
1440
- && target.skillKey) {
1441
- const skillPath = path.join(dynamicSkillRoot, target.skillKey);
1442
- const component = pathComponent({
1443
- kind: 'skill-directory',
1444
- path: skillPath,
1445
- allowedRoot: dynamicSkillRoot,
1446
- identity: ownerIdentity(options.plugin.manifest.name, target.skillKey, 'codex', options.scope),
1447
- });
1448
- writes.push({
1449
- artifact: {
1450
- path: skillPath,
1451
- kind: 'skill',
1452
- platform: 'codex',
1453
- artifactKey: target.skillKey,
1454
- id: target.id,
1455
- target: target.target,
1456
- state,
1457
- },
1458
- ...(state === 'present' ? { component } : {}),
1459
- ...(target.states.includes('present') && target.states.includes('disabled')
1460
- ? { optional: true }
1461
- : {}),
1462
- });
1463
- }
1464
- if (target.target === 'codex.agentRole' && target.roleKey) {
1465
- const rolePath = path.join(paths.configDir, 'agents', `${buildGeneratedName(options.plugin.manifest.name, target.roleKey)}.toml`);
1466
- const roleComponent = pathComponent({
1467
- kind: 'owned-text-file',
1468
- path: rolePath,
1469
- allowedRoot: paths.configDir,
1470
- identity: pluginOwnerIdentity(options.plugin, target.roleKey, 'codex', options.scope),
1471
- });
1472
- const registrationComponent = createCodexRoleRegistrationComponent({
1473
- identity: pluginOwnerIdentity(options.plugin, internalArtifactKey('role', target.roleKey), 'codex', options.scope),
1474
- fileIdentity: pluginOwnerIdentity(options.plugin, target.roleKey, 'codex', options.scope),
1475
- configPath: paths.configTomlPath,
1476
- roleKey: target.roleKey,
1477
- rolePath,
1478
- });
1479
- writes.push({
1480
- artifact: {
1481
- path: rolePath,
1482
- kind: 'agent',
1483
- platform: 'codex',
1484
- artifactKey: target.roleKey,
1485
- roleKey: target.roleKey,
1486
- id: target.id,
1487
- target: target.target,
1488
- state,
1489
- },
1490
- ...(state === 'present'
1491
- ? { component: roleComponent, additionalComponents: [registrationComponent] }
1492
- : {}),
1493
- ...(target.states.includes('present') && target.states.includes('disabled')
1494
- ? { optional: true }
1495
- : {}),
1496
- });
1497
- }
1498
- }
1499
- for (const artifact of generateCodexPermissionArtifacts(options.plugin.manifest.name, resolvePlatformEntries(options.plugin.manifest.extensions?.permissions, 'codex'))) {
1500
- writes.push(generatedTextWrite({
1501
- ...options,
1502
- configDir: paths.configDir,
1503
- relativePath: artifact.relativePath,
1504
- artifactKey: artifact.artifactKey,
1505
- content: artifact.content,
1506
- kind: 'permission',
1507
- }));
1508
- }
1509
- for (const artifact of generateAgentArtifacts(options.plugin.manifest.name, resolvePlatformEntries(options.plugin.manifest.extensions?.agents, 'codex'), 'codex')) {
1510
- writes.push(generatedTextWrite({
1511
- ...options,
1512
- configDir: paths.configDir,
1513
- relativePath: artifact.relativePath,
1514
- artifactKey: artifact.artifactKey,
1515
- content: artifact.content,
1516
- kind: 'agent',
1517
- }));
1518
- }
1519
- const nativeSurface = analyzeCodexSurface(options.plugin.manifest).nativeKinds.length > 0;
1520
- const codexPluginId = `${options.plugin.manifest.name}@${options.marketplaceName ?? options.plugin.marketplaceName ?? MARKETPLACE_NAME}`;
1521
- const userActivationDesired = options.scope === 'user'
1522
- || options.userActivationDesired?.has(codexPluginId) === true;
1523
- for (const activation of !nativeSurface ? [] : options.scope === 'project'
1524
- ? [
1525
- {
1526
- scope: 'user',
1527
- desired: userActivationDesired,
1528
- configPath: resolveCodexPaths(options.cwd, 'user', options.env).configTomlPath,
1529
- },
1530
- { scope: 'project', desired: true, configPath: paths.configTomlPath },
1531
- ]
1532
- : [{ scope: 'user', desired: true, configPath: paths.configTomlPath }]) {
1533
- writes.push({
1534
- artifact: {
1535
- path: activation.configPath,
1536
- kind: 'instruction',
1537
- platform: 'codex',
1538
- artifactKey: internalArtifactKey('activation', activation.scope),
1539
- },
1540
- component: createCodexActivationComponent({
1541
- identity: pluginOwnerIdentity(options.plugin, internalArtifactKey('activation', activation.scope), 'codex', options.scope),
1542
- configPath: activation.configPath,
1543
- codexPluginId,
1544
- desired: activation.desired,
1545
- }),
1546
- });
1547
- }
1548
- return writes;
1549
- }
1550
- async function buildHistoricalComponents(options) {
1551
- const components = [];
1552
- const paths = options.platform === 'codex'
1553
- ? resolveCodexPaths(options.cwd, options.scope, options.env)
1554
- : resolveClaudePaths(options.cwd, options.scope, options.env);
1555
- const historicalIdentity = (artifactKey) => ({
1556
- ...ownerIdentity(options.pluginName, artifactKey, options.platform, options.scope),
1557
- ...(options.sourceId ? {
1558
- sourceId: options.sourceId,
1559
- pluginId: options.pluginId,
1560
- } : {}),
1561
- });
1562
- for (const artifact of options.artifacts) {
1563
- if (artifact.platform !== options.platform)
1564
- continue;
1565
- if (artifact.state === 'disabled')
1566
- continue;
1567
- if (options.platform === 'codex'
1568
- && artifact.artifactKey !== undefined
1569
- && new Set([
1570
- internalArtifactKey('activation', 'user'),
1571
- internalArtifactKey('activation', 'project'),
1572
- ]).has(artifact.artifactKey)) {
1573
- continue;
1574
- }
1575
- if (options.command === 'cleanup'
1576
- && artifact.kind === 'hook'
1577
- && options.platform === 'codex') {
1578
- for (const identity of artifact.hookGroups ?? []) {
1579
- components.push(createLegacyCodexHookCleanupComponent({
1580
- pluginName: options.pluginName,
1581
- scope: options.scope,
1582
- hooksPath: path.join(resolveCodexPaths(options.cwd, options.scope, options.env).configDir, 'hooks.json'),
1583
- identity,
1584
- }));
1585
- }
1586
- continue;
1587
- }
1588
- if (options.command === 'cleanup'
1589
- && artifact.kind === 'mcpServer'
1590
- && options.platform === 'codex'
1591
- && artifact.artifactKey) {
1592
- components.push(createLegacyCodexMcpCleanupComponent({
1593
- identity: historicalIdentity(internalArtifactKey('mcp', artifact.artifactKey)),
1594
- configPath: resolveCodexPaths(options.cwd, options.scope, options.env).configTomlPath,
1595
- serverKey: artifact.artifactKey,
1596
- }));
1597
- continue;
1598
- }
1599
- if (artifact.kind === 'instruction' && options.platform === 'codex' && !artifact.artifactKey) {
1600
- components.push(createCodexInstructionComponent({
1601
- identity: historicalIdentity('developer-instructions'),
1602
- scope: options.scope,
1603
- configPath: resolveCodexPaths(options.cwd, options.scope, options.env).configTomlPath,
1604
- }));
1605
- continue;
1606
- }
1607
- if (artifact.kind === 'permission' && options.platform === 'claude') {
1608
- components.push(createClaudePermissionsComponent({
1609
- identity: historicalIdentity('permissions'),
1610
- scope: options.scope,
1611
- settingsPath: resolveClaudePaths(options.cwd, options.scope, options.env).settingsPath,
1612
- }));
1613
- continue;
1614
- }
1615
- if (artifact.target === 'codex.agentRole'
1616
- && options.platform === 'codex'
1617
- && (artifact.roleKey || artifact.artifactKey)) {
1618
- const roleKey = artifact.roleKey ?? artifact.artifactKey;
1619
- components.push(createCodexRoleRegistrationComponent({
1620
- identity: historicalIdentity(internalArtifactKey('role', roleKey)),
1621
- fileIdentity: historicalIdentity(roleKey),
1622
- configPath: resolveCodexPaths(options.cwd, options.scope, options.env).configTomlPath,
1623
- roleKey,
1624
- rolePath: artifact.path,
1625
- }));
1626
- }
1627
- if (!artifact.artifactKey)
1628
- continue;
1629
- const identity = ownerIdentity(options.pluginName, artifact.artifactKey, options.platform, options.scope);
1630
- components.push(pathComponent({
1631
- kind: artifact.kind === 'skill' ? 'skill-directory' : 'owned-text-file',
1632
- path: artifact.path,
1633
- allowedRoot: artifact.kind === 'skill' ? path.dirname(artifact.path) : paths.configDir,
1634
- identity,
1635
- }));
1636
- }
1637
- return components;
1638
- }
1639
- function isAbsentCodexResiduePath(error) {
1640
- const code = error.code;
1641
- return code === 'ENOENT';
1642
- }
1643
- async function lstatCodexResidueIfPresent(filePath) {
1644
- return fs.lstat(filePath).catch((error) => {
1645
- if (isAbsentCodexResiduePath(error))
1646
- return null;
1647
- throw error;
1648
- });
1649
- }
1650
- async function readdirCodexResidueIfPresent(directory) {
1651
- const stats = await lstatCodexResidueIfPresent(directory);
1652
- if (!stats)
1653
- return [];
1654
- if (!stats.isDirectory() || stats.isSymbolicLink()) {
1655
- throw new Error(`Codex residue root is not a canonical directory: ${directory}`);
1656
- }
1657
- return fs.readdir(directory, { withFileTypes: true });
1658
- }
1659
- export async function inspectCodexNativeResidue(options) {
1660
- const marketplaceName = options.marketplaceName ?? MARKETPLACE_NAME;
1661
- const marketplaceRoot = options.marketplaceRoot ?? resolveAwHome(options.env);
1662
- const pluginId = `${options.pluginName}@${marketplaceName}`;
1663
- const configPath = resolveCodexPaths(options.cwd, 'user', options.env).configTomlPath;
1664
- const config = await readCodexConfigFile(configPath);
1665
- const marketplace = config.snapshot.marketplaces[marketplaceName];
1666
- const canonicalMarketplace = marketplace?.sourceType === 'local'
1667
- && !marketplace.sourcePointsToManifest
1668
- && path.normalize(marketplace.source ?? '') === path.normalize(marketplaceRoot);
1669
- const cacheRoot = path.join(getCodexHome(options.env), 'plugins', 'cache', marketplaceName, options.pluginName);
1670
- const installedRoot = path.join(getCodexHome(options.env), 'plugins', pluginId);
1671
- const installedManifest = path.join(installedRoot, 'manifest.json');
1672
- const labels = [];
1673
- const foreignLabels = [];
1674
- const versions = new Set();
1675
- const installedVersions = new Set();
1676
- const cacheVersions = new Set();
1677
- const configEntry = config.snapshot.plugins[pluginId];
1678
- if (configEntry) {
1679
- (canonicalMarketplace ? labels : foreignLabels).push(`${configPath}#plugins.${pluginId}`);
1680
- }
1681
- const inspectManifest = async (manifestPath, kind, directoryVersion) => {
1682
- const stats = await lstatCodexResidueIfPresent(manifestPath);
1683
- if (!stats)
1684
- return;
1685
- if (!stats.isFile() || stats.isSymbolicLink()) {
1686
- foreignLabels.push(manifestPath);
1687
- return;
1688
- }
1689
- const manifest = await fs.readJson(manifestPath);
1690
- const identity = manifest?.id ?? manifest?.name;
1691
- const version = typeof manifest?.version === 'string' ? manifest.version : null;
1692
- if (!canonicalMarketplace
1693
- || (identity !== options.pluginName && identity !== pluginId)
1694
- || !version
1695
- || (directoryVersion !== undefined && version !== directoryVersion)) {
1696
- foreignLabels.push(manifestPath);
1697
- return;
1698
- }
1699
- labels.push(manifestPath);
1700
- versions.add(version);
1701
- (kind === 'installed' ? installedVersions : cacheVersions).add(version);
1702
- };
1703
- for (const entry of await readdirCodexResidueIfPresent(cacheRoot)) {
1704
- if (!entry.isDirectory() || entry.isSymbolicLink()) {
1705
- foreignLabels.push(path.join(cacheRoot, entry.name));
1706
- continue;
1707
- }
1708
- const versionRoot = path.join(cacheRoot, entry.name);
1709
- const candidates = [
1710
- path.join(versionRoot, 'plugin.json'),
1711
- path.join(versionRoot, '.codex-plugin', 'plugin.json'),
1712
- ];
1713
- const existing = [];
1714
- for (const candidate of candidates) {
1715
- if (await lstatCodexResidueIfPresent(candidate))
1716
- existing.push(candidate);
1717
- }
1718
- if (existing.length === 0)
1719
- foreignLabels.push(versionRoot);
1720
- for (const candidate of existing)
1721
- await inspectManifest(candidate, 'cache', entry.name);
1722
- }
1723
- const installedStats = await lstatCodexResidueIfPresent(installedRoot);
1724
- if (installedStats && (!installedStats.isDirectory() || installedStats.isSymbolicLink())) {
1725
- foreignLabels.push(installedRoot);
1726
- }
1727
- else if (installedStats) {
1728
- await inspectManifest(installedManifest, 'installed');
1729
- }
1730
- return foreignLabels.length > 0
1731
- ? {
1732
- state: 'foreign', labels: [...labels, ...foreignLabels], versions: [...versions],
1733
- installedVersions: [...installedVersions], cacheVersions: [...cacheVersions],
1734
- enabled: configEntry?.enabled === true,
1735
- }
1736
- : labels.length > 0
1737
- ? {
1738
- state: 'present', labels, versions: [...versions],
1739
- installedVersions: [...installedVersions], cacheVersions: [...cacheVersions],
1740
- enabled: configEntry?.enabled === true,
1741
- }
1742
- : {
1743
- state: 'absent', labels: [], versions: [],
1744
- installedVersions: [], cacheVersions: [], enabled: false,
1745
- };
1746
- }
1747
- function isPathWithin(root, candidate) {
1748
- const relative = path.relative(path.resolve(root), path.resolve(candidate));
1749
- return relative === '' || (!relative.startsWith(`..${path.sep}`) && relative !== '..' && !path.isAbsolute(relative));
1750
- }
1751
- async function codexPayloadPathSafe(payloadRoot) {
1752
- const rootStats = await lstatCodexResidueIfPresent(payloadRoot);
1753
- if (!rootStats?.isDirectory() || rootStats.isSymbolicLink())
1754
- return false;
1755
- const pending = [payloadRoot];
1756
- while (pending.length > 0) {
1757
- const current = pending.pop();
1758
- for (const entry of await fs.readdir(current, { withFileTypes: true })) {
1759
- const entryPath = path.join(current, entry.name);
1760
- if (entry.isSymbolicLink()) {
1761
- const target = path.resolve(path.dirname(entryPath), await fs.readlink(entryPath));
1762
- if (!isPathWithin(payloadRoot, target))
1763
- return false;
1764
- }
1765
- else if (entry.isDirectory()) {
1766
- pending.push(entryPath);
1767
- }
1768
- else if (!entry.isFile()) {
1769
- return false;
1770
- }
1771
- }
1772
- }
1773
- return true;
1774
- }
1775
- function codexNativeRenderTargets(plugin) {
1776
- return (plugin.manifest.artifacts?.targets ?? []).filter((target) => target.platform === 'codex'
1777
- && target.target === 'codex.skill'
1778
- && target.delivery?.mode === 'codex-native');
1779
- }
1780
- async function readCodexNativeRenderReceiptEvidence(payloadRoot) {
1781
- const receiptPath = path.join(payloadRoot, CODEX_NATIVE_RENDER_RECEIPT_PATH);
1782
- const stats = await lstatCodexResidueIfPresent(receiptPath);
1783
- if (!stats)
1784
- return { state: 'absent' };
1785
- if (!stats.isFile() || stats.isSymbolicLink())
1786
- return { state: 'unreadable' };
1787
- try {
1788
- return {
1789
- state: 'present',
1790
- receipt: parseCodexNativeRenderReceipt(await fs.readFile(receiptPath, 'utf8')),
1791
- };
1792
- }
1793
- catch {
1794
- return { state: 'unreadable' };
1795
- }
1796
- }
1797
- async function resolveCodexNativeRenderExpectedEvidence(options) {
1798
- const logicalPluginId = options.plugin.pluginId
1799
- ?? (options.plugin.sourceId ? `${options.plugin.sourceId}/${options.plugin.manifest.name}` : undefined);
1800
- if (!logicalPluginId || !options.plugin.sourceId || !options.plugin.sourceIdentity) {
1801
- throw new Error('Codex native render projection requires namespaced source identity.');
1802
- }
1803
- const marketplace = await readStrictNamespacedMarketplaceRoot({
1804
- rootDir: options.marketplaceRoot,
1805
- sourceId: options.plugin.sourceId,
1806
- marketplaceName: options.marketplaceName,
1807
- });
1808
- if (!marketplace) {
1809
- throw new Error('Codex native render canonical marketplace generation is missing.');
1810
- }
1811
- const payloadRoot = path.join(options.marketplaceRoot, 'plugins', options.plugin.manifest.name);
1812
- const receiptEvidence = await readCodexNativeRenderReceiptEvidence(payloadRoot);
1813
- if (receiptEvidence.state !== 'present') {
1814
- throw new Error('Codex native render canonical marketplace receipt is missing or unreadable.');
1815
- }
1816
- const receipt = receiptEvidence.receipt;
1817
- if (receipt.logicalPluginId !== logicalPluginId
1818
- || receipt.strategy !== 'shared-user'
1819
- || receipt.sourceIdentity !== marketplace.config.sourceIdentity) {
1820
- throw new Error('Codex native render canonical marketplace receipt identity is invalid.');
1821
- }
1822
- const payloadDigest = await calculateCodexNativeRenderPayloadDigest(payloadRoot);
1823
- if (payloadDigest !== receipt.payloadDigest) {
1824
- throw new Error('Codex native render canonical marketplace payload does not match its receipt.');
1825
- }
1826
- const sourceManifestPath = path.join(payloadRoot, '.agents-plugin', 'plugin.json');
1827
- const sourceManifestStats = await lstatCodexResidueIfPresent(sourceManifestPath);
1828
- const sourceManifest = sourceManifestStats?.isFile() && !sourceManifestStats.isSymbolicLink()
1829
- ? await fs.readJson(sourceManifestPath).catch(() => null)
1830
- : null;
1831
- if (sourceManifest?.name !== options.plugin.manifest.name
1832
- || sourceManifest.version !== receipt.pluginVersion) {
1833
- throw new Error('Codex native render canonical marketplace plugin identity is invalid.');
1834
- }
1835
- const codexManifestPath = path.join(payloadRoot, '.codex-plugin', 'plugin.json');
1836
- const codexManifestStats = await lstatCodexResidueIfPresent(codexManifestPath);
1837
- const codexManifest = codexManifestStats?.isFile() && !codexManifestStats.isSymbolicLink()
1838
- ? await fs.readJson(codexManifestPath).catch(() => null)
1839
- : null;
1840
- if (!codexManifest) {
1841
- throw new Error('Codex native render canonical marketplace manifest is invalid.');
1842
- }
1843
- const rawSkillsRoot = typeof codexManifest.skills === 'string' && codexManifest.skills.trim() !== ''
1844
- ? codexManifest.skills
1845
- : './codex/skills';
1846
- const skillsRelativeRoot = path.posix.normalize(rawSkillsRoot.replace(/^\.\//u, ''));
1847
- if (skillsRelativeRoot === '..'
1848
- || skillsRelativeRoot.startsWith('../')
1849
- || path.posix.isAbsolute(skillsRelativeRoot)) {
1850
- throw new Error('Codex native render canonical marketplace skills root is invalid.');
1851
- }
1852
- const emptyArtifactDigest = createCodexSkillStagingTreeContentDigest({
1853
- skillKey: 'empty',
1854
- files: {},
1855
- });
1856
- for (const receiptTarget of receipt.targets) {
1857
- const skillRoot = path.join(payloadRoot, ...skillsRelativeRoot.split('/'), receiptTarget.skillKey);
1858
- if (await lstatCodexResidueIfPresent(path.join(skillRoot, '.agent-workflows-owner.json'))) {
1859
- throw new Error('Codex native render canonical marketplace Skill has an AW owner marker.');
1860
- }
1861
- const tree = await readDynamicSkillTree(skillRoot, 'codex.skill');
1862
- const artifactDigest = tree
1863
- ? createCodexSkillStagingTreeContentDigest(tree)
1864
- : emptyArtifactDigest;
1865
- if (artifactDigest !== receiptTarget.artifactDigest) {
1866
- throw new Error('Codex native render canonical marketplace Skill does not match its receipt.');
1867
- }
1868
- }
1869
- const targets = codexNativeRenderTargets(options.plugin);
1870
- const receiptTargets = new Map(receipt.targets.map((target) => [target.id, target]));
1871
- if (targets.length === 0
1872
- || targets.length !== receipt.targets.length
1873
- || targets.some((target) => receiptTargets.get(target.id)?.skillKey !== target.skillKey)) {
1874
- return { payloadDigest };
1875
- }
1876
- const renderer = await resolveArtifactRendererTarget(options.plugin);
1877
- if (renderer.noOp || !renderer.rendererDigest) {
1878
- throw new Error('Codex native render projection requires an artifact renderer.');
1879
- }
1880
- const config = await resolveArtifactRenderConfig({
1881
- sourceId: options.plugin.sourceId,
1882
- sourceIdentity: options.plugin.sourceIdentity,
1883
- pluginName: options.plugin.manifest.name,
1884
- manifest: options.plugin.manifest,
1885
- scope: 'user',
1886
- cwd: options.plugin.rootDir,
1887
- env: options.env,
1888
- });
1889
- if (config.issues.length > 0) {
1890
- throw new Error(`Invalid AW config for ${options.plugin.manifest.name}.`);
1891
- }
1892
- const projection = createCodexNativeRenderProjection({
1893
- logicalPluginId,
1894
- sourceIdentity: options.plugin.sourceIdentity,
1895
- pluginVersion: options.plugin.manifest.version,
1896
- rendererDigest: renderer.rendererDigest,
1897
- targets: targets.map((target) => ({
1898
- id: target.id,
1899
- skillKey: target.skillKey,
1900
- configDigest: createArtifactConfigDigest({
1901
- artifact: {
1902
- id: target.id,
1903
- platform: 'codex',
1904
- target: 'codex.skill',
1905
- refreshKeys: target.refreshKeys,
1906
- },
1907
- configValues: config.values,
1908
- digestSalt: renderer.rendererDigest,
1909
- }),
1910
- artifactDigest: receiptTargets.get(target.id).artifactDigest,
1911
- })),
1912
- });
1913
- return {
1914
- projection,
1915
- payloadDigest,
1916
- };
1917
- }
1918
- function observedCodexNativeState(observation, operation) {
1919
- if (observation.state === 'observed') {
1920
- return observation.value === 'installed' ? 'present' : 'absent';
1921
- }
1922
- if (observation.state === 'unobserved')
1923
- return 'partial';
1924
- throw new Error(`codexNativeObservationFailed: ${operation} requires Codex plugin list evidence.`);
1925
- }
1926
- function requireObservedCodexNativeState(observation, operation) {
1927
- if (observation.state === 'observed')
1928
- return observation.value;
1929
- const code = observation.state === 'failed'
1930
- ? 'codexNativeObservationFailed'
1931
- : 'codexNativeObservationUnavailable';
1932
- throw new Error(`${code}: ${operation} requires Codex plugin list evidence.`);
1933
- }
1934
- export async function inspectCodexPluginRegistrationLive(options) {
1935
- if (!options.nativeCliRunner) {
1936
- return {
1937
- observation: { state: 'unobserved' },
1938
- observations: { user: false, project: false },
1939
- activations: {},
1940
- };
1941
- }
1942
- const manager = options.manager ?? SYSTEM_CODEX_MANAGER;
1943
- const readEntries = async (cwd) => {
1944
- try {
1945
- const result = await runNativeCli(buildCodexPluginListJsonInvocation(manager, cwd, options.env), options.nativeCliRunner);
1946
- if (result.exitCode !== 0) {
1947
- return { observation: { state: 'failed', reason: 'unavailable' } };
1948
- }
1949
- const entries = parseCodexPluginListJson(result.stdout);
1950
- return { entries, observation: observeCodexNativePlugin(options.pluginId, entries) };
1951
- }
1952
- catch (error) {
1953
- return {
1954
- observation: error.code === 'ENOENT'
1955
- ? { state: 'unobserved' }
1956
- : { state: 'failed', reason: 'unavailable' },
1957
- };
1958
- }
1959
- };
1960
- const [exactEntries, neutralEntries] = await Promise.all([
1961
- readEntries(options.cwd),
1962
- readEntries(os.tmpdir()),
1963
- ]);
1964
- const enabled = (values) => values?.find((entry) => entry.pluginId === options.pluginId)?.enabled;
1965
- return {
1966
- observation: combineCodexNativePluginObservations([
1967
- exactEntries.observation,
1968
- neutralEntries.observation,
1969
- ]),
1970
- observations: {
1971
- user: neutralEntries.entries !== undefined,
1972
- project: exactEntries.entries !== undefined,
1973
- },
1974
- activations: {
1975
- user: enabled(neutralEntries.entries),
1976
- project: enabled(exactEntries.entries),
1977
- },
1978
- };
1979
- }
1980
- /**
1981
- * Read-only filesystem projection for the payload-aware shared-native classifier.
1982
- * It does not invoke Codex or create native/config state.
1983
- */
1984
- export async function inspectCodexSharedNativeLive(options) {
1985
- const inspectionMode = options.inspectionMode ?? 'payload';
1986
- const dynamicProjectionExpected = options.nativeRenderPlugin !== undefined
1987
- && codexNativeRenderTargets(options.nativeRenderPlugin).length > 0;
1988
- if (inspectionMode === 'payload'
1989
- && options.expectedPayloadDigest === undefined
1990
- && !dynamicProjectionExpected) {
1991
- throw new Error('Payload inspection requires the expected shared native digest.');
1992
- }
1993
- const marketplaceName = options.marketplaceName ?? MARKETPLACE_NAME;
1994
- const marketplaceRoot = options.marketplaceRoot ?? path.join(resolveAwHome(options.env), 'marketplaces', options.sourceId);
1995
651
  const pluginId = `${options.pluginName}@${marketplaceName}`;
1996
652
  const config = await readCodexConfigFile(resolveCodexPaths(options.cwd, 'user', options.env).configTomlPath);
1997
- const cacheRoot = path.join(getCodexHome(options.env), 'plugins', 'cache', marketplaceName, options.pluginName);
1998
- const installedManifestPath = path.join(getCodexHome(options.env), 'plugins', pluginId, 'manifest.json');
1999
- const cacheRootStats = await lstatCodexResidueIfPresent(cacheRoot);
2000
- let unsafeCachePath = Boolean(cacheRootStats
2001
- && (!cacheRootStats.isDirectory() || cacheRootStats.isSymbolicLink()));
2002
- const candidates = [];
2003
- if (cacheRootStats?.isDirectory() && !cacheRootStats.isSymbolicLink()) {
2004
- for (const entry of await fs.readdir(cacheRoot, { withFileTypes: true })) {
2005
- const payloadPath = path.join(cacheRoot, entry.name);
2006
- if (!entry.isDirectory() || entry.isSymbolicLink()) {
2007
- unsafeCachePath = true;
2008
- continue;
2009
- }
2010
- const manifestPaths = [
2011
- path.join(payloadPath, 'plugin.json'),
2012
- path.join(payloadPath, '.codex-plugin', 'plugin.json'),
2013
- ];
2014
- const existingManifestPaths = [];
2015
- for (const manifestPath of manifestPaths) {
2016
- const stats = await lstatCodexResidueIfPresent(manifestPath);
2017
- if (stats)
2018
- existingManifestPaths.push(manifestPath);
2019
- }
2020
- if (existingManifestPaths.length !== 1) {
2021
- candidates.push({
2022
- version: entry.name,
2023
- manifest: existingManifestPaths.length === 0 ? { state: 'absent' } : { state: 'unreadable' },
2024
- payloadPath,
2025
- projectionReceipt: { state: 'absent' },
2026
- });
2027
- continue;
2028
- }
2029
- const manifestPath = existingManifestPaths[0];
2030
- const manifestStats = await lstatCodexResidueIfPresent(manifestPath);
2031
- if (!manifestStats?.isFile() || manifestStats.isSymbolicLink()) {
2032
- candidates.push({
2033
- version: entry.name,
2034
- manifest: { state: 'unreadable' },
2035
- payloadPath,
2036
- projectionReceipt: { state: 'absent' },
2037
- });
2038
- continue;
2039
- }
2040
- let manifest;
2041
- try {
2042
- const value = await fs.readJson(manifestPath);
2043
- if (!value || typeof value !== 'object' || Array.isArray(value))
2044
- throw new Error('invalid manifest');
2045
- manifest = value;
2046
- }
2047
- catch {
2048
- candidates.push({
2049
- version: entry.name,
2050
- manifest: { state: 'unreadable' },
2051
- payloadPath,
2052
- projectionReceipt: { state: 'absent' },
2053
- });
2054
- continue;
2055
- }
2056
- const projectionReceipt = await readCodexNativeRenderReceiptEvidence(payloadPath);
2057
- const pathSafe = inspectionMode === 'shallow'
2058
- ? true
2059
- : await codexPayloadPathSafe(payloadPath);
2060
- let payloadDigest;
2061
- if (pathSafe && inspectionMode === 'payload') {
2062
- try {
2063
- payloadDigest = dynamicProjectionExpected || projectionReceipt.state !== 'absent'
2064
- ? await calculateCodexNativeRenderPayloadDigest(payloadPath)
2065
- : await calculateCodexSharedNativePayloadDigest(payloadPath);
2066
- }
2067
- catch {
2068
- candidates.push({
2069
- version: entry.name,
2070
- manifest: { state: 'unreadable' },
2071
- payloadPath,
2072
- projectionReceipt,
2073
- });
2074
- continue;
2075
- }
2076
- }
2077
- candidates.push({
2078
- version: typeof manifest.version === 'string' ? manifest.version : entry.name,
2079
- payloadPath,
2080
- projectionReceipt,
2081
- manifest: {
2082
- state: 'present',
2083
- identity: typeof manifest.id === 'string'
2084
- ? manifest.id
2085
- : typeof manifest.name === 'string' ? manifest.name : '',
2086
- version: typeof manifest.version === 'string' ? manifest.version : '',
2087
- payloadDigest,
2088
- payloadPath,
2089
- pathSafe,
2090
- },
2091
- });
2092
- }
2093
- }
2094
- let installedVersion;
2095
- const installedStats = await lstatCodexResidueIfPresent(installedManifestPath);
2096
- if (installedStats?.isFile() && !installedStats.isSymbolicLink()) {
2097
- try {
2098
- const installed = await fs.readJson(installedManifestPath);
2099
- if (typeof installed.version === 'string')
2100
- installedVersion = installed.version;
2101
- }
2102
- catch {
2103
- installedVersion = undefined;
2104
- }
2105
- }
2106
- const selected = candidates.find((candidate) => candidate.version === installedVersion)
2107
- ?? candidates.find((candidate) => candidate.version === options.desiredVersion)
2108
- ?? (candidates.length === 1 ? candidates[0] : undefined);
2109
653
  const pluginList = options.pluginRegistration ?? await inspectCodexPluginRegistrationLive({
2110
654
  pluginId,
2111
655
  cwd: options.cwd,
2112
656
  env: options.env,
2113
657
  nativeCliRunner: options.nativeCliRunner,
2114
658
  manager: options.manager,
659
+ scope: options.observationScope,
660
+ observationSession: options.observationSession,
2115
661
  });
2116
- const manifest = unsafeCachePath
2117
- ? {
2118
- state: 'present',
2119
- identity: pluginId,
2120
- version: options.desiredVersion,
2121
- ...(options.expectedPayloadDigest ? { payloadDigest: options.expectedPayloadDigest } : {}),
2122
- payloadPath: cacheRoot,
2123
- pathSafe: false,
2124
- }
2125
- : selected?.manifest ?? { state: 'absent' };
2126
- const projectionReceipt = selected?.projectionReceipt;
2127
- let expectedProjection;
2128
- let expectedPayloadDigest = dynamicProjectionExpected ? undefined : options.expectedPayloadDigest;
2129
- const expectedLogicalPluginId = options.nativeRenderPlugin?.pluginId
2130
- ?? (options.nativeRenderPlugin?.sourceId
2131
- ? `${options.nativeRenderPlugin.sourceId}/${options.nativeRenderPlugin.manifest.name}`
2132
- : undefined);
2133
- const expectedProjectionIdentity = dynamicProjectionExpected && expectedLogicalPluginId
2134
- ? {
2135
- logicalPluginId: expectedLogicalPluginId,
2136
- strategy: 'shared-user',
2137
- }
2138
- : undefined;
2139
- if (dynamicProjectionExpected && options.nativeRenderPlugin) {
2140
- try {
2141
- const expected = await resolveCodexNativeRenderExpectedEvidence({
2142
- plugin: options.nativeRenderPlugin,
2143
- marketplaceRoot,
2144
- marketplaceName,
2145
- env: options.env,
2146
- });
2147
- expectedProjection = expected.projection;
2148
- expectedPayloadDigest = expected.payloadDigest;
2149
- }
2150
- catch {
2151
- // Canonical marketplace evidence is weak state. Missing evidence is replaceable damage.
2152
- }
2153
- }
662
+ const expectedPayloadDigest = options.expectedPayloadDigest;
2154
663
  const sharedNative = inspectCodexSharedNative({
2155
664
  pluginName: options.pluginName,
2156
665
  pluginId,
@@ -2159,323 +668,14 @@ export async function inspectCodexSharedNativeLive(options) {
2159
668
  desiredVersion: options.desiredVersion,
2160
669
  expectedPayloadDigest,
2161
670
  marketplaceRegistration: config.snapshot.marketplaces[marketplaceName],
2162
- marketplacePathSafe: await isSafeCodexMarketplaceRoot({
2163
- awHome: resolveAwHome(options.env),
2164
- sourceId: options.sourceId,
2165
- marketplaceRoot,
2166
- }),
671
+ marketplacePathSafe,
2167
672
  observation: pluginList.observation,
2168
- manifest,
2169
- ...(dynamicProjectionExpected ? { expectedProjectionRequired: true } : {}),
2170
- ...(expectedProjectionIdentity ? { expectedProjectionIdentity } : {}),
2171
- ...(expectedProjection ? { expectedProjection } : {}),
2172
- ...(projectionReceipt ? { projectionReceipt } : {}),
2173
673
  });
2174
674
  return {
2175
675
  sharedNative,
2176
676
  ...(expectedPayloadDigest ? { expectedPayloadDigest } : {}),
2177
- ...(expectedProjection ? { expectedProjection } : {}),
2178
- activationObservations: pluginList.observations,
2179
- activations: pluginList.activations,
2180
- ...((sharedNative.state === 'missing' || sharedNative.state === 'unobserved')
2181
- && sharedNative.staleCacheObserved
2182
- ? { staleCacheObserved: true }
2183
- : {}),
2184
- ...(manifest.state === 'present' ? {
2185
- ...(manifest.payloadDigest ? { actualPayloadDigest: manifest.payloadDigest } : {}),
2186
- payloadPath: manifest.payloadPath,
2187
- } : {}),
2188
677
  };
2189
678
  }
2190
- export async function codexNativeRemovalRequired(options) {
2191
- const residue = await inspectCodexNativeResidue(options);
2192
- if (residue.state === 'present')
2193
- return true;
2194
- if (residue.state === 'foreign')
2195
- return false;
2196
- const marketplaceName = options.marketplaceName ?? MARKETPLACE_NAME;
2197
- const marketplaceRoot = options.marketplaceRoot ?? resolveAwHome(options.env);
2198
- const config = await readCodexConfigFile(resolveCodexPaths(options.cwd, 'user', options.env).configTomlPath);
2199
- const marketplace = config.snapshot.marketplaces[marketplaceName];
2200
- const canonical = marketplace?.sourceType === 'local'
2201
- && !marketplace.sourcePointsToManifest
2202
- && path.normalize(marketplace.source ?? '') === path.normalize(marketplaceRoot);
2203
- if (!canonical)
2204
- return false;
2205
- return (await discoverCodexNativeResidueNames(options)).length === 0;
2206
- }
2207
- export async function discoverCodexNativeResidueNames(options) {
2208
- const marketplaceName = options.marketplaceName ?? MARKETPLACE_NAME;
2209
- const marketplaceRoot = options.marketplaceRoot ?? resolveAwHome(options.env);
2210
- const names = new Set();
2211
- const config = await readCodexConfigFile(resolveCodexPaths(options.cwd, 'user', options.env).configTomlPath);
2212
- const marketplace = config.snapshot.marketplaces[marketplaceName];
2213
- const canonicalMarketplace = marketplace?.sourceType === 'local'
2214
- && !marketplace.sourcePointsToManifest
2215
- && path.normalize(marketplace.source ?? '') === path.normalize(marketplaceRoot);
2216
- const probeFailure = (location, cause, pluginName) => {
2217
- if (cause instanceof CodexNativeResidueProbeError)
2218
- throw cause;
2219
- throw new CodexNativeResidueProbeError(location, cause, pluginName);
2220
- };
2221
- for (const pluginId of canonicalMarketplace ? Object.keys(config.snapshot.plugins) : []) {
2222
- const suffix = `@${marketplaceName}`;
2223
- const match = pluginId.endsWith(suffix)
2224
- ? /^([a-z0-9]+(?:-[a-z0-9]+)*)$/u.exec(pluginId.slice(0, -suffix.length))
2225
- : null;
2226
- if (match)
2227
- names.add(match[1]);
2228
- }
2229
- const cacheRoot = path.join(getCodexHome(options.env), 'plugins', 'cache', marketplaceName);
2230
- const cacheEntries = await readdirCodexResidueIfPresent(cacheRoot).catch((error) => probeFailure(cacheRoot, error));
2231
- for (const entry of cacheEntries) {
2232
- if (entry.isFile())
2233
- continue;
2234
- const entryPath = path.join(cacheRoot, entry.name);
2235
- const pluginName = /^[a-z0-9]+(?:-[a-z0-9]+)*$/u.test(entry.name)
2236
- ? entry.name
2237
- : probeFailure(entryPath, new Error('Codex cache residue has an invalid plugin identity.'));
2238
- if (!entry.isDirectory() || entry.isSymbolicLink()) {
2239
- probeFailure(entryPath, new Error('Codex cache residue is not a canonical plugin directory.'), pluginName);
2240
- }
2241
- let versionEntries;
2242
- try {
2243
- versionEntries = await fs.readdir(entryPath, { withFileTypes: true });
2244
- }
2245
- catch (error) {
2246
- throw new CodexNativeResidueProbeError(entryPath, error, pluginName);
2247
- }
2248
- if (versionEntries.length === 0) {
2249
- if (names.has(pluginName)) {
2250
- probeFailure(entryPath, new Error('Codex cache residue manifest is missing.'), pluginName);
2251
- }
2252
- continue;
2253
- }
2254
- let residue;
2255
- try {
2256
- residue = await inspectCodexNativeResidue({ ...options, pluginName: entry.name });
2257
- }
2258
- catch (error) {
2259
- throw new CodexNativeResidueProbeError(entryPath, error, pluginName);
2260
- }
2261
- if (residue.state !== 'present') {
2262
- probeFailure(entryPath, new Error('Codex cache residue has foreign or incomplete manifest evidence.'), pluginName);
2263
- }
2264
- names.add(pluginName);
2265
- }
2266
- const pluginsRoot = path.join(getCodexHome(options.env), 'plugins');
2267
- const pluginEntries = await readdirCodexResidueIfPresent(pluginsRoot).catch((error) => probeFailure(pluginsRoot, error));
2268
- for (const entry of pluginEntries) {
2269
- const suffix = `@${marketplaceName}`;
2270
- const match = entry.name.endsWith(suffix)
2271
- ? /^([a-z0-9]+(?:-[a-z0-9]+)*)$/u.exec(entry.name.slice(0, -suffix.length))
2272
- : null;
2273
- if (!match)
2274
- continue;
2275
- const installedRoot = path.join(pluginsRoot, entry.name);
2276
- if (!entry.isDirectory() || entry.isSymbolicLink()) {
2277
- probeFailure(installedRoot, new Error('Codex installed residue is not a canonical plugin directory.'), match[1]);
2278
- }
2279
- const installedStats = await lstatCodexResidueIfPresent(installedRoot);
2280
- if (!installedStats || !installedStats.isDirectory() || installedStats.isSymbolicLink()) {
2281
- probeFailure(installedRoot, new Error('Codex installed residue is not a canonical plugin directory.'), match[1]);
2282
- }
2283
- const manifestPath = path.join(pluginsRoot, entry.name, 'manifest.json');
2284
- const stats = await lstatCodexResidueIfPresent(manifestPath);
2285
- if (!stats?.isFile() || stats.isSymbolicLink()) {
2286
- probeFailure(manifestPath, new Error('Codex installed residue manifest is missing or nonregular.'), match[1]);
2287
- }
2288
- let rawManifest;
2289
- try {
2290
- rawManifest = await fs.readJson(manifestPath);
2291
- }
2292
- catch (error) {
2293
- throw new CodexNativeResidueProbeError(manifestPath, error, match[1]);
2294
- }
2295
- if (!rawManifest || typeof rawManifest !== 'object' || Array.isArray(rawManifest)) {
2296
- probeFailure(manifestPath, new Error('Codex installed residue manifest must be an object.'), match[1]);
2297
- }
2298
- const manifest = rawManifest;
2299
- const identity = manifest.id ?? manifest.name;
2300
- if (!canonicalMarketplace
2301
- || (identity !== match[1] && identity !== entry.name)
2302
- || typeof manifest.version !== 'string'
2303
- || manifest.version.trim() === '') {
2304
- probeFailure(manifestPath, new Error('Codex installed residue manifest has foreign identity or version.'), match[1]);
2305
- }
2306
- names.add(match[1]);
2307
- }
2308
- return [...names];
2309
- }
2310
- export class CodexNativeResidueProbeError extends Error {
2311
- code = 'codexNativeResidueProbeFailed';
2312
- location;
2313
- pluginName;
2314
- constructor(location, cause, pluginName) {
2315
- super(`codexNativeResidueProbeFailed: cannot classify Codex native residue at ${location}.`, {
2316
- cause,
2317
- });
2318
- this.name = 'CodexNativeResidueProbeError';
2319
- this.location = location;
2320
- this.pluginName = pluginName;
2321
- }
2322
- }
2323
- async function ensureCodexActiveSkillsManifest(options) {
2324
- const relativeSkillsRoot = await resolveCodexSkillsRelativeRoot(options.plugin);
2325
- const skillsRoot = path.join(options.marketplaceRoot, 'plugins', options.plugin.manifest.name, ...relativeSkillsRoot.split('/'));
2326
- if (options.hasRequiredSkills && !await fs.pathExists(skillsRoot)) {
2327
- throw new Error(`Codex required Skills root 不存在:${skillsRoot}`);
2328
- }
2329
- const manifestPath = path.join(options.marketplaceRoot, 'plugins', options.plugin.manifest.name, '.codex-plugin', 'plugin.json');
2330
- const manifest = await fs.readJson(manifestPath).catch((error) => {
2331
- if (error.code === 'ENOENT')
2332
- return {};
2333
- throw error;
2334
- });
2335
- const updated = {
2336
- ...manifest,
2337
- name: options.plugin.manifest.name,
2338
- version: options.plugin.manifest.version,
2339
- };
2340
- if (options.hasRequiredSkills)
2341
- updated.skills = `./${relativeSkillsRoot}`;
2342
- else
2343
- delete updated.skills;
2344
- await fs.outputJson(manifestPath, updated, { spaces: 2 });
2345
- return options.hasRequiredSkills ? relativeSkillsRoot : null;
2346
- }
2347
- function createCodexMarketplaceComponent(options) {
2348
- const configPath = resolveCodexPaths(options.cwd, 'user', options.env).configTomlPath;
2349
- return {
2350
- kind: 'shared-config',
2351
- identity: ownerIdentity(options.pluginName, `marketplace-${options.marketplaceName}`, 'codex', 'user'),
2352
- label: `${configPath}#marketplaces.${options.marketplaceName}`,
2353
- locator: {
2354
- kind: 'codex-marketplace',
2355
- marketplaceName: options.marketplaceName,
2356
- marketplaceRoot: options.marketplaceRoot,
2357
- desired: options.desired,
2358
- },
2359
- };
2360
- }
2361
- async function assertCodexNativeInstallState(options) {
2362
- const marketplaceName = options.marketplaceName ?? MARKETPLACE_NAME;
2363
- const pluginId = `${options.pluginName}@${marketplaceName}`;
2364
- const configPath = resolveCodexPaths(options.cwd, 'user', options.env).configTomlPath;
2365
- const config = await readCodexConfigFile(configPath);
2366
- const marketplace = config.snapshot.marketplaces[marketplaceName];
2367
- if (marketplace?.sourceType !== 'local'
2368
- || marketplace.sourcePointsToManifest
2369
- || path.normalize(marketplace.source ?? '') !== path.normalize(options.marketplaceRoot)) {
2370
- throw new Error(`Codex marketplace aw 未指向当前 AW_HOME:${options.marketplaceRoot}`);
2371
- }
2372
- if (config.snapshot.plugins[pluginId]?.enabled !== true) {
2373
- throw new Error(`Codex enabled config 缺少 ${pluginId}。`);
2374
- }
2375
- const cacheRoot = path.join(getCodexHome(options.env), 'plugins', 'cache', marketplaceName, options.pluginName, options.pluginVersion);
2376
- const candidates = [
2377
- path.join(cacheRoot, 'plugin.json'),
2378
- path.join(cacheRoot, '.codex-plugin', 'plugin.json'),
2379
- ];
2380
- const expectedRoot = path.join(options.marketplaceRoot, 'plugins', options.pluginName);
2381
- const assertExpectedPayload = async () => {
2382
- const collectFiles = async (root) => {
2383
- const output = [];
2384
- const pending = [''];
2385
- while (pending.length > 0) {
2386
- const relativeRoot = pending.pop();
2387
- for (const entry of await fs.readdir(path.join(root, relativeRoot), { withFileTypes: true })) {
2388
- const relativePath = path.join(relativeRoot, entry.name);
2389
- if (entry.isSymbolicLink()) {
2390
- throw new Error(`Codex native payload 不能包含符号链接:${path.join(root, relativePath)}`);
2391
- }
2392
- if (entry.isDirectory())
2393
- pending.push(relativePath);
2394
- else if (entry.isFile())
2395
- output.push(relativePath);
2396
- else
2397
- throw new Error(`Codex native payload 包含不支持的节点:${path.join(root, relativePath)}`);
2398
- }
2399
- }
2400
- return output;
2401
- };
2402
- const manifestPaths = new Set(['plugin.json', path.join('.codex-plugin', 'plugin.json')]);
2403
- const allowedMetadata = new Set(['metadata.json', '.metadata.json']);
2404
- const [expectedFiles, actualFiles] = await Promise.all([
2405
- collectFiles(expectedRoot),
2406
- collectFiles(cacheRoot),
2407
- ]);
2408
- const expectedSet = new Set(expectedFiles);
2409
- for (const relativePath of expectedFiles) {
2410
- if (manifestPaths.has(relativePath))
2411
- continue;
2412
- const expectedPath = path.join(expectedRoot, relativePath);
2413
- const actualPath = path.join(cacheRoot, relativePath);
2414
- const actualStats = await fs.lstat(actualPath).catch(() => null);
2415
- if (!actualStats?.isFile() || actualStats.isSymbolicLink()) {
2416
- throw new Error(`Codex native cache 缺少 expected payload:${actualPath}`);
2417
- }
2418
- const [expectedContent, actualContent] = await Promise.all([
2419
- fs.readFile(expectedPath),
2420
- fs.readFile(actualPath),
2421
- ]);
2422
- if (!expectedContent.equals(actualContent)) {
2423
- throw new Error(`Codex native cache expected payload digest 不匹配:${actualPath}`);
2424
- }
2425
- }
2426
- for (const relativePath of actualFiles) {
2427
- if (expectedSet.has(relativePath)
2428
- || manifestPaths.has(relativePath)
2429
- || allowedMetadata.has(relativePath))
2430
- continue;
2431
- throw new Error(`Codex native cache 包含 unexpected loadable payload:${path.join(cacheRoot, relativePath)}`);
2432
- }
2433
- };
2434
- let matchedManifest = false;
2435
- for (const candidate of candidates) {
2436
- if (!await fs.pathExists(candidate))
2437
- continue;
2438
- const manifest = await fs.readJson(candidate).catch(() => null);
2439
- if (!manifest) {
2440
- throw new Error(`Codex native cache manifest 无法解析:${candidate}`);
2441
- }
2442
- const identity = manifest.id ?? manifest.name;
2443
- const matches = ((identity === options.pluginName || identity === pluginId)
2444
- && manifest.version === options.pluginVersion
2445
- && (options.skillsRelativeRoot === null
2446
- ? typeof manifest.skills !== 'string'
2447
- : typeof manifest.skills === 'string'
2448
- && path.posix.normalize(manifest.skills.replace(/^\.\//u, '')) === options.skillsRelativeRoot));
2449
- if (!matches) {
2450
- throw new Error(`Codex native cache manifest 不匹配:${candidate}`);
2451
- }
2452
- matchedManifest = true;
2453
- }
2454
- if (!matchedManifest) {
2455
- throw new Error(`Codex native cache 缺少匹配 ${pluginId}@${options.pluginVersion} 的 manifest:${cacheRoot}`);
2456
- }
2457
- for (const skillKey of options.requiredSkillKeys) {
2458
- const skillPath = path.join(cacheRoot, ...options.skillsRelativeRoot.split('/'), skillKey, 'SKILL.md');
2459
- if (!await fs.pathExists(skillPath)) {
2460
- throw new Error(`Codex native cache 缺少 required Skill:${skillPath}`);
2461
- }
2462
- }
2463
- await assertExpectedPayload();
2464
- if (options.nativeRender) {
2465
- const actualPayloadDigest = await calculateCodexNativeRenderPayloadDigest(cacheRoot);
2466
- if (actualPayloadDigest !== options.nativeRender.payloadDigest) {
2467
- throw new Error(`Codex native cache render payload digest 不匹配:${cacheRoot}`);
2468
- }
2469
- const receiptEvidence = await readCodexNativeRenderReceiptEvidence(cacheRoot);
2470
- if (receiptEvidence.state !== 'present'
2471
- || receiptEvidence.receipt.payloadDigest !== options.nativeRender.payloadDigest
2472
- || !codexNativeRenderProjectionsEqual(receiptEvidence.receipt, options.nativeRender.projection)
2473
- || receiptEvidence.receipt.targets.map((target) => target.skillKey).sort().join('\0')
2474
- !== options.nativeRender.targetSkillKeys.join('\0')) {
2475
- throw new Error(`Codex native cache render receipt 不匹配:${cacheRoot}`);
2476
- }
2477
- }
2478
- }
2479
679
  async function readJsonObjectIfExists(filePath, label) {
2480
680
  let value;
2481
681
  try {
@@ -2729,425 +929,124 @@ async function readClaudeNativeState(options) {
2729
929
  paths,
2730
930
  marketplaceState,
2731
931
  marketplaceDeclaredState,
2732
- marketplaceMaterializedState,
2733
- legacyMarketplaceState,
2734
- pluginState,
2735
- pluginEnabled,
2736
- records,
2737
- marketplaceConsumers,
2738
- marketplaceConsumerEvidence,
2739
- unboundPluginNames: [...unboundPluginNames],
2740
- pluginVersions,
2741
- };
2742
- }
2743
- export async function assertNoClaudeUnboundNativeNameCollision(options) {
2744
- const state = await readClaudeNativeState({
2745
- pluginName: options.pluginName,
2746
- scope: 'user',
2747
- cwd: options.cwd,
2748
- env: options.env,
2749
- });
2750
- if (state.unboundPluginNames.includes(options.pluginName)) {
2751
- throw new Error(`externalOwnershipPresent: Claude has an unbound installed plugin named ${options.pluginName}; native install would overwrite it.`);
2752
- }
2753
- }
2754
- export async function inspectNativeMarketplaceRegistrations(options) {
2755
- const codexConfig = await readCodexConfigFile(resolveCodexPaths(options.cwd, 'user', options.env).configTomlPath);
2756
- const codexMarketplace = codexConfig.snapshot.marketplaces[options.marketplaceName];
2757
- const codexRegistration = codexMarketplace === undefined
2758
- ? 'absent'
2759
- : codexMarketplace.sourceType === 'local'
2760
- && !codexMarketplace.sourcePointsToManifest
2761
- && path.normalize(codexMarketplace.source ?? '') === path.normalize(options.marketplaceRoot)
2762
- ? 'canonical'
2763
- : 'foreign';
2764
- const codexConsumerSuffix = `@${options.marketplaceName}`;
2765
- const codex = codexRegistration === 'absent'
2766
- && Object.keys(codexConfig.snapshot.plugins).some((pluginId) => pluginId.length > codexConsumerSuffix.length && pluginId.endsWith(codexConsumerSuffix))
2767
- ? 'foreign'
2768
- : codexRegistration;
2769
- const claudeState = await readClaudeNativeState({
2770
- pluginName: '__marketplace_preflight__',
2771
- scope: 'user',
2772
- cwd: options.cwd,
2773
- env: options.env,
2774
- marketplaceName: options.marketplaceName,
2775
- marketplaceRoot: options.marketplaceRoot,
2776
- });
2777
- const claude = claudeState.marketplaceState === 'absent'
2778
- && claudeState.marketplaceConsumerEvidence
2779
- ? 'foreign'
2780
- : claudeState.marketplaceState;
2781
- return { codex, claude };
2782
- }
2783
- function createClaudeMarketplaceComponent(options) {
2784
- return {
2785
- kind: 'shared-config',
2786
- identity: ownerIdentity(options.pluginName, `marketplace-${options.marketplaceName}`, 'claude', options.scope),
2787
- label: `${resolveClaudePaths(options.cwd, options.scope, options.env).settingsPath}#marketplaces.${options.marketplaceName}`,
2788
- locator: {
2789
- kind: 'claude-marketplace',
2790
- marketplaceName: options.marketplaceName,
2791
- marketplaceRoot: options.marketplaceRoot,
2792
- desired: options.desired,
2793
- },
2794
- };
2795
- }
2796
- async function materializeCodexNativeRender(options) {
2797
- const targets = codexNativeRenderTargets(options.plugin);
2798
- if (targets.length === 0)
2799
- return null;
2800
- const byScope = new Map();
2801
- for (const target of targets) {
2802
- const requestScope = target.scopes.includes('user') ? 'user' : 'project';
2803
- const group = byScope.get(requestScope) ?? [];
2804
- group.push(target);
2805
- byScope.set(requestScope, group);
2806
- }
2807
- const renderedArtifacts = [];
2808
- let rendererDigest;
2809
- for (const scope of ['user', 'project']) {
2810
- const group = byScope.get(scope);
2811
- if (!group || group.length === 0)
2812
- continue;
2813
- const rendered = await runArtifactRenderer({
2814
- event: 'install',
2815
- plugin: options.plugin,
2816
- platform: 'codex',
2817
- scope,
2818
- configScope: 'user',
2819
- cwd: options.plugin.rootDir,
2820
- commandEnv: options.env,
2821
- previousArtifacts: [],
2822
- requestedArtifactIds: group.map((target) => target.id),
2823
- scriptTimeoutMs: options.scriptTimeoutMs,
2824
- });
2825
- rendererDigest = rendered.target.rendererDigest;
2826
- const validated = validateArtifactPlan({
2827
- manifest: options.plugin.manifest,
2828
- plan: rendered.plan,
2829
- platform: 'codex',
2830
- scope,
2831
- requestedArtifactIds: rendered.context.requestedArtifactIds,
2832
- renderConfig: rendered.context.config,
2833
- rendererDigest: rendered.target.rendererDigest,
2834
- manifestDigest: rendered.target.manifestDigest,
2835
- });
2836
- for (const artifact of validated.artifacts) {
2837
- renderedArtifacts.push({
2838
- artifact,
2839
- sourceDirs: rendered.context.artifactSources.find((entry) => entry.id === artifact.id && entry.target === artifact.target)?.sourceDirs ?? [],
2840
- });
2841
- }
2842
- }
2843
- if (!rendererDigest || renderedArtifacts.length !== targets.length) {
2844
- throw new Error('Codex native renderer did not produce the complete declared target set.');
2845
- }
2846
- const skillsRelativeRoot = await resolveCodexSkillsRelativeRoot(options.plugin);
2847
- const skillsRoot = path.join(options.stagingRoot, 'plugins', options.plugin.manifest.name, ...skillsRelativeRoot.split('/'));
2848
- const receiptTargets = [];
2849
- const presentSkillKeys = [];
2850
- for (const { artifact, sourceDirs } of renderedArtifacts) {
2851
- if (artifact.target !== 'codex.skill') {
2852
- throw new Error(`Codex native renderer returned non-Skill artifact "${artifact.id}".`);
2853
- }
2854
- const skillKey = artifact.skillKey;
2855
- const tree = await createCodexSkillExpectedTree({
2856
- artifact,
2857
- sourceDirs,
2858
- requireReadme: true,
2859
- requirementsReceipt: requirementsReceipt(options.plugin),
2860
- sourceRequirementsReceiptPolicy: 'allow',
2861
- });
2862
- const skillRoot = path.join(skillsRoot, skillKey);
2863
- if (artifact.state === 'present') {
2864
- await writeDynamicSkillTree(skillRoot, tree);
2865
- presentSkillKeys.push(skillKey);
2866
- }
2867
- else {
2868
- await fs.remove(skillRoot);
2869
- }
2870
- receiptTargets.push({
2871
- id: artifact.id,
2872
- skillKey,
2873
- configDigest: artifact.configDigest,
2874
- artifactDigest: createCodexSkillStagingTreeContentDigest(tree),
2875
- });
2876
- }
2877
- await ensureCodexActiveSkillsManifest({
2878
- plugin: options.plugin,
2879
- marketplaceRoot: options.stagingRoot,
2880
- hasRequiredSkills: options.hasStaticSkills || presentSkillKeys.length > 0,
2881
- });
2882
- const logicalPluginId = options.plugin.pluginId
2883
- ?? (options.plugin.sourceId ? `${options.plugin.sourceId}/${options.plugin.manifest.name}` : undefined);
2884
- if (!logicalPluginId || !options.plugin.sourceIdentity) {
2885
- throw new Error('Codex native render materialization requires namespaced source identity.');
2886
- }
2887
- const projection = createCodexNativeRenderProjection({
2888
- logicalPluginId,
2889
- sourceIdentity: options.plugin.sourceIdentity,
2890
- pluginVersion: options.plugin.manifest.version,
2891
- rendererDigest,
2892
- targets: receiptTargets,
2893
- });
2894
- const pluginRoot = path.join(options.stagingRoot, 'plugins', options.plugin.manifest.name);
2895
- const payloadDigest = await calculateCodexNativeRenderPayloadDigest(pluginRoot);
2896
- const receipt = createCodexNativeRenderReceipt({
2897
- logicalPluginId,
2898
- sourceIdentity: options.plugin.sourceIdentity,
2899
- pluginVersion: options.plugin.manifest.version,
2900
- rendererDigest,
2901
- targets: receiptTargets,
2902
- payloadDigest,
2903
- });
2904
- await fs.outputFile(path.join(pluginRoot, CODEX_NATIVE_RENDER_RECEIPT_PATH), serializeCodexNativeRenderReceipt(receipt), 'utf8');
2905
- return {
2906
- payloadDigest,
2907
- projection,
2908
- presentSkillKeys: presentSkillKeys.sort(),
2909
- targetSkillKeys: targets.map((target) => target.skillKey).sort(),
2910
- };
2911
- }
2912
- async function mirrorCatalog(catalog, env, target, prepareStaging) {
2913
- if (!target?.sourceId || !target.marketplaceRoot) {
2914
- const rootDir = resolveAwHome(env);
2915
- await fs.ensureDir(rootDir);
2916
- await fs.copy(catalog.rootDir, rootDir, { overwrite: true });
2917
- await prepareStaging?.(rootDir);
2918
- return { rootDir, commit: async () => undefined, rollback: async () => undefined };
2919
- }
2920
- const awHome = path.resolve(resolveAwHome(env));
2921
- const destination = target.marketplaceRoot;
2922
- const parent = path.dirname(destination);
2923
- if (!await isSafeCodexMarketplaceRoot({
2924
- awHome,
2925
- sourceId: target.sourceId,
2926
- marketplaceRoot: destination,
2927
- })) {
2928
- throw new Error(`marketplaceRootInvalid: marketplace root is unsafe: ${destination}`);
2929
- }
2930
- if (!await fs.pathExists(parent)) {
2931
- await fs.mkdir(parent);
2932
- }
2933
- if (!await isSafeCodexMarketplaceRoot({
2934
- awHome,
2935
- sourceId: target.sourceId,
2936
- marketplaceRoot: destination,
2937
- })) {
2938
- throw new Error(`marketplaceRootInvalid: marketplace root became unsafe: ${destination}`);
2939
- }
2940
- const staging = await fs.mkdtemp(path.join(parent, `.${target.sourceId}-staging-`));
2941
- try {
2942
- for (const entry of ['.agents', '.claude-plugin', 'plugins']) {
2943
- await fs.copy(path.join(catalog.rootDir, entry), path.join(staging, entry));
2944
- }
2945
- const marketplaceName = target.marketplaceName ?? getNamespacedMarketplaceName(target.sourceId);
2946
- for (const [platform, manifestPath] of [
2947
- ['codex', path.join(staging, '.agents', 'plugins', 'marketplace.json')],
2948
- ['claude', path.join(staging, '.claude-plugin', 'marketplace.json')],
2949
- ]) {
2950
- const manifest = await fs.readJson(manifestPath);
2951
- await fs.writeJson(manifestPath, {
2952
- ...projectMarketplaceManifestForPlatform(manifest, platform),
2953
- name: marketplaceName,
2954
- }, { spaces: 2 });
2955
- }
2956
- await fs.writeJson(path.join(staging, 'config.json'), {
2957
- schemaVersion: 2,
2958
- sourceId: target.sourceId,
2959
- sourceIdentity: target.sourceIdentity,
2960
- marketplaceName,
2961
- }, { spaces: 2 });
2962
- const destinationExists = await fs.lstat(destination).then(() => true, (error) => {
2963
- if (error.code === 'ENOENT' || error.code === 'ENOTDIR')
2964
- return false;
2965
- throw error;
2966
- });
2967
- if (destinationExists) {
2968
- await replayNamespacedCodexSkillOverlays({
2969
- sourceRoot: destination,
2970
- stagingRoot: staging,
2971
- sourceId: target.sourceId,
2972
- sourceIdentity: target.sourceIdentity,
2973
- marketplaceName,
2974
- });
2975
- }
2976
- await prepareStaging?.(staging);
2977
- const backup = `${destination}.backup-${crypto.randomUUID()}`;
2978
- if (destinationExists)
2979
- await fs.move(destination, backup);
2980
- try {
2981
- await fs.move(staging, destination);
2982
- }
2983
- catch (error) {
2984
- const backupStats = await fs.lstat(backup).catch((inspectionError) => {
2985
- if (inspectionError.code === 'ENOENT')
2986
- return null;
2987
- throw inspectionError;
2988
- });
2989
- const destinationStats = await fs.lstat(destination).catch((inspectionError) => {
2990
- if (inspectionError.code === 'ENOENT')
2991
- return null;
2992
- throw inspectionError;
2993
- });
2994
- if (backupStats && !destinationStats) {
2995
- await fs.move(backup, destination);
2996
- }
2997
- throw error;
2998
- }
2999
- let state = 'active';
3000
- return {
3001
- rootDir: destination,
3002
- commit: async () => {
3003
- if (state !== 'active')
3004
- return;
3005
- try {
3006
- await commitMarketplaceGenerationBackup({
3007
- backupRoot: backup,
3008
- sourceId: target.sourceId,
3009
- sourceIdentity: target.sourceIdentity,
3010
- ...(target.marketplaceName ? { marketplaceName: target.marketplaceName } : {}),
3011
- env,
3012
- });
3013
- state = 'committed';
3014
- }
3015
- catch (error) {
3016
- if (error.durableMaterializeCommit === true) {
3017
- state = 'committed';
3018
- }
3019
- throw error;
3020
- }
3021
- },
3022
- rollback: async () => {
3023
- if (state !== 'active')
3024
- return;
3025
- const backupStats = await fs.lstat(backup).catch((error) => {
3026
- if (error.code === 'ENOENT')
3027
- return null;
3028
- throw error;
3029
- });
3030
- await fs.remove(destination);
3031
- if (backupStats)
3032
- await fs.move(backup, destination);
3033
- state = 'rolledBack';
3034
- },
3035
- };
3036
- }
3037
- catch (error) {
3038
- await fs.remove(staging);
3039
- throw error;
932
+ marketplaceMaterializedState,
933
+ legacyMarketplaceState,
934
+ pluginState,
935
+ pluginEnabled,
936
+ records,
937
+ marketplaceConsumers,
938
+ marketplaceConsumerEvidence,
939
+ unboundPluginNames: [...unboundPluginNames],
940
+ pluginVersions,
941
+ };
942
+ }
943
+ export async function assertNoClaudeUnboundNativeNameCollision(options) {
944
+ const state = await readClaudeNativeState({
945
+ pluginName: options.pluginName,
946
+ scope: 'user',
947
+ cwd: options.cwd,
948
+ env: options.env,
949
+ });
950
+ if (state.unboundPluginNames.includes(options.pluginName)) {
951
+ throw new Error(`externalOwnershipPresent: Claude has an unbound installed plugin named ${options.pluginName}; native install would overwrite it.`);
3040
952
  }
3041
953
  }
3042
- async function applyPlannedOperationAction(action) {
3043
- switch (action.kind) {
3044
- case 'writeOwnedArtifact': {
3045
- if (!action.owner || !action.allowedRoot || !action.artifactKind) {
3046
- throw new Error('Owned artifact action is missing owner or locator data.');
3047
- }
3048
- if (action.artifactKind === 'owned-text-file') {
3049
- if (action.content === undefined)
3050
- throw new Error('Owned text action is missing content.');
3051
- const digest = `sha256:${crypto.createHash('sha256').update(action.content).digest('hex')}`;
3052
- if (digest !== action.contentDigest)
3053
- throw new Error('Owned text action content digest mismatch.');
3054
- if (action.sourceContent === undefined)
3055
- throw new Error('Owned text action is missing source content.');
3056
- await fs.ensureDir(action.allowedRoot);
3057
- await writeOwnedTextFile({
3058
- path: action.path,
3059
- allowedRoot: action.allowedRoot,
3060
- identity: action.owner,
3061
- content: action.sourceContent,
3062
- });
3063
- return;
3064
- }
3065
- const files = action.files ?? [];
3066
- const digest = `sha256:${crypto.createHash('sha256').update(JSON.stringify(files)).digest('hex')}`;
3067
- if (digest !== action.contentDigest)
3068
- throw new Error('Skill action content digest mismatch.');
3069
- const treeFiles = {};
3070
- for (const file of files) {
3071
- const content = Buffer.from(file.contentBase64, 'base64');
3072
- if (`sha256:${crypto.createHash('sha256').update(content).digest('hex')}` !== file.contentDigest) {
3073
- throw new Error(`Skill action file digest mismatch: ${file.relativePath}`);
3074
- }
3075
- if (file.relativePath !== '.agent-workflows-owner.json')
3076
- treeFiles[file.relativePath] = content;
3077
- }
3078
- await writeDynamicSkillTree(action.path, {
3079
- skillKey: action.owner.artifactKey,
3080
- files: treeFiles,
3081
- }, {
3082
- allowedRoot: action.allowedRoot,
3083
- identity: action.owner,
3084
- allowPristineUnowned: action.allowPristineUnowned === true,
3085
- });
3086
- return;
3087
- }
3088
- case 'updateSharedConfig': {
3089
- const mutation = action.mutation;
3090
- switch (mutation.kind) {
3091
- case 'claude-permissions': {
3092
- if (action.locator.kind !== 'claude-permissions')
3093
- throw new Error('Claude permission locator mismatch.');
3094
- const settings = await readClaudeSettings(action.locator.settingsPath);
3095
- rebuildClaudePermissions(settings, action.locator.ownerKey, mutation.permissions);
3096
- await writeClaudeSettings(action.locator.settingsPath, settings);
3097
- return;
3098
- }
3099
- case 'codex-instruction': {
3100
- if (action.locator.kind !== 'codex-instruction')
3101
- throw new Error('Codex instruction locator mismatch.');
3102
- await applyCodexConfigTransactionFile(action.locator.configPath, mutation.block
3103
- ? { upsertManagedInstructionBlock: mutation.block }
3104
- : { removeManagedInstructionPluginName: action.locator.ownerKey });
3105
- return;
3106
- }
3107
- case 'codex-role-registration': {
3108
- if (action.locator.kind !== 'codex-role-registration')
3109
- throw new Error('Codex role locator mismatch.');
3110
- const locator = action.locator;
3111
- const registration = {
3112
- description: mutation.description,
3113
- configFile: mutation.configFile.split(path.sep).join('/'),
3114
- };
3115
- await applyConditionalCodexConfigTransactionFile(locator.configPath, async (snapshot) => {
3116
- const actual = snapshot.agents[mutation.roleKey];
3117
- if (actual && !await codexRoleRegistrationOwned({
3118
- fileIdentity: locator.fileIdentity,
3119
- configPath: locator.configPath,
3120
- roleKey: mutation.roleKey,
3121
- rolePath: locator.rolePath,
3122
- registration: actual,
3123
- })) {
3124
- throw new Error(`Codex agent registration "${mutation.roleKey}" is foreign and cannot be overwritten by plugin "${action.owner.plugin}".`);
3125
- }
3126
- return { upsertAgentRegistrations: { [mutation.roleKey]: registration } };
3127
- });
3128
- return;
3129
- }
3130
- case 'codex-activation':
3131
- throw new Error('Codex activation must use a setActivation action.');
3132
- }
954
+ export async function inspectNativeMarketplaceRegistration(options) {
955
+ const { sourceId, platform, marketplaceName, cwd, env } = options;
956
+ const [awRoot, configRoot] = await Promise.all([
957
+ canonicalNamespaceRoot(resolveAwHome(env)),
958
+ canonicalNamespaceRoot(resolvePlatformConfigRoot(platform, { cwd, env })),
959
+ ]);
960
+ const marketplaceRoot = path.join(getManagedPlatformRoot(awRoot, platform, `${platform}-marketplace`, configRoot), 'marketplaces', sourceId);
961
+ if (platform === 'codex') {
962
+ const config = await readCodexConfigFile(resolveCodexPaths(cwd, 'user', env).configTomlPath);
963
+ const marketplace = config.snapshot.marketplaces[marketplaceName];
964
+ if (marketplace !== undefined) {
965
+ return isCanonicalCodexMarketplaceRegistration(marketplace, marketplaceRoot) ? 'canonical' : 'foreign';
3133
966
  }
3134
- case 'setActivation':
3135
- if (!action.configPath || !action.registration) {
3136
- throw new Error('Activation action is missing configPath or registration.');
3137
- }
3138
- await applyCodexConfigTransactionFile(action.configPath, {
3139
- upsertPluginActivations: { [action.registration]: action.enabled },
3140
- });
3141
- return;
3142
- default:
3143
- throw new Error(`Operation action ${action.kind} is not an artifact write action.`);
967
+ const suffix = `@${marketplaceName}`;
968
+ // A repair can restore activation before the marketplace has been registered again.
969
+ const ownedActivations = new Set(options.codexPackages
970
+ .filter(({ packageId, sourceLocation }) => sourceLocation === path.join(marketplaceRoot, 'plugins', packageId))
971
+ .map(({ registrationId }) => registrationId));
972
+ return Object.keys(config.snapshot.plugins).some((id) => id.length > suffix.length && id.endsWith(suffix) && !ownedActivations.has(id))
973
+ ? 'foreign' : 'absent';
974
+ }
975
+ const claudeState = await readClaudeNativeState({
976
+ pluginName: '__marketplace_preflight__',
977
+ scope: 'user',
978
+ cwd,
979
+ env,
980
+ marketplaceName,
981
+ marketplaceRoot,
982
+ });
983
+ return claudeState.marketplaceState === 'absent'
984
+ && claudeState.marketplaceConsumerEvidence
985
+ ? 'foreign'
986
+ : claudeState.marketplaceState;
987
+ }
988
+ export async function applyPlannedOperationAction(action) {
989
+ if (action.kind !== 'updateSharedConfig' || action.mutation.kind !== 'claude-permissions'
990
+ || action.locator.kind !== 'claude-permissions') {
991
+ throw new Error('platformContributionUnsupported: expected a Claude permissions contribution.');
3144
992
  }
993
+ const settings = await readClaudeSettings(action.locator.settingsPath);
994
+ const before = settings.permissions;
995
+ rebuildClaudePermissions(settings, action.locator.ownerKey, action.mutation.permissions);
996
+ await writeClaudeSettings(action.locator.settingsPath, settings);
997
+ return {
998
+ allow: (settings.permissions?.allow ?? []).filter((rule) => !before?.allow?.includes(rule)),
999
+ ask: (settings.permissions?.ask ?? []).filter((rule) => !before?.ask?.includes(rule)),
1000
+ deny: (settings.permissions?.deny ?? []).filter((rule) => !before?.deny?.includes(rule)),
1001
+ };
3145
1002
  }
3146
1003
  function createNativeDriver(options) {
3147
1004
  const runner = options.context.nativeCliRunner;
3148
1005
  const currentCodexManager = () => options.context.codexManager;
3149
- const mirrorTransactions = new WeakMap();
3150
- const completedNativeSkillWrites = new WeakSet();
1006
+ const codexNativeStates = new Map();
1007
+ const pendingCodexNativeStates = new Map();
1008
+ const codexMutationMarketplaces = new Set();
1009
+ let nativeMarketplaceEligibleTargets;
1010
+ let deferredPostchecks = [];
1011
+ const nativeGroupKey = (target) => [
1012
+ target.identity.sourceId ?? '',
1013
+ target.identity.sourceIdentity ?? '',
1014
+ target.identity.marketplaceName ?? MARKETPLACE_NAME,
1015
+ path.normalize(path.resolve(target.identity.marketplaceRoot ?? resolveAwHome(options.context.env))),
1016
+ ].join('\0');
1017
+ const observationScopeFor = (target) => codexObservationScope(options.context, target.identity.marketplaceName ?? MARKETPLACE_NAME, target.identity.scope);
1018
+ const checkCodexInstalledVersion = async (target, pluginId, marketplaceName) => {
1019
+ const scope = codexObservationScope(options.context, marketplaceName, target.identity.scope);
1020
+ const registration = await inspectCodexPluginRegistrationLive({
1021
+ pluginId,
1022
+ cwd: target.identity.cwd,
1023
+ env: options.context.env,
1024
+ nativeCliRunner: runner,
1025
+ manager: currentCodexManager(),
1026
+ scope,
1027
+ observationSession: options.context.codexObservationSession,
1028
+ });
1029
+ const state = requireObservedCodexNativeState(registration.observation, 'native install postcheck');
1030
+ requireCodexObservationCoverage(registration, scope, 'native install postcheck');
1031
+ if (state !== 'installed') {
1032
+ throw new Error(`codexNativeInstallIncomplete: ${pluginId} is absent from Codex plugin list.`);
1033
+ }
1034
+ const installedEntry = registration.observation.state === 'observed'
1035
+ && registration.observation.value === 'installed'
1036
+ ? registration.observation.entry
1037
+ : undefined;
1038
+ if (installedEntry?.version !== undefined
1039
+ && installedEntry.version !== target.identity.pluginVersion) {
1040
+ throw new Error(`codexNativeInstallIncomplete: ${pluginId} version is ${installedEntry.version}; expected ${target.identity.pluginVersion}.`);
1041
+ }
1042
+ };
1043
+ const groupTargets = (target) => {
1044
+ if (!options.operationScopedNativePhases)
1045
+ return [target];
1046
+ const key = nativeGroupKey(target);
1047
+ return options.targets.filter((candidate) => nativeGroupKey(candidate) === key
1048
+ && (nativeMarketplaceEligibleTargets?.has(candidate) ?? true));
1049
+ };
3151
1050
  const findTarget = (component) => {
3152
1051
  const target = options.targets.find((entry) => entry.identity.pluginName === component.identity.plugin
3153
1052
  && entry.identity.sourceId === component.identity.sourceId
@@ -3157,43 +1056,81 @@ function createNativeDriver(options) {
3157
1056
  throw new Error('Ownership executor cannot resolve its desired target.');
3158
1057
  return target;
3159
1058
  };
1059
+ const codexStateKey = (target, pluginId) => `${nativeGroupKey(target)}\0${pluginId}`;
1060
+ const runObservedCodexMutation = async (options_) => runCodexMutationWithInvalidation({
1061
+ ...options_,
1062
+ ...(options.operationScopedNativePhases
1063
+ ? {}
1064
+ : { observationSession: options.context.codexObservationSession }),
1065
+ });
1066
+ const prepareNativeGroup = async (target) => {
1067
+ for (const member of groupTargets(target)) {
1068
+ if (member.identity.platform !== 'codex')
1069
+ continue;
1070
+ for (const component of member.ownedComponents) {
1071
+ if (component.kind !== 'native-plugin')
1072
+ continue;
1073
+ const registration = await inspectCodexPluginRegistrationLive({ pluginId: component.pluginId,
1074
+ cwd: member.identity.cwd, env: options.context.env, nativeCliRunner: runner,
1075
+ manager: currentCodexManager(), scope: observationScopeFor(member),
1076
+ observationSession: options.context.codexObservationSession });
1077
+ codexNativeStates.set(codexStateKey(member, component.pluginId), requireObservedCodexNativeState(registration.observation, 'native cleanup precheck') === 'installed' ? 'present' : 'absent');
1078
+ }
1079
+ }
1080
+ };
3160
1081
  const inspectCodexMarketplace = async (component) => {
3161
1082
  if (component.locator.kind !== 'codex-marketplace')
3162
1083
  throw new Error('Invalid Codex marketplace locator.');
3163
1084
  const target = findTarget(component);
3164
1085
  const configPath = resolveCodexPaths(target.identity.cwd, 'user', options.context.env).configTomlPath;
3165
- const marketplace = (await readCodexConfigFile(configPath))
3166
- .snapshot.marketplaces[component.locator.marketplaceName];
1086
+ const config = await readCodexConfigFile(configPath);
1087
+ const marketplace = config.snapshot.marketplaces[component.locator.marketplaceName];
3167
1088
  const canonical = !marketplace
3168
1089
  ? 'absent'
3169
- : marketplace.sourceType === 'local'
3170
- && !marketplace.sourcePointsToManifest
3171
- && path.normalize(marketplace.source ?? '') === path.normalize(component.locator.marketplaceRoot)
1090
+ : isCanonicalCodexMarketplaceRegistration(marketplace, component.locator.marketplaceRoot)
3172
1091
  ? 'present'
3173
1092
  : 'foreign';
3174
- const plugin = await inspectCodexNativeResidue({
3175
- pluginName: component.identity.plugin,
3176
- cwd: target.identity.cwd,
3177
- env: options.context.env,
3178
- marketplaceName: component.locator.marketplaceName,
3179
- marketplaceRoot: component.locator.marketplaceRoot,
3180
- });
3181
- if (plugin.state === 'foreign')
3182
- return 'foreign';
3183
1093
  if (component.locator.desired) {
3184
- return canonical === 'present' && plugin.state === 'present' && plugin.enabled
3185
- ? 'present'
3186
- : 'absent';
1094
+ if (canonical !== 'present')
1095
+ return canonical;
1096
+ const pluginId = `${component.identity.plugin}@${component.locator.marketplaceName}`;
1097
+ return config.snapshot.plugins[pluginId]?.enabled === true ? 'present' : 'absent';
3187
1098
  }
3188
1099
  if (canonical !== 'present')
3189
1100
  return canonical;
3190
- const remaining = await discoverCodexNativeResidueNames({
3191
- cwd: target.identity.cwd,
3192
- env: options.context.env,
3193
- marketplaceName: component.locator.marketplaceName,
3194
- marketplaceRoot: component.locator.marketplaceRoot,
3195
- });
3196
- return remaining.some((name) => name !== component.identity.plugin) ? 'absent' : 'present';
1101
+ const suffix = `@${component.locator.marketplaceName}`;
1102
+ const currentPluginId = `${component.identity.plugin}${suffix}`;
1103
+ const projectConfig = await readCodexConfigFile(resolveCodexPaths(target.identity.cwd, 'project', options.context.env).configTomlPath);
1104
+ const configuredPluginIds = [
1105
+ ...Object.keys(config.snapshot.plugins),
1106
+ ...Object.keys(projectConfig.snapshot.plugins),
1107
+ ].filter((pluginId) => pluginId.endsWith(suffix));
1108
+ if (configuredPluginIds.some((pluginId) => pluginId !== currentPluginId))
1109
+ return 'absent';
1110
+ const ownedSession = options.context.codexObservationSession ? undefined
1111
+ : await createCodexNativeObservationSession({
1112
+ exactCwd: target.identity.cwd,
1113
+ env: options.context.env,
1114
+ runner,
1115
+ manager: currentCodexManager() ?? SYSTEM_CODEX_MANAGER,
1116
+ });
1117
+ const session = options.context.codexObservationSession ?? ownedSession;
1118
+ try {
1119
+ const observationScope = observationScopeFor(target);
1120
+ const snapshot = await session.snapshot(component.locator.marketplaceName, observationScope);
1121
+ if (!snapshot.neutral || (observationScope === 'both' && !snapshot.exact))
1122
+ return 'absent';
1123
+ const officialPluginIds = new Set([
1124
+ ...(snapshot.exact?.keys() ?? []),
1125
+ ...snapshot.neutral.keys(),
1126
+ ]);
1127
+ return [...officialPluginIds].some((pluginId) => pluginId !== currentPluginId)
1128
+ ? 'absent'
1129
+ : 'present';
1130
+ }
1131
+ finally {
1132
+ await ownedSession?.dispose();
1133
+ }
3197
1134
  };
3198
1135
  const inspectClaudeMarketplace = async (component) => {
3199
1136
  if (component.locator.kind !== 'claude-marketplace')
@@ -3231,6 +1168,10 @@ function createNativeDriver(options) {
3231
1168
  inspectShared: async (component) => {
3232
1169
  const locator = component.locator;
3233
1170
  switch (locator.kind) {
1171
+ case 'host-permissions':
1172
+ return await hasPermissionOwner(options.context.env, locator) ? 'present' : 'absent';
1173
+ case 'cursor-workspace-hook':
1174
+ return platformOwnership.inspectShared(component);
3234
1175
  case 'claude-permissions':
3235
1176
  return (await readClaudeSettings(locator.settingsPath)).awGenerated
3236
1177
  ?.permissions?.[locator.ownerKey] ? 'present' : 'absent';
@@ -3281,6 +1222,13 @@ function createNativeDriver(options) {
3281
1222
  removeShared: async (component) => {
3282
1223
  const locator = component.locator;
3283
1224
  switch (locator.kind) {
1225
+ case 'host-permissions':
1226
+ if (options.context.removeHostPermissions)
1227
+ return options.context.removeHostPermissions(locator);
1228
+ await updateHostPermissions({ env: options.context.env, cwd: locator.projectRoot ?? locator.hostConfigRoot, invocationCwd: options.context.invocationCwd }, locator);
1229
+ return;
1230
+ case 'cursor-workspace-hook':
1231
+ return platformOwnership.removeShared(component);
3284
1232
  case 'claude-permissions': {
3285
1233
  const settings = await readClaudeSettings(locator.settingsPath);
3286
1234
  rebuildClaudePermissions(settings, locator.ownerKey);
@@ -3335,6 +1283,20 @@ function createNativeDriver(options) {
3335
1283
  }) ? { removeAgentRegistrations: [locator.roleKey] } : null);
3336
1284
  return;
3337
1285
  case 'codex-marketplace': {
1286
+ if (locator.desired)
1287
+ return;
1288
+ const target = findTarget(component);
1289
+ await prepareNativeGroup(target);
1290
+ const pluginId = `${target.identity.pluginName}@${locator.marketplaceName}`;
1291
+ const stateKey = codexStateKey(target, pluginId);
1292
+ if (codexNativeStates.get(stateKey) === 'present'
1293
+ && pendingCodexNativeStates.get(stateKey) !== 'absent')
1294
+ return;
1295
+ codexMutationMarketplaces.add(locator.marketplaceName);
1296
+ await runObservedCodexMutation({
1297
+ marketplaceName: locator.marketplaceName,
1298
+ mutation: () => runNativeCliChecked(buildCodexMarketplaceRemoveInvocation(currentCodexManager() ?? SYSTEM_CODEX_MANAGER, locator.marketplaceName, target.identity.cwd, options.context.env), runner),
1299
+ });
3338
1300
  return;
3339
1301
  }
3340
1302
  case 'claude-marketplace': {
@@ -3348,7 +1310,7 @@ function createNativeDriver(options) {
3348
1310
  await validateSkillDirectory(component.path, component.identity.platform === 'codex' ? 'codex.skill' : 'claude.skill', { ownerMarkerPolicy: 'ignore', requirementsReceiptPolicy: 'allow' });
3349
1311
  },
3350
1312
  skillDirectoryMatchesPristineTree: async ({ actualPath, files }) => {
3351
- const expectedFiles = new Map(files.map((entry) => [entry.relativePath, entry.contentDigest]));
1313
+ const expectedFiles = new Map(files.map((entry) => [entry.relativePath, entry]));
3352
1314
  const expectedDirectories = new Set(['']);
3353
1315
  for (const relativePath of expectedFiles.keys()) {
3354
1316
  const segments = relativePath.split('/');
@@ -3374,7 +1336,11 @@ function createNativeDriver(options) {
3374
1336
  if (!entry.isFile())
3375
1337
  return false;
3376
1338
  const digest = `sha256:${crypto.createHash('sha256').update(await fs.readFile(absolutePath)).digest('hex')}`;
3377
- if (expectedFiles.get(relativePath) !== digest)
1339
+ const expected = expectedFiles.get(relativePath);
1340
+ if (expected?.contentDigest !== digest)
1341
+ return false;
1342
+ if (catalogFileMode(relativePath, (await fs.stat(absolutePath)).mode)
1343
+ !== catalogFileMode(relativePath, expected.mode ?? 0o644))
3378
1344
  return false;
3379
1345
  seen.add(relativePath);
3380
1346
  }
@@ -3384,22 +1350,86 @@ function createNativeDriver(options) {
3384
1350
  };
3385
1351
  return {
3386
1352
  ownership,
3387
- preflightMutation: async (target) => {
3388
- if (!target.codexNativeObservationExpectation)
3389
- return;
3390
- const pluginId = `${target.identity.pluginName}@${target.identity.marketplaceName ?? MARKETPLACE_NAME}`;
3391
- const registration = await inspectCodexPluginRegistrationLive({
3392
- pluginId,
3393
- cwd: target.identity.cwd,
3394
- env: options.context.env,
3395
- nativeCliRunner: runner,
3396
- manager: currentCodexManager(),
3397
- });
3398
- const observed = requireObservedCodexNativeState(registration.observation, 'artifact write precheck');
3399
- if (target.codexNativeObservationExpectation === 'installed' && observed !== 'installed') {
3400
- throw new Error('codexNativeExpectedInstalled: artifact write precheck requires the reused Codex plugin to remain installed.');
1353
+ setNativeMarketplaceEligibleTargets: (eligibleTargets) => {
1354
+ nativeMarketplaceEligibleTargets = new Set(eligibleTargets);
1355
+ },
1356
+ prepareNativeMarketplaceGroup: prepareNativeGroup,
1357
+ postcheckCodexProvider: async (target) => {
1358
+ const pending = deferredPostchecks.filter((entry) => entry.target === target);
1359
+ deferredPostchecks = deferredPostchecks.filter((entry) => entry.target !== target);
1360
+ // A dependent can write as soon as this returns. Read a new Codex list
1361
+ // after the provider's writes, even when its install receipt was healthy.
1362
+ const marketplaces = new Set((target.platformDeployment?.plans ?? []).flatMap((plan) => plan.evidence.layout.marketplaceName && plan.projections.some((projection) => projection.bundle)
1363
+ ? [plan.evidence.layout.marketplaceName] : []));
1364
+ if (target.requiresNative)
1365
+ marketplaces.add(target.identity.marketplaceName ?? MARKETPLACE_NAME);
1366
+ for (const name of marketplaces)
1367
+ options.context.codexObservationSession?.invalidate(name);
1368
+ if (!pending.length)
1369
+ await target.platformDeployment?.verify();
1370
+ for (const entry of pending)
1371
+ await entry.postcheck();
1372
+ for (const name of marketplaces) {
1373
+ await checkCodexInstalledVersion(target, `${target.identity.pluginName}@${name}`, name);
3401
1374
  }
3402
1375
  },
1376
+ ...(options.operationScopedNativePhases ? {
1377
+ deferPostcheck: (target, postcheck) => {
1378
+ if (target.identity.platform !== 'codex')
1379
+ return false;
1380
+ deferredPostchecks.push({
1381
+ target,
1382
+ postcheck: async () => {
1383
+ if (target.requiresNative) {
1384
+ const marketplaceName = target.identity.marketplaceName ?? MARKETPLACE_NAME;
1385
+ await checkCodexInstalledVersion(target, `${target.identity.pluginName}@${marketplaceName}`, marketplaceName);
1386
+ }
1387
+ await postcheck();
1388
+ },
1389
+ });
1390
+ return true;
1391
+ },
1392
+ completePostchecks: async () => {
1393
+ const pending = deferredPostchecks;
1394
+ deferredPostchecks = [];
1395
+ const failures = [];
1396
+ for (const entry of pending) {
1397
+ try {
1398
+ await entry.postcheck();
1399
+ }
1400
+ catch (error) {
1401
+ failures.push({ target: entry.target, error });
1402
+ }
1403
+ }
1404
+ return failures;
1405
+ },
1406
+ } : {}),
1407
+ captureCodexPostMutationSnapshots: async (marketplaceNames = []) => {
1408
+ for (const marketplaceName of marketplaceNames) {
1409
+ codexMutationMarketplaces.add(marketplaceName);
1410
+ }
1411
+ const changedMarketplaces = [...codexMutationMarketplaces];
1412
+ const marketplaces = [...new Set([...changedMarketplaces, ...deferredPostchecks.flatMap(({ target }) => (target.platformDeployment?.plans ?? []).flatMap((plan) => plan.evidence.layout.marketplaceName
1413
+ && plan.projections.some((projection) => projection.bundle) ? [plan.evidence.layout.marketplaceName] : []))])].sort();
1414
+ if (marketplaces.length === 0)
1415
+ return;
1416
+ const session = options.context.codexObservationSession;
1417
+ if (!session) {
1418
+ throw new Error('codexNativeObservationUnavailable: grouped postcheck requires an observation session.');
1419
+ }
1420
+ codexMutationMarketplaces.clear();
1421
+ // Platform mutations already invalidated and observed their generation.
1422
+ // Only legacy writes still deferred in the dirty set need invalidation.
1423
+ for (const marketplaceName of changedMarketplaces)
1424
+ session.invalidate(marketplaceName);
1425
+ await Promise.all(marketplaces.map((marketplaceName) => session.snapshot(marketplaceName, options.context.codexObservationScopeByMarketplace?.get(marketplaceName)
1426
+ ?? (options.targets.some((target) => (nativeMarketplaceEligibleTargets?.has(target) ?? true)
1427
+ && target.identity.platform === 'codex'
1428
+ && (target.platformDeployment ? target.platformDeployment.plans.some((plan) => plan.evidence.layout.marketplaceName === marketplaceName && plan.binding.scope.kind === 'project')
1429
+ : (target.identity.marketplaceName ?? MARKETPLACE_NAME) === marketplaceName && target.identity.scope === 'project'))
1430
+ ? 'both'
1431
+ : 'user'))));
1432
+ },
3403
1433
  preflightMaterialize: async (target) => {
3404
1434
  if (target.identity.sourceId && options.context.expectedCatalogConfigDigest) {
3405
1435
  const current = await readCatalogSourcesConfigSnapshot(options.context.env);
@@ -3407,42 +1437,20 @@ function createNativeDriver(options) {
3407
1437
  throw new Error('catalogSourceConflict: catalog source config changed before owner publication.');
3408
1438
  }
3409
1439
  }
3410
- if (target.identity.platform === 'claude') {
1440
+ if (target.identity.platform === 'claude')
3411
1441
  await assertNoClaudeUnboundNativeNameCollision({
3412
- pluginName: target.identity.pluginName,
3413
- cwd: target.identity.cwd,
3414
- env: options.context.env,
1442
+ pluginName: target.identity.pluginName, cwd: target.identity.cwd, env: options.context.env
3415
1443
  });
3416
- return;
3417
- }
3418
- const marketplaceName = target.identity.marketplaceName ?? MARKETPLACE_NAME;
3419
- const marketplaceRoot = target.identity.marketplaceRoot ?? resolveAwHome(options.context.env);
3420
- const config = await readCodexConfigFile(resolveCodexPaths(target.identity.cwd, 'user', options.context.env).configTomlPath);
3421
- const marketplace = config.snapshot.marketplaces[marketplaceName];
3422
- const canonical = marketplace?.sourceType === 'local'
3423
- && !marketplace.sourcePointsToManifest
3424
- && path.resolve(marketplace.source ?? '') === path.resolve(marketplaceRoot);
3425
- const expectedPluginId = `${target.identity.pluginName}@${marketplaceName}`;
3426
- if ((marketplace !== undefined && !canonical)
3427
- || (Object.hasOwn(config.snapshot.plugins, expectedPluginId) && !canonical)) {
3428
- throw new Error(`externalOwnershipPresent: Codex marketplace ${marketplaceName} has a foreign source instead of its canonical namespaced root.`);
3429
- }
3430
- },
3431
- commitMaterialize: async (target) => {
3432
- const transaction = mirrorTransactions.get(target);
3433
- if (!transaction)
3434
- return;
3435
- await transaction.commit();
3436
- mirrorTransactions.delete(target);
3437
1444
  },
3438
- rollbackMaterialize: async (target) => {
3439
- const transaction = mirrorTransactions.get(target);
3440
- if (!transaction)
3441
- return;
3442
- await transaction.rollback();
3443
- mirrorTransactions.delete(target);
1445
+ finalizeCleanup: async (target) => {
1446
+ for (const action of target.operationActions ?? []) {
1447
+ if (action.kind !== 'retainManagedCommandPackage')
1448
+ continue;
1449
+ options.context.commandResults ??= [];
1450
+ if (!options.context.commandResults.some((result) => result.packageId === action.packageId))
1451
+ options.context.commandResults.push(retainedManagedCommandPackageResult(action));
1452
+ }
3444
1453
  },
3445
- finalizeCleanup: async () => undefined,
3446
1454
  probeNative: async (component) => {
3447
1455
  const target = findTarget(component);
3448
1456
  if (!target)
@@ -3459,12 +1467,17 @@ function createNativeDriver(options) {
3459
1467
  return state.pluginState;
3460
1468
  }
3461
1469
  if (target.identity.platform === 'codex') {
1470
+ const pending = pendingCodexNativeStates.get(codexStateKey(target, component.pluginId));
1471
+ if (pending)
1472
+ return pending;
3462
1473
  const registration = await inspectCodexPluginRegistrationLive({
3463
1474
  pluginId: component.pluginId,
3464
1475
  cwd: target.identity.cwd,
3465
1476
  env: options.context.env,
3466
1477
  nativeCliRunner: runner,
3467
1478
  manager: currentCodexManager(),
1479
+ scope: observationScopeFor(target),
1480
+ observationSession: options.context.codexObservationSession,
3468
1481
  });
3469
1482
  return observedCodexNativeState(registration.observation, 'native probe');
3470
1483
  }
@@ -3485,7 +1498,9 @@ function createNativeDriver(options) {
3485
1498
  });
3486
1499
  if (state.pluginState !== 'present')
3487
1500
  return state.pluginState;
3488
- return state.pluginEnabled ? 'present' : 'partial';
1501
+ if (!state.pluginEnabled)
1502
+ return 'partial';
1503
+ return 'present';
3489
1504
  }
3490
1505
  if (target.identity.platform === 'codex') {
3491
1506
  const pluginId = `${target.identity.pluginName}@${target.identity.marketplaceName ?? MARKETPLACE_NAME}`;
@@ -3495,41 +1510,20 @@ function createNativeDriver(options) {
3495
1510
  env: options.context.env,
3496
1511
  nativeCliRunner: runner,
3497
1512
  manager: currentCodexManager(),
1513
+ scope: observationScopeFor(target),
1514
+ observationSession: options.context.codexObservationSession,
3498
1515
  });
3499
1516
  const nativeState = observedCodexNativeState(registration.observation, 'native health postcheck');
3500
1517
  if (nativeState !== 'present')
3501
1518
  return nativeState;
3502
- if (!registration.observations[target.identity.scope]
3503
- || registration.activations[target.identity.scope] !== true)
3504
- return 'partial';
3505
- const codexManager = currentCodexManager();
3506
- if (target.identity.scope !== 'project' || !codexManager)
3507
- return 'present';
3508
1519
  const userConfig = await readCodexConfigFile(resolveCodexPaths(target.identity.cwd, 'user', options.context.env).configTomlPath);
1520
+ const userEnabled = userConfig.snapshot.plugins[pluginId]?.enabled === true;
1521
+ if (target.identity.scope === 'user')
1522
+ return userEnabled ? 'present' : 'partial';
3509
1523
  const projectConfig = await readCodexConfigFile(resolveCodexPaths(target.identity.cwd, 'project', options.context.env).configTomlPath);
3510
- const neutralCwd = await fs.mkdtemp(path.join(os.tmpdir(), 'aw-codex-postcheck-'));
3511
- try {
3512
- const invocations = buildCodexProjectPluginListInvocations(codexManager, {
3513
- exactCwd: target.identity.cwd,
3514
- neutralCwd,
3515
- env: options.context.env,
3516
- });
3517
- const [exactResult, neutralResult] = await Promise.all([
3518
- runNativeCliChecked(invocations.exact, runner),
3519
- runNativeCliChecked(invocations.neutral, runner),
3520
- ]);
3521
- postcheckCodexProjectPluginActivation({
3522
- pluginId,
3523
- userEnabled: userConfig.snapshot.plugins[pluginId]?.enabled === true,
3524
- projectEnabled: projectConfig.snapshot.plugins[pluginId]?.enabled === true,
3525
- exactResult,
3526
- neutralResult,
3527
- });
3528
- }
3529
- finally {
3530
- await fs.remove(neutralCwd);
3531
- }
3532
- return 'present';
1524
+ return userEnabled || projectConfig.snapshot.plugins[pluginId]?.enabled === true
1525
+ ? 'present'
1526
+ : 'partial';
3533
1527
  }
3534
1528
  return 'absent';
3535
1529
  },
@@ -3537,33 +1531,59 @@ function createNativeDriver(options) {
3537
1531
  const target = findTarget(component);
3538
1532
  if (!target)
3539
1533
  throw new Error('Native ownership removal cannot resolve its desired target.');
1534
+ await prepareNativeGroup(target);
3540
1535
  if (target.identity.platform === 'codex') {
3541
- const registration = await inspectCodexPluginRegistrationLive({
3542
- pluginId: component.pluginId,
3543
- cwd: target.identity.cwd,
3544
- env: options.context.env,
3545
- nativeCliRunner: runner,
3546
- manager: currentCodexManager(),
3547
- });
3548
- const before = observedCodexNativeState(registration.observation, 'native remove precheck');
1536
+ const marketplaceName = component.pluginId.slice(component.pluginId.lastIndexOf('@') + 1);
1537
+ const stateKey = codexStateKey(target, component.pluginId);
1538
+ const before = pendingCodexNativeStates.get(stateKey) ?? codexNativeStates.get(stateKey);
1539
+ if (!before) {
1540
+ throw new Error('codexNativeObservationUnavailable: native group precheck is missing.');
1541
+ }
3549
1542
  const removalRequired = before === 'present';
3550
1543
  if (!removalRequired)
3551
1544
  return;
3552
1545
  try {
3553
- await runNativeCliChecked(buildCodexPluginRemoveInvocation(requireCodexManager(currentCodexManager()), component.pluginId, target.identity.cwd, options.context.env), runner);
1546
+ codexMutationMarketplaces.add(marketplaceName);
1547
+ await runObservedCodexMutation({
1548
+ marketplaceName,
1549
+ mutation: () => runNativeCliChecked(buildCodexPluginRemoveInvocation(requireCodexManager(currentCodexManager()), component.pluginId, target.identity.cwd, options.context.env), runner),
1550
+ });
3554
1551
  }
3555
- catch {
3556
- throw new Error(`codexNativeRemoveFailed: ${CODEX_REMOVE_RECOVERY}`);
1552
+ catch (error) {
1553
+ const diagnostic = readSafeOperationError(error);
1554
+ throw attachSafeOperationError(new Error(`codexNativeRemoveFailed: ${CODEX_REMOVE_RECOVERY}`), {
1555
+ code: 'codexNativeRemoveFailed',
1556
+ safeMessage: `Codex plugin removal did not complete. ${CODEX_REMOVE_RECOVERY}`,
1557
+ ...(diagnostic?.exitCode === undefined ? {} : { exitCode: diagnostic.exitCode }),
1558
+ ...(diagnostic ? {
1559
+ cause: { code: diagnostic.code, safeMessage: diagnostic.safeMessage },
1560
+ } : {}),
1561
+ });
3557
1562
  }
3558
- const after = await inspectCodexPluginRegistrationLive({
3559
- pluginId: component.pluginId,
3560
- cwd: target.identity.cwd,
3561
- env: options.context.env,
3562
- nativeCliRunner: runner,
3563
- manager: currentCodexManager(),
3564
- });
3565
- if (observedCodexNativeState(after.observation, 'native remove postcheck') !== 'absent') {
3566
- throw new Error(`codexNativeRemoveIncomplete: ${CODEX_REMOVE_RECOVERY}`);
1563
+ pendingCodexNativeStates.set(stateKey, 'absent');
1564
+ const postcheck = async () => {
1565
+ const after = await inspectCodexPluginRegistrationLive({
1566
+ pluginId: component.pluginId,
1567
+ cwd: target.identity.cwd,
1568
+ env: options.context.env,
1569
+ nativeCliRunner: runner,
1570
+ manager: currentCodexManager(),
1571
+ scope: observationScopeFor(target),
1572
+ observationSession: options.context.codexObservationSession,
1573
+ });
1574
+ const expected = pendingCodexNativeStates.get(stateKey) ?? 'absent';
1575
+ const observed = requireObservedCodexNativeState(after.observation, 'native mutation postcheck');
1576
+ if ((observed === 'installed' ? 'present' : 'absent') !== expected) {
1577
+ throw new Error(expected === 'absent'
1578
+ ? `codexNativeRemoveIncomplete: ${CODEX_REMOVE_RECOVERY}`
1579
+ : `codexNativeInstallIncomplete: ${component.pluginId} is absent from Codex plugin list.`);
1580
+ }
1581
+ };
1582
+ if (options.operationScopedNativePhases) {
1583
+ deferredPostchecks.push({ target, postcheck });
1584
+ }
1585
+ else {
1586
+ await postcheck();
3567
1587
  }
3568
1588
  }
3569
1589
  else {
@@ -3592,586 +1612,128 @@ function createNativeDriver(options) {
3592
1612
  }
3593
1613
  }
3594
1614
  },
3595
- writeNative: async (target) => {
3596
- if (!target.catalog)
3597
- throw new Error('Missing rebuild target catalog data.');
3598
- const catalog = target.catalog;
3599
- const marketplaceName = target.identity.marketplaceName ?? MARKETPLACE_NAME;
3600
- const expectedMarketplaceRoot = target.identity.marketplaceRoot ?? resolveAwHome(options.context.env);
3601
- const writeGeneration = async () => {
3602
- if (mirrorTransactions.has(target)) {
3603
- throw new Error('Marketplace mirror transaction is already active for this target.');
3604
- }
3605
- {
3606
- if (target.identity.platform === 'codex') {
3607
- const config = await readCodexConfigFile(resolveCodexPaths(target.identity.cwd, 'user', options.context.env).configTomlPath);
3608
- const marketplace = config.snapshot.marketplaces[marketplaceName];
3609
- if (marketplace && (marketplace.sourceType !== 'local'
3610
- || marketplace.sourcePointsToManifest
3611
- || path.normalize(marketplace.source ?? '') !== path.normalize(expectedMarketplaceRoot))) {
3612
- throw new Error(`externalOwnershipPresent: Codex marketplace ${marketplaceName} 已由 foreign source 占用,拒绝覆盖。`);
3613
- }
3614
- }
3615
- else {
3616
- const state = await readClaudeNativeState({
3617
- pluginName: target.identity.pluginName,
3618
- scope: target.identity.scope,
3619
- cwd: target.identity.cwd,
3620
- env: options.context.env,
3621
- marketplaceName,
3622
- marketplaceRoot: expectedMarketplaceRoot,
3623
- });
3624
- if (state.marketplaceState === 'foreign') {
3625
- throw new Error(`Claude marketplace ${marketplaceName} 已由 foreign source 占用,拒绝覆盖。`);
3626
- }
3627
- }
3628
- const pluginId = `${target.identity.pluginName}@${marketplaceName}`;
3629
- let codexRemovalRequired = false;
3630
- if (target.identity.platform === 'codex') {
3631
- const registration = await inspectCodexPluginRegistrationLive({
3632
- pluginId,
3633
- cwd: target.identity.cwd,
3634
- env: options.context.env,
3635
- nativeCliRunner: runner,
3636
- manager: currentCodexManager(),
3637
- });
3638
- codexRemovalRequired = requireObservedCodexNativeState(registration.observation, 'native write precheck') === 'installed';
3639
- }
3640
- const canonicalMarketplaceRoot = await canonicalizePathThroughExistingAncestor(expectedMarketplaceRoot);
3641
- const nativePluginRoot = await canonicalizePathThroughExistingAncestor(path.join(canonicalMarketplaceRoot, 'plugins', target.identity.pluginName));
3642
- const desiredSkillPaths = new Set((target.desiredOwnedComponents ?? [])
3643
- .filter((component) => component.kind === 'skill-directory')
3644
- .map((component) => path.resolve(component.path)));
3645
- const enabledSkillPaths = new Set(target.artifacts
3646
- .filter((artifact) => artifact.kind === 'skill' && artifact.state !== 'disabled')
3647
- .map((artifact) => path.resolve(artifact.path)));
3648
- const requiredSkillWrites = (target.operationActions ?? []).filter((action) => action.kind === 'writeOwnedArtifact'
3649
- && action.artifactKind === 'skill-directory'
3650
- && desiredSkillPaths.has(path.resolve(action.path))
3651
- && enabledSkillPaths.has(path.resolve(action.path))
3652
- && target.identity.scope === 'user'
3653
- && !path.relative(nativePluginRoot, action.path).startsWith('..'));
3654
- const staged = { nativeRender: null };
3655
- let skillsRelativeRoot = null;
3656
- const mirrored = await mirrorCatalog(catalog, options.context.env, target.identity, target.identity.platform === 'codex' ? async (stagingRoot) => {
3657
- for (const action of requiredSkillWrites) {
3658
- await applyPlannedOperationAction({
3659
- ...action,
3660
- path: path.join(stagingRoot, path.relative(canonicalMarketplaceRoot, action.path)),
3661
- allowedRoot: path.join(stagingRoot, path.relative(canonicalMarketplaceRoot, action.allowedRoot)),
3662
- });
3663
- }
3664
- const hasStaticSkills = target.plugin.manifest.skills !== undefined
3665
- || requiredSkillWrites.length > 0;
3666
- staged.nativeRender = await materializeCodexNativeRender({
3667
- plugin: target.plugin,
3668
- stagingRoot,
3669
- env: options.context.env,
3670
- scriptTimeoutMs: options.scriptTimeoutMs,
3671
- hasStaticSkills,
3672
- });
3673
- skillsRelativeRoot = staged.nativeRender
3674
- && (hasStaticSkills || staged.nativeRender.presentSkillKeys.length > 0)
3675
- ? await resolveCodexSkillsRelativeRoot(target.plugin)
3676
- : staged.nativeRender ? null : await ensureCodexActiveSkillsManifest({
3677
- plugin: target.plugin,
3678
- marketplaceRoot: stagingRoot,
3679
- hasRequiredSkills: hasStaticSkills,
3680
- });
3681
- } : undefined);
3682
- mirrorTransactions.set(target, mirrored);
3683
- for (const action of requiredSkillWrites)
3684
- completedNativeSkillWrites.add(action);
3685
- const marketplaceRoot = mirrored.rootDir;
3686
- if (target.identity.platform === 'codex') {
3687
- if (codexRemovalRequired) {
3688
- await runNativeCliChecked(buildCodexPluginRemoveInvocation(requireCodexManager(currentCodexManager()), pluginId, target.identity.cwd, options.context.env), runner);
3689
- }
3690
- const config = await readCodexConfigFile(resolveCodexPaths(target.identity.cwd, 'user', options.context.env).configTomlPath);
3691
- const marketplace = config.snapshot.marketplaces[marketplaceName];
3692
- if (path.normalize(marketplace?.source ?? '') !== path.normalize(marketplaceRoot)) {
3693
- await runNativeCliChecked(buildCodexMarketplaceAddInvocation(requireCodexManager(currentCodexManager()), marketplaceRoot, target.identity.cwd, options.context.env), runner);
3694
- }
3695
- await runNativeCliChecked(buildCodexMarketplaceListInvocation(requireCodexManager(currentCodexManager()), target.identity.cwd, options.context.env), runner);
3696
- await runNativeCliChecked(buildCodexPluginAddInvocation(requireCodexManager(currentCodexManager()), pluginId, target.identity.cwd, options.context.env), runner);
3697
- await runNativeCliChecked(buildCodexPluginListInvocation(requireCodexManager(currentCodexManager()), target.identity.cwd, options.context.env), runner);
3698
- const installed = await inspectCodexPluginRegistrationLive({
3699
- pluginId,
3700
- cwd: target.identity.cwd,
3701
- env: options.context.env,
3702
- nativeCliRunner: runner,
3703
- manager: currentCodexManager(),
3704
- });
3705
- if (observedCodexNativeState(installed.observation, 'native install postcheck') !== 'present') {
3706
- throw new Error(`codexNativeInstallIncomplete: ${pluginId} is absent from Codex plugin list.`);
3707
- }
3708
- await assertCodexNativeInstallState({
3709
- pluginName: target.identity.pluginName,
3710
- pluginVersion: target.identity.pluginVersion,
3711
- cwd: target.identity.cwd,
3712
- env: options.context.env,
3713
- marketplaceRoot,
3714
- marketplaceName,
3715
- skillsRelativeRoot,
3716
- requiredSkillKeys: [
3717
- ...requiredSkillWrites.flatMap((action) => action.owner?.artifactKey ? [action.owner.artifactKey] : []),
3718
- ...(staged.nativeRender?.presentSkillKeys ?? []),
3719
- ],
3720
- ...(staged.nativeRender ? { nativeRender: staged.nativeRender } : {}),
3721
- });
3722
- }
3723
- else {
3724
- await runNativeCliChecked({
3725
- command: 'claude',
3726
- args: ['plugin', 'marketplace', 'add', marketplaceRoot, '--scope', target.identity.scope],
3727
- cwd: target.identity.cwd,
3728
- env: options.context.env,
3729
- }, runner);
3730
- await runNativeCliChecked({
3731
- command: 'claude',
3732
- args: ['plugin', 'install', pluginId, '--scope', target.identity.scope],
3733
- cwd: target.identity.cwd,
3734
- env: options.context.env,
3735
- }, runner);
3736
- await runNativeCliChecked({
3737
- command: 'claude',
3738
- args: ['plugin', 'list'],
3739
- cwd: target.identity.cwd,
3740
- env: options.context.env,
3741
- }, runner);
3742
- const after = await readClaudeNativeState({
3743
- pluginName: target.identity.pluginName,
3744
- scope: target.identity.scope,
3745
- cwd: target.identity.cwd,
3746
- env: options.context.env,
3747
- marketplaceName,
3748
- marketplaceRoot,
3749
- });
3750
- if (after.marketplaceDeclaredState !== 'canonical'
3751
- || after.marketplaceMaterializedState !== 'canonical'
3752
- || after.pluginState !== 'present'
3753
- || !after.pluginEnabled
3754
- || !after.pluginVersions.includes(target.identity.pluginVersion)) {
3755
- throw new Error(`Claude native install postcheck 失败:declaredMarketplace=${after.marketplaceDeclaredState},materializedMarketplace=${after.marketplaceMaterializedState},plugin=${after.pluginState},enabled=${after.pluginEnabled},versions=${after.pluginVersions.join(',')}。`);
3756
- }
3757
- }
3758
- }
3759
- };
3760
- await writeGeneration();
3761
- },
1615
+ applyManagedCommands: (target) => publishLifecycleCommandPackages(target, options.context),
1616
+ writeNative: async () => { throw new Error('platformDeploymentRequired: native writes require a frozen platform deployment.'); },
3762
1617
  writeArtifacts: async (target) => {
3763
- for (const operationAction of target.operationActions ?? []) {
3764
- if (completedNativeSkillWrites.has(operationAction))
3765
- continue;
3766
- if (operationAction.kind === 'writeOwnedArtifact'
3767
- || operationAction.kind === 'updateSharedConfig'
3768
- || operationAction.kind === 'setActivation') {
3769
- await applyPlannedOperationAction(operationAction);
3770
- }
3771
- }
3772
- return target.artifacts;
1618
+ if (target.artifacts.length > 0)
1619
+ throw new Error('platformDeploymentRequired: platform output requires a frozen platform deployment.');
1620
+ return [];
3773
1621
  },
3774
- runPostApply: async (invocation) => {
3775
- try {
3776
- await runPostApplyScript({
3777
- plugin: invocation.plugin,
3778
- platform: invocation.target.identity.platform,
3779
- scope: invocation.target.identity.scope,
3780
- cwd: invocation.target.identity.cwd,
3781
- context: options.context,
3782
- artifacts: [...invocation.artifacts],
3783
- components: invocation.components,
3784
- scriptTimeoutMs: options.scriptTimeoutMs,
3785
- });
1622
+ runPreApply: async (invocation) => {
1623
+ if (isNativeActivationOnlyTarget(options.context, invocation.target.identity))
1624
+ return;
1625
+ const run = () => runPreApplyScript({
1626
+ plugin: invocation.plugin,
1627
+ platform: invocation.target.identity.platform,
1628
+ scope: invocation.target.identity.scope,
1629
+ cwd: invocation.target.identity.cwd,
1630
+ context: options.context,
1631
+ referencePlugins: options.context.referencePlugins,
1632
+ scriptTimeoutMs: options.scriptTimeoutMs,
1633
+ });
1634
+ const cache = options.context.preApplyExecutionCache;
1635
+ if (!cache) {
1636
+ await run();
1637
+ return;
3786
1638
  }
3787
- catch (error) {
3788
- await options.context.reporter.notice({
3789
- level: 'warning',
3790
- message: error instanceof Error ? error.message : String(error),
3791
- source: {
3792
- plugin: invocation.plugin.manifest.name,
3793
- platform: invocation.target.identity.platform,
3794
- scope: invocation.target.identity.scope,
3795
- action: 'postApply',
3796
- },
3797
- target: 'stderr',
3798
- });
3799
- throw error;
1639
+ const { identity } = invocation.target;
1640
+ const sourceIdentity = invocation.plugin.sourceIdentity ?? identity.sourceIdentity;
1641
+ const pluginId = invocation.plugin.pluginId ?? identity.pluginId;
1642
+ if (!sourceIdentity || !pluginId) {
1643
+ await run();
1644
+ return;
3800
1645
  }
1646
+ const projectKey = identity.scope === 'project'
1647
+ ? identity.platform === 'codex'
1648
+ ? await resolveCodexProjectKey(identity.cwd)
1649
+ : identity.platform === 'claude'
1650
+ ? await resolveClaudeProjectKey(identity.cwd)
1651
+ : createProjectLocationKey(await resolveRequiredCanonicalRepoRoot(identity.cwd))
1652
+ : '';
1653
+ const key = [
1654
+ sourceIdentity,
1655
+ pluginId,
1656
+ identity.platform,
1657
+ identity.scope,
1658
+ projectKey,
1659
+ ].join('\0');
1660
+ let execution = cache.get(key);
1661
+ if (!execution) {
1662
+ execution = run();
1663
+ cache.set(key, execution);
1664
+ }
1665
+ await execution;
3801
1666
  },
3802
1667
  };
3803
1668
  }
1669
+ export function isNativeActivationOnlyTarget(context, identity) {
1670
+ return context.nativeActivationOnlyTargets?.some((target) => target.pluginId === identity.pluginId
1671
+ && target.platform === identity.platform
1672
+ && target.scope === identity.scope) === true;
1673
+ }
3804
1674
  export function createRebuildIntegration(options) {
1675
+ options.context.scriptTimeoutMs ??= options.scriptTimeoutMs;
3805
1676
  const targets = [...(options.preparedTargets ?? [])];
3806
1677
  const driver = createNativeDriver({ targets, ...options });
1678
+ const renderResults = options.context.preparedArtifactRenderResults ??= new Map();
1679
+ const packageCache = options.context.packagePreparationCache
1680
+ ?? commandPackageCandidates.get(renderResults) ?? new Map();
1681
+ commandPackageCandidates.set(renderResults, packageCache);
1682
+ options.context.packagePreparationCache = packageCache;
1683
+ const sessions = platformRebuildSessions.get(renderResults) ?? new Map();
1684
+ platformRebuildSessions.set(renderResults, sessions);
1685
+ const platformSession = options.context.platformRebuildSession
1686
+ ?? sessions.get(options.context.batchInstallPlatform)
1687
+ ?? new PlatformRebuildSession(options.context);
1688
+ sessions.set(options.context.batchInstallPlatform, platformSession);
1689
+ options.context.platformRebuildSession = platformSession;
1690
+ platformSession.updateContext(options.context);
1691
+ platformSession.setOwnershipExecutor(driver.ownership);
1692
+ const targetPlatforms = new Map();
3807
1693
  return {
1694
+ resolvePlatformTargets: async (inputs) => {
1695
+ await platformSession.resolveAll(inputs);
1696
+ for (const input of inputs) {
1697
+ const id = input.pluginId ?? pluginActionId(input.plugin);
1698
+ const platforms = targetPlatforms.get(id) ?? [];
1699
+ if (!platforms.includes(input.platform))
1700
+ platforms.push(input.platform);
1701
+ targetPlatforms.set(id, platforms);
1702
+ }
1703
+ },
1704
+ sealMarketplaceGenerations: (eligible) => platformSession.sealMarketplaceGenerations(eligible),
1705
+ pendingOpenCodeProjectNotices: (interrupted) => platformSession.pendingOpenCodeProjectNotices(interrupted),
3808
1706
  driver,
1707
+ setNativeMarketplaceEligibleTargets: driver.setNativeMarketplaceEligibleTargets,
1708
+ prepareNativeMarketplaceGroup: driver.prepareNativeMarketplaceGroup,
1709
+ postcheckCodexProvider: driver.postcheckCodexProvider,
1710
+ captureCodexPostMutationSnapshots: driver.captureCodexPostMutationSnapshots,
3809
1711
  buildTarget: async (input) => {
3810
- input = {
3811
- ...input,
3812
- sourceId: input.sourceId ?? input.plugin.sourceId,
3813
- sourceIdentity: input.sourceIdentity ?? input.plugin.sourceIdentity,
3814
- pluginId: input.pluginId ?? input.plugin.pluginId,
3815
- };
3816
- const materializing = input.command !== 'cleanup'
3817
- && input.command !== 'uninstall'
3818
- && input.readOnlyInspectionPlan !== true;
3819
- if (input.scope === 'project') {
3820
- const canonicalRepoRoot = await assertCanonicalProjectManagedRoots({
3821
- cwd: input.cwd,
3822
- rootNames: input.platform === 'codex' ? ['.agents', '.codex'] : ['.claude'],
3823
- managedPaths: input.platform === 'codex'
3824
- ? ['.codex/config.toml']
3825
- : ['.claude/settings.json'],
3826
- });
3827
- input = { ...input, cwd: canonicalRepoRoot };
3828
- }
3829
- if (materializing && input.platform === 'codex') {
3830
- await preflightCodexWriteSafety({
3831
- plugin: input.plugin,
3832
- scope: input.scope,
3833
- cwd: input.cwd,
3834
- env: options.context.env,
3835
- });
3836
- }
3837
- const event = 'install';
3838
- const historicalArtifacts = [];
3839
- let writes = [];
3840
- let currentRemovalComponents = [];
3841
- if (input.command !== 'cleanup' && input.command !== 'uninstall') {
3842
- if (input.readOnlyInspectionPlan) {
3843
- writes = await buildInspectionWrites({
3844
- plugin: input.plugin,
3845
- platform: input.platform,
3846
- scope: input.scope,
3847
- cwd: input.cwd,
3848
- env: options.context.env,
3849
- marketplaceRoot: input.marketplaceRoot,
3850
- marketplaceName: input.marketplaceName,
3851
- userActivationDesired: options.context.codexUserActivationDesired,
3852
- shallow: input.readOnlyShallowInspectionPlan,
3853
- });
3854
- }
3855
- else if (input.platform === 'codex') {
3856
- writes = await buildCodexWrites({
3857
- ...input,
3858
- env: options.context.env,
3859
- event,
3860
- historicalArtifacts,
3861
- scriptTimeoutMs: options.scriptTimeoutMs,
3862
- marketplaceName: input.marketplaceName,
3863
- userActivationDesired: options.context.codexUserActivationDesired,
3864
- });
1712
+ const removed = input.command === 'cleanup' || input.command === 'uninstall';
1713
+ const id = input.pluginId ?? pluginActionId(input.plugin);
1714
+ const platforms = options.context.commandPackagePlatforms?.get(id) ?? targetPlatforms.get(id) ?? [input.platform];
1715
+ let commands;
1716
+ if (!input.readOnlyInspectionPlan) {
1717
+ try {
1718
+ commands = await prepareLifecycleCommandPackage({ ...input, removing: removed, platforms, context: options.context });
3865
1719
  }
3866
- else {
3867
- writes = await buildClaudeWrites({
3868
- ...input,
3869
- env: options.context.env,
3870
- event,
3871
- historicalArtifacts,
3872
- scriptTimeoutMs: options.scriptTimeoutMs,
3873
- });
1720
+ catch (error) {
1721
+ if (error instanceof ManagedCommandError)
1722
+ throw new Error(`${error.code}: ${error.message}`, { cause: error });
1723
+ throw error;
3874
1724
  }
3875
1725
  }
3876
- else if (input.platform === 'codex' && input.scope === 'project') {
3877
- const currentRemovalWrites = await buildInspectionWrites({
3878
- plugin: input.plugin,
3879
- platform: input.platform,
3880
- scope: input.scope,
3881
- cwd: input.cwd,
3882
- env: options.context.env,
3883
- marketplaceRoot: input.marketplaceRoot,
3884
- marketplaceName: input.marketplaceName,
3885
- userActivationDesired: options.context.codexUserActivationDesired,
3886
- });
3887
- currentRemovalComponents = currentRemovalWrites.flatMap((write) => [
3888
- ...(write.additionalComponents ?? []),
3889
- ...(write.component ? [write.component] : []),
3890
- ]);
3891
- }
3892
- const desiredComponents = writes.filter((write) => !write.optional).flatMap((write) => [
3893
- ...(write.additionalComponents ?? []),
3894
- ...(write.component ? [write.component] : []),
3895
- ]);
3896
- const optionalComponents = writes
3897
- .filter((write) => write.optional)
3898
- .flatMap((write) => [
3899
- ...(write.additionalComponents ?? []),
3900
- ...(write.component ? [write.component] : []),
3901
- ]);
3902
- const optionalComponentKeys = new Set(optionalComponents.map(componentIdentityKey));
3903
- const preserveCodexSharedNativePayload = input.platform === 'codex'
3904
- && (input.command === 'cleanup'
3905
- || input.command === 'uninstall'
3906
- || input.preserveCodexSharedNativePayload === true);
3907
- const configDir = input.platform === 'codex'
3908
- ? resolveCodexPaths(input.cwd, input.scope, options.context.env).configDir
3909
- : resolveClaudePaths(input.cwd, input.scope, options.context.env).configDir;
3910
- const textRoots = input.platform === 'codex'
3911
- ? [path.join(configDir, 'rules'), path.join(configDir, 'agents')]
3912
- : [path.join(configDir, 'rules'), path.join(configDir, 'agents')];
3913
- const scannedText = (await Promise.all(textRoots.map((root) => scanOwnedTextFiles({
3914
- sourceId: input.sourceId,
3915
- sourceIdentity: input.sourceIdentity,
3916
- root,
3917
- pluginName: input.plugin.manifest.name,
3918
- platform: input.platform,
3919
- scope: input.scope,
3920
- })))).flat();
3921
- const skillRoots = input.platform === 'codex'
3922
- ? input.scope === 'project'
3923
- ? []
3924
- : [
3925
- path.join(getCodexHome(options.context.env), 'skills'),
3926
- ...(preserveCodexSharedNativePayload || input.readOnlyShallowInspectionPlan === true
3927
- ? []
3928
- : [await canonicalizePathThroughExistingAncestor(path.join(input.marketplaceRoot ?? resolveAwHome(options.context.env), 'plugins', input.plugin.manifest.name))]),
3929
- ]
3930
- : [path.join(configDir, 'skills')];
3931
- const scanSkills = () => Promise.all(skillRoots.map((root) => scanOwnedSkills({
3932
- sourceId: input.sourceId,
3933
- sourceIdentity: input.sourceIdentity,
3934
- root,
3935
- pluginName: input.plugin.manifest.name,
3936
- platform: input.platform,
3937
- scope: input.scope,
3938
- })));
3939
- const scannedSkills = (await scanSkills()).flat();
3940
- const legacyCodexNativeSkills = input.platform === 'codex'
3941
- ? (await (async () => {
3942
- const skillKeys = [...new Set(codexNativeRenderTargets(input.plugin)
3943
- .map((target) => target.skillKey))];
3944
- if (skillKeys.length === 0)
3945
- return [];
3946
- const projectRoot = await assertCanonicalProjectManagedRoots({
3947
- cwd: input.cwd,
3948
- rootNames: ['.agents'],
3949
- });
3950
- const roots = [
3951
- {
3952
- root: path.join(getCodexHome(options.context.env), 'skills'),
3953
- scope: 'user',
3954
- },
3955
- {
3956
- root: path.join(projectRoot, '.agents', 'skills'),
3957
- scope: 'project',
3958
- },
3959
- ];
3960
- return roots.flatMap(({ root, scope }) => skillKeys.map((skillKey) => pathComponent({
3961
- kind: 'skill-directory',
3962
- path: path.join(root, skillKey),
3963
- allowedRoot: root,
3964
- identity: ownerIdentity(input.plugin.manifest.name, skillKey, 'codex', scope),
3965
- })));
3966
- })())
3967
- : [];
3968
- let historicalComponents = await buildHistoricalComponents({
3969
- sourceId: input.sourceId,
3970
- sourceIdentity: input.sourceIdentity,
3971
- pluginId: input.pluginId,
3972
- pluginName: input.plugin.manifest.name,
3973
- platform: input.platform,
3974
- scope: input.scope,
3975
- cwd: input.cwd,
3976
- env: options.context.env,
3977
- artifacts: historicalArtifacts,
3978
- command: input.command,
3979
- });
3980
- if (preserveCodexSharedNativePayload) {
3981
- const nativePluginRoots = await Promise.all([
3982
- path.join(input.marketplaceRoot ?? resolveAwHome(options.context.env), 'plugins', input.plugin.manifest.name),
3983
- path.join(getCodexHome(options.context.env), 'plugins', 'cache', input.marketplaceName ?? MARKETPLACE_NAME, input.plugin.manifest.name),
3984
- ].map((root) => canonicalizePathThroughExistingAncestor(root)));
3985
- historicalComponents = historicalComponents.filter((component) => {
3986
- if (!('path' in component))
3987
- return true;
3988
- return nativePluginRoots.every((nativePluginRoot) => {
3989
- const relative = path.relative(nativePluginRoot, path.resolve(component.path));
3990
- return relative === '..'
3991
- || relative.startsWith(`..${path.sep}`)
3992
- || path.isAbsolute(relative);
3993
- });
3994
- });
3995
- }
3996
- const sharedComponents = input.platform === 'codex'
3997
- ? await scanCodexSharedComponents({
3998
- sourceId: input.sourceId,
3999
- sourceIdentity: input.sourceIdentity,
4000
- pluginId: input.pluginId,
4001
- pluginName: input.plugin.manifest.name,
4002
- scope: input.scope,
4003
- paths: resolveCodexPaths(input.cwd, input.scope, options.context.env),
4004
- scanConfig: true,
4005
- ignoreReadErrors: true,
4006
- })
4007
- : await scanClaudeSharedComponents({
4008
- sourceId: input.sourceId,
4009
- sourceIdentity: input.sourceIdentity,
4010
- pluginId: input.pluginId,
4011
- pluginName: input.plugin.manifest.name,
4012
- scope: input.scope,
4013
- settingsPath: resolveClaudePaths(input.cwd, input.scope, options.context.env).settingsPath,
4014
- });
4015
- const nativeRequired = input.platform === 'claude'
4016
- || analyzeCodexSurface(input.plugin.manifest).nativeKinds.length > 0;
4017
- const nativeDesired = nativeRequired
4018
- && input.command !== 'cleanup'
4019
- && input.command !== 'uninstall';
4020
- const codexNative = input.platform === 'codex' && nativeRequired
4021
- ? await inspectCodexNativeResidue({
4022
- pluginName: input.plugin.manifest.name,
4023
- cwd: input.cwd,
4024
- env: options.context.env,
4025
- marketplaceName: input.marketplaceName,
4026
- marketplaceRoot: input.marketplaceRoot,
4027
- })
4028
- : undefined;
4029
- const codexNativeExact = codexNative?.state === 'present'
4030
- && codexNative.versions.length > 0
4031
- && codexNative.versions.every((version) => version === input.plugin.manifest.version);
4032
- const materializeNativeRequired = nativeDesired && (input.platform === 'claude'
4033
- || input.command === 'reinstall'
4034
- || !codexNativeExact);
4035
- const nativeCleanupCandidate = input.platform === 'claude'
4036
- && input.includeNativeCleanupCandidate !== false;
4037
- const nativePlugin = (nativeDesired || nativeCleanupCandidate)
4038
- ? [{
4039
- kind: 'native-plugin',
4040
- identity: ownerIdentity(input.plugin.manifest.name, 'native', input.platform, input.scope),
4041
- pluginId: `${input.plugin.manifest.name}@${input.marketplaceName ?? MARKETPLACE_NAME}`,
4042
- }]
4043
- : [];
4044
- const marketplaceDesired = nativeRequired
4045
- && input.command !== 'cleanup'
4046
- && input.command !== 'uninstall';
4047
- const marketplace = input.platform === 'codex'
4048
- && input.scope === 'user'
4049
- && nativeDesired
4050
- && input.readOnlyShallowInspectionPlan !== true
4051
- ? [createCodexMarketplaceComponent({
4052
- pluginName: input.plugin.manifest.name,
4053
- codexManager: options.context.codexManager,
4054
- cwd: input.cwd,
4055
- env: options.context.env,
4056
- runner: options.context.nativeCliRunner,
4057
- marketplaceName: input.marketplaceName ?? MARKETPLACE_NAME,
4058
- marketplaceRoot: input.marketplaceRoot ?? resolveAwHome(options.context.env),
4059
- desired: marketplaceDesired,
4060
- })]
4061
- : input.platform === 'claude' && marketplaceDesired
4062
- ? [createClaudeMarketplaceComponent({
4063
- pluginName: input.plugin.manifest.name,
4064
- scope: input.scope,
4065
- cwd: input.cwd,
4066
- env: options.context.env,
4067
- runner: options.context.nativeCliRunner,
4068
- marketplaceName: input.marketplaceName ?? MARKETPLACE_NAME,
4069
- marketplaceRoot: input.marketplaceRoot ?? resolveAwHome(options.context.env),
4070
- desired: marketplaceDesired,
4071
- })]
4072
- : [];
4073
- if (nativeDesired)
4074
- desiredComponents.push(...nativePlugin, ...marketplace);
4075
- const activationCleanup = input.platform === 'codex'
4076
- && nativeRequired
4077
- && (input.command === 'cleanup' || input.command === 'uninstall')
4078
- ? (input.scope === 'project'
4079
- ? [
4080
- createCodexActivationComponent({
4081
- identity: ownerIdentity(input.plugin.manifest.name, internalArtifactKey('activation', 'project'), 'codex', input.scope),
4082
- configPath: resolveCodexPaths(input.cwd, 'project', options.context.env).configTomlPath,
4083
- codexPluginId: `${input.plugin.manifest.name}@${input.marketplaceName ?? MARKETPLACE_NAME}`,
4084
- desired: true,
4085
- }),
4086
- ]
4087
- : [createCodexActivationComponent({
4088
- identity: ownerIdentity(input.plugin.manifest.name, internalArtifactKey('activation', 'user'), 'codex', input.scope),
4089
- configPath: resolveCodexPaths(input.cwd, 'user', options.context.env).configTomlPath,
4090
- codexPluginId: `${input.plugin.manifest.name}@${input.marketplaceName ?? MARKETPLACE_NAME}`,
4091
- desired: true,
4092
- })])
4093
- : [];
4094
- const explicitActivationCleanup = input.platform === 'codex'
4095
- ? (input.codexActivationCleanup ?? []).map((activation) => createCodexActivationCleanupComponent({
4096
- identity: ownerIdentity(input.plugin.manifest.name, internalArtifactKey('activation', activation.scope), 'codex', input.scope),
4097
- configPath: resolveCodexPaths(input.cwd, activation.scope, options.context.env).configTomlPath,
4098
- codexPluginId: activation.codexPluginId,
4099
- disposition: activation.disposition,
4100
- }))
4101
- : [];
4102
- const allComponents = dedupeComponents([
4103
- ...desiredComponents,
4104
- ...optionalComponents,
4105
- ...(input.platform === 'codex' ? sharedComponents : []),
4106
- ...scannedText,
4107
- ...scannedSkills,
4108
- ...legacyCodexNativeSkills,
4109
- ...currentRemovalComponents,
4110
- ...historicalComponents,
4111
- ...(input.platform === 'claude' ? sharedComponents : []),
4112
- ...nativePlugin,
4113
- ...marketplace,
4114
- ...activationCleanup,
4115
- ...explicitActivationCleanup,
4116
- ]);
4117
- bindNamespacedOwnerIdentity(allComponents, input);
4118
- const desiredKeys = new Set(desiredComponents.map(componentIdentityKey));
4119
- const operationActions = writes.flatMap((write) => [
4120
- ...(write.action ? [write.action] : []),
4121
- ...(write.additionalActions ?? []),
4122
- ]);
4123
- finalizeOwnedArtifactActions(operationActions);
4124
- if (nativeDesired) {
4125
- operationActions.unshift({
4126
- kind: 'ensureNativeRegistration',
4127
- pluginId: input.pluginId ?? pluginActionId(input.plugin),
4128
- platform: input.platform,
4129
- scope: input.scope,
4130
- registration: `${input.plugin.manifest.name}@${input.marketplaceName ?? MARKETPLACE_NAME}`,
4131
- marketplaceName: input.marketplaceName ?? MARKETPLACE_NAME,
4132
- marketplaceRoot: input.marketplaceRoot ?? resolveAwHome(options.context.env),
4133
- });
4134
- }
4135
- const postApply = input.plugin.manifest.scripts?.postApply;
4136
- if (postApply && input.command !== 'cleanup' && input.command !== 'uninstall') {
4137
- const script = path.resolve(input.plugin.rootDir, postApply.path.replace(/^\.\//u, ''));
4138
- const scriptDigest = `sha256:${crypto.createHash('sha256')
4139
- .update(await fs.readFile(script)).digest('hex')}`;
4140
- operationActions.push({
4141
- kind: 'runPostApply',
4142
- pluginId: input.pluginId ?? pluginActionId(input.plugin),
4143
- platform: input.platform,
4144
- scope: input.scope,
4145
- script: postApply.path,
4146
- scriptDigest,
4147
- });
1726
+ const target = await platformSession.buildTarget({ ...input, commandRecord: commands?.record });
1727
+ if (commands) {
1728
+ const operationActions = commands.actions;
1729
+ const preApply = input.plugin.manifest.scripts?.preApply;
1730
+ if (preApply && !removed && !isNativeActivationOnlyTarget(options.context, target.identity))
1731
+ operationActions.push({ kind: 'runPreApply',
1732
+ pluginId: input.pluginId ?? pluginActionId(input.plugin), platform: input.platform, scope: input.scope,
1733
+ script: preApply.path, scriptDigest: `sha256:${crypto.createHash('sha256')
1734
+ .update(await fs.readFile(path.resolve(input.plugin.rootDir, preApply.path.replace(/^\.\//u, '')))).digest('hex')}` });
1735
+ target.operationActions = operationActions;
4148
1736
  }
4149
- const target = {
4150
- plugin: input.plugin,
4151
- identity: {
4152
- ...(input.sourceId ? {
4153
- sourceId: input.sourceId,
4154
- sourceIdentity: input.sourceIdentity,
4155
- pluginId: input.pluginId,
4156
- marketplaceName: input.marketplaceName,
4157
- marketplaceRoot: input.marketplaceRoot,
4158
- } : {}),
4159
- pluginName: input.plugin.manifest.name,
4160
- pluginVersion: input.plugin.manifest.version,
4161
- platform: input.platform,
4162
- scope: input.scope,
4163
- cwd: input.cwd,
4164
- },
4165
- ownedComponents: allComponents,
4166
- desiredOwnedComponents: allComponents.filter((component) => desiredKeys.has(componentIdentityKey(component))),
4167
- optionalOwnedComponents: allComponents.filter((component) => optionalComponentKeys.has(componentIdentityKey(component))),
4168
- requiresNative: nativeDesired,
4169
- writeNativeRequired: materializeNativeRequired,
4170
- artifacts: writes.map((write) => write.artifact),
4171
- operationActions,
4172
- catalog: input.catalog,
4173
- postApplySource: input.plugin,
4174
- };
4175
1737
  targets.push(target);
4176
1738
  return target;
4177
1739
  },
@@ -4213,24 +1775,22 @@ export async function inspectRebuildTarget(options) {
4213
1775
  if (!target.requiresNative)
4214
1776
  return { target, inspection };
4215
1777
  if (target.identity.platform === 'codex') {
4216
- const residue = await inspectCodexNativeResidue({
4217
- pluginName: target.identity.pluginName,
1778
+ const nativeInspection = inspection.components.find((component) => component.component.kind === 'native-plugin');
1779
+ if (nativeInspection?.state !== 'present')
1780
+ return { target, inspection };
1781
+ const registration = await inspectCodexPluginRegistrationLive({
1782
+ pluginId: `${target.identity.pluginName}@${target.identity.marketplaceName ?? MARKETPLACE_NAME}`,
4218
1783
  cwd: target.identity.cwd,
4219
1784
  env: options.context.env,
4220
- marketplaceName: target.identity.marketplaceName,
4221
- marketplaceRoot: target.identity.marketplaceRoot,
1785
+ nativeCliRunner: options.context.nativeCliRunner,
1786
+ manager: options.context.codexManager,
1787
+ scope: target.identity.scope,
1788
+ observationSession: options.context.codexObservationSession,
4222
1789
  });
4223
- if (residue.state !== 'present' || !residue.enabled)
4224
- return { target, inspection };
4225
- const installed = [...new Set(residue.installedVersions)];
4226
- if (installed.length > 0) {
4227
- return installed.length === 1
4228
- ? { target, inspection, installedVersion: installed[0] }
4229
- : { target, inspection };
4230
- }
4231
- const cached = [...new Set(residue.cacheVersions)];
4232
- return cached.length === 1
4233
- ? { target, inspection, installedVersion: cached[0] }
1790
+ return registration.observation.state === 'observed'
1791
+ && registration.observation.value === 'installed'
1792
+ && registration.observation.entry.version !== undefined
1793
+ ? { target, inspection, installedVersion: registration.observation.entry.version }
4234
1794
  : { target, inspection };
4235
1795
  }
4236
1796
  const native = await readClaudeNativeState({
@@ -4330,13 +1890,11 @@ export async function discoverRebuildTargetNames(options) {
4330
1890
  if (!pluginEntry.isDirectory())
4331
1891
  continue;
4332
1892
  const pluginRoot = path.join(pluginsRoot, pluginEntry.name);
4333
- const platformManifest = await fs.readJson(path.join(pluginRoot, '.codex-plugin', 'plugin.json')).catch(() => null);
4334
- const relativeSkillsRoot = typeof platformManifest?.skills === 'string'
4335
- ? path.posix.normalize(platformManifest.skills.replace(/^\.\//u, ''))
4336
- : 'codex/skills';
4337
- if (relativeSkillsRoot === '..'
4338
- || relativeSkillsRoot.startsWith('../')
4339
- || path.isAbsolute(relativeSkillsRoot))
1893
+ const relativeSkillsRoot = await resolveCodexSkillsRelativeRoot({
1894
+ rootDir: pluginRoot,
1895
+ manifest: { skills: undefined },
1896
+ }).catch(() => null);
1897
+ if (!relativeSkillsRoot)
4340
1898
  continue;
4341
1899
  for (const name of await discoverOwnerNamesInSkillRoot(path.join(pluginRoot, ...relativeSkillsRoot.split('/')), options.platform, options.scope))
4342
1900
  names.add(name);
@@ -4366,13 +1924,46 @@ export async function discoverRebuildTargetNames(options) {
4366
1924
  }
4367
1925
  }
4368
1926
  else {
4369
- for (const name of await discoverCodexNativeResidueNames({
4370
- cwd: options.cwd,
1927
+ const marketplaceName = options.marketplaceName ?? MARKETPLACE_NAME;
1928
+ const marketplaceRoot = options.marketplaceRoot ?? resolveAwHome(options.env);
1929
+ const marketplace = config.snapshot.marketplaces[marketplaceName];
1930
+ const canonicalMarketplace = isCanonicalCodexMarketplaceRegistration(marketplace, marketplaceRoot);
1931
+ const suffix = `@${marketplaceName}`;
1932
+ const configuredPluginIds = Object.keys(config.snapshot.plugins)
1933
+ .filter((pluginId) => pluginId.endsWith(suffix));
1934
+ if (configuredPluginIds.length > 0 && !canonicalMarketplace) {
1935
+ throw new Error(`externalOwnershipPresent: Codex marketplace ${marketplaceName} has a foreign public config source.`);
1936
+ }
1937
+ for (const pluginId of canonicalMarketplace ? configuredPluginIds : []) {
1938
+ const name = pluginId.slice(0, -suffix.length);
1939
+ if (/^[a-z0-9]+(?:-[a-z0-9]+)*$/u.test(name))
1940
+ names.add(name);
1941
+ }
1942
+ const session = await createCodexNativeObservationSession({
1943
+ exactCwd: options.cwd,
4371
1944
  env: options.env,
4372
- marketplaceName: options.marketplaceName,
4373
- marketplaceRoot: options.marketplaceRoot,
4374
- }))
4375
- names.add(name);
1945
+ runner: options.nativeCliRunner,
1946
+ });
1947
+ try {
1948
+ const snapshot = await session.snapshot(marketplaceName, 'user');
1949
+ if (!snapshot.neutral) {
1950
+ if (options.nativeFailureMode !== 'ignore') {
1951
+ throw new Error('codexNativeObservationFailed: cannot discover official Codex plugins.');
1952
+ }
1953
+ }
1954
+ else {
1955
+ for (const pluginId of snapshot.neutral.keys()) {
1956
+ if (!pluginId.endsWith(suffix))
1957
+ continue;
1958
+ const name = pluginId.slice(0, -suffix.length);
1959
+ if (/^[a-z0-9]+(?:-[a-z0-9]+)*$/u.test(name))
1960
+ names.add(name);
1961
+ }
1962
+ }
1963
+ }
1964
+ finally {
1965
+ await session.dispose();
1966
+ }
4376
1967
  }
4377
1968
  }
4378
1969
  else {