carbonless-review 0.4.0-rc.8 → 0.4.0

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 (55) hide show
  1. package/dist/build-info.json +2 -2
  2. package/dist/release-notes.json +216 -0
  3. package/dist/server/access.js +24 -17
  4. package/dist/server/agent/host.js +48 -0
  5. package/dist/server/agent/runner.js +20 -3
  6. package/dist/server/agent/store.js +95 -2
  7. package/dist/server/app.js +206 -11
  8. package/dist/server/diff/clusters.js +3 -1
  9. package/dist/server/diff/snapshot.js +88 -4
  10. package/dist/server/home/search.js +73 -1
  11. package/dist/server/mcp/server.js +3 -3
  12. package/dist/server/newThreads.js +9 -1
  13. package/dist/server/rereview/engine.js +100 -4
  14. package/dist/server/rereview/threads.js +25 -6
  15. package/dist/server/review/events.js +64 -2
  16. package/dist/server/review/refStore.js +7 -3
  17. package/dist/server/review/reviews.js +68 -11
  18. package/dist/server/session.js +41 -5
  19. package/dist/server/testHooks.js +33 -0
  20. package/dist/server/threadWrites.js +9 -0
  21. package/dist/server/update/notes.js +263 -0
  22. package/dist/server/update/registry.js +2 -2
  23. package/dist/server/update/updateHelper.js +57 -6
  24. package/dist/server/update/updater.js +70 -12
  25. package/dist/server/version.js +7 -0
  26. package/dist/shared/agent.js +42 -2
  27. package/dist/shared/imageTypes.js +62 -0
  28. package/dist/shared/linePairing.js +300 -0
  29. package/dist/shared/pieces.js +19 -2
  30. package/dist/shared/releaseNotes.js +299 -0
  31. package/dist/shared/rereview/placement.js +10 -4
  32. package/dist/shared/tokenize.js +50 -0
  33. package/dist/shared/tracer/lineMapper.js +1 -0
  34. package/dist/shared/tracer/trace.js +336 -15
  35. package/dist/shared/whatsNew.js +51 -0
  36. package/dist/web/assets/FilesPage-BK4HXW0F.js +5 -0
  37. package/dist/web/assets/{GitBranchIcon-DwFVRmqW.js → GitBranchIcon-D0MLuOkd.js} +1 -1
  38. package/dist/web/assets/OverviewPage-DZVgGeY8.js +3 -0
  39. package/dist/web/assets/ProgressBar-BVGLkymb.js +82 -0
  40. package/dist/web/assets/VerdictCard-CsF3RkSo.js +1 -0
  41. package/dist/web/assets/{VerdictPage-w40PC2LN.js → VerdictPage-DveXfaF5.js} +1 -1
  42. package/dist/web/assets/{WriteWarnings-CQNEwzbd.js → WriteWarnings-BSysCSqF.js} +1 -1
  43. package/dist/web/assets/index-C-CwkZFP.css +2 -0
  44. package/dist/web/assets/index-CYElfmUd.js +14 -0
  45. package/dist/web/assets/{openedOn-ufYHKvac.js → openedOn-Dc4pmu-V.js} +1 -1
  46. package/dist/web/assets/spanMarks-CEyzqf76.js +1 -0
  47. package/dist/web/index.html +4 -4
  48. package/package.json +220 -2
  49. package/dist/web/assets/FilesPage-CM6admiu.js +0 -5
  50. package/dist/web/assets/OverviewPage-BN6GhuSB.js +0 -3
  51. package/dist/web/assets/ProgressBar-DBfaazr1.js +0 -81
  52. package/dist/web/assets/VerdictCard-DCaHq1DT.js +0 -1
  53. package/dist/web/assets/index-DfdJcj92.css +0 -2
  54. package/dist/web/assets/index-cztJdXtC.js +0 -13
  55. package/dist/web/assets/spanMarks-CtgVXquJ.js +0 -1
@@ -1,4 +1,4 @@
1
1
  {
2
- "version": "0.4.0-rc.8",
3
- "commit": "2483dd0c18d2a7b9fd76b55556b1908404afd068"
2
+ "version": "0.4.0",
3
+ "commit": "aa12748a6d04ac2b939a8f5161a68eba079e4477"
4
4
  }
