@aestheticfunction/dspack-gen 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.
Files changed (79) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/LICENSE +202 -0
  3. package/README.md +147 -0
  4. package/dist/adapters/anthropic.d.ts +16 -0
  5. package/dist/adapters/anthropic.js +71 -0
  6. package/dist/adapters/fake.d.ts +21 -0
  7. package/dist/adapters/fake.js +32 -0
  8. package/dist/adapters/index.d.ts +6 -0
  9. package/dist/adapters/index.js +11 -0
  10. package/dist/adapters/ollama.d.ts +16 -0
  11. package/dist/adapters/ollama.js +91 -0
  12. package/dist/adapters/types.d.ts +66 -0
  13. package/dist/adapters/types.js +47 -0
  14. package/dist/audit/report.d.ts +84 -0
  15. package/dist/audit/report.js +62 -0
  16. package/dist/cli.d.ts +2 -0
  17. package/dist/cli.js +182 -0
  18. package/dist/core/compiler.d.ts +39 -0
  19. package/dist/core/compiler.js +142 -0
  20. package/dist/core/contract.d.ts +175 -0
  21. package/dist/core/contract.js +62 -0
  22. package/dist/core/generation-schema.d.ts +32 -0
  23. package/dist/core/generation-schema.js +85 -0
  24. package/dist/core/index.d.ts +12 -0
  25. package/dist/core/index.js +12 -0
  26. package/dist/core/lint/findings.d.ts +46 -0
  27. package/dist/core/lint/findings.js +28 -0
  28. package/dist/core/lint/index.d.ts +7 -0
  29. package/dist/core/lint/index.js +67 -0
  30. package/dist/core/lint/rules.d.ts +27 -0
  31. package/dist/core/lint/rules.js +212 -0
  32. package/dist/core/lint/vocabulary.d.ts +13 -0
  33. package/dist/core/lint/vocabulary.js +71 -0
  34. package/dist/core/lint/walk.d.ts +14 -0
  35. package/dist/core/lint/walk.js +30 -0
  36. package/dist/core/surface-schema.d.ts +78 -0
  37. package/dist/core/surface-schema.js +85 -0
  38. package/dist/eval/assert.d.ts +2 -0
  39. package/dist/eval/assert.js +50 -0
  40. package/dist/eval/run.d.ts +2 -0
  41. package/dist/eval/run.js +60 -0
  42. package/dist/eval/runner.d.ts +36 -0
  43. package/dist/eval/runner.js +310 -0
  44. package/dist/eval/types.d.ts +143 -0
  45. package/dist/eval/types.js +1 -0
  46. package/dist/index.d.ts +15 -0
  47. package/dist/index.js +14 -0
  48. package/dist/repair/render.d.ts +20 -0
  49. package/dist/repair/render.js +28 -0
  50. package/dist/run/orchestrator.d.ts +90 -0
  51. package/dist/run/orchestrator.js +191 -0
  52. package/dist/serve.d.ts +25 -0
  53. package/dist/serve.js +144 -0
  54. package/package.json +76 -0
  55. package/src/adapters/anthropic.ts +88 -0
  56. package/src/adapters/fake.ts +32 -0
  57. package/src/adapters/index.ts +13 -0
  58. package/src/adapters/ollama.ts +123 -0
  59. package/src/adapters/types.ts +91 -0
  60. package/src/audit/report.ts +139 -0
  61. package/src/cli.ts +191 -0
  62. package/src/core/compiler.ts +205 -0
  63. package/src/core/contract.ts +205 -0
  64. package/src/core/generation-schema.ts +99 -0
  65. package/src/core/index.ts +12 -0
  66. package/src/core/lint/findings.ts +80 -0
  67. package/src/core/lint/index.ts +80 -0
  68. package/src/core/lint/rules.ts +320 -0
  69. package/src/core/lint/vocabulary.ts +80 -0
  70. package/src/core/lint/walk.ts +44 -0
  71. package/src/core/surface-schema.ts +86 -0
  72. package/src/eval/assert.ts +55 -0
  73. package/src/eval/run.ts +62 -0
  74. package/src/eval/runner.ts +366 -0
  75. package/src/eval/types.ts +143 -0
  76. package/src/index.ts +15 -0
  77. package/src/repair/render.ts +68 -0
  78. package/src/run/orchestrator.ts +272 -0
  79. package/src/serve.ts +164 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 — first public release
