@cueloop/schema 0.1.0-alpha.7 → 0.1.0-alpha.71

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,395 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import {
3
+ appendEntry,
4
+ createBranch,
5
+ derivePath,
6
+ followBranch,
7
+ forkHistory,
8
+ historyFromLinear,
9
+ HistoryError,
10
+ labelTip,
11
+ MAIN_BRANCH,
12
+ navigateTo,
13
+ pathOf,
14
+ recaptureMainHead,
15
+ removalEntries,
16
+ switchBranch,
17
+ tipOf,
18
+ validateHistory,
19
+ type SessionHistory,
20
+ } from "./history";
21
+ import type { Annotation, Verdict } from "./types";
22
+
23
+ const AT = "2026-09-01T10:00:00.000Z";
24
+ const VERDICT: Verdict = { kind: "approve", summary: "", feedback: "", resolvedAt: AT };
25
+
26
+ function annotation(id: string, createdAt: string): Annotation {
27
+ return {
28
+ id,
29
+ kind: "comment",
30
+ anchor: { quote: "q", prefix: "", suffix: "" },
31
+ body: id,
32
+ createdAt,
33
+ };
34
+ }
35
+
36
+ /** A fresh history: one agent revision on main. */
37
+ function root(): SessionHistory {
38
+ return historyFromLinear({
39
+ id: "ses_1",
40
+ revisions: [{ revision: 1, content: "Plan v1", submittedAt: AT }],
41
+ annotations: [],
42
+ verdict: null,
43
+ createdAt: AT,
44
+ });
45
+ }
46
+
47
+ describe("appendEntry and derivePath", () => {
48
+ test("entries chain on the current tip and the path derives the head, comments, and verdicts", () => {
49
+ // Arrange
50
+ let history = root();
51
+
52
+ // Act
53
+ history = appendEntry(history, { type: "comment", annotationId: "a1", createdAt: AT }).history;
54
+ history = appendEntry(history, { type: "comment", annotationId: "a2", createdAt: AT }).history;
55
+ history = appendEntry(history, {
56
+ type: "comment-removed",
57
+ annotationId: "a1",
58
+ createdAt: AT,
59
+ }).history;
60
+ history = appendEntry(history, { type: "verdict", verdict: VERDICT, createdAt: AT }).history;
61
+ history = appendEntry(history, {
62
+ type: "revision",
63
+ by: "agent",
64
+ content: "Plan v2",
65
+ createdAt: AT,
66
+ }).history;
67
+
68
+ // Assert
69
+ const derived = derivePath(history);
70
+
71
+ expect(derived.head.content).toBe("Plan v2");
72
+ expect(derived.annotationIds).toEqual(["a2"]);
73
+ expect(derived.verdicts).toEqual([VERDICT]);
74
+ expect(derived.rounds).toBe(2);
75
+ expect(pathOf(history).map((entry) => entry.type)).toEqual([
76
+ "revision",
77
+ "comment",
78
+ "comment",
79
+ "comment-removed",
80
+ "verdict",
81
+ "revision",
82
+ ]);
83
+ });
84
+ });
85
+
86
+ describe("branches and navigation", () => {
87
+ test("a branch starts at the tip, ramble stays off main, navigating back leaves a summary", () => {
88
+ // Arrange
89
+ let history = labelTip(root(), "before ramble");
90
+ const checkpoint = tipOf(history);
91
+
92
+ history = createBranch(history, "ramble");
93
+ history = appendEntry(history, { type: "comment", annotationId: "r1", createdAt: AT }).history;
94
+ history = appendEntry(history, {
95
+ type: "revision",
96
+ by: "reviewer",
97
+ content: "Plan v1 (edited)",
98
+ createdAt: AT,
99
+ }).history;
100
+ expect(derivePath(history).head.content).toBe("Plan v1 (edited)");
101
+ expect(tipOf(history, MAIN_BRANCH)).toBe(checkpoint);
102
+
103
+ // Act
104
+ history = navigateTo(history, checkpoint, {
105
+ summary: "explored dropping the daemon",
106
+ createdAt: AT,
107
+ });
108
+
109
+ // Assert: the ramble's entries stay, the tip sits on the summary at the checkpoint
110
+ expect(history.entries).toHaveLength(4);
111
+ const derived = derivePath(history);
112
+
113
+ expect(derived.head.content).toBe("Plan v1");
114
+ expect(derived.annotationIds).toEqual([]);
115
+ expect(derived.summaries[0]!.abandoned).toHaveLength(2);
116
+ expect(history.labels[checkpoint]).toBe("before ramble");
117
+
118
+ // Act
119
+ history = switchBranch(history, MAIN_BRANCH);
120
+
121
+ // Assert
122
+ expect(derivePath(history).summaries).toEqual([]);
123
+ });
124
+
125
+ test("navigating without a summary just moves the tip", () => {
126
+ // Arrange
127
+ let history = root();
128
+ const checkpoint = tipOf(history);
129
+
130
+ history = appendEntry(history, { type: "comment", annotationId: "a1", createdAt: AT }).history;
131
+
132
+ // Act
133
+ history = navigateTo(history, checkpoint);
134
+
135
+ // Assert
136
+ expect(tipOf(history)).toBe(checkpoint);
137
+ expect(history.entries).toHaveLength(2);
138
+ });
139
+
140
+ test("refusals: unknown entry, the current tip, an entry off the branch, a duplicate branch", () => {
141
+ // Arrange
142
+ let history = root();
143
+ const checkpoint = tipOf(history);
144
+
145
+ history = createBranch(history, "ramble");
146
+ history = appendEntry(history, { type: "comment", annotationId: "r1", createdAt: AT }).history;
147
+ const rambleTip = tipOf(history);
148
+
149
+ history = switchBranch(history, MAIN_BRANCH);
150
+
151
+ // Assert
152
+ expect(() => navigateTo(history, "nope")).toThrow(HistoryError);
153
+ expect(() => navigateTo(history, checkpoint)).toThrow(/already/);
154
+ expect(() => navigateTo(history, rambleTip)).toThrow(/not on branch/);
155
+ expect(() => createBranch(history, "ramble")).toThrow(/exists/);
156
+ expect(() => switchBranch(history, "ghost")).toThrow(/no branch/);
157
+ });
158
+ });
159
+
160
+ describe("validateHistory and cycles", () => {
161
+ test("a healthy tree validates; each broken invariant names itself", () => {
162
+ // Arrange
163
+ const healthy = root();
164
+ const entry = healthy.entries[0]!;
165
+
166
+ // Assert
167
+ expect(validateHistory(healthy)).toBeNull();
168
+ expect(validateHistory({ ...healthy, entries: [entry, { ...entry }] })).toMatch(
169
+ /duplicate entry id/,
170
+ );
171
+ expect(validateHistory({ ...healthy, entries: [{ ...entry, parentId: "ghost" }] })).toMatch(
172
+ /missing parent/,
173
+ );
174
+ expect(validateHistory({ ...healthy, tips: {} })).toMatch(/no "main" branch/);
175
+ expect(validateHistory({ ...healthy, tips: { main: "ghost" } })).toMatch(/missing entry/);
176
+ expect(validateHistory({ ...healthy, branch: "ramble" })).toMatch(/has no tip/);
177
+ expect(
178
+ validateHistory({
179
+ ...healthy,
180
+ entries: [{ id: "c", parentId: null, type: "comment", annotationId: "a", createdAt: AT }],
181
+ tips: { main: "c" },
182
+ }),
183
+ ).toMatch(/no revision/);
184
+ });
185
+
186
+ test("a parent cycle is refused instead of walked forever", () => {
187
+ // Arrange: two entries that point at each other, off the main path
188
+ const healthy = root();
189
+ const loopA = {
190
+ id: "la",
191
+ parentId: "lb",
192
+ type: "comment",
193
+ annotationId: "x",
194
+ createdAt: AT,
195
+ } as const;
196
+ const loopB = {
197
+ id: "lb",
198
+ parentId: "la",
199
+ type: "comment",
200
+ annotationId: "y",
201
+ createdAt: AT,
202
+ } as const;
203
+ const cyclic = { ...healthy, entries: [...healthy.entries, loopA, loopB] };
204
+
205
+ // Assert
206
+ expect(validateHistory(cyclic)).toMatch(/own ancestor/);
207
+ expect(() => pathOf(cyclic, "la")).toThrow(HistoryError);
208
+ });
209
+ });
210
+
211
+ describe("forkHistory", () => {
212
+ test("copies the path with its comments and labels, drops verdicts, starts at the head", () => {
213
+ // Arrange
214
+ let history = root();
215
+
216
+ history = appendEntry(history, { type: "comment", annotationId: "a1", createdAt: AT }).history;
217
+ history = appendEntry(history, { type: "verdict", verdict: VERDICT, createdAt: AT }).history;
218
+ history = labelTip(history, "approved v1");
219
+ const approvedTip = tipOf(history);
220
+
221
+ history = createBranch(history, "other");
222
+ history = appendEntry(history, { type: "comment", annotationId: "off", createdAt: AT }).history;
223
+ history = switchBranch(history, MAIN_BRANCH);
224
+
225
+ // Act
226
+ const fork = forkHistory(history);
227
+
228
+ // Assert
229
+ expect(fork.entries.map((entry) => entry.type)).toEqual(["revision", "comment"]);
230
+ expect(fork.tips).toEqual({ [MAIN_BRANCH]: fork.entries[1]!.id });
231
+ expect(derivePath(fork).annotationIds).toEqual(["a1"]);
232
+ // the label sat on the dropped verdict entry, so it does not travel
233
+ expect(fork.labels).toEqual({});
234
+ expect(history.labels[approvedTip]).toBe("approved v1");
235
+ });
236
+
237
+ test("entries after a dropped one are chained to the kept entry before it", () => {
238
+ // Arrange: rev1, verdict, rev2 by the agent, a reviewer edit, a comment on top
239
+ let history = root();
240
+
241
+ history = appendEntry(history, { type: "verdict", verdict: VERDICT, createdAt: AT }).history;
242
+ history = appendEntry(history, {
243
+ type: "revision",
244
+ by: "agent",
245
+ content: "Plan v2",
246
+ createdAt: AT,
247
+ }).history;
248
+ history = appendEntry(history, {
249
+ type: "revision",
250
+ by: "reviewer",
251
+ content: "Plan v2 edited",
252
+ createdAt: AT,
253
+ }).history;
254
+ history = appendEntry(history, { type: "comment", annotationId: "a1", createdAt: AT }).history;
255
+
256
+ // Act
257
+ const fork = forkHistory(history);
258
+
259
+ // Assert: a valid one-branch tree with the agent's head and the comment, edits reset
260
+ expect(validateHistory(fork)).toBeNull();
261
+ expect(fork.entries.map((entry) => entry.type)).toEqual(["revision", "revision", "comment"]);
262
+ expect(derivePath(fork).head.content).toBe("Plan v2");
263
+ expect(derivePath(fork).annotationIds).toEqual(["a1"]);
264
+ });
265
+ });
266
+
267
+ describe("recaptureMainHead", () => {
268
+ test("replaces the agent's current revision on main in place, wherever the reviewer is", () => {
269
+ // Arrange: main at rev1, a reviewer edit on a branch
270
+ let history = createBranch(root(), "alt");
271
+
272
+ history = appendEntry(history, {
273
+ type: "revision",
274
+ by: "reviewer",
275
+ content: "Plan v1 edited",
276
+ createdAt: AT,
277
+ }).history;
278
+
279
+ // Act
280
+ const recaptured = recaptureMainHead(history, "Plan v1 recaptured");
281
+
282
+ // Assert: same entry count, main's head text moved, the branch keeps its edit
283
+ expect(recaptured.entries).toHaveLength(history.entries.length);
284
+ expect(derivePath(recaptured, tipOf(recaptured, MAIN_BRANCH)).head.content).toBe(
285
+ "Plan v1 recaptured",
286
+ );
287
+ expect(derivePath(recaptured).head.content).toBe("Plan v1 edited");
288
+ });
289
+ });
290
+
291
+ describe("historyFromLinear", () => {
292
+ test("chains revisions on main, files comments after the revision they were made on, ends with the verdict", () => {
293
+ // Act
294
+ const history = historyFromLinear({
295
+ id: "ses_9",
296
+ revisions: [
297
+ { revision: 1, content: "v1", submittedAt: "2026-09-01T10:00:00.000Z" },
298
+ { revision: 2, content: "v2", submittedAt: "2026-09-01T12:00:00.000Z" },
299
+ ],
300
+ annotations: [
301
+ annotation("late", "2026-09-01T13:00:00.000Z"),
302
+ annotation("early", "2026-09-01T11:00:00.000Z"),
303
+ ],
304
+ verdict: VERDICT,
305
+ createdAt: "2026-09-01T09:00:00.000Z",
306
+ });
307
+
308
+ // Assert
309
+ expect(
310
+ history.entries.map(
311
+ (entry) => `${entry.type}:${"annotationId" in entry ? entry.annotationId : ""}`,
312
+ ),
313
+ ).toEqual(["revision:", "comment:early", "revision:", "comment:late", "verdict:"]);
314
+ expect(history.entries[0]!.id).toBe("ses_9_rev1");
315
+ expect(history.branch).toBe(MAIN_BRANCH);
316
+ const derived = derivePath(history);
317
+
318
+ expect(derived.head.content).toBe("v2");
319
+ expect(derived.annotationIds).toEqual(["early", "late"]);
320
+ expect(derived.verdicts).toEqual([VERDICT]);
321
+ expect(derived.rounds).toBe(2);
322
+ });
323
+
324
+ test("is deterministic: the same record migrates to the same tree", () => {
325
+ // Arrange
326
+ const record = {
327
+ id: "ses_2",
328
+ revisions: [{ revision: 1, content: "v1", submittedAt: AT }],
329
+ annotations: [annotation("a", AT)],
330
+ verdict: null,
331
+ createdAt: AT,
332
+ };
333
+
334
+ // Assert
335
+ expect(historyFromLinear(record)).toEqual(historyFromLinear(record));
336
+ });
337
+ });
338
+
339
+ describe("followBranch", () => {
340
+ test("carries one branch's path and its labels, dropping other branches", () => {
341
+ // Arrange: main has rev1 -> a1 (labelled); a branch off a1 adds a comment
342
+ let history = historyFromLinear({
343
+ id: "ses_1",
344
+ revisions: [{ revision: 1, content: "v1", submittedAt: AT }],
345
+ annotations: [],
346
+ verdict: null,
347
+ createdAt: AT,
348
+ });
349
+
350
+ history = appendEntry(history, { type: "comment", annotationId: "a1", createdAt: AT }).history;
351
+ history = labelTip(history, "checkpoint");
352
+ const labelled = tipOf(history);
353
+
354
+ history = createBranch(history, "alt");
355
+ history = appendEntry(history, { type: "comment", annotationId: "b1", createdAt: AT }).history;
356
+
357
+ // Act: a share following main sees only main's path
358
+ const followed = followBranch(history, MAIN_BRANCH);
359
+
360
+ // Assert
361
+ expect(followed.branch).toBe(MAIN_BRANCH);
362
+ expect(followed.tips).toEqual({ [MAIN_BRANCH]: labelled });
363
+ expect(followed.entries.map((entry) => entry.type)).toEqual(["revision", "comment"]);
364
+ expect(followed.labels).toEqual({ [labelled]: "checkpoint" });
365
+ expect(validateHistory(followed)).toBeNull();
366
+ });
367
+ });
368
+
369
+ describe("removalEntries", () => {
370
+ test("returns the comment-removed entries in order, and nothing else", () => {
371
+ // Arrange
372
+ let history = historyFromLinear({
373
+ id: "ses_1",
374
+ revisions: [{ revision: 1, content: "v1", submittedAt: AT }],
375
+ annotations: [],
376
+ verdict: null,
377
+ createdAt: AT,
378
+ });
379
+
380
+ history = appendEntry(history, { type: "comment", annotationId: "a1", createdAt: AT }).history;
381
+ history = appendEntry(history, { type: "comment", annotationId: "a2", createdAt: AT }).history;
382
+ history = appendEntry(history, {
383
+ type: "comment-removed",
384
+ annotationId: "a1",
385
+ createdAt: AT,
386
+ }).history;
387
+
388
+ // Act
389
+ const removals = removalEntries(history);
390
+
391
+ // Assert
392
+ expect(removals.map((entry) => entry.annotationId)).toEqual(["a1"]);
393
+ expect(removals.every((entry) => entry.type === "comment-removed")).toBe(true);
394
+ });
395
+ });