@cueloop/daemon 0.1.0-alpha.8 → 0.1.0-alpha.80
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 +34 -22
- package/src/api.test.ts +925 -65
- package/src/api.ts +1060 -88
- package/src/capabilities.test.ts +57 -0
- package/src/capabilities.ts +84 -0
- package/src/client.test.ts +24 -0
- package/src/client.ts +526 -70
- package/src/curate.ts +109 -0
- package/src/daemon-version.test.ts +93 -0
- package/src/diff-hot-reload.test.ts +272 -0
- package/src/diff-watcher.ts +274 -0
- package/src/gh.ts +37 -0
- package/src/harness-delivery.test.ts +193 -0
- package/src/harness-state-store.test.ts +65 -0
- package/src/harness-state-store.ts +182 -0
- package/src/herdr-pane.test.ts +240 -0
- package/src/herdr-pane.ts +167 -0
- package/src/herdr-split.test.ts +106 -0
- package/src/herdr-split.ts +92 -0
- package/src/herdr-tab-store.test.ts +96 -0
- package/src/herdr-tab-store.ts +66 -0
- package/src/index.ts +8 -9
- package/src/interruptible-wait.test.ts +127 -0
- package/src/interruptible-wait.ts +66 -0
- package/src/main.ts +4 -1
- package/src/paths.ts +44 -0
- package/src/pr-poller.test.ts +76 -0
- package/src/pr-poller.ts +74 -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 +430 -42
- package/src/server.ts +362 -84
- package/src/share-blob.test.ts +112 -0
- package/src/share-blob.ts +55 -0
- package/src/single-instance.test.ts +72 -11
- 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 +394 -0
- package/src/thread-review.ts +324 -0
- package/src/validate.test.ts +214 -35
- package/src/validate.ts +296 -25
- 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
package/src/store.ts
CHANGED
|
@@ -1,68 +1,298 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* Thread persistence: one append-only JSONL log per thread, under a per-project bucket keyed by the
|
|
3
|
+
* repo's durable root commit (a shared bucket for repo-less threads). Each mutation appends a
|
|
4
|
+
* snapshot line and the last valid line wins, so a crash's torn tail is simply skipped on read; the
|
|
5
|
+
* log is compacted to one line on the first write, once it grows long, and when the thread resolves.
|
|
6
|
+
* Boot migrates the old whole-record JSON files into this layout and parks the originals. Records
|
|
7
|
+
* that fail to parse are skipped and reported, never deleted; records from before histories existed
|
|
8
|
+
* are given one on read.
|
|
9
|
+
*
|
|
10
|
+
* `ThreadRepository` is the contract every adapter satisfies; the conformance
|
|
11
|
+
* suite in ./testing/store-conformance.ts pins it for the file store and the
|
|
12
|
+
* in-memory store alike.
|
|
6
13
|
*/
|
|
7
14
|
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
import {
|
|
16
|
+
appendFileSync,
|
|
17
|
+
existsSync,
|
|
18
|
+
mkdirSync,
|
|
19
|
+
readdirSync,
|
|
20
|
+
readFileSync,
|
|
21
|
+
renameSync,
|
|
22
|
+
rmSync,
|
|
23
|
+
statSync,
|
|
24
|
+
writeFileSync,
|
|
25
|
+
} from "node:fs";
|
|
26
|
+
import { dirname, join } from "node:path";
|
|
27
|
+
import { historyFromLinear, withShareLinks, type Thread } from "@cueloop/schema";
|
|
28
|
+
import { migratedSessionsDir, sessionsDir, threadBucket, threadsDir } from "./paths";
|
|
29
|
+
import { validateThreadRecord } from "./validate";
|
|
30
|
+
|
|
31
|
+
/** A thread's log is rewritten to a single snapshot line once it grows past this many, so an
|
|
32
|
+
* active thread's file never bloats unbounded while writes stay cheap appends in between. */
|
|
33
|
+
const COMPACT_THRESHOLD = 40;
|
|
13
34
|
|
|
14
35
|
export interface RecoveryReport {
|
|
15
36
|
recovered: string[];
|
|
16
37
|
skipped: { file: string; error: string }[];
|
|
17
38
|
}
|
|
18
39
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
40
|
+
/** What the daemon needs from session storage. */
|
|
41
|
+
export interface ThreadRepository {
|
|
42
|
+
/** Load what is stored; called once on boot. */
|
|
43
|
+
recover(): RecoveryReport;
|
|
44
|
+
get(id: string): Thread | undefined;
|
|
45
|
+
/** Every session, oldest first. */
|
|
46
|
+
list(): Thread[];
|
|
47
|
+
upsert(session: Thread): void;
|
|
48
|
+
/** True when a session was removed. */
|
|
49
|
+
delete(id: string): boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A record as it is read: a history is derived for records written without
|
|
54
|
+
* one. A record with no revision has no head to derive from and keeps
|
|
55
|
+
* reading without a history - migration never loses a record.
|
|
56
|
+
*/
|
|
57
|
+
export function withHistory(session: Thread): Thread {
|
|
58
|
+
// migrate a legacy single share into shares[] on the same read that fills history
|
|
59
|
+
const migrated = withShareLinks(session);
|
|
60
|
+
|
|
61
|
+
if (migrated.history || migrated.revisions.length === 0) return migrated;
|
|
62
|
+
|
|
63
|
+
return { ...migrated, history: historyFromLinear(migrated) };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Records in the order `list()` promises: oldest first. */
|
|
67
|
+
function oldestFirst(sessions: Iterable<Thread>): Thread[] {
|
|
68
|
+
return [...sessions].sort((left, right) => left.createdAt.localeCompare(right.createdAt));
|
|
69
|
+
}
|
|
22
70
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
71
|
+
export class ThreadStore implements ThreadRepository {
|
|
72
|
+
private sessions = new Map<string, Thread>();
|
|
73
|
+
/** id -> its JSONL path (the bucket depends on the record's root commit, so it is tracked). */
|
|
74
|
+
private files = new Map<string, string>();
|
|
75
|
+
/** id -> lines in its log, so upsert knows when to compact instead of append. */
|
|
76
|
+
private lineCounts = new Map<string, number>();
|
|
77
|
+
|
|
78
|
+
constructor(private readonly home: string) {
|
|
79
|
+
mkdirSync(threadsDir(home), { recursive: true });
|
|
80
|
+
// the legacy dir is where a pre-upgrade daemon wrote; ensure it exists so its scan is uniform
|
|
81
|
+
mkdirSync(sessionsDir(home), { recursive: true });
|
|
26
82
|
}
|
|
27
83
|
|
|
28
|
-
/** Read-only scan of the state directory; called once on boot. */
|
|
29
84
|
recover(): RecoveryReport {
|
|
30
85
|
const report: RecoveryReport = { recovered: [], skipped: [] };
|
|
31
|
-
|
|
86
|
+
|
|
87
|
+
this.migrateLegacy(report);
|
|
88
|
+
const root = threadsDir(this.home);
|
|
89
|
+
|
|
90
|
+
for (const bucket of readdirSync(root)) {
|
|
91
|
+
const bucketPath = join(root, bucket);
|
|
92
|
+
|
|
93
|
+
if (!statSync(bucketPath).isDirectory()) continue;
|
|
94
|
+
for (const file of readdirSync(bucketPath)) {
|
|
95
|
+
if (!file.endsWith(".jsonl")) continue;
|
|
96
|
+
const filePath = join(bucketPath, file);
|
|
97
|
+
try {
|
|
98
|
+
const { session, lines, torn } = readThread(filePath);
|
|
99
|
+
|
|
100
|
+
this.sessions.set(session.id, session);
|
|
101
|
+
this.files.set(session.id, filePath);
|
|
102
|
+
this.lineCounts.set(session.id, lines);
|
|
103
|
+
report.recovered.push(session.id);
|
|
104
|
+
// a torn trailing fragment would fuse with the next append and lose that update, so rewrite
|
|
105
|
+
// the log to a single clean snapshot line now that the last valid record is in hand
|
|
106
|
+
if (torn) this.writeWhole(session);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
report.skipped.push({
|
|
109
|
+
file,
|
|
110
|
+
error: error instanceof Error ? error.message : String(error),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return report;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** One-time upgrade: write each old whole-record JSON as a thread log and park the original, so
|
|
120
|
+
* the threads scan below recovers it. Invalid records are reported and left in place, never lost. */
|
|
121
|
+
private migrateLegacy(report: RecoveryReport): void {
|
|
122
|
+
const legacyDir = sessionsDir(this.home);
|
|
123
|
+
|
|
124
|
+
if (!existsSync(legacyDir)) return;
|
|
125
|
+
const parked = migratedSessionsDir(this.home);
|
|
126
|
+
|
|
127
|
+
for (const file of readdirSync(legacyDir)) {
|
|
32
128
|
if (!file.endsWith(".json")) continue;
|
|
129
|
+
const legacyPath = join(legacyDir, file);
|
|
130
|
+
let session: Thread;
|
|
131
|
+
|
|
33
132
|
try {
|
|
34
|
-
const
|
|
35
|
-
|
|
133
|
+
const parsed = validateThreadRecord(JSON.parse(readFileSync(legacyPath, "utf8")));
|
|
134
|
+
|
|
36
135
|
if (!parsed.ok) throw new Error(`invalid record - ${parsed.error}`);
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
report.
|
|
40
|
-
|
|
41
|
-
|
|
136
|
+
session = withHistory(parsed.value);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
report.skipped.push({
|
|
139
|
+
file,
|
|
140
|
+
error: error instanceof Error ? error.message : String(error),
|
|
141
|
+
});
|
|
142
|
+
continue;
|
|
42
143
|
}
|
|
144
|
+
const targetPath = join(
|
|
145
|
+
threadBucket(session.workspace.rootCommit, this.home),
|
|
146
|
+
`${session.id}.jsonl`,
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
// after a rollback a newer JSONL can already hold this thread; never overwrite it with the
|
|
150
|
+
// older legacy snapshot - the existing log stays authoritative and the threads scan recovers it
|
|
151
|
+
if (!existsSync(targetPath)) this.writeWhole(session);
|
|
152
|
+
// forget the in-memory tracking migration seeded: the threads scan is the one recovery path
|
|
153
|
+
this.files.delete(session.id);
|
|
154
|
+
this.lineCounts.delete(session.id);
|
|
155
|
+
mkdirSync(parked, { recursive: true });
|
|
156
|
+
renameSync(legacyPath, join(parked, file));
|
|
43
157
|
}
|
|
44
|
-
return report;
|
|
45
158
|
}
|
|
46
159
|
|
|
47
|
-
|
|
160
|
+
/** Rewrite the thread to a single snapshot line, atomically - the first write and every compaction. */
|
|
161
|
+
private writeWhole(session: Thread): void {
|
|
162
|
+
const filePath =
|
|
163
|
+
this.files.get(session.id) ??
|
|
164
|
+
join(threadBucket(session.workspace.rootCommit, this.home), `${session.id}.jsonl`);
|
|
165
|
+
|
|
166
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
167
|
+
const tempPath = filePath + ".tmp";
|
|
168
|
+
|
|
169
|
+
writeFileSync(tempPath, JSON.stringify(session) + "\n");
|
|
170
|
+
renameSync(tempPath, filePath);
|
|
171
|
+
this.files.set(session.id, filePath);
|
|
172
|
+
this.lineCounts.set(session.id, 1);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
get(id: string): Thread | undefined {
|
|
48
176
|
return this.sessions.get(id);
|
|
49
177
|
}
|
|
50
178
|
|
|
51
|
-
list():
|
|
52
|
-
return
|
|
179
|
+
list(): Thread[] {
|
|
180
|
+
return oldestFirst(this.sessions.values());
|
|
53
181
|
}
|
|
54
182
|
|
|
55
|
-
upsert(session:
|
|
183
|
+
upsert(session: Thread): void {
|
|
56
184
|
this.sessions.set(session.id, session);
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
185
|
+
const filePath = this.files.get(session.id);
|
|
186
|
+
const lines = this.lineCounts.get(session.id) ?? 0;
|
|
187
|
+
|
|
188
|
+
// the first write, an over-long log, and a resolved (now immutable) thread all compact to one
|
|
189
|
+
// line; every other mutation is a cheap append that a torn tail on crash simply drops on read
|
|
190
|
+
if (filePath === undefined || lines >= COMPACT_THRESHOLD || session.status === "resolved") {
|
|
191
|
+
this.writeWhole(session);
|
|
192
|
+
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
appendFileSync(filePath, JSON.stringify(session) + "\n");
|
|
196
|
+
this.lineCounts.set(session.id, lines + 1);
|
|
61
197
|
}
|
|
62
198
|
|
|
63
199
|
delete(id: string): boolean {
|
|
64
200
|
if (!this.sessions.delete(id)) return false;
|
|
65
|
-
|
|
201
|
+
const filePath = this.files.get(id);
|
|
202
|
+
|
|
203
|
+
if (filePath) rmSync(filePath, { force: true });
|
|
204
|
+
this.files.delete(id);
|
|
205
|
+
this.lineCounts.delete(id);
|
|
206
|
+
|
|
66
207
|
return true;
|
|
67
208
|
}
|
|
68
209
|
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Read a thread log: the last valid snapshot line wins, so a crash's torn trailing line is skipped.
|
|
213
|
+
* `torn` is true when that winning line was not the last physical line - the caller rewrites the log
|
|
214
|
+
* so the next append starts on a clean boundary instead of fusing onto the torn fragment.
|
|
215
|
+
*/
|
|
216
|
+
function readThread(filePath: string) {
|
|
217
|
+
const allLines = readFileSync(filePath, "utf8")
|
|
218
|
+
.split("\n")
|
|
219
|
+
.filter((line) => line.trim().length > 0);
|
|
220
|
+
|
|
221
|
+
for (let index = allLines.length - 1; index >= 0; index--) {
|
|
222
|
+
let record: unknown;
|
|
223
|
+
|
|
224
|
+
try {
|
|
225
|
+
record = JSON.parse(allLines[index]!);
|
|
226
|
+
} catch {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const parsed = validateThreadRecord(record);
|
|
230
|
+
|
|
231
|
+
if (parsed.ok) {
|
|
232
|
+
return {
|
|
233
|
+
session: withHistory(parsed.value),
|
|
234
|
+
lines: allLines.length,
|
|
235
|
+
torn: index !== allLines.length - 1,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
throw new Error("no valid record line");
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The in-memory adapter: the same contract with nothing on disk. `seed` stands
|
|
244
|
+
* in for what a file store finds on recovery, so validation and migration are
|
|
245
|
+
* exercised the same way.
|
|
246
|
+
*/
|
|
247
|
+
export class MemoryThreadStore implements ThreadRepository {
|
|
248
|
+
private sessions = new Map<string, Thread>();
|
|
249
|
+
|
|
250
|
+
constructor(private readonly seed: unknown[] = []) {}
|
|
251
|
+
|
|
252
|
+
recover(): RecoveryReport {
|
|
253
|
+
const report: RecoveryReport = { recovered: [], skipped: [] };
|
|
254
|
+
|
|
255
|
+
this.seed.forEach((record, index) => {
|
|
256
|
+
const parsed = validateThreadRecord(record);
|
|
257
|
+
|
|
258
|
+
if (!parsed.ok) {
|
|
259
|
+
report.skipped.push({ file: `seed[${index}]`, error: `invalid record - ${parsed.error}` });
|
|
260
|
+
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const session = withHistory(parsed.value);
|
|
264
|
+
|
|
265
|
+
this.sessions.set(session.id, session);
|
|
266
|
+
report.recovered.push(session.id);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
return report;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
get(id: string): Thread | undefined {
|
|
273
|
+
return this.sessions.get(id);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
list(): Thread[] {
|
|
277
|
+
return oldestFirst(this.sessions.values());
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
upsert(session: Thread): void {
|
|
281
|
+
this.sessions.set(session.id, session);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
delete(id: string): boolean {
|
|
285
|
+
return this.sessions.delete(id);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** @deprecated use ThreadRepository */
|
|
290
|
+
export type SessionRepository = ThreadRepository;
|
|
291
|
+
/** @deprecated use ThreadStore */
|
|
292
|
+
export const SessionStore = ThreadStore;
|
|
293
|
+
/** @deprecated use ThreadStore */
|
|
294
|
+
export type SessionStore = ThreadStore;
|
|
295
|
+
/** @deprecated use MemoryThreadStore */
|
|
296
|
+
export const MemorySessionStore = MemoryThreadStore;
|
|
297
|
+
/** @deprecated use MemoryThreadStore */
|
|
298
|
+
export type MemorySessionStore = MemoryThreadStore;
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The session repository contract as executable cases. Every adapter runs the
|
|
3
|
+
* same suite: what a store returns, in which order, what survives a restart,
|
|
4
|
+
* what a bad record does, and how a record without a history reads.
|
|
5
|
+
*
|
|
6
|
+
* `restart` gives back a fresh adapter over the same persisted state (or the
|
|
7
|
+
* same seed) so recovery is tested through the contract, never past it.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, expect, test } from "bun:test";
|
|
11
|
+
import { SCHEMA_VERSION, type Thread } from "@cueloop/schema";
|
|
12
|
+
import type { ThreadRepository } from "../store";
|
|
13
|
+
|
|
14
|
+
export interface StoreHarness {
|
|
15
|
+
/** A fresh, empty adapter that will recover `records` on `recover()`. */
|
|
16
|
+
open: (records: unknown[]) => ThreadRepository;
|
|
17
|
+
/** A new adapter over whatever `store` persisted, as after a daemon restart. */
|
|
18
|
+
restart: (store: ThreadRepository) => ThreadRepository;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function createTestThreadRecord(
|
|
22
|
+
id: string,
|
|
23
|
+
createdAt: string,
|
|
24
|
+
overrides: Partial<Thread> = {},
|
|
25
|
+
): Thread {
|
|
26
|
+
return {
|
|
27
|
+
schemaVersion: SCHEMA_VERSION,
|
|
28
|
+
id,
|
|
29
|
+
workspace: { repoRoot: "/repo", branch: "main" },
|
|
30
|
+
artifact: { type: "plan", content: `# ${id}`, meta: {} },
|
|
31
|
+
revisions: [{ revision: 1, content: `# ${id}`, submittedAt: createdAt }],
|
|
32
|
+
annotations: [],
|
|
33
|
+
message: null,
|
|
34
|
+
status: "pending",
|
|
35
|
+
createdAt,
|
|
36
|
+
...overrides,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function runThreadStoreConformance(name: string, harness: StoreHarness): void {
|
|
41
|
+
describe(`${name} - session repository contract`, () => {
|
|
42
|
+
test("a stored session reads back whole and lists oldest first", () => {
|
|
43
|
+
// Arrange
|
|
44
|
+
const store = harness.open([]);
|
|
45
|
+
|
|
46
|
+
store.recover();
|
|
47
|
+
const newer = createTestThreadRecord("ses_b", "2026-09-02T00:00:00.000Z");
|
|
48
|
+
const older = createTestThreadRecord("ses_a", "2026-09-01T00:00:00.000Z");
|
|
49
|
+
|
|
50
|
+
// Act
|
|
51
|
+
store.upsert(newer);
|
|
52
|
+
store.upsert(older);
|
|
53
|
+
|
|
54
|
+
// Assert
|
|
55
|
+
expect(store.get("ses_a")).toEqual(older);
|
|
56
|
+
expect(store.list().map((session) => session.id)).toEqual(["ses_a", "ses_b"]);
|
|
57
|
+
expect(store.get("ses_zzz")).toBeUndefined();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("upsert replaces by id and delete reports whether anything was removed", () => {
|
|
61
|
+
// Arrange
|
|
62
|
+
const store = harness.open([]);
|
|
63
|
+
|
|
64
|
+
store.recover();
|
|
65
|
+
store.upsert(createTestThreadRecord("ses_a", "2026-09-01T00:00:00.000Z"));
|
|
66
|
+
|
|
67
|
+
// Act
|
|
68
|
+
store.upsert(
|
|
69
|
+
createTestThreadRecord("ses_a", "2026-09-01T00:00:00.000Z", { status: "resolved" }),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
// Assert
|
|
73
|
+
expect(store.list()).toHaveLength(1);
|
|
74
|
+
expect(store.get("ses_a")?.status).toBe("resolved");
|
|
75
|
+
expect(store.delete("ses_a")).toBe(true);
|
|
76
|
+
expect(store.delete("ses_a")).toBe(false);
|
|
77
|
+
expect(store.list()).toEqual([]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("what was upserted survives a restart, deletions included", () => {
|
|
81
|
+
// Arrange
|
|
82
|
+
const store = harness.open([]);
|
|
83
|
+
|
|
84
|
+
store.recover();
|
|
85
|
+
store.upsert(createTestThreadRecord("ses_keep", "2026-09-01T00:00:00.000Z"));
|
|
86
|
+
store.upsert(createTestThreadRecord("ses_gone", "2026-09-02T00:00:00.000Z"));
|
|
87
|
+
store.delete("ses_gone");
|
|
88
|
+
|
|
89
|
+
// Act
|
|
90
|
+
const reopened = harness.restart(store);
|
|
91
|
+
const report = reopened.recover();
|
|
92
|
+
|
|
93
|
+
// Assert
|
|
94
|
+
expect(report.recovered).toEqual(["ses_keep"]);
|
|
95
|
+
expect(reopened.get("ses_keep")?.artifact.content).toBe("# ses_keep");
|
|
96
|
+
expect(reopened.get("ses_gone")).toBeUndefined();
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("a record without a history reads as a one-branch tree whose head is the artifact", () => {
|
|
100
|
+
// Arrange: written before histories existed, with two revisions and a comment
|
|
101
|
+
const legacy = createTestThreadRecord("ses_old", "2026-09-01T00:00:00.000Z", {
|
|
102
|
+
artifact: { type: "plan", content: "v2", meta: {} },
|
|
103
|
+
revisions: [
|
|
104
|
+
{ revision: 1, content: "v1", submittedAt: "2026-09-01T00:00:00.000Z" },
|
|
105
|
+
{ revision: 2, content: "v2", submittedAt: "2026-09-01T02:00:00.000Z" },
|
|
106
|
+
],
|
|
107
|
+
annotations: [
|
|
108
|
+
{
|
|
109
|
+
id: "a1",
|
|
110
|
+
kind: "comment",
|
|
111
|
+
anchor: { quote: "v1", prefix: "", suffix: "" },
|
|
112
|
+
body: "why",
|
|
113
|
+
createdAt: "2026-09-01T01:00:00.000Z",
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
});
|
|
117
|
+
const store = harness.open([legacy]);
|
|
118
|
+
|
|
119
|
+
// Act
|
|
120
|
+
const report = store.recover();
|
|
121
|
+
const history = store.get("ses_old")?.history;
|
|
122
|
+
|
|
123
|
+
// Assert
|
|
124
|
+
expect(report.recovered).toEqual(["ses_old"]);
|
|
125
|
+
expect(history?.branch).toBe("main");
|
|
126
|
+
expect(history?.entries.map((entry) => entry.type)).toEqual([
|
|
127
|
+
"revision",
|
|
128
|
+
"comment",
|
|
129
|
+
"revision",
|
|
130
|
+
]);
|
|
131
|
+
expect(history?.entries.at(-1)).toMatchObject({ type: "revision", content: "v2" });
|
|
132
|
+
expect(history?.tips.main).toBe(history?.entries.at(-1)?.id);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("a record with no revision still recovers, without a history", () => {
|
|
136
|
+
// Arrange
|
|
137
|
+
const store = harness.open([
|
|
138
|
+
createTestThreadRecord("ses_empty", "2026-09-01T00:00:00.000Z", { revisions: [] }),
|
|
139
|
+
]);
|
|
140
|
+
|
|
141
|
+
// Act
|
|
142
|
+
const report = store.recover();
|
|
143
|
+
|
|
144
|
+
// Assert
|
|
145
|
+
expect(report.recovered).toEqual(["ses_empty"]);
|
|
146
|
+
expect(store.get("ses_empty")?.history).toBeUndefined();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("a record that already carries a history keeps it untouched", () => {
|
|
150
|
+
// Arrange
|
|
151
|
+
const withTree = createTestThreadRecord("ses_tree", "2026-09-01T00:00:00.000Z", {
|
|
152
|
+
history: {
|
|
153
|
+
entries: [
|
|
154
|
+
{
|
|
155
|
+
id: "root",
|
|
156
|
+
parentId: null,
|
|
157
|
+
type: "revision",
|
|
158
|
+
by: "agent",
|
|
159
|
+
content: "# ses_tree",
|
|
160
|
+
createdAt: "2026-09-01T00:00:00.000Z",
|
|
161
|
+
},
|
|
162
|
+
],
|
|
163
|
+
tips: { main: "root", ramble: "root" },
|
|
164
|
+
branch: "ramble",
|
|
165
|
+
labels: { root: "start" },
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
const store = harness.open([withTree]);
|
|
169
|
+
|
|
170
|
+
// Act
|
|
171
|
+
store.recover();
|
|
172
|
+
|
|
173
|
+
// Assert
|
|
174
|
+
expect(store.get("ses_tree")?.history).toEqual(withTree.history);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("a record that fails validation is skipped and reported, never dropped from the report", () => {
|
|
178
|
+
// Arrange
|
|
179
|
+
const store = harness.open([
|
|
180
|
+
createTestThreadRecord("ses_good", "2026-09-01T00:00:00.000Z"),
|
|
181
|
+
{ schemaVersion: SCHEMA_VERSION, id: "ses_bad" },
|
|
182
|
+
]);
|
|
183
|
+
|
|
184
|
+
// Act
|
|
185
|
+
const report = store.recover();
|
|
186
|
+
|
|
187
|
+
// Assert
|
|
188
|
+
expect(report.recovered).toEqual(["ses_good"]);
|
|
189
|
+
expect(report.skipped).toHaveLength(1);
|
|
190
|
+
expect(report.skipped[0]!.error).toContain("invalid record");
|
|
191
|
+
expect(store.get("ses_bad")).toBeUndefined();
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
}
|