@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,279 @@
|
|
|
1
|
+
// One Question and one Answer for the whole system.
|
|
2
|
+
//
|
|
3
|
+
// The checks:
|
|
4
|
+
// 1. the records of the requests module hold a question and an answer in the one format;
|
|
5
|
+
// 2. an answer that does not fit the kind of its question is refused, and the request stays open;
|
|
6
|
+
// 3. a request is self-describing: its record alone says what was asked and what may be answered;
|
|
7
|
+
// 4. a rule that asks puts a permission question whose subject holds the input of the call as JSON;
|
|
8
|
+
// 5. a script asks with ask.choice, ask.multi, ask.confirm, ask.text and ask.form, and gets a
|
|
9
|
+
// typed value; the type checker holds the script to those types.
|
|
10
|
+
|
|
11
|
+
import { afterEach, expect, test } from "bun:test";
|
|
12
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
|
|
16
|
+
import { AnswerSchema, QuestionSchema, answerMismatch, seq, owner } from "@niwa-labs/core";
|
|
17
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
18
|
+
import { QuestionName, REQUEST_ANSWER, RequestAnswered, RequestCreated } from "@niwa-labs/requests/records";
|
|
19
|
+
import { createNiwa, requests, eventually, tempHome, connectAsOwner, opt, pick, timers, requestIdOf } from "@niwa-labs/testing";
|
|
20
|
+
import type { Host } from "@niwa-labs/testing";
|
|
21
|
+
import { workflow, workflowCheck, workflowStart } from "../src/index.ts";
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
const homes: Array<{ cleanup(): Promise<void> }> = [];
|
|
25
|
+
const hosts: Host[] = [];
|
|
26
|
+
|
|
27
|
+
afterEach(async () => {
|
|
28
|
+
for (const host of hosts.splice(0)) await host.close().catch(() => {});
|
|
29
|
+
for (const home of homes.splice(0)) await home.cleanup();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
async function served(): Promise<{ host: Host; home: string }> {
|
|
33
|
+
const home = await tempHome();
|
|
34
|
+
homes.push(home);
|
|
35
|
+
const host = createNiwa({ home: home.path });
|
|
36
|
+
host.use(requests(), timers([]), workflow());
|
|
37
|
+
await host.serve();
|
|
38
|
+
hosts.push(host);
|
|
39
|
+
return { host, home: home.path };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const recordsOf = (host: Host, type: string): Promise<StoredRecord[]> =>
|
|
43
|
+
host.records.read().then((all) => all.filter((row) => row.type === type));
|
|
44
|
+
|
|
45
|
+
/** One question of every kind, as a request records it. */
|
|
46
|
+
const KINDS = {
|
|
47
|
+
choice: { kind: "choice", title: "Pick one", options: [opt("a"), opt("b")] },
|
|
48
|
+
multi: { kind: "multi", title: "Pick some", options: [opt("a"), opt("b"), opt("c")] },
|
|
49
|
+
confirm: { kind: "confirm", title: "Sure?", options: [opt("yes"), opt("no")] },
|
|
50
|
+
text: { kind: "text", title: "Say something" },
|
|
51
|
+
form: {
|
|
52
|
+
kind: "form",
|
|
53
|
+
title: "Fill it",
|
|
54
|
+
schema: { type: "object", properties: { n: { type: "number" } }, required: ["n"] },
|
|
55
|
+
},
|
|
56
|
+
permission: {
|
|
57
|
+
kind: "permission",
|
|
58
|
+
title: "May it?",
|
|
59
|
+
options: [
|
|
60
|
+
{ id: "ok", label: "Allow", flavor: "allow_once" },
|
|
61
|
+
{ id: "no", label: "Refuse", flavor: "reject_once" },
|
|
62
|
+
],
|
|
63
|
+
subject: { who: "client:x", action: "task.create", rule: "task.create", input: { title: "t", tags: ["a"] } },
|
|
64
|
+
},
|
|
65
|
+
} as const;
|
|
66
|
+
|
|
67
|
+
test("a question of every kind is read back whole, and what is not a question is refused", () => {
|
|
68
|
+
for (const question of Object.values(KINDS)) {
|
|
69
|
+
expect(QuestionSchema.parse(question)).toMatchObject({ kind: question.kind, title: question.title });
|
|
70
|
+
}
|
|
71
|
+
expect(QuestionSchema.safeParse({ kind: "choice", title: "no options", options: [] }).success).toBe(false);
|
|
72
|
+
expect(QuestionSchema.safeParse({ kind: "form", title: "no schema" }).success).toBe(false);
|
|
73
|
+
expect(QuestionSchema.safeParse({ kind: "riddle", title: "?" }).success).toBe(false);
|
|
74
|
+
expect(QuestionSchema.safeParse({ kind: "text" }).success).toBe(false);
|
|
75
|
+
|
|
76
|
+
expect(AnswerSchema.safeParse({ ids: ["a"] }).success).toBe(true);
|
|
77
|
+
expect(AnswerSchema.safeParse({ text: "x" }).success).toBe(true);
|
|
78
|
+
expect(AnswerSchema.safeParse({ form: { n: 1 } }).success).toBe(true);
|
|
79
|
+
expect(AnswerSchema.safeParse({ cancelled: true }).success).toBe(true);
|
|
80
|
+
expect(AnswerSchema.safeParse("main").success).toBe(false);
|
|
81
|
+
expect(AnswerSchema.safeParse({ ids: ["a"], text: "x" }).success).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("the answer must fit the kind of its question", () => {
|
|
85
|
+
const q = (kind: keyof typeof KINDS) => QuestionSchema.parse(KINDS[kind]);
|
|
86
|
+
// choice, confirm and permission take exactly one known id.
|
|
87
|
+
expect(answerMismatch(q("choice"), pick("a"))).toBeUndefined();
|
|
88
|
+
expect(answerMismatch(q("choice"), pick("a", "b"))).toBeDefined();
|
|
89
|
+
expect(answerMismatch(q("choice"), pick("zzz"))).toBeDefined();
|
|
90
|
+
expect(answerMismatch(q("choice"), { text: "a" })).toBeDefined();
|
|
91
|
+
expect(answerMismatch(q("confirm"), pick("yes"))).toBeUndefined();
|
|
92
|
+
expect(answerMismatch(q("permission"), pick("ok"))).toBeUndefined();
|
|
93
|
+
expect(answerMismatch(q("permission"), { text: "ok" })).toBeDefined();
|
|
94
|
+
// multi takes any number of known ids.
|
|
95
|
+
expect(answerMismatch(q("multi"), pick("a", "c"))).toBeUndefined();
|
|
96
|
+
expect(answerMismatch(q("multi"), pick())).toBeUndefined();
|
|
97
|
+
expect(answerMismatch(q("multi"), pick("a", "q"))).toBeDefined();
|
|
98
|
+
// text takes a text, form a value that passes its schema.
|
|
99
|
+
expect(answerMismatch(q("text"), { text: "hi" })).toBeUndefined();
|
|
100
|
+
expect(answerMismatch(q("text"), pick("a"))).toBeDefined();
|
|
101
|
+
expect(answerMismatch(q("form"), { form: { n: 3 } })).toBeUndefined();
|
|
102
|
+
expect(answerMismatch(q("form"), { form: { n: "three" } })).toBeDefined();
|
|
103
|
+
expect(answerMismatch(q("form"), { text: "x" })).toBeDefined();
|
|
104
|
+
// Nothing is answered by cancelling: that is what request.cancel is for.
|
|
105
|
+
for (const kind of Object.keys(KINDS) as Array<keyof typeof KINDS>) {
|
|
106
|
+
expect(answerMismatch(q(kind), { cancelled: true })).toBeDefined();
|
|
107
|
+
}
|
|
108
|
+
// A free text is taken only where the question allows another answer.
|
|
109
|
+
const open = QuestionSchema.parse({ ...KINDS.choice, allowOther: true });
|
|
110
|
+
expect(answerMismatch(open, { text: "c" })).toBeUndefined();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("a request records its question whole, and an answer of the wrong kind is refused and leaves it open", async () => {
|
|
114
|
+
const { host, home } = await served();
|
|
115
|
+
const client = await connectAsOwner(home);
|
|
116
|
+
try {
|
|
117
|
+
for (const [name, question] of Object.entries(KINDS)) {
|
|
118
|
+
await host.emit(RequestCreated, {
|
|
119
|
+
id: requestIdOf(name),
|
|
120
|
+
to: owner,
|
|
121
|
+
question: QuestionName.parse(`format.${name}`),
|
|
122
|
+
input: QuestionSchema.parse(question),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
// The record alone says what was asked: no process that asked is needed to draw it.
|
|
126
|
+
const created = (await recordsOf(host, RequestCreated.name)).map((row) => RequestCreated.schema.parse(row.data));
|
|
127
|
+
expect(created.map((data) => String(data.input.kind))).toEqual(Object.keys(KINDS));
|
|
128
|
+
expect(created.find((data) => data.input.kind === "permission")?.input).toMatchObject({
|
|
129
|
+
subject: { input: { title: "t", tags: ["a"] } },
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const wrong: Record<string, unknown> = {
|
|
133
|
+
choice: { text: "a" },
|
|
134
|
+
multi: pick("zzz"),
|
|
135
|
+
confirm: pick("maybe"),
|
|
136
|
+
text: pick("a"),
|
|
137
|
+
form: { form: { n: "three" } },
|
|
138
|
+
permission: { cancelled: true },
|
|
139
|
+
};
|
|
140
|
+
for (const [name, answer] of Object.entries(wrong)) {
|
|
141
|
+
await expect(client.call(REQUEST_ANSWER, { id: requestIdOf(name), answer })).rejects.toThrow("not valid");
|
|
142
|
+
}
|
|
143
|
+
expect(await recordsOf(host, RequestAnswered.name)).toHaveLength(0);
|
|
144
|
+
|
|
145
|
+
const right: Record<string, unknown> = {
|
|
146
|
+
choice: pick("b"),
|
|
147
|
+
multi: pick("a", "c"),
|
|
148
|
+
confirm: pick("yes"),
|
|
149
|
+
text: { text: "hello" },
|
|
150
|
+
form: { form: { n: 7 } },
|
|
151
|
+
permission: pick("ok"),
|
|
152
|
+
};
|
|
153
|
+
for (const [name, answer] of Object.entries(right)) {
|
|
154
|
+
expect(await client.call(REQUEST_ANSWER, { id: requestIdOf(name), answer })).toMatchObject({ accepted: true });
|
|
155
|
+
}
|
|
156
|
+
const answered = (await recordsOf(host, RequestAnswered.name)).map((row) => RequestAnswered.schema.parse(row.data));
|
|
157
|
+
expect(answered).toHaveLength(6);
|
|
158
|
+
expect(answered.find((data) => data.id === requestIdOf("text"))?.answer).toEqual({ text: "hello" });
|
|
159
|
+
} finally {
|
|
160
|
+
await client.close();
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("a request is not written with a question or an answer that is not in the format", () => {
|
|
165
|
+
const bad = requestIdOf("bad");
|
|
166
|
+
const created = { id: bad, to: owner, question: "format.bad" };
|
|
167
|
+
expect(RequestCreated.schema.safeParse({ ...created, input: { release: "1.0" } }).success).toBe(false);
|
|
168
|
+
expect(RequestCreated.schema.safeParse({ ...created, input: KINDS.text }).success).toBe(true);
|
|
169
|
+
expect(RequestAnswered.schema.safeParse({ id: bad, answer: "main" }).success).toBe(false);
|
|
170
|
+
expect(RequestAnswered.schema.safeParse({ id: bad, answer: { ids: ["main"] } }).success).toBe(true);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// --- scripts ---------------------------------------------------------------------------------
|
|
174
|
+
|
|
175
|
+
async function scriptIn(home: string, name: string, lines: readonly string[]): Promise<string> {
|
|
176
|
+
await mkdir(home, { recursive: true });
|
|
177
|
+
const file = join(home, name);
|
|
178
|
+
await writeFile(file, lines.join("\n"));
|
|
179
|
+
return file;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
test("each ask.<kind> of a script puts its own question and gives a typed value", async () => {
|
|
183
|
+
const { host, home } = await served();
|
|
184
|
+
const file = await scriptIn(home, "asks.js", [
|
|
185
|
+
"export default workflow({ output: z.json() }, async () => {",
|
|
186
|
+
' const choice = await ask.choice("Pick one", ["a", { id: "b", label: "Bee" }]);',
|
|
187
|
+
' const multi = await ask.multi("Pick some", ["x", "y", "z"]);',
|
|
188
|
+
' const confirm = await ask.confirm("Sure?");',
|
|
189
|
+
' const text = await ask.text("Say something");',
|
|
190
|
+
' const form = await ask.form("Fill it", z.object({ n: z.number(), tag: z.string().default("none") }));',
|
|
191
|
+
" return { choice, multi, confirm, text, form };",
|
|
192
|
+
"});",
|
|
193
|
+
]);
|
|
194
|
+
const client = await connectAsOwner(home);
|
|
195
|
+
try {
|
|
196
|
+
const started = await host.call(workflowStart, { file });
|
|
197
|
+
|
|
198
|
+
const next = async (count: number): Promise<RequestIn> => {
|
|
199
|
+
await eventually(async () => (await recordsOf(host, RequestCreated.name)).length === count, 20_000, `the ask ${String(count)} should be put`);
|
|
200
|
+
const row = (await recordsOf(host, RequestCreated.name))[count - 1];
|
|
201
|
+
return RequestCreated.schema.parse(row?.data);
|
|
202
|
+
};
|
|
203
|
+
type RequestIn = z.output<typeof RequestCreated.schema>;
|
|
204
|
+
|
|
205
|
+
const choice = await next(1);
|
|
206
|
+
expect(choice.input).toMatchObject({ kind: "choice", title: "Pick one", options: [{ id: "a", label: "a" }, { id: "b", label: "Bee" }] });
|
|
207
|
+
await client.call(REQUEST_ANSWER, { id: choice.id, answer: pick("b") });
|
|
208
|
+
|
|
209
|
+
const multi = await next(2);
|
|
210
|
+
expect(multi.input.kind).toBe("multi");
|
|
211
|
+
// An id the question does not have is refused, and the ask stays open.
|
|
212
|
+
await expect(client.call(REQUEST_ANSWER, { id: multi.id, answer: pick("x", "nope") })).rejects.toThrow("not valid");
|
|
213
|
+
await client.call(REQUEST_ANSWER, { id: multi.id, answer: pick("x", "z") });
|
|
214
|
+
|
|
215
|
+
const confirm = await next(3);
|
|
216
|
+
expect(confirm.input).toMatchObject({ kind: "confirm", options: [{ id: "yes" }, { id: "no" }] });
|
|
217
|
+
await client.call(REQUEST_ANSWER, { id: confirm.id, answer: pick("yes") });
|
|
218
|
+
|
|
219
|
+
const text = await next(4);
|
|
220
|
+
expect(text.input.kind).toBe("text");
|
|
221
|
+
await client.call(REQUEST_ANSWER, { id: text.id, answer: { text: "hello" } });
|
|
222
|
+
|
|
223
|
+
const form = await next(5);
|
|
224
|
+
// The form carries the JSON Schema of what the script wants, so a surface can draw it.
|
|
225
|
+
expect(form.input).toMatchObject({ kind: "form", schema: { type: "object", required: ["n"] } });
|
|
226
|
+
await expect(client.call(REQUEST_ANSWER, { id: form.id, answer: { form: { n: "seven" } } })).rejects.toThrow("not valid");
|
|
227
|
+
await client.call(REQUEST_ANSWER, { id: form.id, answer: { form: { n: 7 } } });
|
|
228
|
+
|
|
229
|
+
await eventually(async () => (await recordsOf(host, "workflow.run.finished")).length === 1, 20_000, "the run should finish");
|
|
230
|
+
const finished = z.object({ outcome: z.object({ status: z.literal("done"), result: z.unknown() }) }).parse((await recordsOf(host, "workflow.run.finished"))[0]?.data);
|
|
231
|
+
// The value is the id, the ids, a boolean, a string and what the zod schema parsed (its default included).
|
|
232
|
+
expect(finished.outcome.result).toEqual({
|
|
233
|
+
choice: "b",
|
|
234
|
+
multi: ["x", "z"],
|
|
235
|
+
confirm: true,
|
|
236
|
+
text: "hello",
|
|
237
|
+
form: { n: 7, tag: "none" },
|
|
238
|
+
});
|
|
239
|
+
expect(started.run).toBeDefined();
|
|
240
|
+
} finally {
|
|
241
|
+
await client.close();
|
|
242
|
+
}
|
|
243
|
+
}, 60_000);
|
|
244
|
+
|
|
245
|
+
test("the type checker holds a script to the types of ask.<kind>", async () => {
|
|
246
|
+
const { host, home } = await served();
|
|
247
|
+
const typed = await scriptIn(home, "typed.ts", [
|
|
248
|
+
'const Form = z.object({ n: z.number() });',
|
|
249
|
+
"export default workflow({ output: z.json() }, async () => {",
|
|
250
|
+
' const choice: "a" | "b" = await ask.choice("Pick", ["a", "b"]);',
|
|
251
|
+
' const named: "x" | "y" = await ask.choice("Pick", [{ id: "x", label: "X" }, "y"]);',
|
|
252
|
+
' const multi: Array<"a" | "b"> = await ask.multi("Pick", ["a", "b"]);',
|
|
253
|
+
' const confirm: boolean = await ask.confirm("Sure?", { timeout: "1m", label: "sure" });',
|
|
254
|
+
' const text: string = await ask.text("Say");',
|
|
255
|
+
' const form: { n: number } = await ask.form("Fill", Form);',
|
|
256
|
+
" return { choice, named, multi, confirm, text, form };",
|
|
257
|
+
"});",
|
|
258
|
+
]);
|
|
259
|
+
expect(await host.call(workflowCheck, { file: typed })).toMatchObject({ ok: true, errors: [] });
|
|
260
|
+
|
|
261
|
+
const wrong = await scriptIn(home, "wrong.ts", [
|
|
262
|
+
"export default workflow({ output: z.json() }, async () => {",
|
|
263
|
+
' const choice: "c" = await ask.choice("Pick", ["a", "b"]);',
|
|
264
|
+
' const confirm: string = await ask.confirm("Sure?");',
|
|
265
|
+
" return { choice, confirm };",
|
|
266
|
+
"});",
|
|
267
|
+
]);
|
|
268
|
+
const report = await host.call(workflowCheck, { file: wrong });
|
|
269
|
+
expect(report.ok).toBe(false);
|
|
270
|
+
expect(report.errors.map((error) => error.line)).toEqual([2, 3]);
|
|
271
|
+
|
|
272
|
+
// A question with a JSON Schema written by hand is not accepted.
|
|
273
|
+
const old = await scriptIn(home, "old.ts", [
|
|
274
|
+
"export default workflow({ output: z.json() }, async () => {",
|
|
275
|
+
' return await ask("Go on?", { type: "boolean" });',
|
|
276
|
+
"});",
|
|
277
|
+
]);
|
|
278
|
+
expect((await host.call(workflowCheck, { file: old })).ok).toBe(false);
|
|
279
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// The ceiling of a harness holds for a run's agent calls too: under a ceiling of one, two calls of
|
|
2
|
+
// a parallel are answered one after the other.
|
|
3
|
+
|
|
4
|
+
import { expect, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
import { rmSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
|
|
9
|
+
import type { ActionName } from "@niwa-labs/core";
|
|
10
|
+
import { harnessId, machineId, sessionId } from "@niwa-labs/harness";
|
|
11
|
+
import type { Harness, SessionControl, SessionId } from "@niwa-labs/harness";
|
|
12
|
+
import { createNiwa, sessions, eventually, tempDirSync } from "@niwa-labs/testing";
|
|
13
|
+
import { workflow } from "../src/index.ts";
|
|
14
|
+
|
|
15
|
+
const HARNESS = harnessId("fake-harness");
|
|
16
|
+
|
|
17
|
+
/** A side whose asks wait until the test answers them, and that counts the asks it holds at once. */
|
|
18
|
+
class HeldAsks implements SessionControl {
|
|
19
|
+
readonly waiting: Array<() => void> = [];
|
|
20
|
+
most = 0;
|
|
21
|
+
private counter = 0;
|
|
22
|
+
async start(): Promise<{ session: SessionId }> {
|
|
23
|
+
this.counter += 1;
|
|
24
|
+
return { session: sessionId(HARNESS, `session-${String(this.counter)}`) };
|
|
25
|
+
}
|
|
26
|
+
async resume(): Promise<void> {}
|
|
27
|
+
async send() {
|
|
28
|
+
return { status: "started" as const, turn: 1 };
|
|
29
|
+
}
|
|
30
|
+
async interrupt(): Promise<void> {}
|
|
31
|
+
async ask<T>(id: SessionId): Promise<{ session: SessionId; value: T; at: never }> {
|
|
32
|
+
await new Promise<void>((resolve) => {
|
|
33
|
+
this.waiting.push(resolve);
|
|
34
|
+
this.most = Math.max(this.most, this.waiting.length);
|
|
35
|
+
});
|
|
36
|
+
return { session: id, value: { answer: String(id.native) } as T, at: 0 as never };
|
|
37
|
+
}
|
|
38
|
+
/** Answers the oldest ask that waits. */
|
|
39
|
+
answer(): void {
|
|
40
|
+
this.waiting.shift()?.();
|
|
41
|
+
}
|
|
42
|
+
async *life(): AsyncIterable<never> {
|
|
43
|
+
await new Promise<never>(() => {});
|
|
44
|
+
}
|
|
45
|
+
async state(): Promise<"running" | "idle" | "unknown"> {
|
|
46
|
+
return "idle";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const instance: Harness = {
|
|
51
|
+
id: HARNESS,
|
|
52
|
+
kind: "fake-kind" as never,
|
|
53
|
+
machine: machineId("machine-1"),
|
|
54
|
+
home: "/tmp/fake-home",
|
|
55
|
+
sides: {
|
|
56
|
+
control: { start: true, resume: true, send: true, interrupt: false, ask: true, structuredOutput: true, launch: { model: false, effort: [], modes: [] } },
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
test("two agent calls of a parallel under a ceiling of one are asked one after the other", async () => {
|
|
61
|
+
const control = new HeldAsks();
|
|
62
|
+
const home = tempDirSync("agent-caps");
|
|
63
|
+
const niwa = createNiwa({ home });
|
|
64
|
+
niwa.use(sessions({ harnesses: [{ instance, control }], caps: { [HARNESS]: 1 } }));
|
|
65
|
+
niwa.use(workflow({ harnesses: [HARNESS] }));
|
|
66
|
+
await niwa.serve();
|
|
67
|
+
try {
|
|
68
|
+
const script = join(home, "script.js");
|
|
69
|
+
await Bun.write(
|
|
70
|
+
script,
|
|
71
|
+
[
|
|
72
|
+
"export default workflow({ output: z.json() }, async () => {",
|
|
73
|
+
' return await parallel([() => ask({ harness: "fake-harness" }, "one"), () => ask({ harness: "fake-harness" }, "two")]);',
|
|
74
|
+
"});",
|
|
75
|
+
"",
|
|
76
|
+
].join("\n"),
|
|
77
|
+
);
|
|
78
|
+
await niwa.call("workflow.start" as ActionName, { file: script } as never);
|
|
79
|
+
await eventually(() => control.waiting.length === 1, 10_000, "the first ask should be made");
|
|
80
|
+
await Bun.sleep(300);
|
|
81
|
+
expect(control.waiting).toHaveLength(1);
|
|
82
|
+
control.answer();
|
|
83
|
+
await eventually(() => control.waiting.length === 1, 10_000, "the second ask should be made once the first is answered");
|
|
84
|
+
control.answer();
|
|
85
|
+
await eventually(async () => (await niwa.records.read()).some((row) => row.type === "workflow.run.finished"), 10_000, "the run should finish");
|
|
86
|
+
expect(control.most).toBe(1);
|
|
87
|
+
} finally {
|
|
88
|
+
await niwa.close();
|
|
89
|
+
rmSync(home, { recursive: true, force: true });
|
|
90
|
+
}
|
|
91
|
+
}, 30_000);
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// One-shot questions release the harness process before returning to the script.
|
|
2
|
+
|
|
3
|
+
import { expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { rmSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
import type { ActionName } from "@niwa-labs/core";
|
|
9
|
+
import { harnessId, machineId, sessionId } from "@niwa-labs/harness";
|
|
10
|
+
import type { Harness, SessionControl, SessionId } from "@niwa-labs/harness";
|
|
11
|
+
import { createNiwa, sessions, eventually, tempDirSync } from "@niwa-labs/testing";
|
|
12
|
+
import { workflow } from "../src/index.ts";
|
|
13
|
+
|
|
14
|
+
const HARNESS = harnessId("fake-harness");
|
|
15
|
+
|
|
16
|
+
/** A side that answers every ask, and says which sessions it was asked to close. */
|
|
17
|
+
class Closing implements SessionControl {
|
|
18
|
+
readonly closed: string[] = [];
|
|
19
|
+
private counter = 0;
|
|
20
|
+
async start(): Promise<{ session: SessionId }> {
|
|
21
|
+
this.counter += 1;
|
|
22
|
+
return { session: sessionId(HARNESS, `session-${String(this.counter)}`) };
|
|
23
|
+
}
|
|
24
|
+
async resume(): Promise<void> {}
|
|
25
|
+
async send() {
|
|
26
|
+
return { status: "started" as const, turn: 1 };
|
|
27
|
+
}
|
|
28
|
+
async interrupt(): Promise<void> {}
|
|
29
|
+
async ask<T>(id: SessionId): Promise<{ session: SessionId; value: T; at: never }> {
|
|
30
|
+
return { session: id, value: { answer: `from ${String(id.native)}` } as T, at: 0 as never };
|
|
31
|
+
}
|
|
32
|
+
readonly #gone = new Map<string, () => void>();
|
|
33
|
+
/** A session's signs of life go on until its process is let go. */
|
|
34
|
+
async *life(id: SessionId): AsyncIterable<never> {
|
|
35
|
+
await new Promise<void>((resolve) => this.#gone.set(String(id.native), resolve));
|
|
36
|
+
}
|
|
37
|
+
async state(): Promise<"running" | "idle" | "unknown"> {
|
|
38
|
+
return "idle";
|
|
39
|
+
}
|
|
40
|
+
async close(id: SessionId): Promise<void> {
|
|
41
|
+
this.closed.push(String(id.native));
|
|
42
|
+
this.#gone.get(String(id.native))?.();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const instance: Harness = {
|
|
47
|
+
id: HARNESS,
|
|
48
|
+
kind: "fake-kind" as never,
|
|
49
|
+
machine: machineId("machine-1"),
|
|
50
|
+
home: "/tmp/fake-home",
|
|
51
|
+
sides: {
|
|
52
|
+
control: { start: true, resume: true, send: true, interrupt: true, ask: true, structuredOutput: true, launch: { model: false, effort: [], modes: [] } },
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
test("each one-shot question closes its own session before the run finishes", async () => {
|
|
57
|
+
const control = new Closing();
|
|
58
|
+
const home = tempDirSync("agent-lifetime");
|
|
59
|
+
const niwa = createNiwa({ home });
|
|
60
|
+
niwa.use(sessions({ harnesses: [{ instance, control }] }));
|
|
61
|
+
niwa.use(workflow({ harnesses: [HARNESS] }));
|
|
62
|
+
await niwa.serve();
|
|
63
|
+
try {
|
|
64
|
+
const script = join(home, "script.js");
|
|
65
|
+
await Bun.write(
|
|
66
|
+
script,
|
|
67
|
+
[
|
|
68
|
+
"export default workflow({ output: z.json() }, async () => {",
|
|
69
|
+
' const first = await ask({ harness: "fake-harness" }, "one");',
|
|
70
|
+
' const second = await ask({ harness: "fake-harness" }, "two");',
|
|
71
|
+
" return [first, second];",
|
|
72
|
+
"});",
|
|
73
|
+
"",
|
|
74
|
+
].join("\n"),
|
|
75
|
+
);
|
|
76
|
+
await niwa.call("workflow.start" as ActionName, { file: script } as never);
|
|
77
|
+
const ended = async () => (await niwa.records.read()).filter((row) => row.type === "session.ended").map((row) => row.data as { session: string; reason: string });
|
|
78
|
+
await eventually(async () => (await niwa.records.read()).some((row) => row.type === "workflow.run.finished"), 10_000);
|
|
79
|
+
const records = await niwa.records.read();
|
|
80
|
+
const finishedAt = records.find((row) => row.type === "workflow.run.finished")?.seq ?? 0;
|
|
81
|
+
// Neither completed question leaves a harness running while the script continues.
|
|
82
|
+
expect(records.filter((row) => row.type === "session.ended").every((row) => row.seq < finishedAt)).toBe(true);
|
|
83
|
+
expect((await ended()).map((data) => data.session).sort()).toEqual([`${HARNESS} session-1`, `${HARNESS} session-2`]);
|
|
84
|
+
expect((await ended()).every((data) => data.reason === "workflow question finished")).toBe(true);
|
|
85
|
+
// The stream that closes with the process does not write a second end.
|
|
86
|
+
await Bun.sleep(100);
|
|
87
|
+
expect(await ended()).toHaveLength(2);
|
|
88
|
+
expect(control.closed.sort()).toEqual(["session-1", "session-2"]);
|
|
89
|
+
} finally {
|
|
90
|
+
await niwa.close();
|
|
91
|
+
rmSync(home, { recursive: true, force: true });
|
|
92
|
+
}
|
|
93
|
+
}, 30_000);
|