@cueloop/daemon 0.1.0-alpha.10 → 0.1.0-alpha.101

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.
Files changed (83) hide show
  1. package/README.md +7 -0
  2. package/SDK.md +102 -0
  3. package/package.json +44 -22
  4. package/src/agent-tools.ts +98 -0
  5. package/src/agent-turn.test.ts +24 -0
  6. package/src/agent-turn.ts +34 -0
  7. package/src/api.test.ts +1081 -65
  8. package/src/api.ts +1425 -89
  9. package/src/capabilities.test.ts +57 -0
  10. package/src/capabilities.ts +95 -0
  11. package/src/client-errors.ts +24 -0
  12. package/src/client-reliability.test.ts +189 -0
  13. package/src/client.test.ts +37 -0
  14. package/src/client.ts +820 -94
  15. package/src/curate.ts +109 -0
  16. package/src/daemon-sdk-effect.ts +113 -0
  17. package/src/daemon-sdk.test.ts +306 -0
  18. package/src/daemon-sdk.ts +397 -0
  19. package/src/daemon-version.test.ts +142 -0
  20. package/src/diff-hot-reload.test.ts +419 -0
  21. package/src/diff-watcher.test.ts +117 -0
  22. package/src/diff-watcher.ts +326 -0
  23. package/src/examples/daemon-sdk.ts +55 -0
  24. package/src/gh.test.ts +59 -0
  25. package/src/gh.ts +74 -0
  26. package/src/ghostty-thread-surface-store.test.ts +49 -0
  27. package/src/ghostty-thread-surface-store.ts +101 -0
  28. package/src/ghostty-thread-surface.test.ts +398 -0
  29. package/src/ghostty-thread-surface.ts +267 -0
  30. package/src/harness-delivery.test.ts +269 -0
  31. package/src/harness-diagnostics.test.ts +26 -0
  32. package/src/harness-diagnostics.ts +30 -0
  33. package/src/harness-state-store.test.ts +93 -0
  34. package/src/harness-state-store.ts +213 -0
  35. package/src/herdr-split.test.ts +106 -0
  36. package/src/herdr-split.ts +85 -0
  37. package/src/herdr-thread-surface-store.test.ts +81 -0
  38. package/src/herdr-thread-surface-store.ts +66 -0
  39. package/src/herdr-thread-surface.test.ts +465 -0
  40. package/src/herdr-thread-surface.ts +378 -0
  41. package/src/index.ts +8 -9
  42. package/src/interruptible-wait.test.ts +127 -0
  43. package/src/interruptible-wait.ts +66 -0
  44. package/src/jj-working-tree.ts +131 -0
  45. package/src/main.ts +4 -1
  46. package/src/operation-receipts.test.ts +111 -0
  47. package/src/operation-receipts.ts +98 -0
  48. package/src/paths.ts +49 -0
  49. package/src/pr-poller.test.ts +102 -0
  50. package/src/pr-poller.ts +73 -0
  51. package/src/project-files.test.ts +134 -0
  52. package/src/project-files.ts +68 -0
  53. package/src/protocol.ts +78 -7
  54. package/src/retention.test.ts +191 -0
  55. package/src/retention.ts +123 -0
  56. package/src/sdk-identities.ts +35 -0
  57. package/src/server.test.ts +502 -42
  58. package/src/server.ts +631 -88
  59. package/src/share-blob.test.ts +119 -0
  60. package/src/share-blob.ts +57 -0
  61. package/src/shared-agent-protocol.ts +30 -0
  62. package/src/single-instance.test.ts +162 -13
  63. package/src/store.conformance.test.ts +41 -0
  64. package/src/store.jsonl.test.ts +173 -0
  65. package/src/store.ts +267 -34
  66. package/src/testing/store-conformance.ts +192 -0
  67. package/src/thread-agent-validation.ts +121 -0
  68. package/src/thread-agent.test.ts +719 -0
  69. package/src/thread-agent.ts +835 -0
  70. package/src/thread-review.test.ts +483 -0
  71. package/src/thread-review.ts +382 -0
  72. package/src/thread-subscription.test.ts +174 -0
  73. package/src/thread-subscription.ts +129 -0
  74. package/src/thread-surface-config.test.ts +63 -0
  75. package/src/thread-surface-config.ts +61 -0
  76. package/src/validate.test.ts +279 -37
  77. package/src/validate.ts +422 -33
  78. package/src/vcs-source.test.ts +180 -0
  79. package/src/vcs-source.ts +229 -0
  80. package/src/version.ts +6 -0
  81. package/src/working-tree.ts +150 -0
  82. package/src/review.test.ts +0 -167
  83. package/src/review.ts +0 -174