@@ -0,0 +1,216 @@
1
+ [
2
+ {
3
+ "version": "0.4.0",
4
+ "date": null,
5
+ "notes": [
6
+ {
7
+ "id": "CAR-183",
8
+ "kind": "new",
9
+ "prs": [
10
+ 110
11
+ ],
12
+ "text": "Files changed has a Threads tab beside the file tree, listing every conversation on the PR newest first, with where it is, who spoke last and a one-line excerpt."
13
+ },
14
+ {
15
+ "id": "CAR-38",
16
+ "kind": "new",
17
+ "prs": [
18
+ 118
19
+ ],
20
+ "text": "Changed images show as images: compare before and after with a slider or a fade, or side by side. View buttons switch a file between Image, Text and Binary. A renamed binary file no longer says \"Binary file changed\"."
21
+ },
22
+ {
23
+ "id": "CAR-163",
24
+ "kind": "new",
25
+ "prs": [
26
+ 120
27
+ ],
28
+ "text": "Ask your agent about the whole review. The header's agent chip opens a review-wide conversation in a side drawer (Shift+A). Your agent reads the PR, its GitHub threads and your progress through crb's tools, and can open conversations on the lines it points to. The chip's badge counts unread answers; with no agent set up, it opens the agent setup."
29
+ },
30
+ {
31
+ "id": "CAR-197",
32
+ "kind": "new",
33
+ "prs": [
34
+ 127
35
+ ],
36
+ "text": "A new comment has an Ask agent button that moves your text into an agent question, and the Ask agent box has one that moves it back. Text typed in Ask agent now asks before you leave the page."
37
+ },
38
+ {
39
+ "id": "CAR-198",
40
+ "kind": "new",
41
+ "prs": [
42
+ 129
43
+ ],
44
+ "text": "An agent conversation on code can become a GitHub comment: press Comment and the conversation turns into the new thread in place, with the agent conversation attached behind its agent button."
45
+ },
46
+ {
47
+ "id": "CAR-83",
48
+ "kind": "new",
49
+ "prs": [
50
+ 132
51
+ ],
52
+ "text": "The review agent can suggest a review comment: it appears in its conversation as an editable box with Add review comment, which posts your edited text as you, on the conversation's lines or as a reply to its GitHub thread."
53
+ },
54
+ {
55
+ "id": "CAR-201",
56
+ "kind": "new",
57
+ "prs": [
58
+ 144
59
+ ],
60
+ "text": "The update toast opens What's new: every version's notes up to the one on offer, with Update below. Global settings shows the running version's notes and earlier releases, each version linking to its release on GitHub."
61
+ },
62
+ {
63
+ "id": "CAR-193",
64
+ "kind": "new",
65
+ "prs": [
66
+ 145
67
+ ],
68
+ "text": "On the Threads tab, j and k step to the next and previous conversation."
69
+ },
70
+ {
71
+ "id": "CAR-203",
72
+ "kind": "new",
73
+ "prs": [
74
+ 146
75
+ ],
76
+ "text": "Tapping one of the agent's suggested questions fills the Ask agent box."
77
+ },
78
+ {
79
+ "id": "#126",
80
+ "kind": "improved",
81
+ "prs": [
82
+ 126
83
+ ],
84
+ "text": "On a first review, the patch set graph now shows a dot for the PR's current position, with the commit count from the base branch."
85
+ },
86
+ {
87
+ "id": "CAR-184",
88
+ "kind": "improved",
89
+ "prs": [
90
+ 130
91
+ ],
92
+ "text": "With Ignore whitespace on, whitespace-only changes also drop out of the verdict's \"You reviewed X of Y lines\"."
93
+ },
94
+ {
95
+ "id": "#141",
96
+ "kind": "improved",
97
+ "prs": [
98
+ 141
99
+ ],
100
+ "text": "Your browser keeps the access key for a week after your last visit, instead of losing it when the browser closes."
101
+ },
102
+ {
103
+ "id": "CAR-95",
104
+ "kind": "improved",
105
+ "prs": [
106
+ 150
107
+ ],
108
+ "text": "A comment whose line was rewritten now follows the line that resembles it, and is listed under its file as deleted when nothing does, instead of landing on whatever code took its place."
109
+ },
110
+ {
111
+ "id": "CAR-209",
112
+ "kind": "improved",
113
+ "prs": [
114
+ 154
115
+ ],
116
+ "text": "Type a PR number (`123` or `#123`) in Home's open box to list that PR in your view's repos and your recent reviews' repos, and open it with Enter."
117
+ },
118
+ {
119
+ "id": "CAR-129",
120
+ "kind": "fixed",
121
+ "prs": [
122
+ 121
123
+ ],
124
+ "text": "The update helper always writes its result file whole, so a restarting crb never reads a half-written result."
125
+ },
126
+ {
127
+ "id": "CAR-187",
128
+ "kind": "fixed",
129
+ "prs": [
130
+ 133
131
+ ],
132
+ "text": "A second crb server that sees the agent running elsewhere now shows the agent's answer straight away, not after the next poll."
133
+ },
134
+ {
135
+ "id": "CAR-177",
136
+ "kind": "fixed",
137
+ "prs": [
138
+ 134
139
+ ],
140
+ "text": "While crb restarts onto a new version, the page no longer reports a connection error, and a restart that fails quickly shows its failure."
141
+ },
142
+ {
143
+ "id": "#135",
144
+ "kind": "fixed",
145
+ "prs": [
146
+ 135
147
+ ],
148
+ "text": "On Windows, one-click Update finds pnpm installed by pnpm's standalone installer, instead of an older pnpm on PATH that left crb on its old version."
149
+ },
150
+ {
151
+ "id": "CAR-180",
152
+ "kind": "fixed",
153
+ "prs": [
154
+ 136
155
+ ],
156
+ "text": "The verdict card asks again when the PR's head moves before it loads, showing a spinner with its buttons disabled instead of the older head's answer."
157
+ },
158
+ {
159
+ "id": "CAR-194",
160
+ "kind": "fixed",
161
+ "prs": [
162
+ 137
163
+ ],
164
+ "text": "A new patch set never briefly shows the previous patch set's review under its name."
165
+ },
166
+ {
167
+ "id": "CAR-195",
168
+ "kind": "fixed",
169
+ "prs": [
170
+ 138
171
+ ],
172
+ "text": "In the since view, a cluster that grew around unchanged lines now keeps the marks you gave those lines in the earlier patch set, both reviewed and crossed off, so the file can finish there."
173
+ },
174
+ {
175
+ "id": "CAR-199",
176
+ "kind": "fixed",
177
+ "prs": [
178
+ 140
179
+ ],
180
+ "text": "A link to a review clicked on another site (a GitHub PR page, Slack, email) now opens the review instead of \"Missing or wrong access key\"; crb answers another site's page only for a link the reviewer clicked."
181
+ },
182
+ {
183
+ "id": "CAR-208",
184
+ "kind": "fixed",
185
+ "prs": [
186
+ 151
187
+ ],
188
+ "text": "In the since view, the first tick on a new push no longer makes the new and gone chips and the comment threads vanish and reappear, so the rows no longer jump under your pointer."
189
+ },
190
+ {
191
+ "id": "CAR-210",
192
+ "kind": "fixed",
193
+ "prs": [
194
+ 153
195
+ ],
196
+ "text": "With no agent connected, the agent button opens the whole-review conversation with the setup on top, so it is there once setup is done."
197
+ },
198
+ {
199
+ "id": "CAR-215",
200
+ "kind": "fixed",
201
+ "prs": [
202
+ 157
203
+ ],
204
+ "text": "Resetting a file, or any mark that covers several clusters, could briefly read back with some clusters still reviewed, for example after a reload during the save."
205
+ },
206
+ {
207
+ "id": "CAR-217",
208
+ "kind": "fixed",
209
+ "prs": [
210
+ 158
211
+ ],
212
+ "text": "In the since view, a comment on a removed line now goes on that line on GitHub; it used to be refused, or posted on another line. A line only an earlier patch set had can't take a comment, and Comment says why. A draft saved on a removed line in a since view before this version may hold the wrong line: check it before posting."
213
+ }
214
+ ]
215
+ }
216
+ ]
@@ -12,10 +12,19 @@ function cookieName(app) {
12
12
  : "crb_key";
13
13
  }
