@pbuilder/sdk 0.13.0 → 0.15.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.
Files changed (207) hide show
  1. package/README.md +57 -42
  2. package/dist/{transport → acl/irwire}/frame-reader.d.ts +2 -0
  3. package/dist/{transport → acl/irwire}/frame-reader.js +57 -5
  4. package/dist/{transport → acl/irwire}/stdio-engine-client.d.ts +2 -2
  5. package/dist/{transport → acl/irwire}/stdio-engine-client.js +4 -4
  6. package/dist/bin/pbuilder-codegen.js +30 -35
  7. package/dist/bin/pbuilder-runner.js +6 -6
  8. package/dist/bounded-contexts/authoring/application/usecases/authoring-verbs/index.d.ts +106 -0
  9. package/dist/bounded-contexts/authoring/application/usecases/authoring-verbs/index.js +202 -0
  10. package/dist/{pending-directives → bounded-contexts/authoring/application/usecases/pending-directives}/render.d.ts +2 -2
  11. package/dist/{core → bounded-contexts/authoring/application/usecases/run-factory}/context.d.ts +12 -9
  12. package/dist/{core → bounded-contexts/authoring/application/usecases/run-factory}/context.js +59 -120
  13. package/dist/bounded-contexts/authoring/application/usecases/run-factory/driving.d.ts +5 -0
  14. package/dist/bounded-contexts/authoring/application/usecases/run-factory/driving.js +15 -0
  15. package/dist/bounded-contexts/authoring/application/usecases/scaffold/classify-transport.d.ts +51 -0
  16. package/dist/bounded-contexts/authoring/application/usecases/scaffold/classify-transport.js +105 -0
  17. package/dist/{scaffold → bounded-contexts/authoring/application/usecases/scaffold}/expander.d.ts +2 -1
  18. package/dist/{scaffold → bounded-contexts/authoring/application/usecases/scaffold}/expander.js +16 -7
  19. package/dist/{scaffold → bounded-contexts/authoring/application/usecases/scaffold}/index.d.ts +1 -1
  20. package/dist/{scaffold → bounded-contexts/authoring/application/usecases/scaffold}/index.js +7 -6
  21. package/dist/{scaffold → bounded-contexts/authoring/application/usecases/scaffold}/path-guards.d.ts +11 -10
  22. package/dist/{scaffold → bounded-contexts/authoring/application/usecases/scaffold}/path-guards.js +21 -48
  23. package/dist/{scaffold → bounded-contexts/authoring/application/usecases/scaffold}/walk.d.ts +2 -3
  24. package/dist/{scaffold → bounded-contexts/authoring/application/usecases/scaffold}/walk.js +37 -56
  25. package/dist/{core → bounded-contexts/authoring/domain/aggregates/session}/session.d.ts +2 -2
  26. package/dist/{core → bounded-contexts/authoring/domain/aggregates/session}/session.js +1 -1
  27. package/dist/{core → bounded-contexts/authoring/domain/entities}/dialect-handle.d.ts +1 -1
  28. package/dist/{core → bounded-contexts/authoring/domain/entities}/dialect-handle.js +10 -10
  29. package/dist/{core → bounded-contexts/authoring/domain/ports}/engine-client.d.ts +1 -1
  30. package/dist/bounded-contexts/authoring/domain/ports/package-files.d.ts +53 -0
  31. package/dist/bounded-contexts/authoring/domain/ports/package-files.js +11 -0
  32. package/dist/{core → bounded-contexts/authoring/domain/services}/define-dialect.d.ts +2 -2
  33. package/dist/{core → bounded-contexts/authoring/domain/services}/define-dialect.js +3 -3
  34. package/dist/{core → bounded-contexts/authoring/domain/services}/directive-factory.d.ts +2 -2
  35. package/dist/{core → bounded-contexts/authoring/domain/services}/directive-factory.js +37 -12
  36. package/dist/{scaffold → bounded-contexts/authoring/domain/services}/filename-pipeline.d.ts +11 -1
  37. package/dist/{scaffold → bounded-contexts/authoring/domain/services}/filename-pipeline.js +94 -3
  38. package/dist/{core/jsx-name-validator.d.ts → bounded-contexts/authoring/domain/services/identifier-names.d.ts} +8 -4
  39. package/dist/{core/jsx-name-validator.js → bounded-contexts/authoring/domain/services/identifier-names.js} +21 -28
  40. package/dist/bounded-contexts/authoring/domain/services/reject-tail.d.ts +9 -0
  41. package/dist/bounded-contexts/authoring/domain/services/reject-tail.js +30 -0
  42. package/dist/bounded-contexts/authoring/domain/services/run-context.d.ts +5 -0
  43. package/dist/bounded-contexts/authoring/domain/services/run-context.js +70 -0
  44. package/dist/{core → bounded-contexts/authoring/domain/services}/schema/input-rejection.d.ts +1 -1
  45. package/dist/{core → bounded-contexts/authoring/domain/services}/schema/input-rejection.js +1 -1
  46. package/dist/bounded-contexts/authoring/domain/services/schema/schema-files.d.ts +2 -0
  47. package/dist/bounded-contexts/authoring/domain/services/schema/schema-files.js +6 -0
  48. package/dist/{core → bounded-contexts/authoring/domain/services}/schema/schema-locate.d.ts +2 -1
  49. package/dist/{core → bounded-contexts/authoring/domain/services}/schema/schema-locate.js +13 -2
  50. package/dist/{core → bounded-contexts/authoring/domain/services}/schema/schema-parse.d.ts +1 -1
  51. package/dist/{core → bounded-contexts/authoring/domain/services}/schema/schema-parse.js +2 -14
  52. package/dist/{core → bounded-contexts/authoring/domain/services}/schema/schema-sufficiency.d.ts +4 -3
  53. package/dist/{core → bounded-contexts/authoring/domain/services}/schema/schema-sufficiency.js +8 -4
  54. package/dist/{core → bounded-contexts/authoring/domain/services}/schema/schema-validate.d.ts +1 -1
  55. package/dist/{core → bounded-contexts/authoring/domain/services}/schema/schema-validate.js +1 -1
  56. package/dist/{core → bounded-contexts/authoring/domain/valueobjects}/authoring-error.js +1 -1
  57. package/dist/{core → bounded-contexts/authoring/domain/valueobjects}/wire.d.ts +0 -3
  58. package/dist/bounded-contexts/authoring/domain/valueobjects/wire.js +28 -0
  59. package/dist/bounded-contexts/authoring/infrastructure/in/codegen/cli.d.ts +27 -0
  60. package/dist/bounded-contexts/authoring/infrastructure/in/codegen/cli.js +306 -0
  61. package/dist/bounded-contexts/authoring/infrastructure/in/codegen/emit-type.d.ts +7 -0
  62. package/dist/bounded-contexts/authoring/infrastructure/in/codegen/emit-type.js +68 -0
  63. package/dist/bounded-contexts/authoring/infrastructure/in/codegen/project-codegen.d.ts +19 -0
  64. package/dist/bounded-contexts/authoring/infrastructure/in/codegen/project-codegen.js +131 -0
  65. package/dist/bounded-contexts/authoring/infrastructure/in/commons/index.d.ts +182 -0
  66. package/dist/bounded-contexts/authoring/infrastructure/in/commons/index.js +94 -0
  67. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/angular/index.d.ts +63 -0
  68. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/angular/index.js +68 -0
  69. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/angular/ops.js +9 -9
  70. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/css/index.d.ts +40 -0
  71. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/css/index.js +38 -0
  72. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/css/ops.js +2 -2
  73. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/json/index.d.ts +55 -0
  74. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/json/index.js +54 -0
  75. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/json/ops.d.ts +1 -1
  76. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/json/ops.js +35 -8
  77. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/nestjs/index.d.ts +43 -0
  78. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/nestjs/index.js +46 -0
  79. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/nestjs/ops.js +7 -7
  80. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/react/index.d.ts +52 -0
  81. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/react/index.js +73 -0
  82. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/react/jsx-names.d.ts +4 -0
  83. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/react/jsx-names.js +18 -0
  84. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/react/match-tails.d.ts +4 -0
  85. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/react/match-tails.js +25 -0
  86. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/react/ops.js +5 -3
  87. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/typescript/array-element.d.ts +7 -2
  88. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/typescript/array-element.js +29 -15
  89. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/typescript/ast.d.ts +5 -1
  90. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/typescript/ast.js +22 -4
  91. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/typescript/class-property.js +14 -19
  92. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/typescript/index.d.ts +53 -0
  93. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/typescript/index.js +37 -0
  94. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/typescript/ops.d.ts +6 -1
  95. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/typescript/ops.js +90 -54
  96. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/yaml/index.d.ts +69 -0
  97. package/dist/bounded-contexts/authoring/infrastructure/out/dialects/yaml/index.js +70 -0
  98. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/yaml/paths.d.ts +1 -1
  99. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/yaml/paths.js +2 -2
  100. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/yaml/presentation.js +1 -1
  101. package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/yaml/structural.js +2 -2
  102. package/dist/bounded-contexts/authoring/infrastructure/out/fs/package-files.d.ts +8 -0
  103. package/dist/bounded-contexts/authoring/infrastructure/out/fs/package-files.js +133 -0
  104. package/dist/bounded-contexts/authoring/testing/conformance/index.d.ts +157 -0
  105. package/dist/bounded-contexts/authoring/testing/conformance/index.js +261 -0
  106. package/dist/{testing → bounded-contexts/authoring/testing}/contract-fake.d.ts +2 -2
  107. package/dist/{testing → bounded-contexts/authoring/testing}/contract-fake.js +4 -3
  108. package/dist/bounded-contexts/authoring/testing/harness/index.d.ts +89 -0
  109. package/dist/bounded-contexts/authoring/testing/harness/index.js +85 -0
  110. package/dist/{conformance → bounded-contexts/authoring/testing}/run-vehicle.d.ts +1 -1
  111. package/dist/{transport → bounded-contexts/runner-hosting/infrastructure/in}/bootstrap-bridge.js +2 -2
  112. package/dist/{transport → bounded-contexts/runner-hosting/infrastructure/in}/exit-codes.js +5 -4
  113. package/dist/{transport → bounded-contexts/runner-hosting/infrastructure/in}/factory-pointer.js +1 -1
  114. package/dist/{transport → bounded-contexts/runner-hosting/infrastructure/in}/runner.d.ts +4 -0
  115. package/dist/{transport → bounded-contexts/runner-hosting/infrastructure/in}/runner.js +49 -23
  116. package/dist/{transport → bounded-contexts/runner-hosting/infrastructure/in}/single-instance-probe.js +1 -1
  117. package/dist/commons/index.d.ts +1 -278
  118. package/dist/commons/index.js +1 -286
  119. package/dist/conformance/index.d.ts +1 -157
  120. package/dist/conformance/index.js +1 -261
  121. package/dist/dialects/angular/index.d.ts +1 -63
  122. package/dist/dialects/angular/index.js +1 -68
  123. package/dist/dialects/css/index.d.ts +1 -40
  124. package/dist/dialects/css/index.js +1 -38
  125. package/dist/dialects/json/index.d.ts +1 -55
  126. package/dist/dialects/json/index.js +1 -54
  127. package/dist/dialects/nestjs/index.d.ts +1 -43
  128. package/dist/dialects/nestjs/index.js +1 -46
  129. package/dist/dialects/react/index.d.ts +1 -52
  130. package/dist/dialects/react/index.js +1 -73
  131. package/dist/dialects/typescript/index.d.ts +1 -53
  132. package/dist/dialects/typescript/index.js +1 -37
  133. package/dist/dialects/yaml/index.d.ts +1 -69
  134. package/dist/dialects/yaml/index.js +1 -70
  135. package/dist/{core → platform}/deep-equal.js +2 -2
  136. package/dist/{transport → platform}/error-text.js +2 -2
  137. package/dist/runner-manifest.json +66 -46
  138. package/dist/shared-kernel/batch-cap.d.ts +4 -0
  139. package/dist/{core/wire.js → shared-kernel/batch-cap.js} +9 -26
  140. package/dist/{transport → shared-kernel}/framing.js +2 -2
  141. package/dist/{transport → shared-kernel}/wire-protocol.js +1 -1
  142. package/dist/testing/index.d.ts +1 -84
  143. package/dist/testing/index.js +1 -82
  144. package/package.json +9 -3
  145. package/dist/core/index.d.ts +0 -10
  146. package/dist/core/index.js +0 -6
  147. package/dist/core/reject-tail.d.ts +0 -13
  148. package/dist/core/reject-tail.js +0 -52
  149. package/dist/core/schema/schema-discovery.d.ts +0 -13
  150. package/dist/core/schema/schema-discovery.js +0 -51
  151. package/dist/scaffold/classify-transport.d.ts +0 -54
  152. package/dist/scaffold/classify-transport.js +0 -119
  153. /package/dist/{pending-directives → bounded-contexts/authoring/application/usecases/pending-directives}/index.d.ts +0 -0
  154. /package/dist/{pending-directives → bounded-contexts/authoring/application/usecases/pending-directives}/index.js +0 -0
  155. /package/dist/{pending-directives → bounded-contexts/authoring/application/usecases/pending-directives}/render.js +0 -0
  156. /package/dist/{core → bounded-contexts/authoring/domain/entities}/base-handle.d.ts +0 -0
  157. /package/dist/{core → bounded-contexts/authoring/domain/entities}/base-handle.js +0 -0
  158. /package/dist/{core → bounded-contexts/authoring/domain/entities}/handle-state.d.ts +0 -0
  159. /package/dist/{core → bounded-contexts/authoring/domain/entities}/handle-state.js +0 -0
  160. /package/dist/{core → bounded-contexts/authoring/domain/ports}/emit-rejection.d.ts +0 -0
  161. /package/dist/{core → bounded-contexts/authoring/domain/ports}/emit-rejection.js +0 -0
  162. /package/dist/{core → bounded-contexts/authoring/domain/ports}/engine-client.js +0 -0
  163. /package/dist/{commons → bounded-contexts/authoring/domain/services}/classify-content.d.ts +0 -0
  164. /package/dist/{commons → bounded-contexts/authoring/domain/services}/classify-content.js +0 -0
  165. /package/dist/{core → bounded-contexts/authoring/domain/services}/schema/schema-digest.d.ts +0 -0
  166. /package/dist/{core → bounded-contexts/authoring/domain/services}/schema/schema-digest.js +0 -0
  167. /package/dist/{core → bounded-contexts/authoring/domain/valueobjects}/authoring-error.d.ts +0 -0
  168. /package/dist/{core → bounded-contexts/authoring/domain/valueobjects}/dialect-error.d.ts +0 -0
  169. /package/dist/{core → bounded-contexts/authoring/domain/valueobjects}/dialect-error.js +0 -0
  170. /package/dist/{core → bounded-contexts/authoring/domain/valueobjects}/json-value.d.ts +0 -0
  171. /package/dist/{core → bounded-contexts/authoring/domain/valueobjects}/json-value.js +0 -0
  172. /package/dist/{core/schema → bounded-contexts/authoring/domain/valueobjects}/schema-model.d.ts +0 -0
  173. /package/dist/{core/schema → bounded-contexts/authoring/domain/valueobjects}/schema-model.js +0 -0
  174. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/angular/ops.d.ts +0 -0
  175. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/css/ast.d.ts +0 -0
  176. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/css/ast.js +0 -0
  177. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/css/ops.d.ts +0 -0
  178. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/json/ast.d.ts +0 -0
  179. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/json/ast.js +0 -0
  180. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/nestjs/ops.d.ts +0 -0
  181. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/react/ast.d.ts +0 -0
  182. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/react/ast.js +0 -0
  183. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/react/ops.d.ts +0 -0
  184. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/typescript/class-property.d.ts +0 -0
  185. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/typescript/import-order.d.ts +0 -0
  186. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/typescript/import-order.js +0 -0
  187. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/typescript/pack.d.ts +0 -0
  188. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/typescript/pack.js +0 -0
  189. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/yaml/ast.d.ts +0 -0
  190. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/yaml/ast.js +0 -0
  191. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/yaml/documents.d.ts +0 -0
  192. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/yaml/documents.js +0 -0
  193. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/yaml/presentation.d.ts +0 -0
  194. /package/dist/{dialects → bounded-contexts/authoring/infrastructure/out/dialects}/yaml/structural.d.ts +0 -0
  195. /package/dist/{testing → bounded-contexts/authoring/testing}/rejection-messages.d.ts +0 -0
  196. /package/dist/{testing → bounded-contexts/authoring/testing}/rejection-messages.js +0 -0
  197. /package/dist/{conformance → bounded-contexts/authoring/testing}/run-vehicle.js +0 -0
  198. /package/dist/{transport → bounded-contexts/runner-hosting/infrastructure/in}/bootstrap-bridge.d.ts +0 -0
  199. /package/dist/{transport → bounded-contexts/runner-hosting/infrastructure/in}/exit-codes.d.ts +0 -0
  200. /package/dist/{transport → bounded-contexts/runner-hosting/infrastructure/in}/factory-pointer.d.ts +0 -0
  201. /package/dist/{transport → bounded-contexts/runner-hosting/infrastructure/in}/single-instance-probe.d.ts +0 -0
  202. /package/dist/{core → platform}/deep-equal.d.ts +0 -0
  203. /package/dist/{transport → platform}/error-text.d.ts +0 -0
  204. /package/dist/{core → platform}/fs-errors.d.ts +0 -0
  205. /package/dist/{core → platform}/fs-errors.js +0 -0
  206. /package/dist/{transport → shared-kernel}/framing.d.ts +0 -0
  207. /package/dist/{transport → shared-kernel}/wire-protocol.d.ts +0 -0
