@cueloop/schema 0.1.0-alpha.8 → 0.1.0-alpha.81

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,201 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import {
3
+ appendEntry,
4
+ createBranch,
5
+ historyFromLinear,
6
+ navigateTo,
7
+ switchBranch,
8
+ type SessionHistory,
9
+ } from "./history";
10
+ import { applyPathView, viewFollowing, viewOfPath } from "./path-view";
11
+ import type { Annotation, Thread } from "./types";
12
+
13
+ const AT = "2026-09-01T10:00:00.000Z";
14
+
15
+ function annotation(id: string): Annotation {
16
+ return {
17
+ id,
18
+ kind: "comment",
19
+ anchor: { quote: "q", prefix: "", suffix: "" },
20
+ body: id,
21
+ createdAt: AT,
22
+ };
23
+ }
24
+
25
+ /** rev1 (agent) -> comment a1 -> reviewer edit -> comment a2, all on main. */
26
+ function threaded(): SessionHistory {
27
+ let history = historyFromLinear({
28
+ id: "ses_1",
29
+ revisions: [{ revision: 1, content: "Plan v1", submittedAt: AT }],
30
+ annotations: [],
31
+ message: null,
32
+ createdAt: AT,
33
+ });
34
+
35
+ history = appendEntry(history, { type: "comment", annotationId: "a1", createdAt: AT }).history;
36
+ history = appendEntry(history, {
37
+ type: "revision",
38
+ by: "reviewer",
39
+ content: "Plan v1 edited",
40
+ createdAt: AT,
41
+ }).history;
42
+ history = appendEntry(history, { type: "comment", annotationId: "a2", createdAt: AT }).history;
43
+
44
+ return history;
45
+ }
46
+
47
+ describe("viewOfPath", () => {
48
+ test("the artifact is the agent's last revision and the reviewer's head is the working copy", () => {
49
+ // Act
50
+ const view = viewOfPath(threaded(), [annotation("a1"), annotation("a2")]);
51
+
52
+ // Assert
53
+ expect(view.content).toBe("Plan v1");
54
+ expect(view.workingCopy).toBe("Plan v1 edited");
55
+ expect(view.annotations.map((entry) => entry.id)).toEqual(["a1", "a2"]);
56
+ expect(view.shelvedAnnotations).toEqual([]);
57
+ });
58
+
59
+ test("navigating back shelves the comments the path no longer reaches, and forward again unshelves them", () => {
60
+ // Arrange: a branch that stays at the full path, main moved back to the first comment
61
+ let history = createBranch(threaded(), "full");
62
+ const known = [annotation("a1"), annotation("a2")];
63
+
64
+ history = switchBranch(history, "main");
65
+ history = navigateTo(history, history.entries[1]!.id);
66
+
67
+ // Act
68
+ const back = viewOfPath(history, known);
69
+ const forward = viewOfPath(
70
+ switchBranch(history, "full"),
71
+ back.annotations.concat(back.shelvedAnnotations),
72
+ );
73
+
74
+ // Assert
75
+ expect(back.workingCopy).toBeUndefined();
76
+ expect(back.annotations.map((entry) => entry.id)).toEqual(["a1"]);
77
+ expect(back.shelvedAnnotations.map((entry) => entry.id)).toEqual(["a2"]);
78
+ expect(forward.annotations.map((entry) => entry.id)).toEqual(["a1", "a2"]);
79
+ expect(forward.shelvedAnnotations).toEqual([]);
80
+ });
81
+
82
+ test("a removed comment stays shelved, not deleted", () => {
83
+ // Arrange
84
+ let history = threaded();
85
+
86
+ history = appendEntry(history, {
87
+ type: "comment-removed",
88
+ annotationId: "a1",
89
+ createdAt: AT,
90
+ }).history;
91
+
92
+ // Act
93
+ const view = viewOfPath(history, [annotation("a1"), annotation("a2")]);
94
+
95
+ // Assert
96
+ expect(view.annotations.map((entry) => entry.id)).toEqual(["a2"]);
97
+ expect(view.shelvedAnnotations.map((entry) => entry.id)).toEqual(["a1"]);
98
+ });
99
+ });
100
+
101
+ describe("applyPathView", () => {
102
+ test("writes the view into the record and removes the fields the view leaves empty", () => {
103
+ // Arrange
104
+ const session: Thread = {
105
+ schemaVersion: "1",
106
+ id: "ses_1",
107
+ workspace: { repoRoot: "/repo", branch: "main" },
108
+ artifact: { type: "plan", content: "old", meta: {} },
109
+ revisions: [],
110
+ annotations: [annotation("a2")],
111
+ shelvedAnnotations: [annotation("a1")],
112
+ workingCopy: "edited",
113
+ message: null,
114
+ status: "pending",
115
+ createdAt: AT,
116
+ };
117
+
118
+ // Act
119
+ applyPathView(session, {
120
+ content: "Plan v1",
121
+ workingCopy: undefined,
122
+ annotations: [annotation("a1"), annotation("a2")],
123
+ shelvedAnnotations: [],
124
+ });
125
+
126
+ // Assert
127
+ expect(session.artifact.content).toBe("Plan v1");
128
+ expect(session.annotations.map((entry) => entry.id)).toEqual(["a1", "a2"]);
129
+ expect("workingCopy" in session).toBe(false);
130
+ expect("shelvedAnnotations" in session).toBe(false);
131
+ });
132
+ });
133
+
134
+ describe("viewFollowing", () => {
135
+ test("a share carries one branch's path: its entries, tip, labels, and open comments, nothing shelved", () => {
136
+ // Arrange: main has a1 and an edit; a branch off a1 has its own comment; the owner stands on the branch
137
+ let history = threaded();
138
+
139
+ history = navigateTo(history, history.entries[1]!.id);
140
+ history = createBranch(history, "alt");
141
+ history = appendEntry(history, { type: "comment", annotationId: "b1", createdAt: AT }).history;
142
+ const session: Thread = {
143
+ schemaVersion: "1",
144
+ id: "ses_1",
145
+ workspace: { repoRoot: "/repo", branch: "main" },
146
+ artifact: { type: "plan", content: "Plan v1", meta: {} },
147
+ revisions: [],
148
+ annotations: [annotation("a1"), annotation("b1")],
149
+ shelvedAnnotations: [annotation("a2")],
150
+ workingCopy: "Plan v1 edited",
151
+ history,
152
+ message: null,
153
+ status: "pending",
154
+ createdAt: AT,
155
+ };
156
+
157
+ // Act
158
+ const shared = viewFollowing(session);
159
+
160
+ // Assert: main's path only, back at the first comment, no working copy, no shelf, the branch named
161
+ expect(shared.shareBranch).toBe("main");
162
+ expect(shared.history!.tips).toEqual({ main: history.entries[1]!.id });
163
+ expect(shared.history!.entries.map((entry) => entry.type)).toEqual(["revision", "comment"]);
164
+ expect(shared.annotations.map((entry) => entry.id)).toEqual(["a1"]);
165
+ expect("shelvedAnnotations" in shared).toBe(false);
166
+ expect("workingCopy" in shared).toBe(false);
167
+ // the owner's record is untouched
168
+ expect(session.annotations.map((entry) => entry.id)).toEqual(["a1", "b1"]);
169
+ });
170
+
171
+ test("a followed branch that no longer exists falls back to main", () => {
172
+ // Arrange: a plain main-only history, asked to follow a branch that is not there
173
+ const history = historyFromLinear({
174
+ id: "ses_1",
175
+ revisions: [{ revision: 1, content: "Plan v1", submittedAt: AT }],
176
+ annotations: [],
177
+ message: null,
178
+ createdAt: AT,
179
+ });
180
+ const session: Thread = {
181
+ schemaVersion: "1",
182
+ id: "ses_1",
183
+ workspace: { repoRoot: "/repo", branch: "main" },
184
+ artifact: { type: "plan", content: "Plan v1", meta: {} },
185
+ revisions: [],
186
+ annotations: [],
187
+ shareBranch: "gone",
188
+ history,
189
+ message: null,
190
+ status: "pending",
191
+ createdAt: AT,
192
+ };
193
+
194
+ // Act
195
+ const shared = viewFollowing(session);
196
+
197
+ // Assert: it does not throw, and falls back to main
198
+ expect(shared.shareBranch).toBe("main");
199
+ expect(shared.history!.branch).toBe("main");
200
+ });
201
+ });
@@ -0,0 +1,85 @@
1
+ /**
2
+ * The linear view of a session's active path. Every client reads the artifact
3
+ * text, the working copy, and the comments straight off the record, so the
4
+ * record keeps them materialized; this module recomputes them from the path
5
+ * whenever a tip moves. Comments the path does not reach are shelved, never
6
+ * dropped, so a later move can show them again.
7
+ */
8
+
9
+ import { derivePath, followBranch, MAIN_BRANCH, pathOf, type SessionHistory } from "./history";
10
+ import type { Annotation, Thread } from "./types";
11
+
12
+ export interface PathView {
13
+ /** The last agent revision on the path: what the artifact shows. */
14
+ content: string;
15
+ /** The reviewer's edits over it, when the path's head is a reviewer revision. */
16
+ workingCopy: string | undefined;
17
+ /** The comments open on the path, in path order. */
18
+ annotations: Annotation[];
19
+ /** Every other comment the session knows. */
20
+ shelvedAnnotations: Annotation[];
21
+ }
22
+
23
+ /**
24
+ * Fold the active path over every comment the session holds, on or off the
25
+ * path. Comments on the path whose objects are gone stay gone.
26
+ */
27
+ export function viewOfPath(history: SessionHistory, known: Annotation[]): PathView {
28
+ const derived = derivePath(history);
29
+ const agentHead = pathOf(history)
30
+ .filter((entry) => entry.type === "revision" && entry.by === "agent")
31
+ .at(-1);
32
+ const content = agentHead?.type === "revision" ? agentHead.content : derived.head.content;
33
+ const byId = new Map(known.map((annotation) => [annotation.id, annotation]));
34
+ const annotations: Annotation[] = [];
35
+
36
+ for (const id of derived.annotationIds) {
37
+ const annotation = byId.get(id);
38
+
39
+ if (annotation) {
40
+ annotations.push(annotation);
41
+ byId.delete(id);
42
+ }
43
+ }
44
+
45
+ return {
46
+ content,
47
+ workingCopy: derived.head.content === content ? undefined : derived.head.content,
48
+ annotations,
49
+ shelvedAnnotations: [...byId.values()],
50
+ };
51
+ }
52
+
53
+ /** Write a path view back into a record, in place; absent fields are removed. */
54
+ export function applyPathView(session: Thread, view: PathView): void {
55
+ session.artifact = { ...session.artifact, content: view.content };
56
+ session.annotations = view.annotations;
57
+ if (view.workingCopy === undefined) delete session.workingCopy;
58
+ else session.workingCopy = view.workingCopy;
59
+ if (view.shelvedAnnotations.length === 0) delete session.shelvedAnnotations;
60
+ else session.shelvedAnnotations = view.shelvedAnnotations;
61
+ }
62
+
63
+ /**
64
+ * The record as a share carries it: the followed branch's path and what it
65
+ * shows, with no other branch, no shelved comment, and no working copy of the
66
+ * owner's. A followed branch that no longer exists falls back to main. A record
67
+ * without a history travels as it is.
68
+ */
69
+ export function viewFollowing(session: Thread, branch?: string): Thread {
70
+ const asked = branch ?? session.shareBranch ?? MAIN_BRANCH;
71
+ // a branch that no longer exists falls back to main, matching the daemon
72
+ const followed =
73
+ session.history && session.history.tips[asked] === undefined ? MAIN_BRANCH : asked;
74
+ const shared: Thread = { ...session, shareBranch: followed };
75
+
76
+ if (!session.history) return shared;
77
+ const history = followBranch(session.history, followed);
78
+ const known = [...session.annotations, ...(session.shelvedAnnotations ?? [])];
79
+
80
+ shared.history = history;
81
+ applyPathView(shared, viewOfPath(history, known));
82
+ delete shared.shelvedAnnotations;
83
+
84
+ return shared;
85
+ }
@@ -0,0 +1,20 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { isShareViewerAllowed } from "./share-access";
3
+
4
+ describe("isShareViewerAllowed", () => {
5
+ test("an unauthenticated viewer is refused", () => {
6
+ expect(isShareViewerAllowed({ githubLogins: ["octocat"] }, undefined)).toBe(false);
7
+ });
8
+
9
+ test("a member is admitted, case-insensitively", () => {
10
+ expect(isShareViewerAllowed({ githubLogins: ["OctoCat"] }, "octocat")).toBe(true);
11
+ });
12
+
13
+ test("a non-member is refused", () => {
14
+ expect(isShareViewerAllowed({ githubLogins: ["octocat"] }, "hubot")).toBe(false);
15
+ });
16
+
17
+ test("an empty allowlist admits no one", () => {
18
+ expect(isShareViewerAllowed({ githubLogins: [] }, "octocat")).toBe(false);
19
+ });
20
+ });
@@ -0,0 +1,13 @@
1
+ /** Whether a github login may view a private share; membership is case-insensitive and an unauthenticated viewer is never allowed. */
2
+
3
+ import type { ShareAccess } from "./types";
4
+
5
+ export function isShareViewerAllowed(
6
+ access: ShareAccess,
7
+ githubLogin: string | undefined,
8
+ ): boolean {
9
+ if (githubLogin === undefined) return false;
10
+ const login = githubLogin.toLowerCase();
11
+
12
+ return access.githubLogins.some((allowed) => allowed.toLowerCase() === login);
13
+ }
@@ -0,0 +1,63 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { SCHEMA_VERSION, type Thread } from "./types";
3
+ import { threadShareLinks, withShareLinks } from "./share-links";
4
+
5
+ function thread(overrides: Partial<Thread> = {}): Thread {
6
+ return {
7
+ schemaVersion: SCHEMA_VERSION,
8
+ id: "ses_1",
9
+ workspace: { repoRoot: "/repo", branch: "main" },
10
+ artifact: { type: "plan", content: "# Plan\n", meta: {} },
11
+ revisions: [{ revision: 1, content: "# Plan\n", submittedAt: "2026-01-01T00:00:00.000Z" }],
12
+ annotations: [],
13
+ message: null,
14
+ status: "pending",
15
+ createdAt: "2026-01-01T00:00:00.000Z",
16
+ ...overrides,
17
+ };
18
+ }
19
+
20
+ describe("share-link migration", () => {
21
+ test("a never-shared thread has no links", () => {
22
+ expect(threadShareLinks(thread())).toBeUndefined();
23
+ expect(withShareLinks(thread()).shares).toBeUndefined();
24
+ });
25
+
26
+ test("a legacy private share migrates into a one-element list", () => {
27
+ const legacy = thread({
28
+ shareId: "p_abc123xy",
29
+ access: { githubLogins: ["octocat"] },
30
+ owner: "SHA256:owner",
31
+ shareBranch: "feature",
32
+ });
33
+
34
+ expect(withShareLinks(legacy).shares).toEqual([
35
+ {
36
+ id: "p_abc123xy",
37
+ requireAuth: true,
38
+ allowlist: ["octocat"],
39
+ owner: "SHA256:owner",
40
+ shareBranch: "feature",
41
+ },
42
+ ]);
43
+ });
44
+
45
+ test("a legacy public share migrates as requireAuth false with an empty allowlist", () => {
46
+ const legacy = thread({ shareId: "p_public00", owner: "SHA256:owner" });
47
+ const [link] = withShareLinks(legacy).shares!;
48
+
49
+ expect(link).toMatchObject({ id: "p_public00", requireAuth: false, allowlist: [] });
50
+ });
51
+
52
+ test("a thread that already has shares is returned untouched", () => {
53
+ const modern = thread({
54
+ shares: [{ id: "p_modern00", name: "team", requireAuth: false, allowlist: [] }],
55
+ // stale legacy fields must not override the explicit shares
56
+ shareId: "p_legacy00",
57
+ });
58
+
59
+ expect(withShareLinks(modern).shares).toEqual([
60
+ { id: "p_modern00", name: "team", requireAuth: false, allowlist: [] },
61
+ ]);
62
+ });
63
+ });
@@ -0,0 +1,29 @@
1
+ import type { ShareLink, Thread } from "./types";
2
+
3
+ /**
4
+ * A thread's share links, migrating a legacy single share (scalar shareId +
5
+ * access + owner + shareBranch) into a one-element list. Undefined when the
6
+ * thread was never shared. Records already carrying `shares` are returned as-is.
7
+ */
8
+ export function threadShareLinks(session: Thread): ShareLink[] | undefined {
9
+ if (session.shares !== undefined) return session.shares;
10
+ if (session.shareId === undefined) return undefined;
11
+
12
+ return [
13
+ {
14
+ id: session.shareId,
15
+ requireAuth: session.access !== undefined,
16
+ allowlist: session.access?.githubLogins ?? [],
17
+ owner: session.owner,
18
+ shareBranch: session.shareBranch,
19
+ },
20
+ ];
21
+ }
22
+
23
+ /** The thread with `shares` populated from the legacy fields when absent; a never-shared thread is unchanged. */
24
+ export function withShareLinks(session: Thread): Thread {
25
+ if (session.shares !== undefined) return session;
26
+ const shares = threadShareLinks(session);
27
+
28
+ return shares ? { ...session, shares } : session;
29
+ }
@@ -0,0 +1,6 @@
1
+ import { expect, test } from "bun:test";
2
+ import { manualThreadOpenCommand } from "./thread-surface";
3
+
4
+ test("manual Thread command uses the shared harness wording", () => {
5
+ expect(manualThreadOpenCommand("ses_123")).toBe("Open cueloop threads: cueloop ses_123");
6
+ });
@@ -0,0 +1,6 @@
1
+ export type ThreadSurfaceOpenStatus = "opened" | "focused" | "disabled" | "unavailable" | "failed";
2
+
3
+ /** Give a harness the same manual command when any terminal integration cannot open. */
4
+ export function manualThreadOpenCommand(threadId: string): string {
5
+ return `Open cueloop threads: cueloop ${threadId}`;
6
+ }
package/src/types.test.ts CHANGED
@@ -1,17 +1,70 @@
1
1
  import { describe, expect, test } from "bun:test";
