Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

cases.ts

270 lines11.6 KB

1import * as Clock from "effect/Clock";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4​
5import type { Case, World } from "./grade.ts";
6​
7const defined = (world: World, name: string) =>
8 world.definitions.find((definition) => definition.name === name);
9const missing = (name: string) => [`No workflow named ${name} is defined.`];
10​
11const todos = [
12 { file: "src/a.ts", line: 3, text: "handle the empty list" },
13 { file: "src/b.ts", line: 9, text: "remove after the migration" },
14];
15​
16const SUCCEEDED = JSON.stringify({ status: "succeeded", service: "billing" });
17const FAILED = JSON.stringify({ status: "failed", service: "checkout-api" });
18​
19export const cases: ReadonlyArray<Case> = [
20 {
21 id: "scheduled-agent-then-approval",
22 source:
23 "The dependency report in the skill: a schedule, typed agent data, a question, a continued thread.",
24 task: [
25 "Write a workflow named `todo-sweep` that runs every weekday (Monday to Friday) at 09:00.",
26 "An agent in the project with id `proj-1` lists the TODO comments in the repository as data shaped `{ todos: [{ file: string, line: number, text: string }] }`, without changing files.",
27 "If there are none, the run ends there; that is not a failure.",
28 "Otherwise a person is asked whether to fix them, with the choices `fix` and `skip`, and the run waits at most one day for the answer.",
29 "On `fix`, the same agent thread fixes them. On `skip` or no answer, the run ends.",
30 ].join("\n"),
31 scenarios: [
32 {
33 name: "nothing to do",
34 agent: () => ({ output: { todos: [] } }),
35 check: (world) =>
36 world.server(
37 Effect.fn(function* (workflows) {
38 const definition = defined(world, "todo-sweep");
39 if (definition === undefined) return missing("todo-sweep");
40 const problems: Array<string> = [];
41 const schedule = definition.triggers.flatMap((trigger) =>
42 trigger.kind === "schedule" && trigger.schedule.type === "fixed_time"
43 ? [trigger.schedule]
44 : [],
45 )[0];
46 if (schedule === undefined) {
47 problems.push("It has no time-of-day trigger.");
48 } else {
49 if (!/^0?9:00$/.test(schedule.timeOfDay)) {
50 problems.push(`It runs at ${schedule.timeOfDay}, not 09:00.`);
51 }
52 if ([...(schedule.weekdays ?? [])].toSorted().join() !== "1,2,3,4,5") {
53 problems.push("It does not run on exactly Monday to Friday.");
54 }
55 }
56 const { runId } = yield* workflows.start(
57 "todo-sweep",
58 { scheduledAt: "2026-01-05T09:00:00.000Z" },
59 { idempotencyKey: "a" },
60 );
61 const ended = yield* world.reaches(
62 workflows,
63 runId,
64 "stopped",
65 "completed",
66 "paused",
67 );
68 if (Option.isNone(ended)) return [...problems, "The run never ended."];
69 if (ended.value.status === "paused") {
70 problems.push(`The run paused: ${ended.value.detail}`);
71 }
72 if (world.turns.length !== 1) {
73 problems.push(`It asked for ${world.turns.length} agent turns, not 1.`);
74 }
75 if (world.turns[0]?.project !== "proj-1") {
76 problems.push("The agent did not start in proj-1.");
77 }
78 if ((yield* workflows.questions).length > 0) {
79 problems.push("It asked a person although there was nothing to fix.");
80 }
81 return problems;
82 }),
83 ),
84 },
85 {
86 name: "approved",
87 agent: (turn) =>
88 turn.outputSchema === undefined ? { reply: "Fixed." } : { output: { todos } },
89 check: (world) =>
90 world.server(
91 Effect.fn(function* (workflows) {
92 if (defined(world, "todo-sweep") === undefined) return missing("todo-sweep");
93 const { runId } = yield* workflows.start(
94 "todo-sweep",
95 { scheduledAt: "2026-01-05T09:00:00.000Z" },
96 { idempotencyKey: "b" },
97 );
98 const question = yield* world.asks(workflows, runId);
99 if (Option.isNone(question)) return ["It did not ask a person whether to fix them."];
100 yield* workflows.signal(
101 { runId, wait: question.value },
102 { choice: "fix" },
103 { id: "answer-1" },
104 );
105 const ended = yield* world.reaches(
106 workflows,
107 runId,
108 "completed",
109 "paused",
110 "stopped",
111 );
112 if (Option.isNone(ended) || ended.value.status !== "completed") {
113 return [
114 `After "fix" the run did not complete: ${Option.getOrUndefined(ended)?.status ?? "never ended"}`,
115 ];
116 }
117 const [first, second] = world.turns;
118 if (world.turns.length !== 2) {
119 return [`It asked for ${world.turns.length} agent turns, not 2.`];
120 }
121 return second?.thread === `thread-${first?.key}`
122 ? []
123 : ["The fix did not continue the first agent's thread."];
124 }),
125 ),
126 },
127 ],
128 },
129 {
130 id: "webhook-filter-and-dedupe",
131 source:
132 "Scheduled-task webhooks keep filters and replay keys as settings; a workflow has to write both.",
133 task: [
134 "Write a workflow named `deploy-watch` started by a webhook named `deploys`.",
135 'The request body is JSON shaped `{ "status": string, "service": string }`.',
136 "A sender may deliver the same event more than once; the header `x-delivery-id` identifies an event, and one event must be one run.",
137 "Anything whose status is not `failed` is ignored: the run ends there, which is not a failure.",
138 "For a failed deploy, an agent in the project with id `proj-1` investigates. Its brief must name the service.",
139 ].join("\n"),
140 scenarios: [
141 {
142 name: "deliveries",
143 agent: () => ({ reply: "The migration timed out." }),
144 check: (world) =>
145 world.server(
146 Effect.fn(function* (workflows) {
147 if (defined(world, "deploy-watch") === undefined) return missing("deploy-watch");
148 const hook = (yield* workflows.hooks).find((entry) => entry.name === "deploys");
149 if (hook === undefined) return ["It has no webhook named deploys."];
150 const [hookId, token] = hook.path.split("/").slice(-2).map(decodeURIComponent);
151 const deliver = (delivery: string, body: string) =>
152 workflows
153 .deliver({
154 hookId: hookId!,
155 token: token!,
156 request: {
157 method: "POST",
158 headers: { "content-type": "application/json", "x-delivery-id": delivery },
159 query: "",
160 body,
161 },
162 })
163 .pipe(Effect.map(Option.getOrThrow));
164 const problems: Array<string> = [];
165​
166 const fine = yield* deliver("d1", SUCCEEDED);
167 const ignored = yield* world.reaches(
168 workflows,
169 fine.runId,
170 "skipped",
171 "stopped",
172 "completed",
173 "paused",
174 );
175 if (Option.isNone(ignored) || ignored.value.status === "paused") {
176 problems.push("A deploy that did not fail left its run paused or unfinished.");
177 }
178 if (world.turns.length > 0)
179 problems.push("It started an agent for a deploy that did not fail.");
180​
181 const failed = yield* deliver("d2", FAILED);
182 const again = yield* deliver("d2", FAILED);
183 if (again.runId !== failed.runId)
184 problems.push("A redelivered event started a second run.");
185 const done = yield* world.reaches(
186 workflows,
187 failed.runId,
188 "completed",
189 "paused",
190 "stopped",
191 );
192 if (Option.isNone(done) || done.value.status !== "completed") {
193 problems.push(
194 `A failed deploy's run did not complete: ${Option.getOrUndefined(done)?.detail ?? "never ended"}`,
195 );
196 }
197 const turn = world.turns[0];
198 if (turn === undefined)
199 return [...problems, "It started no agent for a failed deploy."];
200 if (turn.project !== "proj-1") problems.push("The agent did not start in proj-1.");
201 if (!turn.prompt.includes("checkout-api"))
202 problems.push("The brief does not name the service.");
203 return problems;
204 }),
205 ),
206 },
207 ],
208 },
209 {
210 id: "replay-keeps-what-it-read",
211 source:
212 "The replay rule: the body runs again after a wait, so a value read from the world outside a step changes under the run.",
213 task: [
214 "Write a workflow named `stamp`, started by hand with the payload `{ label: string }`, that returns a string.",
215 "It reads the current time in milliseconds since the epoch, then waits for a signal named `go` whose value is a string.",
216 "When the signal arrives it returns `<label>:<time>:<signal value>`, where time is the time it read at the start.",
217 "The server may restart while it waits.",
218 ].join("\n"),
219 scenarios: [
220 {
221 name: "restart while waiting",
222 agent: () => ({}),
223 check: Effect.fn(function* (world) {
224 if (defined(world, "stamp") === undefined) return missing("stamp");
225 const started = yield* world.server(
226 Effect.fn(function* (workflows) {
227 const { runId } = yield* workflows.start(
228 "stamp",
229 { label: "a" },
230 { idempotencyKey: "s" },
231 );
232 const waiting = yield* world.reaches(
233 workflows,
234 runId,
235 "waiting",
236 "paused",
237 "completed",
238 );
239 return { runId, waiting };
240 }),
241 );
242 if (Option.isNone(started.waiting) || started.waiting.value.status !== "waiting") {
243 return ["It never waited for the signal."];
244 }
245 const before = yield* Clock.currentTimeMillis;
246 // Long enough that a time read again after the restart is plainly a later one.
247 yield* Effect.sleep("300 millis");
248 return yield* world.server(
249 Effect.fn(function* (workflows) {
250 yield* workflows.signal({ runId: started.runId, wait: "go" }, "now", { id: "go-1" });
251 const done = yield* world.reaches(workflows, started.runId, "completed", "paused");
252 if (Option.isNone(done) || done.value.status !== "completed") {
253 return [
254 `It did not complete after the signal: ${Option.getOrUndefined(done)?.detail ?? "never ended"}`,
255 ];
256 }
257 const match = /^a:(\d+):now$/.exec(String(done.value.result));
258 if (match === null) return [`It returned ${String(done.value.result)}.`];
259 return Number(match[1]) <= before
260 ? []
261 : [
262 "The time it returned was read again after the restart, not kept from the start.",
263 ];
264 }),
265 );
266 }),
267 },
268 ],
269 },
270];