@@ -0,0 +1,106 @@
1
+ import type { JsonValue } from "../../../domain/valueobjects/wire.ts";
2
+ import type { FoundHandle, WritableHandle } from "../../../domain/entities/handle-state.ts";
3
+ import type { PendingDirective } from "../pending-directives/render.ts";
4
+ /**
5
+ * Locates an existing file and returns a `FoundHandle` for reading or removing it.
6
+ *
7
+ * `read()` resolves `string | undefined`: the content, `undefined` when the path does not
8
+ * exist, or `""` when the file exists but is empty. Branch on the three outcomes with strict
9
+ * `=== undefined` / `=== ""` — NEVER `if (!content)`, which would merge `undefined`, `""`,
10
+ * `"0"` and `"false"` and reintroduce the absent-vs-empty bug. Prefer `classifyContent()`
11
+ * over manual comparisons — it names the trichotomy and gives exhaustive-switch parity.
12
+ *
13
+ * @example
14
+ * const c = await find("src/config.ts").read();
15
+ * if (c === undefined) create("src/config.ts", { template, options });
16
+ * else if (c === "") replaceContent("src/config.ts", seedContent);
17
+ * else replaceContent("src/config.ts", patch(c));
18
+ */
19
+ export declare function find(path: string): FoundHandle;
20
+ export declare function create(path: string, opts: {
21
+ options: JsonValue | undefined;
22
+ force?: boolean;
23
+ }, template: (options: JsonValue) => string): WritableHandle;
24
+ /**
25
+ * Schedules an in-place content replacement for an existing file and returns a `WritableHandle`.
26
+ * A rejected Session (e.g. the target does not exist) throws `AuthoringError`.
27
+ *
28
+ * @example
29
+ * replaceContent("src/config.json", '{ "version": "2.0.0" }');
30
+ */
31
+ export declare function replaceContent(path: string, content: string): WritableHandle;
32
+ /**
33
+ * Schedules a file deletion. Idempotent — deleting an absent file is not an error.
34
+ *
35
+ * @example
36
+ * remove("src/legacy.ts");
37
+ */
38
+ export declare function remove(path: string): void;
39
+ /**
40
+ * Schedules a file rename (basename only) and returns a `WritableHandle` for the new path.
41
+ * Renaming onto an existing path is rejected at the engine seam unless
42
+ * `{ force: true }` is passed (overwrite-on-collision, ADR-0017 fail-closed) — a
43
+ * rejected Session throws `AuthoringError`.
44
+ *
45
+ * @example
46
+ * rename("src/foo.ts", "bar.ts");
47
+ */
48
+ export declare function rename(path: string, newName: string, opts?: {
49
+ force?: boolean;
50
+ }): WritableHandle;
51
+ /**
52
+ * Schedules a file move to a different directory and returns a `WritableHandle`.
53
+ * Moving onto an existing destination is rejected at the engine seam unless
54
+ * `{ force: true }` is passed (overwrite-on-collision, ADR-0017 fail-closed) — a
55
+ * rejected Session throws `AuthoringError`.
56
+ * A move whose destination equals its source is a no-op, never a collision
57
+ * (ADR-0017 self-move amendment).
58
+ *
59
+ * @example
60
+ * move("src/utils/helper.ts", "src/shared");
61
+ */
62
+ export declare function move(path: string, toDir: string, opts?: {
63
+ force?: boolean;
64
+ }): WritableHandle;
65
+ /**
66
+ * Schedules a file copy to a new path and returns a `WritableHandle` for the destination.
67
+ * Copying onto an existing destination is rejected at the engine seam unless
68
+ * `{ force: true }` is passed (overwrite-on-collision, ADR-0017 fail-closed) — a
69
+ * rejected Session throws `AuthoringError`.
70
+ *
71
+ * @example
72
+ * copy("src/template.ts", "src/generated/output.ts");
73
+ */
74
+ export declare function copy(from: string, to: string, opts?: {
75
+ force?: boolean;
76
+ }): WritableHandle;
77
+ /**
78
+ * Returns the Directives still pending in the active Session's buffer, rendered in
79
+ * author vocabulary.
80
+ *
81
+ * Only what is still pending appears — the honest answer to "what will this Session
82
+ * still emit", not "what has this Session emitted in total." A `read()` (or any flush)
83
+ * drains the pending buffer, so Directives already sent no longer appear.
84
+ *
85
+ * Entries carry verb and path — no content or byte preview — plus an optional `kind`
86
+ * (`"rendered" | "copied"`) present only on the package-local-read verbs whose transport
87
+ * is classified: `create` (inline or `templateFile`, `kind: "rendered"`) and `copyIn`
88
+ * (`kind: "copied"`). The tree→tree `copy` verb also materializes content but predates
89
+ * this axis and is never package-local-read/classified, so it carries no `kind`.
90
+ *
91
+ * Call it inside an active factory invocation, like every other `./commons` verb.
92
+ *
93
+ * @example
94
+ * const run = () => {
95
+ * create("src/index.ts", { template: "export const version = '1.0.0';", options: {} });
96
+ * find("src/legacy.ts").remove();
97
+ * for (const entry of pendingDirectives()) {
98
+ * console.log(entry.verb, entry.path); // "create src/index.ts", then "remove src/legacy.ts"
99
+ * }
100
+ * };
101
+ *
102
+ * @throws When called outside an active factory invocation, propagates the standard
103
+ * error — authoring verbs "can only be used while a schematic is running" (ADR-0026: the
104
+ * message no longer enumerates individual verbs, so it covers `pendingDirectives` too).
105
+ */
106
+ export declare function pendingDirectives(): PendingDirective[];
@@ -0,0 +1,202 @@
1
+ // The authoring verbs behind the commons facade: each buffers its directive on the active
2
+ // run's Session and returns the handle for the path it leaves behind.
3
+ import { posix } from "node:path";
4
+ import { currentContext } from "../run-factory/context.js";
5
+ import { forceEntry } from "../../../domain/services/directive-factory.js";
6
+ import { renderPendingDirectives } from "../pending-directives/index.js";
7
+ // Pure factory for WritableHandle. Returned by every write verb.
8
+ function buildWritableHandle(path) {
9
+ return {
10
+ read() {
11
+ const { session } = currentContext();
12
+ return session.read(path);
13
+ },
14
+ replaceContent(content) {
15
+ const { session, factory } = currentContext();
16
+ session.buffer(factory.modify({ path, content }));
17
+ return buildWritableHandle(path);
18
+ },
19
+ rename(newName, opts) {
20
+ const { session, factory } = currentContext();
21
+ session.buffer(factory.rename({ path, newName, ...forceEntry(opts?.force) }));
22
+ const dir = posix.dirname(path);
23
+ return buildWritableHandle(dir === "." ? newName : posix.join(dir, newName));
24
+ },
25
+ move(toDir, opts) {
26
+ const { session, factory } = currentContext();
27
+ session.buffer(factory.move({ path, toDir, ...forceEntry(opts?.force) }));
28
+ return buildWritableHandle(posix.join(toDir, posix.basename(path)));
29
+ },
30
+ copy(to, opts) {
31
+ const { session, factory } = currentContext();
32
+ session.buffer(factory.copy({ from: path, to, ...forceEntry(opts?.force) }));
33
+ return buildWritableHandle(to);
34
+ },
35
+ };
36
+ }
37
+ // FoundHandle factory. Returned exclusively by find(). Has remove().
38
+ function buildFoundHandle(path) {
39
+ return {
40
+ read() {
41
+ const { session } = currentContext();
42
+ return session.read(path);
43
+ },
44
+ replaceContent(content) {
45
+ const { session, factory } = currentContext();
46
+ session.buffer(factory.modify({ path, content }));
47
+ return buildWritableHandle(path);
48
+ },
49
+ rename(newName, opts) {
50
+ const { session, factory } = currentContext();
51
+ session.buffer(factory.rename({ path, newName, ...forceEntry(opts?.force) }));
52
+ const dir = posix.dirname(path);
53
+ return buildWritableHandle(dir === "." ? newName : posix.join(dir, newName));
54
+ },
55
+ move(toDir, opts) {
56
+ const { session, factory } = currentContext();
57
+ session.buffer(factory.move({ path, toDir, ...forceEntry(opts?.force) }));
58
+ return buildWritableHandle(posix.join(toDir, posix.basename(path)));
59
+ },
60
+ copy(to, opts) {
61
+ const { session, factory } = currentContext();
62
+ session.buffer(factory.copy({ from: path, to, ...forceEntry(opts?.force) }));
63
+ return buildWritableHandle(to);
64
+ },
65
+ remove() {
66
+ const { session, factory } = currentContext();
67
+ session.buffer(factory.remove({ path }));
68
+ },
69
+ };
70
+ }
71
+ /**
72
+ * Locates an existing file and returns a `FoundHandle` for reading or removing it.
73
+ *
74
+ * `read()` resolves `string | undefined`: the content, `undefined` when the path does not
75
+ * exist, or `""` when the file exists but is empty. Branch on the three outcomes with strict
76
+ * `=== undefined` / `=== ""` — NEVER `if (!content)`, which would merge `undefined`, `""`,
77
+ * `"0"` and `"false"` and reintroduce the absent-vs-empty bug. Prefer `classifyContent()`
78
+ * over manual comparisons — it names the trichotomy and gives exhaustive-switch parity.
79
+ *
80
+ * @example
81
+ * const c = await find("src/config.ts").read();
82
+ * if (c === undefined) create("src/config.ts", { template, options });
83
+ * else if (c === "") replaceContent("src/config.ts", seedContent);
84
+ * else replaceContent("src/config.ts", patch(c));
85
+ */
86
+ export function find(path) {
87
+ return buildFoundHandle(path);
88
+ }
89
+ // `template` receives the defaulted options and runs after the active run is resolved, so a
90
+ // call outside a run fails before any template file is read.
91
+ export function create(path, opts, template) {
92
+ const { session, factory } = currentContext();
93
+ // The wire requires `options`, and an omitted bag (untyped callers) would vanish in JSON and
94
+ // fail the batch's round-trip check as unrepresentable content.
95
+ const options = opts.options === undefined ? {} : opts.options;
96
+ session.buffer(factory.create({
97
+ pathTemplate: path,
98
+ template: template(options),
99
+ options,
100
+ ...forceEntry(opts.force),
101
+ }));
102
+ return buildWritableHandle(path);
103
+ }
104
+ /**
105
+ * Schedules an in-place content replacement for an existing file and returns a `WritableHandle`.
106
+ * A rejected Session (e.g. the target does not exist) throws `AuthoringError`.
107
+ *
108
+ * @example
109
+ * replaceContent("src/config.json", '{ "version": "2.0.0" }');
110
+ */
111
+ export function replaceContent(path, content) {
112
+ const { session, factory } = currentContext();
113
+ session.buffer(factory.modify({ path, content }));
114
+ return buildWritableHandle(path);
115
+ }
116
+ /**
117
+ * Schedules a file deletion. Idempotent — deleting an absent file is not an error.
118
+ *
119
+ * @example
120
+ * remove("src/legacy.ts");
121
+ */
122
+ export function remove(path) {
123
+ const { session, factory } = currentContext();
124
+ session.buffer(factory.remove({ path }));
125
+ }
126
+ /**
127
+ * Schedules a file rename (basename only) and returns a `WritableHandle` for the new path.
128
+ * Renaming onto an existing path is rejected at the engine seam unless
129
+ * `{ force: true }` is passed (overwrite-on-collision, ADR-0017 fail-closed) — a
130
+ * rejected Session throws `AuthoringError`.
131
+ *
132
+ * @example
133
+ * rename("src/foo.ts", "bar.ts");
134
+ */
135
+ export function rename(path, newName, opts) {
136
+ const { session, factory } = currentContext();
137
+ session.buffer(factory.rename({ path, newName, ...forceEntry(opts?.force) }));
138
+ const dir = posix.dirname(path);
139
+ return buildWritableHandle(dir === "." ? newName : posix.join(dir, newName));
140
+ }
141
+ /**
142
+ * Schedules a file move to a different directory and returns a `WritableHandle`.
143
+ * Moving onto an existing destination is rejected at the engine seam unless
144
+ * `{ force: true }` is passed (overwrite-on-collision, ADR-0017 fail-closed) — a
145
+ * rejected Session throws `AuthoringError`.
146
+ * A move whose destination equals its source is a no-op, never a collision
147
+ * (ADR-0017 self-move amendment).
148
+ *
149
+ * @example
150
+ * move("src/utils/helper.ts", "src/shared");
151
+ */
152
+ export function move(path, toDir, opts) {
153
+ const { session, factory } = currentContext();
154
+ session.buffer(factory.move({ path, toDir, ...forceEntry(opts?.force) }));
155
+ return buildWritableHandle(posix.join(toDir, posix.basename(path)));
156
+ }
157
+ /**
158
+ * Schedules a file copy to a new path and returns a `WritableHandle` for the destination.
159
+ * Copying onto an existing destination is rejected at the engine seam unless
160
+ * `{ force: true }` is passed (overwrite-on-collision, ADR-0017 fail-closed) — a
161
+ * rejected Session throws `AuthoringError`.
162
+ *
163
+ * @example
164
+ * copy("src/template.ts", "src/generated/output.ts");
165
+ */
166
+ export function copy(from, to, opts) {
167
+ const { session, factory } = currentContext();
168
+ session.buffer(factory.copy({ from, to, ...forceEntry(opts?.force) }));
169
+ return buildWritableHandle(to);
170
+ }
171
+ /**
172
+ * Returns the Directives still pending in the active Session's buffer, rendered in
173
+ * author vocabulary.
174
+ *
175
+ * Only what is still pending appears — the honest answer to "what will this Session
176
+ * still emit", not "what has this Session emitted in total." A `read()` (or any flush)
177
+ * drains the pending buffer, so Directives already sent no longer appear.
178
+ *
179
+ * Entries carry verb and path — no content or byte preview — plus an optional `kind`
180
+ * (`"rendered" | "copied"`) present only on the package-local-read verbs whose transport
181
+ * is classified: `create` (inline or `templateFile`, `kind: "rendered"`) and `copyIn`
182
+ * (`kind: "copied"`). The tree→tree `copy` verb also materializes content but predates
183
+ * this axis and is never package-local-read/classified, so it carries no `kind`.
184
+ *
185
+ * Call it inside an active factory invocation, like every other `./commons` verb.
186
+ *
187
+ * @example
188
+ * const run = () => {
189
+ * create("src/index.ts", { template: "export const version = '1.0.0';", options: {} });
190
+ * find("src/legacy.ts").remove();
191
+ * for (const entry of pendingDirectives()) {
192
+ * console.log(entry.verb, entry.path); // "create src/index.ts", then "remove src/legacy.ts"
193
+ * }
194
+ * };
195
+ *
196
+ * @throws When called outside an active factory invocation, propagates the standard
197
+ * error — authoring verbs "can only be used while a schematic is running" (ADR-0026: the
198
+ * message no longer enumerates individual verbs, so it covers `pendingDirectives` too).
199
+ */
200
+ export function pendingDirectives() {
201
+ return renderPendingDirectives(currentContext().session.pendingDirectives());
202
+ }
@@ -1,5 +1,5 @@
1
- import type { Directive } from "../core/wire.ts";
2
- import type { AuthoringVerb } from "../core/authoring-error.ts";
1
+ import type { Directive } from "../../../domain/valueobjects/wire.ts";
2
+ import type { AuthoringVerb } from "../../../domain/valueobjects/authoring-error.ts";
3
3
  /**
4
4
  * One pending Directive rendered in author vocabulary: the author verb and the primary path the
5
5
  * operation targets. Carries no content or byte preview.
@@ -1,7 +1,7 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
- import type { EngineClient } from "./engine-client.ts";
3
- import { DirectiveFactory } from "./directive-factory.ts";
4
- import { Session } from "./session.ts";
1
+ import type { EngineClient } from "../../../domain/ports/engine-client.ts";
2
+ import { type PackageFiles } from "../../../domain/ports/package-files.ts";
3
+ import { DirectiveFactory } from "../../../domain/services/directive-factory.ts";
4
+ import { Session } from "../../../domain/aggregates/session/session.ts";
5
5
  export interface DialectRegistry {
6
6
  register(handle: {
7
7
  readonly settle: () => Promise<void>;
@@ -18,13 +18,13 @@ export interface SessionContext {
18
18
  packageAnchors?: {
19
19
  packageDir: string;
20
20
  };
21
+ packageFiles?: PackageFiles;
21
22
  }
22
- export declare const SESSION_ALS_REGISTRY_KEY = "@pbuilder/sdk:core/context#session-als";
23
- export declare function resolveSessionAls(slotValue: unknown): AsyncLocalStorage<SessionContext>;
24
23
  export declare function isDefineFactoryWrapped(value: unknown): boolean;
25
24
  export declare function currentContext(): SessionContext;
26
25
  export declare function requirePackageAnchors(missingAnchorMessage: string): {
27
26
  packageDir: string;
27
+ files: PackageFiles;
28
28
  };
29
29
  /**
30
30
  * Builds a SessionContext and runs the factory inside it, flushing buffered
@@ -40,12 +40,14 @@ export declare function requirePackageAnchors(missingAnchorMessage: string): {
40
40
  * `defineFactory(fn)` call is the untyped opt-out and runs exactly as before this option
41
41
  * existed (REQ-TFO-02).
42
42
  *
43
- * @internal Sanctioned callers only — `src/core/**`, `src/testing/**`, and
44
- * `src/conformance/**` (enforced by `test/fitness/fit-29-sanctioned-definefactory-caller.test.ts`).
43
+ * @internal Sanctioned callers only — `src/core/**` and
44
+ * `src/bounded-contexts/authoring/testing/**` (enforced by `test/fitness/fit-29-sanctioned-definefactory-caller.test.ts`).
45
45
  * Author code never imports this directly; it calls `runFactoryForTest` (test-time) or the
46
46
  * future production runner instead — both wrap this SAME seam internally.
47
47
  *
48
48
  * @param fn - the authoring logic; receives the resolved, schema-validated input.
49
+ * @param options.packageFiles - the package-files adapter the two reads go through; every
50
+ * caller passes it with `packageDir`.
49
51
  * @param options.packageDir - opts into schema-derived input validation and reserved-name
50
52
  * enforcement (both structural checks against this package's `schema.json`/sibling files).
51
53
  * Pass `import.meta.dir` (a directory) — NOT `import.meta.url` (a file URL; a common
@@ -77,7 +79,8 @@ export declare function requirePackageAnchors(missingAnchorMessage: string): {
77
79
  * await runner(input, { client });
78
80
  */
