@pbuilder/sdk 0.1.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/LICENSE +21 -0
- package/README.md +202 -0
- package/dist/bin/pbuilder-codegen.js +506 -0
- package/dist/bin/pbuilder-runner.d.ts +1 -0
- package/dist/bin/pbuilder-runner.js +47 -0
- package/dist/commons/classify-content.d.ts +40 -0
- package/dist/commons/classify-content.js +32 -0
- package/dist/commons/index.d.ts +271 -0
- package/dist/commons/index.js +276 -0
- package/dist/conformance/index.d.ts +115 -0
- package/dist/conformance/index.js +218 -0
- package/dist/conformance/run-vehicle.d.ts +19 -0
- package/dist/conformance/run-vehicle.js +104 -0
- package/dist/core/authoring-error.d.ts +116 -0
- package/dist/core/authoring-error.js +205 -0
- package/dist/core/base-handle.d.ts +17 -0
- package/dist/core/base-handle.js +4 -0
- package/dist/core/context.d.ts +85 -0
- package/dist/core/context.js +356 -0
- package/dist/core/deep-equal.d.ts +1 -0
- package/dist/core/deep-equal.js +29 -0
- package/dist/core/define-dialect.d.ts +94 -0
- package/dist/core/define-dialect.js +121 -0
- package/dist/core/dialect-error.d.ts +8 -0
- package/dist/core/dialect-error.js +26 -0
- package/dist/core/dialect-handle.d.ts +21 -0
- package/dist/core/dialect-handle.js +382 -0
- package/dist/core/directive-factory.d.ts +47 -0
- package/dist/core/directive-factory.js +154 -0
- package/dist/core/emit-rejection.d.ts +19 -0
- package/dist/core/emit-rejection.js +31 -0
- package/dist/core/engine-client.d.ts +7 -0
- package/dist/core/engine-client.js +5 -0
- package/dist/core/fs-errors.d.ts +1 -0
- package/dist/core/fs-errors.js +5 -0
- package/dist/core/handle-state.d.ts +24 -0
- package/dist/core/handle-state.js +4 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.js +6 -0
- package/dist/core/jsx-name-validator.d.ts +19 -0
- package/dist/core/jsx-name-validator.js +104 -0
- package/dist/core/reject-tail.d.ts +23 -0
- package/dist/core/reject-tail.js +63 -0
- package/dist/core/schema/input-rejection.d.ts +4 -0
- package/dist/core/schema/input-rejection.js +32 -0
- package/dist/core/schema/schema-digest.d.ts +1 -0
- package/dist/core/schema/schema-digest.js +7 -0
- package/dist/core/schema/schema-discovery.d.ts +13 -0
- package/dist/core/schema/schema-discovery.js +51 -0
- package/dist/core/schema/schema-locate.d.ts +10 -0
- package/dist/core/schema/schema-locate.js +192 -0
- package/dist/core/schema/schema-model.d.ts +18 -0
- package/dist/core/schema/schema-model.js +15 -0
- package/dist/core/schema/schema-parse.d.ts +20 -0
- package/dist/core/schema/schema-parse.js +80 -0
- package/dist/core/schema/schema-sufficiency.d.ts +17 -0
- package/dist/core/schema/schema-sufficiency.js +54 -0
- package/dist/core/schema/schema-validate.d.ts +14 -0
- package/dist/core/schema/schema-validate.js +64 -0
- package/dist/core/session.d.ts +13 -0
- package/dist/core/session.js +72 -0
- package/dist/core/wire.d.ts +87 -0
- package/dist/core/wire.js +49 -0
- package/dist/dialects/react/ast.d.ts +25 -0
- package/dist/dialects/react/ast.js +80 -0
- package/dist/dialects/react/index.d.ts +39 -0
- package/dist/dialects/react/index.js +71 -0
- package/dist/dialects/react/ops.d.ts +56 -0
- package/dist/dialects/react/ops.js +186 -0
- package/dist/dialects/typescript/ast.d.ts +42 -0
- package/dist/dialects/typescript/ast.js +93 -0
- package/dist/dialects/typescript/index.d.ts +32 -0
- package/dist/dialects/typescript/index.js +40 -0
- package/dist/dialects/typescript/ops.d.ts +123 -0
- package/dist/dialects/typescript/ops.js +365 -0
- package/dist/dry-run/index.d.ts +2 -0
- package/dist/dry-run/index.js +1 -0
- package/dist/dry-run/plan.d.ts +69 -0
- package/dist/dry-run/plan.js +44 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/runner-manifest.json +104 -0
- package/dist/scaffold/classify-transport.d.ts +57 -0
- package/dist/scaffold/classify-transport.js +123 -0
- package/dist/scaffold/containment.d.ts +54 -0
- package/dist/scaffold/containment.js +242 -0
- package/dist/scaffold/expander.d.ts +30 -0
- package/dist/scaffold/expander.js +161 -0
- package/dist/scaffold/filename-pipeline.d.ts +39 -0
- package/dist/scaffold/filename-pipeline.js +125 -0
- package/dist/scaffold/index.d.ts +45 -0
- package/dist/scaffold/index.js +99 -0
- package/dist/scaffold/walk.d.ts +25 -0
- package/dist/scaffold/walk.js +124 -0
- package/dist/testing/contract-fake.d.ts +17 -0
- package/dist/testing/contract-fake.js +221 -0
- package/dist/testing/index.d.ts +84 -0
- package/dist/testing/index.js +82 -0
- package/dist/testing/rejection-messages.d.ts +7 -0
- package/dist/testing/rejection-messages.js +12 -0
- package/dist/transport/bootstrap-bridge.d.ts +33 -0
- package/dist/transport/bootstrap-bridge.js +65 -0
- package/dist/transport/error-text.d.ts +8 -0
- package/dist/transport/error-text.js +44 -0
- package/dist/transport/exit-codes.d.ts +2 -0
- package/dist/transport/exit-codes.js +35 -0
- package/dist/transport/factory-pointer.d.ts +32 -0
- package/dist/transport/factory-pointer.js +63 -0
- package/dist/transport/frame-reader.d.ts +10 -0
- package/dist/transport/frame-reader.js +81 -0
- package/dist/transport/framing.d.ts +20 -0
- package/dist/transport/framing.js +98 -0
- package/dist/transport/runner.d.ts +25 -0
- package/dist/transport/runner.js +308 -0
- package/dist/transport/single-instance-probe.d.ts +23 -0
- package/dist/transport/single-instance-probe.js +119 -0
- package/dist/transport/stdio-engine-client.d.ts +27 -0
- package/dist/transport/stdio-engine-client.js +243 -0
- package/dist/transport/wire-protocol.d.ts +33 -0
- package/dist/transport/wire-protocol.js +22 -0
- package/package.json +93 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// CONF-01: the conformance kit — testDialect / testOpPack (ADR-0012).
|
|
2
|
+
//
|
|
3
|
+
// S-004 (ADR-0012 amendment): real CORE bodies against the REAL TypeScript dialect — byte-
|
|
4
|
+
// exact round-trip (REQ-DC-01), single-op fidelity + unchanged-elsewhere (REQ-DC-02),
|
|
5
|
+
// coalescing-to-one content-verified (REQ-DC-03), seam-serializability (REQ-DC-04), and the
|
|
6
|
+
// read-boundary-split live counterpart (REQ-DC-05.2). Both functions are now
|
|
7
|
+
// `Promise<void>` (rev 3, Q3): observing DC-03/DC-04 requires awaiting a REAL async
|
|
8
|
+
// coalescing run (the handle's `#tail`, the run-boundary join, and `Session.flush` are all
|
|
9
|
+
// async) — a synchronous body could only inspect the emitted batch via a mock/bypass, which
|
|
10
|
+
// ADR-0012 and REQ-TSD-07 (ContractFake-only) forbid. Every failure REJECTS the returned
|
|
11
|
+
// promise with a descriptive Error (never a sync throw) — planted violations (REQ-DC-05)
|
|
12
|
+
// surface through an expect-reject wrapper at the call site.
|
|
13
|
+
//
|
|
14
|
+
// Drives real runs via `defineFactory` + the kit-internal `run-vehicle.ts` transport —
|
|
15
|
+
// imported for INTERNAL use only, never re-exported (FIT-08 bans re-export of kit symbols
|
|
16
|
+
// from this path, not internal use of them).
|
|
17
|
+
import { defineFactory } from "../core/context.js";
|
|
18
|
+
import { deepEqual } from "../core/deep-equal.js";
|
|
19
|
+
import { createRunVehicle } from "./run-vehicle.js";
|
|
20
|
+
// REQ-DC-04 (seam-serializability): `JSON.parse(JSON.stringify(directive))` must deep-equal
|
|
21
|
+
// `directive` for EVERY emitted directive. Two DISTINCT failure modes a planted `.modify()` can
|
|
22
|
+
// trigger (REQ-DC-04.1/.2): a closure attached to directive content is silently DROPPED by
|
|
23
|
+
// JSON.stringify (caught by the deepEqual mismatch below); a live AST node with circular
|
|
24
|
+
// parent pointers makes JSON.stringify itself THROW (caught by the try/catch below).
|
|
25
|
+
function assertSerializable(directives, label) {
|
|
26
|
+
for (const directive of directives) {
|
|
27
|
+
let roundTripped;
|
|
28
|
+
try {
|
|
29
|
+
roundTripped = JSON.parse(JSON.stringify(directive));
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
throw new Error(`testOpPack: REQ-DC-04 seam-serializability (${label}) — a directive failed JSON.stringify, ` +
|
|
33
|
+
`likely a live AST node with circular references attached to directive content: ` +
|
|
34
|
+
`${err instanceof Error ? err.message : String(err)}`);
|
|
35
|
+
}
|
|
36
|
+
if (!deepEqual(directive, roundTripped)) {
|
|
37
|
+
throw new Error(`testOpPack: REQ-DC-04 seam-serializability (${label}) — JSON.parse(JSON.stringify(directive)) ` +
|
|
38
|
+
`did not deep-equal the original directive; a non-serializable value (e.g. a closure) was ` +
|
|
39
|
+
`silently dropped`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// Runs ONE exercise through a fresh, isolated run-vehicle (§4.4c): seeds `path` with `seed`,
|
|
44
|
+
// chains the exercise's ops on `baseDialect.find(path)` (`{op,args}` -> `handle[op](...args)`;
|
|
45
|
+
// `{modify}` -> `handle.modify(fn)`), optionally injects `handle.read()` right after the FIRST op
|
|
46
|
+
// (the read-boundary-split re-run), awaits, and returns every modify directive observed
|
|
47
|
+
// across the whole run, in emission order.
|
|
48
|
+
async function runExercise(dialect, exercise, opts) {
|
|
49
|
+
const path = exercise.path ?? `op-exercise${dialect.extensions[0] ?? ""}`;
|
|
50
|
+
const { client, emitted } = createRunVehicle({ [path]: exercise.seed });
|
|
51
|
+
const runner = defineFactory(async () => {
|
|
52
|
+
// `any`: fixtures span arbitrary op-packs, so the exercised handle's op-derived members
|
|
53
|
+
// are not known at this generic boundary (mirrors OpPackFixture's own `unknown`-at-the-
|
|
54
|
+
// boundary posture, design §4.3).
|
|
55
|
+
let handle = dialect.find(path);
|
|
56
|
+
for (const [index, step] of exercise.chain.entries()) {
|
|
57
|
+
handle = "modify" in step ? handle.modify(step.modify) : handle[step.op](...step.args);
|
|
58
|
+
if (opts.injectReadAfterFirstOp && index === 0) {
|
|
59
|
+
await handle.read();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
await handle;
|
|
63
|
+
});
|
|
64
|
+
await runner(undefined, { client });
|
|
65
|
+
const directives = emitted.flatMap((b) => b.instructions);
|
|
66
|
+
const modifies = directives
|
|
67
|
+
.filter((d) => d.op === "modify")
|
|
68
|
+
.map((d) => d.modify);
|
|
69
|
+
return { modifies, directives };
|
|
70
|
+
}
|
|
71
|
+
// REQ-DC-06 (ADR-0012 amendment clause 1): six mandatory adversarial samples, injected
|
|
72
|
+
// ADDITIVELY into every `testDialect` run — `DialectFixture` carries no field capable of
|
|
73
|
+
// disabling this (compile-level guarantee, REQ-DC-06.2, see test/conformance/planted/
|
|
74
|
+
// opt-out-attempt.test.ts). Each sample is independently verified to round-trip byte-exact
|
|
75
|
+
// against the real TypeScript dialect (`ast.ts`'s parse/print pair) — a dialect that fails one
|
|
76
|
+
// is a real conformance failure, not a fixture artefact.
|
|
77
|
+
const MANDATORY_ADVERSARIAL_SAMPLES = [
|
|
78
|
+
"", // empty file
|
|
79
|
+
"// just a comment, no statements\n", // comment-only file
|
|
80
|
+
`/*${"a".repeat(4 * 1024 * 1024)}*/\nconst x = 1;\n`, // 4 MiB serialized-boundary sample (REQ-TSD-03.7 precedent)
|
|
81
|
+
"const x = 1;\r\nconst y = 2;\r\n", // CRLF
|
|
82
|
+
"const x = 1;\n", // UTF-8 BOM
|
|
83
|
+
'import { a } from "m";\nimport { b } from "m";\n', // duplicate-target: two import statements sharing one module specifier
|
|
84
|
+
];
|
|
85
|
+
// Shared by `testDialect` and `testOpPack` (verify-in-loop-4 Finding #1): the round-trip +
|
|
86
|
+
// real-base-probe loop is ONE mechanism, run against whichever dialect reference the caller
|
|
87
|
+
// holds (`fixture.dialect` for testDialect, `fixture.baseDialect` for testOpPack) — REQ-DC-06's
|
|
88
|
+
// chapeau ("into EVERY testDialect/testOpPack run") and REQ-DC-08 (real-base-dialect rule) both
|
|
89
|
+
// name a dialect's `ast.parse`/`ast.print` pair, not an op-chain, so no per-op synthesis is
|
|
90
|
+
// needed to satisfy either for `testOpPack`. `label` scopes error messages to the caller.
|
|
91
|
+
function runRoundTripProbe(dialectAst, samples, label) {
|
|
92
|
+
for (const sample of samples) {
|
|
93
|
+
const ast = dialectAst.parse(sample);
|
|
94
|
+
// REQ-DC-08 (real-base-dialect rule): a stub/mock parse that returns nothing or hands
|
|
95
|
+
// back the input string unchanged would let an identity round-trip pass vacuously —
|
|
96
|
+
// reject before the round-trip assertion can even run.
|
|
97
|
+
if (ast === null || ast === undefined || ast === sample) {
|
|
98
|
+
throw new Error(`${label}: REQ-DC-08 real-base-dialect rule — dialect.ast.parse(sample) returned a ` +
|
|
99
|
+
"nullish value or the input string unchanged; a real dialect's parse must produce a distinct AST");
|
|
100
|
+
}
|
|
101
|
+
const roundTripped = dialectAst.print(ast);
|
|
102
|
+
if (roundTripped !== sample) {
|
|
103
|
+
const preview = sample.length > 80 ? `${sample.slice(0, 80)}...` : sample;
|
|
104
|
+
throw new Error(`${label}: REQ-DC-01 round-trip fidelity — print(parse(sample)) did not byte-exact match ` +
|
|
105
|
+
`the original sample: ${JSON.stringify(preview)}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Runs the conformance suite for a dialect: asserts parse/print no-op round-trip fidelity
|
|
111
|
+
* (REQ-DC-01) — `print(parse(sample)) === sample` BYTE-EXACT, never AST-equal, for every
|
|
112
|
+
* sample in `fixture.samples` PLUS the six mandatory adversarial samples the kit injects on
|
|
113
|
+
* every run (REQ-DC-06 — additive, not opt-out-able). Before each round-trip check, verifies
|
|
114
|
+
* `fixture.dialect.ast.parse` is a REAL parser, not an identity/stub function (REQ-DC-08): a
|
|
115
|
+
* `parse` returning `null`/`undefined`, or the input string unchanged, fails BEFORE the
|
|
116
|
+
* round-trip assertion could vacuously pass.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* await testDialect({
|
|
120
|
+
* dialect: myTypeScriptDialect,
|
|
121
|
+
* samples: ["const x = 1;"],
|
|
122
|
+
* });
|
|
123
|
+
*/
|
|
124
|
+
export async function testDialect(fixture) {
|
|
125
|
+
const samples = [...fixture.samples, ...MANDATORY_ADVERSARIAL_SAMPLES];
|
|
126
|
+
runRoundTripProbe(fixture.dialect.ast, samples, "testDialect");
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Runs the conformance suite for an op-pack against a REAL base dialect (never a mock,
|
|
130
|
+
* ADR-0012): for each of `fixture.exercises`, asserts single-op fidelity +
|
|
131
|
+
* unchanged-elsewhere (REQ-DC-02) / coalescing-to-one content-verified (REQ-DC-03) via a
|
|
132
|
+
* no-read run, seam-serializability (REQ-DC-04) over every emitted directive, and — for
|
|
133
|
+
* every multi-op exercise — the read-boundary split (REQ-DC-05.2's live counterpart): a
|
|
134
|
+
* mid-chain `read()` must force exactly TWO cumulative modify directives, never one. Finally,
|
|
135
|
+
* asserts `fixture.baseDialect` survives the six mandatory adversarial samples' round-trip +
|
|
136
|
+
* real-base probe (REQ-DC-06/REQ-DC-08 — additive, not opt-out-able, same mechanism
|
|
137
|
+
* `testDialect` runs), unconditionally on every call whose exercises pass.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* await testOpPack({
|
|
141
|
+
* opPack: myInsertImportOps,
|
|
142
|
+
* baseDialect: myTypeScriptDialect,
|
|
143
|
+
* exercises: [
|
|
144
|
+
* {
|
|
145
|
+
* seed: "export const x = 1;",
|
|
146
|
+
* chain: [{ op: "addImport", args: ["readFileSync", "node:fs"] }],
|
|
147
|
+
* expect: 'import { readFileSync } from "node:fs";\nexport const x = 1;',
|
|
148
|
+
* },
|
|
149
|
+
* ],
|
|
150
|
+
* });
|
|
151
|
+
*/
|
|
152
|
+
export async function testOpPack(fixture) {
|
|
153
|
+
if (fixture.exercises.length === 0) {
|
|
154
|
+
throw new Error("testOpPack: fixture.exercises must include at least one exercise — REQ-DC-02..04 are untestable without one");
|
|
155
|
+
}
|
|
156
|
+
const hasMultiOpExercise = fixture.exercises.some((exercise) => exercise.chain.length >= 2);
|
|
157
|
+
if (!hasMultiOpExercise) {
|
|
158
|
+
throw new Error("testOpPack: fixture.exercises must include at least one multi-op exercise (>=2 ops) — " +
|
|
159
|
+
"REQ-DC-03 (coalescing-to-one, >=2 distinguishable ops) is mandatory");
|
|
160
|
+
}
|
|
161
|
+
for (const exercise of fixture.exercises) {
|
|
162
|
+
// REQ-DC-02 (single-op exercises) / REQ-DC-03 (multi-op exercises): a no-read run must
|
|
163
|
+
// coalesce to EXACTLY ONE modify for `path`, byte-exact `expect` — a FULL-output
|
|
164
|
+
// comparison proves BOTH the op's intended effect AND that every other region stayed
|
|
165
|
+
// byte-stable (never a `contains` substring, Theatre Criteria).
|
|
166
|
+
const direct = await runExercise(fixture.baseDialect, exercise, { injectReadAfterFirstOp: false });
|
|
167
|
+
// Serializability checked FIRST (REQ-DC-04) — a directive whose content isn't even a
|
|
168
|
+
// proper serializable string makes a byte-exact comparison moot, and ordering it first
|
|
169
|
+
// proves DC-04's own assertion is what fails a closure-/live-node-smuggle fixture,
|
|
170
|
+
// rather than an incidental content mismatch masking it.
|
|
171
|
+
assertSerializable(direct.directives, "no-read exercise");
|
|
172
|
+
if (direct.modifies.length !== 1) {
|
|
173
|
+
throw new Error(`testOpPack: REQ-DC-02/03 — expected exactly ONE modify directive for a no-read chain, got ${direct.modifies.length}`);
|
|
174
|
+
}
|
|
175
|
+
if (direct.modifies[0]?.content !== exercise.expect) {
|
|
176
|
+
throw new Error("testOpPack: REQ-DC-02/03 — the coalesced modify's content did not byte-exact match the fixture's expect value");
|
|
177
|
+
}
|
|
178
|
+
if (exercise.chain.length >= 2) {
|
|
179
|
+
// Read-boundary split (fifth core assertion, self-derived — the live counterpart of
|
|
180
|
+
// the planted REQ-DC-05.2). Re-run injecting `handle.read()` right after the FIRST
|
|
181
|
+
// op: the GLOBAL flush-before-read (ADR-0015) forces exactly TWO modify directives
|
|
182
|
+
// whose cumulative content equals `expect`; directive #1's expected content is
|
|
183
|
+
// SELF-DERIVED by running the first op ALONE — no extra fixture field.
|
|
184
|
+
const firstOpOnly = {
|
|
185
|
+
seed: exercise.seed,
|
|
186
|
+
path: exercise.path,
|
|
187
|
+
chain: exercise.chain.slice(0, 1),
|
|
188
|
+
};
|
|
189
|
+
const firstOnly = await runExercise(fixture.baseDialect, firstOpOnly, { injectReadAfterFirstOp: false });
|
|
190
|
+
const expectedFirst = firstOnly.modifies[0]?.content;
|
|
191
|
+
if (expectedFirst === undefined) {
|
|
192
|
+
throw new Error("testOpPack: REQ-DC-05.2 — could not self-derive directive #1's expected content (the first op alone produced no modify)");
|
|
193
|
+
}
|
|
194
|
+
const split = await runExercise(fixture.baseDialect, exercise, { injectReadAfterFirstOp: true });
|
|
195
|
+
assertSerializable(split.directives, "read-split exercise");
|
|
196
|
+
if (split.modifies.length !== 2) {
|
|
197
|
+
throw new Error(`testOpPack: REQ-DC-05.2 read-boundary split — expected exactly TWO modify directives after a mid-chain read, got ${split.modifies.length}`);
|
|
198
|
+
}
|
|
199
|
+
if (split.modifies[0]?.content !== expectedFirst) {
|
|
200
|
+
throw new Error("testOpPack: REQ-DC-05.2 — the first split directive's content did not match the self-derived first-op-alone content");
|
|
201
|
+
}
|
|
202
|
+
if (split.modifies[1]?.content !== exercise.expect) {
|
|
203
|
+
throw new Error("testOpPack: REQ-DC-05.2 — the cumulative (second) split directive's content did not byte-exact match the fixture's expect value");
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// REQ-DC-06/REQ-DC-08 (verify-in-loop-4 Finding #1): the six mandatory adversarial
|
|
208
|
+
// samples' round-trip + real-base probe, run against `fixture.baseDialect` — same
|
|
209
|
+
// mechanism `testDialect` runs, independent of `fixture.exercises`. Deliberately LAST
|
|
210
|
+
// (not first, unlike testDialect): a fixture's own exercises target ONE specific
|
|
211
|
+
// REQ-DC-02..05 failure mode against a baseDialect that may be intentionally non-generic
|
|
212
|
+
// (e.g. this repo's own REQ-DC-05 planted-violation fixtures use hand-rolled/identity
|
|
213
|
+
// `ast` pairs to isolate that ONE mode) — ordering the probe last lets that exercise-level
|
|
214
|
+
// failure surface for its own reason first, while still running the probe unconditionally
|
|
215
|
+
// on every call whose exercises pass, exactly as REQ-DC-06's "regardless of what samples
|
|
216
|
+
// the contributor's own fixture supplies" demands.
|
|
217
|
+
runRoundTripProbe(fixture.baseDialect.ast, MANDATORY_ADVERSARIAL_SAMPLES, "testOpPack");
|
|
218
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Batch } from "../core/wire.ts";
|
|
2
|
+
/** Local structural mirror of the transport port — never names the port type (see header). */
|
|
3
|
+
interface RunVehicleClient {
|
|
4
|
+
emit(batch: Batch): Promise<void>;
|
|
5
|
+
read(path: string): Promise<string | undefined>;
|
|
6
|
+
commit(): Promise<void>;
|
|
7
|
+
discard(): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Builds a fresh, single-run in-memory transport seeded with the paths an `OpExercise` needs.
|
|
11
|
+
* Every exercise runs its OWN isolated vehicle instance (§4.4c) — no shared state across
|
|
12
|
+
* exercises. `emitted` collects every batch this vehicle's `emit` observed, in order, for the
|
|
13
|
+
* kit's own directive-content assertions.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createRunVehicle(seed: Record<string, string>): {
|
|
16
|
+
client: RunVehicleClient;
|
|
17
|
+
emitted: Batch[];
|
|
18
|
+
};
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// S-004 — the conformance kit's run vehicle (ADR-0012 amendment, "Run vehicle", rev 4).
|
|
2
|
+
//
|
|
3
|
+
// `testDialect`/`testOpPack`'s DC-02..05 assertions need to observe an EMITTED, coalesced
|
|
4
|
+
// batch — which requires driving a REAL run through `defineFactory` (ADR-0012 forbids a
|
|
5
|
+
// mock/bypass of the emit seam). This module is a MINIMAL, kit-internal, in-memory transport
|
|
6
|
+
// implementing only the semantics those assertions need: stage a batch, apply it to an
|
|
7
|
+
// in-memory tree, read it back, commit/discard. It is NOT exported from `./conformance`
|
|
8
|
+
// (internal port use only — re-export is not, FIT-08). It does NOT replace `test/`'s
|
|
9
|
+
// `ContractFake`: that stays the normative, full-fidelity fake for the SDK's own test suite;
|
|
10
|
+
// this is a deliberately thin, SEPARATE implementation whose own fidelity is pinned by the
|
|
11
|
+
// kit's assertions running the REAL dialect through it (a divergence would fail DC-01..05,
|
|
12
|
+
// not hide).
|
|
13
|
+
//
|
|
14
|
+
// Deliberately does not name the SDK's transport-port TYPE anywhere in this file, in
|
|
15
|
+
// comments included (REQ-FIT-09 / fit-10's structural guard scans ALL text, not just code —
|
|
16
|
+
// its allow-list is exactly one path, and this is not it). The port shape is satisfied via a
|
|
17
|
+
// LOCAL structural type instead — the same sanctioned pattern `src/testing/index.ts`'s facade
|
|
18
|
+
// uses for its own spy-wrapping.
|
|
19
|
+
function applyDirective(tree, directive) {
|
|
20
|
+
if (directive.op === "create") {
|
|
21
|
+
tree.set(directive.create.pathTemplate, directive.create.template);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (directive.op === "modify") {
|
|
25
|
+
tree.set(directive.modify.path, directive.modify.content);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (directive.op === "delete") {
|
|
29
|
+
tree.delete(directive.delete.path);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (directive.op === "rename") {
|
|
33
|
+
const content = tree.get(directive.rename.path);
|
|
34
|
+
if (content !== undefined) {
|
|
35
|
+
tree.delete(directive.rename.path);
|
|
36
|
+
tree.set(directive.rename.newName, content);
|
|
37
|
+
}
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (directive.op === "move") {
|
|
41
|
+
const content = tree.get(directive.move.path);
|
|
42
|
+
if (content !== undefined) {
|
|
43
|
+
tree.delete(directive.move.path);
|
|
44
|
+
const base = directive.move.path.split("/").pop() ?? directive.move.path;
|
|
45
|
+
tree.set(`${directive.move.toDir}/${base}`, content);
|
|
46
|
+
}
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (directive.op === "copy") {
|
|
50
|
+
const content = tree.get(directive.copy.from);
|
|
51
|
+
if (content !== undefined) {
|
|
52
|
+
tree.set(directive.copy.to, content);
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
// S-003 (ATH-16): the ONLY op this vehicle adds collision machinery for — every other
|
|
57
|
+
// branch above applies silently (header note). `copyIn`'s source is package-local, not
|
|
58
|
+
// a tree entry (ADR-0045: SDK-side containment is the legitimate source-existence
|
|
59
|
+
// check, this vehicle never re-checks it) — collision-only, and NEVER writes `to` into
|
|
60
|
+
// `tree` on success (emit-only, REQ-ATH-15.1/BRC-04 — no simulation surface ever
|
|
61
|
+
// materializes by-reference bytes).
|
|
62
|
+
if (directive.op === "copyIn") {
|
|
63
|
+
const { to, force } = directive.copyIn;
|
|
64
|
+
if (tree.has(to) && !force) {
|
|
65
|
+
throw new Error(`copyIn collision — destination "${to}" already exists (use force to overwrite)`);
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// Exhaustiveness guard (final-verify remediation): an 8th wire op would otherwise
|
|
70
|
+
// compile through this if-chain and silently no-op — mirrors the `_exhaustive: never`
|
|
71
|
+
// idiom `authoring-error.ts`'s `messageFor`/`originFor` switches already use.
|
|
72
|
+
const _exhaustive = directive;
|
|
73
|
+
throw new Error(`unhandled directive op: ${_exhaustive.op}`);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Builds a fresh, single-run in-memory transport seeded with the paths an `OpExercise` needs.
|
|
77
|
+
* Every exercise runs its OWN isolated vehicle instance (§4.4c) — no shared state across
|
|
78
|
+
* exercises. `emitted` collects every batch this vehicle's `emit` observed, in order, for the
|
|
79
|
+
* kit's own directive-content assertions.
|
|
80
|
+
*/
|
|
81
|
+
export function createRunVehicle(seed) {
|
|
82
|
+
const tree = new Map(Object.entries(seed));
|
|
83
|
+
const emitted = [];
|
|
84
|
+
const client = {
|
|
85
|
+
async emit(batch) {
|
|
86
|
+
emitted.push(batch);
|
|
87
|
+
for (const directive of batch.instructions) {
|
|
88
|
+
applyDirective(tree, directive);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
async read(path) {
|
|
92
|
+
return tree.get(path);
|
|
93
|
+
},
|
|
94
|
+
async commit() {
|
|
95
|
+
// Nothing to promote — this vehicle applies eagerly on emit (no staging/committed
|
|
96
|
+
// distinction; each exercise is a fresh, single-purpose, single-run instance whose
|
|
97
|
+
// only consumer is the kit's own `emitted` inspection, never a "final tree" read).
|
|
98
|
+
},
|
|
99
|
+
async discard() {
|
|
100
|
+
tree.clear();
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
return { client, emitted };
|
|
104
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { Batch } from "./wire.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The author-vocabulary verb of the operation whose rejection produced this
|
|
4
|
+
* `AuthoringError` — `"remove"`, never the wire op `"delete"` (REQ-10.2).
|
|
5
|
+
* `undefined` for batch-level rejections (`unrepresentable-content`,
|
|
6
|
+
* `changes-too-large`, `unknown`), which have no single offending directive.
|
|
7
|
+
*
|
|
8
|
+
* `schematic-local-files` S-003 (A1): extended from six to seven — `copyIn` added,
|
|
9
|
+
* forced by the `Directive["op"]` union gaining the `copyIn` wire op (ADR-0043); a
|
|
10
|
+
* `copyIn` collision attributes `verb: "copyIn"` (the author never called `copy`).
|
|
11
|
+
*
|
|
12
|
+
* `author-write-surface` S-004 (REQ-AEC-13): `"modify"` is the WIRE-altitude label for
|
|
13
|
+
* the `{op:"modify"}` directive — it is shared by BOTH `.replaceContent()` (the
|
|
14
|
+
* commons/dialect wholesale-replace call) and a dialect handle's `.modify(fn)` AST escape
|
|
15
|
+
* hatch, because both lower to the same wire directive. This member is DELIBERATELY never
|
|
16
|
+
* renamed to `"replaceContent"` — it labels the wire mutation, not either author-facing
|
|
17
|
+
* call name.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* if (err instanceof AuthoringError && err.verb !== undefined) {
|
|
21
|
+
* console.error(`${err.verb} was rejected at ${err.path}`);
|
|
22
|
+
* }
|
|
23
|
+
*/
|
|
24
|
+
export type AuthoringVerb = "create" | "modify" | "remove" | "rename" | "move" | "copy" | "copyIn";
|
|
25
|
+
/**
|
|
26
|
+
* The closed, author-vocabulary cause of an `AuthoringError` (★D2, ADR-0020). Exactly
|
|
27
|
+
* twelve values — adding a thirteenth is a MAJOR change: authors are expected to write
|
|
28
|
+
* exhaustive `switch(reason)` blocks, and TypeScript's exhaustiveness check breaks such
|
|
29
|
+
* a switch on a new member even though nothing breaks at runtime. (V2 → V3 amendment,
|
|
30
|
+
* 2026-07-10, coordinated with `stage-4-typed-options`: extended from six to eight —
|
|
31
|
+
* `invalid-input` and `reserved-name` added, REQ-AEC-07/08. `schematic-local-files`
|
|
32
|
+
* S-002, REQ-AEC-10: extended from eight to twelve — `source-not-found`,
|
|
33
|
+
* `source-outside-package`, `source-not-regular-file`, `source-unreadable` added, all
|
|
34
|
+
* four covering the SDK's own pre-emit read/stat of a package-local source for
|
|
35
|
+
* `scaffold`/`copyIn`/`create({templateFile})`.)
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* switch (err.reason) {
|
|
39
|
+
* case "path-collision":
|
|
40
|
+
* console.error(`${err.verb} collided at ${err.path}`);
|
|
41
|
+
* break;
|
|
42
|
+
* case "path-not-found":
|
|
43
|
+
* case "unrepresentable-content":
|
|
44
|
+
* case "changes-too-large":
|
|
45
|
+
* case "outside-run":
|
|
46
|
+
* case "unknown":
|
|
47
|
+
* case "invalid-input":
|
|
48
|
+
* case "reserved-name":
|
|
49
|
+
* case "source-not-found":
|
|
50
|
+
* case "source-outside-package":
|
|
51
|
+
* case "source-not-regular-file":
|
|
52
|
+
* case "source-unreadable":
|
|
53
|
+
* console.error(err.message);
|
|
54
|
+
* break;
|
|
55
|
+
* }
|
|
56
|
+
*/
|
|
57
|
+
export type AuthoringReason = "path-collision" | "path-not-found" | "unrepresentable-content" | "changes-too-large" | "outside-run" | "unknown" | "invalid-input" | "reserved-name" | "source-not-found" | "source-outside-package" | "source-not-regular-file" | "source-unreadable";
|
|
58
|
+
/**
|
|
59
|
+
* Distinguishes an engine-refused write from an SDK-side misuse (2.4, ADR-0021) —
|
|
60
|
+
* DERIVED from `reason`, never producer-set.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* if (err.origin === "write-rejected") console.error("the engine refused the write");
|
|
64
|
+
* else console.error("the schematic itself was used incorrectly");
|
|
65
|
+
*/
|
|
66
|
+
export type AuthoringOrigin = "write-rejected" | "authoring-rejected";
|
|
67
|
+
/**
|
|
68
|
+
* Thrown when an author's schematic run is rejected — by the engine (a refused write)
|
|
69
|
+
* or by the SDK itself (a misuse detected before any engine round-trip). Catch it,
|
|
70
|
+
* `switch` on `.reason`, and recover.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* import { AuthoringError } from "@pbuilder/sdk/commons";
|
|
74
|
+
*
|
|
75
|
+
* try {
|
|
76
|
+
* await run(options, { client });
|
|
77
|
+
* } catch (err) {
|
|
78
|
+
* if (err instanceof AuthoringError) {
|
|
79
|
+
* switch (err.reason) {
|
|
80
|
+
* case "path-collision":
|
|
81
|
+
* console.error(`${err.verb} collided at ${err.path}`);
|
|
82
|
+
* break;
|
|
83
|
+
* default:
|
|
84
|
+
* console.error(err.message);
|
|
85
|
+
* }
|
|
86
|
+
* }
|
|
87
|
+
* }
|
|
88
|
+
*/
|
|
89
|
+
export declare class AuthoringError extends Error {
|
|
90
|
+
readonly verb: AuthoringVerb | undefined;
|
|
91
|
+
readonly path: string | undefined;
|
|
92
|
+
readonly reason: AuthoringReason;
|
|
93
|
+
readonly origin: AuthoringOrigin;
|
|
94
|
+
/**
|
|
95
|
+
* Counts directives applied within the failing flush before the offender — a
|
|
96
|
+
* diagnostic for locating progress, NOT a persistence promise (a rejected run
|
|
97
|
+
* discards everything, ADR-0015).
|
|
98
|
+
*/
|
|
99
|
+
readonly appliedCount: number;
|
|
100
|
+
constructor(fields: {
|
|
101
|
+
verb: AuthoringVerb | undefined;
|
|
102
|
+
path: string | undefined;
|
|
103
|
+
reason: AuthoringReason;
|
|
104
|
+
appliedCount: number;
|
|
105
|
+
/**
|
|
106
|
+
* Overrides the derived message. REQUIRED for `reason: "invalid-input"` /
|
|
107
|
+
* `"reserved-name"` (REQ-AEC-09) — their templates interpolate caller-known data
|
|
108
|
+
* (`{field}`, `{expectedType}`, `{name}`) `messageFor` cannot derive from `verb`/`path`
|
|
109
|
+
* alone. Optional for every other reason, which derive their message from `reason` +
|
|
110
|
+
* `verb` + `path` as before.
|
|
111
|
+
*/
|
|
112
|
+
message?: string;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
export declare function invalidInput(message: string): AuthoringError;
|
|
116
|
+
export declare function toAuthoringError(raw: unknown, batch: Batch): AuthoringError;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// SEAM-04 (ADR-02, ADR-0020, ADR-0021, ADR-0022, ADR-0023): author-vocabulary error
|
|
2
|
+
// attribution. `toAuthoringError` translates a port-internal `EmitRejection` (or any
|
|
3
|
+
// other rejection value) into the author's own vocabulary: a closed `reason` (★D2), a
|
|
4
|
+
// derived `origin` (2.4), the actual offending directive's verb + primary path, and the
|
|
5
|
+
// applied-boundary count. Classification reads `EmitRejection.code` ONLY — never message
|
|
6
|
+
// text (REQ-ERM-01 ban) — and a non-EmitRejection rejection degrades to `reason:"unknown"`
|
|
7
|
+
// without crashing (REQ-ERM-03). The message is a FRESH string built from the closed
|
|
8
|
+
// reason/verb/path; the raw engine text is discarded and never chained via `.cause`, so
|
|
9
|
+
// the no-engine-text guarantee (REQ-AEC-05, FIT-11) holds for the WHOLE error object, not
|
|
10
|
+
// only `.message`.
|
|
11
|
+
import { EmitRejection } from "./emit-rejection.js";
|
|
12
|
+
// ADR-0021: origin is DERIVED from reason via an exhaustive switch with a `never`
|
|
13
|
+
// default arm — adding a 13th reason breaks the BUILD here, forcing a deliberate origin
|
|
14
|
+
// assignment instead of an accidental default (mirrored by the compile-time pin in
|
|
15
|
+
// test/types/authoring-reason.test.ts). `unknown` maps to "write-rejected" deliberately:
|
|
16
|
+
// an unclassifiable rejection necessarily arrived through the emit/write seam (the only
|
|
17
|
+
// place toAuthoringError runs), so the write side is the honest attribution (spec
|
|
18
|
+
// cross-cutting note 7). `invalid-input`/`reserved-name` (V2 → V3 amendment, REQ-AEC-07/08)
|
|
19
|
+
// are ALWAYS "authoring-rejected" — same rationale as outside-run: an SDK-side misuse
|
|
20
|
+
// detection, not an engine round-trip refusal. The four `source-*` reasons
|
|
21
|
+
// (`schematic-local-files` S-002, REQ-AEC-10) are likewise ALWAYS "authoring-rejected" —
|
|
22
|
+
// every one is detected by the SDK's OWN pre-emit read/stat of a package-local source,
|
|
23
|
+
// never an engine round-trip refusal.
|
|
24
|
+
function originFor(reason) {
|
|
25
|
+
switch (reason) {
|
|
26
|
+
case "path-collision":
|
|
27
|
+
case "path-not-found":
|
|
28
|
+
case "unrepresentable-content":
|
|
29
|
+
case "changes-too-large":
|
|
30
|
+
case "unknown":
|
|
31
|
+
return "write-rejected";
|
|
32
|
+
case "outside-run":
|
|
33
|
+
case "invalid-input":
|
|
34
|
+
case "reserved-name":
|
|
35
|
+
case "source-not-found":
|
|
36
|
+
case "source-outside-package":
|
|
37
|
+
case "source-not-regular-file":
|
|
38
|
+
case "source-unreadable":
|
|
39
|
+
return "authoring-rejected";
|
|
40
|
+
default: {
|
|
41
|
+
const _exhaustive = reason;
|
|
42
|
+
throw new Error(`originFor: unhandled reason ${String(_exhaustive)}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// Total mapping (emit-rejection-metadata REQ-ERM-01): every EmitRejectionCode maps to
|
|
47
|
+
// exactly one AuthoringReason. Classification reads `code` only, never message text.
|
|
48
|
+
const CODE_TO_REASON = {
|
|
49
|
+
collision: "path-collision",
|
|
50
|
+
"not-found": "path-not-found",
|
|
51
|
+
unrepresentable: "unrepresentable-content",
|
|
52
|
+
cap: "changes-too-large",
|
|
53
|
+
unknown: "unknown",
|
|
54
|
+
};
|
|
55
|
+
// wire op → author verb (REQ-10.2): "delete" is the WIRE name; the author vocabulary
|
|
56
|
+
// word is "remove". `remove` never rejects in practice (REQ-16 non-site) — this mapping
|
|
57
|
+
// is a translation-layer contract, proven at the unit level in authoring-error.test.ts.
|
|
58
|
+
function verbFor(op) {
|
|
59
|
+
return op === "delete" ? "remove" : op;
|
|
60
|
+
}
|
|
61
|
+
// primaryPath (design §4.3, frozen table): the AUTHOR-DECLARED SOURCE-SIDE path for
|
|
62
|
+
// every verb, for BOTH failure forms (collision AND source-not-found) — never the
|
|
63
|
+
// computed destination. `path` is a locator ("which of my calls failed"); `reason`
|
|
64
|
+
// carries the why.
|
|
65
|
+
function primaryPath(directive) {
|
|
66
|
+
switch (directive.op) {
|
|
67
|
+
case "create":
|
|
68
|
+
return directive.create.pathTemplate;
|
|
69
|
+
case "modify":
|
|
70
|
+
return directive.modify.path;
|
|
71
|
+
case "delete":
|
|
72
|
+
return directive.delete.path;
|
|
73
|
+
case "rename":
|
|
74
|
+
return directive.rename.path;
|
|
75
|
+
case "move":
|
|
76
|
+
return directive.move.path;
|
|
77
|
+
case "copy":
|
|
78
|
+
return directive.copy.from;
|
|
79
|
+
case "copyIn":
|
|
80
|
+
return directive.copyIn.from;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// REQ-AEC-06: message templates selected BY REASON, three-way — never by verb/path
|
|
84
|
+
// presence. `outside-run` also has verb/path undefined but must NOT get the batch
|
|
85
|
+
// template (the context.test.ts:12 substring pin would break).
|
|
86
|
+
//
|
|
87
|
+
// REQ-AEC-09 (V2 → V3 amendment) adds two MORE families (invalid-input's two variants,
|
|
88
|
+
// reserved-name) whose templates interpolate a `{field}`/`{expectedType}`/`{name}` this
|
|
89
|
+
// function has no locator for — `verb`/`path` cannot carry them without corrupting their
|
|
90
|
+
// existing typed meaning. Those two reasons therefore have NO default template here: the
|
|
91
|
+
// caller (the AuthoringError constructor) MUST supply an explicit `message` for them —
|
|
92
|
+
// see the `default` arm below, which throws rather than silently returning a wrong string.
|
|
93
|
+
//
|
|
94
|
+
// REQ-AEC-11 (`schematic-local-files` S-002, V3 neutral wording — no `"copy failed:"`
|
|
95
|
+
// prefix, dropped because these reasons also fire for `scaffold`/`create({templateFile})`,
|
|
96
|
+
// not only the copy-family verbs) adds four MORE families, one per new `source-*` reason.
|
|
97
|
+
// Unlike invalid-input/reserved-name, these ARE derivable from `path` alone (always
|
|
98
|
+
// package-relative, REQ-PRC-05) — no caller-supplied `message` needed.
|
|
99
|
+
function messageFor(reason, verb, path) {
|
|
100
|
+
switch (reason) {
|
|
101
|
+
case "path-collision":
|
|
102
|
+
case "path-not-found":
|
|
103
|
+
return `${verb} failed at ${path}: ${reason}`;
|
|
104
|
+
case "outside-run":
|
|
105
|
+
return ("@pbuilder/sdk: authoring verbs can only be used while a schematic is running — " +
|
|
106
|
+
"call them inside your factory function, not at module load time.");
|
|
107
|
+
case "unrepresentable-content":
|
|
108
|
+
case "changes-too-large":
|
|
109
|
+
return `changes could not be applied: ${reason}`;
|
|
110
|
+
case "unknown":
|
|
111
|
+
return `changes could not be applied: ${reason} — the SDK could not classify this failure`;
|
|
112
|
+
case "source-not-found":
|
|
113
|
+
return `source file not found: ${path} does not exist in the package`;
|
|
114
|
+
case "source-outside-package":
|
|
115
|
+
return `source file outside package: ${path} resolves outside the package boundary`;
|
|
116
|
+
case "source-not-regular-file":
|
|
117
|
+
return `source file invalid: ${path} is not a regular file`;
|
|
118
|
+
case "source-unreadable":
|
|
119
|
+
return `source file unreadable: ${path} could not be read`;
|
|
120
|
+
case "invalid-input":
|
|
121
|
+
case "reserved-name":
|
|
122
|
+
throw new Error(`messageFor: reason "${reason}" has no default template (REQ-AEC-09) — construct AuthoringError with an explicit \`message\``);
|
|
123
|
+
// Parity with originFor: noImplicitReturns is off, so without this arm a 13th reason
|
|
124
|
+
// would fall through to an implicit `undefined` return instead of a build break.
|
|
125
|
+
default: {
|
|
126
|
+
const _exhaustive = reason;
|
|
127
|
+
throw new Error(`messageFor: unhandled reason ${String(_exhaustive)}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Thrown when an author's schematic run is rejected — by the engine (a refused write)
|
|
133
|
+
* or by the SDK itself (a misuse detected before any engine round-trip). Catch it,
|
|
134
|
+
* `switch` on `.reason`, and recover.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* import { AuthoringError } from "@pbuilder/sdk/commons";
|
|
138
|
+
*
|
|
139
|
+
* try {
|
|
140
|
+
* await run(options, { client });
|
|
141
|
+
* } catch (err) {
|
|
142
|
+
* if (err instanceof AuthoringError) {
|
|
143
|
+
* switch (err.reason) {
|
|
144
|
+
* case "path-collision":
|
|
145
|
+
* console.error(`${err.verb} collided at ${err.path}`);
|
|
146
|
+
* break;
|
|
147
|
+
* default:
|
|
148
|
+
* console.error(err.message);
|
|
149
|
+
* }
|
|
150
|
+
* }
|
|
151
|
+
* }
|
|
152
|
+
*/
|
|
153
|
+
export class AuthoringError extends Error {
|
|
154
|
+
verb;
|
|
155
|
+
path;
|
|
156
|
+
reason;
|
|
157
|
+
origin;
|
|
158
|
+
/**
|
|
159
|
+
* Counts directives applied within the failing flush before the offender — a
|
|
160
|
+
* diagnostic for locating progress, NOT a persistence promise (a rejected run
|
|
161
|
+
* discards everything, ADR-0015).
|
|
162
|
+
*/
|
|
163
|
+
appliedCount;
|
|
164
|
+
constructor(fields) {
|
|
165
|
+
super(fields.message ?? messageFor(fields.reason, fields.verb, fields.path));
|
|
166
|
+
this.name = "AuthoringError";
|
|
167
|
+
this.verb = fields.verb;
|
|
168
|
+
this.path = fields.path;
|
|
169
|
+
this.reason = fields.reason;
|
|
170
|
+
this.origin = originFor(fields.reason);
|
|
171
|
+
this.appliedCount = fields.appliedCount;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
// The one shared constructor-call shape for an SDK-side `invalid-input` misuse rejection
|
|
175
|
+
// (REQ-AEC-09 requires the caller-supplied message; every producer site passes the same
|
|
176
|
+
// verb/path/appliedCount constants). Message TEXT stays owned by each call site.
|
|
177
|
+
export function invalidInput(message) {
|
|
178
|
+
return new AuthoringError({ verb: undefined, path: undefined, reason: "invalid-input", appliedCount: 0, message });
|
|
179
|
+
}
|
|
180
|
+
// Translates a raw emit() rejection (ideally an EmitRejection, ADR-0022) plus the
|
|
181
|
+
// flushed batch into a public AuthoringError. Any rejection that does not carry the
|
|
182
|
+
// EmitRejection shape (string/number/undefined/a metadata-less Error) degrades to
|
|
183
|
+
// reason:"unknown" without crashing (REQ-ERM-03) — never reads message text to classify
|
|
184
|
+
// (REQ-ERM-01 ban).
|
|
185
|
+
export function toAuthoringError(raw, batch) {
|
|
186
|
+
if (raw instanceof EmitRejection) {
|
|
187
|
+
// REQ-ERM-01 totality: an absent/unrecognized code (a buggy or future engine client
|
|
188
|
+
// is not compile-checked at the throw site) degrades to "unknown" — the map lookup
|
|
189
|
+
// alone would yield undefined and crash originFor downstream.
|
|
190
|
+
const reason = CODE_TO_REASON[raw.code] ?? "unknown";
|
|
191
|
+
const offender = raw.failedIndex !== undefined ? batch.instructions[raw.failedIndex] : undefined;
|
|
192
|
+
return new AuthoringError({
|
|
193
|
+
verb: offender ? verbFor(offender.op) : undefined,
|
|
194
|
+
path: offender ? primaryPath(offender) : undefined,
|
|
195
|
+
reason,
|
|
196
|
+
appliedCount: raw.appliedCount,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
return new AuthoringError({
|
|
200
|
+
verb: undefined,
|
|
201
|
+
path: undefined,
|
|
202
|
+
reason: "unknown",
|
|
203
|
+
appliedCount: 0,
|
|
204
|
+
});
|
|
205
|
+
}
|