@mnmal-ai/substrate 1.0.0-alpha.2 → 1.0.0-alpha.21
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 +64 -14
- package/dist/boot-store-Dz6PGwNY.js +4079 -0
- package/dist/{runtime-metrics-DwpDJ8vz.d.ts → bridge-BeVye_Oj.d.ts} +2 -87
- package/dist/cli-CH7M6ZTQ.js +3383 -0
- package/dist/client.browser.js +2485 -0
- package/dist/client.d.ts +273 -135
- package/dist/client.js +6 -6
- package/dist/cluster-worker.js +0 -1
- package/dist/{content-type-C7T28xNb.js → content-type-wLljot5M.js} +0 -1
- package/dist/{env-utils-CGNazUNt.js → env-utils-s25Z1YSA.js} +0 -1
- package/dist/{errors-D4vKuwz8.d.ts → errors-CoHLKAX0.d.ts} +44 -4
- package/dist/{fake-DUVtKVSE.js → fake-DdXtb-m8.js} +0 -1
- package/dist/{index-D7mSOFlM.d.ts → index-D7XRAGC6.d.ts} +7 -11
- package/dist/msgpackr-Dq18g6Ds.js +16 -0
- package/dist/node-version-BtK_Nq6z.js +32 -0
- package/dist/pack.d.ts +75 -11
- package/dist/pack.js +4 -4
- package/dist/pg-edges-store-Dnaxesmy.js +443 -0
- package/dist/{plugin-QuJN9pG_.js → plugin-BIz4Pr5-.js} +132 -8
- package/dist/proposals-bootstrap.js +6 -5
- package/dist/proposals.config.schema.json +5 -0
- package/dist/proposals.d.ts +17 -5
- package/dist/proposals.default-config.json +2 -1
- package/dist/proposals.js +2 -3
- package/dist/recall-bootstrap.js +5 -4
- package/dist/recall.config.schema.json +6 -0
- package/dist/recall.d.ts +35 -6
- package/dist/recall.js +2 -3
- package/dist/{repl-C52wyfMs.js → repl-BUkd9DPZ.js} +65 -33
- package/dist/{row-ByLFE2Wx.d.ts → row-aEWVHEsf.d.ts} +92 -12
- package/dist/run-Cu9w5Y10.js +2435 -0
- package/dist/{plugin-t2g2wX0b.d.ts → schema-DwnXxdjk.d.ts} +1293 -63
- package/dist/script-A9dxMRZA.js +143 -0
- package/dist/script-runner.js +77 -0
- package/dist/sdk.d.ts +227 -38
- package/dist/sdk.js +5 -110
- package/dist/semver-D_zdq0hi.js +17 -0
- package/dist/{src-DWm0iA88.js → src--accZ5Ka.js} +188 -87
- package/dist/{src-DMzc3HfW.js → src-BNdhH_Hx.js} +745 -851
- package/dist/{src-CG7giuHr.js → src-BQnDrs_E.js} +793 -75
- package/dist/{src-BLgbLq2k.js → src-B_kHkbsr.js} +780 -330
- package/dist/src-BbCUeMsI.js +7723 -0
- package/dist/{src-CHp3QyqB.js → src-BmJD4ScT.js} +9215 -3376
- package/dist/{row-DZ0Fnpc6.js → src-DmiUWOLp.js} +2 -1
- package/dist/substrate-admin.js +17 -782
- package/dist/substrate.js +4 -5487
- package/dist/testing.d.ts +730 -53
- package/dist/testing.js +20 -163
- package/dist/{ws-C72iVS5T.d.ts → ws-DJaKruX-.d.ts} +4 -5
- package/package.json +26 -26
- package/dist/dataset-BQ8egGl5.js +0 -1535
- package/dist/schema-g4FQLr0V.d.ts +0 -433
- package/dist/src-CXKnqeTW.js +0 -2339
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
import { $ as publishSchema, D as RESERVED_AUTOCRUD_FIELDS, I as installAutoCrud, It as JSON_HEADERS, J as resolveIdentity, M as field, O as RESERVED_AUTOCRUD_FIELD_NAMES, P as autoCrudOptionsOf, Vn as canonicalize$1, b as assertOrderedBy, d as uuidFromName, f as emitFieldEvents, j as reservedMetadataFieldMessage, l as planServesShape, p as migrateFieldValues, x as defineNamespace, xn as ResolverFrameError, y as assertIndexDecl } from "./src-BmJD4ScT.js";
|
|
2
|
+
import { s as requireCapability } from "./src-DmiUWOLp.js";
|
|
3
|
+
import "./src-B_kHkbsr.js";
|
|
4
4
|
import { randomUUID } from "node:crypto";
|
|
5
5
|
|
|
6
6
|
//#region ../../plugins/substrate-proposals/src/capability.ts
|
|
@@ -8,6 +8,8 @@ import { randomUUID } from "node:crypto";
|
|
|
8
8
|
* Substrate.propose capability policy helper.
|
|
9
9
|
*/
|
|
10
10
|
const DEFAULT_PROPOSE_CAPABILITY = "substrate.propose";
|
|
11
|
+
/** Changes a type's indexes or natural order (`propose_index`); separate from proposing schema. */
|
|
12
|
+
const DEFAULT_INDEX_CAPABILITY = "substrate.index";
|
|
11
13
|
/**
|
|
12
14
|
* Create a policy that requires the given capability to be present in the
|
|
13
15
|
* request identity. Passing an empty string disables the gate entirely
|
|
@@ -19,14 +21,40 @@ function makeProposePolicy(capability = DEFAULT_PROPOSE_CAPABILITY) {
|
|
|
19
21
|
if (capability === "") return () => ({ allow: true });
|
|
20
22
|
return requireCapability(capability);
|
|
21
23
|
}
|
|
24
|
+
/** TypeProposal fields boot replays as indexes and natural order. */
|
|
25
|
+
const INDEX_FIELDS = ["indexesJson", "orderedByJson"];
|
|
22
26
|
/**
|
|
23
27
|
* Writes to the proposal record types need the propose capability: boot
|
|
24
28
|
* replays every row marked applied, so a row written directly would become
|
|
25
|
-
* live schema.
|
|
29
|
+
* live schema. A write that names a type's indexes or natural order (null, which clears
|
|
30
|
+
* them, included) also needs
|
|
31
|
+
* the index capability, which `propose_index` is gated by. Reads stay open to
|
|
32
|
+
* anyone the namespace grant admits.
|
|
26
33
|
*/
|
|
27
|
-
function proposeToWrite(capability = DEFAULT_PROPOSE_CAPABILITY) {
|
|
34
|
+
function proposeToWrite(capability = DEFAULT_PROPOSE_CAPABILITY, indexCapability = DEFAULT_INDEX_CAPABILITY) {
|
|
28
35
|
const gate = makeProposePolicy(capability);
|
|
29
|
-
|
|
36
|
+
const indexGate = makeProposePolicy(indexCapability);
|
|
37
|
+
return (ctx, args) => {
|
|
38
|
+
if (ctx.operation !== "mutate") return { allow: true };
|
|
39
|
+
const decision = gate(ctx, args);
|
|
40
|
+
const params = args.params ?? {};
|
|
41
|
+
if (!decision.allow || !INDEX_FIELDS.some((f) => f in params)) return decision;
|
|
42
|
+
return indexGate(ctx, args);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* `propose_index`'s gate: the index capability in the namespace whose indexes
|
|
47
|
+
* the call changes (`params.namespace`), else in the frame's namespace.
|
|
48
|
+
*/
|
|
49
|
+
function makeIndexPolicy(capability = DEFAULT_INDEX_CAPABILITY) {
|
|
50
|
+
const gate = makeProposePolicy(capability);
|
|
51
|
+
return (ctx, args) => {
|
|
52
|
+
const target = args.params?.namespace;
|
|
53
|
+
return gate(typeof target === "string" ? {
|
|
54
|
+
...ctx,
|
|
55
|
+
namespace: target
|
|
56
|
+
} : ctx, args);
|
|
57
|
+
};
|
|
30
58
|
}
|
|
31
59
|
|
|
32
60
|
//#endregion
|
|
@@ -146,6 +174,9 @@ function crossRefsIn(spec) {
|
|
|
146
174
|
const SCALAR_TYPES = /* @__PURE__ */ new Set([
|
|
147
175
|
"string",
|
|
148
176
|
"number",
|
|
177
|
+
"int16",
|
|
178
|
+
"int32",
|
|
179
|
+
"int64",
|
|
149
180
|
"boolean",
|
|
150
181
|
"date",
|
|
151
182
|
"json"
|
|
@@ -156,9 +187,9 @@ function parseFieldSpec(json) {
|
|
|
156
187
|
if (!SCALAR_TYPES.has(json.type)) throw new Error(`unsupported scalar type: ${String(json.type)}`);
|
|
157
188
|
const def = {
|
|
158
189
|
kind: "scalar",
|
|
159
|
-
type: json.type
|
|
160
|
-
indexed: json.indexed ?? true
|
|
190
|
+
type: json.type
|
|
161
191
|
};
|
|
192
|
+
if (json.indexed !== void 0) def.indexed = json.indexed;
|
|
162
193
|
if (json.nullable !== void 0) def.nullable = json.nullable;
|
|
163
194
|
if (json.readonly !== void 0) def.readonly = json.readonly;
|
|
164
195
|
if (json.enum !== void 0) def.enum = json.enum;
|
|
@@ -260,6 +291,18 @@ function stringifyFieldDef(def) {
|
|
|
260
291
|
}
|
|
261
292
|
}
|
|
262
293
|
/**
|
|
294
|
+
* Refuse a proposed field the engine injects (`shortId`), which apply would overwrite.
|
|
295
|
+
* The resolvers call it, not apply: a stored proposal that names one still replays.
|
|
296
|
+
*
|
|
297
|
+
* @throws ProposalError `invalid_params` naming `fieldName`
|
|
298
|
+
*/
|
|
299
|
+
function assertNotEngineInjected(fieldName) {
|
|
300
|
+
if (RESERVED_AUTOCRUD_FIELD_NAMES.includes(fieldName)) throw new ProposalError("invalid_params", `\`${fieldName}\` is a reserved field name, auto-injected by the engine`, {
|
|
301
|
+
param: "fieldName",
|
|
302
|
+
fieldName
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
263
306
|
* Check that `spec` parses, so a proposal can be refused before any row is
|
|
264
307
|
* written for it.
|
|
265
308
|
*
|
|
@@ -267,6 +310,11 @@ function stringifyFieldDef(def) {
|
|
|
267
310
|
*/
|
|
268
311
|
function assertValidFieldSpec(fieldName, spec) {
|
|
269
312
|
assertProposableName("fieldName", fieldName);
|
|
313
|
+
const reserved = reservedMetadataFieldMessage(fieldName);
|
|
314
|
+
if (reserved !== void 0) throw new ProposalError("invalid_params", reserved, {
|
|
315
|
+
param: "fieldName",
|
|
316
|
+
fieldName
|
|
317
|
+
});
|
|
270
318
|
if (typeof spec !== "object" || spec === null) throw new ProposalError("invalid_field_spec", `Spec for field '${fieldName}' must be an object`, { fieldName });
|
|
271
319
|
try {
|
|
272
320
|
parseFieldSpec(spec);
|
|
@@ -281,7 +329,8 @@ function assertValidFieldSpec(fieldName, spec) {
|
|
|
281
329
|
* substrate-proposals namespace definition.
|
|
282
330
|
*
|
|
283
331
|
* Defines two types for schema evolution proposals:
|
|
284
|
-
* - TypeProposal: proposes a new type (multiple fields at once)
|
|
332
|
+
* - TypeProposal: proposes a new type (multiple fields at once), and holds
|
|
333
|
+
* the indexes and natural order `propose_index` gives it
|
|
285
334
|
* - FieldProposal: proposes a single field on an existing type
|
|
286
335
|
*
|
|
287
336
|
* Both track proposal metadata (proposedBy, proposedAt, reason) and application
|
|
@@ -293,8 +342,8 @@ function assertValidFieldSpec(fieldName, spec) {
|
|
|
293
342
|
* the propose resolvers, held under a per-name lock (`proposal-lock.ts`), not
|
|
294
343
|
* by a schema or database constraint.
|
|
295
344
|
*/
|
|
296
|
-
function defineProposalTypes(namespace, capability = DEFAULT_PROPOSE_CAPABILITY) {
|
|
297
|
-
const policy = proposeToWrite(capability);
|
|
345
|
+
function defineProposalTypes(namespace, capability = DEFAULT_PROPOSE_CAPABILITY, indexCapability = DEFAULT_INDEX_CAPABILITY) {
|
|
346
|
+
const policy = proposeToWrite(capability, indexCapability);
|
|
298
347
|
return defineNamespace(namespace, {
|
|
299
348
|
TypeProposal: {
|
|
300
349
|
policy,
|
|
@@ -308,7 +357,22 @@ function defineProposalTypes(namespace, capability = DEFAULT_PROPOSE_CAPABILITY)
|
|
|
308
357
|
appliedAt: field.date({ nullable: true }),
|
|
309
358
|
resultingSchemaVersion: field.string({ nullable: true }),
|
|
310
359
|
supersededByCode: field.boolean(),
|
|
311
|
-
archived: field.boolean()
|
|
360
|
+
archived: field.boolean(),
|
|
361
|
+
/** `IndexDecl[]` as JSON, set by `propose_index add`; null for none. */
|
|
362
|
+
indexesJson: field.string({
|
|
363
|
+
indexed: false,
|
|
364
|
+
nullable: true
|
|
365
|
+
}),
|
|
366
|
+
/** `OrderedBy` as JSON, set by `propose_index orderedBy`; null for the default. */
|
|
367
|
+
orderedByJson: field.string({
|
|
368
|
+
indexed: false,
|
|
369
|
+
nullable: true
|
|
370
|
+
}),
|
|
371
|
+
/** `'small' | 'large'`, the proposer's size hint for the index advisor; null when none. */
|
|
372
|
+
expectedSize: field.enum(["small", "large"], {
|
|
373
|
+
indexed: false,
|
|
374
|
+
nullable: true
|
|
375
|
+
})
|
|
312
376
|
}
|
|
313
377
|
},
|
|
314
378
|
FieldProposal: {
|
|
@@ -375,10 +439,13 @@ function assertValidFieldProposal(targetType, fieldName, fieldSpec, ownsTarget)
|
|
|
375
439
|
}
|
|
376
440
|
/** Installs the proposed type's autoCRUD surface into `registry`. */
|
|
377
441
|
function installProposedType(registry, args) {
|
|
442
|
+
installAutoCrud(registry, args.typeName, args.dataset, args.edges, { fields: proposedFieldDefs(args.fields) });
|
|
443
|
+
}
|
|
444
|
+
/** A proposed type's field specs as FieldDefs, with the engine-reserved fields added. */
|
|
445
|
+
function proposedFieldDefs(fields) {
|
|
378
446
|
const fieldDefs = {};
|
|
379
|
-
for (const [name, spec] of Object.entries(
|
|
380
|
-
Object.assign(fieldDefs, RESERVED_AUTOCRUD_FIELDS);
|
|
381
|
-
installAutoCrud(registry, args.typeName, args.dataset, args.edges, { fields: fieldDefs });
|
|
447
|
+
for (const [name, spec] of Object.entries(fields)) fieldDefs[name] = parseFieldSpec(spec);
|
|
448
|
+
return Object.assign(fieldDefs, RESERVED_AUTOCRUD_FIELDS);
|
|
382
449
|
}
|
|
383
450
|
/**
|
|
384
451
|
* Every registry key `applyTypeProposal` would register for this proposal —
|
|
@@ -405,6 +472,95 @@ function applyTypeProposal(args) {
|
|
|
405
472
|
binding.invalidateSchemaCaches?.();
|
|
406
473
|
}
|
|
407
474
|
/**
|
|
475
|
+
* Parses a TypeProposal row's `indexesJson` / `orderedByJson` / `expectedSize`
|
|
476
|
+
* (null or absent: none). An `expectedSize` other than `small` or `large` is
|
|
477
|
+
* left out and handed to `onInvalidSize`, so a bad hint never costs the type
|
|
478
|
+
* its indexes or order.
|
|
479
|
+
*/
|
|
480
|
+
function typeIndexOptionsOf(row, onInvalidSize) {
|
|
481
|
+
const parse = (v) => typeof v === "string" ? JSON.parse(v) : void 0;
|
|
482
|
+
const indexes = parse(row.indexesJson);
|
|
483
|
+
const orderedBy = parse(row.orderedByJson);
|
|
484
|
+
const size = row.expectedSize ?? void 0;
|
|
485
|
+
const expectedSize = size === "small" || size === "large" ? size : void 0;
|
|
486
|
+
if (size !== void 0 && expectedSize === void 0) onInvalidSize?.(/* @__PURE__ */ new Error(`expectedSize on ${String(row.typeName)} must be 'small' or 'large', got '${String(size)}'; ignored`));
|
|
487
|
+
return {
|
|
488
|
+
...indexes !== void 0 ? { indexes } : {},
|
|
489
|
+
...orderedBy !== void 0 ? { orderedBy } : {},
|
|
490
|
+
...expectedSize !== void 0 ? { expectedSize } : {}
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Refuse indexes or a natural order `propose_index` would not accept: each
|
|
495
|
+
* index as code may declare it, but never unique and never named (a name
|
|
496
|
+
* adopts whatever index already holds it), so every one is the engine's own,
|
|
497
|
+
* non-unique and generated. Rows are caller-written, so replay checks too.
|
|
498
|
+
*
|
|
499
|
+
* @throws Error naming the first problem
|
|
500
|
+
*/
|
|
501
|
+
function assertValidTypeIndexOptions(typeName, fields, opts) {
|
|
502
|
+
if (opts.indexes !== void 0 && !Array.isArray(opts.indexes)) throw new Error(`indexes on ${typeName} must be an array`);
|
|
503
|
+
for (const decl of opts.indexes ?? []) {
|
|
504
|
+
if (typeof decl !== "object" || decl === null) throw new Error(`index on ${typeName}: must be an object`);
|
|
505
|
+
assertIndexDecl(typeName, fields, decl);
|
|
506
|
+
if (decl.unique !== void 0) throw new Error(`index on ${typeName}: a proposed index is never unique`);
|
|
507
|
+
if (decl.name !== void 0) throw new Error(`index on ${typeName}: a proposed index takes a generated name`);
|
|
508
|
+
}
|
|
509
|
+
if (opts.orderedBy !== void 0) assertOrderedBy(typeName, {
|
|
510
|
+
fields,
|
|
511
|
+
orderedBy: opts.orderedBy
|
|
512
|
+
});
|
|
513
|
+
if (opts.expectedSize !== void 0 && opts.expectedSize !== "small" && opts.expectedSize !== "large") throw new Error(`expectedSize on ${typeName} must be 'small' or 'large'`);
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Reinstall `typeName`'s autoCRUD surface with `patch` over the options it was
|
|
517
|
+
* installed with (policy, postWrite, purge, indexes, order), swapping every
|
|
518
|
+
* entry through `registry.replace` so DuplicateRegistration is never thrown.
|
|
519
|
+
*/
|
|
520
|
+
function reinstallType(registry, typeName, dataset, edges, patch) {
|
|
521
|
+
const existing = registry.get(typeName);
|
|
522
|
+
if (!existing) throw new Error(`type '${typeName}' not registered`);
|
|
523
|
+
const replaceWriter = {
|
|
524
|
+
register: (key, input) => registry.replace(key, input),
|
|
525
|
+
replace: registry.replace.bind(registry),
|
|
526
|
+
get: registry.get.bind(registry)
|
|
527
|
+
};
|
|
528
|
+
const prior = autoCrudOptionsOf(existing.resolver) ?? {
|
|
529
|
+
policy: existing.policy,
|
|
530
|
+
fields: existing.fields
|
|
531
|
+
};
|
|
532
|
+
installAutoCrud(replaceWriter, typeName, dataset, edges, patch(prior));
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Give a registered proposed type the indexes and natural order its row
|
|
536
|
+
* stores, validated against the fields it has now (proposed ones included).
|
|
537
|
+
* Storage builds them on the next `ensureTypeStorage`.
|
|
538
|
+
*/
|
|
539
|
+
function applyTypeIndexOptions(args) {
|
|
540
|
+
const binding = args.registryStore.getBinding(args.namespace);
|
|
541
|
+
const existing = binding.registry.get(args.typeName);
|
|
542
|
+
if (!existing) throw new Error(`type '${args.typeName}' not registered`);
|
|
543
|
+
assertValidTypeIndexOptions(args.typeName, existing.fields, args);
|
|
544
|
+
reinstallType(binding.registry, args.typeName, args.dataset, args.edges, (prior) => {
|
|
545
|
+
const { indexes: _i, orderedBy: _o, expectedSize: _s, ...rest } = prior;
|
|
546
|
+
return {
|
|
547
|
+
...rest,
|
|
548
|
+
...args.indexes !== void 0 ? { indexes: args.indexes } : {},
|
|
549
|
+
...args.orderedBy !== void 0 ? { orderedBy: args.orderedBy } : {},
|
|
550
|
+
...args.expectedSize !== void 0 ? { expectedSize: args.expectedSize } : {}
|
|
551
|
+
};
|
|
552
|
+
});
|
|
553
|
+
binding.invalidateSchemaCaches?.();
|
|
554
|
+
}
|
|
555
|
+
/** Whether the type entry already carries exactly these options. */
|
|
556
|
+
function hasTypeIndexOptions(entry, opts) {
|
|
557
|
+
return sameJson(entry.indexes, opts.indexes) && sameJson(entry.orderedBy, opts.orderedBy) && entry.expectedSize === opts.expectedSize;
|
|
558
|
+
}
|
|
559
|
+
/** Deep equality of JSON values, whatever order their keys were written in. */
|
|
560
|
+
function sameJson(a, b) {
|
|
561
|
+
return JSON.stringify(canonicalize$1(a)) === JSON.stringify(canonicalize$1(b));
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
408
564
|
* Extend an already-registered type with a new field.
|
|
409
565
|
*
|
|
410
566
|
* Replaces the type entry + all autoCRUD mutations atomically with versions
|
|
@@ -419,36 +575,34 @@ function applyFieldProposal(args) {
|
|
|
419
575
|
const store = args.registryStore;
|
|
420
576
|
assertValidFieldProposal(args.targetType, args.fieldName, args.fieldSpec, (ns) => store.ownsNamespace(ns));
|
|
421
577
|
const binding = store.getBinding(args.namespace);
|
|
422
|
-
const
|
|
423
|
-
const existing = registry.get(args.targetType);
|
|
578
|
+
const existing = binding.registry.get(args.targetType);
|
|
424
579
|
if (!existing) throw new Error(`applyFieldProposal: target type '${args.targetType}' not registered`);
|
|
425
580
|
const nextFields = {
|
|
426
581
|
...existing.fields,
|
|
427
582
|
[args.fieldName]: parseFieldSpec(args.fieldSpec)
|
|
428
583
|
};
|
|
429
|
-
|
|
430
|
-
register: (key, input) => registry.replace(key, input),
|
|
431
|
-
replace: registry.replace.bind(registry)
|
|
432
|
-
};
|
|
433
|
-
const prior = autoCrudOptionsOf(existing.resolver) ?? { policy: existing.policy };
|
|
434
|
-
installAutoCrud(replaceWriter, args.targetType, args.dataset, args.edges, {
|
|
584
|
+
reinstallType(binding.registry, args.targetType, args.dataset, args.edges, (prior) => ({
|
|
435
585
|
...prior,
|
|
436
586
|
fields: nextFields
|
|
437
|
-
});
|
|
587
|
+
}));
|
|
438
588
|
binding.invalidateSchemaCaches?.();
|
|
439
589
|
}
|
|
440
590
|
|
|
441
591
|
//#endregion
|
|
442
592
|
//#region ../../plugins/substrate-proposals/src/engine-write.ts
|
|
443
593
|
/**
|
|
594
|
+
* Shared helpers used by the propose_* resolvers.
|
|
595
|
+
*/
|
|
596
|
+
/**
|
|
444
597
|
* Run the autoCRUD create-or-update `op` registered on `registry` inside the
|
|
445
598
|
* caller's own transaction (`args.dataset`), and return the resulting row.
|
|
446
599
|
* A nested `engine.mutate` would open a second transaction, and so take a
|
|
447
600
|
* second pooled connection, beside the one the proposal already holds.
|
|
448
601
|
*
|
|
449
|
-
* @throws
|
|
602
|
+
* @throws ResolverFrameError `policy_denied` when its policy denies
|
|
603
|
+
* @throws Error when `op` is not registered or returns no row
|
|
450
604
|
*/
|
|
451
|
-
async function writeInTx(registry, ctx, args, op, params) {
|
|
605
|
+
async function writeInTx(registry, ctx, args, op, params, skipPolicy = false) {
|
|
452
606
|
const entry = registry.get(op);
|
|
453
607
|
if (entry === void 0) throw new Error(`'${op}' is not registered`);
|
|
454
608
|
const opCtx = {
|
|
@@ -461,8 +615,8 @@ async function writeInTx(registry, ctx, args, op, params) {
|
|
|
461
615
|
params,
|
|
462
616
|
fields: { id: true }
|
|
463
617
|
};
|
|
464
|
-
const decision = entry.policy?.(opCtx, opArgs);
|
|
465
|
-
if (decision !== void 0 && !decision.allow) throw new
|
|
618
|
+
const decision = skipPolicy ? void 0 : entry.policy?.(opCtx, opArgs);
|
|
619
|
+
if (decision !== void 0 && !decision.allow) throw new ResolverFrameError("policy_denied", `'${op}' denied: ${decision.reason ?? "policy denied"}`);
|
|
466
620
|
const row = await entry.resolver(opCtx, opArgs);
|
|
467
621
|
if (row === null || typeof row !== "object" || Array.isArray(row)) throw new Error(`'${op}' returned no row`);
|
|
468
622
|
return row;
|
|
@@ -511,6 +665,61 @@ async function notifyAfterCommit(args, notify) {
|
|
|
511
665
|
if (committed) await notify();
|
|
512
666
|
});
|
|
513
667
|
}
|
|
668
|
+
const pendingStorage = /* @__PURE__ */ new Set();
|
|
669
|
+
/** Resolves once every storage preparation started so far has settled. */
|
|
670
|
+
async function storageSettled() {
|
|
671
|
+
await Promise.all([...pendingStorage]);
|
|
672
|
+
}
|
|
673
|
+
function prepareStorage(dataset, type, handle, scope) {
|
|
674
|
+
if (scope?.signal.aborted === true) return;
|
|
675
|
+
const work = (async () => {
|
|
676
|
+
try {
|
|
677
|
+
const pending = (await dataset.ensureTypeStorage?.(type, {
|
|
678
|
+
newFields: "pending",
|
|
679
|
+
...scope ? { signal: scope.signal } : {}
|
|
680
|
+
}))?.pending ?? [];
|
|
681
|
+
if (handle !== void 0 && pending.length > 0) await emitFieldEvents(handle, pending.map((field) => ({
|
|
682
|
+
kind: "field_pending",
|
|
683
|
+
namespace: dataset.namespace,
|
|
684
|
+
type,
|
|
685
|
+
field,
|
|
686
|
+
attempts: 0
|
|
687
|
+
}))).catch((err) => {
|
|
688
|
+
process.stderr.write(`[substrate-proposals] field_pending for ${dataset.namespace}/${type} not audited: ${err instanceof Error ? err.message : String(err)}\n`);
|
|
689
|
+
});
|
|
690
|
+
} catch (err) {
|
|
691
|
+
process.stderr.write(`[substrate-proposals] storage for ${dataset.namespace}/${type} not prepared: ${err instanceof Error ? err.message : String(err)}\n`);
|
|
692
|
+
}
|
|
693
|
+
})();
|
|
694
|
+
pendingStorage.add(work);
|
|
695
|
+
scope?.pending.add(work);
|
|
696
|
+
work.finally(() => {
|
|
697
|
+
pendingStorage.delete(work);
|
|
698
|
+
scope?.pending.delete(work);
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* Starts preparing `type`'s storage on the namespace's base `dataset` once the
|
|
703
|
+
* caller's transaction has committed (at once when there is none), never
|
|
704
|
+
* inside it: index builds run CONCURRENTLY and would wait forever on it. It
|
|
705
|
+
* runs in the background, because the lock waits and index builds have no
|
|
706
|
+
* bound and must not hold the caller's response; rows written meanwhile land
|
|
707
|
+
* in the DEFAULT partition and are moved. A failure is logged and never
|
|
708
|
+
* undoes the committed proposal; the next boot or proposal retries.
|
|
709
|
+
* A field added to a type that already has a table is recorded `pending`, and
|
|
710
|
+
* one `field_pending` `_system/AuditEvent` per such field is written and
|
|
711
|
+
* announced through `handle` (F11, F12). {@link storageSettled} awaits it;
|
|
712
|
+
* `scope`'s signal aborts it.
|
|
713
|
+
*/
|
|
714
|
+
function ensureStorageAfterCommit(afterTx, dataset, type, handle, scope) {
|
|
715
|
+
if (afterTx === void 0) {
|
|
716
|
+
prepareStorage(dataset, type, handle, scope);
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
afterTx(async ({ committed }) => {
|
|
720
|
+
if (committed) prepareStorage(dataset, type, handle, scope);
|
|
721
|
+
});
|
|
722
|
+
}
|
|
514
723
|
|
|
515
724
|
//#endregion
|
|
516
725
|
//#region ../../plugins/substrate-proposals/src/proposal-lock.ts
|
|
@@ -637,9 +846,7 @@ async function proposeNamespaceLocked(deps, ctx, args, input) {
|
|
|
637
846
|
let proposalId;
|
|
638
847
|
if (prior !== void 0) proposalId = String(prior.id);
|
|
639
848
|
else {
|
|
640
|
-
|
|
641
|
-
await writeInTx(controlBinding.registry, ctx, args, "createNamespaceProposal", {
|
|
642
|
-
id: proposalId,
|
|
849
|
+
const created = await writeInTx(controlBinding.registry, ctx, args, "createNamespaceProposal", {
|
|
643
850
|
ns,
|
|
644
851
|
proposedBy,
|
|
645
852
|
proposedAt: /* @__PURE__ */ new Date(),
|
|
@@ -649,6 +856,7 @@ async function proposeNamespaceLocked(deps, ctx, args, input) {
|
|
|
649
856
|
supersededByCode: false,
|
|
650
857
|
archived: false
|
|
651
858
|
});
|
|
859
|
+
proposalId = String(created.id);
|
|
652
860
|
}
|
|
653
861
|
try {
|
|
654
862
|
installHotAddedNamespace({
|
|
@@ -658,6 +866,7 @@ async function proposeNamespaceLocked(deps, ctx, args, input) {
|
|
|
658
866
|
engine: controlEngine,
|
|
659
867
|
edges: deps.edges,
|
|
660
868
|
...deps.proposeCapability !== void 0 && { proposeCapability: deps.proposeCapability },
|
|
869
|
+
...deps.indexCapability !== void 0 && { indexCapability: deps.indexCapability },
|
|
661
870
|
...deps.registerProposalMutations !== void 0 && { registerProposalMutations: deps.registerProposalMutations }
|
|
662
871
|
});
|
|
663
872
|
} finally {
|
|
@@ -715,12 +924,32 @@ function claimForRollback(deps, ns, engine, args) {
|
|
|
715
924
|
|
|
716
925
|
//#endregion
|
|
717
926
|
//#region ../../plugins/substrate-proposals/src/multi-instance.ts
|
|
927
|
+
/** Why a stored field proposal is skipped when replay skipped its type. */
|
|
928
|
+
function typeSkipped(row) {
|
|
929
|
+
const type = String(row.targetType);
|
|
930
|
+
const field = String(row.fieldName);
|
|
931
|
+
return new ProposalError("unknown_type", `type '${type}' was skipped at replay, so its field '${field}' is not added`, {
|
|
932
|
+
typeName: type,
|
|
933
|
+
fieldName: field,
|
|
934
|
+
typeSkipped: true
|
|
935
|
+
});
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* The warning line for a stored proposal skipped because it names a field reserved for
|
|
939
|
+
* server metadata, or because its type was skipped; undefined for any other reason.
|
|
940
|
+
*/
|
|
941
|
+
function reservedSkipLine(namespace, row, err) {
|
|
942
|
+
if (err instanceof ProposalError && err.details.typeSkipped === true) return `substrate-proposals: ${namespace}/${String(row.targetType)}: field '${String(row.fieldName)}' is not added, because its type was skipped`;
|
|
943
|
+
if (!(err instanceof ProposalError) || !err.message.includes("reserved for server metadata")) return;
|
|
944
|
+
return `substrate-proposals: ${namespace}/${String(row.typeName ?? row.targetType)}: field '${String(err.details.fieldName ?? row.fieldName)}' is reserved for server metadata; ${row.typeName !== void 0 ? "the type is not registered" : "the field is not added"}`;
|
|
945
|
+
}
|
|
718
946
|
async function setupNamespaceListener(args) {
|
|
719
947
|
await args.pubsub.listen(args.channel, async (payload) => {
|
|
720
948
|
await applyProposalFromNotification({
|
|
721
949
|
namespace: args.namespace,
|
|
722
950
|
proposalId: payload,
|
|
723
|
-
registryStore: args.registryStore
|
|
951
|
+
registryStore: args.registryStore,
|
|
952
|
+
...args.onSkip !== void 0 ? { onSkip: args.onSkip } : {}
|
|
724
953
|
}).catch(() => {});
|
|
725
954
|
});
|
|
726
955
|
}
|
|
@@ -729,23 +958,33 @@ async function applyProposalFromNotification(args) {
|
|
|
729
958
|
const typeHit = await binding.dataset.read("TypeProposal", { id: args.proposalId });
|
|
730
959
|
if (typeHit.rows.length > 0) {
|
|
731
960
|
const row = typeHit.rows[0];
|
|
732
|
-
if (row.applied
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
fields
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
961
|
+
if (row.applied !== true) return;
|
|
962
|
+
try {
|
|
963
|
+
const typeName = row.typeName;
|
|
964
|
+
let changed = false;
|
|
965
|
+
if (!binding.registry.get(typeName)) {
|
|
966
|
+
const fields = JSON.parse(row.fieldsJson);
|
|
967
|
+
applyTypeProposal({
|
|
968
|
+
namespace: args.namespace,
|
|
969
|
+
typeName,
|
|
970
|
+
fields,
|
|
971
|
+
registryStore: args.registryStore,
|
|
972
|
+
dataset: binding.dataset,
|
|
973
|
+
edges: binding.edges
|
|
974
|
+
});
|
|
975
|
+
changed = true;
|
|
976
|
+
}
|
|
977
|
+
changed = replayTypeIndexOptions(args.registryStore, args.namespace, row) || changed;
|
|
978
|
+
if (changed) ensureStorageAfterCommit(void 0, binding.dataset, typeName, binding.engine, args.registryStore.storage);
|
|
979
|
+
} catch (err) {
|
|
980
|
+
args.onSkip?.(row, err);
|
|
742
981
|
}
|
|
743
982
|
return;
|
|
744
983
|
}
|
|
745
984
|
const fieldHit = await binding.dataset.read("FieldProposal", { id: args.proposalId });
|
|
746
985
|
if (fieldHit.rows.length > 0) {
|
|
747
986
|
const row = fieldHit.rows[0];
|
|
748
|
-
if (row.applied === true) {
|
|
987
|
+
if (row.applied === true) try {
|
|
749
988
|
const entry = binding.registry.get(row.targetType);
|
|
750
989
|
if (entry && fieldNeedsReplay(entry, row)) {
|
|
751
990
|
const fieldSpec = JSON.parse(row.fieldSpecJson);
|
|
@@ -758,11 +997,35 @@ async function applyProposalFromNotification(args) {
|
|
|
758
997
|
dataset: binding.dataset,
|
|
759
998
|
edges: binding.edges
|
|
760
999
|
});
|
|
1000
|
+
ensureStorageAfterCommit(void 0, binding.dataset, row.targetType, binding.engine, args.registryStore.storage);
|
|
761
1001
|
}
|
|
1002
|
+
} catch (err) {
|
|
1003
|
+
args.onSkip?.(row, err);
|
|
762
1004
|
}
|
|
763
1005
|
}
|
|
764
1006
|
}
|
|
765
1007
|
/**
|
|
1008
|
+
* Bring a registered proposed type's indexes and natural order in line with
|
|
1009
|
+
* its row. False when they already match (or the type is not registered).
|
|
1010
|
+
*
|
|
1011
|
+
* @throws when the row's options are malformed or invalid for the type
|
|
1012
|
+
*/
|
|
1013
|
+
function replayTypeIndexOptions(registryStore, namespace, row, onSkip) {
|
|
1014
|
+
const binding = registryStore.getBinding(namespace);
|
|
1015
|
+
const entry = binding.registry.get(row.typeName);
|
|
1016
|
+
const opts = typeIndexOptionsOf(row, (err) => onSkip?.(row, err));
|
|
1017
|
+
if (entry === void 0 || hasTypeIndexOptions(entry, opts)) return false;
|
|
1018
|
+
applyTypeIndexOptions({
|
|
1019
|
+
namespace,
|
|
1020
|
+
typeName: row.typeName,
|
|
1021
|
+
...opts,
|
|
1022
|
+
registryStore,
|
|
1023
|
+
dataset: binding.dataset,
|
|
1024
|
+
edges: binding.edges
|
|
1025
|
+
});
|
|
1026
|
+
return true;
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
766
1029
|
* A field proposal needs replaying when its field is absent, or when it
|
|
767
1030
|
* narrowed an enum: the type was registered with the wider spec, and the
|
|
768
1031
|
* narrowing exists only in the proposal row.
|
|
@@ -780,6 +1043,8 @@ async function reconcileNamespace(args) {
|
|
|
780
1043
|
] },
|
|
781
1044
|
limit: 1e4
|
|
782
1045
|
});
|
|
1046
|
+
const prepare = /* @__PURE__ */ new Set();
|
|
1047
|
+
const skippedTypes = /* @__PURE__ */ new Set();
|
|
783
1048
|
for (const row of typeRows.rows) {
|
|
784
1049
|
if (binding.registry.get(row.typeName)) continue;
|
|
785
1050
|
try {
|
|
@@ -794,7 +1059,10 @@ async function reconcileNamespace(args) {
|
|
|
794
1059
|
});
|
|
795
1060
|
} catch (err) {
|
|
796
1061
|
args.onSkip?.(row, err);
|
|
1062
|
+
skippedTypes.add(row.typeName);
|
|
1063
|
+
continue;
|
|
797
1064
|
}
|
|
1065
|
+
prepare.add(row.typeName);
|
|
798
1066
|
}
|
|
799
1067
|
const fieldRows = await binding.dataset.read("FieldProposal", {
|
|
800
1068
|
where: { AND: [
|
|
@@ -807,6 +1075,10 @@ async function reconcileNamespace(args) {
|
|
|
807
1075
|
});
|
|
808
1076
|
for (const row of fieldRows.rows) {
|
|
809
1077
|
const entry = binding.registry.get(row.targetType);
|
|
1078
|
+
if (!entry && skippedTypes.has(row.targetType)) {
|
|
1079
|
+
args.onSkip?.(row, typeSkipped(row));
|
|
1080
|
+
continue;
|
|
1081
|
+
}
|
|
810
1082
|
if (!entry || !fieldNeedsReplay(entry, row)) continue;
|
|
811
1083
|
try {
|
|
812
1084
|
const fieldSpec = JSON.parse(row.fieldSpecJson);
|
|
@@ -821,8 +1093,16 @@ async function reconcileNamespace(args) {
|
|
|
821
1093
|
});
|
|
822
1094
|
} catch (err) {
|
|
823
1095
|
args.onSkip?.(row, err);
|
|
1096
|
+
continue;
|
|
824
1097
|
}
|
|
1098
|
+
prepare.add(row.targetType);
|
|
1099
|
+
}
|
|
1100
|
+
for (const row of typeRows.rows) try {
|
|
1101
|
+
if (replayTypeIndexOptions(args.registryStore, args.namespace, row, args.onSkip)) prepare.add(row.typeName);
|
|
1102
|
+
} catch (err) {
|
|
1103
|
+
args.onSkip?.(row, err);
|
|
825
1104
|
}
|
|
1105
|
+
for (const type of prepare) ensureStorageAfterCommit(void 0, binding.dataset, type, binding.engine, args.registryStore.storage);
|
|
826
1106
|
}
|
|
827
1107
|
/**
|
|
828
1108
|
* Shared install path for a hot-added namespace. Three call sites use this:
|
|
@@ -859,7 +1139,7 @@ function installHotAddedNamespace(args) {
|
|
|
859
1139
|
args.registryStore.set(args.ns, binding);
|
|
860
1140
|
}
|
|
861
1141
|
const registry = skipRegistered(binding.registry);
|
|
862
|
-
defineProposalTypes(args.ns, args.proposeCapability).install({
|
|
1142
|
+
defineProposalTypes(args.ns, args.proposeCapability, args.indexCapability).install({
|
|
863
1143
|
registry,
|
|
864
1144
|
dataset: binding.dataset,
|
|
865
1145
|
edges: binding.edges
|
|
@@ -927,6 +1207,7 @@ async function setupClusterNamespaceListener(args) {
|
|
|
927
1207
|
engine: args.engine,
|
|
928
1208
|
edges: args.edges,
|
|
929
1209
|
...args.proposeCapability !== void 0 && { proposeCapability: args.proposeCapability },
|
|
1210
|
+
...args.indexCapability !== void 0 && { indexCapability: args.indexCapability },
|
|
930
1211
|
...args.registerProposalMutations !== void 0 && { registerProposalMutations: args.registerProposalMutations }
|
|
931
1212
|
});
|
|
932
1213
|
} catch (err) {
|
|
@@ -973,6 +1254,7 @@ async function reconcileClusterNamespaces(args) {
|
|
|
973
1254
|
engine: args.engine,
|
|
974
1255
|
edges: args.edges,
|
|
975
1256
|
...args.proposeCapability !== void 0 && { proposeCapability: args.proposeCapability },
|
|
1257
|
+
...args.indexCapability !== void 0 && { indexCapability: args.indexCapability },
|
|
976
1258
|
...args.registerProposalMutations !== void 0 && { registerProposalMutations: args.registerProposalMutations }
|
|
977
1259
|
});
|
|
978
1260
|
} catch (err) {
|
|
@@ -1107,7 +1389,10 @@ function buildOnboardBody(ownedNamespaces) {
|
|
|
1107
1389
|
params: {
|
|
1108
1390
|
typeName: "string — PascalCase, unique within the namespace",
|
|
1109
1391
|
fields: "object — FieldSpecJson map keyed by camelCase field name (see fieldSpecJson below)",
|
|
1110
|
-
proposedReason: "string — short human-readable rationale"
|
|
1392
|
+
proposedReason: "string — short human-readable rationale",
|
|
1393
|
+
orderedBy: "optional { \"<field>\": \"asc\"|\"desc\" } — ask yourself what order this is usually read in. A date or number field, or createdAt / updatedAt. Defaults to createdAt desc.",
|
|
1394
|
+
indexes: "optional [{ fields: [...] }] — indexes the usual reads need. Never unique or named. Needs no extra capability on a new type; changing them later needs substrate.index.",
|
|
1395
|
+
expectedSize: "optional \"small\"|\"large\" — \"large\" gets index advice sooner"
|
|
1111
1396
|
},
|
|
1112
1397
|
returns: {
|
|
1113
1398
|
id: "uuid — the TypeProposal record id",
|
|
@@ -1146,8 +1431,8 @@ function buildOnboardBody(ownedNamespaces) {
|
|
|
1146
1431
|
description: "Narrow wire format mirroring the autoCRUD-expressible subset of FieldDef. Only these four shapes are accepted. Anything more expressive needs code + PR.",
|
|
1147
1432
|
variants: {
|
|
1148
1433
|
scalar: {
|
|
1149
|
-
shape: "{ kind: \"scalar\", type: \"string\"|\"number\"|\"boolean\"|\"date\"|\"json\", indexed?: bool, nullable?: bool }",
|
|
1150
|
-
notes: "indexed defaults to true
|
|
1434
|
+
shape: "{ kind: \"scalar\", type: \"string\"|\"number\"|\"int16\"|\"int32\"|\"int64\"|\"boolean\"|\"date\"|\"json\", indexed?: bool, nullable?: bool }",
|
|
1435
|
+
notes: "indexed is unset by default (no index; crossRef defaults to indexed: true). No field needs an index to filter, and non-json scalars sort without one, so set indexed: true only where a read needs the speed. indexed: false also marks a field as not a search handle (long-form prose). json filters by path only and never sorts. readonly is reserved for engine-managed fields. Integers: pick the narrowest of int16 (-32768..32767), int32 (about ±2.1e9) or int64 (about ±9.2e18; a decimal string on the wire past 2^53) that holds every value the field will ever take, since a field's type cannot change once it has rows; when unsure, go one wider. Use number for fractional values. Use type:\"json\" for arbitrary nested blobs you do not need to filter on (audit details, free-form metadata). Use kind:\"struct\" instead when you have a known nested shape and want per-field where-clause support."
|
|
1151
1436
|
},
|
|
1152
1437
|
crossRef: {
|
|
1153
1438
|
shape: "{ kind: \"crossRef\", ns: string, type: string, onDelete?: \"set_null\"|\"cascade\"|\"restrict\"|\"orphan\", indexed?: bool, nullable?: bool }",
|
|
@@ -1166,7 +1451,7 @@ function buildOnboardBody(ownedNamespaces) {
|
|
|
1166
1451
|
constraints: [
|
|
1167
1452
|
"AutoCRUD-only: every proposed type gets the standard create/get/update/delete/list surface. No custom resolvers — those require code.",
|
|
1168
1453
|
"Additive, with one exception: propose_type rejects if the name exists; propose_field rejects with `destructive_change_required` if the field exists with a different spec, unless the change narrows an enum and a valueMap is supplied. That path rewrites every stored value the map names before narrowing, so it changes data, not just schema.",
|
|
1169
|
-
"First-past-the-post: concurrent proposes of the same name (namespace, type, or field) are serialised by a lock on that name — a
|
|
1454
|
+
"First-past-the-post: concurrent proposes of the same name (namespace, type, or field) are serialised by a lock on that name — a schema-scoped advisory lock on a pg backend, a process-local lock otherwise. The first to take it wins; the next reads its row and gets `namespace_already_proposed` / `type_already_proposed` with the prior proposedBy/at in details, or, for a field with an identical spec, the prior FieldProposal row.",
|
|
1170
1455
|
"Engine-injected fields: every proposed type gets a readonly+indexed `shortId: number` field auto-added by the plugin — callers must NOT declare shortId themselves.",
|
|
1171
1456
|
`Proposals are first-class data: every NamespaceProposal / TypeProposal / FieldProposal is queryable as a normal type. NamespaceProposal lives in \`${CONTROL_NAMESPACE}\`; TypeProposal / FieldProposal live in their target namespace.`,
|
|
1172
1457
|
"Hot-added namespaces are first-class: a namespace created via propose_namespace supports the same propose_type / propose_field surface as a static ownedNamespace, and survives restarts via NamespaceProposal replay on cold boot."
|
|
@@ -1296,6 +1581,7 @@ function makeProposeFieldResolver(deps) {
|
|
|
1296
1581
|
if (!deps.ownedNamespaces.has(deps.namespace) && !deps.registryStore.has(deps.namespace)) throw new ProposalError("unowned_namespace", `Namespace '${deps.namespace}' is not owned by this plugin instance`);
|
|
1297
1582
|
const input = args.params ?? {};
|
|
1298
1583
|
assertValidFieldProposal(input.targetType, input.fieldName, input.fieldSpec, (ns, type) => deps.registryStore.ownsType(ns, type));
|
|
1584
|
+
assertNotEngineInjected(input.fieldName);
|
|
1299
1585
|
if (input.proposedReason !== void 0 && typeof input.proposedReason !== "string") throw new ProposalError("invalid_params", "'proposedReason' must be a string", { param: "proposedReason" });
|
|
1300
1586
|
const dataset = args.dataset ?? deps.dataset;
|
|
1301
1587
|
return withProposalLock(dataset, proposalLockKey("field", deps.namespace, `${input.targetType}.${input.fieldName}`), () => proposeFieldLocked(deps, ctx, {
|
|
@@ -1356,9 +1642,7 @@ async function proposeFieldLocked(deps, ctx, args, input) {
|
|
|
1356
1642
|
}, suppliedMap, { oldVocabulary: oldEnum });
|
|
1357
1643
|
}
|
|
1358
1644
|
const proposedAt = /* @__PURE__ */ new Date();
|
|
1359
|
-
const newId = randomUUID();
|
|
1360
1645
|
const initialParams = {
|
|
1361
|
-
id: newId,
|
|
1362
1646
|
targetType,
|
|
1363
1647
|
fieldName,
|
|
1364
1648
|
fieldSpecJson: JSON.stringify(fieldSpec),
|
|
@@ -1373,8 +1657,8 @@ async function proposeFieldLocked(deps, ctx, args, input) {
|
|
|
1373
1657
|
archived: false
|
|
1374
1658
|
};
|
|
1375
1659
|
const engine = binding.engine;
|
|
1376
|
-
|
|
1377
|
-
|
|
1660
|
+
const newId = engine !== void 0 ? (await writeInTx(binding.registry, ctx, args, "createFieldProposal", initialParams)).id : (await args.dataset.write("FieldProposal", [initialParams])).ids[0];
|
|
1661
|
+
if (newId === void 0) throw new Error("dataset.write did not return an id for FieldProposal");
|
|
1378
1662
|
try {
|
|
1379
1663
|
applyUntilRollback(args, binding.registry, () => applyFieldProposal({
|
|
1380
1664
|
namespace: deps.namespace,
|
|
@@ -1410,6 +1694,7 @@ async function proposeFieldLocked(deps, ctx, args, input) {
|
|
|
1410
1694
|
await args.dataset.write("FieldProposal", [appliedRow]);
|
|
1411
1695
|
}
|
|
1412
1696
|
await notifyAfterCommit(args, () => deps.pubsub.notify(deps.channelFor(deps.namespace), String(newId)));
|
|
1697
|
+
ensureStorageAfterCommit(args.afterTx, deps.dataset, targetType, engine, deps.registryStore.storage);
|
|
1413
1698
|
if (migration === null) return appliedRow;
|
|
1414
1699
|
const { ids: _written, ...summary } = migration;
|
|
1415
1700
|
return {
|
|
@@ -1418,6 +1703,338 @@ async function proposeFieldLocked(deps, ctx, args, input) {
|
|
|
1418
1703
|
};
|
|
1419
1704
|
}
|
|
1420
1705
|
|
|
1706
|
+
//#endregion
|
|
1707
|
+
//#region ../../plugins/substrate-proposals/src/propose-index-resolver.ts
|
|
1708
|
+
/**
|
|
1709
|
+
* Factory for the `propose_index` mutation resolver: the signed approval of an
|
|
1710
|
+
* index recommendation, or a direct index change, under the index capability.
|
|
1711
|
+
*
|
|
1712
|
+
* - `add` appends a declared index to a proposed type's TypeProposal row and
|
|
1713
|
+
* reinstalls the type; storage builds it in the background after commit.
|
|
1714
|
+
* - `orderedBy` sets a proposed type's natural order the same way, and storage
|
|
1715
|
+
* swaps its derived indexes.
|
|
1716
|
+
* - `remove` drops an engine-owned, non-unique index the engine no longer
|
|
1717
|
+
* wants, on any type (`Dataset.dropEngineIndex`, which re-checks it live).
|
|
1718
|
+
* An index wanted only because a proposed type's `indexesJson` declares it
|
|
1719
|
+
* is retracted first: the declaration leaves the row, peers are told, and
|
|
1720
|
+
* the index is dropped.
|
|
1721
|
+
* - `dismiss` closes a recommendation without acting on it.
|
|
1722
|
+
*
|
|
1723
|
+
* A referenced `_system/IndexRecommendation` must be `open`, and moves to
|
|
1724
|
+
* `accepted` or `dismissed` by a conditional update from `open`, claimed
|
|
1725
|
+
* before the audit event and the effect and given back (to `open`) if the
|
|
1726
|
+
* effect fails, so a caller never learns it was closed after its own effect. Its rows are
|
|
1727
|
+
* writable by any index-capability holder, so nothing in one is trusted: an
|
|
1728
|
+
* `add` must still be unserved by the type's index plan (as the advisor's
|
|
1729
|
+
* supersede pass decides) and its declaration validates as a direct one does;
|
|
1730
|
+
* `remove` is checked against the catalog. When that evidence no longer
|
|
1731
|
+
* holds the call is refused `recommendation_stale` and the row superseded.
|
|
1732
|
+
*
|
|
1733
|
+
* Every write here goes through the namespaces' base datasets, outside the
|
|
1734
|
+
* caller's transaction: a `DROP INDEX CONCURRENTLY` cannot run beside it, and
|
|
1735
|
+
* a superseded mark must outlive the refusal that rolls the transaction back.
|
|
1736
|
+
* Each one is stamped with the caller's identity.
|
|
1737
|
+
*
|
|
1738
|
+
* Audit: every applied call writes one `_system/AuditEvent` (`index_proposed`)
|
|
1739
|
+
* once its checks have passed and before its first effect (for a drop, under
|
|
1740
|
+
* the table lock, just before `DROP INDEX`), so no effect lands unaudited. For
|
|
1741
|
+
* a recommendation the event's id derives from the recommendation's, written
|
|
1742
|
+
* insert-only, so a retry after a partial failure converges without a second
|
|
1743
|
+
* event.
|
|
1744
|
+
*/
|
|
1745
|
+
const SYSTEM_NAMESPACE = "_system";
|
|
1746
|
+
const RECOMMENDATION_TYPE = "IndexRecommendation";
|
|
1747
|
+
const ACTIONS = [
|
|
1748
|
+
"add",
|
|
1749
|
+
"remove",
|
|
1750
|
+
"dismiss",
|
|
1751
|
+
"orderedBy"
|
|
1752
|
+
];
|
|
1753
|
+
/** `dropEngineIndex` refusal codes, passed through as the call's own. */
|
|
1754
|
+
const DROP_REFUSALS = /* @__PURE__ */ new Set([
|
|
1755
|
+
"unknown_index",
|
|
1756
|
+
"not_engine_owned",
|
|
1757
|
+
"unique_index",
|
|
1758
|
+
"still_desired",
|
|
1759
|
+
"index_drop_timeout"
|
|
1760
|
+
]);
|
|
1761
|
+
/** A read row without the engine fields reads add (`shortId`), for writing back. */
|
|
1762
|
+
function bodyOf(row) {
|
|
1763
|
+
const body = { ...row };
|
|
1764
|
+
for (const name of RESERVED_AUTOCRUD_FIELD_NAMES) delete body[name];
|
|
1765
|
+
return body;
|
|
1766
|
+
}
|
|
1767
|
+
function invalid(param, message) {
|
|
1768
|
+
return new ProposalError("invalid_params", message, { param });
|
|
1769
|
+
}
|
|
1770
|
+
function closed(id, state) {
|
|
1771
|
+
return new ProposalError("recommendation_closed", `index recommendation '${String(id)}' is no longer open${state === void 0 ? "" : ` (${String(state)})`}`, state === void 0 ? {} : { state });
|
|
1772
|
+
}
|
|
1773
|
+
function parseInput$1(params) {
|
|
1774
|
+
for (const key of [
|
|
1775
|
+
"namespace",
|
|
1776
|
+
"type",
|
|
1777
|
+
"reason"
|
|
1778
|
+
]) if (typeof params[key] !== "string" || key !== "reason" && params[key] === "") throw invalid(key, `'${key}' must be a${key === "reason" ? "" : " non-empty"} string`);
|
|
1779
|
+
if (!ACTIONS.includes(params.action)) throw invalid("action", `'action' must be one of ${ACTIONS.join(", ")}`);
|
|
1780
|
+
for (const key of ["recommendationId", "indexName"]) if (params[key] !== void 0 && typeof params[key] !== "string") throw invalid(key, `'${key}' must be a string`);
|
|
1781
|
+
return params;
|
|
1782
|
+
}
|
|
1783
|
+
function makeProposeIndexResolver(deps) {
|
|
1784
|
+
return async (ctx, args) => {
|
|
1785
|
+
if ((args.txEntries ?? 1) > 1) throw new ProposalError("propose_index_not_alone", "propose_index must be the only entry in its mutate request; send it on its own", { entries: args.txEntries });
|
|
1786
|
+
const input = parseInput$1(args.params ?? {});
|
|
1787
|
+
const lockOn = deps.datasetFor(SYSTEM_NAMESPACE) ?? args.dataset;
|
|
1788
|
+
const run = () => proposeIndex(deps, ctx, args, input);
|
|
1789
|
+
if (lockOn === void 0) return run();
|
|
1790
|
+
return withProposalLock(lockOn, proposalLockKey("index", input.namespace, input.type), run);
|
|
1791
|
+
};
|
|
1792
|
+
}
|
|
1793
|
+
async function proposeIndex(deps, ctx, args, input) {
|
|
1794
|
+
const identity = resolveIdentity(ctx);
|
|
1795
|
+
const system = deps.datasetFor(SYSTEM_NAMESPACE);
|
|
1796
|
+
const now = /* @__PURE__ */ new Date();
|
|
1797
|
+
const rec = input.recommendationId === void 0 ? void 0 : await openRecommendation();
|
|
1798
|
+
async function openRecommendation() {
|
|
1799
|
+
const id = input.recommendationId;
|
|
1800
|
+
const row = system === void 0 ? void 0 : (await system.read(RECOMMENDATION_TYPE, {
|
|
1801
|
+
id,
|
|
1802
|
+
limit: 1
|
|
1803
|
+
})).rows[0];
|
|
1804
|
+
if (row === void 0) throw new ProposalError("unknown_recommendation", `no index recommendation '${id}'`);
|
|
1805
|
+
if (row.state !== "open") throw closed(id, row.state);
|
|
1806
|
+
if (row.namespace !== input.namespace || row.type !== input.type) throw invalid("recommendationId", `recommendation '${id}' is for ${row.namespace}/${row.type}`);
|
|
1807
|
+
if (input.action !== "dismiss" && row.action !== input.action) throw invalid("action", `recommendation '${id}' recommends '${String(row.action)}'`);
|
|
1808
|
+
return row;
|
|
1809
|
+
}
|
|
1810
|
+
const auditId = rec === void 0 ? randomUUID() : uuidFromName(`index_proposed:${String(rec.id)}`);
|
|
1811
|
+
let audited;
|
|
1812
|
+
const audit = async (detail) => {
|
|
1813
|
+
if (audited !== void 0) return;
|
|
1814
|
+
audited = detail;
|
|
1815
|
+
await system?.write("AuditEvent", [{
|
|
1816
|
+
id: auditId,
|
|
1817
|
+
ts: now.toISOString(),
|
|
1818
|
+
actorKid: identity.id,
|
|
1819
|
+
kind: "index_proposed",
|
|
1820
|
+
targetKid: `${input.namespace}/${input.type}`,
|
|
1821
|
+
clientIp: null,
|
|
1822
|
+
details: {
|
|
1823
|
+
action: input.action,
|
|
1824
|
+
reason: input.reason,
|
|
1825
|
+
recommendationId: rec?.id ?? null,
|
|
1826
|
+
...detail
|
|
1827
|
+
}
|
|
1828
|
+
}], {
|
|
1829
|
+
identity,
|
|
1830
|
+
now,
|
|
1831
|
+
insertOnly: true
|
|
1832
|
+
});
|
|
1833
|
+
};
|
|
1834
|
+
/** Moves the recommendation (if any) from `open` to `state`, atomically. */
|
|
1835
|
+
const close = async (state) => {
|
|
1836
|
+
if (rec === void 0 || system === void 0) return;
|
|
1837
|
+
const { matched } = await system.conditionalUpdate(RECOMMENDATION_TYPE, rec.id, { state: "open" }, { state }, identity, now);
|
|
1838
|
+
if (!matched) throw closed(rec.id);
|
|
1839
|
+
};
|
|
1840
|
+
let claimed;
|
|
1841
|
+
const claim = async (state) => {
|
|
1842
|
+
await close(state);
|
|
1843
|
+
if (rec !== void 0 && system !== void 0) claimed = state;
|
|
1844
|
+
};
|
|
1845
|
+
/** Gives a claim back (claimed → open) when the work behind it failed. */
|
|
1846
|
+
const release = async () => {
|
|
1847
|
+
const state = claimed;
|
|
1848
|
+
if (state === void 0 || rec === void 0 || system === void 0) return;
|
|
1849
|
+
claimed = void 0;
|
|
1850
|
+
const fail = (why) => {
|
|
1851
|
+
process.stderr.write(`[substrate-proposals] index recommendation ${String(rec.id)} stays ${state} after a failed ${input.action}: ${why}\n`);
|
|
1852
|
+
};
|
|
1853
|
+
try {
|
|
1854
|
+
const { matched } = await system.conditionalUpdate(RECOMMENDATION_TYPE, rec.id, { state }, { state: "open" }, identity, now);
|
|
1855
|
+
if (!matched) fail("it was changed meanwhile");
|
|
1856
|
+
} catch (err) {
|
|
1857
|
+
fail(err instanceof Error ? err.message : String(err));
|
|
1858
|
+
}
|
|
1859
|
+
};
|
|
1860
|
+
const stale = async (reason, code) => {
|
|
1861
|
+
await release();
|
|
1862
|
+
await close("superseded");
|
|
1863
|
+
throw new ProposalError("recommendation_stale", `index recommendation '${String(rec?.id)}' no longer holds: ${reason}`, code !== void 0 ? { reason: code } : {});
|
|
1864
|
+
};
|
|
1865
|
+
const call = {
|
|
1866
|
+
deps,
|
|
1867
|
+
args,
|
|
1868
|
+
input,
|
|
1869
|
+
identity,
|
|
1870
|
+
rec,
|
|
1871
|
+
audit,
|
|
1872
|
+
stale,
|
|
1873
|
+
claim,
|
|
1874
|
+
release
|
|
1875
|
+
};
|
|
1876
|
+
try {
|
|
1877
|
+
switch (input.action) {
|
|
1878
|
+
case "dismiss":
|
|
1879
|
+
if (rec === void 0) throw invalid("recommendationId", "'dismiss' needs a recommendationId");
|
|
1880
|
+
await claim("dismissed");
|
|
1881
|
+
await audit({});
|
|
1882
|
+
break;
|
|
1883
|
+
case "remove":
|
|
1884
|
+
await removeIndex(call, system, auditId);
|
|
1885
|
+
break;
|
|
1886
|
+
case "add":
|
|
1887
|
+
case "orderedBy": await changeProposedType(call);
|
|
1888
|
+
}
|
|
1889
|
+
} catch (err) {
|
|
1890
|
+
await release();
|
|
1891
|
+
throw err;
|
|
1892
|
+
}
|
|
1893
|
+
return {
|
|
1894
|
+
id: auditId,
|
|
1895
|
+
action: input.action,
|
|
1896
|
+
namespace: input.namespace,
|
|
1897
|
+
type: input.type,
|
|
1898
|
+
recommendationId: rec?.id ?? null,
|
|
1899
|
+
indexName: audited?.indexName ?? null
|
|
1900
|
+
};
|
|
1901
|
+
}
|
|
1902
|
+
/** The applied, live TypeProposal row of `type`, or undefined (a code-defined type). */
|
|
1903
|
+
async function proposalOf(binding, type) {
|
|
1904
|
+
return (await binding.dataset.read("TypeProposal", {
|
|
1905
|
+
where: { AND: [
|
|
1906
|
+
{ typeName: { eq: type } },
|
|
1907
|
+
{ applied: { eq: true } },
|
|
1908
|
+
{ supersededByCode: { eq: false } },
|
|
1909
|
+
{ archived: { eq: false } }
|
|
1910
|
+
] },
|
|
1911
|
+
limit: 1
|
|
1912
|
+
})).rows[0];
|
|
1913
|
+
}
|
|
1914
|
+
/**
|
|
1915
|
+
* Stores `next` on the proposal row, reinstalls the type with it, and tells
|
|
1916
|
+
* peers once the caller's transaction commits.
|
|
1917
|
+
*/
|
|
1918
|
+
async function storeTypeIndexOptions(call, binding, proposal, next) {
|
|
1919
|
+
const { deps, args, input, identity } = call;
|
|
1920
|
+
const { indexes: all, ...rest } = next;
|
|
1921
|
+
const opts = {
|
|
1922
|
+
...rest,
|
|
1923
|
+
...all?.length ? { indexes: all } : {}
|
|
1924
|
+
};
|
|
1925
|
+
await binding.dataset.write("TypeProposal", [{
|
|
1926
|
+
...bodyOf(proposal),
|
|
1927
|
+
indexesJson: opts.indexes !== void 0 ? JSON.stringify(opts.indexes) : null,
|
|
1928
|
+
orderedByJson: opts.orderedBy !== void 0 ? JSON.stringify(opts.orderedBy) : null
|
|
1929
|
+
}], { identity });
|
|
1930
|
+
applyTypeIndexOptions({
|
|
1931
|
+
namespace: input.namespace,
|
|
1932
|
+
typeName: input.type,
|
|
1933
|
+
...opts,
|
|
1934
|
+
registryStore: deps.registryStore,
|
|
1935
|
+
dataset: binding.dataset,
|
|
1936
|
+
edges: binding.edges
|
|
1937
|
+
});
|
|
1938
|
+
await notifyAfterCommit(args, () => deps.pubsub.notify(deps.channelFor(input.namespace), String(proposal.id)));
|
|
1939
|
+
}
|
|
1940
|
+
/** `remove`: retract a proposed declaration if that is all that wants it, then drop. */
|
|
1941
|
+
async function removeIndex(call, system, auditId) {
|
|
1942
|
+
const { deps, input, rec } = call;
|
|
1943
|
+
const name = input.indexName ?? rec?.indexName ?? void 0;
|
|
1944
|
+
if (name === void 0) throw invalid("indexName", "'remove' needs an indexName");
|
|
1945
|
+
const dataset = deps.datasetFor(input.namespace) ?? system?.forNamespace(input.namespace);
|
|
1946
|
+
await call.claim("accepted");
|
|
1947
|
+
if (rec === void 0 && dataset?.indexDeclarationOf !== void 0) {
|
|
1948
|
+
const binding = deps.registryStore.ownsNamespace(input.namespace) ? deps.registryStore.getBinding(input.namespace) : void 0;
|
|
1949
|
+
const proposal = binding === void 0 ? void 0 : await proposalOf(binding, input.type);
|
|
1950
|
+
const decl = proposal === void 0 ? void 0 : await dataset.indexDeclarationOf(input.type, name);
|
|
1951
|
+
const current = proposal === void 0 ? {} : typeIndexOptionsOf(proposal);
|
|
1952
|
+
const kept = (current.indexes ?? []).filter((d) => !sameJson(d, decl));
|
|
1953
|
+
if (binding !== void 0 && proposal !== void 0 && decl !== void 0 && kept.length < (current.indexes ?? []).length) {
|
|
1954
|
+
await call.audit({
|
|
1955
|
+
indexName: name,
|
|
1956
|
+
retracted: decl
|
|
1957
|
+
});
|
|
1958
|
+
await storeTypeIndexOptions(call, binding, proposal, {
|
|
1959
|
+
...current,
|
|
1960
|
+
indexes: kept
|
|
1961
|
+
});
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
try {
|
|
1965
|
+
if (dataset?.dropEngineIndex === void 0) throw Object.assign(/* @__PURE__ */ new Error(`no index ${name} on ${input.namespace}/${input.type}`), { code: "unknown_index" });
|
|
1966
|
+
await dataset.dropEngineIndex(input.type, name, { beforeDrop: () => call.audit({ indexName: name }) });
|
|
1967
|
+
} catch (err) {
|
|
1968
|
+
const code = err.code;
|
|
1969
|
+
if (typeof code !== "string" || !DROP_REFUSALS.has(code)) throw err;
|
|
1970
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
1971
|
+
if (rec !== void 0 && code !== "index_drop_timeout") {
|
|
1972
|
+
if (code === "unknown_index" && (await system?.read("AuditEvent", {
|
|
1973
|
+
id: auditId,
|
|
1974
|
+
limit: 1
|
|
1975
|
+
}))?.rows[0] !== void 0) return;
|
|
1976
|
+
await call.stale(message, code);
|
|
1977
|
+
}
|
|
1978
|
+
await call.release();
|
|
1979
|
+
throw new ProposalError(code, message, { indexName: name });
|
|
1980
|
+
}
|
|
1981
|
+
}
|
|
1982
|
+
/** `add` or `orderedBy` on a proposed type: update its row, then its registry entry. */
|
|
1983
|
+
async function changeProposedType(call) {
|
|
1984
|
+
const { deps, args, input, rec, stale } = call;
|
|
1985
|
+
const { namespace: ns, type } = input;
|
|
1986
|
+
const codeDefined = () => new ProposalError("index_on_code_defined_type", `${ns}/${type} is defined in code by the plugin that owns namespace '${ns}'; declare its ${input.action === "add" ? "index" : "orderedBy"} there`, { namespace: ns });
|
|
1987
|
+
if (!deps.registryStore.ownsNamespace(ns)) throw codeDefined();
|
|
1988
|
+
const binding = deps.registryStore.getBinding(ns);
|
|
1989
|
+
const entry = binding.registry.get(type);
|
|
1990
|
+
if (entry?.kind !== "type") throw new ProposalError("unknown_type", `no type '${type}' in namespace '${ns}'`);
|
|
1991
|
+
const proposal = await proposalOf(binding, type);
|
|
1992
|
+
if (proposal === void 0) throw codeDefined();
|
|
1993
|
+
const current = typeIndexOptionsOf(proposal);
|
|
1994
|
+
let next;
|
|
1995
|
+
let detail;
|
|
1996
|
+
if (input.action === "add") {
|
|
1997
|
+
let decl = input.declaration;
|
|
1998
|
+
if (decl === void 0 && rec !== void 0) try {
|
|
1999
|
+
decl = JSON.parse(rec.declarationJson);
|
|
2000
|
+
} catch {
|
|
2001
|
+
return stale("its declaration is not JSON");
|
|
2002
|
+
}
|
|
2003
|
+
if (decl === void 0) throw invalid("declaration", "'add' needs a declaration");
|
|
2004
|
+
const indexes = current.indexes ?? [];
|
|
2005
|
+
const known = indexes.some((d) => sameJson(d, decl));
|
|
2006
|
+
if (rec !== void 0 && !known) {
|
|
2007
|
+
const plan = await binding.dataset.indexPlan?.(type, ns) ?? { indexes: [] };
|
|
2008
|
+
const served = planServesShape(plan, String(rec.shape));
|
|
2009
|
+
if (served === void 0) return stale("its read shape does not parse");
|
|
2010
|
+
if (served) return stale("an index the type already has serves its read shape");
|
|
2011
|
+
}
|
|
2012
|
+
next = {
|
|
2013
|
+
...current,
|
|
2014
|
+
indexes: known ? indexes : [...indexes, decl]
|
|
2015
|
+
};
|
|
2016
|
+
detail = { declaration: decl };
|
|
2017
|
+
} else {
|
|
2018
|
+
if (input.orderedBy === void 0) throw invalid("orderedBy", "'orderedBy' needs an orderedBy");
|
|
2019
|
+
next = {
|
|
2020
|
+
...current,
|
|
2021
|
+
orderedBy: input.orderedBy
|
|
2022
|
+
};
|
|
2023
|
+
detail = { orderedBy: input.orderedBy };
|
|
2024
|
+
}
|
|
2025
|
+
try {
|
|
2026
|
+
assertValidTypeIndexOptions(type, entry.fields, next);
|
|
2027
|
+
} catch (err) {
|
|
2028
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
2029
|
+
if (rec !== void 0) return stale(message);
|
|
2030
|
+
throw invalid(input.action === "add" ? "declaration" : "orderedBy", message);
|
|
2031
|
+
}
|
|
2032
|
+
await call.claim("accepted");
|
|
2033
|
+
await call.audit(detail);
|
|
2034
|
+
await storeTypeIndexOptions(call, binding, proposal, next);
|
|
2035
|
+
ensureStorageAfterCommit(args.afterTx, binding.dataset, type, binding.engine, deps.registryStore.storage);
|
|
2036
|
+
}
|
|
2037
|
+
|
|
1421
2038
|
//#endregion
|
|
1422
2039
|
//#region ../../plugins/substrate-proposals/src/propose-type-resolver.ts
|
|
1423
2040
|
/**
|
|
@@ -1441,6 +2058,8 @@ async function proposeFieldLocked(deps, ctx, args, input) {
|
|
|
1441
2058
|
function parseInput(params) {
|
|
1442
2059
|
const { proposedReason } = params;
|
|
1443
2060
|
if (proposedReason !== void 0 && typeof proposedReason !== "string") throw new ProposalError("invalid_params", "'proposedReason' must be a string", { param: "proposedReason" });
|
|
2061
|
+
const { expectedSize } = params;
|
|
2062
|
+
if (expectedSize !== void 0 && expectedSize !== "small" && expectedSize !== "large") throw new ProposalError("invalid_params", "'expectedSize' must be 'small' or 'large'", { param: "expectedSize" });
|
|
1444
2063
|
return {
|
|
1445
2064
|
...params,
|
|
1446
2065
|
proposedReason: proposedReason ?? ""
|
|
@@ -1451,6 +2070,12 @@ function makeProposeTypeResolver(deps) {
|
|
|
1451
2070
|
if (!deps.ownedNamespaces.has(deps.namespace) && !deps.registryStore.has(deps.namespace)) throw new ProposalError("unowned_namespace", `Namespace '${deps.namespace}' is not owned by this plugin instance`);
|
|
1452
2071
|
const input = parseInput(args.params ?? {});
|
|
1453
2072
|
assertValidTypeProposal(input.typeName, input.fields, (ns, type) => ns === deps.namespace && type === input.typeName || deps.registryStore.ownsType(ns, type));
|
|
2073
|
+
for (const name of Object.keys(input.fields)) assertNotEngineInjected(name);
|
|
2074
|
+
try {
|
|
2075
|
+
assertValidTypeIndexOptions(input.typeName, proposedFieldDefs(input.fields), indexOptionsOf(input));
|
|
2076
|
+
} catch (cause) {
|
|
2077
|
+
throw new ProposalError("invalid_params", cause instanceof Error ? cause.message : String(cause), {}, { cause });
|
|
2078
|
+
}
|
|
1454
2079
|
const dataset = args.dataset ?? deps.dataset;
|
|
1455
2080
|
return withProposalLock(dataset, proposalLockKey("type", deps.namespace, input.typeName), () => proposeTypeLocked(deps, ctx, {
|
|
1456
2081
|
...args,
|
|
@@ -1458,6 +2083,30 @@ function makeProposeTypeResolver(deps) {
|
|
|
1458
2083
|
}, input));
|
|
1459
2084
|
};
|
|
1460
2085
|
}
|
|
2086
|
+
function indexOptionsOf(input) {
|
|
2087
|
+
const { indexes, orderedBy, expectedSize } = input;
|
|
2088
|
+
return {
|
|
2089
|
+
...indexes?.length ? { indexes } : {},
|
|
2090
|
+
...orderedBy !== void 0 ? { orderedBy } : {},
|
|
2091
|
+
...expectedSize !== void 0 ? { expectedSize } : {}
|
|
2092
|
+
};
|
|
2093
|
+
}
|
|
2094
|
+
/**
|
|
2095
|
+
* Whether `typeName` has storage already: any TypeProposal row by that name,
|
|
2096
|
+
* or any row of the type, archived and tombstoned ones included, so a
|
|
2097
|
+
* propose-only caller cannot clear the signal by deleting them.
|
|
2098
|
+
*/
|
|
2099
|
+
async function hasStorage(dataset, typeName) {
|
|
2100
|
+
if ((await dataset.read("TypeProposal", {
|
|
2101
|
+
where: { typeName: { eq: typeName } },
|
|
2102
|
+
includeDeleted: true,
|
|
2103
|
+
limit: 1
|
|
2104
|
+
})).rows.length > 0) return true;
|
|
2105
|
+
return (await dataset.read(typeName, {
|
|
2106
|
+
includeDeleted: true,
|
|
2107
|
+
limit: 1
|
|
2108
|
+
})).rows.length > 0;
|
|
2109
|
+
}
|
|
1461
2110
|
/** Steps 2-7, run while holding the proposal lock for this type name. */
|
|
1462
2111
|
async function proposeTypeLocked(deps, ctx, args, input) {
|
|
1463
2112
|
const { typeName, fields, proposedReason } = input;
|
|
@@ -1483,12 +2132,9 @@ async function proposeTypeLocked(deps, ctx, args, input) {
|
|
|
1483
2132
|
}).find((key) => liveRegistry.has(key));
|
|
1484
2133
|
if (takenKey !== void 0) throw new ProposalError("type_already_exists", `Type '${typeName}' would register operation '${takenKey}', which is already registered in namespace '${deps.namespace}'`);
|
|
1485
2134
|
const proposedAt = /* @__PURE__ */ new Date();
|
|
1486
|
-
const fieldsJson = JSON.stringify(fields);
|
|
1487
|
-
const newId = randomUUID();
|
|
1488
2135
|
const initialParams = {
|
|
1489
|
-
id: newId,
|
|
1490
2136
|
typeName,
|
|
1491
|
-
fieldsJson,
|
|
2137
|
+
fieldsJson: JSON.stringify(fields),
|
|
1492
2138
|
proposedBy,
|
|
1493
2139
|
proposedAt,
|
|
1494
2140
|
proposedReason,
|
|
@@ -1496,22 +2142,56 @@ async function proposeTypeLocked(deps, ctx, args, input) {
|
|
|
1496
2142
|
appliedAt: null,
|
|
1497
2143
|
resultingSchemaVersion: null,
|
|
1498
2144
|
supersededByCode: false,
|
|
1499
|
-
archived: false
|
|
2145
|
+
archived: false,
|
|
2146
|
+
expectedSize: input.expectedSize ?? null
|
|
2147
|
+
};
|
|
2148
|
+
const indexOpts = indexOptionsOf(input);
|
|
2149
|
+
const indexFields = {
|
|
2150
|
+
indexesJson: indexOpts.indexes ? JSON.stringify(indexOpts.indexes) : null,
|
|
2151
|
+
orderedByJson: indexOpts.orderedBy ? JSON.stringify(indexOpts.orderedBy) : null
|
|
1500
2152
|
};
|
|
1501
2153
|
const binding = deps.registryStore.getBinding(deps.namespace);
|
|
1502
2154
|
const engine = binding.engine;
|
|
1503
|
-
|
|
1504
|
-
|
|
2155
|
+
let newId;
|
|
2156
|
+
if (engine !== void 0) {
|
|
2157
|
+
const declaresIndexes = indexOpts.indexes !== void 0 || indexOpts.orderedBy !== void 0;
|
|
2158
|
+
newId = (await writeInTx(binding.registry, ctx, args, "createTypeProposal", {
|
|
2159
|
+
...initialParams,
|
|
2160
|
+
...indexFields
|
|
2161
|
+
}, !(declaresIndexes && await hasStorage(args.dataset, typeName)))).id;
|
|
2162
|
+
} else {
|
|
2163
|
+
const id = (await args.dataset.write("TypeProposal", [{
|
|
2164
|
+
...initialParams,
|
|
2165
|
+
...indexFields
|
|
2166
|
+
}])).ids[0];
|
|
2167
|
+
if (id === void 0) throw new Error("dataset.write did not return an id for TypeProposal");
|
|
2168
|
+
newId = id;
|
|
2169
|
+
}
|
|
1505
2170
|
try {
|
|
1506
|
-
applyUntilRollback(args, binding.registry, () =>
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
2171
|
+
applyUntilRollback(args, binding.registry, () => {
|
|
2172
|
+
applyTypeProposal({
|
|
2173
|
+
namespace: deps.namespace,
|
|
2174
|
+
typeName,
|
|
2175
|
+
fields,
|
|
2176
|
+
registryStore: deps.registryStore,
|
|
2177
|
+
dataset: deps.dataset,
|
|
2178
|
+
edges: deps.edges
|
|
2179
|
+
});
|
|
2180
|
+
if (Object.keys(indexOpts).length > 0) try {
|
|
2181
|
+
applyTypeIndexOptions({
|
|
2182
|
+
namespace: deps.namespace,
|
|
2183
|
+
typeName,
|
|
2184
|
+
...indexOpts,
|
|
2185
|
+
registryStore: deps.registryStore,
|
|
2186
|
+
dataset: deps.dataset,
|
|
2187
|
+
edges: deps.edges
|
|
2188
|
+
});
|
|
2189
|
+
} catch (cause) {
|
|
2190
|
+
throw new ProposalError("invalid_params", cause instanceof Error ? cause.message : String(cause), {}, { cause });
|
|
2191
|
+
}
|
|
2192
|
+
}, () => binding.invalidateSchemaCaches?.());
|
|
1514
2193
|
} catch (cause) {
|
|
2194
|
+
if (cause instanceof ProposalError) throw cause;
|
|
1515
2195
|
throw new ProposalError("type_already_exists", `Could not apply type '${typeName}': ${cause instanceof Error ? cause.message : String(cause)}`, {}, { cause });
|
|
1516
2196
|
}
|
|
1517
2197
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -1539,6 +2219,7 @@ async function proposeTypeLocked(deps, ctx, args, input) {
|
|
|
1539
2219
|
await args.dataset.write("TypeProposal", [appliedRow]);
|
|
1540
2220
|
}
|
|
1541
2221
|
await notifyAfterCommit(args, () => deps.pubsub.notify(deps.channelFor(deps.namespace), String(newId)));
|
|
2222
|
+
ensureStorageAfterCommit(args.afterTx, deps.dataset, typeName, engine, deps.registryStore.storage);
|
|
1542
2223
|
return appliedRow;
|
|
1543
2224
|
}
|
|
1544
2225
|
|
|
@@ -1554,6 +2235,17 @@ async function proposeTypeLocked(deps, ctx, args, input) {
|
|
|
1554
2235
|
*/
|
|
1555
2236
|
var RegistryStore = class {
|
|
1556
2237
|
bindings = /* @__PURE__ */ new Map();
|
|
2238
|
+
#storageAbort = new AbortController();
|
|
2239
|
+
/** This plugin instance's background storage work. */
|
|
2240
|
+
storage = {
|
|
2241
|
+
signal: this.#storageAbort.signal,
|
|
2242
|
+
pending: /* @__PURE__ */ new Set()
|
|
2243
|
+
};
|
|
2244
|
+
/** Aborts the background storage work (an interrupted build is left INVALID for the next reconcile) and awaits it. */
|
|
2245
|
+
async stopStorage() {
|
|
2246
|
+
this.#storageAbort.abort();
|
|
2247
|
+
await Promise.all([...this.storage.pending]);
|
|
2248
|
+
}
|
|
1557
2249
|
/**
|
|
1558
2250
|
* Register a full NamespaceBinding for the given namespace.
|
|
1559
2251
|
*
|
|
@@ -1644,6 +2336,7 @@ function createProposalsPlugin(config) {
|
|
|
1644
2336
|
const registryStore = new RegistryStore();
|
|
1645
2337
|
const capability = config.proposeCapability ?? "substrate.propose";
|
|
1646
2338
|
const policy = makeProposePolicy(capability);
|
|
2339
|
+
const indexCapability = config.indexCapability ?? "substrate.index";
|
|
1647
2340
|
let lookupDataset = config.datasetFor ?? (() => void 0);
|
|
1648
2341
|
/**
|
|
1649
2342
|
* Shared resolver-registration helper. Used at static-ns install time AND
|
|
@@ -1694,10 +2387,21 @@ function createProposalsPlugin(config) {
|
|
|
1694
2387
|
edges: ctx.edges,
|
|
1695
2388
|
pubsub: config.pubsub,
|
|
1696
2389
|
proposeCapability: capability,
|
|
2390
|
+
indexCapability,
|
|
1697
2391
|
registerProposalMutations
|
|
1698
2392
|
}),
|
|
1699
2393
|
policy
|
|
1700
2394
|
});
|
|
2395
|
+
ctx.registry.register("propose_index", {
|
|
2396
|
+
kind: "mutation",
|
|
2397
|
+
resolver: makeProposeIndexResolver({
|
|
2398
|
+
registryStore,
|
|
2399
|
+
datasetFor: (ns) => lookupDataset(ns),
|
|
2400
|
+
pubsub: config.pubsub,
|
|
2401
|
+
channelFor: (n) => `_substrate_proposals_${n}`
|
|
2402
|
+
}),
|
|
2403
|
+
policy: makeIndexPolicy(indexCapability)
|
|
2404
|
+
});
|
|
1701
2405
|
}
|
|
1702
2406
|
}, ...config.ownedNamespaces.map((ns) => ({
|
|
1703
2407
|
namespace: ns,
|
|
@@ -1707,7 +2411,7 @@ function createProposalsPlugin(config) {
|
|
|
1707
2411
|
dataset: ctx.dataset,
|
|
1708
2412
|
edges: ctx.edges
|
|
1709
2413
|
});
|
|
1710
|
-
defineProposalTypes(ns, capability).install(ctx);
|
|
2414
|
+
defineProposalTypes(ns, capability, indexCapability).install(ctx);
|
|
1711
2415
|
registerProposalMutations({
|
|
1712
2416
|
namespace: ns,
|
|
1713
2417
|
dataset: ctx.dataset,
|
|
@@ -1724,6 +2428,18 @@ function createProposalsPlugin(config) {
|
|
|
1724
2428
|
registryStore.setInvalidator(CONTROL_NAMESPACE, () => ctx.engine.invalidateSchemaCaches(CONTROL_NAMESPACE));
|
|
1725
2429
|
registryStore.setEngine(CONTROL_NAMESPACE, ctx.engine);
|
|
1726
2430
|
if (config.datasetFor === void 0) lookupDataset = (ns) => ctx.engine.datasetFor(ns);
|
|
2431
|
+
const log = config.log ?? ((line) => console.warn(line));
|
|
2432
|
+
const skipped = (ns, row, err) => {
|
|
2433
|
+
ctx.metric("_system_proposals.replay_skipped", {
|
|
2434
|
+
namespace: ns,
|
|
2435
|
+
id: row.id,
|
|
2436
|
+
type: row.typeName ?? row.targetType,
|
|
2437
|
+
field: row.fieldName ?? null,
|
|
2438
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2439
|
+
});
|
|
2440
|
+
const line = reservedSkipLine(ns, row, err);
|
|
2441
|
+
if (line !== void 0) log(line);
|
|
2442
|
+
};
|
|
1727
2443
|
const controlBinding = registryStore.getBinding(CONTROL_NAMESPACE);
|
|
1728
2444
|
const hotAddedNamespaces = await reconcileClusterNamespaces({
|
|
1729
2445
|
controlNamespace: CONTROL_NAMESPACE,
|
|
@@ -1732,6 +2448,7 @@ function createProposalsPlugin(config) {
|
|
|
1732
2448
|
datasetFor: lookupDataset,
|
|
1733
2449
|
edges: controlBinding.edges,
|
|
1734
2450
|
proposeCapability: capability,
|
|
2451
|
+
indexCapability,
|
|
1735
2452
|
registerProposalMutations,
|
|
1736
2453
|
onSkip: (row, err) => ctx.metric("_system_proposals.replay_skipped", {
|
|
1737
2454
|
namespace: CONTROL_NAMESPACE,
|
|
@@ -1746,17 +2463,14 @@ function createProposalsPlugin(config) {
|
|
|
1746
2463
|
await reconcileNamespace({
|
|
1747
2464
|
namespace: ns,
|
|
1748
2465
|
registryStore,
|
|
1749
|
-
onSkip: (row, err) =>
|
|
1750
|
-
namespace: ns,
|
|
1751
|
-
id: row.id,
|
|
1752
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1753
|
-
})
|
|
2466
|
+
onSkip: (row, err) => skipped(ns, row, err)
|
|
1754
2467
|
});
|
|
1755
2468
|
await setupNamespaceListener({
|
|
1756
2469
|
namespace: ns,
|
|
1757
2470
|
channel: `_substrate_proposals_${ns}`,
|
|
1758
2471
|
pubsub: config.pubsub,
|
|
1759
|
-
registryStore
|
|
2472
|
+
registryStore,
|
|
2473
|
+
onSkip: (row, err) => skipped(ns, row, err)
|
|
1760
2474
|
});
|
|
1761
2475
|
ctx.metric("_system_proposals.namespace_ready", { namespace: ns });
|
|
1762
2476
|
}
|
|
@@ -1787,11 +2501,15 @@ function createProposalsPlugin(config) {
|
|
|
1787
2501
|
edges: controlBinding.edges,
|
|
1788
2502
|
metric: ctx.metric,
|
|
1789
2503
|
proposeCapability: capability,
|
|
2504
|
+
indexCapability,
|
|
1790
2505
|
registerProposalMutations
|
|
1791
2506
|
});
|
|
2507
|
+
},
|
|
2508
|
+
async onStop() {
|
|
2509
|
+
await registryStore.stopStorage();
|
|
1792
2510
|
}
|
|
1793
2511
|
};
|
|
1794
2512
|
}
|
|
1795
2513
|
|
|
1796
2514
|
//#endregion
|
|
1797
|
-
export { createNoopPubSub as n,
|
|
2515
|
+
export { ProposalError as i, createNoopPubSub as n, storageSettled as r, createProposalsPlugin as t };
|