@pbuilder/sdk 0.15.1 → 0.16.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.
@@ -0,0 +1,19 @@
1
+ import type { Op } from "./define-dialect.ts";
2
+ /**
3
+ * Declares an op repeatable: its effect depends on how many times it is applied. It adds again
4
+ * on every application, or it acts on a position, so applied again it reaches another element.
5
+ * The conformance kit checks every other op for idempotency (A-11) and skips a chain holding a
6
+ * repeatable op. The op itself is marked and returned, so it stays repeatable in every pack
7
+ * that reuses it.
8
+ *
9
+ * @example
10
+ * export const push = repeatable(function push(ast: string[], line: string): void {
11
+ * ast.push(line);
12
+ * });
13
+ * const pack = defineOpPack({
14
+ * setLine(ast: string[], index: number, line: string) { ast[index] = line; },
15
+ * push,
16
+ * });
17
+ */
18
+ export declare function repeatable<F extends Op>(op: F): F;
19
+ export declare function isRepeatable(op: unknown): boolean;
@@ -0,0 +1,24 @@
1
+ const repeatableOps = new WeakSet();
2
+ /**
3
+ * Declares an op repeatable: its effect depends on how many times it is applied. It adds again
4
+ * on every application, or it acts on a position, so applied again it reaches another element.
5
+ * The conformance kit checks every other op for idempotency (A-11) and skips a chain holding a
6
+ * repeatable op. The op itself is marked and returned, so it stays repeatable in every pack
7
+ * that reuses it.
8
+ *
9
+ * @example
10
+ * export const push = repeatable(function push(ast: string[], line: string): void {
11
+ * ast.push(line);
12
+ * });
13
+ * const pack = defineOpPack({
14
+ * setLine(ast: string[], index: number, line: string) { ast[index] = line; },
15
+ * push,
16
+ * });
17
+ */
18
+ export function repeatable(op) {
19
+ repeatableOps.add(op);
20
+ return op;
21
+ }
22
+ export function isRepeatable(op) {
23
+ return typeof op === "function" && repeatableOps.has(op);
24
+ }
@@ -22,17 +22,17 @@ export declare function removeKey(doc: JsonDocument, path: JsonPath): void;
22
22
  /** Renames an object key in place, keeping its value, position, and comments. */
23
23
  export declare function renameKey(doc: JsonDocument, path: JsonPath, newKey: string): void;
24
24
  /** Inserts `value` before the item at `index`; `index === length` appends. */
25
- export declare function insertItem(doc: JsonDocument, path: JsonPath, index: number, value: JsonValue): void;
25
+ export declare const insertItem: (doc: JsonDocument, path: JsonPath, index: number, value: JsonValue) => void;
26
26
  /** Appends `value`, creating the array when absent; `{ unique: true }` skips a deep-equal duplicate. */
27
- export declare function appendItem(doc: JsonDocument, path: JsonPath, value: JsonValue, options?: AppendOptions): void;
27
+ export declare const appendItem: (doc: JsonDocument, path: JsonPath, value: JsonValue, options?: AppendOptions) => void;
28
28
  /** Removes the item at `index` and its separator. */
29
- export declare function removeItem(doc: JsonDocument, path: JsonPath, index: number): void;
29
+ export declare const removeItem: (doc: JsonDocument, path: JsonPath, index: number) => void;
30
30
  /** Removes every item deep-equal to `value`; no match (or no array) is a no-op. */
31
31
  export declare function removeValue(doc: JsonDocument, path: JsonPath, value: JsonValue): void;
32
32
  /** Replaces the item at `index` in place. */
33
33
  export declare function replaceItem(doc: JsonDocument, path: JsonPath, index: number, value: JsonValue): void;
34
34
  /** Moves the item at `from` so it ends up at `to`; the other items keep their relative order. */
35
- export declare function moveItem(doc: JsonDocument, path: JsonPath, from: number, to: number): void;
35
+ export declare const moveItem: (doc: JsonDocument, path: JsonPath, from: number, to: number) => void;
36
36
  /** Deep-merges a plain object into the object at `path`; arrays and scalars are replaced. */
