@cueloop/daemon 0.1.0-alpha.10 → 0.1.0-alpha.101

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 (83) hide show
  1. package/README.md +7 -0
  2. package/SDK.md +102 -0
  3. package/package.json +44 -22
  4. package/src/agent-tools.ts +98 -0
  5. package/src/agent-turn.test.ts +24 -0
  6. package/src/agent-turn.ts +34 -0
  7. package/src/api.test.ts +1081 -65
  8. package/src/api.ts +1425 -89
  9. package/src/capabilities.test.ts +57 -0
  10. package/src/capabilities.ts +95 -0
  11. package/src/client-errors.ts +24 -0
  12. package/src/client-reliability.test.ts +189 -0
  13. package/src/client.test.ts +37 -0
  14. package/src/client.ts +820 -94
  15. package/src/curate.ts +109 -0
  16. package/src/daemon-sdk-effect.ts +113 -0
  17. package/src/daemon-sdk.test.ts +306 -0
  18. package/src/daemon-sdk.ts +397 -0
  19. package/src/daemon-version.test.ts +142 -0
  20. package/src/diff-hot-reload.test.ts +419 -0
  21. package/src/diff-watcher.test.ts +117 -0
  22. package/src/diff-watcher.ts +326 -0
  23. package/src/examples/daemon-sdk.ts +55 -0
  24. package/src/gh.test.ts +59 -0
  25. package/src/gh.ts +74 -0
  26. package/src/ghostty-thread-surface-store.test.ts +49 -0
  27. package/src/ghostty-thread-surface-store.ts +101 -0
  28. package/src/ghostty-thread-surface.test.ts +398 -0
  29. package/src/ghostty-thread-surface.ts +267 -0
  30. package/src/harness-delivery.test.ts +269 -0
  31. package/src/harness-diagnostics.test.ts +26 -0
  32. package/src/harness-diagnostics.ts +30 -0
  33. package/src/harness-state-store.test.ts +93 -0
  34. package/src/harness-state-store.ts +213 -0
  35. package/src/herdr-split.test.ts +106 -0
  36. package/src/herdr-split.ts +85 -0
  37. package/src/herdr-thread-surface-store.test.ts +81 -0
  38. package/src/herdr-thread-surface-store.ts +66 -0
  39. package/src/herdr-thread-surface.test.ts +465 -0
  40. package/src/herdr-thread-surface.ts +378 -0
  41. package/src/index.ts +8 -9
  42. package/src/interruptible-wait.test.ts +127 -0
  43. package/src/interruptible-wait.ts +66 -0
  44. package/src/jj-working-tree.ts +131 -0
  45. package/src/main.ts +4 -1
  46. package/src/operation-receipts.test.ts +111 -0
  47. package/src/operation-receipts.ts +98 -0
  48. package/src/paths.ts +49 -0
  49. package/src/pr-poller.test.ts +102 -0
  50. package/src/pr-poller.ts +73 -0
  51. package/src/project-files.test.ts +134 -0
  52. package/src/project-files.ts +68 -0
  53. package/src/protocol.ts +78 -7
  54. package/src/retention.test.ts +191 -0
  55. package/src/retention.ts +123 -0
  56. package/src/sdk-identities.ts +35 -0
  57. package/src/server.test.ts +502 -42
  58. package/src/server.ts +631 -88
  59. package/src/share-blob.test.ts +119 -0
  60. package/src/share-blob.ts +57 -0
  61. package/src/shared-agent-protocol.ts +30 -0
  62. package/src/single-instance.test.ts +162 -13
  63. package/src/store.conformance.test.ts +41 -0
  64. package/src/store.jsonl.test.ts +173 -0
  65. package/src/store.ts +267 -34
  66. package/src/testing/store-conformance.ts +192 -0
  67. package/src/thread-agent-validation.ts +121 -0
  68. package/src/thread-agent.test.ts +719 -0
  69. package/src/thread-agent.ts +835 -0
  70. package/src/thread-review.test.ts +483 -0
  71. package/src/thread-review.ts +382 -0
  72. package/src/thread-subscription.test.ts +174 -0
  73. package/src/thread-subscription.ts +129 -0
  74. package/src/thread-surface-config.test.ts +63 -0
  75. package/src/thread-surface-config.ts +61 -0
  76. package/src/validate.test.ts +279 -37
  77. package/src/validate.ts +422 -33
  78. package/src/vcs-source.test.ts +180 -0
  79. package/src/vcs-source.ts +229 -0
  80. package/src/version.ts +6 -0
  81. package/src/working-tree.ts +150 -0
  82. package/src/review.test.ts +0 -167
  83. package/src/review.ts +0 -174
