@niwa-labs/flow-code 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 +26 -2
- package/package.json +16 -4
- package/src/build.ts +162 -0
- package/src/index.ts +13 -0
- package/src/loops.ts +215 -0
- package/src/review.ts +141 -0
- package/src/worktree.ts +177 -0
- package/test/build.test.ts +236 -0
- package/test/code.test.ts +395 -0
- package/test/example.test.ts +86 -0
- package/test/support.ts +187 -0
- package/tsconfig.json +6 -0
package/test/support.ts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// What the flow-code tests share: a real git repository, a host with the git module, a proc.run
|
|
2
|
+
// that runs the command where it is asked to, stub agents that work in the directory they are
|
|
3
|
+
// started in, and a way to run a script that imports the helpers by name.
|
|
4
|
+
|
|
5
|
+
import { realpathSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { mkdir, symlink, writeFile } from "node:fs/promises";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
|
|
11
|
+
import { procRun } from "@niwa-labs/proc/records";
|
|
12
|
+
import { connect, createNiwa, eventually, gate, git, ownerTokenOf, plugin, requests, stubAction, tempDir, tempHome, timers, workflow } from "@niwa-labs/testing";
|
|
13
|
+
import type { Host } from "@niwa-labs/testing";
|
|
14
|
+
|
|
15
|
+
process.env.GIT_CONFIG_GLOBAL = "/dev/null";
|
|
16
|
+
process.env.GIT_CONFIG_NOSYSTEM = "1";
|
|
17
|
+
process.env.GIT_AUTHOR_NAME = "niwa test";
|
|
18
|
+
process.env.GIT_AUTHOR_EMAIL = "test@niwa.invalid";
|
|
19
|
+
process.env.GIT_COMMITTER_NAME = "niwa test";
|
|
20
|
+
process.env.GIT_COMMITTER_EMAIL = "test@niwa.invalid";
|
|
21
|
+
|
|
22
|
+
/** Run git in a directory; its output, trimmed. */
|
|
23
|
+
export function gitIn(dir: string, ...args: string[]): string {
|
|
24
|
+
const result = Bun.spawnSync(["git", "-C", dir, ...args], { stdout: "pipe", stderr: "pipe" });
|
|
25
|
+
if (result.exitCode !== 0) throw new Error(`git ${args.join(" ")} failed: ${result.stderr.toString()}`);
|
|
26
|
+
return result.stdout.toString().trim();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A repository on `main` with one commit. */
|
|
30
|
+
export async function makeRepo(): Promise<string> {
|
|
31
|
+
const repo = realpathSync(await tempDir("flow-repo"));
|
|
32
|
+
gitIn(repo, "init", "-q", "-b", "main");
|
|
33
|
+
writeFileSync(join(repo, "README.md"), "a repository\n");
|
|
34
|
+
gitIn(repo, "add", ".");
|
|
35
|
+
gitIn(repo, "commit", "-q", "-m", "start");
|
|
36
|
+
return repo;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Write a file in a checkout and commit it: the new commit. */
|
|
40
|
+
export function commitFile(dir: string, file: string, text: string): string {
|
|
41
|
+
writeFileSync(join(dir, file), text);
|
|
42
|
+
gitIn(dir, "add", file);
|
|
43
|
+
gitIn(dir, "commit", "-q", "-m", `add ${file}`);
|
|
44
|
+
return gitIn(dir, "rev-parse", "HEAD");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** What an agent call reaches a stub with. */
|
|
48
|
+
export interface AgentCall {
|
|
49
|
+
prompt: string;
|
|
50
|
+
harness?: string;
|
|
51
|
+
cwd?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type Reply = (call: AgentCall) => unknown;
|
|
55
|
+
|
|
56
|
+
export interface CodeHost {
|
|
57
|
+
host: Host;
|
|
58
|
+
home: string;
|
|
59
|
+
repo: string;
|
|
60
|
+
calls: Record<string, AgentCall[]>;
|
|
61
|
+
/** Run a script once and answer how it ended. */
|
|
62
|
+
run(source: string, args?: unknown): Promise<{ state: string; outcome?: { status: string; result?: unknown }; reason?: string }>;
|
|
63
|
+
start(source: string, args?: unknown): Promise<string>;
|
|
64
|
+
/** Answer the owner's open question with an option id. */
|
|
65
|
+
answer(id: string): Promise<void>;
|
|
66
|
+
/** Let the agent of a harness listed in `gated` go on: its calls wait until then. */
|
|
67
|
+
release(harness: string): void;
|
|
68
|
+
close(): Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const PACKAGES = join(import.meta.dir, "..", "..");
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* `proc.run` as a test needs it: the command runs in its cwd, on the input the real action takes, and
|
|
75
|
+
* is stopped when its time is up. It is boxed to its cwd as far as git goes: a linked worktree keeps
|
|
76
|
+
* its git directory in the repository it belongs to, outside the box, so `git` cannot read it there.
|
|
77
|
+
*/
|
|
78
|
+
function fakeProc(home: string) {
|
|
79
|
+
return plugin("fake-proc", (api) =>
|
|
80
|
+
api.action(
|
|
81
|
+
stubAction("proc.run", {
|
|
82
|
+
home,
|
|
83
|
+
// Held to the input of the real action: a helper that calls it another way fails here as it would there.
|
|
84
|
+
input: procRun.input,
|
|
85
|
+
output: z.any(),
|
|
86
|
+
recovery: "repeat",
|
|
87
|
+
result: ({ cmd, cwd, timeout }) => {
|
|
88
|
+
if (cmd[0] === "git") {
|
|
89
|
+
const tail = "fatal: not a git repository: the git directory of this worktree is outside the box\n";
|
|
90
|
+
return { outcome: "exited", exit: 128, durationMs: 1, tail, truncated: false, output: { digest: `sha256:${"0".repeat(64)}`, size: tail.length, type: "text/plain" }, box: { sandbox: "none", write: [cwd], net: [] } };
|
|
91
|
+
}
|
|
92
|
+
const ran = Bun.spawnSync(cmd, { cwd, stdout: "pipe", stderr: "pipe", timeout });
|
|
93
|
+
const tail = (ran.stdout.toString() + ran.stderr.toString()).slice(-4096);
|
|
94
|
+
return {
|
|
95
|
+
outcome: ran.exitedDueToTimeout === true ? "timed-out" : "exited",
|
|
96
|
+
exit: ran.exitedDueToTimeout === true ? null : ran.exitCode,
|
|
97
|
+
durationMs: 1,
|
|
98
|
+
tail,
|
|
99
|
+
truncated: false,
|
|
100
|
+
output: { digest: `sha256:${"0".repeat(64)}`, size: tail.length, type: "text/plain" },
|
|
101
|
+
box: { sandbox: "none", write: [cwd], net: [] },
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
}),
|
|
105
|
+
),
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A host with git, a proc.run, the workflow module and an agent per harness of `agents`. */
|
|
110
|
+
export async function codeHost(agents: Record<string, Reply>, packages: readonly string[] = ["flow", "flow-code"], gated: readonly string[] = []): Promise<CodeHost> {
|
|
111
|
+
const temp = await tempHome();
|
|
112
|
+
const home = realpathSync(temp.path);
|
|
113
|
+
const repo = await makeRepo();
|
|
114
|
+
const calls: Record<string, AgentCall[]> = {};
|
|
115
|
+
const actions = Object.entries(agents).map(([harness, reply]) => {
|
|
116
|
+
calls[harness] = [];
|
|
117
|
+
const seen = calls[harness] as AgentCall[];
|
|
118
|
+
return [
|
|
119
|
+
harness,
|
|
120
|
+
stubAction(`session.start.${harness}`, {
|
|
121
|
+
home,
|
|
122
|
+
input: z.looseObject({ prompt: z.string(), harness: z.string().optional(), cwd: z.string().optional() }),
|
|
123
|
+
output: z.any(),
|
|
124
|
+
recovery: "repeat",
|
|
125
|
+
...(gated.includes(harness) ? { gate: `agent-${harness}` } : {}),
|
|
126
|
+
result: (input) => {
|
|
127
|
+
const call: AgentCall = { prompt: input.prompt, ...(input.harness === undefined ? {} : { harness: input.harness }), ...(input.cwd === undefined ? {} : { cwd: input.cwd }) };
|
|
128
|
+
seen.push(call);
|
|
129
|
+
return reply(call);
|
|
130
|
+
},
|
|
131
|
+
}),
|
|
132
|
+
] as const;
|
|
133
|
+
});
|
|
134
|
+
const host = createNiwa({ home });
|
|
135
|
+
host.use(
|
|
136
|
+
requests(),
|
|
137
|
+
timers([]),
|
|
138
|
+
git({ cleanup: false }),
|
|
139
|
+
fakeProc(home),
|
|
140
|
+
plugin("stub-agents", (api) => {
|
|
141
|
+
for (const [, one] of actions) api.action(one);
|
|
142
|
+
}),
|
|
143
|
+
workflow({ agents: Object.fromEntries(actions) }),
|
|
144
|
+
);
|
|
145
|
+
await host.serve();
|
|
146
|
+
const src = join(home, "src");
|
|
147
|
+
await mkdir(join(src, "node_modules", "@niwa-labs"), { recursive: true });
|
|
148
|
+
for (const name of packages) await symlink(realpathSync(join(PACKAGES, name)), join(src, "node_modules", "@niwa-labs", name), "dir");
|
|
149
|
+
const owner = await connect({ home, id: "owner:flow-code", token: ownerTokenOf(home) });
|
|
150
|
+
let scripts = 0;
|
|
151
|
+
const start = async (source: string, args?: unknown): Promise<string> => {
|
|
152
|
+
scripts += 1;
|
|
153
|
+
const file = join(src, `script-${String(scripts)}.ts`);
|
|
154
|
+
await writeFile(file, source.replaceAll("$REPO", repo));
|
|
155
|
+
return ((await host.call("workflow.start" as never, { file, ...(args === undefined ? {} : { args }) } as never)) as { run: string }).run;
|
|
156
|
+
};
|
|
157
|
+
return {
|
|
158
|
+
host,
|
|
159
|
+
home,
|
|
160
|
+
repo,
|
|
161
|
+
calls,
|
|
162
|
+
start,
|
|
163
|
+
run: async (source, args) => (await host.call("workflow.wait" as never, { run: await start(source, args), timeoutMs: 30_000 } as never)) as never,
|
|
164
|
+
release: (harness) => gate(home, `agent-${harness}`).open(),
|
|
165
|
+
answer: async (choice) => {
|
|
166
|
+
let id: string | undefined;
|
|
167
|
+
await eventually(async () => {
|
|
168
|
+
const open = (await owner.call("request.open" as never, {} as never)) as { requests?: Array<{ id: string }>; items?: Array<{ id: string }> };
|
|
169
|
+
id = (open.requests ?? open.items ?? [])[0]?.id;
|
|
170
|
+
return id !== undefined;
|
|
171
|
+
}, 15_000, "the owner should be asked");
|
|
172
|
+
await owner.call("request.answer" as never, { id, answer: { ids: [choice] } } as never);
|
|
173
|
+
},
|
|
174
|
+
close: async () => {
|
|
175
|
+
for (const harness of gated) gate(home, `agent-${harness}`).open();
|
|
176
|
+
await owner.close?.();
|
|
177
|
+
await host.close().catch(() => {});
|
|
178
|
+
await temp.cleanup();
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** The result of a run that finished, or a failure that says how it ended. */
|
|
184
|
+
export function resultOf(ended: { state: string; outcome?: { status: string; result?: unknown } }): unknown {
|
|
185
|
+
if (ended.state !== "finished" || ended.outcome?.status !== "done") throw new Error(`the run did not finish: ${JSON.stringify(ended).slice(0, 2000)}`);
|
|
186
|
+
return ended.outcome.result;
|
|
187
|
+
}
|