37
37
  export declare function merge(doc: JsonDocument, path: JsonPath, value: JsonObject): void;
38
38
  /** Replaces the value at `path` with `fn(current)`; `current` is `undefined` when absent. */
@@ -1,3 +1,4 @@
1
+ import { repeatable } from "../../../../domain/services/repeatable.js";
1
2
  import { applyEdits, getNodeValue, modify, parseTree, } from "jsonc-parser";
2
3
  import { deepEqual } from "../../../../../../platform/deep-equal.js";
3
4
  import { dialectError } from "../../../../domain/valueobjects/dialect-error.js";
@@ -354,15 +355,15 @@ export function renameKey(doc, path, newKey) {
354
355
  doc.text = doc.text.slice(0, keyNode.offset) + JSON.stringify(newKey) + doc.text.slice(keyNode.offset + keyNode.length);
355
356
  }
356
357
  /** Inserts `value` before the item at `index`; `index === length` appends. */
357
- export function insertItem(doc, path, index, value) {
358
+ export const insertItem = repeatable(function insertItem(doc, path, index, value) {
358
359
  assertPath("insertItem", path);
359
360
  assertJsonValue(value);
360
361
  const length = requireArray("insertItem", doc, path).children.length;
361
362
  assertIndex("insertItem", path, index, length);
362
363
  insertAt("insertItem", doc, path, index, value);
363
- }
364
+ });
364
365
  /** Appends `value`, creating the array when absent; `{ unique: true }` skips a deep-equal duplicate. */
