File size: 25,884 Bytes
f500658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import * as storedValues from "@earendil-works/pi-agent-core";
import * as sessionWrites from "@earendil-works/pi-agent-core";
import { BACKGROUND_CONTEXT, prepareStorageCommit } from "@earendil-works/pi-agent-core";
import { describe, expect, it } from "vitest";
import {
	createNodeSqliteFactory,
	type SqliteDatabase,
	type SqliteStatement,
	SqliteStorage,
	sql,
} from "../src/index.ts";
import { applyInitialSchema } from "../src/sqlite/migrations.ts";
import { advanceNextSeq, readNextSeq } from "../src/sqlite/session/session-sequences.ts";
import { listValueReadQuery } from "../src/sqlite/session/values.ts";

const SESSION_ID = "session";

class TransactionCountingDatabase implements SqliteDatabase {
	readonly source: SqliteDatabase;
	transactionCount = 0;

	constructor(source: SqliteDatabase) {
		this.source = source;
	}

	exec(query: string): void {
		this.source.exec(query);
	}

	prepare(query: string): SqliteStatement {
		return this.source.prepare(query);
	}

	transaction<T>(callback: () => T): T {
		this.transactionCount++;
		return this.source.transaction(callback);
	}

	close(): void {
		this.source.close();
	}
}

const ZERO_USAGE = {
	input: 0,
	output: 0,
	cacheRead: 0,
	cacheWrite: 0,
	totalTokens: 0,
	cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
};

async function withStorage<T>(run: (storage: SqliteStorage, db: SqliteDatabase) => Promise<T>): Promise<T> {
	const db = await createNodeSqliteFactory().open(":memory:");
	try {
		await applyInitialSchema(db);
		const storage = new SqliteStorage(db, { sessionId: SESSION_ID, now: () => 1_700_000_000_000 });
		return await run(storage, db);
	} finally {
		db.close();
	}
}

function explainQueryPlan(db: SqliteDatabase, query: string, ...params: unknown[]): string[] {
	return db
		.prepare(`EXPLAIN QUERY PLAN ${query}`)
		.all<{ detail: string }>(...params)
		.map((row) => row.detail);
}

function expectBranchPlan(plan: string[]): void {
	expect(plan.some((detail) => detail.includes("SEARCH b USING COVERING INDEX ix_be_seq"))).toBe(true);
	expect(plan.some((detail) => detail.includes("SEARCH e USING PRIMARY KEY"))).toBe(true);
	expect(plan.some((detail) => detail.includes("USE TEMP B-TREE"))).toBe(false);
	expect(plan.some((detail) => detail.includes("SCAN e"))).toBe(false);
}

function insertCommitSessionRow(db: SqliteDatabase, nextSeq = 1): void {
	sql`INSERT INTO sessions
		(id, created_at, parent_session_id, storage_version, metadata, message_count, usage_payload, next_seq)
		VALUES (
			${SESSION_ID},
			${1},
			${null},
			${1},
			${null},
			${0},
			${JSON.stringify(ZERO_USAGE)},
			${nextSeq}
		)`.run(db);
}

