@namzu/sdk 69.2.0 → 69.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/dist/authorization/readonly-commands.d.ts.map +1 -1
  3. package/dist/authorization/readonly-commands.js +30 -2
  4. package/dist/authorization/readonly-commands.js.map +1 -1
  5. package/dist/constants/plugin/index.d.ts +8 -0
  6. package/dist/constants/plugin/index.d.ts.map +1 -1
  7. package/dist/constants/plugin/index.js +8 -0
  8. package/dist/constants/plugin/index.js.map +1 -1
  9. package/dist/plugin/common-layout/index.d.ts +5 -0
  10. package/dist/plugin/common-layout/index.d.ts.map +1 -0
  11. package/dist/plugin/common-layout/index.js +3 -0
  12. package/dist/plugin/common-layout/index.js.map +1 -0
  13. package/dist/plugin/common-layout/mcp.d.ts +21 -0
  14. package/dist/plugin/common-layout/mcp.d.ts.map +1 -0
  15. package/dist/plugin/common-layout/mcp.js +305 -0
  16. package/dist/plugin/common-layout/mcp.js.map +1 -0
  17. package/dist/plugin/common-layout/paths.d.ts +27 -0
  18. package/dist/plugin/common-layout/paths.d.ts.map +1 -0
  19. package/dist/plugin/common-layout/paths.js +48 -0
  20. package/dist/plugin/common-layout/paths.js.map +1 -0
  21. package/dist/plugin/common-layout/read.d.ts +25 -0
  22. package/dist/plugin/common-layout/read.d.ts.map +1 -0
  23. package/dist/plugin/common-layout/read.js +1155 -0
  24. package/dist/plugin/common-layout/read.js.map +1 -0
  25. package/dist/plugin/common-layout/types.d.ts +66 -0
  26. package/dist/plugin/common-layout/types.d.ts.map +1 -0
  27. package/dist/plugin/common-layout/types.js +2 -0
  28. package/dist/plugin/common-layout/types.js.map +1 -0
  29. package/dist/plugin/common-layout-names.d.ts +65 -0
  30. package/dist/plugin/common-layout-names.d.ts.map +1 -0
  31. package/dist/plugin/common-layout-names.js +94 -0
  32. package/dist/plugin/common-layout-names.js.map +1 -0
  33. package/dist/plugin/hook-module/capabilities.d.ts +41 -0
  34. package/dist/plugin/hook-module/capabilities.d.ts.map +1 -0
  35. package/dist/plugin/hook-module/capabilities.js +438 -0
  36. package/dist/plugin/hook-module/capabilities.js.map +1 -0
  37. package/dist/plugin/hook-module/consent.d.ts +39 -0
  38. package/dist/plugin/hook-module/consent.d.ts.map +1 -0
  39. package/dist/plugin/hook-module/consent.js +59 -0
  40. package/dist/plugin/hook-module/consent.js.map +1 -0
  41. package/dist/plugin/hook-module/events.d.ts +33 -0
  42. package/dist/plugin/hook-module/events.d.ts.map +1 -0
  43. package/dist/plugin/hook-module/events.js +149 -0
  44. package/dist/plugin/hook-module/events.js.map +1 -0
  45. package/dist/plugin/hook-module/host.d.ts +79 -0
  46. package/dist/plugin/hook-module/host.d.ts.map +1 -0
  47. package/dist/plugin/hook-module/host.js +546 -0
  48. package/dist/plugin/hook-module/host.js.map +1 -0
  49. package/dist/plugin/hook-module/index.d.ts +11 -0
  50. package/dist/plugin/hook-module/index.d.ts.map +1 -0
  51. package/dist/plugin/hook-module/index.js +6 -0
  52. package/dist/plugin/hook-module/index.js.map +1 -0
  53. package/dist/plugin/hook-module/scan.d.ts +99 -0
  54. package/dist/plugin/hook-module/scan.d.ts.map +1 -0
  55. package/dist/plugin/hook-module/scan.js +789 -0
  56. package/dist/plugin/hook-module/scan.js.map +1 -0
  57. package/dist/plugin/hook-module/worker-source.d.ts +31 -0
  58. package/dist/plugin/hook-module/worker-source.d.ts.map +1 -0
  59. package/dist/plugin/hook-module/worker-source.js +498 -0
  60. package/dist/plugin/hook-module/worker-source.js.map +1 -0
  61. package/dist/plugin/index.d.ts +5 -2
  62. package/dist/plugin/index.d.ts.map +1 -1
  63. package/dist/plugin/index.js +4 -1
  64. package/dist/plugin/index.js.map +1 -1
  65. package/dist/plugin/lifecycle.d.ts +57 -0
  66. package/dist/plugin/lifecycle.d.ts.map +1 -1
  67. package/dist/plugin/lifecycle.js +92 -11
  68. package/dist/plugin/lifecycle.js.map +1 -1
  69. package/dist/plugin/loader.d.ts +30 -0
  70. package/dist/plugin/loader.d.ts.map +1 -1
  71. package/dist/plugin/loader.js +76 -19
  72. package/dist/plugin/loader.js.map +1 -1
  73. package/dist/plugin/shell-hook-env.d.ts +7 -0
  74. package/dist/plugin/shell-hook-env.d.ts.map +1 -1
  75. package/dist/plugin/shell-hook-env.js.map +1 -1
  76. package/dist/plugin/shell-hook.d.ts +10 -1
  77. package/dist/plugin/shell-hook.d.ts.map +1 -1
  78. package/dist/plugin/shell-hook.js +81 -70
  79. package/dist/plugin/shell-hook.js.map +1 -1
  80. package/dist/prompt/coding-agent-doctrine.d.ts +1 -1
  81. package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
  82. package/dist/prompt/coding-agent-doctrine.js +1 -1
  83. package/dist/provider/side-request-reasoning.d.ts.map +1 -1
  84. package/dist/provider/side-request-reasoning.js +5 -1
  85. package/dist/provider/side-request-reasoning.js.map +1 -1
  86. package/dist/public-runtime.d.ts +3 -2
  87. package/dist/public-runtime.d.ts.map +1 -1
  88. package/dist/public-runtime.js +2 -2
  89. package/dist/public-runtime.js.map +1 -1
  90. package/dist/runtime/query/auto-review/classifier.d.ts.map +1 -1
  91. package/dist/runtime/query/auto-review/classifier.js +13 -2
  92. package/dist/runtime/query/auto-review/classifier.js.map +1 -1
  93. package/dist/runtime/query/auto-review/failure.d.ts +12 -5
  94. package/dist/runtime/query/auto-review/failure.d.ts.map +1 -1
  95. package/dist/runtime/query/auto-review/failure.js +42 -7
  96. package/dist/runtime/query/auto-review/failure.js.map +1 -1
  97. package/dist/runtime/query/review-policy.d.ts.map +1 -1
  98. package/dist/runtime/query/review-policy.js +22 -6
  99. package/dist/runtime/query/review-policy.js.map +1 -1
  100. package/dist/runtime/query/turn/check-scope.d.ts +18 -0
  101. package/dist/runtime/query/turn/check-scope.d.ts.map +1 -0
  102. package/dist/runtime/query/turn/check-scope.js +90 -0
  103. package/dist/runtime/query/turn/check-scope.js.map +1 -0
  104. package/dist/runtime/query/turn/outcome-ledger.d.ts.map +1 -1
  105. package/dist/runtime/query/turn/outcome-ledger.js +13 -0
  106. package/dist/runtime/query/turn/outcome-ledger.js.map +1 -1
  107. package/dist/skills/loader.d.ts +8 -2
  108. package/dist/skills/loader.d.ts.map +1 -1
  109. package/dist/skills/loader.js +22 -11
  110. package/dist/skills/loader.js.map +1 -1
  111. package/dist/skills/registry.d.ts.map +1 -1
  112. package/dist/skills/registry.js +9 -1
  113. package/dist/skills/registry.js.map +1 -1
  114. package/dist/tools/builtins/job.d.ts.map +1 -1
  115. package/dist/tools/builtins/job.js +4 -1
  116. package/dist/tools/builtins/job.js.map +1 -1
  117. package/dist/tools/builtins/wait-for-job.d.ts.map +1 -1
  118. package/dist/tools/builtins/wait-for-job.js +2 -1
  119. package/dist/tools/builtins/wait-for-job.js.map +1 -1
  120. package/dist/types/plugin/index.d.ts +73 -0
  121. package/dist/types/plugin/index.d.ts.map +1 -1
  122. package/dist/types/plugin/index.js +28 -6
  123. package/dist/types/plugin/index.js.map +1 -1
  124. package/dist/types/skills/index.d.ts +6 -0
  125. package/dist/types/skills/index.d.ts.map +1 -1
  126. package/package.json +2 -1
  127. package/src/authorization/readonly-commands.ts +30 -2
  128. package/src/constants/plugin/index.ts +9 -0
  129. package/src/plugin/common-layout/index.ts +19 -0
  130. package/src/plugin/common-layout/mcp.ts +362 -0
  131. package/src/plugin/common-layout/paths.ts +70 -0
  132. package/src/plugin/common-layout/read.ts +1282 -0
  133. package/src/plugin/common-layout/types.ts +77 -0
  134. package/src/plugin/common-layout-names.ts +108 -0
  135. package/src/plugin/hook-module/capabilities.ts +500 -0
  136. package/src/plugin/hook-module/consent.ts +96 -0
  137. package/src/plugin/hook-module/events.ts +177 -0
  138. package/src/plugin/hook-module/host.ts +649 -0
  139. package/src/plugin/hook-module/index.ts +52 -0
  140. package/src/plugin/hook-module/scan.ts +1004 -0
  141. package/src/plugin/hook-module/worker-source.ts +500 -0
  142. package/src/plugin/index.ts +13 -1
  143. package/src/plugin/lifecycle.ts +179 -17
  144. package/src/plugin/loader.ts +97 -18
  145. package/src/plugin/shell-hook-env.ts +7 -0
  146. package/src/plugin/shell-hook.ts +91 -70
  147. package/src/prompt/coding-agent-doctrine.ts +1 -1
  148. package/src/provider/side-request-reasoning.ts +5 -1
  149. package/src/public-runtime.ts +35 -0
  150. package/src/runtime/query/auto-review/classifier.ts +14 -2
  151. package/src/runtime/query/auto-review/failure.ts +52 -7
  152. package/src/runtime/query/review-policy.ts +30 -6
  153. package/src/runtime/query/turn/check-scope.ts +90 -0
  154. package/src/runtime/query/turn/outcome-ledger.ts +11 -0
  155. package/src/skills/loader.ts +26 -9
  156. package/src/skills/registry.ts +9 -1
  157. package/src/tools/builtins/job.ts +4 -1
  158. package/src/tools/builtins/wait-for-job.ts +3 -1
  159. package/src/types/plugin/index.ts +81 -24
  160. package/src/types/skills/index.ts +7 -0
