fix(mitm): strip trailing assistant prefill to prevent upstream Anthropic 400 errors (#7520)

* chore(ci): add .mergify.yml to main — Mergify only reads config from the default branch (#7168)

* fix(ci): add the auto-enqueue pull_request_rule to the Mergify config (queue_conditions alone are eligibility-only) (#7179)

* fix(ci): migrate Mergify auto-enqueue to merge_protections_settings.auto_merge_conditions (rules-based path is EOL 2026-07-16) (#7216)

* fix(ci): drop Mergify batch settings (batching is a paid-tier feature; free plan queue is serial) (#7220)

* fix(ci): merge queue tolerates the advisory dast-smoke failure (its GH-hosted build hang dequeued every attempt) (#7225)

* test(ci): make the #6634 selfref guard hermetic — main's copy hard-fails every PR (#7341)

main's copy of this test still does git I/O inside a unit test:

    const baseSrc = git(['show', 'origin/main:' + FILE]);

Runners check out a shallow single ref, so origin/main does not resolve and the
test dies with 'fatal: invalid object name origin/main'. Every PR into main
fails Unit Tests (7/8) on it — today that is #7313, #7315, #7316, #7334, #7336
and #7337, six PRs red on a defect none of them introduced. #7313 has no other
red at all.

release/v3.8.49 already carries a fix (2e42b8efc, #7174: try/catch, fetch
origin/main on demand, t.skip() when unreachable), but it only reaches main at
release time — so main stays broken for the whole cycle. Cherry-picking it would
also import a new problem: PR Test Policy classifies t.skip() as a silenced
assertion, which we watched it correctly catch on #7300 today.

This is the hermetic version instead (ported from #7327, which does the same for
the release branch): read the file straight off disk, compare against an empty
base so baseTaut/baseExtTaut are 0 — the strictest possible comparison point —
and call evaluateMasking() directly. No git ref, no fetch, no skip, nothing the
runner's checkout depth can break.

The #6634 regression stays covered: the guard's logic lives in
SELF_TEST_FIXTURE_RE (check-test-masking.mjs:337), not in the test. Proven both
ways on main before committing — neutralise SELF_TEST_FIXTURE_RE to /$^/ and
the test FAILS; restore it and it passes 2/2, with check-test-masking.mjs left
byte-identical.

Co-authored-by: growab <nekron@icloud.com>

* chore(quality): tighten main's coverage baseline to the CI's real numbers (#7347)

main's ratchet had been failing --require-tighten on every PR: 11 metrics
improved but the baseline was never tightened. Same class as the #6634
selfref guard — an infra fix that lands only on the release branch leaves
main red for the whole cycle, and every PR into main pays for it.

Values are the merged-coverage numbers from a run on main itself (a local
run measures ~68% vs CI's ~80%; the baseline's own note warns about that
gap). Only the 11 coverage values change — gitleaks and semgrepFindings
keep main's own state.

No changelog fragment: #7326 carries it on release/v3.8.49, and a second
one here would double the entry at release time.

* fix(mitm): strip trailing assistant prefill to prevent upstream Anthropic 400 errors

* fix(mitm): loop over all trailing assistant turns + never-empty guard

The trailing-assistant-prefill strip only popped a single message, so a
conversation ending in 2+ consecutive assistant turns still hit the
upstream "This model does not support assistant message prefill" 400.
It also had no floor, so a payload whose entire history was trailing
assistant turns collapsed to messages: [], trading one 400 for another
("messages: at least 1 item required").

Loop over all consecutive trailing assistant messages and never strip
below 1 remaining message, mirroring the same pop-loop bound already used
by dropTrailingAssistantPrefill (open-sse/executors/github.ts) and
stripTrailingAntigravityAssistantTurn (open-sse/executors/antigravity.ts).

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: Diego Rodrigues de Sa e Souza <8016841+diegosouzapw@users.noreply.github.com>
Co-authored-by: growab <nekron@icloud.com>
This commit is contained in:
Chirag Singhal
2026-07-19 05:49:38 +05:30
committed by GitHub
parent 4690cbd8e1
commit fac866f70b
2 changed files with 52 additions and 1 deletions

View File

@@ -17,7 +17,7 @@ export class ClaudeCodeHandler extends MitmHandlerBase {
req: IncomingMessage,
res: ServerResponse,
body: Buffer,
mappedModel: string,
mappedModel: string
): Promise<void> {
const startedAt = this.now();
const intercepted = await this.hookBufferStart(req, body, mappedModel);
@@ -26,6 +26,23 @@ export class ClaudeCodeHandler extends MitmHandlerBase {
const payload = JSON.parse(body.toString());
payload.model = mappedModel;
// Strip trailing assistant prefill to prevent "This model does not support assistant
// message prefill" upstream error. Loop over ALL consecutive trailing assistant turns
// (not just one) — mirrors the pop-loop already used for Copilot
// (open-sse/executors/github.ts::dropTrailingAssistantPrefill) and Antigravity/Vertex
// Claude (open-sse/executors/antigravity.ts::stripTrailingAntigravityAssistantTurn).
// Guard: never strip messages down to empty — an empty array is itself an invalid
// request, so at least one entry (even a lone trailing assistant turn) is always
// preserved.
if (Array.isArray(payload.messages) && payload.messages.length > 0) {
while (
payload.messages.length > 1 &&
payload.messages[payload.messages.length - 1]?.role === "assistant"
) {
payload.messages.pop();
}
}
const upstreamStart = this.now();
const upstream = await this.fetchRouter(payload, "/v1/messages", req.headers);

View File

@@ -15,3 +15,37 @@ test("claude-code handler — happy path forwards to /v1/messages", async () =>
const sent = JSON.parse(r.fetchBody);
assert.equal(sent.model, "claude-opus-4.5");
});
test("claude-code handler — strips ALL consecutive trailing assistant turns, not just one", async () => {
const r = await runHandler(
new ClaudeCodeHandler(),
{
model: "claude-3.5-sonnet",
messages: [
{ role: "user", content: "hi" },
{ role: "assistant", content: "partial reply 1" },
{ role: "assistant", content: "partial reply 2" },
],
},
"claude-opus-4.5"
);
assert.ok(r.fetchCalled);
const sent = JSON.parse(r.fetchBody);
assert.equal(sent.messages.length, 1);
assert.equal(sent.messages[0].role, "user");
});
test("claude-code handler — never collapses messages to empty when the ENTIRE history is trailing assistant turns", async () => {
const r = await runHandler(
new ClaudeCodeHandler(),
{
model: "claude-3.5-sonnet",
messages: [{ role: "assistant", content: "lone assistant turn" }],
},
"claude-opus-4.5"
);
assert.ok(r.fetchCalled);
const sent = JSON.parse(r.fetchBody);
assert.equal(sent.messages.length, 1);
assert.equal(sent.messages[0].content, "lone assistant turn");
});