@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,23 @@
|
|
|
1
|
+
// The script: two branches in parallel, with unlabeled calls.
|
|
2
|
+
//
|
|
3
|
+
// The calls carry no label: the key of a call says which branch it is in. The branches make
|
|
4
|
+
// their calls in the order they are written, so the answer of the second branch is not the
|
|
5
|
+
// answer of the first one.
|
|
6
|
+
|
|
7
|
+
// The owner starts it, and a run holds only what it names: the stub agent.
|
|
8
|
+
export default workflow({ phases: [{ title: "Study" }], uses: ["branches.agent"], output: z.json() }, async () => {
|
|
9
|
+
phase("Study");
|
|
10
|
+
const reports = await parallel([
|
|
11
|
+
async () => {
|
|
12
|
+
const first = await ask({}, "b1");
|
|
13
|
+
const second = await ask({}, "b2");
|
|
14
|
+
return [first, second];
|
|
15
|
+
},
|
|
16
|
+
async () => {
|
|
17
|
+
const first = await ask({}, "c1");
|
|
18
|
+
const second = await ask({}, "c2");
|
|
19
|
+
return [first, second];
|
|
20
|
+
},
|
|
21
|
+
]);
|
|
22
|
+
return reports;
|
|
23
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// The host program for the script bridge: workflow, timers, requests and the
|
|
2
|
+
// stub plugin, on the real clock. The home directory comes from argv[2].
|
|
3
|
+
|
|
4
|
+
import { WfNote, createNiwa, requests, stub, timers } from "@niwa-labs/testing";
|
|
5
|
+
import { workflow } from "../../src/index.ts";
|
|
6
|
+
|
|
7
|
+
if (import.meta.main) {
|
|
8
|
+
const home = process.argv[2];
|
|
9
|
+
if (!home) {
|
|
10
|
+
console.error("usage: bun bridge-host.ts <home>");
|
|
11
|
+
process.exit(2);
|
|
12
|
+
}
|
|
13
|
+
const niwa = createNiwa({ home });
|
|
14
|
+
niwa.use(requests());
|
|
15
|
+
niwa.use(timers([]));
|
|
16
|
+
niwa.use(stub(home));
|
|
17
|
+
niwa.use(workflow({ emits: [WfNote] }));
|
|
18
|
+
await niwa.serve();
|
|
19
|
+
|
|
20
|
+
process.on("SIGTERM", () => {
|
|
21
|
+
void niwa.close().then(() => process.exit(0));
|
|
22
|
+
});
|
|
23
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// The script: two branches that call, and read the clock and a random number.
|
|
2
|
+
|
|
3
|
+
// The owner starts it, and a run holds only what it names.
|
|
4
|
+
export default workflow({ uses: ["stub.hold"], output: z.json() }, async () => {
|
|
5
|
+
return await parallel([
|
|
6
|
+
async () => {
|
|
7
|
+
const held = await niwa.stub.hold({ name: "a" });
|
|
8
|
+
return { held, at: await now() };
|
|
9
|
+
},
|
|
10
|
+
async () => {
|
|
11
|
+
const held = await niwa.stub.hold({ name: "b" });
|
|
12
|
+
return { held, dice: await random() };
|
|
13
|
+
},
|
|
14
|
+
]);
|
|
15
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// The script: a call that makes a thing, then a wait for its end.
|
|
2
|
+
|
|
3
|
+
// The owner starts it, and a run holds only what it names.
|
|
4
|
+
export default workflow({ uses: ["stub.create", "niwa.records.read"], output: z.json() }, async () => {
|
|
5
|
+
const made = await niwa.stub.create({ name: "lockfile" }, { label: "create" });
|
|
6
|
+
const done = await wait("stub.finished", { filter: { id: made.id }, timeout: "1h" });
|
|
7
|
+
return { made, done: "timedOut" in done ? done : done.data };
|
|
8
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// The host program where a cancelled run stops its child.
|
|
2
|
+
//
|
|
3
|
+
// It installs the workflow plugin with a stub agent action. The action counts its calls and
|
|
4
|
+
// waits on a gate, so the run is parked inside a call: the script is waiting for the answer of
|
|
5
|
+
// a call that never comes.
|
|
6
|
+
//
|
|
7
|
+
// The home directory comes from argv[2].
|
|
8
|
+
|
|
9
|
+
import { action } from "@niwa-labs/core";
|
|
10
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
11
|
+
import { counter, gate } from "@niwa-labs/testing";
|
|
12
|
+
import { workflow } from "../../src/index.ts";
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const AgentInput = z.object({
|
|
18
|
+
prompt: z.string(),
|
|
19
|
+
harness: z.string().optional(),
|
|
20
|
+
label: z.string().optional(),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const AgentOutput = z.object({ answer: z.string() });
|
|
24
|
+
|
|
25
|
+
if (import.meta.main) {
|
|
26
|
+
const home = process.argv[2];
|
|
27
|
+
if (!home) {
|
|
28
|
+
console.error("usage: bun cancel-host.ts <home>");
|
|
29
|
+
process.exit(2);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const calls = counter(home, "cancel.agent");
|
|
33
|
+
|
|
34
|
+
const agent = action("cancel.agent", {
|
|
35
|
+
description: "A stubbed agent: it counts its calls and waits on a gate.",
|
|
36
|
+
input: AgentInput,
|
|
37
|
+
output: AgentOutput,
|
|
38
|
+
recovery: "repeat",
|
|
39
|
+
effect: "write",
|
|
40
|
+
async run(input: z.infer<typeof AgentInput>): Promise<z.infer<typeof AgentOutput>> {
|
|
41
|
+
calls.hit();
|
|
42
|
+
// The call never finishes on its own: the test opens the gate or the run is cancelled.
|
|
43
|
+
await gate(home, "cancel.gate").wait();
|
|
44
|
+
return { answer: `answered: ${input.prompt}` };
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const { createNiwa } = await import("@niwa-labs/testing");
|
|
49
|
+
const niwa = createNiwa({ home });
|
|
50
|
+
// The stub is an action of the host too, so a script may name it in `meta.uses`: the owner who starts the run holds a grant.
|
|
51
|
+
niwa.use(plugin("cancel-agent", (api) => api.action(agent)));
|
|
52
|
+
niwa.use(workflow({ agentAction: agent }));
|
|
53
|
+
await niwa.serve();
|
|
54
|
+
|
|
55
|
+
process.on("SIGTERM", () => {
|
|
56
|
+
void niwa.close().then(() => process.exit(0));
|
|
57
|
+
});
|
|
58
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The script: a long call, and a second call that must never happen.
|
|
2
|
+
//
|
|
3
|
+
// The first call is long: the stubbed agent waits on a gate that the test never opens. The run
|
|
4
|
+
// is cancelled while that call is in flight, so the second call must never be made.
|
|
5
|
+
|
|
6
|
+
// The owner starts it, and a run holds only what it names: the stub agent.
|
|
7
|
+
export default workflow({ phases: [{ title: "Work" }], uses: ["cancel.agent"], output: z.json() }, async () => {
|
|
8
|
+
phase("Work");
|
|
9
|
+
const first = await ask({ label: "work:first" }, "This call takes a long time.");
|
|
10
|
+
const second = await ask({ label: "work:second" }, "This call must never happen.");
|
|
11
|
+
return { first, second };
|
|
12
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// The host program for workflow resume.
|
|
2
|
+
//
|
|
3
|
+
// It installs the workflow plugin with a stub agent action. The action counts its calls, so the
|
|
4
|
+
// test can tell a repeated call from a reused one, and it waits on a gate: the gate of the
|
|
5
|
+
// harness named in the input, so one branch can finish while the other is still in flight.
|
|
6
|
+
//
|
|
7
|
+
// The home directory comes from argv[2].
|
|
8
|
+
|
|
9
|
+
import { action } from "@niwa-labs/core";
|
|
10
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
11
|
+
import { gate, counter } from "@niwa-labs/testing";
|
|
12
|
+
import { workflow } from "../../src/index.ts";
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const AgentInput = z.object({
|
|
18
|
+
prompt: z.string(),
|
|
19
|
+
harness: z.string().optional(),
|
|
20
|
+
label: z.string().optional(),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const AgentOutput = z.object({ answer: z.string() });
|
|
24
|
+
|
|
25
|
+
if (import.meta.main) {
|
|
26
|
+
const home = process.argv[2];
|
|
27
|
+
if (!home) {
|
|
28
|
+
console.error("usage: bun crash-host.ts <home>");
|
|
29
|
+
process.exit(2);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const calls = counter(home, "crash.agent");
|
|
33
|
+
|
|
34
|
+
const agent = action("crash.agent", {
|
|
35
|
+
description: "A stubbed agent: it counts its calls and waits on the gate of its harness.",
|
|
36
|
+
input: AgentInput,
|
|
37
|
+
output: AgentOutput,
|
|
38
|
+
recovery: "repeat",
|
|
39
|
+
effect: "write",
|
|
40
|
+
async run(input: z.infer<typeof AgentInput>): Promise<z.infer<typeof AgentOutput>> {
|
|
41
|
+
calls.hit();
|
|
42
|
+
// The harness of the call: it names the gate the call waits on.
|
|
43
|
+
const name = input.harness ?? "default";
|
|
44
|
+
await gate(home, `crash.gate.${name}`).wait();
|
|
45
|
+
// The call is counted when it finished, so the counter says what the journal holds.
|
|
46
|
+
counter(home, `crash.${input.harness ?? "default"}`).hit();
|
|
47
|
+
return { answer: `answered: ${input.prompt}` };
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const { createNiwa } = await import("@niwa-labs/testing");
|
|
52
|
+
const niwa = createNiwa({ home });
|
|
53
|
+
// The stub is an action of the host too, so a script may name it in `meta.uses`: the owner who starts the run holds a grant.
|
|
54
|
+
niwa.use(plugin("crash-agent", (api) => api.action(agent)));
|
|
55
|
+
niwa.use(workflow({ agentAction: agent }));
|
|
56
|
+
await niwa.serve();
|
|
57
|
+
|
|
58
|
+
process.on("SIGTERM", () => {
|
|
59
|
+
void niwa.close().then(() => process.exit(0));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// The script: two agent calls in parallel.
|
|
2
|
+
//
|
|
3
|
+
// The calls are in two harnesses, as the check says, and they have explicit keys, so the order
|
|
4
|
+
// of the branches does not decide which call is which.
|
|
5
|
+
|
|
6
|
+
// The owner starts it, and a run holds only what it names: the stub agent.
|
|
7
|
+
export default workflow({ phases: [{ title: "Study" }], uses: ["crash.agent"], output: z.json() }, async () => {
|
|
8
|
+
phase("Study");
|
|
9
|
+
const reports = await parallel([
|
|
10
|
+
() => ask({ harness: "crash.one", label: "study:one" }, "Study the core."),
|
|
11
|
+
() => ask({ harness: "crash.two", label: "study:two" }, "Study the state."),
|
|
12
|
+
]);
|
|
13
|
+
return { reports };
|
|
14
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Record types a plugin of a test host declares and a script imports: a script names them by reference.
|
|
2
|
+
|
|
3
|
+
import { queryOf, record } from "@niwa-labs/core";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
|
|
6
|
+
export const Ping = record("ping.sent", z.object({ id: z.string(), size: z.number(), to: z.object({ id: z.string() }) }));
|
|
7
|
+
export const Pong = record("pong.sent", z.object({ id: z.string(), ok: z.boolean() }));
|
|
8
|
+
/** The same name at a version the host does not write. */
|
|
9
|
+
export const PingAhead = record("ping.sent", z.object({ id: z.string() }), { version: 2 });
|
|
10
|
+
/** A type no plugin of the host declares. */
|
|
11
|
+
export const Unheard = record("never.sent", z.object({ id: z.string() }));
|
|
12
|
+
|
|
13
|
+
/** The pings sent to one address: a query builder, as a plugin's records entry gives one. */
|
|
14
|
+
export const pingsTo = (id: string) => queryOf(Ping, { "to.id": id });
|
|
15
|
+
/** The pings of one id at a version the host does not write, made by a function declaration. */
|
|
16
|
+
export function aheadOf(id: string) {
|
|
17
|
+
return queryOf(PingAhead, { id });
|
|
18
|
+
}
|
|
19
|
+
/** One record of a type no plugin of the host declares, as a builder gives it. */
|
|
20
|
+
export const unheardOf = (id: string) => queryOf(Unheard, { id });
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// The host program for workflow revision.
|
|
2
|
+
//
|
|
3
|
+
// It installs the workflow plugin with a stub agent action. The action counts its calls and
|
|
4
|
+
// fails for the prompt that says "fail", so the run pauses with a request.
|
|
5
|
+
//
|
|
6
|
+
// The home directory comes from argv[2].
|
|
7
|
+
|
|
8
|
+
import { action } from "@niwa-labs/core";
|
|
9
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
10
|
+
import { counter, requests } from "@niwa-labs/testing";
|
|
11
|
+
import { workflow } from "../../src/index.ts";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
const AgentInput = z.object({
|
|
17
|
+
prompt: z.string(),
|
|
18
|
+
harness: z.string().optional(),
|
|
19
|
+
label: z.string().optional(),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const AgentOutput = z.object({ answer: z.string() });
|
|
23
|
+
|
|
24
|
+
if (import.meta.main) {
|
|
25
|
+
const home = process.argv[2];
|
|
26
|
+
if (!home) {
|
|
27
|
+
console.error("usage: bun revision-host.ts <home>");
|
|
28
|
+
process.exit(2);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const calls = counter(home, "revision.agent");
|
|
32
|
+
|
|
33
|
+
const agent = action("revision.agent", {
|
|
34
|
+
description: "A stubbed agent: it counts its calls and fails for the prompt that says so.",
|
|
35
|
+
input: AgentInput,
|
|
36
|
+
output: AgentOutput,
|
|
37
|
+
recovery: "repeat",
|
|
38
|
+
effect: "write",
|
|
39
|
+
async run(input: z.infer<typeof AgentInput>): Promise<z.infer<typeof AgentOutput>> {
|
|
40
|
+
calls.hit();
|
|
41
|
+
if (input.prompt.includes("fail")) throw new Error("the agent could not answer");
|
|
42
|
+
return { answer: `answered: ${input.prompt}` };
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const { createNiwa } = await import("@niwa-labs/testing");
|
|
47
|
+
const niwa = createNiwa({ home });
|
|
48
|
+
niwa.use(requests());
|
|
49
|
+
// The stub is an action of the host too, so a script may name it in `meta.uses`: the owner who starts the run holds a grant.
|
|
50
|
+
niwa.use(plugin("revision-agent", (api) => api.action(agent)));
|
|
51
|
+
niwa.use(workflow({ agentAction: agent }));
|
|
52
|
+
await niwa.serve();
|
|
53
|
+
|
|
54
|
+
process.on("SIGTERM", () => {
|
|
55
|
+
void niwa.close().then(() => process.exit(0));
|
|
56
|
+
});
|
|
57
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The script: one call that finishes, one that fails.
|
|
2
|
+
//
|
|
3
|
+
// The keys are explicit, so the revision knows which call is which.
|
|
4
|
+
|
|
5
|
+
// The owner starts it, and a run holds only what it names: the stub agent.
|
|
6
|
+
export default workflow({ phases: [{ title: "Work" }], uses: ["revision.agent"], output: z.json() }, async () => {
|
|
7
|
+
phase("Work");
|
|
8
|
+
const first = await ask({ label: "work:first" }, "Study the core.");
|
|
9
|
+
const second = await ask({ label: "work:second" }, "This one will fail.");
|
|
10
|
+
return { first, second };
|
|
11
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// The host program where a run may not be started twice at once.
|
|
2
|
+
//
|
|
3
|
+
// It installs the workflow plugin with a stub agent action. The action counts its calls and
|
|
4
|
+
// waits on a gate, so the run is parked inside a call: the run is still going, and the owner
|
|
5
|
+
// may ask for it to be resumed.
|
|
6
|
+
//
|
|
7
|
+
// The home directory comes from argv[2].
|
|
8
|
+
|
|
9
|
+
import { action } from "@niwa-labs/core";
|
|
10
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
11
|
+
import { counter, gate, requests } from "@niwa-labs/testing";
|
|
12
|
+
import { workflow } from "../../src/index.ts";
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const AgentInput = z.object({
|
|
18
|
+
prompt: z.string(),
|
|
19
|
+
harness: z.string().optional(),
|
|
20
|
+
label: z.string().optional(),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const AgentOutput = z.object({ answer: z.string() });
|
|
24
|
+
|
|
25
|
+
if (import.meta.main) {
|
|
26
|
+
const home = process.argv[2];
|
|
27
|
+
if (!home) {
|
|
28
|
+
console.error("usage: bun running-host.ts <home>");
|
|
29
|
+
process.exit(2);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const calls = counter(home, "running.agent");
|
|
33
|
+
|
|
34
|
+
const agent = action("running.agent", {
|
|
35
|
+
description: "A stubbed agent: it counts its calls, fails for a prompt that says so, and waits on a gate.",
|
|
36
|
+
input: AgentInput,
|
|
37
|
+
output: AgentOutput,
|
|
38
|
+
recovery: "repeat",
|
|
39
|
+
effect: "write",
|
|
40
|
+
async run(input: z.infer<typeof AgentInput>): Promise<z.infer<typeof AgentOutput>> {
|
|
41
|
+
calls.hit();
|
|
42
|
+
// The prompt says so: the call fails, and the run pauses on it.
|
|
43
|
+
if (input.prompt.includes("fail")) throw new Error("the agent could not answer");
|
|
44
|
+
// The call never finishes on its own: the test opens the gate or the run is ended.
|
|
45
|
+
await gate(home, "running.gate").wait();
|
|
46
|
+
return { answer: `answered: ${input.prompt}` };
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const { createNiwa } = await import("@niwa-labs/testing");
|
|
51
|
+
const niwa = createNiwa({ home });
|
|
52
|
+
niwa.use(requests());
|
|
53
|
+
// The stub is an action of the host too, so a script may name it in `meta.uses`: the owner who starts the run holds a grant.
|
|
54
|
+
niwa.use(plugin("running-agent", (api) => api.action(agent)));
|
|
55
|
+
niwa.use(workflow({ agentAction: agent }));
|
|
56
|
+
await niwa.serve();
|
|
57
|
+
|
|
58
|
+
process.on("SIGTERM", () => {
|
|
59
|
+
void niwa.close().then(() => process.exit(0));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The script: one call that waits, and a second one.
|
|
2
|
+
//
|
|
3
|
+
// The first call is parked on a gate of the host, so the run is still going when the owner asks
|
|
4
|
+
// for it to be resumed.
|
|
5
|
+
|
|
6
|
+
// The owner starts it, and a run holds only what it names: the stub agent.
|
|
7
|
+
export default workflow({ phases: [{ title: "Work" }], uses: ["running.agent"], output: z.json() }, async () => {
|
|
8
|
+
phase("Work");
|
|
9
|
+
const first = await ask({ label: "work:first" }, "This call waits.");
|
|
10
|
+
const second = await ask({ label: "work:second" }, "This call comes after.");
|
|
11
|
+
return { first, second };
|
|
12
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "n": 2 }
|
package/test/git-repo.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// A real git repository in a temporary directory, for the tests of the git module. Git runs with no
|
|
2
|
+
// global or system configuration, so the owner's hooks, signing and default branch do not reach it;
|
|
3
|
+
// the host processes the tests spawn inherit the same environment.
|
|
4
|
+
|
|
5
|
+
import { mkdirSync, realpathSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
import { tempDir } from "@niwa-labs/testing";
|
|
9
|
+
|
|
10
|
+
process.env.GIT_CONFIG_GLOBAL = "/dev/null";
|
|
11
|
+
process.env.GIT_CONFIG_NOSYSTEM = "1";
|
|
12
|
+
process.env.GIT_AUTHOR_NAME = "niwa test";
|
|
13
|
+
process.env.GIT_AUTHOR_EMAIL = "test@niwa.invalid";
|
|
14
|
+
process.env.GIT_COMMITTER_NAME = "niwa test";
|
|
15
|
+
process.env.GIT_COMMITTER_EMAIL = "test@niwa.invalid";
|
|
16
|
+
|
|
17
|
+
/** Run git in a directory; its output, trimmed. */
|
|
18
|
+
export function gitIn(dir: string, ...args: string[]): string {
|
|
19
|
+
const result = Bun.spawnSync(["git", "-C", dir, ...args], { stdout: "pipe", stderr: "pipe" });
|
|
20
|
+
if (result.exitCode !== 0) throw new Error(`git ${args.join(" ")} failed: ${result.stderr.toString()}`);
|
|
21
|
+
return result.stdout.toString().trim();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A repository on `main` with one commit, a `.gitignore` and a `.worktreeinclude`, and four untracked
|
|
26
|
+
* files: `.env` and `node_modules/pkg/index.js` (ignored and named by the include), `secret.local`
|
|
27
|
+
* (ignored, not named) and `plain.txt` (named, not ignored).
|
|
28
|
+
*/
|
|
29
|
+
export async function makeRepo(): Promise<string> {
|
|
30
|
+
const repo = realpathSync(await tempDir("git-repo"));
|
|
31
|
+
gitIn(repo, "init", "-q", "-b", "main");
|
|
32
|
+
writeFileSync(join(repo, ".gitignore"), ".env\nsecret.local\nnode_modules/\n");
|
|
33
|
+
writeFileSync(join(repo, ".worktreeinclude"), ".env\nnode_modules/\nplain.txt\n");
|
|
34
|
+
mkdirSync(join(repo, "src"));
|
|
35
|
+
writeFileSync(join(repo, "src", "main.ts"), "export {};\n");
|
|
36
|
+
writeFileSync(join(repo, "README.md"), "a repository\n");
|
|
37
|
+
gitIn(repo, "add", ".");
|
|
38
|
+
gitIn(repo, "commit", "-q", "-m", "start");
|
|
39
|
+
writeFileSync(join(repo, ".env"), "TOKEN=1\n");
|
|
40
|
+
writeFileSync(join(repo, "secret.local"), "secret\n");
|
|
41
|
+
mkdirSync(join(repo, "node_modules", "pkg"), { recursive: true });
|
|
42
|
+
writeFileSync(join(repo, "node_modules", "pkg", "index.js"), "module.exports = 1;\n");
|
|
43
|
+
writeFileSync(join(repo, "plain.txt"), "plain\n");
|
|
44
|
+
return repo;
|
|
45
|
+
}
|