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
50 changes: 31 additions & 19 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { runCompressLoop, pickAdapter } from "./loop/index.js";
import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeResponsesMessageItems } from "./loop/adapter-responses.js";
import { rewriteOpenaiJsonResponse } from "./stream-openai.js";
import { rewriteResponsesJsonResponse } from "./stream-responses.js";
import { observeResponsesTerminalState } from "./stream-terminal.js";
import { emitStreamError } from "./stream-error.js";
import { affinityToken, clientConversationHeader, preferPromptCacheKeyIdentity, type ConversationIdentity } from "./session-id.js";
import { prefixAffinity, type AnonymousAffinity } from "./prefix-affinity.js";
Expand Down Expand Up @@ -1401,6 +1402,12 @@ function prepareResponses(

const shouldInject = opts.compress.injectTool;
const injectTools = shouldInject && !pluginMode;
// Codex native remote-compact request: no compress prompt/tools (the model
// produces the compaction itself), no acp tags, plain passthrough so the
// response terminal state can gate the rebase marker.
const isCompactionTrigger = Array.isArray(parsed.input)
&& parsed.input.length > 0
&& parsed.input[parsed.input.length - 1]!.type === "compaction_trigger";
// Route config is keyed by the upstream THIS request goes to (#286: a
// session can outlive its first relay — session.meta.upstreamOrigin is
// first-wins and would silently ignore the new relay's route settings).
Expand All @@ -1416,7 +1423,7 @@ function prepareResponses(
log("info", `[${sessionId}] input items: ${Array.isArray(parsed.input) ? parsed.input.map((i: ResponseInputItem) => i.type).join(",") : "(string)"}`);
}
const tokenCount = session.stats.lastInputTokens;
const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" });
const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: process.env.ACP_RENDER_NONE || isCompactionTrigger ? "none" : "text-only" });
session.state = turn.state;
// The fold from last turn's compress has now materialized in state —
// future usage reports are post-fold reality, drop the credit.
Expand All @@ -1436,7 +1443,7 @@ function prepareResponses(
processedMessages = stripKernelSummaries(turn.messages, turn.state);
reapOrphanBlocks(session, msgs, deactivateBlock);
rebuiltInput = patchResponsesInput(projection, processedMessages);
if (shouldInject && !process.env.ACP_NO_COMPRESS_PROMPT) {
if (shouldInject && !isCompactionTrigger && !process.env.ACP_NO_COMPRESS_PROMPT) {
const prompt = responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts) : buildCompressSystemPrompt(prompts);
const devContent = [...projection.systemParts, prompt].join("\n\n---\n\n");
rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent);
Expand All @@ -1448,15 +1455,10 @@ function prepareResponses(
} else if (projection.systemParts.length > 0) {
rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, projection.systemParts.join("\n\n---\n\n"));
}
// Codex's native remote-compact request ends with a `compaction_trigger`
// item that the upstream requires to be the FINAL input item (else 400
// "must be the final input item"). A nudge appended after it would break
// Codex's own compaction, and is redundant — the native compact IS the
// compression. Skip the nudge for such requests.
const triggerFinal = Array.isArray(rebuiltInput)
&& rebuiltInput.length > 0
&& rebuiltInput[rebuiltInput.length - 1]!.type === "compaction_trigger";
if (opts.compress.injectNudge && turn.nudge?.shouldInject && shouldInject && !triggerFinal) {
// A nudge appended after a trailing `compaction_trigger` would break
// the upstream's "must be the final input item" requirement and is
// redundant — the native compact IS the compression.
if (opts.compress.injectNudge && turn.nudge?.shouldInject && shouldInject && !isCompactionTrigger) {
try {
const rendered = renderNudgeText(turn.nudge, prompts);
if (rendered.text) {
Expand Down Expand Up @@ -1522,11 +1524,12 @@ function prepareResponses(
responsesProjection,
protocol: "responses",
stream,
compressInjected: injectTools,
compressInjected: injectTools && !isCompactionTrigger,
pluginMode,
responsesTextProtocol,
nudge,
prompts,
resetAfterSuccess: isCompactionTrigger,
};
}

Expand Down Expand Up @@ -2127,8 +2130,7 @@ async function forward(
res.end();
clearUpstreamTimer();
if (prepared?.resetAfterSuccess) {
markNativeCompactionBoundary(prepared.session);
log("info", `[${prepared.session.id}] native Responses compact completed; rebase scheduled for next Responses turn`);
log("warn", `[${prepared.session.id}] native compact response had no body; rebase NOT scheduled`);
}
return;
}
Expand Down Expand Up @@ -2160,11 +2162,21 @@ async function forward(
prepared.compressInjected &&
prepared.processedMessages.length > 0;
if (!useRewriter || prepared === null) {
await pipeThrough(upstream.body, res);
clearUpstreamTimer();
if (prepared?.resetAfterSuccess) {
markNativeCompactionBoundary(prepared.session);
log("info", `[${prepared.session.id}] native Responses compact completed; rebase scheduled for next Responses turn`);
if (prepared && prepared.resetAfterSuccess) {
const [toClient, toObserve] = upstream.body.tee();
const observed = observeResponsesTerminalState(toObserve, prepared.stream);
await pipeThrough(toClient, res);
clearUpstreamTimer();
const terminal = await observed;
if (terminal === "completed") {
markNativeCompactionBoundary(prepared.session);
log("info", `[${prepared.session.id}] native Responses compact completed; rebase scheduled for next Responses turn`);
} else {
log("warn", `[${prepared.session.id}] native compact response terminal=${terminal}; rebase NOT scheduled`);
}
} else {
await pipeThrough(upstream.body, res);
clearUpstreamTimer();
}
return;
}
Expand Down
109 changes: 109 additions & 0 deletions src/stream-terminal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// #321 PR-C: terminal-state observer for native-compaction responses. The
// rebase marker may only fire on "completed": a failed/incomplete/truncated
// stream means the client kept its history, and rebasing on a compaction that
// never happened discards valid blocks (#249). "unknown" is the safe default
// (no rebase; the next request self-heals via kernel message-id deactivation).

export type TerminalState = "completed" | "failed" | "unknown";

const SUCCEEDED = "response.completed";
const FAILED_EVENTS = new Set(["response.failed", "response.incomplete"]);
const MAX_JSON_BYTES = 16 << 20;

export async function observeResponsesTerminalState(stream: ReadableStream<Uint8Array>, isStream: boolean): Promise<TerminalState> {
return isStream ? observeSseTerminalState(stream) : observeJsonTerminalState(stream);
}

function terminalFromSseLine(line: string): TerminalState | null {
if (line.startsWith("event:")) {
const name = line.slice(6).trim();
if (name === SUCCEEDED) return "completed";
if (FAILED_EVENTS.has(name)) return "failed";
return null;
}
if (line.startsWith("data:")) {
const data = line.slice(5).trim();
if (!data.startsWith("{")) return null;
try {
const type = (JSON.parse(data) as { type?: unknown }).type;
if (type === SUCCEEDED) return "completed";
if (type === "response.failed" || type === "response.incomplete") return "failed";
} catch {
return null;
}
}
return null;
}

async function observeSseTerminalState(stream: ReadableStream<Uint8Array>): Promise<TerminalState> {
const reader = stream.getReader();
const decoder = new TextDecoder();
let buf = "";
let state: TerminalState = "unknown";
try {
for (;;) {
const { done, value } = await reader.read();
if (done) break;
buf += decoder.decode(value, { stream: true });
let nl: number;
while ((nl = buf.indexOf("\n")) >= 0) {
const line = buf.slice(0, nl).replace(/\r$/, "");
buf = buf.slice(nl + 1);
const next = terminalFromSseLine(line);
if (next !== null) {
state = next;
break;
}
}
if (state !== "unknown") {
// Release this tee branch; the client branch keeps draining the source.
await reader.cancel().catch(() => {});
break;
}
}
} catch {
return "unknown";
} finally {
reader.releaseLock();
}
return state;
}

async function observeJsonTerminalState(stream: ReadableStream<Uint8Array>): Promise<TerminalState> {
const reader = stream.getReader();
const chunks: Buffer[] = [];
let size = 0;
let overflow = false;
try {
for (;;) {
const { done, value } = await reader.read();
if (done) break;
if (!overflow) {
size += value.length;
if (size <= MAX_JSON_BYTES) {
chunks.push(Buffer.from(value));
} else {
overflow = true;
}
}
}
} catch {
return "unknown";
} finally {
reader.releaseLock();
}
if (overflow) return "unknown";
const text = Buffer.concat(chunks).toString("utf8").trim();
if (!text) return "unknown";
let body: unknown;
try {
body = JSON.parse(text);
} catch {
return "unknown";
}
if (typeof body !== "object" || body === null) return "unknown";
const rec = body as Record<string, unknown>;
if (rec.error !== undefined) return "failed";
if (Array.isArray(rec.output)) return "completed";
return "unknown";
}
16 changes: 11 additions & 5 deletions tests/compaction-trigger-finality.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ test("e2e #280r2 (Responses): trailing compaction_trigger stays final — nudge
const proxyPort = proxy.address().port;
const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`;

const post = (input: unknown) => fetch(url, {
const post = (input: unknown, session = "trig-sess") => fetch(url, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: "trig-sess", instructions: "You are the test coding agent.", input }),
body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: session, instructions: "You are the test coding agent.", input }),
});

try {
Expand All @@ -108,9 +108,15 @@ test("e2e #280r2 (Responses): trailing compaction_trigger stays final — nudge
assert.equal(lastItem.type, "compaction_trigger", "compaction_trigger is the final input item");
assert.ok(!bodies[bodies.length - 1].includes("Context limit reached"), "no nudge appended after the trigger");

// Control: same armed session, normal trailing user message — the
// nudge must still be injected.
const r3 = await post([...conversation(), { type: "message", role: "user", content: "What should we do next?" }]);
// Control: normal trailing user message — the nudge must still be
// injected. Runs on a SEPARATE session: since #321 PR-C a completed
// trigger response marks a native-compaction boundary, so the next
// turn on trig-sess rebases (usage reset) and the nudge would be
// legitimately unarmed there. The warmup turn arms the usage report.
const r3a = await post(conversation(), "trig-ctl");
assert.equal(r3a.status, 200);
await r3a.text();
const r3 = await post([...conversation(), { type: "message", role: "user", content: "What should we do next?" }], "trig-ctl");
assert.equal(r3.status, 200);
await r3.text();
assert.ok(bodies[bodies.length - 1].includes("Context limit reached"), "nudge still injected on a normal request");
Expand Down
Loading
Loading