79
81
  export declare function defineFactory<O>(fn: (o: O) => void | Promise<void>, options?: {
80
- packageDir?: string | URL;
82
+ packageDir: string | URL;
83
+ packageFiles: PackageFiles;
81
84
  }): (o: O, deps: {
82
85
  client: EngineClient;
83
86
  }) => Promise<void>;
@@ -1,18 +1,16 @@
1
1
  // KIT-05: Ambient SessionContext via AsyncLocalStorage.
2
2
  // defineFactory returns a runner that builds the context and runs fn inside the ALS.
3
3
  // The EngineClient is injected by the caller (test passes the fake) — no module global.
4
- import { AsyncLocalStorage } from "node:async_hooks";
5
- import { readFileSync } from "node:fs";
6
4
  import { fileURLToPath } from "node:url";
7
- import { relative } from "node:path";
8
- import { DirectiveFactory } from "./directive-factory.js";
9
- import { Session } from "./session.js";
10
- import { AuthoringError, invalidInput } from "./authoring-error.js";
11
- import { schemaPathFor, findReservedSibling } from "./schema/schema-discovery.js";
12
- import { parseSchema, SchemaParseFailure, formatLocator } from "./schema/schema-parse.js";
13
- import { validateInput } from "./schema/schema-validate.js";
14
- import { rejectionFor, rejectionForReservedName } from "./schema/input-rejection.js";
15
- import { isErrnoException } from "./fs-errors.js";
5
+ import { PackageReadFailure } from "../../../domain/ports/package-files.js";
6
+ import { DirectiveFactory } from "../../../domain/services/directive-factory.js";
7
+ import { Session } from "../../../domain/aggregates/session/session.js";
8
+ import { invalidInput } from "../../../domain/valueobjects/authoring-error.js";
9
+ import { currentRun, getSessionAls } from "../../../domain/services/run-context.js";
10
+ import { schemaPathFor } from "../../../domain/services/schema/schema-files.js";
11
+ import { parseSchema, SchemaParseFailure, formatLocator } from "../../../domain/services/schema/schema-parse.js";
12
+ import { validateInput } from "../../../domain/services/schema/schema-validate.js";
13
+ import { rejectionFor, rejectionForReservedName } from "../../../domain/services/schema/input-rejection.js";
16
14
  class DialectRegistryImpl {
17
15
  #handles = [];
18
16
  register(handle) {
@@ -31,55 +29,6 @@ class DialectRegistryImpl {
31
29
  }
32
30
  }
33
31
  }
