@mnmal-ai/substrate 1.0.0-alpha.0 → 1.0.0-alpha.10
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/README.md +51 -9
- package/dist/boot-store-BVqfFxh1.js +4060 -0
- package/dist/{runtime-metrics-DwpDJ8vz.d.ts → bridge-Cp4PaIYO.d.ts} +2 -87
- package/dist/cli-hSCBywhE.js +3126 -0
- package/dist/client.d.ts +130 -8
- package/dist/client.js +6 -5
- package/dist/{plugin-t2g2wX0b.d.ts → dataset-CejF-Fr_.d.ts} +1640 -1320
- package/dist/{agent-token-BIboWOU0.js → env-utils-CGNazUNt.js} +31 -2
- package/dist/{errors-D4vKuwz8.d.ts → errors-7mycvNGq.d.ts} +1 -1
- package/dist/{index-D7mSOFlM.d.ts → index-DgSfwx7M.d.ts} +1 -1
- package/dist/node-version-C5FSmwJX.js +26 -0
- package/dist/pack.d.ts +2 -2
- package/dist/pack.js +1 -1
- package/dist/pg-edges-store-Lxsl5ZnK.js +381 -0
- package/dist/{plugin-j1jt1l3n.js → plugin-CtwOINCM.js} +171 -18
- package/dist/proposals-bootstrap.js +6 -4
- package/dist/proposals.config.schema.json +5 -0
- package/dist/proposals.d.ts +15 -4
- package/dist/proposals.default-config.json +2 -1
- package/dist/proposals.js +2 -2
- package/dist/recall-bootstrap.js +13 -4
- package/dist/recall.config.schema.json +12 -0
- package/dist/recall.d.ts +54 -9
- package/dist/recall.js +2 -2
- package/dist/{repl-C52wyfMs.js → repl-DqfHelK7.js} +62 -29
- package/dist/{row-ByLFE2Wx.d.ts → row-BKKCXIG6.d.ts} +5 -3
- package/dist/run-B5dTbZ3g.js +1997 -0
- package/dist/{schema-g4FQLr0V.d.ts → schema-DoSc-4ig.d.ts} +44 -7
- package/dist/script-C6DSa_Na.js +134 -0
- package/dist/sdk.d.ts +90 -12
- package/dist/sdk.js +5 -110
- package/dist/{src-D91yij6C.js → src-5SJ5GEIM.js} +1557 -327
- package/dist/{src-DuopV8u7.js → src-7CUSMK66.js} +148 -19
- package/dist/{src-CrEgiMJQ.js → src-Bi849yhn.js} +39 -15
- package/dist/{src-D88LvCX8.js → src-CqT-QWZ_.js} +697 -57
- package/dist/src-CyvV7dCK.js +4333 -0
- package/dist/{row-DK7lPZVe.js → src-Da0t6si5.js} +11 -11
- package/dist/{src-C4DzaIQZ.js → src-hZb8wfvg.js} +12 -1
- package/dist/substrate-admin.js +17 -781
- package/dist/substrate.js +4 -5471
- package/dist/testing.d.ts +557 -8
- package/dist/testing.js +39 -3
- package/dist/{ws-C72iVS5T.d.ts → ws-ByZyHk7m.d.ts} +3 -3
- package/package.json +21 -20
- package/dist/dataset-BXkus75W.js +0 -1527
- package/dist/env-utils-B7SGmoeJ.js +0 -21
- package/dist/src-CzxvmhFn.js +0 -2327
|
@@ -1,1568 +1,1888 @@
|
|
|
1
|
-
import { C as
|
|
2
|
-
//#region ../../packages/substrate-types/src/
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
*/
|
|
23
|
-
interface FieldPredicate {
|
|
24
|
-
eq?: number | string | boolean | Date | null;
|
|
25
|
-
neq?: number | string | boolean | Date | null;
|
|
26
|
-
gt?: number | string | Date;
|
|
27
|
-
gte?: number | string | Date;
|
|
28
|
-
lt?: number | string | Date;
|
|
29
|
-
lte?: number | string | Date;
|
|
30
|
-
between?: [number | string | Date, number | string | Date];
|
|
31
|
-
in?: Array<number | string | boolean | Date>;
|
|
32
|
-
nin?: Array<number | string | boolean | Date>;
|
|
33
|
-
contains?: string;
|
|
34
|
-
startsWith?: string;
|
|
35
|
-
endsWith?: string;
|
|
36
|
-
has?: number | string | boolean | Date | null;
|
|
37
|
-
path?: string[];
|
|
1
|
+
import { C as ActorIdentity, E as Id, M as WriteRow, b as EdgesStore, d as PackEnvelope, f as Schema, k as Row, r as FieldDef, u as OrderedBy } from "./schema-DoSc-4ig.js";
|
|
2
|
+
//#region ../../packages/substrate-types/src/policy.d.ts
|
|
3
|
+
interface Identity {
|
|
4
|
+
id: string;
|
|
5
|
+
roles?: string[];
|
|
6
|
+
scopes?: string[];
|
|
7
|
+
/**
|
|
8
|
+
* Per-namespace capabilities — the only grant surface. `{ notes: ['read'],
|
|
9
|
+
* tasks: ['read','mutate'] }`; `'*'` is a legal key. Read it through the
|
|
10
|
+
* `identity-grant.ts` helpers.
|
|
11
|
+
*
|
|
12
|
+
* A plain object rather than a Map: `Identity` is JSON-serialised into the
|
|
13
|
+
* SSE stream ticket, and a Map would arrive as `{}` on the far side.
|
|
14
|
+
*
|
|
15
|
+
* A bare `{ id }` with no grant (direct-call tests, system actors) is the
|
|
16
|
+
* no-claim case: `grantOf` resolves it to `null`, and the engine's grant
|
|
17
|
+
* gate then admits every namespace. An authenticator for remote callers
|
|
18
|
+
* must therefore always set `grants`, `{}` for none.
|
|
19
|
+
*/
|
|
20
|
+
grants: Record<string, string[]>;
|
|
21
|
+
[k: string]: unknown;
|
|
38
22
|
}
|
|
39
23
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* reserved and must not collide with field names declared in any type.
|
|
44
|
-
*/
|
|
45
|
-
type WhereClause = {
|
|
46
|
-
AND: WhereClause[];
|
|
47
|
-
} | {
|
|
48
|
-
OR: WhereClause[];
|
|
49
|
-
} | {
|
|
50
|
-
NOT: WhereClause;
|
|
51
|
-
} | {
|
|
52
|
-
[field: string]: FieldPredicate | WhereClause | unknown;
|
|
53
|
-
};
|
|
54
|
-
/**
|
|
55
|
-
* OrderBy clause. One entry per field, direction per field. Engine appends an
|
|
56
|
-
* implicit `{ id: 'asc' }` when callers omit `id` as the final tie-breaker — a
|
|
57
|
-
* deterministic tie-break is what makes cursor pagination stable across pages.
|
|
24
|
+
* Semantic verb stamped on autoCRUD-registered mutation entries.
|
|
25
|
+
* Absent on type entries and custom mutations — policies needing those use
|
|
26
|
+
* `RequestContext.entry` / `operation` instead.
|
|
58
27
|
*/
|
|
59
|
-
|
|
60
|
-
|
|
28
|
+
type AutoCrudVerb = 'get' | 'create' | 'update' | 'delete';
|
|
29
|
+
interface RequestContext {
|
|
30
|
+
identity: Identity | null;
|
|
31
|
+
namespace: string;
|
|
32
|
+
operation: 'query' | 'mutate' | 'subscribe' | 'stream';
|
|
33
|
+
now: Date;
|
|
34
|
+
headers: Record<string, string>;
|
|
35
|
+
meta: Record<string, unknown>;
|
|
36
|
+
/**
|
|
37
|
+
* Trust-gated client IP computed by the HTTP route handler from the request's
|
|
38
|
+
* `peerIp` + the operator-configured trusted-proxy allowlist. When the peer
|
|
39
|
+
* is in the allowlist, this is the leftmost `x-forwarded-for` entry; when
|
|
40
|
+
* untrusted, this is the peer IP itself. Undefined when the transport doesn't
|
|
41
|
+
* expose peerIp (direct engine unit tests, pre-upgrade WS paths).
|
|
42
|
+
*/
|
|
43
|
+
clientIp?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Whether the deployment verifies credentials at all, as the transport knows
|
|
46
|
+
* it, so a refusal for want of an identity can say whether auth is off
|
|
47
|
+
* rather than implying the caller's key is wrong.
|
|
48
|
+
*
|
|
49
|
+
* `undefined` means the transport did not say (direct engine unit tests,
|
|
50
|
+
* routes with no auth config in scope). Treat it as unknown, never as `false`.
|
|
51
|
+
*/
|
|
52
|
+
authEnabled?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* The registry entry name this dispatch targets — the `<name>` half of the
|
|
55
|
+
* qualified frame key (e.g. `deleteTodo`, `Todo`). Threaded by the engine
|
|
56
|
+
* at every dispatch site (query/mutate/stream/subscribe) so type- and
|
|
57
|
+
* plugin-level policies can discriminate between resolvers that otherwise
|
|
58
|
+
* present identically (`get<T>` and `delete<T>` both arrive as
|
|
59
|
+
* operation:'mutate' with id-only params). Undefined only in direct-call
|
|
60
|
+
* unit tests that construct a bare ctx.
|
|
61
|
+
*/
|
|
62
|
+
entry?: string;
|
|
63
|
+
/**
|
|
64
|
+
* Semantic verb of the targeted entry when autoCRUD registered it.
|
|
65
|
+
* Prefer this over `entry`-name matching for intent-level policies
|
|
66
|
+
* ("deny deletes") — it survives entry renames. Absent for type entries
|
|
67
|
+
* and custom mutations.
|
|
68
|
+
*/
|
|
69
|
+
verb?: AutoCrudVerb;
|
|
61
70
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
limit?: number;
|
|
71
|
+
/** The caller's request context for a policy check made outside dispatch. */
|
|
72
|
+
type PolicyProbeContext = Partial<Pick<RequestContext, 'now' | 'headers' | 'meta' | 'clientIp' | 'authEnabled'>>;
|
|
73
|
+
interface ResolverArgs {
|
|
74
|
+
params?: Record<string, unknown>;
|
|
75
|
+
fields?: Record<string, unknown>;
|
|
76
|
+
rows?: Row[];
|
|
69
77
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
78
|
+
* Engine-injected per-call dataset handle. On `engine.mutate`, this is the
|
|
79
|
+
* transaction-scoped `tx` view (spec §5.3, G1); on `engine.query` it's the
|
|
80
|
+
* plain namespace dataset. Resolvers that perform I/O MUST use this handle
|
|
81
|
+
* — using a dataset captured at registration time bypasses the active
|
|
82
|
+
* transaction and breaks atomicity. Optional purely to keep direct-resolver
|
|
83
|
+
* unit tests concise; the engine always supplies it.
|
|
72
84
|
*/
|
|
73
|
-
|
|
85
|
+
dataset?: Dataset;
|
|
74
86
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
* this
|
|
87
|
+
* Engine-injected per-call edges store (§4.6). Resolvers that touch edges
|
|
88
|
+
* MUST use this handle; the closed-over store from registration is a
|
|
89
|
+
* fallback for direct-call tests. Unlike `dataset`, this is not namespace-
|
|
90
|
+
* scoped — edges are inherently cross-namespace, and a single engine-scoped
|
|
91
|
+
* `EdgesStore` is threaded through every call.
|
|
78
92
|
*/
|
|
79
|
-
|
|
93
|
+
edges?: EdgesStore;
|
|
80
94
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* "include this key"). Nested projection inside JSONB sub-trees is intentionally
|
|
89
|
-
* out of scope; the pack codec already prunes deeper. Row-metadata-style keys
|
|
90
|
-
* (`_metadata`, `shortId`, `_deletedAt`, `id`) are populated from dedicated
|
|
91
|
-
* columns regardless of `fields` and never need to be included here.
|
|
92
|
-
*
|
|
93
|
-
* When absent, substrates return the full body, for raw `dataset.read()`
|
|
94
|
-
* callers (sweep, compose registry, migration scripts) that don't know which
|
|
95
|
-
* body keys they'll need downstream.
|
|
96
|
-
*
|
|
97
|
-
* Substrates MAY ignore this hint — it's a perf optimisation, not a contract.
|
|
98
|
-
* Memory dataset, for instance, has no wire to save and ignores it.
|
|
95
|
+
* Engine-injected post-commit write hook (§5.4). Write-path resolvers
|
|
96
|
+
* (autoCRUD create/update/delete, or custom mutation resolvers) call this
|
|
97
|
+
* AFTER their substrate write lands to announce the change. The engine
|
|
98
|
+
* collects calls into a per-transaction batch and fans them out to the
|
|
99
|
+
* subscription bus only after the enclosing `withTransaction` commits — a
|
|
100
|
+
* rolled-back tx emits nothing. Direct-call unit tests may pass `onWrite` to
|
|
101
|
+
* observe resolver output without spinning up an engine.
|
|
99
102
|
*/
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
+
onWrite?: (event: WriteEvent) => void;
|
|
104
|
+
/**
|
|
105
|
+
* Queue work to run once the surrounding transaction has settled, whether it
|
|
106
|
+
* committed or rolled back: a record that must outlive a rollback, such as
|
|
107
|
+
* an audit row for the refusal that caused it. The work is told which
|
|
108
|
+
* (`committed`), so post-commit work can skip a rollback. Absent outside a
|
|
109
|
+
* transaction, where the caller can simply do the work.
|
|
110
|
+
*/
|
|
111
|
+
afterTx?: (fn: AfterTxWork) => void;
|
|
112
|
+
/**
|
|
113
|
+
* Engine-injected cancellation signal (§5.5). Supplied on `stream` dispatch
|
|
114
|
+
* so generator resolvers can abort mid-iteration when the client cancels or
|
|
115
|
+
* the socket closes. Streaming resolvers SHOULD observe this signal — either
|
|
116
|
+
* by calling `signal.throwIfAborted()` at yield points, or by listening for
|
|
117
|
+
* `abort` to release cursors / close descriptors in their `finally` block.
|
|
118
|
+
* The engine cannot force-stop a generator; cooperation is the contract.
|
|
119
|
+
* Absent on query/mutate dispatch — non-streaming resolvers should not see a
|
|
120
|
+
* signal they have no way to honour.
|
|
121
|
+
*/
|
|
122
|
+
signal?: AbortSignal;
|
|
123
|
+
/**
|
|
124
|
+
* Engine-injected cross-namespace dataset resolver (committed, non-tx view).
|
|
125
|
+
* Write-path resolvers use it to verify crossRef referents that live in a
|
|
126
|
+
* DIFFERENT namespace (onMissing:'reject'). Same-namespace targets should use
|
|
127
|
+
* the tx-scoped `dataset` handle instead. Absent on the query/subscribe path.
|
|
128
|
+
*/
|
|
129
|
+
resolveDataset?: (ns: string) => Dataset | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* Engine-injected on mutate: how many frame entries share this call's
|
|
132
|
+
* transaction, itself included. A resolver whose effect waits on other
|
|
133
|
+
* transactions (a concurrent index drop) refuses to share one.
|
|
134
|
+
*/
|
|
135
|
+
txEntries?: number;
|
|
103
136
|
}
|
|
104
137
|
/**
|
|
105
|
-
*
|
|
106
|
-
*
|
|
138
|
+
* Post-commit write announcement consumed by the engine's subscription bus
|
|
139
|
+
* (§5.4). Emitted by write-path resolvers after the substrate operation
|
|
140
|
+
* commits inside the current transaction. `row` is the post-commit canonical
|
|
141
|
+
* row for creates and updates; for deletes it's the pre-tombstone snapshot
|
|
142
|
+
* (absent if the resolver couldn't read the row before deleting). The engine
|
|
143
|
+
* filters matching subscriptions by `(ns, type)`, tests the `row` against each
|
|
144
|
+
* subscription's `where` predicate, and emits `add` / `update` / `remove`.
|
|
107
145
|
*/
|
|
108
|
-
interface
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
146
|
+
interface WriteEvent {
|
|
147
|
+
ns: string;
|
|
148
|
+
type: string;
|
|
149
|
+
id: Row['id'];
|
|
150
|
+
operation: 'create' | 'update' | 'delete';
|
|
151
|
+
row?: Row;
|
|
112
152
|
}
|
|
113
|
-
interface
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Merged rows after upsert — substrates populate this with the post-merge
|
|
118
|
-
* row state so autoCRUD update can skip its post-read. Order
|
|
119
|
-
* matches `ids` (one entry per input row). Substrates that can't compute
|
|
120
|
-
* the merged shape cheaply may emit input rows verbatim; consumers must
|
|
121
|
-
* not rely on body keys absent from the input being filled in.
|
|
122
|
-
*/
|
|
123
|
-
rows: Row[];
|
|
153
|
+
interface Obligation {
|
|
154
|
+
kind: string;
|
|
155
|
+
[k: string]: unknown;
|
|
124
156
|
}
|
|
125
157
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* - `identity` → both `createdBy` and `updatedBy` (defaults to `null`)
|
|
130
|
-
*
|
|
131
|
-
* On upsert collision, `created_*` columns preserve existing values (immutability);
|
|
132
|
-
* `updated_*` columns adopt the new values (bump intent). Engine path
|
|
133
|
-
* (`autocrud-create`/`autocrud-update`) constructs full `_metadata` itself —
|
|
134
|
-
* this auto-fill is purely a safety net for raw-write bypass paths.
|
|
158
|
+
* Row-filter obligation: emitted by `row<T>(predicate)` policies. The engine
|
|
159
|
+
* scans accumulated obligations after resolver execution and applies each
|
|
160
|
+
* predicate as a silent filter.
|
|
135
161
|
*/
|
|
136
|
-
interface
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Insert only: a row whose id already exists (live or tombstoned) is left
|
|
141
|
-
* untouched and omitted from the result's `ids` and `rows`. The existence
|
|
142
|
-
* check and the insert are one atomic step.
|
|
143
|
-
*/
|
|
144
|
-
readonly insertOnly?: boolean;
|
|
145
|
-
}
|
|
146
|
-
interface DeleteResult {
|
|
147
|
-
deleted: number;
|
|
148
|
-
ids: Id[];
|
|
162
|
+
interface RowFilterObligation<T = unknown> extends Obligation {
|
|
163
|
+
kind: 'row-filter';
|
|
164
|
+
predicate: (ctx: RequestContext, row: T) => boolean;
|
|
149
165
|
}
|
|
150
|
-
interface
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
shortIdsDeleted: number;
|
|
166
|
+
interface PolicyDecision {
|
|
167
|
+
allow: boolean;
|
|
168
|
+
reason?: string;
|
|
169
|
+
obligations?: Obligation[];
|
|
155
170
|
}
|
|
171
|
+
type Policy = (ctx: RequestContext, args: ResolverArgs) => PolicyDecision;
|
|
156
172
|
/**
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
173
|
+
* Engine-facing resolver shape. Spec: §4.3, §5.2, §5.5.
|
|
174
|
+
*
|
|
175
|
+
* The engine treats any resolver's return as opaque rows. A `ReadResult` return
|
|
176
|
+
* signals a paged list; the engine unpacks `rows` into the envelope and copies
|
|
177
|
+
* `nextCursor`/`hasMore` into `_pagination[key]`. An `AsyncIterable<Row>`
|
|
178
|
+
* return is the §5.5 stream-dispatch shape (duck-typed via
|
|
179
|
+
* `Symbol.asyncIterator` so it can't collide with `Row[]`).
|
|
180
|
+
*
|
|
181
|
+
* This is what the registry stores and what the engine dispatches to. Plugin
|
|
182
|
+
* authors declaring custom resolvers use the typed-authoring form
|
|
183
|
+
* `TypedResolver<T>` below; the engine erases to `Resolver` at registration
|
|
184
|
+
* time.
|
|
160
185
|
*/
|
|
161
|
-
type
|
|
162
|
-
interface FilterCapabilities {
|
|
163
|
-
operators: Record<FieldKind, string[]>;
|
|
164
|
-
/** Does the dataset honour `orderBy`? */
|
|
165
|
-
orderBy: boolean;
|
|
166
|
-
/** Does the dataset honour cursor-based pagination? */
|
|
167
|
-
cursor: boolean;
|
|
168
|
-
}
|
|
169
|
-
interface UpdateFieldRef {
|
|
170
|
-
ns?: string;
|
|
171
|
-
type: string;
|
|
172
|
-
id: Id;
|
|
173
|
-
}
|
|
186
|
+
type Resolver = (ctx: RequestContext, args: ResolverArgs) => Promise<Row | Row[] | ReadResult | AsyncIterable<Row> | null> | AsyncIterable<Row>;
|
|
174
187
|
/**
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
* `updates` and returned the merged row. `matched: false` when the row was
|
|
180
|
-
* missing or at least one match condition failed; in both cases no write landed
|
|
181
|
-
* and `row` is `null`.
|
|
188
|
+
* Typed-authoring resolver shape. Used by autoCRUD and hand-authored resolvers
|
|
189
|
+
* that want to preserve the concrete return type before erasing to `Resolver`
|
|
190
|
+
* at registration. `T` is unconstrained at the type level — the engine
|
|
191
|
+
* discriminates on the runtime value shape regardless of static type.
|
|
182
192
|
*/
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
193
|
+
type TypedResolver<T> = (ctx: RequestContext, args: ResolverArgs) => Promise<T>;
|
|
194
|
+
/**
|
|
195
|
+
* Engine-visible return shape for a resolver. `ReadResult` means "paged list";
|
|
196
|
+
* `Row[]` means "full list, no pagination"; `Row` means "single"; `null` means
|
|
197
|
+
* "single, not found". The engine discriminates at runtime.
|
|
198
|
+
*/
|
|
199
|
+
type ResolverReturn = Row | Row[] | ReadResult | null;
|
|
200
|
+
/** Work queued through `ResolverArgs.afterTx`, told whether the transaction committed. */
|
|
201
|
+
type AfterTxWork = (outcome: {
|
|
202
|
+
committed: boolean;
|
|
203
|
+
}) => Promise<void>;
|
|
204
|
+
//#endregion
|
|
205
|
+
//#region ../../packages/substrate-types/src/server.d.ts
|
|
206
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
|
|
207
|
+
interface SubstrateRequest {
|
|
208
|
+
method: HttpMethod;
|
|
209
|
+
path: string;
|
|
210
|
+
headers: Record<string, string>;
|
|
211
|
+
query: Record<string, string>;
|
|
212
|
+
params: Record<string, string>;
|
|
213
|
+
body: Buffer | null;
|
|
214
|
+
identity?: Identity;
|
|
189
215
|
/**
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
* batched mutations when all frames target the same substrate).
|
|
195
|
-
*
|
|
196
|
-
* Convention:
|
|
197
|
-
* - pg: derived from the `postgres.Sql` pool reference identity. All
|
|
198
|
-
* `PgDataset` instances constructed against the same `sql`
|
|
199
|
-
* handle share one `txScopeId`.
|
|
200
|
-
* - memory: unique per `MemoryDataset` instance by default (each
|
|
201
|
-
* instance owns its own `Map`). Test harnesses that want shared-
|
|
202
|
-
* memory semantics can opt in via an explicit constructor option.
|
|
203
|
-
*
|
|
204
|
-
* Datasets with different `txScopeId` values CANNOT participate in a
|
|
205
|
-
* single transaction — the engine refuses mixed-substrate batched
|
|
206
|
-
* mutates with `cross_tx_scope_refused`.
|
|
216
|
+
* Remote peer IP observed at the transport layer. Populated by adapters
|
|
217
|
+
* (Fastify: `request.ip`; memory: caller supplies via `inject({ peerIp })`).
|
|
218
|
+
* Absent when transport doesn't expose it. Consumers that gate on peer IP
|
|
219
|
+
* (e.g. `x-forwarded-for` trust) treat `undefined` as "untrusted peer".
|
|
207
220
|
*/
|
|
208
|
-
|
|
209
|
-
read(type: string, params: ReadParams): Promise<ReadResult>;
|
|
210
|
-
write(type: string, rows: Row[], options?: WriteOptions): Promise<WriteResult>;
|
|
211
|
-
tombstone(type: string, ids: Id[]): Promise<DeleteResult>;
|
|
212
|
-
purge(type: string, ids: Id[]): Promise<DeleteResult>;
|
|
221
|
+
peerIp?: string;
|
|
213
222
|
/**
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
* `archived` state — drop is hard-delete regardless of row status.
|
|
217
|
-
*
|
|
218
|
-
* MUST run inside a caller-supplied `withTransaction` if atomicity with
|
|
219
|
-
* edge cleanup / audit emission is required. When called standalone,
|
|
220
|
-
* implementations MUST still execute the row-delete + short-id-delete as
|
|
221
|
-
* a single atomic step against the substrate.
|
|
222
|
-
*
|
|
223
|
-
* Returns the exact counts physically removed. If the type has no rows,
|
|
224
|
-
* returns zeros without error.
|
|
225
|
-
*
|
|
226
|
-
* Storage-level primitive: operates on any (namespace, type) rows present
|
|
227
|
-
* in storage regardless of whether the type is currently known to the
|
|
228
|
-
* engine's registry.
|
|
223
|
+
* Client IP after the trusted-proxy check, stamped by the host before plugin
|
|
224
|
+
* and kernel route handlers run. Never read from the client.
|
|
229
225
|
*/
|
|
230
|
-
|
|
226
|
+
clientIp?: string;
|
|
227
|
+
/** Whether the deployment verifies credentials, stamped with `clientIp`. */
|
|
228
|
+
authEnabled?: boolean;
|
|
231
229
|
/**
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
* Same atomicity rules as `dropType`. Same "ignores archived state".
|
|
230
|
+
* Per-request abort signal. Production transports (Fastify) wire it to fire
|
|
231
|
+
* on socket close (client disconnect) and on `abortAllRequests()` during
|
|
232
|
+
* graceful shutdown. Route handlers thread it into `engine.query/mutate`
|
|
233
|
+
* via `signal:` so resolvers can cooperatively bail. Optional because
|
|
234
|
+
* test fixtures and embedded `inject()` callers may not need cancellation.
|
|
238
235
|
*/
|
|
239
|
-
|
|
240
|
-
|
|
236
|
+
signal?: AbortSignal;
|
|
237
|
+
}
|
|
238
|
+
interface SubstrateResponse {
|
|
239
|
+
status: number;
|
|
240
|
+
headers?: Record<string, string>;
|
|
241
|
+
body?: Buffer | Uint8Array | string | ReadableStream;
|
|
242
|
+
}
|
|
243
|
+
type HttpHandler = (req: SubstrateRequest) => SubstrateResponse | Promise<SubstrateResponse>;
|
|
244
|
+
/** Request op classes counted by the runtime-metrics surface (§ engine-metrics spec). */
|
|
245
|
+
type RuntimeOp = 'query' | 'mutate';
|
|
246
|
+
/** One observed HTTP request at the transport seam. */
|
|
247
|
+
interface RequestObservation {
|
|
248
|
+
readonly op: RuntimeOp;
|
|
249
|
+
/** Transport-level success — HTTP status < 400. Frame-level envelope errors still count ok. */
|
|
250
|
+
readonly ok: boolean;
|
|
251
|
+
/** Wall-clock elapsed from handler entry to response, measured via Date.now(). Always >= 0. */
|
|
252
|
+
readonly durationMs: number;
|
|
253
|
+
}
|
|
254
|
+
type RequestObserver = (o: RequestObservation) => void;
|
|
255
|
+
interface WsConnection {
|
|
256
|
+
readonly id: string;
|
|
257
|
+
send(frame: Buffer | Uint8Array | string): void;
|
|
241
258
|
/**
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
* - Equality is strict (`===`) for scalars; `Date` compares by
|
|
249
|
-
* `.getTime()`; `null` matches only a stored `null` (including
|
|
250
|
-
* intentionally-null fields set via `updateField`), never an absent field.
|
|
251
|
-
* - A missing row fails the match even when `match` is empty — the
|
|
252
|
-
* operation is "update this existing row if predicate holds".
|
|
253
|
-
* - `updates` is merged onto the stored row the same way `write()` upserts
|
|
254
|
-
* merge: each key overwrites; omitted keys are preserved. `_deletedAt`
|
|
255
|
-
* is reserved and silently skipped (matches `write()`'s sanitisation).
|
|
256
|
-
* - The whole check+write is atomic against other dataset operations.
|
|
257
|
-
* Implementations MUST NOT yield between the predicate test and the
|
|
258
|
-
* write. Pg uses `UPDATE ... WHERE <match> RETURNING *` inside the
|
|
259
|
-
* caller's enclosing `withTransaction`; memory runs the whole body
|
|
260
|
-
* synchronously within the `async` microtask.
|
|
261
|
-
*
|
|
262
|
-
* Motivating use case: the `_system` register route consumes a one-shot
|
|
263
|
-
* invite — the invite row's `consumedAt` must transition from `null` to a
|
|
264
|
-
* timestamp exactly once, even under concurrent registrations.
|
|
265
|
-
*
|
|
266
|
-
* Metadata: a successful match unconditionally bumps `updatedAt` to `now`
|
|
267
|
-
* (a server-set `now()` when omitted) and stamps `updatedBy` with the supplied `actor` (or
|
|
268
|
-
* a `{ kind: 'system', id: 'anonymous' }` sentinel when omitted) — same
|
|
269
|
-
* semantics as `write()`'s upsert path, so recency-ranked recall sees the
|
|
270
|
-
* change. `createdAt` / `createdBy` are preserved.
|
|
259
|
+
* Transport-level back-pressure seam. Resolves once the
|
|
260
|
+
* socket's write buffer is below its high-water mark — immediately when it
|
|
261
|
+
* already is. The stream route awaits this between chunks so a fast producer
|
|
262
|
+
* paces to a slow consumer instead of growing the send buffer unbounded.
|
|
263
|
+
* Optional: transports without buffer introspection (memory tests) omit it,
|
|
264
|
+
* and the route treats a missing hook as "always drained".
|
|
271
265
|
*/
|
|
272
|
-
|
|
273
|
-
|
|
266
|
+
drain?(): Promise<void>;
|
|
267
|
+
onMessage(cb: (frame: Buffer) => void): void;
|
|
268
|
+
onClose(cb: (code?: number) => void): void;
|
|
269
|
+
close(code?: number, reason?: string): void;
|
|
274
270
|
/**
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
* re-acquiring a held key inside `fn` waits forever.
|
|
283
|
-
*
|
|
284
|
-
* OPTIONAL. Callers feature-detect and fall back to a process-local lock.
|
|
285
|
-
*
|
|
286
|
-
* @returns whatever `fn` resolves to
|
|
287
|
-
* @throws whatever `fn` throws, after the lock is released
|
|
271
|
+
* Register a callback invoked synchronously during graceful server shutdown,
|
|
272
|
+
* BEFORE the underlying socket is closed. Higher layers use this to flush
|
|
273
|
+
* structured frames (e.g. `{ kind: 'end', reason: 'server_restart' }` per
|
|
274
|
+
* active subscription) so clients can short-circuit reconnect backoff
|
|
275
|
+
* instead of seeing an abrupt 1006 drop. Optional — transports without a
|
|
276
|
+
* pre-close hook (memory tests) may omit it; the route layer should
|
|
277
|
+
* tolerate undefined.
|
|
288
278
|
*/
|
|
289
|
-
|
|
279
|
+
onShutdown?(cb: (reason: string) => void): void;
|
|
280
|
+
}
|
|
281
|
+
type WsHandler = (req: SubstrateRequest, conn: WsConnection) => void | Promise<void>;
|
|
282
|
+
interface TlsConfig {
|
|
283
|
+
cert: string | Buffer;
|
|
284
|
+
key: string | Buffer;
|
|
285
|
+
ca?: string | Buffer;
|
|
286
|
+
}
|
|
287
|
+
interface StartOpts {
|
|
288
|
+
host?: string;
|
|
289
|
+
port?: number;
|
|
290
|
+
tls?: TlsConfig;
|
|
291
|
+
}
|
|
292
|
+
interface PluginRouter {
|
|
293
|
+
get(path: string, handler: HttpHandler): void;
|
|
294
|
+
post(path: string, handler: HttpHandler): void;
|
|
295
|
+
put(path: string, handler: HttpHandler): void;
|
|
296
|
+
delete(path: string, handler: HttpHandler): void;
|
|
297
|
+
patch(path: string, handler: HttpHandler): void;
|
|
298
|
+
websocket(path: string, handler: WsHandler): void;
|
|
299
|
+
}
|
|
300
|
+
interface SubstrateServer {
|
|
301
|
+
start(opts?: StartOpts): Promise<{
|
|
302
|
+
address: string;
|
|
303
|
+
port: number;
|
|
304
|
+
}>;
|
|
305
|
+
stop(opts?: {
|
|
306
|
+
grace?: number;
|
|
307
|
+
abortInFlight?: boolean;
|
|
308
|
+
}): Promise<void>;
|
|
309
|
+
route(method: HttpMethod, path: string, handler: HttpHandler): void;
|
|
310
|
+
websocket(path: string, handler: WsHandler): void;
|
|
311
|
+
onError?(hook: (err: Error, req?: SubstrateRequest) => void): void;
|
|
312
|
+
createPluginRouter(plugin: {
|
|
313
|
+
name: string;
|
|
314
|
+
}): PluginRouter;
|
|
290
315
|
/**
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
* Scope rules:
|
|
297
|
-
* - `tx.namespace === this.namespace`
|
|
298
|
-
* - Calls on the outer dataset (`this`) inside `fn` bypass the transaction
|
|
299
|
-
* and are NOT rolled back on failure — this is a caller bug; implementations
|
|
300
|
-
* MUST NOT attempt to guard against it. All ops inside `fn` MUST go through
|
|
301
|
-
* `tx`.
|
|
302
|
-
* - Nested `tx.withTransaction(...)` is implementation-defined. The reference
|
|
303
|
-
* impls (memory, pg) flatten nested calls into the outermost transaction
|
|
304
|
-
* (no savepoints).
|
|
316
|
+
* Abort every in-flight HTTP request's `req.signal` so resolvers cooperatively
|
|
317
|
+
* bail. Used during graceful shutdown to give the engine a chance to roll
|
|
318
|
+
* back mutations before fastify's drain timeout fires. WS connections are
|
|
319
|
+
* not affected by this call; their teardown rides on socket close.
|
|
305
320
|
*/
|
|
306
|
-
|
|
321
|
+
abortAllRequests(reason?: string): void;
|
|
307
322
|
/**
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
* containing tx-bound datasets + (optionally) a tx-bound `EdgesStore`.
|
|
311
|
-
* Called by the engine's mutate path (spec §5.3) for both single-ns and
|
|
312
|
-
* cross-ns frames — the engine calls with `[ns]` for the single-ns case.
|
|
313
|
-
*
|
|
314
|
-
* `namespaces` is the full set of namespaces the engine will dispatch
|
|
315
|
-
* frames to inside the tx. Implementations MUST return a tx-bound
|
|
316
|
-
* dataset for every requested namespace in `handles.datasets`; missing
|
|
317
|
-
* entries are a programming error on the caller side.
|
|
318
|
-
*
|
|
319
|
-
* `handles.edges` contract:
|
|
320
|
-
* - `non-null` when the implementation can enlist a shared `EdgesStore`
|
|
321
|
-
* into the same substrate tx (e.g. pg where the `PgEdgesStore` is wired
|
|
322
|
-
* in via constructor and shares the `Sql`). The engine routes edge
|
|
323
|
-
* mutations through this handle — true cross-store atomicity for
|
|
324
|
-
* row + edge within one frame.
|
|
325
|
-
* - `null` when the implementation can't enlist (e.g. memory datasets
|
|
326
|
-
* where edges live in a separate Map). The engine falls back to
|
|
327
|
-
* running edge ops in a nested independent tx and accepts the
|
|
328
|
-
* visibility-ordering quirk documented in `autocrud-delete.ts`.
|
|
323
|
+
* Register a callback invoked synchronously at the START of `stop()` —
|
|
324
|
+
* before in-flight requests are aborted and before the transport drains.
|
|
329
325
|
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
* - Ops on the outer dataset (`this`) inside `fn` bypass the tx — caller
|
|
334
|
-
* bug; implementations do not guard.
|
|
335
|
-
* - A throw or rejection inside `fn` rolls back every mutation across
|
|
336
|
-
* every namespace + edges in the tx.
|
|
326
|
+
* The HTTP analogue of `WsConnection.onShutdown`, for routes that hold
|
|
327
|
+
* a response open indefinitely (SSE): handlers flush a terminal frame and end
|
|
328
|
+
* their response, or the drain would wait forever.
|
|
337
329
|
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
* (and loses edge-enlist for single-ns).
|
|
330
|
+
* Additive, not last-one-wins (unlike `onError`): a dropped registration is
|
|
331
|
+
* a leaked stream. Optional — transports with no graceful phase may omit it,
|
|
332
|
+
* and callers must tolerate undefined.
|
|
342
333
|
*/
|
|
343
|
-
|
|
334
|
+
onShutdown?(cb: (reason: string) => void): void;
|
|
344
335
|
/**
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
* row-id mapping — call `bindShortId` after the row is written.
|
|
348
|
-
*
|
|
349
|
-
* The implementation may use the dataset's current tx handle so the
|
|
350
|
-
* sequence advance rolls back on tx rollback (postgres sequences
|
|
351
|
-
* actually never roll back, but the surrounding tx semantics match
|
|
352
|
-
* what callers expect).
|
|
336
|
+
* Test-only: invoke the server's handler pipeline without opening a socket.
|
|
337
|
+
* Networked implementations may leave this undefined.
|
|
353
338
|
*/
|
|
354
|
-
|
|
339
|
+
inject?(req: Partial<SubstrateRequest>): Promise<SubstrateResponse>;
|
|
355
340
|
/**
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
341
|
+
* Register a per-request observer for op throughput + latency. Optional —
|
|
342
|
+
* transports without instrumentation may omit it. At most one observer; a
|
|
343
|
+
* second call replaces the first. The transport invokes it only for the
|
|
344
|
+
* `/substrate/v1/query` and `/substrate/v1/mutate` routes.
|
|
359
345
|
*/
|
|
360
|
-
|
|
346
|
+
observeRequests?(observer: RequestObserver): void;
|
|
347
|
+
/** Optional: told when a query/mutate HTTP request starts (+1) and ends (-1), including when it throws. */
|
|
348
|
+
observeInFlight?(fn: (op: RuntimeOp, delta: 1 | -1) => void): void;
|
|
349
|
+
}
|
|
350
|
+
//#endregion
|
|
351
|
+
//#region ../../packages/substrate-types/src/plugin.d.ts
|
|
352
|
+
/** Whether a registry entry is a top-level addressable type or a mutation. */
|
|
353
|
+
type RegistryEntryKind = 'type' | 'mutation';
|
|
354
|
+
/**
|
|
355
|
+
* postWrite hook signature. Receives the written row and request context;
|
|
356
|
+
* may perform side-effects (e.g. embedding, index updates). Returned promise
|
|
357
|
+
* is awaited before the engine acks the write to the caller.
|
|
358
|
+
*/
|
|
359
|
+
type PostWriteHook = (row: Row, ctx: RequestContext) => Promise<void>;
|
|
360
|
+
/**
|
|
361
|
+
* Resolver signature stored in a `RegistryEntry`. Identical to the canonical
|
|
362
|
+
* engine-facing `Resolver` in `./policy.ts`; redeclared inline here to keep
|
|
363
|
+
* the registry surface self-describing for the types package. Importers that
|
|
364
|
+
* want the canonical alias should import `Resolver` from this package.
|
|
365
|
+
*/
|
|
366
|
+
type RegistryEntryResolver = (ctx: RequestContext, args: ResolverArgs) => Promise<Row | Row[] | ReadResult | AsyncIterable<Row> | null> | AsyncIterable<Row>;
|
|
367
|
+
/** Input shape for `Registry.register` / `Registry.replace`. */
|
|
368
|
+
interface RegistryEntryInput {
|
|
369
|
+
resolver: RegistryEntryResolver;
|
|
370
|
+
policy?: Policy;
|
|
371
|
+
fields?: Record<string, FieldDef>;
|
|
372
|
+
kind?: RegistryEntryKind;
|
|
373
|
+
params?: Record<string, FieldDef>;
|
|
374
|
+
returns?: FieldDef;
|
|
375
|
+
/** Natural order of a `kind: 'type'` entry; published on its `TypeSchema`. */
|
|
376
|
+
orderedBy?: OrderedBy;
|
|
377
|
+
/** Declared indexes of a `kind: 'type'` entry installed at runtime (a proposed type). */
|
|
378
|
+
indexes?: IndexDecl[];
|
|
379
|
+
/** A proposed type's size hint; `large` lowers the index advisor's row minimum. */
|
|
380
|
+
expectedSize?: 'small' | 'large';
|
|
381
|
+
/**
|
|
382
|
+
* Semantic verb for autoCRUD-registered mutations. The engine
|
|
383
|
+
* threads this into `RequestContext.verb` at dispatch so type-/plugin-level
|
|
384
|
+
* policies can discriminate get/create/update/delete without entry-name
|
|
385
|
+
* string matching. Omit for type entries and custom mutations.
|
|
386
|
+
*/
|
|
387
|
+
verb?: AutoCrudVerb;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Stored shape returned by `Registry.get` and `Registry.all`. Mirrors the
|
|
391
|
+
* concrete `RegistryEntry` in `@mnmal-ai/substrate-core` — the engine reads
|
|
392
|
+
* `policy`, `kind`, `resolver`, `fields`, `params`, and `returns` off this
|
|
393
|
+
* shape on every dispatch.
|
|
394
|
+
*/
|
|
395
|
+
interface RegistryEntry {
|
|
396
|
+
resolver: RegistryEntryResolver;
|
|
397
|
+
policy: Policy;
|
|
398
|
+
fields: Record<string, FieldDef>;
|
|
399
|
+
kind: RegistryEntryKind;
|
|
400
|
+
/** Always present — `{}` for `kind: 'type'` entries where the concept doesn't apply. */
|
|
401
|
+
params: Record<string, FieldDef>;
|
|
402
|
+
/** Present only for `kind: 'mutation'` entries that declared a return shape. */
|
|
403
|
+
returns?: FieldDef;
|
|
404
|
+
/** Mirrors `RegistryEntryInput.orderedBy`. */
|
|
405
|
+
orderedBy?: OrderedBy;
|
|
406
|
+
/** Mirrors `RegistryEntryInput.indexes`. */
|
|
407
|
+
indexes?: IndexDecl[];
|
|
408
|
+
/** Mirrors `RegistryEntryInput.expectedSize`. */
|
|
409
|
+
expectedSize?: 'small' | 'large';
|
|
410
|
+
/** Mirrors `RegistryEntryInput.verb` — present when the registrant stamped one. */
|
|
411
|
+
verb?: AutoCrudVerb;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Canonical per-namespace registry surface, implemented by the concrete
|
|
415
|
+
* `Registry` class in `@mnmal-ai/substrate-core`.
|
|
416
|
+
*/
|
|
417
|
+
interface Registry {
|
|
418
|
+
register(key: string | {
|
|
419
|
+
readonly name: string;
|
|
420
|
+
}, input: RegistryEntryInput): void;
|
|
421
|
+
replace(key: string | {
|
|
422
|
+
readonly name: string;
|
|
423
|
+
}, input: RegistryEntryInput): void;
|
|
361
424
|
/**
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
*
|
|
425
|
+
* Read back a registered entry. Returns undefined when the entry is absent.
|
|
426
|
+
* The full `RegistryEntry` shape (resolver, policy, kind, fields, params,
|
|
427
|
+
* returns) is exposed because the engine reads every field on dispatch.
|
|
365
428
|
*/
|
|
366
|
-
|
|
429
|
+
get(key: string | {
|
|
430
|
+
readonly name: string;
|
|
431
|
+
}): RegistryEntry | undefined;
|
|
432
|
+
/** Presence check. Returns true iff `key` is registered. */
|
|
433
|
+
has(key: string | {
|
|
434
|
+
readonly name: string;
|
|
435
|
+
}): boolean;
|
|
436
|
+
/** Iterate every `[name, entry]` pair. Used by the schema publisher. */
|
|
437
|
+
all(): IterableIterator<[string, RegistryEntry]>;
|
|
367
438
|
/**
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
* before short ids were allocated).
|
|
439
|
+
* Remove an entry. Optional: only a registry that can take back a live
|
|
440
|
+
* apply (a schema proposal rolled back with its transaction) offers it.
|
|
371
441
|
*/
|
|
372
|
-
|
|
442
|
+
unregister?(key: string | {
|
|
443
|
+
readonly name: string;
|
|
444
|
+
}): void;
|
|
373
445
|
/**
|
|
374
|
-
*
|
|
375
|
-
* a
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
* Substrate semantics:
|
|
380
|
-
* - Pg: returns a new `PgDataset` sharing this dataset's `sql` pool +
|
|
381
|
-
* `edgesStore`. The returned dataset has the SAME `txScopeId` (so it
|
|
382
|
-
* can participate in cross-namespace batched mutations on the shared pool).
|
|
383
|
-
* - Memory: returns a new `MemoryDataset` with its own backing `Map`
|
|
384
|
-
* per the default convention (each instance owns its own substrate). The
|
|
385
|
-
* returned dataset has a NEW `txScopeId` — it cannot share a transaction
|
|
386
|
-
* with the caller. Test harnesses that need shared-memory semantics across
|
|
387
|
-
* namespaces should construct each `MemoryDataset` explicitly with
|
|
388
|
-
* the relevant constructor option.
|
|
446
|
+
* Monotonic mutation counter — incremented by every `register` / `replace` / `unregister`.
|
|
447
|
+
* Lets a consumer cache anything derived from the registry (the published
|
|
448
|
+
* schema, above all) and detect staleness from the data, without relying on
|
|
449
|
+
* every registrant remembering to call `engine.invalidateSchemaCaches`.
|
|
389
450
|
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
392
|
-
* to derive datasets for *different* namespaces.
|
|
451
|
+
* Optional so third-party `Registry` implementations remain valid; consumers
|
|
452
|
+
* fall back to callback-driven invalidation when it is absent.
|
|
393
453
|
*/
|
|
394
|
-
|
|
454
|
+
readonly epoch?: number | undefined;
|
|
395
455
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
456
|
+
/**
|
|
457
|
+
* Base class binding the `epoch` bookkeeping so an implementation cannot forget
|
|
458
|
+
* it. Subclasses supply storage via `performRegister` / `performReplace` and
|
|
459
|
+
* never implement `register` / `replace` themselves, so the counter is advanced
|
|
460
|
+
* by construction rather than by discipline.
|
|
461
|
+
*
|
|
462
|
+
* A throw from `performRegister` leaves the epoch untouched, which is correct:
|
|
463
|
+
* a rejected registration changed nothing.
|
|
464
|
+
*
|
|
465
|
+
* Decorators cannot extend this — they wrap an instance rather than subclass it
|
|
466
|
+
* — so a wrapping registry must forward `epoch` from the wrapped instance. See
|
|
467
|
+
* `substrate-compose`'s `_system` install for the worked example.
|
|
468
|
+
*/
|
|
469
|
+
declare abstract class RegistryBase implements Registry {
|
|
470
|
+
#private;
|
|
471
|
+
get epoch(): number;
|
|
472
|
+
register(key: string | {
|
|
473
|
+
readonly name: string;
|
|
474
|
+
}, input: RegistryEntryInput): void;
|
|
475
|
+
replace(key: string | {
|
|
476
|
+
readonly name: string;
|
|
477
|
+
}, input: RegistryEntryInput): void;
|
|
478
|
+
unregister(key: string | {
|
|
479
|
+
readonly name: string;
|
|
480
|
+
}): void;
|
|
481
|
+
/** Store a new entry. Throw to reject; the epoch will not advance. */
|
|
482
|
+
protected abstract performRegister(key: string | {
|
|
483
|
+
readonly name: string;
|
|
484
|
+
}, input: RegistryEntryInput): void;
|
|
485
|
+
/** Overwrite an existing entry. Throw to reject; the epoch will not advance. */
|
|
486
|
+
protected abstract performReplace(key: string | {
|
|
487
|
+
readonly name: string;
|
|
488
|
+
}, input: RegistryEntryInput): void;
|
|
489
|
+
/** Remove an entry. Not abstract, so existing subclasses stay valid; they refuse. */
|
|
490
|
+
protected performUnregister(key: string | {
|
|
491
|
+
readonly name: string;
|
|
492
|
+
}): void;
|
|
493
|
+
abstract get(key: string | {
|
|
494
|
+
readonly name: string;
|
|
495
|
+
}): RegistryEntry | undefined;
|
|
496
|
+
abstract has(key: string | {
|
|
497
|
+
readonly name: string;
|
|
498
|
+
}): boolean;
|
|
499
|
+
abstract all(): IterableIterator<[string, RegistryEntry]>;
|
|
500
|
+
}
|
|
501
|
+
/** See `EngineHandle.addDropParticipant`. */
|
|
502
|
+
interface DropParticipant {
|
|
503
|
+
/** Namespaces whose datasets `onDrop` writes, opened in the drop's transaction. */
|
|
504
|
+
readonly namespaces: readonly string[];
|
|
505
|
+
/** `datasetFor` returns the transaction-bound dataset of one of `namespaces`. */
|
|
506
|
+
onDrop(ns: string, datasetFor: (ns: string) => Dataset): Promise<void>;
|
|
507
|
+
}
|
|
508
|
+
interface NamespaceInstallContext {
|
|
509
|
+
registry: Registry;
|
|
510
|
+
dataset: Dataset;
|
|
511
|
+
edges: EdgesStore;
|
|
400
512
|
/**
|
|
401
|
-
*
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
*
|
|
405
|
-
*
|
|
406
|
-
*
|
|
407
|
-
* detect before subscribing.
|
|
513
|
+
* Plugin-global policy composed from the owning plugin's `policies` array
|
|
514
|
+
* (undefined when the plugin declares none). Namespace install
|
|
515
|
+
* implementations SHOULD compose this with any namespace-scoped or per-type
|
|
516
|
+
* policies via `and()` so plugin-level invariants apply across every
|
|
517
|
+
* resolver the namespace contributes. `defineNamespace` in
|
|
518
|
+
* `@mnmal-ai/substrate-sdk` does this automatically.
|
|
408
519
|
*/
|
|
409
|
-
|
|
520
|
+
pluginPolicy?: Policy;
|
|
410
521
|
/**
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
*
|
|
522
|
+
* Substrate-plugin contributions to per-type postWrite chains. Map
|
|
523
|
+
* `typeName → PostWriteHook[]`. Engine populates by walking each
|
|
524
|
+
* composed substrate plugin's `contributePostWrites(namespace)` and
|
|
525
|
+
* merging results. Hooks run AFTER the type's inline postWrite in
|
|
526
|
+
* declaration order. Empty map / undefined = no contributions.
|
|
416
527
|
*/
|
|
417
|
-
|
|
528
|
+
postWriteContributions?: Record<string, PostWriteHook[]>;
|
|
418
529
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
* no-claim case: `grantOf` resolves it to `null`, and the engine's grant
|
|
435
|
-
* gate then admits every namespace. An authenticator for remote callers
|
|
436
|
-
* must therefore always set `grants`, `{}` for none.
|
|
437
|
-
*/
|
|
438
|
-
grants: Record<string, string[]>;
|
|
439
|
-
[k: string]: unknown;
|
|
530
|
+
/**
|
|
531
|
+
* Read-side context passed to `NamespaceTypeDef.embed.source` when the
|
|
532
|
+
* substrate composing the embedding (e.g. substrate-recall) has a dataset
|
|
533
|
+
* resolver wired. Lets the source function pull related-entity context
|
|
534
|
+
* via crossRefs (e.g. inline a linked Concept's name into the embedded
|
|
535
|
+
* text so semantic recall surfaces relevant rows).
|
|
536
|
+
*
|
|
537
|
+
* Minimal by design — `read` is the only method, and it returns the
|
|
538
|
+
* first matching row or `null`. Source functions that don't need
|
|
539
|
+
* crossRef enrichment ignore the parameter.
|
|
540
|
+
*/
|
|
541
|
+
interface EmbedSourceContext {
|
|
542
|
+
read: (namespace: string, type: string, params: {
|
|
543
|
+
id: string;
|
|
544
|
+
}) => Promise<Row | null>;
|
|
440
545
|
}
|
|
441
546
|
/**
|
|
442
|
-
*
|
|
443
|
-
*
|
|
444
|
-
*
|
|
547
|
+
* A declared index over a type's scalar or crossRef fields, built by the
|
|
548
|
+
* engine with the same expressions the filter compiler emits.
|
|
549
|
+
*
|
|
550
|
+
* - `fields`: key columns in order; `dir` defaults to `asc`. Both directions
|
|
551
|
+
* sort NULLs last, as `orderBy` does.
|
|
552
|
+
* - `unique`: refused at build time while duplicate live values exist.
|
|
553
|
+
* - `where`: always ANDed with "not deleted". `'live'` adds nothing more; a
|
|
554
|
+
* record adds equality to a boolean (or IS NULL for `null`) on the type's
|
|
555
|
+
* own fields. Only those operands compile to literals, which a generic plan
|
|
556
|
+
* needs to match the index predicate.
|
|
557
|
+
* - `name`: the pg index name to own or adopt; generated when omitted. It may
|
|
558
|
+
* not end in `__swap`, the reconciler's temporary suffix.
|
|
445
559
|
*/
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
560
|
+
interface IndexDecl {
|
|
561
|
+
fields: Array<string | {
|
|
562
|
+
field: string;
|
|
563
|
+
dir: 'asc' | 'desc';
|
|
564
|
+
}>;
|
|
565
|
+
unique?: boolean;
|
|
566
|
+
where?: 'live' | Record<string, boolean | null>;
|
|
567
|
+
name?: string;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Structural shape of a type definition, mirroring `TypeInput` from
|
|
571
|
+
* @mnmal-ai/substrate-sdk (redeclared to avoid a substrate-types → substrate-sdk import
|
|
572
|
+
* cycle). Substrate plugins read it structurally; slots not named here pass
|
|
573
|
+
* through opaquely.
|
|
574
|
+
*/
|
|
575
|
+
interface NamespaceTypeDef {
|
|
576
|
+
fields: Record<string, FieldDef>;
|
|
577
|
+
postWrite?: (row: Row, ctx: RequestContext) => Promise<void>;
|
|
578
|
+
embed?: {
|
|
579
|
+
/**
|
|
580
|
+
* Produce the source text fed to the embedder for this row, or `null` /
|
|
581
|
+
* `undefined` to declare the row un-indexable.
|
|
582
|
+
*
|
|
583
|
+
* The null sentinel is a RETRIEVAL POLICY, not an absence of text: it
|
|
584
|
+
* means "this row exists and must be referenceable, but must never enter
|
|
585
|
+
* the recall candidate set". The composing substrate both declines to
|
|
586
|
+
* index the row AND removes any embedding it already has, so a row that
|
|
587
|
+
* flips to un-indexable after being written stops being recallable
|
|
588
|
+
* without being deleted.
|
|
589
|
+
*
|
|
590
|
+
* `''` is NOT the sentinel — an empty string embeds normally. Returning
|
|
591
|
+
* `''` to mean "skip" indexes every such row at the same point in the
|
|
592
|
+
* vector space.
|
|
593
|
+
*
|
|
594
|
+
* `ctx` is optional — when the composing substrate has a dataset
|
|
595
|
+
* resolver wired, source functions may call `ctx.read(ns, type,
|
|
596
|
+
* { id })` to inline related-entity context. When `ctx` is undefined,
|
|
597
|
+
* the source must work from `row` alone. Async return is supported
|
|
598
|
+
* so source functions can await crossRef reads.
|
|
599
|
+
*/
|
|
600
|
+
source: (row: Row, ctx?: EmbedSourceContext) => string | null | undefined | Promise<string | null | undefined>;
|
|
601
|
+
};
|
|
462
602
|
/**
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
* rather than implying the caller's key is wrong.
|
|
603
|
+
* Produce the text fed to the LEXICAL (keyword) index for this row, with the
|
|
604
|
+
* same `null` / `undefined` un-indexable sentinel as `embed.source`.
|
|
466
605
|
*
|
|
467
|
-
* `
|
|
468
|
-
*
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
*
|
|
473
|
-
*
|
|
474
|
-
* at every dispatch site (query/mutate/stream/subscribe) so type- and
|
|
475
|
-
* plugin-level policies can discriminate between resolvers that otherwise
|
|
476
|
-
* present identically (`get<T>` and `delete<T>` both arrive as
|
|
477
|
-
* operation:'mutate' with id-only params). Undefined only in direct-call
|
|
478
|
-
* unit tests that construct a bare ctx.
|
|
606
|
+
* A sibling of `embed` rather than a flag on it, because the two indexes
|
|
607
|
+
* want different text: `embed.source` conventionally takes a short handle
|
|
608
|
+
* (`synopsis || body`), which drops proper nouns a keyword index needs.
|
|
609
|
+
* `lexical.source` is where the long form goes.
|
|
610
|
+
*
|
|
611
|
+
* A type may declare either, both, or neither. Both is the expected shape for
|
|
612
|
+
* a store whose handle is shorter than its content.
|
|
479
613
|
*/
|
|
480
|
-
|
|
614
|
+
lexical?: {
|
|
615
|
+
source: (row: Row) => string | null | undefined | Promise<string | null | undefined>;
|
|
616
|
+
};
|
|
617
|
+
/** Composite and unique indexes the engine builds for this type. See {@link IndexDecl}. */
|
|
618
|
+
indexes?: IndexDecl[];
|
|
619
|
+
/** The type's natural order, the default read order. See {@link OrderedBy}. */
|
|
620
|
+
orderedBy?: OrderedBy;
|
|
621
|
+
/** See the SDK's `TypeInput.ordering`. */
|
|
622
|
+
ordering?: 'visible';
|
|
623
|
+
/** Size hint: `large` lowers the index advisor's row minimum for this type. */
|
|
624
|
+
expectedSize?: 'small' | 'large';
|
|
625
|
+
[key: string]: unknown;
|
|
626
|
+
}
|
|
627
|
+
interface NamespaceDefinition {
|
|
628
|
+
namespace: string;
|
|
481
629
|
/**
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
*
|
|
630
|
+
* Type definitions registered under this namespace, keyed by type name.
|
|
631
|
+
* Read-only metadata exposed for substrate plugins that need to walk
|
|
632
|
+
* the type surface at coordinator-namespace install time (e.g.
|
|
633
|
+
* substrate-recall reads `types[T].embed?.source` to contribute embedding
|
|
634
|
+
* postWrite hooks). Populated by `defineNamespace`; absent on hand-crafted
|
|
635
|
+
* `NamespaceDefinition` objects that bypass the SDK helper.
|
|
486
636
|
*/
|
|
487
|
-
|
|
488
|
-
}
|
|
489
|
-
/** The caller's request context for a policy check made outside dispatch. */
|
|
490
|
-
type PolicyProbeContext = Partial<Pick<RequestContext, 'now' | 'headers' | 'meta' | 'clientIp' | 'authEnabled'>>;
|
|
491
|
-
interface ResolverArgs {
|
|
492
|
-
params?: Record<string, unknown>;
|
|
493
|
-
fields?: Record<string, unknown>;
|
|
494
|
-
rows?: Row[];
|
|
637
|
+
types?: Readonly<Record<string, NamespaceTypeDef>>;
|
|
495
638
|
/**
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
* plain namespace dataset. Resolvers that perform I/O MUST use this handle
|
|
499
|
-
* — using a dataset captured at registration time bypasses the active
|
|
500
|
-
* transaction and breaks atomicity. Optional purely to keep direct-resolver
|
|
501
|
-
* unit tests concise; the engine always supplies it.
|
|
639
|
+
* Opaque per-consumer metadata, keyed by consumer (e.g. `nl`). Core never
|
|
640
|
+
* interprets it; it is passed through to the namespace schema unchanged.
|
|
502
641
|
*/
|
|
503
|
-
|
|
642
|
+
annotations?: Readonly<Record<string, unknown>>;
|
|
504
643
|
/**
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
*
|
|
508
|
-
*
|
|
509
|
-
* `EdgesStore` is threaded through every call.
|
|
644
|
+
* Body keys a SQL dataset mirrors into dedicated `rows` columns, so filters
|
|
645
|
+
* and sorts on them hit an index. The declaring plugin creates the columns in
|
|
646
|
+
* its kernel migrations; the engine creates none. `column` is a bare
|
|
647
|
+
* identifier.
|
|
510
648
|
*/
|
|
511
|
-
|
|
649
|
+
promotedColumns?: Readonly<Record<string, {
|
|
650
|
+
column: string;
|
|
651
|
+
kind: 'boolean' | 'text';
|
|
652
|
+
}>>;
|
|
512
653
|
/**
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
* subscription bus only after the enclosing `withTransaction` commits — a
|
|
518
|
-
* rolled-back tx emits nothing. Direct-call unit tests may pass `onWrite` to
|
|
519
|
-
* observe resolver output without spinning up an engine.
|
|
654
|
+
* Install types + mutations + policies into the registry. Invoked once at
|
|
655
|
+
* registration time, BEFORE the engine is constructed. The plugin owns the
|
|
656
|
+
* dataset handle it uses for resolver closures; wiring datasets into the
|
|
657
|
+
* engine's `DatasetRegistry` is the app composition root's job.
|
|
520
658
|
*/
|
|
521
|
-
|
|
659
|
+
install(ctx: NamespaceInstallContext): void | Promise<void>;
|
|
660
|
+
}
|
|
661
|
+
interface PluginLifecycleContext {
|
|
522
662
|
/**
|
|
523
|
-
*
|
|
524
|
-
*
|
|
525
|
-
*
|
|
526
|
-
*
|
|
527
|
-
*
|
|
663
|
+
* Tagged-union discriminant. Differentiates this context from
|
|
664
|
+
* the structurally-similar `KernelLifecycleContext` so a coordinator's
|
|
665
|
+
* `onStart` handler is type-incompatible with a substrate's
|
|
666
|
+
* `onKernelReady` handler — the composition root sets this, and plugins
|
|
667
|
+
* can also branch on it at runtime if a single helper handles both
|
|
668
|
+
* lifecycle stages.
|
|
528
669
|
*/
|
|
529
|
-
|
|
670
|
+
readonly lifecycle: 'coordinator';
|
|
530
671
|
/**
|
|
531
|
-
*
|
|
532
|
-
*
|
|
533
|
-
* the
|
|
534
|
-
*
|
|
535
|
-
* `abort` to release cursors / close descriptors in their `finally` block.
|
|
536
|
-
* The engine cannot force-stop a generator; cooperation is the contract.
|
|
537
|
-
* Absent on query/mutate dispatch — non-streaming resolvers should not see a
|
|
538
|
-
* signal they have no way to honour.
|
|
672
|
+
* Structured metric/log sink — the hosting binary picks the concrete surface
|
|
673
|
+
* (stdout JSON, Prometheus counter, OpenTelemetry span, …). Plugins MUST NOT
|
|
674
|
+
* assume the sink is synchronous or observable; the contract is fire-and-
|
|
675
|
+
* forget per call.
|
|
539
676
|
*/
|
|
540
|
-
|
|
677
|
+
metric(name: string, payload?: unknown): void;
|
|
541
678
|
/**
|
|
542
|
-
* Engine
|
|
543
|
-
*
|
|
544
|
-
*
|
|
545
|
-
*
|
|
679
|
+
* Engine handle threaded into `onStart` so plugins that mutate their
|
|
680
|
+
* registry post-construction (e.g. substrate-proposals' applyType /
|
|
681
|
+
* applyField paths) can wire `engine.invalidateSchemaCaches(ns)` into
|
|
682
|
+
* their replace flow. Engine construction completes before `onStart`
|
|
683
|
+
* fires, so the handle is always live here.
|
|
546
684
|
*/
|
|
547
|
-
|
|
548
|
-
}
|
|
549
|
-
/**
|
|
550
|
-
* Post-commit write announcement consumed by the engine's subscription bus
|
|
551
|
-
* (§5.4). Emitted by write-path resolvers after the substrate operation
|
|
552
|
-
* commits inside the current transaction. `row` is the post-commit canonical
|
|
553
|
-
* row for creates and updates; for deletes it's the pre-tombstone snapshot
|
|
554
|
-
* (absent if the resolver couldn't read the row before deleting). The engine
|
|
555
|
-
* filters matching subscriptions by `(ns, type)`, tests the `row` against each
|
|
556
|
-
* subscription's `where` predicate, and emits `add` / `update` / `remove`.
|
|
557
|
-
*/
|
|
558
|
-
interface WriteEvent {
|
|
559
|
-
ns: string;
|
|
560
|
-
type: string;
|
|
561
|
-
id: Row['id'];
|
|
562
|
-
operation: 'create' | 'update' | 'delete';
|
|
563
|
-
row?: Row;
|
|
564
|
-
}
|
|
565
|
-
interface Obligation {
|
|
566
|
-
kind: string;
|
|
567
|
-
[k: string]: unknown;
|
|
685
|
+
engine: EngineHandle;
|
|
568
686
|
}
|
|
569
687
|
/**
|
|
570
|
-
*
|
|
571
|
-
*
|
|
572
|
-
*
|
|
688
|
+
* Engine-facing input shape for `query` / `mutate` / `subscribe` / `stream`.
|
|
689
|
+
* Declared here (rather than in substrate-core) so plugin surfaces can reference
|
|
690
|
+
* the engine's call shape as a type without importing the engine package —
|
|
691
|
+
* substrate-types stays zero-runtime.
|
|
573
692
|
*/
|
|
574
|
-
interface
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
693
|
+
interface FrameEntry {
|
|
694
|
+
params?: Record<string, unknown>;
|
|
695
|
+
fields?: Record<string, unknown>;
|
|
696
|
+
/**
|
|
697
|
+
* §5.13 dedup key. Present on a mutate frame entry opts the whole frame
|
|
698
|
+
* into the engine's idempotency store; a repeat call with the same key
|
|
699
|
+
* inside the TTL returns the cached envelope verbatim.
|
|
700
|
+
*/
|
|
701
|
+
idempotencyKey?: string;
|
|
582
702
|
}
|
|
583
|
-
type Policy = (ctx: RequestContext, args: ResolverArgs) => PolicyDecision;
|
|
584
703
|
/**
|
|
585
|
-
*
|
|
586
|
-
*
|
|
587
|
-
* The engine treats any resolver's return as opaque rows. A `ReadResult` return
|
|
588
|
-
* signals a paged list; the engine unpacks `rows` into the envelope and copies
|
|
589
|
-
* `nextCursor`/`hasMore` into `_pagination[key]`. An `AsyncIterable<Row>`
|
|
590
|
-
* return is the §5.5 stream-dispatch shape (duck-typed via
|
|
591
|
-
* `Symbol.asyncIterator` so it can't collide with `Row[]`).
|
|
704
|
+
* Qualified frame (§4.7). Keys are `<ns>/<name>`. `<name>` is the type entry
|
|
705
|
+
* name for query, or the mutation name for mutate.
|
|
592
706
|
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
*
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
/**
|
|
600
|
-
* Typed-authoring resolver shape. Used by autoCRUD and hand-authored resolvers
|
|
601
|
-
* that want to preserve the concrete return type before erasing to `Resolver`
|
|
602
|
-
* at registration. `T` is unconstrained at the type level — the engine
|
|
603
|
-
* discriminates on the runtime value shape regardless of static type.
|
|
707
|
+
* Batched mutate (§5.13): a mutate op-key MAY carry an array of `FrameEntry`
|
|
708
|
+
* values. All frames under one op-key run serially inside the SAME
|
|
709
|
+
* transaction — all-or-nothing per op-key, committed independently of other
|
|
710
|
+
* op-keys. Single-frame op-keys share one transaction when they share a
|
|
711
|
+
* substrate and are refused (`cross_tx_scope_refused`) when they do not.
|
|
712
|
+
* Query / subscribe / stream op-keys reject array values.
|
|
604
713
|
*/
|
|
605
|
-
type
|
|
714
|
+
type QualifiedFrame = Record<string, FrameEntry | Array<FrameEntry>>;
|
|
715
|
+
interface EngineInput {
|
|
716
|
+
frame: QualifiedFrame;
|
|
717
|
+
ctx: Partial<RequestContext>;
|
|
718
|
+
/**
|
|
719
|
+
* §5.13 cancellation. Threaded into each resolver via `args.signal` so
|
|
720
|
+
* resolvers observe cancel by polling `signal.aborted` or registering an
|
|
721
|
+
* `abort` listener.
|
|
722
|
+
*/
|
|
723
|
+
signal?: AbortSignal;
|
|
724
|
+
/**
|
|
725
|
+
* Page bounds for list reads, set by a transport serving remote callers:
|
|
726
|
+
* a type read with no `limit` gets `defaultLimit`, one above `maxLimit` is
|
|
727
|
+
* refused. Absent for in-process callers, which read what they ask for.
|
|
728
|
+
*/
|
|
729
|
+
pageLimits?: {
|
|
730
|
+
readonly defaultLimit: number;
|
|
731
|
+
readonly maxLimit: number;
|
|
732
|
+
};
|
|
733
|
+
}
|
|
606
734
|
/**
|
|
607
|
-
*
|
|
608
|
-
*
|
|
609
|
-
* "single, not found". The engine discriminates at runtime.
|
|
735
|
+
* Per-namespace engine configuration. substrate-core imports this type rather
|
|
736
|
+
* than redeclaring it.
|
|
610
737
|
*/
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
738
|
+
interface NamespaceConfig {
|
|
739
|
+
registry: Registry;
|
|
740
|
+
/**
|
|
741
|
+
* Marks a substrate-contributed namespace. Virtual namespaces
|
|
742
|
+
* have NO dataset: the engine skips the dataset-presence check on
|
|
743
|
+
* dispatch and leaves `args.dataset` undefined — resolvers compute from
|
|
744
|
+
* `params`/`fields` and MUST NOT reach for a dataset handle. They are
|
|
745
|
+
* structurally incapable of autoCRUD/storage — coordinators own data,
|
|
746
|
+
* substrate surfaces own computation. Subscribe refuses these namespaces
|
|
747
|
+
* with `subscription_unsupported_frame`.
|
|
748
|
+
*/
|
|
749
|
+
virtual?: boolean;
|
|
750
|
+
/** From `NamespaceDefinition.annotations`; surfaced on the schema, never hashed. */
|
|
751
|
+
annotations?: Readonly<Record<string, unknown>>;
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Narrow engine handle threaded into scheduled-task / plugin-route contexts.
|
|
755
|
+
* The concrete `Engine` class in `@mnmal-ai/substrate-core` satisfies this
|
|
756
|
+
* structurally; plugin code binds against this handle without importing the
|
|
757
|
+
* engine package.
|
|
758
|
+
*/
|
|
759
|
+
interface EngineHandle {
|
|
760
|
+
query(input: EngineInput): Promise<PackEnvelope>;
|
|
761
|
+
mutate(input: EngineInput): Promise<PackEnvelope>;
|
|
627
762
|
/**
|
|
628
|
-
*
|
|
629
|
-
*
|
|
630
|
-
*
|
|
631
|
-
*
|
|
763
|
+
* Evaluate the read policy for a type entry without dispatching it.
|
|
764
|
+
* Out-of-band read surfaces (kernel routes returning row data
|
|
765
|
+
* outside frame dispatch — e.g. /substrate/v1/recall) call this so the same
|
|
766
|
+
* composed policies that gate `query` gate them too. Fails closed on
|
|
767
|
+
* unknown namespace/type and tombstoned namespaces.
|
|
632
768
|
*/
|
|
633
|
-
|
|
769
|
+
evaluateReadPolicy(ns: string, type: string, identity: Identity | null, probe?: PolicyProbeContext): PolicyDecision;
|
|
634
770
|
/**
|
|
635
|
-
*
|
|
636
|
-
*
|
|
771
|
+
* Invalidate cached schema outputs after an out-of-band registry mutation
|
|
772
|
+
* (e.g. `applyTypeProposal` / `applyFieldProposal` calling
|
|
773
|
+
* `Registry.replace`). Without this hook, `federatedSchema()` and
|
|
774
|
+
* `schemaFor(ns)` keep returning pre-mutation views. Optional `ns` argument also drops the per-namespace `schemaFor` cache;
|
|
775
|
+
* omit to invalidate only the federated outputs.
|
|
637
776
|
*/
|
|
638
|
-
|
|
639
|
-
/** Whether the deployment verifies credentials, stamped with `clientIp`. */
|
|
640
|
-
authEnabled?: boolean;
|
|
777
|
+
invalidateSchemaCaches(ns?: string): void;
|
|
641
778
|
/**
|
|
642
|
-
*
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
*
|
|
646
|
-
*
|
|
779
|
+
* Register a new namespace at runtime. Throws on collision or on a
|
|
780
|
+
* tombstoned name. Used by substrate-proposals' propose_namespace verb to provision
|
|
781
|
+
* per-user / per-tenant namespaces without binary restart. Caller (typically
|
|
782
|
+
* the resolver) is responsible for FPP semantics; this method is the
|
|
783
|
+
* mechanical primitive.
|
|
647
784
|
*/
|
|
648
|
-
|
|
649
|
-
}
|
|
650
|
-
interface SubstrateResponse {
|
|
651
|
-
status: number;
|
|
652
|
-
headers?: Record<string, string>;
|
|
653
|
-
body?: Buffer | Uint8Array | string | ReadableStream;
|
|
654
|
-
}
|
|
655
|
-
type HttpHandler = (req: SubstrateRequest) => SubstrateResponse | Promise<SubstrateResponse>;
|
|
656
|
-
/** Request op classes counted by the runtime-metrics surface (§ engine-metrics spec). */
|
|
657
|
-
type RuntimeOp = 'query' | 'mutate';
|
|
658
|
-
/** One observed HTTP request at the transport seam. */
|
|
659
|
-
interface RequestObservation {
|
|
660
|
-
readonly op: RuntimeOp;
|
|
661
|
-
/** Transport-level success — HTTP status < 400. Frame-level envelope errors still count ok. */
|
|
662
|
-
readonly ok: boolean;
|
|
663
|
-
/** Wall-clock elapsed from handler entry to response, measured via Date.now(). Always >= 0. */
|
|
664
|
-
readonly durationMs: number;
|
|
665
|
-
}
|
|
666
|
-
type RequestObserver = (o: RequestObservation) => void;
|
|
667
|
-
interface WsConnection {
|
|
668
|
-
readonly id: string;
|
|
669
|
-
send(frame: Buffer | Uint8Array | string): void;
|
|
785
|
+
addNamespace(ns: string, config: NamespaceConfig): void;
|
|
670
786
|
/**
|
|
671
|
-
*
|
|
672
|
-
*
|
|
673
|
-
*
|
|
674
|
-
*
|
|
675
|
-
*
|
|
676
|
-
*
|
|
787
|
+
* Mark a namespace as tombstoned. After this call:
|
|
788
|
+
* - schemaFor(ns) throws `unknown namespace`
|
|
789
|
+
* - federatedSchema() omits the ns
|
|
790
|
+
* - query / mutate / subscribe against the ns return a per-key
|
|
791
|
+
* `namespace_tombstoned` error
|
|
792
|
+
* - existing live subscriptions on the ns are closed with a
|
|
793
|
+
* `namespace_tombstoned` error frame
|
|
794
|
+
*
|
|
795
|
+
* Idempotent. The underlying NamespaceConfig stays in `#namespaces` for
|
|
796
|
+
* concurrency safety (in-flight ops keep their references) — full physical
|
|
797
|
+
* reclaim happens at engine restart, gated by whatever persistence layer
|
|
798
|
+
* the host uses to decide which namespaces to re-install at boot. Tombstone
|
|
799
|
+
* state itself is in-memory; restart wipes it.
|
|
677
800
|
*/
|
|
678
|
-
|
|
679
|
-
onMessage(cb: (frame: Buffer) => void): void;
|
|
680
|
-
onClose(cb: (code?: number) => void): void;
|
|
681
|
-
close(code?: number, reason?: string): void;
|
|
801
|
+
tombstoneNamespace(ns: string): void;
|
|
682
802
|
/**
|
|
683
|
-
*
|
|
684
|
-
*
|
|
685
|
-
*
|
|
686
|
-
* active subscription) so clients can short-circuit reconnect backoff
|
|
687
|
-
* instead of seeing an abrupt 1006 drop. Optional — transports without a
|
|
688
|
-
* pre-close hook (memory tests) may omit it; the route layer should
|
|
689
|
-
* tolerate undefined.
|
|
803
|
+
* Be told, after the fact, each time a namespace is tombstoned (a drop), so
|
|
804
|
+
* a plugin that installed it can retire what it owns. A listener's throw is
|
|
805
|
+
* reported as a metric and does not reach the dropper. Returns a disposer.
|
|
690
806
|
*/
|
|
691
|
-
|
|
692
|
-
}
|
|
693
|
-
type WsHandler = (req: SubstrateRequest, conn: WsConnection) => void | Promise<void>;
|
|
694
|
-
interface TlsConfig {
|
|
695
|
-
cert: string | Buffer;
|
|
696
|
-
key: string | Buffer;
|
|
697
|
-
ca?: string | Buffer;
|
|
698
|
-
}
|
|
699
|
-
interface StartOpts {
|
|
700
|
-
host?: string;
|
|
701
|
-
port?: number;
|
|
702
|
-
tls?: TlsConfig;
|
|
703
|
-
}
|
|
704
|
-
interface PluginRouter {
|
|
705
|
-
get(path: string, handler: HttpHandler): void;
|
|
706
|
-
post(path: string, handler: HttpHandler): void;
|
|
707
|
-
put(path: string, handler: HttpHandler): void;
|
|
708
|
-
delete(path: string, handler: HttpHandler): void;
|
|
709
|
-
patch(path: string, handler: HttpHandler): void;
|
|
710
|
-
websocket(path: string, handler: WsHandler): void;
|
|
711
|
-
}
|
|
712
|
-
interface SubstrateServer {
|
|
713
|
-
start(opts?: StartOpts): Promise<{
|
|
714
|
-
address: string;
|
|
715
|
-
port: number;
|
|
716
|
-
}>;
|
|
717
|
-
stop(opts?: {
|
|
718
|
-
grace?: number;
|
|
719
|
-
abortInFlight?: boolean;
|
|
720
|
-
}): Promise<void>;
|
|
721
|
-
route(method: HttpMethod, path: string, handler: HttpHandler): void;
|
|
722
|
-
websocket(path: string, handler: WsHandler): void;
|
|
723
|
-
onError?(hook: (err: Error, req?: SubstrateRequest) => void): void;
|
|
724
|
-
createPluginRouter(plugin: {
|
|
725
|
-
name: string;
|
|
726
|
-
}): PluginRouter;
|
|
807
|
+
onNamespaceTombstoned(listener: (ns: string) => void): () => void;
|
|
727
808
|
/**
|
|
728
|
-
*
|
|
729
|
-
*
|
|
730
|
-
*
|
|
731
|
-
*
|
|
809
|
+
* Take part in every namespace drop's transaction: the drop route opens
|
|
810
|
+
* `participant.namespaces` in its transaction too and runs `onDrop` after
|
|
811
|
+
* the rows are gone and before it commits, so a throw rolls the drop back.
|
|
812
|
+
* Returns a disposer.
|
|
732
813
|
*/
|
|
733
|
-
|
|
814
|
+
addDropParticipant(participant: DropParticipant): () => void;
|
|
815
|
+
/** The participants `addDropParticipant` registered, for the drop route. */
|
|
816
|
+
dropParticipants(): readonly DropParticipant[];
|
|
734
817
|
/**
|
|
735
|
-
*
|
|
736
|
-
*
|
|
737
|
-
*
|
|
738
|
-
*
|
|
739
|
-
* a response open indefinitely (SSE): handlers flush a terminal frame and end
|
|
740
|
-
* their response, or the drain would wait forever.
|
|
741
|
-
*
|
|
742
|
-
* Additive, not last-one-wins (unlike `onError`): a dropped registration is
|
|
743
|
-
* a leaked stream. Optional — transports with no graceful phase may omit it,
|
|
744
|
-
* and callers must tolerate undefined.
|
|
818
|
+
* Take a namespace off the engine and free its name for `addNamespace`.
|
|
819
|
+
* Subscriptions on it close, its schema disappears, new ops answer
|
|
820
|
+
* `unknown_namespace`; in-flight ops keep their resolved config. Undoes a
|
|
821
|
+
* runtime add, e.g. a rolled-back propose_namespace. Idempotent.
|
|
745
822
|
*/
|
|
746
|
-
|
|
823
|
+
removeNamespace(ns: string): void;
|
|
824
|
+
/** Whether `ns` is on the engine, and if so whether it is still served. */
|
|
825
|
+
namespaceState(ns: string): 'absent' | 'live' | 'tombstoned';
|
|
747
826
|
/**
|
|
748
|
-
*
|
|
749
|
-
*
|
|
827
|
+
* Construct a fresh empty Registry that the engine can accept via
|
|
828
|
+
* `addNamespace`. Plugins call this to provision new namespaces at runtime
|
|
829
|
+
* without taking a runtime dep on the concrete Registry class in substrate-core.
|
|
750
830
|
*/
|
|
751
|
-
|
|
831
|
+
createRegistry(): Registry;
|
|
752
832
|
/**
|
|
753
|
-
*
|
|
754
|
-
*
|
|
755
|
-
*
|
|
756
|
-
*
|
|
833
|
+
* Look up the host-registered dataset for a namespace, or `undefined` if the
|
|
834
|
+
* host has not provisioned one. Thin pass-through to the engine's
|
|
835
|
+
* `DatasetRegistry.get`, surfaced on the handle so plugins running on
|
|
836
|
+
* dynamically-added namespaces can resolve datasets without the host having
|
|
837
|
+
* to plumb a `datasetFor` lookup through plugin config.
|
|
838
|
+
*
|
|
839
|
+
* Substrate selection remains the host's responsibility — this method only
|
|
840
|
+
* exposes the lookup. Plugins that hot-add namespaces (substrate-proposals'
|
|
841
|
+
* `propose_namespace`) MUST require the host to register the dataset before
|
|
842
|
+
* the call; this method returns `undefined` for unregistered namespaces.
|
|
757
843
|
*/
|
|
758
|
-
|
|
759
|
-
/** Optional: told when a query/mutate HTTP request starts (+1) and ends (-1), including when it throws. */
|
|
760
|
-
observeInFlight?(fn: (op: RuntimeOp, delta: 1 | -1) => void): void;
|
|
844
|
+
datasetFor(namespace: string): Dataset | undefined;
|
|
761
845
|
}
|
|
762
|
-
//#endregion
|
|
763
|
-
//#region ../../packages/substrate-types/src/plugin.d.ts
|
|
764
|
-
/** Whether a registry entry is a top-level addressable type or a mutation. */
|
|
765
|
-
type RegistryEntryKind = 'type' | 'mutation';
|
|
766
|
-
/**
|
|
767
|
-
* postWrite hook signature. Receives the written row and request context;
|
|
768
|
-
* may perform side-effects (e.g. embedding, index updates). Returned promise
|
|
769
|
-
* is awaited before the engine acks the write to the caller.
|
|
770
|
-
*/
|
|
771
|
-
type PostWriteHook = (row: Row, ctx: RequestContext) => Promise<void>;
|
|
772
846
|
/**
|
|
773
|
-
*
|
|
774
|
-
*
|
|
775
|
-
*
|
|
776
|
-
*
|
|
847
|
+
* Engine surface needed by substrate-pack-aware route helpers — extends the
|
|
848
|
+
* dispatch handle with the two methods compose's `serializeEnvelope` /
|
|
849
|
+
* `mutateRoute` need to drive positional encoding. The concrete
|
|
850
|
+
* `Engine` class satisfies this structurally; tests + alt
|
|
851
|
+
* implementations can satisfy it without depending on the engine package.
|
|
777
852
|
*/
|
|
778
|
-
|
|
779
|
-
/**
|
|
780
|
-
|
|
781
|
-
resolver: RegistryEntryResolver;
|
|
782
|
-
policy?: Policy;
|
|
783
|
-
fields?: Record<string, FieldDef>;
|
|
784
|
-
kind?: RegistryEntryKind;
|
|
785
|
-
params?: Record<string, FieldDef>;
|
|
786
|
-
returns?: FieldDef;
|
|
853
|
+
interface EncodeContext extends EngineHandle {
|
|
854
|
+
/** Federated schema map (per-namespace Schema) for substrate-pack encoding. */
|
|
855
|
+
federatedSchema(): Record<string, Schema>;
|
|
787
856
|
/**
|
|
788
|
-
*
|
|
789
|
-
*
|
|
790
|
-
*
|
|
791
|
-
* string matching. Omit for type entries and custom mutations.
|
|
857
|
+
* Look up a namespace's mutation-return registry view. Returns undefined
|
|
858
|
+
* when the namespace isn't registered. The mutate route uses this to
|
|
859
|
+
* resolve each frame key's `returns` FieldDef for positional encoding.
|
|
792
860
|
*/
|
|
793
|
-
|
|
861
|
+
registryFor(namespace: string): {
|
|
862
|
+
get(name: string): {
|
|
863
|
+
returns?: FieldDef;
|
|
864
|
+
} | undefined;
|
|
865
|
+
} | undefined;
|
|
794
866
|
}
|
|
795
867
|
/**
|
|
796
|
-
*
|
|
797
|
-
* concrete `RegistryEntry` in `@mnmal-ai/substrate-core` — the engine reads
|
|
798
|
-
* `policy`, `kind`, `resolver`, `fields`, `params`, and `returns` off this
|
|
799
|
-
* shape on every dispatch.
|
|
868
|
+
* Context threaded into every `ScheduledTask.handler` call.
|
|
800
869
|
*/
|
|
801
|
-
interface
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
/**
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
/**
|
|
811
|
-
|
|
870
|
+
interface ScheduledTaskContext {
|
|
871
|
+
/** The owning plugin's `name`. */
|
|
872
|
+
pluginName: string;
|
|
873
|
+
/** Engine handle for read/write work performed by the task. */
|
|
874
|
+
engine: EngineHandle;
|
|
875
|
+
/** Structured metric sink — fire-and-forget. */
|
|
876
|
+
metric(name: string, payload?: unknown): void;
|
|
877
|
+
}
|
|
878
|
+
interface ScheduledTask {
|
|
879
|
+
/** Unique identifier within the plugin. Used in metric labels. */
|
|
880
|
+
name: string;
|
|
881
|
+
/** Fire interval in milliseconds. Minimum 100ms — smaller throws at register. */
|
|
882
|
+
intervalMs: number;
|
|
883
|
+
/** Fire once at registration in addition to the interval ticks. Default false. */
|
|
884
|
+
runOnStart?: boolean;
|
|
885
|
+
/** Handler — receives a context with engine access for read/write work. */
|
|
886
|
+
handler(ctx: ScheduledTaskContext): void | Promise<void>;
|
|
812
887
|
}
|
|
813
888
|
/**
|
|
814
|
-
*
|
|
815
|
-
* `Registry` class in `@mnmal-ai/substrate-core`.
|
|
889
|
+
* Context threaded into `CoordinatorPlugin.routes()` calls.
|
|
816
890
|
*/
|
|
817
|
-
interface
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
891
|
+
interface PluginRouteContext {
|
|
892
|
+
/** The owning plugin's `name`. */
|
|
893
|
+
pluginName: string;
|
|
894
|
+
/** Engine handle for plugin-scoped routes that need to dispatch back through the engine. */
|
|
895
|
+
engine: EngineHandle;
|
|
896
|
+
/** Structured metric sink — fire-and-forget. */
|
|
897
|
+
metric(name: string, payload?: unknown): void;
|
|
898
|
+
}
|
|
899
|
+
interface CoordinatorPlugin {
|
|
900
|
+
name: string;
|
|
901
|
+
namespaces: NamespaceDefinition[];
|
|
824
902
|
/**
|
|
825
|
-
*
|
|
826
|
-
*
|
|
827
|
-
*
|
|
903
|
+
* Plugin's bound contract version: a semver RANGE over the binary version,
|
|
904
|
+
* e.g. `'>=4.1.0 <6.0.0'`.
|
|
905
|
+
*
|
|
906
|
+
* The ceiling tracks actual compatibility, not the next major: it moves
|
|
907
|
+
* only when a major genuinely breaks the plugin-facing interface. A ceiling
|
|
908
|
+
* that admits a major which did break lets the plugin load and misbehave
|
|
909
|
+
* silently — if you cannot say whether the last major broke the plugin
|
|
910
|
+
* interface, do not widen it.
|
|
911
|
+
*
|
|
912
|
+
* Enforced at boot: a plugin whose range excludes the running binary is
|
|
913
|
+
* refused.
|
|
828
914
|
*/
|
|
829
|
-
|
|
830
|
-
readonly name: string;
|
|
831
|
-
}): RegistryEntry | undefined;
|
|
832
|
-
/** Presence check. Returns true iff `key` is registered. */
|
|
833
|
-
has(key: string | {
|
|
834
|
-
readonly name: string;
|
|
835
|
-
}): boolean;
|
|
836
|
-
/** Iterate every `[name, entry]` pair. Used by the schema publisher. */
|
|
837
|
-
all(): IterableIterator<[string, RegistryEntry]>;
|
|
915
|
+
contractVersion: string;
|
|
838
916
|
/**
|
|
839
|
-
*
|
|
840
|
-
*
|
|
917
|
+
* Plugin-global policies applied to every resolver in every namespace the
|
|
918
|
+
* plugin contributes. Composed by the loader via `and(...)` — the combined
|
|
919
|
+
* policy must allow. Useful for cross-cutting concerns (audit logging,
|
|
920
|
+
* rate limits, identity enrichment) that span multiple namespaces in the
|
|
921
|
+
* same plugin. The composed result is surfaced through
|
|
922
|
+
* `NamespaceInstallContext.pluginPolicy`.
|
|
841
923
|
*/
|
|
842
|
-
|
|
843
|
-
readonly name: string;
|
|
844
|
-
}): void;
|
|
924
|
+
policies?: Policy[];
|
|
845
925
|
/**
|
|
846
|
-
*
|
|
847
|
-
*
|
|
848
|
-
*
|
|
849
|
-
|
|
926
|
+
* Post-register lifecycle hook. Fires after every plugin's `install()`
|
|
927
|
+
* resolves — use this for resource priming that needs the full namespace
|
|
928
|
+
* map (e.g. cross-plugin edge warmup).
|
|
929
|
+
*/
|
|
930
|
+
onStart?(ctx: PluginLifecycleContext): void | Promise<void>;
|
|
931
|
+
/**
|
|
932
|
+
* Pre-shutdown lifecycle hook. Fires when the host calls `compose()`'s
|
|
933
|
+
* returned `stop()` (which the binary's SIGTERM handler invokes inside
|
|
934
|
+
* the `SUBSTRATE_SHUTDOWN_GRACE_MS` window). Hooks run in REVERSE
|
|
935
|
+
* registration order so plugins tear down in the inverse of the order
|
|
936
|
+
* they came up — late-loading plugins that depended on earlier ones get
|
|
937
|
+
* to release their handles first.
|
|
850
938
|
*
|
|
851
|
-
*
|
|
852
|
-
*
|
|
939
|
+
* Use for resources the OS won't reap on process exit:
|
|
940
|
+
* - subprocess children that should receive SIGTERM cleanly
|
|
941
|
+
* - long-lived `setInterval` timers (`.unref()` keeps them from
|
|
942
|
+
* blocking exit, but explicit teardown is still cleaner for tests)
|
|
943
|
+
* - WebSocket subscriptions / outbound connections that should close
|
|
944
|
+
* cooperatively rather than dropping mid-request
|
|
945
|
+
*
|
|
946
|
+
* Errors are caught + logged via the lifecycle metric sink under
|
|
947
|
+
* `plugin_on_stop_error` — one plugin's failed teardown does NOT abort
|
|
948
|
+
* other plugins' teardown. The engine + pg pool are still live during
|
|
949
|
+
* onStop calls, so plugins can do final writes (e.g. an audit row
|
|
950
|
+
* recording the shutdown). After all onStop hooks resolve, the server
|
|
951
|
+
* stops accepting connections and the substrate's `closeStore()` runs.
|
|
952
|
+
*
|
|
953
|
+
* Symmetric to `onStart`. Skipped (no-op) when omitted.
|
|
853
954
|
*/
|
|
854
|
-
|
|
855
|
-
}
|
|
856
|
-
/**
|
|
857
|
-
* Base class binding the `epoch` bookkeeping so an implementation cannot forget
|
|
858
|
-
* it. Subclasses supply storage via `performRegister` / `performReplace` and
|
|
859
|
-
* never implement `register` / `replace` themselves, so the counter is advanced
|
|
860
|
-
* by construction rather than by discipline.
|
|
861
|
-
*
|
|
862
|
-
* A throw from `performRegister` leaves the epoch untouched, which is correct:
|
|
863
|
-
* a rejected registration changed nothing.
|
|
864
|
-
*
|
|
865
|
-
* Decorators cannot extend this — they wrap an instance rather than subclass it
|
|
866
|
-
* — so a wrapping registry must forward `epoch` from the wrapped instance. See
|
|
867
|
-
* `substrate-compose`'s `_system` install for the worked example.
|
|
868
|
-
*/
|
|
869
|
-
declare abstract class RegistryBase implements Registry {
|
|
870
|
-
#private;
|
|
871
|
-
get epoch(): number;
|
|
872
|
-
register(key: string | {
|
|
873
|
-
readonly name: string;
|
|
874
|
-
}, input: RegistryEntryInput): void;
|
|
875
|
-
replace(key: string | {
|
|
876
|
-
readonly name: string;
|
|
877
|
-
}, input: RegistryEntryInput): void;
|
|
878
|
-
unregister(key: string | {
|
|
879
|
-
readonly name: string;
|
|
880
|
-
}): void;
|
|
881
|
-
/** Store a new entry. Throw to reject; the epoch will not advance. */
|
|
882
|
-
protected abstract performRegister(key: string | {
|
|
883
|
-
readonly name: string;
|
|
884
|
-
}, input: RegistryEntryInput): void;
|
|
885
|
-
/** Overwrite an existing entry. Throw to reject; the epoch will not advance. */
|
|
886
|
-
protected abstract performReplace(key: string | {
|
|
887
|
-
readonly name: string;
|
|
888
|
-
}, input: RegistryEntryInput): void;
|
|
889
|
-
/** Remove an entry. Not abstract, so existing subclasses stay valid; they refuse. */
|
|
890
|
-
protected performUnregister(key: string | {
|
|
891
|
-
readonly name: string;
|
|
892
|
-
}): void;
|
|
893
|
-
abstract get(key: string | {
|
|
894
|
-
readonly name: string;
|
|
895
|
-
}): RegistryEntry | undefined;
|
|
896
|
-
abstract has(key: string | {
|
|
897
|
-
readonly name: string;
|
|
898
|
-
}): boolean;
|
|
899
|
-
abstract all(): IterableIterator<[string, RegistryEntry]>;
|
|
900
|
-
}
|
|
901
|
-
/** See `EngineHandle.addDropParticipant`. */
|
|
902
|
-
interface DropParticipant {
|
|
903
|
-
/** Namespaces whose datasets `onDrop` writes, opened in the drop's transaction. */
|
|
904
|
-
readonly namespaces: readonly string[];
|
|
905
|
-
/** `datasetFor` returns the transaction-bound dataset of one of `namespaces`. */
|
|
906
|
-
onDrop(ns: string, datasetFor: (ns: string) => Dataset): Promise<void>;
|
|
907
|
-
}
|
|
908
|
-
interface NamespaceInstallContext {
|
|
909
|
-
registry: Registry;
|
|
910
|
-
dataset: Dataset;
|
|
911
|
-
edges: EdgesStore;
|
|
955
|
+
onStop?(ctx: PluginLifecycleContext): void | Promise<void>;
|
|
912
956
|
/**
|
|
913
|
-
*
|
|
914
|
-
*
|
|
915
|
-
*
|
|
916
|
-
* policies via `and()` so plugin-level invariants apply across every
|
|
917
|
-
* resolver the namespace contributes. `defineNamespace` in
|
|
918
|
-
* `@mnmal-ai/substrate-sdk` does this automatically.
|
|
957
|
+
* Scheduled tasks — the binary ticks these on `.unref()`'d intervals.
|
|
958
|
+
* Registered with the shared `TaskScheduler` at compose time and started
|
|
959
|
+
* after the server's `start()` resolves.
|
|
919
960
|
*/
|
|
920
|
-
|
|
961
|
+
schedule?: ScheduledTask[];
|
|
921
962
|
/**
|
|
922
|
-
*
|
|
923
|
-
* `
|
|
924
|
-
*
|
|
925
|
-
*
|
|
926
|
-
*
|
|
963
|
+
* Mount plugin-scoped HTTP/WS routes. Called once at compose-time with a
|
|
964
|
+
* `PluginRouter` pre-prefixed `/substrate/v1/plugins/<name>` by the server impl. The
|
|
965
|
+
* router surface matches `PluginRouter` in `./server.ts`.
|
|
966
|
+
*
|
|
967
|
+
* With auth on, HTTP routes require `read` for GET and `mutate` otherwise,
|
|
968
|
+
* granted in one of this plugin's `namespaces`; a plugin with no namespaces
|
|
969
|
+
* accepts the capability granted in any namespace.
|
|
927
970
|
*/
|
|
928
|
-
|
|
971
|
+
routes?(router: PluginRouter, ctx: PluginRouteContext): void | Promise<void>;
|
|
972
|
+
/**
|
|
973
|
+
* Optional onboarding contribution. When set, the hosting binary's
|
|
974
|
+
* `GET /substrate/v1/onboard` aggregates this record under
|
|
975
|
+
* `instructions.plugins.<name>` and enumerates the plugin's conventional
|
|
976
|
+
* `/substrate/v1/plugins/<name>/onboard` route under `instructions.pluginRoutes`. Use
|
|
977
|
+
* this to teach agents about the plugin's types, write flows, or any
|
|
978
|
+
* plugin-specific grammar a cold-start agent would otherwise have to
|
|
979
|
+
* discover per endpoint. The shape is free-form — the binary round-trips
|
|
980
|
+
* it verbatim.
|
|
981
|
+
*/
|
|
982
|
+
onboardInstructions?: Record<string, unknown>;
|
|
929
983
|
}
|
|
930
984
|
/**
|
|
931
|
-
*
|
|
932
|
-
*
|
|
933
|
-
*
|
|
934
|
-
* via crossRefs (e.g. inline a linked Concept's name into the embedded
|
|
935
|
-
* text so semantic recall surfaces relevant rows).
|
|
985
|
+
* Substrate-tier plugin. Augments the kernel rather than the agent-facing
|
|
986
|
+
* surface. Composes BEFORE coordinator plugins so coordinators can rely on
|
|
987
|
+
* substrate registrations being live.
|
|
936
988
|
*
|
|
937
|
-
*
|
|
938
|
-
*
|
|
939
|
-
* crossRef enrichment ignore the parameter.
|
|
940
|
-
*/
|
|
941
|
-
interface EmbedSourceContext {
|
|
942
|
-
read: (namespace: string, type: string, params: {
|
|
943
|
-
id: string;
|
|
944
|
-
}) => Promise<Row | null>;
|
|
945
|
-
}
|
|
946
|
-
/**
|
|
947
|
-
* Structural shape of a type definition, mirroring `TypeInput` from
|
|
948
|
-
* @mnmal-ai/substrate-sdk (redeclared to avoid a substrate-types → substrate-sdk import
|
|
949
|
-
* cycle). Substrate plugins read it structurally; slots not named here pass
|
|
950
|
-
* through opaquely.
|
|
989
|
+
* SubstratePlugins do NOT own data namespaces — that's a CoordinatorPlugin
|
|
990
|
+
* concern (a substrate may claim a virtual `kernelNamespace`).
|
|
951
991
|
*/
|
|
952
|
-
interface
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
* index the row AND removes any embedding it already has, so a row that
|
|
964
|
-
* flips to un-indexable after being written stops being recallable
|
|
965
|
-
* without being deleted.
|
|
966
|
-
*
|
|
967
|
-
* `''` is NOT the sentinel — an empty string embeds normally. Returning
|
|
968
|
-
* `''` to mean "skip" indexes every such row at the same point in the
|
|
969
|
-
* vector space.
|
|
970
|
-
*
|
|
971
|
-
* `ctx` is optional — when the composing substrate has a dataset
|
|
972
|
-
* resolver wired, source functions may call `ctx.read(ns, type,
|
|
973
|
-
* { id })` to inline related-entity context. When `ctx` is undefined,
|
|
974
|
-
* the source must work from `row` alone. Async return is supported
|
|
975
|
-
* so source functions can await crossRef reads.
|
|
976
|
-
*/
|
|
977
|
-
source: (row: Row, ctx?: EmbedSourceContext) => string | null | undefined | Promise<string | null | undefined>;
|
|
978
|
-
};
|
|
992
|
+
interface KernelPlugin {
|
|
993
|
+
/** Unique plugin name. Used in metric labels + collision detection. */
|
|
994
|
+
name: string;
|
|
995
|
+
/**
|
|
996
|
+
* Plugin's bound contract version: a semver RANGE over the binary version,
|
|
997
|
+
* e.g. `'>=4.1.0 <6.0.0'`. Required on substrate plugins. Checked by
|
|
998
|
+
* `composeFromManifests` and REFUSED at boot on mismatch; the
|
|
999
|
+
* ceiling tracks actual compatibility rather than the next major boundary.
|
|
1000
|
+
* See the note on `CoordinatorPlugin.contractVersion` for both.
|
|
1001
|
+
*/
|
|
1002
|
+
contractVersion: string;
|
|
979
1003
|
/**
|
|
980
|
-
*
|
|
981
|
-
*
|
|
982
|
-
*
|
|
983
|
-
*
|
|
984
|
-
* want different text: `embed.source` conventionally takes a short handle
|
|
985
|
-
* (`synopsis || body`), which drops proper nouns a keyword index needs.
|
|
986
|
-
* `lexical.source` is where the long form goes.
|
|
987
|
-
*
|
|
988
|
-
* A type may declare either, both, or neither. Both is the expected shape for
|
|
989
|
-
* a store whose handle is shorter than its content.
|
|
1004
|
+
* Schema migrations the plugin contributes. Applied in declared order
|
|
1005
|
+
* on first install AND on every binary boot (idempotently, gated by
|
|
1006
|
+
* the migrations tracking table seeded by the first KernelPlugin
|
|
1007
|
+
* to populate this slot).
|
|
990
1008
|
*/
|
|
991
|
-
|
|
992
|
-
source: (row: Row) => string | null | undefined | Promise<string | null | undefined>;
|
|
993
|
-
};
|
|
994
|
-
[key: string]: unknown;
|
|
995
|
-
}
|
|
996
|
-
interface NamespaceDefinition {
|
|
997
|
-
namespace: string;
|
|
1009
|
+
migrations?: KernelMigration[];
|
|
998
1010
|
/**
|
|
999
|
-
*
|
|
1000
|
-
*
|
|
1001
|
-
*
|
|
1002
|
-
* substrate-recall reads `types[T].embed?.source` to contribute embedding
|
|
1003
|
-
* postWrite hooks). Populated by `defineNamespace`; absent on hand-crafted
|
|
1004
|
-
* `NamespaceDefinition` objects that bypass the SDK helper.
|
|
1011
|
+
* Names of the tables (in the store's schema) this plugin keeps data in.
|
|
1012
|
+
* `substrate-admin migrate-store` copies them alongside the engine's own
|
|
1013
|
+
* tables and refuses a source table that nothing declares.
|
|
1005
1014
|
*/
|
|
1006
|
-
|
|
1015
|
+
storeTables?: readonly string[];
|
|
1007
1016
|
/**
|
|
1008
|
-
*
|
|
1009
|
-
*
|
|
1017
|
+
* Filter operators contributed to the where-clause grammar. Each entry
|
|
1018
|
+
* registers a custom operator (e.g. `embeddingSimilar`) the engine's
|
|
1019
|
+
* query validator + substrate executor can dispatch to.
|
|
1010
1020
|
*/
|
|
1011
|
-
|
|
1021
|
+
filterOperators?: Record<string, FilterOperatorDef>;
|
|
1012
1022
|
/**
|
|
1013
|
-
*
|
|
1014
|
-
*
|
|
1015
|
-
* its kernel migrations; the engine creates none. `column` is a bare
|
|
1016
|
-
* identifier.
|
|
1023
|
+
* Top-level kernel route claims. Plugin reserves a path under /substrate/v1/.
|
|
1024
|
+
* First-claim wins; collision = boot refusal with actionable error.
|
|
1017
1025
|
*/
|
|
1018
|
-
|
|
1019
|
-
column: string;
|
|
1020
|
-
kind: 'boolean' | 'text';
|
|
1021
|
-
}>>;
|
|
1026
|
+
kernelRoutes?: KernelRouteClaim[];
|
|
1022
1027
|
/**
|
|
1023
|
-
*
|
|
1024
|
-
*
|
|
1025
|
-
* dataset handle it uses for resolver closures; wiring datasets into the
|
|
1026
|
-
* engine's `DatasetRegistry` is the app composition root's job.
|
|
1028
|
+
* Top-level MCP tool registrations. Tool name registers globally with
|
|
1029
|
+
* the MCP server (no /plugins/ prefix on MCP — tools are flat).
|
|
1027
1030
|
*/
|
|
1028
|
-
|
|
1029
|
-
}
|
|
1030
|
-
interface PluginLifecycleContext {
|
|
1031
|
+
mcpTools?: McpToolRegistration[];
|
|
1031
1032
|
/**
|
|
1032
|
-
*
|
|
1033
|
-
*
|
|
1034
|
-
*
|
|
1035
|
-
*
|
|
1036
|
-
* can also branch on it at runtime if a single helper handles both
|
|
1037
|
-
* lifecycle stages.
|
|
1033
|
+
* Frame-surface contribution. Claims one namespace in the engine registry
|
|
1034
|
+
* carrying resolver-backed query types + mutations — no dataset, no
|
|
1035
|
+
* autoCRUD. Dispatch/policy/idempotency/pack/schema/NL/MCP all inherit from
|
|
1036
|
+
* the normal frame path.
|
|
1038
1037
|
*/
|
|
1039
|
-
|
|
1038
|
+
kernelNamespace?: KernelNamespaceContribution;
|
|
1040
1039
|
/**
|
|
1041
|
-
*
|
|
1042
|
-
*
|
|
1043
|
-
*
|
|
1044
|
-
*
|
|
1040
|
+
* Post-register lifecycle hook. Fires after every KernelPlugin's
|
|
1041
|
+
* migrations + registrations resolve, BEFORE any coordinator plugin
|
|
1042
|
+
* composes. Use for resource priming the kernel needs before
|
|
1043
|
+
* coordinators arrive.
|
|
1045
1044
|
*/
|
|
1046
|
-
|
|
1045
|
+
onKernelReady?(ctx: KernelLifecycleContext): void | Promise<void>;
|
|
1047
1046
|
/**
|
|
1048
|
-
*
|
|
1049
|
-
*
|
|
1050
|
-
* applyField paths) can wire `engine.invalidateSchemaCaches(ns)` into
|
|
1051
|
-
* their replace flow. Engine construction completes before `onStart`
|
|
1052
|
-
* fires, so the handle is always live here.
|
|
1047
|
+
* Stops whatever `onKernelReady` started (timers, workers). The host calls
|
|
1048
|
+
* it before it closes the store, and may call it more than once.
|
|
1053
1049
|
*/
|
|
1054
|
-
|
|
1055
|
-
}
|
|
1056
|
-
/**
|
|
1057
|
-
* Engine-facing input shape for `query` / `mutate` / `subscribe` / `stream`.
|
|
1058
|
-
* Declared here (rather than in substrate-core) so plugin surfaces can reference
|
|
1059
|
-
* the engine's call shape as a type without importing the engine package —
|
|
1060
|
-
* substrate-types stays zero-runtime.
|
|
1061
|
-
*/
|
|
1062
|
-
interface FrameEntry {
|
|
1063
|
-
params?: Record<string, unknown>;
|
|
1064
|
-
fields?: Record<string, unknown>;
|
|
1050
|
+
onKernelStop?(): void | Promise<void>;
|
|
1065
1051
|
/**
|
|
1066
|
-
*
|
|
1067
|
-
*
|
|
1068
|
-
*
|
|
1052
|
+
* Contribute postWrite hooks for a coordinator namespace at install time.
|
|
1053
|
+
* Called by the engine ONCE per registered coordinator namespace, AFTER
|
|
1054
|
+
* substrate composition completes. Returned hooks are merged into the
|
|
1055
|
+
* type's effective postWrite chain alongside the type's own inline
|
|
1056
|
+
* `postWrite` (if any). Substrate-supplied hooks run AFTER the type's
|
|
1057
|
+
* inline hook in chain order.
|
|
1058
|
+
*
|
|
1059
|
+
* Use this to inspect `namespace.types[T].embed` and emit embedding
|
|
1060
|
+
* hooks per type without coupling the binary to plugin internals.
|
|
1061
|
+
*
|
|
1062
|
+
* Returns: a record mapping type names to PostWriteHook contributions.
|
|
1063
|
+
* Type names not in the record receive no contribution.
|
|
1069
1064
|
*/
|
|
1070
|
-
|
|
1065
|
+
contributePostWrites?: (namespace: NamespaceDefinition) => Record<string, PostWriteHook>;
|
|
1071
1066
|
}
|
|
1072
|
-
/**
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
*/
|
|
1083
|
-
type QualifiedFrame = Record<string, FrameEntry | Array<FrameEntry>>;
|
|
1084
|
-
interface EngineInput {
|
|
1085
|
-
frame: QualifiedFrame;
|
|
1086
|
-
ctx: Partial<RequestContext>;
|
|
1067
|
+
/** Single migration unit. */
|
|
1068
|
+
interface KernelMigration {
|
|
1069
|
+
/** Unique id within the plugin. Stored in migrations tracking table. */
|
|
1070
|
+
id: string;
|
|
1071
|
+
/** Apply migration. Called only when this migration's id is absent from the migrations table. */
|
|
1072
|
+
apply(ctx: KernelMigrationContext): Promise<void>;
|
|
1073
|
+
/** Inverse — drops what apply created. */
|
|
1074
|
+
rollback?(ctx: KernelMigrationContext): Promise<void>;
|
|
1075
|
+
}
|
|
1076
|
+
interface KernelMigrationContext {
|
|
1087
1077
|
/**
|
|
1088
|
-
*
|
|
1089
|
-
*
|
|
1090
|
-
* `abort` listener.
|
|
1078
|
+
* Direct SQL handle for postgres-backed deployments; null when the
|
|
1079
|
+
* binary is running against the in-memory dataset.
|
|
1091
1080
|
*/
|
|
1092
|
-
|
|
1081
|
+
sql: unknown | null;
|
|
1093
1082
|
/**
|
|
1094
|
-
*
|
|
1095
|
-
*
|
|
1096
|
-
*
|
|
1083
|
+
* How `rows` is stored: `'partitioned'` (LIST(namespace) then LIST(type)) or
|
|
1084
|
+
* `'legacy'` (one plain table). `null` on the in-memory dataset. PG refuses
|
|
1085
|
+
* a unique index on a partitioned parent unless it includes every partition
|
|
1086
|
+
* key column (`namespace`, `type`), so such DDL on `rows` must branch on this.
|
|
1097
1087
|
*/
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
readonly maxLimit: number;
|
|
1101
|
-
};
|
|
1088
|
+
layout: 'legacy' | 'partitioned' | null;
|
|
1089
|
+
metric(name: string, payload?: unknown): void;
|
|
1102
1090
|
}
|
|
1103
1091
|
/**
|
|
1104
|
-
*
|
|
1105
|
-
*
|
|
1092
|
+
* Where-clause operator extension.
|
|
1093
|
+
*
|
|
1094
|
+
* The operator's name is the Record key it's registered under in
|
|
1095
|
+
* `KernelPlugin.filterOperators`.
|
|
1096
|
+
*
|
|
1097
|
+
* `execute` is awaited by the pre-resolve pass (`@mnmal-ai/substrate-filter`'s
|
|
1098
|
+
* `preResolveExtensionOperatorsDetailed`) and must resolve to an ordered,
|
|
1099
|
+
* scored `{ hits: [{ id: string; score: number; vector?: number[] }, ...] }`
|
|
1100
|
+
* envelope; any other shape throws `ExtensionOperatorResultError`. Relevance
|
|
1101
|
+
* order is preserved in the rewritten `id IN`, and score/vector are surfaced
|
|
1102
|
+
* via the pre-pass meta side-channel for the datasets to stitch onto rows.
|
|
1106
1103
|
*/
|
|
1107
|
-
interface
|
|
1108
|
-
|
|
1104
|
+
interface FilterOperatorDef {
|
|
1105
|
+
validate(value: unknown): {
|
|
1106
|
+
ok: true;
|
|
1107
|
+
value: unknown;
|
|
1108
|
+
} | {
|
|
1109
|
+
ok: false;
|
|
1110
|
+
reason: string;
|
|
1111
|
+
};
|
|
1112
|
+
execute(value: unknown, context: unknown): Promise<unknown>;
|
|
1109
1113
|
/**
|
|
1110
|
-
*
|
|
1111
|
-
*
|
|
1112
|
-
*
|
|
1113
|
-
* `params`/`fields` and MUST NOT reach for a dataset handle. They are
|
|
1114
|
-
* structurally incapable of autoCRUD/storage — coordinators own data,
|
|
1115
|
-
* substrate surfaces own computation. Subscribe refuses these namespaces
|
|
1116
|
-
* with `subscription_unsupported_frame`.
|
|
1114
|
+
* Computed projection field names the operator contributes per hit (e.g.
|
|
1115
|
+
* `['_score', '_vector']`). The engine refuses a projection of one of these
|
|
1116
|
+
* fields unless its operator is in the where-clause.
|
|
1117
1117
|
*/
|
|
1118
|
-
|
|
1119
|
-
/** From `NamespaceDefinition.annotations`; surfaced on the schema, never hashed. */
|
|
1120
|
-
annotations?: Readonly<Record<string, unknown>>;
|
|
1118
|
+
computedFields?: string[];
|
|
1121
1119
|
}
|
|
1122
1120
|
/**
|
|
1123
|
-
*
|
|
1124
|
-
*
|
|
1125
|
-
*
|
|
1126
|
-
* engine
|
|
1121
|
+
* Query-only virtual type contributed by a substrate plugin.
|
|
1122
|
+
* `fields` reuses the FieldDef DSL so schema publication + client typing
|
|
1123
|
+
* work unchanged. The resolver receives the standard (ctx, args) pair; the
|
|
1124
|
+
* engine leaves `args.dataset` undefined for virtual namespaces — resolvers
|
|
1125
|
+
* compute from `params`/`fields` and MUST NOT reach for a dataset handle.
|
|
1127
1126
|
*/
|
|
1128
|
-
interface
|
|
1129
|
-
|
|
1130
|
-
|
|
1127
|
+
interface VirtualTypeDef {
|
|
1128
|
+
fields: Record<string, FieldDef>;
|
|
1129
|
+
resolver: RegistryEntryResolver;
|
|
1130
|
+
}
|
|
1131
|
+
/** Mutation contributed by a substrate plugin under its virtual namespace. */
|
|
1132
|
+
interface VirtualMutationDef {
|
|
1133
|
+
resolver: RegistryEntryResolver;
|
|
1134
|
+
/** Optional params schema published into the namespace schema. */
|
|
1135
|
+
params?: Record<string, FieldDef>;
|
|
1136
|
+
}
|
|
1137
|
+
/**
|
|
1138
|
+
* A substrate plugin's claimed frame surface.
|
|
1139
|
+
* Registered into the engine's namespaces map as `{ registry, virtual: true }`.
|
|
1140
|
+
* Collision posture mirrors KernelRouteClaim: second claim = boot refusal.
|
|
1141
|
+
*/
|
|
1142
|
+
interface KernelNamespaceContribution {
|
|
1143
|
+
namespace: string;
|
|
1144
|
+
types?: Record<string, VirtualTypeDef>;
|
|
1145
|
+
mutations?: Record<string, VirtualMutationDef>;
|
|
1146
|
+
}
|
|
1147
|
+
/** Top-level kernel route reservation. */
|
|
1148
|
+
interface KernelRouteClaim {
|
|
1149
|
+
path: string;
|
|
1150
|
+
method: HttpMethod;
|
|
1151
|
+
handler: HttpHandler;
|
|
1131
1152
|
/**
|
|
1132
|
-
*
|
|
1133
|
-
*
|
|
1134
|
-
*
|
|
1135
|
-
*
|
|
1136
|
-
*
|
|
1153
|
+
* Capability the host must check before dispatching, when auth is enabled.
|
|
1154
|
+
* REQUIRED — `'public'` is how a route opts out, so opting out is a
|
|
1155
|
+
* declaration rather than an omission.
|
|
1156
|
+
*
|
|
1157
|
+
* Do not make this optional: kernel routes mount via the host's raw
|
|
1158
|
+
* `server.route`, which applies no gate of its own, so a route with no
|
|
1159
|
+
* capability would get no auth wrapper at all.
|
|
1160
|
+
*/
|
|
1161
|
+
capability: 'read' | 'mutate' | 'admin' | 'public';
|
|
1162
|
+
}
|
|
1163
|
+
/** Top-level MCP tool registration. */
|
|
1164
|
+
interface McpToolRegistration {
|
|
1165
|
+
name: string;
|
|
1166
|
+
description: string;
|
|
1167
|
+
inputSchema: unknown;
|
|
1168
|
+
handler(input: unknown): Promise<unknown>;
|
|
1169
|
+
}
|
|
1170
|
+
interface KernelLifecycleContext {
|
|
1171
|
+
/**
|
|
1172
|
+
* Tagged-union discriminant. Differentiates this context from the
|
|
1173
|
+
* structurally-similar `PluginLifecycleContext`, which would otherwise be
|
|
1174
|
+
* interchangeable under structural typing.
|
|
1137
1175
|
*/
|
|
1138
|
-
|
|
1176
|
+
readonly lifecycle: 'kernel';
|
|
1139
1177
|
/**
|
|
1140
|
-
*
|
|
1141
|
-
*
|
|
1142
|
-
* `
|
|
1143
|
-
*
|
|
1144
|
-
*
|
|
1178
|
+
* Direct SQL handle; null when the binary runs against the in-memory
|
|
1179
|
+
* dataset. Same shape and same null semantics as
|
|
1180
|
+
* `KernelMigrationContext.sql`.
|
|
1181
|
+
*
|
|
1182
|
+
* For convergent, run-every-boot reconciliation: a migration runs only once
|
|
1183
|
+
* per `id`, so it cannot re-scope objects it already created.
|
|
1145
1184
|
*/
|
|
1146
|
-
|
|
1185
|
+
sql: unknown | null;
|
|
1147
1186
|
/**
|
|
1148
|
-
*
|
|
1149
|
-
*
|
|
1150
|
-
*
|
|
1151
|
-
*
|
|
1152
|
-
*
|
|
1187
|
+
* Namespaces DECLARED by the coordinator plugins this binary loaded, in
|
|
1188
|
+
* load order. Empty when none are configured.
|
|
1189
|
+
*
|
|
1190
|
+
* Declared, not installed: coordinators compose AFTER substrate, so the
|
|
1191
|
+
* engine has not yet registered them.
|
|
1192
|
+
*
|
|
1193
|
+
* Read these rather than adding a namespace knob to substrate config, which
|
|
1194
|
+
* could silently disagree with what the coordinator uses.
|
|
1153
1195
|
*/
|
|
1154
|
-
|
|
1196
|
+
coordinatorNamespaces: readonly string[];
|
|
1155
1197
|
/**
|
|
1156
|
-
*
|
|
1157
|
-
*
|
|
1158
|
-
* - federatedSchema() omits the ns
|
|
1159
|
-
* - query / mutate / subscribe against the ns return a per-key
|
|
1160
|
-
* `namespace_tombstoned` error
|
|
1161
|
-
* - existing live subscriptions on the ns are closed with a
|
|
1162
|
-
* `namespace_tombstoned` error frame
|
|
1198
|
+
* The same namespaces, attributed to the coordinator that declared each.
|
|
1199
|
+
* Declaration order preserved within a plugin.
|
|
1163
1200
|
*
|
|
1164
|
-
*
|
|
1165
|
-
*
|
|
1166
|
-
*
|
|
1167
|
-
*
|
|
1168
|
-
*
|
|
1201
|
+
* The two absent cases differ and consumers must branch on them:
|
|
1202
|
+
* - `undefined` — this host does not supply attribution (e.g. an older
|
|
1203
|
+
* binary); do not treat it as an empty list
|
|
1204
|
+
* - `[]` — attribution supplied, and there are no coordinators
|
|
1205
|
+
*
|
|
1206
|
+
* Filtering on `plugin === 'some-name'` makes the coordinator's `name` a
|
|
1207
|
+
* contract term: hold it in one constant shared with the coordinator and pin
|
|
1208
|
+
* it with a test, or a rename makes the filter silently match nothing.
|
|
1169
1209
|
*/
|
|
1170
|
-
|
|
1210
|
+
coordinatorNamespacesByPlugin?: readonly {
|
|
1211
|
+
plugin: string;
|
|
1212
|
+
namespace: string;
|
|
1213
|
+
}[];
|
|
1171
1214
|
/**
|
|
1172
|
-
*
|
|
1173
|
-
*
|
|
1174
|
-
*
|
|
1215
|
+
* Whether this boot may start background work (timers, backfills). False
|
|
1216
|
+
* when the host boots the store only to prepare or copy into it, as
|
|
1217
|
+
* `substrate-admin migrate-store` does: do convergent reconciliation, start
|
|
1218
|
+
* nothing that writes later.
|
|
1175
1219
|
*/
|
|
1176
|
-
|
|
1220
|
+
background: boolean;
|
|
1221
|
+
metric(name: string, payload?: unknown): void;
|
|
1222
|
+
}
|
|
1223
|
+
//#endregion
|
|
1224
|
+
//#region ../../packages/substrate-types/src/runtime-metrics.d.ts
|
|
1225
|
+
/** Per-key resolver latency and error counts, as served by GET /substrate/v1/metrics. */
|
|
1226
|
+
interface ResolverStat {
|
|
1227
|
+
kind: 'query' | 'mutate';
|
|
1228
|
+
calls: number;
|
|
1229
|
+
errors: number;
|
|
1230
|
+
p50Ms: number;
|
|
1231
|
+
p99Ms: number;
|
|
1232
|
+
maxMs: number;
|
|
1233
|
+
lastAt: string;
|
|
1234
|
+
}
|
|
1235
|
+
interface ProcessStat {
|
|
1236
|
+
rssMB: number;
|
|
1237
|
+
heapUsedMB: number;
|
|
1238
|
+
cpuUserMs: number;
|
|
1239
|
+
cpuSystemMs: number;
|
|
1240
|
+
eventLoopLagP99Ms: number;
|
|
1241
|
+
inFlight: number;
|
|
1242
|
+
}
|
|
1243
|
+
interface SlowRequest {
|
|
1244
|
+
key: string;
|
|
1245
|
+
kind: 'query' | 'mutate';
|
|
1246
|
+
durationMs: number;
|
|
1247
|
+
at: string;
|
|
1248
|
+
}
|
|
1249
|
+
interface RuntimeErrorEntry {
|
|
1250
|
+
at: string;
|
|
1251
|
+
op: 'query' | 'mutate';
|
|
1252
|
+
key: string;
|
|
1253
|
+
code: string;
|
|
1254
|
+
message: string;
|
|
1255
|
+
/** The verified identity's id, or `'anonymous'`. */
|
|
1256
|
+
caller?: string;
|
|
1257
|
+
/** Present when consecutive identical `(unknown)` failures were merged into this entry. */
|
|
1258
|
+
count?: number;
|
|
1259
|
+
}
|
|
1260
|
+
interface RuntimeErrors {
|
|
1261
|
+
since: string;
|
|
1262
|
+
errors: RuntimeErrorEntry[];
|
|
1263
|
+
}
|
|
1264
|
+
interface ResolveObservation {
|
|
1265
|
+
key: string;
|
|
1266
|
+
kind: 'query' | 'mutate';
|
|
1267
|
+
durationMs: number;
|
|
1268
|
+
ok: boolean;
|
|
1269
|
+
code?: string;
|
|
1270
|
+
message?: string;
|
|
1271
|
+
/** The verified identity's id; absent for an anonymous caller. */
|
|
1272
|
+
caller?: string;
|
|
1273
|
+
}
|
|
1274
|
+
/**
|
|
1275
|
+
* One type's table and index statistics, for the index advisor. Counters are
|
|
1276
|
+
* cumulative since the server's statistics were last reset. Row counts are
|
|
1277
|
+
* estimates. A type ABSENT from a `typeStats()` result is unknown, not empty:
|
|
1278
|
+
* never read its absence as 0 rows (a sampled store can miss a small type).
|
|
1279
|
+
*/
|
|
1280
|
+
interface TypeStats {
|
|
1281
|
+
namespace: string;
|
|
1282
|
+
type: string;
|
|
1283
|
+
liveRows: number;
|
|
1284
|
+
insertsTotal: number;
|
|
1285
|
+
updatesTotal: number;
|
|
1286
|
+
deletesTotal: number;
|
|
1287
|
+
indexes: Array<{
|
|
1288
|
+
name: string;
|
|
1289
|
+
scans: number;
|
|
1290
|
+
/** The last scan (ISO), or null when none since the statistics were last reset. */
|
|
1291
|
+
lastScanAt: string | null;
|
|
1292
|
+
sizeBytes: number;
|
|
1293
|
+
unique: boolean;
|
|
1294
|
+
/** Built or adopted by the engine's index reconciler for this type. */
|
|
1295
|
+
engineOwned: boolean;
|
|
1296
|
+
}>;
|
|
1297
|
+
}
|
|
1298
|
+
/** What GET /substrate/v1/metrics returns. The new fields are optional so an older server still type-checks. */
|
|
1299
|
+
interface RuntimeMetrics {
|
|
1300
|
+
since: string;
|
|
1301
|
+
requests: {
|
|
1302
|
+
query: {
|
|
1303
|
+
total: number;
|
|
1304
|
+
ok: number;
|
|
1305
|
+
error: number;
|
|
1306
|
+
durationMsSum: number;
|
|
1307
|
+
durationMsMax: number;
|
|
1308
|
+
};
|
|
1309
|
+
mutate: {
|
|
1310
|
+
total: number;
|
|
1311
|
+
ok: number;
|
|
1312
|
+
error: number;
|
|
1313
|
+
durationMsSum: number;
|
|
1314
|
+
durationMsMax: number;
|
|
1315
|
+
};
|
|
1316
|
+
};
|
|
1317
|
+
subscriptions: {
|
|
1318
|
+
active: number;
|
|
1319
|
+
maxQueueDepth?: number;
|
|
1320
|
+
entries?: {
|
|
1321
|
+
id: string;
|
|
1322
|
+
ns: string;
|
|
1323
|
+
type: string;
|
|
1324
|
+
queueLength: number;
|
|
1325
|
+
}[];
|
|
1326
|
+
};
|
|
1327
|
+
events: Record<string, number>;
|
|
1328
|
+
resolvers?: Record<string, ResolverStat>;
|
|
1329
|
+
process?: ProcessStat;
|
|
1330
|
+
slowest?: SlowRequest[];
|
|
1331
|
+
}
|
|
1332
|
+
//#endregion
|
|
1333
|
+
//#region ../../packages/substrate-types/src/dataset.d.ts
|
|
1334
|
+
/**
|
|
1335
|
+
* Handles yielded by `Dataset.withScopedTransaction`. Contains the
|
|
1336
|
+
* tx-bound dataset for every requested namespace plus (optionally) a
|
|
1337
|
+
* tx-bound edges store enlisted into the same substrate transaction.
|
|
1338
|
+
* See `withScopedTransaction` JSDoc for the `edges` contract.
|
|
1339
|
+
*/
|
|
1340
|
+
interface TxScopeHandles {
|
|
1341
|
+
datasets: Map<string, Dataset>;
|
|
1342
|
+
edges: EdgesStore | null;
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* A leaf predicate on a single field. At most one operator per call-site is
|
|
1346
|
+
* used in practice (e.g. `{ age: { gt: 18 } }`), but the shape admits several
|
|
1347
|
+
* operators so `{ age: { gte: 10, lt: 100 } }` can express a half-open range
|
|
1348
|
+
* without the caller reaching for an explicit `AND`. Semantics: the leaf
|
|
1349
|
+
* matches when EVERY specified operator matches (implicit AND within a leaf).
|
|
1350
|
+
*
|
|
1351
|
+
* `eq`/`neq` accept `null` to address IS NULL / IS NOT NULL semantics at the
|
|
1352
|
+
* substrate layer — the validator enforces `null` only on those two operators.
|
|
1353
|
+
*/
|
|
1354
|
+
interface FieldPredicate {
|
|
1355
|
+
eq?: number | string | boolean | Date | null;
|
|
1356
|
+
neq?: number | string | boolean | Date | null;
|
|
1357
|
+
gt?: number | string | Date;
|
|
1358
|
+
gte?: number | string | Date;
|
|
1359
|
+
lt?: number | string | Date;
|
|
1360
|
+
lte?: number | string | Date;
|
|
1361
|
+
between?: [number | string | Date, number | string | Date];
|
|
1362
|
+
in?: Array<number | string | boolean | Date>;
|
|
1363
|
+
nin?: Array<number | string | boolean | Date>;
|
|
1364
|
+
contains?: string;
|
|
1365
|
+
startsWith?: string;
|
|
1366
|
+
endsWith?: string;
|
|
1367
|
+
has?: number | string | boolean | Date | null;
|
|
1368
|
+
path?: string[];
|
|
1369
|
+
}
|
|
1370
|
+
/**
|
|
1371
|
+
* A where clause is either a logical combinator OR a map of field names to
|
|
1372
|
+
* predicates / nested where clauses (for struct fields). Field names and the
|
|
1373
|
+
* three combinator keys (AND/OR/NOT) share a namespace — `AND`/`OR`/`NOT` are
|
|
1374
|
+
* reserved and must not collide with field names declared in any type.
|
|
1375
|
+
*/
|
|
1376
|
+
type WhereClause = {
|
|
1377
|
+
AND: WhereClause[];
|
|
1378
|
+
} | {
|
|
1379
|
+
OR: WhereClause[];
|
|
1380
|
+
} | {
|
|
1381
|
+
NOT: WhereClause;
|
|
1382
|
+
} | {
|
|
1383
|
+
[field: string]: FieldPredicate | WhereClause | unknown;
|
|
1384
|
+
};
|
|
1385
|
+
/**
|
|
1386
|
+
* OrderBy clause. One entry per field, direction per field. Engine appends an
|
|
1387
|
+
* implicit `{ id: 'asc' }` when callers omit `id` as the final tie-breaker — a
|
|
1388
|
+
* deterministic tie-break is what makes cursor pagination stable across pages.
|
|
1389
|
+
*/
|
|
1390
|
+
interface OrderByClause {
|
|
1391
|
+
[field: string]: 'asc' | 'desc';
|
|
1392
|
+
}
|
|
1393
|
+
interface ReadParams {
|
|
1394
|
+
id?: Id;
|
|
1395
|
+
ids?: Id[];
|
|
1396
|
+
includeDeleted?: boolean;
|
|
1397
|
+
where?: WhereClause;
|
|
1398
|
+
orderBy?: OrderByClause[];
|
|
1399
|
+
limit?: number;
|
|
1177
1400
|
/**
|
|
1178
|
-
*
|
|
1179
|
-
*
|
|
1180
|
-
* the rows are gone and before it commits, so a throw rolls the drop back.
|
|
1181
|
-
* Returns a disposer.
|
|
1401
|
+
* Opaque cursor produced by a prior `read()` call. Base64url-encoded JSON
|
|
1402
|
+
* (server-built, server-consumed). Callers should treat it as a black box.
|
|
1182
1403
|
*/
|
|
1183
|
-
|
|
1184
|
-
/** The participants `addDropParticipant` registered, for the drop route. */
|
|
1185
|
-
dropParticipants(): readonly DropParticipant[];
|
|
1404
|
+
cursor?: string;
|
|
1186
1405
|
/**
|
|
1187
|
-
*
|
|
1188
|
-
*
|
|
1189
|
-
*
|
|
1190
|
-
*
|
|
1406
|
+
* Optional caller-supplied projection hint. Substrates MAY use this to
|
|
1407
|
+
* narrow the body keys they materialise from storage — e.g. PgDataset
|
|
1408
|
+
* builds a `jsonb_build_object(...)` projection over only the requested
|
|
1409
|
+
* top-level body keys, saving wire + parse bandwidth.
|
|
1410
|
+
*
|
|
1411
|
+
* When present, ONLY top-level entries are honoured (the value can be `true`
|
|
1412
|
+
* or a nested FieldInput-shaped object — the substrate treats both as
|
|
1413
|
+
* "include this key"). Nested projection inside JSONB sub-trees is intentionally
|
|
1414
|
+
* out of scope; the pack codec already prunes deeper. Row-metadata-style keys
|
|
1415
|
+
* (`_metadata`, `shortId`, `_deletedAt`, `id`) are populated from dedicated
|
|
1416
|
+
* columns regardless of `fields` and never need to be included here.
|
|
1417
|
+
*
|
|
1418
|
+
* When absent, substrates return the full body, for raw `dataset.read()`
|
|
1419
|
+
* callers (sweep, compose registry, migration scripts) that don't know which
|
|
1420
|
+
* body keys they'll need downstream.
|
|
1421
|
+
*
|
|
1422
|
+
* Substrates MAY ignore this hint — it's a perf optimisation, not a contract.
|
|
1423
|
+
* Memory dataset, for instance, has no wire to save and ignores it.
|
|
1191
1424
|
*/
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1425
|
+
fields?: {
|
|
1426
|
+
[field: string]: true | unknown;
|
|
1427
|
+
};
|
|
1428
|
+
}
|
|
1429
|
+
/**
|
|
1430
|
+
* Paged result of a `read()` call. `rows` is always present; `nextCursor` is
|
|
1431
|
+
* present iff `hasMore` is `true` and the current page is non-empty.
|
|
1432
|
+
*/
|
|
1433
|
+
interface ReadResult {
|
|
1434
|
+
rows: Row[];
|
|
1435
|
+
nextCursor?: string;
|
|
1436
|
+
hasMore: boolean;
|
|
1437
|
+
}
|
|
1438
|
+
interface WriteResult {
|
|
1439
|
+
written: number;
|
|
1195
1440
|
/**
|
|
1196
|
-
*
|
|
1197
|
-
*
|
|
1198
|
-
* without taking a runtime dep on the concrete Registry class in substrate-core.
|
|
1441
|
+
* One per written row, in input order: a supplied id verbatim, otherwise
|
|
1442
|
+
* the id the dataset assigned. Under `insertOnly`, skipped rows are absent.
|
|
1199
1443
|
*/
|
|
1200
|
-
|
|
1444
|
+
ids: Id[];
|
|
1201
1445
|
/**
|
|
1202
|
-
*
|
|
1203
|
-
*
|
|
1204
|
-
* `
|
|
1205
|
-
*
|
|
1206
|
-
*
|
|
1207
|
-
*
|
|
1208
|
-
* Substrate selection remains the host's responsibility — this method only
|
|
1209
|
-
* exposes the lookup. Plugins that hot-add namespaces (substrate-proposals'
|
|
1210
|
-
* `propose_namespace`) MUST require the host to register the dataset before
|
|
1211
|
-
* the call; this method returns `undefined` for unregistered namespaces.
|
|
1446
|
+
* Merged rows after upsert — substrates populate this with the post-merge
|
|
1447
|
+
* row state so autoCRUD update can skip its post-read. Order
|
|
1448
|
+
* matches `ids` (one entry per input row). Substrates that can't compute
|
|
1449
|
+
* the merged shape cheaply may emit input rows verbatim; consumers must
|
|
1450
|
+
* not rely on body keys absent from the input being filled in.
|
|
1212
1451
|
*/
|
|
1213
|
-
|
|
1452
|
+
rows: Row[];
|
|
1214
1453
|
}
|
|
1215
1454
|
/**
|
|
1216
|
-
*
|
|
1217
|
-
*
|
|
1218
|
-
* `
|
|
1219
|
-
* `
|
|
1220
|
-
*
|
|
1455
|
+
* Optional 3rd-arg to `dataset.write` / `tx.write`. When the incoming row's
|
|
1456
|
+
* `_metadata` is absent, the substrate auto-fills it from these hints:
|
|
1457
|
+
* - `now` → both `createdAt` and `updatedAt` (defaults to `new Date()`)
|
|
1458
|
+
* - `identity` → both `createdBy` and `updatedBy` (defaults to `null`)
|
|
1459
|
+
*
|
|
1460
|
+
* On upsert collision, `created_*` columns preserve existing values (immutability);
|
|
1461
|
+
* `updated_*` columns adopt the new values (bump intent). Engine path
|
|
1462
|
+
* (`autocrud-create`/`autocrud-update`) constructs full `_metadata` itself —
|
|
1463
|
+
* this auto-fill is purely a safety net for raw-write bypass paths.
|
|
1221
1464
|
*/
|
|
1222
|
-
interface
|
|
1223
|
-
|
|
1224
|
-
|
|
1465
|
+
interface WriteOptions {
|
|
1466
|
+
readonly identity?: ActorIdentity;
|
|
1467
|
+
readonly now?: Date;
|
|
1225
1468
|
/**
|
|
1226
|
-
*
|
|
1227
|
-
*
|
|
1228
|
-
*
|
|
1469
|
+
* Insert only: a row whose id already exists (live or tombstoned) is left
|
|
1470
|
+
* untouched and omitted from the result's `ids` and `rows`. The existence
|
|
1471
|
+
* check and the insert are one atomic step.
|
|
1229
1472
|
*/
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1473
|
+
readonly insertOnly?: boolean;
|
|
1474
|
+
}
|
|
1475
|
+
interface DeleteResult {
|
|
1476
|
+
deleted: number;
|
|
1477
|
+
ids: Id[];
|
|
1478
|
+
}
|
|
1479
|
+
interface DropResult {
|
|
1480
|
+
/** Number of rows physically removed. */
|
|
1481
|
+
rowsDeleted: number;
|
|
1482
|
+
/** Number of short-id allocations physically removed. */
|
|
1483
|
+
shortIdsDeleted: number;
|
|
1235
1484
|
}
|
|
1236
1485
|
/**
|
|
1237
|
-
*
|
|
1486
|
+
* Dataset-declared filter capabilities. §5.2 capability negotiation: the engine
|
|
1487
|
+
* validates every where-clause leaf against `operators[fieldKind]` and rejects
|
|
1488
|
+
* ops the dataset can't execute rather than returning a silently wrong result.
|
|
1238
1489
|
*/
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
/**
|
|
1243
|
-
|
|
1244
|
-
/**
|
|
1245
|
-
|
|
1490
|
+
type FieldKind = 'scalar.number' | 'scalar.string' | 'scalar.boolean' | 'scalar.date' | 'scalar.json' | 'crossRef' | 'list';
|
|
1491
|
+
interface FilterCapabilities {
|
|
1492
|
+
operators: Record<FieldKind, string[]>;
|
|
1493
|
+
/** Does the dataset honour `orderBy`? */
|
|
1494
|
+
orderBy: boolean;
|
|
1495
|
+
/** Does the dataset honour cursor-based pagination? */
|
|
1496
|
+
cursor: boolean;
|
|
1246
1497
|
}
|
|
1247
|
-
interface
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
intervalMs: number;
|
|
1252
|
-
/** Fire once at registration in addition to the interval ticks. Default false. */
|
|
1253
|
-
runOnStart?: boolean;
|
|
1254
|
-
/** Handler — receives a context with engine access for read/write work. */
|
|
1255
|
-
handler(ctx: ScheduledTaskContext): void | Promise<void>;
|
|
1498
|
+
interface UpdateFieldRef {
|
|
1499
|
+
ns?: string;
|
|
1500
|
+
type: string;
|
|
1501
|
+
id: Id;
|
|
1256
1502
|
}
|
|
1257
1503
|
/**
|
|
1258
|
-
*
|
|
1504
|
+
* Outcome of a `conditionalUpdate` call (spec §5.12 Phase 2 / Dispatch B).
|
|
1505
|
+
*
|
|
1506
|
+
* `matched: true` when the row existed AND every key in `match` equalled the
|
|
1507
|
+
* row's stored value at the moment of the update — the substrate applied
|
|
1508
|
+
* `updates` and returned the merged row. `matched: false` when the row was
|
|
1509
|
+
* missing or at least one match condition failed; in both cases no write landed
|
|
1510
|
+
* and `row` is `null`.
|
|
1259
1511
|
*/
|
|
1260
|
-
interface
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
/** Engine handle for plugin-scoped routes that need to dispatch back through the engine. */
|
|
1264
|
-
engine: EngineHandle;
|
|
1265
|
-
/** Structured metric sink — fire-and-forget. */
|
|
1266
|
-
metric(name: string, payload?: unknown): void;
|
|
1512
|
+
interface ConditionalUpdateResult {
|
|
1513
|
+
matched: boolean;
|
|
1514
|
+
row: Row | null;
|
|
1267
1515
|
}
|
|
1268
|
-
interface
|
|
1269
|
-
|
|
1270
|
-
namespaces: NamespaceDefinition[];
|
|
1516
|
+
interface Dataset {
|
|
1517
|
+
readonly namespace: string;
|
|
1271
1518
|
/**
|
|
1272
|
-
*
|
|
1273
|
-
*
|
|
1519
|
+
* Stable identifier for the storage substrate this dataset wraps. Two
|
|
1520
|
+
* datasets share a substrate when their `txScopeId` values are `===`
|
|
1521
|
+
* — they can participate in a single transaction via the engine's
|
|
1522
|
+
* substrate-coalescing path (spec §5.3, enabling cross-namespace G1
|
|
1523
|
+
* batched mutations when all frames target the same substrate).
|
|
1274
1524
|
*
|
|
1275
|
-
*
|
|
1276
|
-
*
|
|
1277
|
-
*
|
|
1278
|
-
*
|
|
1279
|
-
*
|
|
1525
|
+
* Convention:
|
|
1526
|
+
* - pg: derived from the `postgres.Sql` pool reference identity. All
|
|
1527
|
+
* `PgDataset` instances constructed against the same `sql`
|
|
1528
|
+
* handle share one `txScopeId`.
|
|
1529
|
+
* - memory: unique per `MemoryDataset` instance by default (each
|
|
1530
|
+
* instance owns its own `Map`). Test harnesses that want shared-
|
|
1531
|
+
* memory semantics can opt in via an explicit constructor option.
|
|
1280
1532
|
*
|
|
1281
|
-
*
|
|
1282
|
-
*
|
|
1533
|
+
* Datasets with different `txScopeId` values CANNOT participate in a
|
|
1534
|
+
* single transaction — the engine refuses mixed-substrate batched
|
|
1535
|
+
* mutates with `cross_tx_scope_refused`.
|
|
1283
1536
|
*/
|
|
1284
|
-
|
|
1537
|
+
readonly txScopeId: string;
|
|
1538
|
+
read(type: string, params: ReadParams): Promise<ReadResult>;
|
|
1539
|
+
write(type: string, rows: WriteRow[], options?: WriteOptions): Promise<WriteResult>;
|
|
1540
|
+
tombstone(type: string, ids: Id[]): Promise<DeleteResult>;
|
|
1541
|
+
purge(type: string, ids: Id[]): Promise<DeleteResult>;
|
|
1285
1542
|
/**
|
|
1286
|
-
*
|
|
1287
|
-
*
|
|
1288
|
-
*
|
|
1289
|
-
*
|
|
1290
|
-
*
|
|
1291
|
-
*
|
|
1543
|
+
* Physically delete every row of `type` within this dataset's namespace,
|
|
1544
|
+
* plus every short-id allocation for that type. Ignores `_deletedAt` /
|
|
1545
|
+
* `archived` state — drop is hard-delete regardless of row status.
|
|
1546
|
+
*
|
|
1547
|
+
* MUST run inside a caller-supplied `withTransaction` if atomicity with
|
|
1548
|
+
* edge cleanup / audit emission is required. When called standalone,
|
|
1549
|
+
* implementations MUST still execute the row-delete + short-id-delete as
|
|
1550
|
+
* a single atomic step against the substrate.
|
|
1551
|
+
*
|
|
1552
|
+
* Returns the exact counts physically removed. If the type has no rows,
|
|
1553
|
+
* returns zeros without error.
|
|
1554
|
+
*
|
|
1555
|
+
* Storage-level primitive: operates on any (namespace, type) rows present
|
|
1556
|
+
* in storage regardless of whether the type is currently known to the
|
|
1557
|
+
* engine's registry.
|
|
1292
1558
|
*/
|
|
1293
|
-
|
|
1559
|
+
dropType(type: string): Promise<DropResult>;
|
|
1294
1560
|
/**
|
|
1295
|
-
*
|
|
1296
|
-
*
|
|
1297
|
-
*
|
|
1561
|
+
* Physically delete every row AND short-id for `this.namespace`, across
|
|
1562
|
+
* every type. Equivalent to calling `dropType` for each distinct type
|
|
1563
|
+
* seen in storage, but substrate impls SHOULD use a single bulk DELETE
|
|
1564
|
+
* where available (pg: `DELETE FROM rows WHERE namespace = $1`).
|
|
1565
|
+
*
|
|
1566
|
+
* Same atomicity rules as `dropType`. Same "ignores archived state".
|
|
1298
1567
|
*/
|
|
1299
|
-
|
|
1568
|
+
dropNamespace(): Promise<DropResult>;
|
|
1569
|
+
updateField(ref: UpdateFieldRef, fieldName: string, value: unknown): Promise<void>;
|
|
1300
1570
|
/**
|
|
1301
|
-
*
|
|
1302
|
-
*
|
|
1303
|
-
*
|
|
1304
|
-
*
|
|
1305
|
-
* they came up — late-loading plugins that depended on earlier ones get
|
|
1306
|
-
* to release their handles first.
|
|
1571
|
+
* Atomic compare-and-update by PK. If the row `(type, id)` exists AND every
|
|
1572
|
+
* `(field, value)` pair in `match` equals the stored row's field, apply the
|
|
1573
|
+
* `updates` map and return `{ matched: true, row: mergedRow }`. Otherwise
|
|
1574
|
+
* write nothing and return `{ matched: false, row: null }`.
|
|
1307
1575
|
*
|
|
1308
|
-
*
|
|
1309
|
-
* -
|
|
1310
|
-
*
|
|
1311
|
-
*
|
|
1312
|
-
* -
|
|
1313
|
-
*
|
|
1576
|
+
* Semantics:
|
|
1577
|
+
* - Equality is strict (`===`) for scalars; `Date` compares by
|
|
1578
|
+
* `.getTime()`; `null` matches only a stored `null` (including
|
|
1579
|
+
* intentionally-null fields set via `updateField`), never an absent field.
|
|
1580
|
+
* - A missing row fails the match even when `match` is empty — the
|
|
1581
|
+
* operation is "update this existing row if predicate holds".
|
|
1582
|
+
* - `updates` is merged onto the stored row the same way `write()` upserts
|
|
1583
|
+
* merge: each key overwrites; omitted keys are preserved. `_deletedAt`
|
|
1584
|
+
* is reserved and silently skipped (matches `write()`'s sanitisation).
|
|
1585
|
+
* - The whole check+write is atomic against other dataset operations.
|
|
1586
|
+
* Implementations MUST NOT yield between the predicate test and the
|
|
1587
|
+
* write. Pg uses `UPDATE ... WHERE <match> RETURNING *` inside the
|
|
1588
|
+
* caller's enclosing `withTransaction`; memory runs the whole body
|
|
1589
|
+
* synchronously within the `async` microtask.
|
|
1314
1590
|
*
|
|
1315
|
-
*
|
|
1316
|
-
*
|
|
1317
|
-
*
|
|
1318
|
-
* onStop calls, so plugins can do final writes (e.g. an audit row
|
|
1319
|
-
* recording the shutdown). After all onStop hooks resolve, the server
|
|
1320
|
-
* stops accepting connections and the substrate's `closeStore()` runs.
|
|
1591
|
+
* Motivating use case: the `_system` register route consumes a one-shot
|
|
1592
|
+
* invite — the invite row's `consumedAt` must transition from `null` to a
|
|
1593
|
+
* timestamp exactly once, even under concurrent registrations.
|
|
1321
1594
|
*
|
|
1322
|
-
*
|
|
1595
|
+
* Metadata: a successful match unconditionally bumps `updatedAt` to `now`
|
|
1596
|
+
* (a server-set `now()` when omitted) and stamps `updatedBy` with the supplied `actor` (or
|
|
1597
|
+
* a `{ kind: 'system', id: 'anonymous' }` sentinel when omitted) — same
|
|
1598
|
+
* semantics as `write()`'s upsert path, so recency-ranked recall sees the
|
|
1599
|
+
* change. `createdAt` / `createdBy` are preserved.
|
|
1323
1600
|
*/
|
|
1324
|
-
|
|
1601
|
+
conditionalUpdate(type: string, id: Id, match: Record<string, unknown>, updates: Record<string, unknown>, actor?: ActorIdentity, now?: Date): Promise<ConditionalUpdateResult>;
|
|
1602
|
+
readonly capabilities: FilterCapabilities;
|
|
1325
1603
|
/**
|
|
1326
|
-
*
|
|
1327
|
-
*
|
|
1328
|
-
*
|
|
1604
|
+
* Run `fn` while holding a mutual-exclusion lock named `key`, waiting until
|
|
1605
|
+
* it is free. Excludes every other holder of the same key on this substrate:
|
|
1606
|
+
* pg holds a session advisory lock, so the exclusion spans processes sharing
|
|
1607
|
+
* the store's database schema; memory excludes holders on this dataset instance only.
|
|
1608
|
+
* Released when `fn` settles, whether it resolves or throws, except on a
|
|
1609
|
+
* pg tx-bound dataset, where it is held until the transaction ends so the
|
|
1610
|
+
* next holder sees what this one committed. Not re-entrant:
|
|
1611
|
+
* re-acquiring a held key inside `fn` waits forever.
|
|
1612
|
+
*
|
|
1613
|
+
* OPTIONAL. Callers feature-detect and fall back to a process-local lock.
|
|
1614
|
+
*
|
|
1615
|
+
* @returns whatever `fn` resolves to
|
|
1616
|
+
* @throws whatever `fn` throws, after the lock is released
|
|
1329
1617
|
*/
|
|
1330
|
-
|
|
1618
|
+
withAdvisoryLock?<T>(key: string, fn: () => Promise<T>): Promise<T>;
|
|
1331
1619
|
/**
|
|
1332
|
-
*
|
|
1333
|
-
*
|
|
1334
|
-
*
|
|
1620
|
+
* Prepare dedicated storage for `type` in this namespace, moving any rows
|
|
1621
|
+
* already written for it there. Idempotent, and safe beside concurrent
|
|
1622
|
+
* reads and writes: no row is lost, duplicated or seen twice. That holds for
|
|
1623
|
+
* READ COMMITTED readers; a REPEATABLE READ snapshot taken before the move
|
|
1624
|
+
* can miss the moved rows. Also builds the type's indexes, including its
|
|
1625
|
+
* declared `indexes`, and throws when a unique one cannot be built.
|
|
1335
1626
|
*
|
|
1336
|
-
*
|
|
1337
|
-
*
|
|
1338
|
-
*
|
|
1627
|
+
* `indexes` picks which: `unique` builds only the unique ones and leaves the
|
|
1628
|
+
* rest `pending`, for a later `nonUnique` call to build. `signal` cancels an
|
|
1629
|
+
* in-flight non-unique build, which the next call retries. Default `all`.
|
|
1630
|
+
*
|
|
1631
|
+
* OPTIONAL. Backends without per-type storage omit it.
|
|
1339
1632
|
*/
|
|
1340
|
-
|
|
1633
|
+
ensureTypeStorage?(type: string, opts?: {
|
|
1634
|
+
indexes?: 'all' | 'unique' | 'nonUnique';
|
|
1635
|
+
signal?: AbortSignal;
|
|
1636
|
+
}): Promise<void>;
|
|
1341
1637
|
/**
|
|
1342
|
-
*
|
|
1343
|
-
*
|
|
1344
|
-
*
|
|
1345
|
-
*
|
|
1346
|
-
* this to teach agents about the plugin's types, write flows, or any
|
|
1347
|
-
* plugin-specific grammar a cold-start agent would otherwise have to
|
|
1348
|
-
* discover per endpoint. The shape is free-form — the binary round-trips
|
|
1349
|
-
* it verbatim.
|
|
1638
|
+
* Where `type`'s rows live and how its declared indexes stand, read from the
|
|
1639
|
+
* catalog without building anything.
|
|
1640
|
+
*
|
|
1641
|
+
* OPTIONAL. Backends without per-type storage omit it.
|
|
1350
1642
|
*/
|
|
1351
|
-
|
|
1352
|
-
}
|
|
1353
|
-
/**
|
|
1354
|
-
* Substrate-tier plugin. Augments the kernel rather than the agent-facing
|
|
1355
|
-
* surface. Composes BEFORE coordinator plugins so coordinators can rely on
|
|
1356
|
-
* substrate registrations being live.
|
|
1357
|
-
*
|
|
1358
|
-
* SubstratePlugins do NOT own data namespaces — that's a CoordinatorPlugin
|
|
1359
|
-
* concern (a substrate may claim a virtual `kernelNamespace`).
|
|
1360
|
-
*/
|
|
1361
|
-
interface KernelPlugin {
|
|
1362
|
-
/** Unique plugin name. Used in metric labels + collision detection. */
|
|
1363
|
-
name: string;
|
|
1643
|
+
describeStorage?(type: string): Promise<TypeStorageReport>;
|
|
1364
1644
|
/**
|
|
1365
|
-
*
|
|
1366
|
-
*
|
|
1367
|
-
* `
|
|
1368
|
-
*
|
|
1369
|
-
*
|
|
1645
|
+
* The database-wide storage layout and what an operator should look at:
|
|
1646
|
+
* rows parked in DEFAULT partitions, indexes and CHECK constraints on the
|
|
1647
|
+
* `rows` parent, indexes this process has yet to build or failed to build,
|
|
1648
|
+
* and a partition count over `partitionBudget`; and, as `info`, engine
|
|
1649
|
+
* indexes no longer declared and open index recommendations. Never changes
|
|
1650
|
+
* anything.
|
|
1651
|
+
*
|
|
1652
|
+
* OPTIONAL. Backends without per-type storage omit it.
|
|
1370
1653
|
*/
|
|
1371
|
-
|
|
1654
|
+
storageReport?(opts: {
|
|
1655
|
+
partitionBudget: number;
|
|
1656
|
+
}): Promise<StorageReport>;
|
|
1372
1657
|
/**
|
|
1373
|
-
*
|
|
1374
|
-
*
|
|
1375
|
-
*
|
|
1376
|
-
*
|
|
1658
|
+
* Table and index statistics for every type the store holds, every
|
|
1659
|
+
* namespace, read from the database's own statistics. Estimates, never a
|
|
1660
|
+
* count. Never changes anything.
|
|
1661
|
+
*
|
|
1662
|
+
* OPTIONAL. Backends without such statistics omit it.
|
|
1377
1663
|
*/
|
|
1378
|
-
|
|
1664
|
+
typeStats?(): Promise<TypeStats[]>;
|
|
1379
1665
|
/**
|
|
1380
|
-
*
|
|
1381
|
-
*
|
|
1382
|
-
*
|
|
1666
|
+
* The indexes the engine wants for `namespace`/`type` (any namespace, not
|
|
1667
|
+
* only this one), with each one's leading keys, and the type's size hint.
|
|
1668
|
+
* Undefined when the type is unknown. Never changes anything.
|
|
1669
|
+
*
|
|
1670
|
+
* OPTIONAL. Backends that build no indexes omit it.
|
|
1383
1671
|
*/
|
|
1384
|
-
|
|
1672
|
+
indexPlan?(type: string, namespace: string): Promise<IndexPlan | undefined>;
|
|
1385
1673
|
/**
|
|
1386
|
-
*
|
|
1387
|
-
*
|
|
1388
|
-
|
|
1389
|
-
|
|
1674
|
+
* Drop index `name` of this namespace's `type`, a signed `propose_index`
|
|
1675
|
+
* remove. Only an engine-owned, non-unique index the engine no longer wants
|
|
1676
|
+
* (a leftover or a retracted declaration) is dropped; anything else is
|
|
1677
|
+
* refused with an error whose `code` is `unknown_index`, `not_engine_owned`,
|
|
1678
|
+
* `unique_index` or `still_desired`, and nothing is dropped. A drop that
|
|
1679
|
+
* waits longer than `lockTimeoutMs` on another transaction is abandoned with
|
|
1680
|
+
* `index_drop_timeout`; the index may then be left invalid, and a retry drops it.
|
|
1681
|
+
*
|
|
1682
|
+
* OPTIONAL. Backends that build no indexes omit it (the memory dataset):
|
|
1683
|
+
* there, `propose_index remove` has no index to find, so it is refused
|
|
1684
|
+
* `unknown_index`, and a remove recommendation it names is superseded.
|
|
1685
|
+
*/
|
|
1686
|
+
dropEngineIndex?(type: string, name: string, opts?: {
|
|
1687
|
+
/** Runs once every check has passed, just before the drop; a throw drops nothing. */
|
|
1688
|
+
beforeDrop?: () => Promise<void>;
|
|
1689
|
+
/** The drop's bound on each lock wait, in ms. Defaults to the backend's own. */
|
|
1690
|
+
lockTimeoutMs?: number;
|
|
1691
|
+
}): Promise<void>;
|
|
1390
1692
|
/**
|
|
1391
|
-
*
|
|
1392
|
-
*
|
|
1693
|
+
* Drop the engine-owned indexes of this namespace, after a namespace drop
|
|
1694
|
+
* has committed: its types leave the engine, so nothing would remove them.
|
|
1695
|
+
* Non-unique ones only, unless `unique`, which the caller passes only when
|
|
1696
|
+
* the namespace is declared nowhere (in no code and no live proposal): a
|
|
1697
|
+
* declared namespace's types come back, and until the reconciler rebuilds a
|
|
1698
|
+
* unique index, writes would not be held to it. Returns the names dropped.
|
|
1699
|
+
* Never inside a transaction; a tx-bound instance throws. A lock wait that
|
|
1700
|
+
* times out throws `index_drop_timeout`, leaving the rest for
|
|
1701
|
+
* `storageReport` to list as orphaned.
|
|
1702
|
+
*
|
|
1703
|
+
* OPTIONAL. Backends that build no indexes omit it.
|
|
1393
1704
|
*/
|
|
1394
|
-
|
|
1705
|
+
dropNamespaceIndexes?(opts?: {
|
|
1706
|
+
unique?: boolean;
|
|
1707
|
+
}): Promise<string[]>;
|
|
1395
1708
|
/**
|
|
1396
|
-
*
|
|
1397
|
-
*
|
|
1398
|
-
*
|
|
1399
|
-
*
|
|
1709
|
+
* The entry of this namespace's `type`'s declared `indexes` whose index is
|
|
1710
|
+
* named `name`, or undefined when no declaration builds it (a derived or
|
|
1711
|
+
* engine index, or none). `propose_index remove` retracts it when it is a
|
|
1712
|
+
* proposed type's. Never changes anything.
|
|
1713
|
+
*
|
|
1714
|
+
* OPTIONAL. Backends that build no indexes omit it.
|
|
1400
1715
|
*/
|
|
1401
|
-
|
|
1716
|
+
indexDeclarationOf?(type: string, name: string): Promise<IndexDecl | undefined>;
|
|
1402
1717
|
/**
|
|
1403
|
-
*
|
|
1404
|
-
*
|
|
1405
|
-
*
|
|
1406
|
-
*
|
|
1718
|
+
* Execute `fn` atomically. All ops performed on the `tx` argument run inside
|
|
1719
|
+
* one substrate transaction. If `fn` throws or returns a rejected promise,
|
|
1720
|
+
* every mutation inside the callback rolls back. If `fn` resolves, the
|
|
1721
|
+
* transaction commits and the resolved value is returned.
|
|
1722
|
+
*
|
|
1723
|
+
* Scope rules:
|
|
1724
|
+
* - `tx.namespace === this.namespace`
|
|
1725
|
+
* - Calls on the outer dataset (`this`) inside `fn` bypass the transaction
|
|
1726
|
+
* and are NOT rolled back on failure — this is a caller bug; implementations
|
|
1727
|
+
* MUST NOT attempt to guard against it. All ops inside `fn` MUST go through
|
|
1728
|
+
* `tx`.
|
|
1729
|
+
* - Nested `tx.withTransaction(...)` is implementation-defined. The reference
|
|
1730
|
+
* impls (memory, pg) flatten nested calls into the outermost transaction
|
|
1731
|
+
* (no savepoints).
|
|
1407
1732
|
*/
|
|
1408
|
-
|
|
1733
|
+
withTransaction<T>(fn: (tx: Dataset) => Promise<T>): Promise<T>;
|
|
1409
1734
|
/**
|
|
1410
|
-
*
|
|
1411
|
-
*
|
|
1412
|
-
*
|
|
1413
|
-
*
|
|
1414
|
-
*
|
|
1415
|
-
* inline hook in chain order.
|
|
1735
|
+
* Engine-only. Open a substrate-level transaction spanning multiple
|
|
1736
|
+
* namespaces on the same substrate, and invoke `fn` with a handles object
|
|
1737
|
+
* containing tx-bound datasets + (optionally) a tx-bound `EdgesStore`.
|
|
1738
|
+
* Called by the engine's mutate path (spec §5.3) for both single-ns and
|
|
1739
|
+
* cross-ns frames — the engine calls with `[ns]` for the single-ns case.
|
|
1416
1740
|
*
|
|
1417
|
-
*
|
|
1418
|
-
*
|
|
1741
|
+
* `namespaces` is the full set of namespaces the engine will dispatch
|
|
1742
|
+
* frames to inside the tx. Implementations MUST return a tx-bound
|
|
1743
|
+
* dataset for every requested namespace in `handles.datasets`; missing
|
|
1744
|
+
* entries are a programming error on the caller side.
|
|
1419
1745
|
*
|
|
1420
|
-
*
|
|
1421
|
-
*
|
|
1746
|
+
* `handles.edges` contract:
|
|
1747
|
+
* - `non-null` when the implementation can enlist a shared `EdgesStore`
|
|
1748
|
+
* into the same substrate tx (e.g. pg where the `PgEdgesStore` is wired
|
|
1749
|
+
* in via constructor and shares the `Sql`). The engine routes edge
|
|
1750
|
+
* mutations through this handle — true cross-store atomicity for
|
|
1751
|
+
* row + edge within one frame.
|
|
1752
|
+
* - `null` when the implementation can't enlist (e.g. memory datasets
|
|
1753
|
+
* where edges live in a separate Map). The engine falls back to
|
|
1754
|
+
* running edge ops in a nested independent tx and accepts the
|
|
1755
|
+
* visibility-ordering quirk documented in `autocrud-delete.ts`.
|
|
1756
|
+
*
|
|
1757
|
+
* Scope rules mirror `withTransaction`:
|
|
1758
|
+
* - All ops on values from `handles.datasets.get(ns)` and `handles.edges`
|
|
1759
|
+
* run in the same tx.
|
|
1760
|
+
* - Ops on the outer dataset (`this`) inside `fn` bypass the tx — caller
|
|
1761
|
+
* bug; implementations do not guard.
|
|
1762
|
+
* - A throw or rejection inside `fn` rolls back every mutation across
|
|
1763
|
+
* every namespace + edges in the tx.
|
|
1764
|
+
*
|
|
1765
|
+
* OPTIONAL. Implementations that can't coalesce cross-namespace writes
|
|
1766
|
+
* into one substrate transaction omit this method — the engine falls
|
|
1767
|
+
* back to refusing cross-namespace batches with `cross_tx_scope_refused`
|
|
1768
|
+
* (and loses edge-enlist for single-ns).
|
|
1422
1769
|
*/
|
|
1423
|
-
|
|
1424
|
-
}
|
|
1425
|
-
/** Single migration unit. */
|
|
1426
|
-
interface KernelMigration {
|
|
1427
|
-
/** Unique id within the plugin. Stored in migrations tracking table. */
|
|
1428
|
-
id: string;
|
|
1429
|
-
/** Apply migration. Called only when this migration's id is absent from the migrations table. */
|
|
1430
|
-
apply(ctx: KernelMigrationContext): Promise<void>;
|
|
1431
|
-
/** Inverse — drops what apply created. */
|
|
1432
|
-
rollback?(ctx: KernelMigrationContext): Promise<void>;
|
|
1433
|
-
}
|
|
1434
|
-
interface KernelMigrationContext {
|
|
1770
|
+
withScopedTransaction?<T>(namespaces: ReadonlyArray<string>, fn: (handles: TxScopeHandles) => Promise<T>): Promise<T>;
|
|
1435
1771
|
/**
|
|
1436
|
-
*
|
|
1437
|
-
*
|
|
1772
|
+
* Allocate the next short id for the given type within this dataset's
|
|
1773
|
+
* namespace. Monotonic per (namespace, type). Does NOT record any
|
|
1774
|
+
* row-id mapping — call `bindShortId` after the row is written.
|
|
1775
|
+
*
|
|
1776
|
+
* The implementation may use the dataset's current tx handle so the
|
|
1777
|
+
* sequence advance rolls back on tx rollback (postgres sequences
|
|
1778
|
+
* actually never roll back, but the surrounding tx semantics match
|
|
1779
|
+
* what callers expect).
|
|
1438
1780
|
*/
|
|
1439
|
-
|
|
1440
|
-
metric(name: string, payload?: unknown): void;
|
|
1441
|
-
}
|
|
1442
|
-
/**
|
|
1443
|
-
* Where-clause operator extension.
|
|
1444
|
-
*
|
|
1445
|
-
* The operator's name is the Record key it's registered under in
|
|
1446
|
-
* `KernelPlugin.filterOperators`.
|
|
1447
|
-
*
|
|
1448
|
-
* `execute` is awaited by the pre-resolve pass (`@mnmal-ai/substrate-filter`'s
|
|
1449
|
-
* `preResolveExtensionOperatorsDetailed`) and must resolve to an ordered,
|
|
1450
|
-
* scored `{ hits: [{ id: string; score: number; vector?: number[] }, ...] }`
|
|
1451
|
-
* envelope; any other shape throws `ExtensionOperatorResultError`. Relevance
|
|
1452
|
-
* order is preserved in the rewritten `id IN`, and score/vector are surfaced
|
|
1453
|
-
* via the pre-pass meta side-channel for the datasets to stitch onto rows.
|
|
1454
|
-
*/
|
|
1455
|
-
interface FilterOperatorDef {
|
|
1456
|
-
validate(value: unknown): {
|
|
1457
|
-
ok: true;
|
|
1458
|
-
value: unknown;
|
|
1459
|
-
} | {
|
|
1460
|
-
ok: false;
|
|
1461
|
-
reason: string;
|
|
1462
|
-
};
|
|
1463
|
-
execute(value: unknown, context: unknown): Promise<unknown>;
|
|
1781
|
+
allocateShortId(type: string): Promise<number>;
|
|
1464
1782
|
/**
|
|
1465
|
-
*
|
|
1466
|
-
*
|
|
1467
|
-
*
|
|
1783
|
+
* Record the (shortId → rowId) pairing after the row is written.
|
|
1784
|
+
* Called by autoCRUD create immediately after the dataset write()
|
|
1785
|
+
* returns the new row id. Safe to call within the enclosing tx.
|
|
1468
1786
|
*/
|
|
1469
|
-
|
|
1470
|
-
}
|
|
1471
|
-
/**
|
|
1472
|
-
* Query-only virtual type contributed by a substrate plugin.
|
|
1473
|
-
* `fields` reuses the FieldDef DSL so schema publication + client typing
|
|
1474
|
-
* work unchanged. The resolver receives the standard (ctx, args) pair; the
|
|
1475
|
-
* engine leaves `args.dataset` undefined for virtual namespaces — resolvers
|
|
1476
|
-
* compute from `params`/`fields` and MUST NOT reach for a dataset handle.
|
|
1477
|
-
*/
|
|
1478
|
-
interface VirtualTypeDef {
|
|
1479
|
-
fields: Record<string, FieldDef>;
|
|
1480
|
-
resolver: RegistryEntryResolver;
|
|
1481
|
-
}
|
|
1482
|
-
/** Mutation contributed by a substrate plugin under its virtual namespace. */
|
|
1483
|
-
interface VirtualMutationDef {
|
|
1484
|
-
resolver: RegistryEntryResolver;
|
|
1485
|
-
/** Optional params schema published into the namespace schema. */
|
|
1486
|
-
params?: Record<string, FieldDef>;
|
|
1487
|
-
}
|
|
1488
|
-
/**
|
|
1489
|
-
* A substrate plugin's claimed frame surface.
|
|
1490
|
-
* Registered into the engine's namespaces map as `{ registry, virtual: true }`.
|
|
1491
|
-
* Collision posture mirrors KernelRouteClaim: second claim = boot refusal.
|
|
1492
|
-
*/
|
|
1493
|
-
interface KernelNamespaceContribution {
|
|
1494
|
-
namespace: string;
|
|
1495
|
-
types?: Record<string, VirtualTypeDef>;
|
|
1496
|
-
mutations?: Record<string, VirtualMutationDef>;
|
|
1497
|
-
}
|
|
1498
|
-
/** Top-level kernel route reservation. */
|
|
1499
|
-
interface KernelRouteClaim {
|
|
1500
|
-
path: string;
|
|
1501
|
-
method: HttpMethod;
|
|
1502
|
-
handler: HttpHandler;
|
|
1787
|
+
bindShortId(type: string, shortId: number, rowId: string): Promise<void>;
|
|
1503
1788
|
/**
|
|
1504
|
-
*
|
|
1505
|
-
*
|
|
1506
|
-
*
|
|
1507
|
-
*
|
|
1508
|
-
* Do not make this optional: kernel routes mount via the host's raw
|
|
1509
|
-
* `server.route`, which applies no gate of its own, so a route with no
|
|
1510
|
-
* capability would get no auth wrapper at all.
|
|
1789
|
+
* Resolve a short id to the underlying row id, or null if no such short
|
|
1790
|
+
* id exists for this (namespace, type). Does not join `rows` —
|
|
1791
|
+
* callers chain their own `read()` after resolution.
|
|
1511
1792
|
*/
|
|
1512
|
-
|
|
1513
|
-
}
|
|
1514
|
-
/** Top-level MCP tool registration. */
|
|
1515
|
-
interface McpToolRegistration {
|
|
1516
|
-
name: string;
|
|
1517
|
-
description: string;
|
|
1518
|
-
inputSchema: unknown;
|
|
1519
|
-
handler(input: unknown): Promise<unknown>;
|
|
1520
|
-
}
|
|
1521
|
-
interface KernelLifecycleContext {
|
|
1793
|
+
resolveShortId(type: string, shortId: number): Promise<string | null>;
|
|
1522
1794
|
/**
|
|
1523
|
-
*
|
|
1524
|
-
*
|
|
1525
|
-
*
|
|
1795
|
+
* Inverse of `resolveShortId` — look up the short id assigned to a
|
|
1796
|
+
* given row id. Returns null if no sidecar entry exists (e.g. rows written
|
|
1797
|
+
* before short ids were allocated).
|
|
1526
1798
|
*/
|
|
1527
|
-
|
|
1799
|
+
getShortIdForRow(type: string, rowId: string): Promise<number | null>;
|
|
1528
1800
|
/**
|
|
1529
|
-
*
|
|
1530
|
-
*
|
|
1531
|
-
*
|
|
1801
|
+
* Spawn a sibling dataset bound to the same backing store but scoped to
|
|
1802
|
+
* a different namespace. Used by `compose()` to auto-allocate per-namespace
|
|
1803
|
+
* datasets when a coordinator plugin claims a namespace the operator hasn't
|
|
1804
|
+
* mapped explicitly.
|
|
1532
1805
|
*
|
|
1533
|
-
*
|
|
1534
|
-
*
|
|
1806
|
+
* Substrate semantics:
|
|
1807
|
+
* - Pg: returns a new `PgDataset` sharing this dataset's `sql` pool +
|
|
1808
|
+
* `edgesStore`. The returned dataset has the SAME `txScopeId` (so it
|
|
1809
|
+
* can participate in cross-namespace batched mutations on the shared pool).
|
|
1810
|
+
* - Memory: returns a new `MemoryDataset` with its own backing `Map`
|
|
1811
|
+
* per the default convention (each instance owns its own substrate). The
|
|
1812
|
+
* returned dataset has a NEW `txScopeId` — it cannot share a transaction
|
|
1813
|
+
* with the caller. Test harnesses that need shared-memory semantics across
|
|
1814
|
+
* namespaces should construct each `MemoryDataset` explicitly with
|
|
1815
|
+
* the relevant constructor option.
|
|
1816
|
+
*
|
|
1817
|
+
* Idempotency note: callers SHOULD NOT call `forNamespace(this.namespace)`;
|
|
1818
|
+
* the resulting object's identity is implementation-defined. Use this only
|
|
1819
|
+
* to derive datasets for *different* namespaces.
|
|
1535
1820
|
*/
|
|
1536
|
-
|
|
1821
|
+
forNamespace(namespace: string): Dataset;
|
|
1822
|
+
}
|
|
1823
|
+
interface DatasetRegistry {
|
|
1824
|
+
get(ns: string): Dataset | undefined;
|
|
1825
|
+
has(ns: string): boolean;
|
|
1826
|
+
set(ns: string, dataset: Dataset): void;
|
|
1537
1827
|
/**
|
|
1538
|
-
*
|
|
1539
|
-
*
|
|
1540
|
-
*
|
|
1541
|
-
* Declared, not installed: coordinators compose AFTER substrate, so the
|
|
1542
|
-
* engine has not yet registered them.
|
|
1828
|
+
* Observe namespace lifecycle events (§6.4). Fires when a namespace is
|
|
1829
|
+
* disposed — explicit unregister or substrate-signalled loss (memory-backed
|
|
1830
|
+
* restart). Returns a disposer that unsubscribes when invoked.
|
|
1543
1831
|
*
|
|
1544
|
-
*
|
|
1545
|
-
*
|
|
1832
|
+
* Optional: registries over persistent substrates (pg) never lose namespaces
|
|
1833
|
+
* at runtime and can omit this surface entirely. Consumers MUST feature-
|
|
1834
|
+
* detect before subscribing.
|
|
1546
1835
|
*/
|
|
1547
|
-
|
|
1836
|
+
onNamespaceLost?(listener: (ns: string) => void): () => void;
|
|
1548
1837
|
/**
|
|
1549
|
-
*
|
|
1550
|
-
*
|
|
1551
|
-
*
|
|
1552
|
-
* The two absent cases differ and consumers must branch on them:
|
|
1553
|
-
* - `undefined` — this host does not supply attribution (e.g. an older
|
|
1554
|
-
* binary); do not treat it as an empty list
|
|
1555
|
-
* - `[]` — attribution supplied, and there are no coordinators
|
|
1838
|
+
* Explicitly drop `ns` from the registry and fire `onNamespaceLost`
|
|
1839
|
+
* listeners synchronously. Disposing an unregistered namespace is a silent
|
|
1840
|
+
* no-op — keeps graceful-shutdown replays idempotent.
|
|
1556
1841
|
*
|
|
1557
|
-
*
|
|
1558
|
-
* contract term: hold it in one constant shared with the coordinator and pin
|
|
1559
|
-
* it with a test, or a rename makes the filter silently match nothing.
|
|
1842
|
+
* Optional: same rationale as `onNamespaceLost`.
|
|
1560
1843
|
*/
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1844
|
+
dispose?(ns: string): void;
|
|
1845
|
+
}
|
|
1846
|
+
/**
|
|
1847
|
+
* One declared index of a type and its state: as the catalog holds it, or,
|
|
1848
|
+
* while this process defers or runs its build, `pending`, `building` or
|
|
1849
|
+
* `failed`. Filters on an index that is not `valid` scan.
|
|
1850
|
+
*/
|
|
1851
|
+
interface TypeIndexState {
|
|
1852
|
+
name: string;
|
|
1853
|
+
fields: string[];
|
|
1854
|
+
unique: boolean;
|
|
1855
|
+
state: 'valid' | 'building' | 'pending' | 'invalid' | 'failed';
|
|
1856
|
+
outcome: 'created' | 'adopted' | 'swapped' | 'unchanged';
|
|
1857
|
+
/** Why a `failed` index failed: an error code, then a short reason. */
|
|
1858
|
+
error?: string;
|
|
1859
|
+
/** Why a `pending` index was not built: the free-disk check refused it. */
|
|
1860
|
+
reason?: 'insufficient_disk';
|
|
1861
|
+
}
|
|
1862
|
+
/** Where a type's rows live: `partition` is set on the partitioned layout once the type has one. */
|
|
1863
|
+
interface TypeStorageReport {
|
|
1864
|
+
layout: 'legacy' | 'partitioned';
|
|
1865
|
+
partition?: string;
|
|
1866
|
+
indexes: TypeIndexState[];
|
|
1867
|
+
}
|
|
1868
|
+
interface StorageReport {
|
|
1869
|
+
/** `unknown` when the report itself failed. */
|
|
1870
|
+
layout: 'legacy' | 'partitioned' | 'unknown';
|
|
1871
|
+
warnings: string[];
|
|
1872
|
+
/** Worth showing, not a fault: the doctor prints these without failing. */
|
|
1873
|
+
info?: string[];
|
|
1874
|
+
}
|
|
1875
|
+
/** See `Dataset.indexPlan`. */
|
|
1876
|
+
interface IndexPlan {
|
|
1877
|
+
/** Each desired index, its keys in order: a derived index ends with the type's natural order. */
|
|
1878
|
+
indexes: Array<{
|
|
1879
|
+
name: string;
|
|
1880
|
+
keys: Array<[string, 'asc' | 'desc']>;
|
|
1881
|
+
unique: boolean;
|
|
1882
|
+
/** A partial index's predicate beyond `live`: it serves only reads that `eq` each field to its value. */
|
|
1883
|
+
only?: Record<string, boolean | null>;
|
|
1884
|
+
}>;
|
|
1885
|
+
expectedSize?: 'small' | 'large';
|
|
1566
1886
|
}
|
|
1567
1887
|
//#endregion
|
|
1568
|
-
export {
|
|
1888
|
+
export { Registry as $, DropParticipant as A, Resolver as At, KernelMigrationContext as B, ResolverStat as C, AutoCrudVerb as Ct, SlowRequest as D, PolicyDecision as Dt, RuntimeMetrics as E, Policy as Et, FilterOperatorDef as F, WriteEvent as Ft, NamespaceConfig as G, KernelPlugin as H, FrameEntry as I, NamespaceTypeDef as J, NamespaceDefinition as K, IndexDecl as L, EncodeContext as M, ResolverReturn as Mt, EngineHandle as N, RowFilterObligation as Nt, TypeStats as O, PolicyProbeContext as Ot, EngineInput as P, TypedResolver as Pt, QualifiedFrame as Q, KernelLifecycleContext as R, ResolveObservation as S, AfterTxWork as St, RuntimeErrors as T, Obligation as Tt, KernelRouteClaim as U, KernelNamespaceContribution as V, McpToolRegistration as W, PluginRouteContext as X, PluginLifecycleContext as Y, PostWriteHook as Z, UpdateFieldRef as _, SubstrateResponse as _t, DropResult as a, ScheduledTask as at, WriteResult as b, WsConnection as bt, FilterCapabilities as c, VirtualTypeDef as ct, ReadParams as d, PluginRouter as dt, RegistryBase as et, ReadResult as f, RequestObservation as ft, TypeStorageReport as g, SubstrateRequest as gt, TypeIndexState as h, StartOpts as ht, DeleteResult as i, RegistryEntryResolver as it, EmbedSourceContext as j, ResolverArgs as jt, CoordinatorPlugin as k, RequestContext as kt, IndexPlan as l, HttpHandler as lt, TxScopeHandles as m, RuntimeOp as mt, Dataset as n, RegistryEntryInput as nt, FieldKind as o, ScheduledTaskContext as ot, StorageReport as p, RequestObserver as pt, NamespaceInstallContext as q, DatasetRegistry as r, RegistryEntryKind as rt, FieldPredicate as s, VirtualMutationDef as st, ConditionalUpdateResult as t, RegistryEntry as tt, OrderByClause as u, HttpMethod as ut, WhereClause as v, SubstrateServer as vt, RuntimeErrorEntry as w, Identity as wt, ProcessStat as x, WsHandler as xt, WriteOptions as y, TlsConfig as yt, KernelMigration as z };
|