Skip to content

Commit 2cb9a75

Browse files
steipeteampagent
andauthored
1 parent 9b90e10 commit 2cb9a75

15 files changed

Lines changed: 187 additions & 65 deletions

‎docs/reference/transcript-hygiene.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ Implementation:
7777
- Max image side is configurable via `agents.defaults.imageMaxDimensionPx`
7878
(default: `1200`)
7979
- Blank text blocks are removed while this pass walks replay content.
80-
Assistant turns that become empty are dropped from the replay copy; user
81-
and tool-result turns that become empty receive a non-empty
82-
omitted-content placeholder.
80+
Assistant turns that become empty are dropped unless they own opaque
81+
provider replay state; user and tool-result turns that become empty receive
82+
a non-empty omitted-content placeholder.
8383

8484
---
8585

‎packages/ai/src/transports/openai-responses-compaction-replay.test.ts‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -928,19 +928,15 @@ describe("OpenAI Responses compaction replay", () => {
928928
expect(input.map((item) => item.type)).toEqual(["compaction", "message"]);
929929
});
930930

931-
it("replays when session and auth identities match", () => {
932-
const assistant = createOutput();
933-
assistant.providerReplay = compactionState(model, { replayIndex: 0 });
934-
935-
const input = convertResponsesMessages(
936-
model,
937-
{ messages: [assistant] },
938-
new Set(["openai"]),
939-
replayIdentity,
940-
);
931+
it.each(responseConverters)(
932+
"$name replays an empty checkpoint owner when request identities match",
933+
({ convert }) => {
934+
const assistant = createOutput();
935+
assistant.providerReplay = compactionState(model, { replayIndex: 0 });
941936

942-
expect(input.some((item) => item.type === "compaction")).toBe(true);
943-
});
937+
expect(convert({ messages: [assistant] }).map((item) => item.type)).toEqual(["compaction"]);
938+
},
939+
);
944940