365
- export function appendItem(doc, path, value, options) {
366
+ export const appendItem = repeatable(function appendItem(doc, path, value, options) {
366
367
  assertPath("appendItem", path);
367
368
  assertJsonValue(value);
368
369
  const array = arrayAt("appendItem", doc, path);
@@ -373,14 +374,14 @@ export function appendItem(doc, path, value, options) {
373
374
  if (options?.unique === true && array.children.some((item) => deepEqual(getNodeValue(item), value)))
374
375
  return;
375
376
  insertAt("appendItem", doc, path, array.children.length, value);
376
- }
377
+ });
377
378
  /** Removes the item at `index` and its separator. */
378
- export function removeItem(doc, path, index) {
379
+ export const removeItem = repeatable(function removeItem(doc, path, index) {
379
380
  assertPath("removeItem", path);
380
381
  const length = requireArray("removeItem", doc, path).children.length;
381
382
  assertIndex("removeItem", path, index, length - 1);
382
383
  removeAt("removeItem", doc, [...path, index]);
383
- }
384
+ });
384
385
  /** Removes every item deep-equal to `value`; no match (or no array) is a no-op. */
385
386
  export function removeValue(doc, path, value) {
386
387
  assertPath("removeValue", path);
@@ -402,7 +403,7 @@ export function replaceItem(doc, path, index, value) {
402
403
  write("replaceItem", doc, [...path, index], value);
403
404
  }
404
405
  /** Moves the item at `from` so it ends up at `to`; the other items keep their relative order. */
405
- export function moveItem(doc, path, from, to) {
406
+ export const moveItem = repeatable(function moveItem(doc, path, from, to) {
406
407
  assertPath("moveItem", path);
407
408
  const items = requireArray("moveItem", doc, path).children;
408
409
  assertIndex("moveItem", path, from, items.length - 1);
@@ -412,7 +413,7 @@ export function moveItem(doc, path, from, to) {
412
413
  const value = getNodeValue(items[from]);
413
414
  removeAt("moveItem", doc, [...path, from]);
414
415
  insertAt("moveItem", doc, path, to, value);
415
- }
416
+ });
416
417
  function mergeInto(doc, path, value) {
417
418
  for (const [key, incoming] of Object.entries(value)) {
418
419
  const childPath = [...path, key];
@@ -1,8 +1,8 @@
1
1
  import { type YamlStream } from "./ast.ts";
2
2
  import { type YamlValue } from "./paths.ts";
3
3
  /** Inserts a document holding `value` at `options.index`, by default at the end. */
4
- export declare function addDocument(ast: YamlStream, value: YamlValue, options?: {
4
+ export declare const addDocument: (ast: YamlStream, value: YamlValue, options?: {
5
5
  index?: number;
6
- }): void;
6
+ }) => void;
7
7
  /** Removes the document at `index`. */
8
- export declare function removeDocument(ast: YamlStream, index: number): void;
8
+ export declare const removeDocument: (ast: YamlStream, index: number) => void;
@@ -1,4 +1,5 @@
1
1
  // Document-level ops for multi-document files.
2
+ import { repeatable } from "../../../../domain/services/repeatable.js";
2
3
  import { newDocument } from "./ast.js";
3
4
  import { assertValue, fail, isPlainObject } from "./paths.js";
4
5
  function assertIndex(op, index, max) {
@@ -7,18 +8,18 @@ function assertIndex(op, index, max) {
7
8
  }
8
9
  }
9
10
  /** Inserts a document holding `value` at `options.index`, by default at the end. */
10
- export function addDocument(ast, value, options) {
11
+ export const addDocument = repeatable(function addDocument(ast, value, options) {
11
12
  assertValue("addDocument", [], value);
12
13
  if (options !== undefined && !isPlainObject(options))
13
14
  fail("addDocument", null, "options must be a plain object.");
14
15
  const index = options?.index ?? ast.documents.length;
15
16
  assertIndex("addDocument", index, ast.documents.length);
16
17
  ast.documents.splice(index, 0, newDocument(value));
17
- }
18
+ });
18
19
  /** Removes the document at `index`. */
19
- export function removeDocument(ast, index) {
20
+ export const removeDocument = repeatable(function removeDocument(ast, index) {
20
21
  if (typeof index !== "number" || !Number.isInteger(index) || index < 0 || index >= ast.documents.length) {
21
22
  fail("removeDocument", null, `document ${String(index)} is out of range; the file has ${ast.documents.length} document(s).`);
22
23
  }
23
24
  ast.documents.splice(index, 1);
24
- }
25
+ });
@@ -10,19 +10,19 @@ export declare function removeKey(ast: YamlStream, path: YamlPath, options?: Doc
10
10
  /** Renames a mapping key in place, keeping its value, position, and comments. */
11
11
  export declare function renameKey(ast: YamlStream, path: YamlPath, newKey: string, options?: DocumentOption): void;
12
12
  /** Inserts a sequence item before `index`; `index` equal to the length appends. */
13
- export declare function insertItem(ast: YamlStream, path: YamlPath, index: number, value: YamlValue, options?: DocumentOption): void;
13
+ export declare const insertItem: (ast: YamlStream, path: YamlPath, index: number, value: YamlValue, options?: DocumentOption) => void;
14
14
  /** Appends a sequence item, creating the sequence; `{ unique: true }` skips a deep-equal duplicate. */
15
- export declare function appendItem(ast: YamlStream, path: YamlPath, value: YamlValue, options?: DocumentOption & {
15
+ export declare const appendItem: (ast: YamlStream, path: YamlPath, value: YamlValue, options?: DocumentOption & {
16
16
  unique?: boolean;
17
- }): void;
17
+ }) => void;
18
18
  /** Removes the sequence item at `index`. */
19
- export declare function removeItem(ast: YamlStream, path: YamlPath, index: number, options?: DocumentOption): void;
19
+ export declare const removeItem: (ast: YamlStream, path: YamlPath, index: number, options?: DocumentOption) => void;
20
20
  /** Removes every sequence item deep-equal to `value`; no match is a no-op. */
21
21
  export declare function removeValue(ast: YamlStream, path: YamlPath, value: YamlValue, options?: DocumentOption): void;
22
22
  /** Replaces the sequence item at `index`, keeping its comments. */
23
23
  export declare function replaceItem(ast: YamlStream, path: YamlPath, index: number, value: YamlValue, options?: DocumentOption): void;
24
24
  /** Moves the item at `from` so it sits at `to`; the others keep their relative order. */
25
- export declare function moveItem(ast: YamlStream, path: YamlPath, from: number, to: number, options?: DocumentOption): void;
25
+ export declare const moveItem: (ast: YamlStream, path: YamlPath, from: number, to: number, options?: DocumentOption) => void;
26
26
  /** Deep-merges a plain object into the mapping at `path`, creating it when absent. */
27
27
  export declare function merge(ast: YamlStream, path: YamlPath, value: {
28
28
  [key: string]: YamlValue;
@@ -1,5 +1,6 @@
1
1
  // Structural ops: the JSON dialect's catalogue over `yaml` nodes, with "object" read as mapping
2
2
  // and "array" as sequence.
3
+ import { repeatable } from "../../../../domain/services/repeatable.js";
3
4
  import { isAlias, isMap, isSeq } from "yaml";
4
5
  import { deepEqual } from "../../../../../../platform/deep-equal.js";
5
6
  import { formatPath } from "../../../../domain/valueobjects/json-value.js";
@@ -101,16 +102,16 @@ function assertIndex(op, path, index, max) {
101
102
  }
102
103
  }
103
104
  /** Inserts a sequence item before `index`; `index` equal to the length appends. */
104
- export function insertItem(ast, path, index, value, options) {
105
+ export const insertItem = repeatable(function insertItem(ast, path, index, value, options) {
105
106
  assertPath("insertItem", path);
106
107
  assertValue("insertItem", path, value);
107
108
  const document = selectDocument("insertItem", ast, options);
108
109
  const seq = requireSequence("insertItem", document, path);
109
110
  assertIndex("insertItem", path, index, seq.items.length);
110
111
  seq.items.splice(index, 0, createNode(document, value));
111
- }
112
+ });
112
113
  /** Appends a sequence item, creating the sequence; `{ unique: true }` skips a deep-equal duplicate. */
113
- export function appendItem(ast, path, value, options) {
114
+ export const appendItem = repeatable(function appendItem(ast, path, value, options) {
114
115
  assertPath("appendItem", path);
115
116
  assertValue("appendItem", path, value);
116
117
  const document = selectDocument("appendItem", ast, options, true);
@@ -118,16 +119,16 @@ export function appendItem(ast, path, value, options) {
118
119
  if (options?.unique === true && seq.items.some((item) => deepEqual(toValue(document, item), value)))
119
120
  return;
120
121
  seq.items.push(createNode(document, value));
121
- }
122
+ });
122
123
  /** Removes the sequence item at `index`. */
123
- export function removeItem(ast, path, index, options) {
124
+ export const removeItem = repeatable(function removeItem(ast, path, index, options) {
124
125
  assertPath("removeItem", path);
125
126
  const document = selectDocument("removeItem", ast, options);
126
127
  const seq = requireSequence("removeItem", document, path);
127
128
  assertIndex("removeItem", path, index, seq.items.length - 1);
128
129
  seq.items.splice(index, 1);
129
130
  assertAliasesResolve("removeItem", document);
130
- }
131
+ });
131
132
  /** Removes every sequence item deep-equal to `value`; no match is a no-op. */
132
133
  export function removeValue(ast, path, value, options) {
133
134
  assertPath("removeValue", path);
@@ -153,7 +154,7 @@ export function replaceItem(ast, path, index, value, options) {
153
154
  assertAliasesResolve("replaceItem", document);
154
155
  }
155
156
  /** Moves the item at `from` so it sits at `to`; the others keep their relative order. */
156
- export function moveItem(ast, path, from, to, options) {
157
+ export const moveItem = repeatable(function moveItem(ast, path, from, to, options) {
157
158
  assertPath("moveItem", path);
158
159
  const document = selectDocument("moveItem", ast, options);
159
160
  const seq = requireSequence("moveItem", document, path);
@@ -162,7 +163,7 @@ export function moveItem(ast, path, from, to, options) {
162
163
  const [item] = seq.items.splice(from, 1);
163
164
  seq.items.splice(to, 0, item);
164
165
  assertAliasesResolve("moveItem", document);
165
- }
166
+ });
166
167
  function changes(current, incoming) {
167
168
  if (!isPlainObject(current))
168
169
  return true;
@@ -14,6 +14,7 @@
14
14
  // Drives real runs via `defineFactory` + the kit-internal `run-vehicle.ts` transport —
15
15
  // imported for INTERNAL use only, never re-exported (FIT-08 bans re-export of kit symbols
16
16
  // from this path, not internal use of them).
17
+ import { isRepeatable } from "../../domain/services/repeatable.js";
17
18
  import { defineFactory } from "../../application/usecases/run-factory/context.js";
18
19
  import { deepEqual } from "../../../../platform/deep-equal.js";
19
20
  import { createRunVehicle } from "../run-vehicle.js";
@@ -68,6 +69,31 @@ async function runExercise(dialect, exercise, opts) {
68
69
  .map((d) => d.modify);
69
70
  return { modifies, directives };
70
71
  }
72
+ // A-11: every op that sets a state is idempotent, so applying the chain again to its own
73
+ // result leaves the file as it is. A chain holding an op the dialect declares `repeatable`, or
74
+ // a `.modify` step (author code, not a dialect op), is not checked.
75
+ async function assertIdempotent(fixture, exercise) {
76
+ const opNames = [];
77
+ for (const step of exercise.chain) {
78
+ if ("modify" in step)
79
+ return;
80
+ if (isRepeatable(fixture.baseDialect.ops[step.op]) || isRepeatable(fixture.opPack[step.op]))
81
+ return;
82
+ opNames.push(step.op);
83
+ }
84
+ let again;
85
+ try {
86
+ again = await runExercise(fixture.baseDialect, { seed: exercise.expect, path: exercise.path, chain: exercise.chain }, { injectReadAfterFirstOp: false });
87
+ }
88
+ catch (err) {
89
+ throw new Error(`testOpPack: A-11 idempotency — re-applying the chain [${opNames.join(", ")}] to its own result threw: ` +
90
+ (err instanceof Error ? err.message : String(err)));
91
+ }
92
+ if (again.modifies.some((modify) => modify.content !== exercise.expect)) {
93
+ throw new Error(`testOpPack: A-11 idempotency — applying the chain [${opNames.join(", ")}] again to its own result changed the file; ` +
94
+ "an op whose effect depends on how many times it is applied must be declared with repeatable()");
95
+ }
96
+ }
71
97
  // REQ-DC-06 (ADR-0012 amendment clause 1): four mandatory adversarial samples, injected
72
98
  // ADDITIVELY into every `testDialect`/`testOpPack` run — `DialectFixture` carries no field
73
99
  // capable of disabling this (compile-level guarantee, REQ-DC-06.2, see test/conformance/planted/
@@ -246,6 +272,7 @@ export async function testOpPack(fixture) {
246
272
  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");
247
273
  }
248
274
  }
275
+ await assertIdempotent(fixture, exercise);
249
276
  }
250
277
  // REQ-DC-06/REQ-DC-08 (verify-in-loop-4 Finding #1): the four mandatory adversarial
251
278
  // samples' round-trip + real-base probe, run against `fixture.baseDialect` — same
@@ -2,7 +2,7 @@
2
2
  "manifestVersion": 1,
3
3
  "algorithm": "sha256",
4
4
  "entry": "dist/bin/pbuilder-runner.js",
5
- "packageVersion": "0.15.1",
5
+ "packageVersion": "0.16.0",
6
6
  "files": [
7
7
  {
8
8
  "path": "dist/acl/irwire/frame-reader.js",
@@ -118,7 +118,7 @@
118
118
  },
119
119
  {
120
120
  "path": "package.json",
121
- "sha256": "006f5a675cc32087444d83b62340b1dd2e75c6c86a3e6904eab6453e33500e4f"
121
+ "sha256": "f1b948a8bafaf2e5b9d1f487799cc3b4b9be292b450331434f350cb1034ac52c"
122
122
  }
123
123
  ]
124
124
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pbuilder/sdk",
3
- "version": "0.15.1",
3
+ "version": "0.16.0",
4
4
  "description": "The public TypeScript/Bun authoring layer for Project Builder schematics. Authors file mutations (create, replaceContent, remove, rename, move, copy, copyIn), lowers them to IR, and talks to the Project Builder engine over framed stdio (see docs/engine-sdk-wire-spec.md).",
5
5
  "license": "MIT",
6
6
  "type": "module",