@cueloop/daemon 0.1.0-alpha.5 → 0.1.0-alpha.50

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.
@@ -0,0 +1,67 @@
1
+ /**
2
+ * herdr adapter scratch, kept out of the core session record: the tab cueloop
3
+ * opened to render each review, keyed by session id. One JSON map, atomic write.
4
+ * The core `ReviewSession` never mentions herdr; this side-store is the whole
5
+ * leak boundary.
6
+ */
7
+
8
+ import { readFileSync, renameSync, writeFileSync } from "node:fs";
9
+ import { herdrTabsPath } from "./paths";
10
+
11
+ /** The tab and its root pane: pane id checks liveness, tab id focuses it. */
12
+ export interface HerdrTabHandle {
13
+ tabId: string;
14
+ paneId: string;
15
+ }
16
+
17
+ /** Guard a persisted entry - both ids must be present strings, or it is dropped. */
18
+ function isHerdrTabHandle(value: unknown): value is HerdrTabHandle {
19
+ return (
20
+ typeof value === "object" &&
21
+ value !== null &&
22
+ typeof (value as HerdrTabHandle).tabId === "string" &&
23
+ typeof (value as HerdrTabHandle).paneId === "string"
24
+ );
25
+ }
26
+
27
+ export class HerdrTabStore {
28
+ private tabs = new Map<string, HerdrTabHandle>();
29
+ private readonly path: string;
30
+
31
+ constructor(home: string) {
32
+ this.path = herdrTabsPath(home);
33
+ this.load();
34
+ }
35
+
36
+ private load(): void {
37
+ try {
38
+ const parsed: unknown = JSON.parse(readFileSync(this.path, "utf8"));
39
+ if (typeof parsed !== "object" || parsed === null) return;
40
+ for (const [sessionId, value] of Object.entries(parsed)) {
41
+ if (isHerdrTabHandle(value))
42
+ this.tabs.set(sessionId, { tabId: value.tabId, paneId: value.paneId });
43
+ }
44
+ } catch {
45
+ // absent, unreadable, or malformed: start empty - a stale tab just reopens on resubmit
46
+ }
47
+ }
48
+
49
+ private persist(): void {
50
+ const tempPath = this.path + ".tmp";
51
+ writeFileSync(tempPath, JSON.stringify(Object.fromEntries(this.tabs), null, 2));
52
+ renameSync(tempPath, this.path);
53
+ }
54
+
55
+ get(sessionId: string): HerdrTabHandle | null {
56
+ return this.tabs.get(sessionId) ?? null;
57
+ }
58
+
59
+ set(sessionId: string, handle: HerdrTabHandle): void {
60
+ this.tabs.set(sessionId, handle);
61
+ this.persist();
62
+ }
63
+
64
+ delete(sessionId: string): void {
65
+ if (this.tabs.delete(sessionId)) this.persist();
66
+ }
67
+ }
package/src/index.ts CHANGED
@@ -1,8 +1,8 @@
1
- export * from "./api";
2
- export * from "./errors";
3
- export * from "./validate";
4
- export * from "./store";
5
- export * from "./server";
6
- export * from "./protocol";
7
- export * from "./paths";
8
- export { DaemonClient, DaemonClientError, type ConnectOptions } from "./client";
1
+ /**
2
+ * The daemon's public surface. Consumers reach the client and the review
3
+ * helpers through the ./client and ./review subpaths; this barrel carries
4
+ * only the two names imported bare: the server and the home-dir resolver.
5
+ */
6
+
7
+ export { DaemonServer, type DaemonOptions } from "./server";
8
+ export { cueloopHome } from "./paths";
package/src/main.ts CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  import { DaemonServer } from "./server";
4
4
 
5
- const idleExitMs = process.env.CUELOOP_IDLE_EXIT_MS ? Number(process.env.CUELOOP_IDLE_EXIT_MS) : undefined;
5
+ const idleExitMs = process.env.CUELOOP_IDLE_EXIT_MS
6
+ ? Number(process.env.CUELOOP_IDLE_EXIT_MS)
7
+ : undefined;
6
8
  const server = new DaemonServer({ idleExitMs });
7
9
  const path = server.start();
