@jini-ai/plugins 0.0.0-stage → 0.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.
- package/CHANGELOG.md +25 -0
- package/LICENSE +201 -0
- package/README.md +60 -2
- package/dist/glue/attachment-points/content-lifecycle.d.ts +10 -0
- package/dist/glue/attachment-points/content-lifecycle.js +5 -0
- package/dist/glue/attachment-points/events.d.ts +12 -0
- package/dist/glue/attachment-points/events.js +5 -0
- package/dist/glue/attachment-points/tool-registration.d.ts +23 -0
- package/dist/glue/attachment-points/tool-registration.js +43 -0
- package/dist/glue/capability-gate.d.ts +20 -0
- package/dist/glue/capability-gate.js +23 -0
- package/dist/glue/dispatch.d.ts +20 -0
- package/dist/glue/dispatch.js +9 -0
- package/dist/glue/index.d.ts +8 -0
- package/dist/glue/index.js +8 -0
- package/dist/glue/manifest.d.ts +43 -0
- package/dist/glue/manifest.js +106 -0
- package/dist/glue/ports.d.ts +60 -0
- package/dist/glue/ports.js +2 -0
- package/dist/host/activation.d.ts +118 -0
- package/dist/host/activation.js +104 -0
- package/dist/host/attach-loaded-plugin.d.ts +37 -0
- package/dist/host/attach-loaded-plugin.js +19 -0
- package/dist/host/capability-sdk.d.ts +74 -0
- package/dist/host/capability-sdk.js +58 -0
- package/dist/host/claim-conflicts.d.ts +101 -0
- package/dist/host/claim-conflicts.js +139 -0
- package/dist/host/declarative-enable.d.ts +43 -0
- package/dist/host/declarative-enable.js +73 -0
- package/dist/host/discovery-types.d.ts +52 -0
- package/dist/host/discovery-types.js +2 -0
- package/dist/host/errors.d.ts +20 -0
- package/dist/host/errors.js +22 -0
- package/dist/host/hook-registry.d.ts +113 -0
- package/dist/host/hook-registry.js +190 -0
- package/dist/host/index.d.ts +16 -0
- package/dist/host/index.js +16 -0
- package/dist/host/manifest.d.ts +149 -0
- package/dist/host/manifest.js +407 -0
- package/dist/host/node/discovery.d.ts +25 -0
- package/dist/host/node/discovery.js +217 -0
- package/dist/host/node/index.d.ts +7 -0
- package/dist/host/node/index.js +7 -0
- package/dist/host/node/install.d.ts +73 -0
- package/dist/host/node/install.js +301 -0
- package/dist/host/node/loader.d.ts +55 -0
- package/dist/host/node/loader.js +146 -0
- package/dist/host/node/module-snapshot.d.ts +25 -0
- package/dist/host/node/module-snapshot.js +53 -0
- package/dist/host/node/package-files.d.ts +52 -0
- package/dist/host/node/package-files.js +214 -0
- package/dist/host/plugin-claims.d.ts +113 -0
- package/dist/host/plugin-claims.js +134 -0
- package/dist/host/ports.d.ts +196 -0
- package/dist/host/ports.js +2 -0
- package/dist/host/quarantine.d.ts +19 -0
- package/dist/host/quarantine.js +28 -0
- package/dist/host/repo.memory.d.ts +27 -0
- package/dist/host/repo.memory.js +33 -0
- package/dist/host/sql/index.d.ts +4 -0
- package/dist/host/sql/index.js +4 -0
- package/dist/host/sql/repo.d.ts +38 -0
- package/dist/host/sql/repo.js +50 -0
- package/dist/host/sql/repo.rows.d.ts +28 -0
- package/dist/host/sql/repo.rows.js +28 -0
- package/dist/host/uninstall.d.ts +88 -0
- package/dist/host/uninstall.js +112 -0
- package/dist/host/worker/import-seam.d.ts +45 -0
- package/dist/host/worker/import-seam.js +52 -0
- package/dist/host/worker/index.d.ts +5 -0
- package/dist/host/worker/index.js +5 -0
- package/dist/host/worker/protocol.d.ts +62 -0
- package/dist/host/worker/protocol.js +31 -0
- package/dist/host/worker/run-call.d.ts +27 -0
- package/dist/host/worker/run-call.js +76 -0
- package/package.json +102 -3
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export interface PluginPackageFileLimits {
|
|
2
|
+
readonly maxFiles: number;
|
|
3
|
+
/** Files, symlinks, and directories visited — bounds `readdir` calls on a wide, deep tree. */
|
|
4
|
+
readonly maxEntries: number;
|
|
5
|
+
readonly maxFileBytes: number;
|
|
6
|
+
readonly maxTotalBytes: number;
|
|
7
|
+
}
|
|
8
|
+
/** Sized for a plugin package (a manifest, an entry file, a handful of skills or assets), not a
|
|
9
|
+
* vendored dependency tree. Sent to the client with every listing so its notices never hardcode them. */
|
|
10
|
+
export declare const PLUGIN_PACKAGE_FILE_LIMITS: PluginPackageFileLimits;
|
|
11
|
+
export type PluginPackageFileOmission = "binary" | "too-large" | "symlink" | "unreadable";
|
|
12
|
+
export interface PluginPackageFile {
|
|
13
|
+
/** `/`-separated, relative to the plugin's own directory. */
|
|
14
|
+
readonly relativePath: string;
|
|
15
|
+
/** Bytes on disk; `0` for a symlink or a file that could not be opened. */
|
|
16
|
+
readonly sizeBytes: number;
|
|
17
|
+
/** The file's UTF-8 text — `null` exactly when `omitted` is set. */
|
|
18
|
+
readonly content: string | null;
|
|
19
|
+
readonly omitted: PluginPackageFileOmission | null;
|
|
20
|
+
}
|
|
21
|
+
export interface PluginPackageFiles {
|
|
22
|
+
readonly files: readonly PluginPackageFile[];
|
|
23
|
+
/** A file-count, entry, or total-byte cap stopped the walk before every file was listed. */
|
|
24
|
+
readonly truncated: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** `rootDir` is a symlink, not a directory, or resolves outside `containerDir`. The message names
|
|
27
|
+
* server paths — a route must not echo it to a client. */
|
|
28
|
+
export declare class PluginPackagePathError extends Error {
|
|
29
|
+
constructor(message: string);
|
|
30
|
+
}
|
|
31
|
+
export interface ReadPluginPackageFilesRequired {
|
|
32
|
+
readonly input: {
|
|
33
|
+
/** The plugin's own directory. Missing on disk ⇒ an empty listing, not an error. */
|
|
34
|
+
readonly rootDir: string;
|
|
35
|
+
/** The directory `rootDir` must resolve inside (the install dir, or a built-in's own folder). */
|
|
36
|
+
readonly containerDir: string;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export interface ReadPluginPackageFilesOptional {
|
|
40
|
+
readonly limits?: PluginPackageFileLimits;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Lists a plugin package's files, breadth-first — so a package's top-level manifest is always listed
|
|
44
|
+
* before a deep subtree can use up the caps.
|
|
45
|
+
*
|
|
46
|
+
* @throws {PluginPackagePathError} `input.rootDir` is a symlink or not a directory, or resolves
|
|
47
|
+
* outside `input.containerDir`.
|
|
48
|
+
* @complexity O(min(entries, maxEntries)) directory entries plus O(maxTotalBytes) bytes read; memory
|
|
49
|
+
* is bounded by the same two caps.
|
|
50
|
+
*/
|
|
51
|
+
export declare function readPluginPackageFiles(required: ReadPluginPackageFilesRequired, optional?: ReadPluginPackageFilesOptional): Promise<PluginPackageFiles>;
|
|
52
|
+
//# sourceMappingURL=package-files.d.ts.map
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file `readPluginPackageFiles()` — the read-only, bounded file listing behind the admin Plugins
|
|
3
|
+
* screen's package-files viewer (`PLUGIN_FILES`, `routes/plugins/files.ts`, 2026-09-13).
|
|
4
|
+
*
|
|
5
|
+
* Purpose:
|
|
6
|
+
* Walks ONE plugin's own directory and returns each regular file's UTF-8 text. Mechanism only:
|
|
7
|
+
* which directory belongs to which plugin is the composition root's decision
|
|
8
|
+
* (`server/runtime/composition/plugin-runtime.ts`'s `readPluginPackageFiles` binding), and who may
|
|
9
|
+
* call it is the route's.
|
|
10
|
+
*
|
|
11
|
+
* Safety — each guard is independent, not presumed covered by another:
|
|
12
|
+
* - `rootDir` itself must be a plain directory (not a symlink), and its realpath must be
|
|
13
|
+
* `containerDir`'s realpath or a descendant of it. Otherwise `PluginPackagePathError`.
|
|
14
|
+
* - The walk reads `readdir` `Dirent` types, which never follow symlinks: a symlink entry is
|
|
15
|
+
* listed with `omitted: "symlink"` and is neither descended into nor read.
|
|
16
|
+
* - Every file is opened with `O_NOFOLLOW`, so a file swapped for a symlink between listing and
|
|
17
|
+
* open fails (`ELOOP`) and is reported as `"symlink"` instead of reading the link's target.
|
|
18
|
+
* - Caps on files listed, entries visited, bytes per file, and bytes in total. A count or total cap
|
|
19
|
+
* stops the walk and sets `truncated`; one oversized file is listed as `"too-large"`.
|
|
20
|
+
* - A file containing a NUL byte or invalid UTF-8 is listed as `"binary"`, with no content.
|
|
21
|
+
*
|
|
22
|
+
* Residual risk (disclosed, not closed): `O_NOFOLLOW` guards only a path's final component, so a
|
|
23
|
+
* directory swapped for a symlink mid-walk could still be descended. Doing that needs write access
|
|
24
|
+
* to the plugin install directory, which for a tier-3 plugin already means code execution on
|
|
25
|
+
* enable — this viewer is defense in depth, not the boundary.
|
|
26
|
+
*/
|
|
27
|
+
import { constants } from "node:fs";
|
|
28
|
+
import { lstat, open, readdir, realpath } from "node:fs/promises";
|
|
29
|
+
import path from "node:path";
|
|
30
|
+
/** Sized for a plugin package (a manifest, an entry file, a handful of skills or assets), not a
|
|
31
|
+
* vendored dependency tree. Sent to the client with every listing so its notices never hardcode them. */
|
|
32
|
+
export const PLUGIN_PACKAGE_FILE_LIMITS = {
|
|
33
|
+
maxFiles: 200,
|
|
34
|
+
maxEntries: 2000,
|
|
35
|
+
maxFileBytes: 512 * 1024,
|
|
36
|
+
maxTotalBytes: 4 * 1024 * 1024,
|
|
37
|
+
};
|
|
38
|
+
/** `rootDir` is a symlink, not a directory, or resolves outside `containerDir`. The message names
|
|
39
|
+
* server paths — a route must not echo it to a client. */
|
|
40
|
+
export class PluginPackagePathError extends Error {
|
|
41
|
+
constructor(message) {
|
|
42
|
+
super(message);
|
|
43
|
+
this.name = "PluginPackagePathError";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const STRICT_UTF8 = new TextDecoder("utf-8", { fatal: true });
|
|
47
|
+
function errnoCode(error) {
|
|
48
|
+
return error?.code;
|
|
49
|
+
}
|
|
50
|
+
function omittedFile(relativePath, sizeBytes, omitted) {
|
|
51
|
+
return { relativePath, sizeBytes, content: null, omitted };
|
|
52
|
+
}
|
|
53
|
+
/** Code-unit order, so the listing is identical whatever the server's locale. */
|
|
54
|
+
function byName(a, b) {
|
|
55
|
+
if (a.name === b.name)
|
|
56
|
+
return 0;
|
|
57
|
+
return a.name < b.name ? -1 : 1;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @returns `rootDir`'s realpath, or `null` when it does not exist.
|
|
61
|
+
* @throws {PluginPackagePathError} `rootDir` is a symlink or not a directory, or resolves outside `containerDir`.
|
|
62
|
+
*/
|
|
63
|
+
async function resolvePackageRoot(rootDir, containerDir) {
|
|
64
|
+
let stats;
|
|
65
|
+
try {
|
|
66
|
+
stats = await lstat(rootDir);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (errnoCode(error) === "ENOENT")
|
|
70
|
+
return null;
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
if (stats.isSymbolicLink() || !stats.isDirectory()) {
|
|
74
|
+
throw new PluginPackagePathError(`'${rootDir}' is not a plain directory`);
|
|
75
|
+
}
|
|
76
|
+
const [rootReal, containerReal] = await Promise.all([realpath(rootDir), realpath(containerDir)]);
|
|
77
|
+
if (rootReal !== containerReal && !rootReal.startsWith(containerReal + path.sep)) {
|
|
78
|
+
throw new PluginPackagePathError(`'${rootDir}' resolves outside '${containerDir}'`);
|
|
79
|
+
}
|
|
80
|
+
return rootReal;
|
|
81
|
+
}
|
|
82
|
+
/** Counts one entry against the caps. `false` (and `truncated` set) once a cap is already full. */
|
|
83
|
+
function admitEntry(state) {
|
|
84
|
+
if (state.truncated)
|
|
85
|
+
return false;
|
|
86
|
+
if (state.files.length >= state.limits.maxFiles || state.entries >= state.limits.maxEntries) {
|
|
87
|
+
state.truncated = true;
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
state.entries += 1;
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
async function openNoFollow(absolutePath) {
|
|
94
|
+
try {
|
|
95
|
+
// `O_NOFOLLOW` is undefined on Windows; `?? 0` keeps the open valid there.
|
|
96
|
+
return await open(absolutePath, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
const code = errnoCode(error);
|
|
100
|
+
return code === "ELOOP" || code === "EMLINK" ? "symlink" : "unreadable";
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** Reads up to `maxBytes` from the start of the file. */
|
|
104
|
+
async function readAtMost(handle, maxBytes) {
|
|
105
|
+
const buffer = Buffer.alloc(maxBytes);
|
|
106
|
+
let offset = 0;
|
|
107
|
+
while (offset < maxBytes) {
|
|
108
|
+
const { bytesRead } = await handle.read(buffer, offset, maxBytes - offset, offset);
|
|
109
|
+
if (bytesRead === 0)
|
|
110
|
+
break;
|
|
111
|
+
offset += bytesRead;
|
|
112
|
+
}
|
|
113
|
+
return buffer.subarray(0, offset);
|
|
114
|
+
}
|
|
115
|
+
function toTextOrBinary(relativePath, bytes) {
|
|
116
|
+
if (bytes.includes(0))
|
|
117
|
+
return omittedFile(relativePath, bytes.length, "binary");
|
|
118
|
+
try {
|
|
119
|
+
return { relativePath, sizeBytes: bytes.length, content: STRICT_UTF8.decode(bytes), omitted: null };
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return omittedFile(relativePath, bytes.length, "binary");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Records one regular file. Reads at most `min(size, maxFileBytes) + 1` bytes, so a file that grew
|
|
127
|
+
* past the cap after `stat` is still caught as too large rather than read whole.
|
|
128
|
+
*/
|
|
129
|
+
async function readFileEntry(absolutePath, relativePath, state) {
|
|
130
|
+
const opened = await openNoFollow(absolutePath);
|
|
131
|
+
if (typeof opened === "string") {
|
|
132
|
+
state.files.push(omittedFile(relativePath, 0, opened));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
const stats = await opened.stat();
|
|
137
|
+
if (!stats.isFile())
|
|
138
|
+
return;
|
|
139
|
+
const { maxFileBytes, maxTotalBytes } = state.limits;
|
|
140
|
+
if (stats.size > maxFileBytes) {
|
|
141
|
+
state.files.push(omittedFile(relativePath, stats.size, "too-large"));
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (state.totalBytes + stats.size > maxTotalBytes) {
|
|
145
|
+
state.truncated = true;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const bytes = await readAtMost(opened, Math.min(stats.size, maxFileBytes) + 1);
|
|
149
|
+
if (bytes.length > maxFileBytes) {
|
|
150
|
+
state.files.push(omittedFile(relativePath, bytes.length, "too-large"));
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
state.totalBytes += bytes.length;
|
|
154
|
+
state.files.push(toTextOrBinary(relativePath, bytes));
|
|
155
|
+
}
|
|
156
|
+
finally {
|
|
157
|
+
await opened.close();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/** Lists one directory level: symlinks and files are recorded now in name order; subdirectories are
|
|
161
|
+
* returned for the next level. An unreadable subdirectory lists as empty rather than failing the
|
|
162
|
+
* whole package. */
|
|
163
|
+
async function readDirectoryLevel(rootReal, relativeDir, state) {
|
|
164
|
+
let entries;
|
|
165
|
+
try {
|
|
166
|
+
entries = await readdir(path.join(rootReal, relativeDir), { withFileTypes: true });
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return [];
|
|
170
|
+
}
|
|
171
|
+
const subdirectories = [];
|
|
172
|
+
for (const entry of entries.sort(byName)) {
|
|
173
|
+
if (!admitEntry(state))
|
|
174
|
+
break;
|
|
175
|
+
const relativePath = relativeDir === "" ? entry.name : `${relativeDir}/${entry.name}`;
|
|
176
|
+
if (entry.isDirectory())
|
|
177
|
+
subdirectories.push(relativePath);
|
|
178
|
+
else if (entry.isSymbolicLink())
|
|
179
|
+
state.files.push(omittedFile(relativePath, 0, "symlink"));
|
|
180
|
+
else if (entry.isFile())
|
|
181
|
+
await readFileEntry(path.join(rootReal, relativePath), relativePath, state);
|
|
182
|
+
if (state.truncated)
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
return subdirectories;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Lists a plugin package's files, breadth-first — so a package's top-level manifest is always listed
|
|
189
|
+
* before a deep subtree can use up the caps.
|
|
190
|
+
*
|
|
191
|
+
* @throws {PluginPackagePathError} `input.rootDir` is a symlink or not a directory, or resolves
|
|
192
|
+
* outside `input.containerDir`.
|
|
193
|
+
* @complexity O(min(entries, maxEntries)) directory entries plus O(maxTotalBytes) bytes read; memory
|
|
194
|
+
* is bounded by the same two caps.
|
|
195
|
+
*/
|
|
196
|
+
export async function readPluginPackageFiles(required, optional = {}) {
|
|
197
|
+
const { rootDir, containerDir } = required.input;
|
|
198
|
+
const rootReal = await resolvePackageRoot(rootDir, containerDir);
|
|
199
|
+
if (rootReal === null)
|
|
200
|
+
return { files: [], truncated: false };
|
|
201
|
+
const state = {
|
|
202
|
+
limits: optional.limits ?? PLUGIN_PACKAGE_FILE_LIMITS,
|
|
203
|
+
files: [],
|
|
204
|
+
entries: 0,
|
|
205
|
+
totalBytes: 0,
|
|
206
|
+
truncated: false,
|
|
207
|
+
};
|
|
208
|
+
const pending = [""];
|
|
209
|
+
while (pending.length > 0 && !state.truncated) {
|
|
210
|
+
pending.push(...(await readDirectoryLevel(rootReal, pending.shift(), state)));
|
|
211
|
+
}
|
|
212
|
+
return { files: state.files, truncated: state.truncated };
|
|
213
|
+
}
|
|
214
|
+
//# sourceMappingURL=package-files.js.map
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Tovu's plugin conflict rules on top of the neutral detector (`claim-conflicts.ts`):
|
|
3
|
+
* what a `tovu.plugin.json` claims, who keeps a contested name, and the sentence an operator or an
|
|
4
|
+
* agent reads when a plugin is refused.
|
|
5
|
+
*
|
|
6
|
+
* Spec note: `ADS-memory/specs/005-plugin-system/conflicts.spec.md`.
|
|
7
|
+
*
|
|
8
|
+
* ## What a plugin claims
|
|
9
|
+
* - every `contributes` entry (routes, tools, tables, settings, widgets, permissions) — exclusive;
|
|
10
|
+
* - every hook it attaches, with the HOST's semantics for that hook ({@link PLUGIN_HOOK_SEMANTICS}):
|
|
11
|
+
* `content.entry.beforeSave` is a shared pipeline — every enabled filter runs in TB-01 order and
|
|
12
|
+
* writes only its own `ext.{id}` namespace (BR-04), so two plugins on it is the design, not a
|
|
13
|
+
* clash. A hook this table does not know is treated as EXCLUSIVE: a newly added hook point that
|
|
14
|
+
* nobody classified must fail safe (refuse the second plugin) rather than silently stack;
|
|
15
|
+
* - its generated capability tool (`plugin_capability_<id>`) when it declares `fields`, because
|
|
16
|
+
* that tool is registered in the same agent tool registry as everything else;
|
|
17
|
+
* - every content type it declares (`contentTypes[].key`, kind `content-type`, AW-7 Tier 1) — one
|
|
18
|
+
* key is one Collection, so the second plugin to declare `faq` is refused here instead of
|
|
19
|
+
* silently sharing (or schema-clashing on) the first one's type.
|
|
20
|
+
* Field paths are NOT claims: `validateManifest` already forces them under `ext.{id}.`, and ids are
|
|
21
|
+
* unique (ID_DUPLICATE / SHADOWS_BUILT_IN), so two plugins can never name the same field.
|
|
22
|
+
*
|
|
23
|
+
* ## Who wins
|
|
24
|
+
* Core first, always. Then plugins in the order they were turned on (activation `updatedAt`
|
|
25
|
+
* ascending; built-in before site, then id, only to break exact ties), so the plugin that was
|
|
26
|
+
* already working keeps working and the NEWER one is refused. At enable time the plugin being
|
|
27
|
+
* turned on is placed last regardless of timestamps — it is by definition the newer one, even
|
|
28
|
+
* when a coarse or fixed clock gives it the same `updatedAt` as an earlier plugin.
|
|
29
|
+
*
|
|
30
|
+
* ## Where it runs (composition: `server/runtime/composition/plugin-runtime.ts`)
|
|
31
|
+
* - enable (admin route, agent tool, activation poll): `onPluginEnabled` throws
|
|
32
|
+
* {@link PluginConflictError} before any plugin code is imported; `setPluginEnabled` then restores
|
|
33
|
+
* the prior activation row, so a refused enable leaves nothing half-on;
|
|
34
|
+
* - boot: `attachEnabledPluginsAtBoot` resolves every enabled plugin together and QUARANTINES the
|
|
35
|
+
* losers through `quarantine.ts` (the same durable "disabled + reason" record hook failures use);
|
|
36
|
+
* - list (admin screen, `plugins_list`): `listPluginConflicts` reports enabled losers and the clash a
|
|
37
|
+
* disabled plugin WOULD hit if turned on now, so the operator sees it before clicking.
|
|
38
|
+
*/
|
|
39
|
+
import type { ManifestHostPorts } from "./ports.js";
|
|
40
|
+
import type { PluginActivationRecord } from "./activation.js";
|
|
41
|
+
import { type ClaimMode, type ExtensionClaim } from "./claim-conflicts.js";
|
|
42
|
+
import type { PluginDiscoveryRecord } from "./discovery-types.js";
|
|
43
|
+
import { CORE_OWNER_ID, type PluginManifest } from "./manifest.js";
|
|
44
|
+
/** The owner id core's claims are filed under, and how messages name it. */
|
|
45
|
+
export { CORE_OWNER_ID };
|
|
46
|
+
/**
|
|
47
|
+
* `plugin_capability_<pluginId>` — the agent tool `capability-tool-registrations.ts` generates for a
|
|
48
|
+
* plugin that declares `fields` (hyphens folded to underscores, matching `agent_plugin_<pluginId>`'s
|
|
49
|
+
* own scheme). Moved here from that file (2026-10-04) so the tool's registration and the claim it
|
|
50
|
+
* makes for conflict detection derive the id from ONE function and cannot drift apart.
|
|
51
|
+
*/
|
|
52
|
+
export declare function capabilityToolIdFor({ pluginId }: {
|
|
53
|
+
pluginId: string;
|
|
54
|
+
}, _optional?: Record<string, never>): string;
|
|
55
|
+
/** One conflict, as the admin screen and the agent see it — the detector's conflict plus the
|
|
56
|
+
* holder's display name. */
|
|
57
|
+
export interface PluginConflict {
|
|
58
|
+
readonly kind: string;
|
|
59
|
+
readonly key: string;
|
|
60
|
+
readonly heldBy: string;
|
|
61
|
+
readonly heldByName: string;
|
|
62
|
+
readonly heldKey: string;
|
|
63
|
+
}
|
|
64
|
+
/** Every claim one manifest makes (see this file's header for the four sources). */
|
|
65
|
+
export declare function pluginClaimsFromManifest(required: {
|
|
66
|
+
readonly manifest: PluginManifest;
|
|
67
|
+
} & Pick<ManifestHostPorts, "pluginSdkBinding" | "declarativeContentTypes">, optional?: {
|
|
68
|
+
readonly hookSemantics?: Readonly<Record<string, ClaimMode>>;
|
|
69
|
+
}): ExtensionClaim[];
|
|
70
|
+
export interface ResolvePluginConflictsRequired extends Pick<ManifestHostPorts, "pluginSdkBinding" | "declarativeContentTypes" | "coreOwnerName"> {
|
|
71
|
+
readonly workspaceId: string;
|
|
72
|
+
readonly discovery: readonly PluginDiscoveryRecord[];
|
|
73
|
+
readonly activations: readonly PluginActivationRecord[];
|
|
74
|
+
readonly coreClaims: readonly ExtensionClaim[];
|
|
75
|
+
}
|
|
76
|
+
export interface ResolvePluginConflictsOptional {
|
|
77
|
+
/** The plugin being turned on right now — ordered after every other enabled plugin. */
|
|
78
|
+
readonly candidateId?: string;
|
|
79
|
+
readonly hookSemantics?: Readonly<Record<string, ClaimMode>>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Every plugin in `discovery` that has a conflict: an ENABLED plugin that loses to core or an
|
|
83
|
+
* earlier-enabled plugin, and a valid plugin that is OFF but would lose if turned on now.
|
|
84
|
+
*
|
|
85
|
+
* @complexity O(P · S²) — one detector pass for the enabled set plus one per disabled plugin, each
|
|
86
|
+
* over a site's handful of plugins.
|
|
87
|
+
*/
|
|
88
|
+
export declare function resolvePluginConflicts(required: ResolvePluginConflictsRequired, optional?: ResolvePluginConflictsOptional): ReadonlyMap<string, readonly PluginConflict[]>;
|
|
89
|
+
/** Every conflict in words, `; `-joined — shared by the enable refusal and the boot quarantine reason. */
|
|
90
|
+
export declare function describeConflictList({ conflicts }: {
|
|
91
|
+
conflicts: readonly PluginConflict[];
|
|
92
|
+
}, _optional?: Record<string, never>): string;
|
|
93
|
+
/** The operator- and agent-facing sentence for a refused plugin: what clashes, who holds it, what to do. */
|
|
94
|
+
export declare function describePluginConflicts(required: {
|
|
95
|
+
readonly pluginId: string;
|
|
96
|
+
readonly conflicts: readonly PluginConflict[];
|
|
97
|
+
}, _optional?: Record<string, never>): string;
|
|
98
|
+
/** The durable quarantine reason when boot turns a plugin off for a conflict (shown on the admin row). */
|
|
99
|
+
export declare function describeBootConflictQuarantine({ conflicts }: {
|
|
100
|
+
conflicts: readonly PluginConflict[];
|
|
101
|
+
}, _optional?: Record<string, never>): string;
|
|
102
|
+
/** Thrown by the enable path when a plugin would take a name core or an enabled plugin holds.
|
|
103
|
+
* Its message is built only from plugin ids/names and the manifest's own declared names, so it is
|
|
104
|
+
* safe to show an agent verbatim (see `tool-registrations.ts`'s model-facing rule). */
|
|
105
|
+
export declare class PluginConflictError extends Error {
|
|
106
|
+
readonly pluginId: string;
|
|
107
|
+
readonly conflicts: readonly PluginConflict[];
|
|
108
|
+
constructor(required: {
|
|
109
|
+
readonly pluginId: string;
|
|
110
|
+
readonly conflicts: readonly PluginConflict[];
|
|
111
|
+
}, _optional?: Record<string, never>);
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=plugin-claims.d.ts.map
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { resolveClaimConflicts } from "./claim-conflicts.js";
|
|
2
|
+
import { CORE_OWNER_ID } from "./manifest.js";
|
|
3
|
+
/** The owner id core's claims are filed under, and how messages name it. */
|
|
4
|
+
export { CORE_OWNER_ID };
|
|
5
|
+
/** How each host hook point arbitrates several plugins (see this file's header). */
|
|
6
|
+
// Hook semantics are supplied by PluginSdkBinding; product hook names remain host-owned.
|
|
7
|
+
/** `contributes` list -> claim kind. */
|
|
8
|
+
const CONTRIBUTION_KINDS = [
|
|
9
|
+
["routes", "route"],
|
|
10
|
+
["tools", "tool"],
|
|
11
|
+
["tables", "table"],
|
|
12
|
+
["settings", "setting"],
|
|
13
|
+
["widgets", "widget"],
|
|
14
|
+
["permissions", "permission"],
|
|
15
|
+
];
|
|
16
|
+
/**
|
|
17
|
+
* `plugin_capability_<pluginId>` — the agent tool `capability-tool-registrations.ts` generates for a
|
|
18
|
+
* plugin that declares `fields` (hyphens folded to underscores, matching `agent_plugin_<pluginId>`'s
|
|
19
|
+
* own scheme). Moved here from that file (2026-10-04) so the tool's registration and the claim it
|
|
20
|
+
* makes for conflict detection derive the id from ONE function and cannot drift apart.
|
|
21
|
+
*/
|
|
22
|
+
export function capabilityToolIdFor({ pluginId }, _optional = {}) {
|
|
23
|
+
return `plugin_capability_${pluginId.replace(/-/g, "_")}`;
|
|
24
|
+
}
|
|
25
|
+
/** Every claim one manifest makes (see this file's header for the four sources). */
|
|
26
|
+
export function pluginClaimsFromManifest(required, optional = {}) {
|
|
27
|
+
const { manifest } = required;
|
|
28
|
+
const hookSemantics = optional.hookSemantics ?? required.pluginSdkBinding.hookSemantics;
|
|
29
|
+
const contributes = manifest.contributes ?? {};
|
|
30
|
+
const declared = CONTRIBUTION_KINDS.flatMap(([list, kind]) => (contributes[list] ?? []).map((key) => ({ kind, key, mode: "exclusive" })));
|
|
31
|
+
const hooks = manifest.hooks.map((key) => ({ kind: "hook", key, mode: hookSemantics[key] ?? "exclusive" }));
|
|
32
|
+
const generated = manifest.fields.length > 0 ? [{ kind: "tool", key: capabilityToolIdFor({ pluginId: manifest.id }), mode: "exclusive" }] : [];
|
|
33
|
+
const contentTypes = required.declarativeContentTypes.parse({ value: manifest.contentTypes }).decls.map((decl) => ({ kind: "content-type", key: decl.key, mode: "exclusive" }));
|
|
34
|
+
return [...declared, ...hooks, ...generated, ...contentTypes];
|
|
35
|
+
}
|
|
36
|
+
const SOURCE_RANK = { "built-in": 0, site: 1 };
|
|
37
|
+
/** Earliest-enabled first; `candidateId` always last (see this file's header, "Who wins"). */
|
|
38
|
+
function comparePrecedence(candidateId) {
|
|
39
|
+
return (a, b) => {
|
|
40
|
+
const candidateOrder = Number(a.record.id === candidateId) - Number(b.record.id === candidateId);
|
|
41
|
+
if (candidateOrder !== 0)
|
|
42
|
+
return candidateOrder;
|
|
43
|
+
const timeOrder = a.activation.updatedAt.localeCompare(b.activation.updatedAt);
|
|
44
|
+
if (timeOrder !== 0)
|
|
45
|
+
return timeOrder;
|
|
46
|
+
const sourceOrder = SOURCE_RANK[a.record.source] - SOURCE_RANK[b.record.source];
|
|
47
|
+
return sourceOrder !== 0 ? sourceOrder : a.record.id.localeCompare(b.record.id);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function hasManifest(record) {
|
|
51
|
+
return record.status === "valid" && record.manifest !== undefined;
|
|
52
|
+
}
|
|
53
|
+
function toPluginConflicts(conflicts, names) {
|
|
54
|
+
return conflicts.map((conflict) => ({
|
|
55
|
+
kind: conflict.kind,
|
|
56
|
+
key: conflict.key,
|
|
57
|
+
heldBy: conflict.heldBy,
|
|
58
|
+
heldByName: names.get(conflict.heldBy) ?? conflict.heldBy,
|
|
59
|
+
heldKey: conflict.heldKey,
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Every plugin in `discovery` that has a conflict: an ENABLED plugin that loses to core or an
|
|
64
|
+
* earlier-enabled plugin, and a valid plugin that is OFF but would lose if turned on now.
|
|
65
|
+
*
|
|
66
|
+
* @complexity O(P · S²) — one detector pass for the enabled set plus one per disabled plugin, each
|
|
67
|
+
* over a site's handful of plugins.
|
|
68
|
+
*/
|
|
69
|
+
export function resolvePluginConflicts(required, optional = {}) {
|
|
70
|
+
const { workspaceId, discovery, activations, coreClaims } = required;
|
|
71
|
+
const claimsOf = (record) => ({
|
|
72
|
+
id: record.id,
|
|
73
|
+
claims: pluginClaimsFromManifest({ ...required, manifest: record.manifest }, optional.hookSemantics ? { hookSemantics: optional.hookSemantics } : {}),
|
|
74
|
+
});
|
|
75
|
+
const here = new Map(activations.filter((row) => row.workspaceId === workspaceId).map((row) => [row.pluginId, row]));
|
|
76
|
+
const valid = discovery.filter(hasManifest);
|
|
77
|
+
const enabled = valid
|
|
78
|
+
.flatMap((record) => {
|
|
79
|
+
const activation = here.get(record.id);
|
|
80
|
+
return activation?.enabled ? [{ record, activation }] : [];
|
|
81
|
+
})
|
|
82
|
+
.sort(comparePrecedence(optional.candidateId));
|
|
83
|
+
const names = new Map([[CORE_OWNER_ID, required.coreOwnerName], ...valid.map((record) => [record.id, record.name])]);
|
|
84
|
+
const core = { id: CORE_OWNER_ID, claims: coreClaims };
|
|
85
|
+
const live = resolveClaimConflicts({ owners: [core, ...enabled.map((each) => claimsOf(each.record))] });
|
|
86
|
+
const result = new Map();
|
|
87
|
+
for (const [ownerId, conflicts] of live.refused)
|
|
88
|
+
result.set(ownerId, toPluginConflicts(conflicts, names));
|
|
89
|
+
const accepted = new Set(live.accepted);
|
|
90
|
+
const holders = [core, ...enabled.filter((each) => accepted.has(each.record.id)).map((each) => claimsOf(each.record))];
|
|
91
|
+
for (const record of valid) {
|
|
92
|
+
if (here.get(record.id)?.enabled)
|
|
93
|
+
continue;
|
|
94
|
+
const wouldBe = resolveClaimConflicts({ owners: [...holders, claimsOf(record)] }).refused.get(record.id);
|
|
95
|
+
if (wouldBe)
|
|
96
|
+
result.set(record.id, toPluginConflicts(wouldBe, names));
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
/** One conflict, in words: `tool 'x' is provided by plugin 'a' (A)` / `... is reserved by Tovu core ('/api/*')`. */
|
|
101
|
+
function describeOne(conflict) {
|
|
102
|
+
const holder = conflict.heldBy === CORE_OWNER_ID
|
|
103
|
+
? `is reserved by ${conflict.heldByName}${conflict.heldKey === conflict.key ? "" : ` ('${conflict.heldKey}')`}`
|
|
104
|
+
: `is provided by plugin '${conflict.heldBy}' (${conflict.heldByName})`;
|
|
105
|
+
return `${conflict.kind} '${conflict.key}' ${holder}`;
|
|
106
|
+
}
|
|
107
|
+
/** Every conflict in words, `; `-joined — shared by the enable refusal and the boot quarantine reason. */
|
|
108
|
+
export function describeConflictList({ conflicts }, _optional = {}) {
|
|
109
|
+
return conflicts.map(describeOne).join("; ");
|
|
110
|
+
}
|
|
111
|
+
/** The operator- and agent-facing sentence for a refused plugin: what clashes, who holds it, what to do. */
|
|
112
|
+
export function describePluginConflicts(required, _optional = {}) {
|
|
113
|
+
const { pluginId, conflicts } = required;
|
|
114
|
+
return (`Plugin '${pluginId}' was not turned on because it claims things already in use: ` +
|
|
115
|
+
`${describeConflictList({ conflicts })}. Turn off the other plugin first, or keep '${pluginId}' off.`);
|
|
116
|
+
}
|
|
117
|
+
/** The durable quarantine reason when boot turns a plugin off for a conflict (shown on the admin row). */
|
|
118
|
+
export function describeBootConflictQuarantine({ conflicts }, _optional = {}) {
|
|
119
|
+
return `Turned off at startup because it claims things already in use: ${describeConflictList({ conflicts })}. Turn off the other plugin, then turn this one back on.`;
|
|
120
|
+
}
|
|
121
|
+
/** Thrown by the enable path when a plugin would take a name core or an enabled plugin holds.
|
|
122
|
+
* Its message is built only from plugin ids/names and the manifest's own declared names, so it is
|
|
123
|
+
* safe to show an agent verbatim (see `tool-registrations.ts`'s model-facing rule). */
|
|
124
|
+
export class PluginConflictError extends Error {
|
|
125
|
+
pluginId;
|
|
126
|
+
conflicts;
|
|
127
|
+
constructor(required, _optional = {}) {
|
|
128
|
+
super(describePluginConflicts(required));
|
|
129
|
+
this.name = "PluginConflictError";
|
|
130
|
+
this.pluginId = required.pluginId;
|
|
131
|
+
this.conflicts = required.conflicts;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=plugin-claims.js.map
|