Skip to content

Commit a221ad2

Browse files
authored
Drop buffered inline comment when it is posted live (#1405) (#1412)
When classify_inline_comments is enabled, create_inline_comment buffers calls without confirmed=true. The model frequently re-issues the call with confirmed=true after reading the buffered reply, which posts the comment live but leaves the original buffered entry behind. The post-session replay step then posts it again, so every inline comment lands twice. Reconcile the buffer on a live post: after a confirmed comment is created, remove any buffered entry matching the same path, line, startLine and body so it cannot be replayed. Extracts the reconciliation into src/mcp/inline-comment-buffer.ts (the MCP server module starts a server on import) and adds unit tests. Co-authored-by: archievi <[email protected]>
1 parent 558b1d6 commit a221ad2

3 files changed

Lines changed: 204 additions & 0 deletions

File tree

‎src/mcp/github-inline-comment-server.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { appendFileSync } from "fs";
55
import { z } from "zod";
66
import { createOctokit } from "../github/api/client";
77
import { sanitizeContent } from "../github/utils/sanitizer";
8+
import { removeBufferedComment } from "./inline-comment-buffer";
89

910
// Get repository and PR information from environment variables
1011
const REPO_OWNER = process.env.REPO_OWNER;
@@ -180,6 +181,16 @@ server.tool(
180181

181182
const result = await octokit.rest.pulls.createReviewComment(params);
182183

184+
// The comment is now live. Drop any buffered copy of it so the
185+
// post-session replay step cannot post it a second time (the model often
186+
// re-issues a buffered call with confirmed=true after the buffer reply).
187+
if (CLASSIFY_ENABLED) {
188+
removeBufferedComment(
189+
{ path, line, startLine, body: sanitizedBody },
190+
BUFFER_PATH,
191+
);
192+
}
193+
183194
return {
184195
content: [
185196
{

‎src/mcp/inline-comment-buffer.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { existsSync, readFileSync, writeFileSync } from "fs";
2+
3+
export type BufferedCommentMatch = {
4+
path: string;
5+
line?: number;
6+
startLine?: number;
7+
body: string;
8+
};
9+
10+
/**
11+
* Remove any buffered inline comment that matches an already-posted comment.
12+
*
13+
* When a comment is posted live (confirmed=true), an earlier buffered copy of
14+
* the same comment must be dropped so the post-session replay step does not
15+
* post it a second time. The model frequently re-issues a buffered call with
16+
* confirmed=true after reading the "Set confirmed=true to post immediately"
17+
* reply; previously the original buffered entry was left behind and replayed,
18+
* producing duplicate inline comments.
19+
*
20+
* Entries are matched on path, line, startLine and body. Lines that cannot be
21+
* parsed are kept untouched.
22+
*/
23+
export function removeBufferedComment(
24+
match: BufferedCommentMatch,
25+
bufferPath: string,
26+
): void {
27+
if (!existsSync(bufferPath)) {
28+
return;
29+
}
30+
31+
const remaining = readFileSync(bufferPath, "utf8")
32+
.split("\n")
33+
.filter((line) => line.trim() !== "")
34+
.filter((line) => {
35+
let entry: BufferedCommentMatch;
36+
try {
37+
entry = JSON.parse(line);
38+
} catch {
39+
// Keep anything we cannot parse rather than silently dropping it.
40+
return true;
41+
}
42+
const isSameComment =
43+
entry.path === match.path &&
44+
entry.line === match.line &&
45+
entry.startLine === match.startLine &&
46+
entry.body === match.body;
47+
return !isSameComment;
48+
});
49+
50+
writeFileSync(
51+
bufferPath,
52+
remaining.length > 0 ? remaining.join("\n") + "\n" : "",
53+
);
54+
}

‎test/inline-comment-buffer.test.ts‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
2+
import {
3+
existsSync,
4+
mkdtempSync,
5+
readFileSync,
6+
rmSync,
7+
writeFileSync,
8+
} from "fs";
9+
import { tmpdir } from "os";
10+
import { join } from "path";
11+
import { removeBufferedComment } from "../src/mcp/inline-comment-buffer";
12+
13+
describe("removeBufferedComment", () => {
14+
let dir: string;
15+
let bufferPath: string;
16+
17+
const entryA = {
18+
ts: "2026-06-13T00:00:00.000Z",
19+
path: "src/index.ts",
20+
line: 10,
21+
startLine: undefined,
22+
side: "RIGHT",
23+
body: "Comment A",
24+
};
25+
const entryB = {
26+
ts: "2026-06-13T00:00:01.000Z",
27+
path: "src/other.ts",
28+
line: 20,
29+
startLine: undefined,
30+
side: "RIGHT",
31+
body: "Comment B",
32+
};
33+
34+
const writeBuffer = (entries: object[]): void => {
35+
writeFileSync(
36+
bufferPath,
37+
entries.map((e) => JSON.stringify(e)).join("\n") + "\n",
38+
);
39+
};
40+
41+
const readBuffer = (): Array<{ body: string }> => {
42+
if (!existsSync(bufferPath)) {
43+
return [];
44+
}
45+
return readFileSync(bufferPath, "utf8")
46+
.split("\n")
47+
.filter((line) => line.trim() !== "")
48+
.map((line) => JSON.parse(line));
49+
};
50+
51+
beforeEach(() => {
52+
dir = mkdtempSync(join(tmpdir(), "inline-buffer-"));
53+
bufferPath = join(dir, "buffer.jsonl");
54+
});
55+
56+
afterEach(() => {
57+
rmSync(dir, { recursive: true, force: true });
58+
});
59+
60+
it("removes the matching buffered entry and keeps the others", () => {
61+
writeBuffer([entryA, entryB]);
62+
63+
removeBufferedComment(
64+
{
65+
path: "src/index.ts",
66+
line: 10,
67+
startLine: undefined,
68+
body: "Comment A",
69+
},
70+
bufferPath,
71+
);
72+
73+
const remaining = readBuffer();
74+
expect(remaining.map((e) => e.body)).toEqual(["Comment B"]);
75+
});
76+
77+
it("removes every copy when the same comment was buffered more than once", () => {
78+
writeBuffer([entryA, entryA, entryB]);
79+
80+
removeBufferedComment(
81+
{
82+
path: "src/index.ts",
83+
line: 10,
84+
startLine: undefined,
85+
body: "Comment A",
86+
},
87+
bufferPath,
88+
);
89+
90+
expect(readBuffer().map((e) => e.body)).toEqual(["Comment B"]);
91+
});
92+
93+
it("leaves the buffer untouched when nothing matches", () => {
94+
writeBuffer([entryA, entryB]);
95+
96+
removeBufferedComment(
97+
{
98+
path: "src/index.ts",
99+
line: 999,
100+
startLine: undefined,
101+
body: "Comment A",
102+
},
103+
bufferPath,
104+
);
105+
106+
expect(readBuffer().map((e) => e.body)).toEqual(["Comment A", "Comment B"]);
107+
});
108+
109+
it("does nothing when the buffer file does not exist", () => {
110+
expect(() =>
111+
removeBufferedComment(
112+
{ path: "src/index.ts", line: 10, body: "Comment A" },
113+
bufferPath,
114+
),
115+
).not.toThrow();
116+
expect(existsSync(bufferPath)).toBe(false);
117+
});
118+
119+
it("keeps lines that cannot be parsed as JSON", () => {
120+
writeFileSync(
121+
bufferPath,
122+
["not json", JSON.stringify(entryA)].join("\n") + "\n",
123+
);
124+
125+
removeBufferedComment(
126+
{
127+
path: "src/index.ts",
128+
line: 10,
129+
startLine: undefined,
130+
body: "Comment A",
131+
},
132+
bufferPath,
133+
);
134+
135+
const raw = readFileSync(bufferPath, "utf8");
136+
expect(raw).toContain("not json");
137+
expect(raw).not.toContain("Comment A");
138+
});
139+
});

0 commit comments

Comments
 (0)