Skip to content
Closed
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
2 changes: 1 addition & 1 deletion kernel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acp-kernel",
"version": "0.0.175",
"version": "0.0.176",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
32 changes: 25 additions & 7 deletions kernel/src/wire/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,16 @@ export function googleToCore(body: GoogleRequestBody): GoogleFlat {
return { msgs, systemText: googleSystemText(body) };
}

/** #5392 — Claude-family backends routed through Google's v1internal / Cloud
* Code Assist wire translate role:"user" parts into Anthropic content blocks
* IN ORDER, and Anthropic requires every tool_result block to LEAD the user
* message that follows an assistant tool_use turn — the OPPOSITE of the Gemini
* text-first rule below. Reordering such a body to text-first 400s it ("Each
* 'tool_use' block must have a corresponding 'tool_result' block in the next
* message"). Matched against the TARGET model name (case-insensitive), the same
* signal the host threads through every google-wire boundary. */
const CLAUDE_WIRE_MODEL = /claude/i;

/** #3159 — Vertex rejects a request whose user content carries plain text
* AFTER a functionResponse part ("Requests ending with a model turn are not
* supported"); text-before-functionResponse is accepted (controlled A/B on
Expand All @@ -317,8 +327,16 @@ export function googleToCore(body: GoogleRequestBody): GoogleFlat {
* text ahead of every non-text part; otherwise return the input untouched so
* already-safe shapes stay byte-identical (prefix-cache anchor). Signed text
* parts, thought parts and all non-text parts keep their relative order —
* nothing is dropped, renamed or rewritten. */
export function normalizeGoogleUserPartOrder(parts: GooglePart[]): GooglePart[] {
* nothing is dropped, renamed or rewritten.
* #5392 — when `opts.model` names a Claude-family target the input order is
* returned untouched: its natural responses-first shape is exactly what an
* Anthropic-behind-Google backend requires, so the text-first partition below
* applies only to non-Claude targets. */
export function normalizeGoogleUserPartOrder(
parts: GooglePart[],
opts?: { model?: string },
): GooglePart[] {
if (opts && typeof opts.model === "string" && CLAUDE_WIRE_MODEL.test(opts.model)) return parts;
const firstFr = parts.findIndex((p) => p.functionResponse !== undefined);
if (firstFr === -1) return parts;
const movable = (p: GooglePart) =>
Expand Down Expand Up @@ -359,7 +377,7 @@ export function normalizeGoogleUserPartOrder(parts: GooglePart[]): GooglePart[]
* roles), and its output order is the canonical one. Do not reorder this
* emission without moving the pins in
* kernel/tests/issue3939-wire-byte-stability.test.ts. */
export function coreToGoogle(messages: BiliMessage[]): GoogleContent[] {
export function coreToGoogle(messages: BiliMessage[], opts?: { model?: string }): GoogleContent[] {
const out: GoogleContent[] = [];
let model: {
reasoning: GooglePart[];
Expand All @@ -375,10 +393,10 @@ export function coreToGoogle(messages: BiliMessage[]): GoogleContent[] {
};
const flushUser = () => {
if (!user) return;
const parts = normalizeGoogleUserPartOrder([
...user.responses,
...user.content,
]);
const parts = normalizeGoogleUserPartOrder(
[...user.responses, ...user.content],
{ model: opts?.model },
);
user = null;
if (parts.length > 0) out.push({ role: "user", parts });
};
Expand Down
40 changes: 40 additions & 0 deletions kernel/tests/wire-google.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,43 @@ test("an already-safe user part order round-trips byte-identical through coreToG
};
assert.deepEqual(coreToGoogle(googleToCore(body).msgs), body.contents);
});

test("normalizeGoogleUserPartOrder: Claude-family target keeps natural responses-first order (#5392)", () => {
const fr: GooglePart = {
functionResponse: { name: "add", response: { result: 66 }, id: "c1" },
};
const parts: GooglePart[] = [fr, { text: "nudge" }];
assert.deepEqual(normalizeGoogleUserPartOrder(parts), [
{ text: "nudge" },
fr,
]);
// Anthropic-behind-Google needs tool_result(functionResponse) to LEAD the user
// message — the input order is returned byte-for-byte (same reference).
for (const m of ["claude-opus-4-8", "Claude-Sonnet-4-6"]) {
assert.equal(normalizeGoogleUserPartOrder(parts, { model: m }), parts);
}
});

test("coreToGoogle: Claude-family target emits functionResponse before text (#5392)", () => {
const body: GoogleRequestBody = {
contents: [
{ role: "user", parts: [{ text: "go" }] },
{
role: "model",
parts: [{ functionCall: { name: "add", args: { a: 37, b: 29 }, id: "c1" } }],
},
{
role: "user",
parts: [
{ functionResponse: { name: "add", response: { result: 66 }, id: "c1" } },
{ text: "nudge" },
],
},
],
};
const contents = coreToGoogle(googleToCore(body).msgs, { model: "claude-opus-4-8" });
assert.deepEqual(contents[2]!.parts, [
{ functionResponse: { name: "add", response: { result: 66 }, id: "c1" } },
{ text: "nudge" },
]);
});
10 changes: 5 additions & 5 deletions src/chain-checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ const ZERO_DIGEST = "sha256:" + "0".repeat(64);
* see module header). Restamping an already-stamped body REPLACES the
* trailing carrier instead of stacking. Returns the new body or null when
* the shape admits no carrier slot. */
export function insertCheckpointCarrier(parsed: unknown, wire: WireProtocol, tag: string): unknown | null {
export function insertCheckpointCarrier(parsed: unknown, wire: WireProtocol, tag: string, model?: string): unknown | null {
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return null;
const body = parsed as Record<string, unknown>;
if (wire === "openai") {
Expand Down Expand Up @@ -550,10 +550,10 @@ export function insertCheckpointCarrier(parsed: unknown, wire: WireProtocol, tag
if (pp && typeof pp === "object" && !Array.isArray(pp)) {
const ppo = pp as Record<string, unknown>;
if (Object.keys(ppo).length === 1 && typeof ppo.text === "string" && parseChainCheckpoint(ppo.text)) {
return { ...body, contents: [...contents.slice(0, li), { ...c, parts: normalizeGoogleUserPartOrder([...parts.slice(0, pi), { text: tag }] as GooglePart[]) }] };
return { ...body, contents: [...contents.slice(0, li), { ...c, parts: normalizeGoogleUserPartOrder([...parts.slice(0, pi), { text: tag }] as GooglePart[], { model }) }] };
}
}
return { ...body, contents: [...contents.slice(0, li), { ...c, parts: normalizeGoogleUserPartOrder([...parts, { text: tag }] as GooglePart[]) }] };
return { ...body, contents: [...contents.slice(0, li), { ...c, parts: normalizeGoogleUserPartOrder([...parts, { text: tag }] as GooglePart[], { model }) }] };
}
}
return { ...body, contents: [...contents, { role: "user", parts: [{ text: tag }] }] };
Expand All @@ -565,14 +565,14 @@ export function insertCheckpointCarrier(parsed: unknown, wire: WireProtocol, tag
* so any downstream bili verifying the same bytes recomputes the same value.
* requestId derives from the digest — deterministic, constant-size, and it
* correlates the stamp with the exact body it covers. */
export function stampOutbound(parsed: unknown, wire: WireProtocol, processor: string, nowMs: number = Date.now()): unknown | null {
export function stampOutbound(parsed: unknown, wire: WireProtocol, processor: string, nowMs: number = Date.now(), model?: string): unknown | null {
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return null;
const placeholder: Pick<ChainCheckpoint, "v" | "processor" | "issuedAt" | "requestId"> = { v: SUPPORTED_CHECKPOINT_VERSION, processor, issuedAt: nowMs, requestId: "-" };
const digest = computeCheckpointDigest(parsed, wire, placeholder);
if (digest === null) return null;
const requestId = digest.slice("sha256:".length, "sha256:".length + 8);
const tag = renderChainCheckpoint({ v: SUPPORTED_CHECKPOINT_VERSION, processor, issuedAt: nowMs, requestId, digest });
return insertCheckpointCarrier(parsed, wire, tag);
return insertCheckpointCarrier(parsed, wire, tag, model);
}

// Generation-side digest (step 3 stamps with this): insert a zero-digest
Expand Down
8 changes: 5 additions & 3 deletions src/degenerate-retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ export const DEGENERATE_RETRY_NUDGE =

/** The retry body: the forwarded body with the continuation nudge appended as a
* trailing user turn. Null when the body cannot carry one. */
export function injectContinuationNudge(protocol: WireProtocol, body: string | Buffer): string | null {
return appendTrailingUserText(protocol, body, DEGENERATE_RETRY_NUDGE);
export function injectContinuationNudge(protocol: WireProtocol, body: string | Buffer, model?: string): string | null {
return appendTrailingUserText(protocol, body, DEGENERATE_RETRY_NUDGE, model);
}

interface ContinuationRetryOpts {
protocol: WireProtocol;
/** #5392: target model — threaded so the appended nudge keeps tool_result-first order on Claude-through-Google targets. */
model?: string;
/** The body that was forwarded upstream (the agent's own, in plugin mode). */
body: string | Buffer;
upstreamUrl: string;
Expand Down Expand Up @@ -75,7 +77,7 @@ export function makeContinuationRefetch(opts: ContinuationRetryOpts): () => Prom
/* unparseable body: injectContinuationNudge below reports and skips */
}
}
const retryBody = injectContinuationNudge(opts.protocol, opts.body);
const retryBody = injectContinuationNudge(opts.protocol, opts.body, opts.model);
if (retryBody === null) {
opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry skipped: request body carries no turn array`);
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/fake-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ export function isFakeCompletion(protocol: WireProtocol, rawText: string): boole
/** Append the fake-completion hint to the request body as a trailing user turn
* (per-wire shapes live in `appendTrailingUserText`). Returns the hinted JSON,
* or null if the body is unparseable, in which case the retry is skipped. */
export function injectFakeCompletionHint(protocol: WireProtocol, body: string | Buffer): string | null {
return appendTrailingUserText(protocol, body, FAKE_COMPLETION_HINT);
export function injectFakeCompletionHint(protocol: WireProtocol, body: string | Buffer, model?: string): string | null {
return appendTrailingUserText(protocol, body, FAKE_COMPLETION_HINT, model);
}
2 changes: 1 addition & 1 deletion src/loop/adapter-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export function createGoogleAdapter(

return {
buildRequest(coreMessages, systemPrompt, body) {
const contents = coreToGoogle(coreMessages);
const contents = coreToGoogle(coreMessages, { model: modelVersion });
if (notes && notes.length > 0) {
for (const text of notes) contents.push({ role: "user", parts: [{ text }] });
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2557,7 +2557,7 @@ async function handleRequest(
}
if (countTokens) {
return protocol === "google"
? prepareGoogleCountTokens(work as GoogleRequestBody, core, reqConfig, log, session)
? prepareGoogleCountTokens(work as GoogleRequestBody, core, reqConfig, log, session, googleModel ?? v1Model)
: prepareCountTokens(work as AnthropicRequestBody, core, reqConfig, log, session);
}
if (protocol === "google") {
Expand Down
20 changes: 11 additions & 9 deletions src/server/prepare-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ import { effectiveAbsorbBlock } from "./prepare-responses.js";
import { injectGoogleTool, injectTool } from "./inject.js";
import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js";

function appendGoogleNudge(contents: GoogleContent[], text: string): GoogleContent[] {
function appendGoogleNudge(contents: GoogleContent[], text: string, model?: string): GoogleContent[] {
const last = contents[contents.length - 1];
if (last && (last.role ?? "user") !== "model") {
const parts = Array.isArray(last.parts) ? last.parts : [];
// #3159: a plain-text tail after a functionResponse part 400s on
// Vertex — normalize so the new text leads.
// Vertex — normalize so the new text leads (#5392: skipped for Claude
// targets, whose Anthropic-behind-Google backend wants tool_result first).
return [
...contents.slice(0, -1),
{ ...last, parts: normalizeGoogleUserPartOrder([...parts, { text }]) },
{ ...last, parts: normalizeGoogleUserPartOrder([...parts, { text }], { model }) },
];
}
return [...contents, { role: "user", parts: [{ text }] }];
Expand Down Expand Up @@ -283,7 +284,7 @@ export async function prepareGoogle(
// #3400: egress image rescale — outbound copies only; the originals
// stay in session state/persistence untouched.
const egressMessages = await applyImageRescalePass(processedMessages as BiliMessage[], { session, sessionId, log, maxDim: imageRescaleMaxDim(), toolArmed: imageRescaleToolArmed(session) });
rebuiltContents = coreToGoogle(egressMessages);
rebuiltContents = coreToGoogle(egressMessages, { model });

// ONLY the static compress prompt joins the client's system text — the
// system instruction is the prefix-cache anchor and must stay
Expand Down Expand Up @@ -317,7 +318,7 @@ export async function prepareGoogle(
toolsOut = injectGoogleTool(parsed.tools, [...(absorbActive ? [absorbTools.google] : []), ...(rulesActive ? [RULE_TOOL_GOOGLE] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).google] : []), ...(imageRescaleToolArmed(session) ? [READ_ORIGINAL_IMAGE_TOOL_GOOGLE] : [])], surface?.toolPrompts, ccrEnabled(session), externalSummaryEnabled(config));
}
if (sysNotes.length > 0) {
rebuiltContents = appendGoogleNudge(rebuiltContents, sysNotes.join("\n\n---\n\n"));
rebuiltContents = appendGoogleNudge(rebuiltContents, sysNotes.join("\n\n---\n\n"), model);
}
if (willInjectNudge && turn.nudge) {
// #2228: model-decided timing (see prepareAnthropic for the policy):
Expand All @@ -330,7 +331,7 @@ export async function prepareGoogle(
const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections, "google");
const renderedWithPayload = rendered.text;
if (rendered.text) {
rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withFirstSightDrain(renderedWithPayload, turn.nudge.reason, externalSummaryEnabled(config)), visibilityMarkers));
rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withFirstSightDrain(renderedWithPayload, turn.nudge.reason, externalSummaryEnabled(config)), visibilityMarkers), model);
nudgeOutcome = "injected";
} else {
nudgeOutcome = "skipped";
Expand All @@ -346,7 +347,7 @@ export async function prepareGoogle(
if (outcome.kind === "yes") {
const span = resolveDecisionRange(outcome, ranges);
if (span) {
rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withFirstSightDrain(buildDirectiveText(span.startRef, span.endRef, outcome.topic), turn.nudge.reason, externalSummaryEnabled(config)), visibilityMarkers));
rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withFirstSightDrain(buildDirectiveText(span.startRef, span.endRef, outcome.topic), turn.nudge.reason, externalSummaryEnabled(config)), visibilityMarkers), model);
nudgeOutcome = "injected";
} else {
nudgeOutcome = "skipped";
Expand All @@ -362,7 +363,7 @@ export async function prepareGoogle(
const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections, "google");
const renderedWithPayload = rendered.text;
if (rendered.text) {
rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withFirstSightDrain(renderedWithPayload, turn.nudge.reason, externalSummaryEnabled(config)), visibilityMarkers));
rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withFirstSightDrain(renderedWithPayload, turn.nudge.reason, externalSummaryEnabled(config)), visibilityMarkers), model);
nudgeOutcome = "injected";
} else {
nudgeOutcome = "skipped";
Expand Down Expand Up @@ -418,6 +419,7 @@ export function prepareGoogleCountTokens(
config: Config,
log: (level: string, msg: string) => void,
session: Session,
model?: string,
): Prepared {
const sessionId = session.id;
try {
Expand All @@ -426,7 +428,7 @@ export function prepareGoogleCountTokens(
// counted, but nothing is adopted (state is discarded here too).
const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: usageGradeInputBaseline(session), renderTags: knobRenderNone() ? "none" : "text-only", contentStore: contentStoreOf(session) });
const stripped = stripKernelSummaries(turn.messages, turn.state);
const rebuilt: GoogleRequestBody = { ...parsed, contents: coreToGoogle(stripped as BiliMessage[]) };
const rebuilt: GoogleRequestBody = { ...parsed, contents: coreToGoogle(stripped as BiliMessage[], { model }) };
log("info", `[${sessionId}] countTokens pruned: ${msgs.length} → ${stripped.length} msgs`);
return {
body: JSON.stringify(rebuilt),
Expand Down
Loading
Loading