4
+
5
+ The generation and governance pipeline for dspack contracts, previously
6
+ consumed only in-repo and via the bundled core in ds-mcp, published as a
7
+ package.
8
+
9
+ - Root entry (`@aestheticfunction/dspack-gen`): model adapters
10
+ (Ollama/Anthropic/scripted), `runPipeline` (generate → S1/S2/S3 lint →
11
+ bounded repair → protocol emission → audit report), repair-message
12
+ rendering, audit report rendering, and the eval-matrix runner.
13
+ - `/core` subpath: the zero-network, emitter-free compiler + linter
14
+ (`compileContext`, `lintSurface`, generation-schema builder) that ds-mcp
15
+ consumes; the boundary is test-enforced (`src/core/core-boundary.test.ts`).
16
+ - `dspack-gen` CLI bin: `context`, `lint`, `run`, `serve` (localhost NDJSON
17
+ streaming of the pipeline). Exit codes: 0 clean, 1 usage/internal,
18
+ 2 governance failure, 3 emitter gate, 4 unknown rule type.
19
+ - Requires Node >= 20. ESM only.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship made available under
36
+ the License, as indicated by a copyright notice that is included in
37
+ or attached to the work (an example is provided in the Appendix below).
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other modifications
42
+ represent, as a whole, an original work of authorship. For the purposes
43
+ of this License, Derivative Works shall not include works that remain
44
+ separable from, or merely link (or bind by name) to the interfaces of,
45
+ the Work and derivative works thereof.
46
+
47
+ "Contribution" shall mean, as defined in the Licensor's terms of
48
+ contributing, any work of authorship, including the original version of
49
+ the Work and any modifications or additions to that Work or Derivative
50
+ Works of the Work, submitted to the Licensor for inclusion in the Work
51
+ by the copyright owner or by an individual or Legal Entity authorized to
52
+ submit on behalf of the copyright owner. For the purposes of this
53
+ definition, "submitted" means any form of electronic, verbal, or written
54
+ communication sent to the Licensor or its representatives, including but
55
+ not limited to communication on electronic mailing lists, source code
56
+ control systems, and issue tracking systems that are managed by, or on
57
+ behalf of, the Licensor for the purpose of discussing and improving the
58
+ Work, but excluding communication that is conspicuously marked or
59
+ otherwise designated in writing by the copyright owner as "Not a
60
+ Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
63
+ whom a Contribution has been received by the Licensor and incorporated
64
+ within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by the combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" file as part of its
107
+ distribution, You must include a readable copy of the
108
+ attribution notices contained within such NOTICE file, in
109
+ at least one of the following places: within a NOTICE text
110
+ file distributed as part of the Derivative Works; within
111
+ the Source form or documentation, if provided along with the
112
+ Derivative Works; or, within a display generated by the
113
+ Derivative Works, if and wherever such third-party notices
114
+ normally appear. The contents of the NOTICE file are for
115
+ informational purposes only and do not modify the License.
116
+ You may add Your own attribution notices within Derivative
117
+ Works that You distribute, alongside or as an addition to
118
+ the NOTICE text from the Work, provided that such additional
119
+ attribution notices cannot be construed as modifying the License.
120
+
121
+ You may add Your own license statement for Your modifications and
122
+ may provide additional grant of rights to use, copy, modify, merge,
123
+ publish, distribute, sublicense, and/or sell copies of the
124
+ Contribution(s), provided that You also comply with the conditions
125
+ stated in this License.
126
+
127
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
128
+ any Contribution intentionally submitted for inclusion in the Work
129
+ by You to the Licensor shall be under the terms and conditions of
130
+ this License, without any additional terms or conditions.
131
+ Notwithstanding the above, nothing herein shall supersede or modify
132
+ the terms of any separate license agreement you may have executed
133
+ with Licensor regarding such Contributions.
134
+
135
+ 6. Trademarks. This License does not grant permission to use the trade
136
+ names, trademarks, service marks, or product names of the Licensor,
137
+ except as required for reasonable and customary use in describing the
138
+ origin of the Work and reproducing the content of the NOTICE file.
139
+
140
+ 7. Disclaimer of Warranty. Unless required by applicable law or
141
+ agreed to in writing, Licensor provides the Work (and each
142
+ Contributor provides its Contributions) on an "AS IS" BASIS,
143
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
144
+ implied, including, without limitation, any warranties or conditions
145
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
146
+ PARTICULAR PURPOSE. You are solely responsible for determining the
147
+ appropriateness of using or reproducing the Work and assume any
148
+ risks associated with Your exercise of permissions under this License.
149
+
150
+ 8. Limitation of Liability. In no event and under no legal theory,
151
+ whether in tort (including negligence), contract, or otherwise,
152
+ unless required by applicable law (such as deliberate and grossly
153
+ negligent acts) or agreed to in writing, shall any Contributor be
154
+ liable to You for damages, including any direct, indirect, special,
155
+ incidental, or exemplary damages of any character arising as a
156
+ result of this License or out of the use or inability to use the
157
+ Work (including but not limited to damages for loss of goodwill,
158
+ work stoppage, computer failure or malfunction, or all other
159
+ commercial damages or losses), even if such Contributor has been
160
+ advised of the possibility of such damages.
161
+
162
+ 9. Accepting Warranty or Liability. While redistributing the Work or
163
+ Derivative Works thereof, You may choose to offer, and charge a fee
164
+ for, acceptance of support, warranty, indemnity, or other liability
165
+ obligations and/or rights consistent with this License. However, in
166
+ accepting such obligations, You may offer such obligations only on
167
+ Your behalf and on Your sole responsibility, not on behalf of any
168
+ other Contributor, and only if You agree to indemnify, defend, and
169
+ hold each Contributor harmless for any liability incurred by, or
170
+ claims asserted against, such Contributor by reason of your accepting
171
+ any such warranty or additional liability.
172
+
173
+ END OF TERMS AND CONDITIONS
174
+
175
+ APPENDIX: How to apply the Apache License to your work.
176
+
177
+ To apply the Apache License to your work, attach the following
178
+ boilerplate notice, with the fields enclosed by brackets "[]"
179
+ replaced with your own identifying information. (Don't include
180
+ the brackets!) The text should be enclosed in the appropriate
181
+ comment syntax for the file format please. Also, it is recommended
182
+ that a file or directory name and description of purpose be included
183
+ on the same "first page" as the copyright notice for easier
184
+ identification within third-party archives.
185
+
186
+ Copyright [yyyy] [name of copyright owner]
187
+
188
+ Licensed under the Apache License, Version 2.0 (the "License");
189
+ you may not use this file except in compliance with the License.
190
+ You may obtain a copy of the License at
191
+
192
+ http://www.apache.org/licenses/LICENSE-2.0
193
+
194
+ Unless required by applicable law or agreed to in writing, software
195
+ distributed under the License is distributed on an "AS IS" BASIS,
196
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
197
+ See the License for the specific language governing permissions and
198
+ limitations under the License.
199
+
200
+ ---
201
+
202
+ Copyright 2026 Aesthetic Function, LLC
package/README.md ADDED
@@ -0,0 +1,147 @@
1
+ # dspack-gen
2
+
3
+ **Generation + governance pipeline for [dspack](https://github.com/aestheticfunction/dspack)
4
+ contracts.** Compiles a dspack v0.3/v0.4 contract into generation context, generates dspack
5
+ surfaces via schema-constrained LLMs (Ollama local or hosted — interchangeable adapters),
6
+ lints them deterministically against the contract's governance rules, repairs bounded, emits
7
+ to protocols (A2UI and json-render via
8
+ [dspack-emit](https://github.com/aestheticfunction/dspack-emit)), and
9
+ produces a versioned audit report.
10
+
11
+ > *A2UI defines what can render. dspack defines what is correct.*
12
+
13
+ **Status: Milestones 1 and 2 complete.** Tags
14
+ [m1](https://github.com/aestheticfunction/dspack-gen/releases/tag/m1) and
15
+ [m2](https://github.com/aestheticfunction/dspack-gen/releases/tag/m2); write-ups in
16
+ [docs/m1-report.md](docs/m1-report.md) and [docs/m2-report.md](docs/m2-report.md),
17
+ findings in [docs/findings.md](docs/findings.md). M3 (dspack v0.4 and a second
18
+ contract) is in progress.
19
+ The emitter is consumed from npm as `@aestheticfunction/dspack-emit`
20
+ (renamed from `@aestheticfunction/dspack-to-a2ui` with the ADR-D2 repo
21
+ rename; the old package is deprecated with a pointer).
22
+
23
+ ## The three layers (never collapse them)
24
+
25
+ 1. **Schema** answers *"can this object exist"* — the generation schema encodes vocabulary and
26
+ shape only, never governance.
27
+ 2. **Linter** answers *"is this object correct"* — deterministic typed rules (gate S3), each
28
+ with a rationale.
29
+ 3. **Renderer** answers *"can this render"* — protocol emitters compile the surface; the
30
+ target's gates (A1–A3 for A2UI) validate the result.
31
+
32
+ ## Gates
33
+
34
+ | Gate | Check | Where |
35
+ |---|---|---|
36
+ | S1 | generic dspack surface schema | here (pre-emission) |
37
+ | S2 | contract vocabulary (components, sub-components, props, enum values, slots) | here (pre-emission) |
38
+ | S3 | governance rules (typed, deterministic, rationale-bearing) | here (pre-emission) |
39
+ | A1–A3 | schema-compile / catalog-shape / instance validation | dspack-emit (per emitted target) |
40
+
41
+ S1/S2 are checks on **any** produced surface; constrained decoding may implement S2 during
42
+ generation but the gates are always reported independently — the S0 spike found Ollama's mlx
43
+ engine silently ignoring `format`, which is exactly why.
44
+
45
+ ## CLI
46
+
47
+ ```bash
48
+ npm run context -- --dspack fixtures/shadcn.v0_4.dspack.json --intent destructive-action
49
+ # prints { system, schema, fewshot } — the compiled generation context
50
+ ```
51
+
52
+ `lint` (S1–S3) and `run` (full pipeline) land in later M1 PRs.
53
+
54
+ ### Exit codes (whole CLI surface)
55
+
56
+ | Code | Meaning |
57
+ |---|---|
58
+ | 0 | clean |
59
+ | 1 | internal / usage error |
60
+ | 2 | governance failure (any S-gate error) |
61
+ | 3 | emitter-gate failure (A1–A3 or target equivalent) |
62
+ | 4 | unknown rule type (fail-loud; never silently skipped) |
63
+
64
+ (Distinct from dspack-emit's exit codes, where `3` = strict-coverage failure and `4` =
65
+ surface-emission failure.)
66
+
67
+ ## Eval harness
68
+
69
+ ```bash
70
+ npm run eval -- --adapter fake --matrix eval/matrix.fake.json # deterministic; the CI gate
71
+ npm run eval -- --adapter live --matrix eval/matrix.json # documented live run; never CI
72
+ npm run eval -- --adapter live --matrix eval/matrix.json --out out/eval/<dir> --resume # resume an interrupted run
73
+ npm run eval:assert -- --results out/eval/fake/results.json --model <ref> --min-repair-success 0.9
74
+ ```
75
+
76
+ Every cell run goes through the same `runPipeline` as the CLI and demo; the
77
+ harness only iterates and aggregates. **Model ids live in `eval/matrix.json`
78
+ (config), never in code.** Per cell (model × prompt × repair template,
79
+ `runsPerCell` runs): schema-validity, first-attempt-violation, repair-success
80
+ (null over zero violations), end-to-end pass rates, and the count of S3-clean
81
+ surfaces refused by emitter gates (the ADR-D1 signal; `p10`–`p12` probe it
82
+ deliberately). Prompts span repair shapes — substitution, addition, deletion,
83
+ restructuring — and every prompt crosses the two ADR-7 repair templates
84
+ (`standard` vs `permit-restructuring`, a one-instruction-line delta recorded
85
+ in each audit report). One run can never kill the matrix: adapter transport failures are typed
86
+ (`failed-adapter`, with a report) and anything that still escapes a run is
87
+ contained as an explicit `error` run — visible in the distribution with a
88
+ retained `.error.json`, excluded from the model-behavior rate denominators
89
+ (`errorRuns` reports the count). `--resume` skips already-retained reports
90
+ and retries error records. Every run's audit report is retained under
91
+ `out/eval/…/reports/`; `results.json` is the artifact, `report.md` the
92
+ derived view; findings go to [docs/findings.md](docs/findings.md) at measured
93
+ strength. The hosted-model `eval:assert` threshold is the only hard eval gate
94
+ in M2; local models are report-only.
95
+
96
+ ## Library
97
+
98
+ ```bash
99
+ npm install @aestheticfunction/dspack-gen
100
+ ```
101
+
102
+ ESM only, Node >= 20. The root entry is the full pipeline:
103
+
104
+ ```ts
105
+ import { runPipeline, adapterFor } from "@aestheticfunction/dspack-gen";
106
+ import { readFileSync } from "node:fs";
107
+
108
+ const contract = JSON.parse(readFileSync("astryx.dspack.json", "utf8"));
109
+ const result = await runPipeline({
110
+ contract,
111
+ intent: "destructive-action",
112
+ prompt: "a screen to delete a project",
113
+ adapter: adapterFor("ollama:gemma3"),
114
+ });
115
+ console.log(result.report.outcome, result.exitCode);
116
+ ```
117
+
118
+ `@aestheticfunction/dspack-gen/core` is the **zero-network, emitter-free** subpath (compiler +
119
+ linter) that [ds-mcp](https://github.com/aestheticfunction/ds-mcp) consumes without breaking
120
+ its read-only/no-network security posture. The boundary is enforced by
121
+ `src/core/core-boundary.test.ts`:
122
+
123
+ ```ts
124
+ import { compileContext, lintSurface } from "@aestheticfunction/dspack-gen/core";
125
+ ```
126
+
127
+ The CLI installs as `dspack-gen` (same commands as the repo scripts): `context`,
128
+ `lint`, `run`, `serve`. Outside this repository, always pass `--dspack <contract>`
129
+ (the `serve` default path points at a repo fixture).
130
+
131
+ ## Development
132
+
133
+ ```bash
134
+ npm ci
135
+ npm test # vitest: golden context, generation-schema behavior, core boundary
136
+ npm run check:sync # contract-copy drift check vs the dspack repo (CI-run; --write re-syncs)
137
+ ```
138
+
139
+ `fixtures/shadcn.v0_4.dspack.json` is a byte copy of the spec repo's
140
+ `examples/shadcn-ui.dspack.json`; CI fails loudly if they diverge
141
+ (dspack-gen#7). After `--write`, regenerate the derived goldens (context
142
+ golden, F-fixture expected outputs, eval fake golden) and commit the sync +
143
+ regeneration together.
144
+
145
+ ## License
146
+
147
+ Apache-2.0. Copyright 2026 Aesthetic Function, LLC.
@@ -0,0 +1,16 @@
1
+ import { type FetchLike, type GenerateRequest, type GenerateResult, type GenerationAdapter } from "./types.js";
2
+ export interface AnthropicAdapterOptions {
3
+ /** Required — no default model exists in code (ADR-9 as amended). */
4
+ model: string;
5
+ /** Defaults to the SDK's environment resolution (ANTHROPIC_API_KEY, auth token, profile). */
6
+ apiKey?: string;
7
+ baseURL?: string;
8
+ fetch?: FetchLike;
9
+ }
10
+ export declare class AnthropicAdapter implements GenerationAdapter {
11
+ readonly id: string;
12
+ private readonly model;
13
+ private readonly client;
14
+ constructor(options: AnthropicAdapterOptions);
15
+ generate(request: GenerateRequest): Promise<GenerateResult>;
16
+ }
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Anthropic adapter — hosted generation via the official SDK with
3
+ * schema-constrained output (`output_config.format`, type json_schema).
4
+ *
5
+ * The generation schema is compatible by construction: depth-unrolled (the
6
+ * structured-outputs API rejects recursive schemas) and additionalProperties:
7
+ * false on every object. No sampling parameters are sent — they are removed
8
+ * on current Claude models. `stop_reason: "refusal"` is surfaced as a typed
9
+ * AdapterOutputError (never retried silently; retries are the repair loop's
10
+ * job and only for lint findings).
11
+ */
12
+ import Anthropic from "@anthropic-ai/sdk";
13
+ import { AdapterOutputError, parseJsonOutput, } from "./types.js";
14
+ export class AnthropicAdapter {
15
+ id;
16
+ model;
17
+ client;
18
+ constructor(options) {
19
+ if (!options.model)
20
+ throw new Error("AnthropicAdapter requires an explicit model id (config/env/flag)");
21
+ this.model = options.model;
22
+ this.client = new Anthropic({
23
+ apiKey: options.apiKey,
24
+ baseURL: options.baseURL,
25
+ fetch: options.fetch,
26
+ });
27
+ this.id = `anthropic:${this.model}`;
28
+ }
29
+ async generate(request) {
30
+ // Typed transport: SDK connection/API errors surface as failed-adapter
31
+ // outcomes (with a report), never as raw exceptions that kill a matrix.
32
+ let response;
33
+ try {
34
+ response = await this.client.messages.create({
35
+ model: this.model,
36
+ max_tokens: request.params?.maxTokens ?? 16000,
37
+ system: request.system,
38
+ messages: request.messages,
39
+ output_config: {
40
+ format: { type: "json_schema", schema: request.jsonSchema },
41
+ },
42
+ });
43
+ }
44
+ catch (error) {
45
+ if (error instanceof AdapterOutputError)
46
+ throw error;
47
+ const name = error instanceof Error ? error.constructor.name : "unknown";
48
+ throw new AdapterOutputError(this.id, `transport failure (${name}): ${error instanceof Error ? error.message : String(error)}`);
49
+ }
50
+ const message = response;
51
+ if (message.stop_reason === "refusal") {
52
+ throw new AdapterOutputError(this.id, "model refused the request (stop_reason: refusal)");
53
+ }
54
+ const raw = message.content
55
+ .filter((block) => block.type === "text")
56
+ .map((block) => block.text)
57
+ .join("");
58
+ if (raw === "")
59
+ throw new AdapterOutputError(this.id, "empty model output");
60
+ if (message.stop_reason === "max_tokens") {
61
+ throw new AdapterOutputError(this.id, "output truncated (stop_reason: max_tokens) — raise params.maxTokens", raw);
62
+ }
63
+ return {
64
+ json: parseJsonOutput(this.id, raw),
65
+ raw,
66
+ model: message.model,
67
+ usage: { inputTokens: message.usage.input_tokens, outputTokens: message.usage.output_tokens },
68
+ meta: { provider: "anthropic", stop_reason: message.stop_reason },
69
+ };
70
+ }
71
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Scripted adapter — the deterministic instrument behind CI gates, the demo's
3
+ * verification mode, and the eval harness's golden runs. Returns the scripted
4
+ * outputs in order; a script entry of `{ error: "..." }` raises a typed
5
+ * AdapterOutputError instead.
6
+ */
7
+ import { type GenerateRequest, type GenerateResult, type GenerationAdapter } from "./types.js";
8
+ export type ScriptEntry = {
9
+ output: unknown;
10
+ } | {
11
+ error: string;
12
+ };
13
+ export declare class ScriptedAdapter implements GenerationAdapter {
14
+ private readonly script;
15
+ readonly id = "fake:scripted";
16
+ /** Every request received, for assertions on repair-loop conversation state. */
17
+ readonly requests: GenerateRequest[];
18
+ private cursor;
19
+ constructor(script: ScriptEntry[]);
20
+ generate(request: GenerateRequest): Promise<GenerateResult>;
21
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Scripted adapter — the deterministic instrument behind CI gates, the demo's
3
+ * verification mode, and the eval harness's golden runs. Returns the scripted
4
+ * outputs in order; a script entry of `{ error: "..." }` raises a typed
5
+ * AdapterOutputError instead.
6
+ */
7
+ import { AdapterOutputError } from "./types.js";
8
+ export class ScriptedAdapter {
9
+ script;
10
+ id = "fake:scripted";
11
+ /** Every request received, for assertions on repair-loop conversation state. */
12
+ requests = [];
13
+ cursor = 0;
14
+ constructor(script) {
15
+ this.script = script;
16
+ }
17
+ async generate(request) {
18
+ this.requests.push(request);
19
+ const entry = this.script[this.cursor++];
20
+ if (!entry)
21
+ throw new Error(`ScriptedAdapter: script exhausted after ${this.script.length} generation(s)`);
22
+ if ("error" in entry)
23
+ throw new AdapterOutputError(this.id, entry.error);
24
+ return {
25
+ json: entry.output,
26
+ raw: JSON.stringify(entry.output),
27
+ model: "fake-model",
28
+ usage: { inputTokens: 0, outputTokens: 0 },
29
+ meta: { provider: "fake" },
30
+ };
31
+ }
32
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./types.js";
2
+ export { OllamaAdapter, type OllamaAdapterOptions } from "./ollama.js";
3
+ export { AnthropicAdapter, type AnthropicAdapterOptions } from "./anthropic.js";
4
+ import { type GenerationAdapter } from "./types.js";
5
+ /** Build an adapter from a `--model` reference (ollama:<id> | anthropic:<id>). */
6
+ export declare function adapterFor(modelRef: string): GenerationAdapter;
@@ -0,0 +1,11 @@
1
+ export * from "./types.js";
2
+ export { OllamaAdapter } from "./ollama.js";
3
+ export { AnthropicAdapter } from "./anthropic.js";
4
+ import { OllamaAdapter } from "./ollama.js";
5
+ import { AnthropicAdapter } from "./anthropic.js";
6
+ import { parseModelRef } from "./types.js";
7
+ /** Build an adapter from a `--model` reference (ollama:<id> | anthropic:<id>). */
8
+ export function adapterFor(modelRef) {
9
+ const { provider, model } = parseModelRef(modelRef);
10
+ return provider === "ollama" ? new OllamaAdapter({ model }) : new AnthropicAdapter({ model });
11
+ }
@@ -0,0 +1,16 @@
1
+ import { type FetchLike, type GenerateRequest, type GenerateResult, type GenerationAdapter } from "./types.js";
2
+ export interface OllamaAdapterOptions {
3
+ /** Required — no default model exists in code (ADR-9 as amended). */
4
+ model: string;
5
+ /** Defaults to OLLAMA_HOST env or http://localhost:11434. */
6
+ host?: string;
7
+ fetch?: FetchLike;
8
+ }
9
+ export declare class OllamaAdapter implements GenerationAdapter {
10
+ readonly id: string;
11
+ private readonly model;
12
+ private readonly host;
13
+ private readonly fetchImpl;
14
+ constructor(options: OllamaAdapterOptions);
15
+ generate(request: GenerateRequest): Promise<GenerateResult>;
16
+ }
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Ollama adapter — local generation via structured outputs (`format` = the
3
+ * generation schema on /api/chat). Mandatory for the local path per the plan;
4
+ * there is no unconstrained-JSON fallback in this design.
5
+ *
6
+ * S0 spike caveat carried here: on mlx-engine models Ollama silently ignores
7
+ * `format`. The adapter cannot detect the engine from the chat response, so
8
+ * it records the model tag in `meta` and relies on gates S1/S2 to catch
9
+ * unconstrained output; non-JSON output raises AdapterOutputError.
10
+ */
11
+ import { Agent } from "undici";
12
+ import { AdapterOutputError, parseJsonOutput, } from "./types.js";
13
+ /**
14
+ * Local inference is legitimately slow: a single 35B generation can exceed
15
+ * undici's default 300s headersTimeout, which killed runs at exactly ~301s
16
+ * in the 2026-07-03 eval (reports with zero attempts, "fetch failed").
17
+ * One long-lived dispatcher with the header/body timeouts raised to an hour
18
+ * — a deliberate ceiling, not "no timeout": a truly hung server should
19
+ * still fail rather than block a matrix forever.
20
+ */
21
+ const LOCAL_INFERENCE_TIMEOUT_MS = 60 * 60 * 1000;
22
+ const localInferenceDispatcher = new Agent({
23
+ headersTimeout: LOCAL_INFERENCE_TIMEOUT_MS,
24
+ bodyTimeout: LOCAL_INFERENCE_TIMEOUT_MS,
25
+ });
26
+ export class OllamaAdapter {
27
+ id;
28
+ model;
29
+ host;
30
+ fetchImpl;
31
+ constructor(options) {
32
+ if (!options.model)
33
+ throw new Error("OllamaAdapter requires an explicit model id (config/env/flag)");
34
+ this.model = options.model;
35
+ this.host = options.host ?? process.env.OLLAMA_HOST ?? "http://localhost:11434";
36
+ this.fetchImpl = options.fetch ?? fetch;
37
+ this.id = `ollama:${this.model}`;
38
+ }
39
+ async generate(request) {
40
+ const body = {
41
+ model: this.model,
42
+ stream: false,
43
+ format: request.jsonSchema,
44
+ options: { temperature: request.params?.temperature ?? 0.2 },
45
+ messages: [{ role: "system", content: request.system }, ...request.messages],
46
+ };
47
+ // The whole transport exchange is typed: a rejected fetch (connection
48
+ // reset, timeout under a large model load) must surface as a
49
+ // failed-adapter outcome with a report, never as a raw exception that
50
+ // kills a whole eval matrix (the 2026-07-03 live-run crash).
51
+ let data;
52
+ try {
53
+ const init = {
54
+ method: "POST",
55
+ headers: { "content-type": "application/json" },
56
+ body: JSON.stringify(body),
57
+ // undici extension of fetch(init); ignored by injected test fetches.
58
+ dispatcher: localInferenceDispatcher,
59
+ };
60
+ const response = await this.fetchImpl(`${this.host}/api/chat`, init);
61
+ if (!response.ok) {
62
+ // Body read can itself fail on a broken stream — keep the HTTP
63
+ // status in the error either way.
64
+ const detail = await response.text().catch(() => "(response body unreadable)");
65
+ throw new AdapterOutputError(this.id, `HTTP ${response.status}: ${detail.slice(0, 300)}`);
66
+ }
67
+ data = (await response.json());
68
+ }
69
+ catch (error) {
70
+ if (error instanceof AdapterOutputError)
71
+ throw error;
72
+ throw new AdapterOutputError(this.id, `transport failure: ${error instanceof Error ? error.message : String(error)}`);
73
+ }
74
+ const raw = data.message?.content ?? "";
75
+ if (raw === "")
76
+ throw new AdapterOutputError(this.id, "empty model output");
77
+ return {
78
+ json: parseJsonOutput(this.id, raw),
79
+ raw,
80
+ model: data.model ?? this.model,
81
+ usage: { inputTokens: data.prompt_eval_count, outputTokens: data.eval_count },
82
+ meta: {
83
+ provider: "ollama",
84
+ total_duration: data.total_duration,
85
+ load_duration: data.load_duration,
86
+ prompt_eval_duration: data.prompt_eval_duration,
87
+ eval_duration: data.eval_duration,
88
+ },
89
+ };
90
+ }
91
+ }