@@ -34,15 +34,20 @@ import type {
34
34
  PluginManifest,
35
35
  PluginScope,
36
36
  } from '../types/plugin/index.js'
37
- import { assertPluginHookEvent } from '../types/plugin/index.js'
37
+ import {
38
+ CommonLayoutManifestSchema,
39
+ PluginManifestSchema,
40
+ assertPluginHookEvent,
41
+ } from '../types/plugin/index.js'
38
42
  import type { SessionEvent } from '../types/session/index.js'
39
43
  import type { ToolDefinition } from '../types/tool/index.js'
40
44
  import { toErrorMessage } from '../utils/error.js'
41
45
  import { generatePluginId } from '../utils/id.js'
42
46
  import { SCOPE_ATTRIBUTE } from '../utils/log/types.js'
43
47
  import type { Logger } from '../utils/logger.js'
48
+ import type { LayoutFinding } from './common-layout/types.js'
44
49
  import { type DefinedPlugin, definePlugin } from './define.js'
45
- import { assertEnableable, loadPluginManifest } from './loader.js'
50
+ import { assertEnableable, loadPluginManifestWithFindings } from './loader.js'
46
51
 
47
52
  interface PluginContributionRecord {
48
53
  toolNames: string[]
@@ -53,6 +58,8 @@ interface PluginContributionRecord {
53
58
  configReleases: Array<() => void>
54
59
  /** Namespaced skill names, so rollback and disable can take them back. */
55
60
  skillNames: string[]
61
+ /** Ends what a host started for the plugin (a hooks module's worker). */
62
+ disposers: Array<() => Promise<void> | void>
56
63
  }
57
64
 
58
65
  interface PluginAdmission {
@@ -62,6 +69,8 @@ interface PluginAdmission {
62
69
  readonly rootDir: string
63
70
  readonly installedAt: number
64
71
  readonly inCode?: DefinedPlugin
72
+ /** What the common-layout reader found when the manifest came from that layout. */
73
+ readonly findings?: readonly LayoutFinding[]
65
74
  }
66
75
 
67
76
  const DEFAULT_PLUGIN_MCP_CONNECT_TIMEOUT_MS = 10_000
@@ -174,6 +183,14 @@ function immutableManifest(manifest: PluginDefinition['manifest']): PluginDefini
174
183
  : {}),
175
184
  }),