34
- // context-singleton-fix (REQ-MIS-01/02, design §4.3): a module-scope `als` is realm-local —
35
- // when `dist/bin/pbuilder-runner.js` and a src-relative `.ts` factory co-execute in one Bun
36
- // process, each resolves its OWN copy of this module and its OWN `als`, so a Session entered in
37
- // one realm is invisible to `currentContext()` in the other. The store is therefore parked at
38
- // `globalThis[Symbol.for(SESSION_ALS_REGISTRY_KEY)]` — a process-singleton every realm resolving
39
- // this file reaches via the SAME well-known key — lazily installed on first access.
40
- export const SESSION_ALS_REGISTRY_KEY = "@pbuilder/sdk:core/context#session-als";
41
- function isAsyncLocalStorageLike(value) {
42
- return (typeof value === "object" &&
43
- value !== null &&
44
- typeof value.run === "function" &&
45
- typeof value.getStore === "function");
46
- }
47
- // PURE 3-state decision (design §4.3, rev 3 purity split) — no globalThis, no side effects.
48
- // `undefined` (slot absent) -> fresh ALS; a duck-valid ALS-shaped value (cross-realm reuse)
49
- // -> returned verbatim, SAME instance; anything else -> fail LOUD (occupied-slot contract,
50
- // ADR-01 addendum): a foreign/corrupted occupant under this well-known key must never be
51
- // trusted or silently clobbered. Plain `Error` (NOT AuthoringError) — this is
52
- // environment/integration corruption, not author-input misuse; the closed reason enum has no
53
- // member for it (mirrors `validateAtRunBoundary`'s read-path posture).
54
- export function resolveSessionAls(slotValue) {
55
- if (slotValue === undefined) {
56
- return new AsyncLocalStorage();
57
- }
58
- if (isAsyncLocalStorageLike(slotValue)) {
59
- return slotValue;
60
- }
61
- throw new Error(`[pbuilder] run-context registry slot Symbol.for("${SESSION_ALS_REGISTRY_KEY}") is occupied by an ` +
62
- `incompatible value (expected an AsyncLocalStorage, got ${typeof slotValue}) — another module ` +
63
- "may have polluted globalThis");
64
- }
65
- // IMPURE thin shell: reads the shared slot, delegates the 3-state decision to the pure
66
- // `resolveSessionAls`, and installs the resolved ALS ONLY when the slot was absent — a frozen
67
- // descriptor (non-enumerable, non-configurable, non-writable) so REQ-MIS-01.2's no-pollution
68
- // guarantee holds and the slot can never be silently reassigned after first install.
69
- function getSessionAls() {
70
- const registryKey = Symbol.for(SESSION_ALS_REGISTRY_KEY);
71
- const slot = globalThis[registryKey];
72
- const resolved = resolveSessionAls(slot);
73
- if (slot === undefined) {
74
- Object.defineProperty(globalThis, registryKey, {
75
- value: resolved,
76
- writable: false,
77
- enumerable: false,
78
- configurable: false,
79
- });
80
- }
81
- return resolved;
82
- }
83
32
  // ADR-04 (stdio-engine-client, REQ-RUN-06): a non-enumerable, non-configurable brand
