@niwa-labs/workflow 0.0.0-stage → 0.0.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 +13 -2
- package/package.json +55 -4
- package/src/actions.ts +408 -0
- package/src/agent.ts +211 -0
- package/src/ask.ts +84 -0
- package/src/bridge.ts +710 -0
- package/src/catalogue.ts +74 -0
- package/src/check.ts +880 -0
- package/src/child.ts +1378 -0
- package/src/cockpit/actions.ts +128 -0
- package/src/cockpit/catalogue-actions.ts +97 -0
- package/src/cockpit/dashboard.tsx +134 -0
- package/src/cockpit/groups.ts +45 -0
- package/src/cockpit/index.tsx +50 -0
- package/src/cockpit/launch.tsx +136 -0
- package/src/cockpit/plan.ts +101 -0
- package/src/cockpit/run-commands.tsx +102 -0
- package/src/cockpit/run-page.tsx +140 -0
- package/src/cockpit/run-summary.ts +16 -0
- package/src/cockpit/runs.tsx +73 -0
- package/src/cockpit/statistics.tsx +176 -0
- package/src/cockpit/track.tsx +159 -0
- package/src/cockpit/workflows.tsx +383 -0
- package/src/describe-cache.ts +122 -0
- package/src/describe.ts +174 -0
- package/src/guide.ts +213 -0
- package/src/index.ts +2813 -0
- package/src/inspect.ts +235 -0
- package/src/installation.ts +90 -0
- package/src/language.ts +217 -0
- package/src/records-entry.ts +63 -0
- package/src/records.ts +574 -0
- package/src/registry.ts +250 -0
- package/src/revise.ts +89 -0
- package/src/runner.ts +339 -0
- package/src/script-types.ts +549 -0
- package/src/stats.ts +460 -0
- package/src/status.ts +512 -0
- package/src/store.ts +259 -0
- package/src/subscriptions.ts +102 -0
- package/src/waiting.ts +61 -0
- package/test/cockpit-actions.test.ts +20 -0
- package/test/cockpit-groups.test.ts +42 -0
- package/test/cockpit-plan.test.ts +118 -0
- package/test/fixtures/branches-host.ts +58 -0
- package/test/fixtures/branches-script.js +23 -0
- package/test/fixtures/bridge-host.ts +23 -0
- package/test/fixtures/bridge-keys-script.js +15 -0
- package/test/fixtures/bridge-wait-script.js +8 -0
- package/test/fixtures/cancel-host.ts +58 -0
- package/test/fixtures/cancel-script.js +12 -0
- package/test/fixtures/crash-host.ts +61 -0
- package/test/fixtures/crash-script.js +14 -0
- package/test/fixtures/ping-records.ts +20 -0
- package/test/fixtures/revision-host.ts +57 -0
- package/test/fixtures/revision-script.js +11 -0
- package/test/fixtures/running-host.ts +61 -0
- package/test/fixtures/running-script.js +12 -0
- package/test/fixtures/with-data/flow.ts +2 -0
- package/test/fixtures/with-data/table.json +1 -0
- package/test/git-repo.ts +45 -0
- package/test/git-worktree.test.ts +413 -0
- package/test/question-format.test.ts +279 -0
- package/test/workflow-agent-caps.test.ts +91 -0
- package/test/workflow-agent-lifetime.test.ts +93 -0
- package/test/workflow-agent-options.test.ts +216 -0
- package/test/workflow-agent-schema.test.ts +386 -0
- package/test/workflow-agent-typings.test.ts +241 -0
- package/test/workflow-ask-resume.test.ts +145 -0
- package/test/workflow-branch-keys.test.ts +133 -0
- package/test/workflow-bridge-inproc.test.ts +230 -0
- package/test/workflow-bridge.test.ts +141 -0
- package/test/workflow-builtin-candidate.test.ts +230 -0
- package/test/workflow-builtin.test.ts +197 -0
- package/test/workflow-cancel-agent.test.ts +144 -0
- package/test/workflow-cancel.test.ts +166 -0
- package/test/workflow-chain.test.ts +84 -0
- package/test/workflow-check-options.test.ts +144 -0
- package/test/workflow-check-uses.test.ts +148 -0
- package/test/workflow-check.test.ts +282 -0
- package/test/workflow-contract.test.ts +295 -0
- package/test/workflow-crash-resume.test.ts +141 -0
- package/test/workflow-deadline.test.ts +102 -0
- package/test/workflow-describe-cache.test.ts +240 -0
- package/test/workflow-directory.test.ts +73 -0
- package/test/workflow-emits.test.ts +261 -0
- package/test/workflow-group-keys.test.ts +72 -0
- package/test/workflow-harnesses.test.ts +262 -0
- package/test/workflow-inspect.test.ts +152 -0
- package/test/workflow-language-types.test.ts +148 -0
- package/test/workflow-manifest.test.ts +93 -0
- package/test/workflow-manual-pause.test.ts +116 -0
- package/test/workflow-owners.test.ts +154 -0
- package/test/workflow-pause.test.ts +200 -0
- package/test/workflow-phases.test.ts +445 -0
- package/test/workflow-plugin-entries.test.ts +59 -0
- package/test/workflow-record-refs.test.ts +310 -0
- package/test/workflow-replay.test.ts +206 -0
- package/test/workflow-resume-once.test.ts +188 -0
- package/test/workflow-revision.test.ts +216 -0
- package/test/workflow-scope.test.ts +505 -0
- package/test/workflow-script-defaults.test.ts +119 -0
- package/test/workflow-script-error.test.ts +228 -0
- package/test/workflow-script-libraries.test.ts +88 -0
- package/test/workflow-script-packages.test.ts +119 -0
- package/test/workflow-stdout.test.ts +89 -0
- package/test/workflow-stop.test.ts +299 -0
- package/test/workflow-store.test.ts +220 -0
- package/test/workflow-visible.test.ts +136 -0
- package/test/workflow-wait-version.test.ts +54 -0
- package/test/workflow-wait.test.ts +224 -0
- package/test/workflow-waiting-agent.test.ts +64 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// A run is not started twice at once.
|
|
2
|
+
//
|
|
3
|
+
// A resume or a revision starts another invocation of the run's handler. While the invocation
|
|
4
|
+
// that is going has not ended, the two would write over the same children of the run, so the
|
|
5
|
+
// resume is refused.
|
|
6
|
+
//
|
|
7
|
+
// The scenario runs against a real host process (test/fixtures/running-host.ts):
|
|
8
|
+
//
|
|
9
|
+
// 1. the script makes a call that waits: the run is still going
|
|
10
|
+
// 2. the owner resumes the run: it is refused
|
|
11
|
+
// 3. the owner revises the run: it is refused too
|
|
12
|
+
// 4. a run that paused is resolved as part of the resume, and one new run starts
|
|
13
|
+
//
|
|
14
|
+
// Only the public API is used: the entries of the packages, and of testing.
|
|
15
|
+
|
|
16
|
+
import { expect, test } from "bun:test";
|
|
17
|
+
import { fileURLToPath } from "node:url";
|
|
18
|
+
|
|
19
|
+
import type { ActionName, StoredRecord } from "@niwa-labs/core";
|
|
20
|
+
import { counter, eventually, spawnHost, tempHome, watch, connectAsOwner } from "@niwa-labs/testing";
|
|
21
|
+
import { z } from "zod";
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/running-host.ts", import.meta.url));
|
|
25
|
+
const SCRIPT = fileURLToPath(new URL("./fixtures/running-script.js", import.meta.url));
|
|
26
|
+
|
|
27
|
+
/** What the workflow actions answer. */
|
|
28
|
+
const RunOutput = z.object({ run: z.string(), finished: z.boolean() });
|
|
29
|
+
|
|
30
|
+
/** The state of an invocation, as the journal holds it. */
|
|
31
|
+
const InvocationState = z.object({
|
|
32
|
+
invocation: z.string(),
|
|
33
|
+
state: z.string(),
|
|
34
|
+
reason: z.string().optional(),
|
|
35
|
+
choices: z.array(z.string()).optional(),
|
|
36
|
+
key: z.string().optional(),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
/** The states of the invocations, in the order they happened. */
|
|
40
|
+
function statesOf(seen: StoredRecord[]): Array<z.infer<typeof InvocationState>> {
|
|
41
|
+
const states: Array<z.infer<typeof InvocationState>> = [];
|
|
42
|
+
for (const record of seen) {
|
|
43
|
+
if (record.type !== "niwa.invocation.state") continue;
|
|
44
|
+
const parsed = InvocationState.safeParse(record.data);
|
|
45
|
+
if (parsed.success) states.push(parsed.data);
|
|
46
|
+
}
|
|
47
|
+
return states;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The records of one type, as the observer saw them. */
|
|
51
|
+
function ofType(seen: StoredRecord[], type: string): StoredRecord[] {
|
|
52
|
+
return seen.filter((record) => record.type === type);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
test("a run that is still going is not resumed or revised", async () => {
|
|
56
|
+
const home = await tempHome();
|
|
57
|
+
const homePath = home.path;
|
|
58
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
59
|
+
let observed: Awaited<ReturnType<typeof watch>> | undefined;
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
host = spawnHost(HOST_SCRIPT, homePath);
|
|
63
|
+
await host.ready();
|
|
64
|
+
const client = await connectAsOwner(homePath);
|
|
65
|
+
observed = await watch(homePath);
|
|
66
|
+
const seen = observed.seen;
|
|
67
|
+
|
|
68
|
+
// 1. The script runs: the first call is made and waits, so the run is still going.
|
|
69
|
+
const started = RunOutput.parse(
|
|
70
|
+
await client.call("workflow.start" as ActionName, { file: SCRIPT } as never),
|
|
71
|
+
);
|
|
72
|
+
await eventually(
|
|
73
|
+
() => counter(homePath, "running.agent").count() === 1,
|
|
74
|
+
20_000,
|
|
75
|
+
"the first call should have been made",
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
// The run is going: its invocation is running.
|
|
79
|
+
await eventually(
|
|
80
|
+
() => statesOf(seen).some((state) => state.state === "running"),
|
|
81
|
+
20_000,
|
|
82
|
+
"the invocation of the run should be running",
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
// 2. The owner resumes the run: it is refused, and nothing runs.
|
|
86
|
+
await expect(
|
|
87
|
+
client.call("workflow.resume" as ActionName, { run: started.run } as never),
|
|
88
|
+
).rejects.toThrow("still going");
|
|
89
|
+
|
|
90
|
+
// 3. The owner revises the run: it is refused too.
|
|
91
|
+
await expect(
|
|
92
|
+
client.call("workflow.revise" as ActionName, { run: started.run, file: SCRIPT } as never),
|
|
93
|
+
).rejects.toThrow("still going");
|
|
94
|
+
|
|
95
|
+
// No second run was started, and no call was made twice.
|
|
96
|
+
expect(ofType(seen, "workflow.run.started")).toHaveLength(1);
|
|
97
|
+
expect(counter(homePath, "running.agent").count()).toBe(1);
|
|
98
|
+
|
|
99
|
+
await client.close().catch(() => {});
|
|
100
|
+
} finally {
|
|
101
|
+
await observed?.stop();
|
|
102
|
+
await host?.kill().catch(() => {});
|
|
103
|
+
await home.cleanup();
|
|
104
|
+
}
|
|
105
|
+
}, 60_000);
|
|
106
|
+
|
|
107
|
+
test("a paused run is resolved as part of the resume, and one new run starts", async () => {
|
|
108
|
+
const home = await tempHome();
|
|
109
|
+
const homePath = home.path;
|
|
110
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
111
|
+
let observed: Awaited<ReturnType<typeof watch>> | undefined;
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
host = spawnHost(HOST_SCRIPT, homePath);
|
|
115
|
+
await host.ready();
|
|
116
|
+
const client = await connectAsOwner(homePath);
|
|
117
|
+
observed = await watch(homePath);
|
|
118
|
+
const seen = observed.seen;
|
|
119
|
+
|
|
120
|
+
// The script runs: its first call fails, so the run pauses.
|
|
121
|
+
const script = `${homePath}/running-pausing.js`;
|
|
122
|
+
await Bun.write(
|
|
123
|
+
script,
|
|
124
|
+
[
|
|
125
|
+
"export default workflow({ output: z.json() }, async () => {",
|
|
126
|
+
' const answer = await ask({ label: "work:first" }, "This one will fail.");',
|
|
127
|
+
" return answer;",
|
|
128
|
+
"});",
|
|
129
|
+
"",
|
|
130
|
+
].join("\n"),
|
|
131
|
+
);
|
|
132
|
+
const started = RunOutput.parse(
|
|
133
|
+
await client.call("workflow.start" as ActionName, { file: script } as never),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
await eventually(
|
|
137
|
+
() => statesOf(seen).some((state) => state.state === "paused"),
|
|
138
|
+
20_000,
|
|
139
|
+
"the run should have paused",
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
// The resume resolves the pause with a cancel, and starts one new run.
|
|
143
|
+
const resumed = RunOutput.parse(
|
|
144
|
+
await client.call("workflow.resume" as ActionName, { run: started.run } as never),
|
|
145
|
+
);
|
|
146
|
+
expect(resumed.run).toBe(started.run);
|
|
147
|
+
|
|
148
|
+
await eventually(
|
|
149
|
+
() => ofType(seen, "workflow.run.started").length === 2,
|
|
150
|
+
20_000,
|
|
151
|
+
"the resume should have started one new run",
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
const forRun = (record: StoredRecord) => (record.data as { run: string }).run === started.run;
|
|
155
|
+
const requests = ofType(seen, "workflow.run.requested").filter(forRun);
|
|
156
|
+
const starts = ofType(seen, "workflow.run.started").filter(forRun);
|
|
157
|
+
expect(requests).toHaveLength(2);
|
|
158
|
+
expect(starts).toHaveLength(2);
|
|
159
|
+
const admissions = ofType(seen, "niwa.invocation.admitted").map(
|
|
160
|
+
row => row.data as { handler: string; record: string; invocation: string },
|
|
161
|
+
);
|
|
162
|
+
// Each request admits the script once. Cleanup handlers can subscribe to the same requests.
|
|
163
|
+
const invocations = starts.map(({ actor }, index) => {
|
|
164
|
+
if (actor.kind !== "handler") throw new Error("the script start has no handler");
|
|
165
|
+
const admitted = admissions.filter(entry => entry.handler === actor.handler && entry.record === requests[index]?.id);
|
|
166
|
+
expect(admitted.map(entry => entry.invocation)).toEqual([actor.invocation]);
|
|
167
|
+
return actor.invocation;
|
|
168
|
+
});
|
|
169
|
+
const states = invocations.map(
|
|
170
|
+
(invocation) => statesOf(seen).filter((s) => s.invocation === invocation).at(-1)?.state,
|
|
171
|
+
);
|
|
172
|
+
// The old invocation was resolved by the resume: it is over.
|
|
173
|
+
expect(states[0]).toBe("done");
|
|
174
|
+
// The new invocation is the one that goes on: it is running or paused, not done.
|
|
175
|
+
expect(["running", "paused"]).toContain(states[1]);
|
|
176
|
+
|
|
177
|
+
// The resolution of the old invocation was written: the pause was answered with a cancel.
|
|
178
|
+
const resolutions = ofType(seen, "niwa.invocation.resolved");
|
|
179
|
+
expect(resolutions.length).toBe(1);
|
|
180
|
+
expect(resolutions[0]?.data).toMatchObject({ invocation: invocations[0], choice: "cancel" });
|
|
181
|
+
|
|
182
|
+
await client.close().catch(() => {});
|
|
183
|
+
} finally {
|
|
184
|
+
await observed?.stop();
|
|
185
|
+
await host?.kill().catch(() => {});
|
|
186
|
+
await home.cleanup();
|
|
187
|
+
}
|
|
188
|
+
}, 60_000);
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// A run started again on an edited script takes the calls of its earlier start along the longest
|
|
2
|
+
// unchanged prefix. The first new or changed call runs, and so does everything after it in its
|
|
3
|
+
// branch, or after the scope that holds it; a call with an effect that was made before as the same
|
|
4
|
+
// call is taken all the same, never made twice. An agent call is its prompt and its harness. The
|
|
5
|
+
// owner may ask for the steps from one of them, a branch or a phase on to run again. Nothing is
|
|
6
|
+
// taken from another run, and the journal and workflow.inspect say which steps were taken.
|
|
7
|
+
|
|
8
|
+
import { afterEach, expect, test } from "bun:test";
|
|
9
|
+
import { writeFile } from "node:fs/promises";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
|
|
14
|
+
import { action } from "@niwa-labs/core";
|
|
15
|
+
import type { ActionName, StoredRecord } from "@niwa-labs/core";
|
|
16
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
17
|
+
import { createNiwa, eventually, requests, tempHome, testClock, timers } from "@niwa-labs/testing";
|
|
18
|
+
import type { Host } from "@niwa-labs/testing";
|
|
19
|
+
|
|
20
|
+
import { workflow } from "../src/index.ts";
|
|
21
|
+
import type { WorkflowInspection } from "../src/inspect.ts";
|
|
22
|
+
|
|
23
|
+
const homes: Array<{ cleanup(): Promise<void> }> = [];
|
|
24
|
+
const hosts: Host[] = [];
|
|
25
|
+
|
|
26
|
+
afterEach(async () => {
|
|
27
|
+
for (const host of hosts.splice(0)) await host.close().catch(() => {});
|
|
28
|
+
for (const home of homes.splice(0)) await home.cleanup();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const Revision = z.object({
|
|
32
|
+
reused: z.array(z.object({ key: z.string() })),
|
|
33
|
+
rerun: z.array(z.object({ key: z.string(), reason: z.string() })),
|
|
34
|
+
});
|
|
35
|
+
const Resumed = z.object({ run: z.string(), revision: Revision.optional() });
|
|
36
|
+
|
|
37
|
+
async function setup() {
|
|
38
|
+
const home = await tempHome();
|
|
39
|
+
homes.push(home);
|
|
40
|
+
const calls: string[] = [];
|
|
41
|
+
const io = { input: z.object({ name: z.string() }), output: z.object({ name: z.string(), at: z.number() }) };
|
|
42
|
+
const answer = (name: string) => {
|
|
43
|
+
calls.push(name);
|
|
44
|
+
return { name, at: calls.length };
|
|
45
|
+
};
|
|
46
|
+
const agent = action("rev.agent", {
|
|
47
|
+
description: "An agent: it answers with its prompt.",
|
|
48
|
+
input: z.object({ prompt: z.string(), harness: z.string().optional(), label: z.string().optional(), effort: z.string().optional() }),
|
|
49
|
+
output: z.object({ answer: z.string(), at: z.number() }),
|
|
50
|
+
// A session is started and asked: it has an effect.
|
|
51
|
+
recovery: "do-not-repeat",
|
|
52
|
+
effect: "write",
|
|
53
|
+
run: async ({ prompt }) => {
|
|
54
|
+
calls.push(`agent:${prompt}`);
|
|
55
|
+
return { answer: prompt, at: calls.length };
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
const niwa = createNiwa({ home: home.path, clock: testClock(1_700_000_000_000) });
|
|
59
|
+
niwa.use(requests());
|
|
60
|
+
niwa.use(timers([]));
|
|
61
|
+
niwa.use(
|
|
62
|
+
plugin("rev", (api) => {
|
|
63
|
+
api.action(agent);
|
|
64
|
+
api.action(action("rev.read", { ...io, description: "Reads: it may be made again.", recovery: "repeat", effect: "read", run: async ({ name }) => answer(`read:${name}`) }));
|
|
65
|
+
api.action(action("rev.write", { ...io, description: "Writes: it is made once.", recovery: "do-not-repeat", effect: "write", run: async ({ name }) => answer(`write:${name}`) }));
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
niwa.use(workflow({ agentAction: agent }));
|
|
69
|
+
await niwa.serve();
|
|
70
|
+
hosts.push(niwa);
|
|
71
|
+
const file = join(home.path, "script.js");
|
|
72
|
+
const call = <T>(name: string, input: unknown): Promise<T> => niwa.call(name as ActionName, input as never) as Promise<T>;
|
|
73
|
+
const records = (): Promise<StoredRecord[]> => niwa.records.read();
|
|
74
|
+
const finished = async (run: string): Promise<unknown[]> =>
|
|
75
|
+
(await records())
|
|
76
|
+
.filter((row) => row.type === "workflow.run.finished" && (row.data as { run: string }).run === run)
|
|
77
|
+
.map((row) => (row.data as { outcome: { result?: unknown } }).outcome.result);
|
|
78
|
+
return {
|
|
79
|
+
calls,
|
|
80
|
+
records,
|
|
81
|
+
write: (source: string) => writeFile(file, source),
|
|
82
|
+
start: async (): Promise<string> => (await call<{ run: string }>("workflow.start", { file })).run,
|
|
83
|
+
resume: async (run: string, from?: string) =>
|
|
84
|
+
Resumed.parse(await call("workflow.resume", { run, ...(from === undefined ? {} : { from }) })),
|
|
85
|
+
inspect: (run: string) => call<WorkflowInspection>("workflow.inspect", { run }),
|
|
86
|
+
/** Wait for the n-th end of the run, and give what it returned. */
|
|
87
|
+
ended: async (run: string, n: number): Promise<unknown> => {
|
|
88
|
+
await eventually(async () => (await finished(run)).length >= n, 15_000, `the run should end ${String(n)} times`);
|
|
89
|
+
return (await finished(run))[n - 1];
|
|
90
|
+
},
|
|
91
|
+
reusedKeys: async (): Promise<string[]> =>
|
|
92
|
+
(await records()).filter((row) => row.type === "workflow.step.reused").map((row) => (row.data as { key: string }).key),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const script = (second: string) => `export default workflow({ uses: ["rev.*"], output: z.json() }, async () => {
|
|
97
|
+
const a = await ask({ label: "a" }, "first");
|
|
98
|
+
const r1 = await niwa.rev.read({ name: "one" }, { label: "r1" });
|
|
99
|
+
const b = await ask({ label: "b" }, "${second}");
|
|
100
|
+
const r2 = await niwa.rev.read({ name: "two" }, { label: "r2" });
|
|
101
|
+
const w = await niwa.rev.write({ name: "once" }, { label: "w" });
|
|
102
|
+
return [a.answer, r1.name, b.answer, r2.name, w.name];
|
|
103
|
+
});`;
|
|
104
|
+
|
|
105
|
+
test("a changed call and what comes after it run; the unchanged prefix and an effect made before are taken", async () => {
|
|
106
|
+
const t = await setup();
|
|
107
|
+
await t.write(script("second"));
|
|
108
|
+
const run = await t.start();
|
|
109
|
+
expect(await t.ended(run, 1)).toEqual(["first", "read:one", "second", "read:two", "write:once"]);
|
|
110
|
+
expect(t.calls).toEqual(["agent:first", "read:one", "agent:second", "read:two", "write:once"]);
|
|
111
|
+
|
|
112
|
+
await t.write(script("second, changed"));
|
|
113
|
+
const resumed = await t.resume(run);
|
|
114
|
+
expect(resumed.revision?.reused.map((one) => one.key)).toEqual(["label:a", "label:r1", "label:w"]);
|
|
115
|
+
expect(resumed.revision?.rerun.map((one) => [one.key, one.reason])).toEqual([
|
|
116
|
+
["label:b", "changed"],
|
|
117
|
+
["label:r2", "after a change"],
|
|
118
|
+
]);
|
|
119
|
+
expect(await t.ended(run, 2)).toEqual(["first", "read:one", "second, changed", "read:two", "write:once"]);
|
|
120
|
+
// The changed call and the read after it ran; the write after it was made before, and is not made twice.
|
|
121
|
+
expect(t.calls.slice(5)).toEqual(["agent:second, changed", "read:two"]);
|
|
122
|
+
expect(await t.reusedKeys()).toEqual(["label:a", "label:r1", "label:w"]);
|
|
123
|
+
|
|
124
|
+
// workflow.inspect says which steps a later start took, and which have an effect.
|
|
125
|
+
const steps = (await t.inspect(run)).steps;
|
|
126
|
+
const first = steps.filter((step) => step.key === "label:a");
|
|
127
|
+
expect(first.map((step) => step.reused === true)).toEqual([true]);
|
|
128
|
+
expect(steps.find((step) => step.key === "label:w")).toMatchObject({ reused: true, effect: true });
|
|
129
|
+
expect(steps.find((step) => step.key === "label:r1")?.effect).toBeUndefined();
|
|
130
|
+
}, 30_000);
|
|
131
|
+
|
|
132
|
+
test("an agent call is its prompt and its harness: another effort takes the recorded answer", async () => {
|
|
133
|
+
const t = await setup();
|
|
134
|
+
const source = (effort: string) => `export default workflow({ uses: ["rev.*"], output: z.json() }, async () => {
|
|
135
|
+
return (await ask({ label: "only", effort: "${effort}" }, "same prompt")).answer;
|
|
136
|
+
});`;
|
|
137
|
+
await t.write(source("low"));
|
|
138
|
+
const run = await t.start();
|
|
139
|
+
expect(await t.ended(run, 1)).toBe("same prompt");
|
|
140
|
+
await t.write(source("high"));
|
|
141
|
+
const resumed = await t.resume(run);
|
|
142
|
+
expect(resumed.revision?.reused.map((one) => one.key)).toEqual(["label:only"]);
|
|
143
|
+
expect(await t.ended(run, 2)).toBe("same prompt");
|
|
144
|
+
expect(t.calls).toEqual(["agent:same prompt"]);
|
|
145
|
+
}, 30_000);
|
|
146
|
+
|
|
147
|
+
test("the owner asks for the steps from one on to run again: the effects before it and after it are not made twice", async () => {
|
|
148
|
+
const t = await setup();
|
|
149
|
+
await t.write(script("second"));
|
|
150
|
+
const run = await t.start();
|
|
151
|
+
await t.ended(run, 1);
|
|
152
|
+
const resumed = await t.resume(run, "label:b");
|
|
153
|
+
expect(resumed.revision).toBeUndefined();
|
|
154
|
+
expect(await t.ended(run, 2)).toEqual(["first", "read:one", "second", "read:two", "write:once"]);
|
|
155
|
+
expect(t.calls.slice(5)).toEqual(["agent:second", "read:two"]);
|
|
156
|
+
expect(await t.reusedKeys()).toEqual(["label:a", "label:r1", "label:w"]);
|
|
157
|
+
}, 30_000);
|
|
158
|
+
|
|
159
|
+
test("the owner asks for a phase to run again: its steps run, and the steps before it are taken", async () => {
|
|
160
|
+
const t = await setup();
|
|
161
|
+
await t.write(`export default workflow({ uses: ["rev.*"], phases: [{ title: "Gather" }, { title: "Judge" }], output: z.json() }, async () => {
|
|
162
|
+
phase("Gather");
|
|
163
|
+
const one = await niwa.rev.read({ name: "one" }, { label: "one" });
|
|
164
|
+
phase("Judge");
|
|
165
|
+
const two = await niwa.rev.read({ name: "two" }, { label: "two" });
|
|
166
|
+
return [one.at, two.at];
|
|
167
|
+
});`);
|
|
168
|
+
const run = await t.start();
|
|
169
|
+
expect(await t.ended(run, 1)).toEqual([1, 2]);
|
|
170
|
+
await t.resume(run, "Judge");
|
|
171
|
+
expect(await t.ended(run, 2)).toEqual([1, 3]);
|
|
172
|
+
expect(t.calls).toEqual(["read:one", "read:two", "read:two"]);
|
|
173
|
+
}, 30_000);
|
|
174
|
+
|
|
175
|
+
test("a change in one branch of a scope leaves the other branches taken, and what comes after the scope runs", async () => {
|
|
176
|
+
const t = await setup();
|
|
177
|
+
const source = (x: string) => `export default workflow({ uses: ["rev.*"], output: z.json() }, async () => {
|
|
178
|
+
const got = await scope("s", undefined, async (s) => {
|
|
179
|
+
s.start("x", async () => { await ask({}, "${x}"); return (await niwa.rev.read({ name: "x2" })).at; });
|
|
180
|
+
s.start("y", async () => { await niwa.rev.read({ name: "y1" }); return (await niwa.rev.read({ name: "y2" })).at; });
|
|
181
|
+
return await s.rest();
|
|
182
|
+
});
|
|
183
|
+
const after = await niwa.rev.read({ name: "after" });
|
|
184
|
+
return [got.x.value, got.y.value, after.name];
|
|
185
|
+
});`;
|
|
186
|
+
await t.write(source("x1"));
|
|
187
|
+
const run = await t.start();
|
|
188
|
+
await t.ended(run, 1);
|
|
189
|
+
expect([...t.calls].sort()).toEqual(["agent:x1", "read:after", "read:x2", "read:y1", "read:y2"]);
|
|
190
|
+
const before = t.calls.length;
|
|
191
|
+
|
|
192
|
+
await t.write(source("x1, changed"));
|
|
193
|
+
const resumed = await t.resume(run);
|
|
194
|
+
expect(resumed.revision?.reused.map((one) => one.key).sort()).toEqual(["@s.y.call-1", "@s.y.call-2"]);
|
|
195
|
+
expect(Object.fromEntries((resumed.revision?.rerun ?? []).map((one) => [one.key, one.reason]))).toEqual({
|
|
196
|
+
"@s.x.call-1": "changed",
|
|
197
|
+
"@s.x.call-2": "after a change",
|
|
198
|
+
"call-1": "after a change",
|
|
199
|
+
});
|
|
200
|
+
await t.ended(run, 2);
|
|
201
|
+
expect(t.calls.slice(before).sort()).toEqual(["agent:x1, changed", "read:after", "read:x2"]);
|
|
202
|
+
}, 30_000);
|
|
203
|
+
|
|
204
|
+
test("a new run of the same script takes nothing: only the starts of one run share their steps", async () => {
|
|
205
|
+
const t = await setup();
|
|
206
|
+
await t.write(script("second"));
|
|
207
|
+
const one = await t.start();
|
|
208
|
+
await t.ended(one, 1);
|
|
209
|
+
const two = await t.start();
|
|
210
|
+
await t.ended(two, 1);
|
|
211
|
+
expect(t.calls).toEqual([
|
|
212
|
+
"agent:first", "read:one", "agent:second", "read:two", "write:once",
|
|
213
|
+
"agent:first", "read:one", "agent:second", "read:two", "write:once",
|
|
214
|
+
]);
|
|
215
|
+
expect(await t.reusedKeys()).toEqual([]);
|
|
216
|
+
}, 30_000);
|