@ultimat3/testing 30.7.0 → 30.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +5 -9
- package/README.md +1 -1
- package/package.json +17 -17
- package/src/define-exercise.ts +36 -12
- package/src/drill-writes.ts +3 -1
- package/src/errors.ts +24 -93
- package/src/exercise-actor.ts +28 -4
- package/src/exercise-judge.ts +78 -0
- package/src/exercise-observed.ts +93 -0
- package/src/exercise-override-input.ts +51 -9
- package/src/exercise-report.ts +5 -0
- package/src/exercise-request.ts +7 -24
- package/src/exercise-setup.ts +34 -17
- package/src/exercise-subjects.ts +20 -5
- package/src/exercise-sweeps.ts +12 -7
- package/src/exercise.ts +134 -119
- package/src/fixture-island.ts +1 -1
- package/src/fixture-jobs.ts +23 -4
- package/src/island-errors.ts +82 -0
- package/src/plan-conditions.ts +2 -2
- package/src/plan-oracle.ts +38 -11
- package/src/plan-probes.ts +94 -0
- package/src/plan-sweep-fix.ts +12 -1
- package/src/plan-sweep.ts +37 -12
- package/src/scale-sweep-graph.ts +3 -0
- package/src/scale-sweep-run.ts +17 -8
- package/src/sealed-dns-resolver.ts +56 -0
- package/src/sealed-dns.ts +63 -64
- package/src/sealed-network.ts +13 -12
- package/src/sealed-node-sockets.ts +63 -0
- package/src/sealed-sockets.ts +13 -8
- package/src/sealed-udp.ts +118 -0
- package/src/seed-graph-plan.ts +11 -0
- package/src/seed-graph.ts +9 -2
- package/src/subject-proof.ts +12 -2
- package/src/tenant-proof-report.ts +79 -29
- package/src/tenant-proof.ts +7 -2
package/src/plan-oracle.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
// The plan oracle: one `explain (format json)` plan, taken with sequential
|
|
2
|
-
// sorts disabled, in; the nodes no index could replace, out.
|
|
3
|
-
// a scan or a sort only when nothing else answers
|
|
1
|
+
// The plan oracle: one `explain (format json)` plan, taken with sequential and bitmap scans (and,
|
|
2
|
+
// for a limited sort, sorts) disabled, in; the nodes no index could replace, out. The planner then
|
|
3
|
+
// picks a scan or a sort only when nothing else answers: the verdict reads the schema, not rows.
|
|
4
4
|
// Pure: JSON in, verdicts out (plan 101, 40-next-query-plans).
|
|
5
5
|
|
|
6
6
|
import { columnsIn } from './plan-conditions';
|
|
7
|
+
import type { ProbeScan } from './plan-probes';
|
|
8
|
+
import { probesOf } from './plan-probes';
|
|
7
9
|
import type { PlanUnservable, SortKeyRef } from './plan-sort-key';
|
|
8
10
|
import { sortKeyPart, sortUnservable } from './plan-sort-key';
|
|
9
11
|
|
|
@@ -28,6 +30,11 @@ export interface PlanVerdict {
|
|
|
28
30
|
readonly arms?: readonly (readonly string[])[];
|
|
29
31
|
/** The alias the plan reads the table by, when it is not the table's own name. */
|
|
30
32
|
readonly alias?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Columns a join ties this whole read to a scan bound by its own key (`plan-probes.ts`): one
|
|
35
|
+
* that leads an index is a probe the planner had and declined on an empty table's costs.
|
|
36
|
+
*/
|
|
37
|
+
readonly probes?: readonly string[];
|
|
31
38
|
}
|
|
32
39
|
|
|
33
40
|
interface PlanNode {
|
|
@@ -36,12 +43,14 @@ interface PlanNode {
|
|
|
36
43
|
readonly alias: string | undefined;
|
|
37
44
|
readonly conditions: readonly string[];
|
|
38
45
|
readonly sortKey: readonly string[];
|
|
39
|
-
readonly indexCond:
|
|
46
|
+
readonly indexCond: string | undefined;
|
|
40
47
|
readonly filter: boolean;
|
|
41
48
|
readonly children: readonly PlanNode[];
|
|
42
49
|
}
|
|
43
50
|
|
|
44
51
|
const SCANS: ReadonlySet<string> = new Set(['Seq Scan', 'Index Scan', 'Index Only Scan']);
|
|
52
|
+
/** Nodes that hold their child's rows for a join: the join's conditions are the child's still. */
|
|
53
|
+
const HOLDS: ReadonlySet<string> = new Set(['Hash', 'Materialize', 'Memoize']);
|
|
45
54
|
/** Conditions a join or a recheck carries for the scans beneath it. */
|
|
46
55
|
const CONDITION_KEYS = ['Index Cond', 'Filter', 'Hash Cond', 'Merge Cond', 'Join Filter'] as const;
|
|
47
56
|
|
|
@@ -67,7 +76,7 @@ function nodeOf(value: unknown): PlanNode | undefined {
|
|
|
67
76
|
alias: text(raw['Alias']),
|
|
68
77
|
conditions: CONDITION_KEYS.flatMap((key) => text(raw[key]) ?? []),
|
|
69
78
|
sortKey,
|
|
70
|
-
indexCond: text(raw['Index Cond'])
|
|
79
|
+
indexCond: text(raw['Index Cond']),
|
|
71
80
|
filter: text(raw['Filter']) !== undefined,
|
|
72
81
|
children: plans.flatMap((child) => nodeOf(child) ?? []),
|
|
73
82
|
};
|
|
@@ -80,6 +89,13 @@ export function planRoot(answer: unknown): unknown {
|
|
|
80
89
|
return record(first)?.['Plan'] ?? first;
|
|
81
90
|
}
|
|
82
91
|
|
|
92
|
+
/** A `Limit` anywhere in the plan: only beneath one is a sort judged, so only then is one planned. */
|
|
93
|
+
export function planLimited(answer: unknown): boolean {
|
|
94
|
+
const limited = (node: PlanNode): boolean => node.type === 'Limit' || node.children.some(limited);
|
|
95
|
+
const root = nodeOf(planRoot(answer));
|
|
96
|
+
return root !== undefined && limited(root);
|
|
97
|
+
}
|
|
98
|
+
|
|
83
99
|
/** Every scan beneath `node` (the node itself included), of any table. */
|
|
84
100
|
const scansBeneath = (node: PlanNode): readonly PlanNode[] => [
|
|
85
101
|
...(node.table !== undefined && SCANS.has(node.type) ? [node] : []),
|
|
@@ -143,18 +159,22 @@ export type PlanTableColumns = ReadonlyMap<string, ReadonlySet<string>>;
|
|
|
143
159
|
/**
|
|
144
160
|
* Every red node of one plan. `Seq Scan` on a judged table; an index scan with no `Index Cond`,
|
|
145
161
|
* unless a `Limit` above it stops it early and no `Filter` makes it walk on; a `Sort` under a
|
|
146
|
-
* `Limit`. A join's conditions are handed down,
|
|
162
|
+
* `Limit`. A join's conditions are handed down, through the nodes that only hold rows for it, so
|
|
163
|
+
* the scan of its inner side names its key — and a whole read tied to a keyed side, its probe.
|
|
147
164
|
*/
|
|
148
165
|
export function judgePlan(answer: unknown, tables: PlanTableColumns): readonly PlanVerdict[] {
|
|
149
166
|
const root = nodeOf(planRoot(answer));
|
|
150
167
|
if (root === undefined) return [];
|
|
151
168
|
const verdicts: PlanVerdict[] = [];
|
|
169
|
+
const scans: (ProbeScan & { readonly verdict: number | undefined })[] = [];
|
|
152
170
|
const walk = (node: PlanNode, limited: boolean, inherited: readonly string[]): void => {
|
|
153
171
|
const conditions = [...inherited, ...node.conditions];
|
|
154
172
|
const columns = node.table === undefined ? undefined : tables.get(node.table);
|
|
155
|
-
if (node.table !== undefined &&
|
|
156
|
-
const
|
|
157
|
-
|
|
173
|
+
if (node.table !== undefined && SCANS.has(node.type)) {
|
|
174
|
+
const judged = columns !== undefined;
|
|
175
|
+
const unbounded =
|
|
176
|
+
node.type === 'Seq Scan' || (node.indexCond === undefined && (!limited || node.filter));
|
|
177
|
+
if (judged && unbounded) {
|
|
158
178
|
const found = columnsIn(conditions, node, columns);
|
|
159
179
|
verdicts.push({
|
|
160
180
|
code: 'X_QUERY_UNINDEXED',
|
|
@@ -165,6 +185,9 @@ export function judgePlan(answer: unknown, tables: PlanTableColumns): readonly P
|
|
|
165
185
|
...(node.alias === undefined || node.alias === node.table ? {} : { alias: node.alias }),
|
|
166
186
|
});
|
|
167
187
|
}
|
|
188
|
+
const { table, alias, indexCond } = node;
|
|
189
|
+
const verdict = judged && unbounded ? verdicts.length - 1 : undefined;
|
|
190
|
+
scans.push({ table, alias, indexCond, conditions, verdict });
|
|
168
191
|
}
|
|
169
192
|
if (node.type === 'Sort' && limited) {
|
|
170
193
|
const sorted = sortVerdict(node, conditions, tables);
|
|
@@ -173,11 +196,15 @@ export function judgePlan(answer: unknown, tables: PlanTableColumns): readonly P
|
|
|
173
196
|
const below = limited || node.type === 'Limit';
|
|
174
197
|
// A join's conditions name both sides: each child keeps only the conjuncts about itself.
|
|
175
198
|
const handed =
|
|
176
|
-
node.type.includes('Join') || node.type === 'Nested Loop' || node.type
|
|
199
|
+
node.type.includes('Join') || node.type === 'Nested Loop' || HOLDS.has(node.type)
|
|
177
200
|
? conditions
|
|
178
201
|
: [];
|
|
179
202
|
for (const child of node.children) walk(child, below, handed);
|
|
180
203
|
};
|
|
181
204
|
walk(root, false, []);
|
|
182
|
-
|
|
205
|
+
const probes = probesOf(scans);
|
|
206
|
+
return verdicts.map((verdict, at) => {
|
|
207
|
+
const found = probes[scans.findIndex((scan) => scan.verdict === at)] ?? [];
|
|
208
|
+
return found.length === 0 ? verdict : { ...verdict, probes: found };
|
|
209
|
+
});
|
|
183
210
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Which whole reads in one plan a keyed scan could have probed instead: the join columns that tie
|
|
2
|
+
// a scan to a side bound by its own key. On empty tables the planner may walk a small-looking
|
|
3
|
+
// index beside a join rather than probe it per row; the probe exists when such a column leads an
|
|
4
|
+
// index, which `plan-sweep-fix.ts` reads off the declaration. Pure.
|
|
5
|
+
|
|
6
|
+
import { splitTop, unwrap } from './plan-conditions';
|
|
7
|
+
|
|
8
|
+
/** One scan of the plan, with every condition it was handed and its own `Index Cond`. */
|
|
9
|
+
export interface ProbeScan {
|
|
10
|
+
readonly table: string;
|
|
11
|
+
readonly alias: string | undefined;
|
|
12
|
+
readonly indexCond: string | undefined;
|
|
13
|
+
readonly conditions: readonly string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const SIDE = String.raw`(?:"?([A-Za-z_][\w$]*)"?\.)?"?([A-Za-z_][\w$]*)"?`;
|
|
17
|
+
/** `a.x = b.y`: one column against another, the only shape a probe answers. */
|
|
18
|
+
const COLUMN_EQUALITY = new RegExp(`^${SIDE}\\s*=\\s*${SIDE}$`);
|
|
19
|
+
const QUALIFIER = /"?([A-Za-z_][\w$]*)"?\.(?="?[A-Za-z_])/g;
|
|
20
|
+
|
|
21
|
+
const nameOf = (scan: ProbeScan): string => scan.alias ?? scan.table;
|
|
22
|
+
|
|
23
|
+
const conjuncts = (conditions: readonly string[]): string[] =>
|
|
24
|
+
conditions.flatMap((condition) => splitTop(unwrap(condition), 'AND'));
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Bound by its own key: an `Index Cond` conjunct that names no other scan. A parameterized inner
|
|
28
|
+
* scan's condition is the join itself, so it keys nothing on its own.
|
|
29
|
+
*/
|
|
30
|
+
function selfKeyed(scan: ProbeScan, others: ReadonlySet<string>): boolean {
|
|
31
|
+
if (scan.indexCond === undefined) return false;
|
|
32
|
+
return conjuncts([scan.indexCond]).some((conjunct) =>
|
|
33
|
+
[...conjunct.replace(/'[^']*'/g, '').matchAll(QUALIFIER)].every(
|
|
34
|
+
([, qualifier]) => qualifier === undefined || !others.has(qualifier),
|
|
35
|
+
),
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface Tie {
|
|
40
|
+
readonly scan: string;
|
|
41
|
+
readonly column: string;
|
|
42
|
+
readonly other: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Every `a.x = b.y` between two scans, both ways round, wherever the plan states it: a join's
|
|
47
|
+
* filter, or a parameterized scan's own `Index Cond`, where its columns go unqualified.
|
|
48
|
+
*/
|
|
49
|
+
function tiesOf(scans: readonly ProbeScan[], names: ReadonlySet<string>): readonly Tie[] {
|
|
50
|
+
const ties: Tie[] = [];
|
|
51
|
+
for (const scan of scans) {
|
|
52
|
+
for (const conjunct of conjuncts([...scan.conditions, scan.indexCond ?? ''])) {
|
|
53
|
+
const match = COLUMN_EQUALITY.exec(conjunct);
|
|
54
|
+
if (match === null) continue;
|
|
55
|
+
const [, leftOf = nameOf(scan), left, rightOf = nameOf(scan), right] = match;
|
|
56
|
+
if (left === undefined || right === undefined || leftOf === rightOf) continue;
|
|
57
|
+
if (!names.has(leftOf) || !names.has(rightOf)) continue;
|
|
58
|
+
ties.push({ scan: leftOf, column: left, other: rightOf });
|
|
59
|
+
ties.push({ scan: rightOf, column: right, other: leftOf });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return ties;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Each scan's probe columns, in plan order: the columns it is tied to a keyed scan by. A scan
|
|
67
|
+
* that has any is keyed in turn, so the probe carries along a chain of joins; two scans tied only
|
|
68
|
+
* to each other probe nothing — one of them is read whole either way.
|
|
69
|
+
*/
|
|
70
|
+
export function probesOf(scans: readonly ProbeScan[]): readonly (readonly string[])[] {
|
|
71
|
+
const names = new Set(scans.map(nameOf));
|
|
72
|
+
const keyed = new Set(
|
|
73
|
+
scans
|
|
74
|
+
.filter((scan) => selfKeyed(scan, new Set([...names].filter((n) => n !== nameOf(scan)))))
|
|
75
|
+
.map(nameOf),
|
|
76
|
+
);
|
|
77
|
+
const ties = tiesOf(scans, names);
|
|
78
|
+
const probes = scans.map((): string[] => []);
|
|
79
|
+
for (let grew = true; grew; ) {
|
|
80
|
+
grew = false;
|
|
81
|
+
scans.forEach((scan, at) => {
|
|
82
|
+
const own = nameOf(scan);
|
|
83
|
+
if (keyed.has(own)) return;
|
|
84
|
+
const found = ties.flatMap((tie) =>
|
|
85
|
+
tie.scan === own && keyed.has(tie.other) ? [tie.column] : [],
|
|
86
|
+
);
|
|
87
|
+
if (found.length === 0) return;
|
|
88
|
+
probes[at] = [...new Set(found)];
|
|
89
|
+
keyed.add(own);
|
|
90
|
+
grew = true;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
return probes;
|
|
94
|
+
}
|
package/src/plan-sweep-fix.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// From the oracle's red nodes to what an author pastes: the entity, and the `indexes: [{ on }]`
|
|
2
2
|
// entry by property. Pure. Also where a red node is argued away by the declaration itself: a
|
|
3
|
-
// lookup that pins a unique key reads one row whatever the planner walked to find it
|
|
3
|
+
// lookup that pins a unique key reads one row whatever the planner walked to find it, and a whole
|
|
4
|
+
// read a keyed join side could probe by a declared index is one the planner declined on cost.
|
|
4
5
|
|
|
5
6
|
import type { PlanVerdict, PlanVerdictCode } from './plan-oracle';
|
|
6
7
|
import type { PlanUnservable } from './plan-sort-key';
|
|
@@ -173,6 +174,15 @@ function armFindings(
|
|
|
173
174
|
.map((arm) => arm.flatMap((column) => property(column) ?? []));
|
|
174
175
|
}
|
|
175
176
|
|
|
177
|
+
/**
|
|
178
|
+
* A whole read a keyed side could probe: a column it is joined by leads a declared index. On empty
|
|
179
|
+
* tables the planner may walk a small-looking index (a partial one the statement implies) rather
|
|
180
|
+
* than probe it per row; at size it probes. Credited, never re-proven, as a range index is.
|
|
181
|
+
*/
|
|
182
|
+
const probed = (keys: readonly (readonly string[])[], verdict: PlanVerdict): boolean =>
|
|
183
|
+
verdict.code === 'X_QUERY_UNINDEXED' &&
|
|
184
|
+
(verdict.probes ?? []).some((column) => keys.some((key) => key[0] === column));
|
|
185
|
+
|
|
176
186
|
/**
|
|
177
187
|
* The plan was taken with every declared index in place: one that leads with exactly these
|
|
178
188
|
* columns is already there, and printing it as the fix would change nothing.
|
|
@@ -220,6 +230,7 @@ export function planFindings(
|
|
|
220
230
|
// no order after a range column, so the index is the equality columns, then the range.
|
|
221
231
|
const ranged = walked && verdict.range.length > 0;
|
|
222
232
|
const declared = declaredIndexes(entity, statement);
|
|
233
|
+
if (probed(declared, verdict)) return [];
|
|
223
234
|
const bounded = verdict.equality.length > 0 || verdict.range.length > 0;
|
|
224
235
|
if (!bounded && verdict.arms !== undefined && verdict.code === 'X_QUERY_UNINDEXED') {
|
|
225
236
|
const arms = armFindings(verdict, declared, (column) => byColumn.get(column));
|
package/src/plan-sweep.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
// The plan sweep: every distinct statement the exercise pass sent whose first relation is an app
|
|
2
|
-
// table, planned on the migrated database with sequential
|
|
3
|
-
// judged by `plan-oracle.ts`, each red node named as the
|
|
4
|
-
// Read-only: `PREPARE` in a read-only transaction
|
|
2
|
+
// table, planned on the migrated database with sequential and bitmap scans disabled — and, for a
|
|
3
|
+
// limited read's sort only, sorts too — judged by `plan-oracle.ts`, each red node named as the
|
|
4
|
+
// `indexes:` entry that answers it. Read-only: `PREPARE` in a read-only transaction, `DEALLOCATE`.
|
|
5
5
|
|
|
6
6
|
import { renderThrowable, UltimateError } from '@ultimat3/core';
|
|
7
7
|
import type { DbClient } from '@ultimat3/db';
|
|
8
8
|
import { isReservable, raw, sql, withReadTransaction } from '@ultimat3/db';
|
|
9
9
|
import './errors';
|
|
10
|
-
import type { PlanTableColumns } from './plan-oracle';
|
|
11
|
-
import { judgePlan } from './plan-oracle';
|
|
10
|
+
import type { PlanTableColumns, PlanVerdict } from './plan-oracle';
|
|
11
|
+
import { judgePlan, planLimited } from './plan-oracle';
|
|
12
12
|
import type { PlanFinding } from './plan-sweep-fix';
|
|
13
13
|
import { planFindings } from './plan-sweep-fix';
|
|
14
14
|
import type { SeedEntity } from './seed-graph-shape';
|
|
@@ -47,13 +47,18 @@ export class QueryPlanUnavailableError extends UltimateError {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
/**
|
|
51
|
-
|
|
50
|
+
/**
|
|
51
|
+
* The planner's choices the sweep removes: what remains is what the schema can serve. The access
|
|
52
|
+
* path is judged with sorts allowed — off, an `order by` makes the planner walk whatever index
|
|
53
|
+
* holds the order, whole, instead of the one that keys the read. Only a sort under a `Limit` is
|
|
54
|
+
* judged, so only a limited read is planned again with sorts off, for its sort alone.
|
|
55
|
+
*/
|
|
56
|
+
const ACCESS = {
|
|
52
57
|
enable_seqscan: 'off',
|
|
53
58
|
enable_bitmapscan: 'off',
|
|
54
|
-
enable_sort: 'off',
|
|
55
59
|
plan_cache_mode: 'force_generic_plan',
|
|
56
60
|
} as const;
|
|
61
|
+
const SORTS = { ...ACCESS, enable_sort: 'off' } as const;
|
|
57
62
|
const PREPARED = 'x_plan_sweep';
|
|
58
63
|
const FIRST_RELATION = /\b(?:from|into|update|join)\s+(?:"?\w+"?\.)?"?([A-Za-z_][\w$]*)"?/i;
|
|
59
64
|
|
|
@@ -66,7 +71,11 @@ interface Planned {
|
|
|
66
71
|
readonly refused?: string;
|
|
67
72
|
}
|
|
68
73
|
|
|
69
|
-
async function explain(
|
|
74
|
+
async function explain(
|
|
75
|
+
client: DbClient,
|
|
76
|
+
statement: string,
|
|
77
|
+
settings: typeof ACCESS | typeof SORTS,
|
|
78
|
+
): Promise<Planned> {
|
|
70
79
|
const connection = isReservable(client) ? await client.reserve() : undefined;
|
|
71
80
|
const on = connection ?? client;
|
|
72
81
|
let prepared = false;
|
|
@@ -81,7 +90,7 @@ async function explain(client: DbClient, statement: string): Promise<Planned> {
|
|
|
81
90
|
raw(`explain (format json) execute ${PREPARED}${args}`),
|
|
82
91
|
);
|
|
83
92
|
},
|
|
84
|
-
{ settings
|
|
93
|
+
{ settings, client: on },
|
|
85
94
|
);
|
|
86
95
|
return { plan: rows[0]?.['QUERY PLAN'] };
|
|
87
96
|
} catch (error) {
|
|
@@ -93,6 +102,22 @@ async function explain(client: DbClient, statement: string): Promise<Planned> {
|
|
|
93
102
|
}
|
|
94
103
|
}
|
|
95
104
|
|
|
105
|
+
/** One statement's verdicts: its access path planned as is, its limited sort with sorts off. */
|
|
106
|
+
async function judge(
|
|
107
|
+
client: DbClient,
|
|
108
|
+
statement: string,
|
|
109
|
+
tables: PlanTableColumns,
|
|
110
|
+
): Promise<{ readonly verdicts?: readonly PlanVerdict[]; readonly refused?: string }> {
|
|
111
|
+
const access = await explain(client, statement, ACCESS);
|
|
112
|
+
if (access.refused !== undefined) return { refused: access.refused };
|
|
113
|
+
const read = judgePlan(access.plan, tables).filter((v) => v.code === 'X_QUERY_UNINDEXED');
|
|
114
|
+
if (!planLimited(access.plan)) return { verdicts: read };
|
|
115
|
+
const sorted = await explain(client, statement, SORTS);
|
|
116
|
+
if (sorted.refused !== undefined) return { refused: sorted.refused };
|
|
117
|
+
const sorts = judgePlan(sorted.plan, tables).filter((v) => v.code === 'X_QUERY_SORT_UNSERVED');
|
|
118
|
+
return { verdicts: [...read, ...sorts] };
|
|
119
|
+
}
|
|
120
|
+
|
|
96
121
|
interface ProofBuilder {
|
|
97
122
|
planned: number;
|
|
98
123
|
readonly findings: PlanFinding[];
|
|
@@ -132,7 +157,7 @@ export async function sweepPlans(
|
|
|
132
157
|
if (entity === undefined) continue;
|
|
133
158
|
const declared = entity.$describe().scan;
|
|
134
159
|
const exemptBy = scan ?? declared;
|
|
135
|
-
const planned = exemptBy === undefined ? await
|
|
160
|
+
const planned = exemptBy === undefined ? await judge(client, statement, judged) : {};
|
|
136
161
|
for (const name of primitives) {
|
|
137
162
|
const proof = proofOf(name);
|
|
138
163
|
proof.planned += 1;
|
|
@@ -140,7 +165,7 @@ export async function sweepPlans(
|
|
|
140
165
|
else if (planned.refused !== undefined) {
|
|
141
166
|
proof.unplannable.push({ statement, reason: planned.refused });
|
|
142
167
|
} else {
|
|
143
|
-
proof.findings.push(...planFindings(statement,
|
|
168
|
+
proof.findings.push(...planFindings(statement, planned.verdicts ?? [], byTable));
|
|
144
169
|
}
|
|
145
170
|
}
|
|
146
171
|
}
|
package/src/scale-sweep-graph.ts
CHANGED
|
@@ -28,12 +28,15 @@ export function scaleGraph(
|
|
|
28
28
|
readonly seed: number;
|
|
29
29
|
/** The app's `defineExercise` factories: a table only they satisfy is built by them here too. */
|
|
30
30
|
readonly factories?: Readonly<Record<string, SeedFactory>> | undefined;
|
|
31
|
+
/** The last graph written to the database before this one; `main` when omitted. */
|
|
32
|
+
readonly beside?: SeedGraph | undefined;
|
|
31
33
|
},
|
|
32
34
|
): ScaleGraph {
|
|
33
35
|
const sized = seedGraph(Object.fromEntries(entities.map((entity) => [entity.$name, entity])), {
|
|
34
36
|
tenants: 1,
|
|
35
37
|
perTable: options.perTable,
|
|
36
38
|
seed: options.seed,
|
|
39
|
+
beside: options.beside ?? main,
|
|
37
40
|
...(options.factories === undefined ? {} : { factories: options.factories }),
|
|
38
41
|
});
|
|
39
42
|
const shared = (graph: SeedGraph) => graph.inserts.filter((insert) => insert.tenant === null);
|
package/src/scale-sweep-run.ts
CHANGED
|
@@ -9,6 +9,7 @@ import type { RunJobs } from './fixture-jobs';
|
|
|
9
9
|
import { testJobs } from './fixture-jobs';
|
|
10
10
|
import type { ScaleProof, StatementTally } from './scale-sweep';
|
|
11
11
|
import { judgeScale, SCALE_SIZES } from './scale-sweep';
|
|
12
|
+
import type { ScaleGraph } from './scale-sweep-graph';
|
|
12
13
|
import { scaleGraph } from './scale-sweep-graph';
|
|
13
14
|
import type { SeedFactory, SeedGraph, SeedWriter } from './seed-graph-plan';
|
|
14
15
|
import { insertSeedGraph } from './seed-graph-plan';
|
|
@@ -58,6 +59,9 @@ async function callAt(
|
|
|
58
59
|
options: ScaleSweepOptions,
|
|
59
60
|
self: boolean,
|
|
60
61
|
): Promise<Call> {
|
|
62
|
+
// Before the setup: an override's input binds on the queue this call runs on, so a job it
|
|
63
|
+
// enqueues is there when the call looks it up by id.
|
|
64
|
+
await using jobs = await testJobs();
|
|
61
65
|
let prepared: Awaited<ReturnType<ScaleSetup>>;
|
|
62
66
|
try {
|
|
63
67
|
prepared = await options.setup(subject, graph, tenant, self);
|
|
@@ -67,7 +71,6 @@ async function callAt(
|
|
|
67
71
|
return { failed: `its exercise.ts input threw: ${renderThrowable(error)}` };
|
|
68
72
|
}
|
|
69
73
|
if (prepared === undefined) return { failed: 'no actor its policy permits' };
|
|
70
|
-
await using jobs = await testJobs();
|
|
71
74
|
options.room.begin();
|
|
72
75
|
try {
|
|
73
76
|
const answer = await subject.call(prepared.input, prepared.actor, tenant, jobs);
|
|
@@ -85,14 +88,20 @@ export async function scaleSweep(
|
|
|
85
88
|
): Promise<ReadonlyMap<string, ScaleProof>> {
|
|
86
89
|
const proofs = new Map<string, ScaleProof>();
|
|
87
90
|
if (options.subjects.length === 0) return proofs;
|
|
88
|
-
const sizes = [
|
|
91
|
+
const sizes: ScaleGraph[] = [];
|
|
92
|
+
for (const [i, perTable] of [SCALE_SIZES.small, SCALE_SIZES.large].entries()) {
|
|
89
93
|
// Hashed, never `seed + 1`: a later pass on the same database may be seeded with that number.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
94
|
+
// Each beside the one written before it, so no row position — and no value built from one —
|
|
95
|
+
// repeats across the three graphs in the database.
|
|
96
|
+
sizes.push(
|
|
97
|
+
scaleGraph(options.main, options.entities, {
|
|
98
|
+
perTable,
|
|
99
|
+
seed: fnv1a(`scale:${options.seed}:${i}`) || 1,
|
|
100
|
+
factories: options.factories,
|
|
101
|
+
beside: sizes[i - 1]?.write ?? options.main,
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
}
|
|
96
105
|
try {
|
|
97
106
|
for (const size of sizes) await insertSeedGraph(size.write, options.writer);
|
|
98
107
|
} catch (error) {
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// The `Resolver` half of the sealed resolver: an instance asks its own c-ares channel, never
|
|
2
|
+
// `Bun.dns`, so each class is sealed on its prototype (a named import shares it). A sealed instance
|
|
3
|
+
// answers exactly what the module function of the same name answers — that one is sealed at the root.
|
|
4
|
+
|
|
5
|
+
// why: `node:dns`'s two `Resolver` classes are what an app's own resolver instance is built from.
|
|
6
|
+
import nodeDns from 'node:dns';
|
|
7
|
+
import type { DnsGate } from './sealed-dns';
|
|
8
|
+
|
|
9
|
+
/** Each `Resolver` method and the query its arguments ask; `resolve` names its record type. */
|
|
10
|
+
const RESOLVER_QUERIES: Readonly<Record<string, (args: readonly unknown[]) => string>> = {
|
|
11
|
+
resolve: (args) => (typeof args[1] === 'string' ? args[1].toUpperCase() : 'A'),
|
|
12
|
+
resolve4: () => 'A',
|
|
13
|
+
resolve6: () => 'AAAA',
|
|
14
|
+
resolveAny: () => 'ANY',
|
|
15
|
+
resolveCaa: () => 'CAA',
|
|
16
|
+
resolveCname: () => 'CNAME',
|
|
17
|
+
resolveMx: () => 'MX',
|
|
18
|
+
resolveNaptr: () => 'NAPTR',
|
|
19
|
+
resolveNs: () => 'NS',
|
|
20
|
+
resolvePtr: () => 'PTR',
|
|
21
|
+
resolveSoa: () => 'SOA',
|
|
22
|
+
resolveSrv: () => 'SRV',
|
|
23
|
+
resolveTxt: () => 'TXT',
|
|
24
|
+
reverse: () => 'REVERSE',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type Method = (this: unknown, ...args: unknown[]) => unknown;
|
|
28
|
+
|
|
29
|
+
/** Seal one class's prototype, answering through `moduleApi`; returns the uninstaller. */
|
|
30
|
+
const sealPrototype = (prototype: object, moduleApi: object, gate: DnsGate): (() => void) => {
|
|
31
|
+
const originals = new Map<string, Method>();
|
|
32
|
+
for (const [name, queryOf] of Object.entries(RESOLVER_QUERIES)) {
|
|
33
|
+
const original = Reflect.get(prototype, name) as Method;
|
|
34
|
+
const viaModule = Reflect.get(moduleApi, name) as Method;
|
|
35
|
+
originals.set(name, original);
|
|
36
|
+
// A `function`, not an arrow: the real method needs the instance its own servers live on.
|
|
37
|
+
Reflect.set(prototype, name, function sealed(this: unknown, ...args: unknown[]): unknown {
|
|
38
|
+
const verdict = gate(String(args[0]), queryOf(args));
|
|
39
|
+
if ('real' in verdict) return Reflect.apply(original, this, args);
|
|
40
|
+
return Reflect.apply(viaModule, moduleApi, args);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return () => {
|
|
44
|
+
for (const [name, original] of originals) Reflect.set(prototype, name, original);
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** Seal both `Resolver` classes — callback and promise — and return the one uninstaller. */
|
|
49
|
+
export function installResolverSeal(gate: DnsGate): () => void {
|
|
50
|
+
const restoreCallback = sealPrototype(nodeDns.Resolver.prototype, nodeDns, gate);
|
|
51
|
+
const restorePromise = sealPrototype(nodeDns.promises.Resolver.prototype, nodeDns.promises, gate);
|
|
52
|
+
return () => {
|
|
53
|
+
restoreCallback();
|
|
54
|
+
restorePromise();
|
|
55
|
+
};
|
|
56
|
+
}
|
package/src/sealed-dns.ts
CHANGED
|
@@ -1,86 +1,85 @@
|
|
|
1
|
-
// The resolver half of the sealed network: `Bun.dns
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// The resolver half of the sealed network: every `Bun.dns` resolution function, which is the ROOT
|
|
2
|
+
// every `node:dns` module function calls at call time — so a named import bound before the seal is
|
|
3
|
+
// sealed too (`sealed-dns-paths.test.ts`). `Resolver` instances: `sealed-dns-resolver.ts`. WHAT a
|
|
4
|
+
// name resolves to is `sealed-network.ts`'s one gate, handed in.
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
import nodeDns from 'node:dns';
|
|
6
|
+
import { installResolverSeal } from './sealed-dns-resolver';
|
|
8
7
|
|
|
9
|
-
/** One
|
|
8
|
+
/** One query's verdict: the addresses to answer, a refusal to raise, or `real` for the resolver. */
|
|
10
9
|
export type DnsVerdict =
|
|
11
10
|
| { readonly answer: readonly string[] }
|
|
12
11
|
| { readonly refused: Error }
|
|
13
12
|
| { readonly real: true };
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
/** `LOOKUP` (getaddrinfo), `REVERSE` (an address to its names) or a record type: `A`, `MX`, … */
|
|
15
|
+
export type DnsGate = (name: string, query: string) => DnsVerdict;
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
/**
|
|
18
|
+
* The queries an address list can answer. A mock declares an HTTP endpoint, not a zone: an MX or
|
|
19
|
+
* TXT answer would be invented, so the gate answers only these and refuses the rest.
|
|
20
|
+
*/
|
|
21
|
+
export const ADDRESS_QUERIES: ReadonlySet<string> = new Set(['LOOKUP', 'A', 'ANY']);
|
|
21
22
|
|
|
22
23
|
const familyOf = (address: string): number => (address.includes(':') ? 6 : 4);
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
/** The raw shape `Bun.dns` answers per query; `node:dns` maps it as it maps a real one. */
|
|
26
|
+
const shaped = (query: string, addresses: readonly string[]): unknown[] =>
|
|
27
|
+
addresses.map((address) => {
|
|
28
|
+
if (query === 'LOOKUP') return { address, family: familyOf(address), ttl: 0 };
|
|
29
|
+
if (query === 'ANY') return { address, ttl: 0, type: familyOf(address) === 6 ? 'AAAA' : 'A' };
|
|
30
|
+
return { address, ttl: 0 };
|
|
31
|
+
});
|
|
26
32
|
|
|
27
|
-
|
|
33
|
+
/** Each async `Bun.dns` resolver and the query its arguments ask. */
|
|
34
|
+
const BUN_QUERIES: Readonly<Record<string, (args: readonly unknown[]) => string>> = {
|
|
35
|
+
lookup: () => 'LOOKUP',
|
|
36
|
+
resolve: (args) => (typeof args[1] === 'string' ? args[1].toUpperCase() : 'A'),
|
|
37
|
+
resolveAny: () => 'ANY',
|
|
38
|
+
resolveCaa: () => 'CAA',
|
|
39
|
+
resolveCname: () => 'CNAME',
|
|
40
|
+
resolveMx: () => 'MX',
|
|
41
|
+
resolveNaptr: () => 'NAPTR',
|
|
42
|
+
resolveNs: () => 'NS',
|
|
43
|
+
resolvePtr: () => 'PTR',
|
|
44
|
+
resolveSoa: () => 'SOA',
|
|
45
|
+
resolveSrv: () => 'SRV',
|
|
46
|
+
resolveTxt: () => 'TXT',
|
|
47
|
+
reverse: () => 'REVERSE',
|
|
48
|
+
lookupService: () => 'REVERSE',
|
|
49
|
+
};
|
|
28
50
|
|
|
29
|
-
|
|
30
|
-
const wantsAll = (options: unknown): boolean =>
|
|
31
|
-
typeof options === 'object' && options !== null && 'all' in options && options.all === true;
|
|
51
|
+
type Resolve = (...args: unknown[]) => Promise<unknown>;
|
|
32
52
|
|
|
33
53
|
/**
|
|
34
|
-
* Patch
|
|
54
|
+
* Patch every resolver and return the uninstaller, which puts back exactly what was there.
|
|
35
55
|
* Assigned through `Reflect.set`: each is a writable data property the types declare a function.
|
|
36
56
|
*/
|
|
37
57
|
export function installDnsSeal(gate: DnsGate): () => void {
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
58
|
+
const originals = new Map<string, unknown>();
|
|
59
|
+
for (const [name, queryOf] of Object.entries(BUN_QUERIES)) {
|
|
60
|
+
const original = Reflect.get(Bun.dns, name) as Resolve;
|
|
61
|
+
originals.set(name, original);
|
|
62
|
+
const sealed: Resolve = async (...args) => {
|
|
63
|
+
const query = queryOf(args);
|
|
64
|
+
const verdict = gate(String(args[0]), query);
|
|
65
|
+
if ('refused' in verdict) throw verdict.refused;
|
|
66
|
+
if ('answer' in verdict) return shaped(query, verdict.answer);
|
|
67
|
+
return Reflect.apply(original, Bun.dns, args);
|
|
68
|
+
};
|
|
69
|
+
Reflect.set(Bun.dns, name, sealed);
|
|
70
|
+
}
|
|
71
|
+
// `prefetch` is synchronous and answers nothing: a mocked name has nothing to warm, and an
|
|
72
|
+
// unmocked one throws here rather than hide the egress until the dial that follows it.
|
|
73
|
+
const originalPrefetch = Reflect.get(Bun.dns, 'prefetch') as (...args: unknown[]) => void;
|
|
74
|
+
originals.set('prefetch', originalPrefetch);
|
|
75
|
+
Reflect.set(Bun.dns, 'prefetch', (...args: unknown[]): void => {
|
|
76
|
+
const verdict = gate(String(args[0]), 'LOOKUP');
|
|
46
77
|
if ('refused' in verdict) throw verdict.refused;
|
|
47
|
-
if ('
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const sealedNode = (hostname: string, ...rest: unknown[]): void => {
|
|
52
|
-
const verdict = gate(hostname);
|
|
53
|
-
if ('real' in verdict) {
|
|
54
|
-
Reflect.apply(originalNode, nodeDns, [hostname, ...rest]);
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
const callback = rest.findLast((one) => typeof one === 'function') as NodeCallback | undefined;
|
|
58
|
-
const options = rest.length > 1 ? rest[0] : undefined;
|
|
59
|
-
// A real lookup answers on a later tick, and a caller may rely on it: never synchronously.
|
|
60
|
-
queueMicrotask(() => {
|
|
61
|
-
if ('refused' in verdict) return callback?.(verdict.refused);
|
|
62
|
-
const entries = entriesOf(verdict.answer);
|
|
63
|
-
if (wantsAll(options)) return callback?.(null, entries);
|
|
64
|
-
return callback?.(null, entries[0]?.address, entries[0]?.family);
|
|
65
|
-
});
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const sealedPromises = async (hostname: string, options?: unknown): Promise<unknown> => {
|
|
69
|
-
const verdict = gate(hostname);
|
|
70
|
-
if ('refused' in verdict) throw verdict.refused;
|
|
71
|
-
if ('real' in verdict) {
|
|
72
|
-
return Reflect.apply(originalPromises, nodeDns.promises, [hostname, options]);
|
|
73
|
-
}
|
|
74
|
-
const entries = entriesOf(verdict.answer);
|
|
75
|
-
return wantsAll(options) ? entries : entries[0];
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
Reflect.set(Bun.dns, 'lookup', sealedBun);
|
|
79
|
-
Reflect.set(nodeDns, 'lookup', sealedNode);
|
|
80
|
-
Reflect.set(nodeDns.promises, 'lookup', sealedPromises);
|
|
78
|
+
if ('real' in verdict) Reflect.apply(originalPrefetch, Bun.dns, args);
|
|
79
|
+
});
|
|
80
|
+
const restoreResolver = installResolverSeal(gate);
|
|
81
81
|
return () => {
|
|
82
|
-
Reflect.set(Bun.dns,
|
|
83
|
-
|
|
84
|
-
Reflect.set(nodeDns.promises, 'lookup', originalPromises);
|
|
82
|
+
for (const [name, original] of originals) Reflect.set(Bun.dns, name, original);
|
|
83
|
+
restoreResolver();
|
|
85
84
|
};
|
|
86
85
|
}
|