@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,192 @@
|
|
|
1
|
+
// ADR-0032: hand-rolled JSON position locator — replaces the dead V8-only
|
|
2
|
+
// `at position N` engine-message extraction (Bun/JavaScriptCore's JSON.parse never emits
|
|
3
|
+
// a byte offset). Pure, zero-dep, engine-independent: a minimal left-to-right JSON syntax
|
|
4
|
+
// scanner (not a value builder) that re-scans already-rejected text to pin the first grammar
|
|
5
|
+
// deviation. Runs ONLY in schema-parse.ts's catch branch — no hot path.
|
|
6
|
+
//
|
|
7
|
+
// Fidelity boundary (ADR-0032, deliberately bounded): strings are consumed STRUCTURALLY
|
|
8
|
+
// (a backslash escapes the next char, full stop) — no validation of escape internals. That
|
|
9
|
+
// means an in-string escape violation (bad `\u` hex, invalid escape char) scans as a
|
|
10
|
+
// well-formed string and the scanner can report the document "grammatically complete" even
|
|
11
|
+
// though native JSON.parse threw. That divergence is intentional: it is the one case this
|
|
12
|
+
// locator returns `undefined`, giving REQ-TFO-04.4's fallback branch a real fixture instead of
|
|
13
|
+
// pretending full JSON-grammar fidelity for no user-visible gain.
|
|
14
|
+
function ok(next) {
|
|
15
|
+
return { ok: true, next };
|
|
16
|
+
}
|
|
17
|
+
function fail(offset) {
|
|
18
|
+
return { ok: false, offset };
|
|
19
|
+
}
|
|
20
|
+
function isWhitespace(ch) {
|
|
21
|
+
return ch === " " || ch === "\t" || ch === "\n" || ch === "\r";
|
|
22
|
+
}
|
|
23
|
+
function isDigit(ch) {
|
|
24
|
+
return ch !== undefined && ch >= "0" && ch <= "9";
|
|
25
|
+
}
|
|
26
|
+
function skipWhitespace(raw, i) {
|
|
27
|
+
let pos = i;
|
|
28
|
+
while (pos < raw.length && isWhitespace(raw[pos]))
|
|
29
|
+
pos++;
|
|
30
|
+
return pos;
|
|
31
|
+
}
|
|
32
|
+
// Consumes a string starting at `raw[i] === '"'`. Structural only — see fidelity boundary note.
|
|
33
|
+
function scanString(raw, i) {
|
|
34
|
+
let pos = i + 1;
|
|
35
|
+
while (pos < raw.length) {
|
|
36
|
+
const ch = raw[pos];
|
|
37
|
+
if (ch === '"')
|
|
38
|
+
return ok(pos + 1);
|
|
39
|
+
if (ch === "\\") {
|
|
40
|
+
pos += 2;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
pos++;
|
|
44
|
+
}
|
|
45
|
+
return fail(pos);
|
|
46
|
+
}
|
|
47
|
+
// JSON number grammar: -?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?
|
|
48
|
+
function scanNumber(raw, i) {
|
|
49
|
+
let pos = i;
|
|
50
|
+
if (raw[pos] === "-")
|
|
51
|
+
pos++;
|
|
52
|
+
if (!isDigit(raw[pos]))
|
|
53
|
+
return fail(pos);
|
|
54
|
+
if (raw[pos] === "0") {
|
|
55
|
+
pos++;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
while (isDigit(raw[pos]))
|
|
59
|
+
pos++;
|
|
60
|
+
}
|
|
61
|
+
if (raw[pos] === ".") {
|
|
62
|
+
pos++;
|
|
63
|
+
if (!isDigit(raw[pos]))
|
|
64
|
+
return fail(pos);
|
|
65
|
+
while (isDigit(raw[pos]))
|
|
66
|
+
pos++;
|
|
67
|
+
}
|
|
68
|
+
if (raw[pos] === "e" || raw[pos] === "E") {
|
|
69
|
+
pos++;
|
|
70
|
+
if (raw[pos] === "+" || raw[pos] === "-")
|
|
71
|
+
pos++;
|
|
72
|
+
if (!isDigit(raw[pos]))
|
|
73
|
+
return fail(pos);
|
|
74
|
+
while (isDigit(raw[pos]))
|
|
75
|
+
pos++;
|
|
76
|
+
}
|
|
77
|
+
return ok(pos);
|
|
78
|
+
}
|
|
79
|
+
function scanObject(raw, i) {
|
|
80
|
+
let pos = skipWhitespace(raw, i + 1);
|
|
81
|
+
if (pos >= raw.length)
|
|
82
|
+
return fail(pos);
|
|
83
|
+
if (raw[pos] === "}")
|
|
84
|
+
return ok(pos + 1);
|
|
85
|
+
for (;;) {
|
|
86
|
+
pos = skipWhitespace(raw, pos);
|
|
87
|
+
if (pos >= raw.length)
|
|
88
|
+
return fail(pos);
|
|
89
|
+
if (raw[pos] !== '"')
|
|
90
|
+
return fail(pos); // unquoted key / non-string property name
|
|
91
|
+
const key = scanString(raw, pos);
|
|
92
|
+
if (!key.ok)
|
|
93
|
+
return key;
|
|
94
|
+
pos = skipWhitespace(raw, key.next);
|
|
95
|
+
if (pos >= raw.length)
|
|
96
|
+
return fail(pos);
|
|
97
|
+
if (raw[pos] !== ":")
|
|
98
|
+
return fail(pos);
|
|
99
|
+
const value = scanValue(raw, pos + 1);
|
|
100
|
+
if (!value.ok)
|
|
101
|
+
return value;
|
|
102
|
+
pos = skipWhitespace(raw, value.next);
|
|
103
|
+
if (pos >= raw.length)
|
|
104
|
+
return fail(pos);
|
|
105
|
+
if (raw[pos] === ",") {
|
|
106
|
+
pos = skipWhitespace(raw, pos + 1);
|
|
107
|
+
if (pos >= raw.length)
|
|
108
|
+
return fail(pos);
|
|
109
|
+
if (raw[pos] === "}")
|
|
110
|
+
return fail(pos); // trailing comma
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (raw[pos] === "}")
|
|
114
|
+
return ok(pos + 1);
|
|
115
|
+
return fail(pos); // expected ',' or '}'
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function scanArray(raw, i) {
|
|
119
|
+
let pos = skipWhitespace(raw, i + 1);
|
|
120
|
+
if (pos >= raw.length)
|
|
121
|
+
return fail(pos);
|
|
122
|
+
if (raw[pos] === "]")
|
|
123
|
+
return ok(pos + 1);
|
|
124
|
+
for (;;) {
|
|
125
|
+
const value = scanValue(raw, pos);
|
|
126
|
+
if (!value.ok)
|
|
127
|
+
return value;
|
|
128
|
+
pos = skipWhitespace(raw, value.next);
|
|
129
|
+
if (pos >= raw.length)
|
|
130
|
+
return fail(pos);
|
|
131
|
+
if (raw[pos] === ",") {
|
|
132
|
+
pos = skipWhitespace(raw, pos + 1);
|
|
133
|
+
if (pos >= raw.length)
|
|
134
|
+
return fail(pos);
|
|
135
|
+
if (raw[pos] === "]")
|
|
136
|
+
return fail(pos); // trailing comma
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (raw[pos] === "]")
|
|
140
|
+
return ok(pos + 1);
|
|
141
|
+
return fail(pos); // expected ',' or ']'
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function scanValue(raw, i0) {
|
|
145
|
+
const i = skipWhitespace(raw, i0);
|
|
146
|
+
if (i >= raw.length)
|
|
147
|
+
return fail(i); // premature EOF where a value was expected
|
|
148
|
+
const ch = raw[i];
|
|
149
|
+
if (ch === "{")
|
|
150
|
+
return scanObject(raw, i);
|
|
151
|
+
if (ch === "[")
|
|
152
|
+
return scanArray(raw, i);
|
|
153
|
+
if (ch === '"')
|
|
154
|
+
return scanString(raw, i);
|
|
155
|
+
if (ch === "-" || isDigit(ch))
|
|
156
|
+
return scanNumber(raw, i);
|
|
157
|
+
if (raw.startsWith("true", i))
|
|
158
|
+
return ok(i + 4);
|
|
159
|
+
if (raw.startsWith("false", i))
|
|
160
|
+
return ok(i + 5);
|
|
161
|
+
if (raw.startsWith("null", i))
|
|
162
|
+
return ok(i + 4);
|
|
163
|
+
return fail(i); // stray/garbage token, malformed keyword
|
|
164
|
+
}
|
|
165
|
+
function offsetToLineColumn(raw, offset) {
|
|
166
|
+
let line = 1;
|
|
167
|
+
let lastNewlineAt = -1;
|
|
168
|
+
for (let i = 0; i < offset && i < raw.length; i++) {
|
|
169
|
+
if (raw[i] === "\n") {
|
|
170
|
+
line++;
|
|
171
|
+
lastNewlineAt = i;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return { line, column: offset - lastNewlineAt };
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Re-scans `raw` (text already rejected by `JSON.parse`) to pin the 1-based `{line, column}`
|
|
178
|
+
* of the first JSON-grammar deviation. Returns `undefined` when the scanner completes without
|
|
179
|
+
* finding one — either an in-string escape violation (bounded fidelity, ADR-0032) or a
|
|
180
|
+
* defensive scanner/`JSON.parse` divergence. Pure; no I/O.
|
|
181
|
+
*/
|
|
182
|
+
export function locateFirstJsonSyntaxError(raw) {
|
|
183
|
+
const result = scanValue(raw, 0);
|
|
184
|
+
if (!result.ok) {
|
|
185
|
+
return offsetToLineColumn(raw, result.offset);
|
|
186
|
+
}
|
|
187
|
+
const trailing = skipWhitespace(raw, result.next);
|
|
188
|
+
if (trailing < raw.length) {
|
|
189
|
+
return offsetToLineColumn(raw, trailing);
|
|
190
|
+
}
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type SchemaKind = "string" | "number" | "boolean" | "enum";
|
|
2
|
+
export interface SchemaProperty {
|
|
3
|
+
key: string;
|
|
4
|
+
type: SchemaKind;
|
|
5
|
+
label?: string;
|
|
6
|
+
choices?: string[];
|
|
7
|
+
default?: unknown;
|
|
8
|
+
required?: boolean;
|
|
9
|
+
description?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface Schema {
|
|
12
|
+
properties: SchemaProperty[];
|
|
13
|
+
}
|
|
14
|
+
export declare const RESERVED_LIFECYCLE_NAMES: readonly ["pre-execute", "post-execute"];
|
|
15
|
+
export declare const FORBIDDEN_KEYS: Set<string>;
|
|
16
|
+
export declare function isPlainObject(value: unknown): value is {
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// In-memory schema representation (design §4.3). Kept an ARRAY (never a Record) so
|
|
2
|
+
// FIT-07's no-path-keyed-collection guard stays clean over src/core/**.
|
|
3
|
+
//
|
|
4
|
+
// This is a DISTINCT shape from the on-disk wire contract (ADR-0027): the wire form is a
|
|
5
|
+
// wrapped `{ "properties": { "<key>": {...} } }` map (the SDK<->Go-CLI cross-repo contract);
|
|
6
|
+
// schema-parse.ts lifts that map into this array, `key` becoming an explicit field.
|
|
7
|
+
// Cross-repo lifecycle tokens (2026-07-06 engine handoff) — schematic-level, kebab-case,
|
|
8
|
+
// case-insensitive matching lives in the reserved-name scan (S-004, ADR-0028).
|
|
9
|
+
export const RESERVED_LIFECYCLE_NAMES = ["pre-execute", "post-execute"];
|
|
10
|
+
// Proto-pollution guard (ADR-0029 safe iteration): shared by schema-validate.ts's
|
|
11
|
+
// input-key scan and schema-sufficiency.ts's declared-property-key scan.
|
|
12
|
+
export const FORBIDDEN_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
13
|
+
export function isPlainObject(value) {
|
|
14
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
15
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Schema } from "./schema-model.ts";
|
|
2
|
+
export interface SchemaParseFailureInfo {
|
|
3
|
+
problem: string;
|
|
4
|
+
line: number | undefined;
|
|
5
|
+
column: number | undefined;
|
|
6
|
+
}
|
|
7
|
+
export declare class SchemaParseFailure extends Error {
|
|
8
|
+
readonly problem: string;
|
|
9
|
+
readonly line: number | undefined;
|
|
10
|
+
readonly column: number | undefined;
|
|
11
|
+
constructor(info: SchemaParseFailureInfo);
|
|
12
|
+
}
|
|
13
|
+
export declare function formatLocator(line: number | undefined, column: number | undefined): string;
|
|
14
|
+
/**
|
|
15
|
+
* Parses the on-disk WRAPPED `schema.json` wire shape (ADR-0027: top-level
|
|
16
|
+
* `{ "properties": { "<key>": {...} } }`) into the in-memory `Schema` array model.
|
|
17
|
+
* Fail-closed: invalid JSON or a missing/non-object `"properties"` key both throw
|
|
18
|
+
* `SchemaParseFailure` on the same path — the raw content is never echoed.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseSchema(raw: string): Schema;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Bytes -> Schema. Shared by the bin (TFO-04's parse-error template) and the runtime
|
|
2
|
+
// (RBV-05.1's fail-closed literal) — both format SchemaParseFailure into their own
|
|
3
|
+
// vocabulary; this module never renders a caller-facing message itself (no-echo, ADR-0027).
|
|
4
|
+
import { isPlainObject } from "./schema-model.js";
|
|
5
|
+
import { locateFirstJsonSyntaxError } from "./schema-locate.js";
|
|
6
|
+
export class SchemaParseFailure extends Error {
|
|
7
|
+
problem;
|
|
8
|
+
line;
|
|
9
|
+
column;
|
|
10
|
+
constructor(info) {
|
|
11
|
+
super(`schema parse failure: ${info.problem}`);
|
|
12
|
+
this.name = "SchemaParseFailure";
|
|
13
|
+
this.problem = info.problem;
|
|
14
|
+
this.line = info.line;
|
|
15
|
+
this.column = info.column;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
// Shared parse-error locator suffix — both the runtime (context.ts's RBV-05.1 literal) and
|
|
19
|
+
// the bin (pbuilder-codegen.ts's TW-m6 pinned template) render a `SchemaParseFailure`'s
|
|
20
|
+
// line/column into their own message; this is the one place that formats it.
|
|
21
|
+
export function formatLocator(line, column) {
|
|
22
|
+
return line !== undefined && column !== undefined ? `(line ${line}, column ${column})` : "(position unknown)";
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Parses the on-disk WRAPPED `schema.json` wire shape (ADR-0027: top-level
|
|
26
|
+
* `{ "properties": { "<key>": {...} } }`) into the in-memory `Schema` array model.
|
|
27
|
+
* Fail-closed: invalid JSON or a missing/non-object `"properties"` key both throw
|
|
28
|
+
* `SchemaParseFailure` on the same path — the raw content is never echoed.
|
|
29
|
+
*/
|
|
30
|
+
export function parseSchema(raw) {
|
|
31
|
+
let parsed;
|
|
32
|
+
try {
|
|
33
|
+
parsed = JSON.parse(raw);
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
// The locator re-scans already-rejected text and is itself recursive (ADR-0032) — a
|
|
37
|
+
// pathologically deep document (e.g. thousands of nested `[`) can overflow the call
|
|
38
|
+
// stack. That RangeError is not a grammar finding; degrade to the same "no offset"
|
|
39
|
+
// fallback as a bounded-fidelity locator result rather than let it escape raw.
|
|
40
|
+
let locator;
|
|
41
|
+
if (err instanceof SyntaxError) {
|
|
42
|
+
try {
|
|
43
|
+
locator = locateFirstJsonSyntaxError(raw);
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
locator = undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
throw new SchemaParseFailure({
|
|
50
|
+
problem: "invalid JSON",
|
|
51
|
+
line: locator?.line,
|
|
52
|
+
column: locator?.column,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
if (!isPlainObject(parsed) || !isPlainObject(parsed.properties)) {
|
|
56
|
+
throw new SchemaParseFailure({
|
|
57
|
+
problem: 'missing "properties" object',
|
|
58
|
+
line: undefined,
|
|
59
|
+
column: undefined,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const propertiesMap = parsed.properties;
|
|
63
|
+
const properties = [];
|
|
64
|
+
// Object.keys (never for-in / bare property access) — safe iteration (constraint 5).
|
|
65
|
+
for (const key of Object.keys(propertiesMap)) {
|
|
66
|
+
const value = propertiesMap[key];
|
|
67
|
+
if (!isPlainObject(value))
|
|
68
|
+
continue;
|
|
69
|
+
properties.push({
|
|
70
|
+
key,
|
|
71
|
+
type: value.type,
|
|
72
|
+
label: typeof value.label === "string" ? value.label : undefined,
|
|
73
|
+
choices: Array.isArray(value.choices) ? value.choices : undefined,
|
|
74
|
+
default: value.default,
|
|
75
|
+
required: typeof value.required === "boolean" ? value.required : undefined,
|
|
76
|
+
description: typeof value.description === "string" ? value.description : undefined,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return { properties };
|
|
80
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare const RECOGNIZED_KINDS: Set<string>;
|
|
2
|
+
export type SufficiencyReason = "forbidden-key" | "missing-type" | "nonsensical-type" | "enum-missing-choices" | "missing-label";
|
|
3
|
+
export interface SufficiencyFinding {
|
|
4
|
+
key: string;
|
|
5
|
+
reason: SufficiencyReason;
|
|
6
|
+
detail?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Hard-fail rules over the raw `schema.json` `"properties"` map (REQ-SCP-02): missing
|
|
10
|
+
* `type`; an unrecognized `type` value; an enum property missing (or empty) `choices`; a
|
|
11
|
+
* missing `label`; or a `__proto__`/`constructor`/`prototype` property key. Does NOT fail
|
|
12
|
+
* for a property missing `default`, `required`, or `description` (advisory only, SCP-02.6).
|
|
13
|
+
*
|
|
14
|
+
* Assumes `raw` is syntactically valid JSON (JSON-validity is schema-parse.ts's/RBV-05's
|
|
15
|
+
* concern, not sufficiency's) — a malformed `raw` throws, same as `JSON.parse` would.
|
|
16
|
+
*/
|
|
17
|
+
export declare function checkSufficiency(raw: string): SufficiencyFinding[];
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Schema-sufficiency hard-fail rules (design §4.7, REQ-SCP-02, FIT-13). Operates on the RAW
|
|
2
|
+
// wrapped wire shape directly (ADR-0027) — not the narrowed in-memory `Schema` array
|
|
3
|
+
// (schema-parse.ts casts `type` to `SchemaKind`, which would make an absent/garbage `type`
|
|
4
|
+
// value unrepresentable to this checker without fighting the type system; sufficiency's
|
|
5
|
+
// whole job is auditing data that may NOT conform to that narrowed shape).
|
|
6
|
+
//
|
|
7
|
+
// Safe iteration via Object.keys only (constraint 5) — a hostile `__proto__`-named property
|
|
8
|
+
// is itself one of the hard-fail conditions this module exists to catch, so the walk must
|
|
9
|
+
// never trigger a prototype getter via bare/for-in access.
|
|
10
|
+
import { FORBIDDEN_KEYS, isPlainObject } from "./schema-model.js";
|
|
11
|
+
// Exported as the single canonical allow-list — `emitPropertyType` (bin/emit-type.ts) reuses
|
|
12
|
+
// this same Set as its last-line-of-defence guard against an unrecognized `type`, rather than
|
|
13
|
+
// duplicating the recognized-kind vocabulary at the emit boundary.
|
|
14
|
+
export const RECOGNIZED_KINDS = new Set(["string", "number", "boolean", "enum"]);
|
|
15
|
+
/**
|
|
16
|
+
* Hard-fail rules over the raw `schema.json` `"properties"` map (REQ-SCP-02): missing
|
|
17
|
+
* `type`; an unrecognized `type` value; an enum property missing (or empty) `choices`; a
|
|
18
|
+
* missing `label`; or a `__proto__`/`constructor`/`prototype` property key. Does NOT fail
|
|
19
|
+
* for a property missing `default`, `required`, or `description` (advisory only, SCP-02.6).
|
|
20
|
+
*
|
|
21
|
+
* Assumes `raw` is syntactically valid JSON (JSON-validity is schema-parse.ts's/RBV-05's
|
|
22
|
+
* concern, not sufficiency's) — a malformed `raw` throws, same as `JSON.parse` would.
|
|
23
|
+
*/
|
|
24
|
+
export function checkSufficiency(raw) {
|
|
25
|
+
const parsed = JSON.parse(raw);
|
|
26
|
+
const findings = [];
|
|
27
|
+
if (!isPlainObject(parsed) || !isPlainObject(parsed.properties)) {
|
|
28
|
+
return findings; // shape validity (missing/non-object "properties") is parse's concern
|
|
29
|
+
}
|
|
30
|
+
const propertiesMap = parsed.properties;
|
|
31
|
+
for (const key of Object.keys(propertiesMap)) {
|
|
32
|
+
if (FORBIDDEN_KEYS.has(key)) {
|
|
33
|
+
findings.push({ key, reason: "forbidden-key" });
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const value = propertiesMap[key];
|
|
37
|
+
if (!isPlainObject(value))
|
|
38
|
+
continue; // non-object property values are parse's concern
|
|
39
|
+
const type = value.type;
|
|
40
|
+
if (type === undefined) {
|
|
41
|
+
findings.push({ key, reason: "missing-type" });
|
|
42
|
+
}
|
|
43
|
+
else if (typeof type !== "string" || !RECOGNIZED_KINDS.has(type)) {
|
|
44
|
+
findings.push({ key, reason: "nonsensical-type", detail: String(type) });
|
|
45
|
+
}
|
|
46
|
+
else if (type === "enum" && (!Array.isArray(value.choices) || value.choices.length === 0)) {
|
|
47
|
+
findings.push({ key, reason: "enum-missing-choices" });
|
|
48
|
+
}
|
|
49
|
+
if (typeof value.label !== "string") {
|
|
50
|
+
findings.push({ key, reason: "missing-label" });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return findings;
|
|
54
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Schema } from "./schema-model.ts";
|
|
2
|
+
export type ValidationFinding = {
|
|
3
|
+
kind: "missing";
|
|
4
|
+
field: string;
|
|
5
|
+
expectedType: string;
|
|
6
|
+
} | {
|
|
7
|
+
kind: "wrong-type";
|
|
8
|
+
field: string;
|
|
9
|
+
expectedType: string;
|
|
10
|
+
} | {
|
|
11
|
+
kind: "disallowed-key";
|
|
12
|
+
field: string;
|
|
13
|
+
};
|
|
14
|
+
export declare function validateInput(schema: Schema, input: unknown): ValidationFinding[];
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Resolved input -> findings (design §4.2/§4.3, ADR-0029 safe iteration). Two independent
|
|
2
|
+
// walks, both safe against a hostile `__proto__`-shaped input: (1) the schema's OWN
|
|
3
|
+
// declared properties drive missing/wrong-type checks (never `for...in`/bare access on the
|
|
4
|
+
// input); (2) Object.keys(input) drives excess/reserved/proto-key detection — Object.keys
|
|
5
|
+
// returns only OWN enumerable string keys, so it can never be redirected by a prototype
|
|
6
|
+
// getter, and this module never WRITES through a computed key (the actual pollution
|
|
7
|
+
// vector — `acc[key] = v` on a plain-object accumulator), only reads and reports names.
|
|
8
|
+
//
|
|
9
|
+
// S-000 built the "missing" finding (RBV-01.1 site proof). S-003 (this module) triangulates
|
|
10
|
+
// the remaining branches — see slices Coverage Check.
|
|
11
|
+
import { FORBIDDEN_KEYS, RESERVED_LIFECYCLE_NAMES } from "./schema-model.js";
|
|
12
|
+
const RESERVED_INPUT_KEYS = new Set(RESERVED_LIFECYCLE_NAMES);
|
|
13
|
+
// {expectedType} rendering (design §4.4, branch table): always the DECLARED expectation,
|
|
14
|
+
// never the received value's kind — an enum renders its choice list, everything else
|
|
15
|
+
// renders its SchemaKind verbatim.
|
|
16
|
+
function expectedTypeFor(property) {
|
|
17
|
+
if (property.type === "enum") {
|
|
18
|
+
return `one of: ${(property.choices ?? []).join(", ")}`;
|
|
19
|
+
}
|
|
20
|
+
return property.type;
|
|
21
|
+
}
|
|
22
|
+
function matchesType(property, value) {
|
|
23
|
+
switch (property.type) {
|
|
24
|
+
case "string":
|
|
25
|
+
return typeof value === "string";
|
|
26
|
+
case "number":
|
|
27
|
+
return typeof value === "number";
|
|
28
|
+
case "boolean":
|
|
29
|
+
return typeof value === "boolean";
|
|
30
|
+
case "enum":
|
|
31
|
+
return typeof value === "string" && (property.choices ?? []).includes(value);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function validateInput(schema, input) {
|
|
35
|
+
const findings = [];
|
|
36
|
+
const record = (input !== null && typeof input === "object" ? input : {});
|
|
37
|
+
for (const property of schema.properties) {
|
|
38
|
+
const isRequired = property.required !== false;
|
|
39
|
+
// A present-but-undefined value is treated as absent (REQ-RBV-01.7's "undefined" leg
|
|
40
|
+
// of the trichotomy is equivalent to "no key at all" — there is no JSON representation
|
|
41
|
+
// of an explicit `undefined`).
|
|
42
|
+
const present = Object.hasOwn(record, property.key) && record[property.key] !== undefined;
|
|
43
|
+
if (!present) {
|
|
44
|
+
if (isRequired) {
|
|
45
|
+
findings.push({ kind: "missing", field: property.key, expectedType: expectedTypeFor(property) });
|
|
46
|
+
}
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (!matchesType(property, record[property.key])) {
|
|
50
|
+
findings.push({ kind: "wrong-type", field: property.key, expectedType: expectedTypeFor(property) });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// REQ-RBV-01.3/.5/.6: any input key that is not a declared property, OR is itself a
|
|
54
|
+
// reserved lifecycle name, OR is __proto__/constructor/prototype — regardless of whether
|
|
55
|
+
// it happens to also be declared — is a single "disallowed-key" class (branch→template
|
|
56
|
+
// mapping, slices load-bearing literals).
|
|
57
|
+
const declaredKeys = new Set(schema.properties.map((property) => property.key));
|
|
58
|
+
for (const key of Object.keys(record)) {
|
|
59
|
+
if (RESERVED_INPUT_KEYS.has(key) || FORBIDDEN_KEYS.has(key) || !declaredKeys.has(key)) {
|
|
60
|
+
findings.push({ kind: "disallowed-key", field: key });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return findings;
|
|
64
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EngineClient } from "./engine-client.ts";
|
|
2
|
+
import type { Directive } from "./wire.ts";
|
|
3
|
+
export declare class Session {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(client: EngineClient);
|
|
6
|
+
buffer(directive: Directive): void;
|
|
7
|
+
pendingSnapshot(): readonly Directive[];
|
|
8
|
+
isPending(directive: Directive): boolean;
|
|
9
|
+
read(path: string): Promise<string | undefined>;
|
|
10
|
+
commit(): Promise<void>;
|
|
11
|
+
discard(): Promise<void>;
|
|
12
|
+
flush(): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// KIT-02 / ADR-0008: holds ONLY the pending directive buffer + the EngineClient.
|
|
2
|
+
// No path-keyed map, no tree. read() flushes pending BEFORE delegating to the client.
|
|
3
|
+
// REQ-KIT-05: flush() is package-internal — called by defineFactory after fn resolves
|
|
4
|
+
// so write-only factories (no read call) still emit their batch.
|
|
5
|
+
//
|
|
6
|
+
// envelope-level `force` is part of the engine wire contract that the fake honors,
|
|
7
|
+
// but v1's author surface exposes only op-level `force`; this field is hard-coded false
|
|
8
|
+
// here because the author API has no run-wide force concept yet. The fake's Row-3 test
|
|
9
|
+
// (FAKE-04) is intentional engine-contract coverage, not dead-code coverage.
|
|
10
|
+
import { toAuthoringError } from "./authoring-error.js";
|
|
11
|
+
export class Session {
|
|
12
|
+
#client;
|
|
13
|
+
#pending = [];
|
|
14
|
+
constructor(client) {
|
|
15
|
+
this.#client = client;
|
|
16
|
+
}
|
|
17
|
+
buffer(directive) {
|
|
18
|
+
this.#pending.push(directive);
|
|
19
|
+
}
|
|
20
|
+
// SEAM-02 (REQ-03): a defensive copy of the pending buffer for the dry-run renderer.
|
|
21
|
+
// Later mutations to #pending must NOT affect a snapshot taken earlier.
|
|
22
|
+
pendingSnapshot() {
|
|
23
|
+
return this.#pending.slice();
|
|
24
|
+
}
|
|
25
|
+
// Row-141 kept-half: an identity membership check against the LIVE buffer (no defensive
|
|
26
|
+
// copy) for call sites that only need a yes/no answer per directive, e.g. the dialect
|
|
27
|
+
// handle's orphan-guard (#ensureOpen) and row-136's modify-reject predicate.
|
|
28
|
+
isPending(directive) {
|
|
29
|
+
return this.#pending.includes(directive);
|
|
30
|
+
}
|
|
31
|
+
async read(path) {
|
|
32
|
+
await this.flush();
|
|
33
|
+
return this.#client.read(path);
|
|
34
|
+
}
|
|
35
|
+
// SEAM-03 (ADR-01): thin wrappers delegating to #client. Session owns #client, so the
|
|
36
|
+
// commit boundary stays inside Session — defineFactory never names #client directly.
|
|
37
|
+
async commit() {
|
|
38
|
+
await this.#client.commit();
|
|
39
|
+
}
|
|
40
|
+
async discard() {
|
|
41
|
+
await this.#client.discard();
|
|
42
|
+
}
|
|
43
|
+
// Called by defineFactory after fn resolves (REQ-KIT-05). Also called by read() before
|
|
44
|
+
// delegating to the client (REQ-KIT-02). SEAM-04 (ADR-02): the #client.emit call site is
|
|
45
|
+
// wrapped so a raw engine rejection surfaces as an AuthoringError in author vocabulary,
|
|
46
|
+
// attributed to the ACTUAL offending directive via the rejection's structured metadata
|
|
47
|
+
// (emit-rejection-metadata REQ-ERM-01) — not instructions[0].
|
|
48
|
+
//
|
|
49
|
+
// stdio-engine-client REQ-SEC-08.3/REQ-EXC-01: a TRANSPORT-class fault from the client is
|
|
50
|
+
// NOT an engine rejection — it must reach the runner's classifier as-is (exit 3), never
|
|
51
|
+
// degraded to AuthoringError{unknown} (exit 2). Detected by the error's stable `name`
|
|
52
|
+
// identity, not `instanceof`: importing the TransportFault class here would pull the whole
|
|
53
|
+
// transport client into core's (and the ./commons bundle's) module graph, inverting the
|
|
54
|
+
// port direction. ERM-03's degrade still applies to everything else non-EmitRejection.
|
|
55
|
+
async flush() {
|
|
56
|
+
if (this.#pending.length === 0)
|
|
57
|
+
return;
|
|
58
|
+
const batch = {
|
|
59
|
+
protocolVersion: 1,
|
|
60
|
+
force: false,
|
|
61
|
+
instructions: this.#pending.splice(0),
|
|
62
|
+
};
|
|
63
|
+
try {
|
|
64
|
+
await this.#client.emit(batch);
|
|
65
|
+
}
|
|
66
|
+
catch (raw) {
|
|
67
|
+
if (raw instanceof Error && raw.name === "TransportFault")
|
|
68
|
+
throw raw;
|
|
69
|
+
throw toAuthoringError(raw, batch);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
export type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
2
|
+
[k: string]: JsonValue;
|
|
3
|
+
};
|
|
4
|
+
/**
|
|
5
|
+
* The wire-level mutation instruction the SDK emits and the engine applies (ADR-0001,
|
|
6
|
+
* ADR-0013). Opaque to the template DSL — `template`/`pathTemplate` are uninterpreted
|
|
7
|
+
* strings from the SDK's perspective; rendering (if any) is the engine's job.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const directive: Directive = {
|
|
11
|
+
* op: "create",
|
|
12
|
+
* create: {
|
|
13
|
+
* pathTemplate: "src/greeting.ts",
|
|
14
|
+
* template: "export const greeting = '{{name}}';",
|
|
15
|
+
* options: { name: "world" },
|
|
16
|
+
* },
|
|
17
|
+
* };
|
|
18
|
+
*/
|
|
19
|
+
export type Directive = {
|
|
20
|
+
op: "create";
|
|
21
|
+
create: {
|
|
22
|
+
pathTemplate: string;
|
|
23
|
+
template: string;
|
|
24
|
+
options: JsonValue;
|
|
25
|
+
force?: boolean;
|
|
26
|
+
};
|
|
27
|
+
} | {
|
|
28
|
+
op: "modify";
|
|
29
|
+
modify: {
|
|
30
|
+
path: string;
|
|
31
|
+
content: string;
|
|
32
|
+
};
|
|
33
|
+
} | {
|
|
34
|
+
op: "delete";
|
|
35
|
+
delete: {
|
|
36
|
+
path: string;
|
|
37
|
+
};
|
|
38
|
+
} | {
|
|
39
|
+
op: "rename";
|
|
40
|
+
rename: {
|
|
41
|
+
path: string;
|
|
42
|
+
newName: string;
|
|
43
|
+
force?: boolean;
|
|
44
|
+
};
|
|
45
|
+
} | {
|
|
46
|
+
op: "move";
|
|
47
|
+
move: {
|
|
48
|
+
path: string;
|
|
49
|
+
toDir: string;
|
|
50
|
+
force?: boolean;
|
|
51
|
+
};
|
|
52
|
+
} | {
|
|
53
|
+
op: "copy";
|
|
54
|
+
copy: {
|
|
55
|
+
from: string;
|
|
56
|
+
to: string;
|
|
57
|
+
force?: boolean;
|
|
58
|
+
};
|
|
59
|
+
} | {
|
|
60
|
+
op: "copyIn";
|
|
61
|
+
copyIn: {
|
|
62
|
+
from: string;
|
|
63
|
+
to: string;
|
|
64
|
+
force?: boolean;
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* The all-or-nothing wire envelope the SDK emits per `emit()` call — a fixed-order set of
|
|
69
|
+
* `Directive`s plus the flags the engine checks before applying anything. Envelope field
|
|
70
|
+
* order is fixed: `protocolVersion`, `force`, `instructions`.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* const batch: Batch = { protocolVersion: 1, force: false, instructions: [] };
|
|
74
|
+
*/
|
|
75
|
+
export type Batch = {
|
|
76
|
+
protocolVersion: 1;
|
|
77
|
+
force: boolean;
|
|
78
|
+
instructions: Directive[];
|
|
79
|
+
};
|
|
80
|
+
export declare const BATCH_CAP_BYTES: number;
|
|
81
|
+
export declare function serializedBatchBytes(batch: Batch): number;
|
|
82
|
+
export declare function serializedBatchSize(instructions: readonly Directive[]): number;
|
|
83
|
+
export declare function exceedsBatchCap(subject: Batch): boolean;
|
|
84
|
+
export declare function exceedsBatchCap(subject: number): boolean;
|
|
85
|
+
export declare const EMIT_FRAME_ENVELOPE_ALLOWANCE_BYTES: number;
|
|
86
|
+
export declare const EMIT_BATCH_BUDGET_BYTES: number;
|
|
87
|
+
export declare function exceedsEmitBatchBudget(batch: Batch): boolean;
|