@cueloop/daemon 0.1.0-alpha.20 → 0.1.0-alpha.21

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 CHANGED
@@ -1,15 +1,16 @@
1
1
  {
2
2
  "name": "@cueloop/daemon",
3
- "version": "0.1.0-alpha.20",
3
+ "version": "0.1.0-alpha.21",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./src/index.ts",
8
8
  "./client": "./src/client.ts",
9
- "./review": "./src/review.ts"
9
+ "./review": "./src/review.ts",
10
+ "./herdr-pane": "./src/herdr-pane.ts"
10
11
  },
11
12
  "dependencies": {
12
- "@cueloop/schema": "0.1.0-alpha.20",
13
+ "@cueloop/schema": "0.1.0-alpha.21",
13
14
  "valibot": "^1.4.2"
14
15
  },
15
16
  "publishConfig": {
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Creation-side herdr pane opener tests. The herdr binary is stubbed by a
3
+ * script that both records its argv to a log file and, for `tab create`,
4
+ * prints the JSON the helper reads to learn the new pane id. The gating
5
+ * wrapper is exercised for its three outcomes: no-op outside herdr, no-op for
6
+ * a revision (the open pane is reused), and a full open for a genuinely new
7
+ * session.
8
+ */
9
+
10
+ import { afterAll, describe, expect, test } from "bun:test";
11
+ import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
12
+ import { tmpdir } from "node:os";
13
+ import { join } from "node:path";
14
+ import type { ReviewSession } from "@cueloop/schema";
15
+ import { openHerdrPane, openHerdrPaneForReview } from "./herdr-pane";
16
+
17
+ const dir = mkdtempSync(join(tmpdir(), "cueloop-herdr-pane-"));
18
+
19
+ /**
20
+ * A stub herdr binary. It appends its argv to logPath and, when the verb is
21
+ * `tab create`, prints a JSON result carrying paneId on stdout so the helper
22
+ * can drive send-text and send-keys.
23
+ */
24
+ function makeStub(name: string, paneId = "w1:p2"): { binPath: string; logPath: string } {
25
+ const logPath = join(dir, `${name}.log`);
26
+ const binPath = join(dir, `${name}.sh`);
27
+ writeFileSync(
28
+ binPath,
29
+ `#!/bin/sh\nprintf '%s\\n' "$*" >> "${logPath}"\nif [ "$1" = "tab" ] && [ "$2" = "create" ]; then\n printf '{"result":{"pane":{"id":"${paneId}"}}}'\nfi\n`,
30
+ );
31
+ chmodSync(binPath, 0o755);
32
+ return { binPath, logPath };
33
+ }
34
+
35
+ function readLines(logPath: string): string[] {
36
+ if (!existsSync(logPath)) return [];
37
+ return readFileSync(logPath, "utf8").split("\n").filter(Boolean);
38
+ }
39
+
40
+ function newSession(overrides: Partial<ReviewSession> = {}): ReviewSession {
41
+ return {
42
+ schemaVersion: "1",
43
+ id: "ses_new1",
44
+ workspace: { repoRoot: "/repo", branch: "main" },
45
+ artifact: { type: "plan", content: "# P", meta: { cwd: "/repo/work", title: "Rollout Plan" } },
46
+ revisions: [{ revision: 1, content: "# P", submittedAt: "now" }],
47
+ annotations: [],
48
+ verdict: null,
49
+ status: "pending",
50
+ createdAt: "now",
51
+ ...overrides,
52
+ };
53
+ }
54
+
55
+ describe("openHerdrPane", () => {
56
+ test("creates a focused tab, types the cueloop command, and presses enter", () => {
57
+ const stub = makeStub("open");
58
+ const ok = openHerdrPane({ sessionId: "ses_abc", cwd: "/repo/work", binPath: stub.binPath, label: "Rollout Plan" });
59
+ expect(ok).toBeTrue();
60
+ expect(readLines(stub.logPath)).toEqual([
61
+ "tab create --cwd /repo/work --label Rollout Plan --focus",
62
+ "pane send-text w1:p2 cueloop ses_abc",
63
+ "pane send-keys w1:p2 enter",
64
+ ]);
65
+ });
66
+
67
+ test("returns false and never throws on a broken binary", () => {
68
+ const ok = openHerdrPane({
69
+ sessionId: "ses_abc",
70
+ cwd: "/repo/work",
71
+ binPath: join(dir, "missing-bin"),
72
+ label: "x",
73
+ });
74
+ expect(ok).toBeFalse();
75
+ });
76
+
77
+ test("bails out when tab create yields no pane id - no send-text, no send-keys", () => {
78
+ const logPath = join(dir, "nopane.log");
79
+ const binPath = join(dir, "nopane.sh");
80
+ writeFileSync(binPath, `#!/bin/sh\nprintf '%s\\n' "$*" >> "${logPath}"\nprintf '{"result":{}}'\n`);
81
+ chmodSync(binPath, 0o755);
82
+ const ok = openHerdrPane({ sessionId: "ses_abc", cwd: "/repo/work", binPath, label: "x" });
83
+ expect(ok).toBeFalse();
84
+ expect(readLines(logPath)).toEqual(["tab create --cwd /repo/work --label x --focus"]);
85
+ });
86
+ });
87
+
88
+ describe("openHerdrPaneForReview", () => {
89
+ test("opens a pane for a genuinely new session inside herdr", () => {
90
+ const stub = makeStub("gated-new");
91
+ const env = { HERDR_ENV: "1", HERDR_PANE_ID: "w1:p1", HERDR_BIN_PATH: stub.binPath };
92
+ openHerdrPaneForReview(newSession({ id: "ses_xyz" }), env);
93
+ expect(readLines(stub.logPath)).toEqual([
94
+ "tab create --cwd /repo/work --label Rollout Plan --focus",
95
+ "pane send-text w1:p2 cueloop ses_xyz",
96
+ "pane send-keys w1:p2 enter",
97
+ ]);
98
+ });
99
+
100
+ test("no-op outside herdr - no herdr process is spawned", () => {
101
+ const stub = makeStub("gated-outside");
102
+ openHerdrPaneForReview(newSession(), { HERDR_PANE_ID: "w1:p1", HERDR_BIN_PATH: stub.binPath });
103
+ expect(existsSync(stub.logPath)).toBeFalse();
104
+ });
105
+
106
+ test("no-op for a revision - the already open pane is reused, not spammed", () => {
107
+ const stub = makeStub("gated-revision");
108
+ const env = { HERDR_ENV: "1", HERDR_PANE_ID: "w1:p1", HERDR_BIN_PATH: stub.binPath };
109
+ const revised = newSession({
110
+ revisions: [
111
+ { revision: 1, content: "# P", submittedAt: "now" },
112
+ { revision: 2, content: "# P2", submittedAt: "now" },
113
+ ],
114
+ });
115
+ openHerdrPaneForReview(revised, env);
116
+ expect(existsSync(stub.logPath)).toBeFalse();
117
+ });
118
+ });
119
+
120
+ afterAll(() => {
121
+ rmSync(dir, { recursive: true, force: true, maxRetries: 3 });
122
+ });
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Creation-side herdr pane opener: when a review is created from inside a
3
+ * herdr pane, cueloop opens a NEW herdr tab that renders the review, so the
4
+ * creator does not have to run a command by hand. This is the counterpart to
5
+ * the reviewer-side focusHerdrPane in @cueloop/client. It lives here, beside
6
+ * the shared review core (openReview), because both creation sites - the
7
+ * Claude Code hook and the `cueloop session create` CLI - already depend on
8
+ * this package and drive creation through it, so a single home reaches both
9
+ * with no new cross-package dependency.
10
+ *
11
+ * A fresh tab hosts a plain shell with no command argument, so the review is
12
+ * launched exactly as a human would: type `cueloop <sessionId>` into the new
13
+ * pane and press enter. The herdr CLI is spawned synchronously because the
14
+ * send-text and send-keys steps need the pane id that `tab create` returns.
15
+ *
16
+ * Best-effort like the rest of the herdr tier: a missing or broken binary, an
17
+ * unexpected JSON shape, or any spawn failure is swallowed and never blocks
18
+ * review creation. Each spawn is bounded by HERDR_SPAWN_TIMEOUT_MS so a hung
19
+ * herdr binary cannot stall the review-creation path - a timeout kills the
20
+ * child and reads as a non-zero exit, i.e. a best-effort bail-out.
21
+ */
22
+
23
+ import { detectHerdr, type HerdrEnv, type ReviewSession } from "@cueloop/schema";
24
+
25
+ /** Upper bound per herdr spawn, so a stuck binary can never block creation. */
26
+ const HERDR_SPAWN_TIMEOUT_MS = 2000;
27
+
28
+ export interface OpenHerdrPaneOptions {
29
+ /** The review session id; `cueloop <sessionId>` opens it in the new pane. */
30
+ sessionId: string;
31
+ /** Working directory for the new tab; the review's cwd. */
32
+ cwd: string;
33
+ /** Absolute path to the herdr binary, from the ambient env contract. */
34
+ binPath: string;
35
+ /** Sidebar label for the new tab. */
36
+ label: string;
37
+ }
38
+
39
+ /**
40
+ * Open a herdr tab that renders the review. `tab create` prints a JSON result
41
+ * shaped like `{ result: { pane: { id } } }` - the same `result.pane` nesting
42
+ * focusHerdrPane reads - and the pane id drives the two follow-up steps.
43
+ * Returns true when the full open-and-launch sequence completed, false on any
44
+ * best-effort bail-out.
45
+ */
46
+ export function openHerdrPane(options: OpenHerdrPaneOptions): boolean {
47
+ const { sessionId, cwd, binPath, label } = options;
48
+ try {
49
+ const created = Bun.spawnSync([binPath, "tab", "create", "--cwd", cwd, "--label", label, "--focus"], {
50
+ stdout: "pipe",
51
+ stderr: "ignore",
52
+ timeout: HERDR_SPAWN_TIMEOUT_MS,
53
+ });
54
+ if (created.exitCode !== 0) return false;
55
+ const parsed = JSON.parse(created.stdout.toString()) as { result?: { pane?: { id?: string } } };
56
+ const paneId = parsed.result?.pane?.id;
57
+ if (!paneId) return false;
58
+ // A fresh tab hosts a plain shell, so the review is typed in like a human.
59
+ const typed = Bun.spawnSync([binPath, "pane", "send-text", paneId, `cueloop ${sessionId}`], {
60
+ stdout: "ignore",
61
+ stderr: "ignore",
62
+ timeout: HERDR_SPAWN_TIMEOUT_MS,
63
+ });
64
+ if (typed.exitCode !== 0) return false;
65
+ return (
66
+ Bun.spawnSync([binPath, "pane", "send-keys", paneId, "enter"], {
67
+ stdout: "ignore",
68
+ stderr: "ignore",
69
+ timeout: HERDR_SPAWN_TIMEOUT_MS,
70
+ }).exitCode === 0
71
+ );
72
+ } catch {
73
+ return false;
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Open a pane for a freshly created review, guarded for the one case that
79
+ * wants it: inside herdr and genuinely new. A revision reuses the pane the
80
+ * original review already opened, so a resubmit (revisions beyond the first)
81
+ * never spawns another tab. A no-op outside herdr keeps a plain terminal
82
+ * byte-identical.
83
+ */
84
+ export function openHerdrPaneForReview(session: ReviewSession, env: HerdrEnv = process.env): void {
85
+ const herdr = detectHerdr(env);
86
+ if (!herdr) return;
87
+ if (session.revisions.length !== 1) return;
88
+ openHerdrPane({
89
+ sessionId: session.id,
90
+ cwd: session.artifact.meta.cwd ?? process.cwd(),
91
+ binPath: herdr.binPath,
92
+ label: session.artifact.meta.title ?? session.id,
93
+ });
94
+ }