@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.
- package/package.json +39 -22
- package/src/api.test.ts +1060 -65
- package/src/api.ts +1398 -89
- package/src/capabilities.test.ts +57 -0
- package/src/capabilities.ts +89 -0
- package/src/client.test.ts +24 -0
- package/src/client.ts +669 -86
- package/src/curate.ts +109 -0
- package/src/daemon-version.test.ts +142 -0
- package/src/diff-hot-reload.test.ts +419 -0
- package/src/diff-watcher.ts +293 -0
- package/src/gh.test.ts +59 -0
- package/src/gh.ts +74 -0
- package/src/ghostty-thread-surface-store.test.ts +49 -0
- package/src/ghostty-thread-surface-store.ts +101 -0
- package/src/ghostty-thread-surface.test.ts +398 -0
- package/src/ghostty-thread-surface.ts +267 -0
- package/src/harness-delivery.test.ts +268 -0
- package/src/harness-state-store.test.ts +93 -0
- package/src/harness-state-store.ts +213 -0
- package/src/herdr-split.test.ts +106 -0
- package/src/herdr-split.ts +92 -0
- package/src/herdr-thread-surface-store.test.ts +81 -0
- package/src/herdr-thread-surface-store.ts +64 -0
- package/src/herdr-thread-surface.test.ts +411 -0
- package/src/herdr-thread-surface.ts +355 -0
- package/src/index.ts +8 -9
- package/src/interruptible-wait.test.ts +127 -0
- package/src/interruptible-wait.ts +66 -0
- package/src/jj-working-tree.ts +131 -0
- package/src/main.ts +4 -1
- package/src/paths.ts +49 -0
- package/src/pr-poller.test.ts +102 -0
- package/src/pr-poller.ts +73 -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 +191 -0
- package/src/retention.ts +123 -0
- package/src/server.test.ts +453 -42
- package/src/server.ts +433 -88
- package/src/share-blob.test.ts +112 -0
- package/src/share-blob.ts +55 -0
- package/src/single-instance.test.ts +162 -13
- package/src/store.conformance.test.ts +41 -0
- package/src/store.jsonl.test.ts +159 -0
- package/src/store.ts +264 -34
- package/src/testing/store-conformance.ts +194 -0
- package/src/thread-review.test.ts +483 -0
- package/src/thread-review.ts +398 -0
- package/src/thread-surface-config.test.ts +63 -0
- package/src/thread-surface-config.ts +61 -0
- package/src/validate.test.ts +282 -37
- package/src/validate.ts +398 -33
- package/src/vcs-source.test.ts +180 -0
- package/src/vcs-source.ts +228 -0
- package/src/version.ts +6 -0
- package/src/working-tree.ts +149 -0
- package/src/review.test.ts +0 -167
- package/src/review.ts +0 -174
|
@@ -0,0 +1,191 @@
|
|
|
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 { Thread, SessionStatus } from "@cueloop/schema";
|
|
8
|
+
import { ThreadStore } from "./store";
|
|
9
|
+
import { reportsDir } from "./paths";
|
|
10
|
+
import {
|
|
11
|
+
DEFAULT_CLEANUP_PERIOD_DAYS,
|
|
12
|
+
LATEST_REPORT_FILENAME,
|
|
13
|
+
isExpired,
|
|
14
|
+
parseRefineState,
|
|
15
|
+
pruneExpiredReports,
|
|
16
|
+
pruneExpiredSessions,
|
|
17
|
+
resolveCleanupPeriodDays,
|
|
18
|
+
} from "./retention";
|
|
19
|
+
|
|
20
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
21
|
+
const NOW_MS = Date.parse("2026-08-26T00:00:00Z");
|
|
22
|
+
|
|
23
|
+
function tempDir(prefix: string): string {
|
|
24
|
+
return mkdtempSync(join(tmpdir(), prefix));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function daysAgo(days: number): string {
|
|
28
|
+
return new Date(NOW_MS - days * DAY_MS).toISOString();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function session(id: string, createdAt: string, status: SessionStatus = "resolved"): Thread {
|
|
32
|
+
return {
|
|
33
|
+
schemaVersion: "1",
|
|
34
|
+
id,
|
|
35
|
+
workspace: { repoRoot: "/repo", branch: "main" },
|
|
36
|
+
artifact: { type: "plan", content: "", meta: {} },
|
|
37
|
+
revisions: [],
|
|
38
|
+
annotations: [],
|
|
39
|
+
message: null,
|
|
40
|
+
status,
|
|
41
|
+
createdAt,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe("resolveCleanupPeriodDays", () => {
|
|
46
|
+
test("defaults to 30 when no config file is present", () => {
|
|
47
|
+
// Arrange
|
|
48
|
+
const missing = join(tempDir("cueloop-retention-"), "config.toml");
|
|
49
|
+
|
|
50
|
+
// Act
|
|
51
|
+
const days = resolveCleanupPeriodDays({ CUELOOP_CONFIG: missing });
|
|
52
|
+
|
|
53
|
+
// Assert
|
|
54
|
+
expect(days).toBe(DEFAULT_CLEANUP_PERIOD_DAYS);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("honors [cleanup] period_days", () => {
|
|
58
|
+
// Arrange
|
|
59
|
+
const path = join(tempDir("cueloop-retention-"), "config.toml");
|
|
60
|
+
|
|
61
|
+
writeFileSync(path, "[cleanup]\nperiod_days = 7\n");
|
|
62
|
+
|
|
63
|
+
// Act
|
|
64
|
+
const days = resolveCleanupPeriodDays({ CUELOOP_CONFIG: path });
|
|
65
|
+
|
|
66
|
+
// Assert
|
|
67
|
+
expect(days).toBe(7);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("falls back to the default when the value is not a number", () => {
|
|
71
|
+
// Arrange
|
|
72
|
+
const path = join(tempDir("cueloop-retention-"), "config.toml");
|
|
73
|
+
|
|
74
|
+
writeFileSync(path, '[cleanup]\nperiod_days = "soon"\n');
|
|
75
|
+
|
|
76
|
+
// Act
|
|
77
|
+
const days = resolveCleanupPeriodDays({ CUELOOP_CONFIG: path });
|
|
78
|
+
|
|
79
|
+
// Assert
|
|
80
|
+
expect(days).toBe(DEFAULT_CLEANUP_PERIOD_DAYS);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("parseRefineState", () => {
|
|
85
|
+
test("reads the analyzed fingerprint map", () => {
|
|
86
|
+
// Assert
|
|
87
|
+
expect([...parseRefineState({ analyzed: { ses_a: "1:2:x" } })]).toEqual([["ses_a", "1:2:x"]]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("returns an empty map for the legacy or malformed shape", () => {
|
|
91
|
+
// Assert
|
|
92
|
+
expect(parseRefineState({ seenSessionIds: ["ses_a"] }).size).toBe(0);
|
|
93
|
+
expect(parseRefineState({ analyzed: { ses_a: 5 } }).size).toBe(0);
|
|
94
|
+
expect(parseRefineState(null).size).toBe(0);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe("isExpired", () => {
|
|
99
|
+
test("is true past the window and false inside it", () => {
|
|
100
|
+
// Assert
|
|
101
|
+
expect(isExpired(daysAgo(40), 30, NOW_MS)).toBe(true);
|
|
102
|
+
expect(isExpired(daysAgo(10), 30, NOW_MS)).toBe(false);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("a period of zero disables expiry", () => {
|
|
106
|
+
// Assert
|
|
107
|
+
expect(isExpired(daysAgo(999), 0, NOW_MS)).toBe(false);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe("pruneExpiredSessions", () => {
|
|
112
|
+
test("deletes resolved sessions past the window and keeps recent ones", () => {
|
|
113
|
+
// Arrange
|
|
114
|
+
const home = tempDir("cueloop-retention-home-");
|
|
115
|
+
const store = new ThreadStore(home);
|
|
116
|
+
|
|
117
|
+
store.upsert(session("ses_old", daysAgo(40)));
|
|
118
|
+
store.upsert(session("ses_new", daysAgo(2)));
|
|
119
|
+
|
|
120
|
+
// Act
|
|
121
|
+
const pruned = pruneExpiredSessions(store, 30, NOW_MS);
|
|
122
|
+
|
|
123
|
+
// Assert
|
|
124
|
+
expect(pruned).toEqual(["ses_old"]);
|
|
125
|
+
expect(store.get("ses_old")).toBeUndefined();
|
|
126
|
+
expect(store.get("ses_new")).toBeDefined();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("keeps an expired resolved Thread while its Message is awaiting delivery", () => {
|
|
130
|
+
const store = new ThreadStore(tempDir("cueloop-retention-home-"));
|
|
131
|
+
|
|
132
|
+
store.upsert(session("ses_waiting", daysAgo(40)));
|
|
133
|
+
|
|
134
|
+
expect(pruneExpiredSessions(store, 30, NOW_MS, new Set(["ses_waiting"]))).toEqual([]);
|
|
135
|
+
expect(store.get("ses_waiting")).toBeDefined();
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("never deletes an active pending session however old", () => {
|
|
139
|
+
// Arrange
|
|
140
|
+
const home = tempDir("cueloop-retention-home-");
|
|
141
|
+
const store = new ThreadStore(home);
|
|
142
|
+
|
|
143
|
+
store.upsert(session("ses_active", daysAgo(400), "pending"));
|
|
144
|
+
|
|
145
|
+
// Act
|
|
146
|
+
const pruned = pruneExpiredSessions(store, 30, NOW_MS);
|
|
147
|
+
|
|
148
|
+
// Assert
|
|
149
|
+
expect(pruned).toEqual([]);
|
|
150
|
+
expect(store.get("ses_active")).toBeDefined();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("a period of zero prunes nothing", () => {
|
|
154
|
+
// Arrange
|
|
155
|
+
const home = tempDir("cueloop-retention-home-");
|
|
156
|
+
const store = new ThreadStore(home);
|
|
157
|
+
|
|
158
|
+
store.upsert(session("ses_old", daysAgo(999)));
|
|
159
|
+
|
|
160
|
+
// Act
|
|
161
|
+
const pruned = pruneExpiredSessions(store, 0, NOW_MS);
|
|
162
|
+
|
|
163
|
+
// Assert
|
|
164
|
+
expect(pruned).toEqual([]);
|
|
165
|
+
expect(store.get("ses_old")).toBeDefined();
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe("pruneExpiredReports", () => {
|
|
170
|
+
test("removes timestamped reports past the window but keeps the latest report", () => {
|
|
171
|
+
// Arrange
|
|
172
|
+
const home = tempDir("cueloop-retention-home-");
|
|
173
|
+
const directory = reportsDir(home);
|
|
174
|
+
|
|
175
|
+
mkdirSync(directory, { recursive: true });
|
|
176
|
+
const latest = join(directory, LATEST_REPORT_FILENAME);
|
|
177
|
+
const stale = join(directory, "refine-2026-06-01T00-00-00-000Z.md");
|
|
178
|
+
const fresh = join(directory, "refine-2026-08-25T00-00-00-000Z.md");
|
|
179
|
+
|
|
180
|
+
for (const path of [latest, stale, fresh]) writeFileSync(path, "report");
|
|
181
|
+
const staleSeconds = (NOW_MS - 60 * DAY_MS) / 1000;
|
|
182
|
+
|
|
183
|
+
utimesSync(stale, staleSeconds, staleSeconds);
|
|
184
|
+
|
|
185
|
+
// Act
|
|
186
|
+
const pruned = pruneExpiredReports(directory, 30, NOW_MS);
|
|
187
|
+
|
|
188
|
+
// Assert
|
|
189
|
+
expect(pruned).toEqual(["refine-2026-06-01T00-00-00-000Z.md"]);
|
|
190
|
+
});
|
|
191
|
+
});
|
package/src/retention.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { readdirSync, readFileSync, rmSync, statSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import * as v from "valibot";
|
|
5
|
+
import type { ThreadStore } from "./store";
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_CLEANUP_PERIOD_DAYS = 30;
|
|
8
|
+
export const LATEST_REPORT_FILENAME = "report.md";
|
|
9
|
+
export const TIMESTAMPED_REPORT_PREFIX = "refine-";
|
|
10
|
+
|
|
11
|
+
const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
|
|
12
|
+
|
|
13
|
+
const CleanupConfigSchema = v.object({
|
|
14
|
+
cleanup: v.optional(v.object({ period_days: v.optional(v.number()) })),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const RefineStateSchema = v.object({
|
|
18
|
+
analyzed: v.optional(v.record(v.string(), v.string()), {}),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export type RefineState = v.InferOutput<typeof RefineStateSchema>;
|
|
22
|
+
|
|
23
|
+
export function parseRefineState(raw: Parameters<typeof v.safeParse>[1]): Map<string, string> {
|
|
24
|
+
const parsed = v.safeParse(RefineStateSchema, raw);
|
|
25
|
+
|
|
26
|
+
if (!parsed.success) return new Map();
|
|
27
|
+
|
|
28
|
+
return new Map(Object.entries(parsed.output.analyzed));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function userConfigPath(env: NodeJS.ProcessEnv): string {
|
|
32
|
+
const explicit = env.CUELOOP_CONFIG;
|
|
33
|
+
|
|
34
|
+
if (explicit && explicit.trim()) return explicit;
|
|
35
|
+
const xdgConfigHome = env.XDG_CONFIG_HOME;
|
|
36
|
+
const base = xdgConfigHome && xdgConfigHome.trim() ? xdgConfigHome : join(homedir(), ".config");
|
|
37
|
+
|
|
38
|
+
return join(base, "cueloop", "config.toml");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function readConfiguredPeriodDays(path: string): number | undefined {
|
|
42
|
+
let raw: unknown;
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
raw = Bun.TOML.parse(readFileSync(path, "utf8"));
|
|
46
|
+
} catch {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const parsed = v.safeParse(CleanupConfigSchema, raw);
|
|
50
|
+
|
|
51
|
+
if (!parsed.success) return undefined;
|
|
52
|
+
const value = parsed.output.cleanup?.period_days;
|
|
53
|
+
|
|
54
|
+
return value !== undefined && Number.isFinite(value) && value >= 0 ? value : undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function resolveCleanupPeriodDays(env: NodeJS.ProcessEnv = process.env): number {
|
|
58
|
+
return readConfiguredPeriodDays(userConfigPath(env)) ?? DEFAULT_CLEANUP_PERIOD_DAYS;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function isExpired(createdAt: string, periodDays: number, nowMs: number): boolean {
|
|
62
|
+
if (periodDays <= 0) return false;
|
|
63
|
+
const createdMs = Date.parse(createdAt);
|
|
64
|
+
|
|
65
|
+
if (Number.isNaN(createdMs)) return false;
|
|
66
|
+
|
|
67
|
+
return nowMs - createdMs > periodDays * MILLISECONDS_PER_DAY;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function pruneExpiredSessions(
|
|
71
|
+
store: ThreadStore,
|
|
72
|
+
periodDays: number,
|
|
73
|
+
nowMs: number,
|
|
74
|
+
protectedThreadIds: ReadonlySet<string> = new Set(),
|
|
75
|
+
): string[] {
|
|
76
|
+
if (periodDays <= 0) return [];
|
|
77
|
+
const pruned: string[] = [];
|
|
78
|
+
|
|
79
|
+
for (const session of store.list()) {
|
|
80
|
+
if (session.status !== "resolved" || protectedThreadIds.has(session.id)) continue;
|
|
81
|
+
if (isExpired(session.createdAt, periodDays, nowMs) && store.delete(session.id)) {
|
|
82
|
+
pruned.push(session.id);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return pruned;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function timestampedReportFilename(nowMs: number): string {
|
|
90
|
+
const stamp = new Date(nowMs).toISOString().replace(/[:.]/g, "-");
|
|
91
|
+
|
|
92
|
+
return `${TIMESTAMPED_REPORT_PREFIX}${stamp}.md`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function pruneExpiredReports(
|
|
96
|
+
reportsDirectory: string,
|
|
97
|
+
periodDays: number,
|
|
98
|
+
nowMs: number,
|
|
99
|
+
): string[] {
|
|
100
|
+
if (periodDays <= 0) return [];
|
|
101
|
+
let entries: string[];
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
entries = readdirSync(reportsDirectory);
|
|
105
|
+
} catch {
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
const cutoffMs = nowMs - periodDays * MILLISECONDS_PER_DAY;
|
|
109
|
+
const pruned: string[] = [];
|
|
110
|
+
|
|
111
|
+
for (const entry of entries) {
|
|
112
|
+
if (entry === LATEST_REPORT_FILENAME) continue;
|
|
113
|
+
if (!entry.startsWith(TIMESTAMPED_REPORT_PREFIX)) continue;
|
|
114
|
+
const path = join(reportsDirectory, entry);
|
|
115
|
+
|
|
116
|
+
if (statSync(path).mtimeMs < cutoffMs) {
|
|
117
|
+
rmSync(path, { force: true });
|
|
118
|
+
pruned.push(entry);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return pruned;
|
|
123
|
+
}
|