@cueloop/daemon 0.1.0-alpha.9 → 0.1.0-alpha.90

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.
Files changed (60) hide show
  1. package/package.json +39 -22
  2. package/src/api.test.ts +1060 -65
  3. package/src/api.ts +1398 -89
  4. package/src/capabilities.test.ts +57 -0
  5. package/src/capabilities.ts +89 -0
  6. package/src/client.test.ts +24 -0
  7. package/src/client.ts +669 -86
  8. package/src/curate.ts +109 -0
  9. package/src/daemon-version.test.ts +142 -0
  10. package/src/diff-hot-reload.test.ts +419 -0
  11. package/src/diff-watcher.ts +293 -0
  12. package/src/gh.test.ts +59 -0
  13. package/src/gh.ts +74 -0
  14. package/src/ghostty-thread-surface-store.test.ts +49 -0
  15. package/src/ghostty-thread-surface-store.ts +101 -0
  16. package/src/ghostty-thread-surface.test.ts +398 -0
  17. package/src/ghostty-thread-surface.ts +267 -0
  18. package/src/harness-delivery.test.ts +268 -0
  19. package/src/harness-state-store.test.ts +93 -0
  20. package/src/harness-state-store.ts +213 -0
  21. package/src/herdr-split.test.ts +106 -0
  22. package/src/herdr-split.ts +92 -0
  23. package/src/herdr-thread-surface-store.test.ts +81 -0
  24. package/src/herdr-thread-surface-store.ts +64 -0
  25. package/src/herdr-thread-surface.test.ts +411 -0
  26. package/src/herdr-thread-surface.ts +355 -0
  27. package/src/index.ts +8 -9
  28. package/src/interruptible-wait.test.ts +127 -0
  29. package/src/interruptible-wait.ts +66 -0
  30. package/src/jj-working-tree.ts +131 -0
  31. package/src/main.ts +4 -1
  32. package/src/paths.ts +49 -0
  33. package/src/pr-poller.test.ts +102 -0
  34. package/src/pr-poller.ts +73 -0
  35. package/src/project-files.test.ts +124 -0
  36. package/src/project-files.ts +56 -0
  37. package/src/protocol.ts +79 -7
  38. package/src/retention.test.ts +191 -0
  39. package/src/retention.ts +123 -0
  40. package/src/server.test.ts +453 -42
  41. package/src/server.ts +433 -88
  42. package/src/share-blob.test.ts +112 -0
  43. package/src/share-blob.ts +55 -0
  44. package/src/single-instance.test.ts +162 -13
  45. package/src/store.conformance.test.ts +41 -0
  46. package/src/store.jsonl.test.ts +159 -0
  47. package/src/store.ts +264 -34
  48. package/src/testing/store-conformance.ts +194 -0
  49. package/src/thread-review.test.ts +483 -0
  50. package/src/thread-review.ts +398 -0
  51. package/src/thread-surface-config.test.ts +63 -0
  52. package/src/thread-surface-config.ts +61 -0
  53. package/src/validate.test.ts +282 -37
  54. package/src/validate.ts +398 -33
  55. package/src/vcs-source.test.ts +180 -0
  56. package/src/vcs-source.ts +228 -0
  57. package/src/version.ts +6 -0
  58. package/src/working-tree.ts +149 -0
  59. package/src/review.test.ts +0 -167
  60. package/src/review.ts +0 -174
