@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,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file `resolveClaimConflicts()` — product-neutral detection of two extensions claiming the same
|
|
3
|
+
* named thing (a route, a tool name, a table, a setting id, a widget id, a permission id, an
|
|
4
|
+
* exclusive hook).
|
|
5
|
+
*
|
|
6
|
+
* JINI CANDIDATE: nothing in this file knows about Tovu, its manifest, its plugins or its storage —
|
|
7
|
+
* it takes owners and their claims as plain data and returns who keeps what. It belongs in
|
|
8
|
+
* `@jini-ai/plugins` (or `@jini-ai/core`) beside the glue capability gates. Not moved yet because
|
|
9
|
+
* Tovu's deploy build resolves `@jini-ai/*` from the published registry, so a Jini-side copy would be
|
|
10
|
+
* unreachable from production until the owner publishes; moving it is a file move plus one import.
|
|
11
|
+
*
|
|
12
|
+
* ## The rules (also in `ADS-memory/specs/005-plugin-system/conflicts.spec.md`)
|
|
13
|
+
*
|
|
14
|
+
* 1. Owners are processed in PRECEDENCE order — the caller's order, first wins. Core goes first;
|
|
15
|
+
* after that the caller decides (Tovu: earliest-enabled first, so the NEWER plugin is the one
|
|
16
|
+
* refused — the plugin that was already working keeps working).
|
|
17
|
+
* 2. A claim is `exclusive` (only one owner may hold it) or `shared` (any number of owners may hold
|
|
18
|
+
* it together — a filter pipeline hook where every filter runs in a fixed order is the example).
|
|
19
|
+
* An exclusive claim conflicts with ANY other owner's claim on the same slot, shared or not; two
|
|
20
|
+
* shared claims never conflict.
|
|
21
|
+
* 3. A REFUSED owner contributes nothing: if A holds `x`, B is refused for `x`, and C claims B's
|
|
22
|
+
* other key `y`, C is accepted. Refusal is all-or-nothing per owner — half a plugin is not a
|
|
23
|
+
* state anyone can reason about, so an owner with any conflict keeps none of its claims.
|
|
24
|
+
* 4. One owner claiming the same slot twice is not a conflict (a manifest repeating itself is a
|
|
25
|
+
* validation concern, not a collision between two parties).
|
|
26
|
+
* 5. Keys are compared after the kind's normalization (see {@link DEFAULT_CLAIM_KIND_RULES}):
|
|
27
|
+
* identifiers are case-insensitive; a route is `METHOD /path` with parameter names erased
|
|
28
|
+
* (`/a/:id` and `/a/{slug}` are the same route) and `*`/`ANY` meaning every method.
|
|
29
|
+
* 6. A key ending in `*` is a PREFIX claim: it holds every key that starts with what precedes the
|
|
30
|
+
* `*`. Core uses this to reserve whole namespaces (`/api/*`, `admin.*`) without listing every
|
|
31
|
+
* name in them; an exact claim inside a held prefix conflicts, and so does a prefix claim that
|
|
32
|
+
* covers a held exact key or overlaps a held prefix.
|
|
33
|
+
*
|
|
34
|
+
* Pure: no I/O, no clock, no mutation of its input.
|
|
35
|
+
*/
|
|
36
|
+
const HTTP_METHODS = ["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"];
|
|
37
|
+
const PREFIX_MARKER = "*";
|
|
38
|
+
/** Trimmed, lower-cased — tool names, table names, setting/widget/permission ids. SQLite table
|
|
39
|
+
* names are case-insensitive and the rest are human-typed ids where `Foo` and `foo` side by side
|
|
40
|
+
* would only ever be a mistake, so case never distinguishes two claims. */
|
|
41
|
+
export function normalizeIdentifier({ key }, _optional = {}) {
|
|
42
|
+
return [key.trim().toLowerCase()];
|
|
43
|
+
}
|
|
44
|
+
/** One path: lower-cased, slashes collapsed, trailing slash dropped, every parameter segment
|
|
45
|
+
* (`:id`, `{id}`, `[id]`) erased to `:` so two routes that differ only by parameter NAME — which
|
|
46
|
+
* a router cannot tell apart — compare equal. A trailing `*` survives as the prefix marker. */
|
|
47
|
+
function normalizePath(path) {
|
|
48
|
+
const segments = path
|
|
49
|
+
.trim()
|
|
50
|
+
.toLowerCase()
|
|
51
|
+
.split("/")
|
|
52
|
+
.filter((segment) => segment.length > 0)
|
|
53
|
+
.map((segment) => (/^(:.+|\{.+\}|\[.+\])$/.test(segment) ? ":" : segment));
|
|
54
|
+
return `/${segments.join("/")}`;
|
|
55
|
+
}
|
|
56
|
+
/** `"GET /a/:id"` -> `["GET /a/:"]`; `"/a"` or `"* /a"` or `"ANY /a"` -> one slot per method. */
|
|
57
|
+
export function normalizeRoute({ key }, _optional = {}) {
|
|
58
|
+
const trimmed = key.trim();
|
|
59
|
+
const space = trimmed.indexOf(" ");
|
|
60
|
+
const method = space === -1 ? "*" : trimmed.slice(0, space).toUpperCase();
|
|
61
|
+
const path = normalizePath(space === -1 ? trimmed : trimmed.slice(space + 1));
|
|
62
|
+
const methods = method === "*" || method === "ANY" ? HTTP_METHODS : [method];
|
|
63
|
+
return methods.map((each) => `${each} ${path}`);
|
|
64
|
+
}
|
|
65
|
+
/** The kinds Tovu (and most hosts) need. A host adds or overrides kinds by spreading this. */
|
|
66
|
+
export const DEFAULT_CLAIM_KIND_RULES = {
|
|
67
|
+
route: { normalize: (key) => normalizeRoute({ key }) },
|
|
68
|
+
};
|
|
69
|
+
function isPrefix(slot) {
|
|
70
|
+
return slot.endsWith(PREFIX_MARKER);
|
|
71
|
+
}
|
|
72
|
+
/** Whether two normalized slots of the same kind name overlapping things (rule 6). */
|
|
73
|
+
function slotsOverlap(a, b) {
|
|
74
|
+
if (a === b)
|
|
75
|
+
return true;
|
|
76
|
+
const aStem = isPrefix(a) ? a.slice(0, -1) : undefined;
|
|
77
|
+
const bStem = isPrefix(b) ? b.slice(0, -1) : undefined;
|
|
78
|
+
if (aStem !== undefined && b.startsWith(aStem))
|
|
79
|
+
return true;
|
|
80
|
+
if (bStem !== undefined && a.startsWith(bStem))
|
|
81
|
+
return true;
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
function slotsFor(claim, rules) {
|
|
85
|
+
const rule = rules[claim.kind];
|
|
86
|
+
const slots = rule ? rule.normalize(claim.key) : normalizeIdentifier({ key: claim.key });
|
|
87
|
+
return slots.map((slot) => ({ claim, slot }));
|
|
88
|
+
}
|
|
89
|
+
/** The first already-held slot a pending slot collides with, if any (rule 2: shared+shared never
|
|
90
|
+
* collides; anything involving an exclusive claim does). */
|
|
91
|
+
function findHolder(held, ownerId, pending) {
|
|
92
|
+
return held.find((each) => each.ownerId !== ownerId &&
|
|
93
|
+
each.kind === pending.claim.kind &&
|
|
94
|
+
!(each.mode === "shared" && pending.claim.mode === "shared") &&
|
|
95
|
+
slotsOverlap(each.slot, pending.slot));
|
|
96
|
+
}
|
|
97
|
+
/** Every conflict one owner has against what is already held; one entry per distinct claim. */
|
|
98
|
+
function conflictsFor(held, owner, pending) {
|
|
99
|
+
const conflicts = [];
|
|
100
|
+
const reported = new Set();
|
|
101
|
+
for (const each of pending) {
|
|
102
|
+
if (reported.has(each.claim))
|
|
103
|
+
continue;
|
|
104
|
+
const holder = findHolder(held, owner.id, each);
|
|
105
|
+
if (!holder)
|
|
106
|
+
continue;
|
|
107
|
+
reported.add(each.claim);
|
|
108
|
+
conflicts.push({ ownerId: owner.id, kind: each.claim.kind, key: each.claim.key, heldBy: holder.ownerId, heldKey: holder.key });
|
|
109
|
+
}
|
|
110
|
+
return conflicts;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Decides, owner by owner in precedence order, who keeps their claims (see this file's header for
|
|
114
|
+
* the six rules).
|
|
115
|
+
*
|
|
116
|
+
* @complexity O(S²) in the total slot count across all owners — a site has tens of plugins with a
|
|
117
|
+
* handful of claims each, so a linear scan of held slots beats an index that prefix claims would
|
|
118
|
+
* make complicated.
|
|
119
|
+
*/
|
|
120
|
+
export function resolveClaimConflicts(required, optional = {}) {
|
|
121
|
+
const rules = optional.rules ?? DEFAULT_CLAIM_KIND_RULES;
|
|
122
|
+
const held = [];
|
|
123
|
+
const accepted = [];
|
|
124
|
+
const refused = new Map();
|
|
125
|
+
for (const owner of required.owners) {
|
|
126
|
+
const pending = owner.claims.flatMap((claim) => slotsFor(claim, rules));
|
|
127
|
+
const conflicts = conflictsFor(held, owner, pending);
|
|
128
|
+
if (conflicts.length > 0) {
|
|
129
|
+
refused.set(owner.id, conflicts);
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
for (const each of pending) {
|
|
133
|
+
held.push({ ownerId: owner.id, kind: each.claim.kind, key: each.claim.key, slot: each.slot, mode: each.claim.mode });
|
|
134
|
+
}
|
|
135
|
+
accepted.push(owner.id);
|
|
136
|
+
}
|
|
137
|
+
return { accepted, refused };
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=claim-conflicts.js.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { DeclarativeContentTypesPort, DeclaredContentTypePorts } from "./ports.js";
|
|
2
|
+
import type { PluginManifest } from "./manifest.js";
|
|
3
|
+
export interface EnableDeclaredPluginRequired {
|
|
4
|
+
readonly declarativeContentTypes: DeclarativeContentTypesPort;
|
|
5
|
+
readonly pluginId: string;
|
|
6
|
+
readonly workspaceId: string;
|
|
7
|
+
/** The discovery record's manifest — already through `validateManifest`, declarations included. */
|
|
8
|
+
readonly manifest: PluginManifest;
|
|
9
|
+
/** Loads and attaches the plugin's code (the runtime's own load path). Never called for tier-1. */
|
|
10
|
+
readonly loadCode: () => Promise<void>;
|
|
11
|
+
/** Detaches what `loadCode` attached — called only when a content type fails to create after it. */
|
|
12
|
+
readonly unloadCode: () => void;
|
|
13
|
+
}
|
|
14
|
+
export interface EnableDeclaredPluginOptional {
|
|
15
|
+
/** Where declared content types are created. Omitted (a composition with no content-type store)
|
|
16
|
+
* ⇒ a plugin that declares any is refused rather than enabled without them. */
|
|
17
|
+
readonly contentTypes?: DeclaredContentTypePorts;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Turns one plugin on around its declarations. A plugin that declares no content types is just
|
|
21
|
+
* `loadCode()` (tier-1 with none: nothing at all). Content-type conflicts with what the site
|
|
22
|
+
* already has throw `PluginInvalidError` (the enable route maps it to 422; `setPluginEnabled`
|
|
23
|
+
* restores the prior activation row).
|
|
24
|
+
*
|
|
25
|
+
* @complexity O(t) port reads/writes over the manifest's declared types (bounded, ≤ 20).
|
|
26
|
+
*/
|
|
27
|
+
export declare function enableDeclaredPlugin(required: EnableDeclaredPluginRequired, optional?: EnableDeclaredPluginOptional): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Binds the host's `registerContentType` for the provisioner. `authorize` always allows, on purpose: the
|
|
30
|
+
* real authorization already happened one layer up — turning a plugin on requires the plugin-manage
|
|
31
|
+
* permission and the owner's consent, and these types are what that consented action declares
|
|
32
|
+
* (the same reasoning as widgets' `PRE_AUTHORIZED`). The write is recorded as `principalKind:
|
|
33
|
+
* "system"` with the plugin as actor, so the revision log says no human typed this schema.
|
|
34
|
+
*/
|
|
35
|
+
/**
|
|
36
|
+
* Ports built on first use. Both composition roots compose the plugin runtime before their
|
|
37
|
+
* content-type repo and outbox exist (top-level composition is synchronous and enable never runs
|
|
38
|
+
* during it), so they hand the runtime this and bind the real ports where those are declared.
|
|
39
|
+
*/
|
|
40
|
+
export declare function deferDeclaredContentTypePorts(required: {
|
|
41
|
+
build: () => DeclaredContentTypePorts;
|
|
42
|
+
}, _optional?: Record<string, never>): DeclaredContentTypePorts;
|
|
43
|
+
//# sourceMappingURL=declarative-enable.d.ts.map
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/** Concrete content-type construction is host-owned. The required declaration port owns its
|
|
2
|
+
* grammar, planning and application; this module owns ordering and deferred binding. */
|
|
3
|
+
/**
|
|
4
|
+
* @file The enable path for a plugin's DECLARED contributions (AW-7 Tier 1): a `tier-1`
|
|
5
|
+
* (manifest-only) plugin is applied from its manifest and never imported, and a code plugin that
|
|
6
|
+
* also declares content types gets them.
|
|
7
|
+
*
|
|
8
|
+
* Runs inside `composePluginRuntime().onPluginEnabled` (`server/runtime/composition/plugin-runtime.ts`),
|
|
9
|
+
* AFTER its conflict gate, so a declared content-type key is checked against every other plugin's
|
|
10
|
+
* claims first (`plugin-claims.ts`, kind `content-type`). Boot re-attach skips tier-1 plugins: their
|
|
11
|
+
* types were stored when they were turned on, and there is no code to load.
|
|
12
|
+
*
|
|
13
|
+
* Order for a code plugin with content types: plan (read-only) → load code → create types. A
|
|
14
|
+
* conflict refuses the enable before the plugin's code is imported, and a failed load leaves no
|
|
15
|
+
* types behind. A type that still fails to create (everything core would refuse is already refused
|
|
16
|
+
* at discovery, so this is an infrastructure failure) unloads the code again, so a refused enable
|
|
17
|
+
* never leaves a hook running. Types created before that failure stay: retain-by-default, and
|
|
18
|
+
* tombstoning them would burn their keys for good (content-types INV-06); a later enable keeps them.
|
|
19
|
+
*/
|
|
20
|
+
import { PluginInvalidError } from "./activation.js";
|
|
21
|
+
/**
|
|
22
|
+
* Turns one plugin on around its declarations. A plugin that declares no content types is just
|
|
23
|
+
* `loadCode()` (tier-1 with none: nothing at all). Content-type conflicts with what the site
|
|
24
|
+
* already has throw `PluginInvalidError` (the enable route maps it to 422; `setPluginEnabled`
|
|
25
|
+
* restores the prior activation row).
|
|
26
|
+
*
|
|
27
|
+
* @complexity O(t) port reads/writes over the manifest's declared types (bounded, ≤ 20).
|
|
28
|
+
*/
|
|
29
|
+
export async function enableDeclaredPlugin(required, optional = {}) {
|
|
30
|
+
const { pluginId, workspaceId, manifest, loadCode } = required;
|
|
31
|
+
// ADR-024 §1: a Tier-1 plugin is zero executable code — the runtime never imports anything for it.
|
|
32
|
+
const load = manifest.tier === "tier-1" ? async () => { } : loadCode;
|
|
33
|
+
const { decls } = required.declarativeContentTypes.parse({ value: manifest.contentTypes });
|
|
34
|
+
if (decls.length === 0)
|
|
35
|
+
return load();
|
|
36
|
+
const ports = optional.contentTypes;
|
|
37
|
+
if (!ports)
|
|
38
|
+
throw new PluginInvalidError(`plugin '${pluginId}' declares content types, but this site cannot create them`);
|
|
39
|
+
const plan = await required.declarativeContentTypes.plan({ ports, workspaceId, decls });
|
|
40
|
+
if (plan.conflicts.length > 0) {
|
|
41
|
+
throw new PluginInvalidError(`plugin '${pluginId}' cannot be turned on: ${plan.conflicts.join("; ")}`);
|
|
42
|
+
}
|
|
43
|
+
await load();
|
|
44
|
+
try {
|
|
45
|
+
await required.declarativeContentTypes.apply({ ports, workspaceId, pluginId, plan });
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
required.unloadCode();
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Binds the host's `registerContentType` for the provisioner. `authorize` always allows, on purpose: the
|
|
54
|
+
* real authorization already happened one layer up — turning a plugin on requires the plugin-manage
|
|
55
|
+
* permission and the owner's consent, and these types are what that consented action declares
|
|
56
|
+
* (the same reasoning as widgets' `PRE_AUTHORIZED`). The write is recorded as `principalKind:
|
|
57
|
+
* "system"` with the plugin as actor, so the revision log says no human typed this schema.
|
|
58
|
+
*/
|
|
59
|
+
// createDeclaredContentTypePorts stays at the host composition boundary with the core write service.
|
|
60
|
+
/**
|
|
61
|
+
* Ports built on first use. Both composition roots compose the plugin runtime before their
|
|
62
|
+
* content-type repo and outbox exist (top-level composition is synchronous and enable never runs
|
|
63
|
+
* during it), so they hand the runtime this and bind the real ports where those are declared.
|
|
64
|
+
*/
|
|
65
|
+
export function deferDeclaredContentTypePorts(required, _optional = {}) {
|
|
66
|
+
let ports;
|
|
67
|
+
const resolve = () => (ports ??= required.build());
|
|
68
|
+
return {
|
|
69
|
+
findByKey: (params) => resolve().findByKey(params),
|
|
70
|
+
register: (input) => resolve().register(input),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=declarative-enable.js.map
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { PluginManifest, PluginValidationError } from "./manifest.js";
|
|
2
|
+
/** A built-in plugin's in-code manifest-equivalent (ADR Decision item 4) — no tarball, no
|
|
3
|
+
* integrity file (compiled-in code is definitionally not tampered), but still validated through
|
|
4
|
+
* the same `validateManifest()` path as a site-installed plugin. */
|
|
5
|
+
export interface BuiltInPluginSource {
|
|
6
|
+
readonly manifest: PluginManifest;
|
|
7
|
+
}
|
|
8
|
+
/** In-memory, rebuilt each discovery pass — NOT persisted (state.spec.md §2). Feeds `PLUGINS_LIST`
|
|
9
|
+
* once activation state is projected onto it by the caller (the HTTP route layer, C-016/C-017 —
|
|
10
|
+
* discovery itself does not know about `plugin_activations`). */
|
|
11
|
+
export interface PluginDiscoveryRecord {
|
|
12
|
+
readonly id: string;
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly version: string;
|
|
15
|
+
readonly source: "built-in" | "site";
|
|
16
|
+
/** (1.1.2, REQ-10/REQ-18) The plugin's trust tier, projected verbatim from the already-required
|
|
17
|
+
* `PluginManifest.tier` (REQ-01/ADR-024 §1) — mirrors how `source` already passes through
|
|
18
|
+
* unchanged. Optional on this stub type only so pre-existing fixtures in unrelated test files
|
|
19
|
+
* (`activation.integration.test.ts`, `loader.integration.test.ts`) that construct
|
|
20
|
+
* `PluginDiscoveryRecord` literals for concerns unrelated to REQ-10/`PLUGINS_LIST` are not forced
|
|
21
|
+
* to retrofit it; the real `discoverPlugins()` implementation must always populate it (a
|
|
22
|
+
* discovered plugin's manifest always has a validated `tier` by the time a record exists),
|
|
23
|
+
* enforced behaviorally by the dedicated REQ-10 pass-through tests in `plugins-dto.unit.test.ts`
|
|
24
|
+
* / `plugins-http.integration.test.ts`, not by widening this field to non-optional everywhere. */
|
|
25
|
+
readonly tier?: "tier-1" | "tier-2" | "tier-3";
|
|
26
|
+
readonly status: "valid" | "invalid" | "incompatible";
|
|
27
|
+
readonly errors: readonly PluginValidationError[];
|
|
28
|
+
/**
|
|
29
|
+
* (Milestone 1b, 2026-08-20) The already-parsed, already-`validateManifest()`-checked manifest
|
|
30
|
+
* this record was built from — present if and only if `status === "valid"` (`undefined`
|
|
31
|
+
* otherwise: an invalid/incompatible candidate's raw JSON may not conform to `PluginManifest`'s
|
|
32
|
+
* shape at all, so this field never claims a validated type for a record that failed validation).
|
|
33
|
+
*
|
|
34
|
+
* Exists so a composition root's enable path (`server/plugin-runtime.ts`'s `onPluginEnabled()`)
|
|
35
|
+
* can hand `loadPlugin()` the SAME manifest object this discovery pass already parsed, instead of
|
|
36
|
+
* re-reading `tovu.plugin.json` a second time. This matters beyond avoiding duplicate I/O: every
|
|
37
|
+
* real caller of `onPluginEnabled` (`activation.ts`'s `setPluginEnabled`, BR-05 step 1) already
|
|
38
|
+
* calls `discoverPlugins()` fresh, in the SAME request, immediately before invoking it (see
|
|
39
|
+
* `routes/admin/plugins/set-enabled.ts`) — so reusing that record's manifest is not a staleness
|
|
40
|
+
* risk, it is the one-and-only read for this attempt. A second, independent load-time re-read
|
|
41
|
+
* would instead open a narrow TOCTOU window: an attacker who can write to `installDir` between
|
|
42
|
+
* the discovery read and a second load-time read could swap in a self-consistent tampered
|
|
43
|
+
* manifest+entry pair that the discovery pass never actually validated. Reusing this field closes
|
|
44
|
+
* that window by construction — there is only one read to race against, not two.
|
|
45
|
+
*
|
|
46
|
+
* Built-in records also carry it (trivially, from `BuiltInPluginSource.manifest`) for type
|
|
47
|
+
* uniformity, but `onPluginEnabled`'s built-in branch does not need it — it already has the
|
|
48
|
+
* composition root's own static `PluginRuntimeSource.manifest`, unchanged from before this slice.
|
|
49
|
+
*/
|
|
50
|
+
readonly manifest?: PluginManifest | undefined;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=discovery-types.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Host adapters must re-export this shared class to preserve instanceof identity; product
|
|
2
|
+
* refusal copy stays host-owned. */
|
|
3
|
+
/**
|
|
4
|
+
* @file Shared plugin save-hook refusal. Callers handling saves and the plugin runtime import
|
|
5
|
+
* this neutral error contract to avoid a dependency cycle or making the save domain plugin-aware.
|
|
6
|
+
*/
|
|
7
|
+
/** Thrown (and caught by the caller, mapped to 500 `PLUGIN_HOOK_FAILED`) when a filter throws,
|
|
8
|
+
* triggers `CapabilityDeniedError`, or returns an invalid `ext` write (BR-07/EC-10). */
|
|
9
|
+
export declare class PluginHookFailedError extends Error {
|
|
10
|
+
readonly pluginId: string;
|
|
11
|
+
/** The item a multi-item apply was writing when the hook refused (e.g. `post:<id>`), set only by
|
|
12
|
+
* an apply that is NOT all-or-nothing (`publish-content/apply-loop.ts`), so items applied before
|
|
13
|
+
* it stay saved. `null` for a single save, where the refusal means nothing was saved. */
|
|
14
|
+
readonly refusedItemRef: string | null;
|
|
15
|
+
constructor(pluginId: string, message: string, options?: {
|
|
16
|
+
cause?: unknown;
|
|
17
|
+
refusedItemRef?: string;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Host adapters must re-export this shared class to preserve instanceof identity; product
|
|
2
|
+
* refusal copy stays host-owned. */
|
|
3
|
+
/**
|
|
4
|
+
* @file Shared plugin save-hook refusal. Callers handling saves and the plugin runtime import
|
|
5
|
+
* this neutral error contract to avoid a dependency cycle or making the save domain plugin-aware.
|
|
6
|
+
*/
|
|
7
|
+
/** Thrown (and caught by the caller, mapped to 500 `PLUGIN_HOOK_FAILED`) when a filter throws,
|
|
8
|
+
* triggers `CapabilityDeniedError`, or returns an invalid `ext` write (BR-07/EC-10). */
|
|
9
|
+
export class PluginHookFailedError extends Error {
|
|
10
|
+
pluginId;
|
|
11
|
+
/** The item a multi-item apply was writing when the hook refused (e.g. `post:<id>`), set only by
|
|
12
|
+
* an apply that is NOT all-or-nothing (`publish-content/apply-loop.ts`), so items applied before
|
|
13
|
+
* it stay saved. `null` for a single save, where the refusal means nothing was saved. */
|
|
14
|
+
refusedItemRef;
|
|
15
|
+
constructor(pluginId, message, options) {
|
|
16
|
+
super(message, options);
|
|
17
|
+
this.name = "PluginHookFailedError";
|
|
18
|
+
this.pluginId = pluginId;
|
|
19
|
+
this.refusedItemRef = options?.refusedItemRef ?? null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file `createHookRegistry()` / `runBeforeSave()` — composes enabled plugins' `content.entry.
|
|
3
|
+
* beforeSave` filters, merges `ext`, fail-closed (SPEC-005 REQ-05/REQ-06; BR-04/BR-06/BR-07;
|
|
4
|
+
* CIC U-004, no escalation marker).
|
|
5
|
+
*
|
|
6
|
+
* Purpose:
|
|
7
|
+
* The concrete adapter for `BeforeSaveHookPort` (`src/features/post/post.ts`'s contract). Keeping
|
|
8
|
+
* this composition logic here — not in `post.ts` — is what keeps `post.ts` plugin-ignorant
|
|
9
|
+
* (Module Map). `activation.ts` calls `.detach()` on disable.
|
|
10
|
+
*
|
|
11
|
+
* **Corrected 2026-08-04 (ADR-057 Decision 2.1):** this comment previously claimed `loader.ts`
|
|
12
|
+
* calls `.attach()` on this registry as each plugin's `setup()` runs. That was false — `loadPlugin()`
|
|
13
|
+
* (`loader.ts`) has never called `.attach()`; steps 4-5 of BR-01 were left unwired, documented but
|
|
14
|
+
* dead, and no other production code called it either (verified: `grep -rn "hookRegistry\.attach"
|
|
15
|
+
* src --include=*.ts`, excluding tests, returned only this file's own now-corrected doc comment).
|
|
16
|
+
* `loader.ts` exports `attachLoadedPlugin()`, the real (not stub) extraction of that dead logic's
|
|
17
|
+
* attach half, widened to accept a `"glue"` source. Site Glue's content-lifecycle attachment point
|
|
18
|
+
* is `attachLoadedPlugin`'s first production caller; `loadPlugin()` itself still does not call it —
|
|
19
|
+
* a future SPEC-005 fix for its own activation path is expected to call the same function, per
|
|
20
|
+
* ADR-057 Decision 2.1, rather than re-inventing this wiring a second time.
|
|
21
|
+
*
|
|
22
|
+
* **CIC U-004 (Binding, no escalation marker — bounded blast radius via SPEC-001's revert):**
|
|
23
|
+
* `runBeforeSave()` must fully resolve (returning the merged patch) or throw before its caller
|
|
24
|
+
* (`post.ts`) constructs the final `PostRecord` passed to `deps.repo.save()`. There is exactly one
|
|
25
|
+
* `repo.save()` call per `createPost`/`updatePost` invocation, and it must never be reached if
|
|
26
|
+
* this function throws (BR-06 same-transaction requirement; BR-07/EC-10 fail-closed: no partial
|
|
27
|
+
* write, no change set on failure). This module's own responsibility ends at "resolve or throw
|
|
28
|
+
* before returning" — `post.ts` is responsible for not calling `repo.save()` on the throw path.
|
|
29
|
+
*
|
|
30
|
+
* Composition order is TB-01 (built-ins first by id ascending, then site plugins by id
|
|
31
|
+
* ascending) — the SAME order `PLUGINS_LIST` uses. Each plugin writes only its own
|
|
32
|
+
* `ext.{pluginId}` namespace (BR-04), so composition order does not create write conflicts, but
|
|
33
|
+
* the ordering is still fixed and deterministic (not incidental) because later filters observe
|
|
34
|
+
* earlier filters' already-merged `ext` values on the entry snapshot they receive.
|
|
35
|
+
*
|
|
36
|
+
* Architectural role:
|
|
37
|
+
* TDD-certified implementation (implementation outline C-010, CIC U-004). Signature and JSDoc are
|
|
38
|
+
* design-frozen; `runBeforeSave()` orders attached filters, gives each an isolated entry snapshot,
|
|
39
|
+
* validates each declared-field patch, and returns the merged per-plugin `ext` object fail-closed.
|
|
40
|
+
*/
|
|
41
|
+
import type { JsonObject } from "@jini-ai/core/primitives";
|
|
42
|
+
import type { BeforeSaveFilter, ContentEntryDraft, HookPoints, PluginSdkBinding } from "./ports.js";
|
|
43
|
+
import { PluginHookFailedError } from "./errors.js";
|
|
44
|
+
/** Who attached a given filter — extended by ADR-057 Decision 3 with `"glue"`, ranked after
|
|
45
|
+
* `"site"` in {@link compareTb01}. Exported so `loader.ts`'s `attachLoadedPlugin()` and any other
|
|
46
|
+
* caller share this one source of truth for the vocabulary rather than re-declaring the union. */
|
|
47
|
+
export type AttachmentSource = "built-in" | "site" | "glue";
|
|
48
|
+
export { PluginHookFailedError };
|
|
49
|
+
/** One field this plugin declared in its manifest (BR-06's validation target — the merge step
|
|
50
|
+
* rejects a returned patch key/value that doesn't match a declared field). */
|
|
51
|
+
export interface HookRegistryFieldDecl {
|
|
52
|
+
readonly path: string;
|
|
53
|
+
readonly type: "string" | "integer" | "number" | "boolean";
|
|
54
|
+
}
|
|
55
|
+
/** Default recovery policy: tolerate two transient failures, quarantine on the third consecutive
|
|
56
|
+
* failure. Composition may inject a different positive integer per runtime instance. */
|
|
57
|
+
export declare const DEFAULT_PLUGIN_FAILURE_THRESHOLD = 3;
|
|
58
|
+
export interface PluginQuarantineEvent {
|
|
59
|
+
readonly pluginId: string;
|
|
60
|
+
readonly workspaceId: string;
|
|
61
|
+
readonly consecutiveFailures: number;
|
|
62
|
+
readonly reason: string;
|
|
63
|
+
}
|
|
64
|
+
export interface CreateHookRegistryOptions {
|
|
65
|
+
readonly failureThreshold?: number;
|
|
66
|
+
/** Persistence/action port supplied by composition. Absent keeps the registry's legacy
|
|
67
|
+
* fail-closed-only behavior (used by isolated consumers that have no activation repository). */
|
|
68
|
+
readonly onQuarantine?: (event: PluginQuarantineEvent) => Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
export interface HookRegistry {
|
|
71
|
+
/** Registers one currently-enabled, successfully-loaded plugin's filter (BR-01 step 5). Safe to
|
|
72
|
+
* call again for the same `pluginId` — a second `attach` for an id already attached replaces
|
|
73
|
+
* the prior registration (re-enable after disable, or a loader retry). */
|
|
74
|
+
attach(required: {
|
|
75
|
+
pluginId: string;
|
|
76
|
+
source: AttachmentSource;
|
|
77
|
+
filter: BeforeSaveFilter;
|
|
78
|
+
declaredFields: readonly HookRegistryFieldDecl[];
|
|
79
|
+
}, optional?: Record<string, never>): void;
|
|
80
|
+
/** Removes a plugin's filter (disable, BR-05) — its `ext` data is untouched (INV-03); only the
|
|
81
|
+
* filter stops firing on subsequent saves. */
|
|
82
|
+
detach(required: {
|
|
83
|
+
pluginId: string;
|
|
84
|
+
}, optional?: Record<string, never>): void;
|
|
85
|
+
/** Implements `BeforeSaveHookPort` — the sole surface `post.ts` depends on. */
|
|
86
|
+
runBeforeSave(required: {
|
|
87
|
+
entry: Readonly<ContentEntryDraft>;
|
|
88
|
+
}, optional?: Record<string, never>): Promise<JsonObject>;
|
|
89
|
+
/** Admin preview (AW-7): runs ONE attached plugin's filter on a draft with the same snapshot and
|
|
90
|
+
* declared-field validation as a save, but merges, saves and counts nothing — a preview failure
|
|
91
|
+
* throws `PluginHookFailedError` without touching the quarantine counter or the attachment, so
|
|
92
|
+
* an editor trying drafts can never quarantine a plugin. Resolves `null` when `pluginId` is not
|
|
93
|
+
* attached (disabled, quarantined, or never loaded). */
|
|
94
|
+
previewBeforeSave(required: {
|
|
95
|
+
pluginId: string;
|
|
96
|
+
entry: Readonly<ContentEntryDraft>;
|
|
97
|
+
}, optional?: Record<string, never>): Promise<JsonObject | null>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Creates a fresh, empty hook registry (no plugins attached). Each process has exactly one
|
|
101
|
+
* long-lived instance, constructed once at composition-root time and shared between `loader.ts`
|
|
102
|
+
* (attach/detach) and `post.ts` (via `BeforeSaveHookPort`, `runBeforeSave` only).
|
|
103
|
+
*
|
|
104
|
+
* @complexity `attach`/`detach` are O(1) (Map operations). `runBeforeSave` is
|
|
105
|
+
* O(attached-plugin-count · own-declared-field-count) for merge/validation bookkeeping, plus each
|
|
106
|
+
* plugin's own filter cost (unbounded, plugin-defined — no latency budget exists, ADR scalability
|
|
107
|
+
* axis, a disclosed gap).
|
|
108
|
+
* @overallScore 100/100
|
|
109
|
+
*/
|
|
110
|
+
export declare function createHookRegistry<H extends HookPoints>(required: {
|
|
111
|
+
pluginSdkBinding: PluginSdkBinding<H>;
|
|
112
|
+
}, options?: CreateHookRegistryOptions): HookRegistry;
|
|
113
|
+
//# sourceMappingURL=hook-registry.d.ts.map
|