14
14
  const digest = (value) => createHash("sha256").update(value).digest();
15
+ /**
16
+ * How long the key cookie lasts since the last request that carried the key
17
+ * (in the URL or the cookie). Without one it was a session cookie: a phone
18
+ * closing the browser or a home-screen app in the background dropped it, and
19
+ * the next reload (the page strips `?key=`) was a 401.
20
+ */
21
+ export const KEY_COOKIE_MAX_AGE_S = 7 * 24 * 60 * 60;
15
22
  /**
16
23
  * Requires `key` on every request (API and static) when it's set: as
17
24
  * `?key=<key>`, which also sets an HttpOnly cookie (named per listening
18
- * port) for the page's later requests, or as that cookie. Used when crb listens beyond loopback.
25
+ * port) for the page's later requests, or as that cookie. Every request the
26
+ * key lets through sets the cookie again, so it lasts a week from the last
27
+ * visit. Used when crb listens beyond loopback.
19
28
  */
20
29
  export async function registerAccessKey(app, key, publicUrl) {
21
30
  if (key === null)
@@ -32,23 +41,21 @@ export async function registerAccessKey(app, key, publicUrl) {
32
41
  app.addHook("onRequest", async (request, reply) => {
33
42
  const cookie = cookieName(app);
34
43
  const query = request.query;
35
- if (matches(query?.key)) {
36
- // Lax, not Strict: a browser holds a Strict cookie back when the
37
- // reviewer clicks a link to a review on another site (GitHub),
38
- // which registerHostCheck allows on purpose. Every other request
39
- // from another site (no click: a script's navigation, a redirect
40
- // it started, a frame, a write) is refused there, on any path.
41
- reply.setCookie(cookie, key, {
42
- httpOnly: true,
43
- sameSite: "lax",
44
- path: "/",
45
- secure: (request.headers.host ?? "").toLowerCase() === secureHost,
46
- });
47
- return;
44
+ if (!matches(query?.key) && !matches(request.cookies[cookie])) {
45
+ return reply.code(401).send({ error: "Missing or wrong access key" });
48
46
  }
49
- if (matches(request.cookies[cookie]))
50
- return;
51
- return reply.code(401).send({ error: "Missing or wrong access key" });
47
+ // Lax, not Strict: a browser holds a Strict cookie back when the
48
+ // reviewer clicks a link to a review on another site (GitHub),
49
+ // which registerHostCheck allows on purpose. Every other request
50
+ // from another site (no click: a script's navigation, a redirect
51
+ // it started, a frame, a write) is refused there, on any path.
52
+ reply.setCookie(cookie, key, {
53
+ httpOnly: true,
54
+ sameSite: "lax",
55
+ path: "/",
56
+ maxAge: KEY_COOKIE_MAX_AGE_S,
57
+ secure: (request.headers.host ?? "").toLowerCase() === secureHost,
58
+ });
52
59
  });
53
60
  }
54
61
  /**
@@ -90,6 +90,20 @@ export class CoverageError extends Error {
90
90
  /** A request the agent host refuses as made (the app answers 400). */
91
91
  export class AgentRequestError extends Error {
92
92
  }
93
+ /** Whether a conversation's span anchor is the posted span (columns aside: GitHub has none). */
94
+ export function sameSpan(a, s) {
95
+ if (a.subject === "thread" || a.subject === "review")
96
+ return false;
97
+ if (a.path !== s.path || a.commit !== s.commit || a.subject !== s.subject) {
98
+ return false;
99
+ }
100
+ if (a.subject === "file")
101
+ return true;
102
+ return (a.side === s.side &&
103
+ a.line === s.line &&
104
+ a.startSide === (s.startSide ?? s.side) &&
105
+ a.startLine === (s.startLine ?? s.line));
106
+ }
93
107
  /**
94
108
  * A request that conflicts with what is there (the app answers 409): a
95
109
  * conversation already on that GitHub thread (`thread` names it), a
@@ -254,6 +268,7 @@ export class AgentHost {
254
268
  const { answering: _, error: __, elsewhere: ___, ...rest } = out;
255
269
  return {
256
270
  ...rest,
271
+ connected: true,
257
272
  state: presence === "listening"
258
273
  ? "ready"
259
274
  : presence === "busy"
@@ -423,11 +438,36 @@ export class AgentHost {
423
438
  const t = await this.store.setResolved(thread, resolved);
424
439
  return this.#view(await this.#joinedOne(t));
425
440
  }
441
+ /**
442
+ * The reviewer posted GitHub thread `github` on `span` from span
443
+ * conversation `thread` (POST threads' `agentThread`): the
444
+ * conversation becomes that thread's (store.link). A comment on
445
+ * another span than the conversation's (file, commit, sides, lines)
446
+ * is refused (AgentRequestError); a resend links nothing new.
447
+ */
448
+ async link(thread, github, span) {
449
+ const current = await this.store.find(thread);
450
+ // On a GitHub thread already (a resend, or another): store.link decides.
451
+ if (current.anchor.subject !== "thread" &&
452
+ !sameSpan(current.anchor, span)) {
453
+ throw new AgentRequestError("The comment is on another span than the conversation");
454
+ }
455
+ const t = await this.store.link(current.id, github);
456
+ return this.#view(await this.#joinedOne(t));
457
+ }
426
458
  /** The reviewer has seen the thread up to a message (no write when already seen). */
427
459
  async seen(thread, upTo) {
428
460
  const t = await this.store.seen(thread, upTo);
429
461
  return this.#view(await this.#joinedOne(t));
430
462
  }
463
+ /**
464
+ * The reviewer posted a ```comment block of an agent message (after
465
+ * GitHub took it): recorded once per block.
466
+ */
467
+ async posted(thread, req) {
468
+ const t = await this.store.posted(thread, req);
469
+ return this.#view(await this.#joinedOne(t));
470
+ }
431
471
  cancel() {
432
472
  return this.runner.cancel();
433
473
  }
@@ -612,6 +652,14 @@ export class AgentHost {
612
652
  ...(m.origin && requestText(m.origin) !== undefined
613
653
  ? { re: requestText(m.origin) }
614
654
  : {}),
655
+ ...(m.posted?.length
656
+ ? {
657
+ posted: m.posted.map((p) => ({
658
+ block: p.block,
659
+ text: p.text,
660
+ })),
661
+ }
662
+ : {}),
615
663
  }
616
664
  : {
617
665
  author: viewer && role ? { login: viewer, role } : { role: "reviewer" },
@@ -3,6 +3,7 @@ import { existsSync } from "node:fs";
3
3
  import { hostname } from "node:os";
4
4
  import { basename, join } from "node:path";
5
5
  import { AGENT_TURN_MS, isAgentQuestion, JOINED_PREAMBLE, } from "../../shared/agent.js";
6
+ import { GitError } from "../git.js";
6
7
  import { AcpAgent, AgentStartError, agentEnv } from "./acp.js";
7
8
  import { agentsDir, createCheckout } from "./checkout.js";
8
9
  import { takeClaim } from "./claim.js";
@@ -229,13 +230,19 @@ export class AgentRunner {
229
230
  }
230
231
  }
231
232
  let waiting = 0;
233
+ let threadsRev;
232
234
  try {
235
+ const read = await this.#deps.store.readWithRevision();
233
236
  // The raw fold, not GitHub-joined: an orphan still has subject "thread"
234
237
  // here, so its own resolve counts (isAgentQuestion would ignore it).
235
- waiting = (await this.#deps.store.read()).filter((t) => t.waiting && (t.anchor.subject === "review" || !t.resolved)).length;
238
+ waiting = read.threads.filter((t) => t.waiting && (t.anchor.subject === "review" || !t.resolved)).length;
239
+ threadsRev = read.revision;
236
240
  }
237
- catch {
238
- // The count is a hint; the ref may be unreadable a moment.
241
+ catch (error) {
242
+ // The count is a hint; the ref may be unreadable a moment. Only
243
+ // git's failures: anything else is a bug, not an empty ref.
244
+ if (!(error instanceof GitError))
245
+ throw error;
239
246
  }
240
247
  // The ignored-wake warning ends once nothing waits.
241
248
  if (waiting === 0 &&
@@ -259,6 +266,14 @@ export class AgentRunner {
259
266
  ? this.#problems.join("; ")
260
267
  : undefined;
261
268
  const hand = this.#liveHand();
269
+ // The current process up and usable, whatever its last turn did (a
270
+ // failed turn keeps it) and whatever config it was started with (one
271
+ // replaced by a config change is connected until it stops).
272
+ const connected = !stopping &&
273
+ !this.#closed &&
274
+ !this.#off() &&
275
+ !this.#proc?.stopping &&
276
+ Boolean(this.#proc?.agent?.alive);
262
277
  return {
263
278
  state: stopping ? "off" : this.#state,
264
279
  configured: this.#configured,
@@ -270,7 +285,9 @@ export class AgentRunner {
270
285
  ? { elsewhere: this.#elsewhere }
271
286
  : {}),
272
287
  ...(this.#seenAt() ? { seen: this.#seenAt() } : {}),
288
+ ...(connected ? { connected: true } : {}),
273
289
  waiting,
290
+ ...(threadsRev !== undefined ? { threadsRev } : {}),
274
291
  };
275
292
  }
276
293
  /** The handed-out question with its live activity, while the spawned agent holds it. */
@@ -1,5 +1,5 @@
1
1
  import { monotonicFactory } from "ulid";
2
- import { MAX_ACTIVITY_BYTES, MAX_ACTIVITY_ENTRIES, MAX_AGENT_ERROR, MAX_AGENT_TEXT, } from "../../shared/agent.js";
2
+ import { agentCommentClientId, linkedAnchor, MAX_ACTIVITY_BYTES, MAX_ACTIVITY_ENTRIES, MAX_AGENT_ERROR, MAX_AGENT_TEXT, } from "../../shared/agent.js";
3
3
  import { isLowerUlid, isSlug, newEvent, } from "../review/events.js";
4
4
  /**
5
5
  * The reviewer's private threads with their own agent, as events in
@@ -32,11 +32,19 @@ export class UnknownAgentThreadError extends Error {
32
32
  this.thread = thread;
33
33
  }
34
34
  }
35
+ /**
36
+ * A link the store won't write (the app answers 409): the conversation is
37
+ * already a GitHub thread's, or that GitHub thread already has one.
38
+ */
39
+ export class AgentLinkError extends Error {
40
+ }
35
41
  const iso = (ms) => new Date(ms).toISOString();
36
42
  const isAgentEvent = (e) => e.type === "agent-thread-opened" ||
37
43
  e.type === "agent-message" ||
38
44
  e.type === "agent-thread-resolved" ||
39
- e.type === "agent-thread-seen";
45
+ e.type === "agent-thread-seen" ||
46
+ e.type === "agent-comment-posted" ||
47
+ e.type === "agent-thread-linked";
40
48
  const clientIdOf = (e) => {
41
49
  const id = e.details?.clientId;
42
50
  return typeof id === "string" ? id : undefined;
@@ -147,6 +155,30 @@ function foldWithAliases(events) {
147
155
  if (thread)
148
156
  seenUpTo.set(id, [...(seenUpTo.get(id) ?? []), e.upTo]);
149
157
  break;
158
+ case "agent-comment-posted": {
159
+ // Only onto an agent message the thread holds; a block once.
160
+ const m = thread?.messages.find((x) => x.id === e.message);
161
+ if (m?.by !== "ai")
162
+ break;
163
+ if (m.posted?.some((p) => p.block === e.block))
164
+ break;
165
+ m.posted = [
166
+ ...(m.posted ?? []),
167
+ { block: e.block, url: e.github.url, at: iso(e.ts), text: e.text },
168
+ ].sort((a, b) => a.block - b.block);
169
+ break;
170
+ }
171
+ case "agent-thread-linked":
172
+ // A span conversation becomes the GitHub thread's, once; a link
173
+ // onto a GitHub thread that has a conversation already is
174
+ // dropped (the conversation stays on its span, still listed).
175
+ if (!thread || thread.anchor.subject === "thread")
176
+ break;
177
+ if (byGithub.has(e.github))
178
+ break;
179
+ thread.anchor = linkedAnchor(thread.anchor, e.github);
180
+ byGithub.set(e.github, id);
181
+ break;
150
182
  }
151
183
  }
152
184
  const out = [...threads.values()];
@@ -273,6 +305,17 @@ export class AgentThreadStore {
273
305
  async read() {
274
306
  return foldAgentThreads(await this.#events());
275
307
  }
308
+ /**
309
+ * The threads, and a revision that changes with every write to the ref
310
+ * (any server's): the tip commit the threads were read from, "" before
311
+ * the first write.
312
+ */
313
+ async readWithRevision() {
314
+ const { tip, events } = await this.store.readEventsAt(this.#ref);
315
+ for (const e of events)
316
+ this.#remember(e);
317
+ return { threads: foldAgentThreads(events), revision: tip ?? "" };
318
+ }
276
319
  /** The thread with this id (a later opening's id finds the first's); UnknownAgentThreadError if none. */
277
320
  async find(thread) {
278
321
  return this.#find(await this.#events(), thread);
@@ -407,6 +450,56 @@ export class AgentThreadStore {
407
450
  return this.#write(events, "crb: agent thread seen", event);
408
451
  });
409
452
  }
453
+ /**
454
+ * The reviewer posted block `block` of agent message `message` to
455
+ * GitHub (after GitHub took it). A block already recorded writes
456
+ * nothing; a message the thread doesn't hold, or not the agent's, is
457
+ * UnknownAgentMessageError.
458
+ */
459
+ posted(thread, input) {
460
+ return this.#serial(async () => {
461
+ const events = await this.#events();
462
+ const current = this.#find(events, thread);
463
+ const m = current.messages.find((x) => x.id === input.message);
464
+ if (m?.by !== "ai")
465
+ throw new UnknownAgentMessageError(input.message);
466
+ if (m.posted?.some((p) => p.block === input.block))
467
+ return current;
468
+ const event = newEvent("agent-comment-posted", this.slug, {
469
+ thread: current.id,
470
+ message: input.message,
471
+ block: input.block,
472
+ clientId: agentCommentClientId(current.id, input.message, input.block),
473
+ github: input.github,
474
+ text: capText(input.text).text,
475
+ }, "human", stampAfter(events));
476
+ return this.#write(events, "crb: agent comment posted", event);
477
+ });
478
+ }
479
+ /**
480
+ * The reviewer started GitHub thread `github` from this span
481
+ * conversation: it becomes that thread's conversation. Already linked
482
+ * to it: nothing written. Refused (AgentLinkError) when the
483
+ * conversation is another GitHub thread's, or `github` has one.
484
+ */
485
+ link(thread, github) {
486
+ return this.#serial(async () => {
487
+ const events = await this.#events();
488
+ const { threads } = foldWithAliases(events);
489
+ const current = this.#find(events, thread);
490
+ if (current.anchor.subject === "thread") {
491
+ if (current.anchor.thread === github)
492
+ return current;
493
+ throw new AgentLinkError("This conversation is already on a GitHub thread");
494
+ }
495
+ const taken = threads.find((t) => t.anchor.subject === "thread" && t.anchor.thread === github);
496
+ if (taken) {
497
+ throw new AgentLinkError("That GitHub thread already has a conversation");
498
+ }
499
+ const event = newEvent("agent-thread-linked", this.slug, { thread: current.id, github }, "human", stampAfter(events));
500
+ return this.#write(events, "crb: agent thread linked to a GitHub thread", event);
501
+ });
502
+ }
410
503
  /** The ref's events; every human clientId in them is remembered. */
411
504
  async #events() {
412
505
  const events = await this.store.readEvents(this.#ref);