diff --git a/src/plugin/request-helpers.test.ts b/src/plugin/request-helpers.test.ts index f48e20e..2d678ae 100644 --- a/src/plugin/request-helpers.test.ts +++ b/src/plugin/request-helpers.test.ts @@ -23,6 +23,7 @@ import { cleanJSONSchemaForAntigravity, createSyntheticErrorResponse, neutralizeBlockedSystemInstructionLiterals, + neutralizeSystemInstructionInPayload, recursivelyParseJsonStrings, sanitizeEndingModelTurn, } from "./request-helpers"; @@ -2099,3 +2100,40 @@ describe("neutralizeBlockedSystemInstructionLiterals", () => { expect(out.length).toBe(input.length + 1); }); }); + +describe("neutralizeSystemInstructionInPayload", () => { + const BLOCKED = "tools.opencode.session_move"; + const SAFE = "tools.opencode. session_move"; + + it("cleans the top-level systemInstruction string", () => { + const payload: Record = { systemInstruction: `call ${BLOCKED}` }; + neutralizeSystemInstructionInPayload(payload); + expect(payload.systemInstruction).toBe(`call ${SAFE}`); + }); + + it("cleans every part of a top-level systemInstruction object", () => { + const payload: Record = { + systemInstruction: { parts: [{ text: `a ${BLOCKED}` }, { text: `b ${BLOCKED}` }] }, + }; + neutralizeSystemInstructionInPayload(payload); + const parts = (payload.systemInstruction as { parts: Array<{ text: string }> }).parts; + expect(parts.map((p) => p.text)).toEqual([`a ${SAFE}`, `b ${SAFE}`]); + }); + + it("cleans a body that arrives already wrapped by the caller", () => { + const payload: Record = { + project: "managed-project", + request: { systemInstruction: { parts: [{ text: `wrapped ${BLOCKED}` }] } }, + }; + neutralizeSystemInstructionInPayload(payload); + const request = payload.request as { systemInstruction: { parts: Array<{ text: string }> } }; + expect(request.systemInstruction.parts[0]?.text).toBe(`wrapped ${SAFE}`); + }); + + it("leaves a payload without systemInstruction alone", () => { + const payload: Record = { contents: [{ role: "user", parts: [{ text: BLOCKED }] }] }; + const antes = JSON.stringify(payload); + neutralizeSystemInstructionInPayload(payload); + expect(JSON.stringify(payload)).toBe(antes); + }); +}); diff --git a/src/plugin/request-helpers.ts b/src/plugin/request-helpers.ts index 67ca04d..0ff17f0 100644 --- a/src/plugin/request-helpers.ts +++ b/src/plugin/request-helpers.ts @@ -1694,6 +1694,41 @@ export function neutralizeBlockedSystemInstructionLiterals(text: string): string return result; } +/** + * Applies the blocked-literal neutralization to whichever shape the payload uses. + * + * A request arrives either unwrapped (`systemInstruction` at the top level) or + * already wrapped by a caller (`{ project, request: { systemInstruction } }`). + * The unwrapped path neutralized on its own, so a wrapped request still carried + * the literal and still earned the 429; this walks both shapes before the split. + */ +export function neutralizeSystemInstructionInPayload(payload: Record): void { + const applyTo = (holder: Record): void => { + const value = holder.systemInstruction; + if (typeof value === "string") { + holder.systemInstruction = neutralizeBlockedSystemInstructionLiterals(value); + return; + } + if (value && typeof value === "object" && !Array.isArray(value)) { + const parts = (value as Record).parts; + if (Array.isArray(parts)) { + for (const part of parts) { + if (part && typeof part === "object" && typeof (part as Record).text === "string") { + const concreto = part as Record; + concreto.text = neutralizeBlockedSystemInstructionLiterals(concreto.text as string); + } + } + } + } + }; + + applyTo(payload); + const inner = payload.request; + if (inner && typeof inner === "object" && !Array.isArray(inner)) { + applyTo(inner as Record); + } +} + /** * Enhances 404 errors for Antigravity models with a direct preview-access message. */ diff --git a/src/plugin/request.ts b/src/plugin/request.ts index c61c0da..5085cfe 100644 --- a/src/plugin/request.ts +++ b/src/plugin/request.ts @@ -43,6 +43,7 @@ import { isThinkingCapableModel, normalizeThinkingConfig, neutralizeBlockedSystemInstructionLiterals, + neutralizeSystemInstructionInPayload, parseAntigravityApiBody, resolveThinkingConfig, rewriteAntigravityPreviewAccessError, @@ -985,6 +986,9 @@ export function prepareAntigravityRequest( const parsedBody = JSON.parse(baseInit.body) as Record; const isWrapped = typeof parsedBody.project === "string" && "request" in parsedBody; + // Both shapes carry the literal, and only the unwrapped path used to clean it. + neutralizeSystemInstructionInPayload(parsedBody); + // #37: apply the thinking variant (effort) BEFORE resolving the backend model // so the selected tier is honored on both wrapped and unwrapped bodies. const requestRootForVariant = isWrapped