Skip to content

Commit 94a5b57

Browse files
PR Batch TesterGigioxxclaude
committed
fix(server): Codex generated images show in chat, including on Android with a Windows host
Codex reports a finished image generation as an imageGeneration item with savedPath, whether the model calls the tool directly or from inside exec. The adapter ignored that item, so the image only appeared when the model embedded it in its reply. Remember saved paths per turn and append any that the final reply does not already embed. Windows saved paths are embedded with forward slashes. Markdown reads the `\.` in `C:\Users\me\.codex` as an escape, and the mobile parser has no Windows path handling, so the backslash form loads the wrong file there. Co-authored-by: Guillermo Casanova <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 98a6246 commit 94a5b57

2 files changed

Lines changed: 211 additions & 0 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3779,6 +3779,119 @@ describe("CodexAdapterV2 post-settle continuation", () => {
37793779
});
37803780
};
37813781

3782+
it.effect.each([
3783+
{ name: "text-only final", reply: "Done.", image: {}, appends: true },
3784+
{ name: "path mention", reply: "Saved to /tmp/generated image.png", image: {}, appends: true },
3785+
{
3786+
name: "ordinary link",
3787+
reply: "[Open](</tmp/generated image.png>)",
3788+
image: {},
3789+
appends: true,
3790+
},
3791+
{
3792+
name: "angle bracket path",
3793+
reply: "Done.",
3794+
image: { savedPath: "/tmp/a<b>.png" },
3795+
appends: true,
3796+
embed: "![](</tmp/a\\<b\\>.png>)",
3797+
},
3798+
{
3799+
name: "Windows path",
3800+
reply: "Done.",
3801+
image: { savedPath: "C:\\Users\\me\\.codex\\generated_images\\t\\exec-1.png" },
3802+
appends: true,
3803+
embed: "![](<C:/Users/me/.codex/generated_images/t/exec-1.png>)",
3804+
},
3805+
{
3806+
name: "existing forward-slash Windows image",
3807+
reply: "![Apple](C:/Users/me/.codex/generated_images/t/exec-1.png)",
3808+
image: { savedPath: "C:\\Users\\me\\.codex\\generated_images\\t\\exec-1.png" },
3809+
},
3810+
{ name: "existing image", reply: "![Generated](</tmp/generated image.png>)", image: {} },
3811+
{
3812+
name: "existing plain image",
3813+
reply: "![Generated](/tmp/generated.png)",
3814+
image: { savedPath: "/tmp/generated.png" },
3815+
},
3816+
{ name: "failed status", reply: "Done.", image: { status: "failed" } },
3817+
{
3818+
name: "failure detail",
3819+
reply: "Done.",
3820+
image: { failure: { type: "usageLimitExceeded", limitId: "image-generation" } },
3821+
},
3822+
{ name: "missing path", reply: "Done.", image: { savedPath: null } },
3823+
{ name: "empty path", reply: "Done.", image: { savedPath: "" } },
3824+
{ name: "no assistant message", reply: null, image: {} },
3825+
])("projects native image generation: $name", ({ reply, image, appends, embed }) =>
3826+
Effect.scoped(
3827+
Effect.gen(function* () {
3828+
const scenario = "codex-native-image";
3829+
const savedPath = "/tmp/generated image.png";
3830+
const result = "aW1hZ2UtYnl0ZXMtbXVzdC1uZXZlci1yZWFjaC1jbGllbnRz";
3831+
const base = finalAnswerTranscript(
3832+
scenario,
3833+
reply === null ? [] : [{ id: "image-answer", text: reply }],
3834+
);
3835+
const transcript = makeCodexReplayTranscript({
3836+
scenario,
3837+
entries: [
3838+
...base.entries.slice(0, -1),
3839+
...(["item/started", "item/completed"] as const).map((method) => ({
3840+
type: "emit_inbound" as const,
3841+
frame: {
3842+
method,
3843+
params: {
3844+
threadId: `native-${scenario}-thread`,
3845+
turnId: `native-${scenario}-turn`,
3846+
item: {
3847+
type: "imageGeneration",
3848+
id: "generated-image",
3849+
status: "completed",
3850+
savedPath,
3851+
result,
3852+
...image,
3853+
},
3854+
},
3855+
},
3856+
})),
3857+
...base.entries.slice(-1),
3858+
],
3859+
});
3860+
const harness = yield* makeCodexReplayHarness(transcript);
3861+
const now = yield* DateTime.now;
3862+
yield* harness.runtime.startTurn(
3863+
makeCodexTestTurnInput({
3864+
threadId: harness.threadId,
3865+
providerThread: harness.providerThread,
3866+
now,
3867+
attemptId: RunAttemptId.make("attempt-native-image"),
3868+
text: "Reply with the requested recovery marker.",
3869+
}),
3870+
);
3871+
yield* harness.firstTerminal;
3872+
3873+
const messages = assistantMessages(harness.events);
3874+
const final = messages.at(-1)?.message;
3875+
const markdown = embed ?? `![](<${savedPath}>)`;
3876+
const expected = reply === null ? markdown : appends ? `${reply}\n\n${markdown}` : reply;
3877+
assert.strictEqual(final?.text, expected);
3878+
assert.strictEqual(final?.streaming, false);
3879+
assert.notInclude(encodeUnknownJson(harness.events), result);
3880+
if (appends) {
3881+
assert.lengthOf(messages, 2);
3882+
assert.strictEqual(final?.id, messages[0]?.message.id);
3883+
assert.strictEqual(final?.nodeId, messages[0]?.message.nodeId);
3884+
} else {
3885+
assert.lengthOf(messages, 1);
3886+
}
3887+
}).pipe(
3888+
Effect.provide(
3889+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3890+
),
3891+
),
3892+
),
3893+
);
3894+
37823895
it.effect("suppresses a trailing empty final answer after a non-empty final answer", () =>
37833896
Effect.scoped(
37843897
Effect.gen(function* () {

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
dynamicToolTitle,
4242
} from "@t3tools/shared/toolActivity";
4343
import { getModelSelectionStringOptionValue, modelSelectionsEqual } from "@t3tools/shared/model";
44+
import { isWindowsAbsolutePath } from "@t3tools/shared/path";
4445
import { resolveSpawnCommand } from "@t3tools/shared/shell";
4546
import type {
4647
ChatAttachment,
@@ -1890,6 +1891,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
18901891
const offeredContinuationItemsByTurn = yield* Ref.make(new Map<string, Set<string>>());
18911892
const finalAnswerItemIdsByTurn = yield* Ref.make(new Map<string, Set<string>>());
18921893
const completedFinalAnswerTextsByTurn = yield* Ref.make(new Map<string, Set<string>>());
1894+
const lastFinalAnswerByTurn = yield* Ref.make(
1895+
new Map<string, { readonly id: string; readonly text: string }>(),
1896+
);
1897+
const generatedImagesByTurn = yield* Ref.make(new Map<string, Map<string, string>>());
18931898
// Native completion and the interrupt timeout share one finalization
18941899
// path. Serialize the race so only one can publish terminal events.
18951900
const turnTerminalizationPermit = yield* Semaphore.make(1);
@@ -2215,6 +2220,22 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
22152220
updated.delete(nativeTurnId);
22162221
return updated;
22172222
});
2223+
yield* Ref.update(lastFinalAnswerByTurn, (current) => {
2224+
if (!current.has(nativeTurnId)) {
2225+
return current;
2226+
}
2227+
const updated = new Map(current);
2228+
updated.delete(nativeTurnId);
2229+
return updated;
2230+
});
2231+
yield* Ref.update(generatedImagesByTurn, (current) => {
2232+
if (!current.has(nativeTurnId)) {
2233+
return current;
2234+
}
2235+
const updated = new Map(current);
2236+
updated.delete(nativeTurnId);
2237+
return updated;
2238+
});
22182239
yield* Ref.update(finalAnswerItemIdsByTurn, (current) => {
22192240
if (!current.has(nativeTurnId)) {
22202241
return current;
@@ -3416,6 +3437,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
34163437
turnItem: artifacts.turnItem,
34173438
});
34183439
if (finalAnswerItem && update.completed) {
3440+
yield* Ref.update(lastFinalAnswerByTurn, (current) =>
3441+
new Map(current).set(update.turnId, { id: update.itemId, text: update.text }),
3442+
);
34193443
yield* Ref.update(completedFinalAnswerTextsByTurn, (current) => {
34203444
const updated = new Map(current);
34213445
const texts = new Set(updated.get(update.turnId) ?? []);
@@ -4968,6 +4992,20 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
49684992
return;
49694993
}
49704994

4995+
if (payload.item.type === "imageGeneration") {
4996+
const { id, savedPath, status, failure } = payload.item;
4997+
if (savedPath?.trim() && status !== "failed" && failure == null) {
4998+
yield* Ref.update(generatedImagesByTurn, (current) => {
4999+
const updated = new Map(current);
5000+
const images = new Map(updated.get(payload.turnId));
5001+
images.set(id, savedPath);
5002+
updated.set(payload.turnId, images);
5003+
return updated;
5004+
});
5005+
}
5006+
return;
5007+
}
5008+
49715009
if (payload.item.type !== "agentMessage") {
49725010
return;
49735011
}
@@ -5971,6 +6009,22 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
59716009
updated.delete(input.nativeTurnId);
59726010
return updated;
59736011
});
6012+
yield* Ref.update(lastFinalAnswerByTurn, (current) => {
6013+
if (!current.has(input.nativeTurnId)) {
6014+
return current;
6015+
}
6016+
const updated = new Map(current);
6017+
updated.delete(input.nativeTurnId);
6018+
return updated;
6019+
});
6020+
yield* Ref.update(generatedImagesByTurn, (current) => {
6021+
if (!current.has(input.nativeTurnId)) {
6022+
return current;
6023+
}
6024+
const updated = new Map(current);
6025+
updated.delete(input.nativeTurnId);
6026+
return updated;
6027+
});
59746028
yield* Ref.update(finalAnswerItemIdsByTurn, (current) => {
59756029
if (!current.has(input.nativeTurnId)) {
59766030
return current;
@@ -5996,6 +6050,50 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
59966050
(yield* Ref.get(interruptingNativeTurns)).has(payload.turn.id)
59976051
? "interrupted"
59986052
: nativeStatus;
6053+
const images = (yield* Ref.get(generatedImagesByTurn)).get(payload.turn.id);
6054+
const firstImageItemId = images?.keys().next().value;
6055+
if (images !== undefined && firstImageItemId !== undefined) {
6056+
const finalAnswer = (yield* Ref.get(lastFinalAnswerByTurn)).get(payload.turn.id);
6057+
let text = finalAnswer?.text ?? "";
6058+
for (const savedPath of images.values()) {
6059+
// Markdown reads the `\.` in `C:\Users\me\.codex` as an escape. Clients without
6060+
// Windows path handling, such as released mobile apps, would load the wrong file.
6061+
const embedPath = isWindowsAbsolutePath(savedPath)
6062+
? savedPath.replaceAll("\\", "/")
6063+
: savedPath;
6064+
// Only an existing image embed counts; a plain path mention or link still needs a preview.
6065+
const embedded = [savedPath, embedPath].some((candidate) => {
6066+
const escapedPath = candidate.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
6067+
return new RegExp(`!\\[[^\\]]*\\]\\(<?${escapedPath}>?[\\s)]`).test(text);
6068+
});
6069+
if (!embedded) {
6070+
text += `${text.length > 0 ? "\n\n" : ""}![](<${embedPath.replace(/[<>]/g, "\\$&")}>)`;
6071+
}
6072+
}
6073+
// Settled timelines retain the final assistant message, so attach images there.
6074+
if (text !== (finalAnswer?.text ?? "")) {
6075+
const artifacts = yield* buildAgentMessageArtifacts(
6076+
context,
6077+
{ id: finalAnswer?.id ?? firstImageItemId, text },
6078+
true,
6079+
);
6080+
yield* emitProviderEvent({
6081+
type: "node.updated",
6082+
driver: CODEX_PROVIDER,
6083+
node: artifacts.node,
6084+
});
6085+
yield* emitProviderEvent({
6086+
type: "message.updated",
6087+
driver: CODEX_PROVIDER,
6088+
message: artifacts.message,
6089+
});
6090+
yield* emitProviderEvent({
6091+
type: "turn_item.updated",
6092+
driver: CODEX_PROVIDER,
6093+
turnItem: artifacts.turnItem,
6094+
});
6095+
}
6096+
}
59996097
yield* finalizeCodexTurn({
60006098
context,
60016099
nativeTurnId: payload.turn.id,

0 commit comments

Comments
 (0)