The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 03:15 UTC
fix(gitlab): threads on changed code show as outdated (#30423)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com>
Antony committed
Nov 6, 2026 at 02:02 UTC
6593f78f4437dcde2757077beb562ddf83340e5e
6 files changed
+193
-47
packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
+7
-1
@@ -1260,6 +1260,9 @@ layer("GitLabPullRequestCli.layer", (it) => {
1260
old_path: "src/a.ts",
1261
new_line: 12,
1262
old_line: null,
1263
+ base_sha: "base",
1264
+ start_sha: "start",
1265
+ head_sha: "head",
1266
},
1267
},
1268
{
@@ -1342,7 +1345,7 @@ layer("GitLabPullRequestCli.layer", (it) => {
1345
);
1346
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1347
1345
- const { threads, overviewFindingIds } = yield* cli.listDiscussions({
1348
+ const { threads, diffRefsByThreadId, overviewFindingIds } = yield* cli.listDiscussions({
1349
cwd: "/w",
1350
repository: "acme/web",
1351
number: 7,
@@ -1354,6 +1357,9 @@ layer("GitLabPullRequestCli.layer", (it) => {
1357
{ id: "file789", path: "src/b.ts", line: null, side: "right" },
1358
{ id: "img012", path: "logo.png", line: null, side: "right" },
1359
]);
1360
+ expect([...diffRefsByThreadId]).toEqual([
1361
+ ["abc123", { baseSha: "base", startSha: "start", headSha: "head" }],
1362
+ ]);
1363
expect(threads[0]).toMatchObject({ isResolved: true });
1364
assert.strictEqual(threads[0]?.comments.length, 2);
1365
}),
packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
+36
-26
@@ -283,6 +283,13 @@ const DIFF_TIMEOUT_MS = 60_000;
283
const isVcsProcessExitError = Schema.is(VcsProcessExitError);
284
const DIFF_FILE_MAX_OUTPUT_BYTES = 1024 * 1024;
285
286
+export interface GitLabDiscussionThreads {
287
+ readonly threads: ReadonlyArray<PullRequestReviewThread>;
288
+ readonly diffRefsByThreadId: ReadonlyMap<string, GitLabDiffRefs>;
289
+ readonly overviewFindingIds: ReadonlyArray<string>;
290
+ readonly truncated: boolean;
291
+}
292
+
293
export interface GitLabMergeRequestListBatch {
294
readonly items: ReadonlyArray<GitLabMergeRequestListItem>;
295
readonly truncated: boolean;
@@ -523,14 +530,7 @@ export class GitLabPullRequestCli extends Context.Service<
530
readonly host?: string | undefined;
531
readonly repository: string;
532
readonly number: number;
526
- }) => Effect.Effect<
527
- {
528
- readonly threads: ReadonlyArray<PullRequestReviewThread>;
529
- readonly overviewFindingIds: ReadonlyArray<string>;
530
- readonly truncated: boolean;
531
- },
532
- GitLabPullRequestCliError
533
- >;
533
+ }) => Effect.Effect<GitLabDiscussionThreads, GitLabPullRequestCliError>;
534
535
readonly submitReview: (input: {
536
readonly cwd: string;
@@ -1073,15 +1073,9 @@ export const make = Effect.gen(function* () {
1073
readonly number: number;
1074
readonly page: number;
1075
readonly collected: ReadonlyArray<PullRequestReviewThread>;
1076
+ readonly collectedDiffRefs: ReadonlyMap<string, GitLabDiffRefs>;
1077
readonly collectedFindingIds: ReadonlyArray<string>;
1077
- }): Effect.Effect<
1078
- {
1079
- readonly threads: ReadonlyArray<PullRequestReviewThread>;
1080
- readonly overviewFindingIds: ReadonlyArray<string>;
1081
- readonly truncated: boolean;
1082
- },
1083
- GitLabPullRequestCliError
1084
- > =>
1078
+ }): Effect.Effect<GitLabDiscussionThreads, GitLabPullRequestCliError> =>
1079
api({
1080
cwd: input.cwd,
1081
host: input.host,
@@ -1105,21 +1099,31 @@ export const make = Effect.gen(function* () {
1099
);
1100
}
1101
const collected = [...input.collected, ...decoded.success.threads];
1102
+ const collectedDiffRefs = new Map([
1103
+ ...input.collectedDiffRefs,
1104
+ ...decoded.success.diffRefsByThreadId,
1105
+ ]);
1106
const collectedFindingIds = [
1107
...input.collectedFindingIds,
1108
...decoded.success.overviewFindingIds,
1109
];
1112
- const done = (truncated: boolean) =>
1113
- Effect.succeed({
1114
- threads: collected,
1115
- overviewFindingIds: collectedFindingIds,
1116
- truncated,
1117
- });
1110
+ const read = (truncated: boolean) => ({
1111
+ threads: collected,
1112
+ diffRefsByThreadId: collectedDiffRefs,
1113
+ overviewFindingIds: collectedFindingIds,
1114
+ truncated,
1115
+ });
1116
// Plain notes count toward the page size even when no positioned threads survive decoding.
1119
- if (decoded.success.rawCount < MAX_PAGE_SIZE) return done(false);
1117
+ if (decoded.success.rawCount < MAX_PAGE_SIZE) return Effect.succeed(read(false));
1118
return input.page >= CONVERSATION_PAGES
1121
- ? done(true)
1122
- : discussionsPage({ ...input, page: input.page + 1, collected, collectedFindingIds });
1119
+ ? Effect.succeed(read(true))
1120
+ : discussionsPage({
1121
+ ...input,
1122
+ page: input.page + 1,
1123
+ collected,
1124
+ collectedDiffRefs,
1125
+ collectedFindingIds,
1126
+ });
1127
}),
1128
);
1129
@@ -2295,7 +2299,13 @@ export const make = Effect.gen(function* () {
2299
}),
2300
2301
listDiscussions: Effect.fn("GitLabPullRequestCli.listDiscussions")(function (input) {
2298
- return discussionsPage({ ...input, page: 1, collected: [], collectedFindingIds: [] });
2302
+ return discussionsPage({
2303
+ ...input,
2304
+ page: 1,
2305
+ collected: [],
2306
+ collectedDiffRefs: new Map(),
2307
+ collectedFindingIds: [],
2308
+ });
2309
}),
2310
2311
submitReview: Effect.fn("GitLabPullRequestCli.submitReview")(function* (input) {
packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts
+64
-2
@@ -503,6 +503,7 @@ describe("optional activity reads", () => {
503
committedDate: "2026-07-01T00:00:00Z",
504
};
505
const webUrl = "https://git.acme.dev/acme/web/-/merge_requests/7";
506
+ const currentRefs = { baseSha: "base2", startSha: "start2", headSha: "head2" };
507
const reads = {
508
getMergeRequestDetail: () =>
509
Effect.succeed({
@@ -529,11 +530,17 @@ describe("optional activity reads", () => {
530
checks: [],
531
viewerCanMerge: true,
532
reviewerIds: [],
533
+ diffRefs: currentRefs,
534
}),
535
listNotes: () => Effect.succeed({ comments: [comment], truncated: false }),
536
listCommits: () => Effect.succeed([commit]),
537
listDiscussions: () =>
536
- Effect.succeed({ threads: [], overviewFindingIds: [], truncated: false }),
538
+ Effect.succeed({
539
+ threads: [],
540
+ diffRefsByThreadId: new Map(),
541
+ overviewFindingIds: [],
542
+ truncated: false,
543
+ }),
544
listReactions: () =>
545
Effect.succeed({
546
reactions: [],
@@ -598,7 +605,12 @@ describe("optional activity reads", () => {
605
listNotes: () =>
606
Effect.succeed({ comments: [comment, finding, reply], truncated: false }),
607
listDiscussions: () =>
601
- Effect.succeed({ threads: [], overviewFindingIds: ["10"], truncated: false }),
608
+ Effect.succeed({
609
+ threads: [],
610
+ diffRefsByThreadId: new Map(),
611
+ overviewFindingIds: ["10"],
612
+ truncated: false,
613
+ }),
614
}),
615
),
616
);
@@ -633,6 +645,7 @@ describe("optional activity reads", () => {
645
comments: [comment],
646
},
647
],
648
+ diffRefsByThreadId: new Map(),
649
overviewFindingIds: [],
650
truncated: false,
651
}),
@@ -673,6 +686,7 @@ describe("optional activity reads", () => {
686
comments: [comment],
687
},
688
],
689
+ diffRefsByThreadId: new Map(),
690
overviewFindingIds: [],
691
truncated: false,
692
}),
@@ -687,6 +701,53 @@ describe("optional activity reads", () => {
701
}),
702
);
703
704
+ it.effect("marks a thread outdated once the merge request's diff moves past it", () =>
705
+ Effect.gen(function* () {
706
+ let detailReads = 0;
707
+ const thread = (id: string) => ({
708
+ id,
709
+ path: "src/a.ts",
710
+ line: 3,
711
+ side: "right" as const,
712
+ isResolved: false,
713
+ isOutdated: false,
714
+ comments: [],
715
+ });
716
+ const provider = yield* make.pipe(
717
+ Effect.provide(
718
+ Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
719
+ ...reads,
720
+ getMergeRequestDetail: () =>
721
+ Effect.map(reads.getMergeRequestDetail(), (detail) => ({
722
+ ...detail,
723
+ diffRefs: ++detailReads === 1 ? { ...currentRefs, headSha: "head1" } : currentRefs,
724
+ })),
725
+ listDiscussions: () =>
726
+ Effect.succeed({
727
+ threads: [thread("current"), thread("moved"), thread("unplaced")],
728
+ diffRefsByThreadId: new Map([
729
+ ["current", currentRefs],
730
+ ["moved", { ...currentRefs, headSha: "head1" }],
731
+ ]),
732
+ overviewFindingIds: [],
733
+ truncated: false,
734
+ }),
735
+ }),
736
+ ),
737
+ );
738
+
739
+ const checks = provider.getChangeRequestChecks;
740
+ if (!checks) throw new Error("missing getChangeRequestChecks");
741
+ yield* checks(reference);
742
+ const { reviewThreads } = yield* provider.getChangeRequestActivity(reference);
743
+ expect(reviewThreads.map(({ id, isOutdated }) => [id, isOutdated])).toEqual([
744
+ ["current", false],
745
+ ["moved", true],
746
+ ["unplaced", false],
747
+ ]);
748
+ }),
749
+ );
750
+
751
it.effect("keeps comments and commits when optional discussions, reactions and links fail", () =>
752
Effect.gen(function* () {
753
const provider = yield* make.pipe(
@@ -750,6 +811,7 @@ describe("optional activity reads", () => {
811
],
812
},
813
],
814
+ diffRefsByThreadId: new Map(),
815
overviewFindingIds: [],
816
truncated: false,
817
}),
packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts
+26
-5
@@ -12,7 +12,7 @@ import type {
12
} from "@t3tools/contracts";
13
14
import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
15
-import type { GitLabMergeRequestListState } from "./gitLabMergeRequestJson.ts";
15
+import type { GitLabDiffRefs, GitLabMergeRequestListState } from "./gitLabMergeRequestJson.ts";
16
import {
17
PullRequestProviderError,
18
type PullRequestProviderFailure,
@@ -107,6 +107,17 @@ function gitLabProviderFailure(
107
return { reason: "failed" };
108
}
109
110
+// GitLab keeps a thread on the latest diff while its line survives, so older refs mean the code moved.
111
+const isOutdated = (
112
+ thread: GitLabDiffRefs | undefined,
113
+ current: GitLabDiffRefs | undefined,
114
+): boolean =>
115
+ thread !== undefined &&
116
+ current !== undefined &&
117
+ (thread.baseSha !== current.baseSha ||
118
+ thread.startSha !== current.startSha ||
119
+ thread.headSha !== current.headSha);
120
+
121
export const make = Effect.gen(function* () {
122
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
123
@@ -364,6 +375,7 @@ export const make = Effect.gen(function* () {
375
recoverRead(cli.listCommits(input), []),
376
recoverRead(cli.listDiscussions(input), {
377
threads: [],
378
+ diffRefsByThreadId: new Map(),
379
overviewFindingIds: [],
380
truncated: true,
381
}),
@@ -373,9 +385,14 @@ export const make = Effect.gen(function* () {
385
editedAtByNoteId: new Map<string, string>(),
386
botLogins: new Set<string>(),
387
}),
376
- // Usually cached by the detail read beside this one; web_url holds the real web origin.
388
+ // Fresh, not the cached detail: refs from before a push would mark every thread outdated.
389
recoverRead(
378
- mergeRequestDetail(input).pipe(Effect.map((mergeRequest) => mergeRequest.url)),
390
+ cli.getMergeRequestDetail({ ...input, downstreamChecks: false }).pipe(
391
+ Effect.map((mergeRequest) => ({
392
+ url: mergeRequest.url,
393
+ diffRefs: mergeRequest.diffRefs,
394
+ })),
395
+ ),
396
null,
397
),
398
],
@@ -383,8 +400,8 @@ export const make = Effect.gen(function* () {
400
).pipe(
401
Effect.mapError(fail("getChangeRequestActivity")),
402
Effect.map(
386
- ([notes, commits, discussions, awards, webUrl]): ProviderChangeRequestActivity => {
387
- const noteUrl = (id: string) => (webUrl === null ? null : `${webUrl}#note_${id}`);
403
+ ([notes, commits, discussions, awards, detail]): ProviderChangeRequestActivity => {
404
+ const noteUrl = (id: string) => (detail === null ? null : `${detail.url}#note_${id}`);
405
const findingIds = new Set(discussions.overviewFindingIds);
406
const edited = (id: string) => {
407
const editedAt = awards.editedAtByNoteId.get(id);
@@ -409,6 +426,10 @@ export const make = Effect.gen(function* () {
426
commentsTruncated: notes.truncated || discussions.truncated,
427
reviewThreads: discussions.threads.map((thread) => ({
428
...thread,
429
+ isOutdated: isOutdated(
430
+ discussions.diffRefsByThreadId.get(thread.id),
431
+ detail?.diffRefs,
432
+ ),
433
comments: thread.comments.map((comment) => ({
434
...comment,
435
...edited(comment.id),
packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts
+15
@@ -181,6 +181,21 @@ describe("decodeMergeRequestDetailJson", () => {
181
expect(headSha({ sha: " " })).toBeUndefined();
182
});
183
184
+ it("reads the diff refs threads are placed against", () => {
185
+ const diffRefs = (entry: Record<string, unknown>) =>
186
+ expectSuccess(decodeMergeRequestDetailJson(detailJson(entry))).diffRefs;
187
+
188
+ expect(diffRefs({ diff_refs: { base_sha: "b", start_sha: "s", head_sha: "h" } })).toEqual({
189
+ baseSha: "b",
190
+ startSha: "s",
191
+ headSha: "h",
192
+ });
193
+ expect(diffRefs({ diff_refs: null })).toBeUndefined();
194
+ expect(
195
+ diffRefs({ diff_refs: { base_sha: null, start_sha: "s", head_sha: "h" } }),
196
+ ).toBeUndefined();
197
+ });
198
+
199
it("reads either auto-merge field, and says nothing where GitLab named neither", () => {
200
const armed = (entry: Record<string, unknown>) =>
201
expectSuccess(decodeMergeRequestDetailJson(detailJson(entry))).autoMergeEnabled;
packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts
+45
-13
@@ -47,6 +47,18 @@ const RawTriggerJobSchema = Schema.Struct({
47
downstream_pipeline: Schema.optional(Schema.NullOr(RawPipelineSchema)),
48
});
49
50
+const RawDiffRefs = Schema.Struct({
51
+ base_sha: Schema.String,
52
+ head_sha: Schema.String,
53
+ start_sha: Schema.String,
54
+});
55
+
56
+const RawMergeRequestDiffRefs = Schema.Struct({
57
+ base_sha: Schema.NullOr(Schema.String),
58
+ head_sha: Schema.NullOr(Schema.String),
59
+ start_sha: Schema.NullOr(Schema.String),
60
+});
61
+
62
const RawMergeRequestSchema = Schema.Struct({
63
iid: Schema.Int,
64
title: Schema.String,
@@ -79,6 +91,7 @@ const RawMergeRequestSchema = Schema.Struct({
91
squash: Schema.optional(Schema.NullOr(Schema.Boolean)),
92
/** Only detail reads with include_diverged_commits_count return this expensive count. */
93
diverged_commits_count: Schema.optional(Schema.NullOr(Schema.Int)),
94
+ diff_refs: Schema.optional(Schema.NullOr(RawMergeRequestDiffRefs)),
95
});
96
97
const RawNoteSchema = Schema.Struct({
@@ -114,6 +127,9 @@ const RawDiscussionNoteSchema = Schema.Struct({
127
old_path: Schema.optional(Schema.NullOr(Schema.String)),
128
new_line: Schema.optional(Schema.NullOr(Schema.Int)),
129
old_line: Schema.optional(Schema.NullOr(Schema.Int)),
130
+ base_sha: Schema.optional(Schema.NullOr(Schema.String)),
131
+ start_sha: Schema.optional(Schema.NullOr(Schema.String)),
132
+ head_sha: Schema.optional(Schema.NullOr(Schema.String)),
133
}),
134
),
135
),
@@ -125,15 +141,7 @@ const RawDiscussionSchema = Schema.Struct({
141
});
142
143
const RawDiffRefsSchema = Schema.Struct({
128
- diff_refs: Schema.optional(
129
- Schema.NullOr(
130
- Schema.Struct({
131
- base_sha: Schema.String,
132
- head_sha: Schema.String,
133
- start_sha: Schema.String,
134
- }),
135
- ),
136
- ),
144
+ diff_refs: Schema.optional(Schema.NullOr(RawDiffRefs)),
145
});
146
147
const RawCommitSchema = Schema.Struct({
@@ -212,6 +220,7 @@ export interface GitLabMergeRequestDetail extends GitLabMergeRequestListItem {
220
/** Absent means not counted, not up to date. */
221
readonly divergedCommits?: number;
222
readonly headSha?: string;
223
+ readonly diffRefs?: GitLabDiffRefs;
224
/** On a fork the pipeline often belongs to the source project, not the target. */
225
readonly headPipeline?: { readonly id: number; readonly projectId: number };
226
}
@@ -324,6 +333,11 @@ function toListItem(
333
function toDetail(raw: Schema.Schema.Type<typeof RawMergeRequestSchema>): GitLabMergeRequestDetail {
334
const listItem = toListItem(raw);
335
const headSha = trimmed(raw.sha);
336
+ const refs = raw.diff_refs;
337
+ const diffRefs =
338
+ refs && refs.base_sha !== null && refs.head_sha !== null && refs.start_sha !== null
339
+ ? toDiffRefs({ base_sha: refs.base_sha, head_sha: refs.head_sha, start_sha: refs.start_sha })
340
+ : null;
341
const autoMerge =
342
raw.merge_when_pipeline_succeeds == null && raw.auto_merge_enabled == null
343
? undefined
@@ -349,6 +363,7 @@ function toDetail(raw: Schema.Schema.Type<typeof RawMergeRequestSchema>): GitLab
363
: {}),
364
...(raw.diverged_commits_count == null ? {} : { divergedCommits: raw.diverged_commits_count }),
365
...(headSha === null ? {} : { headSha }),
366
+ ...(diffRefs === null ? {} : { diffRefs }),
367
...(raw.head_pipeline?.id == null || raw.head_pipeline.project_id == null
368
? {}
369
: { headPipeline: { id: raw.head_pipeline.id, projectId: raw.head_pipeline.project_id } }),
@@ -556,11 +571,17 @@ export interface GitLabDiffRefs {
571
572
export interface GitLabDiscussions {
573
readonly threads: ReadonlyArray<PullRequestReviewThread>;
574
+ /** GitLab moves a thread onto each new diff that still has its line; this is the last one. */
575
+ readonly diffRefsByThreadId: ReadonlyMap<string, GitLabDiffRefs>;
576
/** First notes of unresolved discussions on the MR itself rather than a diff line. */
577
readonly overviewFindingIds: ReadonlyArray<string>;
578
readonly rawCount: number;
579
}
580
581
+function toDiffRefs(raw: Schema.Schema.Type<typeof RawDiffRefs>): GitLabDiffRefs {
582
+ return { baseSha: raw.base_sha, headSha: raw.head_sha, startSha: raw.start_sha };
583
+}
584
+
585
export function decodeDiscussionsJson(
586
raw: string,
587
): Result.Result<GitLabDiscussions, DecodeFailure> {
@@ -569,6 +590,7 @@ export function decodeDiscussionsJson(
590
return Result.fail(decoded.failure);
591
}
592
const threads: PullRequestReviewThread[] = [];
593
+ const diffRefsByThreadId = new Map<string, GitLabDiffRefs>();
594
const overviewFindingIds: string[] = [];
595
for (const entry of decoded.success) {
596
const discussion = decodeDiscussionEntry(entry);
@@ -595,6 +617,13 @@ export function decodeDiscussionsJson(
617
continue;
618
}
619
if (path === null) continue;
620
+ if (position.base_sha && position.start_sha && position.head_sha) {
621
+ diffRefsByThreadId.set(discussion.value.id, {
622
+ baseSha: position.base_sha,
623
+ startSha: position.start_sha,
624
+ headSha: position.head_sha,
625
+ });
626
+ }
627
threads.push({
628
id: discussion.value.id,
629
path,
@@ -611,7 +640,12 @@ export function decodeDiscussionsJson(
640
})),
641
});
642
}
614
- return Result.succeed({ threads, overviewFindingIds, rawCount: decoded.success.length });
643
+ return Result.succeed({
644
+ threads,
645
+ diffRefsByThreadId,
646
+ overviewFindingIds,
647
+ rawCount: decoded.success.length,
648
+ });
649
}
650
651
export function decodeDiffRefsJson(
@@ -620,9 +654,7 @@ export function decodeDiffRefsJson(
654
const decoded = decodeDiffRefs(raw);
655
if (!Result.isSuccess(decoded)) return Result.fail(decoded.failure);
656
const refs = decoded.success.diff_refs;
623
- return Result.succeed(
624
- refs ? { baseSha: refs.base_sha, headSha: refs.head_sha, startSha: refs.start_sha } : null,
625
- );
657
+ return Result.succeed(refs ? toDiffRefs(refs) : null);
658
}
659
660
export function decodeNotesJson(