@jini-ai/agent-plugins 0.4.0 → 0.4.2

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 (47) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +19 -0
  3. package/core/persistent-state.d.ts +25 -0
  4. package/core/persistent-state.js +260 -0
  5. package/dist/lifecycle/activation.js +38 -166
  6. package/dist/lifecycle/bundled-digests.d.ts +6 -0
  7. package/dist/lifecycle/bundled-digests.js +156 -0
  8. package/dist/lifecycle/bundled-source-archive.d.ts +3 -0
  9. package/dist/lifecycle/bundled-source-archive.js +74 -0
  10. package/dist/lifecycle/contribution-loader.d.ts +102 -0
  11. package/dist/lifecycle/contribution-loader.js +92 -0
  12. package/dist/lifecycle/fetch-archive.d.ts +1 -2
  13. package/dist/lifecycle/index.d.ts +34 -4
  14. package/dist/lifecycle/index.js +19 -1
  15. package/dist/lifecycle/install-from-url.d.ts +2 -5
  16. package/dist/lifecycle/install-from-url.js +2 -5
  17. package/dist/lifecycle/install.d.ts +57 -2
  18. package/dist/lifecycle/install.js +228 -13
  19. package/dist/lifecycle/layout.d.ts +10 -0
  20. package/dist/lifecycle/layout.js +9 -1
  21. package/dist/lifecycle/manifest.d.ts +3 -9
  22. package/dist/lifecycle/manifest.js +4 -11
  23. package/dist/lifecycle/package-paths.d.ts +2 -0
  24. package/dist/lifecycle/package-paths.js +62 -0
  25. package/dist/lifecycle/persistent-state.d.ts +24 -0
  26. package/dist/lifecycle/persistent-state.js +32 -0
  27. package/dist/lifecycle/ports.d.ts +15 -3
  28. package/dist/lifecycle/resolve-agent-plugin-refs.d.ts +14 -2
  29. package/dist/lifecycle/resolve-agent-plugin-refs.js +173 -10
  30. package/dist/lifecycle/retire-bundled.d.ts +50 -1
  31. package/dist/lifecycle/retire-bundled.js +10 -3
  32. package/dist/lifecycle/search.d.ts +63 -0
  33. package/dist/lifecycle/search.js +29 -0
  34. package/dist/lifecycle/seed-bundled.d.ts +18 -1
  35. package/dist/lifecycle/seed-bundled.js +139 -1
  36. package/dist/lifecycle/set-enabled.d.ts +7 -0
  37. package/dist/lifecycle/set-enabled.js +6 -1
  38. package/dist/lifecycle/trusted-plugin-files.d.ts +38 -0
  39. package/dist/lifecycle/trusted-plugin-files.js +19 -1
  40. package/dist/lifecycle/types.d.ts +12 -0
  41. package/dist/lifecycle/uninstall.d.ts +30 -0
  42. package/dist/lifecycle/uninstall.js +232 -4
  43. package/dist/lifecycle/yauzl-archive-reader.d.ts +0 -1
  44. package/dist/lifecycle/yauzl-archive-reader.js +5 -4
  45. package/package.json +34 -9
  46. package/persistent-state.d.ts +25 -0
  47. package/persistent-state.js +2 -0
@@ -1,3 +1,4 @@
1
+ import { createPersistentStateModule } from "./persistent-state.js";
1
2
  import { fetchAgentPluginArchive, maxAgentPluginArchiveBytes } from './fetch-archive.js';
2
3
  import { createSeedBundledModule } from './seed-bundled.js';
3
4
  import { createRetireBundledModule } from './retire-bundled.js';
@@ -12,12 +13,14 @@ import { createResolveAgentPluginRefsModule } from './resolve-agent-plugin-refs.
12
13
  import { createSearchModule } from './search.js';
13
14
  import { createSetEnabledModule } from './set-enabled.js';
14
15
  import { createTrustedPluginFilesModule } from './trusted-plugin-files.js';
16
+ import { createPluginContributionLoader } from './contribution-loader.js';
15
17
  import { createUninstallModule } from './uninstall.js';
16
18
  export * from './ports.js';
17
19
  export * from './layout.js';
18
20
  export * from './manifest.js';
19
21
  export * from './fetch-archive.js';
20
22
  export * from './mcp-provisioning.js';
23
+ export * from './contribution-loader.js';
21
24
  export { createAgentPluginActivations, ACTIVATIONS_FILENAME, ACTIVATIONS_LOCK_FILENAME, AgentPluginActivationsBusyError, AgentPluginActivationsUnreadableError } from './activation.js';
22
25
  export { BUNDLED_DIGESTS_FILENAME } from './bundled-digests.js';
23
26
  export { AgentPluginInstallError } from './install.js';
