@abelxiaoxing/cadence 1.8.4 → 2.0.1

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 (202) hide show
  1. package/README.md +324 -117
  2. package/THIRD_PARTY_NOTICES.md +15 -0
  3. package/agents/design-explorer.md +14 -18
  4. package/agents/diagnosis-worker.md +14 -14
  5. package/agents/implementation-worker.md +16 -17
  6. package/config/workflow-task.example.json +7 -0
  7. package/docs/remote-access.md +60 -0
  8. package/docs/reuse-and-distribution.md +44 -0
  9. package/docs/service-management.md +108 -0
  10. package/licenses/bundled-web.txt +6245 -0
  11. package/licenses/pi-web-ui-MIT.txt +21 -0
  12. package/licenses/pi-web-ui-reuse.md +22 -0
  13. package/package.json +92 -20
  14. package/src/index.ts +21 -1802
  15. package/src/native-build.ts +6 -0
  16. package/src/native-client.ts +103 -0
  17. package/src/native-export.ts +58 -0
  18. package/src/native-history.ts +359 -0
  19. package/src/operator-cli.mjs +453 -1937
  20. package/src/plan-proposal.ts +87 -0
  21. package/src/sdk-worker.mjs +549 -0
  22. package/src/service/contracts/active.ts +82 -0
  23. package/src/service/contracts/budget.ts +148 -0
  24. package/src/service/contracts/build.ts +67 -0
  25. package/src/service/contracts/capture.ts +48 -0
  26. package/src/service/contracts/core.ts +33 -0
  27. package/src/service/contracts/current.ts +261 -0
  28. package/src/service/contracts/domain.ts +68 -0
  29. package/src/service/contracts/history.ts +2245 -0
  30. package/src/service/contracts/models.ts +139 -0
  31. package/src/service/coordination.ts +862 -0
  32. package/src/service/private-files.ts +204 -0
  33. package/src/service-cli.mjs +5592 -0
  34. package/src/service-client.ts +747 -0
  35. package/src/service-contract.ts +245 -0
  36. package/src/service-host.mjs +22382 -0
  37. package/src/service-http-client.ts +425 -0
  38. package/src/service-wire.ts +372 -0
  39. package/src/task-shell.ts +1045 -0
  40. package/src/task-store.ts +479 -0
  41. package/src/task-types.ts +184 -0
  42. package/src/verification-diagnostics.ts +29 -72
  43. package/src/worker-capture.mjs +1197 -0
  44. package/src/worker-public.mjs +1753 -0
  45. package/src/worker-redaction.mjs +608 -0
  46. package/src/worker-tools.mjs +202 -0
  47. package/web/dist/assets/KaTeX_AMS-Regular.ttf +0 -0
  48. package/web/dist/assets/KaTeX_AMS-Regular.woff +0 -0
  49. package/web/dist/assets/KaTeX_AMS-Regular.woff2 +0 -0
  50. package/web/dist/assets/KaTeX_Caligraphic-Bold.ttf +0 -0
  51. package/web/dist/assets/KaTeX_Caligraphic-Bold.woff +0 -0
  52. package/web/dist/assets/KaTeX_Caligraphic-Bold.woff2 +0 -0
  53. package/web/dist/assets/KaTeX_Caligraphic-Regular.ttf +0 -0
  54. package/web/dist/assets/KaTeX_Caligraphic-Regular.woff +0 -0
  55. package/web/dist/assets/KaTeX_Caligraphic-Regular.woff2 +0 -0
  56. package/web/dist/assets/KaTeX_Fraktur-Bold.ttf +0 -0
  57. package/web/dist/assets/KaTeX_Fraktur-Bold.woff +0 -0
  58. package/web/dist/assets/KaTeX_Fraktur-Bold.woff2 +0 -0
  59. package/web/dist/assets/KaTeX_Fraktur-Regular.ttf +0 -0
  60. package/web/dist/assets/KaTeX_Fraktur-Regular.woff +0 -0
  61. package/web/dist/assets/KaTeX_Fraktur-Regular.woff2 +0 -0
  62. package/web/dist/assets/KaTeX_Main-Bold.ttf +0 -0
  63. package/web/dist/assets/KaTeX_Main-Bold.woff +0 -0
  64. package/web/dist/assets/KaTeX_Main-Bold.woff2 +0 -0
  65. package/web/dist/assets/KaTeX_Main-BoldItalic.ttf +0 -0
  66. package/web/dist/assets/KaTeX_Main-BoldItalic.woff +0 -0
  67. package/web/dist/assets/KaTeX_Main-BoldItalic.woff2 +0 -0
  68. package/web/dist/assets/KaTeX_Main-Italic.ttf +0 -0
  69. package/web/dist/assets/KaTeX_Main-Italic.woff +0 -0
  70. package/web/dist/assets/KaTeX_Main-Italic.woff2 +0 -0
  71. package/web/dist/assets/KaTeX_Main-Regular.ttf +0 -0
  72. package/web/dist/assets/KaTeX_Main-Regular.woff +0 -0
  73. package/web/dist/assets/KaTeX_Main-Regular.woff2 +0 -0
  74. package/web/dist/assets/KaTeX_Math-BoldItalic.ttf +0 -0
  75. package/web/dist/assets/KaTeX_Math-BoldItalic.woff +0 -0
  76. package/web/dist/assets/KaTeX_Math-BoldItalic.woff2 +0 -0
  77. package/web/dist/assets/KaTeX_Math-Italic.ttf +0 -0
  78. package/web/dist/assets/KaTeX_Math-Italic.woff +0 -0
  79. package/web/dist/assets/KaTeX_Math-Italic.woff2 +0 -0
  80. package/web/dist/assets/KaTeX_SansSerif-Bold.ttf +0 -0
  81. package/web/dist/assets/KaTeX_SansSerif-Bold.woff +0 -0
  82. package/web/dist/assets/KaTeX_SansSerif-Bold.woff2 +0 -0
  83. package/web/dist/assets/KaTeX_SansSerif-Italic.ttf +0 -0
  84. package/web/dist/assets/KaTeX_SansSerif-Italic.woff +0 -0
  85. package/web/dist/assets/KaTeX_SansSerif-Italic.woff2 +0 -0
  86. package/web/dist/assets/KaTeX_SansSerif-Regular.ttf +0 -0
  87. package/web/dist/assets/KaTeX_SansSerif-Regular.woff +0 -0
  88. package/web/dist/assets/KaTeX_SansSerif-Regular.woff2 +0 -0
  89. package/web/dist/assets/KaTeX_Script-Regular.ttf +0 -0
  90. package/web/dist/assets/KaTeX_Script-Regular.woff +0 -0
  91. package/web/dist/assets/KaTeX_Script-Regular.woff2 +0 -0
  92. package/web/dist/assets/KaTeX_Size1-Regular.ttf +0 -0
  93. package/web/dist/assets/KaTeX_Size1-Regular.woff +0 -0
  94. package/web/dist/assets/KaTeX_Size1-Regular.woff2 +0 -0
  95. package/web/dist/assets/KaTeX_Size2-Regular.ttf +0 -0
  96. package/web/dist/assets/KaTeX_Size2-Regular.woff +0 -0
  97. package/web/dist/assets/KaTeX_Size2-Regular.woff2 +0 -0
  98. package/web/dist/assets/KaTeX_Size3-Regular.ttf +0 -0
  99. package/web/dist/assets/KaTeX_Size3-Regular.woff +0 -0
  100. package/web/dist/assets/KaTeX_Size3-Regular.woff2 +0 -0
  101. package/web/dist/assets/KaTeX_Size4-Regular.ttf +0 -0
  102. package/web/dist/assets/KaTeX_Size4-Regular.woff +0 -0
  103. package/web/dist/assets/KaTeX_Size4-Regular.woff2 +0 -0
  104. package/web/dist/assets/KaTeX_Typewriter-Regular.ttf +0 -0
  105. package/web/dist/assets/KaTeX_Typewriter-Regular.woff +0 -0
  106. package/web/dist/assets/KaTeX_Typewriter-Regular.woff2 +0 -0
  107. package/web/dist/assets/cadence.css +2 -0
  108. package/web/dist/assets/cadence.js +7 -0
  109. package/web/dist/assets/markdown.js +296 -0
  110. package/web/dist/assets/react.js +9 -0
  111. package/web/dist/assets/rolldown-runtime.js +1 -0
  112. package/web/dist/assets/timeline-client.js +41 -0
  113. package/web/dist/assets/vis-timeline.css +1274 -0
  114. package/web/dist/assets/vis-timeline.js +32 -0
  115. package/web/dist/index.html +18 -0
  116. package/web/dist/manifest.json +505 -0
  117. package/config/plan-draft.example.json +0 -125
  118. package/config/plan-draft.multiple-tasks.example.json +0 -375
  119. package/config/plan-draft.quick.example.json +0 -78
  120. package/prompts/abel-design.md +0 -382
  121. package/prompts/abel-diagnose.md +0 -71
  122. package/prompts/abel-implement.md +0 -284
  123. package/prompts/abel-init.md +0 -52
  124. package/src/activation.ts +0 -44
  125. package/src/activity-contracts.ts +0 -82
  126. package/src/agent-registry.ts +0 -53
  127. package/src/apply-transaction.ts +0 -1437
  128. package/src/artifact-store.ts +0 -213
  129. package/src/build-windows-job.mjs +0 -95
  130. package/src/candidate-artifact.ts +0 -143
  131. package/src/candidate-context.ts +0 -289
  132. package/src/candidate-patch.ts +0 -271
  133. package/src/candidate-workspace.ts +0 -80
  134. package/src/canonical.ts +0 -16
  135. package/src/change-contract-schema.ts +0 -191
  136. package/src/change-contract.ts +0 -324
  137. package/src/change-verification.ts +0 -1617
  138. package/src/contracts.ts +0 -2466
  139. package/src/control-contracts.ts +0 -382
  140. package/src/delivery-compiler.ts +0 -1805
  141. package/src/delivery-revision.ts +0 -42
  142. package/src/design-control.ts +0 -1299
  143. package/src/design-diagnostics.ts +0 -181
  144. package/src/design-journal.ts +0 -1773
  145. package/src/directory-sync.ts +0 -36
  146. package/src/durable-contracts.ts +0 -227
  147. package/src/durable-workflow.ts +0 -822
  148. package/src/evidence-draft.ts +0 -122
  149. package/src/execution-profile.ts +0 -64
  150. package/src/execution-retention.ts +0 -77
  151. package/src/file-snapshot.ts +0 -163
  152. package/src/implement-continuation.ts +0 -559
  153. package/src/implement-graph.ts +0 -690
  154. package/src/implement-plan.ts +0 -72
  155. package/src/install-config.d.mts +0 -6
  156. package/src/isolation-backend.ts +0 -353
  157. package/src/model-source.ts +0 -22
  158. package/src/openspec-cli.ts +0 -494
  159. package/src/operator-cli-entry.mjs +0 -24
  160. package/src/operator-tools.ts +0 -309
  161. package/src/package-candidate-process.ts +0 -206
  162. package/src/package-candidate.ts +0 -24
  163. package/src/package-delivery.ts +0 -486
  164. package/src/package-state.ts +0 -320
  165. package/src/package-verification.ts +0 -635
  166. package/src/package-workflow.ts +0 -305
  167. package/src/packet-runtime.ts +0 -459
  168. package/src/phase-execution.ts +0 -2163
  169. package/src/pi-adapter.ts +0 -19
  170. package/src/plan-draft-summary.ts +0 -527
  171. package/src/plan-draft.ts +0 -502
  172. package/src/remove-path.ts +0 -32
  173. package/src/run-state.ts +0 -420
  174. package/src/run-store.ts +0 -1179
  175. package/src/safe-path.ts +0 -77
  176. package/src/scoped-grep-worker.mjs +0 -23
  177. package/src/scoped-tools.ts +0 -775
  178. package/src/single-task-draft.ts +0 -142
  179. package/src/sqlite-schema.ts +0 -171
  180. package/src/state-root.ts +0 -215
  181. package/src/storage-schema.ts +0 -360
  182. package/src/subagent-activity.ts +0 -1417
  183. package/src/subagent-process.ts +0 -476
  184. package/src/task-ledger.ts +0 -1338
  185. package/src/transport-budget.ts +0 -50
  186. package/src/verification-capability.ts +0 -1191
  187. package/src/verification-environment-io.ts +0 -389
  188. package/src/verification-environment.ts +0 -162
  189. package/src/verification-identity.ts +0 -77
  190. package/src/verification-scan.ts +0 -64
  191. package/src/windows-job-backend.ts +0 -419
  192. package/src/windows-job.c +0 -149
  193. package/src/workflow-engine.ts +0 -28
  194. package/src/workflow-policy.ts +0 -1670
  195. package/src/workflow-recovery-policy.ts +0 -97
  196. package/src/workflow-scheduling.ts +0 -105
  197. package/src/workflow-state-machine.ts +0 -4341
  198. package/src/workflow-status.ts +0 -576
  199. package/src/workspace-io-entry.mjs +0 -76
  200. package/src/workspace-io-worker.mjs +0 -1312
  201. package/src/workspace-io.ts +0 -89
  202. package/src/workspace-store.ts +0 -634
