@devries-cameron20 / t3code-1 / commits / 028ed851d5
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): Codex generated images show in chat, including on Android with a Windows host (#19066)

Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Julius Marminge <julius0216@outlook.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

shivam committed Oct 30, 2026 at 22:25 UTC 028ed851d552eda86d8c28a758de3d8157b80d56
9 files changed +583 -1
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+211 -1
@@ -77,6 +77,7 @@ const encodeStringJson = Schema.encodeEffect(Schema.fromJsonString(Schema.String
77 const isProviderAdapterEventStreamError = Schema.is(
78 ProviderAdapter.ProviderAdapterEventStreamError,
79 );
80 +const encodeUnknownJson = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown));
81
82 describe("CodexAdapterV2 context usage", () => {
83 it("uses the current context rather than cumulative processed tokens", () => {
@@ -1086,6 +1087,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
1087 Effect.void,
1088 onRequest: (method: string, params: unknown) => Effect.Effect<void> = () => Effect.void,
1089 readChildMetadata?: Parameters<typeof withCodexReplayChildMetadata>[2],
1090 + beforeEmitInbound?: CodexReplay.CodexAppServerReplayDriver["beforeEmitInbound"],
1091 ) =>
1092 Effect.gen(function* () {
1093 const fileSystem = yield* FileSystem.FileSystem;
@@ -1096,9 +1098,13 @@ describe("CodexAdapterV2 post-settle continuation", () => {
1098 const changes = yield* makeChangeSignal;
1099 const continuationRequests: Array<ProviderContinuationRequests.ProviderContinuationRequest> =
1100 [];
1101 + const driver = yield* CodexReplay.makeReplayDriver(
1102 + transcript,
1103 + beforeEmitInbound === undefined ? {} : { beforeEmitInbound },
1104 + );
1105 const clientFactory: CodexAdapterV2.CodexAppServerClientFactoryShape = {
1106 open: (openInput) =>
1101 - Layer.build(CodexReplay.layerReplay(transcript)).pipe(
1107 + Layer.build(CodexReplay.layerReplayWithDriver(driver)).pipe(
1108 Effect.mapError(
1109 (cause) =>
1110 new ProviderAdapter.ProviderAdapterOpenSessionError({
@@ -3237,6 +3243,210 @@ describe("CodexAdapterV2 post-settle continuation", () => {
3243 });
3244 };
3245
3246 + it.effect.each([
3247 + {
3248 + name: "a text reply gains each saved image once",
3249 + reply: "Done.",
3250 + images: [
3251 + { savedPath: "/tmp/a.png" },
3252 + { savedPath: "/tmp/a.png" },
3253 + { savedPath: "/tmp/failed.png", status: "failed" },
3254 + {
3255 + savedPath: "/tmp/limited.png",
3256 + failure: { type: "usageLimitExceeded", limitId: "image-generation" },
3257 + },
3258 + { savedPath: null },
3259 + ],
3260 + expected: "Done.\n\n![](</tmp/a.png>)",
3261 + },
3262 + {
3263 + name: "an image-only result becomes the reply",
3264 + reply: null,
3265 + images: [{ savedPath: "/tmp/a.png" }],
3266 + expected: "![](</tmp/a.png>)",
3267 + },
3268 + ])("projects generated images: $name", ({ reply, images, expected }) =>
3269 + Effect.gen(function* () {
3270 + const scenario = "codex-generated-images";
3271 + const result = "aW1hZ2UtYnl0ZXMtbXVzdC1uZXZlci1yZWFjaC1jbGllbnRz";
3272 + const base = finalAnswerTranscript(
3273 + scenario,
3274 + reply === null ? [] : [{ id: "answer", text: reply }],
3275 + );
3276 + const harness = yield* makeCodexReplayHarness(
3277 + makeCodexReplayTranscript({
3278 + scenario,
3279 + entries: [
3280 + ...base.entries.slice(0, -1),
3281 + ...images.map((image, index) => ({
3282 + type: "emit_inbound" as const,
3283 + frame: {
3284 + method: "item/completed",
3285 + params: {
3286 + threadId: `native-${scenario}-thread`,
3287 + turnId: `native-${scenario}-turn`,
3288 + item: {
3289 + type: "imageGeneration",
3290 + id: `image-${index}`,
3291 + status: "completed",
3292 + result,
3293 + ...image,
3294 + },
3295 + },
3296 + },
3297 + })),
3298 + ...base.entries.slice(-1),
3299 + ],
3300 + }),
3301 + );
3302 + yield* harness.runtime.startTurn(
3303 + makeCodexTestTurnInput({
3304 + threadId: harness.threadId,
3305 + providerThread: harness.providerThread,
3306 + now: yield* DateTime.now,
3307 + attemptId: RunAttemptId.make("attempt-generated-images"),
3308 + text: "Reply with the requested recovery marker.",
3309 + }),
3310 + );
3311 + yield* harness.firstTerminal;
3312 +
3313 + const messages = assistantMessages(harness.events);
3314 + assert.strictEqual(messages.at(-1)?.message.text, expected);
3315 + assert.strictEqual(messages.at(-1)?.message.streaming, false);
3316 + assert.strictEqual(new Set(messages.map(({ message }) => message.id)).size, 1);
3317 + // Image bytes stay with Codex; clients load the saved file.
3318 + assert.notInclude(encodeUnknownJson(harness.events), result);
3319 + }).pipe(
3320 + Effect.scoped,
3321 + Effect.provide(
3322 + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3323 + ),
3324 + ),
3325 + );
3326 +
3327 + it.effect.each(["complete", "stop"] as const)(
3328 + "retains generating images after settlement until %s",
3329 + (outcome) =>
3330 + Effect.gen(function* () {
3331 + const scenario = "codex-retained-image";
3332 + const base = finalAnswerTranscript(scenario, [{ id: "answer", text: "Done." }]);
3333 + const completions = {
3334 + first: yield* Deferred.make<void>(),
3335 + second: yield* Deferred.make<void>(),
3336 + };
3337 + const projected = yield* Deferred.make<void>();
3338 + const drained = yield* Deferred.make<void>();
3339 + const firstText = "Done.\n\n![](</tmp/first.png>)";
3340 + const imageEvent = (image: "first" | "second", phase: "started" | "completed") => ({
3341 + type: "emit_inbound" as const,
3342 + label: `${image}-${phase}`,
3343 + frame: {
3344 + method: `item/${phase}`,
3345 + params: {
3346 + threadId: `native-${scenario}-thread`,
3347 + turnId: `native-${scenario}-turn`,
3348 + item: {
3349 + type: "imageGeneration",
3350 + id: image,
3351 + status: phase === "started" ? "inProgress" : "completed",
3352 + result: "",
3353 + ...(phase === "completed" ? { savedPath: `/tmp/${image}.png` } : {}),
3354 + },
3355 + },
3356 + },
3357 + });
3358 + const harness = yield* makeCodexReplayHarness(
3359 + makeCodexReplayTranscript({
3360 + scenario,
3361 + entries: [
3362 + ...base.entries.slice(0, -2),
3363 + imageEvent("first", "started"),
3364 + imageEvent("second", "started"),
3365 + ...base.entries.slice(-2),
3366 + imageEvent("first", "completed"),
3367 + imageEvent("second", "completed"),
3368 + ],
3369 + }),
3370 + (event) =>
3371 + event.type === "message.updated" && event.message.text === firstText
3372 + ? Deferred.succeed(projected, undefined)
3373 + : event.type === "background_work.updated" && !event.pending
3374 + ? Deferred.succeed(drained, undefined)
3375 + : Effect.void,
3376 + undefined,
3377 + undefined,
3378 + (entry) =>
3379 + entry.label === "first-completed"
3380 + ? Deferred.await(completions.first)
3381 + : entry.label === "second-completed"
3382 + ? Deferred.await(completions.second)
3383 + : Effect.void,
3384 + );
3385 + yield* harness.runtime.startTurn(
3386 + makeCodexTestTurnInput({
3387 + threadId: harness.threadId,
3388 + providerThread: harness.providerThread,
3389 + now: yield* DateTime.now,
3390 + attemptId: RunAttemptId.make("attempt-retained-image"),
3391 + text: "Reply with the requested recovery marker.",
3392 + }),
3393 + );
3394 + yield* harness.firstTerminal;
3395 + const pending = Effect.all([
3396 + harness.runtime.hasPendingBackgroundWorkForThread!(harness.providerThread),
3397 + harness.hasPendingBackgroundWork,
3398 + ]);
3399 + assert.deepEqual(yield* pending, [true, true]);
3400 + assert.strictEqual(assistantMessages(harness.events).at(-1)?.message.text, "Done.");
3401 + const cleared = () =>
3402 + harness.events.some((e) => e.type === "background_work.updated" && !e.pending);
3403 +
3404 + if (outcome === "complete") {
3405 + yield* Deferred.succeed(completions.first, undefined);
3406 + yield* Deferred.await(projected);
3407 + // The second image is still generating, so the first must not release the turn.
3408 + assert.deepEqual(yield* pending, [true, true]);
3409 + assert.isFalse(cleared());
3410 + yield* Deferred.succeed(completions.second, undefined);
3411 + } else {
3412 + yield* harness.runtime.interruptTurn({
3413 + providerThread: harness.providerThread,
3414 + providerTurnId: harness.terminalEvents()[0]!.providerTurnId,
3415 + requestRuntimeRestart: true,
3416 + });
3417 + }
3418 + yield* Deferred.await(drained);
3419 + assert.deepEqual(yield* pending, [false, false]);
3420 + assert.lengthOf(harness.terminalEvents(), 1);
3421 + // Run ingestion follows these updates, so they must bracket the terminal and the images.
3422 + const indexOf = (predicate: (event: ProviderAdapter.ProviderAdapterV2Event) => boolean) =>
3423 + harness.events.findIndex(predicate);
3424 + const started = indexOf((e) => e.type === "background_work.updated" && e.pending);
3425 + const terminal = indexOf((e) => e.type === "turn.terminal");
3426 + const finished = indexOf((e) => e.type === "background_work.updated" && !e.pending);
3427 + assert.isAtLeast(started, 0);
3428 + assert.isAbove(terminal, started);
3429 + assert.isAbove(finished, terminal);
3430 + if (outcome === "complete") {
3431 + const messages = assistantMessages(harness.events);
3432 + const projectedBoth = indexOf(
3433 + (e) =>
3434 + e.type === "message.updated" &&
3435 + e.message.text === `${firstText}\n\n![](</tmp/second.png>)`,
3436 + );
3437 + assert.isAbove(projectedBoth, terminal);
3438 + assert.isAbove(finished, projectedBoth);
3439 + assert.strictEqual(messages.at(-1)?.message.id, messages[0]?.message.id);
3440 + assert.strictEqual(messages.at(-1)?.message.streaming, false);
3441 + }
3442 + }).pipe(
3443 + Effect.scoped,
3444 + Effect.provide(
3445 + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3446 + ),
3447 + ),
3448 + );
3449 +
3450 it.effect("suppresses a trailing empty final answer after a non-empty final answer", () =>
3451 Effect.scoped(
3452 Effect.gen(function* () {
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+119
@@ -47,6 +47,7 @@ import {
47 dynamicToolTitle,
48 } from "@t3tools/shared/toolActivity";
49 import { getModelSelectionStringOptionValue, modelSelectionsEqual } from "@t3tools/shared/model";
50 +import { appendGeneratedImageEmbeds } from "@t3tools/shared/generatedImageMarkdown";
51 import { resolveSpawnCommand } from "@t3tools/shared/shell";
52 import type {
53 ChatAttachment,
@@ -1842,6 +1843,23 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
1843 const offeredContinuationItemsByTurn = yield* Ref.make(new Map<string, Set<string>>());
1844 const finalAnswerItemIdsByTurn = yield* Ref.make(new Map<string, Set<string>>());
1845 const completedFinalAnswerTextsByTurn = yield* Ref.make(new Map<string, Set<string>>());
1846 + const imageTurns = new Map<
1847 + string,
1848 + {
1849 + readonly running: Set<string>;
1850 + readonly saved: Map<string, string>;
1851 + pending: boolean;
1852 + answer?: { readonly id: string; readonly text: string };
1853 + }
1854 + >();
1855 + const imageTurn = (nativeTurnId: string) => {
1856 + let state = imageTurns.get(nativeTurnId);
1857 + if (state === undefined) {
1858 + state = { running: new Set(), saved: new Map(), pending: false };
1859 + imageTurns.set(nativeTurnId, state);
1860 + }
1861 + return state;
1862 + };
1863 // Serialize native completion and interrupt timeout so only one publishes terminal events.
1864 const turnTerminalizationPermit = yield* Semaphore.make(1);
1865 const goalsByNativeThread = new Map<string, OrchestrationV2ProviderGoal | null>();
@@ -1868,6 +1886,27 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
1886 const emitProviderEvent = (event: ProviderAdapter.ProviderAdapterV2Event) =>
1887 Queue.offer(events, event).pipe(Effect.asVoid);
1888
1889 + const emitImageWorkState =
1890 + /* Provider events include streaming deltas; avoid a span for each update. */
1891 + Effect.fnUntraced(function* (context: ActiveCodexTurnContext, pending: boolean) {
1892 + const state = imageTurns.get(context.nativeTurnId);
1893 + if (state === undefined || state.pending === pending) return;
1894 + state.pending = pending;
1895 + yield* emitProviderEvent({
1896 + type: "background_work.updated",
1897 + driver: CODEX_PROVIDER,
1898 + providerTurnId: context.providerTurnId,
1899 + pending,
1900 + });
1901 + });
1902 +
1903 + const clearImageWork = Effect.fn("CodexAdapterV2.clearImageWork")(function* (
1904 + context: ActiveCodexTurnContext,
1905 + ) {
1906 + yield* emitImageWorkState(context, false);
1907 + imageTurns.delete(context.nativeTurnId);
1908 + });
1909 +
1910 // Setting a goal passes through cleared and paused states; publish only the result.
1911 const goalUpdatesHeld = new Set<string>();
1912 const emitGoalUpdate = Effect.fnUntraced(function* (nativeThreadId: string) {
@@ -2136,6 +2175,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
2175 if ((yield* Ref.get(pendingMcpAppCaptures)).has(nativeTurnId)) {
2176 return true;
2177 }
2178 + const images = imageTurns.get(nativeTurnId);
2179 + if (images !== undefined && (images.running.size > 0 || images.saved.size > 0)) {
2180 + return true;
2181 + }
2182 const tools = (yield* Ref.get(runningDynamicToolsByTurn)).get(nativeTurnId);
2183 if (tools === undefined) {
2184 return false;
@@ -2178,6 +2221,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
2221 updated.delete(nativeTurnId);
2222 return updated;
2223 });
2224 + imageTurns.delete(nativeTurnId);
2225 yield* Ref.update(finalAnswerItemIdsByTurn, (current) => {
2226 if (!current.has(nativeTurnId)) {
2227 return current;
@@ -3389,6 +3433,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
3433 turnItem: artifacts.turnItem,
3434 });
3435 if (finalAnswerItem && update.completed) {
3436 + imageTurn(update.turnId).answer = { id: update.itemId, text: update.text };
3437 yield* Ref.update(completedFinalAnswerTextsByTurn, (current) => {
3438 const updated = new Map(current);
3439 const texts = new Set(updated.get(update.turnId) ?? []);
@@ -4714,6 +4759,14 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
4759 return;
4760 }
4761
4762 + if (payload.item.type === "imageGeneration") {
4763 + if (!(yield* Ref.get(interruptingNativeTurns)).has(payload.turnId)) {
4764 + imageTurn(payload.turnId).running.add(payload.item.id);
4765 + yield* emitImageWorkState(context, true);
4766 + }
4767 + return;
4768 + }
4769 +
4770 if (payload.item.type === "commandExecution") {
4771 if (!codexItemStatus(payload.item.status).completed) {
4772 yield* trackRunningCommandItem(payload.turnId, {
@@ -4992,6 +5045,25 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
5045 return;
5046 }
5047
5048 + if (payload.item.type === "imageGeneration") {
5049 + if ((yield* Ref.get(interruptingNativeTurns)).has(payload.turnId)) return;
5050 + const { id, savedPath, status, failure } = payload.item;
5051 + const state = imageTurn(payload.turnId);
5052 + if (savedPath?.trim() && status === "completed" && failure == null) {
5053 + state.saved.set(id, savedPath);
5054 + }
5055 + // Idle release runs concurrently, so keep this work pending through projection.
5056 + if (settled) {
5057 + yield* projectGeneratedImages(context, payload.turnId);
5058 + }
5059 + state.running.delete(id);
5060 + yield* emitImageWorkState(context, state.running.size > 0 || state.saved.size > 0);
5061 + if (settled) {
5062 + yield* releaseSettledTurnIfIdle(payload.turnId);
5063 + }
5064 + return;
5065 + }
5066 +
5067 if (payload.item.type !== "agentMessage") {
5068 return;
5069 }
@@ -5846,6 +5918,39 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
5918 },
5919 );
5920
5921 + const projectGeneratedImages = Effect.fn("CodexAdapterV2.projectGeneratedImages")(
5922 + function* (context: ActiveCodexTurnContext, nativeTurnId: string) {
5923 + const state = imageTurns.get(nativeTurnId);
5924 + const firstImageItemId = state?.saved.keys().next().value;
5925 + if (state === undefined || firstImageItemId === undefined) return;
5926 + const finalAnswer = state.answer;
5927 + const text = appendGeneratedImageEmbeds(finalAnswer?.text ?? "", state.saved.values());
5928 + if (text === (finalAnswer?.text ?? "")) {
5929 + state.saved.clear();
5930 + return;
5931 + }
5932 + const message = { id: finalAnswer?.id ?? firstImageItemId, text };
5933 + state.answer = message;
5934 + const artifacts = yield* buildAgentMessageArtifacts(context, message, true);
5935 + yield* emitProviderEvent({
5936 + type: "node.updated",
5937 + driver: CODEX_PROVIDER,
5938 + node: artifacts.node,
5939 + });
5940 + yield* emitProviderEvent({
5941 + type: "message.updated",
5942 + driver: CODEX_PROVIDER,
5943 + message: artifacts.message,
5944 + });
5945 + yield* emitProviderEvent({
5946 + type: "turn_item.updated",
5947 + driver: CODEX_PROVIDER,
5948 + turnItem: artifacts.turnItem,
5949 + });
5950 + state.saved.clear();
5951 + },
5952 + );
5953 +
5954 const finalizeCodexTurn = (input: {
5955 readonly context: ActiveCodexTurnContext;
5956 readonly nativeTurnId: string;
@@ -5919,6 +6024,15 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6024 input.completedAt,
6025 );
6026 }
6027 + yield* projectGeneratedImages(input.context, input.nativeTurnId);
6028 + if (input.status !== "completed") {
6029 + yield* clearImageWork(input.context);
6030 + } else {
6031 + const images = imageTurns.get(input.nativeTurnId);
6032 + if (images !== undefined) {
6033 + yield* emitImageWorkState(input.context, images.running.size > 0);
6034 + }
6035 + }
6036 const nativeThreadId = input.context.providerThread.nativeThreadRef?.nativeId;
6037 const activation =
6038 nativeThreadId == null ? undefined : goalActivations.get(nativeThreadId);
@@ -6139,6 +6253,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6253 updated.delete(input.nativeTurnId);
6254 return updated;
6255 });
6256 + imageTurns.delete(input.nativeTurnId);
6257 yield* Ref.update(finalAnswerItemIdsByTurn, (current) => {
6258 if (!current.has(input.nativeTurnId)) {
6259 return current;
@@ -6748,6 +6863,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6863 if ((yield* Ref.get(pendingMcpAppCaptures)).size > 0) {
6864 return true;
6865 }
6866 + for (const state of imageTurns.values()) {
6867 + if (state.running.size > 0 || state.saved.size > 0) return true;
6868 + }
6869 for (const subagent of (yield* Ref.get(subagentThreads)).values()) {
6870 if (subagent.task.status === "running") {
6871 return true;
@@ -7418,6 +7536,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7536 updated.delete(context.nativeTurnId);
7537 return updated;
7538 });
7539 + yield* clearImageWork(context);
7540 yield* releaseSettledTurnIfIdle(context.nativeTurnId);
7541 }
7542 }),
apps/server/src/orchestration-v2/ProviderEventIngestor.ts
+2
@@ -700,6 +700,8 @@ export const layer: Layer.Layer<
700 }),
701 ];
702 }
703 + case "background_work.updated":
704 + return [];
705 case "command_output.delta":
706 // Live output is routed to the command output hub before ingestion.
707 return [];
apps/server/src/orchestration-v2/RunExecutionService.test.ts
+74
@@ -1780,6 +1780,74 @@ it.effect("ends a nested native subagent and its thread when the root run is int
1780 }),
1781 );
1782
1783 +it.effect("keeps ingesting until queued background work clears after the probe goes idle", () =>
1784 + Effect.gen(function* () {
1785 + // The adapter's probe runs ahead of its event queue, so only the queued update may release.
1786 + // A child transferred to another run takes its pending work with it.
1787 + const pending = yield* Ref.make(true);
1788 + const consumed = yield* Deferred.make<void>();
1789 + const result = yield* Effect.raceFirst(
1790 + runBackgroundItemScenario(
1791 + "queued-background-work",
1792 + (ids) => {
1793 + const childProviderThreadId = ProviderThreadId.make(`${ids.providerThreadId}:child`);
1794 + const childProviderTurnId = ProviderTurnId.make(`${ids.rootProviderTurnId}:child`);
1795 + const running = subagentEvent(ids, "running");
1796 + if (running.type !== "subagent.updated") throw new Error("Missing subagent");
1797 + const linked = {
1798 + ...running,
1799 + subagent: {
1800 + ...running.subagent,
1801 + origin: "provider_native" as const,
1802 + childThreadId: ids.childThreadId,
1803 + providerThreadId: childProviderThreadId,
1804 + },
1805 + };
1806 + const backgroundWork = (providerTurnId: ProviderTurnId, pending: boolean) =>
1807 + ({
1808 + type: "background_work.updated",
1809 + driver,
1810 + providerTurnId,
1811 + pending,
1812 + }) satisfies ProviderAdapter.ProviderAdapterV2Event;
1813 + return [
1814 + childThreadCreatedEvent(ids),
1815 + linked,
1816 + {
1817 + type: "provider_turn.updated",
1818 + driver,
1819 + threadId: ids.childThreadId,
1820 + providerTurn: {
1821 + id: childProviderTurnId,
1822 + providerThreadId: childProviderThreadId,
1823 + runAttemptId: null,
1824 + status: "completed",
1825 + },
1826 + } as ProviderAdapter.ProviderAdapterV2Event,
1827 + backgroundWork(childProviderTurnId, true),
1828 + backgroundWork(ids.rootProviderTurnId, true),
1829 + rootTerminalEvent(ids, "completed"),
1830 + { ...linked, subagent: { ...linked.subagent, runId: RunId.make("run:replacement") } },
1831 + backgroundTurnItemEvent(ids, "dynamic_tool", "completed", 1),
1832 + backgroundWork(ids.rootProviderTurnId, false),
1833 + ];
1834 + },
1835 + {
1836 + keepEventStreamOpen: true,
1837 + hasPendingBackgroundWorkForThread: () => Ref.get(pending),
1838 + onFinalized: () => Ref.set(pending, false),
1839 + onEventsConsumed: Deferred.succeed(consumed, undefined).pipe(Effect.asVoid),
1840 + },
1841 + ).pipe(Effect.asSome),
1842 + Deferred.await(consumed).pipe(Effect.as(Option.none())),
1843 + );
1844 + assert.deepEqual(
1845 + result,
1846 + Option.some(["subagent:running", "root-finalized", "turn_item:completed"]),
1847 + );
1848 + }),
1849 +);
1850 +
1851 it.effect("keeps ingesting a child thread's late background item completion", () =>
1852 Effect.gen(function* () {
1853 const observed = yield* runBackgroundItemScenario("bg-child-item", (ids) => [
@@ -3528,6 +3596,9 @@ function runBackgroundItemScenario(
3596 makeEvents: (ids: BackgroundScenarioIds) => ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>,
3597 options?: {
3598 readonly keepEventStreamOpen?: boolean;
3599 + readonly hasPendingBackgroundWorkForThread?: (
3600 + providerThread: OrchestrationV2ProviderThread,
3601 + ) => Effect.Effect<boolean>;
3602 readonly loadInheritedBackgroundTurnItems?: () => Effect.Effect<
3603 ReadonlyArray<{ readonly id: TurnItemId; readonly runId: RunId }>
3604 >;
@@ -3605,6 +3676,9 @@ function runBackgroundItemScenario(
3676 providerSessionId: ProviderSessionId.make(`session:${key}`),
3677 session: {
3678 events: Stream.empty,
3679 + ...(options?.hasPendingBackgroundWorkForThread === undefined
3680 + ? {}
3681 + : { hasPendingBackgroundWorkForThread: options.hasPendingBackgroundWorkForThread }),
3682 subscribeEvents: Effect.gen(function* () {
3683 yield* options?.onSubscribe ?? Effect.void;
3684 const events = Stream.fromIterable(makeEvents(ids)).pipe(
apps/server/src/orchestration-v2/RunExecutionService.ts
+18
@@ -507,6 +507,8 @@ export function routeProviderEvent(
507 state.ownedProviderTurns.has(event.runtimeRequest.providerTurnId)),
508 state,
509 ];
510 + case "background_work.updated":
511 + return [state.ownedProviderTurns.has(event.providerTurnId), state];
512 case "command_output.delta":
513 // The session manager hands live output to the command output hub; runs never see it.
514 return [false, state];
@@ -1030,6 +1032,7 @@ export const layer: Layer.Layer<
1032 const rootTerminalSeen = yield* Ref.make(false);
1033 const rootRunFinalized = yield* Ref.make(false);
1034 const providerThreadOwnerLost = yield* Ref.make(false);
1035 + const pendingBackgroundProviderTurns = new Set<ProviderTurnId>();
1036 const activeChildProviderTurns = yield* Ref.make<ReadonlySet<ProviderTurnId>>(new Set());
1037 const activeChildSubagents = yield* Ref.make<ReadonlyMap<NodeId, ThreadId>>(new Map());
1038 const activeBackgroundTurnItems = yield* Ref.make<
@@ -1231,6 +1234,9 @@ export const layer: Layer.Layer<
1234 ) {
1235 return true;
1236 }
1237 + if (pendingBackgroundProviderTurns.size > 0) {
1238 + return false;
1239 + }
1240 const childProviderTurns = yield* Ref.get(activeChildProviderTurns);
1241 if (childProviderTurns.size > 0) {
1242 return false;
@@ -1350,6 +1356,11 @@ export const layer: Layer.Layer<
1356 (current) =>
1357 new Set([...current].filter((id) => routing.ownedProviderTurns.has(id))),
1358 );
1359 + for (const id of pendingBackgroundProviderTurns) {
1360 + if (!routing.ownedProviderTurns.has(id)) {
1361 + pendingBackgroundProviderTurns.delete(id);
1362 + }
1363 + }
1364 yield* Ref.update(
1365 activeBackgroundTurnItems,
1366 (current) => new Set([...current].filter((id) => !detached.itemIds.has(id))),
@@ -1454,6 +1465,13 @@ export const layer: Layer.Layer<
1465 yield* Ref.set(rootTerminalSeen, true);
1466 yield* finalizeRootRun(event);
1467 }
1468 + if (event.type === "background_work.updated") {
1469 + if (event.pending) {
1470 + pendingBackgroundProviderTurns.add(event.providerTurnId);
1471 + } else {
1472 + pendingBackgroundProviderTurns.delete(event.providerTurnId);
1473 + }
1474 + }
1475 yield* trackChildLifecycle(event, deliveredEvent !== null);
1476 }),
1477 ),
packages/provider-core/src/server/ProviderAdapter.ts
+8
@@ -119,6 +119,14 @@ export const ProviderAdapterV2Event = Schema.Union([
119 driver: ProviderDriverKind,
120 turnItem: OrchestrationV2TurnItem,
121 }),
122 + // Start before the root terminal; clear only after the final covered projection.
123 + // Consumer lifetime cannot depend on a producer probe that runs ahead of its queue.
124 + Schema.Struct({
125 + type: Schema.Literal("background_work.updated"),
126 + driver: ProviderDriverKind,
127 + providerTurnId: ProviderTurnId,
128 + pending: Schema.Boolean,
129 + }),
130 /** Unpersisted deltas go only to clients showing the row; final output arrives in turn_item.updated. */
131 Schema.Struct({
132 type: Schema.Literal("command_output.delta"),
packages/shared/package.json
+4
@@ -463,6 +463,10 @@
463 "types": "./src/markdownWindowsPaths.ts",
464 "import": "./src/markdownWindowsPaths.ts"
465 },
466 + "./generatedImageMarkdown": {
467 + "types": "./src/generatedImageMarkdown.ts",
468 + "import": "./src/generatedImageMarkdown.ts"
469 + },
470 "./markdownPipeline": {
471 "types": "./src/markdownPipeline.ts",
472 "import": "./src/markdownPipeline.ts"
packages/shared/src/generatedImageMarkdown.test.ts new
+78
@@ -0,0 +1,78 @@
1 +import { expect, it } from "vite-plus/test";
2 +
3 +import { appendGeneratedImageEmbeds } from "./generatedImageMarkdown.ts";
4 +
5 +it.each([
6 + {
7 + name: "appends each new image once",
8 + text: "Done.",
9 + saved: ["/tmp/a.png", "/tmp/b.png", "/tmp/a.png"],
10 + expected: "Done.\n\n![](</tmp/a.png>)\n\n![](</tmp/b.png>)",
11 + },
12 + {
13 + name: "skips images the reply already shows",
14 + text: String.raw`![A](</tmp/a b.png>) ![B](c:\Users\me\.codex\b.png) ![C][a\*b] ![D][unc]
15 +
16 +[a\*b]: c:\Users\me\.codex\c.png
17 +[unc]: <\\HOST\Share\d.png>
18 +
19 +![E](file://host/share/e.png)`,
20 + saved: [
21 + "/tmp/a b.png",
22 + String.raw`C:\Users\Me\.codex\b.png`,
23 + String.raw`C:\Users\Me\.codex\c.png`,
24 + String.raw`\\host\share\d.png`,
25 + String.raw`\\HOST\SHARE\e.png`,
26 + "/tmp/C.png",
27 + ],
28 + expected: String.raw`![A](</tmp/a b.png>) ![B](c:\Users\me\.codex\b.png) ![C][a\*b] ![D][unc]
29 +
30 +[a\*b]: c:\Users\me\.codex\c.png
31 +[unc]: <\\HOST\Share\d.png>
32 +
33 +![E](file://host/share/e.png)
34 +
35 +![](</tmp/C.png>)`,
36 + },
37 + {
38 + name: "links, code and escaped embeds still need a preview",
39 + text: "[open](/tmp/a.png) `![](/tmp/a.png)`\n\n```md\n![](/tmp/a.png)\n```\n\\![](/tmp/a.png)",
40 + saved: ["/tmp/a.png"],
41 + expected:
42 + "[open](/tmp/a.png) `![](/tmp/a.png)`\n\n```md\n![](/tmp/a.png)\n```\n\\![](/tmp/a.png)\n\n![](</tmp/a.png>)",
43 + },
44 + {
45 + name: "an unclosed trailing fence is closed first",
46 + text: 'Created.\n\n~~~js\nconsole.log("done");',
47 + saved: ["/tmp/a.png"],
48 + expected: 'Created.\n\n~~~js\nconsole.log("done");\n~~~\n\n![](</tmp/a.png>)',
49 + },
50 + {
51 + name: "an unclosed trailing HTML comment is closed first",
52 + text: "<!--\n![](</tmp/a.png>)",
53 + saved: ["/tmp/a.png"],
54 + expected: "<!--\n![](</tmp/a.png>)\n-->\n\n![](</tmp/a.png>)",
55 + },
56 + {
57 + name: "an unclosed trailing raw HTML block is closed first",
58 + text: "<pre>\nlog",
59 + saved: ["/tmp/a.png"],
60 + expected: "<pre>\nlog\n</pre>\n\n![](</tmp/a.png>)",
61 + },
62 + {
63 + name: "Windows, UNC and reserved characters keep their path",
64 + text: "",
65 + saved: [
66 + String.raw`C:\Users\me\a b#1.png`,
67 + String.raw`\\host\share\a.png`,
68 + "/tmp/a#b?100%25&copy;\\!\n<x>.png",
69 + ],
70 + expected:
71 + "![](<C:/Users/me/a b%231.png>)\n\n![](<file://host/share/a.png>)\n\n![](</tmp/a%23b%3F100%2525%26copy;%5C!%0A\\<x\\>.png>)",
72 + },
73 +])("generated image embeds: $name", ({ text, saved, expected }) => {
74 + const result = appendGeneratedImageEmbeds(text, saved);
75 + expect(result).toBe(expected);
76 + // Clients read every appended embed back as its saved file.
77 + expect(appendGeneratedImageEmbeds(result, saved)).toBe(result);
78 +});
packages/shared/src/generatedImageMarkdown.ts new
+69
@@ -0,0 +1,69 @@
1 +import remarkParse from "remark-parse";
2 +import { unified } from "unified";
3 +
4 +import { markdownDestinationHostPath } from "./markdownLinks.ts";
5 +import { chatMarkdownImageSources } from "./markdownPipeline.ts";
6 +import { isUncPath, isWindowsDrivePath, normalizeProjectPathForComparison } from "./path.ts";
7 +
8 +const parser = unified().use(remarkParse);
9 +
10 +function imageEmbed(path: string): string {
11 + // Forward slashes read the same in every client parser; UNC paths also need file: to
12 + // distinguish them from protocol-relative web URLs.
13 + const destination = isWindowsDrivePath(path)
14 + ? path.replaceAll("\\", "/")
15 + : isUncPath(path)
16 + ? `file:${path.replaceAll("\\", "/")}`
17 + : path;
18 + // Clients split `?`/`#`, decode `%` and `&` entities, and read POSIX backslashes as escapes;
19 + // line breaks end an angle destination.
20 + const escaped = destination.replace(/[%?#&\\\r\n]/g, encodeURIComponent).replace(/[<>]/g, "\\$&");
21 + return `![](<${escaped}>)`;
22 +}
23 +
24 +// An unclosed fence or raw HTML block swallows the rest of the reply. Only a root-level block
25 +// can swallow appended embeds; nested ones end with their container.
26 +function unclosedBlockEnd(text: string): string | undefined {
27 + const last = parser.parse(text).children.at(-1);
28 + const source = text.slice(last?.position?.start.offset, last?.position?.end.offset);
29 + if (last?.type === "html") {
30 + const tag = /^ {0,3}<(script|pre|style|textarea)(?=[\s>]|$)/i.exec(source)?.[1];
31 + if (tag !== undefined) {
32 + return /<\/(?:script|pre|style|textarea)>/i.test(source) ? undefined : `</${tag}>`;
33 + }
34 + return /^ {0,3}<!--/.test(source) && !source.includes("-->") ? "-->" : undefined;
35 + }
36 + if (last?.type !== "code") return undefined;
37 + const fence = /^ {0,3}(`{3,}|~{3,})/.exec(source)?.[1];
38 + if (fence === undefined) return undefined;
39 + const lines = source.split(/\r\n|\r|\n/);
40 + const closing = /^ {0,3}(`{3,}|~{3,})[\t ]*$/.exec(lines.at(-1) ?? "")?.[1];
41 + const closed =
42 + lines.length > 1 &&
43 + closing !== undefined &&
44 + closing[0] === fence[0] &&
45 + closing.length >= fence.length;
46 + return closed ? undefined : fence;
47 +}
48 +
49 +/** Appends an embed for each saved image the reply does not already show. */
50 +export function appendGeneratedImageEmbeds(text: string, savedPaths: Iterable<string>): string {
51 + const shown = new Set(
52 + chatMarkdownImageSources(text).flatMap((source) => {
53 + const path = markdownDestinationHostPath(source);
54 + return path === null ? [] : [normalizeProjectPathForComparison(path)];
55 + }),
56 + );
57 + const missing = new Map<string, string>();
58 + for (const path of savedPaths) {
59 + const key = normalizeProjectPathForComparison(path);
60 + if (!shown.has(key) && !missing.has(key)) missing.set(key, path);
61 + }
62 + if (missing.size === 0) return text;
63 + const end = unclosedBlockEnd(text);
64 + let result = end === undefined ? text : `${text}${/[\r\n]$/.test(text) ? "" : "\n"}${end}`;
65 + for (const path of missing.values()) {
66 + result += `${result.length > 0 ? "\n\n" : ""}${imageEmbed(path)}`;
67 + }
68 + return result;
69 +}