@ai-agent-forge/agent-forge 0.86.3 → 0.86.5

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 (238) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/dist/bundle/.build-manifest.json +500 -434
  3. package/dist/bundle/chunks/anthropic-messages-ALDZBUKI.js +6 -0
  4. package/dist/bundle/chunks/anthropic.js +2 -2
  5. package/dist/bundle/chunks/{azure-openai-responses-WQCZWFAH.js → azure-openai-responses-A2WJWZFB.js} +1 -1
  6. package/dist/bundle/chunks/bedrock-converse-stream.js +1 -1
  7. package/dist/bundle/chunks/chunk-A7GUOX2H.js +141 -0
  8. package/dist/bundle/chunks/{chunk-WGLBM2BP.js → chunk-CIX3FSBK.js} +1 -1
  9. package/dist/bundle/chunks/{chunk-RK2X2KU2.js → chunk-EALJ5DDX.js} +111 -101
  10. package/dist/bundle/chunks/chunk-OO6DJ3CL.js +31 -0
  11. package/dist/bundle/chunks/{chunk-N6BQJSGH.js → chunk-SQFG22WN.js} +3 -3
  12. package/dist/bundle/chunks/{chunk-V46XUTK3.js → chunk-Y7HXTASS.js} +1 -1
  13. package/dist/bundle/chunks/{chunk-3WE3W52L.js → chunk-YJCQVXW5.js} +1 -1
  14. package/dist/bundle/chunks/github-copilot.js +1 -1
  15. package/dist/bundle/chunks/google-generative-ai-NIB4WONV.js +2 -0
  16. package/dist/bundle/chunks/google-vertex-DCPYUEUO.js +2 -0
  17. package/dist/bundle/chunks/mistral-conversations-OC6LA5XL.js +5 -0
  18. package/dist/bundle/chunks/{openai-codex-responses-YDJSZOIH.js → openai-codex-responses-IO2X4Y7L.js} +1 -1
  19. package/dist/bundle/chunks/openai-codex.js +2 -2
  20. package/dist/bundle/chunks/{openai-completions-UPRWTQPT.js → openai-completions-XYQQHETU.js} +1 -1
  21. package/dist/bundle/chunks/{openai-responses-O63W6HQ3.js → openai-responses-DLIRN3CI.js} +1 -1
  22. package/dist/bundle/chunks/{openrouter-images-G3GT5BVT.js → openrouter-images-G4GQRUSG.js} +1 -1
  23. package/dist/bundle/chunks/openrouter.js +2 -2
  24. package/dist/bundle/chunks/{pi-messages-SLF355GF.js → pi-messages-E6RRYBO7.js} +1 -1
  25. package/dist/bundle/chunks/radius.js +2 -2
  26. package/dist/bundle/chunks/{virtual-modules-O72ICTMQ.js → virtual-modules-3IZ4MYSQ.js} +1 -1
  27. package/dist/bundle/cli.js +1 -1
  28. package/dist/bundle/index.js +1 -1
  29. package/dist/bundle/rpc-entry.js +1 -1
  30. package/dist/capabilities/builtin.d.ts +4 -2
  31. package/dist/capabilities/builtin.d.ts.map +1 -1
  32. package/dist/capabilities/builtin.js +5 -1
  33. package/dist/capabilities/builtin.js.map +1 -1
  34. package/dist/cli/plugin-command.d.ts.map +1 -1
  35. package/dist/cli/plugin-command.js +51 -5
  36. package/dist/cli/plugin-command.js.map +1 -1
  37. package/dist/core/agent-session-compaction.d.ts +2 -2
  38. package/dist/core/agent-session-compaction.d.ts.map +1 -1
  39. package/dist/core/agent-session-compaction.js +54 -6
  40. package/dist/core/agent-session-compaction.js.map +1 -1
  41. package/dist/core/agent-session-internal.d.ts +2 -4
  42. package/dist/core/agent-session-internal.d.ts.map +1 -1
  43. package/dist/core/agent-session-internal.js.map +1 -1
  44. package/dist/core/agent-session-paths.d.ts +2 -0
  45. package/dist/core/agent-session-paths.d.ts.map +1 -1
  46. package/dist/core/agent-session-paths.js +2 -1
  47. package/dist/core/agent-session-paths.js.map +1 -1
  48. package/dist/core/agent-session-projection.d.ts +10 -4
  49. package/dist/core/agent-session-projection.d.ts.map +1 -1
  50. package/dist/core/agent-session-projection.js +17 -4
  51. package/dist/core/agent-session-projection.js.map +1 -1
  52. package/dist/core/agent-session.d.ts +19 -5
  53. package/dist/core/agent-session.d.ts.map +1 -1
  54. package/dist/core/agent-session.js +30 -4
  55. package/dist/core/agent-session.js.map +1 -1
  56. package/dist/core/extensions/bootstrap-compaction-policy.d.ts.map +1 -1
  57. package/dist/core/extensions/bootstrap-compaction-policy.js +1 -2
  58. package/dist/core/extensions/bootstrap-compaction-policy.js.map +1 -1
  59. package/dist/core/extensions/bootstrap-lifecycle.d.ts +1 -1
  60. package/dist/core/extensions/bootstrap-lifecycle.d.ts.map +1 -1
  61. package/dist/core/extensions/bootstrap-lifecycle.js +5 -2
  62. package/dist/core/extensions/bootstrap-lifecycle.js.map +1 -1
  63. package/dist/core/extensions/capability-runtime-store.d.ts.map +1 -1
  64. package/dist/core/extensions/capability-runtime-store.js +1 -0
  65. package/dist/core/extensions/capability-runtime-store.js.map +1 -1
  66. package/dist/core/extensions/capability-runtime-types.d.ts +16 -1
  67. package/dist/core/extensions/capability-runtime-types.d.ts.map +1 -1
  68. package/dist/core/extensions/capability-runtime-types.js.map +1 -1
  69. package/dist/core/extensions/capability-runtime.d.ts.map +1 -1
  70. package/dist/core/extensions/capability-runtime.js +38 -2
  71. package/dist/core/extensions/capability-runtime.js.map +1 -1
  72. package/dist/core/extensions/index.d.ts +1 -1
  73. package/dist/core/extensions/index.d.ts.map +1 -1
  74. package/dist/core/extensions/index.js.map +1 -1
  75. package/dist/core/extensions/lifecycle-catalog.d.ts +46 -0
  76. package/dist/core/extensions/lifecycle-catalog.d.ts.map +1 -1
  77. package/dist/core/extensions/lifecycle-catalog.js +101 -0
  78. package/dist/core/extensions/lifecycle-catalog.js.map +1 -1
  79. package/dist/core/extensions/microcompact.d.ts +16 -8
  80. package/dist/core/extensions/microcompact.d.ts.map +1 -1
  81. package/dist/core/extensions/microcompact.js +9 -7
  82. package/dist/core/extensions/microcompact.js.map +1 -1
  83. package/dist/core/extensions/provider-failover.d.ts +106 -0
  84. package/dist/core/extensions/provider-failover.d.ts.map +1 -0
  85. package/dist/core/extensions/provider-failover.js +692 -0
  86. package/dist/core/extensions/provider-failover.js.map +1 -0
  87. package/dist/core/facade/coding-agent-facade.d.ts +13 -6
  88. package/dist/core/facade/coding-agent-facade.d.ts.map +1 -1
  89. package/dist/core/facade/coding-agent-facade.js +3 -1
  90. package/dist/core/facade/coding-agent-facade.js.map +1 -1
  91. package/dist/core/keybindings.d.ts +6 -6
  92. package/dist/core/keybindings.d.ts.map +1 -1
  93. package/dist/core/keybindings.js +23 -6
  94. package/dist/core/keybindings.js.map +1 -1
  95. package/dist/core/model-config.d.ts +102 -0
  96. package/dist/core/model-config.d.ts.map +1 -1
  97. package/dist/core/model-config.js +46 -14
  98. package/dist/core/model-config.js.map +1 -1
  99. package/dist/core/model-resolver.d.ts.map +1 -1
  100. package/dist/core/model-resolver.js +1 -1
  101. package/dist/core/model-resolver.js.map +1 -1
  102. package/dist/core/model-runtime.d.ts +15 -0
  103. package/dist/core/model-runtime.d.ts.map +1 -1
  104. package/dist/core/model-runtime.js +30 -18
  105. package/dist/core/model-runtime.js.map +1 -1
  106. package/dist/core/remote-catalog-provider.d.ts.map +1 -1
  107. package/dist/core/remote-catalog-provider.js +4 -9
  108. package/dist/core/remote-catalog-provider.js.map +1 -1
  109. package/dist/core/sdk.d.ts.map +1 -1
  110. package/dist/core/sdk.js +107 -24
  111. package/dist/core/sdk.js.map +1 -1
  112. package/dist/core/session-manager.d.ts +2 -0
  113. package/dist/core/session-manager.d.ts.map +1 -1
  114. package/dist/core/session-manager.js +10 -5
  115. package/dist/core/session-manager.js.map +1 -1
  116. package/dist/core/settings-manager.d.ts +74 -5
  117. package/dist/core/settings-manager.d.ts.map +1 -1
  118. package/dist/core/settings-manager.js +143 -10
  119. package/dist/core/settings-manager.js.map +1 -1
  120. package/dist/core/slash-commands.d.ts.map +1 -1
  121. package/dist/core/slash-commands.js +17 -2
  122. package/dist/core/slash-commands.js.map +1 -1
  123. package/dist/core/tools/bash.d.ts.map +1 -1
  124. package/dist/core/tools/bash.js +9 -10
  125. package/dist/core/tools/bash.js.map +1 -1
  126. package/dist/core/tools/render-utils.d.ts +6 -0
  127. package/dist/core/tools/render-utils.d.ts.map +1 -1
  128. package/dist/core/tools/render-utils.js +26 -0
  129. package/dist/core/tools/render-utils.js.map +1 -1
  130. package/dist/core/tools/tool-definition.d.ts +21 -2
  131. package/dist/core/tools/tool-definition.d.ts.map +1 -1
  132. package/dist/core/tools/tool-definition.js.map +1 -1
  133. package/dist/main.d.ts.map +1 -1
  134. package/dist/main.js +3 -6
  135. package/dist/main.js.map +1 -1
  136. package/dist/modes/interactive/components/agents-view.d.ts +57 -0
  137. package/dist/modes/interactive/components/agents-view.d.ts.map +1 -0
  138. package/dist/modes/interactive/components/agents-view.js +260 -0
  139. package/dist/modes/interactive/components/agents-view.js.map +1 -0
  140. package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -1
  141. package/dist/modes/interactive/components/custom-editor.js +2 -2
  142. package/dist/modes/interactive/components/custom-editor.js.map +1 -1
  143. package/dist/modes/interactive/components/footer.d.ts +15 -0
  144. package/dist/modes/interactive/components/footer.d.ts.map +1 -1
  145. package/dist/modes/interactive/components/footer.js +90 -9
  146. package/dist/modes/interactive/components/footer.js.map +1 -1
  147. package/dist/modes/interactive/components/scoped-models-selector.d.ts +1 -1
  148. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
  149. package/dist/modes/interactive/components/scoped-models-selector.js +1 -1
  150. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
  151. package/dist/modes/interactive/components/settings-selector.d.ts +3 -1
  152. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  153. package/dist/modes/interactive/components/settings-selector.js +12 -2
  154. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  155. package/dist/modes/interactive/components/thinking-selector.d.ts.map +1 -1
  156. package/dist/modes/interactive/components/thinking-selector.js +1 -1
  157. package/dist/modes/interactive/components/thinking-selector.js.map +1 -1
  158. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  159. package/dist/modes/interactive/components/tool-execution.js +89 -10
  160. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  161. package/dist/modes/interactive/components/user-message-selector.d.ts +3 -0
  162. package/dist/modes/interactive/components/user-message-selector.d.ts.map +1 -1
  163. package/dist/modes/interactive/components/user-message-selector.js +9 -3
  164. package/dist/modes/interactive/components/user-message-selector.js.map +1 -1
  165. package/dist/modes/interactive/components/workflow-view.d.ts +119 -0
  166. package/dist/modes/interactive/components/workflow-view.d.ts.map +1 -0
  167. package/dist/modes/interactive/components/workflow-view.js +638 -0
  168. package/dist/modes/interactive/components/workflow-view.js.map +1 -0
  169. package/dist/modes/interactive/interactive-commands.d.ts +22 -0
  170. package/dist/modes/interactive/interactive-commands.d.ts.map +1 -1
  171. package/dist/modes/interactive/interactive-commands.js +56 -0
  172. package/dist/modes/interactive/interactive-commands.js.map +1 -1
  173. package/dist/modes/interactive/interactive-mode.d.ts +51 -1
  174. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  175. package/dist/modes/interactive/interactive-mode.js +218 -37
  176. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  177. package/dist/modes/interactive/interactive-selectors.d.ts +2 -4
  178. package/dist/modes/interactive/interactive-selectors.d.ts.map +1 -1
  179. package/dist/modes/interactive/interactive-selectors.js +10 -1
  180. package/dist/modes/interactive/interactive-selectors.js.map +1 -1
  181. package/dist/modes/print-mode.d.ts.map +1 -1
  182. package/dist/modes/print-mode.js +5 -0
  183. package/dist/modes/print-mode.js.map +1 -1
  184. package/dist/modes/rpc/rpc-client.d.ts +3 -1
  185. package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  186. package/dist/modes/rpc/rpc-client.js +4 -2
  187. package/dist/modes/rpc/rpc-client.js.map +1 -1
  188. package/dist/modes/rpc/rpc-types.d.ts +1 -0
  189. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  190. package/dist/modes/rpc/rpc-types.js.map +1 -1
  191. package/dist/plugins/ecosystem/bootstrap-factory-manifest.d.ts.map +1 -1
  192. package/dist/plugins/ecosystem/bootstrap-factory-manifest.js +7 -0
  193. package/dist/plugins/ecosystem/bootstrap-factory-manifest.js.map +1 -1
  194. package/dist/plugins/ecosystem/cli.d.ts.map +1 -1
  195. package/dist/plugins/ecosystem/cli.js +1 -1
  196. package/dist/plugins/ecosystem/cli.js.map +1 -1
  197. package/dist/plugins/ecosystem/install-store-paths.d.ts +15 -0
  198. package/dist/plugins/ecosystem/install-store-paths.d.ts.map +1 -0
  199. package/dist/plugins/ecosystem/install-store-paths.js +58 -0
  200. package/dist/plugins/ecosystem/install-store-paths.js.map +1 -0
  201. package/dist/plugins/ecosystem/node-disk-registry-source.d.ts.map +1 -1
  202. package/dist/plugins/ecosystem/node-disk-registry-source.js +3 -8
  203. package/dist/plugins/ecosystem/node-disk-registry-source.js.map +1 -1
  204. package/dist/plugins/ecosystem/node-package-install-host.d.ts.map +1 -1
  205. package/dist/plugins/ecosystem/node-package-install-host.js +9 -1
  206. package/dist/plugins/ecosystem/node-package-install-host.js.map +1 -1
  207. package/dist/plugins/ecosystem/node-package-install-persistence.d.ts.map +1 -1
  208. package/dist/plugins/ecosystem/node-package-install-persistence.js +3 -8
  209. package/dist/plugins/ecosystem/node-package-install-persistence.js.map +1 -1
  210. package/dist/profile/plugin-profiles.d.ts +2 -0
  211. package/dist/profile/plugin-profiles.d.ts.map +1 -1
  212. package/dist/profile/plugin-profiles.js +4 -0
  213. package/dist/profile/plugin-profiles.js.map +1 -1
  214. package/dist/profiles/builtin-suites.d.ts +19 -2
  215. package/dist/profiles/builtin-suites.d.ts.map +1 -1
  216. package/dist/profiles/builtin-suites.js +20 -2
  217. package/dist/profiles/builtin-suites.js.map +1 -1
  218. package/dist/profiles/suite-loader.d.ts.map +1 -1
  219. package/dist/profiles/suite-loader.js +4 -1
  220. package/dist/profiles/suite-loader.js.map +1 -1
  221. package/dist/utils/clipboard.d.ts.map +1 -1
  222. package/dist/utils/clipboard.js +10 -10
  223. package/dist/utils/clipboard.js.map +1 -1
  224. package/dist/utils/shell.d.ts +1 -3
  225. package/dist/utils/shell.d.ts.map +1 -1
  226. package/dist/utils/shell.js +3 -30
  227. package/dist/utils/shell.js.map +1 -1
  228. package/docs/keybindings.md +8 -8
  229. package/docs/models.md +1 -1
  230. package/docs/rpc.md +3 -1
  231. package/docs/settings.md +2 -1
  232. package/package.json +4 -4
  233. package/dist/bundle/chunks/anthropic-messages-EVFQ6MLR.js +0 -6
  234. package/dist/bundle/chunks/chunk-F6SBZUJG.js +0 -141
  235. package/dist/bundle/chunks/chunk-NUHFSC37.js +0 -25
  236. package/dist/bundle/chunks/google-generative-ai-ISTTUZMW.js +0 -2
  237. package/dist/bundle/chunks/google-vertex-NS7ZF7DK.js +0 -2
  238. package/dist/bundle/chunks/mistral-conversations-GINO6Z4I.js +0 -5