2
- import { newAnnotationId, verdictAllows } from "./types";
2
+ import {
3
+ annotationTarget,
4
+ ARTIFACT_TYPES,
5
+ WORKFLOW_KINDS,
6
+ isArtifactType,
7
+ isMarkdownArtifact,
8
+ newAnnotationId,
9
+ messageAllows,
10
+ } from "./types";
3
11
 
4
- describe("verdictAllows", () => {
12
+ describe("annotationTarget", () => {
13
+ test("an annotation with no target means the reviewed artifact", () => {
14
+ expect(annotationTarget({ target: undefined })).toEqual({ kind: "artifact" });
15
+ });
16
+
17
+ test("a stamped target is returned as-is", () => {
18
+ const target = { kind: "file", path: "src/x.ts", rev: "worktree" } as const;
19
+
20
+ expect(annotationTarget({ target })).toBe(target);
21
+ });
22
+ });
23
+
24
+ describe("ARTIFACT_TYPES", () => {
25
+ test("names every primitive exactly once", () => {
26
+ expect([...ARTIFACT_TYPES].sort()).toEqual(["diff", "plan", "prototype", "reply"]);
27
+ expect(new Set(ARTIFACT_TYPES).size).toBe(ARTIFACT_TYPES.length);
28
+ });
29
+
30
+ test("isArtifactType accepts every member and rejects strangers", () => {
31
+ for (const type of ARTIFACT_TYPES) expect(isArtifactType(type)).toBe(true);
32
+ expect(isArtifactType("blueprint")).toBe(false);
33
+ expect(isArtifactType("")).toBe(false);
34
+ });
35
+ });
36
+
37
+ describe("WORKFLOW_KINDS", () => {
38
+ test("review and refine are workflows, not artifact types", () => {
39
+ expect(WORKFLOW_KINDS).toEqual(["plan", "reply", "prototype", "diff", "review", "refine"]);
40
+ expect(isArtifactType("review")).toBe(false);
41
+ expect(isArtifactType("refine")).toBe(false);
42
+ });
43
+ });
44
+
45
+ describe("messageAllows", () => {
5
46
  test("only approve maps to allow", () => {
6
- expect(verdictAllows("approve")).toBe(true);
7
- expect(verdictAllows("comment")).toBe(false);
8
- expect(verdictAllows("request_changes")).toBe(false);
47
+ expect(messageAllows("approved")).toBe(true);
48
+ expect(messageAllows("changes_requested")).toBe(false);
49
+ expect(messageAllows("changes_requested")).toBe(false);
50
+ });
51
+ });
52
+
53
+ describe("isMarkdownArtifact", () => {
54
+ test("plan, reply, and prototype are markdown; diff is not", () => {
55
+ expect(isMarkdownArtifact("plan")).toBe(true);
56
+ expect(isMarkdownArtifact("reply")).toBe(true);
57
+ expect(isMarkdownArtifact("prototype")).toBe(true);
58
+ expect(isMarkdownArtifact("diff")).toBe(false);
9
59
  });
10
60
  });
11
61
 
12
62
  describe("newAnnotationId", () => {
13
63
  test("unique by construction within a process, even on one millisecond", () => {
64
+ // Act
14
65
  const ids = new Set(Array.from({ length: 10_000 }, () => newAnnotationId()));
66
+
67
+ // Assert
15
68
  expect(ids.size).toBe(10_000);
16
69
  for (const id of ids) expect(id).toMatch(/^a_[0-9a-z]+_[0-9a-z]+$/);
17
70
  });