84
33
  // stamped on defineFactory's returned wrapper — arity-independent double-wrap detection
85
34
  // (RUN-06.2 rejected V1's arity-sniffing: an author's bare `(o, unused) => {}` factory must
@@ -90,67 +39,46 @@ const DEFINE_FACTORY_BRAND = Symbol("pbuilder.defineFactory.wrapped");
90
39
  export function isDefineFactoryWrapped(value) {
91
40
  return typeof value === "function" && Reflect.get(value, DEFINE_FACTORY_BRAND) === true;
92
41
  }
93
- // REQ-AEC-02.1 (2.4): a verb called outside any defineFactory Session is SDK-side misuse,
94
- // independent of any engine round-trip — an AuthoringError{authoring-rejected,outside-session},
95
- // not a plain Error. The prose lives in authoring-error.ts's outside-session message template
96
- // (the third template, REQ-AEC-06); this is the one call site that reaches it.
97
42
  export function currentContext() {
98
- const ctx = getSessionAls().getStore();
99
- if (ctx === undefined) {
100
- throw new AuthoringError({
101
- verb: undefined,
102
- path: undefined,
103
- reason: "outside-session",
104
- appliedCount: 0,
105
- });
106
- }
107
- return ctx;
43
+ return currentRun();
108
44
  }