@@ -1 +1 @@
1
- {"version":3,"file":"node-disk-registry-source.d.ts","sourceRoot":"","sources":["../../../src/plugins/ecosystem/node-disk-registry-source.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAoC,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAG9F,QAAA,MAAM,oCAAoC,GAAa,CAAC;AACxD,oEAAoE;AACpE,eAAO,MAAM,qCAAqC,QAAc,CAAC;AACjE,+EAA+E;AAC/E,eAAO,MAAM,wCAAwC,QAAY,CAAC;AAClE,6FAA6F;AAC7F,eAAO,MAAM,iCAAiC,MAAM,CAAC;AAOrD,MAAM,MAAM,iCAAiC,GAC1C,WAAW,GACX,uBAAuB,GACvB,eAAe,GACf,kBAAkB,GAClB,mBAAmB,GACnB,kBAAkB,GAClB,WAAW,CAAC;AAEf;;;GAGG;AACH,qBAAa,6BAA8B,SAAQ,KAAK;IACvD,QAAQ,CAAC,IAAI,EAAE,iCAAiC,CAAC;IAEjD,YAAY,IAAI,EAAE,iCAAiC,EAAE,OAAO,EAAE,MAAM,EAInE;CACD;AAED,MAAM,WAAW,+BAA+B;IAC/C,0FAA0F;IAC1F,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,oCAAoC;IACpD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC9B;AAED,MAAM,WAAW,wBAAyB,SAAQ,sBAAsB;IACvE,QAAQ,CAAC,OAAO,EAAE,OAAO,oCAAoC,CAAC;IAC9D;;;;;;OAMG;IACH,QAAQ,CAAC,6BAA6B,CAAC,EAAE,CACxC,KAAK,EAAE,oCAAoC,KACvC,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;CACjC;AA6JD;;;;;;GAMG;AACH,wBAAgB,oCAAoC,CACnD,OAAO,EAAE,+BAA+B,GACtC,wBAAwB,CAkD1B","sourcesContent":["import { open } from \"node:fs/promises\";\nimport { isAbsolute, join, relative } from \"node:path\";\nimport type { PluginRegistryManifestEnvelopeV1, PluginRegistrySourceV1 } from \"./registry.ts\";\nimport type { PluginManifestV1 } from \"./types.ts\";\n\nconst NODE_DISK_REGISTRY_SOURCE_VERSION_V1 = 1 as const;\n/** Maximum index.v1.json bytes accepted by this source boundary. */\nexport const MAX_NODE_DISK_REGISTRY_INDEX_BYTES_V1 = 1024 * 1024;\n/** Maximum per-installation agent-forge.json bytes accepted by this source. */\nexport const MAX_NODE_DISK_REGISTRY_MANIFEST_BYTES_V1 = 64 * 1024;\n/** Maximum index records projected by one list() call; matches the registry manifest cap. */\nexport const MAX_NODE_DISK_REGISTRY_RECORDS_V1 = 256;\n\nconst INDEX_FILE_V1 = \"index.v1.json\";\nconst ECOSYSTEM_MANIFEST_FILE_V1 = \"agent-forge.json\";\nconst CAPABILITY_MANIFEST_FILE_V1 = \"plugin.json\";\nconst NODE_PACKAGE_FORMAT_VERSION_V1 = 1;\n\nexport type NodeDiskRegistrySourceErrorCodeV1 =\n\t| \"cancelled\"\n\t| \"invalid_configuration\"\n\t| \"invalid_index\"\n\t| \"invalid_manifest\"\n\t| \"identity_mismatch\"\n\t| \"too_many_records\"\n\t| \"io_failed\";\n\n/**\n * Deliberately does not retain filesystem exceptions or file contents: callers\n * must not turn registry diagnostics into a path or package disclosure.\n */\nexport class NodeDiskRegistrySourceErrorV1 extends Error {\n\treadonly code: NodeDiskRegistrySourceErrorCodeV1;\n\n\tconstructor(code: NodeDiskRegistrySourceErrorCodeV1, message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"NodeDiskRegistrySourceErrorV1\";\n\t\tthis.code = code;\n\t}\n}\n\nexport interface NodeDiskRegistrySourceOptionsV1 {\n\t/** Absolute host-owned default install store directory. It is never returned in a DTO. */\n\treadonly rootDirectory: string;\n}\n\nexport interface NodeDiskRegistrySourceResolveInputV1 {\n\treadonly pluginId: string;\n\treadonly pluginVersion: string;\n\treadonly signal?: AbortSignal;\n}\n\nexport interface NodeDiskRegistrySourceV1 extends PluginRegistrySourceV1 {\n\treadonly version: typeof NODE_DISK_REGISTRY_SOURCE_VERSION_V1;\n\t/**\n\t * Internal host channel (D-075 S2): absolute `plugin.json` path of the\n\t * installation directory matching `{pluginId, pluginVersion}` in the store\n\t * index, for the sdk's installed-plugin code loading. Registry snapshots\n\t * still carry no paths; undefined means no matching record or the manifest\n\t * file is absent from the installation directory.\n\t */\n\treadonly resolveCapabilityManifestPath?: (\n\t\tinput: NodeDiskRegistrySourceResolveInputV1,\n\t) => Promise<string | undefined>;\n}\n\ninterface DiskIndexRecordV1 {\n\treadonly installationRef: string;\n\treadonly directory: string;\n\treadonly pluginId: string;\n\treadonly pluginVersion: string;\n\treadonly manifestDigest: string;\n\treadonly scope: \"global\" | \"project\";\n\treadonly scopeRef: string;\n\treadonly state: \"installed\" | \"active\" | \"retained\";\n}\n\nconst INDEX_KEYS_V1 = [\n\t\"installationRef\",\n\t\"directory\",\n\t\"pluginId\",\n\t\"pluginVersion\",\n\t\"manifestDigest\",\n\t\"scope\",\n\t\"scopeRef\",\n\t\"state\",\n] as const;\n\nfunction opaqueField(value: unknown, max: number): value is string {\n\treturn (\n\t\ttypeof value === \"string\" &&\n\t\tvalue.length > 0 &&\n\t\tvalue.length <= max &&\n\t\t!/[\\u0000-\\u001f\\u007f]/u.test(value) &&\n\t\t!/\\s/u.test(value) &&\n\t\t!/^(?:file:|[a-z]:[\\\\/]|[\\\\/]{1,2}|\\\\\\\\)/iu.test(value)\n\t);\n}\n\nfunction boundedField(value: unknown, max: number): value is string {\n\treturn typeof value === \"string\" && value.length > 0 && value.length <= max && !/[\\u0000-\\u001f\\u007f]/u.test(value);\n}\n\nfunction withinRoot(root: string, value: string): boolean {\n\tif (!isAbsolute(value)) return false;\n\tconst relativePath = relative(root, value);\n\treturn relativePath !== \"\" && !relativePath.startsWith(\"..\") && !isAbsolute(relativePath);\n}\n\nfunction validateRecord(root: string, value: unknown, index: number): DiskIndexRecordV1 {\n\tif (value === null || typeof value !== \"object\" || Array.isArray(value))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", `Index record ${index} is not an object`);\n\tconst record = value as Record<string, unknown>;\n\tconst keys = INDEX_KEYS_V1;\n\tif (Object.keys(record).length !== keys.length || keys.some((key) => !(key in record)))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", `Index record ${index} has an invalid schema`);\n\tif (\n\t\t!opaqueField(record.installationRef, 2048) ||\n\t\t!boundedField(record.directory, 2048) ||\n\t\t!boundedField(record.pluginId, 256) ||\n\t\t!boundedField(record.pluginVersion, 256) ||\n\t\t!boundedField(record.manifestDigest, 512) ||\n\t\t!opaqueField(record.scopeRef, 2048) ||\n\t\t(record.scope !== \"global\" && record.scope !== \"project\") ||\n\t\t(record.state !== \"installed\" && record.state !== \"active\" && record.state !== \"retained\") ||\n\t\t!withinRoot(root, record.directory)\n\t)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", `Index record ${index} is invalid`);\n\treturn {\n\t\tinstallationRef: record.installationRef,\n\t\tdirectory: record.directory,\n\t\tpluginId: record.pluginId,\n\t\tpluginVersion: record.pluginVersion,\n\t\tmanifestDigest: record.manifestDigest,\n\t\tscope: record.scope,\n\t\tscopeRef: record.scopeRef,\n\t\tstate: record.state,\n\t};\n}\n\nfunction parseIndex(root: string, raw: string): readonly DiskIndexRecordV1[] {\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(raw);\n\t} catch {\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", \"Install store index JSON is invalid\");\n\t}\n\tif (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", \"Install store index is not an object\");\n\tconst value = parsed as Record<string, unknown>;\n\tif (\n\t\tObject.keys(value).length !== 2 ||\n\t\tvalue.version !== NODE_DISK_REGISTRY_SOURCE_VERSION_V1 ||\n\t\t!Array.isArray(value.records)\n\t)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", \"Install store index schema is unsupported\");\n\tif (value.records.length > MAX_NODE_DISK_REGISTRY_RECORDS_V1)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"too_many_records\", \"Install store index exceeds the record limit\");\n\treturn value.records.map((record, index) => validateRecord(root, record, index));\n}\n\n/** Read a bounded UTF-8 file; the absolute path never appears in a thrown message. */\nasync function readBoundedFile(path: string, maxBytes: number, label: string): Promise<string> {\n\tconst handle = await open(path, \"r\");\n\ttry {\n\t\tconst stat = await handle.stat();\n\t\tif (!stat.isFile() || stat.size > maxBytes)\n\t\t\tthrow new NodeDiskRegistrySourceErrorV1(\n\t\t\t\tlabel === \"index\" ? \"invalid_index\" : \"invalid_manifest\",\n\t\t\t\t`${label} exceeds the size limit`,\n\t\t\t);\n\t\treturn await handle.readFile(\"utf8\");\n\t} finally {\n\t\tawait handle.close().catch(() => undefined);\n\t}\n}\n\nfunction parseManifest(record: DiskIndexRecordV1, raw: string): PluginManifestV1 {\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(raw);\n\t} catch {\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_manifest\", \"Installed plugin manifest JSON is invalid\");\n\t}\n\tif (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_manifest\", \"Installed plugin manifest is not an object\");\n\tconst value = { ...(parsed as Record<string, unknown>) };\n\tif (value.packageFormatVersion !== undefined && value.packageFormatVersion !== NODE_PACKAGE_FORMAT_VERSION_V1)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_manifest\", \"Installed plugin package format is unsupported\");\n\t// The package envelope version is an install-host field; it is not part of\n\t// PluginManifestV1 and the stable manifest digest excludes it.\n\tdelete value.packageFormatVersion;\n\tif (value.id !== record.pluginId || value.pluginVersion !== record.pluginVersion)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\n\t\t\t\"identity_mismatch\",\n\t\t\t`Installed plugin manifest identity does not match index record for ${record.pluginId}@${record.pluginVersion}`,\n\t\t);\n\treturn value as unknown as PluginManifestV1;\n}\n\n/** Read the install store index records; undefined when the store is empty. */\nasync function readIndexRecords(\n\trootDirectory: string,\n\tsignal: AbortSignal | undefined,\n): Promise<readonly DiskIndexRecordV1[] | undefined> {\n\tif (signal?.aborted) throw new NodeDiskRegistrySourceErrorV1(\"cancelled\", \"Disk registry source read was cancelled\");\n\tlet rawIndex: string;\n\ttry {\n\t\trawIndex = await readBoundedFile(\n\t\t\tjoin(rootDirectory, INDEX_FILE_V1),\n\t\t\tMAX_NODE_DISK_REGISTRY_INDEX_BYTES_V1,\n\t\t\t\"index\",\n\t\t);\n\t} catch (error) {\n\t\tif (error instanceof NodeDiskRegistrySourceErrorV1) throw error;\n\t\tif (error instanceof Error && \"code\" in error && error.code === \"ENOENT\") return undefined;\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"io_failed\", \"Install store index could not be read\");\n\t}\n\treturn parseIndex(rootDirectory, rawIndex);\n}\n\n/**\n * Read-only registry source over the default install store. It projects the\n * persistent index and each installation instance's ecosystem manifest as\n * bounded envelopes for the registry's own digest verification. It never\n * writes the index or installation directories, and installation directories\n * stay inside the host-owned store.\n */\nexport function createNodeDiskPluginRegistrySourceV1(\n\toptions: NodeDiskRegistrySourceOptionsV1,\n): NodeDiskRegistrySourceV1 {\n\tconst rootDirectory = options.rootDirectory;\n\tif (typeof rootDirectory !== \"string\" || !isAbsolute(rootDirectory) || rootDirectory.trim() === \"\")\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_configuration\", \"Install store root directory must be absolute\");\n\treturn {\n\t\tversion: NODE_DISK_REGISTRY_SOURCE_VERSION_V1,\n\t\tlist: async ({ signal } = {}) => {\n\t\t\tconst records = await readIndexRecords(rootDirectory, signal);\n\t\t\tif (records === undefined) return Object.freeze([]);\n\t\t\tconst envelopes: PluginRegistryManifestEnvelopeV1[] = [];\n\t\t\tfor (const record of records) {\n\t\t\t\tif (signal?.aborted)\n\t\t\t\t\tthrow new NodeDiskRegistrySourceErrorV1(\"cancelled\", \"Disk registry source read was cancelled\");\n\t\t\t\tlet rawManifest: string;\n\t\t\t\ttry {\n\t\t\t\t\trawManifest = await readBoundedFile(\n\t\t\t\t\t\tjoin(record.directory, ECOSYSTEM_MANIFEST_FILE_V1),\n\t\t\t\t\t\tMAX_NODE_DISK_REGISTRY_MANIFEST_BYTES_V1,\n\t\t\t\t\t\t\"manifest\",\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (error instanceof NodeDiskRegistrySourceErrorV1) throw error;\n\t\t\t\t\tthrow new NodeDiskRegistrySourceErrorV1(\"io_failed\", \"Installed plugin manifest could not be read\");\n\t\t\t\t}\n\t\t\t\tconst manifest = parseManifest(record, rawManifest);\n\t\t\t\tenvelopes.push({\n\t\t\t\t\tmanifest,\n\t\t\t\t\tsource: record.installationRef,\n\t\t\t\t\tmanifestDigest: record.manifestDigest,\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn Object.freeze(envelopes);\n\t\t},\n\t\tresolveCapabilityManifestPath: async ({ pluginId, pluginVersion, signal }) => {\n\t\t\tconst records = await readIndexRecords(rootDirectory, signal);\n\t\t\tif (records === undefined) return undefined;\n\t\t\tconst record = records.find(\n\t\t\t\t(candidate) => candidate.pluginId === pluginId && candidate.pluginVersion === pluginVersion,\n\t\t\t);\n\t\t\tif (record === undefined) return undefined;\n\t\t\tconst manifestPath = join(record.directory, CAPABILITY_MANIFEST_FILE_V1);\n\t\t\ttry {\n\t\t\t\tconst handle = await open(manifestPath, \"r\");\n\t\t\t\tawait handle.close().catch(() => undefined);\n\t\t\t} catch {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\treturn manifestPath;\n\t\t},\n\t};\n}\n"]}
1
+ {"version":3,"file":"node-disk-registry-source.d.ts","sourceRoot":"","sources":["../../../src/plugins/ecosystem/node-disk-registry-source.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAoC,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAG9F,QAAA,MAAM,oCAAoC,GAAa,CAAC;AACxD,oEAAoE;AACpE,eAAO,MAAM,qCAAqC,QAAc,CAAC;AACjE,+EAA+E;AAC/E,eAAO,MAAM,wCAAwC,QAAY,CAAC;AAClE,6FAA6F;AAC7F,eAAO,MAAM,iCAAiC,MAAM,CAAC;AAOrD,MAAM,MAAM,iCAAiC,GAC1C,WAAW,GACX,uBAAuB,GACvB,eAAe,GACf,kBAAkB,GAClB,mBAAmB,GACnB,kBAAkB,GAClB,WAAW,CAAC;AAEf;;;GAGG;AACH,qBAAa,6BAA8B,SAAQ,KAAK;IACvD,QAAQ,CAAC,IAAI,EAAE,iCAAiC,CAAC;IAEjD,YAAY,IAAI,EAAE,iCAAiC,EAAE,OAAO,EAAE,MAAM,EAInE;CACD;AAED,MAAM,WAAW,+BAA+B;IAC/C,0FAA0F;IAC1F,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,oCAAoC;IACpD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC9B;AAED,MAAM,WAAW,wBAAyB,SAAQ,sBAAsB;IACvE,QAAQ,CAAC,OAAO,EAAE,OAAO,oCAAoC,CAAC;IAC9D;;;;;;OAMG;IACH,QAAQ,CAAC,6BAA6B,CAAC,EAAE,CACxC,KAAK,EAAE,oCAAoC,KACvC,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;CACjC;AAuJD;;;;;;GAMG;AACH,wBAAgB,oCAAoC,CACnD,OAAO,EAAE,+BAA+B,GACtC,wBAAwB,CAkD1B","sourcesContent":["import { open } from \"node:fs/promises\";\nimport { isAbsolute, join } from \"node:path\";\nimport { withinInstallStoreRoot } from \"./install-store-paths.ts\";\nimport type { PluginRegistryManifestEnvelopeV1, PluginRegistrySourceV1 } from \"./registry.ts\";\nimport type { PluginManifestV1 } from \"./types.ts\";\n\nconst NODE_DISK_REGISTRY_SOURCE_VERSION_V1 = 1 as const;\n/** Maximum index.v1.json bytes accepted by this source boundary. */\nexport const MAX_NODE_DISK_REGISTRY_INDEX_BYTES_V1 = 1024 * 1024;\n/** Maximum per-installation agent-forge.json bytes accepted by this source. */\nexport const MAX_NODE_DISK_REGISTRY_MANIFEST_BYTES_V1 = 64 * 1024;\n/** Maximum index records projected by one list() call; matches the registry manifest cap. */\nexport const MAX_NODE_DISK_REGISTRY_RECORDS_V1 = 256;\n\nconst INDEX_FILE_V1 = \"index.v1.json\";\nconst ECOSYSTEM_MANIFEST_FILE_V1 = \"agent-forge.json\";\nconst CAPABILITY_MANIFEST_FILE_V1 = \"plugin.json\";\nconst NODE_PACKAGE_FORMAT_VERSION_V1 = 1;\n\nexport type NodeDiskRegistrySourceErrorCodeV1 =\n\t| \"cancelled\"\n\t| \"invalid_configuration\"\n\t| \"invalid_index\"\n\t| \"invalid_manifest\"\n\t| \"identity_mismatch\"\n\t| \"too_many_records\"\n\t| \"io_failed\";\n\n/**\n * Deliberately does not retain filesystem exceptions or file contents: callers\n * must not turn registry diagnostics into a path or package disclosure.\n */\nexport class NodeDiskRegistrySourceErrorV1 extends Error {\n\treadonly code: NodeDiskRegistrySourceErrorCodeV1;\n\n\tconstructor(code: NodeDiskRegistrySourceErrorCodeV1, message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"NodeDiskRegistrySourceErrorV1\";\n\t\tthis.code = code;\n\t}\n}\n\nexport interface NodeDiskRegistrySourceOptionsV1 {\n\t/** Absolute host-owned default install store directory. It is never returned in a DTO. */\n\treadonly rootDirectory: string;\n}\n\nexport interface NodeDiskRegistrySourceResolveInputV1 {\n\treadonly pluginId: string;\n\treadonly pluginVersion: string;\n\treadonly signal?: AbortSignal;\n}\n\nexport interface NodeDiskRegistrySourceV1 extends PluginRegistrySourceV1 {\n\treadonly version: typeof NODE_DISK_REGISTRY_SOURCE_VERSION_V1;\n\t/**\n\t * Internal host channel (D-075 S2): absolute `plugin.json` path of the\n\t * installation directory matching `{pluginId, pluginVersion}` in the store\n\t * index, for the sdk's installed-plugin code loading. Registry snapshots\n\t * still carry no paths; undefined means no matching record or the manifest\n\t * file is absent from the installation directory.\n\t */\n\treadonly resolveCapabilityManifestPath?: (\n\t\tinput: NodeDiskRegistrySourceResolveInputV1,\n\t) => Promise<string | undefined>;\n}\n\ninterface DiskIndexRecordV1 {\n\treadonly installationRef: string;\n\treadonly directory: string;\n\treadonly pluginId: string;\n\treadonly pluginVersion: string;\n\treadonly manifestDigest: string;\n\treadonly scope: \"global\" | \"project\";\n\treadonly scopeRef: string;\n\treadonly state: \"installed\" | \"active\" | \"retained\";\n}\n\nconst INDEX_KEYS_V1 = [\n\t\"installationRef\",\n\t\"directory\",\n\t\"pluginId\",\n\t\"pluginVersion\",\n\t\"manifestDigest\",\n\t\"scope\",\n\t\"scopeRef\",\n\t\"state\",\n] as const;\n\nfunction opaqueField(value: unknown, max: number): value is string {\n\treturn (\n\t\ttypeof value === \"string\" &&\n\t\tvalue.length > 0 &&\n\t\tvalue.length <= max &&\n\t\t!/[\\u0000-\\u001f\\u007f]/u.test(value) &&\n\t\t!/\\s/u.test(value) &&\n\t\t!/^(?:file:|[a-z]:[\\\\/]|[\\\\/]{1,2}|\\\\\\\\)/iu.test(value)\n\t);\n}\n\nfunction boundedField(value: unknown, max: number): value is string {\n\treturn typeof value === \"string\" && value.length > 0 && value.length <= max && !/[\\u0000-\\u001f\\u007f]/u.test(value);\n}\n\nfunction validateRecord(root: string, value: unknown, index: number): DiskIndexRecordV1 {\n\tif (value === null || typeof value !== \"object\" || Array.isArray(value))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", `Index record ${index} is not an object`);\n\tconst record = value as Record<string, unknown>;\n\tconst keys = INDEX_KEYS_V1;\n\tif (Object.keys(record).length !== keys.length || keys.some((key) => !(key in record)))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", `Index record ${index} has an invalid schema`);\n\tif (\n\t\t!opaqueField(record.installationRef, 2048) ||\n\t\t!boundedField(record.directory, 2048) ||\n\t\t!boundedField(record.pluginId, 256) ||\n\t\t!boundedField(record.pluginVersion, 256) ||\n\t\t!boundedField(record.manifestDigest, 512) ||\n\t\t!opaqueField(record.scopeRef, 2048) ||\n\t\t(record.scope !== \"global\" && record.scope !== \"project\") ||\n\t\t(record.state !== \"installed\" && record.state !== \"active\" && record.state !== \"retained\") ||\n\t\t!withinInstallStoreRoot(root, record.directory)\n\t)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", `Index record ${index} is invalid`);\n\treturn {\n\t\tinstallationRef: record.installationRef,\n\t\tdirectory: record.directory,\n\t\tpluginId: record.pluginId,\n\t\tpluginVersion: record.pluginVersion,\n\t\tmanifestDigest: record.manifestDigest,\n\t\tscope: record.scope,\n\t\tscopeRef: record.scopeRef,\n\t\tstate: record.state,\n\t};\n}\n\nfunction parseIndex(root: string, raw: string): readonly DiskIndexRecordV1[] {\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(raw);\n\t} catch {\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", \"Install store index JSON is invalid\");\n\t}\n\tif (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", \"Install store index is not an object\");\n\tconst value = parsed as Record<string, unknown>;\n\tif (\n\t\tObject.keys(value).length !== 2 ||\n\t\tvalue.version !== NODE_DISK_REGISTRY_SOURCE_VERSION_V1 ||\n\t\t!Array.isArray(value.records)\n\t)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", \"Install store index schema is unsupported\");\n\tif (value.records.length > MAX_NODE_DISK_REGISTRY_RECORDS_V1)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"too_many_records\", \"Install store index exceeds the record limit\");\n\treturn value.records.map((record, index) => validateRecord(root, record, index));\n}\n\n/** Read a bounded UTF-8 file; the absolute path never appears in a thrown message. */\nasync function readBoundedFile(path: string, maxBytes: number, label: string): Promise<string> {\n\tconst handle = await open(path, \"r\");\n\ttry {\n\t\tconst stat = await handle.stat();\n\t\tif (!stat.isFile() || stat.size > maxBytes)\n\t\t\tthrow new NodeDiskRegistrySourceErrorV1(\n\t\t\t\tlabel === \"index\" ? \"invalid_index\" : \"invalid_manifest\",\n\t\t\t\t`${label} exceeds the size limit`,\n\t\t\t);\n\t\treturn await handle.readFile(\"utf8\");\n\t} finally {\n\t\tawait handle.close().catch(() => undefined);\n\t}\n}\n\nfunction parseManifest(record: DiskIndexRecordV1, raw: string): PluginManifestV1 {\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(raw);\n\t} catch {\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_manifest\", \"Installed plugin manifest JSON is invalid\");\n\t}\n\tif (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_manifest\", \"Installed plugin manifest is not an object\");\n\tconst value = { ...(parsed as Record<string, unknown>) };\n\tif (value.packageFormatVersion !== undefined && value.packageFormatVersion !== NODE_PACKAGE_FORMAT_VERSION_V1)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_manifest\", \"Installed plugin package format is unsupported\");\n\t// The package envelope version is an install-host field; it is not part of\n\t// PluginManifestV1 and the stable manifest digest excludes it.\n\tdelete value.packageFormatVersion;\n\tif (value.id !== record.pluginId || value.pluginVersion !== record.pluginVersion)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\n\t\t\t\"identity_mismatch\",\n\t\t\t`Installed plugin manifest identity does not match index record for ${record.pluginId}@${record.pluginVersion}`,\n\t\t);\n\treturn value as unknown as PluginManifestV1;\n}\n\n/** Read the install store index records; undefined when the store is empty. */\nasync function readIndexRecords(\n\trootDirectory: string,\n\tsignal: AbortSignal | undefined,\n): Promise<readonly DiskIndexRecordV1[] | undefined> {\n\tif (signal?.aborted) throw new NodeDiskRegistrySourceErrorV1(\"cancelled\", \"Disk registry source read was cancelled\");\n\tlet rawIndex: string;\n\ttry {\n\t\trawIndex = await readBoundedFile(\n\t\t\tjoin(rootDirectory, INDEX_FILE_V1),\n\t\t\tMAX_NODE_DISK_REGISTRY_INDEX_BYTES_V1,\n\t\t\t\"index\",\n\t\t);\n\t} catch (error) {\n\t\tif (error instanceof NodeDiskRegistrySourceErrorV1) throw error;\n\t\tif (error instanceof Error && \"code\" in error && error.code === \"ENOENT\") return undefined;\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"io_failed\", \"Install store index could not be read\");\n\t}\n\treturn parseIndex(rootDirectory, rawIndex);\n}\n\n/**\n * Read-only registry source over the default install store. It projects the\n * persistent index and each installation instance's ecosystem manifest as\n * bounded envelopes for the registry's own digest verification. It never\n * writes the index or installation directories, and installation directories\n * stay inside the host-owned store.\n */\nexport function createNodeDiskPluginRegistrySourceV1(\n\toptions: NodeDiskRegistrySourceOptionsV1,\n): NodeDiskRegistrySourceV1 {\n\tconst rootDirectory = options.rootDirectory;\n\tif (typeof rootDirectory !== \"string\" || !isAbsolute(rootDirectory) || rootDirectory.trim() === \"\")\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_configuration\", \"Install store root directory must be absolute\");\n\treturn {\n\t\tversion: NODE_DISK_REGISTRY_SOURCE_VERSION_V1,\n\t\tlist: async ({ signal } = {}) => {\n\t\t\tconst records = await readIndexRecords(rootDirectory, signal);\n\t\t\tif (records === undefined) return Object.freeze([]);\n\t\t\tconst envelopes: PluginRegistryManifestEnvelopeV1[] = [];\n\t\t\tfor (const record of records) {\n\t\t\t\tif (signal?.aborted)\n\t\t\t\t\tthrow new NodeDiskRegistrySourceErrorV1(\"cancelled\", \"Disk registry source read was cancelled\");\n\t\t\t\tlet rawManifest: string;\n\t\t\t\ttry {\n\t\t\t\t\trawManifest = await readBoundedFile(\n\t\t\t\t\t\tjoin(record.directory, ECOSYSTEM_MANIFEST_FILE_V1),\n\t\t\t\t\t\tMAX_NODE_DISK_REGISTRY_MANIFEST_BYTES_V1,\n\t\t\t\t\t\t\"manifest\",\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (error instanceof NodeDiskRegistrySourceErrorV1) throw error;\n\t\t\t\t\tthrow new NodeDiskRegistrySourceErrorV1(\"io_failed\", \"Installed plugin manifest could not be read\");\n\t\t\t\t}\n\t\t\t\tconst manifest = parseManifest(record, rawManifest);\n\t\t\t\tenvelopes.push({\n\t\t\t\t\tmanifest,\n\t\t\t\t\tsource: record.installationRef,\n\t\t\t\t\tmanifestDigest: record.manifestDigest,\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn Object.freeze(envelopes);\n\t\t},\n\t\tresolveCapabilityManifestPath: async ({ pluginId, pluginVersion, signal }) => {\n\t\t\tconst records = await readIndexRecords(rootDirectory, signal);\n\t\t\tif (records === undefined) return undefined;\n\t\t\tconst record = records.find(\n\t\t\t\t(candidate) => candidate.pluginId === pluginId && candidate.pluginVersion === pluginVersion,\n\t\t\t);\n\t\t\tif (record === undefined) return undefined;\n\t\t\tconst manifestPath = join(record.directory, CAPABILITY_MANIFEST_FILE_V1);\n\t\t\ttry {\n\t\t\t\tconst handle = await open(manifestPath, \"r\");\n\t\t\t\tawait handle.close().catch(() => undefined);\n\t\t\t} catch {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\treturn manifestPath;\n\t\t},\n\t};\n}\n"]}
@@ -1,5 +1,6 @@
1
1
  import { open } from "node:fs/promises";