@@ -1,191 +0,0 @@
1
- import { Type } from "typebox";
2
- import { Value } from "typebox/value";
3
- import {
4
- isValidRelativePath,
5
- VERIFICATION_LIMITS,
6
- validateVerificationContract,
7
- } from "./contracts.ts";
8
- import type { DesignPlanDiagnostic } from "./design-diagnostics.ts";
9
-
10
- const text = Type.String({ minLength: 1, maxLength: 8192, pattern: "\\S" });
11
- const id = Type.String({ pattern: "^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$" });
12
- const object = <T extends Parameters<typeof Type.Object>[0]>(properties: T) =>
13
- Type.Object(properties, { additionalProperties: false });
14
- const managers = Type.Union([
15
- Type.Literal("bun"),
16
- Type.Literal("npm"),
17
- Type.Literal("pnpm"),
18
- Type.Literal("yarn"),
19
- ]);
20
- const packageRunner = {
21
- kind: Type.Literal("package-script"),
22
- packageManager: managers,
23
- script: text,
24
- command: text,
25
- };
26
- const executable = Type.Union([
27
- object(packageRunner),
28
- object({ kind: Type.Literal("local-binary"), executable: text }),
29
- object({
30
- kind: Type.Literal("npx"),
31
- executable: text,
32
- noInstall: Type.Literal(true),
33
- }),
34
- ]);
35
- const classification = Type.Union([
36
- Type.Literal("expected-red"),
37
- Type.Literal("expected-green"),
38
- Type.Literal("expected-refactor"),
39
- ]);
40
- const base = {
41
- id,
42
- classification,
43
- expectedFailure: Type.Optional(Type.String({ minLength: 1, maxLength: 512 })),
44
- executionBindings: Type.Optional(
45
- Type.Record(Type.String(), Type.Union([Type.String(), Type.Null()])),
46
- ),
47
- };
48
- const args = Type.Array(Type.String(), { maxItems: 128 });
49
- export const ATOMIC_VERIFICATION_SCHEMAS = {
50
- vitest: object({
51
- ...base,
52
- kind: Type.Literal("vitest"),
53
- runner: executable,
54
- testFiles: Type.Array(text, {
55
- minItems: VERIFICATION_LIMITS.minTestFiles,
56
- maxItems: VERIFICATION_LIMITS.maxTestFiles,
57
- uniqueItems: true,
58
- }),
59
- args,
60
- minTests: Type.Integer({ minimum: 1 }),
61
- }),
62
- "package-script": object({ ...base, ...packageRunner, args }),
63
- "static-check": object({
64
- ...base,
65
- kind: Type.Literal("static-check"),
66
- runner: Type.Union([
67
- executable,
68
- object({ kind: Type.Literal("node"), script: text }),
69
- ]),
70
- args,
71
- }),
72
- };
73
- const atomic = Type.Union(Object.values(ATOMIC_VERIFICATION_SCHEMAS));
74
- const steps = object({
75
- kind: Type.Literal("steps"),
76
- id,
77
- classification,
78
- steps: Type.Array(atomic, {
79
- minItems: VERIFICATION_LIMITS.minSteps,
80
- maxItems: VERIFICATION_LIMITS.maxSteps,
81
- }),
82
- });
83
- export const VERIFICATION_SCHEMA = Type.Union([atomic, steps]);
84
- export const CHANGE_CONTRACT_SCHEMA = object({
85
- goal: text,
86
- acceptance: Type.Array(
87
- object({ id, statement: text, verification: VERIFICATION_SCHEMA }),
88
- { minItems: 1, maxItems: 256 },
89
- ),
90
- constraints: Type.Array(object({ id, statement: text }), { maxItems: 256 }),
91
- policy: object({
92
- writeRoots: Type.Array(text, {
93
- minItems: 1,
94
- maxItems: 256,
95
- uniqueItems: true,
96
- }),
97
- dependencies: Type.Array(text, { maxItems: 256, uniqueItems: true }),
98
- verificationModes: Type.Array(
99
- Type.Union([
100
- Type.Literal("behavior"),
101
- Type.Literal("mechanical"),
102
- Type.Literal("refactor"),
103
- ]),
104
- { minItems: 1, maxItems: 256, uniqueItems: true },
105
- ),
106
- }),
107
- });
108
- function record(value: unknown): value is Record<string, unknown> {
109
- return !!value && typeof value === "object" && !Array.isArray(value);
110
- }
111
-
112
- const schemaFields = new Set([
113
- "goal",
114
- "acceptance",
115
- "id",
116
- "statement",
117
- "verification",
118
- "constraints",
119
- "policy",
120
- "writeRoots",
121
- "dependencies",
122
- "verificationModes",
123
- "classification",
124
- "expectedFailure",
125
- "executionBindings",
126
- "kind",
127
- "runner",
128
- "packageManager",
129
- "script",
130
- "command",
131
- "executable",
132
- "noInstall",
133
- "testFiles",
134
- "args",
135
- "minTests",
136
- "steps",
137
- ]);
138
- function diagnosticField(pointer: string): string {
139
- const fields = ["contract"];
140
- for (const part of pointer.split("/").slice(1)) {
141
- if (!schemaFields.has(part) && !/^(0|[1-9][0-9]{0,5})$/u.test(part)) break;
142
- fields.push(part);
143
- // Binding keys are author data, even when they happen to look like schema fields.
144
- if (part === "executionBindings") break;
145
- }
146
- return fields.join(".");
147
- }
148
-
149
- /** Shared shape, bounded field-only diagnostics; never return submitted values or parser messages. */
150
- export function changeContractDiagnostics(
151
- value: unknown,
152
- ): DesignPlanDiagnostic[] {
153
- const diagnostics: DesignPlanDiagnostic[] = Value.Errors(
154
- CHANGE_CONTRACT_SCHEMA,
155
- value,
156
- )
157
- .slice(0, 32)
158
- .map((error) => ({
159
- code: "change-contract-field-invalid",
160
- field: diagnosticField(error.instancePath),
161
- category: error.keyword,
162
- }));
163
- if (!record(value)) return diagnostics;
164
- if (Array.isArray(value.acceptance))
165
- value.acceptance.slice(0, 256).forEach((item, index) => {
166
- if (!record(item)) return;
167
- const verification = item.verification;
168
- if (!validateVerificationContract(verification).ok) {
169
- const field = `contract.acceptance.${index}.verification`;
170
- if (
171
- record(verification) &&
172
- verification.classification === "expected-red" &&
173
- typeof verification.expectedFailure !== "string"
174
- )
175
- diagnostics.push({
176
- code: "change-contract-field-invalid",
177
- field: `${field}.expectedFailure`,
178
- });
179
- else diagnostics.push({ code: "change-contract-field-invalid", field });
180
- }
181
- });
182
- if (record(value.policy) && Array.isArray(value.policy.writeRoots))
183
- value.policy.writeRoots.slice(0, 256).forEach((root, index) => {
184
- if (root !== "." && !isValidRelativePath(root))
185
- diagnostics.push({
186
- code: "change-contract-field-invalid",
187
- field: `contract.policy.writeRoots.${index}`,
188
- });
189
- });
190
- return diagnostics.slice(0, 32);
191
- }
@@ -1,324 +0,0 @@
1
- import { changeContractDiagnostics } from "./change-contract-schema.ts";
2
- import {
3
- type ImplementTaskBoundary,
4
- isValidRelativePath,
5
- type StructuredVerificationContract,
6
- validateVerificationContract,
7
- verificationInputPaths,
8
- } from "./contracts.ts";
9
- import {
10
- type DesignPlanDiagnostic,
11
- DesignPlanValidationError,
12
- } from "./design-diagnostics.ts";
13
- import { canonicalJson } from "./run-state.ts";
14
-
15
- export interface ChangeContract {
16
- goal: string;
17
- acceptance: Array<{
18
- id: string;
19
- statement: string;
20
- verification: StructuredVerificationContract;
21
- }>;
22
- constraints: Array<{ id: string; statement: string }>;
23
- policy: {
24
- writeRoots: string[];
25
- dependencies: string[];
26
- verificationModes: Array<"behavior" | "mechanical" | "refactor">;
27
- };
28
- }
29
-
30
- function record(value: unknown): value is Record<string, unknown> {
31
- return !!value && typeof value === "object" && !Array.isArray(value);
32
- }
33
- function keys(value: Record<string, unknown>, expected: string[]) {
34
- return (
35
- canonicalJson(Object.keys(value).sort()) === canonicalJson(expected.sort())
36
- );
37
- }
38
- function text(value: unknown): value is string {
39
- return (
40
- typeof value === "string" && value.trim().length > 0 && value.length <= 8192
41
- );
42
- }
43
- const identifier = /^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$/u;
44
-
45
- export function normalizeChangeContract(value: unknown): ChangeContract {
46
- const diagnostics = changeContractDiagnostics(value);
47
- if (diagnostics.length)
48
- throw new DesignPlanValidationError("change-contract-invalid", diagnostics);
49
- if (
50
- !record(value) ||
51
- !keys(value, ["goal", "acceptance", "constraints", "policy"]) ||
52
- !text(value.goal) ||
53
- !Array.isArray(value.acceptance) ||
54
- value.acceptance.length < 1 ||
55
- value.acceptance.length > 256 ||
56
- !Array.isArray(value.constraints) ||
57
- value.constraints.length > 256 ||
58
- !record(value.policy)
59
- )
60
- throw new Error("change-contract-invalid");
61
- const entries = (values: unknown[], acceptance: boolean) =>
62
- values
63
- .map((entry) => {
64
- if (
65
- !record(entry) ||
66
- !keys(
67
- entry,
68
- acceptance
69
- ? ["id", "statement", "verification"]
70
- : ["id", "statement"],
71
- ) ||
72
- typeof entry.id !== "string" ||
73
- !identifier.test(entry.id) ||
74
- !text(entry.statement)
75
- )
76
- throw new Error("change-contract-invalid");
77
- const verification = acceptance
78
- ? validateVerificationContract(entry.verification)
79
- : undefined;
80
- if (verification && !verification.ok)
81
- throw new Error("change-contract-invalid");
82
- return {
83
- id: entry.id,
84
- statement: entry.statement.replace(/\r\n?/gu, "\n").trim(),
85
- ...(verification?.ok ? { verification: verification.value } : {}),
86
- };
87
- })
88
- .sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
89
- const acceptance = entries(value.acceptance, true);
90
- const constraints = entries(value.constraints, false);
91
- for (const group of [acceptance, constraints])
92
- if (new Set(group.map((item) => item.id)).size !== group.length)
93
- throw new Error("change-contract-invalid");
94
- const policy = value.policy;
95
- if (!keys(policy, ["writeRoots", "dependencies", "verificationModes"]))
96
- throw new Error("change-contract-invalid");
97
- for (const key of ["writeRoots", "dependencies", "verificationModes"])
98
- if (
99
- !Array.isArray(policy[key]) ||
100
- policy[key].length > 256 ||
101
- !policy[key].every(text) ||
102
- new Set(policy[key]).size !== policy[key].length
103
- )
104
- throw new Error("change-contract-invalid");
105
- const roots = policy.writeRoots as string[];
106
- const modes = policy.verificationModes as string[];
107
- if (
108
- !roots.length ||
109
- !roots.every((root) => root === "." || isValidRelativePath(root)) ||
110
- !modes.length ||
111
- !modes.every((mode) =>
112
- ["behavior", "mechanical", "refactor"].includes(mode),
113
- )
114
- )
115
- throw new Error("change-contract-invalid");
116
- const result = {
117
- goal: value.goal.replace(/\r\n?/gu, "\n").trim(),
118
- acceptance,
119
- constraints,
120
- policy: {
121
- writeRoots: [...roots].sort(),
122
- dependencies: [...(policy.dependencies as string[])].sort(),
123
- verificationModes: [...modes].sort(),
124
- },
125
- } as ChangeContract;
126
- if (Buffer.byteLength(canonicalJson(result)) > 64 * 1024)
127
- throw new Error("change-contract-invalid");
128
- return result;
129
- }
130
-
131
- /** Display identities and fresh input bindings do not change a verification obligation. */
132
- export function verificationObligation(
133
- value: StructuredVerificationContract,
134
- ): string {
135
- const strip = (item: unknown): unknown =>
136
- Array.isArray(item)
137
- ? item.map(strip)
138
- : record(item)
139
- ? Object.fromEntries(
140
- Object.entries(item)
141
- .filter(([key]) => !["id", "executionBindings"].includes(key))
142
- .map(([key, child]) => [key, strip(child)]),
143
- )
144
- : item;
145
- return canonicalJson(strip(value));
146
- }
147
-
148
- export function assertPlanWithinChangeContract(
149
- contract: ChangeContract,
150
- tasks: readonly (ImplementTaskBoundary & {
151
- verificationMode?: string;
152
- affectedVerification: StructuredVerificationContract;
153
- repairVerification: StructuredVerificationContract;
154
- })[],
155
- verifications: readonly StructuredVerificationContract[],
156
- ): void {
157
- const candidates = [
158
- ...verifications,
159
- ...tasks.flatMap((task) => [
160
- task.affectedVerification,
161
- ...Object.entries(task.phases)
162
- .filter(
163
- ([phase]) =>
164
- phase !== "red" ||
165
- !task.verificationMode ||
166
- task.verificationMode === "behavior",
167
- )
168
- .map(([, phase]) => phase.verification),
169
- ]),
170
- ];
171
- const available = new Set(candidates.map(verificationObligation));
172
- const diagnostics: DesignPlanDiagnostic[] = [];
173
- for (const [index, accepted] of contract.acceptance.entries()) {
174
- if (available.has(verificationObligation(accepted.verification))) continue;
175
- // Only attribute a mismatch when the display ID selects one distinct
176
- // obligation. Never guess a "closest" verifier or echo command/argument bytes.
177
- const matching = [
178
- ...new Set(
179
- candidates
180
- .filter((candidate) => candidate.id === accepted.verification.id)
181
- .map(verificationObligation),
182
- ),
183
- ];
184
- const expected = JSON.parse(verificationObligation(accepted.verification));
185
- const actual =
186
- matching.length === 1 && matching[0]
187
- ? JSON.parse(matching[0])
188
- : undefined;
189
- diagnostics.push({
190
- code: "change-contract-acceptance-missing",
191
- acceptanceId: accepted.id,
192
- verificationId: accepted.verification.id,
193
- field: `changeContract.acceptance.${index}.verification`,
194
- ...(actual
195
- ? {
196
- mismatchedFields: [
197
- "kind",
198
- "runner",
199
- "packageManager",
200
- "script",
201
- "command",
202
- "args",
203
- "testFiles",
204
- "minTests",
205
- "classification",
206
- "expectedFailure",
207
- "steps",
208
- ]
209
- .filter(
210
- (key) =>
211
- canonicalJson(expected[key] ?? null) !==
212
- canonicalJson(actual[key] ?? null),
213
- )
214
- .sort(),
215
- }
216
- : {}),
217
- });
218
- }
219
- if (diagnostics.length)
220
- throw new DesignPlanValidationError(
221
- "change-contract-acceptance-missing",
222
- diagnostics,
223
- );
224
- for (const task of tasks) {
225
- if (
226
- !contract.policy.verificationModes.includes(
227
- (task.verificationMode ?? "behavior") as "behavior",
228
- )
229
- )
230
- throw new Error("change-contract-verification-mode");
231
- if (
232
- task.approvedDependencies.some(
233
- (name) => !contract.policy.dependencies.includes(name),
234
- )
235
- )
236
- throw new Error("change-contract-dependency-outside-policy");
237
- if (
238
- task.agents &&
239
- task.agents.impact !== "none" &&
240
- !contract.policy.writeRoots.includes(task.agents.target ?? "")
241
- )
242
- throw new Error("change-contract-agents-outside-policy");
243
- const mode = task.verificationMode ?? "behavior";
244
- const writes = Object.entries(task.phases)
245
- .filter(([phase]) => phase !== "red" || mode === "behavior")
246
- .flatMap(([, phase]) => [...phase.write, ...phase.delete]);
247
- if (
248
- mode !== "behavior" &&
249
- task.impactClosure?.changedSurfaces.some((surface) => surface !== "none")
250
- )
251
- throw new Error("change-contract-behavior-requires-red");
252
- if (
253
- mode === "mechanical" &&
254
- writes.some((file) => !/\.(?:md|txt|json|ya?ml|toml|lock)$/iu.test(file))
255
- )
256
- throw new Error("change-contract-mechanical-path");
257
- if (
258
- mode === "refactor" &&
259
- contract.acceptance.some((accepted) =>
260
- verificationInputPaths(accepted.verification).some((file) =>
261
- writes.includes(file),
262
- ),
263
- )
264
- )
265
- throw new Error("change-contract-refactor-verification-mutation");
266
- for (const phase of Object.values(task.phases))
267
- for (const file of [...phase.write, ...phase.delete])
268
- if (
269
- !contract.policy.writeRoots.some(
270
- (root) =>
271
- root === "." || file === root || file.startsWith(`${root}/`),
272
- )
273
- )
274
- throw new Error("change-contract-write-outside-policy");
275
- }
276
- }
277
-
278
- export function retainedChangeContract(plan: {
279
- changeContract?: ChangeContract;
280
- changeId: string;
281
- tasks: readonly (ImplementTaskBoundary & {
282
- affectedVerification: StructuredVerificationContract;
283
- })[];
284
- verification: {
285
- change: {
286
- fullSuite: StructuredVerificationContract;
287
- postApply: StructuredVerificationContract;
288
- };
289
- };
290
- }): ChangeContract {
291
- if (plan.changeContract) return normalizeChangeContract(plan.changeContract);
292
- return normalizeChangeContract({
293
- goal: plan.changeId,
294
- acceptance: [
295
- plan.verification.change.fullSuite,
296
- plan.verification.change.postApply,
297
- ...plan.tasks.map((task) => task.affectedVerification),
298
- ].map((verification, index) => ({
299
- id: `legacy-${index}`,
300
- statement: "Retained verification obligation",
301
- verification,
302
- })),
303
- constraints: [],
304
- policy: {
305
- writeRoots: [
306
- ...new Set(
307
- plan.tasks.flatMap((task) => [
308
- ...(task.agents.impact !== "none" && task.agents.target
309
- ? [task.agents.target]
310
- : []),
311
- ...Object.values(task.phases).flatMap((phase) => [
312
- ...phase.write,
313
- ...phase.delete,
314
- ]),
315
- ]),
316
- ),
317
- ],
318
- dependencies: [
319
- ...new Set(plan.tasks.flatMap((task) => task.approvedDependencies)),
320
- ],
321
- verificationModes: ["behavior"],
322
- },
323
- });
324
- }