carbonless-review 0.4.0 → 0.5.0-rc.2

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 (35) hide show
  1. package/dist/build-info.json +2 -2
  2. package/dist/release-notes.json +9 -1
  3. package/dist/server/agent/host.js +2 -1
  4. package/dist/server/agent/store.js +76 -0
  5. package/dist/server/app.js +25 -8
  6. package/dist/server/rereview/moveBody.js +85 -0
  7. package/dist/server/rereview/threads.js +121 -26
  8. package/dist/server/review/events.js +69 -0
  9. package/dist/server/review/fold.js +4 -10
  10. package/dist/server/review/reviews.js +0 -19
  11. package/dist/server/session.js +172 -34
  12. package/dist/shared/rereview/moves.js +156 -0
  13. package/dist/shared/rereview/placement.js +13 -0
  14. package/dist/web/assets/FilesPage-D_okrQcT.js +5 -0
  15. package/dist/web/assets/{GitBranchIcon-D0MLuOkd.js → GitBranchIcon-JyM-TJV4.js} +1 -1
  16. package/dist/web/assets/OverviewPage-BIKbg_n-.js +3 -0
  17. package/dist/web/assets/ProgressBar-BjvpyHo6.js +82 -0
  18. package/dist/web/assets/VerdictCard-Cfu8jreO.js +1 -0
  19. package/dist/web/assets/{VerdictPage-DveXfaF5.js → VerdictPage-BtO7JCRn.js} +1 -1
  20. package/dist/web/assets/{WriteWarnings-BSysCSqF.js → WriteWarnings-BE2OWR0u.js} +1 -1
  21. package/dist/web/assets/index-BOBT658S.css +2 -0
  22. package/dist/web/assets/index-DCusIKiM.js +14 -0
  23. package/dist/web/assets/spanMarks-BIXbaXED.js +1 -0
  24. package/dist/web/assets/sticky-BDxR8yfy.js +1 -0
  25. package/dist/web/index.html +4 -4
  26. package/package.json +11 -3
  27. package/dist/server/rereview/placementBody.js +0 -35
  28. package/dist/web/assets/FilesPage-BK4HXW0F.js +0 -5
  29. package/dist/web/assets/OverviewPage-DZVgGeY8.js +0 -3
  30. package/dist/web/assets/ProgressBar-BVGLkymb.js +0 -82
  31. package/dist/web/assets/VerdictCard-CsF3RkSo.js +0 -1
  32. package/dist/web/assets/index-C-CwkZFP.css +0 -2
  33. package/dist/web/assets/index-CYElfmUd.js +0 -14
  34. package/dist/web/assets/openedOn-Dc4pmu-V.js +0 -1
  35. package/dist/web/assets/spanMarks-CEyzqf76.js +0 -1
@@ -1,4 +1,4 @@
1
1
  {
2
- "version": "0.4.0",
3
- "commit": "aa12748a6d04ac2b939a8f5161a68eba079e4477"
2
+ "version": "0.5.0-rc.2",
3
+ "commit": "210c9f7eded6cb65b81d3ee9e8df653ee5ffe919"
4
4
  }
