Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/plugin/request-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
cleanJSONSchemaForAntigravity,
createSyntheticErrorResponse,
neutralizeBlockedSystemInstructionLiterals,
neutralizeSystemInstructionInPayload,
recursivelyParseJsonStrings,
sanitizeEndingModelTurn,
} from "./request-helpers";
Expand Down Expand Up @@ -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<string, unknown> = { systemInstruction: `call ${BLOCKED}` };
neutralizeSystemInstructionInPayload(payload);
expect(payload.systemInstruction).toBe(`call ${SAFE}`);
});

it("cleans every part of a top-level systemInstruction object", () => {
const payload: Record<string, unknown> = {
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<string, unknown> = {
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<string, unknown> = { contents: [{ role: "user", parts: [{ text: BLOCKED }] }] };
const antes = JSON.stringify(payload);
neutralizeSystemInstructionInPayload(payload);
expect(JSON.stringify(payload)).toBe(antes);
});
});
35 changes: 35 additions & 0 deletions src/plugin/request-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>): void {
const applyTo = (holder: Record<string, unknown>): 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<string, unknown>).parts;
if (Array.isArray(parts)) {
for (const part of parts) {
if (part && typeof part === "object" && typeof (part as Record<string, unknown>).text === "string") {
const concreto = part as Record<string, unknown>;
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<string, unknown>);
}
}

/**
* Enhances 404 errors for Antigravity models with a direct preview-access message.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/plugin/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
isThinkingCapableModel,
normalizeThinkingConfig,
neutralizeBlockedSystemInstructionLiterals,
neutralizeSystemInstructionInPayload,
parseAntigravityApiBody,
resolveThinkingConfig,
rewriteAntigravityPreviewAccessError,
Expand Down Expand Up @@ -985,6 +986,9 @@ export function prepareAntigravityRequest(
const parsedBody = JSON.parse(baseInit.body) as Record<string, unknown>;
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
Expand Down
Loading