@theoklitosbam7/verdict-kit 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +625 -2
- package/config/routing/engineering.action-risk.consensus.v1.json +16 -0
- package/config/routing/engineering.task-route.cascade.v1.json +21 -0
- package/dist/action-policy.d.ts +104 -0
- package/dist/action-policy.js +173 -0
- package/dist/cache.d.ts +17 -0
- package/dist/cache.js +139 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +142 -0
- package/dist/concurrency.d.ts +12 -0
- package/dist/concurrency.js +110 -0
- package/dist/config.d.ts +61 -0
- package/dist/config.js +247 -0
- package/dist/decision.d.ts +75 -0
- package/dist/decision.js +278 -0
- package/dist/engine.d.ts +63 -0
- package/dist/engine.js +79 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.js +19 -0
- package/dist/http.d.ts +18 -0
- package/dist/http.js +140 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +14 -0
- package/dist/package-info.d.ts +1 -0
- package/dist/package-info.js +5 -0
- package/dist/policy/evaluate.d.ts +7 -0
- package/dist/policy/evaluate.js +39 -0
- package/dist/policy/events.d.ts +32 -0
- package/dist/policy/events.js +31 -0
- package/dist/policy/payload.d.ts +6 -0
- package/dist/policy/payload.js +11 -0
- package/dist/policy/registry.d.ts +22 -0
- package/dist/policy/registry.js +89 -0
- package/dist/policy/schema.d.ts +198 -0
- package/dist/policy/schema.js +112 -0
- package/dist/policy/scope.d.ts +5 -0
- package/dist/policy/scope.js +120 -0
- package/dist/probability.d.ts +1 -0
- package/dist/probability.js +3 -0
- package/dist/providers/discovery.d.ts +10 -0
- package/dist/providers/discovery.js +113 -0
- package/dist/providers/index.d.ts +56 -0
- package/dist/providers/index.js +83 -0
- package/dist/providers/ollama.d.ts +12 -0
- package/dist/providers/ollama.js +93 -0
- package/dist/providers/opencode-go.d.ts +3 -0
- package/dist/providers/opencode-go.js +40 -0
- package/dist/providers/system-one.d.ts +21 -0
- package/dist/providers/system-one.js +44 -0
- package/dist/providers/typesafe.d.ts +3 -0
- package/dist/providers/typesafe.js +36 -0
- package/dist/public.d.ts +5 -0
- package/dist/public.js +5 -0
- package/dist/response.d.ts +6 -0
- package/dist/response.js +104 -0
- package/dist/routing/cascade.d.ts +112 -0
- package/dist/routing/cascade.js +220 -0
- package/dist/routing/consensus.d.ts +148 -0
- package/dist/routing/consensus.js +367 -0
- package/dist/routing/retry.d.ts +3 -0
- package/dist/routing/retry.js +47 -0
- package/dist/routing/router.d.ts +12 -0
- package/dist/routing/router.js +27 -0
- package/dist/schemas.d.ts +339 -0
- package/dist/schemas.js +283 -0
- package/dist/server.d.ts +42 -0
- package/dist/server.js +416 -0
- package/dist/telemetry/events.d.ts +15 -0
- package/dist/telemetry/events.js +87 -0
- package/dist/telemetry/sink.d.ts +7 -0
- package/dist/telemetry/sink.js +59 -0
- package/dist/templates/registry.d.ts +13 -0
- package/dist/templates/registry.js +100 -0
- package/dist/templates/schema.d.ts +115 -0
- package/dist/templates/schema.js +70 -0
- package/dist/types.d.ts +86 -0
- package/dist/types.js +2 -0
- package/package.json +77 -3
- package/policies/engineering-default.v1.json +36 -0
- package/policies/engineering-strict.v1.json +36 -0
- package/templates/engineering.action-risk.v1.json +32 -0
- package/templates/engineering.compatibility.v1.json +25 -0
- package/templates/engineering.pr-risk.v1.json +26 -0
- package/templates/engineering.security-sensitivity.v1.json +20 -0
- package/templates/engineering.task-route.v1.json +27 -0
- package/templates/engineering.test-depth.v1.json +26 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import * as z from "zod/v4";
|
|
2
|
+
import { type DecisionErrorCode } from "../errors.js";
|
|
3
|
+
import type { ProviderRegistry } from "../providers/index.js";
|
|
4
|
+
import type { TemplateRegistry } from "../templates/registry.js";
|
|
5
|
+
import { type TemplateIdentity } from "../templates/schema.js";
|
|
6
|
+
import type { DecisionContext, DecisionQuestions, DecisionResponse, JsonValue } from "../types.js";
|
|
7
|
+
export declare const cascadeProfileSchema: z.ZodObject<{
|
|
8
|
+
id: z.ZodString;
|
|
9
|
+
version: z.ZodNumber;
|
|
10
|
+
template: z.ZodObject<{
|
|
11
|
+
id: z.ZodString;
|
|
12
|
+
version: z.ZodNumber;
|
|
13
|
+
}, z.core.$strict>;
|
|
14
|
+
max_attempts: z.ZodNumber;
|
|
15
|
+
max_non_local_attempts: z.ZodNumber;
|
|
16
|
+
stages: z.ZodArray<z.ZodObject<{
|
|
17
|
+
provider: z.ZodEnum<{
|
|
18
|
+
ollama: "ollama";
|
|
19
|
+
"opencode-go": "opencode-go";
|
|
20
|
+
typesafe: "typesafe";
|
|
21
|
+
}>;
|
|
22
|
+
model: z.ZodString;
|
|
23
|
+
revision: z.ZodString;
|
|
24
|
+
min_margin: z.ZodNumber;
|
|
25
|
+
}, z.core.$strict>>;
|
|
26
|
+
calibration: z.ZodObject<{
|
|
27
|
+
dataset: z.ZodString;
|
|
28
|
+
sha256: z.ZodString;
|
|
29
|
+
recorded: z.ZodISODate;
|
|
30
|
+
}, z.core.$strict>;
|
|
31
|
+
}, z.core.$strict>;
|
|
32
|
+
export type CascadeProfile = z.infer<typeof cascadeProfileSchema>;
|
|
33
|
+
export declare const cascadeInputSchema: z.ZodObject<{
|
|
34
|
+
cascade: z.ZodObject<{
|
|
35
|
+
id: z.ZodString;
|
|
36
|
+
version: z.ZodNumber;
|
|
37
|
+
}, z.core.$strict>;
|
|
38
|
+
state: z.ZodPipe<z.ZodUnknown, z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
|
|
39
|
+
}, z.core.$strict>;
|
|
40
|
+
export declare const cascadeOutputSchema: z.ZodObject<{
|
|
41
|
+
cascade: z.ZodObject<{
|
|
42
|
+
id: z.ZodString;
|
|
43
|
+
version: z.ZodNumber;
|
|
44
|
+
}, z.core.$strict>;
|
|
45
|
+
template: z.ZodObject<{
|
|
46
|
+
id: z.ZodString;
|
|
47
|
+
version: z.ZodNumber;
|
|
48
|
+
}, z.core.$strict>;
|
|
49
|
+
status: z.ZodEnum<{
|
|
50
|
+
abstained: "abstained";
|
|
51
|
+
accepted: "accepted";
|
|
52
|
+
}>;
|
|
53
|
+
result: z.ZodOptional<z.ZodObject<{
|
|
54
|
+
provider: z.ZodString;
|
|
55
|
+
model: z.ZodOptional<z.ZodString>;
|
|
56
|
+
answers: z.ZodRecord<z.ZodString, z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
57
|
+
confidence: z.ZodOptional<z.ZodNumber>;
|
|
58
|
+
probabilities: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
|
|
59
|
+
legend: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
60
|
+
type: z.ZodLiteral<"choice">;
|
|
61
|
+
choice: z.ZodString;
|
|
62
|
+
}, z.core.$catchall<z.ZodPipe<z.ZodUnknown, z.ZodJSONSchema>>>, z.ZodObject<{
|
|
63
|
+
confidence: z.ZodOptional<z.ZodNumber>;
|
|
64
|
+
probabilities: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
|
|
65
|
+
legend: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
66
|
+
type: z.ZodLiteral<"noul">;
|
|
67
|
+
noul: z.ZodNumber;
|
|
68
|
+
}, z.core.$catchall<z.ZodPipe<z.ZodUnknown, z.ZodJSONSchema>>>, z.ZodObject<{
|
|
69
|
+
confidence: z.ZodOptional<z.ZodNumber>;
|
|
70
|
+
probabilities: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
|
|
71
|
+
legend: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
72
|
+
type: z.ZodLiteral<"score">;
|
|
73
|
+
score: z.ZodNumber;
|
|
74
|
+
}, z.core.$catchall<z.ZodPipe<z.ZodUnknown, z.ZodJSONSchema>>>], "type">>;
|
|
75
|
+
usage: z.ZodOptional<z.ZodJSONSchema>;
|
|
76
|
+
}, z.core.$strip>>;
|
|
77
|
+
stages: z.ZodArray<z.ZodObject<{
|
|
78
|
+
provider: z.ZodString;
|
|
79
|
+
model: z.ZodString;
|
|
80
|
+
outcome: z.ZodString;
|
|
81
|
+
margin: z.ZodOptional<z.ZodNumber>;
|
|
82
|
+
}, z.core.$strip>>;
|
|
83
|
+
}, z.core.$strip>;
|
|
84
|
+
export type ResolvedCascade = {
|
|
85
|
+
definition: CascadeProfile;
|
|
86
|
+
questions: DecisionQuestions;
|
|
87
|
+
};
|
|
88
|
+
export type CascadeStage = {
|
|
89
|
+
provider: string;
|
|
90
|
+
model: string;
|
|
91
|
+
outcome: "accepted" | "uncertain" | "uncalibrated" | "budget" | Exclude<DecisionErrorCode, "cancelled" | "configuration" | "invalid_request">;
|
|
92
|
+
margin?: number;
|
|
93
|
+
};
|
|
94
|
+
export type CascadeOutcome = {
|
|
95
|
+
status: "accepted" | "abstained";
|
|
96
|
+
result?: DecisionResponse;
|
|
97
|
+
stages: CascadeStage[];
|
|
98
|
+
};
|
|
99
|
+
export declare function createCascadeRegistry(definitions: readonly unknown[], templates: TemplateRegistry): {
|
|
100
|
+
get({ id, version }: TemplateIdentity): ResolvedCascade;
|
|
101
|
+
list(): {
|
|
102
|
+
id: string;
|
|
103
|
+
version: number;
|
|
104
|
+
template: {
|
|
105
|
+
id: string;
|
|
106
|
+
version: number;
|
|
107
|
+
};
|
|
108
|
+
}[];
|
|
109
|
+
};
|
|
110
|
+
export type CascadeRegistry = ReturnType<typeof createCascadeRegistry>;
|
|
111
|
+
export declare function loadCascadeRegistry(templates: TemplateRegistry): CascadeRegistry;
|
|
112
|
+
export declare function runCascade(cascade: ResolvedCascade, state: JsonValue, registry: ProviderRegistry, context?: DecisionContext): Promise<CascadeOutcome>;
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import * as z from "zod/v4";
|
|
3
|
+
import { providerIds } from "../config.js";
|
|
4
|
+
import { decisionStopError } from "../concurrency.js";
|
|
5
|
+
import { routeDecision } from "../decision.js";
|
|
6
|
+
import { DecisionError } from "../errors.js";
|
|
7
|
+
import { isProbability } from "../probability.js";
|
|
8
|
+
import { decisionOutputSchema, jsonValueSchema, withinPayloadLimit } from "../schemas.js";
|
|
9
|
+
import { templateIdentitySchema } from "../templates/schema.js";
|
|
10
|
+
const stageSchema = z.strictObject({
|
|
11
|
+
provider: z.enum(providerIds),
|
|
12
|
+
model: z.string().min(1).max(256),
|
|
13
|
+
// The evaluated model revision, at least the 12-character Ollama ID prefix of its digest.
|
|
14
|
+
revision: z.string().regex(/^[0-9a-f]{12,64}$/),
|
|
15
|
+
// The smallest gap between the top two probabilities that this stage accepts.
|
|
16
|
+
min_margin: z.number().min(0).max(1),
|
|
17
|
+
});
|
|
18
|
+
export const cascadeProfileSchema = templateIdentitySchema
|
|
19
|
+
.extend({
|
|
20
|
+
template: templateIdentitySchema,
|
|
21
|
+
max_attempts: z.number().int().min(1),
|
|
22
|
+
max_non_local_attempts: z.number().int().min(0),
|
|
23
|
+
stages: z.array(stageSchema).min(2).max(4),
|
|
24
|
+
calibration: z.strictObject({
|
|
25
|
+
dataset: z.string().min(1).max(128),
|
|
26
|
+
sha256: z.string().regex(/^[0-9a-f]{64}$/),
|
|
27
|
+
recorded: z.iso.date(),
|
|
28
|
+
}),
|
|
29
|
+
})
|
|
30
|
+
.refine(({ max_attempts, stages }) => max_attempts <= stages.length &&
|
|
31
|
+
new Set(stages.map(({ provider, model }) => `${provider}:${model}`)).size === stages.length);
|
|
32
|
+
export const cascadeInputSchema = z
|
|
33
|
+
.strictObject({
|
|
34
|
+
cascade: templateIdentitySchema.describe("Exact ID and version of a trusted cascade profile."),
|
|
35
|
+
state: jsonValueSchema.describe("State for the cascade profile's template."),
|
|
36
|
+
})
|
|
37
|
+
.refine(withinPayloadLimit, { message: "Request payload is too large." });
|
|
38
|
+
export const cascadeOutputSchema = z.object({
|
|
39
|
+
cascade: templateIdentitySchema,
|
|
40
|
+
template: templateIdentitySchema,
|
|
41
|
+
status: z.enum(["accepted", "abstained"]),
|
|
42
|
+
result: decisionOutputSchema.optional(),
|
|
43
|
+
stages: z.array(z.object({
|
|
44
|
+
provider: z.string(),
|
|
45
|
+
model: z.string(),
|
|
46
|
+
outcome: z.string(),
|
|
47
|
+
margin: z.number().optional(),
|
|
48
|
+
})),
|
|
49
|
+
});
|
|
50
|
+
const bundledFiles = [
|
|
51
|
+
new URL("../../config/routing/engineering.task-route.cascade.v1.json", import.meta.url),
|
|
52
|
+
];
|
|
53
|
+
export function createCascadeRegistry(definitions, templates) {
|
|
54
|
+
const entries = new Map();
|
|
55
|
+
for (const raw of definitions) {
|
|
56
|
+
const parsed = cascadeProfileSchema.safeParse(raw);
|
|
57
|
+
if (!parsed.success)
|
|
58
|
+
throw new DecisionError("configuration", "Invalid cascade profile.");
|
|
59
|
+
const definition = parsed.data;
|
|
60
|
+
const key = `${definition.id}@${definition.version}`;
|
|
61
|
+
if (entries.has(key))
|
|
62
|
+
throw new DecisionError("configuration", `Duplicate cascade profile ${key}.`);
|
|
63
|
+
let questions;
|
|
64
|
+
try {
|
|
65
|
+
({ questions } = templates.get(definition.template));
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
throw new DecisionError("configuration", `Cascade profile ${key} names an unknown template.`);
|
|
69
|
+
}
|
|
70
|
+
entries.set(key, { definition, questions });
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
get({ id, version }) {
|
|
74
|
+
const entry = entries.get(`${id}@${version}`);
|
|
75
|
+
if (!entry)
|
|
76
|
+
throw new DecisionError("invalid_request", "Unknown cascade profile ID and version.");
|
|
77
|
+
return structuredClone(entry);
|
|
78
|
+
},
|
|
79
|
+
list() {
|
|
80
|
+
return Array.from(entries.values(), ({ definition: { id, version, template } }) => ({
|
|
81
|
+
id,
|
|
82
|
+
version,
|
|
83
|
+
template,
|
|
84
|
+
})).sort((left, right) => left.id.localeCompare(right.id) || left.version - right.version);
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export function loadCascadeRegistry(templates) {
|
|
89
|
+
const definitions = bundledFiles.map((file) => {
|
|
90
|
+
try {
|
|
91
|
+
return JSON.parse(readFileSync(file, "utf8"));
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
throw new DecisionError("configuration", "Could not read a bundled cascade profile.");
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
return createCascadeRegistry(definitions, templates);
|
|
98
|
+
}
|
|
99
|
+
function distributionMargin(probabilities, labels, choice) {
|
|
100
|
+
if (typeof probabilities !== "object" || probabilities === null || Array.isArray(probabilities)) {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
const values = [];
|
|
104
|
+
for (const label of labels) {
|
|
105
|
+
const value = Object.hasOwn(probabilities, label) ? probabilities[label] : undefined;
|
|
106
|
+
if (!isProbability(value))
|
|
107
|
+
return undefined;
|
|
108
|
+
values.push(value);
|
|
109
|
+
}
|
|
110
|
+
values.sort((left, right) => right - left);
|
|
111
|
+
if (choice !== undefined && (!labels.includes(choice) || probabilities[choice] !== values[0])) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
return values[0] - values[1];
|
|
115
|
+
}
|
|
116
|
+
// The class margin is the gap between the two most likely answers. Without a complete
|
|
117
|
+
// probability distribution, the margin is unknown and the answer is never accepted.
|
|
118
|
+
function answerMargin(question, answer) {
|
|
119
|
+
if (!answer || answer.type !== question.type)
|
|
120
|
+
return undefined;
|
|
121
|
+
if (answer.type === "noul")
|
|
122
|
+
return isProbability(answer.noul) ? Math.abs(2 * answer.noul - 1) : undefined;
|
|
123
|
+
const labels = question.type === "choice"
|
|
124
|
+
? Object.keys(question.criteria)
|
|
125
|
+
: question.type === "score"
|
|
126
|
+
? question.criteria.map((_, level) => String(level))
|
|
127
|
+
: [];
|
|
128
|
+
return distributionMargin(answer.probabilities, labels, answer.type === "choice" ? answer.choice : undefined);
|
|
129
|
+
}
|
|
130
|
+
function responseMargin(questions, response) {
|
|
131
|
+
let lowest = Infinity;
|
|
132
|
+
for (const [name, question] of Object.entries(questions)) {
|
|
133
|
+
const answer = Object.hasOwn(response.answers, name) ? response.answers[name] : undefined;
|
|
134
|
+
const margin = answerMargin(question, answer);
|
|
135
|
+
if (margin === undefined)
|
|
136
|
+
return undefined;
|
|
137
|
+
lowest = Math.min(lowest, margin);
|
|
138
|
+
}
|
|
139
|
+
return lowest;
|
|
140
|
+
}
|
|
141
|
+
// Thresholds apply only to the model revision that was evaluated.
|
|
142
|
+
async function revisionMatches(provider, model, revision, signal, deadlineAt) {
|
|
143
|
+
try {
|
|
144
|
+
const status = await provider.available(signal, deadlineAt);
|
|
145
|
+
const entry = status.models?.find((candidate) => candidate.name === model);
|
|
146
|
+
return entry?.status === "ready" && entry.revision?.toLowerCase().startsWith(revision) === true;
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export async function runCascade(cascade, state, registry, context = {}) {
|
|
153
|
+
const { definition, questions } = cascade;
|
|
154
|
+
// Every stage must pass the provider, model and locality rules before any inference starts.
|
|
155
|
+
for (const { provider, model } of definition.stages) {
|
|
156
|
+
registry.getProvider(provider);
|
|
157
|
+
registry.effectiveModel(provider, model);
|
|
158
|
+
if (registry.localOnly && registry.locality(provider, model) !== "local") {
|
|
159
|
+
throw new DecisionError("configuration", "Local-only mode rejects a non-local cascade stage.");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const first = registry.getProvider(definition.stages[0].provider);
|
|
163
|
+
const deadlineAt = Math.min(context.deadlineAt ?? Infinity, performance.now() + (first.timeoutMs ?? 30_000));
|
|
164
|
+
const { signal } = context;
|
|
165
|
+
const stages = [];
|
|
166
|
+
let attempts = 0;
|
|
167
|
+
let nonLocalAttempts = 0;
|
|
168
|
+
for (const stage of definition.stages) {
|
|
169
|
+
const stopped = decisionStopError({ signal, deadlineAt });
|
|
170
|
+
if (stopped?.code === "cancelled")
|
|
171
|
+
throw stopped;
|
|
172
|
+
if (stopped)
|
|
173
|
+
break;
|
|
174
|
+
const target = { provider: stage.provider, model: stage.model };
|
|
175
|
+
const nonLocal = registry.locality(stage.provider, stage.model) !== "local";
|
|
176
|
+
if (attempts >= definition.max_attempts ||
|
|
177
|
+
(nonLocal && nonLocalAttempts >= definition.max_non_local_attempts)) {
|
|
178
|
+
stages.push({ ...target, outcome: "budget" });
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
const provider = registry.getProvider(stage.provider);
|
|
182
|
+
const matches = await revisionMatches(provider, stage.model, stage.revision, signal, deadlineAt);
|
|
183
|
+
const stoppedAfterProbe = decisionStopError({ signal, deadlineAt });
|
|
184
|
+
if (stoppedAfterProbe?.code === "cancelled")
|
|
185
|
+
throw stoppedAfterProbe;
|
|
186
|
+
if (stoppedAfterProbe)
|
|
187
|
+
break;
|
|
188
|
+
if (!matches) {
|
|
189
|
+
stages.push({ ...target, outcome: "uncalibrated" });
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
attempts++;
|
|
193
|
+
if (nonLocal)
|
|
194
|
+
nonLocalAttempts++;
|
|
195
|
+
let response;
|
|
196
|
+
try {
|
|
197
|
+
({ result: response } = await routeDecision({ ...target, state, questions, context: { signal, deadlineAt } }, registry));
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
if (!(error instanceof DecisionError) ||
|
|
201
|
+
error.code === "cancelled" ||
|
|
202
|
+
error.code === "configuration" ||
|
|
203
|
+
error.code === "invalid_request") {
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
stages.push({ ...target, outcome: error.code });
|
|
207
|
+
if (error.code === "timeout")
|
|
208
|
+
break;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const margin = responseMargin(questions, response);
|
|
212
|
+
const measured = margin === undefined ? {} : { margin };
|
|
213
|
+
if (margin !== undefined && margin >= stage.min_margin) {
|
|
214
|
+
stages.push({ ...target, outcome: "accepted", ...measured });
|
|
215
|
+
return { status: "accepted", result: response, stages };
|
|
216
|
+
}
|
|
217
|
+
stages.push({ ...target, outcome: "uncertain", ...measured });
|
|
218
|
+
}
|
|
219
|
+
return { status: "abstained", stages };
|
|
220
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import * as z from "zod/v4";
|
|
2
|
+
import { type ActionPolicyRequest } from "../action-policy.js";
|
|
3
|
+
import { type DecisionErrorCode } from "../errors.js";
|
|
4
|
+
import type { ResolvedPolicyProfile } from "../policy/registry.js";
|
|
5
|
+
import type { ProviderRegistry } from "../providers/index.js";
|
|
6
|
+
import type { TemplateRegistry } from "../templates/registry.js";
|
|
7
|
+
import { type TemplateIdentity } from "../templates/schema.js";
|
|
8
|
+
import type { DecisionContext, DecisionQuestions, DecisionResponse, JsonValue } from "../types.js";
|
|
9
|
+
export declare const consensusProfileSchema: z.ZodObject<{
|
|
10
|
+
id: z.ZodString;
|
|
11
|
+
version: z.ZodNumber;
|
|
12
|
+
template: z.ZodObject<{
|
|
13
|
+
id: z.ZodString;
|
|
14
|
+
version: z.ZodNumber;
|
|
15
|
+
}, z.core.$strict>;
|
|
16
|
+
policy_profile: z.ZodOptional<z.ZodObject<{
|
|
17
|
+
id: z.ZodString;
|
|
18
|
+
version: z.ZodNumber;
|
|
19
|
+
}, z.core.$strict>>;
|
|
20
|
+
members: z.ZodArray<z.ZodObject<{
|
|
21
|
+
provider: z.ZodEnum<{
|
|
22
|
+
ollama: "ollama";
|
|
23
|
+
"opencode-go": "opencode-go";
|
|
24
|
+
typesafe: "typesafe";
|
|
25
|
+
}>;
|
|
26
|
+
model: z.ZodString;
|
|
27
|
+
revision: z.ZodOptional<z.ZodString>;
|
|
28
|
+
}, z.core.$strict>>;
|
|
29
|
+
quorum: z.ZodNumber;
|
|
30
|
+
max_calls: z.ZodNumber;
|
|
31
|
+
agreement: z.ZodLiteral<"unanimous">;
|
|
32
|
+
noul_threshold: z.ZodNumber;
|
|
33
|
+
score_tolerance: z.ZodNumber;
|
|
34
|
+
allow_non_local: z.ZodBoolean;
|
|
35
|
+
}, z.core.$strict>;
|
|
36
|
+
export declare const consensusInputSchema: z.ZodObject<{
|
|
37
|
+
consensus: z.ZodObject<{
|
|
38
|
+
id: z.ZodString;
|
|
39
|
+
version: z.ZodNumber;
|
|
40
|
+
}, z.core.$strict>;
|
|
41
|
+
state: z.ZodPipe<z.ZodUnknown, z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
|
|
42
|
+
}, z.core.$strict>;
|
|
43
|
+
export declare const consensusOutputSchema: z.ZodObject<{
|
|
44
|
+
consensus: z.ZodObject<{
|
|
45
|
+
id: z.ZodString;
|
|
46
|
+
version: z.ZodNumber;
|
|
47
|
+
}, z.core.$strict>;
|
|
48
|
+
template: z.ZodObject<{
|
|
49
|
+
id: z.ZodString;
|
|
50
|
+
version: z.ZodNumber;
|
|
51
|
+
}, z.core.$strict>;
|
|
52
|
+
status: z.ZodEnum<{
|
|
53
|
+
agreed: "agreed";
|
|
54
|
+
disagreed: "disagreed";
|
|
55
|
+
no_quorum: "no_quorum";
|
|
56
|
+
}>;
|
|
57
|
+
result: z.ZodOptional<z.ZodObject<{
|
|
58
|
+
provider: z.ZodString;
|
|
59
|
+
model: z.ZodOptional<z.ZodString>;
|
|
60
|
+
answers: z.ZodRecord<z.ZodString, z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
61
|
+
confidence: z.ZodOptional<z.ZodNumber>;
|
|
62
|
+
probabilities: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
|
|
63
|
+
legend: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
64
|
+
type: z.ZodLiteral<"choice">;
|
|
65
|
+
choice: z.ZodString;
|
|
66
|
+
}, z.core.$catchall<z.ZodPipe<z.ZodUnknown, z.ZodJSONSchema>>>, z.ZodObject<{
|
|
67
|
+
confidence: z.ZodOptional<z.ZodNumber>;
|
|
68
|
+
probabilities: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
|
|
69
|
+
legend: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
70
|
+
type: z.ZodLiteral<"noul">;
|
|
71
|
+
noul: z.ZodNumber;
|
|
72
|
+
}, z.core.$catchall<z.ZodPipe<z.ZodUnknown, z.ZodJSONSchema>>>, z.ZodObject<{
|
|
73
|
+
confidence: z.ZodOptional<z.ZodNumber>;
|
|
74
|
+
probabilities: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
|
|
75
|
+
legend: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
76
|
+
type: z.ZodLiteral<"score">;
|
|
77
|
+
score: z.ZodNumber;
|
|
78
|
+
}, z.core.$catchall<z.ZodPipe<z.ZodUnknown, z.ZodJSONSchema>>>], "type">>;
|
|
79
|
+
usage: z.ZodOptional<z.ZodJSONSchema>;
|
|
80
|
+
}, z.core.$strip>>;
|
|
81
|
+
members: z.ZodArray<z.ZodObject<{
|
|
82
|
+
provider: z.ZodString;
|
|
83
|
+
model: z.ZodString;
|
|
84
|
+
outcome: z.ZodString;
|
|
85
|
+
answers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodBoolean, z.ZodNumber]>>>;
|
|
86
|
+
}, z.core.$strip>>;
|
|
87
|
+
}, z.core.$strip>;
|
|
88
|
+
export type ConsensusProfile = z.infer<typeof consensusProfileSchema>;
|
|
89
|
+
export type ResolvedConsensus = {
|
|
90
|
+
definition: ConsensusProfile;
|
|
91
|
+
questions: DecisionQuestions;
|
|
92
|
+
};
|
|
93
|
+
type Target = {
|
|
94
|
+
provider: string;
|
|
95
|
+
model: string;
|
|
96
|
+
};
|
|
97
|
+
type MemberFailure = DecisionErrorCode | "internal" | "budget";
|
|
98
|
+
type AnswerSummary = Record<string, string | boolean | number>;
|
|
99
|
+
export type ConsensusMember = Target & {
|
|
100
|
+
outcome: "ok" | MemberFailure;
|
|
101
|
+
answers?: AnswerSummary;
|
|
102
|
+
};
|
|
103
|
+
export type ConsensusOutcome = {
|
|
104
|
+
status: "agreed" | "disagreed" | "no_quorum";
|
|
105
|
+
result?: DecisionResponse;
|
|
106
|
+
members: ConsensusMember[];
|
|
107
|
+
};
|
|
108
|
+
export declare function createConsensusRegistry(definitions: readonly unknown[], templates: TemplateRegistry): {
|
|
109
|
+
get({ id, version }: TemplateIdentity): ResolvedConsensus;
|
|
110
|
+
list(): {
|
|
111
|
+
id: string;
|
|
112
|
+
version: number;
|
|
113
|
+
template: {
|
|
114
|
+
id: string;
|
|
115
|
+
version: number;
|
|
116
|
+
};
|
|
117
|
+
}[];
|
|
118
|
+
};
|
|
119
|
+
export type ConsensusRegistry = ReturnType<typeof createConsensusRegistry>;
|
|
120
|
+
export declare function loadConsensusRegistry(templates: TemplateRegistry): ConsensusRegistry;
|
|
121
|
+
export declare function runConsensus(consensus: ResolvedConsensus, state: JsonValue, registry: ProviderRegistry, context?: DecisionContext): Promise<ConsensusOutcome>;
|
|
122
|
+
type PolicyAction = "allow" | "review" | "block";
|
|
123
|
+
export type ConsensusPolicyMember = Target & ({
|
|
124
|
+
status: "evaluated";
|
|
125
|
+
action: PolicyAction;
|
|
126
|
+
reasonCode: string;
|
|
127
|
+
} | {
|
|
128
|
+
status: "skipped";
|
|
129
|
+
reasonCode: "local_context_required" | "policy_state_invalid";
|
|
130
|
+
} | {
|
|
131
|
+
status: "failed";
|
|
132
|
+
reasonCode: DecisionErrorCode | "internal";
|
|
133
|
+
} | {
|
|
134
|
+
status: "budget";
|
|
135
|
+
});
|
|
136
|
+
export type ConsensusPolicyResult = {
|
|
137
|
+
profile: TemplateIdentity;
|
|
138
|
+
consensus: TemplateIdentity;
|
|
139
|
+
status: "evaluated" | "skipped";
|
|
140
|
+
action: PolicyAction;
|
|
141
|
+
reasonCode: "out_of_scope" | "recursive_tool" | "consensus_allow" | "consensus_review" | "consensus_block" | "consensus_disagreement" | "consensus_no_quorum";
|
|
142
|
+
reasons: string[];
|
|
143
|
+
members: ConsensusPolicyMember[];
|
|
144
|
+
};
|
|
145
|
+
export declare function checkPolicyConsensusProfile(profile: ResolvedPolicyProfile, consensus: ResolvedConsensus): void;
|
|
146
|
+
export declare function loadActionPolicyConsensus(selection: string | undefined, consensuses: ConsensusRegistry, profile: ResolvedPolicyProfile, registry: ProviderRegistry): ResolvedConsensus | undefined;
|
|
147
|
+
export declare function evaluateConsensusActionPolicy(request: ActionPolicyRequest, registry: ProviderRegistry, profile: ResolvedPolicyProfile, consensus: ResolvedConsensus): Promise<ConsensusPolicyResult>;
|
|
148
|
+
export {};
|