109
45
  // The one gate every package-local read verb (`scaffold`/`copyIn`/`create({templateFile})`)
110
- // passes through: returns the run's sole anchor or throws the verb's own
111
- // no-resolution-anchor `invalid-input` when the run opted out of `packageDir`.
46
+ // passes through: returns the run's sole anchor and the adapter that reads under it, or
47
+ // throws the verb's own no-resolution-anchor `invalid-input` when the run opted out of
48
+ // `packageDir`.
112
49
  export function requirePackageAnchors(missingAnchorMessage) {
113
- const { packageAnchors } = currentContext();
50
+ const { packageAnchors, packageFiles } = currentContext();
114
51
  if (packageAnchors === undefined) {
115
52
  throw invalidInput(missingAnchorMessage);
116
53
  }
117
- return packageAnchors;
54
+ // defineFactory sets both together; an anchor without its adapter is an SDK defect, so it
55
+ // must not read as the author forgetting `packageDir`.
56
+ if (packageFiles === undefined) {
57
+ throw new Error("[pbuilder] internal error: the run has a package directory but no package-files adapter");
58
+ }
59
+ return { packageDir: packageAnchors.packageDir, files: packageFiles };
118
60
  }
119
61
  // packageDir accepts a directory URL (import.meta.dir is a Bun string; import.meta.url