package/src/validate.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Runtime validation of the wire boundary (#14). Everything arriving over the
2
+ * Runtime validation of the wire boundary. Everything arriving over the
3
3
  * socket is untrusted JSON: adapters, the CLI, extensions, and any script a
4
4
  * user writes all speak this protocol. Validating here means DaemonCore only
5
5
  * ever sees well-formed input, and a malformed request gets a precise error
@@ -11,17 +11,36 @@
11
11
 
12
12
  import * as v from "valibot";
13
13
  import {
14
+ ARTIFACT_TYPES,
15
+ MESSAGE_OUTCOMES,
16
+ WORKFLOW_KINDS,
14
17
  SCHEMA_VERSION,
15
18
  type Anchor,
16
19
  type Annotation,
20
+ type AnnotationTarget,
17
21
  type Artifact,
18
22
  type ArtifactMeta,
19
- type ReviewSession,
23
+ type DiffFileContents,
24
+ type Identity,
25
+ type Thread,
26
+ type TextCut,
27
+ type HunkRejection,
28
+ type HarnessBinding,
29
+ type Delivery,
30
+ type PendingDelivery,
20
31
  type Revision,
21
- type Verdict,
32
+ REVIEW_SEVERITIES,
33
+ type ReviewComment,
34
+ type SessionHistory,
35
+ type ShareLink,
36
+ validateHistory,
37
+ type Message,
22
38
  type WorkspaceKey,
39
+ applyTextCuts,
23
40
  } from "@cueloop/schema";
24
41
  import { DaemonError } from "./errors";
42
+ import type { Request } from "./protocol";
43
+ import { AgentCommentSchema } from "./thread-agent-validation";
25
44
 
26
45
  /**
27
46
  * Drift guard for every hand-mirrored shape below. v.object strips keys it
@@ -34,25 +53,69 @@ type EntriesOf<T> = { [K in keyof T]-?: v.GenericSchema<any, any> };
34
53
 
35
54
  const NonEmpty = v.pipe(v.string(), v.minLength(1));
36
55
 
56
+ const TextCutSchema = v.object({
57
+ start: v.pipe(v.number(), v.integer(), v.minValue(0)),
58
+ end: v.pipe(v.number(), v.integer(), v.minValue(1)),
59
+ quote: NonEmpty,
60
+ } satisfies EntriesOf<TextCut>);
61
+
62
+ const TextCutsSchema = v.pipe(
63
+ v.array(TextCutSchema),
64
+ v.check(
65
+ (cuts) =>
66
+ cuts.every(
67
+ (cut, index) =>
68
+ cut.end - cut.start === cut.quote.length &&
69
+ (index === 0 || cuts[index - 1]!.end <= cut.start),
70
+ ),
71
+ "text Cuts must be ordered, non-overlapping, and match their quote lengths",
72
+ ),
73
+ );
74
+
37
75
  export const WorkspaceSchema = v.object({
38
76
  repoRoot: NonEmpty,
39
77
  branch: NonEmpty,
78
+ rootCommit: v.optional(NonEmpty),
79
+ remote: v.optional(NonEmpty),
40
80
  } satisfies EntriesOf<WorkspaceKey>);
41
81
 
42
82
  export const ArtifactMetaSchema = v.object({
83
+ workflow: v.optional(v.picklist(WORKFLOW_KINDS)),
43
84
  cwd: v.optional(v.string()),
85
+ vcs: v.optional(NonEmpty),
86
+ vcsChangeId: v.optional(NonEmpty),
87
+ vcsRevisionId: v.optional(NonEmpty),
44
88
  agent: v.optional(v.string()),
45
89
  agentSessionId: v.optional(v.string()),
46
90
  planPath: v.optional(v.string()),
91
+ prototypePath: v.optional(v.string()),
47
92
  pr: v.optional(v.string()),
93
+ prBrief: v.optional(v.string()),
94
+ prBaseSha: v.optional(NonEmpty),
95
+ prHeadSha: v.optional(NonEmpty),
96
+ prRefreshBaseSha: v.optional(NonEmpty),
97
+ prRefreshHeadSha: v.optional(NonEmpty),
98
+ prUrl: v.optional(NonEmpty),
48
99
  herdrPane: v.optional(v.string()),
49
100
  title: v.optional(v.string()),
101
+ workbench: v.optional(v.boolean()),
102
+ snapshot: v.optional(v.boolean()),
50
103
  } satisfies EntriesOf<ArtifactMeta>);
51
104
 
105
+ export const DiffFileContentsSchema = v.object({
106
+ path: NonEmpty,
107
+ oldContents: v.string(),
108
+ newContents: v.string(),
109
+ status: v.picklist(["added", "modified", "deleted"]),
110
+ } satisfies EntriesOf<DiffFileContents>);
111
+
52
112
  export const ArtifactSchema = v.object({
53
- type: v.picklist(["plan", "diff"]),
113
+ // Derived from the schema's runtime union: a new primitive extends the wire
114
+ // contract without touching this file.
115
+ type: v.picklist(ARTIFACT_TYPES),
54
116
  content: v.string(),
55
117
  meta: v.optional(ArtifactMetaSchema, {}),
118
+ files: v.optional(v.array(DiffFileContentsSchema)),
56
119
  } satisfies EntriesOf<Artifact>);
57
120
 
58
121
  export const AnchorSchema = v.object({
@@ -60,23 +123,119 @@ export const AnchorSchema = v.object({
60
123
  prefix: v.optional(v.string(), ""),
61
124
  suffix: v.optional(v.string(), ""),
62
125
  blockIndex: v.optional(v.number()),
126
+ endBlockIndex: v.optional(v.number()),
63
127
  start: v.optional(v.number()),
64
128
  end: v.optional(v.number()),
129
+ selector: v.optional(v.string()),
65
130
  } satisfies EntriesOf<Anchor>);
66
131
 
132
+ /** The surface a note was made on; absent means the reviewed artifact. */
133
+ export const AnnotationTargetSchema: v.GenericSchema<AnnotationTarget> = v.variant("kind", [
134
+ v.object({ kind: v.literal("artifact") }),
135
+ v.object({ kind: v.literal("file"), path: NonEmpty, rev: v.picklist(["worktree", "head"]) }),
136
+ v.object({ kind: v.literal("welcome") }),
137
+ ]);
138
+
67
139
  /** Wire annotations arrive without createdAt - the daemon stamps it. */
68
140
  export const AnnotationSchema = v.object({
69
141
  id: NonEmpty,
70
- /** Open kind set (#2): built-ins are comment and suggestion. */
142
+ /** Open kind set: the built-in is comment. */
71
143
  kind: NonEmpty,
72
144
  anchor: AnchorSchema,
145
+ target: v.optional(AnnotationTargetSchema),
73
146
  body: v.string(),
74
147
  orphan: v.optional(v.boolean()),
148
+ author: v.optional(v.string()),
149
+ reviewComment: v.optional(
150
+ v.object({
151
+ severity: v.picklist(REVIEW_SEVERITIES),
152
+ title: NonEmpty,
153
+ path: NonEmpty,
154
+ line: v.pipe(v.number(), v.integer(), v.minValue(1)),
155
+ startLine: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1))),
156
+ startAnchor: v.optional(AnchorSchema),
157
+ side: v.picklist(["LEFT", "RIGHT"]),
158
+ suggestion: v.optional(v.string()),
159
+ prompt: v.optional(v.string()),
160
+ } satisfies EntriesOf<ReviewComment>),
161
+ ),
162
+ replyTo: v.optional(NonEmpty),
163
+ resolution: v.optional(
164
+ v.object({
165
+ revision: v.number(),
166
+ source: v.picklist(["agent", "drift"]),
167
+ }),
168
+ ),
75
169
  } satisfies EntriesOf<Omit<Annotation, "createdAt">>);