2
- import { isAbsolute, join, relative } from "node:path";
2
+ import { isAbsolute, join } from "node:path";
3
+ import { withinInstallStoreRoot } from "./install-store-paths.js";
3
4
  const NODE_DISK_REGISTRY_SOURCE_VERSION_V1 = 1;
4
5
  /** Maximum index.v1.json bytes accepted by this source boundary. */
5
6
  export const MAX_NODE_DISK_REGISTRY_INDEX_BYTES_V1 = 1024 * 1024;
@@ -44,12 +45,6 @@ function opaqueField(value, max) {
44
45
  function boundedField(value, max) {
45
46
  return typeof value === "string" && value.length > 0 && value.length <= max && !/[\u0000-\u001f\u007f]/u.test(value);
46
47
  }
47
- function withinRoot(root, value) {
48
- if (!isAbsolute(value))
49
- return false;
50
- const relativePath = relative(root, value);
51
- return relativePath !== "" && !relativePath.startsWith("..") && !isAbsolute(relativePath);
52
- }
53
48
  function validateRecord(root, value, index) {
54
49
  if (value === null || typeof value !== "object" || Array.isArray(value))
55
50
  throw new NodeDiskRegistrySourceErrorV1("invalid_index", `Index record ${index} is not an object`);
@@ -65,7 +60,7 @@ function validateRecord(root, value, index) {
65
60
  !opaqueField(record.scopeRef, 2048) ||
66
61
  (record.scope !== "global" && record.scope !== "project") ||
67
62
  (record.state !== "installed" && record.state !== "active" && record.state !== "retained") ||
68
- !withinRoot(root, record.directory))
63
+ !withinInstallStoreRoot(root, record.directory))
69
64
  throw new NodeDiskRegistrySourceErrorV1("invalid_index", `Index record ${index} is invalid`);
70
65
  return {
71
66
  installationRef: record.installationRef,
@@ -1 +1 @@
1
- {"version":3,"file":"node-disk-registry-source.js","sourceRoot":"","sources":["../../../src/plugins/ecosystem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C,MAAM,CAAC,SAAS,EAAE,2BAA2B,CAAC,CAAC;YACzE,IAAI,CAAC;gBACJ,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,YAAY,EAAE,GAAG,CAAC,CAAC;gBAC7C,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;YAC7C,CAAC;YAAC,MAAM,CAAC;gBACR,OAAO,SAAS,CAAC;YAClB,CAAC;YACD,OAAO,YAAY,CAAC;QAAA,CACpB;KACD,CAAC;AAAA,CACF","sourcesContent":["import { open } from \"node:fs/promises\";\nimport { isAbsolute, join, relative } from \"node:path\";\nimport type { PluginRegistryManifestEnvelopeV1, PluginRegistrySourceV1 } from \"./registry.ts\";\nimport type { PluginManifestV1 } from \"./types.ts\";\n\nconst NODE_DISK_REGISTRY_SOURCE_VERSION_V1 = 1 as const;\n/** Maximum index.v1.json bytes accepted by this source boundary. */\nexport const MAX_NODE_DISK_REGISTRY_INDEX_BYTES_V1 = 1024 * 1024;\n/** Maximum per-installation agent-forge.json bytes accepted by this source. */\nexport const MAX_NODE_DISK_REGISTRY_MANIFEST_BYTES_V1 = 64 * 1024;\n/** Maximum index records projected by one list() call; matches the registry manifest cap. */\nexport const MAX_NODE_DISK_REGISTRY_RECORDS_V1 = 256;\n\nconst INDEX_FILE_V1 = \"index.v1.json\";\nconst ECOSYSTEM_MANIFEST_FILE_V1 = \"agent-forge.json\";\nconst CAPABILITY_MANIFEST_FILE_V1 = \"plugin.json\";\nconst NODE_PACKAGE_FORMAT_VERSION_V1 = 1;\n\nexport type NodeDiskRegistrySourceErrorCodeV1 =\n\t| \"cancelled\"\n\t| \"invalid_configuration\"\n\t| \"invalid_index\"\n\t| \"invalid_manifest\"\n\t| \"identity_mismatch\"\n\t| \"too_many_records\"\n\t| \"io_failed\";\n\n/**\n * Deliberately does not retain filesystem exceptions or file contents: callers\n * must not turn registry diagnostics into a path or package disclosure.\n */\nexport class NodeDiskRegistrySourceErrorV1 extends Error {\n\treadonly code: NodeDiskRegistrySourceErrorCodeV1;\n\n\tconstructor(code: NodeDiskRegistrySourceErrorCodeV1, message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"NodeDiskRegistrySourceErrorV1\";\n\t\tthis.code = code;\n\t}\n}\n\nexport interface NodeDiskRegistrySourceOptionsV1 {\n\t/** Absolute host-owned default install store directory. It is never returned in a DTO. */\n\treadonly rootDirectory: string;\n}\n\nexport interface NodeDiskRegistrySourceResolveInputV1 {\n\treadonly pluginId: string;\n\treadonly pluginVersion: string;\n\treadonly signal?: AbortSignal;\n}\n\nexport interface NodeDiskRegistrySourceV1 extends PluginRegistrySourceV1 {\n\treadonly version: typeof NODE_DISK_REGISTRY_SOURCE_VERSION_V1;\n\t/**\n\t * Internal host channel (D-075 S2): absolute `plugin.json` path of the\n\t * installation directory matching `{pluginId, pluginVersion}` in the store\n\t * index, for the sdk's installed-plugin code loading. Registry snapshots\n\t * still carry no paths; undefined means no matching record or the manifest\n\t * file is absent from the installation directory.\n\t */\n\treadonly resolveCapabilityManifestPath?: (\n\t\tinput: NodeDiskRegistrySourceResolveInputV1,\n\t) => Promise<string | undefined>;\n}\n\ninterface DiskIndexRecordV1 {\n\treadonly installationRef: string;\n\treadonly directory: string;\n\treadonly pluginId: string;\n\treadonly pluginVersion: string;\n\treadonly manifestDigest: string;\n\treadonly scope: \"global\" | \"project\";\n\treadonly scopeRef: string;\n\treadonly state: \"installed\" | \"active\" | \"retained\";\n}\n\nconst INDEX_KEYS_V1 = [\n\t\"installationRef\",\n\t\"directory\",\n\t\"pluginId\",\n\t\"pluginVersion\",\n\t\"manifestDigest\",\n\t\"scope\",\n\t\"scopeRef\",\n\t\"state\",\n] as const;\n\nfunction opaqueField(value: unknown, max: number): value is string {\n\treturn (\n\t\ttypeof value === \"string\" &&\n\t\tvalue.length > 0 &&\n\t\tvalue.length <= max &&\n\t\t!/[\\u0000-\\u001f\\u007f]/u.test(value) &&\n\t\t!/\\s/u.test(value) &&\n\t\t!/^(?:file:|[a-z]:[\\\\/]|[\\\\/]{1,2}|\\\\\\\\)/iu.test(value)\n\t);\n}\n\nfunction boundedField(value: unknown, max: number): value is string {\n\treturn typeof value === \"string\" && value.length > 0 && value.length <= max && !/[\\u0000-\\u001f\\u007f]/u.test(value);\n}\n\nfunction withinRoot(root: string, value: string): boolean {\n\tif (!isAbsolute(value)) return false;\n\tconst relativePath = relative(root, value);\n\treturn relativePath !== \"\" && !relativePath.startsWith(\"..\") && !isAbsolute(relativePath);\n}\n\nfunction validateRecord(root: string, value: unknown, index: number): DiskIndexRecordV1 {\n\tif (value === null || typeof value !== \"object\" || Array.isArray(value))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", `Index record ${index} is not an object`);\n\tconst record = value as Record<string, unknown>;\n\tconst keys = INDEX_KEYS_V1;\n\tif (Object.keys(record).length !== keys.length || keys.some((key) => !(key in record)))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", `Index record ${index} has an invalid schema`);\n\tif (\n\t\t!opaqueField(record.installationRef, 2048) ||\n\t\t!boundedField(record.directory, 2048) ||\n\t\t!boundedField(record.pluginId, 256) ||\n\t\t!boundedField(record.pluginVersion, 256) ||\n\t\t!boundedField(record.manifestDigest, 512) ||\n\t\t!opaqueField(record.scopeRef, 2048) ||\n\t\t(record.scope !== \"global\" && record.scope !== \"project\") ||\n\t\t(record.state !== \"installed\" && record.state !== \"active\" && record.state !== \"retained\") ||\n\t\t!withinRoot(root, record.directory)\n\t)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", `Index record ${index} is invalid`);\n\treturn {\n\t\tinstallationRef: record.installationRef,\n\t\tdirectory: record.directory,\n\t\tpluginId: record.pluginId,\n\t\tpluginVersion: record.pluginVersion,\n\t\tmanifestDigest: record.manifestDigest,\n\t\tscope: record.scope,\n\t\tscopeRef: record.scopeRef,\n\t\tstate: record.state,\n\t};\n}\n\nfunction parseIndex(root: string, raw: string): readonly DiskIndexRecordV1[] {\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(raw);\n\t} catch {\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", \"Install store index JSON is invalid\");\n\t}\n\tif (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", \"Install store index is not an object\");\n\tconst value = parsed as Record<string, unknown>;\n\tif (\n\t\tObject.keys(value).length !== 2 ||\n\t\tvalue.version !== NODE_DISK_REGISTRY_SOURCE_VERSION_V1 ||\n\t\t!Array.isArray(value.records)\n\t)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", \"Install store index schema is unsupported\");\n\tif (value.records.length > MAX_NODE_DISK_REGISTRY_RECORDS_V1)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"too_many_records\", \"Install store index exceeds the record limit\");\n\treturn value.records.map((record, index) => validateRecord(root, record, index));\n}\n\n/** Read a bounded UTF-8 file; the absolute path never appears in a thrown message. */\nasync function readBoundedFile(path: string, maxBytes: number, label: string): Promise<string> {\n\tconst handle = await open(path, \"r\");\n\ttry {\n\t\tconst stat = await handle.stat();\n\t\tif (!stat.isFile() || stat.size > maxBytes)\n\t\t\tthrow new NodeDiskRegistrySourceErrorV1(\n\t\t\t\tlabel === \"index\" ? \"invalid_index\" : \"invalid_manifest\",\n\t\t\t\t`${label} exceeds the size limit`,\n\t\t\t);\n\t\treturn await handle.readFile(\"utf8\");\n\t} finally {\n\t\tawait handle.close().catch(() => undefined);\n\t}\n}\n\nfunction parseManifest(record: DiskIndexRecordV1, raw: string): PluginManifestV1 {\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(raw);\n\t} catch {\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_manifest\", \"Installed plugin manifest JSON is invalid\");\n\t}\n\tif (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_manifest\", \"Installed plugin manifest is not an object\");\n\tconst value = { ...(parsed as Record<string, unknown>) };\n\tif (value.packageFormatVersion !== undefined && value.packageFormatVersion !== NODE_PACKAGE_FORMAT_VERSION_V1)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_manifest\", \"Installed plugin package format is unsupported\");\n\t// The package envelope version is an install-host field; it is not part of\n\t// PluginManifestV1 and the stable manifest digest excludes it.\n\tdelete value.packageFormatVersion;\n\tif (value.id !== record.pluginId || value.pluginVersion !== record.pluginVersion)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\n\t\t\t\"identity_mismatch\",\n\t\t\t`Installed plugin manifest identity does not match index record for ${record.pluginId}@${record.pluginVersion}`,\n\t\t);\n\treturn value as unknown as PluginManifestV1;\n}\n\n/** Read the install store index records; undefined when the store is empty. */\nasync function readIndexRecords(\n\trootDirectory: string,\n\tsignal: AbortSignal | undefined,\n): Promise<readonly DiskIndexRecordV1[] | undefined> {\n\tif (signal?.aborted) throw new NodeDiskRegistrySourceErrorV1(\"cancelled\", \"Disk registry source read was cancelled\");\n\tlet rawIndex: string;\n\ttry {\n\t\trawIndex = await readBoundedFile(\n\t\t\tjoin(rootDirectory, INDEX_FILE_V1),\n\t\t\tMAX_NODE_DISK_REGISTRY_INDEX_BYTES_V1,\n\t\t\t\"index\",\n\t\t);\n\t} catch (error) {\n\t\tif (error instanceof NodeDiskRegistrySourceErrorV1) throw error;\n\t\tif (error instanceof Error && \"code\" in error && error.code === \"ENOENT\") return undefined;\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"io_failed\", \"Install store index could not be read\");\n\t}\n\treturn parseIndex(rootDirectory, rawIndex);\n}\n\n/**\n * Read-only registry source over the default install store. It projects the\n * persistent index and each installation instance's ecosystem manifest as\n * bounded envelopes for the registry's own digest verification. It never\n * writes the index or installation directories, and installation directories\n * stay inside the host-owned store.\n */\nexport function createNodeDiskPluginRegistrySourceV1(\n\toptions: NodeDiskRegistrySourceOptionsV1,\n): NodeDiskRegistrySourceV1 {\n\tconst rootDirectory = options.rootDirectory;\n\tif (typeof rootDirectory !== \"string\" || !isAbsolute(rootDirectory) || rootDirectory.trim() === \"\")\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_configuration\", \"Install store root directory must be absolute\");\n\treturn {\n\t\tversion: NODE_DISK_REGISTRY_SOURCE_VERSION_V1,\n\t\tlist: async ({ signal } = {}) => {\n\t\t\tconst records = await readIndexRecords(rootDirectory, signal);\n\t\t\tif (records === undefined) return Object.freeze([]);\n\t\t\tconst envelopes: PluginRegistryManifestEnvelopeV1[] = [];\n\t\t\tfor (const record of records) {\n\t\t\t\tif (signal?.aborted)\n\t\t\t\t\tthrow new NodeDiskRegistrySourceErrorV1(\"cancelled\", \"Disk registry source read was cancelled\");\n\t\t\t\tlet rawManifest: string;\n\t\t\t\ttry {\n\t\t\t\t\trawManifest = await readBoundedFile(\n\t\t\t\t\t\tjoin(record.directory, ECOSYSTEM_MANIFEST_FILE_V1),\n\t\t\t\t\t\tMAX_NODE_DISK_REGISTRY_MANIFEST_BYTES_V1,\n\t\t\t\t\t\t\"manifest\",\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (error instanceof NodeDiskRegistrySourceErrorV1) throw error;\n\t\t\t\t\tthrow new NodeDiskRegistrySourceErrorV1(\"io_failed\", \"Installed plugin manifest could not be read\");\n\t\t\t\t}\n\t\t\t\tconst manifest = parseManifest(record, rawManifest);\n\t\t\t\tenvelopes.push({\n\t\t\t\t\tmanifest,\n\t\t\t\t\tsource: record.installationRef,\n\t\t\t\t\tmanifestDigest: record.manifestDigest,\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn Object.freeze(envelopes);\n\t\t},\n\t\tresolveCapabilityManifestPath: async ({ pluginId, pluginVersion, signal }) => {\n\t\t\tconst records = await readIndexRecords(rootDirectory, signal);\n\t\t\tif (records === undefined) return undefined;\n\t\t\tconst record = records.find(\n\t\t\t\t(candidate) => candidate.pluginId === pluginId && candidate.pluginVersion === pluginVersion,\n\t\t\t);\n\t\t\tif (record === undefined) return undefined;\n\t\t\tconst manifestPath = join(record.directory, CAPABILITY_MANIFEST_FILE_V1);\n\t\t\ttry {\n\t\t\t\tconst handle = await open(manifestPath, \"r\");\n\t\t\t\tawait handle.close().catch(() => undefined);\n\t\t\t} catch {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\treturn manifestPath;\n\t\t},\n\t};\n}\n"]}
1
+ {"version":3,"file":"node-disk-registry-source.js","sourceRoot":"","sources":["../../../src/plugins/ecosystem/node-disk-registry-source.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAIlE,MAAM,oCAAoC,GAAG,CAAU,CAAC;AACxD,oEAAoE;AACpE,MAAM,CAAC,MAAM,qCAAqC,GAAG,IAAI,GAAG,IAAI,CAAC;AACjE,+EAA+E;AAC/E,MAAM,CAAC,MAAM,wCAAwC,GAAG,EAAE,GAAG,IAAI,CAAC;AAClE,6FAA6F;AAC7F,MAAM,CAAC,MAAM,iCAAiC,GAAG,GAAG,CAAC;AAErD,MAAM,aAAa,GAAG,eAAe,CAAC;AACtC,MAAM,0BAA0B,GAAG,kBAAkB,CAAC;AACtD,MAAM,2BAA2B,GAAG,aAAa,CAAC;AAClD,MAAM,8BAA8B,GAAG,CAAC,CAAC;AAWzC;;;GAGG;AACH,MAAM,OAAO,6BAA8B,SAAQ,KAAK;IAC9C,IAAI,CAAoC;IAEjD,YAAY,IAAuC,EAAE,OAAe,EAAE;QACrE,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,+BAA+B,CAAC;QAC5C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IAAA,CAC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a,CAC3F,CAAC;YACF,IAAI,MAAM,KAAK,SAAS;gBAAE,OAAO,SAAS,CAAC;YAC3C,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,2BAA2B,CAAC,CAAC;YACzE,IAAI,CAAC;gBACJ,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,YAAY,EAAE,GAAG,CAAC,CAAC;gBAC7C,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;YAC7C,CAAC;YAAC,MAAM,CAAC;gBACR,OAAO,SAAS,CAAC;YAClB,CAAC;YACD,OAAO,YAAY,CAAC;QAAA,CACpB;KACD,CAAC;AAAA,CACF","sourcesContent":["import { open } from \"node:fs/promises\";\nimport { isAbsolute, join } from \"node:path\";\nimport { withinInstallStoreRoot } from \"./install-store-paths.ts\";\nimport type { PluginRegistryManifestEnvelopeV1, PluginRegistrySourceV1 } from \"./registry.ts\";\nimport type { PluginManifestV1 } from \"./types.ts\";\n\nconst NODE_DISK_REGISTRY_SOURCE_VERSION_V1 = 1 as const;\n/** Maximum index.v1.json bytes accepted by this source boundary. */\nexport const MAX_NODE_DISK_REGISTRY_INDEX_BYTES_V1 = 1024 * 1024;\n/** Maximum per-installation agent-forge.json bytes accepted by this source. */\nexport const MAX_NODE_DISK_REGISTRY_MANIFEST_BYTES_V1 = 64 * 1024;\n/** Maximum index records projected by one list() call; matches the registry manifest cap. */\nexport const MAX_NODE_DISK_REGISTRY_RECORDS_V1 = 256;\n\nconst INDEX_FILE_V1 = \"index.v1.json\";\nconst ECOSYSTEM_MANIFEST_FILE_V1 = \"agent-forge.json\";\nconst CAPABILITY_MANIFEST_FILE_V1 = \"plugin.json\";\nconst NODE_PACKAGE_FORMAT_VERSION_V1 = 1;\n\nexport type NodeDiskRegistrySourceErrorCodeV1 =\n\t| \"cancelled\"\n\t| \"invalid_configuration\"\n\t| \"invalid_index\"\n\t| \"invalid_manifest\"\n\t| \"identity_mismatch\"\n\t| \"too_many_records\"\n\t| \"io_failed\";\n\n/**\n * Deliberately does not retain filesystem exceptions or file contents: callers\n * must not turn registry diagnostics into a path or package disclosure.\n */\nexport class NodeDiskRegistrySourceErrorV1 extends Error {\n\treadonly code: NodeDiskRegistrySourceErrorCodeV1;\n\n\tconstructor(code: NodeDiskRegistrySourceErrorCodeV1, message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"NodeDiskRegistrySourceErrorV1\";\n\t\tthis.code = code;\n\t}\n}\n\nexport interface NodeDiskRegistrySourceOptionsV1 {\n\t/** Absolute host-owned default install store directory. It is never returned in a DTO. */\n\treadonly rootDirectory: string;\n}\n\nexport interface NodeDiskRegistrySourceResolveInputV1 {\n\treadonly pluginId: string;\n\treadonly pluginVersion: string;\n\treadonly signal?: AbortSignal;\n}\n\nexport interface NodeDiskRegistrySourceV1 extends PluginRegistrySourceV1 {\n\treadonly version: typeof NODE_DISK_REGISTRY_SOURCE_VERSION_V1;\n\t/**\n\t * Internal host channel (D-075 S2): absolute `plugin.json` path of the\n\t * installation directory matching `{pluginId, pluginVersion}` in the store\n\t * index, for the sdk's installed-plugin code loading. Registry snapshots\n\t * still carry no paths; undefined means no matching record or the manifest\n\t * file is absent from the installation directory.\n\t */\n\treadonly resolveCapabilityManifestPath?: (\n\t\tinput: NodeDiskRegistrySourceResolveInputV1,\n\t) => Promise<string | undefined>;\n}\n\ninterface DiskIndexRecordV1 {\n\treadonly installationRef: string;\n\treadonly directory: string;\n\treadonly pluginId: string;\n\treadonly pluginVersion: string;\n\treadonly manifestDigest: string;\n\treadonly scope: \"global\" | \"project\";\n\treadonly scopeRef: string;\n\treadonly state: \"installed\" | \"active\" | \"retained\";\n}\n\nconst INDEX_KEYS_V1 = [\n\t\"installationRef\",\n\t\"directory\",\n\t\"pluginId\",\n\t\"pluginVersion\",\n\t\"manifestDigest\",\n\t\"scope\",\n\t\"scopeRef\",\n\t\"state\",\n] as const;\n\nfunction opaqueField(value: unknown, max: number): value is string {\n\treturn (\n\t\ttypeof value === \"string\" &&\n\t\tvalue.length > 0 &&\n\t\tvalue.length <= max &&\n\t\t!/[\\u0000-\\u001f\\u007f]/u.test(value) &&\n\t\t!/\\s/u.test(value) &&\n\t\t!/^(?:file:|[a-z]:[\\\\/]|[\\\\/]{1,2}|\\\\\\\\)/iu.test(value)\n\t);\n}\n\nfunction boundedField(value: unknown, max: number): value is string {\n\treturn typeof value === \"string\" && value.length > 0 && value.length <= max && !/[\\u0000-\\u001f\\u007f]/u.test(value);\n}\n\nfunction validateRecord(root: string, value: unknown, index: number): DiskIndexRecordV1 {\n\tif (value === null || typeof value !== \"object\" || Array.isArray(value))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", `Index record ${index} is not an object`);\n\tconst record = value as Record<string, unknown>;\n\tconst keys = INDEX_KEYS_V1;\n\tif (Object.keys(record).length !== keys.length || keys.some((key) => !(key in record)))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", `Index record ${index} has an invalid schema`);\n\tif (\n\t\t!opaqueField(record.installationRef, 2048) ||\n\t\t!boundedField(record.directory, 2048) ||\n\t\t!boundedField(record.pluginId, 256) ||\n\t\t!boundedField(record.pluginVersion, 256) ||\n\t\t!boundedField(record.manifestDigest, 512) ||\n\t\t!opaqueField(record.scopeRef, 2048) ||\n\t\t(record.scope !== \"global\" && record.scope !== \"project\") ||\n\t\t(record.state !== \"installed\" && record.state !== \"active\" && record.state !== \"retained\") ||\n\t\t!withinInstallStoreRoot(root, record.directory)\n\t)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", `Index record ${index} is invalid`);\n\treturn {\n\t\tinstallationRef: record.installationRef,\n\t\tdirectory: record.directory,\n\t\tpluginId: record.pluginId,\n\t\tpluginVersion: record.pluginVersion,\n\t\tmanifestDigest: record.manifestDigest,\n\t\tscope: record.scope,\n\t\tscopeRef: record.scopeRef,\n\t\tstate: record.state,\n\t};\n}\n\nfunction parseIndex(root: string, raw: string): readonly DiskIndexRecordV1[] {\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(raw);\n\t} catch {\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", \"Install store index JSON is invalid\");\n\t}\n\tif (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", \"Install store index is not an object\");\n\tconst value = parsed as Record<string, unknown>;\n\tif (\n\t\tObject.keys(value).length !== 2 ||\n\t\tvalue.version !== NODE_DISK_REGISTRY_SOURCE_VERSION_V1 ||\n\t\t!Array.isArray(value.records)\n\t)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_index\", \"Install store index schema is unsupported\");\n\tif (value.records.length > MAX_NODE_DISK_REGISTRY_RECORDS_V1)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"too_many_records\", \"Install store index exceeds the record limit\");\n\treturn value.records.map((record, index) => validateRecord(root, record, index));\n}\n\n/** Read a bounded UTF-8 file; the absolute path never appears in a thrown message. */\nasync function readBoundedFile(path: string, maxBytes: number, label: string): Promise<string> {\n\tconst handle = await open(path, \"r\");\n\ttry {\n\t\tconst stat = await handle.stat();\n\t\tif (!stat.isFile() || stat.size > maxBytes)\n\t\t\tthrow new NodeDiskRegistrySourceErrorV1(\n\t\t\t\tlabel === \"index\" ? \"invalid_index\" : \"invalid_manifest\",\n\t\t\t\t`${label} exceeds the size limit`,\n\t\t\t);\n\t\treturn await handle.readFile(\"utf8\");\n\t} finally {\n\t\tawait handle.close().catch(() => undefined);\n\t}\n}\n\nfunction parseManifest(record: DiskIndexRecordV1, raw: string): PluginManifestV1 {\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(raw);\n\t} catch {\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_manifest\", \"Installed plugin manifest JSON is invalid\");\n\t}\n\tif (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed))\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_manifest\", \"Installed plugin manifest is not an object\");\n\tconst value = { ...(parsed as Record<string, unknown>) };\n\tif (value.packageFormatVersion !== undefined && value.packageFormatVersion !== NODE_PACKAGE_FORMAT_VERSION_V1)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_manifest\", \"Installed plugin package format is unsupported\");\n\t// The package envelope version is an install-host field; it is not part of\n\t// PluginManifestV1 and the stable manifest digest excludes it.\n\tdelete value.packageFormatVersion;\n\tif (value.id !== record.pluginId || value.pluginVersion !== record.pluginVersion)\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\n\t\t\t\"identity_mismatch\",\n\t\t\t`Installed plugin manifest identity does not match index record for ${record.pluginId}@${record.pluginVersion}`,\n\t\t);\n\treturn value as unknown as PluginManifestV1;\n}\n\n/** Read the install store index records; undefined when the store is empty. */\nasync function readIndexRecords(\n\trootDirectory: string,\n\tsignal: AbortSignal | undefined,\n): Promise<readonly DiskIndexRecordV1[] | undefined> {\n\tif (signal?.aborted) throw new NodeDiskRegistrySourceErrorV1(\"cancelled\", \"Disk registry source read was cancelled\");\n\tlet rawIndex: string;\n\ttry {\n\t\trawIndex = await readBoundedFile(\n\t\t\tjoin(rootDirectory, INDEX_FILE_V1),\n\t\t\tMAX_NODE_DISK_REGISTRY_INDEX_BYTES_V1,\n\t\t\t\"index\",\n\t\t);\n\t} catch (error) {\n\t\tif (error instanceof NodeDiskRegistrySourceErrorV1) throw error;\n\t\tif (error instanceof Error && \"code\" in error && error.code === \"ENOENT\") return undefined;\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"io_failed\", \"Install store index could not be read\");\n\t}\n\treturn parseIndex(rootDirectory, rawIndex);\n}\n\n/**\n * Read-only registry source over the default install store. It projects the\n * persistent index and each installation instance's ecosystem manifest as\n * bounded envelopes for the registry's own digest verification. It never\n * writes the index or installation directories, and installation directories\n * stay inside the host-owned store.\n */\nexport function createNodeDiskPluginRegistrySourceV1(\n\toptions: NodeDiskRegistrySourceOptionsV1,\n): NodeDiskRegistrySourceV1 {\n\tconst rootDirectory = options.rootDirectory;\n\tif (typeof rootDirectory !== \"string\" || !isAbsolute(rootDirectory) || rootDirectory.trim() === \"\")\n\t\tthrow new NodeDiskRegistrySourceErrorV1(\"invalid_configuration\", \"Install store root directory must be absolute\");\n\treturn {\n\t\tversion: NODE_DISK_REGISTRY_SOURCE_VERSION_V1,\n\t\tlist: async ({ signal } = {}) => {\n\t\t\tconst records = await readIndexRecords(rootDirectory, signal);\n\t\t\tif (records === undefined) return Object.freeze([]);\n\t\t\tconst envelopes: PluginRegistryManifestEnvelopeV1[] = [];\n\t\t\tfor (const record of records) {\n\t\t\t\tif (signal?.aborted)\n\t\t\t\t\tthrow new NodeDiskRegistrySourceErrorV1(\"cancelled\", \"Disk registry source read was cancelled\");\n\t\t\t\tlet rawManifest: string;\n\t\t\t\ttry {\n\t\t\t\t\trawManifest = await readBoundedFile(\n\t\t\t\t\t\tjoin(record.directory, ECOSYSTEM_MANIFEST_FILE_V1),\n\t\t\t\t\t\tMAX_NODE_DISK_REGISTRY_MANIFEST_BYTES_V1,\n\t\t\t\t\t\t\"manifest\",\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (error instanceof NodeDiskRegistrySourceErrorV1) throw error;\n\t\t\t\t\tthrow new NodeDiskRegistrySourceErrorV1(\"io_failed\", \"Installed plugin manifest could not be read\");\n\t\t\t\t}\n\t\t\t\tconst manifest = parseManifest(record, rawManifest);\n\t\t\t\tenvelopes.push({\n\t\t\t\t\tmanifest,\n\t\t\t\t\tsource: record.installationRef,\n\t\t\t\t\tmanifestDigest: record.manifestDigest,\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn Object.freeze(envelopes);\n\t\t},\n\t\tresolveCapabilityManifestPath: async ({ pluginId, pluginVersion, signal }) => {\n\t\t\tconst records = await readIndexRecords(rootDirectory, signal);\n\t\t\tif (records === undefined) return undefined;\n\t\t\tconst record = records.find(\n\t\t\t\t(candidate) => candidate.pluginId === pluginId && candidate.pluginVersion === pluginVersion,\n\t\t\t);\n\t\t\tif (record === undefined) return undefined;\n\t\t\tconst manifestPath = join(record.directory, CAPABILITY_MANIFEST_FILE_V1);\n\t\t\ttry {\n\t\t\t\tconst handle = await open(manifestPath, \"r\");\n\t\t\t\tawait handle.close().catch(() => undefined);\n\t\t\t} catch {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\treturn manifestPath;\n\t\t},\n\t};\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"node-package-install-host.d.ts","sourceRoot":"","sources":["../../../src/plugins/ecosystem/node-package-install-host.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,KAAK,EAAE,yBAAyB,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAO3G,OAAO,KAAK,EAAoB,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAGzE,QAAA,MAAM,oCAAoC,GAAa,CAAC;AACxD,eAAO,MAAM,8BAA8B,GAAa,CAAC;AAmBzD,MAAM,MAAM,6BAA6B,GACtC,WAAW,GACX,aAAa,GACb,kBAAkB,GAClB,wBAAwB,GACxB,iBAAiB,GACjB,yBAAyB,GACzB,0BAA0B,GAC1B,0BAA0B,GAC1B,2BAA2B,GAC3B,sBAAsB,GACtB,gCAAgC,GAChC,wBAAwB,CAAC;AAE5B,qBAAa,yBAA0B,SAAQ,KAAK;IACnD,QAAQ,CAAC,IAAI,EAAE,6BAA6B,CAAC;IAE7C,YAAY,IAAI,EAAE,6BAA6B,EAI9C;CACD;AAED,MAAM,WAAW,qBAAqB;IACrC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;CAC3B;AAED,MAAM,WAAW,sBAA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,+BAA+B,GACtC,OAAO,CAAC,wBAAwB,CAAC,CAwZnC","sourcesContent":["import { createHash, randomUUID } from \"node:crypto\";\nimport { mkdir, mkdtemp, rename, rm, writeFile } from \"node:fs/promises\";\nimport { tmpdir } from \"node:os\";\nimport { isAbsolute, join } from \"node:path\";\nimport { inflateRawSync } from \"node:zlib\";\nimport { parseCapabilityPluginManifest } from \"../../core/extensions/capability-plugin.ts\";\nimport type { PluginDistributionArtifactV1 } from \"./distribution.ts\";\nimport type { PluginInstallationScopeV1, PluginInstallationV1, PluginInstallHostV1 } from \"./installer.ts\";\nimport {\n\tcreateNodePackageInstallPersistenceV1,\n\ttype NodePackageInstallPersistenceV1,\n\ttype NodePackageInstallPersistentRecordV1,\n} from \"./node-package-install-persistence.ts\";\nimport { parseNodeTarGzPackageV1 } from \"./node-tar-package.ts\";\nimport type { PluginManifestV1, PluginUpdateRecordV1 } from \"./types.ts\";\nimport { pluginManifestDigestV1 } from \"./updates.ts\";\n\nconst NODE_PACKAGE_INSTALL_HOST_VERSION_V1 = 1 as const;\nexport const NODE_PACKAGE_FORMAT_VERSION_V1 = 1 as const;\nconst DEFAULT_MAX_ARCHIVE_BYTES_V1 = 128 * 1024 * 1024;\nconst MAX_ARCHIVE_BYTES_V1 = 512 * 1024 * 1024;\nconst DEFAULT_MAX_EXTRACTED_BYTES_V1 = 256 * 1024 * 1024;\nconst MAX_EXTRACTED_BYTES_V1 = 1024 * 1024 * 1024;\nconst DEFAULT_MAX_ENTRY_BYTES_V1 = 64 * 1024 * 1024;\nconst MAX_ENTRY_BYTES_V1 = 256 * 1024 * 1024;\nconst DEFAULT_MAX_ENTRIES_V1 = 2048;\nconst MAX_ENTRIES_V1 = 8192;\nconst MAX_PACKAGE_MANIFEST_BYTES_V1 = 64 * 1024;\nconst ECOSYSTEM_MANIFEST_FILE_V1 = \"agent-forge.json\";\nconst CAPABILITY_MANIFEST_FILE_V1 = \"plugin.json\";\n\nconst LOCAL_FILE_HEADER_V1 = 0x04034b50;\nconst CENTRAL_FILE_HEADER_V1 = 0x02014b50;\nconst END_OF_CENTRAL_DIRECTORY_V1 = 0x06054b50;\nconst ZIP64_END_OF_CENTRAL_DIRECTORY_V1 = 0x06064b50;\nconst ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_V1 = 0x07064b50;\n\nexport type NodePackageInstallErrorCodeV1 =\n\t| \"cancelled\"\n\t| \"host_closed\"\n\t| \"invalid_artifact\"\n\t| \"unsupported_media_type\"\n\t| \"archive_invalid\"\n\t| \"archive_budget_exceeded\"\n\t| \"package_manifest_missing\"\n\t| \"package_manifest_invalid\"\n\t| \"package_manifest_mismatch\"\n\t| \"installation_unknown\"\n\t| \"installation_identity_mismatch\"\n\t| \"installation_io_failed\";\n\nexport class NodePackageInstallErrorV1 extends Error {\n\treadonly code: NodePackageInstallErrorCodeV1;\n\n\tconstructor(code: NodePackageInstallErrorCodeV1) {\n\t\tsuper(`Node package installation failed: ${code}`);\n\t\tthis.name = \"NodePackageInstallErrorV1\";\n\t\tthis.code = code;\n\t}\n}\n\nexport interface NodeZipPackageEntryV1 {\n\treadonly name: string;\n\treadonly directory: boolean;\n\treadonly bytes: Uint8Array;\n}\n\nexport interface NodeZipPackageLimitsV1 {\n\treadonly maxArchiveBytes?: number;\n\treadonly maxExtractedBytes?: number;\n\treadonly maxEntryBytes?: number;\n\treadonly maxEntries?: number;\n}\n\nexport interface NodePackageInstallHostOptionsV1 extends NodeZipPackageLimitsV1 {\n\t/** An opaque artifact reader owned by the distribution host. */\n\treadonly openArtifact: (input: {\n\t\treadonly artifact: PluginDistributionArtifactV1;\n\t\treadonly signal?: AbortSignal;\n\t}) => AsyncIterable<Uint8Array>;\n\t/** Parent directory owned by the host; no local path leaves this adapter. */\n\treadonly installationDirectory?: string;\n\treadonly supportedScopes?: readonly PluginInstallationScopeV1[];\n\t/** Persistent host-owned index; omitted keeps the test-friendly ephemeral mode. */\n\treadonly persistence?: \"ephemeral\" | \"persistent\";\n\treadonly persistenceLockTimeoutMs?: number;\n\t/**\n\t * Host-only runtime assembly seam. The directory and manifest path never\n\t * cross the public installer DTO or model/tool context.\n\t */\n\treadonly loadPlugin?: (input: {\n\t\treadonly installation: PluginInstallationV1;\n\t\treadonly record?: PluginUpdateRecordV1;\n\t\treadonly directory: string;\n\t\treadonly manifestPath: string;\n\t\treadonly signal: AbortSignal;\n\t}) => Promise<{ readonly dispose?: () => void | Promise<void> } | void>;\n}\n\n/** Minimal structural bridge for an AgentSession-owned capability loader. */\nexport interface NodePackageInstallCapabilityPluginSessionV1 {\n\treadonly loadCapabilityPluginManifest: (manifestPath: string) => Promise<{\n\t\treadonly dispose: () => void | Promise<void>;\n\t}>;\n}\n\n/**\n * Adapt an AgentSession-like loader to the host-only runtime seam. The\n * manifest path is never returned as a DTO or sent through model context.\n */\nexport function createNodePackageInstallSessionLoaderV1(\n\tsession: NodePackageInstallCapabilityPluginSessionV1,\n): NonNullable<NodePackageInstallHostOptionsV1[\"loadPlugin\"]> {\n\treturn async ({ manifestPath }) => {\n\t\tconst handle = await session.loadCapabilityPluginManifest(manifestPath);\n\t\treturn { dispose: handle.dispose };\n\t};\n}\n\n/**\n * Create a package host with an explicit AgentSession-compatible runtime\n * loader. This is an opt-in composition helper; it does not alter Core's\n * default session construction or install untrusted package code.\n */\nexport async function createNodePackageInstallHostForSessionV1(\n\toptions: NodePackageInstallHostOptionsV1,\n\tsession: NodePackageInstallCapabilityPluginSessionV1,\n): Promise<NodePackageInstallHostV1> {\n\tif (options.loadPlugin !== undefined) throw new Error(\"loadPlugin must be omitted when binding a session\");\n\treturn await createNodePackageInstallHostV1({\n\t\t...options,\n\t\tloadPlugin: createNodePackageInstallSessionLoaderV1(session),\n\t});\n}\n\nexport interface NodePackageInstallHostV1 extends PluginInstallHostV1 {\n\treadonly version: typeof NODE_PACKAGE_INSTALL_HOST_VERSION_V1;\n\treadonly uninstall: NonNullable<PluginInstallHostV1[\"uninstall\"]>;\n\t/** Host-only bridge for local runtime assembly; never put this path in a DTO or transcript. */\n\treadonly getInstallationDirectory: (input: { readonly installation: PluginInstallationV1 }) => string;\n\treadonly close: () => Promise<void>;\n}\n\ninterface ZipEntryDescriptorV1 {\n\treadonly name: string;\n\treadonly directory: boolean;\n\treadonly flags: number;\n\treadonly method: number;\n\treadonly compressedSize: number;\n\treadonly uncompressedSize: number;\n\treadonly crc32: number;\n\treadonly localOffset: number;\n}\n\ninterface ZipLocalSpanV1 {\n\treadonly start: number;\n\treadonly end: number;\n}\n\ninterface InstalledRecordV1 {\n\treadonly installationRef: string;\n\treadonly directory: string;\n\treadonly pluginId: string;\n\treadonly pluginVersion: string;\n\treadonly manifestDigest: string;\n\treadonly scope: PluginInstallationScopeV1;\n\treadonly scopeRef: string;\n\tstate: \"installed\" | \"active\" | \"retained\";\n}\n\nfunction positiveLimit(value: number | undefined, fallback: number, maximum: number, label: string): number {\n\tconst result = value ?? fallback;\n\tif (!Number.isSafeInteger(result) || result < 1 || result > maximum)\n\t\tthrow new Error(`${label} must be a positive integer at most ${maximum}`);\n\treturn result;\n}\n\n/** Resolve the archive budget limits shared by the ZIP and tgz package parsers. */\nexport function resolveNodePackageLimitsV1(limitsInput: NodeZipPackageLimitsV1): Required<NodeZipPackageLimitsV1> {\n\treturn {\n\t\tmaxArchiveBytes: positiveLimit(\n\t\t\tlimitsInput.maxArchiveBytes,\n\t\t\tDEFAULT_MAX_ARCHIVE_BYTES_V1,\n\t\t\tMAX_ARCHIVE_BYTES_V1,\n\t\t\t\"maxArchiveBytes\",\n\t\t),\n\t\tmaxExtractedBytes: positiveLimit(\n\t\t\tlimitsInput.maxExtractedBytes,\n\t\t\tDEFAULT_MAX_EXTRACTED_BYTES_V1,\n\t\t\tMAX_EXTRACTED_BYTES_V1,\n\t\t\t\"maxExtractedBytes\",\n\t\t),\n\t\tmaxEntryBytes: positiveLimit(\n\t\t\tlimitsInput.maxEntryBytes,\n\t\t\tDEFAULT_MAX_ENTRY_BYTES_V1,\n\t\t\tMAX_ENTRY_BYTES_V1,\n\t\t\t\"maxEntryBytes\",\n\t\t),\n\t\tmaxEntries: positiveLimit(limitsInput.maxEntries, DEFAULT_MAX_ENTRIES_V1, MAX_ENTRIES_V1, \"maxEntries\"),\n\t};\n}\n\nfunction readU16(bytes: Uint8Array, offset: number): number {\n\tif (offset < 0 || offset + 2 > bytes.byteLength) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\treturn bytes[offset]! | (bytes[offset + 1]! << 8);\n}\n\nfunction readU32(bytes: Uint8Array, offset: number): number {\n\tif (offset < 0 || offset + 4 > bytes.byteLength) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\treturn (bytes[offset]! | (bytes[offset + 1]! << 8) | (bytes[offset + 2]! << 16) | (bytes[offset + 3]! << 24)) >>> 0;\n}\n\nfunction decodeName(bytes: Uint8Array, utf8: boolean): string {\n\ttry {\n\t\tconst value = new TextDecoder(\"utf-8\", { fatal: true }).decode(bytes);\n\t\tif (!utf8 && /[^\\x00-\\x7f]/u.test(value)) throw new Error(\"non-ascii zip name\");\n\t\treturn value;\n\t} catch {\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t}\n}\n\n/** Normalized archive entry name shared by the ZIP and tgz package parsers. */\nexport function normalizedEntryName(value: string): { readonly name: string; readonly directory: boolean } {\n\tif (value.length === 0 || value.includes(\"\\\\\") || /[\\u0000-\\u001f\\u007f]/u.test(value))\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst directory = value.endsWith(\"/\");\n\tif (value.startsWith(\"/\") || /^[a-z]:/iu.test(value)) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst parts = value.split(\"/\");\n\tif (directory) parts.pop();\n\tif (\n\t\tparts.length === 0 ||\n\t\tparts.some(\n\t\t\t(part) =>\n\t\t\t\tpart === \"\" ||\n\t\t\t\tpart === \".\" ||\n\t\t\t\tpart === \"..\" ||\n\t\t\t\tpart.includes(\":\") ||\n\t\t\t\t/[ .]$/u.test(part) ||\n\t\t\t\t/^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\\..*)?$/iu.test(part),\n\t\t)\n\t)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\treturn { name: parts.join(\"/\"), directory };\n}\n\nfunction crcTable(): Uint32Array {\n\tconst table = new Uint32Array(256);\n\tfor (let index = 0; index < table.length; index += 1) {\n\t\tlet value = index;\n\t\tfor (let bit = 0; bit < 8; bit += 1) value = (value & 1) === 0 ? value >>> 1 : (value >>> 1) ^ 0xedb88320;\n\t\ttable[index] = value >>> 0;\n\t}\n\treturn table;\n}\n\nconst CRC_TABLE_V1 = crcTable();\n\nfunction crc32(bytes: Uint8Array): number {\n\tlet value = 0xffffffff;\n\tfor (const byte of bytes) value = (value >>> 8) ^ CRC_TABLE_V1[(value ^ byte) & 0xff]!;\n\treturn (value ^ 0xffffffff) >>> 0;\n}\n\nfunction findEndOfCentralDirectory(bytes: Uint8Array): {\n\treadonly offset: number;\n\treadonly entries: number;\n\treadonly size: number;\n\treadonly start: number;\n} {\n\tconst minimum = Math.max(0, bytes.byteLength - 0xffff - 22);\n\tfor (let offset = bytes.byteLength - 22; offset >= minimum; offset -= 1) {\n\t\tif (readU32(bytes, offset) !== END_OF_CENTRAL_DIRECTORY_V1) continue;\n\t\tconst disk = readU16(bytes, offset + 4);\n\t\tconst centralDisk = readU16(bytes, offset + 6);\n\t\tconst entries = readU16(bytes, offset + 10);\n\t\tconst centralEntries = readU16(bytes, offset + 8);\n\t\tconst size = readU32(bytes, offset + 12);\n\t\tconst start = readU32(bytes, offset + 16);\n\t\tconst commentLength = readU16(bytes, offset + 20);\n\t\tif (\n\t\t\tdisk !== 0 ||\n\t\t\tcentralDisk !== 0 ||\n\t\t\tentries !== centralEntries ||\n\t\t\toffset + 22 + commentLength !== bytes.byteLength\n\t\t)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tif (entries === 0xffff || size === 0xffffffff || start === 0xffffffff)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tif (start + size > offset) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\treturn { offset, entries, size, start };\n\t}\n\tfor (let offset = Math.max(0, bytes.byteLength - 64); offset < bytes.byteLength - 3; offset += 1) {\n\t\tconst signature = readU32(bytes, offset);\n\t\tif (signature === ZIP64_END_OF_CENTRAL_DIRECTORY_V1 || signature === ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_V1)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t}\n\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n}\n\nfunction parseCentralDirectory(\n\tbytes: Uint8Array,\n\tlimits: Required<NodeZipPackageLimitsV1>,\n): readonly ZipEntryDescriptorV1[] {\n\tconst end = findEndOfCentralDirectory(bytes);\n\tif (end.entries > limits.maxEntries || end.size > bytes.byteLength || end.start + end.size > end.offset)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_budget_exceeded\");\n\tconst entries: ZipEntryDescriptorV1[] = [];\n\tconst names = new Set<string>();\n\tlet offset = end.start;\n\tfor (let index = 0; index < end.entries; index += 1) {\n\t\tif (readU32(bytes, offset) !== CENTRAL_FILE_HEADER_V1) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tconst madeBy = readU16(bytes, offset + 4) >>> 8;\n\t\tconst flags = readU16(bytes, offset + 8);\n\t\tconst method = readU16(bytes, offset + 10);\n\t\tconst crc = readU32(bytes, offset + 16);\n\t\tconst compressedSize = readU32(bytes, offset + 20);\n\t\tconst uncompressedSize = readU32(bytes, offset + 24);\n\t\tconst nameLength = readU16(bytes, offset + 28);\n\t\tconst extraLength = readU16(bytes, offset + 30);\n\t\tconst commentLength = readU16(bytes, offset + 32);\n\t\tconst disk = readU16(bytes, offset + 34);\n\t\tconst externalAttributes = readU32(bytes, offset + 38);\n\t\tconst localOffset = readU32(bytes, offset + 42);\n\t\tconst nameStart = offset + 46;\n\t\tconst recordEnd = nameStart + nameLength + extraLength + commentLength;\n\t\tif (\n\t\t\trecordEnd > end.start + end.size ||\n\t\t\tdisk !== 0 ||\n\t\t\t(flags & ~(0x8 | 0x800)) !== 0 ||\n\t\t\t(flags & 1) !== 0 ||\n\t\t\t(flags & 0x40) !== 0\n\t\t)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tif (method !== 0 && method !== 8) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tif (uncompressedSize > limits.maxEntryBytes || compressedSize > limits.maxArchiveBytes)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_budget_exceeded\");\n\t\tconst name = normalizedEntryName(\n\t\t\tdecodeName(bytes.subarray(nameStart, nameStart + nameLength), (flags & 0x800) !== 0),\n\t\t);\n\t\tif (names.has(name.name)) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tnames.add(name.name);\n\t\tconst unixMode = madeBy === 3 ? (externalAttributes >>> 16) & 0xffff : 0;\n\t\tif ((unixMode & 0xf000) === 0xa000) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tentries.push({\n\t\t\tname: name.name,\n\t\t\tdirectory: name.directory,\n\t\t\tflags,\n\t\t\tmethod,\n\t\t\tcompressedSize,\n\t\t\tuncompressedSize,\n\t\t\tcrc32: crc,\n\t\t\tlocalOffset,\n\t\t});\n\t\toffset = recordEnd;\n\t}\n\tif (offset !== end.start + end.size) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t// Validate the local-file regions before extraction. A forged central\n\t// directory must not be able to point into another entry or into the\n\t// central directory/EOCD; otherwise extraction could reinterpret metadata\n\t// bytes as package contents.\n\tconst spans: ZipLocalSpanV1[] = [];\n\tfor (const entry of entries) {\n\t\tif (entry.localOffset < 0 || entry.localOffset + 30 > end.start)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tif (readU32(bytes, entry.localOffset) !== LOCAL_FILE_HEADER_V1)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tconst localNameLength = readU16(bytes, entry.localOffset + 26);\n\t\tconst localExtraLength = readU16(bytes, entry.localOffset + 28);\n\t\tconst dataStart = entry.localOffset + 30 + localNameLength + localExtraLength;\n\t\tconst dataEnd = dataStart + entry.compressedSize;\n\t\tif (dataStart < entry.localOffset + 30 || dataEnd < dataStart || dataEnd > end.start)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tspans.push({ start: entry.localOffset, end: dataEnd });\n\t}\n\tspans.sort((left, right) => left.start - right.start || left.end - right.end);\n\tfor (let index = 1; index < spans.length; index += 1) {\n\t\tif (spans[index - 1]!.end > spans[index]!.start) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t}\n\treturn entries;\n}\n\nfunction extractEntry(bytes: Uint8Array, entry: ZipEntryDescriptorV1, maxEntryBytes: number): Uint8Array {\n\tif (entry.directory) {\n\t\tif (entry.compressedSize !== 0 || entry.uncompressedSize !== 0)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\treturn new Uint8Array();\n\t}\n\tif (entry.localOffset + 30 > bytes.byteLength) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\tif (readU32(bytes, entry.localOffset) !== LOCAL_FILE_HEADER_V1)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst localFlags = readU16(bytes, entry.localOffset + 6);\n\tconst localMethod = readU16(bytes, entry.localOffset + 8);\n\tconst nameLength = readU16(bytes, entry.localOffset + 26);\n\tconst extraLength = readU16(bytes, entry.localOffset + 28);\n\tconst localName = decodeName(\n\t\tbytes.subarray(entry.localOffset + 30, entry.localOffset + 30 + nameLength),\n\t\t(entry.flags & 0x800) !== 0,\n\t);\n\tif (normalizedEntryName(localName).name !== entry.name || localFlags !== entry.flags || localMethod !== entry.method)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst localCompressedSize = readU32(bytes, entry.localOffset + 18);\n\tconst localUncompressedSize = readU32(bytes, entry.localOffset + 22);\n\tif (\n\t\t(entry.flags & 8) === 0 &&\n\t\t(localCompressedSize !== entry.compressedSize || localUncompressedSize !== entry.uncompressedSize)\n\t)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst dataStart = entry.localOffset + 30 + nameLength + extraLength;\n\tconst dataEnd = dataStart + entry.compressedSize;\n\tif (dataStart > bytes.byteLength || dataEnd > bytes.byteLength)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst compressed = bytes.subarray(dataStart, dataEnd);\n\tlet output: Uint8Array;\n\ttry {\n\t\toutput =\n\t\t\tentry.method === 0\n\t\t\t\t? new Uint8Array(compressed)\n\t\t\t\t: inflateRawSync(compressed, { maxOutputLength: maxEntryBytes });\n\t} catch {\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t}\n\tif (\n\t\toutput.byteLength !== entry.uncompressedSize ||\n\t\toutput.byteLength > maxEntryBytes ||\n\t\tcrc32(output) !== entry.crc32\n\t)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\treturn output;\n}\n\nexport function parseNodeZipPackageV1(\n\tinput: Uint8Array,\n\tlimitsInput: NodeZipPackageLimitsV1 = {},\n): readonly NodeZipPackageEntryV1[] {\n\tif (!(input instanceof Uint8Array)) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst limits = resolveNodePackageLimitsV1(limitsInput);\n\tif (input.byteLength > limits.maxArchiveBytes) throw new NodePackageInstallErrorV1(\"archive_budget_exceeded\");\n\tconst descriptors = parseCentralDirectory(input, limits);\n\tlet extractedBytes = 0;\n\treturn Object.freeze(\n\t\tdescriptors.map((descriptor) => {\n\t\t\tconst bytes = extractEntry(input, descriptor, limits.maxEntryBytes);\n\t\t\textractedBytes += bytes.byteLength;\n\t\t\tif (extractedBytes > limits.maxExtractedBytes) throw new NodePackageInstallErrorV1(\"archive_budget_exceeded\");\n\t\t\treturn Object.freeze({ name: descriptor.name, directory: descriptor.directory, bytes });\n\t\t}),\n\t);\n}\n\nfunction manifestKeys(value: Record<string, unknown>): boolean {\n\tconst allowed = new Set([\n\t\t\"packageFormatVersion\",\n\t\t\"id\",\n\t\t\"pluginVersion\",\n\t\t\"apiVersion\",\n\t\t\"minHostVersion\",\n\t\t\"maxHostVersion\",\n\t\t\"capabilityRefs\",\n\t\t\"requires\",\n\t\t\"platform\",\n\t\t\"testOnly\",\n\t]);\n\treturn Object.keys(value).every((key) => allowed.has(key));\n}\n\nfunction packageManifest(value: unknown, record: PluginUpdateRecordV1): void {\n\tif (value === null || typeof value !== \"object\" || Array.isArray(value))\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\tconst manifest = value as Record<string, unknown>;\n\tif (\n\t\t!manifestKeys(manifest) ||\n\t\tmanifest.packageFormatVersion !== NODE_PACKAGE_FORMAT_VERSION_V1 ||\n\t\tmanifest.id !== record.pluginId ||\n\t\tmanifest.pluginVersion !== record.pluginVersion\n\t)\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_mismatch\");\n\tif (typeof manifest.apiVersion !== \"string\" || manifest.apiVersion !== record.manifest.apiVersion)\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_mismatch\");\n\ttry {\n\t\tif (pluginManifestDigestV1(manifest as unknown as PluginManifestV1) !== record.manifestDigest)\n\t\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_mismatch\");\n\t} catch (error) {\n\t\tif (error instanceof NodePackageInstallErrorV1) throw error;\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\t}\n}\n\nfunction capabilityManifest(\n\tvalue: unknown,\n\trecord: PluginUpdateRecordV1,\n\tentries: readonly NodeZipPackageEntryV1[],\n): void {\n\tlet manifest: ReturnType<typeof parseCapabilityPluginManifest>;\n\ttry {\n\t\tmanifest = parseCapabilityPluginManifest(value, \"<installed plugin.json>\");\n\t} catch {\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\t}\n\tif (manifest.id !== record.pluginId || manifest.version !== record.pluginVersion)\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_mismatch\");\n\tconst entryValue = manifest.entry.startsWith(\"./\") ? manifest.entry.slice(2) : manifest.entry;\n\tconst entry = normalizedEntryName(entryValue);\n\tif (entry.directory || !entries.some((candidate) => candidate.name === entry.name && !candidate.directory))\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_mismatch\");\n}\n\nfunction opaqueScope(value: string): string {\n\tif (\n\t\ttypeof value !== \"string\" ||\n\t\tvalue.trim() === \"\" ||\n\t\tvalue.length > 2048 ||\n\t\t/\\s|[\\u0000-\\u001f\\u007f]/u.test(value)\n\t)\n\t\tthrow new NodePackageInstallErrorV1(\"installation_identity_mismatch\");\n\treturn value;\n}\n\nfunction installationKey(scope: PluginInstallationScopeV1, scopeRef: string, pluginId: string): string {\n\treturn `${scope}\\u0000${scopeRef}\\u0000${pluginId}`;\n}\n\nfunction identityMatches(\n\trecord: InstalledRecordV1,\n\tinput: {\n\t\treadonly pluginId: string;\n\t\treadonly pluginVersion: string;\n\t\treadonly manifestDigest: string;\n\t\treadonly scope: PluginInstallationScopeV1;\n\t\treadonly scopeRef: string;\n\t},\n): boolean {\n\treturn (\n\t\trecord.pluginId === input.pluginId &&\n\t\trecord.pluginVersion === input.pluginVersion &&\n\t\trecord.manifestDigest === input.manifestDigest &&\n\t\trecord.scope === input.scope &&\n\t\trecord.scopeRef === input.scopeRef\n\t);\n}\n\nfunction artifactIdentityMatchesRecord(artifact: PluginDistributionArtifactV1, record: PluginUpdateRecordV1): boolean {\n\treturn (\n\t\tartifact.version === 1 &&\n\t\tartifact.pluginId === record.pluginId &&\n\t\tartifact.pluginVersion === record.pluginVersion &&\n\t\tartifact.manifestDigest === record.manifestDigest\n\t);\n}\n\nfunction expectedArtifactDigest(value: string): { readonly algorithm: \"sha256\" | \"sha512\"; readonly hex: string } {\n\tconst prefixed = /^(sha256|sha512):([0-9a-f]+)$/iu.exec(value);\n\tif (prefixed !== null) {\n\t\tconst algorithm = prefixed[1]!.toLowerCase() as \"sha256\" | \"sha512\";\n\t\tconst hex = prefixed[2]!.toLowerCase();\n\t\tif ((algorithm === \"sha256\" && hex.length === 64) || (algorithm === \"sha512\" && hex.length === 128))\n\t\t\treturn { algorithm, hex };\n\t}\n\tif (/^[0-9a-f]{64}$/iu.test(value)) return { algorithm: \"sha256\", hex: value.toLowerCase() };\n\tif (/^[0-9a-f]{128}$/iu.test(value)) return { algorithm: \"sha512\", hex: value.toLowerCase() };\n\tthrow new NodePackageInstallErrorV1(\"invalid_artifact\");\n}\n\nasync function collectArtifact(\n\topenArtifact: NodePackageInstallHostOptionsV1[\"openArtifact\"],\n\tartifact: PluginDistributionArtifactV1,\n\tmaxBytes: number,\n\tsignal: AbortSignal,\n): Promise<Uint8Array> {\n\tconst chunks: Uint8Array[] = [];\n\tlet total = 0;\n\tfor await (const chunk of openArtifact({ artifact, signal })) {\n\t\tif (signal.aborted) throw new NodePackageInstallErrorV1(\"cancelled\");\n\t\tif (!(chunk instanceof Uint8Array)) throw new NodePackageInstallErrorV1(\"invalid_artifact\");\n\t\tif (chunk.byteLength > maxBytes - total) throw new NodePackageInstallErrorV1(\"archive_budget_exceeded\");\n\t\tchunks.push(new Uint8Array(chunk));\n\t\ttotal += chunk.byteLength;\n\t}\n\tconst result = new Uint8Array(total);\n\tlet offset = 0;\n\tfor (const chunk of chunks) {\n\t\tresult.set(chunk, offset);\n\t\toffset += chunk.byteLength;\n\t}\n\treturn result;\n}\n\nfunction packagePath(root: string, key: string): string {\n\treturn join(root, createHash(\"sha256\").update(key).digest(\"hex\"));\n}\n\nasync function writeEntries(\n\troot: string,\n\tentries: readonly NodeZipPackageEntryV1[],\n\tsignal: AbortSignal,\n): Promise<void> {\n\tawait mkdir(root, { recursive: true, mode: 0o700 });\n\tfor (const entry of entries) {\n\t\tif (signal.aborted) throw new NodePackageInstallErrorV1(\"cancelled\");\n\t\tconst target = join(root, ...entry.name.split(\"/\"));\n\t\tif (entry.directory) {\n\t\t\tawait mkdir(target, { recursive: true, mode: 0o700 });\n\t\t\tcontinue;\n\t\t}\n\t\tconst parent = join(root, ...entry.name.split(\"/\").slice(0, -1));\n\t\tawait mkdir(parent, { recursive: true, mode: 0o700 });\n\t\tawait writeFile(target, entry.bytes, { flag: \"wx\", mode: 0o600 });\n\t}\n}\n\n/**\n * Host-owned ZIP/tgz package installer. It extracts only a bounded,\n * non-encrypted ZIP (\"application/zip\") or gzip-compressed tar\n * (\"application/gzip\") package and never executes package contents. The\n * returned installation handle is opaque; paths stay inside the host-owned\n * instance directory.\n */\nexport async function createNodePackageInstallHostV1(\n\toptions: NodePackageInstallHostOptionsV1,\n): Promise<NodePackageInstallHostV1> {\n\tif (typeof options.openArtifact !== \"function\") throw new Error(\"openArtifact is required\");\n\tconst limits = resolveNodePackageLimitsV1(options);\n\tconst parent = options.installationDirectory ?? tmpdir();\n\tif (!isAbsolute(parent)) throw new Error(\"installationDirectory must be absolute\");\n\tif (options.persistence !== undefined && options.persistence !== \"ephemeral\" && options.persistence !== \"persistent\")\n\t\tthrow new Error(\"persistence mode is invalid\");\n\tconst supportedScopes = options.supportedScopes ?? [\"global\", \"project\"];\n\tif (\n\t\t!Array.isArray(supportedScopes) ||\n\t\tsupportedScopes.length === 0 ||\n\t\tnew Set(supportedScopes).size !== supportedScopes.length ||\n\t\tsupportedScopes.some((scope) => scope !== \"global\" && scope !== \"project\")\n\t)\n\t\tthrow new Error(\"supportedScopes is invalid\");\n\tawait mkdir(parent, { recursive: true, mode: 0o700 });\n\tconst persistent = options.persistence === \"persistent\";\n\tconst instanceRoot = persistent\n\t\t? join(parent, \"agent-forge-plugin-install-store-v1\")\n\t\t: await mkdtemp(join(parent, \"agent-forge-plugin-install-\"));\n\tconst persistence: NodePackageInstallPersistenceV1 | undefined = persistent\n\t\t? await createNodePackageInstallPersistenceV1({\n\t\t\t\trootDirectory: instanceRoot,\n\t\t\t\tlockTimeoutMs: options.persistenceLockTimeoutMs,\n\t\t\t})\n\t\t: undefined;\n\tconst records = new Map<string, InstalledRecordV1>();\n\tconst activeByKey = new Map<string, string>();\n\tif (persistence !== undefined) {\n\t\tfor (const record of await persistence.load()) records.set(record.installationRef, { ...record });\n\t}\n\tfor (const record of records.values()) {\n\t\tif (record.state === \"active\")\n\t\t\tactiveByKey.set(installationKey(record.scope, record.scopeRef, record.pluginId), record.installationRef);\n\t}\n\tlet closed = false;\n\tconst activeOperations = new Set<AbortController>();\n\tconst runtimeHandles = new Map<string, { readonly dispose?: () => void | Promise<void> }>();\n\tconst applyPersistentSnapshot = (snapshot: readonly NodePackageInstallPersistentRecordV1[]): void => {\n\t\trecords.clear();\n\t\tactiveByKey.clear();\n\t\tfor (const value of snapshot) records.set(value.installationRef, { ...value });\n\t\tfor (const value of records.values()) {\n\t\t\tif (value.state === \"active\")\n\t\t\t\tactiveByKey.set(installationKey(value.scope, value.scopeRef, value.pluginId), value.installationRef);\n\t\t}\n\t};\n\tconst commitRecords = async (\n\t\tupdate: (values: Map<string, NodePackageInstallPersistentRecordV1>) => void | Promise<void>,\n\t): Promise<void> => {\n\t\tif (persistence === undefined) {\n\t\t\tawait update(records);\n\t\t\treturn;\n\t\t}\n\t\tapplyPersistentSnapshot(await persistence.mutate({ update }));\n\t};\n\tlet drained: (() => void) | undefined;\n\tlet closePromise: Promise<void> | undefined;\n\n\tconst begin = (\n\t\tparentSignal: AbortSignal | undefined,\n\t): { readonly controller: AbortController; readonly signal: AbortSignal } => {\n\t\tif (closed) throw new NodePackageInstallErrorV1(\"host_closed\");\n\t\tconst controller = new AbortController();\n\t\tif (parentSignal?.aborted) controller.abort(parentSignal.reason);\n\t\telse parentSignal?.addEventListener(\"abort\", () => controller.abort(parentSignal.reason), { once: true });\n\t\tactiveOperations.add(controller);\n\t\treturn { controller, signal: controller.signal };\n\t};\n\tconst end = (controller: AbortController): void => {\n\t\tactiveOperations.delete(controller);\n\t\tif (activeOperations.size === 0) {\n\t\t\tdrained?.();\n\t\t\tdrained = undefined;\n\t\t}\n\t};\n\tconst waitDrained = async (): Promise<void> => {\n\t\tif (activeOperations.size === 0) return;\n\t\tawait new Promise<void>((resolve) => {\n\t\t\tdrained = resolve;\n\t\t});\n\t};\n\tconst getRecord = (\n\t\tinstallation: PluginInstallationV1,\n\t\trecord: PluginUpdateRecordV1,\n\t\tscope: PluginInstallationScopeV1,\n\t\tscopeRef: string,\n\t): InstalledRecordV1 => {\n\t\tconst value = records.get(installation.installationRef);\n\t\tif (\n\t\t\tvalue === undefined ||\n\t\t\tinstallation.pluginId !== record.pluginId ||\n\t\t\tinstallation.pluginVersion !== record.pluginVersion ||\n\t\t\tinstallation.manifestDigest !== record.manifestDigest ||\n\t\t\tinstallation.scope !== scope ||\n\t\t\tinstallation.scopeRef !== scopeRef ||\n\t\t\t!identityMatches(value, {\n\t\t\t\tpluginId: record.pluginId,\n\t\t\t\tpluginVersion: record.pluginVersion,\n\t\t\t\tmanifestDigest: record.manifestDigest,\n\t\t\t\tscope,\n\t\t\t\tscopeRef,\n\t\t\t})\n\t\t)\n\t\t\tthrow new NodePackageInstallErrorV1(\"installation_identity_mismatch\");\n\t\treturn value;\n\t};\n\tconst disposeRuntime = async (installationRef: string): Promise<void> => {\n\t\tconst handle = runtimeHandles.get(installationRef);\n\t\tif (handle === undefined) return;\n\t\tif (handle.dispose !== undefined) await Promise.resolve(handle.dispose());\n\t\truntimeHandles.delete(installationRef);\n\t};\n\tconst loadRuntime = async (\n\t\tinstallation: PluginInstallationV1,\n\t\trecord: PluginUpdateRecordV1 | undefined,\n\t\tdirectory: string,\n\t\tsignal: AbortSignal,\n\t): Promise<{ readonly dispose?: () => void | Promise<void> } | undefined> => {\n\t\tif (options.loadPlugin === undefined) return undefined;\n\t\ttry {\n\t\t\tconst loaded = await options.loadPlugin({\n\t\t\t\tinstallation,\n\t\t\t\trecord,\n\t\t\t\tdirectory,\n\t\t\t\tmanifestPath: join(directory, CAPABILITY_MANIFEST_FILE_V1),\n\t\t\t\tsignal,\n\t\t\t});\n\t\t\treturn loaded ?? undefined;\n\t\t} catch (error) {\n\t\t\tif (error instanceof NodePackageInstallErrorV1) throw error;\n\t\t\tthrow new NodePackageInstallErrorV1(\"installation_io_failed\");\n\t\t}\n\t};\n\n\tconst host: NodePackageInstallHostV1 = {\n\t\tversion: NODE_PACKAGE_INSTALL_HOST_VERSION_V1,\n\t\tsupportedScopes: Object.freeze([...supportedScopes]),\n\t\tgetInstallationDirectory(input) {\n\t\t\tconst record = records.get(input.installation.installationRef);\n\t\t\tif (\n\t\t\t\trecord === undefined ||\n\t\t\t\trecord.pluginId !== input.installation.pluginId ||\n\t\t\t\trecord.pluginVersion !== input.installation.pluginVersion ||\n\t\t\t\trecord.manifestDigest !== input.installation.manifestDigest ||\n\t\t\t\trecord.scope !== input.installation.scope ||\n\t\t\t\trecord.scopeRef !== input.installation.scopeRef\n\t\t\t)\n\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_unknown\");\n\t\t\treturn record.directory;\n\t\t},\n\t\tasync install(input) {\n\t\t\tconst operation = begin(input.signal);\n\t\t\tlet staging: string | undefined;\n\t\t\ttry {\n\t\t\t\tif (input.artifact === undefined) throw new NodePackageInstallErrorV1(\"invalid_artifact\");\n\t\t\t\tif (!artifactIdentityMatchesRecord(input.artifact, input.record))\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"invalid_artifact\");\n\t\t\t\tconst mediaType = input.artifact.mediaType.toLowerCase();\n\t\t\t\tif (mediaType !== \"application/zip\" && mediaType !== \"application/gzip\")\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"unsupported_media_type\");\n\t\t\t\tconst scopeRef = opaqueScope(input.scopeRef);\n\t\t\t\tconst bytes = await collectArtifact(\n\t\t\t\t\toptions.openArtifact,\n\t\t\t\t\tinput.artifact,\n\t\t\t\t\tlimits.maxArchiveBytes,\n\t\t\t\t\toperation.signal,\n\t\t\t\t);\n\t\t\t\tif (input.artifact.sizeBytes !== bytes.byteLength) throw new NodePackageInstallErrorV1(\"invalid_artifact\");\n\t\t\t\tconst digest = expectedArtifactDigest(input.artifact.artifactDigest);\n\t\t\t\tif (createHash(digest.algorithm).update(bytes).digest(\"hex\") !== digest.hex)\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"invalid_artifact\");\n\t\t\t\tconst entries =\n\t\t\t\t\tmediaType === \"application/gzip\"\n\t\t\t\t\t\t? await parseNodeTarGzPackageV1(bytes, limits)\n\t\t\t\t\t\t: parseNodeZipPackageV1(bytes, limits);\n\t\t\t\tconst manifestEntry = entries.find(\n\t\t\t\t\t(entry) => entry.name === ECOSYSTEM_MANIFEST_FILE_V1 && !entry.directory,\n\t\t\t\t);\n\t\t\t\tif (manifestEntry === undefined) throw new NodePackageInstallErrorV1(\"package_manifest_missing\");\n\t\t\t\tif (manifestEntry.bytes.byteLength > MAX_PACKAGE_MANIFEST_BYTES_V1)\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\t\t\t\tlet manifest: unknown;\n\t\t\t\ttry {\n\t\t\t\t\tmanifest = JSON.parse(new TextDecoder(\"utf-8\", { fatal: true }).decode(manifestEntry.bytes));\n\t\t\t\t} catch {\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\t\t\t\t}\n\t\t\t\tpackageManifest(manifest, input.record);\n\t\t\t\tconst capabilityEntry = entries.find(\n\t\t\t\t\t(entry) => entry.name === CAPABILITY_MANIFEST_FILE_V1 && !entry.directory,\n\t\t\t\t);\n\t\t\t\tif (capabilityEntry === undefined) throw new NodePackageInstallErrorV1(\"package_manifest_missing\");\n\t\t\t\tif (capabilityEntry.bytes.byteLength > MAX_PACKAGE_MANIFEST_BYTES_V1)\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\t\t\t\tlet capability: unknown;\n\t\t\t\ttry {\n\t\t\t\t\tcapability = JSON.parse(new TextDecoder(\"utf-8\", { fatal: true }).decode(capabilityEntry.bytes));\n\t\t\t\t} catch {\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\t\t\t\t}\n\t\t\t\tcapabilityManifest(capability, input.record, entries);\n\t\t\t\tconst key = installationKey(input.scope, scopeRef, input.record.pluginId);\n\t\t\t\tstaging = join(instanceRoot, `.staging-${randomUUID()}`);\n\t\t\t\tawait writeEntries(staging, entries, operation.signal);\n\t\t\t\tconst installRef = `plugin-install://node-zip/${randomUUID()}`;\n\t\t\t\tconst destination = packagePath(instanceRoot, `${key}\\u0000${installRef}`);\n\t\t\t\tawait rename(staging, destination);\n\t\t\t\tstaging = undefined;\n\t\t\t\tconst installedRecord: InstalledRecordV1 = {\n\t\t\t\t\tinstallationRef: installRef,\n\t\t\t\t\tdirectory: destination,\n\t\t\t\t\tpluginId: input.record.pluginId,\n\t\t\t\t\tpluginVersion: input.record.pluginVersion,\n\t\t\t\t\tmanifestDigest: input.record.manifestDigest,\n\t\t\t\t\tscope: input.scope,\n\t\t\t\t\tscopeRef,\n\t\t\t\t\tstate: \"installed\",\n\t\t\t\t};\n\t\t\t\ttry {\n\t\t\t\t\tawait commitRecords((values) => {\n\t\t\t\t\t\tvalues.set(installRef, installedRecord);\n\t\t\t\t\t});\n\t\t\t\t} catch (error) {\n\t\t\t\t\tawait rm(destination, { recursive: true, force: true }).catch(() => undefined);\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t\treturn { installationRef: installRef, scope: input.scope, scopeRef };\n\t\t\t} catch (error) {\n\t\t\t\tif (operation.signal.aborted || input.signal?.aborted) throw new NodePackageInstallErrorV1(\"cancelled\");\n\t\t\t\tif (error instanceof NodePackageInstallErrorV1) throw error;\n\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_io_failed\");\n\t\t\t} finally {\n\t\t\t\tif (staging !== undefined) await rm(staging, { recursive: true, force: true }).catch(() => undefined);\n\t\t\t\tend(operation.controller);\n\t\t\t}\n\t\t},\n\t\tasync activate(input) {\n\t\t\tconst operation = begin(input.signal);\n\t\t\tlet candidate: { readonly dispose?: () => void | Promise<void> } | undefined;\n\t\t\ttry {\n\t\t\t\tconst scopeRef = opaqueScope(input.scopeRef);\n\t\t\t\tconst record = getRecord(input.installation, input.record, input.scope, scopeRef);\n\t\t\t\tif (record.state !== \"installed\") throw new NodePackageInstallErrorV1(\"installation_identity_mismatch\");\n\t\t\t\tcandidate = await loadRuntime(input.installation, input.record, record.directory, operation.signal);\n\t\t\t\tconst key = installationKey(input.scope, scopeRef, input.record.pluginId);\n\t\t\t\tconst previousRef = activeByKey.get(key);\n\t\t\t\tif (previousRef !== undefined) {\n\t\t\t\t\tconst previous = records.get(previousRef);\n\t\t\t\t\tif (previous !== undefined) {\n\t\t\t\t\t\tawait disposeRuntime(previousRef);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tawait commitRecords((values) => {\n\t\t\t\t\tconst candidateRecord = values.get(input.installation.installationRef);\n\t\t\t\t\tif (candidateRecord === undefined) throw new NodePackageInstallErrorV1(\"installation_identity_mismatch\");\n\t\t\t\t\tif (previousRef !== undefined) {\n\t\t\t\t\t\tconst previousRecord = values.get(previousRef);\n\t\t\t\t\t\tif (previousRecord !== undefined) values.set(previousRef, { ...previousRecord, state: \"retained\" });\n\t\t\t\t\t}\n\t\t\t\t\tvalues.set(input.installation.installationRef, { ...candidateRecord, state: \"active\" });\n\t\t\t\t});\n\t\t\t\tactiveByKey.set(key, input.installation.installationRef);\n\t\t\t\tif (candidate !== undefined) runtimeHandles.set(input.installation.installationRef, candidate);\n\t\t\t\tcandidate = undefined;\n\t\t\t} catch (error) {\n\t\t\t\tif (candidate?.dispose !== undefined) await Promise.resolve(candidate.dispose()).catch(() => undefined);\n\t\t\t\tif (error instanceof NodePackageInstallErrorV1) throw error;\n\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_io_failed\");\n\t\t\t} finally {\n\t\t\t\tend(operation.controller);\n\t\t\t}\n\t\t},\n\t\tasync rollback(input) {\n\t\t\tconst operation = begin(input.signal);\n\t\t\tlet candidate: { readonly dispose?: () => void | Promise<void> } | undefined;\n\t\t\ttry {\n\t\t\t\tconst scopeRef = opaqueScope(input.scopeRef);\n\t\t\t\tconst failed = records.get(input.failed.installationRef);\n\t\t\t\tif (\n\t\t\t\t\tfailed === undefined ||\n\t\t\t\t\t!identityMatches(failed, {\n\t\t\t\t\t\tpluginId: input.failed.pluginId,\n\t\t\t\t\t\tpluginVersion: input.failed.pluginVersion,\n\t\t\t\t\t\tmanifestDigest: input.failed.manifestDigest,\n\t\t\t\t\t\tscope: input.scope,\n\t\t\t\t\t\tscopeRef,\n\t\t\t\t\t})\n\t\t\t\t)\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_identity_mismatch\");\n\t\t\t\tconst previous = records.get(input.previous.installationRef);\n\t\t\t\tif (\n\t\t\t\t\tprevious === undefined ||\n\t\t\t\t\t!identityMatches(previous, {\n\t\t\t\t\t\tpluginId: input.previous.pluginId,\n\t\t\t\t\t\tpluginVersion: input.previous.pluginVersion,\n\t\t\t\t\t\tmanifestDigest: input.previous.manifestDigest,\n\t\t\t\t\t\tscope: input.scope,\n\t\t\t\t\t\tscopeRef,\n\t\t\t\t\t}) ||\n\t\t\t\t\t(previous.state !== \"active\" && previous.state !== \"retained\") ||\n\t\t\t\t\tfailed.pluginId !== previous.pluginId\n\t\t\t\t)\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_identity_mismatch\");\n\t\t\t\tconst previousInstallation: PluginInstallationV1 = {\n\t\t\t\t\tversion: 1,\n\t\t\t\t\tpluginId: previous.pluginId,\n\t\t\t\t\tpluginVersion: previous.pluginVersion,\n\t\t\t\t\tmanifestDigest: previous.manifestDigest,\n\t\t\t\t\tscope: previous.scope,\n\t\t\t\t\tscopeRef: previous.scopeRef,\n\t\t\t\t\tinstallationRef: input.previous.installationRef,\n\t\t\t\t\tstate: \"active\",\n\t\t\t\t};\n\t\t\t\tcandidate = await loadRuntime(previousInstallation, undefined, previous.directory, operation.signal);\n\t\t\t\tawait disposeRuntime(input.failed.installationRef);\n\t\t\t\tawait rm(failed.directory, { recursive: true, force: true });\n\t\t\t\tawait commitRecords((values) => {\n\t\t\t\t\tvalues.delete(input.failed.installationRef);\n\t\t\t\t\tconst previousRecord = values.get(input.previous.installationRef);\n\t\t\t\t\tif (previousRecord !== undefined)\n\t\t\t\t\t\tvalues.set(input.previous.installationRef, { ...previousRecord, state: \"active\" });\n\t\t\t\t});\n\t\t\t\tactiveByKey.set(installationKey(input.scope, scopeRef, failed.pluginId), input.previous.installationRef);\n\t\t\t\tif (candidate !== undefined) runtimeHandles.set(input.previous.installationRef, candidate);\n\t\t\t\tcandidate = undefined;\n\t\t\t} catch (error) {\n\t\t\t\tif (candidate?.dispose !== undefined) await Promise.resolve(candidate.dispose()).catch(() => undefined);\n\t\t\t\tif (error instanceof NodePackageInstallErrorV1) throw error;\n\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_io_failed\");\n\t\t\t} finally {\n\t\t\t\tend(operation.controller);\n\t\t\t}\n\t\t},\n\t\tasync uninstall(input) {\n\t\t\tconst operation = begin(input.signal);\n\t\t\ttry {\n\t\t\t\tconst scopeRef = opaqueScope(input.scopeRef);\n\t\t\t\tconst record = records.get(input.installation.installationRef);\n\t\t\t\tif (\n\t\t\t\t\trecord === undefined ||\n\t\t\t\t\t!identityMatches(record, {\n\t\t\t\t\t\tpluginId: input.installation.pluginId,\n\t\t\t\t\t\tpluginVersion: input.installation.pluginVersion,\n\t\t\t\t\t\tmanifestDigest: input.installation.manifestDigest,\n\t\t\t\t\t\tscope: input.scope,\n\t\t\t\t\t\tscopeRef,\n\t\t\t\t\t})\n\t\t\t\t)\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_unknown\");\n\t\t\t\tawait disposeRuntime(input.installation.installationRef);\n\t\t\t\tawait rm(record.directory, { recursive: true, force: false });\n\t\t\t\tawait commitRecords((values) => {\n\t\t\t\t\tvalues.delete(input.installation.installationRef);\n\t\t\t\t});\n\t\t\t\tconst key = installationKey(input.scope, scopeRef, record.pluginId);\n\t\t\t\tif (activeByKey.get(key) === input.installation.installationRef) activeByKey.delete(key);\n\t\t\t} finally {\n\t\t\t\tend(operation.controller);\n\t\t\t}\n\t\t},\n\t\tasync close() {\n\t\t\tif (closePromise !== undefined) return await closePromise;\n\t\t\tclosed = true;\n\t\t\tfor (const operation of activeOperations) operation.abort();\n\t\t\tclosePromise = (async () => {\n\t\t\t\tawait waitDrained();\n\t\t\t\tlet disposeError = false;\n\t\t\t\tfor (const installationRef of runtimeHandles.keys()) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait disposeRuntime(installationRef);\n\t\t\t\t\t} catch {\n\t\t\t\t\t\tdisposeError = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (persistence === undefined) await rm(instanceRoot, { recursive: true, force: true });\n\t\t\t\telse await persistence.close();\n\t\t\t\trecords.clear();\n\t\t\t\tactiveByKey.clear();\n\t\t\t\tif (disposeError) throw new NodePackageInstallErrorV1(\"installation_io_failed\");\n\t\t\t})();\n\t\t\treturn await closePromise;\n\t\t},\n\t};\n\tif (options.loadPlugin !== undefined) {\n\t\ttry {\n\t\t\tfor (const record of records.values()) {\n\t\t\t\tif (record.state !== \"active\") continue;\n\t\t\t\tconst installation: PluginInstallationV1 = {\n\t\t\t\t\tversion: 1,\n\t\t\t\t\tpluginId: record.pluginId,\n\t\t\t\t\tpluginVersion: record.pluginVersion,\n\t\t\t\t\tmanifestDigest: record.manifestDigest,\n\t\t\t\t\tscope: record.scope,\n\t\t\t\t\tscopeRef: record.scopeRef,\n\t\t\t\t\tinstallationRef: record.installationRef,\n\t\t\t\t\tstate: \"active\",\n\t\t\t\t};\n\t\t\t\tconst runtime = await loadRuntime(installation, undefined, record.directory, new AbortController().signal);\n\t\t\t\tif (runtime !== undefined) runtimeHandles.set(record.installationRef, runtime);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tawait host.close().catch(() => undefined);\n\t\t\tif (error instanceof NodePackageInstallErrorV1) throw error;\n\t\t\tthrow new NodePackageInstallErrorV1(\"installation_io_failed\");\n\t\t}\n\t}\n\treturn host;\n}\n"]}
1
+ {"version":3,"file":"node-package-install-host.d.ts","sourceRoot":"","sources":["../../../src/plugins/ecosystem/node-package-install-host.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,KAAK,EAAE,yBAAyB,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAO3G,OAAO,KAAK,EAAoB,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAGzE,QAAA,MAAM,oCAAoC,GAAa,CAAC;AACxD,eAAO,MAAM,8BAA8B,GAAa,CAAC;AAmBzD,MAAM,MAAM,6BAA6B,GACtC,WAAW,GACX,aAAa,GACb,kBAAkB,GAClB,wBAAwB,GACxB,iBAAiB,GACjB,yBAAyB,GACzB,0BAA0B,GAC1B,0BAA0B,GAC1B,2BAA2B,GAC3B,sBAAsB,GACtB,gCAAgC,GAChC,wBAAwB,CAAC;AAE5B,qBAAa,yBAA0B,SAAQ,KAAK;IACnD,QAAQ,CAAC,IAAI,EAAE,6BAA6B,CAAC;IAE7C,YAAY,IAAI,EAAE,6BAA6B,EAI9C;CACD;AAED,MAAM,WAAW,qBAAqB;IACrC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;CAC3B;AAED,MAAM,WAAW,sBAAsB;IACtC,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;IAClC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IACpC,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,+B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AC,+BAA+B,CAAC,YAAY,CAAC,CAAC,CAK5D;AAED;;;;GAIG;AACH,wBAAsB,wCAAwC,CAC7D,OAAO,EAAE,+BAA+B,EACxC,OAAO,EAAE,2CAA2C,GAClD,OAAO,CAAC,wBAAwB,CAAC,CAMnC;AAED,MAAM,WAAW,wBAAyB,SAAQ,mBAAmB;IACpE,QAAQ,CAAC,OAAO,EAAE,OAAO,oCAAoC,CAAC;IAC9D,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC,mBAAmB,CAAC,WAAW,CAAC,CAAC,CAAC;IAClE,+FAA+F;IAC/F,QAAQ,CAAC,wBAAwB,EAAE,CAAC,KAAK,EAAE;QAAE,QAAQ,CAAC,YAAY,EAAE,oBAAoB,CAAA;KAAE,KAAK,MAAM,CAAC;IACtG,QAAQ,CAAC,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACpC;AAoCD,mFAAmF;AACnF,wBAAgB,0BAA0B,CAAC,WAAW,EAAE,sBAAsB,GAAG,QAAQ,CAAC,sBAAsB,CAAC,CAsBhH;AAsBD,+EAA+E;AAC/E,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAA;CAAE,CAqBzG;AA2LD,wBAAgB,qBAAqB,CACpC,KAAK,EAAE,UAAU,EACjB,WAAW,GAAE,sBAA2B,GACtC,SAAS,qBAAqB,EAAE,CAclC;AAkKD;;;;;;GAMG;AACH,wBAAsB,8BAA8B,CACnD,OAAO,EAAE,+BAA+B,GACtC,OAAO,CAAC,wBAAwB,CAAC,CA+ZnC","sourcesContent":["import { createHash, randomUUID } from \"node:crypto\";\nimport { mkdir, mkdtemp, rename, rm, writeFile } from \"node:fs/promises\";\nimport { tmpdir } from \"node:os\";\nimport { isAbsolute, join } from \"node:path\";\nimport { inflateRawSync } from \"node:zlib\";\nimport { parseCapabilityPluginManifest } from \"../../core/extensions/capability-plugin.ts\";\nimport type { PluginDistributionArtifactV1 } from \"./distribution.ts\";\nimport type { PluginInstallationScopeV1, PluginInstallationV1, PluginInstallHostV1 } from \"./installer.ts\";\nimport {\n\tcreateNodePackageInstallPersistenceV1,\n\ttype NodePackageInstallPersistenceV1,\n\ttype NodePackageInstallPersistentRecordV1,\n} from \"./node-package-install-persistence.ts\";\nimport { parseNodeTarGzPackageV1 } from \"./node-tar-package.ts\";\nimport type { PluginManifestV1, PluginUpdateRecordV1 } from \"./types.ts\";\nimport { pluginManifestDigestV1 } from \"./updates.ts\";\n\nconst NODE_PACKAGE_INSTALL_HOST_VERSION_V1 = 1 as const;\nexport const NODE_PACKAGE_FORMAT_VERSION_V1 = 1 as const;\nconst DEFAULT_MAX_ARCHIVE_BYTES_V1 = 128 * 1024 * 1024;\nconst MAX_ARCHIVE_BYTES_V1 = 512 * 1024 * 1024;\nconst DEFAULT_MAX_EXTRACTED_BYTES_V1 = 256 * 1024 * 1024;\nconst MAX_EXTRACTED_BYTES_V1 = 1024 * 1024 * 1024;\nconst DEFAULT_MAX_ENTRY_BYTES_V1 = 64 * 1024 * 1024;\nconst MAX_ENTRY_BYTES_V1 = 256 * 1024 * 1024;\nconst DEFAULT_MAX_ENTRIES_V1 = 2048;\nconst MAX_ENTRIES_V1 = 8192;\nconst MAX_PACKAGE_MANIFEST_BYTES_V1 = 64 * 1024;\nconst ECOSYSTEM_MANIFEST_FILE_V1 = \"agent-forge.json\";\nconst CAPABILITY_MANIFEST_FILE_V1 = \"plugin.json\";\n\nconst LOCAL_FILE_HEADER_V1 = 0x04034b50;\nconst CENTRAL_FILE_HEADER_V1 = 0x02014b50;\nconst END_OF_CENTRAL_DIRECTORY_V1 = 0x06054b50;\nconst ZIP64_END_OF_CENTRAL_DIRECTORY_V1 = 0x06064b50;\nconst ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_V1 = 0x07064b50;\n\nexport type NodePackageInstallErrorCodeV1 =\n\t| \"cancelled\"\n\t| \"host_closed\"\n\t| \"invalid_artifact\"\n\t| \"unsupported_media_type\"\n\t| \"archive_invalid\"\n\t| \"archive_budget_exceeded\"\n\t| \"package_manifest_missing\"\n\t| \"package_manifest_invalid\"\n\t| \"package_manifest_mismatch\"\n\t| \"installation_unknown\"\n\t| \"installation_identity_mismatch\"\n\t| \"installation_io_failed\";\n\nexport class NodePackageInstallErrorV1 extends Error {\n\treadonly code: NodePackageInstallErrorCodeV1;\n\n\tconstructor(code: NodePackageInstallErrorCodeV1) {\n\t\tsuper(`Node package installation failed: ${code}`);\n\t\tthis.name = \"NodePackageInstallErrorV1\";\n\t\tthis.code = code;\n\t}\n}\n\nexport interface NodeZipPackageEntryV1 {\n\treadonly name: string;\n\treadonly directory: boolean;\n\treadonly bytes: Uint8Array;\n}\n\nexport interface NodeZipPackageLimitsV1 {\n\treadonly maxArchiveBytes?: number;\n\treadonly maxExtractedBytes?: number;\n\treadonly maxEntryBytes?: number;\n\treadonly maxEntries?: number;\n}\n\nexport interface NodePackageInstallHostOptionsV1 extends NodeZipPackageLimitsV1 {\n\t/** An opaque artifact reader owned by the distribution host. */\n\treadonly openArtifact: (input: {\n\t\treadonly artifact: PluginDistributionArtifactV1;\n\t\treadonly signal?: AbortSignal;\n\t}) => AsyncIterable<Uint8Array>;\n\t/** Parent directory owned by the host; no local path leaves this adapter. */\n\treadonly installationDirectory?: string;\n\treadonly supportedScopes?: readonly PluginInstallationScopeV1[];\n\t/** Persistent host-owned index; omitted keeps the test-friendly ephemeral mode. */\n\treadonly persistence?: \"ephemeral\" | \"persistent\";\n\treadonly persistenceLockTimeoutMs?: number;\n\t/**\n\t * Host-only runtime assembly seam. The directory and manifest path never\n\t * cross the public installer DTO or model/tool context.\n\t */\n\treadonly loadPlugin?: (input: {\n\t\treadonly installation: PluginInstallationV1;\n\t\treadonly record?: PluginUpdateRecordV1;\n\t\treadonly directory: string;\n\t\treadonly manifestPath: string;\n\t\treadonly signal: AbortSignal;\n\t}) => Promise<{ readonly dispose?: () => void | Promise<void> } | void>;\n}\n\n/** Minimal structural bridge for an AgentSession-owned capability loader. */\nexport interface NodePackageInstallCapabilityPluginSessionV1 {\n\treadonly loadCapabilityPluginManifest: (manifestPath: string) => Promise<{\n\t\treadonly dispose: () => void | Promise<void>;\n\t}>;\n}\n\n/**\n * Adapt an AgentSession-like loader to the host-only runtime seam. The\n * manifest path is never returned as a DTO or sent through model context.\n */\nexport function createNodePackageInstallSessionLoaderV1(\n\tsession: NodePackageInstallCapabilityPluginSessionV1,\n): NonNullable<NodePackageInstallHostOptionsV1[\"loadPlugin\"]> {\n\treturn async ({ manifestPath }) => {\n\t\tconst handle = await session.loadCapabilityPluginManifest(manifestPath);\n\t\treturn { dispose: handle.dispose };\n\t};\n}\n\n/**\n * Create a package host with an explicit AgentSession-compatible runtime\n * loader. This is an opt-in composition helper; it does not alter Core's\n * default session construction or install untrusted package code.\n */\nexport async function createNodePackageInstallHostForSessionV1(\n\toptions: NodePackageInstallHostOptionsV1,\n\tsession: NodePackageInstallCapabilityPluginSessionV1,\n): Promise<NodePackageInstallHostV1> {\n\tif (options.loadPlugin !== undefined) throw new Error(\"loadPlugin must be omitted when binding a session\");\n\treturn await createNodePackageInstallHostV1({\n\t\t...options,\n\t\tloadPlugin: createNodePackageInstallSessionLoaderV1(session),\n\t});\n}\n\nexport interface NodePackageInstallHostV1 extends PluginInstallHostV1 {\n\treadonly version: typeof NODE_PACKAGE_INSTALL_HOST_VERSION_V1;\n\treadonly uninstall: NonNullable<PluginInstallHostV1[\"uninstall\"]>;\n\t/** Host-only bridge for local runtime assembly; never put this path in a DTO or transcript. */\n\treadonly getInstallationDirectory: (input: { readonly installation: PluginInstallationV1 }) => string;\n\treadonly close: () => Promise<void>;\n}\n\ninterface ZipEntryDescriptorV1 {\n\treadonly name: string;\n\treadonly directory: boolean;\n\treadonly flags: number;\n\treadonly method: number;\n\treadonly compressedSize: number;\n\treadonly uncompressedSize: number;\n\treadonly crc32: number;\n\treadonly localOffset: number;\n}\n\ninterface ZipLocalSpanV1 {\n\treadonly start: number;\n\treadonly end: number;\n}\n\ninterface InstalledRecordV1 {\n\treadonly installationRef: string;\n\treadonly directory: string;\n\treadonly pluginId: string;\n\treadonly pluginVersion: string;\n\treadonly manifestDigest: string;\n\treadonly scope: PluginInstallationScopeV1;\n\treadonly scopeRef: string;\n\tstate: \"installed\" | \"active\" | \"retained\";\n}\n\nfunction positiveLimit(value: number | undefined, fallback: number, maximum: number, label: string): number {\n\tconst result = value ?? fallback;\n\tif (!Number.isSafeInteger(result) || result < 1 || result > maximum)\n\t\tthrow new Error(`${label} must be a positive integer at most ${maximum}`);\n\treturn result;\n}\n\n/** Resolve the archive budget limits shared by the ZIP and tgz package parsers. */\nexport function resolveNodePackageLimitsV1(limitsInput: NodeZipPackageLimitsV1): Required<NodeZipPackageLimitsV1> {\n\treturn {\n\t\tmaxArchiveBytes: positiveLimit(\n\t\t\tlimitsInput.maxArchiveBytes,\n\t\t\tDEFAULT_MAX_ARCHIVE_BYTES_V1,\n\t\t\tMAX_ARCHIVE_BYTES_V1,\n\t\t\t\"maxArchiveBytes\",\n\t\t),\n\t\tmaxExtractedBytes: positiveLimit(\n\t\t\tlimitsInput.maxExtractedBytes,\n\t\t\tDEFAULT_MAX_EXTRACTED_BYTES_V1,\n\t\t\tMAX_EXTRACTED_BYTES_V1,\n\t\t\t\"maxExtractedBytes\",\n\t\t),\n\t\tmaxEntryBytes: positiveLimit(\n\t\t\tlimitsInput.maxEntryBytes,\n\t\t\tDEFAULT_MAX_ENTRY_BYTES_V1,\n\t\t\tMAX_ENTRY_BYTES_V1,\n\t\t\t\"maxEntryBytes\",\n\t\t),\n\t\tmaxEntries: positiveLimit(limitsInput.maxEntries, DEFAULT_MAX_ENTRIES_V1, MAX_ENTRIES_V1, \"maxEntries\"),\n\t};\n}\n\nfunction readU16(bytes: Uint8Array, offset: number): number {\n\tif (offset < 0 || offset + 2 > bytes.byteLength) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\treturn bytes[offset]! | (bytes[offset + 1]! << 8);\n}\n\nfunction readU32(bytes: Uint8Array, offset: number): number {\n\tif (offset < 0 || offset + 4 > bytes.byteLength) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\treturn (bytes[offset]! | (bytes[offset + 1]! << 8) | (bytes[offset + 2]! << 16) | (bytes[offset + 3]! << 24)) >>> 0;\n}\n\nfunction decodeName(bytes: Uint8Array, utf8: boolean): string {\n\ttry {\n\t\tconst value = new TextDecoder(\"utf-8\", { fatal: true }).decode(bytes);\n\t\tif (!utf8 && /[^\\x00-\\x7f]/u.test(value)) throw new Error(\"non-ascii zip name\");\n\t\treturn value;\n\t} catch {\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t}\n}\n\n/** Normalized archive entry name shared by the ZIP and tgz package parsers. */\nexport function normalizedEntryName(value: string): { readonly name: string; readonly directory: boolean } {\n\tif (value.length === 0 || value.includes(\"\\\\\") || /[\\u0000-\\u001f\\u007f]/u.test(value))\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst directory = value.endsWith(\"/\");\n\tif (value.startsWith(\"/\") || /^[a-z]:/iu.test(value)) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst parts = value.split(\"/\");\n\tif (directory) parts.pop();\n\tif (\n\t\tparts.length === 0 ||\n\t\tparts.some(\n\t\t\t(part) =>\n\t\t\t\tpart === \"\" ||\n\t\t\t\tpart === \".\" ||\n\t\t\t\tpart === \"..\" ||\n\t\t\t\tpart.includes(\":\") ||\n\t\t\t\t/[ .]$/u.test(part) ||\n\t\t\t\t/^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\\..*)?$/iu.test(part),\n\t\t)\n\t)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\treturn { name: parts.join(\"/\"), directory };\n}\n\nfunction crcTable(): Uint32Array {\n\tconst table = new Uint32Array(256);\n\tfor (let index = 0; index < table.length; index += 1) {\n\t\tlet value = index;\n\t\tfor (let bit = 0; bit < 8; bit += 1) value = (value & 1) === 0 ? value >>> 1 : (value >>> 1) ^ 0xedb88320;\n\t\ttable[index] = value >>> 0;\n\t}\n\treturn table;\n}\n\nconst CRC_TABLE_V1 = crcTable();\n\nfunction crc32(bytes: Uint8Array): number {\n\tlet value = 0xffffffff;\n\tfor (const byte of bytes) value = (value >>> 8) ^ CRC_TABLE_V1[(value ^ byte) & 0xff]!;\n\treturn (value ^ 0xffffffff) >>> 0;\n}\n\nfunction findEndOfCentralDirectory(bytes: Uint8Array): {\n\treadonly offset: number;\n\treadonly entries: number;\n\treadonly size: number;\n\treadonly start: number;\n} {\n\tconst minimum = Math.max(0, bytes.byteLength - 0xffff - 22);\n\tfor (let offset = bytes.byteLength - 22; offset >= minimum; offset -= 1) {\n\t\tif (readU32(bytes, offset) !== END_OF_CENTRAL_DIRECTORY_V1) continue;\n\t\tconst disk = readU16(bytes, offset + 4);\n\t\tconst centralDisk = readU16(bytes, offset + 6);\n\t\tconst entries = readU16(bytes, offset + 10);\n\t\tconst centralEntries = readU16(bytes, offset + 8);\n\t\tconst size = readU32(bytes, offset + 12);\n\t\tconst start = readU32(bytes, offset + 16);\n\t\tconst commentLength = readU16(bytes, offset + 20);\n\t\tif (\n\t\t\tdisk !== 0 ||\n\t\t\tcentralDisk !== 0 ||\n\t\t\tentries !== centralEntries ||\n\t\t\toffset + 22 + commentLength !== bytes.byteLength\n\t\t)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tif (entries === 0xffff || size === 0xffffffff || start === 0xffffffff)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tif (start + size > offset) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\treturn { offset, entries, size, start };\n\t}\n\tfor (let offset = Math.max(0, bytes.byteLength - 64); offset < bytes.byteLength - 3; offset += 1) {\n\t\tconst signature = readU32(bytes, offset);\n\t\tif (signature === ZIP64_END_OF_CENTRAL_DIRECTORY_V1 || signature === ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_V1)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t}\n\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n}\n\nfunction parseCentralDirectory(\n\tbytes: Uint8Array,\n\tlimits: Required<NodeZipPackageLimitsV1>,\n): readonly ZipEntryDescriptorV1[] {\n\tconst end = findEndOfCentralDirectory(bytes);\n\tif (end.entries > limits.maxEntries || end.size > bytes.byteLength || end.start + end.size > end.offset)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_budget_exceeded\");\n\tconst entries: ZipEntryDescriptorV1[] = [];\n\tconst names = new Set<string>();\n\tlet offset = end.start;\n\tfor (let index = 0; index < end.entries; index += 1) {\n\t\tif (readU32(bytes, offset) !== CENTRAL_FILE_HEADER_V1) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tconst madeBy = readU16(bytes, offset + 4) >>> 8;\n\t\tconst flags = readU16(bytes, offset + 8);\n\t\tconst method = readU16(bytes, offset + 10);\n\t\tconst crc = readU32(bytes, offset + 16);\n\t\tconst compressedSize = readU32(bytes, offset + 20);\n\t\tconst uncompressedSize = readU32(bytes, offset + 24);\n\t\tconst nameLength = readU16(bytes, offset + 28);\n\t\tconst extraLength = readU16(bytes, offset + 30);\n\t\tconst commentLength = readU16(bytes, offset + 32);\n\t\tconst disk = readU16(bytes, offset + 34);\n\t\tconst externalAttributes = readU32(bytes, offset + 38);\n\t\tconst localOffset = readU32(bytes, offset + 42);\n\t\tconst nameStart = offset + 46;\n\t\tconst recordEnd = nameStart + nameLength + extraLength + commentLength;\n\t\tif (\n\t\t\trecordEnd > end.start + end.size ||\n\t\t\tdisk !== 0 ||\n\t\t\t(flags & ~(0x8 | 0x800)) !== 0 ||\n\t\t\t(flags & 1) !== 0 ||\n\t\t\t(flags & 0x40) !== 0\n\t\t)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tif (method !== 0 && method !== 8) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tif (uncompressedSize > limits.maxEntryBytes || compressedSize > limits.maxArchiveBytes)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_budget_exceeded\");\n\t\tconst name = normalizedEntryName(\n\t\t\tdecodeName(bytes.subarray(nameStart, nameStart + nameLength), (flags & 0x800) !== 0),\n\t\t);\n\t\tif (names.has(name.name)) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tnames.add(name.name);\n\t\tconst unixMode = madeBy === 3 ? (externalAttributes >>> 16) & 0xffff : 0;\n\t\tif ((unixMode & 0xf000) === 0xa000) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tentries.push({\n\t\t\tname: name.name,\n\t\t\tdirectory: name.directory,\n\t\t\tflags,\n\t\t\tmethod,\n\t\t\tcompressedSize,\n\t\t\tuncompressedSize,\n\t\t\tcrc32: crc,\n\t\t\tlocalOffset,\n\t\t});\n\t\toffset = recordEnd;\n\t}\n\tif (offset !== end.start + end.size) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t// Validate the local-file regions before extraction. A forged central\n\t// directory must not be able to point into another entry or into the\n\t// central directory/EOCD; otherwise extraction could reinterpret metadata\n\t// bytes as package contents.\n\tconst spans: ZipLocalSpanV1[] = [];\n\tfor (const entry of entries) {\n\t\tif (entry.localOffset < 0 || entry.localOffset + 30 > end.start)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tif (readU32(bytes, entry.localOffset) !== LOCAL_FILE_HEADER_V1)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tconst localNameLength = readU16(bytes, entry.localOffset + 26);\n\t\tconst localExtraLength = readU16(bytes, entry.localOffset + 28);\n\t\tconst dataStart = entry.localOffset + 30 + localNameLength + localExtraLength;\n\t\tconst dataEnd = dataStart + entry.compressedSize;\n\t\tif (dataStart < entry.localOffset + 30 || dataEnd < dataStart || dataEnd > end.start)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\tspans.push({ start: entry.localOffset, end: dataEnd });\n\t}\n\tspans.sort((left, right) => left.start - right.start || left.end - right.end);\n\tfor (let index = 1; index < spans.length; index += 1) {\n\t\tif (spans[index - 1]!.end > spans[index]!.start) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\t}\n\treturn entries;\n}\n\nfunction extractEntry(bytes: Uint8Array, entry: ZipEntryDescriptorV1, maxEntryBytes: number): Uint8Array {\n\tif (entry.directory) {\n\t\tif (entry.compressedSize !== 0 || entry.uncompressedSize !== 0)\n\t\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t\treturn new Uint8Array();\n\t}\n\tif (entry.localOffset + 30 > bytes.byteLength) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\tif (readU32(bytes, entry.localOffset) !== LOCAL_FILE_HEADER_V1)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst localFlags = readU16(bytes, entry.localOffset + 6);\n\tconst localMethod = readU16(bytes, entry.localOffset + 8);\n\tconst nameLength = readU16(bytes, entry.localOffset + 26);\n\tconst extraLength = readU16(bytes, entry.localOffset + 28);\n\tconst localName = decodeName(\n\t\tbytes.subarray(entry.localOffset + 30, entry.localOffset + 30 + nameLength),\n\t\t(entry.flags & 0x800) !== 0,\n\t);\n\tif (normalizedEntryName(localName).name !== entry.name || localFlags !== entry.flags || localMethod !== entry.method)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst localCompressedSize = readU32(bytes, entry.localOffset + 18);\n\tconst localUncompressedSize = readU32(bytes, entry.localOffset + 22);\n\tif (\n\t\t(entry.flags & 8) === 0 &&\n\t\t(localCompressedSize !== entry.compressedSize || localUncompressedSize !== entry.uncompressedSize)\n\t)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst dataStart = entry.localOffset + 30 + nameLength + extraLength;\n\tconst dataEnd = dataStart + entry.compressedSize;\n\tif (dataStart > bytes.byteLength || dataEnd > bytes.byteLength)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst compressed = bytes.subarray(dataStart, dataEnd);\n\tlet output: Uint8Array;\n\ttry {\n\t\toutput =\n\t\t\tentry.method === 0\n\t\t\t\t? new Uint8Array(compressed)\n\t\t\t\t: inflateRawSync(compressed, { maxOutputLength: maxEntryBytes });\n\t} catch {\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\t}\n\tif (\n\t\toutput.byteLength !== entry.uncompressedSize ||\n\t\toutput.byteLength > maxEntryBytes ||\n\t\tcrc32(output) !== entry.crc32\n\t)\n\t\tthrow new NodePackageInstallErrorV1(\"archive_invalid\");\n\treturn output;\n}\n\nexport function parseNodeZipPackageV1(\n\tinput: Uint8Array,\n\tlimitsInput: NodeZipPackageLimitsV1 = {},\n): readonly NodeZipPackageEntryV1[] {\n\tif (!(input instanceof Uint8Array)) throw new NodePackageInstallErrorV1(\"archive_invalid\");\n\tconst limits = resolveNodePackageLimitsV1(limitsInput);\n\tif (input.byteLength > limits.maxArchiveBytes) throw new NodePackageInstallErrorV1(\"archive_budget_exceeded\");\n\tconst descriptors = parseCentralDirectory(input, limits);\n\tlet extractedBytes = 0;\n\treturn Object.freeze(\n\t\tdescriptors.map((descriptor) => {\n\t\t\tconst bytes = extractEntry(input, descriptor, limits.maxEntryBytes);\n\t\t\textractedBytes += bytes.byteLength;\n\t\t\tif (extractedBytes > limits.maxExtractedBytes) throw new NodePackageInstallErrorV1(\"archive_budget_exceeded\");\n\t\t\treturn Object.freeze({ name: descriptor.name, directory: descriptor.directory, bytes });\n\t\t}),\n\t);\n}\n\nfunction manifestKeys(value: Record<string, unknown>): boolean {\n\tconst allowed = new Set([\n\t\t\"packageFormatVersion\",\n\t\t\"id\",\n\t\t\"pluginVersion\",\n\t\t\"apiVersion\",\n\t\t\"minHostVersion\",\n\t\t\"maxHostVersion\",\n\t\t\"capabilityRefs\",\n\t\t\"requires\",\n\t\t\"platform\",\n\t\t\"testOnly\",\n\t]);\n\treturn Object.keys(value).every((key) => allowed.has(key));\n}\n\nfunction packageManifest(value: unknown, record: PluginUpdateRecordV1): void {\n\tif (value === null || typeof value !== \"object\" || Array.isArray(value))\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\tconst manifest = value as Record<string, unknown>;\n\tif (\n\t\t!manifestKeys(manifest) ||\n\t\tmanifest.packageFormatVersion !== NODE_PACKAGE_FORMAT_VERSION_V1 ||\n\t\tmanifest.id !== record.pluginId ||\n\t\tmanifest.pluginVersion !== record.pluginVersion\n\t)\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_mismatch\");\n\tif (typeof manifest.apiVersion !== \"string\" || manifest.apiVersion !== record.manifest.apiVersion)\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_mismatch\");\n\ttry {\n\t\tif (pluginManifestDigestV1(manifest as unknown as PluginManifestV1) !== record.manifestDigest)\n\t\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_mismatch\");\n\t} catch (error) {\n\t\tif (error instanceof NodePackageInstallErrorV1) throw error;\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\t}\n}\n\nfunction capabilityManifest(\n\tvalue: unknown,\n\trecord: PluginUpdateRecordV1,\n\tentries: readonly NodeZipPackageEntryV1[],\n): void {\n\tlet manifest: ReturnType<typeof parseCapabilityPluginManifest>;\n\ttry {\n\t\tmanifest = parseCapabilityPluginManifest(value, \"<installed plugin.json>\");\n\t} catch {\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\t}\n\tif (manifest.id !== record.pluginId || manifest.version !== record.pluginVersion)\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_mismatch\");\n\tconst entryValue = manifest.entry.startsWith(\"./\") ? manifest.entry.slice(2) : manifest.entry;\n\tconst entry = normalizedEntryName(entryValue);\n\tif (entry.directory || !entries.some((candidate) => candidate.name === entry.name && !candidate.directory))\n\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_mismatch\");\n}\n\nfunction opaqueScope(value: string): string {\n\tif (\n\t\ttypeof value !== \"string\" ||\n\t\tvalue.trim() === \"\" ||\n\t\tvalue.length > 2048 ||\n\t\t/\\s|[\\u0000-\\u001f\\u007f]/u.test(value)\n\t)\n\t\tthrow new NodePackageInstallErrorV1(\"installation_identity_mismatch\");\n\treturn value;\n}\n\nfunction installationKey(scope: PluginInstallationScopeV1, scopeRef: string, pluginId: string): string {\n\treturn `${scope}\\u0000${scopeRef}\\u0000${pluginId}`;\n}\n\nfunction identityMatches(\n\trecord: InstalledRecordV1,\n\tinput: {\n\t\treadonly pluginId: string;\n\t\treadonly pluginVersion: string;\n\t\treadonly manifestDigest: string;\n\t\treadonly scope: PluginInstallationScopeV1;\n\t\treadonly scopeRef: string;\n\t},\n): boolean {\n\treturn (\n\t\trecord.pluginId === input.pluginId &&\n\t\trecord.pluginVersion === input.pluginVersion &&\n\t\trecord.manifestDigest === input.manifestDigest &&\n\t\trecord.scope === input.scope &&\n\t\trecord.scopeRef === input.scopeRef\n\t);\n}\n\nfunction artifactIdentityMatchesRecord(artifact: PluginDistributionArtifactV1, record: PluginUpdateRecordV1): boolean {\n\treturn (\n\t\tartifact.version === 1 &&\n\t\tartifact.pluginId === record.pluginId &&\n\t\tartifact.pluginVersion === record.pluginVersion &&\n\t\tartifact.manifestDigest === record.manifestDigest\n\t);\n}\n\nfunction expectedArtifactDigest(value: string): { readonly algorithm: \"sha256\" | \"sha512\"; readonly hex: string } {\n\tconst prefixed = /^(sha256|sha512):([0-9a-f]+)$/iu.exec(value);\n\tif (prefixed !== null) {\n\t\tconst algorithm = prefixed[1]!.toLowerCase() as \"sha256\" | \"sha512\";\n\t\tconst hex = prefixed[2]!.toLowerCase();\n\t\tif ((algorithm === \"sha256\" && hex.length === 64) || (algorithm === \"sha512\" && hex.length === 128))\n\t\t\treturn { algorithm, hex };\n\t}\n\tif (/^[0-9a-f]{64}$/iu.test(value)) return { algorithm: \"sha256\", hex: value.toLowerCase() };\n\tif (/^[0-9a-f]{128}$/iu.test(value)) return { algorithm: \"sha512\", hex: value.toLowerCase() };\n\tthrow new NodePackageInstallErrorV1(\"invalid_artifact\");\n}\n\nasync function collectArtifact(\n\topenArtifact: NodePackageInstallHostOptionsV1[\"openArtifact\"],\n\tartifact: PluginDistributionArtifactV1,\n\tmaxBytes: number,\n\tsignal: AbortSignal,\n): Promise<Uint8Array> {\n\tconst chunks: Uint8Array[] = [];\n\tlet total = 0;\n\tfor await (const chunk of openArtifact({ artifact, signal })) {\n\t\tif (signal.aborted) throw new NodePackageInstallErrorV1(\"cancelled\");\n\t\tif (!(chunk instanceof Uint8Array)) throw new NodePackageInstallErrorV1(\"invalid_artifact\");\n\t\tif (chunk.byteLength > maxBytes - total) throw new NodePackageInstallErrorV1(\"archive_budget_exceeded\");\n\t\tchunks.push(new Uint8Array(chunk));\n\t\ttotal += chunk.byteLength;\n\t}\n\tconst result = new Uint8Array(total);\n\tlet offset = 0;\n\tfor (const chunk of chunks) {\n\t\tresult.set(chunk, offset);\n\t\toffset += chunk.byteLength;\n\t}\n\treturn result;\n}\n\nfunction packagePath(root: string, key: string): string {\n\treturn join(root, createHash(\"sha256\").update(key).digest(\"hex\"));\n}\n\nasync function writeEntries(\n\troot: string,\n\tentries: readonly NodeZipPackageEntryV1[],\n\tsignal: AbortSignal,\n): Promise<void> {\n\tawait mkdir(root, { recursive: true, mode: 0o700 });\n\tfor (const entry of entries) {\n\t\tif (signal.aborted) throw new NodePackageInstallErrorV1(\"cancelled\");\n\t\tconst target = join(root, ...entry.name.split(\"/\"));\n\t\tif (entry.directory) {\n\t\t\tawait mkdir(target, { recursive: true, mode: 0o700 });\n\t\t\tcontinue;\n\t\t}\n\t\tconst parent = join(root, ...entry.name.split(\"/\").slice(0, -1));\n\t\tawait mkdir(parent, { recursive: true, mode: 0o700 });\n\t\tawait writeFile(target, entry.bytes, { flag: \"wx\", mode: 0o600 });\n\t}\n}\n\n/**\n * Host-owned ZIP/tgz package installer. It extracts only a bounded,\n * non-encrypted ZIP (\"application/zip\") or gzip-compressed tar\n * (\"application/gzip\") package and never executes package contents. The\n * returned installation handle is opaque; paths stay inside the host-owned\n * instance directory.\n */\nexport async function createNodePackageInstallHostV1(\n\toptions: NodePackageInstallHostOptionsV1,\n): Promise<NodePackageInstallHostV1> {\n\tif (typeof options.openArtifact !== \"function\") throw new Error(\"openArtifact is required\");\n\tconst limits = resolveNodePackageLimitsV1(options);\n\tconst parent = options.installationDirectory ?? tmpdir();\n\tif (!isAbsolute(parent)) throw new Error(\"installationDirectory must be absolute\");\n\tif (options.persistence !== undefined && options.persistence !== \"ephemeral\" && options.persistence !== \"persistent\")\n\t\tthrow new Error(\"persistence mode is invalid\");\n\tconst supportedScopes = options.supportedScopes ?? [\"global\", \"project\"];\n\tif (\n\t\t!Array.isArray(supportedScopes) ||\n\t\tsupportedScopes.length === 0 ||\n\t\tnew Set(supportedScopes).size !== supportedScopes.length ||\n\t\tsupportedScopes.some((scope) => scope !== \"global\" && scope !== \"project\")\n\t)\n\t\tthrow new Error(\"supportedScopes is invalid\");\n\tawait mkdir(parent, { recursive: true, mode: 0o700 });\n\tconst persistent = options.persistence === \"persistent\";\n\tconst instanceRoot = persistent\n\t\t? join(parent, \"agent-forge-plugin-install-store-v1\")\n\t\t: await mkdtemp(join(parent, \"agent-forge-plugin-install-\"));\n\tconst persistence: NodePackageInstallPersistenceV1 | undefined = persistent\n\t\t? await createNodePackageInstallPersistenceV1({\n\t\t\t\trootDirectory: instanceRoot,\n\t\t\t\tlockTimeoutMs: options.persistenceLockTimeoutMs,\n\t\t\t})\n\t\t: undefined;\n\tconst records = new Map<string, InstalledRecordV1>();\n\tconst activeByKey = new Map<string, string>();\n\tif (persistence !== undefined) {\n\t\tfor (const record of await persistence.load()) records.set(record.installationRef, { ...record });\n\t}\n\tfor (const record of records.values()) {\n\t\tif (record.state === \"active\")\n\t\t\tactiveByKey.set(installationKey(record.scope, record.scopeRef, record.pluginId), record.installationRef);\n\t}\n\tlet closed = false;\n\tconst activeOperations = new Set<AbortController>();\n\tconst runtimeHandles = new Map<string, { readonly dispose?: () => void | Promise<void> }>();\n\tconst applyPersistentSnapshot = (snapshot: readonly NodePackageInstallPersistentRecordV1[]): void => {\n\t\trecords.clear();\n\t\tactiveByKey.clear();\n\t\tfor (const value of snapshot) records.set(value.installationRef, { ...value });\n\t\tfor (const value of records.values()) {\n\t\t\tif (value.state === \"active\")\n\t\t\t\tactiveByKey.set(installationKey(value.scope, value.scopeRef, value.pluginId), value.installationRef);\n\t\t}\n\t};\n\tconst commitRecords = async (\n\t\tupdate: (values: Map<string, NodePackageInstallPersistentRecordV1>) => void | Promise<void>,\n\t): Promise<void> => {\n\t\tif (persistence === undefined) {\n\t\t\tawait update(records);\n\t\t\treturn;\n\t\t}\n\t\tapplyPersistentSnapshot(await persistence.mutate({ update }));\n\t};\n\tlet drained: (() => void) | undefined;\n\tlet closePromise: Promise<void> | undefined;\n\n\tconst begin = (\n\t\tparentSignal: AbortSignal | undefined,\n\t): { readonly controller: AbortController; readonly signal: AbortSignal } => {\n\t\tif (closed) throw new NodePackageInstallErrorV1(\"host_closed\");\n\t\tconst controller = new AbortController();\n\t\tif (parentSignal?.aborted) controller.abort(parentSignal.reason);\n\t\telse parentSignal?.addEventListener(\"abort\", () => controller.abort(parentSignal.reason), { once: true });\n\t\tactiveOperations.add(controller);\n\t\treturn { controller, signal: controller.signal };\n\t};\n\tconst end = (controller: AbortController): void => {\n\t\tactiveOperations.delete(controller);\n\t\tif (activeOperations.size === 0) {\n\t\t\tdrained?.();\n\t\t\tdrained = undefined;\n\t\t}\n\t};\n\tconst waitDrained = async (): Promise<void> => {\n\t\tif (activeOperations.size === 0) return;\n\t\tawait new Promise<void>((resolve) => {\n\t\t\tdrained = resolve;\n\t\t});\n\t};\n\tconst getRecord = (\n\t\tinstallation: PluginInstallationV1,\n\t\trecord: PluginUpdateRecordV1,\n\t\tscope: PluginInstallationScopeV1,\n\t\tscopeRef: string,\n\t): InstalledRecordV1 => {\n\t\tconst value = records.get(installation.installationRef);\n\t\tif (\n\t\t\tvalue === undefined ||\n\t\t\tinstallation.pluginId !== record.pluginId ||\n\t\t\tinstallation.pluginVersion !== record.pluginVersion ||\n\t\t\tinstallation.manifestDigest !== record.manifestDigest ||\n\t\t\tinstallation.scope !== scope ||\n\t\t\tinstallation.scopeRef !== scopeRef ||\n\t\t\t!identityMatches(value, {\n\t\t\t\tpluginId: record.pluginId,\n\t\t\t\tpluginVersion: record.pluginVersion,\n\t\t\t\tmanifestDigest: record.manifestDigest,\n\t\t\t\tscope,\n\t\t\t\tscopeRef,\n\t\t\t})\n\t\t)\n\t\t\tthrow new NodePackageInstallErrorV1(\"installation_identity_mismatch\");\n\t\treturn value;\n\t};\n\tconst disposeRuntime = async (installationRef: string): Promise<void> => {\n\t\tconst handle = runtimeHandles.get(installationRef);\n\t\tif (handle === undefined) return;\n\t\tif (handle.dispose !== undefined) await Promise.resolve(handle.dispose());\n\t\truntimeHandles.delete(installationRef);\n\t};\n\tconst loadRuntime = async (\n\t\tinstallation: PluginInstallationV1,\n\t\trecord: PluginUpdateRecordV1 | undefined,\n\t\tdirectory: string,\n\t\tsignal: AbortSignal,\n\t): Promise<{ readonly dispose?: () => void | Promise<void> } | undefined> => {\n\t\tif (options.loadPlugin === undefined) return undefined;\n\t\ttry {\n\t\t\tconst loaded = await options.loadPlugin({\n\t\t\t\tinstallation,\n\t\t\t\trecord,\n\t\t\t\tdirectory,\n\t\t\t\tmanifestPath: join(directory, CAPABILITY_MANIFEST_FILE_V1),\n\t\t\t\tsignal,\n\t\t\t});\n\t\t\treturn loaded ?? undefined;\n\t\t} catch (error) {\n\t\t\tif (error instanceof NodePackageInstallErrorV1) throw error;\n\t\t\tthrow new NodePackageInstallErrorV1(\"installation_io_failed\");\n\t\t}\n\t};\n\n\tconst host: NodePackageInstallHostV1 = {\n\t\tversion: NODE_PACKAGE_INSTALL_HOST_VERSION_V1,\n\t\tsupportedScopes: Object.freeze([...supportedScopes]),\n\t\tgetInstallationDirectory(input) {\n\t\t\tconst record = records.get(input.installation.installationRef);\n\t\t\tif (\n\t\t\t\trecord === undefined ||\n\t\t\t\trecord.pluginId !== input.installation.pluginId ||\n\t\t\t\trecord.pluginVersion !== input.installation.pluginVersion ||\n\t\t\t\trecord.manifestDigest !== input.installation.manifestDigest ||\n\t\t\t\trecord.scope !== input.installation.scope ||\n\t\t\t\trecord.scopeRef !== input.installation.scopeRef\n\t\t\t)\n\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_unknown\");\n\t\t\treturn record.directory;\n\t\t},\n\t\tasync install(input) {\n\t\t\tconst operation = begin(input.signal);\n\t\t\tlet staging: string | undefined;\n\t\t\ttry {\n\t\t\t\tif (input.artifact === undefined) throw new NodePackageInstallErrorV1(\"invalid_artifact\");\n\t\t\t\tif (!artifactIdentityMatchesRecord(input.artifact, input.record))\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"invalid_artifact\");\n\t\t\t\tconst mediaType = input.artifact.mediaType.toLowerCase();\n\t\t\t\tif (mediaType !== \"application/zip\" && mediaType !== \"application/gzip\")\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"unsupported_media_type\");\n\t\t\t\tconst scopeRef = opaqueScope(input.scopeRef);\n\t\t\t\tconst bytes = await collectArtifact(\n\t\t\t\t\toptions.openArtifact,\n\t\t\t\t\tinput.artifact,\n\t\t\t\t\tlimits.maxArchiveBytes,\n\t\t\t\t\toperation.signal,\n\t\t\t\t);\n\t\t\t\tif (input.artifact.sizeBytes !== bytes.byteLength) throw new NodePackageInstallErrorV1(\"invalid_artifact\");\n\t\t\t\tconst digest = expectedArtifactDigest(input.artifact.artifactDigest);\n\t\t\t\tif (createHash(digest.algorithm).update(bytes).digest(\"hex\") !== digest.hex)\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"invalid_artifact\");\n\t\t\t\tconst entries =\n\t\t\t\t\tmediaType === \"application/gzip\"\n\t\t\t\t\t\t? await parseNodeTarGzPackageV1(bytes, limits)\n\t\t\t\t\t\t: parseNodeZipPackageV1(bytes, limits);\n\t\t\t\tconst manifestEntry = entries.find(\n\t\t\t\t\t(entry) => entry.name === ECOSYSTEM_MANIFEST_FILE_V1 && !entry.directory,\n\t\t\t\t);\n\t\t\t\tif (manifestEntry === undefined) throw new NodePackageInstallErrorV1(\"package_manifest_missing\");\n\t\t\t\tif (manifestEntry.bytes.byteLength > MAX_PACKAGE_MANIFEST_BYTES_V1)\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\t\t\t\tlet manifest: unknown;\n\t\t\t\ttry {\n\t\t\t\t\tmanifest = JSON.parse(new TextDecoder(\"utf-8\", { fatal: true }).decode(manifestEntry.bytes));\n\t\t\t\t} catch {\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\t\t\t\t}\n\t\t\t\tpackageManifest(manifest, input.record);\n\t\t\t\tconst capabilityEntry = entries.find(\n\t\t\t\t\t(entry) => entry.name === CAPABILITY_MANIFEST_FILE_V1 && !entry.directory,\n\t\t\t\t);\n\t\t\t\tif (capabilityEntry === undefined) throw new NodePackageInstallErrorV1(\"package_manifest_missing\");\n\t\t\t\tif (capabilityEntry.bytes.byteLength > MAX_PACKAGE_MANIFEST_BYTES_V1)\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\t\t\t\tlet capability: unknown;\n\t\t\t\ttry {\n\t\t\t\t\tcapability = JSON.parse(new TextDecoder(\"utf-8\", { fatal: true }).decode(capabilityEntry.bytes));\n\t\t\t\t} catch {\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"package_manifest_invalid\");\n\t\t\t\t}\n\t\t\t\tcapabilityManifest(capability, input.record, entries);\n\t\t\t\tconst key = installationKey(input.scope, scopeRef, input.record.pluginId);\n\t\t\t\tstaging = join(instanceRoot, `.staging-${randomUUID()}`);\n\t\t\t\tawait writeEntries(staging, entries, operation.signal);\n\t\t\t\tconst installRef = `plugin-install://node-zip/${randomUUID()}`;\n\t\t\t\tconst destination = packagePath(instanceRoot, `${key}\\u0000${installRef}`);\n\t\t\t\tawait rename(staging, destination);\n\t\t\t\tstaging = undefined;\n\t\t\t\tconst installedRecord: InstalledRecordV1 = {\n\t\t\t\t\tinstallationRef: installRef,\n\t\t\t\t\tdirectory: destination,\n\t\t\t\t\tpluginId: input.record.pluginId,\n\t\t\t\t\tpluginVersion: input.record.pluginVersion,\n\t\t\t\t\tmanifestDigest: input.record.manifestDigest,\n\t\t\t\t\tscope: input.scope,\n\t\t\t\t\tscopeRef,\n\t\t\t\t\tstate: \"installed\",\n\t\t\t\t};\n\t\t\t\ttry {\n\t\t\t\t\tawait commitRecords((values) => {\n\t\t\t\t\t\tvalues.set(installRef, installedRecord);\n\t\t\t\t\t});\n\t\t\t\t} catch (error) {\n\t\t\t\t\tawait rm(destination, { recursive: true, force: true }).catch(() => undefined);\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t\treturn { installationRef: installRef, scope: input.scope, scopeRef };\n\t\t\t} catch (error) {\n\t\t\t\tif (operation.signal.aborted || input.signal?.aborted) throw new NodePackageInstallErrorV1(\"cancelled\");\n\t\t\t\tif (error instanceof NodePackageInstallErrorV1) throw error;\n\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_io_failed\");\n\t\t\t} finally {\n\t\t\t\tif (staging !== undefined) await rm(staging, { recursive: true, force: true }).catch(() => undefined);\n\t\t\t\tend(operation.controller);\n\t\t\t}\n\t\t},\n\t\tasync activate(input) {\n\t\t\tconst operation = begin(input.signal);\n\t\t\tlet candidate: { readonly dispose?: () => void | Promise<void> } | undefined;\n\t\t\ttry {\n\t\t\t\tconst scopeRef = opaqueScope(input.scopeRef);\n\t\t\t\tconst record = getRecord(input.installation, input.record, input.scope, scopeRef);\n\t\t\t\tif (record.state !== \"installed\") throw new NodePackageInstallErrorV1(\"installation_identity_mismatch\");\n\t\t\t\tcandidate = await loadRuntime(input.installation, input.record, record.directory, operation.signal);\n\t\t\t\tconst key = installationKey(input.scope, scopeRef, input.record.pluginId);\n\t\t\t\tconst previousRef = activeByKey.get(key);\n\t\t\t\tif (previousRef !== undefined) {\n\t\t\t\t\tconst previous = records.get(previousRef);\n\t\t\t\t\tif (previous !== undefined) {\n\t\t\t\t\t\tawait disposeRuntime(previousRef);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tawait commitRecords((values) => {\n\t\t\t\t\tconst candidateRecord = values.get(input.installation.installationRef);\n\t\t\t\t\tif (candidateRecord === undefined) throw new NodePackageInstallErrorV1(\"installation_identity_mismatch\");\n\t\t\t\t\tif (previousRef !== undefined) {\n\t\t\t\t\t\tconst previousRecord = values.get(previousRef);\n\t\t\t\t\t\tif (previousRecord !== undefined) values.set(previousRef, { ...previousRecord, state: \"retained\" });\n\t\t\t\t\t}\n\t\t\t\t\tvalues.set(input.installation.installationRef, { ...candidateRecord, state: \"active\" });\n\t\t\t\t});\n\t\t\t\tactiveByKey.set(key, input.installation.installationRef);\n\t\t\t\tif (candidate !== undefined) runtimeHandles.set(input.installation.installationRef, candidate);\n\t\t\t\tcandidate = undefined;\n\t\t\t} catch (error) {\n\t\t\t\tif (candidate?.dispose !== undefined) await Promise.resolve(candidate.dispose()).catch(() => undefined);\n\t\t\t\tif (error instanceof NodePackageInstallErrorV1) throw error;\n\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_io_failed\");\n\t\t\t} finally {\n\t\t\t\tend(operation.controller);\n\t\t\t}\n\t\t},\n\t\tasync rollback(input) {\n\t\t\tconst operation = begin(input.signal);\n\t\t\tlet candidate: { readonly dispose?: () => void | Promise<void> } | undefined;\n\t\t\ttry {\n\t\t\t\tconst scopeRef = opaqueScope(input.scopeRef);\n\t\t\t\tconst failed = records.get(input.failed.installationRef);\n\t\t\t\tif (\n\t\t\t\t\tfailed === undefined ||\n\t\t\t\t\t!identityMatches(failed, {\n\t\t\t\t\t\tpluginId: input.failed.pluginId,\n\t\t\t\t\t\tpluginVersion: input.failed.pluginVersion,\n\t\t\t\t\t\tmanifestDigest: input.failed.manifestDigest,\n\t\t\t\t\t\tscope: input.scope,\n\t\t\t\t\t\tscopeRef,\n\t\t\t\t\t})\n\t\t\t\t)\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_identity_mismatch\");\n\t\t\t\tconst previous = records.get(input.previous.installationRef);\n\t\t\t\tif (\n\t\t\t\t\tprevious === undefined ||\n\t\t\t\t\t!identityMatches(previous, {\n\t\t\t\t\t\tpluginId: input.previous.pluginId,\n\t\t\t\t\t\tpluginVersion: input.previous.pluginVersion,\n\t\t\t\t\t\tmanifestDigest: input.previous.manifestDigest,\n\t\t\t\t\t\tscope: input.scope,\n\t\t\t\t\t\tscopeRef,\n\t\t\t\t\t}) ||\n\t\t\t\t\t(previous.state !== \"active\" && previous.state !== \"retained\") ||\n\t\t\t\t\tfailed.pluginId !== previous.pluginId\n\t\t\t\t)\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_identity_mismatch\");\n\t\t\t\tconst previousInstallation: PluginInstallationV1 = {\n\t\t\t\t\tversion: 1,\n\t\t\t\t\tpluginId: previous.pluginId,\n\t\t\t\t\tpluginVersion: previous.pluginVersion,\n\t\t\t\t\tmanifestDigest: previous.manifestDigest,\n\t\t\t\t\tscope: previous.scope,\n\t\t\t\t\tscopeRef: previous.scopeRef,\n\t\t\t\t\tinstallationRef: input.previous.installationRef,\n\t\t\t\t\tstate: \"active\",\n\t\t\t\t};\n\t\t\t\tcandidate = await loadRuntime(previousInstallation, undefined, previous.directory, operation.signal);\n\t\t\t\tawait disposeRuntime(input.failed.installationRef);\n\t\t\t\tawait rm(failed.directory, { recursive: true, force: true });\n\t\t\t\tawait commitRecords((values) => {\n\t\t\t\t\tvalues.delete(input.failed.installationRef);\n\t\t\t\t\tconst previousRecord = values.get(input.previous.installationRef);\n\t\t\t\t\tif (previousRecord !== undefined)\n\t\t\t\t\t\tvalues.set(input.previous.installationRef, { ...previousRecord, state: \"active\" });\n\t\t\t\t});\n\t\t\t\tactiveByKey.set(installationKey(input.scope, scopeRef, failed.pluginId), input.previous.installationRef);\n\t\t\t\tif (candidate !== undefined) runtimeHandles.set(input.previous.installationRef, candidate);\n\t\t\t\tcandidate = undefined;\n\t\t\t} catch (error) {\n\t\t\t\tif (candidate?.dispose !== undefined) await Promise.resolve(candidate.dispose()).catch(() => undefined);\n\t\t\t\tif (error instanceof NodePackageInstallErrorV1) throw error;\n\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_io_failed\");\n\t\t\t} finally {\n\t\t\t\tend(operation.controller);\n\t\t\t}\n\t\t},\n\t\tasync uninstall(input) {\n\t\t\tconst operation = begin(input.signal);\n\t\t\ttry {\n\t\t\t\tconst scopeRef = opaqueScope(input.scopeRef);\n\t\t\t\tconst record = records.get(input.installation.installationRef);\n\t\t\t\tif (\n\t\t\t\t\trecord === undefined ||\n\t\t\t\t\t!identityMatches(record, {\n\t\t\t\t\t\tpluginId: input.installation.pluginId,\n\t\t\t\t\t\tpluginVersion: input.installation.pluginVersion,\n\t\t\t\t\t\tmanifestDigest: input.installation.manifestDigest,\n\t\t\t\t\t\tscope: input.scope,\n\t\t\t\t\t\tscopeRef,\n\t\t\t\t\t})\n\t\t\t\t)\n\t\t\t\t\tthrow new NodePackageInstallErrorV1(\"installation_unknown\");\n\t\t\t\tawait disposeRuntime(input.installation.installationRef);\n\t\t\t\t// force:false keeps rm strict, but a directory that is already\n\t\t\t\t// gone (ghost record from an interrupted uninstall) is the\n\t\t\t\t// desired end state — continue to the index commit instead of\n\t\t\t\t// bricking every later store operation on ENOENT.\n\t\t\t\tawait rm(record.directory, { recursive: true, force: false }).catch((error: unknown) => {\n\t\t\t\t\tif ((error as NodeJS.ErrnoException).code === \"ENOENT\") return;\n\t\t\t\t\tthrow error;\n\t\t\t\t});\n\t\t\t\tawait commitRecords((values) => {\n\t\t\t\t\tvalues.delete(input.installation.installationRef);\n\t\t\t\t});\n\t\t\t\tconst key = installationKey(input.scope, scopeRef, record.pluginId);\n\t\t\t\tif (activeByKey.get(key) === input.installation.installationRef) activeByKey.delete(key);\n\t\t\t} finally {\n\t\t\t\tend(operation.controller);\n\t\t\t}\n\t\t},\n\t\tasync close() {\n\t\t\tif (closePromise !== undefined) return await closePromise;\n\t\t\tclosed = true;\n\t\t\tfor (const operation of activeOperations) operation.abort();\n\t\t\tclosePromise = (async () => {\n\t\t\t\tawait waitDrained();\n\t\t\t\tlet disposeError = false;\n\t\t\t\tfor (const installationRef of runtimeHandles.keys()) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait disposeRuntime(installationRef);\n\t\t\t\t\t} catch {\n\t\t\t\t\t\tdisposeError = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (persistence === undefined) await rm(instanceRoot, { recursive: true, force: true });\n\t\t\t\telse await persistence.close();\n\t\t\t\trecords.clear();\n\t\t\t\tactiveByKey.clear();\n\t\t\t\tif (disposeError) throw new NodePackageInstallErrorV1(\"installation_io_failed\");\n\t\t\t})();\n\t\t\treturn await closePromise;\n\t\t},\n\t};\n\tif (options.loadPlugin !== undefined) {\n\t\ttry {\n\t\t\tfor (const record of records.values()) {\n\t\t\t\tif (record.state !== \"active\") continue;\n\t\t\t\tconst installation: PluginInstallationV1 = {\n\t\t\t\t\tversion: 1,\n\t\t\t\t\tpluginId: record.pluginId,\n\t\t\t\t\tpluginVersion: record.pluginVersion,\n\t\t\t\t\tmanifestDigest: record.manifestDigest,\n\t\t\t\t\tscope: record.scope,\n\t\t\t\t\tscopeRef: record.scopeRef,\n\t\t\t\t\tinstallationRef: record.installationRef,\n\t\t\t\t\tstate: \"active\",\n\t\t\t\t};\n\t\t\t\tconst runtime = await loadRuntime(installation, undefined, record.directory, new AbortController().signal);\n\t\t\t\tif (runtime !== undefined) runtimeHandles.set(record.installationRef, runtime);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tawait host.close().catch(() => undefined);\n\t\t\tif (error instanceof NodePackageInstallErrorV1) throw error;\n\t\t\tthrow new NodePackageInstallErrorV1(\"installation_io_failed\");\n\t\t}\n\t}\n\treturn host;\n}\n"]}
@@ -784,7 +784,15 @@ export async function createNodePackageInstallHostV1(options) {
784
784
  }))
785
785
  throw new NodePackageInstallErrorV1("installation_unknown");
786
786
  await disposeRuntime(input.installation.installationRef);
787
- await rm(record.directory, { recursive: true, force: false });
787
+ // force:false keeps rm strict, but a directory that is already
788
+ // gone (ghost record from an interrupted uninstall) is the
789
+ // desired end state — continue to the index commit instead of
790
+ // bricking every later store operation on ENOENT.
791
+ await rm(record.directory, { recursive: true, force: false }).catch((error) => {
792
+ if (error.code === "ENOENT")
793
+ return;
794
+ throw error;
795
+ });
788
796
  await commitRecords((values) => {
789
797
  values.delete(input.installation.installationRef);
790
798
  });