@@ -1,7 +1,15 @@
1
1
  [
2
2
  {
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "date": null,
5
+ "notes": [],
6
+ "uncovered": [
7
+ 149
8
+ ]
9
+ },
10
+ {
11
+ "version": "0.4.0",
12
+ "date": "2026-11-22",
5
13
  "notes": [
6
14
  {
7
15
  "id": "CAR-183",
@@ -1482,7 +1482,8 @@ export async function agentConfig() {
1482
1482
  }
1483
1483
  /** The review's agent host: one thread store, one runner, the session's facts. */
1484
1484
  export function reviewAgentHost(src) {
1485
- const store = new AgentThreadStore(new RefStore(src.repo), src.reviewId, src.viewer);
1485
+ const store = src.store ??
1486
+ new AgentThreadStore(new RefStore(src.repo), src.reviewId, src.viewer);
1486
1487
  /** The PR author's login; "" when it can't be read (nobody is then). */
1487
1488
  const prAuthor = () => src.prAuthor().catch(() => "");
1488
1489
  const facts = {
@@ -1,5 +1,6 @@
1
1
  import { monotonicFactory } from "ulid";
2
2
  import { agentCommentClientId, linkedAnchor, MAX_ACTIVITY_BYTES, MAX_ACTIVITY_ENTRIES, MAX_AGENT_ERROR, MAX_AGENT_TEXT, } from "../../shared/agent.js";
3
+ import { TRACER_VERSION } from "../../shared/tracer/index.js";
3
4
  import { isLowerUlid, isSlug, newEvent, } from "../review/events.js";
4
5
  /**
5
6
  * The reviewer's private threads with their own agent, as events in
@@ -476,6 +477,52 @@ export class AgentThreadStore {
476
477
  return this.#write(events, "crb: agent comment posted", event);
477
478
  });
478
479
  }
480
+ /**
481
+ * The thread moves in the ref (`thread-moved`, `thread-move-undone`),
482
+ * as read: an agent thread's move is dropped unless that thread was
483
+ * opened on lines (a file thread, a thread conversation and the review
484
+ * conversation are never moved); a GitHub thread's is kept (the
485
+ * caller uses it only for a thread GitHub lists). Folding is the
486
+ * caller's (foldMoves), with the legacy moves it holds.
487
+ */
488
+ async moves() {
489
+ return movesOf(await this.#events());
490
+ }
491
+ /** Writes a `thread-moved` (by the reviewer); returns the move. */
492
+ move(input) {
493
+ return this.#serial(async () => {
494
+ const events = await this.#events();
495
+ const { path, startSide, startLine, side, line } = input.anchor;
496
+ const event = newEvent("thread-moved", this.slug, {
497
+ thread: { source: input.thread.source, id: input.thread.id },
498
+ on: input.on,
499
+ anchor: { path, startSide, startLine, side, line },
500
+ engine: TRACER_VERSION,
501
+ }, "human", stampAfter(events));
502
+ const where = `${oneLinePath(path)}:${line}`;
503
+ const on = input.on.patchset !== undefined
504
+ ? `PS${input.on.patchset}`
505
+ : input.on.head.slice(0, 7);
506
+ await this.store.appendEvent(this.#ref, `crb: moved a thread to ${where} on ${on}`, event, this.slug);
507
+ return {
508
+ id: event.id,
509
+ ts: event.ts,
510
+ thread: event.thread,
511
+ on: event.on,
512
+ anchor: event.anchor,
513
+ engine: event.engine,
514
+ };
515
+ });
516
+ }
517
+ /** Writes a `thread-move-undone` taking back move `move` of `thread`. */
518
+ undoMove(thread, move) {
519
+ return this.#serial(async () => {
520
+ const events = await this.#events();
521
+ const event = newEvent("thread-move-undone", this.slug, { thread: { source: thread.source, id: thread.id }, move }, "human", stampAfter(events));
522
+ await this.store.appendEvent(this.#ref, "crb: undid a thread move", event, this.slug);
523
+ return { id: event.id, ts: event.ts, thread: event.thread, move };
524
+ });
525
+ }
479
526
  /**
480
527
  * The reviewer started GitHub thread `github` from this span
481
528
  * conversation: it becomes that thread's conversation. Already linked
@@ -577,6 +624,35 @@ export class AgentThreadStore {
577
624
  return next;
578
625
  }
579
626
  }
627
+ /** A path on one line, for a commit's summary. */
628
+ const oneLinePath = (path) => path.replace(/[\r\n]+/g, " ").slice(0, 200);
629
+ /** The moves and undos of a ref's events (AgentThreadStore.moves). */
630
+ export function movesOf(events) {
631
+ const lines = new Set(foldAgentThreads(events)
632
+ .filter((t) => t.anchor.subject === "line")
633
+ .map((t) => t.id));
634
+ const movable = (t) => t.source === "github" || lines.has(t.id);
635
+ const moves = [];
636
+ const undos = [];
637
+ for (const e of events) {
638
+ if (e.by !== "human")
639
+ continue;
640
+ if (e.type === "thread-moved" && movable(e.thread)) {
641
+ moves.push({
642
+ id: e.id,
643
+ ts: e.ts,
644
+ thread: e.thread,
645
+ on: e.on,
646
+ anchor: e.anchor,
647
+ engine: e.engine,
648
+ });
649
+ }
650
+ else if (e.type === "thread-move-undone") {
651
+ undos.push({ id: e.id, ts: e.ts, thread: e.thread, move: e.move });
652
+ }
653
+ }
654
+ return { moves, undos };
655
+ }
580
656
  const aiKey = (origin, key) => `${origin.message} ${key}`;
581
657
  /** Now, or just after the newest stored event when the clock is behind it. */
582
658
  function stampAfter(events) {
@@ -26,8 +26,8 @@ import { joinableDeps, listJoinable, registerMcp } from "./mcp/routes.js";
26
26
  import { newThreadProblem } from "./newThreads.js";
27
27
  import { parseHead } from "./pinnedHead.js";
28
28
  import { RefusedReviewError, UnknownReviewError, } from "./registry.js";
29
- import { parsePlacementBody } from "./rereview/placementBody.js";
30
- import { InvalidPlacementError } from "./rereview/threads.js";
29
+ import { parseMoveBody } from "./rereview/moveBody.js";
30
+ import { DeadMoveError, InvalidPlacementError, MoveCapError, UnknownMoveThreadError, } from "./rereview/threads.js";
31
31
  import { isLowerUlid } from "./review/events.js";
32
32
  import { InvalidChangeError, RefusedChangeError, refusedMessage, StaleSnapshotError, } from "./review/reviews.js";
33
33
  import { SessionClosedError, UnknownReadError, } from "./session.js";
@@ -220,6 +220,11 @@ export async function buildApp(registry, options) {
220
220
  if (error instanceof InvalidPlacementError) {
221
221
  return send(400, error.message);
222
222
  }
223
+ if (error instanceof UnknownMoveThreadError)
224
+ return send(404, error.message);
225
+ if (error instanceof DeadMoveError || error instanceof MoveCapError) {
226
+ return send(409, error.message);
227
+ }
223
228
  if (error instanceof NoAgentError) {
224
229
  return reply.status(404).send({ error: error.message, code: "no-agent" });
225
230
  }
@@ -669,12 +674,6 @@ export async function buildApp(registry, options) {
669
674
  }
670
675
  return sessionOf(request).setThreadResolved(threadIdParam(request.params.threadId), resolved);
671
676
  });
672
- // The reviewer places a thread on lines of their latest patch set
673
- // (a drag): `thread-placed`, checked against the patch set first.
674
- review.post("/threads/:threadId/placement", (request) => {
675
- const { patchset, anchor } = parsePlacementBody(request.body);
676
- return sessionOf(request).placeThread(threadIdParam(request.params.threadId), patchset, anchor);
677
- });
678
677
  review.patch("/threads/:threadId/comments/:commentId", (request) => {
679
678
  const problem = editProblem(request.body);
680
679
  if (problem)
@@ -706,6 +705,24 @@ export async function buildApp(registry, options) {
706
705
  retry: r.retry,
707
706
  });
708
707
  });
708
+ // The reviewer moves a thread (a review or agent thread) to lines
709
+ // of the target they view, and takes a move back (CAR-139):
710
+ // their private ref only, never GitHub.
711
+ review.post("/moves", (request) => sessionOf(request).moveThread(parseMoveBody(request.body)));
712
+ review.delete("/moves/:move", (request) => {
713
+ const id = request.params.move;
714
+ if (!isLowerUlid(id))
715
+ throw new BadRequestError("Not a move id");
716
+ // The target viewed, as for a move: an earlier patch set is
717
+ // read-only, its moves too.
718
+ const { ps, head } = request.query;
719
+ if ((ps === undefined) === (head === undefined)) {
720
+ throw new BadRequestError("ps (a patch set) or head (a commit id), one of them");
721
+ }
722
+ return sessionOf(request).undoMove(id, ps !== undefined
723
+ ? { patchset: patchsetParam(ps) }
724
+ : { head: oidParam("head", head) });
725
+ });
709
726
  // The reviewer's own agent (review buddy): status, private threads.
710
727
  review.get("/agent", (request) => agentOf(request).status());
711
728
  review.get("/agent/threads", async (request) => ({
@@ -0,0 +1,85 @@
1
+ import { MAX_GITHUB_THREAD_ID, MAX_MOVE_PATH, } from "../../shared/rereview/moves.js";
2
+ import { isLowerUlid } from "../review/events.js";
3
+ import { InvalidPlacementError } from "./threads.js";
4
+ const fail = (message) => new InvalidPlacementError(message);
5
+ const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
6
+ const isSide = (v) => v === "LEFT" || v === "RIGHT";
7
+ const isLine = (v) => typeof v === "number" && Number.isInteger(v) && v >= 1 && v < 2 ** 31;
8
+ const OID = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
9
+ /** A move's thread: `{source: "agent", id: <lowercase ULID>}` or `{source: "github", id: <node id>}`. */
10
+ export function parseThreadRef(raw) {
11
+ if (!isObj(raw))
12
+ throw fail("thread is { source, id }");
13
+ const { source, id } = raw;
14
+ if (source === "agent") {
15
+ if (typeof id === "string" && isLowerUlid(id))
16
+ return { source, id };
17
+ throw fail("An agent thread's id is a lowercase ULID");
18
+ }
19
+ if (source === "github") {
20
+ if (typeof id === "string" &&
21
+ id !== "" &&
22
+ id.length <= MAX_GITHUB_THREAD_ID)
23
+ return { source, id };
24
+ throw fail("A GitHub thread's id is 1 to 200 characters");
25
+ }
26
+ throw fail("thread.source must be agent or github");
27
+ }
28
+ /** `{patchset}` or `{head}`, one of them (the target viewed). */
29
+ export function parseMoveTarget(raw) {
30
+ if (!isObj(raw))
31
+ throw fail("to is { patchset } or { head }");
32
+ const { patchset, head } = raw;
33
+ if ((patchset === undefined) === (head === undefined)) {
34
+ throw fail("to: a patch set or a head, one of them");
35
+ }
36
+ if (patchset !== undefined) {
37
+ if (!isLine(patchset))
38
+ throw fail("to.patchset must be a patch set number");
39
+ return { patchset };
40
+ }
41
+ if (typeof head !== "string" || !OID.test(head)) {
42
+ throw fail("to.head must be a commit id");
43
+ }
44
+ return { head };
45
+ }
46
+ /**
47
+ * A move's lines: sides LEFT or RIGHT, lines positive integers (start not
48
+ * after end on one side) and a path of at most MAX_MOVE_PATH bytes (the
49
+ * session checks it against the target's files). Only these fields.
50
+ */
51
+ export function parseMoveAnchor(raw) {
52
+ if (!isObj(raw))
53
+ throw fail("anchor is required");
54
+ const { path, startSide, startLine, side, line } = raw;
55
+ if (typeof path !== "string" ||
56
+ path === "" ||
57
+ Buffer.byteLength(path) > MAX_MOVE_PATH) {
58
+ throw fail("anchor.path must be a file's path");
59
+ }
60
+ if (!isSide(side) || !isSide(startSide)) {
61
+ throw fail("anchor sides must be LEFT or RIGHT");
62
+ }
63
+ if (!isLine(line) || !isLine(startLine)) {
64
+ throw fail("anchor lines must be positive integers");
65
+ }
66
+ if (side === startSide && startLine > line) {
67
+ throw fail("anchor must start before it ends");
68
+ }
69
+ return { path, startSide, startLine, side, line };
70
+ }
71
+ /**
72
+ * POST /moves's body: `{thread, to, anchor}`, each checked; nothing else
73
+ * is taken (the snapshot's base and head are the server's, never the
74
+ * body's).
75
+ */
76
+ export function parseMoveBody(body) {
77
+ if (!isObj(body))
78
+ throw fail("A move is { thread, to, anchor }");
79
+ return {
80
+ thread: parseThreadRef(body.thread),
81
+ to: parseMoveTarget(body.to),
82
+ anchor: parseMoveAnchor(body.anchor),
83
+ };
84
+ }
85
+ //# sourceMappingURL=moveBody.js.map
@@ -8,7 +8,8 @@
8
8
  * Everything git answers is keyed by OIDs, so it is cached for the
9
9
  * session; only the reviewer's stored placements are read each time.
10
10
  */
11
- import { placeThread, } from "../../shared/rereview/placement.js";
11
+ import { effectiveMove, placeWithMoves, } from "../../shared/rereview/moves.js";
12
+ import { placeThread, validAnchor, } from "../../shared/rereview/placement.js";
12
13
  import { createLineMapper, IDENTITY, } from "../../shared/tracer/index.js";
13
14
  import { fileText } from "../diff/snapshot.js";
14
15
  import { runGitBuffer } from "../git.js";
@@ -16,6 +17,40 @@ import { diffMap, linesMapper, NONE } from "./engine.js";
16
17
  /** A placement the reviewer asked for that doesn't fit the patch set (400). */
17
18
  export class InvalidPlacementError extends Error {
18
19
  }
20
+ /** A move of a thread the reviewer doesn't have (404). */
21
+ export class UnknownMoveThreadError extends Error {
22
+ }
23
+ /** An undo of a move that isn't live (409): nothing changes. */
24
+ export class DeadMoveError extends Error {
25
+ }
26
+ /** A move of a thread that already has MAX_MOVES_PER_THREAD live moves (409). */
27
+ export class MoveCapError extends Error {
28
+ }
29
+ /**
30
+ * The reviewer's `thread-placed` events (their progress ref) read as moves
31
+ * of GitHub threads (CAR-139), on their patch set's snapshot (pinned by
32
+ * `patchsets/<n>` and `-base`); one naming an unknown patch set is
33
+ * dropped. Nothing is written: they stay as they are.
34
+ */
35
+ export function legacyMoves(progress) {
36
+ const out = [];
37
+ for (const e of progress.placed) {
38
+ if (e.by !== "human")
39
+ continue;
40
+ const ps = progress.patchsets.find((p) => p.number === e.patchset);
41
+ if (!ps)
42
+ continue;
43
+ out.push({
44
+ id: e.id,
45
+ ts: e.ts,
46
+ thread: { source: "github", id: e.thread },
47
+ on: { base: ps.mergeBase, head: ps.head, patchset: ps.number },
48
+ anchor: e.anchor,
49
+ engine: e.engine,
50
+ });
51
+ }
52
+ return out;
53
+ }
19
54
  /**
20
55
  * The threads placement covers: those still on GitHub, with an id.
21
56
  * Resolved ones too (owner, 2026-11-08): an unplaced resolved thread
@@ -46,19 +81,20 @@ export function agentTraceable(t) {
46
81
  }
47
82
  /**
48
83
  * The reviewer's agent threads placed into `target` by the same tracer as
49
- * review threads (no stored placements: a drag places review threads
50
- * only). Each is traced on its own, so one whose tracing fails is left
51
- * out, never the rest: the page then shows it where it was asked (on an
52
- * earlier head, its file's lead, outdated), never nowhere. A thread
53
- * conversation (a `thread` anchor) is never traced: it goes where its
54
- * GitHub thread goes (AgentHost.asked() gives an orphan its span anchor).
84
+ * review threads, with the reviewer's moves laid over (`movesOf`, by
85
+ * thread id; ThreadPlacer.placeMoved). Each is traced on its own, so one
86
+ * whose tracing fails is left out, never the rest: the page then shows it
87
+ * where it was asked (on an earlier head, its file's lead, outdated),
88
+ * never nowhere. A thread conversation (a `thread` anchor) is never
89
+ * traced: it goes where its GitHub thread goes (AgentHost.asked() gives
90
+ * an orphan its span anchor).
55
91
  */
56
- export async function placeAgentThreads(placer, target, threads, base) {
92
+ export async function placeAgentThreads(placer, target, threads, base, movesOf = () => undefined, visible = () => true) {
57
93
  const spans = threads.filter((t) => t.anchor.subject !== "thread");
58
94
  const placed = await Promise.all(spans.map(async (t) => {
59
95
  try {
60
96
  const inputs = await placer.inputs(target, [agentTraceable(t)], base);
61
- return await placer.place(target, inputs, new Map());
97
+ return await placer.placeMoved(target, inputs, movesOf, base, visible);
62
98
  }
63
99
  catch {
64
100
  return [];
@@ -66,6 +102,29 @@ export async function placeAgentThreads(placer, target, threads, base) {
66
102
  }));
67
103
  return placed.flat();
68
104
  }
105
+ /**
106
+ * A moved thread as the tracer reads it: on the move's lines, traced from
107
+ * the move's own snapshot (never from GitHub's line).
108
+ */
109
+ export function movedTraceable(id, m) {
110
+ const at = new Date(m.ts).toISOString();
111
+ return {
112
+ id,
113
+ path: m.anchor.path,
114
+ commit: m.on.head,
115
+ createdAt: at,
116
+ anchor: {
117
+ path: m.anchor.path,
118
+ commit: m.on.head,
119
+ side: m.anchor.side,
120
+ line: m.anchor.line,
121
+ startSide: m.anchor.startSide,
122
+ startLine: m.anchor.startLine,
123
+ at,
124
+ },
125
+ ends: { base: m.on.base, head: m.on.head },
126
+ };
127
+ }
69
128
  const EMPTY = Buffer.alloc(0);
70
129
  /** A thread's range as the tracer reads it, or a stand-in for a file comment. */
71
130
  function anchorOf(t) {
@@ -209,6 +268,9 @@ export class ThreadPlacer {
209
268
  const list = placeable(threads);
210
269
  const own = await Promise.all(list.map(async (t) => {
211
270
  const anchor = anchorOf(t);
271
+ // A move's own snapshot: traced from it alone.
272
+ if (t.ends)
273
+ return { anchor: t.anchor && anchor, ends: t.ends };
212
274
  const ends = await this.code.endpoints(anchor, base);
213
275
  if (ends)
214
276
  return { anchor: t.anchor && anchor, ends };
@@ -312,25 +374,58 @@ export class ThreadPlacer {
312
374
  }));
313
375
  }
314
376
  /**
315
- * The placements of `inputs` in `target` with the reviewer's stored
316
- * ones (by thread id) laid over, each checked against the target.
377
+ * The placements of `inputs` in `target` with the reviewer's moves laid
378
+ * over (moves.ts: a move made on the target, else the last visible one
379
+ * traced from its snapshot, else the thread's own trace). `movesOf`
380
+ * gives a thread's live moves, oldest first. A move's trace that git
381
+ * can't make (its commits gone) leaves the thread `rewritten`; every
382
+ * input is placed exactly once.
317
383
  */
318
- async place(target, inputs, stored) {
319
- const counts = new Map();
320
- await Promise.all([...stored.values()].flatMap((a) => [a.side, a.startSide].map(async (side) => {
321
- const key = `${side}:${a.path}`;
322
- if (!counts.has(key)) {
323
- counts.set(key, null);
324
- counts.set(key, await this.lineCount(target, a.path, side));
384
+ async placeMoved(target, inputs, movesOf, base, visible = () => true) {
385
+ const here = (m) => m.on.base === target.base && m.on.head === target.head;
386
+ // Every line count a move made here needs, all in before any is
387
+ // read: a count still on its way never reads as "no such file".
388
+ const wanted = new Map();
389
+ for (const input of inputs) {
390
+ for (const m of movesOf(input.id) ?? []) {
391
+ if (!here(m) || !visible(m))
392
+ continue;
393
+ for (const side of [m.anchor.side, m.anchor.startSide]) {
394
+ wanted.set(`${side}:${m.anchor.path}`, { path: m.anchor.path, side });
395
+ }
325
396
  }
326
- })));
327
- return inputs.map((input) => {
328
- const s = stored.get(input.id);
329
- return {
330
- id: input.id,
331
- placement: placeThread(s ? { ...input, stored: s } : input, (path, side) => counts.get(`${side}:${path}`) ?? null),
332
- };
333
- });
397
+ }
398
+ const counts = new Map(await Promise.all([...wanted].map(async ([key, { path, side }]) => [key, await this.lineCount(target, path, side)])));
399
+ const lineCount = (path, side) => counts.get(`${side}:${path}`) ?? null;
400
+ const choices = new Map();
401
+ await Promise.all(inputs.map(async (input) => {
402
+ const moves = movesOf(input.id);
403
+ if (!moves || moves.length === 0)
404
+ return;
405
+ const chosen = effectiveMove(moves, target, {
406
+ visible,
407
+ fits: (m) => validAnchor(m.anchor, lineCount),
408
+ });
409
+ if (!chosen)
410
+ return;
411
+ if (chosen.here) {
412
+ choices.set(input.id, { move: chosen.move, here: true });
413
+ return;
414
+ }
415
+ const m = chosen.move;
416
+ const inComparison = target.files.some((f) => f.path === m.anchor.path || f.oldPath === m.anchor.path);
417
+ const traced = await this.inputs(target, [movedTraceable(input.id, m)], base)
418
+ .then((list) => list[0] ?? null)
419
+ // git can't read the move's snapshot: its commits are gone.
420
+ .catch(() => null);
421
+ choices.set(input.id, {
422
+ move: m,
423
+ here: false,
424
+ input: traced,
425
+ inComparison,
426
+ });
427
+ }));
428
+ return placeWithMoves(inputs, choices, lineCount, (input) => placeThread(input, lineCount));
334
429
  }
335
430
  }
336
431
  //# sourceMappingURL=threads.js.map
@@ -1,6 +1,7 @@
1
1
  import { monotonicFactory } from "ulid";
2
2
  import { MAX_ACTIVITY_ENTRIES, MAX_AGENT_ERROR, MAX_AGENT_TEXT, } from "../../shared/agent.js";
3
3
  import { parseRanges, size } from "../../shared/lineRanges.js";
4
+ import { MAX_GITHUB_THREAD_ID, MAX_MOVE_PATH, } from "../../shared/rereview/moves.js";
4
5
  import { oneLine } from "../agent/prompt.js";
5
6
  const SLUG = /^[a-z0-9-]{1,64}$/;
6
7
  /** A GitHub login. */
@@ -102,6 +103,10 @@ export function validateForWrite(e) {
102
103
  if (e.type === "thread-placed" && e.by === "auto") {
103
104
  throw new Error("thread-placed events must be written by a human or ai");
104
105
  }
106
+ if ((e.type === "thread-moved" || e.type === "thread-move-undone") &&
107
+ e.by !== "human") {
108
+ throw new Error(`${e.type} events must be written by a human`);
109
+ }
105
110
  if (e.type === "agent-thread-resolved" && e.by !== "human") {
106
111
  throw new Error("agent-thread-resolved events must be written by a human");
107
112
  }
@@ -379,6 +384,10 @@ function parseEvent(raw) {
379
384
  engine,
380
385
  };
381
386
  }
387
+ case "thread-moved":
388
+ case "thread-move-undone":
389
+ // A move is the reviewer's own view: never an AI's, never automatic.
390
+ return base.by === "human" ? parseMoveEvent(raw, base) : null;
382
391
  case "cluster-state": {
383
392
  // Fail safe: only a human can change a cluster's state.
384
393
  if (base.by !== "human")
@@ -482,6 +491,66 @@ function parseEvent(raw) {
482
491
  return null;
483
492
  }
484
493
  }
494
+ /** A move's thread: a known source and its id; null for anything else (an unknown source is skipped). */
495
+ function parseThreadRef(raw) {
496
+ if (!isObj(raw))
497
+ return null;
498
+ const { source, id } = raw;
499
+ if (typeof id !== "string")
500
+ return null;
501
+ if (source === "agent")
502
+ return LOWER_ULID.test(id) ? { source, id } : null;
503
+ if (source === "github") {
504
+ return id !== "" && id.length <= MAX_GITHUB_THREAD_ID
505
+ ? { source, id }
506
+ : null;
507
+ }
508
+ return null;
509
+ }
510
+ /** A move's lines: as `thread-placed`'s, plus a capped path and start not after end on one side. */
511
+ function parseMoveAnchor(raw) {
512
+ if (!isObj(raw))
513
+ return null;
514
+ const { path, startSide, startLine, side, line } = raw;
515
+ if (!isStr(path) ||
516
+ Buffer.byteLength(path) > MAX_MOVE_PATH ||
517
+ !isSide(startSide) ||
518
+ !isSide(side) ||
519
+ !isPosInt(startLine) ||
520
+ !isPosInt(line) ||
521
+ (startSide === side && startLine > line)) {
522
+ return null;
523
+ }
524
+ return { path, startSide, startLine, side, line };
525
+ }
526
+ function parseMoveEvent(raw, base) {
527
+ const thread = parseThreadRef(raw.thread);
528
+ if (!thread)
529
+ return null;
530
+ if (raw.type === "thread-move-undone") {
531
+ const { move } = raw;
532
+ if (typeof move !== "string" || !LOWER_ULID.test(move))
533
+ return null;
534
+ return { ...base, type: "thread-move-undone", thread, move };
535
+ }
536
+ const { on, engine } = raw;
537
+ const anchor = parseMoveAnchor(raw.anchor);
538
+ if (!anchor || !isPosInt(engine) || !isObj(on))
539
+ return null;
540
+ const { base: b, head, patchset } = on;
541
+ if (!isCommit(b) || !isCommit(head))
542
+ return null;
543
+ if (patchset !== undefined && !isPosInt(patchset))
544
+ return null;
545
+ return {
546
+ ...base,
547
+ type: "thread-moved",
548
+ thread,
549
+ on: { base: b, head, ...(patchset !== undefined ? { patchset } : {}) },
550
+ anchor,
551
+ engine,
552
+ };
553
+ }
485
554
  function parseInboxEvent(raw, base) {
486
555
  const { pr } = raw;
487
556
  if (!isPosInt(pr))
@@ -33,7 +33,7 @@ export function foldProgress(events) {
33
33
  const patchsets = [];
34
34
  const byNumber = new Map();
35
35
  const statesByRevision = new Map();
36
- const placements = new Map();
36
+ const placed = [];
37
37
  const sinceMarks = new Map();
38
38
  // Patch sets first, whatever their events' timestamps: a state stamped
39
39
  // before its patch set's start (a clock oddity) still belongs to it.
@@ -283,14 +283,8 @@ export function foldProgress(events) {
283
283
  break;
284
284
  }
285
285
  case "thread-placed": {
286
- if (e.by === "auto")
287
- break;
288
- let threads = placements.get(e.patchset);
289
- if (!threads) {
290
- threads = new Map();
291
- placements.set(e.patchset, threads);
292
- }
293
- threads.set(e.thread, e);
286
+ if (e.by === "human")
287
+ placed.push(e);
294
288
  break;
295
289
  }
296
290
  default:
@@ -304,7 +298,7 @@ export function foldProgress(events) {
304
298
  states: (patchset && statesByRevision.get(patchset.revision)) ?? new Map(),
305
299
  statesByRevision,
306
300
  sinceMarks,
307
- placements,
301
+ placed,
308
302
  };
309
303
  }
310
304
  function compare(a, b) {
@@ -441,25 +441,6 @@ export class ReviewService {
441
441
  }
442
442
  }
443
443
  }
444
- /**
445
- * Appends the reviewer's placement of a thread in patch set `patchset`
446
- * (a thread-placed event by them; the anchor was checked against the
447
- * patch set's snapshot by the caller).
448
- */
449
- async recordPlacement(reviewId, slug, patchset, thread, anchor, engine) {
450
- checkReviewId(reviewId);
451
- checkSlug(slug);
452
- checkPatchsetNumber(patchset);
453
- const ref = progressRef(reviewId, slug);
454
- const existing = await this.store.readEvents(ref);
455
- const progress = foldProgress(existing);
456
- if (!progress.patchsets.some((p) => p.number === patchset)) {
457
- throw new StaleSnapshotError(`No patch set ${patchset}; reload the review`);
458
- }
459
- const event = newEvent("thread-placed", slug, { thread, patchset, anchor, engine }, "human", stamper(existing)());
460
- await this.store.appendEvent(ref, `crb: placed thread in ${anchor.path} on PS${patchset}`, event, slug);
461
- return this.getProgress(reviewId, slug);
462
- }
463
444
  /**
464
445
  * Writes patch set n+1 on the snapshot: the start and the carries in one
465
446
  * commit on the progress ref's tip, and the ref moves with the two pin