@cueloop/daemon 0.1.0-alpha.7 → 0.1.0-alpha.71
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/package.json +33 -22
- package/src/api.test.ts +891 -50
- package/src/api.ts +849 -72
- package/src/capabilities.test.ts +57 -0
- package/src/capabilities.ts +77 -0
- package/src/client.test.ts +24 -0
- package/src/client.ts +459 -69
- package/src/curate.ts +109 -0
- package/src/daemon-version.test.ts +93 -0
- package/src/diff-hot-reload.test.ts +186 -0
- package/src/diff-watcher.ts +102 -0
- package/src/herdr-pane.test.ts +240 -0
- package/src/herdr-pane.ts +167 -0
- package/src/herdr-split.test.ts +106 -0
- package/src/herdr-split.ts +92 -0
- package/src/herdr-tab-store.test.ts +96 -0
- package/src/herdr-tab-store.ts +66 -0
- package/src/index.ts +8 -9
- package/src/interruptible-wait.test.ts +127 -0
- package/src/interruptible-wait.ts +66 -0
- package/src/main.ts +4 -1
- package/src/paths.ts +39 -0
- package/src/project-files.test.ts +124 -0
- package/src/project-files.ts +56 -0
- package/src/protocol.ts +79 -7
- package/src/retention.test.ts +182 -0
- package/src/retention.ts +122 -0
- package/src/review.test.ts +255 -28
- package/src/review.ts +164 -62
- package/src/server.test.ts +381 -39
- package/src/server.ts +322 -84
- package/src/share-blob.test.ts +112 -0
- package/src/share-blob.ts +55 -0
- package/src/single-instance.test.ts +38 -11
- package/src/store.conformance.test.ts +41 -0
- package/src/store.jsonl.test.ts +159 -0
- package/src/store.ts +261 -34
- package/src/testing/store-conformance.ts +194 -0
- package/src/validate.test.ts +174 -28
- package/src/validate.ts +226 -16
- package/src/version.ts +6 -0
- package/src/working-tree.ts +149 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/** Creation-side herdr pane opener: a stubbed herdr binary logs its argv, openHerdrPane returns the tab handle, and openHerdrPaneForReview opens+records for a new review, focuses a still-alive recorded tab, and reopens a dead one - collision-free by pane id. */
|
|
2
|
+
|
|
3
|
+
import { afterAll, describe, expect, test } from "bun:test";
|
|
4
|
+
import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import type { Thread } from "@cueloop/schema";
|
|
8
|
+
import { openHerdrPane, openHerdrPaneForReview } from "./herdr-pane";
|
|
9
|
+
import type { HerdrTabPersistence } from "./herdr-pane";
|
|
10
|
+
import type { HerdrTabHandle } from "./herdr-tab-store";
|
|
11
|
+
|
|
12
|
+
const dir = mkdtempSync(join(tmpdir(), "cueloop-herdr-pane-"));
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A stub herdr binary. It logs argv, prints the tab-create result carrying both
|
|
16
|
+
* pane_id and tab_id (herdr 0.8.2 shape), and answers `pane get` alive or dead
|
|
17
|
+
* per `paneAlive` so the liveness branch can be exercised.
|
|
18
|
+
*/
|
|
19
|
+
function makeStub(name: string, paneAlive = false) {
|
|
20
|
+
const logPath = join(dir, `${name}.log`);
|
|
21
|
+
const binPath = join(dir, `${name}.sh`);
|
|
22
|
+
const paneGet = paneAlive ? `printf '{"result":{"pane":{"pane_id":"w1:p2"}}}'` : "exit 1";
|
|
23
|
+
|
|
24
|
+
writeFileSync(
|
|
25
|
+
binPath,
|
|
26
|
+
`#!/bin/sh
|
|
27
|
+
printf '%s\\n' "$*" >> "${logPath}"
|
|
28
|
+
if [ "$1" = "tab" ] && [ "$2" = "create" ]; then
|
|
29
|
+
printf '{"result":{"root_pane":{"pane_id":"w1:p2","tab_id":"w1:t2"}}}'
|
|
30
|
+
fi
|
|
31
|
+
if [ "$1" = "pane" ] && [ "$2" = "get" ]; then ${paneGet}; fi
|
|
32
|
+
exit 0
|
|
33
|
+
`,
|
|
34
|
+
);
|
|
35
|
+
chmodSync(binPath, 0o755);
|
|
36
|
+
|
|
37
|
+
return { binPath, logPath };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function readLines(logPath: string): string[] {
|
|
41
|
+
if (!existsSync(logPath)) return [];
|
|
42
|
+
|
|
43
|
+
return readFileSync(logPath, "utf8").split("\n").filter(Boolean);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** In-memory persistence double: records what openHerdrPaneForReview saves. */
|
|
47
|
+
function fakePersistence(initial: HerdrTabHandle | null = null) {
|
|
48
|
+
let stored = initial;
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
persistence: {
|
|
52
|
+
herdrGetTab: async () => stored,
|
|
53
|
+
herdrSetTab: async (_sessionId: string, handle: HerdrTabHandle) => {
|
|
54
|
+
stored = handle;
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
saved: () => stored,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function newSession(overrides: Partial<Thread> = {}): Thread {
|
|
62
|
+
return {
|
|
63
|
+
schemaVersion: "1",
|
|
64
|
+
id: "ses_new1",
|
|
65
|
+
workspace: { repoRoot: "/repo", branch: "main" },
|
|
66
|
+
artifact: { type: "plan", content: "# P", meta: { cwd: "/repo/work", title: "Rollout Plan" } },
|
|
67
|
+
revisions: [{ revision: 1, content: "# P", submittedAt: "now" }],
|
|
68
|
+
annotations: [],
|
|
69
|
+
verdict: null,
|
|
70
|
+
status: "pending",
|
|
71
|
+
createdAt: "now",
|
|
72
|
+
...overrides,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
describe("openHerdrPane", () => {
|
|
77
|
+
test("creates a focused tab, launches the review, and returns the handle", () => {
|
|
78
|
+
// Arrange
|
|
79
|
+
const stub = makeStub("open");
|
|
80
|
+
|
|
81
|
+
// Act
|
|
82
|
+
const handle = openHerdrPane({
|
|
83
|
+
sessionId: "ses_abc",
|
|
84
|
+
cwd: "/repo/work",
|
|
85
|
+
binPath: stub.binPath,
|
|
86
|
+
label: "Rollout Plan",
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Assert
|
|
90
|
+
expect(handle).toEqual({ tabId: "w1:t2", paneId: "w1:p2" });
|
|
91
|
+
expect(readLines(stub.logPath)).toEqual([
|
|
92
|
+
"tab create --cwd /repo/work --label Rollout Plan --focus",
|
|
93
|
+
"pane send-text w1:p2 cueloop ses_abc",
|
|
94
|
+
"pane send-keys w1:p2 enter",
|
|
95
|
+
]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("returns null and never throws on a broken binary", () => {
|
|
99
|
+
expect(
|
|
100
|
+
openHerdrPane({
|
|
101
|
+
sessionId: "ses_abc",
|
|
102
|
+
cwd: "/repo/work",
|
|
103
|
+
binPath: join(dir, "missing-bin"),
|
|
104
|
+
label: "x",
|
|
105
|
+
}),
|
|
106
|
+
).toBeNull();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("returns null when tab create yields no ids - no send-text, no send-keys", () => {
|
|
110
|
+
// Arrange
|
|
111
|
+
const logPath = join(dir, "nopane.log");
|
|
112
|
+
const binPath = join(dir, "nopane.sh");
|
|
113
|
+
|
|
114
|
+
writeFileSync(
|
|
115
|
+
binPath,
|
|
116
|
+
`#!/bin/sh\nprintf '%s\\n' "$*" >> "${logPath}"\nprintf '{"result":{}}'\n`,
|
|
117
|
+
);
|
|
118
|
+
chmodSync(binPath, 0o755);
|
|
119
|
+
|
|
120
|
+
// Act
|
|
121
|
+
const handle = openHerdrPane({ sessionId: "ses_abc", cwd: "/repo/work", binPath, label: "x" });
|
|
122
|
+
|
|
123
|
+
// Assert
|
|
124
|
+
expect(handle).toBeNull();
|
|
125
|
+
expect(readLines(logPath)).toEqual(["tab create --cwd /repo/work --label x --focus"]);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe("openHerdrPaneForReview", () => {
|
|
130
|
+
test("opens and records a tab for a review with no recorded tab", async () => {
|
|
131
|
+
// Arrange
|
|
132
|
+
const stub = makeStub("gated-new");
|
|
133
|
+
const env = { HERDR_ENV: "1", HERDR_PANE_ID: "w1:p1", HERDR_BIN_PATH: stub.binPath };
|
|
134
|
+
const store = fakePersistence(null);
|
|
135
|
+
|
|
136
|
+
// Act
|
|
137
|
+
await openHerdrPaneForReview(newSession({ id: "ses_xyz" }), store.persistence, env);
|
|
138
|
+
|
|
139
|
+
// Assert
|
|
140
|
+
expect(readLines(stub.logPath)).toEqual([
|
|
141
|
+
"tab create --cwd /repo/work --label Rollout Plan --focus",
|
|
142
|
+
"pane send-text w1:p2 cueloop ses_xyz",
|
|
143
|
+
"pane send-keys w1:p2 enter",
|
|
144
|
+
]);
|
|
145
|
+
expect(store.saved()).toEqual({ tabId: "w1:t2", paneId: "w1:p2" });
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("no-op outside herdr - no herdr process is spawned", async () => {
|
|
149
|
+
// Arrange
|
|
150
|
+
const stub = makeStub("gated-outside");
|
|
151
|
+
const store = fakePersistence(null);
|
|
152
|
+
|
|
153
|
+
// Act
|
|
154
|
+
await openHerdrPaneForReview(newSession(), store.persistence, {
|
|
155
|
+
HERDR_PANE_ID: "w1:p1",
|
|
156
|
+
HERDR_BIN_PATH: stub.binPath,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Assert
|
|
160
|
+
expect(existsSync(stub.logPath)).toBeFalse();
|
|
161
|
+
expect(store.saved()).toBeNull();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("focuses the recorded tab when its pane is still alive, without reopening", async () => {
|
|
165
|
+
// Arrange
|
|
166
|
+
const stub = makeStub("gated-alive", true);
|
|
167
|
+
const env = { HERDR_ENV: "1", HERDR_PANE_ID: "w1:p1", HERDR_BIN_PATH: stub.binPath };
|
|
168
|
+
const store = fakePersistence({ tabId: "w9:t9", paneId: "w9:p9" });
|
|
169
|
+
|
|
170
|
+
// Act
|
|
171
|
+
await openHerdrPaneForReview(newSession(), store.persistence, env);
|
|
172
|
+
|
|
173
|
+
// Assert
|
|
174
|
+
const lines = readLines(stub.logPath);
|
|
175
|
+
|
|
176
|
+
expect(lines).toContain("pane get w9:p9");
|
|
177
|
+
expect(lines).toContain("tab focus w9:t9");
|
|
178
|
+
expect(lines.some((line) => line.startsWith("tab create"))).toBeFalse();
|
|
179
|
+
expect(store.saved()).toEqual({ tabId: "w9:t9", paneId: "w9:p9" });
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("a persistence failure never escapes - review creation stays best-effort", async () => {
|
|
183
|
+
// Arrange - the daemon store rejects; the opener must still resolve quietly
|
|
184
|
+
const stub = makeStub("gated-persist-fail");
|
|
185
|
+
const env = { HERDR_ENV: "1", HERDR_PANE_ID: "w1:p1", HERDR_BIN_PATH: stub.binPath };
|
|
186
|
+
const persistence: HerdrTabPersistence = {
|
|
187
|
+
herdrGetTab: async () => null,
|
|
188
|
+
herdrSetTab: async () => {
|
|
189
|
+
throw new Error("disk full");
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// Act & Assert - no throw
|
|
194
|
+
await openHerdrPaneForReview(newSession(), persistence, env);
|
|
195
|
+
expect(readLines(stub.logPath).some((line) => line.startsWith("tab create"))).toBeTrue();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("a recall failure (stale daemon) still opens a fresh tab", async () => {
|
|
199
|
+
// Arrange - a daemon predating the herdr-tab primitives rejects the recall
|
|
200
|
+
const stub = makeStub("gated-recall-fail");
|
|
201
|
+
const env = { HERDR_ENV: "1", HERDR_PANE_ID: "w1:p1", HERDR_BIN_PATH: stub.binPath };
|
|
202
|
+
const persistence: HerdrTabPersistence = {
|
|
203
|
+
herdrGetTab: async () => {
|
|
204
|
+
throw new Error("unknown method herdr.getTab");
|
|
205
|
+
},
|
|
206
|
+
herdrSetTab: async () => {},
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// Act
|
|
210
|
+
await openHerdrPaneForReview(newSession({ id: "ses_stale" }), persistence, env);
|
|
211
|
+
|
|
212
|
+
// Assert - the recall throwing must not abort the open
|
|
213
|
+
expect(readLines(stub.logPath)).toEqual([
|
|
214
|
+
"tab create --cwd /repo/work --label Rollout Plan --focus",
|
|
215
|
+
"pane send-text w1:p2 cueloop ses_stale",
|
|
216
|
+
"pane send-keys w1:p2 enter",
|
|
217
|
+
]);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test("reopens and re-records when the recorded pane is dead", async () => {
|
|
221
|
+
// Arrange
|
|
222
|
+
const stub = makeStub("gated-dead", false);
|
|
223
|
+
const env = { HERDR_ENV: "1", HERDR_PANE_ID: "w1:p1", HERDR_BIN_PATH: stub.binPath };
|
|
224
|
+
const store = fakePersistence({ tabId: "old:t", paneId: "old:p" });
|
|
225
|
+
|
|
226
|
+
// Act
|
|
227
|
+
await openHerdrPaneForReview(newSession({ id: "ses_re" }), store.persistence, env);
|
|
228
|
+
|
|
229
|
+
// Assert
|
|
230
|
+
const lines = readLines(stub.logPath);
|
|
231
|
+
|
|
232
|
+
expect(lines).toContain("pane get old:p");
|
|
233
|
+
expect(lines).toContain("tab create --cwd /repo/work --label Rollout Plan --focus");
|
|
234
|
+
expect(store.saved()).toEqual({ tabId: "w1:t2", paneId: "w1:p2" });
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
afterAll(() => {
|
|
239
|
+
rmSync(dir, { recursive: true, force: true, maxRetries: 3 });
|
|
240
|
+
});
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opens a herdr tab that renders a review, from the creation sites (the Claude
|
|
3
|
+
* Code hook and `cueloop session create`) that run inside a herdr pane. On a
|
|
4
|
+
* resubmit it reopens only when the recorded pane is dead, so a live tab is
|
|
5
|
+
* focused rather than duplicated. Best-effort: any spawn failure is swallowed
|
|
6
|
+
* and never blocks review creation; each spawn is bounded by a timeout.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { detectHerdr, type HerdrEnv, type Thread } from "@cueloop/schema";
|
|
10
|
+
import * as v from "valibot";
|
|
11
|
+
import type { HerdrTabHandle } from "./herdr-tab-store";
|
|
12
|
+
|
|
13
|
+
const HERDR_SPAWN_TIMEOUT_MS = 2000;
|
|
14
|
+
const CreatedTabSchema = v.object({
|
|
15
|
+
result: v.optional(
|
|
16
|
+
v.object({
|
|
17
|
+
root_pane: v.optional(
|
|
18
|
+
v.object({ pane_id: v.optional(v.string()), tab_id: v.optional(v.string()) }),
|
|
19
|
+
),
|
|
20
|
+
}),
|
|
21
|
+
),
|
|
22
|
+
});
|
|
23
|
+
const PaneResultSchema = v.object({
|
|
24
|
+
result: v.optional(v.object({ pane: v.optional(v.unknown()) })),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export interface OpenHerdrPaneOptions {
|
|
28
|
+
sessionId: string;
|
|
29
|
+
cwd: string;
|
|
30
|
+
binPath: string;
|
|
31
|
+
label: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The daemon-side handle store, as the two methods this module needs. DaemonClient satisfies it. */
|
|
35
|
+
export interface HerdrTabPersistence {
|
|
36
|
+
herdrGetTab(sessionId: string): Promise<HerdrTabHandle | null>;
|
|
37
|
+
herdrSetTab(sessionId: string, handle: HerdrTabHandle): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Open a focused tab and launch the review in it. `tab create` prints
|
|
42
|
+
* `{ result: { root_pane: { pane_id, tab_id } } }` (herdr 0.8.2); both ids are
|
|
43
|
+
* returned so a later resubmit can liveness-check and focus. Null on any
|
|
44
|
+
* best-effort bail-out.
|
|
45
|
+
*/
|
|
46
|
+
export function openHerdrPane(options: OpenHerdrPaneOptions): HerdrTabHandle | null {
|
|
47
|
+
const { sessionId, cwd, binPath, label } = options;
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const created = Bun.spawnSync(
|
|
51
|
+
[binPath, "tab", "create", "--cwd", cwd, "--label", label, "--focus"],
|
|
52
|
+
{ stdout: "pipe", stderr: "ignore", timeout: HERDR_SPAWN_TIMEOUT_MS },
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
if (created.exitCode !== 0) return null;
|
|
56
|
+
const parsed = v.safeParse(CreatedTabSchema, JSON.parse(created.stdout.toString()));
|
|
57
|
+
|
|
58
|
+
if (!parsed.success) return null;
|
|
59
|
+
const paneId = parsed.output.result?.root_pane?.pane_id;
|
|
60
|
+
const tabId = parsed.output.result?.root_pane?.tab_id;
|
|
61
|
+
|
|
62
|
+
if (!paneId || !tabId) return null;
|
|
63
|
+
// A fresh tab hosts a plain shell, so the review is typed in like a human.
|
|
64
|
+
const typed = Bun.spawnSync([binPath, "pane", "send-text", paneId, `cueloop ${sessionId}`], {
|
|
65
|
+
stdout: "ignore",
|
|
66
|
+
stderr: "ignore",
|
|
67
|
+
timeout: HERDR_SPAWN_TIMEOUT_MS,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (typed.exitCode !== 0) return null;
|
|
71
|
+
const entered = Bun.spawnSync([binPath, "pane", "send-keys", paneId, "enter"], {
|
|
72
|
+
stdout: "ignore",
|
|
73
|
+
stderr: "ignore",
|
|
74
|
+
timeout: HERDR_SPAWN_TIMEOUT_MS,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return entered.exitCode === 0 ? { tabId, paneId } : null;
|
|
78
|
+
} catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** True when the pane still exists (`herdr pane get` returns it). */
|
|
84
|
+
function herdrPaneAlive(binPath: string, paneId: string): boolean {
|
|
85
|
+
try {
|
|
86
|
+
const got = Bun.spawnSync([binPath, "pane", "get", paneId], {
|
|
87
|
+
stdout: "pipe",
|
|
88
|
+
stderr: "ignore",
|
|
89
|
+
timeout: HERDR_SPAWN_TIMEOUT_MS,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (got.exitCode !== 0) return false;
|
|
93
|
+
const parsed = v.safeParse(PaneResultSchema, JSON.parse(got.stdout.toString()));
|
|
94
|
+
|
|
95
|
+
return parsed.success && parsed.output.result?.pane != null;
|
|
96
|
+
} catch {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Bring an existing review tab to the front; best-effort. */
|
|
102
|
+
function focusHerdrTab(binPath: string, tabId: string): void {
|
|
103
|
+
try {
|
|
104
|
+
Bun.spawnSync([binPath, "tab", "focus", tabId], {
|
|
105
|
+
stdout: "ignore",
|
|
106
|
+
stderr: "ignore",
|
|
107
|
+
timeout: HERDR_SPAWN_TIMEOUT_MS,
|
|
108
|
+
});
|
|
109
|
+
} catch {
|
|
110
|
+
// best-effort
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Ensure a herdr tab is rendering this review. Inside herdr only: focus the
|
|
116
|
+
* recorded tab if its pane is still alive, otherwise open a fresh one and record
|
|
117
|
+
* its handle. No-op outside herdr.
|
|
118
|
+
*/
|
|
119
|
+
export async function openHerdrPaneForReview(
|
|
120
|
+
session: Thread,
|
|
121
|
+
persistence: HerdrTabPersistence,
|
|
122
|
+
env: HerdrEnv = process.env,
|
|
123
|
+
): Promise<void> {
|
|
124
|
+
const herdr = detectHerdr(env);
|
|
125
|
+
|
|
126
|
+
if (!herdr) return;
|
|
127
|
+
const recorded = await recallHerdrTab(persistence, session.id);
|
|
128
|
+
|
|
129
|
+
if (recorded && herdrPaneAlive(herdr.binPath, recorded.paneId)) {
|
|
130
|
+
focusHerdrTab(herdr.binPath, recorded.tabId);
|
|
131
|
+
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const opened = openHerdrPane({
|
|
135
|
+
sessionId: session.id,
|
|
136
|
+
cwd: session.artifact.meta.cwd ?? process.cwd(),
|
|
137
|
+
binPath: herdr.binPath,
|
|
138
|
+
label: session.artifact.meta.title ?? session.id,
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
if (opened) await rememberHerdrTab(persistence, session.id, opened);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The recorded handle, or null on any recall failure - a stale daemon must still open a fresh tab, not abort. */
|
|
145
|
+
async function recallHerdrTab(
|
|
146
|
+
persistence: HerdrTabPersistence,
|
|
147
|
+
sessionId: string,
|
|
148
|
+
): Promise<HerdrTabHandle | null> {
|
|
149
|
+
try {
|
|
150
|
+
return await persistence.herdrGetTab(sessionId);
|
|
151
|
+
} catch {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Record the handle for later liveness checks; a store failure loses dedup, never the already-open tab. */
|
|
157
|
+
async function rememberHerdrTab(
|
|
158
|
+
persistence: HerdrTabPersistence,
|
|
159
|
+
sessionId: string,
|
|
160
|
+
handle: HerdrTabHandle,
|
|
161
|
+
): Promise<void> {
|
|
162
|
+
try {
|
|
163
|
+
await persistence.herdrSetTab(sessionId, handle);
|
|
164
|
+
} catch {
|
|
165
|
+
// stale daemon: the tab is already shown; only the liveness-dedup handle is lost
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/** launchHarnessInSplit: a stubbed herdr binary logs its argv, and the launcher splits the current pane, runs the harness in the new pane, and optionally types the plan-context seed - a no-op outside herdr. */
|
|
2
|
+
|
|
3
|
+
import { afterAll, describe, expect, test } from "bun:test";
|
|
4
|
+
import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { launchHarnessInSplit } from "./herdr-split";
|
|
8
|
+
|
|
9
|
+
const dir = mkdtempSync(join(tmpdir(), "cueloop-herdr-split-"));
|
|
10
|
+
|
|
11
|
+
/** A stub herdr binary that logs argv and prints the pane-split result (pane_id of the new split). */
|
|
12
|
+
function makeStub(name: string, splitOk = true) {
|
|
13
|
+
const logPath = join(dir, `${name}.log`);
|
|
14
|
+
const binPath = join(dir, `${name}.sh`);
|
|
15
|
+
const splitResult = splitOk
|
|
16
|
+
? `printf '{"result":{"pane":{"pane_id":"w1:p9"}}}'`
|
|
17
|
+
: `printf '{"result":{}}'`;
|
|
18
|
+
|
|
19
|
+
writeFileSync(
|
|
20
|
+
binPath,
|
|
21
|
+
`#!/bin/sh
|
|
22
|
+
printf '%s\\n' "$*" >> "${logPath}"
|
|
23
|
+
if [ "$1" = "pane" ] && [ "$2" = "split" ]; then ${splitResult}; fi
|
|
24
|
+
exit 0
|
|
25
|
+
`,
|
|
26
|
+
);
|
|
27
|
+
chmodSync(binPath, 0o755);
|
|
28
|
+
|
|
29
|
+
return { binPath, logPath };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function readLines(logPath: string): string[] {
|
|
33
|
+
if (!existsSync(logPath)) return [];
|
|
34
|
+
|
|
35
|
+
return readFileSync(logPath, "utf8").split("\n").filter(Boolean);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe("launchHarnessInSplit", () => {
|
|
39
|
+
test("splits the current pane and runs the harness in the new pane", () => {
|
|
40
|
+
// Arrange
|
|
41
|
+
const stub = makeStub("run");
|
|
42
|
+
const env = { HERDR_ENV: "1", HERDR_PANE_ID: "w1:p1", HERDR_BIN_PATH: stub.binPath };
|
|
43
|
+
|
|
44
|
+
// Act
|
|
45
|
+
const launched = launchHarnessInSplit({ command: "cc", cwd: "/repo/work" }, env);
|
|
46
|
+
|
|
47
|
+
// Assert
|
|
48
|
+
expect(launched).toBeTrue();
|
|
49
|
+
expect(readLines(stub.logPath)).toEqual([
|
|
50
|
+
"pane split w1:p1 --direction right --ratio 0.4 --cwd /repo/work",
|
|
51
|
+
"pane send-text w1:p9 cc",
|
|
52
|
+
"pane send-keys w1:p9 enter",
|
|
53
|
+
]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("types the plan-context seed into the split after launching", () => {
|
|
57
|
+
// Arrange
|
|
58
|
+
const stub = makeStub("seeded");
|
|
59
|
+
const env = { HERDR_ENV: "1", HERDR_PANE_ID: "w1:p1", HERDR_BIN_PATH: stub.binPath };
|
|
60
|
+
|
|
61
|
+
// Act
|
|
62
|
+
launchHarnessInSplit({ command: "pi", cwd: "/repo", seedText: "read plan ses_1" }, env);
|
|
63
|
+
|
|
64
|
+
// Assert
|
|
65
|
+
expect(readLines(stub.logPath)).toEqual([
|
|
66
|
+
"pane split w1:p1 --direction right --ratio 0.4 --cwd /repo",
|
|
67
|
+
"pane send-text w1:p9 pi",
|
|
68
|
+
"pane send-keys w1:p9 enter",
|
|
69
|
+
"pane send-text w1:p9 read plan ses_1",
|
|
70
|
+
]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("no-op outside herdr - returns false and spawns nothing", () => {
|
|
74
|
+
// Arrange
|
|
75
|
+
const stub = makeStub("outside");
|
|
76
|
+
|
|
77
|
+
// Act
|
|
78
|
+
const launched = launchHarnessInSplit(
|
|
79
|
+
{ command: "cc", cwd: "/repo" },
|
|
80
|
+
{ HERDR_BIN_PATH: stub.binPath },
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
// Assert
|
|
84
|
+
expect(launched).toBeFalse();
|
|
85
|
+
expect(existsSync(stub.logPath)).toBeFalse();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("returns false when the split yields no pane id", () => {
|
|
89
|
+
// Arrange
|
|
90
|
+
const stub = makeStub("nopane", false);
|
|
91
|
+
const env = { HERDR_ENV: "1", HERDR_PANE_ID: "w1:p1", HERDR_BIN_PATH: stub.binPath };
|
|
92
|
+
|
|
93
|
+
// Act
|
|
94
|
+
const launched = launchHarnessInSplit({ command: "cc", cwd: "/repo" }, env);
|
|
95
|
+
|
|
96
|
+
// Assert - the split ran but no harness was launched into a missing pane
|
|
97
|
+
expect(launched).toBeFalse();
|
|
98
|
+
expect(readLines(stub.logPath)).toEqual([
|
|
99
|
+
"pane split w1:p1 --direction right --ratio 0.4 --cwd /repo",
|
|
100
|
+
]);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
afterAll(() => {
|
|
105
|
+
rmSync(dir, { recursive: true, force: true, maxRetries: 3 });
|
|
106
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Launches a bring-your-own review harness (claude code / pi / codex) in a herdr
|
|
3
|
+
* split beside the current pane - the Agent rail's "run it right here" spawn, so
|
|
4
|
+
* a reviewer chats with an agent about the plan without leaving the review tab.
|
|
5
|
+
* Best-effort and herdr-only: a no-op returning false outside herdr or on any
|
|
6
|
+
* spawn failure, each spawn bounded by a timeout.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { detectHerdr, type HerdrEnv } from "@cueloop/schema";
|
|
10
|
+
import * as v from "valibot";
|
|
11
|
+
|
|
12
|
+
const HERDR_SPAWN_TIMEOUT_MS = 2000;
|
|
13
|
+
const SplitPaneSchema = v.object({
|
|
14
|
+
result: v.optional(v.object({ pane: v.optional(v.object({ pane_id: v.optional(v.string()) })) })),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
/** How much of the current pane the launched harness split takes (herdr --ratio). */
|
|
18
|
+
const HARNESS_SPLIT_RATIO = "0.4";
|
|
19
|
+
|
|
20
|
+
export interface LaunchHarnessInSplitOptions {
|
|
21
|
+
/** The harness launch command run in the split, e.g. "cc", "pi", "codex". */
|
|
22
|
+
command: string;
|
|
23
|
+
/** Working directory for the split - the reviewed session's cwd. */
|
|
24
|
+
cwd: string;
|
|
25
|
+
/**
|
|
26
|
+
* A plan-context briefing typed into the split after the harness command, left
|
|
27
|
+
* unsubmitted for the reviewer to send. Best-effort: on a slow harness start it
|
|
28
|
+
* may reach the shell instead, so the launch never depends on it.
|
|
29
|
+
*/
|
|
30
|
+
seedText?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Split the current pane and run the harness in it. Returns true when the split
|
|
35
|
+
* opened, false on any best-effort bail-out. No-op outside herdr.
|
|
36
|
+
*/
|
|
37
|
+
export function launchHarnessInSplit(
|
|
38
|
+
options: LaunchHarnessInSplitOptions,
|
|
39
|
+
env: HerdrEnv = process.env,
|
|
40
|
+
): boolean {
|
|
41
|
+
const herdr = detectHerdr(env);
|
|
42
|
+
|
|
43
|
+
if (!herdr) return false;
|
|
44
|
+
try {
|
|
45
|
+
const split = Bun.spawnSync(
|
|
46
|
+
[
|
|
47
|
+
herdr.binPath,
|
|
48
|
+
"pane",
|
|
49
|
+
"split",
|
|
50
|
+
herdr.paneId,
|
|
51
|
+
"--direction",
|
|
52
|
+
"right",
|
|
53
|
+
"--ratio",
|
|
54
|
+
HARNESS_SPLIT_RATIO,
|
|
55
|
+
"--cwd",
|
|
56
|
+
options.cwd,
|
|
57
|
+
],
|
|
58
|
+
{ stdout: "pipe", stderr: "ignore", timeout: HERDR_SPAWN_TIMEOUT_MS },
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
if (split.exitCode !== 0) return false;
|
|
62
|
+
const parsed = v.safeParse(SplitPaneSchema, JSON.parse(split.stdout.toString()));
|
|
63
|
+
|
|
64
|
+
if (!parsed.success) return false;
|
|
65
|
+
const paneId = parsed.output.result?.pane?.pane_id;
|
|
66
|
+
|
|
67
|
+
if (!paneId) return false;
|
|
68
|
+
sendText(herdr.binPath, paneId, options.command);
|
|
69
|
+
sendKeys(herdr.binPath, paneId, "enter");
|
|
70
|
+
if (options.seedText) sendText(herdr.binPath, paneId, options.seedText);
|
|
71
|
+
|
|
72
|
+
return true;
|
|
73
|
+
} catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function sendText(binPath: string, paneId: string, text: string): void {
|
|
79
|
+
Bun.spawnSync([binPath, "pane", "send-text", paneId, text], {
|
|
80
|
+
stdout: "ignore",
|
|
81
|
+
stderr: "ignore",
|
|
82
|
+
timeout: HERDR_SPAWN_TIMEOUT_MS,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function sendKeys(binPath: string, paneId: string, keys: string): void {
|
|
87
|
+
Bun.spawnSync([binPath, "pane", "send-keys", paneId, keys], {
|
|
88
|
+
stdout: "ignore",
|
|
89
|
+
stderr: "ignore",
|
|
90
|
+
timeout: HERDR_SPAWN_TIMEOUT_MS,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/** herdr tab side-store: set/get/delete round-trips, survives a reload from disk, and ignores a corrupt file. */
|
|
2
|
+
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
4
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { HerdrTabStore } from "./herdr-tab-store";
|
|
8
|
+
import { herdrTabsPath } from "./paths";
|
|
9
|
+
|
|
10
|
+
let home: string;
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
home = mkdtempSync(join(tmpdir(), "cueloop-herdr-store-"));
|
|
14
|
+
});
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
rmSync(home, { recursive: true, force: true });
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe("HerdrTabStore", () => {
|
|
20
|
+
test("set then get returns the handle; get for an unknown session is null", () => {
|
|
21
|
+
// Arrange
|
|
22
|
+
const store = new HerdrTabStore(home);
|
|
23
|
+
|
|
24
|
+
// Act
|
|
25
|
+
store.set("ses_1", { tabId: "w1:t2", paneId: "w1:p2" });
|
|
26
|
+
|
|
27
|
+
// Assert
|
|
28
|
+
expect(store.get("ses_1")).toEqual({ tabId: "w1:t2", paneId: "w1:p2" });
|
|
29
|
+
expect(store.get("ses_missing")).toBeNull();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("handles survive a reload from disk", () => {
|
|
33
|
+
// Arrange
|
|
34
|
+
new HerdrTabStore(home).set("ses_1", { tabId: "w1:t2", paneId: "w1:p2" });
|
|
35
|
+
|
|
36
|
+
// Act - a fresh store over the same home loads the persisted file
|
|
37
|
+
const reloaded = new HerdrTabStore(home);
|
|
38
|
+
|
|
39
|
+
// Assert
|
|
40
|
+
expect(reloaded.get("ses_1")).toEqual({ tabId: "w1:t2", paneId: "w1:p2" });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("delete removes the handle", () => {
|
|
44
|
+
// Arrange
|
|
45
|
+
const store = new HerdrTabStore(home);
|
|
46
|
+
|
|
47
|
+
store.set("ses_1", { tabId: "w1:t2", paneId: "w1:p2" });
|
|
48
|
+
|
|
49
|
+
// Act
|
|
50
|
+
store.delete("ses_1");
|
|
51
|
+
|
|
52
|
+
// Assert
|
|
53
|
+
expect(store.get("ses_1")).toBeNull();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("a corrupt file starts empty instead of throwing", () => {
|
|
57
|
+
// Arrange
|
|
58
|
+
writeFileSync(herdrTabsPath(home), "not json");
|
|
59
|
+
|
|
60
|
+
// Act
|
|
61
|
+
const store = new HerdrTabStore(home);
|
|
62
|
+
|
|
63
|
+
// Assert
|
|
64
|
+
expect(store.get("ses_1")).toBeNull();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("malformed entries are dropped, well-formed ones survive", () => {
|
|
68
|
+
// Arrange - valid JSON, but entries with the wrong shape
|
|
69
|
+
writeFileSync(
|
|
70
|
+
herdrTabsPath(home),
|
|
71
|
+
JSON.stringify({
|
|
72
|
+
ses_ok: { tabId: "w1:t2", paneId: "w1:p2" },
|
|
73
|
+
ses_partial: { tabId: "w1:t2" },
|
|
74
|
+
ses_typed: { tabId: 1, paneId: 2 },
|
|
75
|
+
ses_null: null,
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// Act
|
|
80
|
+
const store = new HerdrTabStore(home);
|
|
81
|
+
|
|
82
|
+
// Assert
|
|
83
|
+
expect(store.get("ses_ok")).toEqual({ tabId: "w1:t2", paneId: "w1:p2" });
|
|
84
|
+
expect(store.get("ses_partial")).toBeNull();
|
|
85
|
+
expect(store.get("ses_typed")).toBeNull();
|
|
86
|
+
expect(store.get("ses_null")).toBeNull();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("a non-object top level starts empty", () => {
|
|
90
|
+
// Arrange
|
|
91
|
+
writeFileSync(herdrTabsPath(home), JSON.stringify(["not", "a", "map"]));
|
|
92
|
+
|
|
93
|
+
// Act & Assert
|
|
94
|
+
expect(new HerdrTabStore(home).get("ses_1")).toBeNull();
|
|
95
|
+
});
|
|
96
|
+
});
|