@cueloop/schema 0.1.0-alpha.0 → 0.1.0-alpha.100

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,233 @@
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, sharePublicationView, 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
+ textCuts: [{ start: 5, end: 8, quote: "v1 " }],
41
+ createdAt: AT,
42
+ }).history;
43
+ history = appendEntry(history, { type: "comment", annotationId: "a2", createdAt: AT }).history;
44
+
45
+ return history;
46
+ }
47
+
48
+ describe("viewOfPath", () => {
49
+ test("the artifact is the agent's last revision and the reviewer's head is the working copy", () => {
50
+ // Act
51
+ const view = viewOfPath(threaded(), [annotation("a1"), annotation("a2")]);
52
+
53
+ // Assert
54
+ expect(view.content).toBe("Plan v1");
55
+ expect(view.workingCopy).toBe("Plan v1 edited");
56
+ expect(view.textCuts).toEqual([{ start: 5, end: 8, quote: "v1 " }]);
57
+ expect(view.annotations.map((entry) => entry.id)).toEqual(["a1", "a2"]);
58
+ expect(view.shelvedAnnotations).toEqual([]);
59
+ });
60
+
61
+ test("navigating back shelves the comments the path no longer reaches, and forward again unshelves them", () => {
62
+ // Arrange: a branch that stays at the full path, main moved back to the first comment
63
+ let history = createBranch(threaded(), "full");
64
+ const known = [annotation("a1"), annotation("a2")];
65
+
66
+ history = switchBranch(history, "main");
67
+ history = navigateTo(history, history.entries[1]!.id);
68
+
69
+ // Act
70
+ const back = viewOfPath(history, known);
71
+ const forward = viewOfPath(
72
+ switchBranch(history, "full"),
73
+ back.annotations.concat(back.shelvedAnnotations),
74
+ );
75
+
76
+ // Assert
77
+ expect(back.workingCopy).toBeUndefined();
78
+ expect(back.textCuts).toBeUndefined();
79
+ expect(back.annotations.map((entry) => entry.id)).toEqual(["a1"]);
80
+ expect(back.shelvedAnnotations.map((entry) => entry.id)).toEqual(["a2"]);
81
+ expect(forward.annotations.map((entry) => entry.id)).toEqual(["a1", "a2"]);
82
+ expect(forward.textCuts).toEqual([{ start: 5, end: 8, quote: "v1 " }]);
83
+ expect(forward.shelvedAnnotations).toEqual([]);
84
+ });
85
+
86
+ test("a removed comment stays shelved, not deleted", () => {
87
+ // Arrange
88
+ let history = threaded();
89
+
90
+ history = appendEntry(history, {
91
+ type: "comment-removed",
92
+ annotationId: "a1",
93
+ createdAt: AT,
94
+ }).history;
95
+
96
+ // Act
97
+ const view = viewOfPath(history, [annotation("a1"), annotation("a2")]);
98
+
99
+ // Assert
100
+ expect(view.annotations.map((entry) => entry.id)).toEqual(["a2"]);
101
+ expect(view.shelvedAnnotations.map((entry) => entry.id)).toEqual(["a1"]);
102
+ });
103
+ });
104
+
105
+ describe("applyPathView", () => {
106
+ test("writes the view into the record and removes the fields the view leaves empty", () => {
107
+ // Arrange
108
+ const session: Thread = {
109
+ schemaVersion: "1",
110
+ id: "ses_1",
111
+ workspace: { repoRoot: "/repo", branch: "main" },
112
+ artifact: { type: "plan", content: "old", meta: {} },
113
+ revisions: [],
114
+ annotations: [annotation("a2")],
115
+ shelvedAnnotations: [annotation("a1")],
116
+ workingCopy: "edited",
117
+ message: null,
118
+ status: "pending",
119
+ createdAt: AT,
120
+ };
121
+
122
+ // Act
123
+ applyPathView(session, {
124
+ content: "Plan v1",
125
+ workingCopy: undefined,
126
+ textCuts: undefined,
127
+ annotations: [annotation("a1"), annotation("a2")],
128
+ shelvedAnnotations: [],
129
+ });
130
+
131
+ // Assert
132
+ expect(session.artifact.content).toBe("Plan v1");
133
+ expect(session.annotations.map((entry) => entry.id)).toEqual(["a1", "a2"]);
134
+ expect("workingCopy" in session).toBe(false);
135
+ expect("shelvedAnnotations" in session).toBe(false);
136
+ });
137
+ });
138
+
139
+ describe("viewFollowing", () => {
140
+ test("a share carries one branch's path: its entries, tip, labels, and open comments, nothing shelved", () => {
141
+ // Arrange: main has a1 and an edit; a branch off a1 has its own comment; the owner stands on the branch
142
+ let history = threaded();
143
+
144
+ history = navigateTo(history, history.entries[1]!.id);
145
+ history = createBranch(history, "alt");
146
+ history = appendEntry(history, { type: "comment", annotationId: "b1", createdAt: AT }).history;
147
+ const session: Thread = {
148
+ schemaVersion: "1",
149
+ id: "ses_1",
150
+ workspace: { repoRoot: "/repo", branch: "main" },
151
+ artifact: { type: "plan", content: "Plan v1", meta: {} },
152
+ revisions: [],
153
+ annotations: [annotation("a1"), annotation("b1")],
154
+ shelvedAnnotations: [annotation("a2")],
155
+ workingCopy: "Plan v1 edited",
156
+ history,
157
+ message: null,
158
+ status: "pending",
159
+ createdAt: AT,
160
+ };
161
+
162
+ // Act
163
+ const shared = viewFollowing(session);
164
+
165
+ // Assert: main's path only, back at the first comment, no working copy, no shelf, the branch named
166
+ expect(shared.history!.branch).toBe("main");
167
+ expect(shared.history!.tips).toEqual({ main: history.entries[1]!.id });
168
+ expect(shared.history!.entries.map((entry) => entry.type)).toEqual(["revision", "comment"]);
169
+ expect(shared.annotations.map((entry) => entry.id)).toEqual(["a1"]);
170
+ expect("shelvedAnnotations" in shared).toBe(false);
171
+ expect("workingCopy" in shared).toBe(false);
172
+ // the owner's record is untouched
173
+ expect(session.annotations.map((entry) => entry.id)).toEqual(["a1", "b1"]);
174
+ });
175
+
176
+ test("a followed branch that no longer exists falls back to main", () => {
177
+ // Arrange: a plain main-only history, asked to follow a branch that is not there
178
+ const history = historyFromLinear({
179
+ id: "ses_1",
180
+ revisions: [{ revision: 1, content: "Plan v1", submittedAt: AT }],
181
+ annotations: [],
182
+ message: null,
183
+ createdAt: AT,
184
+ });
185
+ const session: Thread = {
186
+ schemaVersion: "1",
187
+ id: "ses_1",
188
+ workspace: { repoRoot: "/repo", branch: "main" },
189
+ artifact: { type: "plan", content: "Plan v1", meta: {} },
190
+ revisions: [],
191
+ annotations: [],
192
+ shares: [{ id: "p_test", requireAuth: false, allowlist: [], shareBranch: "gone" }],
193
+ history,
194
+ message: null,
195
+ status: "pending",
196
+ createdAt: AT,
197
+ };
198
+
199
+ // Act
200
+ const shared = viewFollowing(session);
201
+
202
+ // Assert: it does not throw, and falls back to main
203
+ expect(shared.history!.branch).toBe("main");
204
+ expect(shared.history!.branch).toBe("main");
205
+ });
206
+ });
207
+
208
+ test("a publication carries only its selected link's policy, without other owners or allowlists", () => {
209
+ const session: Thread = {
210
+ schemaVersion: "1",
211
+ id: "ses_1",
212
+ workspace: { repoRoot: "/repo", branch: "main" },
213
+ artifact: { type: "plan", content: "Plan", meta: {} },
214
+ revisions: [],
215
+ annotations: [],
216
+ message: null,
217
+ status: "pending",
218
+ createdAt: AT,
219
+ shares: [
220
+ { id: "p_other", owner: "SHA256:owner", requireAuth: true, allowlist: ["private-member"] },
221
+ ],
222
+ };
223
+ const shared = sharePublicationView(session, { requireAuth: true, allowlist: ["octocat"] });
224
+
225
+ expect(shared.shares).toEqual([
226
+ { id: session.id, requireAuth: true, allowlist: ["octocat"], shareBranch: "main" },
227
+ ]);
228
+ expect(session.shares![0]!.allowlist).toEqual(["private-member"]);
229
+ expect(
230
+ sharePublicationView(session, { requireAuth: false, allowlist: ["discarded"] }).shares![0]!
231
+ .allowlist,
232
+ ).toEqual([]);
233
+ });
@@ -0,0 +1,105 @@
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, ShareLink, SharePolicy, Thread, TextCut } from "./types";
11
+
12
+ export interface PathView {
13
+ content: string;
14
+ workingCopy: string | undefined;
15
+ textCuts: TextCut[] | undefined;
16
+ annotations: Annotation[];
17
+ shelvedAnnotations: Annotation[];
18
+ }
19
+
20
+ /**
21
+ * Fold the active path over every comment the session holds, on or off the
22
+ * path. Comments on the path whose objects are gone stay gone.
23
+ */
24
+ export function viewOfPath(history: SessionHistory, known: Annotation[]): PathView {
25
+ const derived = derivePath(history);
26
+ const agentHead = pathOf(history)
27
+ .filter((entry) => entry.type === "revision" && entry.by === "agent")
28
+ .at(-1);
29
+ const content = agentHead?.type === "revision" ? agentHead.content : derived.head.content;
30
+ const byId = new Map(known.map((annotation) => [annotation.id, annotation]));
31
+ const annotations: Annotation[] = [];
32
+
33
+ for (const id of derived.annotationIds) {
34
+ const annotation = byId.get(id);
35
+
36
+ if (annotation) {
37
+ annotations.push(annotation);
38
+ byId.delete(id);
39
+ }
40
+ }
41
+
42
+ return {
43
+ content,
44
+ workingCopy: derived.head.content === content ? undefined : derived.head.content,
45
+ textCuts: derived.head.by === "reviewer" ? derived.head.textCuts : undefined,
46
+ annotations,
47
+ shelvedAnnotations: [...byId.values()],
48
+ };
49
+ }
50
+
51
+ /** Write a path view back into a record, in place; absent fields are removed. */
52
+ export function applyPathView(session: Thread, view: PathView): void {
53
+ session.artifact = { ...session.artifact, content: view.content };
54
+ session.annotations = view.annotations;
55
+ if (view.textCuts?.length) session.textCuts = view.textCuts;
56
+ else delete session.textCuts;
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.shares?.at(-1)?.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 };
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
+ }
86
+
87
+ /** Publish one link's branch and policy, without carrying other links; the gateway assigns its final ID and owner. */
88
+ export function sharePublicationView(
89
+ session: Thread,
90
+ publication: SharePolicy & Pick<ShareLink, "shareBranch">,
91
+ ): Thread {
92
+ const shared = viewFollowing(session, publication.shareBranch ?? session.history?.branch);
93
+
94
+ return {
95
+ ...shared,
96
+ shares: [
97
+ {
98
+ id: session.id,
99
+ requireAuth: publication.requireAuth,
100
+ allowlist: publication.requireAuth ? publication.allowlist : [],
101
+ shareBranch: shared.history?.branch ?? publication.shareBranch ?? MAIN_BRANCH,
102
+ },
103
+ ],
104
+ };
105
+ }
@@ -0,0 +1,29 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { isShareViewerAllowed } from "./share-access";
3
+
4
+ describe("isShareViewerAllowed", () => {
5
+ test("a public link admits an unauthenticated viewer", () => {
6
+ expect(isShareViewerAllowed({ requireAuth: false, allowlist: [] }, undefined)).toBe(true);
7
+ });
8
+ test("an unauthenticated viewer is refused", () => {
9
+ expect(isShareViewerAllowed({ requireAuth: true, allowlist: ["octocat"] }, undefined)).toBe(
10
+ false,
11
+ );
12
+ });
13
+
14
+ test("a member is admitted, case-insensitively", () => {
15
+ expect(isShareViewerAllowed({ requireAuth: true, allowlist: ["OctoCat"] }, "octocat")).toBe(
16
+ true,
17
+ );
18
+ });
19
+
20
+ test("a non-member is refused", () => {
21
+ expect(isShareViewerAllowed({ requireAuth: true, allowlist: ["octocat"] }, "hubot")).toBe(
22
+ false,
23
+ );
24
+ });
25
+
26
+ test("an empty allowlist admits no one", () => {
27
+ expect(isShareViewerAllowed({ requireAuth: true, allowlist: [] }, "octocat")).toBe(false);
28
+ });
29
+ });
@@ -0,0 +1,14 @@
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 { SharePolicy } from "./types";
4
+
5
+ export function isShareViewerAllowed(
6
+ policy: SharePolicy,
7
+ githubLogin: string | undefined,
8
+ ): boolean {
9
+ if (!policy.requireAuth) return true;
10
+ if (githubLogin === undefined) return false;
11
+ const login = githubLogin.toLowerCase();
12
+
13
+ return policy.allowlist.some((allowed) => allowed.toLowerCase() === login);
14
+ }
@@ -0,0 +1,92 @@
1
+ import type { Anchor, OperationReceipt } from "./types";
2
+
3
+ /** An agent message keeps its identity after streaming and across reconnects. */
4
+ export interface AgentMessage {
5
+ id: string;
6
+ role: "user" | "agent";
7
+ text: string;
8
+ complete: boolean;
9
+ revision: number;
10
+ submissionId?: string;
11
+ }
12
+
13
+ /** Tool activity belongs to a turn; its output is bounded by the daemon. */
14
+ export interface AgentTool {
15
+ id: string;
16
+ turnId: string;
17
+ title: string;
18
+ kind: string;
19
+ status: "pending" | "in_progress" | "completed" | "failed" | "cancelled";
20
+ output: string;
21
+ locations: { path: string; line?: number }[];
22
+ }
23
+
24
+ /** Agent feedback targets a completed message, never a changing stream. */
25
+ export interface AgentComment {
26
+ id: string;
27
+ messageId: string;
28
+ anchor: Anchor;
29
+ body: string;
30
+ sent: boolean;
31
+ replyTo?: string;
32
+ author?: string;
33
+ }
34
+
35
+ /** Permission options come from the active agent request, not client defaults. */
36
+ export interface AgentPermission {
37
+ id: string;
38
+ title: string;
39
+ options: { optionId: string; name: string; kind: string }[];
40
+ }
41
+
42
+ /** Agent lifecycle state distinguishes a waiting permission from an active turn. */
43
+ export type AgentPhase =
44
+ | { kind: "idle" }
45
+ | { kind: "running" }
46
+ | { kind: "permission"; permission: AgentPermission }
47
+ | { kind: "failed"; error: string };
48
+
49
+ /** A thread agent transcript is separate from the artifact and its revision history. */
50
+ export interface ThreadAgentState {
51
+ threadId: string;
52
+ harness?: { id: string; label: string; sessionId?: string };
53
+ phase: AgentPhase;
54
+ messages: AgentMessage[];
55
+ tools: AgentTool[];
56
+ comments: AgentComment[];
57
+ submissions?: AgentSubmission[];
58
+ promptOperations?: (OperationReceipt<string[]> & {
59
+ outcome?: "completed" | "failed" | "cancelled";
60
+ })[];
61
+ configOptions?: AgentConfigOption[];
62
+ }
63
+
64
+ /** A submission freezes its input; retry keeps the same mirror and discussion origin. */
65
+ export interface AgentSubmission {
66
+ id: string;
67
+ commentId?: string;
68
+ messageId?: string;
69
+ prompt: string;
70
+ quote?: string;
71
+ context?: string;
72
+ status: "queued" | "running" | "completed" | "failed";
73
+ cancelled?: boolean;
74
+ }
75
+
76
+ /** Model and reasoning choices are advertised by the harness, never invented by the UI. */
77
+ export interface AgentConfigOption {
78
+ id: string;
79
+ name: string;
80
+ category?: string;
81
+ currentValue: string;
82
+ options: { value: string; name: string }[];
83
+ }
84
+
85
+ /** Prompt submission preserves operation identity while optional context remains separate from input. */
86
+ export interface AgentPromptRequest {
87
+ id: string;
88
+ text: string;
89
+ context?: string;
90
+ retry?: string;
91
+ operationId?: string;
92
+ }
@@ -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
+ }
@@ -0,0 +1,79 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import {
3
+ annotationTarget,
4
+ ARTIFACT_TYPES,
5
+ WORKFLOW_KINDS,
6
+ WORKFLOW_SKILL_KINDS,
7
+ isArtifactType,
8
+ isMarkdownArtifact,
9
+ newAnnotationId,
10
+ messageAllows,
11
+ } from "./types";
12
+
13
+ describe("annotationTarget", () => {
14
+ test("an annotation with no target means the reviewed artifact", () => {
15
+ expect(annotationTarget({ target: undefined })).toEqual({ kind: "artifact" });
16
+ });
17
+
18
+ test("a stamped target is returned as-is", () => {
19
+ const target = { kind: "file", path: "src/x.ts", rev: "worktree" } as const;
20
+
21
+ expect(annotationTarget({ target })).toBe(target);
22
+ });
23
+ });
24
+
25
+ describe("ARTIFACT_TYPES", () => {
26
+ test("names every primitive exactly once", () => {
27
+ expect([...ARTIFACT_TYPES].sort()).toEqual(["diff", "plan", "prototype", "reply"]);
28
+ expect(new Set(ARTIFACT_TYPES).size).toBe(ARTIFACT_TYPES.length);
29
+ });
30
+
31
+ test("isArtifactType accepts every member and rejects strangers", () => {
32
+ for (const type of ARTIFACT_TYPES) expect(isArtifactType(type)).toBe(true);
33
+ expect(isArtifactType("blueprint")).toBe(false);
34
+ expect(isArtifactType("")).toBe(false);
35
+ });
36
+ });
37
+
38
+ describe("WORKFLOW_KINDS", () => {
39
+ test("review and refine are workflows, not artifact types", () => {
40
+ expect(WORKFLOW_KINDS).toEqual(["plan", "reply", "prototype", "diff", "review", "refine"]);
41
+ expect(isArtifactType("review")).toBe(false);
42
+ expect(isArtifactType("refine")).toBe(false);
43
+ });
44
+ });
45
+
46
+ describe("WORKFLOW_SKILL_KINDS", () => {
47
+ test("includes the live pairing skill without adding it to artifact submission workflows", () => {
48
+ expect(WORKFLOW_SKILL_KINDS).toEqual([...WORKFLOW_KINDS, "pair"]);
49
+ expect(WORKFLOW_KINDS).not.toContain("pair");
50
+ });
51
+ });
52
+
53
+ describe("messageAllows", () => {
54
+ test("only approve maps to allow", () => {
55
+ expect(messageAllows("approved")).toBe(true);
56
+ expect(messageAllows("comment")).toBe(false);
57
+ expect(messageAllows("changes_requested")).toBe(false);
58
+ });
59
+ });
60
+
61
+ describe("isMarkdownArtifact", () => {
62
+ test("plan, reply, and prototype are markdown; diff is not", () => {
63
+ expect(isMarkdownArtifact("plan")).toBe(true);
64
+ expect(isMarkdownArtifact("reply")).toBe(true);
65
+ expect(isMarkdownArtifact("prototype")).toBe(true);
66
+ expect(isMarkdownArtifact("diff")).toBe(false);
67
+ });
68
+ });
69
+
70
+ describe("newAnnotationId", () => {
71
+ test("unique by construction within a process, even on one millisecond", () => {
72
+ // Act
73
+ const ids = new Set(Array.from({ length: 10_000 }, () => newAnnotationId()));
74
+
75
+ // Assert
76
+ expect(ids.size).toBe(10_000);
77
+ for (const id of ids) expect(id).toMatch(/^a_[0-9a-z]+_[0-9a-z]+$/);
78
+ });
79
+ });