@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,413 @@
|
|
|
1
|
+
// The git module's worktrees, on real git repositories in temporary directories: a worktree is made
|
|
2
|
+
// from the current branch with the ignored files `.worktreeinclude` names, the list flags what is odd,
|
|
3
|
+
// a remove refuses a dirty, unmerged or occupied worktree unless forced, `session.start({ worktree })`
|
|
4
|
+
// runs the session in one, `ask({ worktree })` removes a clean one and keeps a changed one, and the
|
|
5
|
+
// built-in cleanup removes the old clean ones only. The replay of a create cut off by a crash is
|
|
6
|
+
// tested in the git package.
|
|
7
|
+
|
|
8
|
+
import { afterEach, expect, test } from "bun:test";
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
|
|
12
|
+
import { seq } from "@niwa-labs/core";
|
|
13
|
+
import type { ActionName, StoredRecord } from "@niwa-labs/core";
|
|
14
|
+
import { gitHeadObserve, gitWorktreeCreate, gitWorktreeGet, gitWorktreeList, gitWorktreeRemove } from "@niwa-labs/git/records";
|
|
15
|
+
import { NoOptions, harnessId, kindOptions, machineId, sessionId } from "@niwa-labs/harness";
|
|
16
|
+
import type { AskAnswer, AskOptions, Harness, SessionControl, SessionId, StartOptions, StartedSession } from "@niwa-labs/harness";
|
|
17
|
+
import { SessionTurnEnded, sessionStart } from "@niwa-labs/sessions/records";
|
|
18
|
+
import { createNiwa, CLEANUP_TIMER, WorktreeRefusal, git, registeredWorktree, sessions, timer, timerList, timers, eventually, tempHome } from "@niwa-labs/testing";
|
|
19
|
+
import type { Host } from "@niwa-labs/testing";
|
|
20
|
+
import { workflow, workflowInstallationList } from "../src/index.ts";
|
|
21
|
+
|
|
22
|
+
import { gitIn, makeRepo } from "./git-repo.ts";
|
|
23
|
+
|
|
24
|
+
const hosts: Host[] = [];
|
|
25
|
+
const homes: Array<{ cleanup(): Promise<void> }> = [];
|
|
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
|
+
const HARNESS = harnessId("claude-sonnet");
|
|
33
|
+
|
|
34
|
+
/** A control side that keeps what each session was started with and keeps it alive; a prompt with "change" writes a file where the session runs. */
|
|
35
|
+
class WorkingControl implements SessionControl {
|
|
36
|
+
readonly starts: StartOptions[] = [];
|
|
37
|
+
private readonly cwds = new Map<string, string | undefined>();
|
|
38
|
+
|
|
39
|
+
async start(options: StartOptions): Promise<StartedSession> {
|
|
40
|
+
this.starts.push(options);
|
|
41
|
+
const id = sessionId(HARNESS, `session-${String(this.starts.length)}`);
|
|
42
|
+
this.cwds.set(String(id), options.cwd);
|
|
43
|
+
return { session: id };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async resume(): Promise<void> {
|
|
47
|
+
throw new Error("this session cannot be resumed");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async send() {
|
|
51
|
+
return { status: "started" as const, turn: 1 };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async interrupt(): Promise<void> {}
|
|
55
|
+
|
|
56
|
+
async ask<T>(id: SessionId, options: AskOptions<T>): Promise<AskAnswer<T>> {
|
|
57
|
+
const cwd = this.cwds.get(String(id));
|
|
58
|
+
if (JSON.stringify(options.prompt).includes("change") && cwd !== undefined) writeFileSync(join(cwd, "made-by-agent.txt"), "work\n");
|
|
59
|
+
return { session: id, value: { answer: "done" } as T, at: 0 as never };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The sessions live until the host stops: their stream of life ends only then. */
|
|
63
|
+
private stop = (): void => {};
|
|
64
|
+
private readonly stopped = new Promise<void>((resolve) => (this.stop = resolve));
|
|
65
|
+
|
|
66
|
+
async *life(): AsyncIterable<never> {
|
|
67
|
+
await this.stopped;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async dispose(): Promise<void> {
|
|
71
|
+
this.stop();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async state(): Promise<"unknown"> {
|
|
75
|
+
return "unknown";
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const instance: Harness = {
|
|
80
|
+
id: HARNESS,
|
|
81
|
+
kind: "claude-code" as never,
|
|
82
|
+
machine: machineId("machine-1"),
|
|
83
|
+
home: "/tmp/fake-home",
|
|
84
|
+
sides: {
|
|
85
|
+
control: {
|
|
86
|
+
start: true,
|
|
87
|
+
resume: false,
|
|
88
|
+
send: true,
|
|
89
|
+
interrupt: true,
|
|
90
|
+
ask: true,
|
|
91
|
+
structuredOutput: true,
|
|
92
|
+
launch: { model: false, effort: [], modes: [] },
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** A host with the git module, and the sessions and workflow modules over the working control when asked. */
|
|
98
|
+
async function served(withSessions = false): Promise<{ host: Host; home: string; control: WorkingControl }> {
|
|
99
|
+
const home = await tempHome();
|
|
100
|
+
homes.push(home);
|
|
101
|
+
const control = new WorkingControl();
|
|
102
|
+
const host = createNiwa({ home: home.path });
|
|
103
|
+
// The cleanup is its own test: these hosts have no timers or workflow module to run it.
|
|
104
|
+
host.use(git({ cleanup: false }));
|
|
105
|
+
if (withSessions) {
|
|
106
|
+
host.use(sessions({
|
|
107
|
+
harnesses: [{ instance, control, options: kindOptions("claude", NoOptions) }],
|
|
108
|
+
worktreeFor: registeredWorktree,
|
|
109
|
+
}));
|
|
110
|
+
host.use(workflow({ harnesses: [HARNESS] }));
|
|
111
|
+
}
|
|
112
|
+
await host.serve();
|
|
113
|
+
hosts.push(host);
|
|
114
|
+
return { host, home: realpathSync(home.path), control };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const ofType = async (host: Host, type: string): Promise<StoredRecord[]> =>
|
|
118
|
+
(await host.records.read()).filter((row) => row.type === type);
|
|
119
|
+
|
|
120
|
+
/** The refusal a call ended with. */
|
|
121
|
+
async function refusalOf(call: Promise<unknown>): Promise<string> {
|
|
122
|
+
try {
|
|
123
|
+
await call;
|
|
124
|
+
} catch (error) {
|
|
125
|
+
expect(error).toBeInstanceOf(WorktreeRefusal);
|
|
126
|
+
return (error as WorktreeRefusal).code;
|
|
127
|
+
}
|
|
128
|
+
throw new Error("the call was not refused");
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
test("a worktree is made from the current branch, on a branch named like its id, with the ignored files .worktreeinclude names", async () => {
|
|
132
|
+
const repo = await makeRepo();
|
|
133
|
+
gitIn(repo, "switch", "-q", "-c", "feature");
|
|
134
|
+
writeFileSync(join(repo, "feature.txt"), "feature\n");
|
|
135
|
+
gitIn(repo, "add", "feature.txt");
|
|
136
|
+
gitIn(repo, "commit", "-q", "-m", "feature");
|
|
137
|
+
const { host, home } = await served();
|
|
138
|
+
|
|
139
|
+
const made = await host.call(gitWorktreeCreate, { repo: join(repo, "src"), description: "try the feature" });
|
|
140
|
+
|
|
141
|
+
expect(made.id).toMatch(/^wt-[0-9a-f]{6,8}$/);
|
|
142
|
+
expect(made.branch).toBe(made.id);
|
|
143
|
+
expect(made.path).toBe(join(home, "worktrees", made.id));
|
|
144
|
+
expect(made.repo).toBe(repo);
|
|
145
|
+
expect(made.base).toBe("feature");
|
|
146
|
+
expect(made.baseSha).toBe(gitIn(repo, "rev-parse", "feature"));
|
|
147
|
+
expect(gitIn(made.path, "symbolic-ref", "--short", "HEAD")).toBe(made.id);
|
|
148
|
+
expect(existsSync(join(made.path, "feature.txt"))).toBe(true);
|
|
149
|
+
// Ignored and named: copied. Ignored and not named, or named and not ignored: not.
|
|
150
|
+
expect(readFileSync(join(made.path, ".env"), "utf8")).toBe("TOKEN=1\n");
|
|
151
|
+
expect(readFileSync(join(made.path, "node_modules", "pkg", "index.js"), "utf8")).toBe("module.exports = 1;\n");
|
|
152
|
+
expect(existsSync(join(made.path, "secret.local"))).toBe(false);
|
|
153
|
+
expect(existsSync(join(made.path, "plain.txt"))).toBe(false);
|
|
154
|
+
|
|
155
|
+
const now = await host.call(gitWorktreeGet, { id: made.id.slice(0, 5) });
|
|
156
|
+
expect(now).toMatchObject({ id: made.id, dirty: false, aheadOfBase: 0, sessions: [] });
|
|
157
|
+
expect((await ofType(host, "git.worktree.created")).map((row) => row.data)).toEqual([made]);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("a detached HEAD is the base by its commit; an explicit base is taken as given", async () => {
|
|
161
|
+
const repo = await makeRepo();
|
|
162
|
+
const sha = gitIn(repo, "rev-parse", "HEAD");
|
|
163
|
+
gitIn(repo, "switch", "-q", "--detach", "HEAD");
|
|
164
|
+
const { host } = await served();
|
|
165
|
+
|
|
166
|
+
const detached = await host.call(gitWorktreeCreate, { repo, description: "from a detached head" });
|
|
167
|
+
expect(detached.base).toBe(sha);
|
|
168
|
+
const named = await host.call(gitWorktreeCreate, { repo, base: "main", description: "from main" });
|
|
169
|
+
expect(named.base).toBe("main");
|
|
170
|
+
expect(named.id).not.toBe(detached.id);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test("a remove refuses a dirty worktree and one ahead of its base, removes it with force, and a second remove is a no-op", async () => {
|
|
174
|
+
const repo = await makeRepo();
|
|
175
|
+
const { host } = await served();
|
|
176
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "work to keep" });
|
|
177
|
+
|
|
178
|
+
writeFileSync(join(made.path, "new.txt"), "draft\n");
|
|
179
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, { id: made.id }))).toBe("dirty");
|
|
180
|
+
gitIn(made.path, "add", "new.txt");
|
|
181
|
+
gitIn(made.path, "commit", "-q", "-m", "draft");
|
|
182
|
+
expect(await host.call(gitWorktreeGet, { id: made.id })).toMatchObject({ dirty: false, aheadOfBase: 1 });
|
|
183
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, { id: made.id }))).toBe("ahead");
|
|
184
|
+
expect(existsSync(made.path)).toBe(true);
|
|
185
|
+
|
|
186
|
+
expect(await host.call(gitWorktreeRemove, { id: made.id, force: true })).toEqual({ id: made.id, removed: true });
|
|
187
|
+
expect(existsSync(made.path)).toBe(false);
|
|
188
|
+
expect(gitIn(repo, "branch", "--list", made.id)).toBe("");
|
|
189
|
+
expect(await host.call(gitWorktreeRemove, { id: made.id })).toEqual({ id: made.id, removed: false });
|
|
190
|
+
expect((await ofType(host, "git.worktree.removed")).map((row) => row.data)).toEqual([{ id: made.id, forced: true }]);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("commits merged into the base do not hold a worktree: a clean one whose base took its commits is removed", async () => {
|
|
194
|
+
const repo = await makeRepo();
|
|
195
|
+
const { host } = await served();
|
|
196
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "merged work" });
|
|
197
|
+
writeFileSync(join(made.path, "done.txt"), "done\n");
|
|
198
|
+
gitIn(made.path, "add", "done.txt");
|
|
199
|
+
gitIn(made.path, "commit", "-q", "-m", "done");
|
|
200
|
+
gitIn(repo, "merge", "-q", "--ff-only", made.branch);
|
|
201
|
+
|
|
202
|
+
expect(await host.call(gitWorktreeRemove, { id: made.id })).toEqual({ id: made.id, removed: true });
|
|
203
|
+
expect((await ofType(host, "git.worktree.removed")).map((row) => row.data)).toEqual([{ id: made.id, forced: false }]);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test("the list flags a worktree whose directory is gone and a directory of the store no record names", async () => {
|
|
207
|
+
const repo = await makeRepo();
|
|
208
|
+
const { host, home } = await served();
|
|
209
|
+
const kept = await host.call(gitWorktreeCreate, { repo, description: "stays as it is" });
|
|
210
|
+
const lost = await host.call(gitWorktreeCreate, { repo, base: "main", description: "its directory goes" });
|
|
211
|
+
rmSync(lost.path, { recursive: true, force: true });
|
|
212
|
+
mkdirSync(join(home, "worktrees", "wt-0abcde"), { recursive: true });
|
|
213
|
+
|
|
214
|
+
const { items } = await host.call(gitWorktreeList, {});
|
|
215
|
+
const byId = new Map(items.map((item) => [item.id, item]));
|
|
216
|
+
expect(byId.get(kept.id)).toMatchObject({ state: "ok", dirty: false, aheadOfBase: 0, sessions: [] });
|
|
217
|
+
expect(byId.get(kept.id)?.ageDays).toBeGreaterThanOrEqual(0);
|
|
218
|
+
expect(byId.get(lost.id)).toMatchObject({ state: "missing" });
|
|
219
|
+
expect(byId.get("wt-0abcde" as never)).toMatchObject({ state: "orphan", path: join(home, "worktrees", "wt-0abcde") });
|
|
220
|
+
|
|
221
|
+
// An orphan goes with force only; a missing one is removed with its branch.
|
|
222
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, { id: "wt-0abcde" }))).toBe("orphan");
|
|
223
|
+
expect(await host.call(gitWorktreeRemove, { id: "wt-0abcde", force: true })).toEqual({ id: "wt-0abcde", removed: true });
|
|
224
|
+
expect(await host.call(gitWorktreeRemove, { id: lost.id })).toEqual({ id: lost.id, removed: true });
|
|
225
|
+
expect(gitIn(repo, "branch", "--list", lost.id)).toBe("");
|
|
226
|
+
expect((await host.call(gitWorktreeList, {})).items.map((item) => item.id)).toEqual([kept.id]);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("session.start({ worktree }) runs the session in a new worktree of its cwd, which a remove then refuses for the live session", async () => {
|
|
230
|
+
const repo = await makeRepo();
|
|
231
|
+
const { host, control } = await served(true);
|
|
232
|
+
|
|
233
|
+
const started = await host.call(sessionStart, { harness: HARNESS, prompt: "", cwd: repo, worktree: true });
|
|
234
|
+
|
|
235
|
+
const made = started.worktree;
|
|
236
|
+
expect(made).toBeDefined();
|
|
237
|
+
if (made === undefined) return;
|
|
238
|
+
expect(control.starts[0]?.cwd).toBe(made.path);
|
|
239
|
+
const [record] = await ofType(host, "session.started");
|
|
240
|
+
expect((record?.data as { cwd?: string }).cwd).toBe(made.path);
|
|
241
|
+
expect(await host.call(gitWorktreeGet, { id: made.id })).toMatchObject({ sessions: [started.session], description: "a session of claude-sonnet" });
|
|
242
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, { id: made.id }))).toBe("live-session");
|
|
243
|
+
expect(await host.call(gitWorktreeRemove, { id: made.id, force: true })).toEqual({ id: made.id, removed: true });
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
/** Run a script to its end on the host; the outcome it finished with. */
|
|
247
|
+
async function runScript(host: Host, home: string, lines: readonly string[]): Promise<unknown> {
|
|
248
|
+
const file = join(home, `script-${String(Date.now())}.js`);
|
|
249
|
+
writeFileSync(file, lines.join("\n"));
|
|
250
|
+
await host.call("workflow.start" as ActionName, { file } as never);
|
|
251
|
+
let outcome: unknown;
|
|
252
|
+
await eventually(
|
|
253
|
+
async () => {
|
|
254
|
+
outcome = (await ofType(host, "workflow.run.finished")).at(-1)?.data;
|
|
255
|
+
return outcome !== undefined;
|
|
256
|
+
},
|
|
257
|
+
20_000,
|
|
258
|
+
"the run should finish",
|
|
259
|
+
);
|
|
260
|
+
return (outcome as { outcome: unknown }).outcome;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
test("ask({ worktree: true }) removes the worktree when the agent left it clean", async () => {
|
|
264
|
+
const repo = await makeRepo();
|
|
265
|
+
const { host, home } = await served(true);
|
|
266
|
+
|
|
267
|
+
const outcome = await runScript(host, home, [
|
|
268
|
+
'export default workflow({ uses: ["session.start.claude-sonnet"], output: z.json() }, async () => {',
|
|
269
|
+
` return await ask({ harness: "claude-sonnet", cwd: ${JSON.stringify(repo)}, worktree: true }, "look around");`,
|
|
270
|
+
"});",
|
|
271
|
+
]);
|
|
272
|
+
|
|
273
|
+
expect(outcome).toEqual({ status: "done", result: { answer: "done" } });
|
|
274
|
+
const [created] = await ofType(host, "git.worktree.created");
|
|
275
|
+
const made = created?.data as { id: string; path: string; description: string };
|
|
276
|
+
expect(made.description).toBe("look around");
|
|
277
|
+
expect(existsSync(made.path)).toBe(false);
|
|
278
|
+
// Closing the session first permits a normal removal, which still checks for other work.
|
|
279
|
+
expect((await ofType(host, "git.worktree.removed")).map((row) => row.data)).toEqual([{ id: made.id, forced: false }]);
|
|
280
|
+
}, 30_000);
|
|
281
|
+
|
|
282
|
+
test("ask({ worktree }) keeps the worktree the agent changed, and the answer says where it is", async () => {
|
|
283
|
+
const repo = await makeRepo();
|
|
284
|
+
const { host, home } = await served(true);
|
|
285
|
+
|
|
286
|
+
const outcome = await runScript(host, home, [
|
|
287
|
+
'export default workflow({ uses: ["session.start.claude-sonnet"], output: z.json() }, async () => {',
|
|
288
|
+
" const answer = await ask({",
|
|
289
|
+
` harness: "claude-sonnet", worktree: { repo: ${JSON.stringify(repo)}, description: "the change" },`,
|
|
290
|
+
" schema: z.object({ answer: z.string() }),",
|
|
291
|
+
' }, "change a file");',
|
|
292
|
+
" return answer;",
|
|
293
|
+
"});",
|
|
294
|
+
]);
|
|
295
|
+
|
|
296
|
+
const [created] = await ofType(host, "git.worktree.created");
|
|
297
|
+
const made = created?.data as { id: string; path: string; branch: string };
|
|
298
|
+
expect(outcome).toEqual({
|
|
299
|
+
status: "done",
|
|
300
|
+
result: { answer: "done", worktree: { id: made.id, path: made.path, branch: made.branch, changed: true } },
|
|
301
|
+
});
|
|
302
|
+
expect(readFileSync(join(made.path, "made-by-agent.txt"), "utf8")).toBe("work\n");
|
|
303
|
+
expect(await ofType(host, "git.worktree.removed")).toEqual([]);
|
|
304
|
+
}, 30_000);
|
|
305
|
+
|
|
306
|
+
test("the built-in cleanup removes the old clean worktrees and keeps the dirty and the unmerged ones", async () => {
|
|
307
|
+
const repo = await makeRepo();
|
|
308
|
+
const home = await tempHome();
|
|
309
|
+
homes.push(home);
|
|
310
|
+
const host = createNiwa({ home: home.path });
|
|
311
|
+
// The host writes only git(): the module offers its timer and its workflow. A timer of the same id the host lists is
|
|
312
|
+
// the one scheduled, so the test fires it often; no days, so the worktrees just made are old enough.
|
|
313
|
+
host.use(timers([timer.every(300, { id: CLEANUP_TIMER })]), git({ cleanup: { days: 0 } }), workflow());
|
|
314
|
+
await host.serve();
|
|
315
|
+
hosts.push(host);
|
|
316
|
+
|
|
317
|
+
const clean = await host.call(gitWorktreeCreate, { repo, description: "nothing in it" });
|
|
318
|
+
const dirty = await host.call(gitWorktreeCreate, { repo, base: "main", description: "a draft in it" });
|
|
319
|
+
writeFileSync(join(dirty.path, "draft.txt"), "draft\n");
|
|
320
|
+
const ahead = await host.call(gitWorktreeCreate, { repo, base: gitIn(repo, "rev-parse", "HEAD"), description: "a commit in it" });
|
|
321
|
+
writeFileSync(join(ahead.path, "work.txt"), "work\n");
|
|
322
|
+
gitIn(ahead.path, "add", "work.txt");
|
|
323
|
+
gitIn(ahead.path, "commit", "-q", "-m", "work");
|
|
324
|
+
|
|
325
|
+
await eventually(async () => (await ofType(host, "git.worktree.removed")).length > 0, 20_000, "cleanup should record its removal");
|
|
326
|
+
await eventually(async () => (await ofType(host, "workflow.run.finished")).length >= 2, 20_000, "two cleanups should run");
|
|
327
|
+
expect(existsSync(clean.path)).toBe(false);
|
|
328
|
+
expect(existsSync(dirty.path)).toBe(true);
|
|
329
|
+
expect(existsSync(ahead.path)).toBe(true);
|
|
330
|
+
expect((await ofType(host, "git.worktree.removed")).map((row) => row.data)).toEqual([{ id: clean.id, forced: false }]);
|
|
331
|
+
const [installed] = (await host.call(workflowInstallationList, {})).items;
|
|
332
|
+
expect(installed?.builtin).toEqual({ plugin: "git", name: "worktree-cleanup" });
|
|
333
|
+
}, 40_000);
|
|
334
|
+
|
|
335
|
+
test("git({ cleanup: false }) offers no cleanup: nothing is registered or installed", async () => {
|
|
336
|
+
const home = await tempHome();
|
|
337
|
+
homes.push(home);
|
|
338
|
+
const host = createNiwa({ home: home.path });
|
|
339
|
+
host.use(timers([]), git({ cleanup: false }), workflow());
|
|
340
|
+
await host.serve();
|
|
341
|
+
hosts.push(host);
|
|
342
|
+
await Bun.sleep(300);
|
|
343
|
+
expect((await host.call(workflowInstallationList, {})).items).toEqual([]);
|
|
344
|
+
expect(await ofType(host, "workflow.registered")).toEqual([]);
|
|
345
|
+
expect((await host.call(timerList, {})).timers).toEqual([]);
|
|
346
|
+
}, 20_000);
|
|
347
|
+
|
|
348
|
+
test("the list shows each worktree's head and the remote its base pushes to, when fetch and push name the same project", async () => {
|
|
349
|
+
const repo = await makeRepo();
|
|
350
|
+
gitIn(repo, "remote", "add", "origin", "git@GitLab.Example.com:acme/billing.git");
|
|
351
|
+
gitIn(repo, "config", "branch.main.remote", "origin");
|
|
352
|
+
gitIn(repo, "config", "branch.main.merge", "refs/heads/main");
|
|
353
|
+
const other = await makeRepo();
|
|
354
|
+
gitIn(other, "remote", "add", "upstream", "https://token@gitlab.example.com:8443/acme/tools/cli.git/");
|
|
355
|
+
gitIn(other, "config", "branch.main.remote", "upstream");
|
|
356
|
+
const split = await makeRepo();
|
|
357
|
+
gitIn(split, "remote", "add", "origin", "https://gitlab.example.com/acme/billing.git");
|
|
358
|
+
gitIn(split, "remote", "set-url", "--push", "origin", "https://gitlab.example.com/acme/fork.git");
|
|
359
|
+
const local = await makeRepo();
|
|
360
|
+
gitIn(local, "remote", "add", "origin", "/srv/git/billing.git");
|
|
361
|
+
const bare = await makeRepo();
|
|
362
|
+
const { host } = await served();
|
|
363
|
+
|
|
364
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "with a remote" });
|
|
365
|
+
const tools = await host.call(gitWorktreeCreate, { repo: other, description: "tracks upstream" });
|
|
366
|
+
const forked = await host.call(gitWorktreeCreate, { repo: split, description: "pushes elsewhere" });
|
|
367
|
+
const onDisk = await host.call(gitWorktreeCreate, { repo: local, description: "a local remote" });
|
|
368
|
+
const none = await host.call(gitWorktreeCreate, { repo: bare, description: "no remote" });
|
|
369
|
+
writeFileSync(join(made.path, "next.txt"), "next\n");
|
|
370
|
+
gitIn(made.path, "add", "next.txt");
|
|
371
|
+
gitIn(made.path, "commit", "-q", "-m", "next");
|
|
372
|
+
|
|
373
|
+
const byId = new Map((await host.call(gitWorktreeList, {})).items.map((item) => [item.id, item]));
|
|
374
|
+
expect(byId.get(made.id)).toMatchObject({ head: gitIn(made.path, "rev-parse", "HEAD"), remote: { name: "origin", host: "gitlab.example.com", path: "acme/billing" } });
|
|
375
|
+
expect(byId.get(tools.id)?.remote).toEqual({ name: "upstream", host: "gitlab.example.com", path: "acme/tools/cli" });
|
|
376
|
+
expect(byId.get(none.id)?.head).toBe(none.baseSha);
|
|
377
|
+
for (const unverified of [forked, onDisk, none]) expect(byId.get(unverified.id)?.remote).toBeUndefined();
|
|
378
|
+
expect(await host.call(gitWorktreeGet, { id: made.id })).toMatchObject({ remote: { host: "gitlab.example.com", path: "acme/billing" } });
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
test("a head that moved in a worktree is recorded once, for the session whose turn ended there or the caller that looked", async () => {
|
|
382
|
+
const repo = await makeRepo();
|
|
383
|
+
const { host } = await served(true);
|
|
384
|
+
const started = await host.call(sessionStart, { harness: HARNESS, prompt: "", cwd: repo, worktree: true });
|
|
385
|
+
const made = started.worktree;
|
|
386
|
+
if (made === undefined) throw new Error("no worktree");
|
|
387
|
+
const commit = (file: string): string => {
|
|
388
|
+
writeFileSync(join(made.path, file), `${file}\n`);
|
|
389
|
+
gitIn(made.path, "add", file);
|
|
390
|
+
gitIn(made.path, "commit", "-q", "-m", file);
|
|
391
|
+
return gitIn(made.path, "rev-parse", "HEAD");
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
// A turn that moved nothing writes nothing.
|
|
395
|
+
await host.emit(SessionTurnEnded, { session: started.ref as never, turn: 1 });
|
|
396
|
+
const first = commit("one.txt");
|
|
397
|
+
await host.emit(SessionTurnEnded, { session: started.ref as never, turn: 2 });
|
|
398
|
+
await eventually(async () => (await ofType(host, "git.head.moved")).length > 0);
|
|
399
|
+
expect((await ofType(host, "git.head.moved")).map((row) => row.data)).toEqual([
|
|
400
|
+
{ worktree: made.id, repo, path: made.path, branch: made.branch, head: first, previous: gitIn(repo, "rev-parse", "main"), session: started.ref },
|
|
401
|
+
]);
|
|
402
|
+
// Looked at again, a head that stayed is not written again.
|
|
403
|
+
expect(await host.call(gitHeadObserve, {})).toEqual({ moved: [] });
|
|
404
|
+
|
|
405
|
+
// A caller that looks itself is who the record is of: no session is named.
|
|
406
|
+
const second = commit("two.txt");
|
|
407
|
+
expect(await host.call(gitHeadObserve, { worktree: made.id })).toEqual({ moved: [{ worktree: made.id, head: second }] });
|
|
408
|
+
const rows = await ofType(host, "git.head.moved");
|
|
409
|
+
expect(rows.map((row) => row.data)).toEqual([
|
|
410
|
+
expect.objectContaining({ head: first, session: started.ref }),
|
|
411
|
+
{ worktree: made.id, repo, path: made.path, branch: made.branch, head: second, previous: first },
|
|
412
|
+
]);
|
|
413
|
+
});
|