@abelxiaoxing/cadence 1.3.0 → 1.4.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.
- package/README.md +172 -13
- package/agents/design-explorer.md +17 -11
- package/agents/diagnosis-worker.md +9 -1
- package/agents/implementation-worker.md +3 -1
- package/config/plan-draft.example.json +122 -0
- package/config/plan-draft.multiple-tasks.example.json +345 -0
- package/package.json +9 -5
- package/prompts/abel-design.md +141 -38
- package/prompts/abel-diagnose.md +10 -1
- package/prompts/abel-implement.md +118 -20
- package/prompts/abel-init.md +9 -2
- package/src/activity-contracts.ts +82 -0
- package/src/apply-transaction.ts +16 -14
- package/src/artifact-store.ts +17 -0
- package/src/candidate-artifact.ts +143 -0
- package/src/candidate-patch.ts +33 -12
- package/src/candidate-workspace.ts +74 -0
- package/src/canonical.ts +16 -0
- package/src/change-contract-schema.ts +191 -0
- package/src/change-contract.ts +273 -0
- package/src/change-verification.ts +896 -0
- package/src/child-model.ts +129 -0
- package/src/child-session.ts +296 -328
- package/src/contracts.ts +325 -109
- package/src/control-contracts.ts +51 -1
- package/src/delivery-compiler.ts +241 -141
- package/src/delivery-revision.ts +42 -0
- package/src/design-control.ts +182 -15
- package/src/design-diagnostics.ts +150 -0
- package/src/design-journal.ts +216 -72
- package/src/durable-contracts.ts +210 -0
- package/src/durable-workflow.ts +963 -0
- package/src/evidence-draft.ts +122 -0
- package/src/implement-graph.ts +14 -16
- package/src/implement-plan.ts +70 -0
- package/src/index.ts +341 -1405
- package/src/isolation-backend.ts +84 -23
- package/src/model-source.ts +22 -0
- package/src/package-candidate.ts +224 -0
- package/src/package-delivery.ts +486 -0
- package/src/package-verification.ts +509 -0
- package/src/package-workflow.ts +253 -0
- package/src/packet-runtime.ts +27 -48
- package/src/parent-provider.ts +86 -198
- package/src/phase-execution.ts +2248 -0
- package/src/pi-adapter.ts +19 -0
- package/src/plan-draft-summary.ts +146 -0
- package/src/plan-draft.ts +493 -0
- package/src/route-policy.ts +1 -11
- package/src/run-state.ts +4 -16
- package/src/run-store.ts +5 -144
- package/src/scoped-grep-worker.mjs +23 -0
- package/src/scoped-tools.ts +312 -116
- package/src/sqlite-schema.ts +171 -0
- package/src/storage-schema.ts +352 -0
- package/src/subagent-activity.ts +46 -67
- package/src/submit-tool.ts +40 -61
- package/src/task-ledger.ts +14 -55
- package/src/transport-budget.ts +32 -0
- package/src/verification-capability.ts +298 -102
- package/src/verification-environment.ts +213 -0
- package/src/verification-identity.ts +77 -0
- package/src/worker-broker.ts +134 -91
- package/src/workflow-engine.ts +28 -8285
- package/src/workflow-policy.ts +1388 -0
- package/src/workflow-recovery-policy.ts +95 -0
- package/src/workflow-scheduling.ts +105 -0
- package/src/workflow-state-machine.ts +4086 -0
- package/src/workflow-status.ts +384 -0
- package/src/workspace-io-entry.mjs +58 -0
- package/src/workspace-io-worker.mjs +991 -0
- package/src/workspace-io.ts +84 -0
- package/src/workspace-store.ts +125 -19
- package/skills/abel-workflow/SKILL.md +0 -205
- package/src/empty-resource-loader.ts +0 -38
- package/src/parent-payload-bridge.ts +0 -523
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { PackageContext } from "./model-source.ts";
|
|
3
|
+
|
|
4
|
+
/** Project only the model capabilities used by the control services. */
|
|
5
|
+
export function packageContext(
|
|
6
|
+
context: Pick<ExtensionContext, "cwd" | "model" | "modelRegistry">,
|
|
7
|
+
): PackageContext {
|
|
8
|
+
return {
|
|
9
|
+
cwd: context.cwd,
|
|
10
|
+
get model() {
|
|
11
|
+
return context.model;
|
|
12
|
+
},
|
|
13
|
+
modelRegistry: {
|
|
14
|
+
getProvider: (id) => context.modelRegistry.getProvider(id),
|
|
15
|
+
getApiKeyAndHeaders: (model) =>
|
|
16
|
+
context.modelRegistry.getApiKeyAndHeaders(model),
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { isValidRelativePath } from "./contracts.ts";
|
|
2
|
+
import type { ImplementPlan } from "./implement-plan.ts";
|
|
3
|
+
|
|
4
|
+
/** Review data only: no executable callbacks, evidence cache or approval authority. */
|
|
5
|
+
function record(value: unknown): Record<string, unknown> {
|
|
6
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
7
|
+
? (value as Record<string, unknown>)
|
|
8
|
+
: {};
|
|
9
|
+
}
|
|
10
|
+
export function planAuthoringSummary(
|
|
11
|
+
plan: ImplementPlan,
|
|
12
|
+
authoredDraft?: unknown,
|
|
13
|
+
) {
|
|
14
|
+
const authored = record(authoredDraft);
|
|
15
|
+
const derivations: Array<{ field: string; source: string; taskId?: string }> =
|
|
16
|
+
[];
|
|
17
|
+
let derivationsTruncated = false;
|
|
18
|
+
const derived = (field: string, source: string, taskId?: string) => {
|
|
19
|
+
if (derivations.length >= 128) {
|
|
20
|
+
derivationsTruncated = true;
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
derivations.push({ field, source, ...(taskId ? { taskId } : {}) });
|
|
24
|
+
};
|
|
25
|
+
if (authoredDraft !== undefined) {
|
|
26
|
+
if (!Object.hasOwn(authored, "tracking")) derived("tracking", "task-ids");
|
|
27
|
+
if (plan.changeContract && !Object.hasOwn(authored, "changeContract"))
|
|
28
|
+
derived("changeContract", "approved-gate-a");
|
|
29
|
+
for (const task of plan.tasks) {
|
|
30
|
+
const input = record(
|
|
31
|
+
Array.isArray(authored.tasks)
|
|
32
|
+
? authored.tasks.find((item) => record(item).taskId === task.taskId)
|
|
33
|
+
: undefined,
|
|
34
|
+
);
|
|
35
|
+
for (const [phase, boundary] of Object.entries(task.phases)) {
|
|
36
|
+
const phaseInput = record(record(input.phases)[phase]);
|
|
37
|
+
if (!Object.hasOwn(phaseInput, "verificationInputs"))
|
|
38
|
+
derived(
|
|
39
|
+
`phases.${phase}.verificationInputs`,
|
|
40
|
+
"contract-and-declared-outputs",
|
|
41
|
+
task.taskId,
|
|
42
|
+
);
|
|
43
|
+
if (Object.hasOwn(input, "read"))
|
|
44
|
+
derived(
|
|
45
|
+
`phases.${phase}.read`,
|
|
46
|
+
"common-and-phase-reads",
|
|
47
|
+
task.taskId,
|
|
48
|
+
);
|
|
49
|
+
const verification = record(phaseInput.verification);
|
|
50
|
+
if (typeof verification.use === "string")
|
|
51
|
+
derived(
|
|
52
|
+
`phases.${phase}.verification`,
|
|
53
|
+
`definition:${verification.use}`,
|
|
54
|
+
task.taskId,
|
|
55
|
+
);
|
|
56
|
+
if (!Object.hasOwn(verification, "id"))
|
|
57
|
+
derived(
|
|
58
|
+
`phases.${phase}.verification.id`,
|
|
59
|
+
"stable-task-purpose-command",
|
|
60
|
+
task.taskId,
|
|
61
|
+
);
|
|
62
|
+
if (!Object.hasOwn(verification, "classification"))
|
|
63
|
+
derived(
|
|
64
|
+
`phases.${phase}.verification.classification`,
|
|
65
|
+
boundary.verification.classification,
|
|
66
|
+
task.taskId,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
const tests = record(input.impactClosure).relatedTests;
|
|
70
|
+
if (
|
|
71
|
+
Array.isArray(tests) &&
|
|
72
|
+
tests.some((test) => !Object.hasOwn(record(test), "disposition"))
|
|
73
|
+
)
|
|
74
|
+
derived("impactClosure.relatedTests", "declared-writers", task.taskId);
|
|
75
|
+
if (!Object.hasOwn(record(input.agents), "managedOnly"))
|
|
76
|
+
derived("agents.managedOnly", "parent-managed-boundary", task.taskId);
|
|
77
|
+
}
|
|
78
|
+
const verification = record(authored.verification);
|
|
79
|
+
for (const [group, keys] of [
|
|
80
|
+
["baseline", ["target", "affected", "failureIdentity"]],
|
|
81
|
+
["change", ["affected"]],
|
|
82
|
+
[
|
|
83
|
+
"repair",
|
|
84
|
+
["inBoundaryOnly", "approvalOnBoundaryExpansion", "attribution"],
|
|
85
|
+
],
|
|
86
|
+
] as const) {
|
|
87
|
+
for (const key of keys)
|
|
88
|
+
if (!Object.hasOwn(record(verification[group]), key))
|
|
89
|
+
derived(`verification.${group}.${key}`, "fixed-execution-contract");
|
|
90
|
+
}
|
|
91
|
+
if (!Object.hasOwn(verification, "agentsCheckpoint"))
|
|
92
|
+
derived("verification.agentsCheckpoint", "explicit-none-impacts");
|
|
93
|
+
}
|
|
94
|
+
let truncated = plan.tasks.length > 16;
|
|
95
|
+
const paths = (values: readonly string[]) => {
|
|
96
|
+
const safe = values.filter(isValidRelativePath);
|
|
97
|
+
if (safe.length !== values.length || safe.length > 32) truncated = true;
|
|
98
|
+
return safe.slice(0, 32);
|
|
99
|
+
};
|
|
100
|
+
const tasks = plan.tasks.slice(0, 16).map((task) => ({
|
|
101
|
+
taskId: task.taskId,
|
|
102
|
+
dependsOn: task.dependsOn.slice(0, 16),
|
|
103
|
+
phases: Object.entries(task.phases).map(([phase, boundary]) => ({
|
|
104
|
+
phase,
|
|
105
|
+
read: paths(boundary.read),
|
|
106
|
+
write: paths(boundary.write),
|
|
107
|
+
delete: paths(boundary.delete),
|
|
108
|
+
verificationId: boundary.verification.id,
|
|
109
|
+
classification: boundary.verification.classification,
|
|
110
|
+
verificationInputs: boundary.verificationInputs.slice(0, 32),
|
|
111
|
+
})),
|
|
112
|
+
affectedVerification: task.affectedVerification.id,
|
|
113
|
+
repairVerification: task.repairVerification.id,
|
|
114
|
+
agentsImpact: task.agents.impact,
|
|
115
|
+
}));
|
|
116
|
+
if (
|
|
117
|
+
plan.tasks.some(
|
|
118
|
+
(task) =>
|
|
119
|
+
task.dependsOn.length > 16 ||
|
|
120
|
+
Object.values(task.phases).some(
|
|
121
|
+
(phase) => phase.verificationInputs.length > 32,
|
|
122
|
+
),
|
|
123
|
+
)
|
|
124
|
+
)
|
|
125
|
+
truncated = true;
|
|
126
|
+
return {
|
|
127
|
+
tasks,
|
|
128
|
+
recovery: {
|
|
129
|
+
artifactCorrection: plan.verification.artifactCorrection.maxAttempts,
|
|
130
|
+
repair: plan.verification.repair.maxAttempts,
|
|
131
|
+
},
|
|
132
|
+
verifications: {
|
|
133
|
+
baseline: plan.verification.baseline.fullSuite.id,
|
|
134
|
+
fullSuite: plan.verification.change.fullSuite.id,
|
|
135
|
+
postApply: plan.verification.change.postApply.id,
|
|
136
|
+
},
|
|
137
|
+
derivations,
|
|
138
|
+
derivationRules: [
|
|
139
|
+
"tracking:task-ids",
|
|
140
|
+
"verificationInputs:contract-and-declared-outputs",
|
|
141
|
+
"relatedTests:declared-writers",
|
|
142
|
+
"verification-identities:stable-task-and-purpose",
|
|
143
|
+
],
|
|
144
|
+
truncated: truncated || derivationsTruncated,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { canonicalJson, compareCanonicalStrings } from "./canonical.ts";
|
|
3
|
+
import type { ChangeContract } from "./change-contract.ts";
|
|
4
|
+
import {
|
|
5
|
+
type AtomicVerificationContract,
|
|
6
|
+
type ImplementGraphOutput,
|
|
7
|
+
isValidRelativePath,
|
|
8
|
+
type PhaseBoundary,
|
|
9
|
+
type StructuredVerificationContract,
|
|
10
|
+
validateVerificationContract,
|
|
11
|
+
verificationInputPaths,
|
|
12
|
+
} from "./contracts.ts";
|
|
13
|
+
import {
|
|
14
|
+
type DesignPlanDiagnostic,
|
|
15
|
+
DesignPlanValidationError,
|
|
16
|
+
} from "./design-diagnostics.ts";
|
|
17
|
+
import type {
|
|
18
|
+
PlanTaskDraft,
|
|
19
|
+
PlanTracking,
|
|
20
|
+
PlanVerification,
|
|
21
|
+
} from "./implement-plan.ts";
|
|
22
|
+
|
|
23
|
+
type DraftCommand<T> = T extends { kind: "package-script" }
|
|
24
|
+
? Omit<T, "command"> & { command?: string }
|
|
25
|
+
: T;
|
|
26
|
+
type DraftRunner<T> = T extends { runner: infer R }
|
|
27
|
+
? Omit<T, "runner"> & { runner: DraftCommand<R> }
|
|
28
|
+
: DraftCommand<T>;
|
|
29
|
+
type Template<T> = T extends AtomicVerificationContract
|
|
30
|
+
? DraftRunner<
|
|
31
|
+
Omit<T, "id" | "classification" | "expectedFailure" | "executionBindings">
|
|
32
|
+
>
|
|
33
|
+
: never;
|
|
34
|
+
export type VerificationDefinition = Template<AtomicVerificationContract>;
|
|
35
|
+
export type DraftVerification =
|
|
36
|
+
| StructuredVerificationContract
|
|
37
|
+
| (VerificationDefinition & {
|
|
38
|
+
id?: string;
|
|
39
|
+
classification?: StructuredVerificationContract["classification"];
|
|
40
|
+
expectedFailure?: string;
|
|
41
|
+
})
|
|
42
|
+
| { use: string; expectedFailure?: string };
|
|
43
|
+
export type PlanPhaseDraft = Omit<
|
|
44
|
+
PhaseBoundary,
|
|
45
|
+
"verificationInputs" | "read" | "verification"
|
|
46
|
+
> & {
|
|
47
|
+
read?: string[];
|
|
48
|
+
verification: DraftVerification;
|
|
49
|
+
verificationInputs?: PhaseBoundary["verificationInputs"];
|
|
50
|
+
};
|
|
51
|
+
export interface PlanTaskInput
|
|
52
|
+
extends Omit<
|
|
53
|
+
PlanTaskDraft,
|
|
54
|
+
| "phases"
|
|
55
|
+
| "impactClosure"
|
|
56
|
+
| "affectedVerification"
|
|
57
|
+
| "repairVerification"
|
|
58
|
+
| "agents"
|
|
59
|
+
> {
|
|
60
|
+
read?: string[];
|
|
61
|
+
phases: {
|
|
62
|
+
red?: PlanPhaseDraft;
|
|
63
|
+
green: PlanPhaseDraft;
|
|
64
|
+
refactor?: PlanPhaseDraft;
|
|
65
|
+
};
|
|
66
|
+
affectedVerification: DraftVerification;
|
|
67
|
+
repairVerification: DraftVerification;
|
|
68
|
+
agents: Omit<PlanTaskDraft["agents"], "managedOnly"> & { managedOnly?: true };
|
|
69
|
+
impactClosure: Omit<PlanTaskDraft["impactClosure"], "relatedTests"> & {
|
|
70
|
+
relatedTests: Array<
|
|
71
|
+
Omit<
|
|
72
|
+
PlanTaskDraft["impactClosure"]["relatedTests"][number],
|
|
73
|
+
"disposition"
|
|
74
|
+
> & {
|
|
75
|
+
disposition?: PlanTaskDraft["impactClosure"]["relatedTests"][number]["disposition"];
|
|
76
|
+
}
|
|
77
|
+
>;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export interface PlanVerificationInput {
|
|
81
|
+
baseline: Partial<Omit<PlanVerification["baseline"], "fullSuite">> & {
|
|
82
|
+
fullSuite: DraftVerification;
|
|
83
|
+
};
|
|
84
|
+
change: Partial<Pick<PlanVerification["change"], "affected">> & {
|
|
85
|
+
fullSuite: DraftVerification;
|
|
86
|
+
postApply: DraftVerification;
|
|
87
|
+
};
|
|
88
|
+
artifactCorrection: PlanVerification["artifactCorrection"];
|
|
89
|
+
repair: Pick<PlanVerification["repair"], "maxAttempts"> &
|
|
90
|
+
Partial<Omit<PlanVerification["repair"], "maxAttempts">>;
|
|
91
|
+
agentsCheckpoint?: Omit<
|
|
92
|
+
PlanVerification["agentsCheckpoint"],
|
|
93
|
+
"verification"
|
|
94
|
+
> & { verification: DraftVerification | null };
|
|
95
|
+
}
|
|
96
|
+
/** Authoring only. The sealed plan does not inherit this interface. */
|
|
97
|
+
export interface PlanDraft {
|
|
98
|
+
changeContract?: ChangeContract;
|
|
99
|
+
changeId: string;
|
|
100
|
+
tasks: PlanTaskInput[];
|
|
101
|
+
outputs: ImplementGraphOutput[];
|
|
102
|
+
verification: PlanVerificationInput;
|
|
103
|
+
tracking?: PlanTracking;
|
|
104
|
+
verificationDefinitions?: Record<string, VerificationDefinition>;
|
|
105
|
+
}
|
|
106
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
107
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
108
|
+
}
|
|
109
|
+
const ID = /^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$/u;
|
|
110
|
+
|
|
111
|
+
/** Expand finite, local syntax only. All authority and executable validation stays strict. */
|
|
112
|
+
export function expandPlanDraft(value: unknown): unknown {
|
|
113
|
+
if (
|
|
114
|
+
!isRecord(value) ||
|
|
115
|
+
!Array.isArray(value.tasks) ||
|
|
116
|
+
value.tasks.length > 128
|
|
117
|
+
)
|
|
118
|
+
return value;
|
|
119
|
+
const draft = structuredClone(value);
|
|
120
|
+
const diagnostics: DesignPlanDiagnostic[] = [];
|
|
121
|
+
const definitions = new Map<string, Record<string, unknown>>();
|
|
122
|
+
const source = draft.verificationDefinitions;
|
|
123
|
+
if (source !== undefined) {
|
|
124
|
+
if (!isRecord(source) || Object.keys(source).length > 512) {
|
|
125
|
+
throw new DesignPlanValidationError("verification-definition-invalid", [
|
|
126
|
+
{
|
|
127
|
+
code: "verification-definition-invalid",
|
|
128
|
+
field: "verificationDefinitions",
|
|
129
|
+
},
|
|
130
|
+
]);
|
|
131
|
+
}
|
|
132
|
+
for (const [name, definition] of Object.entries(source)) {
|
|
133
|
+
const validated =
|
|
134
|
+
isRecord(definition) &&
|
|
135
|
+
ID.test(name) &&
|
|
136
|
+
![
|
|
137
|
+
"id",
|
|
138
|
+
"classification",
|
|
139
|
+
"expectedFailure",
|
|
140
|
+
"executionBindings",
|
|
141
|
+
"use",
|
|
142
|
+
"steps",
|
|
143
|
+
].some((key) => Object.hasOwn(definition, key)) &&
|
|
144
|
+
validateVerificationContract({
|
|
145
|
+
...definition,
|
|
146
|
+
id: "definition",
|
|
147
|
+
classification: "expected-green",
|
|
148
|
+
}).ok;
|
|
149
|
+
if (!validated)
|
|
150
|
+
diagnostics.push({
|
|
151
|
+
code: "verification-definition-invalid",
|
|
152
|
+
field: "verificationDefinitions",
|
|
153
|
+
});
|
|
154
|
+
else definitions.set(name, definition as Record<string, unknown>);
|
|
155
|
+
}
|
|
156
|
+
delete draft.verificationDefinitions;
|
|
157
|
+
}
|
|
158
|
+
const generated = new Set<string>();
|
|
159
|
+
const authored = new Set<string>();
|
|
160
|
+
const collect = (node: unknown): void => {
|
|
161
|
+
if (Array.isArray(node)) node.forEach(collect);
|
|
162
|
+
else if (isRecord(node)) {
|
|
163
|
+
if (
|
|
164
|
+
["vitest", "static-check", "package-script", "steps"].includes(
|
|
165
|
+
String(node.kind),
|
|
166
|
+
) &&
|
|
167
|
+
typeof node.id === "string"
|
|
168
|
+
)
|
|
169
|
+
authored.add(node.id);
|
|
170
|
+
Object.entries(node)
|
|
171
|
+
.filter(([key]) => key !== "changeContract")
|
|
172
|
+
.forEach(([, item]) => {
|
|
173
|
+
collect(item);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
collect(draft);
|
|
178
|
+
const expand = (
|
|
179
|
+
node: unknown,
|
|
180
|
+
field: string,
|
|
181
|
+
classification: "expected-red" | "expected-green" | "expected-refactor",
|
|
182
|
+
taskId?: string,
|
|
183
|
+
): unknown => {
|
|
184
|
+
if (!isRecord(node)) return node;
|
|
185
|
+
if (!Object.hasOwn(node, "use")) {
|
|
186
|
+
if (
|
|
187
|
+
!["vitest", "package-script", "static-check"].includes(
|
|
188
|
+
String(node.kind),
|
|
189
|
+
)
|
|
190
|
+
)
|
|
191
|
+
return node;
|
|
192
|
+
if (Object.hasOwn(node, "id") && Object.hasOwn(node, "classification"))
|
|
193
|
+
return node;
|
|
194
|
+
const command = structuredClone(node);
|
|
195
|
+
delete command.id;
|
|
196
|
+
delete command.classification;
|
|
197
|
+
delete command.executionBindings;
|
|
198
|
+
if (command.kind === "vitest" && Array.isArray(command.testFiles))
|
|
199
|
+
command.testFiles = [...command.testFiles].sort(
|
|
200
|
+
compareCanonicalStrings,
|
|
201
|
+
);
|
|
202
|
+
const id = `verify-${createHash("sha256")
|
|
203
|
+
.update(
|
|
204
|
+
canonicalJson([
|
|
205
|
+
"cadence-verifier-v1",
|
|
206
|
+
draft.changeId,
|
|
207
|
+
taskId ?? null,
|
|
208
|
+
field,
|
|
209
|
+
command,
|
|
210
|
+
classification,
|
|
211
|
+
]),
|
|
212
|
+
)
|
|
213
|
+
.digest("hex")}`;
|
|
214
|
+
if (!Object.hasOwn(node, "id")) {
|
|
215
|
+
if (generated.has(id) || authored.has(id))
|
|
216
|
+
diagnostics.push({
|
|
217
|
+
code: "verification-identity-collision",
|
|
218
|
+
field,
|
|
219
|
+
taskId,
|
|
220
|
+
});
|
|
221
|
+
generated.add(id);
|
|
222
|
+
}
|
|
223
|
+
return { id, classification, ...node };
|
|
224
|
+
}
|
|
225
|
+
if (
|
|
226
|
+
Object.keys(node).some(
|
|
227
|
+
(key) => !["use", "expectedFailure"].includes(key),
|
|
228
|
+
) ||
|
|
229
|
+
typeof node.use !== "string" ||
|
|
230
|
+
!ID.test(node.use)
|
|
231
|
+
) {
|
|
232
|
+
diagnostics.push({
|
|
233
|
+
code: "verification-reference-invalid",
|
|
234
|
+
field,
|
|
235
|
+
taskId,
|
|
236
|
+
});
|
|
237
|
+
return node;
|
|
238
|
+
}
|
|
239
|
+
const definition = definitions.get(node.use);
|
|
240
|
+
if (!definition) {
|
|
241
|
+
diagnostics.push({
|
|
242
|
+
code: "verification-reference-unavailable",
|
|
243
|
+
field,
|
|
244
|
+
taskId,
|
|
245
|
+
});
|
|
246
|
+
return node;
|
|
247
|
+
}
|
|
248
|
+
const command = structuredClone(definition);
|
|
249
|
+
if (command.kind === "vitest" && Array.isArray(command.testFiles))
|
|
250
|
+
command.testFiles = [...command.testFiles].sort(compareCanonicalStrings);
|
|
251
|
+
if (Object.hasOwn(node, "expectedFailure"))
|
|
252
|
+
command.expectedFailure = node.expectedFailure;
|
|
253
|
+
const id = `verify-${createHash("sha256")
|
|
254
|
+
.update(
|
|
255
|
+
canonicalJson([
|
|
256
|
+
"cadence-verifier-v1",
|
|
257
|
+
draft.changeId,
|
|
258
|
+
taskId ?? null,
|
|
259
|
+
field,
|
|
260
|
+
command,
|
|
261
|
+
classification,
|
|
262
|
+
]),
|
|
263
|
+
)
|
|
264
|
+
.digest("hex")}`;
|
|
265
|
+
if (generated.has(id) || authored.has(id))
|
|
266
|
+
diagnostics.push({
|
|
267
|
+
code: "verification-identity-collision",
|
|
268
|
+
field,
|
|
269
|
+
taskId,
|
|
270
|
+
});
|
|
271
|
+
generated.add(id);
|
|
272
|
+
return {
|
|
273
|
+
...command,
|
|
274
|
+
id,
|
|
275
|
+
classification,
|
|
276
|
+
...(Object.hasOwn(node, "expectedFailure")
|
|
277
|
+
? { expectedFailure: node.expectedFailure }
|
|
278
|
+
: {}),
|
|
279
|
+
};
|
|
280
|
+
};
|
|
281
|
+
const defaults = (node: unknown, fields: Record<string, unknown>) => {
|
|
282
|
+
if (isRecord(node))
|
|
283
|
+
for (const [key, value] of Object.entries(fields))
|
|
284
|
+
if (!Object.hasOwn(node, key)) node[key] = value;
|
|
285
|
+
};
|
|
286
|
+
const tasks = draft.tasks as unknown[];
|
|
287
|
+
for (const task of tasks) {
|
|
288
|
+
if (!isRecord(task)) continue;
|
|
289
|
+
const taskId = typeof task.taskId === "string" ? task.taskId : undefined;
|
|
290
|
+
const common = task.read;
|
|
291
|
+
if (Object.hasOwn(task, "read")) {
|
|
292
|
+
if (!Array.isArray(common) || !common.every(isValidRelativePath))
|
|
293
|
+
diagnostics.push({
|
|
294
|
+
code: "draft-common-read-invalid",
|
|
295
|
+
field: "read",
|
|
296
|
+
taskId,
|
|
297
|
+
});
|
|
298
|
+
delete task.read;
|
|
299
|
+
}
|
|
300
|
+
defaults(task.agents, { managedOnly: true });
|
|
301
|
+
if (isRecord(task.phases))
|
|
302
|
+
for (const [name, phase] of Object.entries(task.phases)) {
|
|
303
|
+
if (!isRecord(phase)) continue;
|
|
304
|
+
if (Array.isArray(common) && common.every(isValidRelativePath)) {
|
|
305
|
+
if (phase.read === undefined) phase.read = [...common];
|
|
306
|
+
else if (Array.isArray(phase.read))
|
|
307
|
+
phase.read = [...new Set([...common, ...phase.read])];
|
|
308
|
+
}
|
|
309
|
+
const classification =
|
|
310
|
+
name === "refactor"
|
|
311
|
+
? "expected-refactor"
|
|
312
|
+
: name === "red" &&
|
|
313
|
+
!["mechanical", "refactor"].includes(
|
|
314
|
+
String(task.verificationMode),
|
|
315
|
+
)
|
|
316
|
+
? "expected-red"
|
|
317
|
+
: "expected-green";
|
|
318
|
+
phase.verification = expand(
|
|
319
|
+
phase.verification,
|
|
320
|
+
`phases.${name}.verification`,
|
|
321
|
+
classification,
|
|
322
|
+
taskId,
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
for (const field of ["affectedVerification", "repairVerification"])
|
|
326
|
+
task[field] = expand(task[field], field, "expected-green", taskId);
|
|
327
|
+
}
|
|
328
|
+
if (isRecord(draft.verification)) {
|
|
329
|
+
const verification = draft.verification;
|
|
330
|
+
defaults(verification.baseline, {
|
|
331
|
+
target: "task-red-contracts",
|
|
332
|
+
affected: "task-affected-contracts",
|
|
333
|
+
failureIdentity: "normalized",
|
|
334
|
+
});
|
|
335
|
+
defaults(verification.change, { affected: "task-affected-contracts" });
|
|
336
|
+
defaults(verification.repair, {
|
|
337
|
+
inBoundaryOnly: true,
|
|
338
|
+
approvalOnBoundaryExpansion: true,
|
|
339
|
+
attribution: ["pre-existing", "introduced", "unresolved", "environment"],
|
|
340
|
+
});
|
|
341
|
+
if (
|
|
342
|
+
!Object.hasOwn(verification, "agentsCheckpoint") &&
|
|
343
|
+
tasks.every(
|
|
344
|
+
(task) =>
|
|
345
|
+
isRecord(task) &&
|
|
346
|
+
isRecord(task.agents) &&
|
|
347
|
+
task.agents.impact === "none",
|
|
348
|
+
)
|
|
349
|
+
) {
|
|
350
|
+
verification.agentsCheckpoint = {
|
|
351
|
+
required: false,
|
|
352
|
+
verification: null,
|
|
353
|
+
operations: [],
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
for (const [group, fields] of [
|
|
357
|
+
["baseline", ["fullSuite"]],
|
|
358
|
+
["change", ["fullSuite", "postApply"]],
|
|
359
|
+
["agentsCheckpoint", ["verification"]],
|
|
360
|
+
] as const) {
|
|
361
|
+
const block = verification[group];
|
|
362
|
+
if (isRecord(block))
|
|
363
|
+
for (const field of fields)
|
|
364
|
+
block[field] = expand(
|
|
365
|
+
block[field],
|
|
366
|
+
`verification.${group}.${field}`,
|
|
367
|
+
"expected-green",
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
if (diagnostics.length)
|
|
372
|
+
throw new DesignPlanValidationError(
|
|
373
|
+
"design-plan-expansion-invalid",
|
|
374
|
+
diagnostics,
|
|
375
|
+
);
|
|
376
|
+
return JSON.stringify(draft) === JSON.stringify(value) ? value : draft;
|
|
377
|
+
}
|
|
378
|
+
/** Derive only omitted fields; the strict normalizer and graph still own admission. */
|
|
379
|
+
export function preparePlanDraft(value: unknown): unknown {
|
|
380
|
+
if (
|
|
381
|
+
!isRecord(value) ||
|
|
382
|
+
!Array.isArray(value.tasks) ||
|
|
383
|
+
value.tasks.length > 128 ||
|
|
384
|
+
!Array.isArray(value.outputs) ||
|
|
385
|
+
value.outputs.length > 512
|
|
386
|
+
)
|
|
387
|
+
return value;
|
|
388
|
+
const draft = structuredClone(value);
|
|
389
|
+
let changed = false;
|
|
390
|
+
const tasks = (draft.tasks as unknown[]).filter(isRecord);
|
|
391
|
+
const outputs = (draft.outputs as unknown[]).filter(isRecord);
|
|
392
|
+
const diagnostics: DesignPlanDiagnostic[] = [];
|
|
393
|
+
const writes = (task: Record<string, unknown>): Set<string> => {
|
|
394
|
+
const paths = new Set<string>();
|
|
395
|
+
if (!isRecord(task.phases)) return paths;
|
|
396
|
+
for (const [phase, boundary] of Object.entries(task.phases)) {
|
|
397
|
+
if (!isRecord(boundary)) continue;
|
|
398
|
+
if (
|
|
399
|
+
phase === "red" &&
|
|
400
|
+
["mechanical", "refactor"].includes(String(task.verificationMode))
|
|
401
|
+
)
|
|
402
|
+
continue;
|
|
403
|
+
for (const candidates of [boundary.write, boundary.delete]) {
|
|
404
|
+
if (!Array.isArray(candidates)) continue;
|
|
405
|
+
for (const file of candidates)
|
|
406
|
+
if (isValidRelativePath(file)) paths.add(file);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return paths;
|
|
410
|
+
};
|
|
411
|
+
const owners = new Map<string, Record<string, unknown>[]>();
|
|
412
|
+
for (const task of tasks)
|
|
413
|
+
for (const file of writes(task))
|
|
414
|
+
owners.set(file, [...(owners.get(file) ?? []), task]);
|
|
415
|
+
if (!Object.hasOwn(draft, "tracking")) {
|
|
416
|
+
changed = true;
|
|
417
|
+
draft.tracking = {
|
|
418
|
+
path: "tasks.md",
|
|
419
|
+
format: "markdown-checkbox",
|
|
420
|
+
taskIds: (draft.tasks as unknown[]).map((task) =>
|
|
421
|
+
isRecord(task) ? task.taskId : undefined,
|
|
422
|
+
),
|
|
423
|
+
completionOwner: "parent",
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
for (const task of tasks) {
|
|
427
|
+
const taskId = typeof task.taskId === "string" ? task.taskId : undefined;
|
|
428
|
+
if (isRecord(task.phases))
|
|
429
|
+
for (const [phase, boundary] of Object.entries(task.phases)) {
|
|
430
|
+
if (
|
|
431
|
+
!isRecord(boundary) ||
|
|
432
|
+
Object.hasOwn(boundary, "verificationInputs")
|
|
433
|
+
)
|
|
434
|
+
continue;
|
|
435
|
+
const verification = validateVerificationContract(
|
|
436
|
+
boundary.verification,
|
|
437
|
+
);
|
|
438
|
+
// Invalid commands get their normal structural diagnostic; never infer from invalid paths.
|
|
439
|
+
if (!verification.ok) continue;
|
|
440
|
+
changed = true;
|
|
441
|
+
boundary.verificationInputs = verificationInputPaths(
|
|
442
|
+
verification.value,
|
|
443
|
+
).map((file) => {
|
|
444
|
+
const producers = outputs.filter((output) => output.path === file);
|
|
445
|
+
if (producers.length > 1)
|
|
446
|
+
diagnostics.push({
|
|
447
|
+
code: "multiple-output-producers",
|
|
448
|
+
taskId,
|
|
449
|
+
phase,
|
|
450
|
+
field: "outputs",
|
|
451
|
+
path: file,
|
|
452
|
+
});
|
|
453
|
+
return producers.length === 1
|
|
454
|
+
? { kind: "output", outputId: producers[0]?.id }
|
|
455
|
+
: { kind: "workspace", path: file };
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
if (
|
|
459
|
+
!isRecord(task.impactClosure) ||
|
|
460
|
+
!Array.isArray(task.impactClosure.relatedTests)
|
|
461
|
+
)
|
|
462
|
+
continue;
|
|
463
|
+
for (const test of task.impactClosure.relatedTests) {
|
|
464
|
+
if (
|
|
465
|
+
!isRecord(test) ||
|
|
466
|
+
Object.hasOwn(test, "disposition") ||
|
|
467
|
+
!isValidRelativePath(test.path)
|
|
468
|
+
)
|
|
469
|
+
continue;
|
|
470
|
+
changed = true;
|
|
471
|
+
const writers = owners.get(test.path) ?? [];
|
|
472
|
+
if (writers.includes(task)) test.disposition = "current-task";
|
|
473
|
+
else if (writers.length === 0) test.disposition = "unaffected";
|
|
474
|
+
else if (writers.length === 1) {
|
|
475
|
+
test.disposition = "regression-task";
|
|
476
|
+
if (!Object.hasOwn(test, "regressionTaskId"))
|
|
477
|
+
test.regressionTaskId = writers[0]?.taskId;
|
|
478
|
+
} else
|
|
479
|
+
diagnostics.push({
|
|
480
|
+
code: "related-test-owner-ambiguous",
|
|
481
|
+
taskId,
|
|
482
|
+
field: "impactClosure.relatedTests",
|
|
483
|
+
path: test.path,
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
if (diagnostics.length)
|
|
488
|
+
throw new DesignPlanValidationError(
|
|
489
|
+
"delivery-plan-inference-ambiguous",
|
|
490
|
+
diagnostics,
|
|
491
|
+
);
|
|
492
|
+
return changed ? draft : value;
|
|
493
|
+
}
|
package/src/route-policy.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import { lstatSync, readFileSync } from "node:fs";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
|
+
import { canonicalJson } from "./canonical.ts";
|
|
5
6
|
import { ROLES } from "./contracts.ts";
|
|
6
7
|
|
|
7
8
|
export const ROUTE_DIALECTS = [
|
|
@@ -166,17 +167,6 @@ function httpUrl(value: unknown): value is string {
|
|
|
166
167
|
}
|
|
167
168
|
}
|
|
168
169
|
|
|
169
|
-
function canonicalJson(value: unknown): string {
|
|
170
|
-
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
|
|
171
|
-
if (isRecord(value)) {
|
|
172
|
-
return `{${Object.entries(value)
|
|
173
|
-
.sort(([left], [right]) => left.localeCompare(right))
|
|
174
|
-
.map(([key, entry]) => `${JSON.stringify(key)}:${canonicalJson(entry)}`)
|
|
175
|
-
.join(",")}}`;
|
|
176
|
-
}
|
|
177
|
-
return JSON.stringify(value) ?? "null";
|
|
178
|
-
}
|
|
179
|
-
|
|
180
170
|
function routeFingerprint(route: Record<string, unknown>): string {
|
|
181
171
|
return createHash("sha256").update(canonicalJson(route)).digest("hex");
|
|
182
172
|
}
|