76
170
 
77
171
  const SessionId = NonEmpty;
78
172
 
173
+ /** A stored annotation: the wire shape plus the daemon-stamped createdAt. */
174
+ export const FullAnnotationSchema = v.object({
175
+ ...AnnotationSchema.entries,
176
+ createdAt: v.string(),
177
+ } satisfies EntriesOf<Annotation>);
178
+
179
+ export const IdentitySchema = v.object({
180
+ id: NonEmpty,
181
+ provider: v.picklist(["ssh", "github"]),
182
+ name: v.optional(v.string()),
183
+ handle: v.optional(v.string()),
184
+ } satisfies EntriesOf<Identity>);
185
+
186
+ export const ShareLinkSchema = v.object({
187
+ id: NonEmpty,
188
+ name: v.optional(v.string()),
189
+ requireAuth: v.boolean(),
190
+ allowlist: v.array(NonEmpty),
191
+ agentEnabled: v.optional(v.boolean()),
192
+ owner: v.optional(v.string()),
193
+ shareBranch: v.optional(v.string()),
194
+ } satisfies EntriesOf<ShareLink>);
195
+
196
+ export const HarnessBindingSchema = v.object({
197
+ id: NonEmpty,
198
+ threadId: NonEmpty,
199
+ harness: NonEmpty,
200
+ harnessSessionId: NonEmpty,
201
+ createdAt: NonEmpty,
202
+ approvedRetryMessageId: v.optional(NonEmpty),
203
+ } satisfies EntriesOf<HarnessBinding>);
204
+
205
+ export const DeliverySchema = v.object({
206
+ id: NonEmpty,
207
+ messageId: NonEmpty,
208
+ bindingId: NonEmpty,
209
+ status: v.picklist(["pending", "acknowledged"]),
210
+ createdAt: NonEmpty,
211
+ acknowledgedAt: v.optional(NonEmpty),
212
+ } satisfies EntriesOf<Delivery>);
213
+
79
214
  export const Params = {
215
+ "agent.configure": v.object({
216
+ id: SessionId,
217
+ configId: v.optional(NonEmpty),
218
+ value: v.optional(NonEmpty),
219
+ }),
220
+ "agent.get": v.object({ id: SessionId }),
221
+ "agent.prompt": v.object({
222
+ id: SessionId,
223
+ text: v.pipe(v.string(), v.maxLength(65_536)),
224
+ context: v.optional(v.pipe(v.string(), v.maxLength(32_768))),
225
+ retry: v.optional(NonEmpty),
226
+ commentId: v.optional(NonEmpty),
227
+ inputOnly: v.optional(v.boolean()),
228
+ discussion: v.optional(v.pipe(v.string(), v.maxLength(320_000))),
229
+ operationId: v.optional(v.pipe(NonEmpty, v.maxLength(128))),
230
+ }),
231
+ "agent.reply": v.object({
232
+ id: SessionId,
233
+ commentId: NonEmpty,
234
+ body: v.pipe(NonEmpty, v.maxLength(32_768)),
235
+ }),
236
+ "agent.cancel": v.object({ id: SessionId }),
237
+ "agent.comment": v.object({ id: SessionId, comment: AgentCommentSchema }),
238
+ "agent.permission": v.object({ id: SessionId, requestId: NonEmpty, optionId: NonEmpty }),
80
239
  "session.create": v.object({ workspace: WorkspaceSchema, artifact: ArtifactSchema }),
81
240
  "session.get": v.object({ id: SessionId }),
82
241
  "session.list": v.object({
@@ -90,20 +249,142 @@ export const Params = {
90
249
  "session.wait": v.object({
91
250
  id: SessionId,
92
251
  // clamped: a negative or absurd timeout is a client bug, not a daemon one
93
- timeoutMs: v.optional(v.pipe(v.number(), v.minValue(0), v.maxValue(24 * 60 * 60 * 1000)), 60_000),
252
+ timeoutMs: v.optional(
253
+ v.pipe(v.number(), v.minValue(0), v.maxValue(24 * 60 * 60 * 1000)),
254
+ 60_000,
255
+ ),
256
+ }),
257
+ "session.annotate": v.object({
258
+ id: SessionId,
259
+ annotation: AnnotationSchema,
260
+ authorName: v.optional(v.string()),
261
+ }),
262
+ // "session.comment" is the primary annotate method; "session.annotate" stays an accepted alias
263
+ "session.comment": v.object({
264
+ id: SessionId,
265
+ annotation: AnnotationSchema,
266
+ authorName: v.optional(v.string()),
94
267
  }),
95
- "session.annotate": v.object({ id: SessionId, annotation: AnnotationSchema }),
96
268
  "session.removeAnnotation": v.object({ id: SessionId, annotationId: NonEmpty }),
97
- "session.setWorkingCopy": v.object({ id: SessionId, workingCopy: v.optional(v.string()) }),
98
- "session.resolve": v.object({
269
+ "session.setParticipantName": v.object({ id: SessionId, author: NonEmpty, name: NonEmpty }),
270
+ "session.setWorkingCopy": v.object({
271
+ id: SessionId,
272
+ workingCopy: v.optional(v.string()),
273
+ textCuts: v.optional(TextCutsSchema),
274
+ }),
275
+ "session.cutBlock": v.object({
276
+ id: SessionId,
277
+ blockIndex: v.pipe(v.number(), v.integer(), v.minValue(0)),
278
+ }),
279
+ "session.restoreBlock": v.object({
280
+ id: SessionId,
281
+ baseBlockIndex: v.pipe(v.number(), v.integer(), v.minValue(0)),
282
+ line: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0))),
283
+ }),
284
+ "session.curate": v.object({
285
+ id: SessionId,
286
+ rejections: v.array(
287
+ v.object({
288
+ path: NonEmpty,
289
+ hunkIndex: v.pipe(v.number(), v.integer(), v.minValue(0)),
290
+ changeIndex: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0))),
291
+ } satisfies EntriesOf<HunkRejection>),
292
+ ),
293
+ }),
294
+ "session.setViewed": v.object({ id: SessionId, viewedPaths: v.array(v.string()) }),
295
+ "session.setTitle": v.object({ id: SessionId, title: v.string() }),
296
+ "session.projectFiles": v.object({ id: SessionId }),
297
+ "session.fileContents": v.object({ id: SessionId, path: NonEmpty }),
298
+ "repo.files": v.object({ cwd: NonEmpty }),
299
+ "repo.fileContents": v.object({ cwd: NonEmpty, path: NonEmpty }),
300
+ "repo.changes": v.object({ cwd: NonEmpty }),
301
+ "repo.diff": v.object({ cwd: NonEmpty, vcs: v.optional(NonEmpty) }),
302
+ "session.workbench": v.object({ cwd: NonEmpty }),
303
+ "session.workbenchReview": v.object({ cwd: NonEmpty }),
304
+ "session.navigate": v.object({
305
+ id: SessionId,
306
+ entryId: NonEmpty,
307
+ summary: v.optional(v.string()),
308
+ // stand on this branch first, so a move on another branch is one request
309
+ branch: v.optional(NonEmpty),
310
+ }),
311
+ "session.branch": v.object({ id: SessionId, name: NonEmpty }),
312
+ "session.switch": v.object({ id: SessionId, branch: NonEmpty }),
313
+ "session.label": v.object({ id: SessionId, label: NonEmpty }),
314
+ "session.fork": v.object({ id: SessionId }),
315
+ "session.refreshDiff": v.object({ id: SessionId }),
316
+ "session.setShares": v.object({ id: SessionId, shares: v.array(ShareLinkSchema) }),
317
+ "session.delete": v.object({ id: SessionId }),
318
+ "session.mergeShared": v.object({
99
319
  id: SessionId,
100
- verdictKind: v.picklist(["comment", "approve", "request_changes"]),
320
+ shareId: v.optional(NonEmpty),
321
+ annotations: v.array(FullAnnotationSchema),
322
+ participants: v.optional(v.array(IdentitySchema)),
323
+ // the removal entries a share recorded, carried by id so a merge applies each once
324
+ removals: v.optional(
325
+ v.array(v.object({ id: NonEmpty, annotationId: NonEmpty, createdAt: v.string() })),
326
+ ),
327
+ }),
328
+ "session.sendMessage": v.object({
329
+ id: SessionId,
330
+ outcome: v.picklist(MESSAGE_OUTCOMES),
101
331
  summary: v.optional(v.string(), ""),
332
+ actionBodies: v.optional(v.record(v.string(), v.string())),
333
+ operationId: v.optional(v.pipe(NonEmpty, v.maxLength(128))),
334
+ }),
335
+ "harness.bind": v.object({
336
+ threadId: SessionId,
337
+ harness: NonEmpty,
338
+ harnessSessionId: NonEmpty,
339
+ }),
340
+ "harness.getBinding": v.object({ bindingId: NonEmpty }),
341
+ "harness.bindingsForSession": v.object({ harness: NonEmpty, harnessSessionId: NonEmpty }),
342
+ "harness.consumeApprovedRetry": v.object({
343
+ bindingId: NonEmpty,
344
+ messageId: NonEmpty,
345
+ content: v.string(),
346
+ }),
347
+ "delivery.pending": v.object({ bindingId: NonEmpty }),
348
+ "delivery.acknowledge": v.object({ deliveryId: NonEmpty }),
349
+ "session.submitRevision": v.object({
350
+ id: SessionId,
351
+ content: v.string(),
352
+ files: v.optional(v.array(DiffFileContentsSchema)),
353
+ source: v.optional(
354
+ v.object({
355
+ vcs: NonEmpty,
356
+ changeId: v.optional(NonEmpty),
357
+ revisionId: v.optional(NonEmpty),
358
+ baseRevisionId: v.optional(NonEmpty),
359
+ }),
360
+ ),
361
+ /** Annotation ids the agent acted on; each is marked addressed. */
362
+ addressedAnnotationIds: v.optional(v.array(NonEmpty), []),
102
363
  }),