@@ -0,0 +1,268 @@
1
+ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { DaemonCore } from "./api";
6
+ import { harnessStatePath } from "./paths";
7
+
8
+ let home: string;
9
+ let cores: DaemonCore[];
10
+
11
+ beforeEach(() => {
12
+ home = mkdtempSync(join(tmpdir(), "cueloop-harness-delivery-"));
13
+ cores = [];
14
+ });
15
+
16
+ afterEach(() => {
17
+ for (const core of cores) core.dispose();
18
+ rmSync(home, { recursive: true, force: true });
19
+ });
20
+
21
+ function core(): DaemonCore {
22
+ const instance = new DaemonCore(home);
23
+
24
+ cores.push(instance);
25
+
26
+ return instance;
27
+ }
28
+
29
+ function createPlan(instance: DaemonCore) {
30
+ return instance.sessionCreate({
31
+ workspace: { repoRoot: "/repo", branch: "main" },
32
+ artifact: { type: "plan", content: "# Plan", meta: {} },
33
+ });
34
+ }
35
+
36
+ describe("durable harness delivery", () => {
37
+ test("delivers Comment without resolving the Thread", () => {
38
+ const instance = core();
39
+ const thread = createPlan(instance);
40
+ const binding = instance.harnessBind({
41
+ threadId: thread.id,
42
+ harness: "fake",
43
+ harnessSessionId: "fake_1",
44
+ });
45
+
46
+ const sent = instance.sessionSendMessage(thread.id, "comment", "Interim note.");
47
+ const pending = instance.deliveryPending(binding.id);
48
+
49
+ expect(sent.status).toBe("pending");
50
+ expect(pending).toHaveLength(1);
51
+ expect(pending[0]!.message.outcome).toBe("comment");
52
+ });
53
+
54
+ test("reload finds only bindings for the active harness conversation", () => {
55
+ const first = core();
56
+ const thread = createPlan(first);
57
+ const current = first.harnessBind({
58
+ threadId: thread.id,
59
+ harness: "pi",
60
+ harnessSessionId: "pi-current",
61
+ });
62
+
63
+ first.harnessBind({ threadId: thread.id, harness: "pi", harnessSessionId: "pi-other" });
64
+ first.harnessBind({ threadId: thread.id, harness: "codex", harnessSessionId: "pi-current" });
65
+
66
+ expect(core().harnessBindingsForSession("pi", "pi-current")).toEqual([current]);
67
+ });
68
+
69
+ test("Thread deletion removes pi, Codex, and Claude harness state", () => {
70
+ const instance = core();
71
+ const thread = createPlan(instance);
72
+ const bindings = ["pi", "codex", "claude-code"].map((harness) =>
73
+ instance.harnessBind({
74
+ threadId: thread.id,
75
+ harness,
76
+ harnessSessionId: `${harness}-session`,
77
+ }),
78
+ );
79
+
80
+ instance.sessionSendMessage(thread.id, "approved", "Ready.");
81
+ const deliveryId = instance.deliveryPending(bindings[0]!.id)[0]!.delivery.id;
82
+
83
+ instance.sessionDelete(thread.id);
84
+
85
+ for (const [index, harness] of ["pi", "codex", "claude-code"].entries()) {
86
+ expect(instance.harnessBindingsForSession(harness, `${harness}-session`)).toEqual([]);
87
+ expect(() => instance.harnessGetBinding(bindings[index]!.id)).toThrow("no harness binding");
88
+ }
89
+ expect(() => instance.deliveryAcknowledge(deliveryId)).toThrow("no delivery");
90
+ });
91
+
92
+ test("startup removes harness state orphaned by an older deletion", () => {
93
+ const first = core();
94
+ const thread = createPlan(first);
95
+ const binding = first.harnessBind({
96
+ threadId: thread.id,
97
+ harness: "codex",
98
+ harnessSessionId: "codex-orphan",
99
+ });
100
+
101
+ first.sessionSendMessage(thread.id, "approved", "Ready.");
102
+ const deliveryId = first.deliveryPending(binding.id)[0]!.delivery.id;
103
+
104
+ first.store.delete(thread.id);
105
+ const restarted = core();
106
+
107
+ expect(restarted.harnessBindingsForSession("codex", "codex-orphan")).toEqual([]);
108
+ expect(() => restarted.harnessGetBinding(binding.id)).toThrow("no harness binding");
109
+ expect(() => restarted.deliveryAcknowledge(deliveryId)).toThrow("no delivery");
110
+ });
111
+
112
+ test("redelivers after reload until the harness acknowledges the Message", () => {
113
+ const first = core();
114
+ const thread = createPlan(first);
115
+ const binding = first.harnessBind({
116
+ threadId: thread.id,
117
+ harness: "fake",
118
+ harnessSessionId: "fake_1",
119
+ });
120
+ const sent = first.sessionSendMessage(thread.id, "changes_requested", "Add detail.");
121
+ const initial = first.deliveryPending(binding.id);
122
+
123
+ expect(initial).toHaveLength(1);
124
+ expect(initial[0]!.message).toEqual(sent.message!);
125
+
126
+ const reloaded = core();
127
+ const redelivered = reloaded.deliveryPending(binding.id);
128
+
129
+ expect(redelivered).toEqual(initial);
130
+ reloaded.deliveryAcknowledge(redelivered[0]!.delivery.id);
131
+
132
+ expect(core().deliveryPending(binding.id)).toEqual([]);
133
+ });
134
+
135
+ test("retains an expired Thread until its pending Message is acknowledged", () => {
136
+ const first = core();
137
+ const thread = createPlan(first);
138
+ const binding = first.harnessBind({
139
+ threadId: thread.id,
140
+ harness: "fake",
141
+ harnessSessionId: "fake_1",
142
+ });
143
+
144
+ first.sessionSendMessage(thread.id, "approved", "Ready.");
145
+ const oldThread = first.sessionGet(thread.id);
146
+
147
+ first.store.upsert({ ...oldThread, createdAt: "2020-01-01T00:00:00.000Z" });
148
+
149
+ const reloaded = core();
150
+ const delivery = reloaded.deliveryPending(binding.id)[0];
151
+
152
+ expect(delivery?.message.body).toContain("Ready.");
153
+ reloaded.deliveryAcknowledge(delivery!.delivery.id);
154
+
155
+ expect(core().store.get(thread.id)).toBeUndefined();
156
+ });
157
+
158
+ test("rebuilds a delivery if the Thread saved its Message before enqueue", () => {
159
+ const first = core();
160
+ const thread = createPlan(first);
161
+ const binding = first.harnessBind({
162
+ threadId: thread.id,
163
+ harness: "fake",
164
+ harnessSessionId: "fake_1",
165
+ });
166
+ const sent = first.sessionSendMessage(thread.id, "approved", "Ready.");
167
+ first.store.upsert({
168
+ ...first.sessionGet(thread.id),
169
+ createdAt: "2020-01-01T00:00:00.000Z",
170
+ });
171
+
172
+ writeFileSync(harnessStatePath(home), JSON.stringify({ bindings: [binding], deliveries: [] }));
173
+
174
+ const recovered = core().deliveryPending(binding.id);
175
+
176
+ expect(recovered).toHaveLength(1);
177
+ expect(recovered[0]!.message).toEqual(sent.message!);
178
+ });
179
+
180
+ test("routes an approved retry as a new Message", () => {
181
+ const instance = core();
182
+ const thread = createPlan(instance);
183
+ const binding = instance.harnessBind({
184
+ threadId: thread.id,
185
+ harness: "fake",
186
+ harnessSessionId: "fake_1",
187
+ });
188
+
189
+ instance.sessionSendMessage(thread.id, "changes_requested", "Try once more.");
190
+ const first = instance.deliveryPending(binding.id)[0]!;
191
+
192
+ instance.deliveryAcknowledge(first.delivery.id);
193
+ instance.sessionSubmitRevision(thread.id, "# Plan");
194
+ instance.sessionSendMessage(thread.id, "approved", "Ready.");
195
+ const retry = instance.deliveryPending(binding.id);
196
+
197
+ expect(retry).toHaveLength(1);
198
+ expect(retry[0]!.message.outcome).toBe("approved");
199
+ expect(retry[0]!.message.id).not.toBe(first.message.id);
200
+ });
201
+
202
+ test("keeps an older pending Message available after a Thread revision", () => {
203
+ const instance = core();
204
+ const thread = createPlan(instance);
205
+ const binding = instance.harnessBind({
206
+ threadId: thread.id,
207
+ harness: "fake",
208
+ harnessSessionId: "fake_1",
209
+ });
210
+
211
+ instance.sessionSendMessage(thread.id, "changes_requested", "Add detail.");
212
+ const first = instance.deliveryPending(binding.id)[0]!;
213
+
214
+ instance.sessionSubmitRevision(thread.id, "# Revised plan");
215
+ instance.sessionSendMessage(thread.id, "approved", "Ready.");
216
+
217
+ const pending = core().deliveryPending(binding.id);
218
+
219
+ expect(pending.map((item) => item.message.id)).toEqual([first.message.id, expect.any(String)]);
220
+ expect(pending[0]!.message.body).toBe(first.message.body);
221
+ });
222
+
223
+ test("defaults delivery to the submitting binding only", () => {
224
+ const instance = core();
225
+ const thread = createPlan(instance);
226
+ const submitter = instance.harnessBind({
227
+ threadId: thread.id,
228
+ harness: "fake",
229
+ harnessSessionId: "fake_1",
230
+ });
231
+ const other = instance.harnessBind({
232
+ threadId: thread.id,
233
+ harness: "other",
234
+ harnessSessionId: "other_1",
235
+ });
236
+
237
+ instance.sessionSendMessage(thread.id, "approved", "Ready.");
238
+
239
+ expect(instance.deliveryPending(submitter.id)).toHaveLength(1);
240
+ expect(instance.deliveryPending(other.id)).toEqual([]);
241
+ });
242
+
243
+ test("consumes the unchanged approved retry once across daemon reload", () => {
244
+ const instance = core();
245
+ const thread = createPlan(instance);
246
+ const binding = instance.harnessBind({
247
+ threadId: thread.id,
248
+ harness: "fake",
249
+ harnessSessionId: "fake_1",
250
+ });
251
+ const sent = instance.sessionSendMessage(thread.id, "approved", "Ready.");
252
+ const messageId = sent.message!.id;
253
+
254
+ expect(() => instance.harnessConsumeApprovedRetry(binding.id, messageId, "# Plan")).toThrow(
255
+ "no unchanged approved plan",
256
+ );
257
+
258
+ const delivery = instance.deliveryPending(binding.id)[0]!.delivery;
259
+
260
+ instance.deliveryAcknowledge(delivery.id);
261
+
262
+ expect(instance.harnessConsumeApprovedRetry(binding.id, messageId, "# Plan")).toBe(true);
263
+ expect(core().harnessConsumeApprovedRetry(binding.id, messageId, "# Plan")).toBe(false);
264
+ expect(() => core().harnessConsumeApprovedRetry(binding.id, messageId, "# Changed")).toThrow(
265
+ "no unchanged approved plan",
266
+ );
267
+ });
268
+ });
@@ -0,0 +1,93 @@
1
+ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { HarnessStateStore } from "./harness-state-store";
6
+ import { harnessStatePath } from "./paths";
7
+
8
+ let home: string;
9
+
10
+ beforeEach(() => {
11
+ home = mkdtempSync(join(tmpdir(), "cueloop-harness-state-"));
12
+ });
13
+
14
+ afterEach(() => {
15
+ rmSync(home, { recursive: true, force: true });
16
+ });
17
+
18
+ describe("HarnessStateStore", () => {
19
+ test("redelivers an unacknowledged Message after reload", () => {
20
+ const store = new HarnessStateStore(home);
21
+ const binding = store.bind({
22
+ threadId: "ses_1",
23
+ harness: "fake",
24
+ harnessSessionId: "fake_1",
25
+ });
26
+ const delivery = store.enqueue({ bindingId: binding.id, messageId: "msg_1" });
27
+
28
+ expect(store.pending(binding.id)).toEqual([delivery]);
29
+
30
+ const reloaded = new HarnessStateStore(home);
31
+
32
+ expect(reloaded.pending(binding.id)).toEqual([delivery]);
33
+ });
34
+
35
+ test("does not redeliver an acknowledged Message", () => {
36
+ const store = new HarnessStateStore(home);
37
+ const binding = store.bind({
38
+ threadId: "ses_1",
39
+ harness: "fake",
40
+ harnessSessionId: "fake_1",
41
+ });
42
+ const delivery = store.enqueue({ bindingId: binding.id, messageId: "msg_1" });
43
+
44
+ store.acknowledge(delivery.id, "2026-09-20T20:00:00.000Z");
45
+
46
+ expect(new HarnessStateStore(home).pending(binding.id)).toEqual([]);
47
+ });
48
+
49
+ test("reuses the binding for the same harness session and thread", () => {
50
+ const store = new HarnessStateStore(home);
51
+ const input = {
52
+ threadId: "ses_1",
53
+ harness: "fake",
54
+ harnessSessionId: "fake_1",
55
+ };
56
+
57
+ expect(store.bind(input)).toEqual(store.bind(input));
58
+ });
59
+
60
+ test("deletes every harness binding and delivery for a Thread", () => {
61
+ const store = new HarnessStateStore(home);
62
+ const bindings = ["pi", "codex", "claude-code"].map((harness) =>
63
+ store.bind({ threadId: "ses_1", harness, harnessSessionId: `${harness}_1` }),
64
+ );
65
+ const deliveries = bindings.map((binding, index) =>
66
+ store.enqueue({ bindingId: binding.id, messageId: `msg_${index}` }),
67
+ );
68
+
69
+ expect(store.deleteThreadState("ses_1")).toBeTrue();
70
+ expect(store.deleteThreadState("ses_1")).toBeFalse();
71
+ const reloaded = new HarnessStateStore(home);
72
+
73
+ for (const binding of bindings) expect(reloaded.binding(binding.id)).toBeNull();
74
+ for (const delivery of deliveries) expect(reloaded.delivery(delivery.id)).toBeNull();
75
+ });
76
+
77
+ test("deletes state for Threads missing at startup", () => {
78
+ const store = new HarnessStateStore(home);
79
+
80
+ store.bind({ threadId: "ses_kept", harness: "pi", harnessSessionId: "pi_1" });
81
+ store.bind({ threadId: "ses_orphan", harness: "codex", harnessSessionId: "codex_1" });
82
+
83
+ expect(store.deleteOrphanedThreadState(new Set(["ses_kept"]))).toEqual(["ses_orphan"]);
84
+ expect(store.bindingsForSession("pi", "pi_1")).toHaveLength(1);
85
+ expect(store.bindingsForSession("codex", "codex_1")).toEqual([]);
86
+ });
87
+
88
+ test("does not discard pending deliveries when persisted state is corrupt", () => {
89
+ writeFileSync(harnessStatePath(home), "{broken");
90
+
91
+ expect(() => new HarnessStateStore(home)).toThrow();
92
+ });
93
+ });
@@ -0,0 +1,213 @@
1
+ import { readFileSync, renameSync, writeFileSync } from "node:fs";
2
+ import type { Delivery, HarnessBinding } from "@cueloop/schema";
3
+ import * as v from "valibot";
4
+ import { harnessStatePath } from "./paths";
5
+ import { DeliverySchema, HarnessBindingSchema } from "./validate";
6
+
7
+ type BindInput = Pick<HarnessBinding, "threadId" | "harness" | "harnessSessionId">;
8
+ type EnqueueInput = Pick<Delivery, "bindingId" | "messageId">;
9
+
10
+ const HarnessStateSchema = v.object({
11
+ bindings: v.array(HarnessBindingSchema),
12
+ deliveries: v.array(DeliverySchema),
13
+ });
14
+ const FileErrorSchema = v.object({ code: v.string() });
15
+
16
+ export class HarnessStateStore {
17
+ private readonly path: string;
18
+ private bindings = new Map<string, HarnessBinding>();
19
+ private deliveries = new Map<string, Delivery>();
20
+
21
+ constructor(home: string) {
22
+ this.path = harnessStatePath(home);
23
+ this.load();
24
+ }
25
+
26
+ bind(input: BindInput): HarnessBinding {
27
+ const existing = [...this.bindings.values()].find(
28
+ (binding) =>
29
+ binding.threadId === input.threadId &&
30
+ binding.harness === input.harness &&
31
+ binding.harnessSessionId === input.harnessSessionId,
32
+ );
33
+
34
+ if (existing) return existing;
35
+ const binding: HarnessBinding = {
36
+ ...input,
37
+ id: newRoutingId("bind"),
38
+ createdAt: new Date().toISOString(),
39
+ };
40
+
41
+ this.bindings.set(binding.id, binding);
42
+ this.persist();
43
+
44
+ return binding;
45
+ }
46
+
47
+ binding(id: string): HarnessBinding | null {
48
+ return this.bindings.get(id) ?? null;
49
+ }
50
+
51
+ bindingsForSession(harness: string, harnessSessionId: string): HarnessBinding[] {
52
+ return [...this.bindings.values()].filter(
53
+ (binding) => binding.harness === harness && binding.harnessSessionId === harnessSessionId,
54
+ );
55
+ }
56
+
57
+ deleteThreadState(threadId: string): boolean {
58
+ const bindingIds = new Set(
59
+ [...this.bindings.values()]
60
+ .filter((binding) => binding.threadId === threadId)
61
+ .map((binding) => binding.id),
62
+ );
63
+
64
+ return this.deleteBindingsAndDeliveries(bindingIds);
65
+ }
66
+
67
+ deleteOrphanedThreadState(existingThreadIds: ReadonlySet<string>): string[] {
68
+ const orphanedBindings = [...this.bindings.values()].filter(
69
+ (binding) => !existingThreadIds.has(binding.threadId),
70
+ );
71
+ const orphanedThreadIds = [...new Set(orphanedBindings.map((binding) => binding.threadId))];
72
+
73
+ this.deleteBindingsAndDeliveries(new Set(orphanedBindings.map((binding) => binding.id)));
74
+
75
+ return orphanedThreadIds;
76
+ }
77
+
78
+ private deleteBindingsAndDeliveries(bindingIds: ReadonlySet<string>): boolean {
79
+ if (bindingIds.size === 0) return false;
80
+ for (const bindingId of bindingIds) this.bindings.delete(bindingId);
81
+ for (const delivery of this.deliveries.values()) {
82
+ if (bindingIds.has(delivery.bindingId)) this.deliveries.delete(delivery.id);
83
+ }
84
+ this.persist();
85
+
86
+ return true;
87
+ }
88
+
89
+ consumeApprovedRetry(bindingId: string, messageId: string): boolean {
90
+ const binding = this.bindings.get(bindingId);
91
+
92
+ if (!binding) throw new Error(`no harness binding ${bindingId}`);
93
+
94
+ if (binding.approvedRetryMessageId === messageId) return false;
95
+
96
+ this.bindings.set(bindingId, { ...binding, approvedRetryMessageId: messageId });
97
+ this.persist();
98
+
99
+ return true;
100
+ }
101
+
102
+ /** The first binding is the submitter; other bindings need explicit future routing. */
103
+ submittingBinding(threadId: string): HarnessBinding | null {
104
+ return [...this.bindings.values()].find((binding) => binding.threadId === threadId) ?? null;
105
+ }
106
+
107
+ enqueue(input: EnqueueInput): Delivery {
108
+ const existing = [...this.deliveries.values()].find(
109
+ (delivery) =>
110
+ delivery.bindingId === input.bindingId && delivery.messageId === input.messageId,
111
+ );
112
+
113
+ if (existing) return existing;
114
+ const delivery: Delivery = {
115
+ ...input,
116
+ id: newRoutingId("del"),
117
+ status: "pending",
118
+ createdAt: new Date().toISOString(),
119
+ };
120
+
121
+ this.deliveries.set(delivery.id, delivery);
122
+ this.persist();
123
+
124
+ return delivery;
125
+ }
126
+
127
+ pending(bindingId: string): Delivery[] {
128
+ return [...this.deliveries.values()].filter(
129
+ (delivery) => delivery.bindingId === bindingId && delivery.status === "pending",
130
+ );
131
+ }
132
+
133
+ /** A pending delivery keeps its Thread's immutable Message body available for redelivery. */
134
+ pendingThreadIds(): Set<string> {
135
+ const ids = new Set<string>();
136
+
137
+ for (const delivery of this.deliveries.values()) {
138
+ if (delivery.status !== "pending") continue;
139
+ const binding = this.bindings.get(delivery.bindingId);
140
+
141
+ if (binding) ids.add(binding.threadId);
142
+ }
143
+
144
+ return ids;
145
+ }
146
+
147
+ acknowledged(bindingId: string, messageId: string): boolean {
148
+ return [...this.deliveries.values()].some(
149
+ (delivery) =>
150
+ delivery.bindingId === bindingId &&
151
+ delivery.messageId === messageId &&
152
+ delivery.status === "acknowledged",
153
+ );
154
+ }
155
+
156
+ delivery(deliveryId: string): Delivery | null {
157
+ return this.deliveries.get(deliveryId) ?? null;
158
+ }
159
+
160
+ acknowledge(deliveryId: string, acknowledgedAt = new Date().toISOString()): Delivery {
161
+ const delivery = this.deliveries.get(deliveryId);
162
+
163
+ if (!delivery) throw new Error(`no delivery ${deliveryId}`);
164
+
165
+ if (delivery.status === "acknowledged") return delivery;
166
+ const acknowledged: Delivery = {
167
+ ...delivery,
168
+ status: "acknowledged",
169
+ acknowledgedAt,
170
+ };
171
+
172
+ this.deliveries.set(deliveryId, acknowledged);
173
+ this.persist();
174
+
175
+ return acknowledged;
176
+ }
177
+
178
+ private load(): void {
179
+ let serialized: string;
180
+
181
+ try {
182
+ serialized = readFileSync(this.path, "utf8");
183
+ } catch (error) {
184
+ const parsed = v.safeParse(FileErrorSchema, error);
185
+
186
+ if (parsed.success && parsed.output.code === "ENOENT") {
187
+ return;
188
+ }
189
+
190
+ throw error;
191
+ }
192
+
193
+ const state = v.parse(HarnessStateSchema, JSON.parse(serialized));
194
+
195
+ this.bindings = new Map(state.bindings.map((binding) => [binding.id, binding]));
196
+ this.deliveries = new Map(state.deliveries.map((delivery) => [delivery.id, delivery]));
197
+ }
198
+
199
+ private persist(): void {
200
+ const temporaryPath = this.path + ".tmp";
201
+ const state = {
202
+ bindings: [...this.bindings.values()],
203
+ deliveries: [...this.deliveries.values()],
204
+ };
205
+
206
+ writeFileSync(temporaryPath, JSON.stringify(state, null, 2));
207
+ renameSync(temporaryPath, this.path);
208
+ }
209
+ }
210
+
211
+ function newRoutingId(prefix: "bind" | "del"): string {
212
+ return `${prefix}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
213
+ }
@@ -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
+ });