@@ -45,8 +48,14 @@ export function createAgentPluginLifecycle(required, optional = {}) {
45
48
  const search = createSearchModule(context);
46
49
  const enabled = createSetEnabledModule(context);
47
50
  const trusted = createTrustedPluginFilesModule(context);
51
+ const contributions = createPluginContributionLoader({
52
+ findPackages: input => trusted.findTrustedPluginPackages(input),
53
+ readFile: ({ plugin, filename }) => trusted.readTrustedPluginFile(plugin, filename),
54
+ importModule: ({ plugin, modulePath }) => trusted.importContainedModule(plugin, modulePath),
55
+ });
48
56
  const uninstall = createUninstallModule(context);
49
57
  return {
58
+ ...contributions,
50
59
  fetchAgentPluginArchive: (input, optional = {}) => fetchAgentPluginArchive({ ...input, fetch: context.fetch, outboundGuard: context.outboundGuard }, optional),
51
60
  maxAgentPluginArchiveBytes,
52
61
  seedBundledAgentPlugins: createSeedBundledModule(context).seedBundledAgentPlugins,
@@ -63,6 +72,9 @@ export function createAgentPluginLifecycle(required, optional = {}) {
63
72
  findTrustedPluginPackages: (input, optional = {}) => trusted.findTrustedPluginPackages({ ...input, ...optional }),
64
73
  previewAgentPluginUninstall: uninstall.previewAgentPluginUninstall,
65
74
  uninstallAgentPlugin: uninstall.uninstallAgentPlugin,
75
+ stageForRemoval: (input, _optional = {}) => uninstall.stageForRemoval(input.packagesDir, input.packageRoot),
76
+ restoreStagedTrees: (input, _optional = {}) => uninstall.restoreStagedTrees(input.staged, input.cause),
77
+ removeFrozenPackageTree: (input, _optional = {}) => uninstall.removeFrozenPackageTree(input.root),
66
78
  isAgentPluginActive: (input) => activation.isAgentPluginActive(input.activations, input.pluginId),
67
79
  filterActiveAgentPlugins: (input) => activation.filterActiveAgentPlugins(input.activations, input.items, item => input.pluginIdOf({ item })),
68
80
  readAgentPluginActivations: (input) => activation.readAgentPluginActivations(input.workspaceRoot),
@@ -80,7 +92,13 @@ export function createAgentPluginLifecycle(required, optional = {}) {
80
92
  readInstalledMcpServerIds: (input) => capabilities.readInstalledMcpServerIds(input.packageRoot),
81
93
  readInstalledMcpServers: (input) => capabilities.readInstalledMcpServers(input.packageRoot),
82
94
  resolveAgentPluginRefs: (input, optional = {}) => refs.resolveAgentPluginRefs(input.pluginRefIds, input.workspaceLayout, optional.deliveryMode ?? context.deliveryMode),
83
- listInstalledPlugins: (input) => refs.listInstalledPlugins(input.packagesDir),
95
+ pluginMemory: (input) => createPersistentStateModule(context).memory({
96
+ workspaceRoot: context.layout.forWorkspace({ workspaceId: input.workspaceId }).root, pluginId: input.pluginId,
97
+ }),
98
+ migratePluginLayout: (input) => createPersistentStateModule(context).migrate({
99
+ workspaceRoot: context.layout.forWorkspace({ workspaceId: input.workspaceId }).root,
100
+ }),
101
+ listInstalledPlugins: (input) => refs.listInstalledPlugins(input.workspaceRoot),
84
102
  isInstalledDigestPresent: (input) => refs.isInstalledDigestPresent(input.packagesDir, input.archiveDigest),
85
103
  rankInstalledAgentPlugins: (input) => search.rankInstalledAgentPlugins(input.query, input.candidates, input.limit),
86
104
  readTrustedPluginFile: (input) => trusted.readTrustedPluginFile(input.plugin, input.filename),
@@ -1,20 +1,17 @@
1
1
  /**
2
- * Historical lifecycle rationale, carried with the implementation during consumer adoption.
3
2
  *
4
- * Host command-line rationale retained when the unused URL-install CLI was removed:
3
+ * Caller integrity constraints:
5
4
  * - A digest-only preview downloads and reports SHA-256 without installing. Hashing those bytes
6
5
  * does not establish provenance; the host must make that distinction visible to the operator.
7
6
  * - A real install requires exactly one explicit integrity decision: an independent pinned digest
8
7
  * or trust-on-first-use. Missing or conflicting choices must be refused before execution, with
9
8
  * diagnostics naming the choice the operator needs to supply.
10
- * - Keep pure token classification separate from argument validation and exit/reporting effects,
11
- * so both parsing and legal combinations are directly testable without a network or process exit.
12
9
  * - The CLI composes shared download/extraction operations; digest verification, expansion bounds,
13
10
  * path containment and tenant isolation belong in those operations rather than a CLI-only guard.
14
11
  * @file The one composition step that turns "here is an address" into "the plugin is on this
15
12
  * machine": {@link fetchAgentPluginArchive} -> `installAgentPlugin`.
16
13
  *
17
- * This is the caller `install.ts` was written for and never had. It adds no extraction, path, or
14
+ * It adds no extraction, path, or
18
15
  * manifest logic of its own — every one of those guarantees still comes from `install.ts`, reached
19
16
  * with bytes instead of a URL.
20
17
  *
@@ -1,20 +1,17 @@
1
1
  /**
2
- * Historical lifecycle rationale, carried with the implementation during consumer adoption.
3
2
  *
4
- * Host command-line rationale retained when the unused URL-install CLI was removed:
3
+ * Caller integrity constraints:
5
4
  * - A digest-only preview downloads and reports SHA-256 without installing. Hashing those bytes
6
5
  * does not establish provenance; the host must make that distinction visible to the operator.
7
6
  * - A real install requires exactly one explicit integrity decision: an independent pinned digest
8
7
  * or trust-on-first-use. Missing or conflicting choices must be refused before execution, with
9
8
  * diagnostics naming the choice the operator needs to supply.
10
- * - Keep pure token classification separate from argument validation and exit/reporting effects,
11
- * so both parsing and legal combinations are directly testable without a network or process exit.
12
9
  * - The CLI composes shared download/extraction operations; digest verification, expansion bounds,
13
10
  * path containment and tenant isolation belong in those operations rather than a CLI-only guard.
14
11
  * @file The one composition step that turns "here is an address" into "the plugin is on this
15
12
  * machine": {@link fetchAgentPluginArchive} -> `installAgentPlugin`.
16
13
  *
17
- * This is the caller `install.ts` was written for and never had. It adds no extraction, path, or
14
+ * It adds no extraction, path, or
18
15
  * manifest logic of its own — every one of those guarantees still comes from `install.ts`, reached
19
16
  * with bytes instead of a URL.
20
17
  *
@@ -2,6 +2,9 @@ import type { AgentPluginLayout } from "./layout.js";
2
2
  import type { AgentPluginLifecyclePorts } from "./ports.js";
3
3
  import type { InstalledAgentPlugin } from "./types.js";
4
4
  export type { InstalledAgentPlugin, InstalledAgentPluginSkill } from "./types.js";
5
+ /** One extraction/install failure reason. A caller (an admin route, a future marketplace installer)
6
+ * branches on `code` rather than parsing `message` — this codebase's own convention
7
+ * (`PluginLoadError.reason` in `plugin-runtime/loader.ts` is the direct precedent). */
5
8
  export type AgentPluginInstallErrorCode = "ARCHIVE_TOO_LARGE" | "DIGEST_MISMATCH" | "TOO_MANY_ENTRIES" | "UNSAFE_ENTRY_PATH" | "SYMLINK_ENTRY_REJECTED" | "UNSUPPORTED_ENTRY_KIND" | "DUPLICATE_ENTRY" | "FILE_TOO_LARGE" | "DECOMPRESSION_BOMB" | "TOTAL_SIZE_EXCEEDED" | "MANIFEST_MISSING" | "MANIFEST_INVALID" | "PUBLISH_FAILED";
6
9
  export declare class AgentPluginInstallError extends Error {
7
10
  readonly code: AgentPluginInstallErrorCode;
@@ -12,9 +15,14 @@ export declare class AgentPluginInstallError extends Error {
12
15
  cause?: unknown;
13
16
  });
14
17
  }
18
+ /** One archive entry. A symlink is modeled as its OWN kind rather than folded into "file" — the
19
+ * zip-slip research this module's header cites is explicit that the symlink-entry vector is
20
+ * distinct from the lexical-traversal one, and the two need independent tests and independent
21
+ * handling (see the "symlink entries are rejected outright" rule below). */
15
22
  export type AgentPluginArchiveEntry = {
16
23
  readonly kind: "file";
17
24
  readonly entryPath: string;
25
+ /** The archive format's own claimed size. Never trusted alone — see `DECOMPRESSION_BOMB`. */
18
26
  readonly declaredSize?: (number) | undefined;
19
27
  readonly executable?: (boolean) | undefined;
20
28
  readonly openReadStream: (required: Record<string, never>) => AsyncIterable<Uint8Array>;
@@ -30,6 +38,7 @@ export type AgentPluginArchiveEntry = {
30
38
  * `entries` must be a pure function of the archive bytes: an install iterates it twice, once to
31
39
  * check entry-count limits from metadata alone and once to extract.
32
40
  */
41
+ /** `entries` is a pure function of archive bytes: metadata preflight and extraction iterate it twice. */
33
42
  export interface AgentPluginArchiveReaderPort {
34
43
  entries(required: {
35
44
  readonly archive: Uint8Array;
@@ -37,16 +46,62 @@ export interface AgentPluginArchiveReaderPort {
37
46
  }
38
47
  export interface InstallAgentPluginRequired {
39
48
  readonly archive: Uint8Array;
49
+ /** Lowercase hex SHA-256 the caller expects `archive` to hash to — a real marketplace flow would
50
+ * supply this from the server's own metadata; verified here, never trusted from the archive
51
+ * itself. */
40
52
  readonly expectedSha256: string;
41
53
  readonly archiveReader: AgentPluginArchiveReaderPort;
54
+ /**
55
+ * SECURITY (2026-08-13, security pass Finding 2 — tenant isolation, `ADS-memory/reports/security/
56
+ * 2026-08-13-post-session-security-pass.md`): this field used to be an already-resolved
57
+ * `AgentPluginWorkspaceLayout` (`{ root, packages, staging, pluginDataDir }`) — a plain interface
58
+ * of four independently-typed string/function fields with no tag identifying which workspace it
59
+ * came from. Nothing stopped a caller from hand-assembling one from two DIFFERENT real
60
+ * `forWorkspace()` results (wrong variable capture, a stale cached layout, a future code path that
61
+ * builds one by hand instead of calling `forWorkspace`) — such a value type-checked identically to
62
+ * a correctly-resolved one, and `installAgentPlugin` published into whatever `packages`/`staging`
63
+ * it was handed with no consistency check. Proven at the unit level in `install.unit.test.ts`
64
+ * (a layout stitching workspace A's `root`/`pluginDataDir` onto workspace B's real `packages`/
65
+ * `staging` published into workspace B's real, on-disk store with no error).
66
+ *
67
+ * The fix: `installAgentPlugin` no longer accepts a pre-resolved workspace layout as input AT ALL.
68
+ * It takes the RAW MATERIALS instead — the INSTANCE-level layout (`resolveAgentPluginLayout()`,
69
+ * carries no workspace-scoped path of its own) plus one `workspaceId` — and calls
70
+ * `layout.forWorkspace(workspaceId)` itself, exactly once, internally. There is no longer any
71
+ * `AgentPluginWorkspaceLayout`-shaped parameter here for a caller to stitch fields into: the one
72
+ * and only path from these two inputs to `packages`/`staging`/`pluginDataDir` is the real
73
+ * `forWorkspace()` closure, called atomically, so the four resulting paths can never disagree with
74
+ * each other about which workspace they belong to. Passing a hand-built `{ packages, staging, ... }`
75
+ * literal where this field is expected is now a compile-time type error, not a runtime hazard.
76
+ *
77
+ * `workspaceId` must still come from the authenticated principal's own request context, never
78
+ * derived from `layout` itself (a hand-built or malicious `AgentPluginLayout` could fake a
79
+ * `forWorkspace` implementation identically) — that half of the guarantee is a caller obligation
80
+ * this type cannot enforce, the same way `archiveReader` above is a fully caller-trusted port. What
81
+ * IS closed is the specific, demonstrated bug class: stitching or hand-assembling an
82
+ * already-resolved workspace layout from parts.
83
+ */
42
84
  readonly layout: AgentPluginLayout;
85
+ /** The workspace this install is for. Combined with {@link layout} via `layout.forWorkspace(workspaceId)`
86
+ * — see the SECURITY note on {@link layout} above. */
43
87
  readonly workspaceId: string;
44
88
  }
45
- export type InstallAgentPluginOptional = {};
89
+ export interface InstallAgentPluginOptional {
90
+ /** Full bounded extraction/validation in staging, with no package or memory publication. */
91
+ readonly previewOnly?: boolean;
92
+ /** Host policy transaction inside the per-plugin lock after validation. Call publish exactly
93
+ * once to install; returning an existing package without calling it is a deduplicated no-op. */
94
+ /** Host policy transaction, called inside the existing per-plugin lock after validation.
95
+ * It must invoke publish exactly once to install. Shared lifecycle counterpart is in chat-tools.jini.patch. */
96
+ readonly publishGuard?: (required: {
97
+ readonly plugin: InstalledAgentPlugin;
98
+ readonly publish: () => Promise<InstalledAgentPlugin>;
99
+ }, optional: Record<string, never>) => Promise<InstalledAgentPlugin>;
100
+ }
46
101
  /** Internal composition; one state/queue instance per injected host context. */
47
102
  export declare function createInstallModule(ports: AgentPluginLifecyclePorts): {
48
103
  maxAgentPluginInstallArchiveBytes: () => number;
49
- installAgentPlugin: (required: InstallAgentPluginRequired, _optional?: InstallAgentPluginOptional) => Promise<InstalledAgentPlugin>;
104
+ installAgentPlugin: (required: InstallAgentPluginRequired, optional?: InstallAgentPluginOptional) => Promise<InstalledAgentPlugin>;
50
105
  indexInstalledRoot: (packageRoot: string, archiveDigest: string) => Promise<InstalledAgentPlugin>;
51
106
  };
52
107
  //# sourceMappingURL=install.d.ts.map
@@ -1,8 +1,79 @@
1
+ /**
2
+ * @file `installAgentPlugin()` — content-addressed, adversarially-hardened extraction of one Agent
3
+ * Plugin (agent-plugins.org) archive into one workspace's own `AgentPluginWorkspaceLayout.packages`.
4
+ *
5
+ * This is the highest-risk unit in the whole feature. Extraction is where containment actually
6
+ * breaks — everything downstream (capability projection, a future MCP admission gate) trusts that a
7
+ * "package root" is exactly the bytes the archive declared, laid out exactly where the archive said,
8
+ * with nothing outside it touched. Two independently-cited attack classes drive the checks here:
9
+ *
10
+ * - **Zip-slip / archive-extraction path traversal** (Snyk's zip-slip research; JFrog's
11
+ * `mholt/archiver` writeup; the `node-tar` GHSA-8qq5-rm4j-mr97 advisory) has two vectors: a
12
+ * crafted entry NAME containing `../` (caught by {@link normalizePackageEntryPath}'s lexical
13
+ * check), and a crafted SYMLINK entry whose target a later, innocent-looking entry name walks
14
+ * through. The convergent ecosystem fix for the second vector — and the one this module takes — is
15
+ * to refuse symlink entries outright rather than attempt to validate where they point: a plugin
16
+ * has no legitimate reason to ship a symlink inside its own package, and the Agent Plugins spec's
17
+ * own path-safety clause only ever promises symlinks MAY resolve inside the root, never that a
18
+ * client must accept one.
19
+ * - **Decompression bombs**, in both the classic "one file lies about its size" shape (caught by
20
+ * bounding bytes actually observed leaving `openReadStream()`, not the archive's own declared
21
+ * size) and the "many small files" shape (caught by a running total across the whole archive).
22
+ *
23
+ * Why this is a SEPARATE format from the host runtime-plugin format's loader (`plugin-runtime/loader.ts`), not a
24
+ * shared one: verified against that file directly. `loadPlugin()`'s BR-01 pipeline requires
25
+ * `manifest.integrity` (a per-file SHA-256 map), `manifest.sdkRange` checked against a runtime
26
+ * the host plugin SDK version, and a `server/index.mjs` ESM entry point it `import()`s. An Agent Plugin has
27
+ * none of those — no integrity map, no SDK range, no single entry point, and (per this feature's own
28
+ * FINAL decision) no code the host ever imports or executes at all in v1: Skills are markdown read for
29
+ * context injection, and MCP servers are preview-only (`capability-projection.ts`). Adapting Agent
30
+ * Plugins into `loadPlugin()`'s pipeline would mean inventing values for fields the open standard
31
+ * does not define, which is precisely the "package format vs. execution/trust model" conflation this
32
+ * feature's own owner-locked decisions (see `CTX-AGENTPLUGINS-2026-08-12.md` F2) rule out.
33
+ *
34
+ * Why the archive itself is a `ArchiveReaderPort`, not a concrete zip/tar library call: this
35
+ * codebase's own port+adapter discipline (`mcp-federation/ports.ts`'s `McpSessionPort`/
36
+ * `McpStdioChannel` split is the direct precedent) — and, concretely, no zip/tar dependency exists
37
+ * in this repo's `package.json` today. Choosing and vetting one (license, maintenance, streaming
38
+ * support) is a real decision outside a Programmer's scope to make silently; this module declares
39
+ * the seam and is exercised end-to-end by a scripted double, so a real adapter is a pure addition
40
+ * with zero change to the hardening logic here. See the handoff's REMAINING section.
41
+ *
42
+ * Content-addressing (`ws/<workspaceId>/packages/sha256/<archiveDigest>/`): the digest is verified
43
+ * BEFORE extraction ever touches the archive reader, so an install with a mismatched digest never
44
+ * even attempts to unpack — the digest is the trust boundary between "bytes a marketplace claimed"
45
+ * and "bytes this process is willing to run a parser over". A second install of byte-identical
46
+ * content BY THE SAME WORKSPACE is recognized from the digest alone and short-circuits without a
47
+ * second extraction. Deliberately NOT shared across workspaces — see `layout.ts`'s header (owner
48
+ * decision, tenant-grade isolation, 2026-08-12): this function resolves the workspace-scoped layout
49
+ * itself, exactly once, from an instance-level `AgentPluginLayout` plus one `workspaceId` (see the
50
+ * SECURITY note on {@link InstallAgentPluginRequired} below) — the caller is what makes two
51
+ * different workspaces' installs land in disjoint trees, simply by supplying a different
52
+ * `workspaceId` for each; this function's own internal `forWorkspace()` call is what turns that into
53
+ * disjoint, internally-consistent paths.
54
+ *
55
+ * Architectural role:
56
+ * The one place this feature performs filesystem writes for installed package bytes. No network I/O
57
+ * (archive bytes and their expected digest are caller-supplied — see the module header above for why
58
+ * "how does the archive get here" is out of this slice) and no execution of anything extracted.
59
+ */
60
+ /** One trimmed, non-blank string field of the host-supplied extension namespace, capped at `maxLength`, else
61
+ * `undefined` — a malformed value falls back rather than failing the whole index, since every
62
+ * field read this way is cosmetic. The cap stops a hostile package flooding the admin row.
63
+ * @complexity O(maxLength). */
64
+ /** `{ displayName }` when the host namespace's `displayName` is a non-blank string, else `{}` (callers
65
+ * title-case the id).
66
+ * @complexity O(1). */
67
+ /** `{ summary }` when the host namespace's `summary` is a non-blank string, else `{}` (the admin shows
68
+ * the spec `description` instead). Inner newlines are kept: blank lines are the paragraph breaks
69
+ * the admin renders.
70
+ * @complexity O(1). */
71
+ import { createPersistentStateModule } from "./persistent-state.js";
1
72
  /** Extracted from the host plugin lifecycle. Effects are supplied per host context. */
2
73
  import { createHash } from "node:crypto";
3
74
  import { constants } from "node:fs";
4
75
  import path from "node:path";
5
- import { parseAgentPluginManifest as parseAgentPluginManifestValue } from "./manifest.js";
76
+ import { readAgentPluginExtension, parseAgentPluginManifest as parseAgentPluginManifestValue } from "./manifest.js";
6
77
  import { PackagePathViolation } from "./package-paths.js";
7
78
  import { createPackagePathsModule } from "./package-paths.js";
8
79
  export class AgentPluginInstallError extends Error {
@@ -14,6 +85,7 @@ export class AgentPluginInstallError extends Error {
14
85
  }
15
86
  }
16
87
  function buildModule(ports) {
88
+ const persistentState = createPersistentStateModule(ports);
17
89
  const chmod = ports.filesystem.chmod.bind(ports.filesystem);
18
90
  const mkdir = ports.filesystem.mkdir.bind(ports.filesystem);
19
91
  const mkdtemp = ports.filesystem.mkdtemp.bind(ports.filesystem);
@@ -31,11 +103,32 @@ function buildModule(ports) {
31
103
  maxFileBytes: 16 * 1024 * 1024,
32
104
  maxTotalExtractedBytes: 64 * 1024 * 1024,
33
105
  };
106
+ /** Exposes `LIMITS.maxArchiveBytes` for a caller outside this module — concretely, a test asserting
107
+ * `@jini-ai/agent-plugins/lifecycle`'s fetch cap constant stays pinned to this one. The local fetch-archive.ts fork was deleted; see the package's fetch module
108
+ * for why it duplicates the number instead of importing it (no runtime dependency on `install.ts`);
109
+ * this accessor is what lets a test assert the pairing without either module importing the other. */
34
110
  function maxAgentPluginInstallArchiveBytes() {
35
111
  return LIMITS.maxArchiveBytes;
36
112
  }
37
113
  const SHA256_HEX_PATTERN = /^[a-f0-9]{64}$/;
38
- async function installAgentPlugin(required, _optional = {}) {
114
+ /**
115
+ * Verifies, extracts, and publishes one Agent Plugin archive.
116
+ *
117
+ * @throws {AgentPluginInstallError} For every expected failure — a hostile or malformed archive, a
118
+ * digest mismatch, an invalid manifest. Never partially publishes: any failure after extraction has
119
+ * begun removes the whole staging directory before rethrowing.
120
+ * @complexity O(e) in archive entry count, O(b) in total extracted bytes — both explicitly bounded
121
+ * by {@link LIMITS}, so this function's cost cannot be driven unbounded by a hostile archive.
122
+ */
123
+ // The ONE call that turns (instance layout, workspaceId) into real, internally-consistent
124
+ // packages/staging/pluginDataDir paths — see the SECURITY note on `InstallAgentPluginRequired.layout`
125
+ // above for why this replaces accepting an already-resolved `AgentPluginWorkspaceLayout` directly.
126
+ // `forWorkspace` itself still throws for a syntactically invalid `workspaceId`, unchanged.
127
+ // Whether this install succeeded (bytes now live at `finalRoot`) or failed, the staging
128
+ // transaction directory itself must never be left behind — on success `rename` already moved
129
+ // `extractionRoot` out of it, so this only ever removes the (now-empty, or failure-abandoned)
130
+ // temp directory, never the published package.
131
+ async function installAgentPlugin(required, optional = {}) {
39
132
  const { archive, expectedSha256, archiveReader, layout, workspaceId } = required;
40
133
  const workspaceLayout = layout.forWorkspace({ workspaceId });
41
134
  if (archive.byteLength > LIMITS.maxArchiveBytes) {
@@ -45,12 +138,8 @@ function buildModule(ports) {
45
138
  if (!SHA256_HEX_PATTERN.test(expectedSha256) || digest !== expectedSha256.toLowerCase()) {
46
139
  throw new AgentPluginInstallError({ code: "DIGEST_MISMATCH", message: `archive SHA-256 '${digest}' does not match the expected '${expectedSha256}' — refusing to extract unverified bytes` });
47
140
  }
48
- await mkdir(workspaceLayout.packages, { recursive: true, mode: 0o700 });
49
- const finalRoot = path.join(workspaceLayout.packages, digest);
50
- const alreadyPublished = await isRealDirectory(finalRoot);
51
- if (alreadyPublished) {
52
- return indexInstalledRoot(finalRoot, digest);
53
- }
141
+ await mkdir(workspaceLayout.root, { recursive: true, mode: 0o700 });
142
+ await persistentState.assertOwnedPath({ workspaceRoot: workspaceLayout.root, entryPath: "staging" });
54
143
  await mkdir(workspaceLayout.staging, { recursive: true, mode: 0o700 });
55
144
  await assertEntryCountWithinCap(archiveReader.entries({ archive }));
56
145
  const transactionRoot = await mkdtemp(path.join(workspaceLayout.staging, "install-"));
@@ -59,18 +148,60 @@ function buildModule(ports) {
59
148
  try {
60
149
  const executablePaths = await extractEntries(archiveReader.entries({ archive }), extractionRoot);
61
150
  const indexed = await indexInstalledRoot(extractionRoot, digest);
62
- await publish(extractionRoot, finalRoot);
63
- await freezeTree(finalRoot, executablePaths);
64
- return { ...indexed, packageRoot: finalRoot };
151
+ if (optional.previewOnly)
152
+ return indexed;
153
+ return await persistentState.withPluginLock({ workspaceRoot: workspaceLayout.root, pluginId: indexed.pluginId, run: async () => {
154
+ // Layout B needs the manifest id before choosing the package destination. Dedup still
155
+ // skips publication and freezing, after one bounded staging extraction.
156
+ // Owner decision 2026-10-04: learned facts begin empty for each site; author guidance stays
157
+ // in package docs. Package extension files never seed learned memory or the user's notes.
158
+ // Content-addressed dedup, scoped to THIS workspace's own tree (`workspaceLayout` was just
159
+ // resolved above, atomically, from this same `workspaceId`): identical bytes were already
160
+ // extracted, verified, and frozen by a prior install of this same workspace's — never
161
+ // re-extracted for a different workspace, by construction, since a different workspace's
162
+ // `workspaceLayout.packages` is a different path entirely. one bounded staging extraction precedes this check.
163
+ // Publish BEFORE freezing permissions, not after: `rename()` moves `extractionRoot` as a single
164
+ // directory-entry operation, and on this filesystem renaming an already-read-only (0o555)
165
+ // directory itself fails EACCES (observed, not merely theoretical — see the regression test this
166
+ // ordering fixed). Freezing after publish also closes a smaller race for free: nothing outside
167
+ // this function can observe the package at its live path while it is still writable, because the
168
+ // live path does not exist until `publish` returns.
169
+ const publishPackage = async () => {
170
+ // The manifest identifies the plugin only after bounded staging extraction (Layout B).
171
+ const pluginId = indexed.pluginId;
172
+ const packagesDir = workspaceLayout.pluginPackagesDir({ pluginId });
173
+ await persistentState.assertOwnedPath({ workspaceRoot: workspaceLayout.root, entryPath: path.relative(workspaceLayout.root, packagesDir) });
174
+ await mkdir(packagesDir, { recursive: true, mode: 0o700 });
175
+ const finalRoot = await assertContainedOnDisk(packagesDir, digest);
176
+ for (const directory of [workspaceLayout.pluginDataDir({ pluginId }),
177
+ workspaceLayout.pluginMemoryDir({ pluginId, kind: 'learned' }),
178
+ workspaceLayout.pluginMemoryDir({ pluginId, kind: 'notes' })]) {
179
+ await persistentState.assertOwnedPath({ workspaceRoot: workspaceLayout.root, entryPath: path.relative(workspaceLayout.root, directory) });
180
+ await mkdir(directory, { recursive: true, mode: 0o700 });
181
+ }
182
+ if (await isRealDirectory(finalRoot))
183
+ return indexInstalledRoot(finalRoot, digest);
184
+ await publish(extractionRoot, finalRoot);
185
+ await freezeTree(finalRoot, executablePaths);
186
+ /** Absolute, read-only (frozen) path to the extracted package root. */
187
+ return { ...indexed, packageRoot: finalRoot };
188
+ };
189
+ return optional.publishGuard ? optional.publishGuard({ plugin: indexed, publish: publishPackage }, {}) : publishPackage();
190
+ } });
65
191
  }
66
192
  finally {
67
193
  await rm(transactionRoot, { recursive: true, force: true });
68
194
  }
69
195
  }
196
+ /** The `(target '...')` suffix on the symlink-rejection message, or nothing when the entry carries
197
+ * no `linkTarget` at all — split out purely to keep that `&&`/ternary pair out of
198
+ * {@link extractOneEntry}'s own branch count. */
70
199
  function symlinkRejectionMessage(entry) {
71
200
  const targetSuffix = "linkTarget" in entry && entry.linkTarget ? ` (target '${entry.linkTarget}')` : "";
72
201
  return `archive entry '${entry.entryPath}' has kind '${entry.kind}', which Agent Plugin packages are not permitted to contain${targetSuffix}`;
73
202
  }
203
+ /** `normalizePackageEntryPath()`, with its thrown error re-shaped into the module's own
204
+ * `AgentPluginInstallError` vocabulary. */
74
205
  function normalizeEntryPathOrThrow(entryPath) {
75
206
  try {
76
207
  return normalizePackageEntryPath(entryPath);
@@ -79,6 +210,7 @@ function buildModule(ports) {
79
210
  throw new AgentPluginInstallError({ code: "UNSAFE_ENTRY_PATH", message: `archive entry path is unsafe: '${entryPath}'` }, { cause: error });
80
211
  }
81
212
  }
213
+ /** `assertContainedOnDisk()`, with its thrown error re-shaped the same way. */
82
214
  async function resolveContainedDestinationOrThrow(extractionRoot, normalized) {
83
215
  try {
84
216
  return await assertContainedOnDisk(extractionRoot, normalized);
@@ -89,6 +221,15 @@ function buildModule(ports) {
89
221
  });
90
222
  }
91
223
  }
224
+ /**
225
+ * Applies every hardening rule in this module's header to one archive entry: symlink rejection,
226
+ * path normalization, duplicate detection, containment, the per-file size cap, and (for a file
227
+ * entry) the actual write. Returns the running total-extracted-bytes count, since that is the one
228
+ * piece of `extractEntries`'s loop state a single entry's processing can change.
229
+ */
230
+ // Refused BEFORE path normalization: a symlink entry is never acceptable regardless of how
231
+ // "safe" its own name looks, matching the ecosystem fix this module's header cites — validating
232
+ // where a permitted symlink might point is more attack surface than refusing the entry kind.
92
233
  async function extractOneEntry(args) {
93
234
  const { entry, extractionRoot, seen, executablePaths, totalBytesSoFar } = args;
94
235
  if (entry.kind !== "file" && entry.kind !== "directory") {
@@ -125,6 +266,8 @@ function buildModule(ports) {
125
266
  * its entries from the archive bytes, so a second iteration costs one central-directory walk
126
267
  * and opens no content stream.
127
268
  */
269
+ /** Ported from Jini 78d4b60b: reject from metadata before a staging transaction exists.
270
+ * A second central-directory walk opens no content streams and avoids over-cap disk churn. */
128
271
  async function assertEntryCountWithinCap(entries) {
129
272
  let entryCount = 0;
130
273
  for await (const _entry of entries) {
@@ -136,6 +279,9 @@ function buildModule(ports) {
136
279
  function tooManyEntriesError() {
137
280
  return new AgentPluginInstallError({ code: "TOO_MANY_ENTRIES", message: `archive exceeds the ${LIMITS.maxEntries}-entry cap` });
138
281
  }
282
+ /** Extracts every entry into `extractionRoot`, enforcing every hardening rule in this module's
283
+ * header. Returns the set of archive-relative paths the archive marked executable, for
284
+ * {@link freezeTree} to preserve the `+x` bit on. */
139
285
  async function extractEntries(entries, extractionRoot) {
140
286
  const seen = new Set();
141
287
  const executablePaths = new Set();
@@ -150,6 +296,12 @@ function buildModule(ports) {
150
296
  }
151
297
  return executablePaths;
152
298
  }
299
+ /** Streams one file entry to disk, bounding both its own size and the running archive total against
300
+ * bytes ACTUALLY observed — never the archive format's declared size, which a decompression bomb
301
+ * lies about by construction. `O_EXCL` refuses to write over anything already at `destination`
302
+ * (there cannot legitimately be one — `extractEntries`'s duplicate check already refused a second
303
+ * entry for the same path); `O_NOFOLLOW` refuses to follow a final-component symlink if the
304
+ * filesystem somehow already had one (defense in depth — this module never creates one itself). */
153
305
  async function writeContainedFile(params) {
154
306
  let flags = constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL;
155
307
  if (ports.process.platform !== "win32")
@@ -175,7 +327,36 @@ function buildModule(ports) {
175
327
  }
176
328
  return totalBytes;
177
329
  }
178
- async function indexInstalledRoot(packageRoot, archiveDigest) {
330
+ /** Cosmetic fields are trimmed and capped rather than invalidating the package. The cap stops
331
+ * a hostile package flooding an admin row. Inner newlines remain paragraph breaks in summaries.
332
+ * @complexity O(n) in the supplied text length; output is capped at maxLength. */
333
+ function readDisplayText(required, _optional = {}) {
334
+ const text = readAgentPluginExtension({ manifest: required.manifest, namespace: ports.extensionNamespace,
335
+ read: ({ value }) => {
336
+ const field = value[required.key];
337
+ return typeof field === 'string' ? field.trim() : undefined;
338
+ } });
339
+ return text ? { [required.key]: text.slice(0, required.maxLength) } : {};
340
+ }
341
+ /**
342
+ * Reads the (already-extracted-and-trusted, or already-published) `plugin.json` at `packageRoot`
343
+ * and walks the tree to build the `files`/`skills` index. Shared between a fresh install and the
344
+ * content-addressed dedup fast path so both return an identically-shaped result.
345
+ *
346
+ * Exported (2026-08-21) for a second caller outside this module:
347
+ * `resolve-agent-plugin-refs.ts`'s run-time resolution of a pinned `pluginRefId` against the
348
+ * packages already on disk needs the SAME `plugin.json`-parse-plus-tree-walk this function
349
+ * already does correctly (manifest validation via `parseAgentPluginManifest`, sorted `files`), so
350
+ * it re-derives an `InstalledAgentPlugin` per installed digest rather than hand-rolling a second,
351
+ * less-validated reader.
352
+ */
353
+ // Conditional spreads, not `field: parsed.manifest.field` directly: an `exactOptionalPropertyTypes`-
354
+ // style caller (and this codebase's own convention elsewhere — see `buildToolSource`'s identical
355
+ // `defaultSkillReason` spread in `tool-registrations.ts`) must see an ABSENT key, not a key present
356
+ // with value `undefined`, when the manifest declared none of these — `deepEqual` fixtures across
357
+ // this feature's own tests already assert exact object shape.
358
+ async function indexInstalledRoot(packageRoot, /** SHA-256 of the raw archive bytes — the content-addressing key and the descriptor `revision`
359
+ * a future capability projection pins invocation to (`capability-projection.ts`). */ archiveDigest) {
179
360
  let manifestRaw;
180
361
  try {
181
362
  manifestRaw = await readFile(path.join(packageRoot, "plugin.json"), "utf8");
@@ -211,22 +392,47 @@ function buildModule(ports) {
211
392
  if (match)
212
393
  skills.push({ name: match[1], skillPath: relative });
213
394
  }
395
+ // Anything that is neither a directory nor a regular file (a symlink somehow already present,
396
+ // a device node) is skipped rather than indexed — extraction itself never creates one, so
397
+ // encountering one here would mean the filesystem changed out from under an already-published,
398
+ // supposedly-frozen package, which this function has no business acting on either way.
214
399
  }
215
400
  }
216
401
  await walk(packageRoot, "");
217
402
  return {
218
403
  pluginId: parsed.manifest.name,
219
404
  version: parsed.manifest.version,
220
- ...(parsed.manifest.description !== undefined ? { description: parsed.manifest.description } : {}),
405
+ ...( /**
406
+ * `plugin.json`'s own `description`/`keywords`/`author`/`license` (`manifest.ts`'s
407
+ * `AgentPluginManifest`) — parsed at index time but, before 2026-09-09, discarded immediately
408
+ * after: only `name`/`version` ever reached `InstalledAgentPlugin`. Added for
409
+ * `search_agent_plugin_local` (`tool-registrations.ts`), which needs the description and the
410
+ * author-curated keywords to rank installed plugins against a free-text query — the manifest is
411
+ * already read and parsed here on every call, so carrying these through is zero additional I/O,
412
+ * not a new read path. All four are optional because the spec itself makes them optional (the real
413
+ * installed `ui-ux-design` fixture ships `description` but no `keywords`; a bare-minimum manifest
414
+ * ships neither) — a caller must not assume any of them are present.
415
+ */parsed.manifest.description !== undefined ? { description: parsed.manifest.description } : {}),
221
416
  ...(parsed.manifest.keywords !== undefined ? { keywords: parsed.manifest.keywords } : {}),
222
417
  ...(parsed.manifest.author !== undefined ? { author: parsed.manifest.author } : {}),
223
418
  ...(parsed.manifest.license !== undefined ? { license: parsed.manifest.license } : {}),
419
+ ...readDisplayText({ manifest: parsed.manifest, key: "displayName", maxLength: 64 }),
420
+ ...readDisplayText({ manifest: parsed.manifest, key: "summary", maxLength: 1200 }),
224
421
  archiveDigest,
225
422
  packageRoot,
226
423
  files: files.sort(),
227
424
  skills: skills.sort((a, b) => a.name.localeCompare(b.name)),
228
425
  };
229
426
  }
427
+ /** Makes every file/directory under `root` read-only (executables keep `+x`, per the Agent Plugins
428
+ * spec's own MCP `command` needing to launch a `./`-relative script — not exercised by anything in
429
+ * this slice, since MCP execution is out of scope for v1, but the bit is preserved now rather than
430
+ * silently dropped for whenever it is).
431
+ *
432
+ * Stated plainly rather than implied (`mcp-federation/trust.ts`'s own "what this tier deliberately
433
+ * does NOT claim" discipline): this is best-effort defense against an ACCIDENTAL same-process write
434
+ * into what is supposed to be a read-only `PLUGIN_ROOT`, not a security boundary against a
435
+ * determined attacker already running code as this same OS user — that user can `chmod` again. */
230
436
  async function freezeTree(root, executables, prefix = "") {
231
437
  for (const entry of await readdir(root, { withFileTypes: true })) {
232
438
  const relative = prefix ? `${prefix}/${entry.name}` : entry.name;
@@ -241,6 +447,13 @@ function buildModule(ports) {
241
447
  }
242
448
  await chmod(root, 0o555);
243
449
  }
450
+ /** Publishes `extractionRoot` to `finalRoot` by rename — atomic on the same filesystem, so a reader
451
+ * can never observe a partially-extracted package at the live path. Tolerates losing a race against
452
+ * a concurrent install of the byte-identical digest (the same content, published a moment earlier by
453
+ * another process) rather than treating that as a failure. */
454
+ // macOS reports EACCES, rather than ENOTEMPTY, when the winning install has already
455
+ // frozen its destination directory. Still require a real directory below: a permission
456
+ // failure with no published winner must remain PUBLISH_FAILED.
244
457
  async function publish(extractionRoot, finalRoot) {
245
458
  try {
246
459
  await rename(extractionRoot, finalRoot);
@@ -252,6 +465,8 @@ function buildModule(ports) {
252
465
  if (!(await isRealDirectory(finalRoot))) {
253
466
  throw new AgentPluginInstallError({ code: "PUBLISH_FAILED", message: `'${finalRoot}' exists but is not a real, published package directory` }, { cause: error });
254
467
  }
468
+ // Lost the race to a concurrent install of identical bytes — the bytes at `finalRoot` are the
469
+ // ones this install itself would have written, so this is not a failure.
255
470
  }
256
471
  }
257
472
  async function isRealDirectory(target) {
@@ -2,6 +2,16 @@ export interface AgentPluginWorkspaceLayout {
2
2
  readonly root: string;
3
3
  readonly packages: string;
4
4
  readonly staging: string;
5
+ pluginRootDir(required: {
6
+ readonly pluginId: string;
7
+ }, optional?: Record<string, never>): string;
8
+ pluginPackagesDir(required: {
9
+ readonly pluginId: string;
10
+ }, optional?: Record<string, never>): string;
11
+ pluginMemoryDir(required: {
12
+ readonly pluginId: string;
13
+ readonly kind: 'learned' | 'notes';
14
+ }, optional?: Record<string, never>): string;
5
15
  pluginDataDir(required: {
6
16
  readonly pluginId: string;
7
17
  }): string;
@@ -1,4 +1,5 @@
1
1
  import path from 'node:path';
2
+ import { pluginStatePaths } from '../../core/persistent-state.js';
2
3
  /** Explicit root only; site-root and environment resolution belong to the consumer. */
3
4
  export function createAgentPluginLayout(required) {
4
5
  if (!path.isAbsolute(required.root))
@@ -14,9 +15,16 @@ export function createAgentPluginLayout(required) {
14
15
  root: workspaceRoot,
15
16
  packages: path.join(workspaceRoot, 'packages', 'sha256'),
16
17
  staging: path.join(workspaceRoot, 'staging'),
18
+ pluginRootDir: ({ pluginId }, _optional = {}) => pluginStatePaths({ workspaceRoot, pluginId }).root,
19
+ pluginPackagesDir: ({ pluginId }, _optional = {}) => pluginStatePaths({ workspaceRoot, pluginId }).packages,
20
+ pluginMemoryDir: ({ pluginId, kind }, _optional = {}) => {
21
+ if (kind !== 'learned' && kind !== 'notes')
22
+ throw new Error('Invalid memory kind');
23
+ return pluginStatePaths({ workspaceRoot, pluginId })[kind];
24
+ },
17
25
  pluginDataDir({ pluginId }) {
18
26
  assertSafeSegment(pluginId);
19
- return path.join(workspaceRoot, 'data', pluginId);
27
+ return pluginStatePaths({ workspaceRoot, pluginId }).data;
20
28
  },
21
29
  };
22
30
  },