176
185
  )
186
+ const remoteMcpServers = manifest.remoteMcpServers?.map((server) =>
187
+ Object.freeze({
188
+ name: server.name,
189
+ url: server.url,
190
+ ...(server.transport !== undefined ? { transport: server.transport } : {}),
191
+ ...(server.headers ? { headers: Object.freeze({ ...server.headers }) } : {}),
192
+ }),
193
+ )
177
194
  return Object.freeze({
178
195
  name: manifest.name,
179
196
  version: manifest.version,
@@ -187,10 +204,14 @@ function immutableManifest(manifest: PluginDefinition['manifest']): PluginDefini
187
204
  ? { hookEntries: Object.freeze(structuredClone(manifest.hookEntries)) }
188
205
  : {}),
189
206
  ...(mcpServers ? { mcpServers: Object.freeze(mcpServers) } : {}),
207
+ ...(remoteMcpServers ? { remoteMcpServers: Object.freeze(remoteMcpServers) } : {}),
190
208
  ...(manifest.connectors ? { connectors: Object.freeze([...manifest.connectors]) } : {}),
191
209
  ...(manifest.personas ? { personas: Object.freeze([...manifest.personas]) } : {}),
192
210
  ...(manifest.commands ? { commands: Object.freeze([...manifest.commands]) } : {}),
193
211
  ...(manifest.agents ? { agents: Object.freeze([...manifest.agents]) } : {}),
212
+ ...(manifest.platforms ? { platforms: Object.freeze([...manifest.platforms]) } : {}),
213
+ ...(manifest.engines ? { engines: Object.freeze({ ...manifest.engines }) } : {}),
214
+ ...(manifest.layout !== undefined ? { layout: manifest.layout } : {}),
194
215
  })