945941
it.each(responseConverters)(
946942
"$name does not replay or prune across a different or missing request identity",

‎scripts/lib/state-schema-inline-plugin.mts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ export function createStateSchemaInlinePlugin(rootDir = process.cwd()) {
2121
const schemasByModulePath = new Map(
2222
STATE_SCHEMA_MODULES.map((schema) => [path.resolve(rootDir, schema.modulePath), schema]),
2323
);
24+
const cacheKeyForSchema = ({ id }: { id: string }) => {
25+
const schema = schemasByModulePath.get(path.resolve(id));
26+
return schema ? fs.readFileSync(path.resolve(rootDir, schema.schemaPath), "utf8") : undefined;
27+
};
2428

2529
return {
2630
name: STATE_SCHEMA_INLINE_PLUGIN_NAME,
31+
configureVitest(context: {
32+
experimental_defineCacheKeyGenerator(callback: typeof cacheKeyForSchema): void;
33+
}) {
34+
context.experimental_defineCacheKeyGenerator(cacheKeyForSchema);
35+
},
2736
load(this: { addWatchFile(id: string): void }, id: string) {
2837
const schema = schemasByModulePath.get(path.resolve(id));
2938
if (!schema) {

‎src/agents/embedded-agent-helpers.sanitize-session-messages-images.removes-empty-assistant-text-blocks-but-preserves.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,31 @@ describe("sanitizeSessionMessagesImages", () => {
250250
expect(out).toHaveLength(1);
251251
expect(out[0]?.role).toBe("user");
252252
});
253+
it.each([
254+
["full", "length"],
255+
["images-only", "length"],
256+
["full", "error"],
257+
["images-only", "error"],
258+
] as const)(
259+
"preserves an empty provider replay owner in %s mode after %s",
260+
async (sanitizeMode, stopReason) => {
261+
const checkpoint = {
262+
...makeOpenAiResponsesAssistantMessage([{ type: "text", text: "" }], stopReason),
263+
providerReplay: {
264+
v: 1,
265+
type: "opaque-checkpoint",
266+
data: "opaque-state",
267+
provider: "openai",
268+
api: "openai-responses",
269+
model: "gpt-5.4",
270+
},
271+
} satisfies AssistantMessage;
272+
273+
const out = await sanitizeSessionMessagesImages([checkpoint], "test", { sanitizeMode });
274+
275+
expect(out).toEqual([{ ...checkpoint, content: [] }]);
276+
},
277+
);
253278
it("drops empty assistant error messages", async () => {
254279
const input = castAgentMessages([
255280
{ role: "user", content: "hello", timestamp: nextTimestamp() } satisfies UserMessage,

‎src/agents/embedded-agent-helpers/images.ts‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ export async function sanitizeSessionMessagesImages(
5353
};
5454
} & ImageSanitizationLimits,
5555
): Promise<AgentMessage[]> {
56-
const sanitizeMode = options?.sanitizeMode ?? "full";
57-
const allowNonImageSanitization = sanitizeMode === "full";
5856
const imageSanitization = {
5957
maxDimensionPx: options?.maxDimensionPx,
6058
maxBytes: options?.maxBytes,
@@ -113,7 +111,7 @@ export async function sanitizeSessionMessagesImages(
113111
imageSanitization,
114112
)) as unknown as typeof assistantMsg.content;
115113
const finalContent = dropEmptyTextBlocks(nextContent);
116-
if (finalContent.length > 0) {
114+
if (finalContent.length > 0 || assistantMsg.providerReplay) {
117115
out.push({ ...assistantMsg, content: finalContent });
118116
}
119117
} else {
@@ -126,28 +124,14 @@ export async function sanitizeSessionMessagesImages(
126124
const strippedContent = options?.preserveSignatures
127125
? content // Keep signatures for Antigravity Claude
128126
: stripThoughtSignatures(content, options?.sanitizeThoughtSignatures); // Strip for Gemini
129-
if (!allowNonImageSanitization) {
130-
const nextContent = (await sanitizeContentBlocksImages(
131-
dropEmptyTextBlocks(strippedContent) as unknown as ContentBlock[],
132-
label,
133-
imageSanitization,
134-
)) as unknown as typeof assistantMsg.content;
135-
if (nextContent.length > 0) {
136-
out.push({ ...assistantMsg, content: nextContent });
137-
}
138-
continue;
139-
}
140-
141-
const filteredContent = dropEmptyTextBlocks(strippedContent);
142127
const finalContent = (await sanitizeContentBlocksImages(
143-
filteredContent as unknown as ContentBlock[],
128+
dropEmptyTextBlocks(strippedContent) as unknown as ContentBlock[],
144129
label,
145130
imageSanitization,
146131
)) as unknown as typeof assistantMsg.content;
147-
if (finalContent.length === 0) {
148-
continue;
132+
if (finalContent.length > 0 || assistantMsg.providerReplay) {
133+
out.push({ ...assistantMsg, content: finalContent });
149134
}
150-
out.push({ ...assistantMsg, content: finalContent });
151135
continue;
152136
}
153137
}

‎src/agents/embedded-agent-runner/run.incomplete-turn.empty-response-recovery.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,48 @@ describe("runEmbeddedAgent incomplete-turn safety", () => {
134134
expectWarnMessageWith("empty response detected");
135135
});
136136

137+
it("continues after an OpenAI Responses compaction-only incomplete turn", async () => {
138+
const checkpoint = makeLastAssistant({
139+
api: "openai-responses",
140+
provider: "openai",
141+
model: "gpt-5.6-luna",
142+
stopReason: "length",
143+
providerReplay: {
144+
v: 1,
145+
type: "openai-responses-compaction",
146+
data: "opaque-checkpoint",
147+
provider: "openai",
148+
api: "openai-responses",
149+
model: "gpt-5.6-luna",
150+
},
151+
});
152+
mockedRunEmbeddedAttempt.mockResolvedValueOnce(
153+
makeAttemptResult({
154+
assistantTexts: [],
155+
currentAttemptAssistant: checkpoint,
156+
lastAssistant: checkpoint,
157+
}),
158+
);
159+
mockedRunEmbeddedAttempt.mockResolvedValueOnce(
160+
makeAttemptResult({
161+
assistantTexts: ["Visible answer after compaction."],
162+
lastAssistant: makeLastAssistant({
163+
content: [{ type: "text", text: "Visible answer after compaction." }],
164+
}),
165+
}),
166+
);
167+
168+
await runEmbeddedAgent(
169+
makeRunParams("run-provider-compaction-continuation", {
170+
provider: "openai",
171+
model: "gpt-5.6-luna",
172+
}),
173+
);
174+
175+
expect(mockedRunEmbeddedAttempt).toHaveBeenCalledTimes(2);
176+
expectWarnMessageWith("compaction interrupted visible final answer");
177+
});
178+
137179
it("retries empty Anthropic-compatible stop turns even when the provider is not Kimi", async () => {
138180
mockedClassifyFailoverReason.mockReturnValue(null);
139181
mockedResolveModelAsync.mockResolvedValue({

‎src/agents/embedded-agent-runner/run/terminal-resolution.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ export async function resolveEmbeddedRunTerminal(input: {
341341
if (
342342
!emptyAssistantReplyIsSilent &&
343343
!settledTurnFinalizationAttempted &&
344-
input.attemptCompactionCount > 0 &&
344+
(input.attemptCompactionCount > 0 ||
345+
attempt.currentAttemptAssistant?.providerReplay?.type === "openai-responses-compaction") &&
345346
payloadCount === 0 &&
346347
!terminalInterrupted &&
347348
!promptError &&

‎src/gateway/gateway-codex-harness.live-helpers.test.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,21 @@ describe("gateway codex harness live helpers", () => {
6262
guardianProbe: false,
6363
imageProbe: false,
6464
mcpProbe: false,
65+
multiSessionProbe: false,
6566
resumeStress: false,
6667
subagentProbe: true,
6768
};
6869

6970
expect(shouldUseCodexHarnessSubagentOnlyFastPath(base)).toBe(true);
70-
expect(shouldUseCodexHarnessSubagentOnlyFastPath({ ...base, resumeStress: true })).toBe(false);
71-
expect(shouldUseCodexHarnessSubagentOnlyFastPath({ ...base, compactionStress: true })).toBe(
72-
false,
73-
);
74-
expect(shouldUseCodexHarnessSubagentOnlyFastPath({ ...base, codeModeOnly: true })).toBe(false);
75-
expect(shouldUseCodexHarnessSubagentOnlyFastPath({ ...base, explicitOptOut: true })).toBe(
76-
false,
77-
);
71+
for (const flag of [
72+
"codeModeOnly",
73+
"compactionStress",
74+
"explicitOptOut",
75+
"multiSessionProbe",
76+
"resumeStress",
77+
] as const) {
78+
expect(shouldUseCodexHarnessSubagentOnlyFastPath({ ...base, [flag]: true })).toBe(false);
79+
}
7880
});
7981

8082
it("classifies sessions.list timeouts as retryable live Codex errors", () => {

‎src/gateway/gateway-codex-harness.live-helpers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export function shouldUseCodexHarnessSubagentOnlyFastPath(params: {
9696
guardianProbe: boolean;
9797
imageProbe: boolean;
9898
mcpProbe: boolean;
99+
multiSessionProbe: boolean;
99100
resumeStress: boolean;
100101
subagentProbe: boolean;
101102
}): boolean {
@@ -107,6 +108,7 @@ export function shouldUseCodexHarnessSubagentOnlyFastPath(params: {
107108
!params.guardianProbe &&
108109
!params.imageProbe &&
109110
!params.mcpProbe &&
111+
!params.multiSessionProbe &&
110112
!params.resumeStress &&
111113
!params.explicitOptOut
112114
);

‎src/gateway/gateway-codex-harness.live.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ const CODEX_HARNESS_SUBAGENT_ONLY = shouldUseCodexHarnessSubagentOnlyFastPath({
148148
guardianProbe: CODEX_HARNESS_GUARDIAN_PROBE,
149149
imageProbe: CODEX_HARNESS_IMAGE_PROBE,
150150
mcpProbe: CODEX_HARNESS_MCP_PROBE,
151+
multiSessionProbe: CODEX_HARNESS_MULTI_SESSION_PROBE,
151152
resumeStress: CODEX_HARNESS_RESUME_STRESS,
152153
subagentProbe: CODEX_HARNESS_SUBAGENT_PROBE,
153154
});
@@ -2209,7 +2210,6 @@ describeLive("gateway live (Codex harness)", () => {
22092210
},
22102211
workspace,
22112212
});
2212-
break;
22132213
}
22142214

22152215
if (CODEX_HARNESS_SUBAGENT_PROBE) {

0 commit comments

Comments
 (0)