103
- "session.submitRevision": v.object({ id: SessionId, content: v.string() }),
104
364
  "events.subscribe": v.object({}),
105
365
  "daemon.ping": v.object({}),
366
+ // the owner token proves ownership; without it a request for owner stays a collaborator
367
+ "daemon.hello": v.object({
368
+ role: v.picklist(["owner", "collaborator", "agent"]),
369
+ clientVersion: v.optional(v.string()),
370
+ token: v.optional(v.string()),
371
+ // the author a non-owner acts as, bound once for the connection
372
+ author: v.optional(NonEmpty),
373
+ }),
106
374
  "daemon.shutdown": v.object({}),
375
+ // Herdr terminal handles stay outside canonical Thread records.
376
+ "herdr.getThreadSurface": v.object({ id: SessionId }),
377
+ "herdr.setThreadSurface": v.object({
378
+ id: SessionId,
379
+ tabId: NonEmpty,
380
+ paneId: NonEmpty,
381
+ mode: v.optional(v.picklist(["tab", "pane"])),
382
+ layout: v.optional(v.literal("pair")),
383
+ }),
384
+ "ghostty.getThreadSurface": v.object({ id: SessionId }),
385
+ "ghostty.setThreadSurface": v.object({ id: SessionId, terminalId: NonEmpty }),
386
+ "ghostty.claimThreadSurface": v.object({ id: SessionId }),
387
+ "ghostty.releaseThreadSurface": v.object({ id: SessionId }),
107
388
  } as const;