@@ -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,85 @@
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
+ command: string;
22
+ cwd: string;
23
+ seedText?: string;
24
+ }
25
+
26
+ /**
27
+ * Split the current pane and run the harness in it. Returns true when the split
28
+ * opened, false on any best-effort bail-out. No-op outside herdr.
29
+ */
30
+ export function launchHarnessInSplit(
31
+ options: LaunchHarnessInSplitOptions,
32
+ env: HerdrEnv = process.env,
33
+ ): boolean {
34
+ const herdr = detectHerdr(env);
35
+
36
+ if (!herdr) return false;
37
+ try {
38
+ const split = Bun.spawnSync(
39
+ [
40
+ herdr.binPath,
41
+ "pane",
42
+ "split",
43
+ herdr.paneId,
44
+ "--direction",
45
+ "right",
46
+ "--ratio",
47
+ HARNESS_SPLIT_RATIO,
48
+ "--cwd",
49
+ options.cwd,
50
+ ],
51
+ { stdout: "pipe", stderr: "ignore", timeout: HERDR_SPAWN_TIMEOUT_MS },
52
+ );
53
+
54
+ if (split.exitCode !== 0) return false;
55
+ const parsed = v.safeParse(SplitPaneSchema, JSON.parse(split.stdout.toString()));
56
+
57
+ if (!parsed.success) return false;
58
+ const paneId = parsed.output.result?.pane?.pane_id;
59
+
60
+ if (!paneId) return false;
61
+ sendText(herdr.binPath, paneId, options.command);
62
+ sendKeys(herdr.binPath, paneId, "enter");
63
+ if (options.seedText) sendText(herdr.binPath, paneId, options.seedText);
64
+
65
+ return true;
66
+ } catch {
67
+ return false;
68
+ }
69
+ }
70
+
71
+ function sendText(binPath: string, paneId: string, text: string): void {
72
+ Bun.spawnSync([binPath, "pane", "send-text", paneId, text], {
73
+ stdout: "ignore",
74
+ stderr: "ignore",
75
+ timeout: HERDR_SPAWN_TIMEOUT_MS,
76
+ });
77
+ }
78
+
79
+ function sendKeys(binPath: string, paneId: string, keys: string): void {
80
+ Bun.spawnSync([binPath, "pane", "send-keys", paneId, keys], {
81
+ stdout: "ignore",
82
+ stderr: "ignore",
83
+ timeout: HERDR_SPAWN_TIMEOUT_MS,
84
+ });
85
+ }
@@ -0,0 +1,81 @@
1
+ /** Herdr Thread-surface handles survive reload and reject corrupt records. */
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 { HerdrThreadSurfaceStore } from "./herdr-thread-surface-store";
8
+ import { herdrThreadSurfacesPath } 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("HerdrThreadSurfaceStore", () => {
20
+ test("set then get returns the handle; get for an unknown session is null", () => {
21
+ const store = new HerdrThreadSurfaceStore(home);
22
+
23
+ store.set("ses_1", { tabId: "w1:t2", paneId: "w1:p2" });
24
+
25
+ expect(store.get("ses_1")).toEqual({ tabId: "w1:t2", paneId: "w1:p2" });
26
+ expect(store.get("ses_missing")).toBeNull();
27
+ });
28
+
29
+ test("handles survive a reload from disk", () => {
30
+ new HerdrThreadSurfaceStore(home).set("ses_1", { tabId: "w1:t2", paneId: "w1:p2" });
31
+
32
+ const reloaded = new HerdrThreadSurfaceStore(home);
33
+
34
+ expect(reloaded.get("ses_1")).toEqual({ tabId: "w1:t2", paneId: "w1:p2" });
35
+ });
36
+
37
+ test("delete removes the handle", () => {
38
+ const store = new HerdrThreadSurfaceStore(home);
39
+
40
+ store.set("ses_1", { tabId: "w1:t2", paneId: "w1:p2" });
41
+
42
+ store.delete("ses_1");
43
+
44
+ expect(store.get("ses_1")).toBeNull();
45
+ });
46
+
47
+ test("a corrupt file starts empty instead of throwing", () => {
48
+ writeFileSync(herdrThreadSurfacesPath(home), "not json");
49
+
50
+ const store = new HerdrThreadSurfaceStore(home);
51
+
52
+ expect(store.get("ses_1")).toBeNull();
53
+ });
54
+
55
+ test("malformed entries are dropped, well-formed ones survive", () => {
56
+ writeFileSync(
57
+ herdrThreadSurfacesPath(home),
58
+ JSON.stringify({
59
+ ses_ok: { tabId: "w1:t2", paneId: "w1:p2" },
60
+ ses_partial: { tabId: "w1:t2" },
61
+ ses_typed: { tabId: 1, paneId: 2 },
62
+ ses_invalid_mode: { tabId: "w1:t2", paneId: "w1:p3", mode: "window" },
63
+ ses_null: null,
64
+ }),
65
+ );
66
+
67
+ const store = new HerdrThreadSurfaceStore(home);
68
+
69
+ expect(store.get("ses_ok")).toEqual({ tabId: "w1:t2", paneId: "w1:p2" });
70
+ expect(store.get("ses_partial")).toBeNull();
71
+ expect(store.get("ses_typed")).toBeNull();
72
+ expect(store.get("ses_invalid_mode")).toBeNull();
73
+ expect(store.get("ses_null")).toBeNull();
74
+ });
75
+
76
+ test("a non-object top level starts empty", () => {
77
+ writeFileSync(herdrThreadSurfacesPath(home), JSON.stringify(["not", "a", "map"]));
78
+
79
+ expect(new HerdrThreadSurfaceStore(home).get("ses_1")).toBeNull();
80
+ });
81
+ });
@@ -0,0 +1,66 @@
1
+ import { readFileSync, renameSync, writeFileSync } from "node:fs";
2
+ import * as v from "valibot";
3
+ import { herdrThreadSurfacesPath } from "./paths";
4
+
5
+ /** Herdr-native handle, stored outside the canonical Thread. */
6
+ export interface HerdrThreadSurfaceHandle {
7
+ mode?: "tab" | "pane";
8
+ layout?: "pair";
9
+ tabId: string;
10
+ paneId: string;
11
+ }
12
+
13
+ const HerdrThreadSurfaceHandleSchema = v.object({
14
+ tabId: v.string(),
15
+ paneId: v.string(),
16
+ mode: v.optional(v.picklist(["tab", "pane"])),
17
+ layout: v.optional(v.literal("pair")),
18
+ });
19
+ const HerdrThreadSurfaceMapSchema = v.record(v.string(), v.unknown());
20
+
21
+ export class HerdrThreadSurfaceStore {
22
+ private surfaces = new Map<string, HerdrThreadSurfaceHandle>();
23
+ private readonly path: string;
24
+
25
+ constructor(home: string) {
26
+ this.path = herdrThreadSurfacesPath(home);
27
+ this.load();
28
+ }
29
+
30
+ private load(): void {
31
+ try {
32
+ const parsed = v.parse(
33
+ HerdrThreadSurfaceMapSchema,
34
+ JSON.parse(readFileSync(this.path, "utf8")),
35
+ );
36
+
37
+ for (const [sessionId, value] of Object.entries(parsed)) {
38
+ const handle = v.safeParse(HerdrThreadSurfaceHandleSchema, value);
39
+
40
+ if (handle.success) this.surfaces.set(sessionId, handle.output);
41
+ }
42
+ } catch {
43
+ // A missing or corrupt side-store cannot block a pending Thread.
44
+ }
45
+ }
46
+
47
+ private persist(): void {
48
+ const tempPath = this.path + ".tmp";
49
+
50
+ writeFileSync(tempPath, JSON.stringify(Object.fromEntries(this.surfaces), null, 2));
51
+ renameSync(tempPath, this.path);
52
+ }
53
+
54
+ get(sessionId: string): HerdrThreadSurfaceHandle | null {
55
+ return this.surfaces.get(sessionId) ?? null;
56
+ }
57
+
58
+ set(sessionId: string, handle: HerdrThreadSurfaceHandle): void {
59
+ this.surfaces.set(sessionId, handle);
60
+ this.persist();
61
+ }
62
+
63
+ delete(sessionId: string): void {
64
+ if (this.surfaces.delete(sessionId)) this.persist();
65
+ }
66
+ }