195
216
  }
196
217
 
@@ -218,6 +239,21 @@ export interface PluginLifecycleManagerConfig {
218
239
  skillRegistry?: SkillRegistry
219
240
  log: Logger
220
241
  hookTimeoutMs?: number
242
+ /**
243
+ * The data and project folders a plugin's server configuration may name
244
+ * (`${NAMZU_PLUGIN_DATA}`, `${NAMZU_PROJECT_DIR}` and their layout-compatible
245
+ * spellings). Read for a plugin in the common layout. Absent, the project
246
+ * folder is the project scope's root and the data folder is not known, so a
247
+ * server that names it is reported and not started.
248
+ */
249
+ pathValues?: (plugin: {
250
+ readonly name: string
251
+ readonly scope: PluginScope
252
+ readonly rootDir: string
253
+ }) => {
254
+ readonly data?: string
255
+ readonly project?: string
256
+ }
221
257
  /**
222
258
  * Hooks a host reads for a plugin when it is enabled, beyond the JavaScript
223
259
  * modules its manifest lists (shell, http and prompt entries a plugin ships,
@@ -232,6 +268,33 @@ export interface PluginLifecycleManagerConfig {
232
268
  readonly rootDir: string
233
269
  readonly scope: PluginScope
234
270
  }) => readonly PluginHookDefinition[] | Promise<readonly PluginHookDefinition[]>
271
+ /**
272
+ * The hooks module a plugin ships, when the host runs it. Called when the
273
+ * plugin is enabled; the hooks it returns are attached beside the plugin's
274
+ * others, and `dispose` is called when the plugin is disabled, uninstalled
275
+ * or its enable is rolled back, so the module lives exactly as long as the
276
+ * plugin is on. A module that cannot load answers with no hooks; it must not
277
+ * throw for that, so one plugin's bad module does not stop the plugin's other
278
+ * contributions. Not called for a plugin defined in code.
279
+ */
280
+ hookModuleFor?: (plugin: {
281
+ readonly id: PluginId
282
+ readonly manifest: PluginManifest
283
+ readonly rootDir: string
284
+ readonly scope: PluginScope
285
+ }) =>
286
+ | {
287
+ readonly hooks: readonly PluginHookDefinition[]
288
+ readonly dispose: () => Promise<void> | void
289
+ }
290
+ | undefined
291
+ | Promise<
292
+ | {
293
+ readonly hooks: readonly PluginHookDefinition[]
294
+ readonly dispose: () => Promise<void> | void
295
+ }
296
+ | undefined
297
+ >
235
298
  /**
236
299
  * Where each MCP server's reconnect policy is registered, so an operator
237
300
  * can retune it while a turn is live.
@@ -316,6 +379,8 @@ export class PluginLifecycleManager {
316
379
  private pluginAdmissions: Map<PluginId, PluginAdmission> = new Map()
317
380
  private hookTimeoutMs: number
318
381
  private readonly hooksFor: PluginLifecycleManagerConfig['hooksFor']
382
+ private readonly hookModuleFor: PluginLifecycleManagerConfig['hookModuleFor']
383
+ private readonly pathValues: PluginLifecycleManagerConfig['pathValues']
319
384
  private readonly configRegistry: ConfigRegistry | undefined
320
385
  private readonly scopeRoots: Readonly<Record<PluginScope, string>>
321
386
  private log: Logger
@@ -343,6 +408,8 @@ export class PluginLifecycleManager {
343
408
  this.scopeRoots = Object.freeze({ ...config.scopeRoots })
344
409
  this.hookTimeoutMs = config.hookTimeoutMs ?? HOOK_TIMEOUT_MS
345
410
  this.hooksFor = config.hooksFor
411
+ this.hookModuleFor = config.hookModuleFor
412
+ this.pathValues = config.pathValues
346
413
  this.configRegistry = config.configRegistry
347
414
  this.mcpToolPolicies = config.mcpToolPolicies
348
415
  this.onMCPToolDrift = config.onMCPToolDrift
@@ -598,7 +665,16 @@ export class PluginLifecycleManager {
598
665
  async install(
599
666
  pluginDir: string,
600
667
  scope: PluginScope,
601
- options: { readonly authorityRoot?: string } = {},
668
+ options: {
669
+ readonly authorityRoot?: string
670
+ /**
671
+ * A manifest the caller already read (a marketplace install reads the
672
+ * folder with its install record). It is validated again here and the
673
+ * folder is still resolved inside the authority root; only the reading
674
+ * is skipped.
675
+ */
676
+ readonly manifestSource?: { readonly manifest: PluginManifest }
677
+ } = {},
602
678
  ): Promise<PluginDefinition> {
603
679
  // Admission and enable read the same constructor-owned capability. The
604
680
  // loader defaults this to false for direct callers; a manager that really
@@ -607,11 +683,11 @@ export class PluginLifecycleManager {
607
683
  options.authorityRoot ?? this.scopeRoots[scope],
608
684
  pluginDir,
609
685
  )
610
- const manifest = immutableManifest(
611
- await loadPluginManifest(rootDir, {
612
- skillsSupported: Boolean(this.skillRegistry),
613
- }),
614
- )
686
+ const loaded = options.manifestSource
687
+ ? await this.admitGivenManifest(options.manifestSource.manifest)
688
+ : await this.readManifest(rootDir, scope)
689
+ const manifest = immutableManifest(loaded.manifest)
690
+ const findings = loaded.findings
615
691
 
616
692
  const existing = this.pluginRegistry.findByName(manifest.name)
617
693
  if (existing) {
@@ -625,6 +701,7 @@ export class PluginLifecycleManager {
625
701
  scope,
626
702
  rootDir,
627
703
  installedAt: Date.now(),
704
+ ...(findings.length > 0 ? { findings } : {}),
628
705
  })
629
706
  const definition = this.definitionFrom(admission, 'installed')
630
707
 
@@ -649,6 +726,50 @@ export class PluginLifecycleManager {
649
726
  return definition
650
727
  }
651
728
 
729
+ /** The manifest of a folder, read as Namzu's own or in the common layout. */
730
+ private async readManifest(
731
+ rootDir: string,
732
+ scope: PluginScope,
733
+ ): Promise<{
734
+ readonly manifest: PluginManifest
735
+ readonly findings: readonly LayoutFinding[]
736
+ }> {
737
+ const pathValues = this.pathValues
738
+ return loadPluginManifestWithFindings(rootDir, {
739
+ skillsSupported: Boolean(this.skillRegistry),
740
+ commonLayout: {
741
+ installedIds: new Set(this.pluginRegistry.getAll().map((plugin) => plugin.manifest.name)),
742
+ paths: (name) => ({
743
+ project: this.scopeRoots.project,
744
+ ...(pathValues?.({ name, scope, rootDir }) ?? {}),
745
+ }),
746
+ },
747
+ })
748
+ }
749
+
750
+ private async admitGivenManifest(given: PluginManifest): Promise<{
751
+ readonly manifest: PluginManifest
752
+ readonly findings: readonly LayoutFinding[]
753
+ }> {
754
+ const parsed = (
755
+ given.layout === 'common' ? CommonLayoutManifestSchema : PluginManifestSchema
756
+ ).parse(given)
757
+ const manifest: PluginManifest =
758
+ given.layout === 'common' ? { ...parsed, layout: 'common' } : parsed
759
+ assertEnableable(manifest, {
760
+ skillsSupported: Boolean(this.skillRegistry),
761
+ })
762
+ return { manifest, findings: [] }
763
+ }
764
+
765
+ /**
766
+ * What the common-layout reader found for a plugin read from that layout
767
+ * (empty for any other). Notes only: nothing here changes what loads.
768
+ */
769
+ layoutFindings(pluginId: PluginId): readonly LayoutFinding[] {
770
+ return this.pluginAdmissions.get(pluginId)?.findings ?? []
771
+ }
772
+
652
773
  /** Register a host-authored plugin without reading a manifest or importing modules. */
653
774
  installDefined(plugin: DefinedPlugin, scope: PluginScope = 'project'): PluginDefinition {
654
775
  const defined = definePlugin(plugin)
@@ -714,11 +835,7 @@ export class PluginLifecycleManager {
714
835
 
715
836
  private async readLegacyAdmission(plugin: PluginDefinition): Promise<PluginAdmission> {
716
837
  const rootDir = await resolveWithinReal(this.scopeRoots[plugin.scope], plugin.rootDir)
717
- const manifest = immutableManifest(
718
- await loadPluginManifest(rootDir, {
719
- skillsSupported: Boolean(this.skillRegistry),
720
- }),
721
- )
838
+ const manifest = immutableManifest((await this.readManifest(rootDir, plugin.scope)).manifest)
722
839
  if (manifest.name !== plugin.manifest.name) {
723
840
  throw new Error(
724
841
  `Plugin registry record "${plugin.id}" names "${plugin.manifest.name}", but its admitted manifest names "${manifest.name}". Reinstall the plugin from its declared scope root.`,
@@ -808,6 +925,7 @@ export class PluginLifecycleManager {
808
925
  mcpNamesBySource: new Map(),
809
926
  configReleases: [],
810
927
  skillNames: [],
928
+ disposers: [],
811
929
  }
812
930
 
813
931
  try {
@@ -861,10 +979,14 @@ export class PluginLifecycleManager {
861
979
  }
862
980
  for (const skillPath of manifest.skills) {
863
981
  const absolutePath = await resolveWithinReal(admission.rootDir, skillPath)
864
- const { skill } = await loadSkill(absolutePath, 'metadata', this.log)
982
+ const { skill } = await loadSkill(absolutePath, 'metadata', this.log, {
983
+ anyFolderName:
984
+ manifest.layout === 'common' && (skillPath === '.' || skillPath === './'),
985
+ })
865
986
  const namespacedName = manifest.name + PLUGIN_NAMESPACE_SEPARATOR + skill.metadata.name
866
987
  skillRegistry.add(namespacedName, {
867
988
  ...skill,
989
+ pluginRoot: admission.rootDir,
868
990
  metadata: { ...skill.metadata, name: namespacedName },
869
991
  })
870
992
  contributions.skillNames.push(namespacedName)
@@ -874,7 +996,7 @@ export class PluginLifecycleManager {
874
996
  // Load hooks
875
997
  if (admission.inCode) {
876
998
  for (const hook of admission.inCode.hooks) this.registerHook(pluginId, hook)
877
- } else if (manifest.hooks && manifest.hooks.length > 0) {
999
+ } else if (manifest.hooks && manifest.hooks.length > 0 && manifest.layout !== 'common') {
878
1000
  for (const hookPath of manifest.hooks) {
879
1001
  // A `.json` path is a file of hook entries, read by the host through
880
1002
  // `hooksFor`; it is not a module.
@@ -909,6 +1031,20 @@ export class PluginLifecycleManager {
909
1031
  for (const hook of shipped) this.addHook(pluginId, hook, true)
910
1032
  }
911
1033
 
1034
+ // The module the plugin ships, run in a worker of its own by the host.
1035
+ if (!admission.inCode && this.hookModuleFor) {
1036
+ const started = await this.hookModuleFor({
1037
+ id: pluginId,
1038
+ manifest,
1039
+ rootDir: admission.rootDir,
1040
+ scope: admission.scope,
1041
+ })
1042
+ if (started !== undefined) {
1043
+ contributions.disposers.push(started.dispose)
1044
+ for (const hook of started.hooks) this.registerHook(pluginId, hook)
1045
+ }
1046
+ }
1047
+
912
1048
  // Start MCP servers and adapt their tools
913
1049
  if (manifest.mcpServers && manifest.mcpServers.length > 0) {
914
1050
  for (const serverConfig of manifest.mcpServers) {
@@ -1074,6 +1210,7 @@ export class PluginLifecycleManager {
1074
1210
  contributions: PluginContributionRecord,
1075
1211
  ): Promise<void> {
1076
1212
  await this.stopMcpContributions(contributions, 'rollback')
1213
+ await this.runDisposers(contributions, 'rollback')
1077
1214
  for (const name of contributions.toolNames) {
1078
1215
  try {
1079
1216
  this.removeContributedTool(name)
@@ -1100,6 +1237,22 @@ export class PluginLifecycleManager {
1100
1237
  this.notifyHooksChanged()
1101
1238
  }
1102
1239
 
1240
+ private async runDisposers(
1241
+ contributions: PluginContributionRecord,
1242
+ operation: 'rollback' | 'disable',
1243
+ ): Promise<void> {
1244
+ for (const dispose of contributions.disposers.splice(0)) {
1245
+ try {
1246
+ await dispose()
1247
+ } catch (error) {
1248
+ this.log.warn('Plugin disposer failed', {
1249
+ 'namzu.plugin.operation': operation,
1250
+ 'exception.message': toErrorMessage(error),
1251
+ })
1252
+ }
1253
+ }
1254
+ }
1255
+
1103
1256
  private async stopMcpContributions(
1104
1257
  contributions: PluginContributionRecord,
1105
1258
  operation: 'rollback' | 'disable',
@@ -1158,6 +1311,7 @@ export class PluginLifecycleManager {
1158
1311
  }
1159
1312
 
1160
1313
  await this.stopMcpContributions(contributions, 'disable')
1314
+ await this.runDisposers(contributions, 'disable')
1161
1315
 
1162
1316
  // Unregister file-declared tools; MCP entries were removed above.
1163
1317
  for (const name of contributions.toolNames) {
@@ -1258,7 +1412,11 @@ export class PluginLifecycleManager {
1258
1412
  // by one, `post_*` in reverse, each seeing the previous hook's `modify`.
1259
1413
  const entries = handlers.map((hookEntry) => ({
1260
1414
  hookEntry,
1261
- hookContext: { ...context, pluginId: hookEntry.pluginId, event } as PluginHookContext,
1415
+ hookContext: {
1416
+ ...context,
1417
+ pluginId: hookEntry.pluginId,
1418
+ event,
1419
+ } as PluginHookContext,
1262
1420
  }))
1263
1421
  for (const { hookEntry } of entries) {
1264
1422
  if (!emitSessionEvent) break
@@ -1318,7 +1476,11 @@ export class PluginLifecycleManager {
1318
1476
  },
1319
1477
  hookContext: PluginHookContext,
1320
1478
  callerSignal: AbortSignal | undefined,
1321
- ): Promise<{ hookEntry: typeof hookEntry; result: PluginHookResult; durationMs: number }> {
1479
+ ): Promise<{
1480
+ hookEntry: typeof hookEntry
1481
+ result: PluginHookResult
1482
+ durationMs: number
1483
+ }> {
1322
1484
  const { handler: handlerFn } = hookEntry
1323
1485
  const start = performance.now()
1324
1486
  let result: PluginHookResult
@@ -12,6 +12,9 @@ import { toErrorMessage } from '../utils/error.js'
12
12
  import { SCOPE_ATTRIBUTE } from '../utils/log/types.js'
13
13
  import { type Logger, resolveLogger } from '../utils/logger.js'
14
14
  import { VERSION } from '../version.js'
15
+ import { COMMON_MANIFEST_DIR, COMMON_MANIFEST_FILE } from './common-layout-names.js'
16
+ import { readCommonLayout } from './common-layout/read.js'
17
+ import type { CommonLayoutOptions, LayoutFinding } from './common-layout/types.js'
15
18
  import { type PluginHostFacts, assertCompatible } from './compat.js'
16
19
 
17
20
  /**
@@ -29,7 +32,9 @@ export async function discoverPlugins(parentDir: string, log?: Logger): Promise<
29
32
  // it. Without a `log`, discovery emits nothing (`resolveLogger` is
30
33
  // `NOOP_LOGGER`), which is the deliberate default rather than a fallback
31
34
  // to somebody else's stream.
32
- const logger = resolveLogger(log).child({ [SCOPE_ATTRIBUTE]: 'plugin/loader' })
35
+ const logger = resolveLogger(log).child({
36
+ [SCOPE_ATTRIBUTE]: 'plugin/loader',
37
+ })
33
38
  const dirs: string[] = []
34
39
 
35
40
  try {
@@ -52,54 +57,120 @@ export async function discoverPlugins(parentDir: string, log?: Logger): Promise<
52
57
  // directory listed is not the directory loaded.
53
58
  const s = await lstat(fullPath)
54
59
  if (s.isSymbolicLink()) {
55
- logger.warn('Refusing a symlinked plugin directory', { 'namzu.plugin.path': fullPath })
60
+ logger.warn('Refusing a symlinked plugin directory', {
61
+ 'namzu.plugin.path': fullPath,
62
+ })
56
63
  continue
57
64
  }
58
65
  if (!s.isDirectory()) continue
59
66
 
60
- const manifestPath = join(fullPath, PLUGIN_MANIFEST_FILENAME)
61
- try {
62
- const manifest = await lstat(manifestPath)
63
- if (manifest.isSymbolicLink() || !manifest.isFile()) {
64
- logger.warn('Refusing a non-regular plugin manifest', {
65
- 'namzu.plugin.path': fullPath,
66
- 'namzu.plugin.manifest_path': manifestPath,
67
- })
68
- continue
67
+ // Namzu's own manifest first, then the common layout's. Either must be a
68
+ // regular file in a regular folder; a folder with neither is not a plugin.
69
+ let admitted = false
70
+ for (const manifestPath of [
71
+ join(fullPath, PLUGIN_MANIFEST_FILENAME),
72
+ join(fullPath, COMMON_MANIFEST_DIR, COMMON_MANIFEST_FILE),
73
+ ]) {
74
+ try {
75
+ const manifest = await lstat(manifestPath)
76
+ if (manifest.isSymbolicLink() || !manifest.isFile()) {
77
+ logger.warn('Refusing a non-regular plugin manifest', {
78
+ 'namzu.plugin.path': fullPath,
79
+ 'namzu.plugin.manifest_path': manifestPath,
80
+ })
81
+ continue
82
+ }
83
+ if (manifestPath !== join(fullPath, PLUGIN_MANIFEST_FILENAME)) {
84
+ const folder = await lstat(join(fullPath, COMMON_MANIFEST_DIR))
85
+ if (folder.isSymbolicLink() || !folder.isDirectory()) {
86
+ logger.warn('Refusing a non-regular plugin manifest folder', {
87
+ 'namzu.plugin.path': fullPath,
88
+ })
89
+ continue
90
+ }
91
+ }
92
+ admitted = true
93
+ break
94
+ } catch {
95
+ // No manifest at this path — try the next
69
96
  }
70
- dirs.push(fullPath)
71
- } catch {
72
- // No manifest in this directory — skip
73
97
  }
98
+ if (admitted) dirs.push(fullPath)
74
99
  }
75
100
  } catch {
76
- logger.debug('Plugins directory not found', { 'namzu.plugin.parent_dir': parentDir })
101
+ logger.debug('Plugins directory not found', {
102
+ 'namzu.plugin.parent_dir': parentDir,
103
+ })
77
104
  }
78
105
 
79
106
  return dirs
80
107
  }
81
108
 
109
+ /**
110
+ * What a plugin folder read through the loader holds: the manifest to admit,
111
+ * and, for a folder in the common plugin layout, what the reader found.
112
+ */
113
+ export interface LoadedPluginManifest {
114
+ readonly manifest: PluginManifest
115
+ /** Findings of the common-layout reader; empty for a Namzu `plugin.json`. */
116
+ readonly findings: readonly LayoutFinding[]
117
+ }
118
+
82
119
  /**
83
120
  * Reads and validates a plugin manifest from a plugin directory.
84
121
  * Throws on invalid manifest (fail fast).
122
+ *
123
+ * A folder with Namzu's own `plugin.json` is read as before. Without one, the
124
+ * folder is read in the common plugin layout (its own manifest, or only its
125
+ * conventional folders), and a refused plugin throws with the reason.
85
126
  */
86
127
  export async function loadPluginManifest(
87
128
  pluginDir: string,
88
129
  capabilities: PluginEnablementCapabilities = {},
89
130
  ): Promise<PluginManifest> {
131
+ return (await loadPluginManifestWithFindings(pluginDir, capabilities)).manifest
132
+ }
133
+
134
+ /** {@link loadPluginManifest}, and what the common-layout reader found. */
135
+ /**
136
+ * A folder read in the common plugin layout that Namzu refuses (nothing usable,
137
+ * an unmet dependency, a bad name). A host can report it and carry on loading
138
+ * the other plugins, unlike a malformed Namzu manifest.
139
+ */
140
+ export class PluginLayoutRefusedError extends Error {
141
+ override readonly name = 'PluginLayoutRefusedError'
142
+ }
143
+
144
+ export async function loadPluginManifestWithFindings(
145
+ pluginDir: string,
146
+ capabilities: PluginEnablementCapabilities = {},
147
+ ): Promise<LoadedPluginManifest> {
90
148
  const root = await lstat(pluginDir)
91
149
  if (root.isSymbolicLink() || !root.isDirectory()) {
92
150
  throw new Error(`Plugin root must be a regular directory: ${pluginDir}`)
93
151
  }
94
152
  const manifestPath = join(pluginDir, PLUGIN_MANIFEST_FILENAME)
95
- const manifestEntry = await lstat(manifestPath)
153
+ const manifestEntry = await lstat(manifestPath).catch((error: NodeJS.ErrnoException) => {
154
+ if (error.code === 'ENOENT') return undefined
155
+ throw error
156
+ })
157
+ if (manifestEntry === undefined) {
158
+ const read = await readCommonLayout(pluginDir, capabilities.commonLayout)
159
+ if (read.refusal !== undefined || read.manifest === undefined) {
160
+ throw new PluginLayoutRefusedError(
161
+ `Plugin at ${pluginDir} was not loaded: ${read.refusal?.message ?? 'there is no manifest here.'}`,
162
+ )
163
+ }
164
+ assertEnableable(read.manifest, capabilities)
165
+ return { manifest: read.manifest, findings: read.findings }
166
+ }
96
167
  if (manifestEntry.isSymbolicLink() || !manifestEntry.isFile()) {
97
168
  throw new Error(`Plugin manifest must be a regular file, not a symlink: ${manifestPath}`)
98
169
  }
99
170
  const raw = await readFile(manifestPath, 'utf-8')
100
171
  const manifest = parsePluginManifestText(raw)
101
172
  assertEnableable(manifest, capabilities)
102
- return manifest
173
+ return { manifest, findings: [] }
103
174
  }
104
175
 
105
176
  /**
@@ -209,6 +280,12 @@ export interface PluginEnablementCapabilities extends PluginHostFacts {
209
280
  * nowhere.
210
281
  */
211
282
  readonly skillsSupported?: boolean
283
+ /**
284
+ * Options for the common-layout reader, used when the folder has no
285
+ * `plugin.json`. `entrySidecar` belongs to a caller that holds an install
286
+ * record for the folder; a discovered folder never passes it.
287
+ */
288
+ readonly commonLayout?: CommonLayoutOptions
212
289
  }
213
290
 
214
291
  export function assertEnableable(
@@ -294,7 +371,9 @@ export async function discoverAllPluginDirs(
294
371
  ): Promise<{ project: string[]; user: string[] }> {
295
372
  // Resolved here too — see discoverPlugins above for why module scope
296
373
  // froze this logger's level, and its very reference, at import time.
297
- const logger = resolveLogger(options?.log).child({ [SCOPE_ATTRIBUTE]: 'plugin/loader' })
374
+ const logger = resolveLogger(options?.log).child({
375
+ [SCOPE_ATTRIBUTE]: 'plugin/loader',
376
+ })
298
377
  if (options?.enabled === false || options?.autoDiscovery === false) {
299
378
  logger.debug('Plugin discovery skipped', {
300
379
  'namzu.plugin.enabled': options.enabled,
@@ -69,6 +69,13 @@ export interface PluginFolders {
69
69
  readonly root: string
70
70
  /** The folder kept for the plugin's data, under the Namzu home and never inside `root`. */
71
71
  readonly dataDir: string
72
+ /**
73
+ * Set for a plugin read through the common plugin layout: its hooks also
74
+ * get the three compatibility names in their environment, so a script the
75
+ * hook starts finds its folders under the names it was written for. A Namzu
76
+ * plugin's environment is not widened.
77
+ */
78
+ readonly layout?: 'common'
72
79
  }
73
80
 
74
81
  /** Whether a variable name stands for a plugin folder by its ending. */