@mnmal-ai/substrate 1.0.0-alpha.2 → 1.0.0-alpha.20
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-BaDqbBS-.js +4062 -0
- package/dist/{runtime-metrics-DwpDJ8vz.d.ts → bridge-BhiJKwfF.d.ts} +2 -87
- package/dist/cli-RRFphMtQ.js +3366 -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-CYOaWZ8l.d.ts} +44 -4
- package/dist/{fake-DUVtKVSE.js → fake-DdXtb-m8.js} +0 -1
- package/dist/{index-D7mSOFlM.d.ts → index-ZKeYFHDD.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-C-IRv-O9.js +443 -0
- package/dist/{plugin-QuJN9pG_.js → plugin-MSbSZ6Al.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-Ob5OyQqj.js} +65 -33
- package/dist/{row-ByLFE2Wx.d.ts → row-CfsHSEfB.d.ts} +92 -12
- package/dist/run-B438GGJK.js +2435 -0
- package/dist/{plugin-t2g2wX0b.d.ts → schema-laGLMvMC.d.ts} +1287 -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-DMzc3HfW.js → src-BNdhH_Hx.js} +745 -851
- package/dist/{src-CG7giuHr.js → src-BSL2yyVy.js} +779 -75
- package/dist/{src-BLgbLq2k.js → src-B_kHkbsr.js} +780 -330
- package/dist/{src-DWm0iA88.js → src-De9j33jv.js} +188 -87
- package/dist/src-DkxrpQOz.js +7709 -0
- package/dist/{row-DZ0Fnpc6.js → src-DmiUWOLp.js} +2 -1
- package/dist/{src-CHp3QyqB.js → src-S84ef4e3.js} +9177 -3377
- 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-BT8kL1IF.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-S84ef4e3.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;
|
|
@@ -267,6 +298,11 @@ function stringifyFieldDef(def) {
|
|
|
267
298
|
*/
|
|
268
299
|
function assertValidFieldSpec(fieldName, spec) {
|
|
269
300
|
assertProposableName("fieldName", fieldName);
|
|
301
|
+
const reserved = reservedMetadataFieldMessage(fieldName);
|
|
302
|
+
if (reserved !== void 0) throw new ProposalError("invalid_params", reserved, {
|
|
303
|
+
param: "fieldName",
|
|
304
|
+
fieldName
|
|
305
|
+
});
|
|
270
306
|
if (typeof spec !== "object" || spec === null) throw new ProposalError("invalid_field_spec", `Spec for field '${fieldName}' must be an object`, { fieldName });
|
|
271
307
|
try {
|
|
272
308
|
parseFieldSpec(spec);
|
|
@@ -281,7 +317,8 @@ function assertValidFieldSpec(fieldName, spec) {
|
|
|
281
317
|
* substrate-proposals namespace definition.
|
|
282
318
|
*
|
|
283
319
|
* Defines two types for schema evolution proposals:
|
|
284
|
-
* - TypeProposal: proposes a new type (multiple fields at once)
|
|
320
|
+
* - TypeProposal: proposes a new type (multiple fields at once), and holds
|
|
321
|
+
* the indexes and natural order `propose_index` gives it
|
|
285
322
|
* - FieldProposal: proposes a single field on an existing type
|
|
286
323
|
*
|
|
287
324
|
* Both track proposal metadata (proposedBy, proposedAt, reason) and application
|
|
@@ -293,8 +330,8 @@ function assertValidFieldSpec(fieldName, spec) {
|
|
|
293
330
|
* the propose resolvers, held under a per-name lock (`proposal-lock.ts`), not
|
|
294
331
|
* by a schema or database constraint.
|
|
295
332
|
*/
|
|
296
|
-
function defineProposalTypes(namespace, capability = DEFAULT_PROPOSE_CAPABILITY) {
|
|
297
|
-
const policy = proposeToWrite(capability);
|
|
333
|
+
function defineProposalTypes(namespace, capability = DEFAULT_PROPOSE_CAPABILITY, indexCapability = DEFAULT_INDEX_CAPABILITY) {
|
|
334
|
+
const policy = proposeToWrite(capability, indexCapability);
|
|
298
335
|
return defineNamespace(namespace, {
|
|
299
336
|
TypeProposal: {
|
|
300
337
|
policy,
|
|
@@ -308,7 +345,22 @@ function defineProposalTypes(namespace, capability = DEFAULT_PROPOSE_CAPABILITY)
|
|
|
308
345
|
appliedAt: field.date({ nullable: true }),
|
|
309
346
|
resultingSchemaVersion: field.string({ nullable: true }),
|
|
310
347
|
supersededByCode: field.boolean(),
|
|
311
|
-
archived: field.boolean()
|
|
348
|
+
archived: field.boolean(),
|
|
349
|
+
/** `IndexDecl[]` as JSON, set by `propose_index add`; null for none. */
|
|
350
|
+
indexesJson: field.string({
|
|
351
|
+
indexed: false,
|
|
352
|
+
nullable: true
|
|
353
|
+
}),
|
|
354
|
+
/** `OrderedBy` as JSON, set by `propose_index orderedBy`; null for the default. */
|
|
355
|
+
orderedByJson: field.string({
|
|
356
|
+
indexed: false,
|
|
357
|
+
nullable: true
|
|
358
|
+
}),
|
|
359
|
+
/** `'small' | 'large'`, the proposer's size hint for the index advisor; null when none. */
|
|
360
|
+
expectedSize: field.enum(["small", "large"], {
|
|
361
|
+
indexed: false,
|
|
362
|
+
nullable: true
|
|
363
|
+
})
|
|
312
364
|
}
|
|
313
365
|
},
|
|
314
366
|
FieldProposal: {
|
|
@@ -375,10 +427,13 @@ function assertValidFieldProposal(targetType, fieldName, fieldSpec, ownsTarget)
|
|
|
375
427
|
}
|
|
376
428
|
/** Installs the proposed type's autoCRUD surface into `registry`. */
|
|
377
429
|
function installProposedType(registry, args) {
|
|
430
|
+
installAutoCrud(registry, args.typeName, args.dataset, args.edges, { fields: proposedFieldDefs(args.fields) });
|
|
431
|
+
}
|
|
432
|
+
/** A proposed type's field specs as FieldDefs, with the engine-reserved fields added. */
|
|
433
|
+
function proposedFieldDefs(fields) {
|
|
378
434
|
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 });
|
|
435
|
+
for (const [name, spec] of Object.entries(fields)) fieldDefs[name] = parseFieldSpec(spec);
|
|
436
|
+
return Object.assign(fieldDefs, RESERVED_AUTOCRUD_FIELDS);
|
|
382
437
|
}
|
|
383
438
|
/**
|
|
384
439
|
* Every registry key `applyTypeProposal` would register for this proposal —
|
|
@@ -405,6 +460,95 @@ function applyTypeProposal(args) {
|
|
|
405
460
|
binding.invalidateSchemaCaches?.();
|
|
406
461
|
}
|
|
407
462
|
/**
|
|
463
|
+
* Parses a TypeProposal row's `indexesJson` / `orderedByJson` / `expectedSize`
|
|
464
|
+
* (null or absent: none). An `expectedSize` other than `small` or `large` is
|
|
465
|
+
* left out and handed to `onInvalidSize`, so a bad hint never costs the type
|
|
466
|
+
* its indexes or order.
|
|
467
|
+
*/
|
|
468
|
+
function typeIndexOptionsOf(row, onInvalidSize) {
|
|
469
|
+
const parse = (v) => typeof v === "string" ? JSON.parse(v) : void 0;
|
|
470
|
+
const indexes = parse(row.indexesJson);
|
|
471
|
+
const orderedBy = parse(row.orderedByJson);
|
|
472
|
+
const size = row.expectedSize ?? void 0;
|
|
473
|
+
const expectedSize = size === "small" || size === "large" ? size : void 0;
|
|
474
|
+
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`));
|
|
475
|
+
return {
|
|
476
|
+
...indexes !== void 0 ? { indexes } : {},
|
|
477
|
+
...orderedBy !== void 0 ? { orderedBy } : {},
|
|
478
|
+
...expectedSize !== void 0 ? { expectedSize } : {}
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Refuse indexes or a natural order `propose_index` would not accept: each
|
|
483
|
+
* index as code may declare it, but never unique and never named (a name
|
|
484
|
+
* adopts whatever index already holds it), so every one is the engine's own,
|
|
485
|
+
* non-unique and generated. Rows are caller-written, so replay checks too.
|
|
486
|
+
*
|
|
487
|
+
* @throws Error naming the first problem
|
|
488
|
+
*/
|
|
489
|
+
function assertValidTypeIndexOptions(typeName, fields, opts) {
|
|
490
|
+
if (opts.indexes !== void 0 && !Array.isArray(opts.indexes)) throw new Error(`indexes on ${typeName} must be an array`);
|
|
491
|
+
for (const decl of opts.indexes ?? []) {
|
|
492
|
+
if (typeof decl !== "object" || decl === null) throw new Error(`index on ${typeName}: must be an object`);
|
|
493
|
+
assertIndexDecl(typeName, fields, decl);
|
|
494
|
+
if (decl.unique !== void 0) throw new Error(`index on ${typeName}: a proposed index is never unique`);
|
|
495
|
+
if (decl.name !== void 0) throw new Error(`index on ${typeName}: a proposed index takes a generated name`);
|
|
496
|
+
}
|
|
497
|
+
if (opts.orderedBy !== void 0) assertOrderedBy(typeName, {
|
|
498
|
+
fields,
|
|
499
|
+
orderedBy: opts.orderedBy
|
|
500
|
+
});
|
|
501
|
+
if (opts.expectedSize !== void 0 && opts.expectedSize !== "small" && opts.expectedSize !== "large") throw new Error(`expectedSize on ${typeName} must be 'small' or 'large'`);
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Reinstall `typeName`'s autoCRUD surface with `patch` over the options it was
|
|
505
|
+
* installed with (policy, postWrite, purge, indexes, order), swapping every
|
|
506
|
+
* entry through `registry.replace` so DuplicateRegistration is never thrown.
|
|
507
|
+
*/
|
|
508
|
+
function reinstallType(registry, typeName, dataset, edges, patch) {
|
|
509
|
+
const existing = registry.get(typeName);
|
|
510
|
+
if (!existing) throw new Error(`type '${typeName}' not registered`);
|
|
511
|
+
const replaceWriter = {
|
|
512
|
+
register: (key, input) => registry.replace(key, input),
|
|
513
|
+
replace: registry.replace.bind(registry),
|
|
514
|
+
get: registry.get.bind(registry)
|
|
515
|
+
};
|
|
516
|
+
const prior = autoCrudOptionsOf(existing.resolver) ?? {
|
|
517
|
+
policy: existing.policy,
|
|
518
|
+
fields: existing.fields
|
|
519
|
+
};
|
|
520
|
+
installAutoCrud(replaceWriter, typeName, dataset, edges, patch(prior));
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Give a registered proposed type the indexes and natural order its row
|
|
524
|
+
* stores, validated against the fields it has now (proposed ones included).
|
|
525
|
+
* Storage builds them on the next `ensureTypeStorage`.
|
|
526
|
+
*/
|
|
527
|
+
function applyTypeIndexOptions(args) {
|
|
528
|
+
const binding = args.registryStore.getBinding(args.namespace);
|
|
529
|
+
const existing = binding.registry.get(args.typeName);
|
|
530
|
+
if (!existing) throw new Error(`type '${args.typeName}' not registered`);
|
|
531
|
+
assertValidTypeIndexOptions(args.typeName, existing.fields, args);
|
|
532
|
+
reinstallType(binding.registry, args.typeName, args.dataset, args.edges, (prior) => {
|
|
533
|
+
const { indexes: _i, orderedBy: _o, expectedSize: _s, ...rest } = prior;
|
|
534
|
+
return {
|
|
535
|
+
...rest,
|
|
536
|
+
...args.indexes !== void 0 ? { indexes: args.indexes } : {},
|
|
537
|
+
...args.orderedBy !== void 0 ? { orderedBy: args.orderedBy } : {},
|
|
538
|
+
...args.expectedSize !== void 0 ? { expectedSize: args.expectedSize } : {}
|
|
539
|
+
};
|
|
540
|
+
});
|
|
541
|
+
binding.invalidateSchemaCaches?.();
|
|
542
|
+
}
|
|
543
|
+
/** Whether the type entry already carries exactly these options. */
|
|
544
|
+
function hasTypeIndexOptions(entry, opts) {
|
|
545
|
+
return sameJson(entry.indexes, opts.indexes) && sameJson(entry.orderedBy, opts.orderedBy) && entry.expectedSize === opts.expectedSize;
|
|
546
|
+
}
|
|
547
|
+
/** Deep equality of JSON values, whatever order their keys were written in. */
|
|
548
|
+
function sameJson(a, b) {
|
|
549
|
+
return JSON.stringify(canonicalize$1(a)) === JSON.stringify(canonicalize$1(b));
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
408
552
|
* Extend an already-registered type with a new field.
|
|
409
553
|
*
|
|
410
554
|
* Replaces the type entry + all autoCRUD mutations atomically with versions
|
|
@@ -419,36 +563,34 @@ function applyFieldProposal(args) {
|
|
|
419
563
|
const store = args.registryStore;
|
|
420
564
|
assertValidFieldProposal(args.targetType, args.fieldName, args.fieldSpec, (ns) => store.ownsNamespace(ns));
|
|
421
565
|
const binding = store.getBinding(args.namespace);
|
|
422
|
-
const
|
|
423
|
-
const existing = registry.get(args.targetType);
|
|
566
|
+
const existing = binding.registry.get(args.targetType);
|
|
424
567
|
if (!existing) throw new Error(`applyFieldProposal: target type '${args.targetType}' not registered`);
|
|
425
568
|
const nextFields = {
|
|
426
569
|
...existing.fields,
|
|
427
570
|
[args.fieldName]: parseFieldSpec(args.fieldSpec)
|
|
428
571
|
};
|
|
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, {
|
|
572
|
+
reinstallType(binding.registry, args.targetType, args.dataset, args.edges, (prior) => ({
|
|
435
573
|
...prior,
|
|
436
574
|
fields: nextFields
|
|
437
|
-
});
|
|
575
|
+
}));
|
|
438
576
|
binding.invalidateSchemaCaches?.();
|
|
439
577
|
}
|
|
440
578
|
|
|
441
579
|
//#endregion
|
|
442
580
|
//#region ../../plugins/substrate-proposals/src/engine-write.ts
|
|
443
581
|
/**
|
|
582
|
+
* Shared helpers used by the propose_* resolvers.
|
|
583
|
+
*/
|
|
584
|
+
/**
|
|
444
585
|
* Run the autoCRUD create-or-update `op` registered on `registry` inside the
|
|
445
586
|
* caller's own transaction (`args.dataset`), and return the resulting row.
|
|
446
587
|
* A nested `engine.mutate` would open a second transaction, and so take a
|
|
447
588
|
* second pooled connection, beside the one the proposal already holds.
|
|
448
589
|
*
|
|
449
|
-
* @throws
|
|
590
|
+
* @throws ResolverFrameError `policy_denied` when its policy denies
|
|
591
|
+
* @throws Error when `op` is not registered or returns no row
|
|
450
592
|
*/
|
|
451
|
-
async function writeInTx(registry, ctx, args, op, params) {
|
|
593
|
+
async function writeInTx(registry, ctx, args, op, params, skipPolicy = false) {
|
|
452
594
|
const entry = registry.get(op);
|
|
453
595
|
if (entry === void 0) throw new Error(`'${op}' is not registered`);
|
|
454
596
|
const opCtx = {
|
|
@@ -461,8 +603,8 @@ async function writeInTx(registry, ctx, args, op, params) {
|
|
|
461
603
|
params,
|
|
462
604
|
fields: { id: true }
|
|
463
605
|
};
|
|
464
|
-
const decision = entry.policy?.(opCtx, opArgs);
|
|
465
|
-
if (decision !== void 0 && !decision.allow) throw new
|
|
606
|
+
const decision = skipPolicy ? void 0 : entry.policy?.(opCtx, opArgs);
|
|
607
|
+
if (decision !== void 0 && !decision.allow) throw new ResolverFrameError("policy_denied", `'${op}' denied: ${decision.reason ?? "policy denied"}`);
|
|
466
608
|
const row = await entry.resolver(opCtx, opArgs);
|
|
467
609
|
if (row === null || typeof row !== "object" || Array.isArray(row)) throw new Error(`'${op}' returned no row`);
|
|
468
610
|
return row;
|
|
@@ -511,6 +653,61 @@ async function notifyAfterCommit(args, notify) {
|
|
|
511
653
|
if (committed) await notify();
|
|
512
654
|
});
|
|
513
655
|
}
|
|
656
|
+
const pendingStorage = /* @__PURE__ */ new Set();
|
|
657
|
+
/** Resolves once every storage preparation started so far has settled. */
|
|
658
|
+
async function storageSettled() {
|
|
659
|
+
await Promise.all([...pendingStorage]);
|
|
660
|
+
}
|
|
661
|
+
function prepareStorage(dataset, type, handle, scope) {
|
|
662
|
+
if (scope?.signal.aborted === true) return;
|
|
663
|
+
const work = (async () => {
|
|
664
|
+
try {
|
|
665
|
+
const pending = (await dataset.ensureTypeStorage?.(type, {
|
|
666
|
+
newFields: "pending",
|
|
667
|
+
...scope ? { signal: scope.signal } : {}
|
|
668
|
+
}))?.pending ?? [];
|
|
669
|
+
if (handle !== void 0 && pending.length > 0) await emitFieldEvents(handle, pending.map((field) => ({
|
|
670
|
+
kind: "field_pending",
|
|
671
|
+
namespace: dataset.namespace,
|
|
672
|
+
type,
|
|
673
|
+
field,
|
|
674
|
+
attempts: 0
|
|
675
|
+
}))).catch((err) => {
|
|
676
|
+
process.stderr.write(`[substrate-proposals] field_pending for ${dataset.namespace}/${type} not audited: ${err instanceof Error ? err.message : String(err)}\n`);
|
|
677
|
+
});
|
|
678
|
+
} catch (err) {
|
|
679
|
+
process.stderr.write(`[substrate-proposals] storage for ${dataset.namespace}/${type} not prepared: ${err instanceof Error ? err.message : String(err)}\n`);
|
|
680
|
+
}
|
|
681
|
+
})();
|
|
682
|
+
pendingStorage.add(work);
|
|
683
|
+
scope?.pending.add(work);
|
|
684
|
+
work.finally(() => {
|
|
685
|
+
pendingStorage.delete(work);
|
|
686
|
+
scope?.pending.delete(work);
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* Starts preparing `type`'s storage on the namespace's base `dataset` once the
|
|
691
|
+
* caller's transaction has committed (at once when there is none), never
|
|
692
|
+
* inside it: index builds run CONCURRENTLY and would wait forever on it. It
|
|
693
|
+
* runs in the background, because the lock waits and index builds have no
|
|
694
|
+
* bound and must not hold the caller's response; rows written meanwhile land
|
|
695
|
+
* in the DEFAULT partition and are moved. A failure is logged and never
|
|
696
|
+
* undoes the committed proposal; the next boot or proposal retries.
|
|
697
|
+
* A field added to a type that already has a table is recorded `pending`, and
|
|
698
|
+
* one `field_pending` `_system/AuditEvent` per such field is written and
|
|
699
|
+
* announced through `handle` (F11, F12). {@link storageSettled} awaits it;
|
|
700
|
+
* `scope`'s signal aborts it.
|
|
701
|
+
*/
|
|
702
|
+
function ensureStorageAfterCommit(afterTx, dataset, type, handle, scope) {
|
|
703
|
+
if (afterTx === void 0) {
|
|
704
|
+
prepareStorage(dataset, type, handle, scope);
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
afterTx(async ({ committed }) => {
|
|
708
|
+
if (committed) prepareStorage(dataset, type, handle, scope);
|
|
709
|
+
});
|
|
710
|
+
}
|
|
514
711
|
|
|
515
712
|
//#endregion
|
|
516
713
|
//#region ../../plugins/substrate-proposals/src/proposal-lock.ts
|
|
@@ -637,9 +834,7 @@ async function proposeNamespaceLocked(deps, ctx, args, input) {
|
|
|
637
834
|
let proposalId;
|
|
638
835
|
if (prior !== void 0) proposalId = String(prior.id);
|
|
639
836
|
else {
|
|
640
|
-
|
|
641
|
-
await writeInTx(controlBinding.registry, ctx, args, "createNamespaceProposal", {
|
|
642
|
-
id: proposalId,
|
|
837
|
+
const created = await writeInTx(controlBinding.registry, ctx, args, "createNamespaceProposal", {
|
|
643
838
|
ns,
|
|
644
839
|
proposedBy,
|
|
645
840
|
proposedAt: /* @__PURE__ */ new Date(),
|
|
@@ -649,6 +844,7 @@ async function proposeNamespaceLocked(deps, ctx, args, input) {
|
|
|
649
844
|
supersededByCode: false,
|
|
650
845
|
archived: false
|
|
651
846
|
});
|
|
847
|
+
proposalId = String(created.id);
|
|
652
848
|
}
|
|
653
849
|
try {
|
|
654
850
|
installHotAddedNamespace({
|
|
@@ -658,6 +854,7 @@ async function proposeNamespaceLocked(deps, ctx, args, input) {
|
|
|
658
854
|
engine: controlEngine,
|
|
659
855
|
edges: deps.edges,
|
|
660
856
|
...deps.proposeCapability !== void 0 && { proposeCapability: deps.proposeCapability },
|
|
857
|
+
...deps.indexCapability !== void 0 && { indexCapability: deps.indexCapability },
|
|
661
858
|
...deps.registerProposalMutations !== void 0 && { registerProposalMutations: deps.registerProposalMutations }
|
|
662
859
|
});
|
|
663
860
|
} finally {
|
|
@@ -715,12 +912,32 @@ function claimForRollback(deps, ns, engine, args) {
|
|
|
715
912
|
|
|
716
913
|
//#endregion
|
|
717
914
|
//#region ../../plugins/substrate-proposals/src/multi-instance.ts
|
|
915
|
+
/** Why a stored field proposal is skipped when replay skipped its type. */
|
|
916
|
+
function typeSkipped(row) {
|
|
917
|
+
const type = String(row.targetType);
|
|
918
|
+
const field = String(row.fieldName);
|
|
919
|
+
return new ProposalError("unknown_type", `type '${type}' was skipped at replay, so its field '${field}' is not added`, {
|
|
920
|
+
typeName: type,
|
|
921
|
+
fieldName: field,
|
|
922
|
+
typeSkipped: true
|
|
923
|
+
});
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* The warning line for a stored proposal skipped because it names a field reserved for
|
|
927
|
+
* server metadata, or because its type was skipped; undefined for any other reason.
|
|
928
|
+
*/
|
|
929
|
+
function reservedSkipLine(namespace, row, err) {
|
|
930
|
+
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`;
|
|
931
|
+
if (!(err instanceof ProposalError) || !err.message.includes("reserved for server metadata")) return;
|
|
932
|
+
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"}`;
|
|
933
|
+
}
|
|
718
934
|
async function setupNamespaceListener(args) {
|
|
719
935
|
await args.pubsub.listen(args.channel, async (payload) => {
|
|
720
936
|
await applyProposalFromNotification({
|
|
721
937
|
namespace: args.namespace,
|
|
722
938
|
proposalId: payload,
|
|
723
|
-
registryStore: args.registryStore
|
|
939
|
+
registryStore: args.registryStore,
|
|
940
|
+
...args.onSkip !== void 0 ? { onSkip: args.onSkip } : {}
|
|
724
941
|
}).catch(() => {});
|
|
725
942
|
});
|
|
726
943
|
}
|
|
@@ -729,23 +946,33 @@ async function applyProposalFromNotification(args) {
|
|
|
729
946
|
const typeHit = await binding.dataset.read("TypeProposal", { id: args.proposalId });
|
|
730
947
|
if (typeHit.rows.length > 0) {
|
|
731
948
|
const row = typeHit.rows[0];
|
|
732
|
-
if (row.applied
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
fields
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
949
|
+
if (row.applied !== true) return;
|
|
950
|
+
try {
|
|
951
|
+
const typeName = row.typeName;
|
|
952
|
+
let changed = false;
|
|
953
|
+
if (!binding.registry.get(typeName)) {
|
|
954
|
+
const fields = JSON.parse(row.fieldsJson);
|
|
955
|
+
applyTypeProposal({
|
|
956
|
+
namespace: args.namespace,
|
|
957
|
+
typeName,
|
|
958
|
+
fields,
|
|
959
|
+
registryStore: args.registryStore,
|
|
960
|
+
dataset: binding.dataset,
|
|
961
|
+
edges: binding.edges
|
|
962
|
+
});
|
|
963
|
+
changed = true;
|
|
964
|
+
}
|
|
965
|
+
changed = replayTypeIndexOptions(args.registryStore, args.namespace, row) || changed;
|
|
966
|
+
if (changed) ensureStorageAfterCommit(void 0, binding.dataset, typeName, binding.engine, args.registryStore.storage);
|
|
967
|
+
} catch (err) {
|
|
968
|
+
args.onSkip?.(row, err);
|
|
742
969
|
}
|
|
743
970
|
return;
|
|
744
971
|
}
|
|
745
972
|
const fieldHit = await binding.dataset.read("FieldProposal", { id: args.proposalId });
|
|
746
973
|
if (fieldHit.rows.length > 0) {
|
|
747
974
|
const row = fieldHit.rows[0];
|
|
748
|
-
if (row.applied === true) {
|
|
975
|
+
if (row.applied === true) try {
|
|
749
976
|
const entry = binding.registry.get(row.targetType);
|
|
750
977
|
if (entry && fieldNeedsReplay(entry, row)) {
|
|
751
978
|
const fieldSpec = JSON.parse(row.fieldSpecJson);
|
|
@@ -758,11 +985,35 @@ async function applyProposalFromNotification(args) {
|
|
|
758
985
|
dataset: binding.dataset,
|
|
759
986
|
edges: binding.edges
|
|
760
987
|
});
|
|
988
|
+
ensureStorageAfterCommit(void 0, binding.dataset, row.targetType, binding.engine, args.registryStore.storage);
|
|
761
989
|
}
|
|
990
|
+
} catch (err) {
|
|
991
|
+
args.onSkip?.(row, err);
|
|
762
992
|
}
|
|
763
993
|
}
|
|
764
994
|
}
|
|
765
995
|
/**
|
|
996
|
+
* Bring a registered proposed type's indexes and natural order in line with
|
|
997
|
+
* its row. False when they already match (or the type is not registered).
|
|
998
|
+
*
|
|
999
|
+
* @throws when the row's options are malformed or invalid for the type
|
|
1000
|
+
*/
|
|
1001
|
+
function replayTypeIndexOptions(registryStore, namespace, row, onSkip) {
|
|
1002
|
+
const binding = registryStore.getBinding(namespace);
|
|
1003
|
+
const entry = binding.registry.get(row.typeName);
|
|
1004
|
+
const opts = typeIndexOptionsOf(row, (err) => onSkip?.(row, err));
|
|
1005
|
+
if (entry === void 0 || hasTypeIndexOptions(entry, opts)) return false;
|
|
1006
|
+
applyTypeIndexOptions({
|
|
1007
|
+
namespace,
|
|
1008
|
+
typeName: row.typeName,
|
|
1009
|
+
...opts,
|
|
1010
|
+
registryStore,
|
|
1011
|
+
dataset: binding.dataset,
|
|
1012
|
+
edges: binding.edges
|
|
1013
|
+
});
|
|
1014
|
+
return true;
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
766
1017
|
* A field proposal needs replaying when its field is absent, or when it
|
|
767
1018
|
* narrowed an enum: the type was registered with the wider spec, and the
|
|
768
1019
|
* narrowing exists only in the proposal row.
|
|
@@ -780,6 +1031,8 @@ async function reconcileNamespace(args) {
|
|
|
780
1031
|
] },
|
|
781
1032
|
limit: 1e4
|
|
782
1033
|
});
|
|
1034
|
+
const prepare = /* @__PURE__ */ new Set();
|
|
1035
|
+
const skippedTypes = /* @__PURE__ */ new Set();
|
|
783
1036
|
for (const row of typeRows.rows) {
|
|
784
1037
|
if (binding.registry.get(row.typeName)) continue;
|
|
785
1038
|
try {
|
|
@@ -794,7 +1047,10 @@ async function reconcileNamespace(args) {
|
|
|
794
1047
|
});
|
|
795
1048
|
} catch (err) {
|
|
796
1049
|
args.onSkip?.(row, err);
|
|
1050
|
+
skippedTypes.add(row.typeName);
|
|
1051
|
+
continue;
|
|
797
1052
|
}
|
|
1053
|
+
prepare.add(row.typeName);
|
|
798
1054
|
}
|
|
799
1055
|
const fieldRows = await binding.dataset.read("FieldProposal", {
|
|
800
1056
|
where: { AND: [
|
|
@@ -807,6 +1063,10 @@ async function reconcileNamespace(args) {
|
|
|
807
1063
|
});
|
|
808
1064
|
for (const row of fieldRows.rows) {
|
|
809
1065
|
const entry = binding.registry.get(row.targetType);
|
|
1066
|
+
if (!entry && skippedTypes.has(row.targetType)) {
|
|
1067
|
+
args.onSkip?.(row, typeSkipped(row));
|
|
1068
|
+
continue;
|
|
1069
|
+
}
|
|
810
1070
|
if (!entry || !fieldNeedsReplay(entry, row)) continue;
|
|
811
1071
|
try {
|
|
812
1072
|
const fieldSpec = JSON.parse(row.fieldSpecJson);
|
|
@@ -821,8 +1081,16 @@ async function reconcileNamespace(args) {
|
|
|
821
1081
|
});
|
|
822
1082
|
} catch (err) {
|
|
823
1083
|
args.onSkip?.(row, err);
|
|
1084
|
+
continue;
|
|
824
1085
|
}
|
|
1086
|
+
prepare.add(row.targetType);
|
|
825
1087
|
}
|
|
1088
|
+
for (const row of typeRows.rows) try {
|
|
1089
|
+
if (replayTypeIndexOptions(args.registryStore, args.namespace, row, args.onSkip)) prepare.add(row.typeName);
|
|
1090
|
+
} catch (err) {
|
|
1091
|
+
args.onSkip?.(row, err);
|
|
1092
|
+
}
|
|
1093
|
+
for (const type of prepare) ensureStorageAfterCommit(void 0, binding.dataset, type, binding.engine, args.registryStore.storage);
|
|
826
1094
|
}
|
|
827
1095
|
/**
|
|
828
1096
|
* Shared install path for a hot-added namespace. Three call sites use this:
|
|
@@ -859,7 +1127,7 @@ function installHotAddedNamespace(args) {
|
|
|
859
1127
|
args.registryStore.set(args.ns, binding);
|
|
860
1128
|
}
|
|
861
1129
|
const registry = skipRegistered(binding.registry);
|
|
862
|
-
defineProposalTypes(args.ns, args.proposeCapability).install({
|
|
1130
|
+
defineProposalTypes(args.ns, args.proposeCapability, args.indexCapability).install({
|
|
863
1131
|
registry,
|
|
864
1132
|
dataset: binding.dataset,
|
|
865
1133
|
edges: binding.edges
|
|
@@ -927,6 +1195,7 @@ async function setupClusterNamespaceListener(args) {
|
|
|
927
1195
|
engine: args.engine,
|
|
928
1196
|
edges: args.edges,
|
|
929
1197
|
...args.proposeCapability !== void 0 && { proposeCapability: args.proposeCapability },
|
|
1198
|
+
...args.indexCapability !== void 0 && { indexCapability: args.indexCapability },
|
|
930
1199
|
...args.registerProposalMutations !== void 0 && { registerProposalMutations: args.registerProposalMutations }
|
|
931
1200
|
});
|
|
932
1201
|
} catch (err) {
|
|
@@ -973,6 +1242,7 @@ async function reconcileClusterNamespaces(args) {
|
|
|
973
1242
|
engine: args.engine,
|
|
974
1243
|
edges: args.edges,
|
|
975
1244
|
...args.proposeCapability !== void 0 && { proposeCapability: args.proposeCapability },
|
|
1245
|
+
...args.indexCapability !== void 0 && { indexCapability: args.indexCapability },
|
|
976
1246
|
...args.registerProposalMutations !== void 0 && { registerProposalMutations: args.registerProposalMutations }
|
|
977
1247
|
});
|
|
978
1248
|
} catch (err) {
|
|
@@ -1107,7 +1377,10 @@ function buildOnboardBody(ownedNamespaces) {
|
|
|
1107
1377
|
params: {
|
|
1108
1378
|
typeName: "string — PascalCase, unique within the namespace",
|
|
1109
1379
|
fields: "object — FieldSpecJson map keyed by camelCase field name (see fieldSpecJson below)",
|
|
1110
|
-
proposedReason: "string — short human-readable rationale"
|
|
1380
|
+
proposedReason: "string — short human-readable rationale",
|
|
1381
|
+
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.",
|
|
1382
|
+
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.",
|
|
1383
|
+
expectedSize: "optional \"small\"|\"large\" — \"large\" gets index advice sooner"
|
|
1111
1384
|
},
|
|
1112
1385
|
returns: {
|
|
1113
1386
|
id: "uuid — the TypeProposal record id",
|
|
@@ -1146,8 +1419,8 @@ function buildOnboardBody(ownedNamespaces) {
|
|
|
1146
1419
|
description: "Narrow wire format mirroring the autoCRUD-expressible subset of FieldDef. Only these four shapes are accepted. Anything more expressive needs code + PR.",
|
|
1147
1420
|
variants: {
|
|
1148
1421
|
scalar: {
|
|
1149
|
-
shape: "{ kind: \"scalar\", type: \"string\"|\"number\"|\"boolean\"|\"date\"|\"json\", indexed?: bool, nullable?: bool }",
|
|
1150
|
-
notes: "indexed defaults to true
|
|
1422
|
+
shape: "{ kind: \"scalar\", type: \"string\"|\"number\"|\"int16\"|\"int32\"|\"int64\"|\"boolean\"|\"date\"|\"json\", indexed?: bool, nullable?: bool }",
|
|
1423
|
+
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
1424
|
},
|
|
1152
1425
|
crossRef: {
|
|
1153
1426
|
shape: "{ kind: \"crossRef\", ns: string, type: string, onDelete?: \"set_null\"|\"cascade\"|\"restrict\"|\"orphan\", indexed?: bool, nullable?: bool }",
|
|
@@ -1166,7 +1439,7 @@ function buildOnboardBody(ownedNamespaces) {
|
|
|
1166
1439
|
constraints: [
|
|
1167
1440
|
"AutoCRUD-only: every proposed type gets the standard create/get/update/delete/list surface. No custom resolvers — those require code.",
|
|
1168
1441
|
"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
|
|
1442
|
+
"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
1443
|
"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
1444
|
`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
1445
|
"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."
|
|
@@ -1356,9 +1629,7 @@ async function proposeFieldLocked(deps, ctx, args, input) {
|
|
|
1356
1629
|
}, suppliedMap, { oldVocabulary: oldEnum });
|
|
1357
1630
|
}
|
|
1358
1631
|
const proposedAt = /* @__PURE__ */ new Date();
|
|
1359
|
-
const newId = randomUUID();
|
|
1360
1632
|
const initialParams = {
|
|
1361
|
-
id: newId,
|
|
1362
1633
|
targetType,
|
|
1363
1634
|
fieldName,
|
|
1364
1635
|
fieldSpecJson: JSON.stringify(fieldSpec),
|
|
@@ -1373,8 +1644,8 @@ async function proposeFieldLocked(deps, ctx, args, input) {
|
|
|
1373
1644
|
archived: false
|
|
1374
1645
|
};
|
|
1375
1646
|
const engine = binding.engine;
|
|
1376
|
-
|
|
1377
|
-
|
|
1647
|
+
const newId = engine !== void 0 ? (await writeInTx(binding.registry, ctx, args, "createFieldProposal", initialParams)).id : (await args.dataset.write("FieldProposal", [initialParams])).ids[0];
|
|
1648
|
+
if (newId === void 0) throw new Error("dataset.write did not return an id for FieldProposal");
|
|
1378
1649
|
try {
|
|
1379
1650
|
applyUntilRollback(args, binding.registry, () => applyFieldProposal({
|
|
1380
1651
|
namespace: deps.namespace,
|
|
@@ -1410,6 +1681,7 @@ async function proposeFieldLocked(deps, ctx, args, input) {
|
|
|
1410
1681
|
await args.dataset.write("FieldProposal", [appliedRow]);
|
|
1411
1682
|
}
|
|
1412
1683
|
await notifyAfterCommit(args, () => deps.pubsub.notify(deps.channelFor(deps.namespace), String(newId)));
|
|
1684
|
+
ensureStorageAfterCommit(args.afterTx, deps.dataset, targetType, engine, deps.registryStore.storage);
|
|
1413
1685
|
if (migration === null) return appliedRow;
|
|
1414
1686
|
const { ids: _written, ...summary } = migration;
|
|
1415
1687
|
return {
|
|
@@ -1418,6 +1690,338 @@ async function proposeFieldLocked(deps, ctx, args, input) {
|
|
|
1418
1690
|
};
|
|
1419
1691
|
}
|
|
1420
1692
|
|
|
1693
|
+
//#endregion
|
|
1694
|
+
//#region ../../plugins/substrate-proposals/src/propose-index-resolver.ts
|
|
1695
|
+
/**
|
|
1696
|
+
* Factory for the `propose_index` mutation resolver: the signed approval of an
|
|
1697
|
+
* index recommendation, or a direct index change, under the index capability.
|
|
1698
|
+
*
|
|
1699
|
+
* - `add` appends a declared index to a proposed type's TypeProposal row and
|
|
1700
|
+
* reinstalls the type; storage builds it in the background after commit.
|
|
1701
|
+
* - `orderedBy` sets a proposed type's natural order the same way, and storage
|
|
1702
|
+
* swaps its derived indexes.
|
|
1703
|
+
* - `remove` drops an engine-owned, non-unique index the engine no longer
|
|
1704
|
+
* wants, on any type (`Dataset.dropEngineIndex`, which re-checks it live).
|
|
1705
|
+
* An index wanted only because a proposed type's `indexesJson` declares it
|
|
1706
|
+
* is retracted first: the declaration leaves the row, peers are told, and
|
|
1707
|
+
* the index is dropped.
|
|
1708
|
+
* - `dismiss` closes a recommendation without acting on it.
|
|
1709
|
+
*
|
|
1710
|
+
* A referenced `_system/IndexRecommendation` must be `open`, and moves to
|
|
1711
|
+
* `accepted` or `dismissed` by a conditional update from `open`, claimed
|
|
1712
|
+
* before the audit event and the effect and given back (to `open`) if the
|
|
1713
|
+
* effect fails, so a caller never learns it was closed after its own effect. Its rows are
|
|
1714
|
+
* writable by any index-capability holder, so nothing in one is trusted: an
|
|
1715
|
+
* `add` must still be unserved by the type's index plan (as the advisor's
|
|
1716
|
+
* supersede pass decides) and its declaration validates as a direct one does;
|
|
1717
|
+
* `remove` is checked against the catalog. When that evidence no longer
|
|
1718
|
+
* holds the call is refused `recommendation_stale` and the row superseded.
|
|
1719
|
+
*
|
|
1720
|
+
* Every write here goes through the namespaces' base datasets, outside the
|
|
1721
|
+
* caller's transaction: a `DROP INDEX CONCURRENTLY` cannot run beside it, and
|
|
1722
|
+
* a superseded mark must outlive the refusal that rolls the transaction back.
|
|
1723
|
+
* Each one is stamped with the caller's identity.
|
|
1724
|
+
*
|
|
1725
|
+
* Audit: every applied call writes one `_system/AuditEvent` (`index_proposed`)
|
|
1726
|
+
* once its checks have passed and before its first effect (for a drop, under
|
|
1727
|
+
* the table lock, just before `DROP INDEX`), so no effect lands unaudited. For
|
|
1728
|
+
* a recommendation the event's id derives from the recommendation's, written
|
|
1729
|
+
* insert-only, so a retry after a partial failure converges without a second
|
|
1730
|
+
* event.
|
|
1731
|
+
*/
|
|
1732
|
+
const SYSTEM_NAMESPACE = "_system";
|
|
1733
|
+
const RECOMMENDATION_TYPE = "IndexRecommendation";
|
|
1734
|
+
const ACTIONS = [
|
|
1735
|
+
"add",
|
|
1736
|
+
"remove",
|
|
1737
|
+
"dismiss",
|
|
1738
|
+
"orderedBy"
|
|
1739
|
+
];
|
|
1740
|
+
/** `dropEngineIndex` refusal codes, passed through as the call's own. */
|
|
1741
|
+
const DROP_REFUSALS = /* @__PURE__ */ new Set([
|
|
1742
|
+
"unknown_index",
|
|
1743
|
+
"not_engine_owned",
|
|
1744
|
+
"unique_index",
|
|
1745
|
+
"still_desired",
|
|
1746
|
+
"index_drop_timeout"
|
|
1747
|
+
]);
|
|
1748
|
+
/** A read row without the engine fields reads add (`shortId`), for writing back. */
|
|
1749
|
+
function bodyOf(row) {
|
|
1750
|
+
const body = { ...row };
|
|
1751
|
+
for (const name of RESERVED_AUTOCRUD_FIELD_NAMES) delete body[name];
|
|
1752
|
+
return body;
|
|
1753
|
+
}
|
|
1754
|
+
function invalid(param, message) {
|
|
1755
|
+
return new ProposalError("invalid_params", message, { param });
|
|
1756
|
+
}
|
|
1757
|
+
function closed(id, state) {
|
|
1758
|
+
return new ProposalError("recommendation_closed", `index recommendation '${String(id)}' is no longer open${state === void 0 ? "" : ` (${String(state)})`}`, state === void 0 ? {} : { state });
|
|
1759
|
+
}
|
|
1760
|
+
function parseInput$1(params) {
|
|
1761
|
+
for (const key of [
|
|
1762
|
+
"namespace",
|
|
1763
|
+
"type",
|
|
1764
|
+
"reason"
|
|
1765
|
+
]) if (typeof params[key] !== "string" || key !== "reason" && params[key] === "") throw invalid(key, `'${key}' must be a${key === "reason" ? "" : " non-empty"} string`);
|
|
1766
|
+
if (!ACTIONS.includes(params.action)) throw invalid("action", `'action' must be one of ${ACTIONS.join(", ")}`);
|
|
1767
|
+
for (const key of ["recommendationId", "indexName"]) if (params[key] !== void 0 && typeof params[key] !== "string") throw invalid(key, `'${key}' must be a string`);
|
|
1768
|
+
return params;
|
|
1769
|
+
}
|
|
1770
|
+
function makeProposeIndexResolver(deps) {
|
|
1771
|
+
return async (ctx, args) => {
|
|
1772
|
+
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 });
|
|
1773
|
+
const input = parseInput$1(args.params ?? {});
|
|
1774
|
+
const lockOn = deps.datasetFor(SYSTEM_NAMESPACE) ?? args.dataset;
|
|
1775
|
+
const run = () => proposeIndex(deps, ctx, args, input);
|
|
1776
|
+
if (lockOn === void 0) return run();
|
|
1777
|
+
return withProposalLock(lockOn, proposalLockKey("index", input.namespace, input.type), run);
|
|
1778
|
+
};
|
|
1779
|
+
}
|
|
1780
|
+
async function proposeIndex(deps, ctx, args, input) {
|
|
1781
|
+
const identity = resolveIdentity(ctx);
|
|
1782
|
+
const system = deps.datasetFor(SYSTEM_NAMESPACE);
|
|
1783
|
+
const now = /* @__PURE__ */ new Date();
|
|
1784
|
+
const rec = input.recommendationId === void 0 ? void 0 : await openRecommendation();
|
|
1785
|
+
async function openRecommendation() {
|
|
1786
|
+
const id = input.recommendationId;
|
|
1787
|
+
const row = system === void 0 ? void 0 : (await system.read(RECOMMENDATION_TYPE, {
|
|
1788
|
+
id,
|
|
1789
|
+
limit: 1
|
|
1790
|
+
})).rows[0];
|
|
1791
|
+
if (row === void 0) throw new ProposalError("unknown_recommendation", `no index recommendation '${id}'`);
|
|
1792
|
+
if (row.state !== "open") throw closed(id, row.state);
|
|
1793
|
+
if (row.namespace !== input.namespace || row.type !== input.type) throw invalid("recommendationId", `recommendation '${id}' is for ${row.namespace}/${row.type}`);
|
|
1794
|
+
if (input.action !== "dismiss" && row.action !== input.action) throw invalid("action", `recommendation '${id}' recommends '${String(row.action)}'`);
|
|
1795
|
+
return row;
|
|
1796
|
+
}
|
|
1797
|
+
const auditId = rec === void 0 ? randomUUID() : uuidFromName(`index_proposed:${String(rec.id)}`);
|
|
1798
|
+
let audited;
|
|
1799
|
+
const audit = async (detail) => {
|
|
1800
|
+
if (audited !== void 0) return;
|
|
1801
|
+
audited = detail;
|
|
1802
|
+
await system?.write("AuditEvent", [{
|
|
1803
|
+
id: auditId,
|
|
1804
|
+
ts: now.toISOString(),
|
|
1805
|
+
actorKid: identity.id,
|
|
1806
|
+
kind: "index_proposed",
|
|
1807
|
+
targetKid: `${input.namespace}/${input.type}`,
|
|
1808
|
+
clientIp: null,
|
|
1809
|
+
details: {
|
|
1810
|
+
action: input.action,
|
|
1811
|
+
reason: input.reason,
|
|
1812
|
+
recommendationId: rec?.id ?? null,
|
|
1813
|
+
...detail
|
|
1814
|
+
}
|
|
1815
|
+
}], {
|
|
1816
|
+
identity,
|
|
1817
|
+
now,
|
|
1818
|
+
insertOnly: true
|
|
1819
|
+
});
|
|
1820
|
+
};
|
|
1821
|
+
/** Moves the recommendation (if any) from `open` to `state`, atomically. */
|
|
1822
|
+
const close = async (state) => {
|
|
1823
|
+
if (rec === void 0 || system === void 0) return;
|
|
1824
|
+
const { matched } = await system.conditionalUpdate(RECOMMENDATION_TYPE, rec.id, { state: "open" }, { state }, identity, now);
|
|
1825
|
+
if (!matched) throw closed(rec.id);
|
|
1826
|
+
};
|
|
1827
|
+
let claimed;
|
|
1828
|
+
const claim = async (state) => {
|
|
1829
|
+
await close(state);
|
|
1830
|
+
if (rec !== void 0 && system !== void 0) claimed = state;
|
|
1831
|
+
};
|
|
1832
|
+
/** Gives a claim back (claimed → open) when the work behind it failed. */
|
|
1833
|
+
const release = async () => {
|
|
1834
|
+
const state = claimed;
|
|
1835
|
+
if (state === void 0 || rec === void 0 || system === void 0) return;
|
|
1836
|
+
claimed = void 0;
|
|
1837
|
+
const fail = (why) => {
|
|
1838
|
+
process.stderr.write(`[substrate-proposals] index recommendation ${String(rec.id)} stays ${state} after a failed ${input.action}: ${why}\n`);
|
|
1839
|
+
};
|
|
1840
|
+
try {
|
|
1841
|
+
const { matched } = await system.conditionalUpdate(RECOMMENDATION_TYPE, rec.id, { state }, { state: "open" }, identity, now);
|
|
1842
|
+
if (!matched) fail("it was changed meanwhile");
|
|
1843
|
+
} catch (err) {
|
|
1844
|
+
fail(err instanceof Error ? err.message : String(err));
|
|
1845
|
+
}
|
|
1846
|
+
};
|
|
1847
|
+
const stale = async (reason, code) => {
|
|
1848
|
+
await release();
|
|
1849
|
+
await close("superseded");
|
|
1850
|
+
throw new ProposalError("recommendation_stale", `index recommendation '${String(rec?.id)}' no longer holds: ${reason}`, code !== void 0 ? { reason: code } : {});
|
|
1851
|
+
};
|
|
1852
|
+
const call = {
|
|
1853
|
+
deps,
|
|
1854
|
+
args,
|
|
1855
|
+
input,
|
|
1856
|
+
identity,
|
|
1857
|
+
rec,
|
|
1858
|
+
audit,
|
|
1859
|
+
stale,
|
|
1860
|
+
claim,
|
|
1861
|
+
release
|
|
1862
|
+
};
|
|
1863
|
+
try {
|
|
1864
|
+
switch (input.action) {
|
|
1865
|
+
case "dismiss":
|
|
1866
|
+
if (rec === void 0) throw invalid("recommendationId", "'dismiss' needs a recommendationId");
|
|
1867
|
+
await claim("dismissed");
|
|
1868
|
+
await audit({});
|
|
1869
|
+
break;
|
|
1870
|
+
case "remove":
|
|
1871
|
+
await removeIndex(call, system, auditId);
|
|
1872
|
+
break;
|
|
1873
|
+
case "add":
|
|
1874
|
+
case "orderedBy": await changeProposedType(call);
|
|
1875
|
+
}
|
|
1876
|
+
} catch (err) {
|
|
1877
|
+
await release();
|
|
1878
|
+
throw err;
|
|
1879
|
+
}
|
|
1880
|
+
return {
|
|
1881
|
+
id: auditId,
|
|
1882
|
+
action: input.action,
|
|
1883
|
+
namespace: input.namespace,
|
|
1884
|
+
type: input.type,
|
|
1885
|
+
recommendationId: rec?.id ?? null,
|
|
1886
|
+
indexName: audited?.indexName ?? null
|
|
1887
|
+
};
|
|
1888
|
+
}
|
|
1889
|
+
/** The applied, live TypeProposal row of `type`, or undefined (a code-defined type). */
|
|
1890
|
+
async function proposalOf(binding, type) {
|
|
1891
|
+
return (await binding.dataset.read("TypeProposal", {
|
|
1892
|
+
where: { AND: [
|
|
1893
|
+
{ typeName: { eq: type } },
|
|
1894
|
+
{ applied: { eq: true } },
|
|
1895
|
+
{ supersededByCode: { eq: false } },
|
|
1896
|
+
{ archived: { eq: false } }
|
|
1897
|
+
] },
|
|
1898
|
+
limit: 1
|
|
1899
|
+
})).rows[0];
|
|
1900
|
+
}
|
|
1901
|
+
/**
|
|
1902
|
+
* Stores `next` on the proposal row, reinstalls the type with it, and tells
|
|
1903
|
+
* peers once the caller's transaction commits.
|
|
1904
|
+
*/
|
|
1905
|
+
async function storeTypeIndexOptions(call, binding, proposal, next) {
|
|
1906
|
+
const { deps, args, input, identity } = call;
|
|
1907
|
+
const { indexes: all, ...rest } = next;
|
|
1908
|
+
const opts = {
|
|
1909
|
+
...rest,
|
|
1910
|
+
...all?.length ? { indexes: all } : {}
|
|
1911
|
+
};
|
|
1912
|
+
await binding.dataset.write("TypeProposal", [{
|
|
1913
|
+
...bodyOf(proposal),
|
|
1914
|
+
indexesJson: opts.indexes !== void 0 ? JSON.stringify(opts.indexes) : null,
|
|
1915
|
+
orderedByJson: opts.orderedBy !== void 0 ? JSON.stringify(opts.orderedBy) : null
|
|
1916
|
+
}], { identity });
|
|
1917
|
+
applyTypeIndexOptions({
|
|
1918
|
+
namespace: input.namespace,
|
|
1919
|
+
typeName: input.type,
|
|
1920
|
+
...opts,
|
|
1921
|
+
registryStore: deps.registryStore,
|
|
1922
|
+
dataset: binding.dataset,
|
|
1923
|
+
edges: binding.edges
|
|
1924
|
+
});
|
|
1925
|
+
await notifyAfterCommit(args, () => deps.pubsub.notify(deps.channelFor(input.namespace), String(proposal.id)));
|
|
1926
|
+
}
|
|
1927
|
+
/** `remove`: retract a proposed declaration if that is all that wants it, then drop. */
|
|
1928
|
+
async function removeIndex(call, system, auditId) {
|
|
1929
|
+
const { deps, input, rec } = call;
|
|
1930
|
+
const name = input.indexName ?? rec?.indexName ?? void 0;
|
|
1931
|
+
if (name === void 0) throw invalid("indexName", "'remove' needs an indexName");
|
|
1932
|
+
const dataset = deps.datasetFor(input.namespace) ?? system?.forNamespace(input.namespace);
|
|
1933
|
+
await call.claim("accepted");
|
|
1934
|
+
if (rec === void 0 && dataset?.indexDeclarationOf !== void 0) {
|
|
1935
|
+
const binding = deps.registryStore.ownsNamespace(input.namespace) ? deps.registryStore.getBinding(input.namespace) : void 0;
|
|
1936
|
+
const proposal = binding === void 0 ? void 0 : await proposalOf(binding, input.type);
|
|
1937
|
+
const decl = proposal === void 0 ? void 0 : await dataset.indexDeclarationOf(input.type, name);
|
|
1938
|
+
const current = proposal === void 0 ? {} : typeIndexOptionsOf(proposal);
|
|
1939
|
+
const kept = (current.indexes ?? []).filter((d) => !sameJson(d, decl));
|
|
1940
|
+
if (binding !== void 0 && proposal !== void 0 && decl !== void 0 && kept.length < (current.indexes ?? []).length) {
|
|
1941
|
+
await call.audit({
|
|
1942
|
+
indexName: name,
|
|
1943
|
+
retracted: decl
|
|
1944
|
+
});
|
|
1945
|
+
await storeTypeIndexOptions(call, binding, proposal, {
|
|
1946
|
+
...current,
|
|
1947
|
+
indexes: kept
|
|
1948
|
+
});
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
try {
|
|
1952
|
+
if (dataset?.dropEngineIndex === void 0) throw Object.assign(/* @__PURE__ */ new Error(`no index ${name} on ${input.namespace}/${input.type}`), { code: "unknown_index" });
|
|
1953
|
+
await dataset.dropEngineIndex(input.type, name, { beforeDrop: () => call.audit({ indexName: name }) });
|
|
1954
|
+
} catch (err) {
|
|
1955
|
+
const code = err.code;
|
|
1956
|
+
if (typeof code !== "string" || !DROP_REFUSALS.has(code)) throw err;
|
|
1957
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
1958
|
+
if (rec !== void 0 && code !== "index_drop_timeout") {
|
|
1959
|
+
if (code === "unknown_index" && (await system?.read("AuditEvent", {
|
|
1960
|
+
id: auditId,
|
|
1961
|
+
limit: 1
|
|
1962
|
+
}))?.rows[0] !== void 0) return;
|
|
1963
|
+
await call.stale(message, code);
|
|
1964
|
+
}
|
|
1965
|
+
await call.release();
|
|
1966
|
+
throw new ProposalError(code, message, { indexName: name });
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
/** `add` or `orderedBy` on a proposed type: update its row, then its registry entry. */
|
|
1970
|
+
async function changeProposedType(call) {
|
|
1971
|
+
const { deps, args, input, rec, stale } = call;
|
|
1972
|
+
const { namespace: ns, type } = input;
|
|
1973
|
+
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 });
|
|
1974
|
+
if (!deps.registryStore.ownsNamespace(ns)) throw codeDefined();
|
|
1975
|
+
const binding = deps.registryStore.getBinding(ns);
|
|
1976
|
+
const entry = binding.registry.get(type);
|
|
1977
|
+
if (entry?.kind !== "type") throw new ProposalError("unknown_type", `no type '${type}' in namespace '${ns}'`);
|
|
1978
|
+
const proposal = await proposalOf(binding, type);
|
|
1979
|
+
if (proposal === void 0) throw codeDefined();
|
|
1980
|
+
const current = typeIndexOptionsOf(proposal);
|
|
1981
|
+
let next;
|
|
1982
|
+
let detail;
|
|
1983
|
+
if (input.action === "add") {
|
|
1984
|
+
let decl = input.declaration;
|
|
1985
|
+
if (decl === void 0 && rec !== void 0) try {
|
|
1986
|
+
decl = JSON.parse(rec.declarationJson);
|
|
1987
|
+
} catch {
|
|
1988
|
+
return stale("its declaration is not JSON");
|
|
1989
|
+
}
|
|
1990
|
+
if (decl === void 0) throw invalid("declaration", "'add' needs a declaration");
|
|
1991
|
+
const indexes = current.indexes ?? [];
|
|
1992
|
+
const known = indexes.some((d) => sameJson(d, decl));
|
|
1993
|
+
if (rec !== void 0 && !known) {
|
|
1994
|
+
const plan = await binding.dataset.indexPlan?.(type, ns) ?? { indexes: [] };
|
|
1995
|
+
const served = planServesShape(plan, String(rec.shape));
|
|
1996
|
+
if (served === void 0) return stale("its read shape does not parse");
|
|
1997
|
+
if (served) return stale("an index the type already has serves its read shape");
|
|
1998
|
+
}
|
|
1999
|
+
next = {
|
|
2000
|
+
...current,
|
|
2001
|
+
indexes: known ? indexes : [...indexes, decl]
|
|
2002
|
+
};
|
|
2003
|
+
detail = { declaration: decl };
|
|
2004
|
+
} else {
|
|
2005
|
+
if (input.orderedBy === void 0) throw invalid("orderedBy", "'orderedBy' needs an orderedBy");
|
|
2006
|
+
next = {
|
|
2007
|
+
...current,
|
|
2008
|
+
orderedBy: input.orderedBy
|
|
2009
|
+
};
|
|
2010
|
+
detail = { orderedBy: input.orderedBy };
|
|
2011
|
+
}
|
|
2012
|
+
try {
|
|
2013
|
+
assertValidTypeIndexOptions(type, entry.fields, next);
|
|
2014
|
+
} catch (err) {
|
|
2015
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
2016
|
+
if (rec !== void 0) return stale(message);
|
|
2017
|
+
throw invalid(input.action === "add" ? "declaration" : "orderedBy", message);
|
|
2018
|
+
}
|
|
2019
|
+
await call.claim("accepted");
|
|
2020
|
+
await call.audit(detail);
|
|
2021
|
+
await storeTypeIndexOptions(call, binding, proposal, next);
|
|
2022
|
+
ensureStorageAfterCommit(args.afterTx, binding.dataset, type, binding.engine, deps.registryStore.storage);
|
|
2023
|
+
}
|
|
2024
|
+
|
|
1421
2025
|
//#endregion
|
|
1422
2026
|
//#region ../../plugins/substrate-proposals/src/propose-type-resolver.ts
|
|
1423
2027
|
/**
|
|
@@ -1441,6 +2045,8 @@ async function proposeFieldLocked(deps, ctx, args, input) {
|
|
|
1441
2045
|
function parseInput(params) {
|
|
1442
2046
|
const { proposedReason } = params;
|
|
1443
2047
|
if (proposedReason !== void 0 && typeof proposedReason !== "string") throw new ProposalError("invalid_params", "'proposedReason' must be a string", { param: "proposedReason" });
|
|
2048
|
+
const { expectedSize } = params;
|
|
2049
|
+
if (expectedSize !== void 0 && expectedSize !== "small" && expectedSize !== "large") throw new ProposalError("invalid_params", "'expectedSize' must be 'small' or 'large'", { param: "expectedSize" });
|
|
1444
2050
|
return {
|
|
1445
2051
|
...params,
|
|
1446
2052
|
proposedReason: proposedReason ?? ""
|
|
@@ -1451,6 +2057,11 @@ function makeProposeTypeResolver(deps) {
|
|
|
1451
2057
|
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
2058
|
const input = parseInput(args.params ?? {});
|
|
1453
2059
|
assertValidTypeProposal(input.typeName, input.fields, (ns, type) => ns === deps.namespace && type === input.typeName || deps.registryStore.ownsType(ns, type));
|
|
2060
|
+
try {
|
|
2061
|
+
assertValidTypeIndexOptions(input.typeName, proposedFieldDefs(input.fields), indexOptionsOf(input));
|
|
2062
|
+
} catch (cause) {
|
|
2063
|
+
throw new ProposalError("invalid_params", cause instanceof Error ? cause.message : String(cause), {}, { cause });
|
|
2064
|
+
}
|
|
1454
2065
|
const dataset = args.dataset ?? deps.dataset;
|
|
1455
2066
|
return withProposalLock(dataset, proposalLockKey("type", deps.namespace, input.typeName), () => proposeTypeLocked(deps, ctx, {
|
|
1456
2067
|
...args,
|
|
@@ -1458,6 +2069,30 @@ function makeProposeTypeResolver(deps) {
|
|
|
1458
2069
|
}, input));
|
|
1459
2070
|
};
|
|
1460
2071
|
}
|
|
2072
|
+
function indexOptionsOf(input) {
|
|
2073
|
+
const { indexes, orderedBy, expectedSize } = input;
|
|
2074
|
+
return {
|
|
2075
|
+
...indexes?.length ? { indexes } : {},
|
|
2076
|
+
...orderedBy !== void 0 ? { orderedBy } : {},
|
|
2077
|
+
...expectedSize !== void 0 ? { expectedSize } : {}
|
|
2078
|
+
};
|
|
2079
|
+
}
|
|
2080
|
+
/**
|
|
2081
|
+
* Whether `typeName` has storage already: any TypeProposal row by that name,
|
|
2082
|
+
* or any row of the type, archived and tombstoned ones included, so a
|
|
2083
|
+
* propose-only caller cannot clear the signal by deleting them.
|
|
2084
|
+
*/
|
|
2085
|
+
async function hasStorage(dataset, typeName) {
|
|
2086
|
+
if ((await dataset.read("TypeProposal", {
|
|
2087
|
+
where: { typeName: { eq: typeName } },
|
|
2088
|
+
includeDeleted: true,
|
|
2089
|
+
limit: 1
|
|
2090
|
+
})).rows.length > 0) return true;
|
|
2091
|
+
return (await dataset.read(typeName, {
|
|
2092
|
+
includeDeleted: true,
|
|
2093
|
+
limit: 1
|
|
2094
|
+
})).rows.length > 0;
|
|
2095
|
+
}
|
|
1461
2096
|
/** Steps 2-7, run while holding the proposal lock for this type name. */
|
|
1462
2097
|
async function proposeTypeLocked(deps, ctx, args, input) {
|
|
1463
2098
|
const { typeName, fields, proposedReason } = input;
|
|
@@ -1483,12 +2118,9 @@ async function proposeTypeLocked(deps, ctx, args, input) {
|
|
|
1483
2118
|
}).find((key) => liveRegistry.has(key));
|
|
1484
2119
|
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
2120
|
const proposedAt = /* @__PURE__ */ new Date();
|
|
1486
|
-
const fieldsJson = JSON.stringify(fields);
|
|
1487
|
-
const newId = randomUUID();
|
|
1488
2121
|
const initialParams = {
|
|
1489
|
-
id: newId,
|
|
1490
2122
|
typeName,
|
|
1491
|
-
fieldsJson,
|
|
2123
|
+
fieldsJson: JSON.stringify(fields),
|
|
1492
2124
|
proposedBy,
|
|
1493
2125
|
proposedAt,
|
|
1494
2126
|
proposedReason,
|
|
@@ -1496,22 +2128,56 @@ async function proposeTypeLocked(deps, ctx, args, input) {
|
|
|
1496
2128
|
appliedAt: null,
|
|
1497
2129
|
resultingSchemaVersion: null,
|
|
1498
2130
|
supersededByCode: false,
|
|
1499
|
-
archived: false
|
|
2131
|
+
archived: false,
|
|
2132
|
+
expectedSize: input.expectedSize ?? null
|
|
2133
|
+
};
|
|
2134
|
+
const indexOpts = indexOptionsOf(input);
|
|
2135
|
+
const indexFields = {
|
|
2136
|
+
indexesJson: indexOpts.indexes ? JSON.stringify(indexOpts.indexes) : null,
|
|
2137
|
+
orderedByJson: indexOpts.orderedBy ? JSON.stringify(indexOpts.orderedBy) : null
|
|
1500
2138
|
};
|
|
1501
2139
|
const binding = deps.registryStore.getBinding(deps.namespace);
|
|
1502
2140
|
const engine = binding.engine;
|
|
1503
|
-
|
|
1504
|
-
|
|
2141
|
+
let newId;
|
|
2142
|
+
if (engine !== void 0) {
|
|
2143
|
+
const declaresIndexes = indexOpts.indexes !== void 0 || indexOpts.orderedBy !== void 0;
|
|
2144
|
+
newId = (await writeInTx(binding.registry, ctx, args, "createTypeProposal", {
|
|
2145
|
+
...initialParams,
|
|
2146
|
+
...indexFields
|
|
2147
|
+
}, !(declaresIndexes && await hasStorage(args.dataset, typeName)))).id;
|
|
2148
|
+
} else {
|
|
2149
|
+
const id = (await args.dataset.write("TypeProposal", [{
|
|
2150
|
+
...initialParams,
|
|
2151
|
+
...indexFields
|
|
2152
|
+
}])).ids[0];
|
|
2153
|
+
if (id === void 0) throw new Error("dataset.write did not return an id for TypeProposal");
|
|
2154
|
+
newId = id;
|
|
2155
|
+
}
|
|
1505
2156
|
try {
|
|
1506
|
-
applyUntilRollback(args, binding.registry, () =>
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
2157
|
+
applyUntilRollback(args, binding.registry, () => {
|
|
2158
|
+
applyTypeProposal({
|
|
2159
|
+
namespace: deps.namespace,
|
|
2160
|
+
typeName,
|
|
2161
|
+
fields,
|
|
2162
|
+
registryStore: deps.registryStore,
|
|
2163
|
+
dataset: deps.dataset,
|
|
2164
|
+
edges: deps.edges
|
|
2165
|
+
});
|
|
2166
|
+
if (Object.keys(indexOpts).length > 0) try {
|
|
2167
|
+
applyTypeIndexOptions({
|
|
2168
|
+
namespace: deps.namespace,
|
|
2169
|
+
typeName,
|
|
2170
|
+
...indexOpts,
|
|
2171
|
+
registryStore: deps.registryStore,
|
|
2172
|
+
dataset: deps.dataset,
|
|
2173
|
+
edges: deps.edges
|
|
2174
|
+
});
|
|
2175
|
+
} catch (cause) {
|
|
2176
|
+
throw new ProposalError("invalid_params", cause instanceof Error ? cause.message : String(cause), {}, { cause });
|
|
2177
|
+
}
|
|
2178
|
+
}, () => binding.invalidateSchemaCaches?.());
|
|
1514
2179
|
} catch (cause) {
|
|
2180
|
+
if (cause instanceof ProposalError) throw cause;
|
|
1515
2181
|
throw new ProposalError("type_already_exists", `Could not apply type '${typeName}': ${cause instanceof Error ? cause.message : String(cause)}`, {}, { cause });
|
|
1516
2182
|
}
|
|
1517
2183
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -1539,6 +2205,7 @@ async function proposeTypeLocked(deps, ctx, args, input) {
|
|
|
1539
2205
|
await args.dataset.write("TypeProposal", [appliedRow]);
|
|
1540
2206
|
}
|
|
1541
2207
|
await notifyAfterCommit(args, () => deps.pubsub.notify(deps.channelFor(deps.namespace), String(newId)));
|
|
2208
|
+
ensureStorageAfterCommit(args.afterTx, deps.dataset, typeName, engine, deps.registryStore.storage);
|
|
1542
2209
|
return appliedRow;
|
|
1543
2210
|
}
|
|
1544
2211
|
|
|
@@ -1554,6 +2221,17 @@ async function proposeTypeLocked(deps, ctx, args, input) {
|
|
|
1554
2221
|
*/
|
|
1555
2222
|
var RegistryStore = class {
|
|
1556
2223
|
bindings = /* @__PURE__ */ new Map();
|
|
2224
|
+
#storageAbort = new AbortController();
|
|
2225
|
+
/** This plugin instance's background storage work. */
|
|
2226
|
+
storage = {
|
|
2227
|
+
signal: this.#storageAbort.signal,
|
|
2228
|
+
pending: /* @__PURE__ */ new Set()
|
|
2229
|
+
};
|
|
2230
|
+
/** Aborts the background storage work (an interrupted build is left INVALID for the next reconcile) and awaits it. */
|
|
2231
|
+
async stopStorage() {
|
|
2232
|
+
this.#storageAbort.abort();
|
|
2233
|
+
await Promise.all([...this.storage.pending]);
|
|
2234
|
+
}
|
|
1557
2235
|
/**
|
|
1558
2236
|
* Register a full NamespaceBinding for the given namespace.
|
|
1559
2237
|
*
|
|
@@ -1644,6 +2322,7 @@ function createProposalsPlugin(config) {
|
|
|
1644
2322
|
const registryStore = new RegistryStore();
|
|
1645
2323
|
const capability = config.proposeCapability ?? "substrate.propose";
|
|
1646
2324
|
const policy = makeProposePolicy(capability);
|
|
2325
|
+
const indexCapability = config.indexCapability ?? "substrate.index";
|
|
1647
2326
|
let lookupDataset = config.datasetFor ?? (() => void 0);
|
|
1648
2327
|
/**
|
|
1649
2328
|
* Shared resolver-registration helper. Used at static-ns install time AND
|
|
@@ -1694,10 +2373,21 @@ function createProposalsPlugin(config) {
|
|
|
1694
2373
|
edges: ctx.edges,
|
|
1695
2374
|
pubsub: config.pubsub,
|
|
1696
2375
|
proposeCapability: capability,
|
|
2376
|
+
indexCapability,
|
|
1697
2377
|
registerProposalMutations
|
|
1698
2378
|
}),
|
|
1699
2379
|
policy
|
|
1700
2380
|
});
|
|
2381
|
+
ctx.registry.register("propose_index", {
|
|
2382
|
+
kind: "mutation",
|
|
2383
|
+
resolver: makeProposeIndexResolver({
|
|
2384
|
+
registryStore,
|
|
2385
|
+
datasetFor: (ns) => lookupDataset(ns),
|
|
2386
|
+
pubsub: config.pubsub,
|
|
2387
|
+
channelFor: (n) => `_substrate_proposals_${n}`
|
|
2388
|
+
}),
|
|
2389
|
+
policy: makeIndexPolicy(indexCapability)
|
|
2390
|
+
});
|
|
1701
2391
|
}
|
|
1702
2392
|
}, ...config.ownedNamespaces.map((ns) => ({
|
|
1703
2393
|
namespace: ns,
|
|
@@ -1707,7 +2397,7 @@ function createProposalsPlugin(config) {
|
|
|
1707
2397
|
dataset: ctx.dataset,
|
|
1708
2398
|
edges: ctx.edges
|
|
1709
2399
|
});
|
|
1710
|
-
defineProposalTypes(ns, capability).install(ctx);
|
|
2400
|
+
defineProposalTypes(ns, capability, indexCapability).install(ctx);
|
|
1711
2401
|
registerProposalMutations({
|
|
1712
2402
|
namespace: ns,
|
|
1713
2403
|
dataset: ctx.dataset,
|
|
@@ -1724,6 +2414,18 @@ function createProposalsPlugin(config) {
|
|
|
1724
2414
|
registryStore.setInvalidator(CONTROL_NAMESPACE, () => ctx.engine.invalidateSchemaCaches(CONTROL_NAMESPACE));
|
|
1725
2415
|
registryStore.setEngine(CONTROL_NAMESPACE, ctx.engine);
|
|
1726
2416
|
if (config.datasetFor === void 0) lookupDataset = (ns) => ctx.engine.datasetFor(ns);
|
|
2417
|
+
const log = config.log ?? ((line) => console.warn(line));
|
|
2418
|
+
const skipped = (ns, row, err) => {
|
|
2419
|
+
ctx.metric("_system_proposals.replay_skipped", {
|
|
2420
|
+
namespace: ns,
|
|
2421
|
+
id: row.id,
|
|
2422
|
+
type: row.typeName ?? row.targetType,
|
|
2423
|
+
field: row.fieldName ?? null,
|
|
2424
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2425
|
+
});
|
|
2426
|
+
const line = reservedSkipLine(ns, row, err);
|
|
2427
|
+
if (line !== void 0) log(line);
|
|
2428
|
+
};
|
|
1727
2429
|
const controlBinding = registryStore.getBinding(CONTROL_NAMESPACE);
|
|
1728
2430
|
const hotAddedNamespaces = await reconcileClusterNamespaces({
|
|
1729
2431
|
controlNamespace: CONTROL_NAMESPACE,
|
|
@@ -1732,6 +2434,7 @@ function createProposalsPlugin(config) {
|
|
|
1732
2434
|
datasetFor: lookupDataset,
|
|
1733
2435
|
edges: controlBinding.edges,
|
|
1734
2436
|
proposeCapability: capability,
|
|
2437
|
+
indexCapability,
|
|
1735
2438
|
registerProposalMutations,
|
|
1736
2439
|
onSkip: (row, err) => ctx.metric("_system_proposals.replay_skipped", {
|
|
1737
2440
|
namespace: CONTROL_NAMESPACE,
|
|
@@ -1746,17 +2449,14 @@ function createProposalsPlugin(config) {
|
|
|
1746
2449
|
await reconcileNamespace({
|
|
1747
2450
|
namespace: ns,
|
|
1748
2451
|
registryStore,
|
|
1749
|
-
onSkip: (row, err) =>
|
|
1750
|
-
namespace: ns,
|
|
1751
|
-
id: row.id,
|
|
1752
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1753
|
-
})
|
|
2452
|
+
onSkip: (row, err) => skipped(ns, row, err)
|
|
1754
2453
|
});
|
|
1755
2454
|
await setupNamespaceListener({
|
|
1756
2455
|
namespace: ns,
|
|
1757
2456
|
channel: `_substrate_proposals_${ns}`,
|
|
1758
2457
|
pubsub: config.pubsub,
|
|
1759
|
-
registryStore
|
|
2458
|
+
registryStore,
|
|
2459
|
+
onSkip: (row, err) => skipped(ns, row, err)
|
|
1760
2460
|
});
|
|
1761
2461
|
ctx.metric("_system_proposals.namespace_ready", { namespace: ns });
|
|
1762
2462
|
}
|
|
@@ -1787,11 +2487,15 @@ function createProposalsPlugin(config) {
|
|
|
1787
2487
|
edges: controlBinding.edges,
|
|
1788
2488
|
metric: ctx.metric,
|
|
1789
2489
|
proposeCapability: capability,
|
|
2490
|
+
indexCapability,
|
|
1790
2491
|
registerProposalMutations
|
|
1791
2492
|
});
|
|
2493
|
+
},
|
|
2494
|
+
async onStop() {
|
|
2495
|
+
await registryStore.stopStorage();
|
|
1792
2496
|
}
|
|
1793
2497
|
};
|
|
1794
2498
|
}
|
|
1795
2499
|
|
|
1796
2500
|
//#endregion
|
|
1797
|
-
export { createNoopPubSub as n,
|
|
2501
|
+
export { ProposalError as i, createNoopPubSub as n, storageSettled as r, createProposalsPlugin as t };
|