Skip to content

Commit 78638ef

Browse files
PR Batch Testerclaude
andcommitted
fix(server): generated images attach inside turn finalization
Project generated images from the guarded turn finalization, so a late turn/completed cannot post after an interrupt already closed the turn, and the interrupt-timeout path keeps images too. An image from work that outlives turn/completed, such as a backgrounded exec, attaches when it lands on the settled turn. UNC saved paths embed as file: URLs, because `//host` reads as a web URL on every client. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 94a5b57 commit 78638ef

2 files changed

Lines changed: 151 additions & 45 deletions

File tree

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ describe("Codex context usage compatibility", () => {
9999
});
100100
});
101101

102+
describe("appendGeneratedImageEmbeds", () => {
103+
it("writes Windows paths in a form every Markdown parser keeps", () => {
104+
assert.strictEqual(
105+
CodexAdapterV2.appendGeneratedImageEmbeds("", ["\\\\host\\share\\.codex\\a.png"]),
106+
"![](<file://host/share/.codex/a.png>)",
107+
);
108+
});
109+
110+
it("appends each image once", () => {
111+
assert.strictEqual(
112+
CodexAdapterV2.appendGeneratedImageEmbeds("![](/tmp/a.png)", ["/tmp/a.png", "/tmp/b.png"]),
113+
"![](/tmp/a.png)\n\n![](</tmp/b.png>)",
114+
);
115+
});
116+
});
117+
102118
describe("CodexAdapterV2 file change approvals", () => {
103119
it("uses nonblank reasons before sorted file operations and renamed paths", () => {
104120
const fileChanges = {
@@ -4429,6 +4445,67 @@ describe("CodexAdapterV2 post-settle continuation", () => {
44294445
],
44304446
});
44314447

4448+
it.effect("attaches an image that finishes after the turn settles to the final answer", () =>
4449+
Effect.scoped(
4450+
Effect.gen(function* () {
4451+
const savedPath = "/tmp/late image.png";
4452+
const lateImage = {
4453+
type: "emit_inbound" as const,
4454+
label: "item/completed/image-late",
4455+
afterMs: 10_000,
4456+
frame: {
4457+
method: "item/completed",
4458+
params: {
4459+
item: {
4460+
type: "imageGeneration",
4461+
id: "late-image",
4462+
status: "completed",
4463+
savedPath,
4464+
result: "aW1hZ2U=",
4465+
},
4466+
threadId: BG_NATIVE_THREAD,
4467+
turnId: BG_NATIVE_TURN,
4468+
completedAtMs: 1782622451000,
4469+
},
4470+
},
4471+
};
4472+
const harness = yield* makeCodexReplayHarness(
4473+
makeCodexReplayTranscript({
4474+
scenario: `${BG_SCENARIO}-late-image`,
4475+
entries: [
4476+
...backgroundExecTranscript.entries.slice(0, -1),
4477+
lateImage,
4478+
backgroundExecTranscript.entries.at(-1)!,
4479+
],
4480+
}),
4481+
);
4482+
yield* harness.runtime.startTurn(
4483+
makeCodexTestTurnInput({
4484+
threadId: harness.threadId,
4485+
providerThread: harness.providerThread,
4486+
now: yield* DateTime.now,
4487+
attemptId: RunAttemptId.make("attempt-codex-bg-image"),
4488+
text: BG_PROMPT,
4489+
}),
4490+
);
4491+
yield* awaitUntil(() => harness.terminalEvents().length === 1, "root turn terminal");
4492+
4493+
yield* TestClock.adjust("10 seconds");
4494+
const expected = `STARTED\n\n![](<${savedPath}>)`;
4495+
yield* awaitUntil(
4496+
() => assistantMessages(harness.events).at(-1)?.message.text === expected,
4497+
"late image projection",
4498+
);
4499+
const messages = assistantMessages(harness.events);
4500+
assert.strictEqual(messages.at(-1)?.message.id, messages[0]?.message.id);
4501+
}).pipe(
4502+
Effect.provide(
4503+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
4504+
),
4505+
),
4506+
),
4507+
);
4508+
44324509
it.effect(
44334510
"projects a post-settle background command completion and requests a continuation",
44344511
() =>

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

Lines changed: 74 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +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";
44+
import { isUncPath, isWindowsDrivePath } from "@t3tools/shared/path";
4545
import { resolveSpawnCommand } from "@t3tools/shared/shell";
4646
import type {
4747
ChatAttachment,
@@ -1107,6 +1107,32 @@ function parseCodexRetryProgress(
11071107
return { attempt, maxAttempts };
11081108
}
11091109

1110+
/**
1111+
* Appends a Markdown image for each saved path the text does not already embed.
1112+
* Markdown reads the `\.` in `C:\Users\me\.codex` as an escape, and clients without
1113+
* Windows path handling, such as released mobile apps, would load the wrong file. Drive
1114+
* paths use forward slashes; UNC paths use `file:` URLs, since `//host` reads as a web URL.
1115+
*/
1116+
export function appendGeneratedImageEmbeds(text: string, savedPaths: Iterable<string>): string {
1117+
let result = text;
1118+
for (const savedPath of savedPaths) {
1119+
const embedPath = isWindowsDrivePath(savedPath)
1120+
? savedPath.replaceAll("\\", "/")
1121+
: isUncPath(savedPath)
1122+
? `file:${savedPath.replaceAll("\\", "/")}`
1123+
: savedPath;
1124+
// Only an existing image embed counts; a plain path mention or link still needs a preview.
1125+
const embedded = [savedPath, embedPath].some((candidate) => {
1126+
const escapedPath = candidate.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1127+
return new RegExp(`!\\[[^\\]]*\\]\\(<?${escapedPath}>?[\\s)]`).test(result);
1128+
});
1129+
if (!embedded) {
1130+
result += `${result.length > 0 ? "\n\n" : ""}![](<${embedPath.replace(/[<>]/g, "\\$&")}>)`;
1131+
}
1132+
}
1133+
return result;
1134+
}
1135+
11101136
function codexErrorInfoCode(value: unknown): string | null {
11111137
if (typeof value === "string") {
11121138
return value;
@@ -5002,6 +5028,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
50025028
updated.set(payload.turnId, images);
50035029
return updated;
50045030
});
5031+
// Work that outlives turn/completed, such as a backgrounded exec, can finish later.
5032+
if (settled) {
5033+
yield* projectGeneratedImages(context, payload.turnId);
5034+
}
50055035
}
50065036
return;
50075037
}
@@ -5763,6 +5793,48 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
57635793
},
57645794
);
57655795

5796+
// Settled timelines retain the final assistant message, so generated images attach there.
5797+
const projectGeneratedImages = (context: ActiveCodexTurnContext, nativeTurnId: string) =>
5798+
Effect.gen(function* () {
5799+
const images = yield* Ref.modify(generatedImagesByTurn, (current) => {
5800+
const pending = current.get(nativeTurnId);
5801+
if (pending === undefined) return [undefined, current] as const;
5802+
const updated = new Map(current);
5803+
updated.delete(nativeTurnId);
5804+
return [pending, updated] as const;
5805+
});
5806+
const firstImageItemId = images?.keys().next().value;
5807+
if (images === undefined || firstImageItemId === undefined) {
5808+
return;
5809+
}
5810+
const finalAnswer = (yield* Ref.get(lastFinalAnswerByTurn)).get(nativeTurnId);
5811+
const text = appendGeneratedImageEmbeds(finalAnswer?.text ?? "", images.values());
5812+
if (text === (finalAnswer?.text ?? "")) {
5813+
return;
5814+
}
5815+
const message = { id: finalAnswer?.id ?? firstImageItemId, text };
5816+
// A later image on a settled turn appends to this text instead of replacing it.
5817+
yield* Ref.update(lastFinalAnswerByTurn, (current) =>
5818+
new Map(current).set(nativeTurnId, message),
5819+
);
5820+
const artifacts = yield* buildAgentMessageArtifacts(context, message, true);
5821+
yield* emitProviderEvent({
5822+
type: "node.updated",
5823+
driver: CODEX_PROVIDER,
5824+
node: artifacts.node,
5825+
});
5826+
yield* emitProviderEvent({
5827+
type: "message.updated",
5828+
driver: CODEX_PROVIDER,
5829+
message: artifacts.message,
5830+
});
5831+
yield* emitProviderEvent({
5832+
type: "turn_item.updated",
5833+
driver: CODEX_PROVIDER,
5834+
turnItem: artifacts.turnItem,
5835+
});
5836+
});
5837+
57665838
const finalizeCodexTurn = (input: {
57675839
readonly context: ActiveCodexTurnContext;
57685840
readonly nativeTurnId: string;
@@ -5821,6 +5893,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
58215893
for (const [key, part] of reasoningParts) {
58225894
if (part.turnId === input.nativeTurnId) reasoningParts.delete(key);
58235895
}
5896+
yield* projectGeneratedImages(input.context, input.nativeTurnId);
58245897
const nativeThreadId = input.context.providerThread.nativeThreadRef?.nativeId;
58255898
// Codex continues an active goal with another turn, so the run stays
58265899
// open and this turn reads as running until that turn starts.
@@ -6050,50 +6123,6 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
60506123
(yield* Ref.get(interruptingNativeTurns)).has(payload.turn.id)
60516124
? "interrupted"
60526125
: 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-
}
60976126
yield* finalizeCodexTurn({
60986127
context,
60996128
nativeTurnId: payload.turn.id,

0 commit comments

Comments
 (0)