120
62
  // is a file URL some authors may pass instead) — never a file URL pointing at a file.
121
63
  function resolvePackageDir(packageDir) {
122
64
  return typeof packageDir === "string" ? packageDir : fileURLToPath(packageDir);
123
65
  }
124
- // `<dir>` token (all runtime warning/error literals, Executor Context pin): project-root-
125
- // relative, never absolute — tests run from the repo root, so this is deterministic and
126
- // leaks no absolute path.
127
- function relativeDir(packageDir) {
128
- return relative(process.cwd(), packageDir);
129
- }
130
- // Author-vocabulary description of a non-ENOENT read failure — never the raw errno
131
- // message (no-echo). ENOENT is handled by its own opt-out branch and never reaches this.
132
- function describeReadFailure(err) {
133
- if (isErrnoException(err)) {
134
- if (err.code === "EACCES" || err.code === "EPERM")
135
- return "permission denied";
136
- if (err.code === "EISDIR")
137
- return "is a directory, not a file";
138
- }
139
- return "unreadable";
66
+ function relativeDir({ files, dir }) {
67
+ return files.relativeToCwd(dir);
140
68
  }
141
69
  // RBV-05.1 runtime literal (slices load-bearing literals, design §4.4): ONE literal covers
142
70
  // both present-but-unparseable AND invalid-shape schemas, `<problem>` distinguishing them;
143
71
  // also reused (with no locator) for non-ENOENT read failures (constraint 4) — same
144
72
  // "could not be read" vocabulary, since both are "the run boundary could not establish
145
73
  // what this factory's schema contract is" failures.
146
- function malformedSchemaMessage(packageDir, problem, line, column) {
147
- return `[pbuilder] factory at ${relativeDir(packageDir)}: schema.json could not be read: ${problem} ${formatLocator(line, column)}`;
74
+ function malformedSchemaMessage(pkg, problem, line, column) {
75
+ return `[pbuilder] factory at ${relativeDir(pkg)}: schema.json could not be read: ${problem} ${formatLocator(line, column)}`;
148
76
  }
149
- function noSchemaWarning(packageDir) {
150
- return `[pbuilder] factory at ${relativeDir(packageDir)}: no schema.json found — running WITHOUT schema-derived input validation`;
77
+ function noSchemaWarning(pkg) {
78
+ return `[pbuilder] factory at ${relativeDir(pkg)}: no schema.json found — running WITHOUT schema-derived input validation`;
151
79
  }
152
- function emptySchemaWarning(packageDir) {
153
- return `[pbuilder] factory at ${relativeDir(packageDir)}: schema.json declares zero properties — no input validation performed`;
80
+ function emptySchemaWarning(pkg) {
81
+ return `[pbuilder] factory at ${relativeDir(pkg)}: schema.json declares zero properties — no input validation performed`;
154
82
  }
155
83
  // Pre-`als.run` schema-conformance check (ADR-0029, REQ-RBV-01/03/05): validates BEFORE
156
84
  // SessionContext is built, so a rejection throws with nothing buffered — all-or-nothing holds
@@ -162,18 +90,20 @@ function emptySchemaWarning(packageDir) {
162
90
  // a corrupt/unreadable schema file, only for author-input rejections (invalid-input /
163
91
  // reserved-name). A parsed-but-empty schema (zero declared properties) is distinct from no
164
92
  // schema at all (REQ-RBV-05.2) — it warns and skips validation, it does not opt out.
165
- function validateAtRunBoundary(packageDir, input) {
166
- const schemaPath = schemaPathFor(packageDir);
93
+ function validateAtRunBoundary(pkg, input) {
167
94
  let raw;
168
95
  try {
169
- raw = readFileSync(schemaPath, "utf-8");
96
+ raw = pkg.files.readText(schemaPathFor(pkg.dir));
170
97
  }
171
98
  catch (err) {
172
- if (isErrnoException(err) && err.code === "ENOENT") {
173
- console.warn(noSchemaWarning(packageDir));
174
- return;
99
+ if (err instanceof PackageReadFailure) {
100
+ throw new Error(malformedSchemaMessage(pkg, err.problem, undefined, undefined));
175
101
  }
176
- throw new Error(malformedSchemaMessage(packageDir, describeReadFailure(err), undefined, undefined));
102
+ throw err;
103
+ }
104
+ if (raw === undefined) {
105
+ console.warn(noSchemaWarning(pkg));
106
+ return;
177
107
  }
178
108
  let schema;
179
109
  try {
@@ -181,12 +111,12 @@ function validateAtRunBoundary(packageDir, input) {
181
111
  }
182
112
  catch (err) {
183
113
  if (err instanceof SchemaParseFailure) {
184
- throw new Error(malformedSchemaMessage(packageDir, err.problem, err.line, err.column));
114
+ throw new Error(malformedSchemaMessage(pkg, err.problem, err.line, err.column));
185
115
  }
186
116
  throw err;
187
117
  }
188
118
  if (schema.properties.length === 0) {
189
- console.warn(emptySchemaWarning(packageDir));
119
+ console.warn(emptySchemaWarning(pkg));
190
120
  return;
191
121
  }
192
122
  const findings = validateInput(schema, input);
@@ -200,13 +130,15 @@ function validateAtRunBoundary(packageDir, input) {
200
130
  // resolved run-time inputs (that is REQ-RBV-01.5's territory) or from `schema.json` field
201
131
  // names (REQ-RLN-01.4). Non-ENOENT scan failures fail closed, same read-path posture as
202
132
  // `validateAtRunBoundary` (constraint 4).
203
- function checkReservedNames(packageDir) {
133
+ function checkReservedNames(pkg) {
204
134
  let reserved;
205
135
  try {
206
- reserved = findReservedSibling(packageDir);
136
+ reserved = pkg.files.findReservedSibling(pkg.dir);
207
137
  }
208
138
  catch (err) {
209
- throw new Error(`[pbuilder] factory at ${relativeDir(packageDir)}: could not scan for reserved lifecycle names: ${describeReadFailure(err)}`);
139
+ if (!(err instanceof PackageReadFailure))
140
+ throw err;
141
+ throw new Error(`[pbuilder] factory at ${relativeDir(pkg)}: could not scan for reserved lifecycle names: ${err.problem}`);
210
142
  }
211
143
  if (reserved !== undefined) {
212
144
  throw rejectionForReservedName(reserved);
@@ -226,12 +158,14 @@ function checkReservedNames(packageDir) {
226
158
  * `defineFactory(fn)` call is the untyped opt-out and runs exactly as before this option
227
159
  * existed (REQ-TFO-02).
228
160
  *
229
- * @internal Sanctioned callers only — `src/core/**`, `src/testing/**`, and
230
- * `src/conformance/**` (enforced by `test/fitness/fit-29-sanctioned-definefactory-caller.test.ts`).
161
+ * @internal Sanctioned callers only — `src/core/**` and
162
+ * `src/bounded-contexts/authoring/testing/**` (enforced by `test/fitness/fit-29-sanctioned-definefactory-caller.test.ts`).
231
163
  * Author code never imports this directly; it calls `runFactoryForTest` (test-time) or the
232
164
  * future production runner instead — both wrap this SAME seam internally.
233
165
  *
234
166
  * @param fn - the authoring logic; receives the resolved, schema-validated input.
167
+ * @param options.packageFiles - the package-files adapter the two reads go through; every
168
+ * caller passes it with `packageDir`.
235
169
  * @param options.packageDir - opts into schema-derived input validation and reserved-name
236
170
  * enforcement (both structural checks against this package's `schema.json`/sibling files).
237
171
  * Pass `import.meta.dir` (a directory) — NOT `import.meta.url` (a file URL; a common
@@ -265,19 +199,22 @@ function checkReservedNames(packageDir) {
265
199
  export function defineFactory(fn, options) {
266
200
  const wrapped = async (o, { client }) => {
267
201
  let packageAnchors;
268
- if (options?.packageDir !== undefined) {
269
- const resolvedDir = resolvePackageDir(options.packageDir);
202
+ let packageFiles;
203
+ if (options !== undefined) {
204
+ const pkg = { files: options.packageFiles, dir: resolvePackageDir(options.packageDir) };
270
205
  // REQ-RBV-06: exactly TWO reads, in this order — reserved-name check, then schema
271
206
  // validation. No third (containment-ceiling) read exists (ADR-0077 §C).
272
- checkReservedNames(resolvedDir);
273
- validateAtRunBoundary(resolvedDir, o);
274
- packageAnchors = { packageDir: resolvedDir };
207
+ checkReservedNames(pkg);
208
+ validateAtRunBoundary(pkg, o);
209
+ packageAnchors = { packageDir: pkg.dir };
210
+ packageFiles = pkg.files;
275
211
  }
276
212
  const ctx = {
277
213
  session: new Session(client),
278
214
  factory: new DirectiveFactory(),
279
215
  dialects: new DialectRegistryImpl(),
280
216
  packageAnchors,
217
+ packageFiles,
281
218
  };
282
219
  // ADR-01: no `finally` — success (commit) and failure (discard) are distinct paths.
283
220
  // The final flush is INSIDE the try, so a flush-time emit rejection (already an
@@ -293,7 +230,9 @@ export function defineFactory(fn, options) {
293
230
  await ctx.session.flush();
294
231
  await ctx.session.commit();
295
232
  }
296
- catch (err) {
233
+ catch (thrown) {
234
+ // A thrown `undefined` or `null` would read as success to anyone testing the error.
235
+ const err = thrown ?? new Error(`the factory threw ${String(thrown)}`);
297
236
  // REQ-10: double-fault preservation. A discard() rejection (E2) must never replace
298
237
  // or swallow the factory's original error (E1) — attach it as cause and re-throw E1.
299
238
  try {