108
389
 
109
390
  export type MethodName = keyof typeof Params;
@@ -113,13 +394,22 @@ export function isKnownMethod(method: string): method is MethodName {
113
394
  }
114
395
 
115
396
  /** Validate params for a method, or throw a DaemonError the client can read. */
116
- export function parseParams<M extends MethodName>(method: M, params: unknown): v.InferOutput<(typeof Params)[M]> {
397
+ export function parseParams<M extends MethodName>(
398
+ method: M,
399
+ params: Request["params"],
400
+ ): v.InferOutput<(typeof Params)[M]> {
117
401
  const result = v.safeParse(Params[method], params ?? {});
402
+
118
403
  if (!result.success) {
119
404
  const issue = result.issues[0]!;
120
- const path = issue.path?.map((p) => String(p.key)).join(".") ?? "";
121
- throw new DaemonError("invalid_params", `${method}: ${path ? path + " - " : ""}${issue.message}`);
405
+ const path = issue.path?.map((pathSegment) => String(pathSegment.key)).join(".") ?? "";
406
+
407
+ throw new DaemonError(
408
+ "invalid_params",
409
+ `${method}: ${path ? path + " - " : ""}${issue.message}`,
410
+ );
122
411
  }
412
+
123
413
  return result.output;
124
414
  }
125
415
 
@@ -127,33 +417,132 @@ export const RevisionSchema = v.object({
127
417
  revision: v.number(),
128
418
  content: v.string(),
129
419
  submittedAt: v.string(),
420
+ files: v.optional(v.array(DiffFileContentsSchema)),
421
+ source: v.optional(
422
+ v.object({
423
+ vcs: NonEmpty,
424
+ changeId: v.optional(NonEmpty),
425
+ revisionId: v.optional(NonEmpty),
426
+ baseRevisionId: v.optional(NonEmpty),
427
+ }),
428
+ ),
130
429
  } satisfies EntriesOf<Revision>);
131
430
 
132
- export const VerdictSchema = v.object({
133
- kind: v.picklist(["comment", "approve", "request_changes"]),
431
+ export const MessageSchema = v.object({
432
+ id: NonEmpty,
433
+ outcome: v.picklist(MESSAGE_OUTCOMES),
134
434
  summary: v.string(),
135
- feedback: v.string(),
136
- resolvedAt: v.string(),
137
- } satisfies EntriesOf<Verdict>);
435
+ body: v.string(),
436
+ annotations: v.optional(v.array(FullAnnotationSchema)),
437
+ sentAt: v.string(),
438
+ } satisfies EntriesOf<Message>);
138
439
 
139
- /** Persisted records are validated on recovery: a bad file is skipped, not fatal. */
140
- export const SessionRecordSchema = v.object({
141
- schemaVersion: v.literal(SCHEMA_VERSION),
440
+ export const PendingDeliverySchema = v.object({
441
+ delivery: DeliverySchema,
442
+ message: MessageSchema,
443
+ } satisfies EntriesOf<PendingDelivery>);
444
+
445
+ const EntryBaseEntries = {
142
446
  id: NonEmpty,
143
- workspace: WorkspaceSchema,
144
- artifact: ArtifactSchema,
145
- revisions: v.array(RevisionSchema),
146
- annotations: v.array(v.object({ ...AnnotationSchema.entries, createdAt: v.string() } satisfies EntriesOf<Annotation>)),
147
- workingCopy: v.optional(v.string()),
148
- verdict: v.nullable(VerdictSchema),
149
- status: v.picklist(["pending", "resolved"]),
447
+ parentId: v.nullable(NonEmpty),
150
448
  createdAt: v.string(),
151
- } satisfies EntriesOf<ReviewSession>);
449
+ };
450
+
451
+ /** One record of a session's history; the tree pointers plus the entry's own fields. */
452
+ export const SessionEntrySchema = v.variant("type", [
453
+ v.object({
454
+ ...EntryBaseEntries,
455
+ type: v.literal("revision"),
456
+ by: v.picklist(["agent", "reviewer"]),
457
+ content: v.string(),
458
+ textCuts: v.optional(TextCutsSchema),
459
+ }),
460
+ v.object({ ...EntryBaseEntries, type: v.literal("comment"), annotationId: NonEmpty }),
461
+ v.object({ ...EntryBaseEntries, type: v.literal("comment-removed"), annotationId: NonEmpty }),
462
+ v.object({ ...EntryBaseEntries, type: v.literal("message"), message: MessageSchema }),
463
+ v.object({
464
+ ...EntryBaseEntries,
465
+ type: v.literal("branch-summary"),
466
+ text: v.string(),
467
+ abandoned: v.array(NonEmpty),
468
+ }),
469
+ ]);
470
+
471
+ export const SessionHistorySchema = v.pipe(
472
+ v.object({
473
+ entries: v.array(SessionEntrySchema),
474
+ tips: v.record(v.string(), NonEmpty),
475
+ branch: NonEmpty,
476
+ labels: v.record(v.string(), v.string()),
477
+ } satisfies EntriesOf<SessionHistory>),
478
+ // the shape is not enough: the tree itself must hold, or a walk hangs or rewires the path
479
+ v.rawCheck(({ dataset, addIssue }) => {
480
+ if (!dataset.typed) return;
481
+ const problem = validateHistory(dataset.value);
482
+
483
+ if (problem !== null) addIssue({ message: `history: ${problem}` });
484
+ }),
485
+ );
486
+
487
+ /** Persisted records are validated on recovery: a bad file is skipped, not fatal. */
488
+ export const ThreadRecordSchema = v.pipe(
489
+ v.strictObject({
490
+ schemaVersion: v.literal(SCHEMA_VERSION),
491
+ id: NonEmpty,
492
+ workspace: WorkspaceSchema,
493
+ artifact: ArtifactSchema,
494
+ revisions: v.array(RevisionSchema),
495
+ annotations: v.array(FullAnnotationSchema),
496
+ history: v.optional(SessionHistorySchema),
497
+ curation: v.optional(
498
+ v.array(
499
+ v.object({
500
+ path: NonEmpty,
501
+ hunkIndex: v.number(),
502
+ changeIndex: v.optional(v.number()),
503
+ } satisfies EntriesOf<HunkRejection>),
504
+ ),
505
+ ),
506
+ workingCopy: v.optional(v.string()),
507
+ textCuts: v.optional(TextCutsSchema),
508
+ viewedPaths: v.optional(v.array(v.string())),
509
+ messageOperations: v.optional(
510
+ v.array(v.object({ operationId: NonEmpty, fingerprint: NonEmpty, result: MessageSchema })),
511
+ ),
512
+ message: v.nullable(MessageSchema),
513
+ status: v.picklist(["pending", "resolved"]),
514
+ createdAt: v.string(),
515
+ shelvedAnnotations: v.optional(v.array(FullAnnotationSchema)),
516
+ parentSessionId: v.optional(v.string()),
517
+ shares: v.optional(v.array(ShareLinkSchema)),
518
+ participants: v.optional(v.array(IdentitySchema)),
519
+ } satisfies EntriesOf<Thread>),
520
+ v.rawCheck(({ dataset, addIssue }) => {
521
+ if (!dataset.typed || !dataset.value.textCuts?.length) return;
522
+ const { artifact, textCuts, workingCopy } = dataset.value;
523
+ const matchesSource = textCuts.every(
524
+ (cut) => artifact.content.slice(cut.start, cut.end) === cut.quote,
525
+ );
526
+
527
+ if (!matchesSource || workingCopy !== applyTextCuts(artifact.content, textCuts)) {
528
+ addIssue({ message: "text Cuts do not match the submitted artifact and working copy" });
529
+ }
530
+ }),
531
+ );
532
+
533
+ export function validateThreadRecord(
534
+ raw: Parameters<typeof v.safeParse>[1],
535
+ ): { ok: true; value: v.InferOutput<typeof ThreadRecordSchema> } | { ok: false; error: string } {
536
+ const result = v.safeParse(ThreadRecordSchema, raw);
152
537
 
153
- export function validateSessionRecord(raw: unknown): { ok: true; value: v.InferOutput<typeof SessionRecordSchema> } | { ok: false; error: string } {
154
- const result = v.safeParse(SessionRecordSchema, raw);
155
538
  if (result.success) return { ok: true, value: result.output };
156
539
  const issue = result.issues[0]!;
157
- const path = issue.path?.map((p) => String(p.key)).join(".") ?? "";
540
+ const path = issue.path?.map((pathSegment) => String(pathSegment.key)).join(".") ?? "";
541
+
158
542
  return { ok: false, error: `${path ? path + ": " : ""}${issue.message}` };
159
543
  }
544
+
545
+ /** @deprecated use ThreadRecordSchema */
546
+ export const SessionRecordSchema = ThreadRecordSchema;
547
+ /** @deprecated use validateThreadRecord */
548
+ export const validateSessionRecord = validateThreadRecord;