@artieeez/cf-sdk 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/README.md +96 -2
- package/dist/fragments/approval.d.ts +83 -0
- package/dist/fragments/approval.d.ts.map +1 -0
- package/dist/fragments/approval.js +170 -0
- package/dist/fragments/approval.js.map +1 -0
- package/dist/fragments/common.d.ts +65 -0
- package/dist/fragments/common.d.ts.map +1 -0
- package/dist/fragments/common.js +138 -0
- package/dist/fragments/common.js.map +1 -0
- package/dist/fragments/deliver.d.ts +178 -0
- package/dist/fragments/deliver.d.ts.map +1 -0
- package/dist/fragments/deliver.js +232 -0
- package/dist/fragments/deliver.js.map +1 -0
- package/dist/fragments/index.d.ts +6 -0
- package/dist/fragments/index.d.ts.map +1 -0
- package/dist/fragments/index.js +6 -0
- package/dist/fragments/index.js.map +1 -0
- package/dist/fragments/validated.d.ts +64 -0
- package/dist/fragments/validated.d.ts.map +1 -0
- package/dist/fragments/validated.js +95 -0
- package/dist/fragments/validated.js.map +1 -0
- package/dist/fragments/verifier-round.d.ts +128 -0
- package/dist/fragments/verifier-round.d.ts.map +1 -0
- package/dist/fragments/verifier-round.js +295 -0
- package/dist/fragments/verifier-round.js.map +1 -0
- package/dist/index.d.ts +597 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +244 -0
- package/dist/index.js.map +1 -0
- package/dist/kit.d.ts +44 -0
- package/dist/kit.d.ts.map +1 -0
- package/dist/kit.js +32 -0
- package/dist/kit.js.map +1 -0
- package/dist/plugin.d.ts +126 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +15 -0
- package/dist/plugin.js.map +1 -0
- package/dist/prompt.d.ts +10 -0
- package/dist/prompt.d.ts.map +1 -0
- package/dist/prompt.js +34 -0
- package/dist/prompt.js.map +1 -0
- package/dist/runner.d.ts +88 -0
- package/dist/runner.d.ts.map +1 -0
- package/dist/runner.js +224 -0
- package/dist/runner.js.map +1 -0
- package/package.json +79 -3
- package/src/fragments/approval.ts +267 -0
- package/src/fragments/common.ts +195 -0
- package/src/fragments/deliver.ts +447 -0
- package/src/fragments/index.ts +57 -0
- package/src/fragments/validated.ts +217 -0
- package/src/fragments/verifier-round.ts +442 -0
- package/src/index.ts +890 -0
- package/src/kit.ts +137 -0
- package/src/plugin.ts +128 -0
- package/src/prompt.ts +29 -0
- package/src/runner.ts +278 -0
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import type {
|
|
3
|
+
NoEvents,
|
|
4
|
+
PluginApis,
|
|
5
|
+
Settings,
|
|
6
|
+
TaskContext,
|
|
7
|
+
TaskResult,
|
|
8
|
+
TaskStep,
|
|
9
|
+
} from '../index.ts';
|
|
10
|
+
import {
|
|
11
|
+
assertPositiveInt,
|
|
12
|
+
assertSlug,
|
|
13
|
+
runValidationScript,
|
|
14
|
+
stepId,
|
|
15
|
+
ValidationOutput,
|
|
16
|
+
type ValidationScript,
|
|
17
|
+
} from './common.ts';
|
|
18
|
+
|
|
19
|
+
type OutputIn<Sch> = Sch extends z.ZodType ? z.input<Sch> : unknown;
|
|
20
|
+
|
|
21
|
+
/** Default validated outcome inferred from `O`: `'done'`, else `'ready'`, else `O` itself. */
|
|
22
|
+
export type DefaultValidateOutcome<O extends string> = 'done' extends O
|
|
23
|
+
? 'done'
|
|
24
|
+
: 'ready' extends O
|
|
25
|
+
? 'ready'
|
|
26
|
+
: O;
|
|
27
|
+
|
|
28
|
+
/** Routing map required for the non-validated outcomes of an inline `agent` step. */
|
|
29
|
+
type OtherOutcomesNext<O extends string, V extends string> = [Exclude<O, V>] extends [never]
|
|
30
|
+
? { next?: Record<never, string> }
|
|
31
|
+
: { next: Record<Exclude<O, V>, string> };
|
|
32
|
+
|
|
33
|
+
/** Inline producer step definition for `validated` (its validated outcome is wired automatically). */
|
|
34
|
+
export type ValidatedAgentInline<
|
|
35
|
+
I,
|
|
36
|
+
O extends string,
|
|
37
|
+
V extends O,
|
|
38
|
+
P extends PluginApis,
|
|
39
|
+
Sch extends z.ZodType | undefined,
|
|
40
|
+
S,
|
|
41
|
+
E,
|
|
42
|
+
> = {
|
|
43
|
+
kind?: never;
|
|
44
|
+
outcomes: readonly O[];
|
|
45
|
+
maxVisits?: number;
|
|
46
|
+
onExhausted?: string;
|
|
47
|
+
wd?: string;
|
|
48
|
+
waivable?: readonly O[];
|
|
49
|
+
output?: Sch;
|
|
50
|
+
/** Runs the producer; `lastFailure` is the most recent failed `ValidationOutput`, else `undefined`. */
|
|
51
|
+
run(
|
|
52
|
+
ctx: TaskContext<I, P, S, E>,
|
|
53
|
+
lastFailure: ValidationOutput | undefined,
|
|
54
|
+
): Promise<TaskResult<O, OutputIn<Sch>>> | TaskResult<O, OutputIn<Sch>>;
|
|
55
|
+
} & OtherOutcomesNext<O, V>;
|
|
56
|
+
|
|
57
|
+
/** Options for `validated(id, opts)`. */
|
|
58
|
+
export type ValidatedOptions<
|
|
59
|
+
I,
|
|
60
|
+
O extends string,
|
|
61
|
+
V extends O,
|
|
62
|
+
P extends PluginApis,
|
|
63
|
+
Sch extends z.ZodType | undefined,
|
|
64
|
+
S,
|
|
65
|
+
E,
|
|
66
|
+
> = {
|
|
67
|
+
/** Producer step: either an inline step spec (receiving `lastFailure`) or a pre-built `TaskStep`. */
|
|
68
|
+
agent: ValidatedAgentInline<I, O, V, P, Sch, S, E> | TaskStep<I, O, P, Sch, S, E>;
|
|
69
|
+
/** Outcome of `agent` that routes to validation (defaults to `'done'`, `'ready'`, or the sole outcome). */
|
|
70
|
+
on?: V;
|
|
71
|
+
/** Validation script run via `ctx.exec` or a custom `{ run(ctx) }` validator. */
|
|
72
|
+
script: ValidationScript<I, P, S, E>;
|
|
73
|
+
/** Target step when validation passes (and when waived if `waivable: true`). */
|
|
74
|
+
next: string;
|
|
75
|
+
/** Max validation visits before routing to `onExhausted`; default `3`. */
|
|
76
|
+
maxVisits?: number;
|
|
77
|
+
/** Target step when `maxVisits` is exhausted; default `'$fail'`. */
|
|
78
|
+
onExhausted?: string;
|
|
79
|
+
/** Working directory for the validator step; defaults to `agent.wd`. */
|
|
80
|
+
wd?: string;
|
|
81
|
+
/** When `true`, marks `'failed'` waivable and routes `next.waived` to `next`. */
|
|
82
|
+
waivable?: boolean;
|
|
83
|
+
/** Per-stream character cap before surrogate-safe truncation; default `16_000`. */
|
|
84
|
+
maxOutputChars?: number;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** What `validated(id, opts)` returns: entry id, step handles, and spreadable `steps`. */
|
|
88
|
+
export interface ValidatedFragment<
|
|
89
|
+
Id extends string,
|
|
90
|
+
I,
|
|
91
|
+
O extends string,
|
|
92
|
+
P extends PluginApis,
|
|
93
|
+
Sch extends z.ZodType | undefined,
|
|
94
|
+
S,
|
|
95
|
+
E,
|
|
96
|
+
> {
|
|
97
|
+
start: Id;
|
|
98
|
+
ids: { producer: Id; validate: `${Id}-validate` };
|
|
99
|
+
producer: TaskStep<I, O, P, Sch, S, E>;
|
|
100
|
+
validate: TaskStep<I, 'passed' | 'failed', P, typeof ValidationOutput, S, E>;
|
|
101
|
+
steps: Record<Id, TaskStep<I, O, P, Sch, S, E>> &
|
|
102
|
+
Record<`${Id}-validate`, TaskStep<I, 'passed' | 'failed', P, typeof ValidationOutput, S, E>>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function isTaskStep<I, O extends string, P extends PluginApis, Sch extends z.ZodType | undefined, S, E>(
|
|
106
|
+
agent: ValidatedAgentInline<I, O, O, P, Sch, S, E> | TaskStep<I, O, P, Sch, S, E>,
|
|
107
|
+
): agent is TaskStep<I, O, P, Sch, S, E> {
|
|
108
|
+
return agent.kind === 'task';
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function resolveValidateOutcome<O extends string>(id: string, outcomes: readonly O[], explicit: O | undefined): O {
|
|
112
|
+
if (explicit !== undefined) {
|
|
113
|
+
if (!outcomes.includes(explicit)) {
|
|
114
|
+
throw new Error(`validated(${id}): outcome "${explicit}" is not in agent outcomes [${outcomes.join(', ')}]`);
|
|
115
|
+
}
|
|
116
|
+
return explicit;
|
|
117
|
+
}
|
|
118
|
+
if (outcomes.includes('done' as O)) return 'done' as O;
|
|
119
|
+
if (outcomes.includes('ready' as O)) return 'ready' as O;
|
|
120
|
+
if (outcomes.length === 1) return outcomes[0]!;
|
|
121
|
+
throw new Error(
|
|
122
|
+
`validated(${id}): ambiguous validation outcome for outcomes [${outcomes.join(', ')}]; specify "on"`,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Builds an agent-plus-script validation loop: `<id>` produces artifacts and routes its validated
|
|
128
|
+
* outcome to `<id>-validate`, which runs `script` and loops back on failure up to `maxVisits`
|
|
129
|
+
* (default `3`).
|
|
130
|
+
*/
|
|
131
|
+
export function validated<
|
|
132
|
+
const Id extends string,
|
|
133
|
+
I = unknown,
|
|
134
|
+
const O extends string = string,
|
|
135
|
+
const V extends O = DefaultValidateOutcome<O>,
|
|
136
|
+
P extends PluginApis = PluginApis,
|
|
137
|
+
Sch extends z.ZodType | undefined = undefined,
|
|
138
|
+
S = Settings,
|
|
139
|
+
E = NoEvents,
|
|
140
|
+
>(
|
|
141
|
+
id: Id,
|
|
142
|
+
opts: ValidatedOptions<I, O, V, P, Sch, S, E>,
|
|
143
|
+
): ValidatedFragment<Id, I, O, P, Sch, S, E> {
|
|
144
|
+
assertSlug(id, 'validated');
|
|
145
|
+
const maxVisits = opts.maxVisits ?? 3;
|
|
146
|
+
assertPositiveInt(maxVisits, `validated(${id}) maxVisits`);
|
|
147
|
+
if (opts.maxOutputChars !== undefined) {
|
|
148
|
+
assertPositiveInt(opts.maxOutputChars, `validated(${id}) maxOutputChars`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const validateId = stepId(id, 'validate');
|
|
152
|
+
const validateOn = resolveValidateOutcome(id, opts.agent.outcomes, opts.on);
|
|
153
|
+
const wd = opts.wd ?? opts.agent.wd;
|
|
154
|
+
const waivable = opts.waivable === true;
|
|
155
|
+
|
|
156
|
+
const validate: TaskStep<I, 'passed' | 'failed', P, typeof ValidationOutput, S, E> = {
|
|
157
|
+
kind: 'task',
|
|
158
|
+
outcomes: ['passed', 'failed'] as const,
|
|
159
|
+
next: waivable
|
|
160
|
+
? { passed: opts.next, failed: id, waived: opts.next }
|
|
161
|
+
: { passed: opts.next, failed: id },
|
|
162
|
+
...(waivable ? { waivable: ['failed'] as const } : {}),
|
|
163
|
+
maxVisits,
|
|
164
|
+
onExhausted: opts.onExhausted ?? '$fail',
|
|
165
|
+
...(wd !== undefined ? { wd } : {}),
|
|
166
|
+
output: ValidationOutput,
|
|
167
|
+
async run(ctx) {
|
|
168
|
+
const output = await runValidationScript(ctx, opts.script, {
|
|
169
|
+
artifactPrefix: validateId,
|
|
170
|
+
...(opts.maxOutputChars !== undefined ? { maxOutputChars: opts.maxOutputChars } : {}),
|
|
171
|
+
});
|
|
172
|
+
return { outcome: output.passed ? 'passed' : 'failed', output };
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
let producer: TaskStep<I, O, P, Sch, S, E>;
|
|
177
|
+
if (isTaskStep(opts.agent)) {
|
|
178
|
+
// Clone without mutating the caller's step object.
|
|
179
|
+
producer = {
|
|
180
|
+
...opts.agent,
|
|
181
|
+
next: { ...opts.agent.next, [validateOn]: validateId } as Record<O, string> & { waived?: string },
|
|
182
|
+
};
|
|
183
|
+
} else {
|
|
184
|
+
const inline = opts.agent;
|
|
185
|
+
const baseNext = (inline.next ?? {}) as Record<string, string>;
|
|
186
|
+
const fullNext = { ...baseNext, [validateOn]: validateId } as Record<O, string> & { waived?: string };
|
|
187
|
+
producer = {
|
|
188
|
+
kind: 'task',
|
|
189
|
+
outcomes: inline.outcomes,
|
|
190
|
+
next: fullNext,
|
|
191
|
+
...(inline.maxVisits !== undefined ? { maxVisits: inline.maxVisits } : {}),
|
|
192
|
+
...(inline.onExhausted !== undefined ? { onExhausted: inline.onExhausted } : {}),
|
|
193
|
+
...(inline.wd !== undefined ? { wd: inline.wd } : {}),
|
|
194
|
+
...(inline.waivable !== undefined ? { waivable: inline.waivable } : {}),
|
|
195
|
+
...(inline.output !== undefined ? { output: inline.output } : {}),
|
|
196
|
+
run(ctx) {
|
|
197
|
+
const lastVisit = ctx.visitsOf(validate).at(-1);
|
|
198
|
+
const lastFailure = lastVisit?.outcome === 'failed' ? lastVisit.output : undefined;
|
|
199
|
+
return inline.run(ctx, lastFailure);
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const steps = {
|
|
205
|
+
[id]: producer,
|
|
206
|
+
[validateId]: validate,
|
|
207
|
+
} as Record<Id, TaskStep<I, O, P, Sch, S, E>> &
|
|
208
|
+
Record<`${Id}-validate`, TaskStep<I, 'passed' | 'failed', P, typeof ValidationOutput, S, E>>;
|
|
209
|
+
|
|
210
|
+
return {
|
|
211
|
+
start: id,
|
|
212
|
+
ids: { producer: id, validate: validateId },
|
|
213
|
+
producer,
|
|
214
|
+
validate,
|
|
215
|
+
steps,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import {
|
|
3
|
+
parallel,
|
|
4
|
+
type AnyStep,
|
|
5
|
+
type HumanStep,
|
|
6
|
+
type NoEvents,
|
|
7
|
+
type ParallelStep,
|
|
8
|
+
type PluginApis,
|
|
9
|
+
type Settings,
|
|
10
|
+
type TaskContext,
|
|
11
|
+
type TaskStep,
|
|
12
|
+
} from '../index.ts';
|
|
13
|
+
import {
|
|
14
|
+
assertPositiveInt,
|
|
15
|
+
assertSlug,
|
|
16
|
+
runValidationScript,
|
|
17
|
+
scopePrefix,
|
|
18
|
+
stepId,
|
|
19
|
+
ValidationOutput,
|
|
20
|
+
type ValidationScript,
|
|
21
|
+
} from './common.ts';
|
|
22
|
+
|
|
23
|
+
/** Output schema of the independent verifier step (AD-031). */
|
|
24
|
+
export const ReviewResultSchema = z.strictObject({
|
|
25
|
+
findings: z.array(z.string()),
|
|
26
|
+
summary: z.string().optional(),
|
|
27
|
+
sessionId: z.string().optional(),
|
|
28
|
+
});
|
|
29
|
+
export type ReviewResult = z.infer<typeof ReviewResultSchema>;
|
|
30
|
+
|
|
31
|
+
/** Output schema of the screens sub-agent step (AD-031). */
|
|
32
|
+
export const ScreensResultSchema = z.strictObject({
|
|
33
|
+
passed: z.boolean(),
|
|
34
|
+
findings: z.array(z.string()),
|
|
35
|
+
summary: z.string().optional(),
|
|
36
|
+
sessionId: z.string().optional(),
|
|
37
|
+
});
|
|
38
|
+
export type ScreensResult = z.infer<typeof ScreensResultSchema>;
|
|
39
|
+
|
|
40
|
+
/** Aggregated findings passed to the single fix pass (AD-031). */
|
|
41
|
+
export const FindingsOutput = z.strictObject({
|
|
42
|
+
hasFindings: z.boolean(),
|
|
43
|
+
findings: z.array(z.string()),
|
|
44
|
+
verifierFindings: z.array(z.string()),
|
|
45
|
+
screensFindings: z.array(z.string()),
|
|
46
|
+
});
|
|
47
|
+
export type FindingsOutput = z.infer<typeof FindingsOutput>;
|
|
48
|
+
|
|
49
|
+
/** Form schema of the built-in escalation human step. */
|
|
50
|
+
export const EscalateForm = z.strictObject({
|
|
51
|
+
note: z.string().optional(),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
/** Round metadata passed to `screens.run`. */
|
|
55
|
+
export interface ScreensRoundInfo {
|
|
56
|
+
round: 1 | 2;
|
|
57
|
+
/** Round-1 `sessionId` when running round 2 (if round 1 returned one). */
|
|
58
|
+
resumeSessionId?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Options for `verifierRound(opts)`. */
|
|
62
|
+
export interface VerifierRoundOptions<
|
|
63
|
+
Id extends string = 'verify',
|
|
64
|
+
I = unknown,
|
|
65
|
+
P extends PluginApis = PluginApis,
|
|
66
|
+
S = Settings,
|
|
67
|
+
E = NoEvents,
|
|
68
|
+
> {
|
|
69
|
+
/** Slug prefix for all steps in this fragment; default `'verify'`. */
|
|
70
|
+
id?: Id;
|
|
71
|
+
/** Proof verification script (run before review and again after the fix pass). */
|
|
72
|
+
proofs: ValidationScript<I, P, S, E>;
|
|
73
|
+
/** Optional repair loop when round-1 proofs fail (`maxVisits` default `2` on `proofs`). */
|
|
74
|
+
repair?: {
|
|
75
|
+
run(
|
|
76
|
+
ctx: TaskContext<I, P, S, E>,
|
|
77
|
+
failure: ValidationOutput | undefined,
|
|
78
|
+
): Promise<void | 'repaired' | 'fail'> | void | 'repaired' | 'fail';
|
|
79
|
+
maxVisits?: number;
|
|
80
|
+
wd?: string;
|
|
81
|
+
};
|
|
82
|
+
/** Independent verifier agent step (always runs). */
|
|
83
|
+
verifier: {
|
|
84
|
+
run(ctx: TaskContext<I, P, S, E>): Promise<ReviewResult> | ReviewResult;
|
|
85
|
+
wd?: string;
|
|
86
|
+
};
|
|
87
|
+
/** Optional screens sub-agent (runs beside verifier in parallel, then round 2 after fix). */
|
|
88
|
+
screens?: {
|
|
89
|
+
run(ctx: TaskContext<I, P, S, E>, info: ScreensRoundInfo): Promise<ScreensResult> | ScreensResult;
|
|
90
|
+
wd?: string;
|
|
91
|
+
};
|
|
92
|
+
/** Optional custom findings aggregator over verifier + screens outputs. */
|
|
93
|
+
findings?: (
|
|
94
|
+
ctx: TaskContext<I, P, S, E>,
|
|
95
|
+
review: { verifier?: ReviewResult; screens?: ScreensResult },
|
|
96
|
+
) => Promise<{ hasFindings: boolean; findings: string[] }> | { hasFindings: boolean; findings: string[] };
|
|
97
|
+
/** Single fix pass addressing all findings before round 2. */
|
|
98
|
+
fix: {
|
|
99
|
+
run(ctx: TaskContext<I, P, S, E>, findings: FindingsOutput): Promise<void | 'done' | 'fail'> | void | 'done' | 'fail';
|
|
100
|
+
wd?: string;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Escalation target when proofs exhaust, fix fails, or round 2 still fails:
|
|
104
|
+
* - omitted or object: emits a `<id>-escalate` human step (`resolved -> <id>-gate`, `abort -> onAbort ?? '$fail'`)
|
|
105
|
+
* - string: routes directly to that target step and emits no human step
|
|
106
|
+
*/
|
|
107
|
+
escalate?: string | { title?: string; onAbort?: string };
|
|
108
|
+
/** Completion gate script (e.g. `validate_verification.py`). */
|
|
109
|
+
gate: ValidationScript<I, P, S, E>;
|
|
110
|
+
/** When `true`, marks `'failed'` waivable on `<id>-gate` and routes `waived -> next`. */
|
|
111
|
+
gateWaivable?: boolean;
|
|
112
|
+
/** Target step when `<id>-gate` fails; default `'$fail'`. */
|
|
113
|
+
onGateFailed?: string;
|
|
114
|
+
/** Target step when `<id>-gate` passes. */
|
|
115
|
+
next: string;
|
|
116
|
+
/** Default working directory for tasks in this fragment. */
|
|
117
|
+
wd?: string;
|
|
118
|
+
/** Per-stream output char cap for `proofs` and `gate`; default `16_000`. */
|
|
119
|
+
maxOutputChars?: number;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** What `verifierRound(opts)` returns: entry id, step handles, and spreadable `steps`. */
|
|
123
|
+
export interface VerifierRoundFragment<
|
|
124
|
+
Id extends string,
|
|
125
|
+
I,
|
|
126
|
+
P extends PluginApis,
|
|
127
|
+
S,
|
|
128
|
+
E,
|
|
129
|
+
> {
|
|
130
|
+
start: `${Id}-proofs`;
|
|
131
|
+
ids: {
|
|
132
|
+
proofs: `${Id}-proofs`;
|
|
133
|
+
repair?: `${Id}-repair`;
|
|
134
|
+
review: `${Id}-review`;
|
|
135
|
+
findings: `${Id}-findings`;
|
|
136
|
+
fix: `${Id}-fix`;
|
|
137
|
+
proofs2: `${Id}-proofs-2`;
|
|
138
|
+
screens2?: `${Id}-screens-2`;
|
|
139
|
+
escalate?: `${Id}-escalate`;
|
|
140
|
+
gate: `${Id}-gate`;
|
|
141
|
+
};
|
|
142
|
+
proofs: TaskStep<I, 'green' | 'red', P, typeof ValidationOutput, S, E>;
|
|
143
|
+
repair?: TaskStep<I, 'repaired' | 'fail', P, undefined, S, E>;
|
|
144
|
+
review: ParallelStep | TaskStep<I, 'done' | 'fail', P, typeof ReviewResultSchema, S, E>;
|
|
145
|
+
findings: TaskStep<I, 'none' | 'some', P, typeof FindingsOutput, S, E>;
|
|
146
|
+
fix: TaskStep<I, 'done' | 'fail', P, undefined, S, E>;
|
|
147
|
+
proofs2: TaskStep<I, 'green' | 'red', P, typeof ValidationOutput, S, E>;
|
|
148
|
+
screens2?: TaskStep<I, 'passed' | 'failed', P, typeof ScreensResultSchema, S, E>;
|
|
149
|
+
escalate?: HumanStep<'resolved' | 'abort', typeof EscalateForm>;
|
|
150
|
+
gate: TaskStep<I, 'passed' | 'failed', P, typeof ValidationOutput, S, E>;
|
|
151
|
+
steps: Record<string, AnyStep>;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Builds the spec-lean verification topology:
|
|
156
|
+
* `proofs (+ optional repair) -> review (parallel verifier+screens, or verifier task) -> findings
|
|
157
|
+
* -> single fix pass -> proofs-2 -> optional screens-2 (resuming round-1 session) -> escalate -> gate`.
|
|
158
|
+
*/
|
|
159
|
+
export function verifierRound<
|
|
160
|
+
const Id extends string = 'verify',
|
|
161
|
+
I = unknown,
|
|
162
|
+
P extends PluginApis = PluginApis,
|
|
163
|
+
S = Settings,
|
|
164
|
+
E = NoEvents,
|
|
165
|
+
>(opts: VerifierRoundOptions<Id, I, P, S, E>): VerifierRoundFragment<Id, I, P, S, E> {
|
|
166
|
+
const prefix = (opts.id ?? 'verify') as Id;
|
|
167
|
+
assertSlug(prefix, 'verifierRound');
|
|
168
|
+
if (opts.repair?.maxVisits !== undefined) {
|
|
169
|
+
assertPositiveInt(opts.repair.maxVisits, `verifierRound(${prefix}) repair.maxVisits`);
|
|
170
|
+
}
|
|
171
|
+
if (opts.maxOutputChars !== undefined) {
|
|
172
|
+
assertPositiveInt(opts.maxOutputChars, `verifierRound(${prefix}) maxOutputChars`);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const proofsId = stepId(prefix, 'proofs');
|
|
176
|
+
const repairId = stepId(prefix, 'repair');
|
|
177
|
+
const reviewId = stepId(prefix, 'review');
|
|
178
|
+
const findingsId = stepId(prefix, 'findings');
|
|
179
|
+
const fixId = stepId(prefix, 'fix');
|
|
180
|
+
const proofs2Id = stepId(prefix, 'proofs-2');
|
|
181
|
+
const screens2Id = stepId(prefix, 'screens-2');
|
|
182
|
+
const escalateId = stepId(prefix, 'escalate');
|
|
183
|
+
const gateId = stepId(prefix, 'gate');
|
|
184
|
+
|
|
185
|
+
const hasRepair = opts.repair !== undefined;
|
|
186
|
+
const hasScreens = opts.screens !== undefined;
|
|
187
|
+
const hasHumanEscalate = typeof opts.escalate !== 'string';
|
|
188
|
+
const escalateTarget = typeof opts.escalate === 'string' ? opts.escalate : escalateId;
|
|
189
|
+
|
|
190
|
+
const proofs: TaskStep<I, 'green' | 'red', P, typeof ValidationOutput, S, E> = {
|
|
191
|
+
kind: 'task',
|
|
192
|
+
outcomes: ['green', 'red'] as const,
|
|
193
|
+
next: { green: reviewId, red: hasRepair ? repairId : escalateTarget },
|
|
194
|
+
...(hasRepair ? { maxVisits: opts.repair!.maxVisits ?? 2, onExhausted: escalateTarget } : {}),
|
|
195
|
+
...(opts.wd !== undefined ? { wd: opts.wd } : {}),
|
|
196
|
+
output: ValidationOutput,
|
|
197
|
+
async run(ctx) {
|
|
198
|
+
const output = await runValidationScript(ctx, opts.proofs, {
|
|
199
|
+
artifactPrefix: proofsId,
|
|
200
|
+
...(opts.maxOutputChars !== undefined ? { maxOutputChars: opts.maxOutputChars } : {}),
|
|
201
|
+
});
|
|
202
|
+
return { outcome: output.passed ? 'green' : 'red', output };
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
let repair: TaskStep<I, 'repaired' | 'fail', P, undefined, S, E> | undefined;
|
|
207
|
+
if (opts.repair !== undefined) {
|
|
208
|
+
const repairCfg = opts.repair;
|
|
209
|
+
const repairWd = repairCfg.wd ?? opts.wd;
|
|
210
|
+
repair = {
|
|
211
|
+
kind: 'task',
|
|
212
|
+
outcomes: ['repaired', 'fail'] as const,
|
|
213
|
+
next: { repaired: proofsId, fail: escalateTarget },
|
|
214
|
+
...(repairWd !== undefined ? { wd: repairWd } : {}),
|
|
215
|
+
async run(ctx) {
|
|
216
|
+
const lastVisit = ctx.visitsOf(proofs).at(-1);
|
|
217
|
+
const failure = lastVisit?.outcome === 'red' ? lastVisit.output : undefined;
|
|
218
|
+
const outcome = await repairCfg.run(ctx, failure);
|
|
219
|
+
return { outcome: outcome === 'fail' ? 'fail' : 'repaired' };
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
let review: ParallelStep | TaskStep<I, 'done' | 'fail', P, typeof ReviewResultSchema, S, E>;
|
|
225
|
+
if (opts.screens !== undefined) {
|
|
226
|
+
const screensCfg = opts.screens;
|
|
227
|
+
const verifierWd = opts.verifier.wd ?? opts.wd;
|
|
228
|
+
const screensWd = screensCfg.wd ?? opts.wd;
|
|
229
|
+
const verifierBranch: TaskStep<I, 'done' | 'fail', P, typeof ReviewResultSchema, S, E> = {
|
|
230
|
+
kind: 'task',
|
|
231
|
+
outcomes: ['done', 'fail'] as const,
|
|
232
|
+
next: { done: '$end', fail: '$fail' },
|
|
233
|
+
...(verifierWd !== undefined ? { wd: verifierWd } : {}),
|
|
234
|
+
output: ReviewResultSchema,
|
|
235
|
+
async run(ctx) {
|
|
236
|
+
const output = await opts.verifier.run(ctx);
|
|
237
|
+
return { outcome: 'done', output };
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
const screensBranch: TaskStep<I, 'done' | 'fail', P, typeof ScreensResultSchema, S, E> = {
|
|
241
|
+
kind: 'task',
|
|
242
|
+
outcomes: ['done', 'fail'] as const,
|
|
243
|
+
next: { done: '$end', fail: '$fail' },
|
|
244
|
+
...(screensWd !== undefined ? { wd: screensWd } : {}),
|
|
245
|
+
output: ScreensResultSchema,
|
|
246
|
+
async run(ctx) {
|
|
247
|
+
const output = await screensCfg.run(ctx, { round: 1 });
|
|
248
|
+
return { outcome: 'done', output };
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
review = parallel({
|
|
252
|
+
branches: {
|
|
253
|
+
verifier: { start: 'verifier', steps: { verifier: verifierBranch } },
|
|
254
|
+
screens: { start: 'screens', steps: { screens: screensBranch } },
|
|
255
|
+
},
|
|
256
|
+
next: { all_ok: findingsId, partial: escalateTarget, all_failed: escalateTarget },
|
|
257
|
+
});
|
|
258
|
+
} else {
|
|
259
|
+
const verifierWd = opts.verifier.wd ?? opts.wd;
|
|
260
|
+
review = {
|
|
261
|
+
kind: 'task',
|
|
262
|
+
outcomes: ['done', 'fail'] as const,
|
|
263
|
+
next: { done: findingsId, fail: escalateTarget },
|
|
264
|
+
...(verifierWd !== undefined ? { wd: verifierWd } : {}),
|
|
265
|
+
output: ReviewResultSchema,
|
|
266
|
+
async run(ctx) {
|
|
267
|
+
const output = await opts.verifier.run(ctx);
|
|
268
|
+
return { outcome: 'done', output };
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const readParallelScreens = (ctx: TaskContext<I, P, S, E>): ScreensResult | undefined => {
|
|
274
|
+
const scope = scopePrefix(ctx.step);
|
|
275
|
+
return ctx.outputs[`${scope}${reviewId}[screens].screens`] as ScreensResult | undefined;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const findings: TaskStep<I, 'none' | 'some', P, typeof FindingsOutput, S, E> = {
|
|
279
|
+
kind: 'task',
|
|
280
|
+
outcomes: ['none', 'some'] as const,
|
|
281
|
+
next: { none: gateId, some: fixId },
|
|
282
|
+
...(opts.wd !== undefined ? { wd: opts.wd } : {}),
|
|
283
|
+
output: FindingsOutput,
|
|
284
|
+
async run(ctx) {
|
|
285
|
+
const scope = scopePrefix(ctx.step);
|
|
286
|
+
const verifierOut = hasScreens
|
|
287
|
+
? (ctx.outputs[`${scope}${reviewId}[verifier].verifier`] as ReviewResult | undefined)
|
|
288
|
+
: (ctx.output(review as TaskStep<I, 'done' | 'fail', P, typeof ReviewResultSchema, S, E>) ??
|
|
289
|
+
(ctx.outputs[`${scope}${reviewId}`] as ReviewResult | undefined));
|
|
290
|
+
const screensOut = hasScreens ? readParallelScreens(ctx) : undefined;
|
|
291
|
+
|
|
292
|
+
const verifierFindings = verifierOut?.findings ?? [];
|
|
293
|
+
const screensFindings = screensOut?.findings ?? [];
|
|
294
|
+
|
|
295
|
+
let hasFindings: boolean;
|
|
296
|
+
let combined: string[];
|
|
297
|
+
if (opts.findings !== undefined) {
|
|
298
|
+
const custom = await opts.findings(ctx, {
|
|
299
|
+
...(verifierOut !== undefined ? { verifier: verifierOut } : {}),
|
|
300
|
+
...(screensOut !== undefined ? { screens: screensOut } : {}),
|
|
301
|
+
});
|
|
302
|
+
hasFindings = custom.hasFindings;
|
|
303
|
+
combined = custom.findings;
|
|
304
|
+
} else {
|
|
305
|
+
const extra =
|
|
306
|
+
screensOut?.passed === false && screensFindings.length === 0
|
|
307
|
+
? ['screens verification failed']
|
|
308
|
+
: [];
|
|
309
|
+
combined = [...verifierFindings, ...screensFindings, ...extra];
|
|
310
|
+
hasFindings = combined.length > 0 || screensOut?.passed === false;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
outcome: hasFindings ? 'some' : 'none',
|
|
315
|
+
output: { hasFindings, findings: combined, verifierFindings, screensFindings },
|
|
316
|
+
};
|
|
317
|
+
},
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
const fixWd = opts.fix.wd ?? opts.wd;
|
|
321
|
+
const fix: TaskStep<I, 'done' | 'fail', P, undefined, S, E> = {
|
|
322
|
+
kind: 'task',
|
|
323
|
+
outcomes: ['done', 'fail'] as const,
|
|
324
|
+
next: { done: proofs2Id, fail: escalateTarget },
|
|
325
|
+
...(fixWd !== undefined ? { wd: fixWd } : {}),
|
|
326
|
+
async run(ctx) {
|
|
327
|
+
const current = ctx.output(findings) ?? {
|
|
328
|
+
hasFindings: false,
|
|
329
|
+
findings: [],
|
|
330
|
+
verifierFindings: [],
|
|
331
|
+
screensFindings: [],
|
|
332
|
+
};
|
|
333
|
+
const outcome = await opts.fix.run(ctx, current);
|
|
334
|
+
return { outcome: outcome === 'fail' ? 'fail' : 'done' };
|
|
335
|
+
},
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const proofs2: TaskStep<I, 'green' | 'red', P, typeof ValidationOutput, S, E> = {
|
|
339
|
+
kind: 'task',
|
|
340
|
+
outcomes: ['green', 'red'] as const,
|
|
341
|
+
next: { green: hasScreens ? screens2Id : gateId, red: escalateTarget },
|
|
342
|
+
...(opts.wd !== undefined ? { wd: opts.wd } : {}),
|
|
343
|
+
output: ValidationOutput,
|
|
344
|
+
async run(ctx) {
|
|
345
|
+
const output = await runValidationScript(ctx, opts.proofs, {
|
|
346
|
+
artifactPrefix: proofs2Id,
|
|
347
|
+
...(opts.maxOutputChars !== undefined ? { maxOutputChars: opts.maxOutputChars } : {}),
|
|
348
|
+
});
|
|
349
|
+
return { outcome: output.passed ? 'green' : 'red', output };
|
|
350
|
+
},
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
let screens2: TaskStep<I, 'passed' | 'failed', P, typeof ScreensResultSchema, S, E> | undefined;
|
|
354
|
+
if (opts.screens !== undefined) {
|
|
355
|
+
const screensCfg = opts.screens;
|
|
356
|
+
const screens2Wd = screensCfg.wd ?? opts.wd;
|
|
357
|
+
screens2 = {
|
|
358
|
+
kind: 'task',
|
|
359
|
+
outcomes: ['passed', 'failed'] as const,
|
|
360
|
+
next: { passed: gateId, failed: escalateTarget },
|
|
361
|
+
...(screens2Wd !== undefined ? { wd: screens2Wd } : {}),
|
|
362
|
+
output: ScreensResultSchema,
|
|
363
|
+
async run(ctx) {
|
|
364
|
+
const round1 = readParallelScreens(ctx);
|
|
365
|
+
const output = await screensCfg.run(ctx, {
|
|
366
|
+
round: 2,
|
|
367
|
+
...(round1?.sessionId !== undefined ? { resumeSessionId: round1.sessionId } : {}),
|
|
368
|
+
});
|
|
369
|
+
return { outcome: output.passed ? 'passed' : 'failed', output };
|
|
370
|
+
},
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
let escalate: HumanStep<'resolved' | 'abort', typeof EscalateForm> | undefined;
|
|
375
|
+
if (hasHumanEscalate) {
|
|
376
|
+
const cfg = typeof opts.escalate === 'object' ? opts.escalate : undefined;
|
|
377
|
+
escalate = {
|
|
378
|
+
kind: 'human',
|
|
379
|
+
title: cfg?.title ?? 'Verification needs human attention',
|
|
380
|
+
form: EscalateForm,
|
|
381
|
+
outcomes: ['resolved', 'abort'] as const,
|
|
382
|
+
next: { resolved: gateId, abort: cfg?.onAbort ?? '$fail' },
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const gateWaivable = opts.gateWaivable === true;
|
|
387
|
+
const gateFailedTarget = opts.onGateFailed ?? '$fail';
|
|
388
|
+
const gate: TaskStep<I, 'passed' | 'failed', P, typeof ValidationOutput, S, E> = {
|
|
389
|
+
kind: 'task',
|
|
390
|
+
outcomes: ['passed', 'failed'] as const,
|
|
391
|
+
next: gateWaivable
|
|
392
|
+
? { passed: opts.next, failed: gateFailedTarget, waived: opts.next }
|
|
393
|
+
: { passed: opts.next, failed: gateFailedTarget },
|
|
394
|
+
...(gateWaivable ? { waivable: ['failed'] as const } : {}),
|
|
395
|
+
...(opts.wd !== undefined ? { wd: opts.wd } : {}),
|
|
396
|
+
output: ValidationOutput,
|
|
397
|
+
async run(ctx) {
|
|
398
|
+
const output = await runValidationScript(ctx, opts.gate, {
|
|
399
|
+
artifactPrefix: gateId,
|
|
400
|
+
...(opts.maxOutputChars !== undefined ? { maxOutputChars: opts.maxOutputChars } : {}),
|
|
401
|
+
});
|
|
402
|
+
return { outcome: output.passed ? 'passed' : 'failed', output };
|
|
403
|
+
},
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
const steps: Record<string, AnyStep> = {
|
|
407
|
+
[proofsId]: proofs,
|
|
408
|
+
...(repair !== undefined ? { [repairId]: repair } : {}),
|
|
409
|
+
[reviewId]: review,
|
|
410
|
+
[findingsId]: findings,
|
|
411
|
+
[fixId]: fix,
|
|
412
|
+
[proofs2Id]: proofs2,
|
|
413
|
+
...(screens2 !== undefined ? { [screens2Id]: screens2 } : {}),
|
|
414
|
+
...(escalate !== undefined ? { [escalateId]: escalate } : {}),
|
|
415
|
+
[gateId]: gate,
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
return {
|
|
419
|
+
start: proofsId,
|
|
420
|
+
ids: {
|
|
421
|
+
proofs: proofsId,
|
|
422
|
+
...(repair !== undefined ? { repair: repairId } : {}),
|
|
423
|
+
review: reviewId,
|
|
424
|
+
findings: findingsId,
|
|
425
|
+
fix: fixId,
|
|
426
|
+
proofs2: proofs2Id,
|
|
427
|
+
...(screens2 !== undefined ? { screens2: screens2Id } : {}),
|
|
428
|
+
...(escalate !== undefined ? { escalate: escalateId } : {}),
|
|
429
|
+
gate: gateId,
|
|
430
|
+
},
|
|
431
|
+
proofs,
|
|
432
|
+
...(repair !== undefined ? { repair } : {}),
|
|
433
|
+
review,
|
|
434
|
+
findings,
|
|
435
|
+
fix,
|
|
436
|
+
proofs2,
|
|
437
|
+
...(screens2 !== undefined ? { screens2 } : {}),
|
|
438
|
+
...(escalate !== undefined ? { escalate } : {}),
|
|
439
|
+
gate,
|
|
440
|
+
steps,
|
|
441
|
+
};
|
|
442
|
+
}
|