8
10
  if (path === null) {
package/src/paths.ts CHANGED
@@ -18,6 +18,10 @@ export function sessionsDir(home = cueloopHome()): string {
18
18
  return join(home, "sessions");
19
19
  }
20
20
 
21
+ export function reportsDir(home = cueloopHome()): string {
22
+ return join(home, "reports");
23
+ }
24
+
21
25
  export function pidPath(home = cueloopHome()): string {
22
26
  return join(home, "cueloop.pid");
23
27
  }
@@ -25,3 +29,8 @@ export function pidPath(home = cueloopHome()): string {
25
29
  export function lockPath(home = cueloopHome()): string {
26
30
  return join(home, "cueloop.lock");
27
31
  }
32
+
33
+ /** Adapter scratch: herdr tab handles keyed by session id, kept out of the core session record. */
34
+ export function herdrTabsPath(home = cueloopHome()): string {
35
+ return join(home, "herdr-tabs.json");
36
+ }
package/src/protocol.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Wire protocol (#14): newline-delimited JSON over the unix socket.
2
+ * Wire protocol: newline-delimited JSON over the unix socket.
3
3
  * Requests: { id, method, params } Responses: { id, result } | { id, error }
4
4
  * Events (push, after events.subscribe): { event, sessionId }
5
5
  */
@@ -25,16 +25,53 @@ export type Frame = Response | EventFrame;
25
25
 
26
26
  /** Incremental NDJSON splitter; tolerates partial writes. */
27
27
  export class LineBuffer {
28
- private buf = "";
28
+ private buffered = "";
29
29
 
30
30
  push(chunk: string, onLine: (line: string) => void): void {
31
- this.buf += chunk;
31
+ this.buffered += chunk;
32
32
  for (;;) {
33
- const nl = this.buf.indexOf("\n");
34
- if (nl === -1) return;
35
- const line = this.buf.slice(0, nl).trim();
36
- this.buf = this.buf.slice(nl + 1);
33
+ const newlineIndex = this.buffered.indexOf("\n");
34
+ if (newlineIndex === -1) return;
35
+ const line = this.buffered.slice(0, newlineIndex).trim();
36
+ this.buffered = this.buffered.slice(newlineIndex + 1);
37
37
  if (line) onLine(line);
38
38
  }
39
39
  }
40
40
  }
41
+
42
+ /** The surface both Bun.listen and Bun.connect sockets satisfy. */
43
+ interface WritableSocket {
44
+ write(data: Buffer): number;
45
+ }
46
+
47
+ /**
48
+ * Outbound writes honoring socket backpressure. A frame larger than the
49
+ * kernel buffer is written partially by the socket; the unwritten tail must
50
+ * wait for the drain callback or the frame truncates mid-line and the NDJSON
51
+ * stream corrupts for good. Frames never reorder: while a tail is pending,
52
+ * new writes append behind it.
53
+ */
54
+ export class BackpressureWriter {
55
+ private pending: Buffer | null = null;
56
+
57
+ constructor(private socket: WritableSocket) {}
58
+
59
+ write(data: string): void {
60
+ const bytes = Buffer.from(data, "utf8");
61
+ if (this.pending) {
62
+ this.pending = Buffer.concat([this.pending, bytes]);
63
+ return;
64
+ }
65
+ const written = Math.max(0, this.socket.write(bytes));
66
+ if (written < bytes.length) this.pending = bytes.subarray(written);
67
+ }
68
+
69
+ /** Call from the socket's drain handler. */
70
+ drain(): void {
71
+ if (!this.pending) return;
72
+ const tail = this.pending;
73
+ this.pending = null;
74
+ const written = Math.max(0, this.socket.write(tail));
75
+ if (written < tail.length) this.pending = tail.subarray(written);
76
+ }
77
+ }
@@ -0,0 +1,159 @@
1
+ /** Retention: the cleanup period defaults to 30, honors [cleanup] period_days, and prune drops only the sessions and reports past the window. */
2
+
3
+ import { describe, expect, test } from "bun:test";
4
+ import { mkdirSync, mkdtempSync, utimesSync, writeFileSync } from "node:fs";
5
+ import { tmpdir } from "node:os";
6
+ import { join } from "node:path";
7
+ import type { ReviewSession, SessionStatus } from "@cueloop/schema";
8
+ import { SessionStore } from "./store";
9
+ import { reportsDir } from "./paths";
10
+ import {
11
+ DEFAULT_CLEANUP_PERIOD_DAYS,
12
+ LATEST_REPORT_FILENAME,
13
+ isExpired,
14
+ pruneExpiredReports,
15
+ pruneExpiredSessions,
16
+ resolveCleanupPeriodDays,
17
+ } from "./retention";
18
+
19
+ const DAY_MS = 24 * 60 * 60 * 1000;
20
+ const NOW_MS = Date.parse("2026-08-26T00:00:00Z");
21
+
22
+ function tempDir(prefix: string): string {
23
+ return mkdtempSync(join(tmpdir(), prefix));
24
+ }
25
+
26
+ function daysAgo(days: number): string {
27
+ return new Date(NOW_MS - days * DAY_MS).toISOString();
28
+ }
29
+
30
+ function session(id: string, createdAt: string, status: SessionStatus = "resolved"): ReviewSession {
31
+ return {
32
+ schemaVersion: "1",
33
+ id,
34
+ workspace: { repoRoot: "/repo", branch: "main" },
35
+ artifact: { type: "plan", content: "", meta: {} },
36
+ revisions: [],
37
+ annotations: [],
38
+ verdict: null,
39
+ status,
40
+ createdAt,
41
+ };
42
+ }
43
+
44
+ describe("resolveCleanupPeriodDays", () => {
45
+ test("defaults to 30 when no config file is present", () => {
46
+ // Arrange
47
+ const missing = join(tempDir("cueloop-retention-"), "config.toml");
48
+
49
+ // Act
50
+ const days = resolveCleanupPeriodDays({ CUELOOP_CONFIG: missing } as NodeJS.ProcessEnv);
51
+
52
+ // Assert
53
+ expect(days).toBe(DEFAULT_CLEANUP_PERIOD_DAYS);
54
+ });
55
+
56
+ test("honors [cleanup] period_days", () => {
57
+ // Arrange
58
+ const path = join(tempDir("cueloop-retention-"), "config.toml");
59
+ writeFileSync(path, "[cleanup]\nperiod_days = 7\n");
60
+
61
+ // Act
62
+ const days = resolveCleanupPeriodDays({ CUELOOP_CONFIG: path } as NodeJS.ProcessEnv);
63
+
64
+ // Assert
65
+ expect(days).toBe(7);
66
+ });
67
+
68
+ test("falls back to the default when the value is not a number", () => {
69
+ // Arrange
70
+ const path = join(tempDir("cueloop-retention-"), "config.toml");
71
+ writeFileSync(path, '[cleanup]\nperiod_days = "soon"\n');
72
+
73
+ // Act
74
+ const days = resolveCleanupPeriodDays({ CUELOOP_CONFIG: path } as NodeJS.ProcessEnv);
75
+
76
+ // Assert
77
+ expect(days).toBe(DEFAULT_CLEANUP_PERIOD_DAYS);
78
+ });
79
+ });
80
+
81
+ describe("isExpired", () => {
82
+ test("is true past the window and false inside it", () => {
83
+ // Assert
84
+ expect(isExpired(daysAgo(40), 30, NOW_MS)).toBe(true);
85
+ expect(isExpired(daysAgo(10), 30, NOW_MS)).toBe(false);
86
+ });
87
+
88
+ test("a period of zero disables expiry", () => {
89
+ // Assert
90
+ expect(isExpired(daysAgo(999), 0, NOW_MS)).toBe(false);
91
+ });
92
+ });
93
+
94
+ describe("pruneExpiredSessions", () => {
95
+ test("deletes resolved sessions past the window and keeps recent ones", () => {
96
+ // Arrange
97
+ const home = tempDir("cueloop-retention-home-");
98
+ const store = new SessionStore(home);
99
+ store.upsert(session("ses_old", daysAgo(40)));
100
+ store.upsert(session("ses_new", daysAgo(2)));
101
+
102
+ // Act
103
+ const pruned = pruneExpiredSessions(store, 30, NOW_MS);
104
+
105
+ // Assert
106
+ expect(pruned).toEqual(["ses_old"]);
107
+ expect(store.get("ses_old")).toBeUndefined();
108
+ expect(store.get("ses_new")).toBeDefined();
109
+ });
110
+
111
+ test("never deletes an active pending session however old", () => {
112
+ // Arrange
113
+ const home = tempDir("cueloop-retention-home-");
114
+ const store = new SessionStore(home);
115
+ store.upsert(session("ses_active", daysAgo(400), "pending"));
116
+
117
+ // Act
118
+ const pruned = pruneExpiredSessions(store, 30, NOW_MS);
119
+
120
+ // Assert
121
+ expect(pruned).toEqual([]);
122
+ expect(store.get("ses_active")).toBeDefined();
123
+ });
124
+
125
+ test("a period of zero prunes nothing", () => {
126
+ // Arrange
127
+ const home = tempDir("cueloop-retention-home-");
128
+ const store = new SessionStore(home);
129
+ store.upsert(session("ses_old", daysAgo(999)));
130
+
131
+ // Act
132
+ const pruned = pruneExpiredSessions(store, 0, NOW_MS);
133
+
134
+ // Assert
135
+ expect(pruned).toEqual([]);
136
+ expect(store.get("ses_old")).toBeDefined();
137
+ });
138
+ });
139
+
140
+ describe("pruneExpiredReports", () => {
141
+ test("removes timestamped reports past the window but keeps the latest report", () => {
142
+ // Arrange
143
+ const home = tempDir("cueloop-retention-home-");
144
+ const directory = reportsDir(home);
145
+ mkdirSync(directory, { recursive: true });
146
+ const latest = join(directory, LATEST_REPORT_FILENAME);
147
+ const stale = join(directory, "refine-2026-06-01T00-00-00-000Z.md");
148
+ const fresh = join(directory, "refine-2026-08-25T00-00-00-000Z.md");
149
+ for (const path of [latest, stale, fresh]) writeFileSync(path, "report");
150
+ const staleSeconds = (NOW_MS - 60 * DAY_MS) / 1000;
151
+ utimesSync(stale, staleSeconds, staleSeconds);
152
+
153
+ // Act
154
+ const pruned = pruneExpiredReports(directory, 30, NOW_MS);
155
+
156
+ // Assert
157
+ expect(pruned).toEqual(["refine-2026-06-01T00-00-00-000Z.md"]);
158
+ });
159
+ });
@@ -0,0 +1,88 @@
1
+ import { readdirSync, readFileSync, rmSync, statSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { join } from "node:path";
4
+ import type { SessionStore } from "./store";
5
+
6
+ export const DEFAULT_CLEANUP_PERIOD_DAYS = 30;
7
+ export const LATEST_REPORT_FILENAME = "report.md";
8
+ export const TIMESTAMPED_REPORT_PREFIX = "refine-";
9
+
10
+ const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
11
+
12
+ function userConfigPath(env: NodeJS.ProcessEnv): string {
13
+ const explicit = env.CUELOOP_CONFIG;
14
+ if (explicit && explicit.trim()) return explicit;
15
+ const xdgConfigHome = env.XDG_CONFIG_HOME;
16
+ const base = xdgConfigHome && xdgConfigHome.trim() ? xdgConfigHome : join(homedir(), ".config");
17
+ return join(base, "cueloop", "config.toml");
18
+ }
19
+
20
+ function readConfiguredPeriodDays(path: string): number | undefined {
21
+ let raw: Record<string, unknown>;
22
+ try {
23
+ raw = Bun.TOML.parse(readFileSync(path, "utf8")) as Record<string, unknown>;
24
+ } catch {
25
+ return undefined;
26
+ }
27
+ const cleanup = raw["cleanup"] as Record<string, unknown> | undefined;
28
+ const value = cleanup?.["period_days"];
29
+ return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined;
30
+ }
31
+
32
+ export function resolveCleanupPeriodDays(env: NodeJS.ProcessEnv = process.env): number {
33
+ return readConfiguredPeriodDays(userConfigPath(env)) ?? DEFAULT_CLEANUP_PERIOD_DAYS;
34
+ }
35
+
36
+ export function isExpired(createdAt: string, periodDays: number, nowMs: number): boolean {
37
+ if (periodDays <= 0) return false;
38
+ const createdMs = Date.parse(createdAt);
39
+ if (Number.isNaN(createdMs)) return false;
40
+ return nowMs - createdMs > periodDays * MILLISECONDS_PER_DAY;
41
+ }
42
+
43
+ export function pruneExpiredSessions(
44
+ store: SessionStore,
45
+ periodDays: number,
46
+ nowMs: number,
47
+ ): string[] {
48
+ if (periodDays <= 0) return [];
49
+ const pruned: string[] = [];
50
+ for (const session of store.list()) {
51
+ if (session.status !== "resolved") continue;
52
+ if (isExpired(session.createdAt, periodDays, nowMs) && store.delete(session.id)) {
53
+ pruned.push(session.id);
54
+ }
55
+ }
56
+ return pruned;
57
+ }
58
+
59
+ export function timestampedReportFilename(nowMs: number): string {
60
+ const stamp = new Date(nowMs).toISOString().replace(/[:.]/g, "-");
61
+ return `${TIMESTAMPED_REPORT_PREFIX}${stamp}.md`;
62
+ }
63
+
64
+ export function pruneExpiredReports(
65
+ reportsDirectory: string,
66
+ periodDays: number,
67
+ nowMs: number,
68
+ ): string[] {
69
+ if (periodDays <= 0) return [];
70
+ let entries: string[];
71
+ try {
72
+ entries = readdirSync(reportsDirectory);
73
+ } catch {
74
+ return [];
75
+ }
76
+ const cutoffMs = nowMs - periodDays * MILLISECONDS_PER_DAY;
77
+ const pruned: string[] = [];
78
+ for (const entry of entries) {
79
+ if (entry === LATEST_REPORT_FILENAME) continue;
80
+ if (!entry.startsWith(TIMESTAMPED_REPORT_PREFIX)) continue;
81
+ const path = join(reportsDirectory, entry);
82
+ if (statSync(path).mtimeMs < cutoffMs) {
83
+ rmSync(path, { force: true });
84
+ pruned.push(entry);
85
+ }
86
+ }
87
+ return pruned;
88
+ }