describe("SqliteStorage", () => {
	it("uses one write transaction for an ordinary commit", async () => {
		const db = new TransactionCountingDatabase(await createNodeSqliteFactory().open(":memory:"));
		try {
			await applyInitialSchema(db);
			insertCommitSessionRow(db);
			const storage = new SqliteStorage(db, { sessionId: SESSION_ID, now: () => 1_700_000_000_000 });

			await storage.commit([storedValues.setValue(storedValues.sessionName, "name")], BACKGROUND_CONTEXT);

			expect(db.transactionCount).toBe(1);
			await storage.close(BACKGROUND_CONTEXT);
		} finally {
			db.close();
		}
	});

	it("commits root entries and append-to-tip entries into the branch index", async () => {
		await withStorage(async (storage, db) => {
			insertCommitSessionRow(db);

			expect(
				await storage.commit(
					[
						sessionWrites.insertEntry({
							id: "root",
							parentId: null,
							type: "message",
							message: { role: "user", content: "root", timestamp: 10 },
						}),
					],
					BACKGROUND_CONTEXT,
				),
			).toEqual({
				firstSeq: 1,
				seqs: [1],
				timestamp: 1_700_000_000_000,
				stats: { messageCount: 1, usage: ZERO_USAGE },
			});
			expect(
				await storage.commit(
					[
						sessionWrites.insertEntry({
							id: "child",
							parentId: "root",
							type: "message",
							message: { role: "user", content: "child", timestamp: 11 },
						}),
					],
					BACKGROUND_CONTEXT,
				),
			).toEqual({
				firstSeq: 2,
				seqs: [2],
				timestamp: 1_700_000_000_000,
				stats: { messageCount: 2, usage: ZERO_USAGE },
			});

			expect(sql`SELECT branch_id, tip_entry_id, tip_seq FROM branch_meta`.all(db)).toEqual([
				{ branch_id: "root", tip_entry_id: "child", tip_seq: 2 },
			]);
			expect(
				sql`SELECT branch_id, entry_id, entry_seq, entry_type FROM branch_entries ORDER BY entry_seq`.all(db),
			).toEqual([
				{ branch_id: "root", entry_id: "root", entry_seq: 1, entry_type: "message" },
				{ branch_id: "root", entry_id: "child", entry_seq: 2, entry_type: "message" },
			]);
			expect((await storage.scanBranch({ start: "child" }, BACKGROUND_CONTEXT)).map((entry) => entry.id)).toEqual([
				"child",
				"root",
			]);
		});
	});

	it("commits divergent branch entries by materializing a new segment", async () => {
		await withStorage(async (storage, db) => {
			insertCommitSessionRow(db);
			await storage.commit(
				[
					sessionWrites.insertEntry({
						id: "root",
						parentId: null,
						type: "message",
						message: { role: "user", content: "root", timestamp: 10 },
					}),
					sessionWrites.insertEntry({
						id: "left",
						parentId: "root",
						type: "message",
						message: { role: "user", content: "left", timestamp: 11 },
					}),
					sessionWrites.insertEntry({
						id: "right",
						parentId: "root",
						type: "message",
						message: { role: "user", content: "right", timestamp: 12 },
					}),
				],
				BACKGROUND_CONTEXT,
			);

			expect(
				sql`SELECT branch_id, tip_entry_id, tip_seq, base_branch_id, base_seq FROM branch_meta ORDER BY branch_id`.all(
					db,
				),
			).toEqual([
				{ branch_id: "right", tip_entry_id: "right", tip_seq: 3, base_branch_id: null, base_seq: null },
				{ branch_id: "root", tip_entry_id: "left", tip_seq: 2, base_branch_id: null, base_seq: null },
			]);
			expect(
				sql`SELECT branch_id, entry_id, entry_seq, entry_type FROM branch_entries ORDER BY branch_id, entry_seq`.all(
					db,
				),
			).toEqual([
				{ branch_id: "right", entry_id: "root", entry_seq: 1, entry_type: "message" },
				{ branch_id: "right", entry_id: "right", entry_seq: 3, entry_type: "message" },
				{ branch_id: "root", entry_id: "root", entry_seq: 1, entry_type: "message" },
				{ branch_id: "root", entry_id: "left", entry_seq: 2, entry_type: "message" },
			]);
			expect((await storage.scanBranch({ start: "right" }, BACKGROUND_CONTEXT)).map((entry) => entry.id)).toEqual([
				"right",
				"root",
			]);
			expect((await storage.scanBranch({ start: "left" }, BACKGROUND_CONTEXT)).map((entry) => entry.id)).toEqual([
				"left",
				"root",
			]);
		});
	});

	it("bases divergent branch segments at the newest compaction", async () => {
		await withStorage(async (storage, db) => {
			insertCommitSessionRow(db);
			await storage.commit(
				[
					sessionWrites.insertEntry({
						id: "root",
						parentId: null,
						type: "message",
						message: { role: "user", content: "root", timestamp: 10 },
					}),
					sessionWrites.insertEntry({
						id: "compact",
						parentId: "root",
						type: "compaction",
						summary: "summary",
						retainedTail: [],
						tokensBefore: 1,
						fromHook: false,
					}),
					sessionWrites.insertEntry({
						id: "left",
						parentId: "compact",
						type: "message",
						message: { role: "user", content: "left", timestamp: 11 },
					}),
					sessionWrites.insertEntry({
						id: "leaf",
						parentId: "left",
						type: "message",
						message: { role: "user", content: "leaf", timestamp: 12 },
					}),
					sessionWrites.insertEntry({
						id: "right",
						parentId: "left",
						type: "message",
						message: { role: "user", content: "right", timestamp: 13 },
					}),
				],
				BACKGROUND_CONTEXT,
			);

			expect(
				sql`SELECT branch_id, tip_entry_id, tip_seq, base_branch_id, base_seq FROM branch_meta WHERE branch_id = ${"right"}`.get(
					db,
				),
			).toEqual({
				branch_id: "right",
				tip_entry_id: "right",
				tip_seq: 5,
				base_branch_id: "root",
				base_seq: 2,
			});
			expect(
				sql`SELECT branch_id, entry_id, entry_seq, entry_type FROM branch_entries WHERE branch_id = ${"right"} ORDER BY entry_seq`.all(
					db,
				),
			).toEqual([
				{ branch_id: "right", entry_id: "left", entry_seq: 3, entry_type: "message" },
				{ branch_id: "right", entry_id: "right", entry_seq: 5, entry_type: "message" },
			]);
			expect((await storage.scanBranch({ start: "right" }, BACKGROUND_CONTEXT)).map((entry) => entry.id)).toEqual([
				"right",
				"left",
				"compact",
				"root",
			]);
		});
	});

	it("gets entries by requested id order", async () => {
		await withStorage(async (storage, db) => {
			sql`INSERT INTO entries (session_id, id, parent_id, seq, type, custom_type, timestamp, payload)
				VALUES
					(${SESSION_ID}, ${"first"}, ${null}, ${1}, ${"custom"}, ${"note"}, ${10}, ${JSON.stringify({ data: { value: 1 } })}),
					(${SESSION_ID}, ${"second"}, ${"first"}, ${2}, ${"message"}, ${null}, ${11}, ${JSON.stringify({ message: { role: "user", content: "hi", timestamp: 11 } })})`.run(
				db,
			);

			const entries = await storage.getEntries(["second", "missing", "first"], BACKGROUND_CONTEXT);

			expect([...entries.keys()]).toEqual(["second", "first"]);
			expect(entries.get("second")).toMatchObject({ id: "second", type: "message", parentId: "first" });
			expect(entries.get("first")).toMatchObject({ id: "first", type: "custom", customType: "note" });
		});
	});

	it("scans decoded entries with filters and sequence bounds", async () => {
		await withStorage(async (storage, db) => {
			sql`INSERT INTO entries (session_id, id, parent_id, seq, type, custom_type, timestamp, payload)
				VALUES
					(${SESSION_ID}, ${"one"}, ${null}, ${1}, ${"custom"}, ${"note"}, ${10}, ${JSON.stringify({ data: 1 })}),
					(${SESSION_ID}, ${"two"}, ${"one"}, ${2}, ${"message"}, ${null}, ${11}, ${JSON.stringify({ message: { role: "user", content: "two", timestamp: 11 } })}),
					(${SESSION_ID}, ${"three"}, ${"two"}, ${3}, ${"custom"}, ${"note"}, ${12}, ${JSON.stringify({ data: 3 })})`.run(
				db,
			);

			expect(
				(await storage.scanEntries({ order: "desc", type: "custom", fromSeq: 2 }, BACKGROUND_CONTEXT)).map(
					(entry) => entry.id,
				),
			).toEqual(["three"]);
			expect(
				(await storage.scanEntries({ order: "asc", customType: "note", limit: 2 }, BACKGROUND_CONTEXT)).map(
					(entry) => entry.id,
				),
			).toEqual(["one", "three"]);
		});
	});

	it("scans branch entries through materialized branch segments", async () => {
		await withStorage(async (storage, db) => {
			sql`INSERT INTO entries (session_id, id, parent_id, seq, type, custom_type, timestamp, payload)
				VALUES
					(${SESSION_ID}, ${"root"}, ${null}, ${1}, ${"message"}, ${null}, ${10}, ${JSON.stringify({ message: { role: "user", content: "root", timestamp: 10 } })}),
					(${SESSION_ID}, ${"compact"}, ${"root"}, ${2}, ${"compaction"}, ${null}, ${11}, ${JSON.stringify({ summary: "s", retainedTail: [], tokensBefore: 1, fromHook: false })}),
					(${SESSION_ID}, ${"old"}, ${"compact"}, ${3}, ${"message"}, ${null}, ${12}, ${JSON.stringify({ message: { role: "assistant", content: "old", timestamp: 12 } })}),
					(${SESSION_ID}, ${"custom"}, ${"old"}, ${4}, ${"custom"}, ${"note"}, ${13}, ${JSON.stringify({ data: 4 })}),
					(${SESSION_ID}, ${"leaf"}, ${"custom"}, ${5}, ${"message"}, ${null}, ${14}, ${JSON.stringify({ message: { role: "user", content: "leaf", timestamp: 14 } })})`.run(
				db,
			);
			sql`INSERT INTO branch_meta (session_id, branch_id, tip_entry_id, tip_seq, base_branch_id, base_seq)
				VALUES
					(${SESSION_ID}, ${"base"}, ${"compact"}, ${2}, ${null}, ${null}),
					(${SESSION_ID}, ${"new"}, ${"leaf"}, ${5}, ${"base"}, ${2})`.run(db);
			sql`INSERT INTO branch_entries (session_id, branch_id, entry_id, entry_seq, entry_type)
				VALUES
					(${SESSION_ID}, ${"base"}, ${"root"}, ${1}, ${"message"}),
					(${SESSION_ID}, ${"base"}, ${"compact"}, ${2}, ${"compaction"}),
					(${SESSION_ID}, ${"new"}, ${"old"}, ${3}, ${"message"}),
					(${SESSION_ID}, ${"new"}, ${"custom"}, ${4}, ${"custom"}),
					(${SESSION_ID}, ${"new"}, ${"leaf"}, ${5}, ${"message"})`.run(db);

			expect(
				(await storage.scanBranch({ start: "leaf", stopAtType: "compaction", limit: 2 }, BACKGROUND_CONTEXT)).map(
					(entry) => entry.id,
				),
			).toEqual(["leaf", "custom"]);
			expect(
				(
					await storage.scanBranch(
						{ start: "leaf", order: "oldestFirst", type: "message", cursor: { seq: 1 } },
						BACKGROUND_CONTEXT,
					)
				).map((entry) => entry.id),
			).toEqual(["old", "leaf"]);
		});
	});

	it("resolves a branch segment that physically contains the start entry", async () => {
		await withStorage(async (storage, db) => {
			sql`INSERT INTO entries (session_id, id, parent_id, seq, type, custom_type, timestamp, payload)
				VALUES
					(${SESSION_ID}, ${"root"}, ${null}, ${1}, ${"message"}, ${null}, ${10}, ${JSON.stringify({ message: { role: "user", content: "root", timestamp: 10 } })}),
					(${SESSION_ID}, ${"base-tip"}, ${"root"}, ${2}, ${"message"}, ${null}, ${11}, ${JSON.stringify({ message: { role: "assistant", content: "base", timestamp: 11 } })}),
					(${SESSION_ID}, ${"new-tip"}, ${"base-tip"}, ${3}, ${"message"}, ${null}, ${12}, ${JSON.stringify({ message: { role: "user", content: "new", timestamp: 12 } })})`.run(
				db,
			);
			sql`INSERT INTO branch_meta (session_id, branch_id, tip_entry_id, tip_seq, base_branch_id, base_seq)
				VALUES
					(${SESSION_ID}, ${"aaa-new"}, ${"new-tip"}, ${3}, ${"zzz-base"}, ${2}),
					(${SESSION_ID}, ${"zzz-base"}, ${"base-tip"}, ${2}, ${null}, ${null})`.run(db);
			sql`INSERT INTO branch_entries (session_id, branch_id, entry_id, entry_seq, entry_type)
				VALUES
					(${SESSION_ID}, ${"aaa-new"}, ${"new-tip"}, ${3}, ${"message"}),
					(${SESSION_ID}, ${"zzz-base"}, ${"root"}, ${1}, ${"message"}),
					(${SESSION_ID}, ${"zzz-base"}, ${"base-tip"}, ${2}, ${"message"})`.run(db);

			expect(
				(await storage.scanBranch({ start: "base-tip", order: "oldestFirst" }, BACKGROUND_CONTEXT)).map(
					(entry) => entry.id,
				),
			).toEqual(["root", "base-tip"]);
		});
	});

	it("applies branch stop boundaries across base segments before filtering", async () => {
		await withStorage(async (storage, db) => {
			sql`INSERT INTO entries (session_id, id, parent_id, seq, type, custom_type, timestamp, payload)
				VALUES
					(${SESSION_ID}, ${"root"}, ${null}, ${1}, ${"message"}, ${null}, ${10}, ${JSON.stringify({ message: { role: "user", content: "root", timestamp: 10 } })}),
					(${SESSION_ID}, ${"compact"}, ${"root"}, ${2}, ${"compaction"}, ${null}, ${11}, ${JSON.stringify({ summary: "s", retainedTail: [], tokensBefore: 1, fromHook: false })}),
					(${SESSION_ID}, ${"after"}, ${"compact"}, ${3}, ${"message"}, ${null}, ${12}, ${JSON.stringify({ message: { role: "assistant", content: "after", timestamp: 12 } })}),
					(${SESSION_ID}, ${"leaf"}, ${"after"}, ${4}, ${"custom"}, ${"note"}, ${13}, ${JSON.stringify({ data: 4 })})`.run(
				db,
			);
			sql`INSERT INTO branch_meta (session_id, branch_id, tip_entry_id, tip_seq, base_branch_id, base_seq)
				VALUES
					(${SESSION_ID}, ${"base"}, ${"compact"}, ${2}, ${null}, ${null}),
					(${SESSION_ID}, ${"new"}, ${"leaf"}, ${4}, ${"base"}, ${2})`.run(db);
			sql`INSERT INTO branch_entries (session_id, branch_id, entry_id, entry_seq, entry_type)
				VALUES
					(${SESSION_ID}, ${"base"}, ${"root"}, ${1}, ${"message"}),
					(${SESSION_ID}, ${"base"}, ${"compact"}, ${2}, ${"compaction"}),
					(${SESSION_ID}, ${"new"}, ${"after"}, ${3}, ${"message"}),
					(${SESSION_ID}, ${"new"}, ${"leaf"}, ${4}, ${"custom"})`.run(db);

			expect(
				(await storage.scanBranch({ start: "leaf", stopAtType: "compaction" }, BACKGROUND_CONTEXT)).map(
					(entry) => entry.id,
				),
			).toEqual(["leaf", "after", "compact"]);
			expect(
				(
					await storage.scanBranch(
						{ start: "leaf", stopAtType: "compaction", type: "message" },
						BACKGROUND_CONTEXT,
					)
				).map((entry) => entry.id),
			).toEqual(["after"]);
		});
	});

	it("uses branch_entries as the outer scan for branch payload queries", async () => {
		await withStorage(async (_storage, db) => {
			const plan = explainQueryPlan(
				db,
				`SELECT e.id, e.parent_id, e.seq, e.type, e.custom_type, e.timestamp, e.payload
				FROM branch_entries b
				CROSS JOIN entries e ON e.session_id = b.session_id AND e.id = b.entry_id
				WHERE b.session_id = ? AND b.branch_id = ? AND b.entry_seq > ? AND b.entry_seq <= ?
				ORDER BY b.entry_seq DESC LIMIT ?`,
				SESSION_ID,
				"main",
				0,
				10,
				2,
			);

			expectBranchPlan(plan);
		});
	});

	it("scans branch structure without payloads", async () => {
		await withStorage(async (storage, db) => {
			sql`INSERT INTO entries (session_id, id, parent_id, seq, type, custom_type, timestamp, payload)
				VALUES
					(${SESSION_ID}, ${"root"}, ${null}, ${1}, ${"message"}, ${null}, ${10}, ${JSON.stringify({ message: { role: "user", content: "root", timestamp: 10 } })}),
					(${SESSION_ID}, ${"custom"}, ${"root"}, ${2}, ${"custom"}, ${"note"}, ${11}, ${JSON.stringify({ data: 2 })})`.run(
				db,
			);
			sql`INSERT INTO branch_meta (session_id, branch_id, tip_entry_id, tip_seq, base_branch_id, base_seq)
				VALUES (${SESSION_ID}, ${"main"}, ${"custom"}, ${2}, ${null}, ${null})`.run(db);
			sql`INSERT INTO branch_entries (session_id, branch_id, entry_id, entry_seq, entry_type)
				VALUES
					(${SESSION_ID}, ${"main"}, ${"root"}, ${1}, ${"message"}),
					(${SESSION_ID}, ${"main"}, ${"custom"}, ${2}, ${"custom"})`.run(db);

			expect(await storage.scanBranchStructure({ start: "custom", customType: "note" }, BACKGROUND_CONTEXT)).toEqual(
				[
					{
						id: "custom",
						parentId: "root",
						seq: 2,
						timestamp: 11,
						type: "custom",
						customType: "note",
					},
				],
			);
		});
	});

	it("uses branch_entries as the outer scan for branch structure queries", async () => {
		await withStorage(async (_storage, db) => {
			const plan = explainQueryPlan(
				db,
				`SELECT e.id, e.parent_id, e.seq, e.type, e.custom_type, e.timestamp
				FROM branch_entries b
				CROSS JOIN entries e ON e.session_id = b.session_id AND e.id = b.entry_id
				WHERE b.session_id = ? AND b.branch_id = ? AND b.entry_seq > ? AND b.entry_seq <= ?
				ORDER BY b.entry_seq ASC LIMIT ?`,
				SESSION_ID,
				"main",
				0,
				10,
				2,
			);

			expectBranchPlan(plan);
		});
	});

	it("scans decoded usage rows with sequence bounds", async () => {
		await withStorage(async (storage, db) => {
			const usage = {
				input: 1,
				output: 2,
				cacheRead: 3,
				cacheWrite: 4,
				totalTokens: 10,
				cost: { input: 0.1, output: 0.2, cacheRead: 0.3, cacheWrite: 0.4, total: 1 },
			};
			sql`INSERT INTO usage_ledger (session_id, id, seq, entry_id, adjustment, usage, details)
				VALUES
					(${SESSION_ID}, ${"u1"}, ${1}, ${"e1"}, ${0}, ${JSON.stringify(usage)}, ${null}),
					(${SESSION_ID}, ${"u2"}, ${2}, ${null}, ${1}, ${JSON.stringify(usage)}, ${JSON.stringify({ reason: "adjust" })}),
					(${SESSION_ID}, ${"u3"}, ${3}, ${"e3"}, ${0}, ${JSON.stringify(usage)}, ${null})`.run(db);

			expect(await storage.scanUsage({ fromSeq: 2, order: "asc", limit: 1 }, BACKGROUND_CONTEXT)).toEqual([
				{ id: "u2", seq: 2, usage, adjustment: true, details: { reason: "adjust" } },
			]);
			expect(
				(await storage.scanUsage({ toSeq: 2, order: "desc" }, BACKGROUND_CONTEXT)).map((row) => row.id),
			).toEqual(["u2", "u1"]);
		});
	});

	it("prepares committed writes with assigned sequences and timestamp", () => {
		const prepared = prepareStorageCommit(
			[
				sessionWrites.insertEntry({
					id: "entry",
					parentId: null,
					type: "message",
					message: { role: "user", content: "hi", timestamp: 1 },
				}),
				sessionWrites.insertUsage({
					id: "usage",
					usage: {
						input: 1,
						output: 2,
						cacheRead: 0,
						cacheWrite: 0,
						totalTokens: 3,
						cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
					},
					adjustment: false,
				}),
				storedValues.setValue(storedValues.sessionName, "name"),
				storedValues.deleteValue(storedValues.entryLabel("entry")),
			],
			7,
			1_700_000_000_000,
		);

		expect(prepared.result).toEqual({ firstSeq: 7, seqs: [7, 8, 9, 10], timestamp: 1_700_000_000_000 });
		expect(prepared.writes).toMatchObject([
			{ kind: "entry", id: "entry", seq: 7, timestamp: 1_700_000_000_000 },
			{ kind: "usage", id: "usage", seq: 8 },
			storedValues.setValue(storedValues.sessionName, "name"),
			storedValues.deleteValue(storedValues.entryLabel("entry")),
		]);
	});

	it("reads and advances the next commit sequence", async () => {
		await withStorage(async (_storage, db) => {
			sql`INSERT INTO sessions
				(id, created_at, parent_session_id, storage_version, metadata, message_count, usage_payload, next_seq)
				VALUES (${SESSION_ID}, ${1}, ${null}, ${1}, ${null}, ${0}, ${JSON.stringify({})}, ${7})`.run(db);

			expect(readNextSeq(db, SESSION_ID)).toBe(7);
			advanceNextSeq(db, SESSION_ID, 10);
			expect(readNextSeq(db, SESSION_ID)).toBe(10);
		});
	});

	it("gets maintained session stats", async () => {
		await withStorage(async (storage, db) => {
			const usage = {
				input: 1,
				output: 2,
				cacheRead: 3,
				cacheWrite: 4,
				totalTokens: 10,
				cost: { input: 0.1, output: 0.2, cacheRead: 0.3, cacheWrite: 0.4, total: 1 },
			};
			sql`INSERT INTO sessions
				(id, created_at, parent_session_id, storage_version, metadata, message_count, usage_payload, next_seq)
				VALUES (${SESSION_ID}, ${1}, ${null}, ${1}, ${null}, ${2}, ${JSON.stringify(usage)}, ${3})`.run(db);

			expect(await storage.getStats(BACKGROUND_CONTEXT)).toEqual({ messageCount: 2, usage });
			const next = await storage.commit(
				[storedValues.setValue(storedValues.sessionName, "after-history")],
				BACKGROUND_CONTEXT,
			);
			expect(next.stats).toEqual({ messageCount: 2, usage });
		});
	});

	it("includes historical totals in the first commit after storage reopen", async () => {
		await withStorage(async (storage, db) => {
			insertCommitSessionRow(db);
			const usage = {
				input: 1,
				output: 2,
				cacheRead: 0,
				cacheWrite: 0,
				totalTokens: 3,
				cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
			};
			await storage.commit(
				[
					sessionWrites.insertEntry({
						id: "history",
						parentId: null,
						type: "message",
						message: { role: "user", content: "history", timestamp: 1 },
					}),
					sessionWrites.insertUsage({ id: "usage", usage, adjustment: false }),
				],
				BACKGROUND_CONTEXT,
			);
			await storage.close(BACKGROUND_CONTEXT);

			const reopened = new SqliteStorage(db, { sessionId: SESSION_ID, now: () => 1_700_000_000_000 });
			const result = await reopened.commit(
				[storedValues.setValue(storedValues.sessionName, "reopened")],
				BACKGROUND_CONTEXT,
			);
			expect(result.stats).toEqual({ messageCount: 1, usage });
			expect(result.stats).toEqual(await reopened.getStats(BACKGROUND_CONTEXT));
			await reopened.close(BACKGROUND_CONTEXT);
		});
	});

	it("gets a decoded scalar value by bound address", async () => {
		await withStorage(async (storage, db) => {
			const address = storedValues.value<{ ready: boolean }>("test.value", "state");
			sql`INSERT INTO scalar_values (session_id, namespace, key, seq, value)
				VALUES (${SESSION_ID}, ${address.namespace}, ${address.key}, ${1}, ${JSON.stringify({ ready: true })})`.run(
				db,
			);

			expect(await storage.getValue(address, BACKGROUND_CONTEXT)).toEqual({
				address,
				seq: 1,
				value: { ready: true },
			});
			expect(
				await storage.getValue(storedValues.value<unknown>("test.value", "missing"), BACKGROUND_CONTEXT),
			).toBeUndefined();
		});
	});

	it("scans decoded scalar values by namespace and key prefix", async () => {
		await withStorage(async (storage, db) => {
			sql`INSERT INTO scalar_values (session_id, namespace, key, seq, value)
				VALUES
					(${SESSION_ID}, ${"test.value"}, ${"app:one"}, ${1}, ${JSON.stringify(1)}),
					(${SESSION_ID}, ${"test.value"}, ${"app:two"}, ${2}, ${JSON.stringify(2)}),
					(${SESSION_ID}, ${"test.value"}, ${"app:\ufffftail"}, ${3}, ${JSON.stringify(3)}),
					(${SESSION_ID}, ${"test.value"}, ${"app;other"}, ${4}, ${JSON.stringify(4)}),
					(${SESSION_ID}, ${"test.value"}, ${"other"}, ${5}, ${JSON.stringify(5)}),
					(${SESSION_ID}, ${"other.value"}, ${""}, ${6}, ${JSON.stringify("name")})`.run(db);

			expect(
				await storage.scanValues(storedValues.value<unknown>("test.value", "app:"), BACKGROUND_CONTEXT),
			).toEqual([
				{ address: storedValues.value<unknown>("test.value", "app:one"), seq: 1, value: 1 },
				{ address: storedValues.value<unknown>("test.value", "app:two"), seq: 2, value: 2 },
				{ address: storedValues.value<unknown>("test.value", "app:\ufffftail"), seq: 3, value: 3 },
			]);
		});
	});

	it("uses the list primary key for ascending and descending cursor pages", async () => {
		await withStorage(async (_storage, db) => {
			const address = storedValues.list<unknown>("test.list", "events");
			for (const options of [
				{ order: "asc" as const },
				{ order: "asc" as const, cursor: { seq: 4 } },
				{ order: "desc" as const },
				{ order: "desc" as const, cursor: { seq: 4 } },
			]) {
				const query = listValueReadQuery(SESSION_ID, address, options);
				const plan = explainQueryPlan(db, query.queryText, ...query.params);
				expect(plan.some((detail) => detail.includes("USING PRIMARY KEY"))).toBe(true);
				expect(plan.some((detail) => detail.includes("SCAN list_values"))).toBe(false);
				expect(plan.some((detail) => detail.includes("USE TEMP B-TREE"))).toBe(false);
			}
		});
	});
});