Skip to content

Commit 836b86a

Browse files
authored
fix(chat): keep final replies visible after steering (#34501)
1 parent 11f61a5 commit 836b86a

7 files changed

Lines changed: 225 additions & 74 deletions

File tree

‎apps/mobile/src/lib/threadActivity.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,22 @@ describe("buildThreadFeed", () => {
13881388
expect(rows(turn, "zen")).toEqual(["user", "final"]);
13891389
const [steer, answer] = [say("steer", 2, true, true), say("answer", 3)];
13901390
expect(rows([user, steer, answer, final], "zen")).toEqual(["user", "steer", "answer", "final"]);
1391+
const commentary = { ...step, phase: "commentary" as const };
1392+
expect(rows([user, commentary, steer, final], "zen")).toEqual(["user", "steer", "final"]);
1393+
for (const phase of ["commentary", "partial_answer", "final_answer", undefined] as const) {
1394+
const partial = { ...answer, ...(phase === undefined ? {} : { phase }) };
1395+
const interrupted = buildThreadFeed(
1396+
[user, passed, partial, steer, final].map((item, index) => projected(item, index)),
1397+
);
1398+
expect(
1399+
deriveThreadFeedPresentation(interrupted, null, new Set()).some(
1400+
(row) => row.type === "message" && row.message.id === answer.messageId,
1401+
),
1402+
).toBe(phase === "final_answer");
1403+
expect(rows([user, partial, steer, final], "zen").includes("answer")).toBe(
1404+
phase === "final_answer",
1405+
);
1406+
}
13911407
expect(rows([user, steer, step, passed, final], "zen")).toEqual(["user", "steer", "final"]);
13921408
expect(rows([user, steer, answer, step], "zen", running)).toEqual([
13931409
"user",

‎apps/mobile/src/lib/threadActivity.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,13 +1073,33 @@ export function messageFoldRunId(
10731073
return null;
10741074
}
10751075

1076+
function answersBeforeSteers(feed: ReadonlyArray<ThreadFeedEntry>): ReadonlySet<MessageId> {
1077+
const ids = new Set<MessageId>();
1078+
let previous: ThreadFeedEntry | undefined;
1079+
for (const entry of feed) {
1080+
if (
1081+
entry.type === "message" &&
1082+
isSteerMessage(entry.message) &&
1083+
previous?.type === "message" &&
1084+
previous.message.role === "assistant" &&
1085+
previous.message.projectedItem?.item.type === "assistant_message" &&
1086+
previous.message.projectedItem.item.phase === "final_answer"
1087+
) {
1088+
ids.add(previous.message.id);
1089+
}
1090+
previous = entry;
1091+
}
1092+
return ids;
1093+
}
1094+
10761095
function deriveThreadFeedRunFolds(
10771096
feed: ReadonlyArray<ThreadFeedEntry>,
10781097
latestRun: ThreadFeedLatestRun | null,
10791098
runlessWorkActive: boolean,
10801099
feedFailedRunIds: ReadonlySet<RunId>,
10811100
): ReadonlyMap<string, ThreadFeedRunFold> {
10821101
const terminalAssistantMessageIdByRun = new Map<RunId, string>();
1102+
const retainedAnswerIds = answersBeforeSteers(feed);
10831103
const interruptedRunIds = new Set<RunId>();
10841104
const failedRunIds = new Set(feedFailedRunIds);
10851105
const promptIndexByRunId = new Map<RunId, number>();
@@ -1170,6 +1190,7 @@ function deriveThreadFeedRunFolds(
11701190
.filter(
11711191
(entry) =>
11721192
entry.id !== terminalAssistantId &&
1193+
!(entry.type === "message" && retainedAnswerIds.has(entry.message.id)) &&
11731194
entry.type !== "html-render" &&
11741195
entry.type !== "mcp-app" &&
11751196
entry.id !== visibleCompactionId &&
@@ -1432,7 +1453,7 @@ function zenAnswerMessageIds(
14321453
): ReadonlySet<MessageId> {
14331454
const replies: Array<{ readonly message: ThreadFeedMessage; readonly index: number }> = [];
14341455
const lastReplyByResponse = new Map<string, MessageId>();
1435-
const steerAnswers = new Set<MessageId>();
1456+
const steerAnswers = new Set(answersBeforeSteers(feed));
14361457
let userMessages = 0;
14371458
let awaitingReply = false;
14381459
let candidate: MessageId | null = null;

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

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4237,10 +4237,50 @@ describe("CodexAdapterV2 post-settle continuation", () => {
42374237
Effect.scoped(
42384238
Effect.gen(function* () {
42394239
const transcript = finalAnswerTranscript("codex-partial-then-final", [
4240-
{ id: "partial", text: "CODEX_RECOVERY_OK", phase: "partial_answer", streamed },
4241-
{ id: "final", text: "CODEX_RECOVERY_OK", phase: "final_answer", streamed },
4240+
{
4241+
id: "status",
4242+
text: "Checking the files",
4243+
phase: "commentary",
4244+
streamed,
4245+
},
4246+
{
4247+
id: "partial",
4248+
text: "CODEX_RECOVERY_OK",
4249+
phase: "partial_answer",
4250+
streamed,
4251+
},
4252+
{
4253+
id: "final",
4254+
text: "CODEX_RECOVERY_OK",
4255+
phase: "final_answer",
4256+
streamed,
4257+
},
42424258
]);
4243-
const harness = yield* makeCodexReplayHarness(transcript);
4259+
const gates = yield* Effect.forEach(["status", "partial", "final"], (id) =>
4260+
Effect.gen(function* () {
4261+
return {
4262+
id,
4263+
ready: yield* Deferred.make<void>(),
4264+
release: yield* Deferred.make<void>(),
4265+
};
4266+
}),
4267+
);
4268+
const harness = yield* makeCodexReplayHarness(
4269+
transcript,
4270+
undefined,
4271+
undefined,
4272+
undefined,
4273+
(entry) => {
4274+
const gate = streamed
4275+
? gates.find((gate) => entry.label === `item/completed/${gate.id}`)
4276+
: undefined;
4277+
return gate === undefined
4278+
? Effect.void
4279+
: Deferred.succeed(gate.ready, undefined).pipe(
4280+
Effect.andThen(Deferred.await(gate.release)),
4281+
);
4282+
},
4283+
);
42444284
const now = yield* DateTime.now;
42454285
yield* harness.runtime.startTurn(
42464286
makeCodexTestTurnInput({
@@ -4251,11 +4291,41 @@ describe("CodexAdapterV2 post-settle continuation", () => {
42514291
text: "Reply with the requested recovery marker.",
42524292
}),
42534293
);
4294+
if (streamed) {
4295+
for (const gate of gates) {
4296+
yield* Deferred.await(gate.ready);
4297+
yield* TestClock.adjust("50 millis");
4298+
yield* harness.until(() =>
4299+
harness.events.some(
4300+
(event) =>
4301+
event.type === "turn_item.updated" &&
4302+
event.turnItem.type === "assistant_message" &&
4303+
event.turnItem.nativeItemRef?.nativeId === gate.id &&
4304+
event.turnItem.streaming,
4305+
),
4306+
);
4307+
yield* Deferred.succeed(gate.release, undefined);
4308+
}
4309+
}
42544310
yield* harness.awaitTerminal(1);
42554311
assert.deepEqual(
42564312
assistantMessages(harness.events).map((event) => event.message.text),
4257-
["CODEX_RECOVERY_OK", "CODEX_RECOVERY_OK"],
4313+
["Checking the files", "CODEX_RECOVERY_OK", "CODEX_RECOVERY_OK"],
4314+
);
4315+
const updates = harness.events.flatMap((event) =>
4316+
event.type === "turn_item.updated" && event.turnItem.type === "assistant_message"
4317+
? [event.turnItem]
4318+
: [],
42584319
);
4320+
for (const [text, phase] of [
4321+
["Checking the files", "commentary"],
4322+
["CODEX_RECOVERY_OK", "partial_answer"],
4323+
["CODEX_RECOVERY_OK", "final_answer"],
4324+
]) {
4325+
const matching = updates.filter((item) => item.text === text && item.phase === phase);
4326+
assert.isTrue(matching.some((item) => !item.streaming));
4327+
if (streamed) assert.isTrue(matching.some((item) => item.streaming));
4328+
}
42594329
}).pipe(
42604330
Effect.provide(
42614331
Layer.mergeAll(

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,7 @@ interface ActiveCodexTurnContext {
12231223
readonly runningCompactionItemIds: Set<string>;
12241224
// Modern approval requests omit paths; keep the kinds without retaining patch bodies.
12251225
readonly fileChangeKindsByItem: Map<string, CodexFileChangeKinds>;
1226+
readonly messagePhases: Map<string, "commentary" | "partial_answer" | "final_answer">;
12261227
readonly itemPositions: Map<
12271228
string,
12281229
{ readonly ordinal: number; readonly startedAt: DateTime.Utc }
@@ -2207,6 +2208,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
22072208
startedAt: input.startedAt,
22082209
itemPositions: new Map(),
22092210
fileChangeKindsByItem: new Map(),
2211+
messagePhases: new Map(),
22102212
runningCompactionItemIds: new Set(),
22112213
};
22122214
const nativeThreadId = input.turnInput.providerThread.nativeThreadRef?.nativeId;
@@ -2813,6 +2815,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
28132815
startedAt: turn.startedAt,
28142816
itemPositions: new Map(),
28152817
fileChangeKindsByItem: new Map(),
2818+
messagePhases: new Map(),
28162819
runningCompactionItemIds: new Set(),
28172820
};
28182821
beginTurnTokenUsage(activeContext);
@@ -3490,6 +3493,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
34903493
completedAt,
34913494
updatedAt,
34923495
type: "assistant_message",
3496+
...(context.messagePhases.has(item.id)
3497+
? { phase: context.messagePhases.get(item.id) }
3498+
: {}),
34933499
messageId,
34943500
text: item.text,
34953501
streaming: !completed,
@@ -5054,6 +5060,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
50545060
}
50555061

50565062
if (payload.item.type === "agentMessage") {
5063+
if (payload.item.phase != null)
5064+
context.messagePhases.set(payload.item.id, payload.item.phase);
50575065
if (payload.item.phase == null || payload.item.phase === "final_answer") {
50585066
yield* Ref.update(finalAnswerItemIdsByTurn, (current) => {
50595067
const updated = new Map(current);
@@ -5442,6 +5450,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
54425450
return;
54435451
}
54445452

5453+
if (payload.item.phase != null)
5454+
context.messagePhases.set(payload.item.id, payload.item.phase);
54455455
const finalAnswer = payload.item.phase == null || payload.item.phase === "final_answer";
54465456
if (finalAnswer) {
54475457
yield* Ref.update(finalAnswerItemIdsByTurn, (current) => {
@@ -6913,6 +6923,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
69136923
startedAt: now,
69146924
itemPositions: new Map(),
69156925
fileChangeKindsByItem: new Map(),
6926+
messagePhases: new Map(),
69166927
runningCompactionItemIds: new Set(),
69176928
};
69186929
const providerTurn = {

‎apps/web/src/components/chat/MessagesTimeline.logic.test.ts‎

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3347,6 +3347,7 @@ describe("v2 run and attempt history", () => {
33473347
id: string,
33483348
attempt: typeof supersededAttempt | typeof nextAttempt,
33493349
completedSecond: number,
3350+
phase?: "commentary" | "partial_answer" | "final_answer",
33503351
): TimelineEntry => ({
33513352
id,
33523353
kind: "message",
@@ -3364,6 +3365,7 @@ describe("v2 run and attempt history", () => {
33643365
projectedItem: {
33653366
item: {
33663367
type: "assistant_message",
3368+
...(phase === undefined ? {} : { phase }),
33673369
status: "completed",
33683370
completedAt: DateTime.makeUnsafe(at(completedSecond)),
33693371
},
@@ -3372,51 +3374,58 @@ describe("v2 run and attempt history", () => {
33723374
const rowsFor = (
33733375
replyCompletedSecond: number,
33743376
runStatus: "running" | "completed" = "completed",
3377+
toolCallVisibility: "showAll" | "zen" = "showAll",
3378+
phase?: "commentary" | "partial_answer" | "final_answer",
3379+
nativeSteer = false,
33753380
) =>
33763381
deriveMessagesTimelineRows({
3377-
timelineEntries: [
3378-
{
3379-
id: "initial-user-entry",
3380-
kind: "message",
3381-
createdAt: at(0),
3382-
attempt: supersededAttempt,
3383-
message: {
3384-
id: MessageId.make("initial-user"),
3385-
role: "user",
3386-
text: "Build it",
3387-
runId,
3388-
inputIntent: "turn_start",
3382+
toolCallVisibility,
3383+
timelineEntries: (
3384+
[
3385+
{
3386+
id: "initial-user-entry",
3387+
kind: "message",
33893388
createdAt: at(0),
3390-
updatedAt: at(0),
3391-
streaming: false,
3389+
attempt: supersededAttempt,
3390+
message: {
3391+
id: MessageId.make("initial-user"),
3392+
role: "user",
3393+
text: "Build it",
3394+
runId,
3395+
inputIntent: "turn_start",
3396+
createdAt: at(0),
3397+
updatedAt: at(0),
3398+
streaming: false,
3399+
},
33923400
},
3393-
},
3394-
{
3395-
id: "old-work-entry",
3396-
kind: "work",
3397-
createdAt: at(1),
3398-
attempt: supersededAttempt,
3399-
entry: { id: "old-work", createdAt: at(1), runId, label: "Read", tone: "tool" },
3400-
},
3401-
assistantEntry("old-reply-entry", supersededAttempt, replyCompletedSecond),
3402-
{
3403-
id: "steer-user-entry",
3404-
kind: "message",
3405-
createdAt: at(10),
3406-
attempt: nextAttempt,
3407-
message: {
3408-
id: MessageId.make("steer-user"),
3409-
role: "user",
3410-
text: "What are you working on?",
3411-
runId,
3412-
inputIntent: "steer",
3401+
{
3402+
id: "old-work-entry",
3403+
kind: "work",
3404+
createdAt: at(1),
3405+
attempt: supersededAttempt,
3406+
entry: { id: "old-work", createdAt: at(1), runId, label: "Read", tone: "tool" },
3407+
},
3408+
assistantEntry("status-entry", supersededAttempt, 1, "commentary"),
3409+
assistantEntry("old-reply-entry", supersededAttempt, replyCompletedSecond, phase),
3410+
{
3411+
id: "steer-user-entry",
3412+
kind: "message",
34133413
createdAt: at(10),
3414-
updatedAt: at(10),
3415-
streaming: false,
3414+
attempt: nextAttempt,
3415+
message: {
3416+
id: MessageId.make("steer-user"),
3417+
role: "user",
3418+
text: "What are you working on?",
3419+
runId,
3420+
inputIntent: "steer",
3421+
createdAt: at(10),
3422+
updatedAt: at(10),
3423+
streaming: false,
3424+
},
34163425
},
3417-
},
3418-
assistantEntry("new-reply-entry", nextAttempt, 14),
3419-
],
3426+
assistantEntry("new-reply-entry", nextAttempt, 14),
3427+
] satisfies ReadonlyArray<TimelineEntry>
3428+
).map((entry) => (nativeSteer ? { ...entry, attempt: nextAttempt } : entry)),
34203429
latestRun: {
34213430
runId,
34223431
status: runStatus,
@@ -3428,23 +3437,33 @@ describe("v2 run and attempt history", () => {
34283437
supportsConversationRollback: false,
34293438
});
34303439

3431-
it.each(["running", "completed"] as const)(
3432-
"keeps a reply that finished before the steer visible while the run is %s",
3433-
(runStatus) => {
3434-
const rows = rowsFor(4, runStatus);
3435-
expect(rows.some((row) => row.kind === "attempt-fold")).toBe(false);
3436-
expect(rows.map((row) => row.id)).toContain("old-reply-entry");
3440+
it.each([
3441+
["final_answer", 4, true],
3442+
["final_answer", 11, true],
3443+
["commentary", 4, false],
3444+
["commentary", 11, false],
3445+
["partial_answer", 11, false],
3446+
[undefined, 4, true],
3447+
[undefined, 11, false],
3448+
] as const)(
3449+
"retains phase %s ending at %s only when it is a final reply: %s",
3450+
(phase, completedSecond, retained) => {
3451+
for (const runStatus of ["running", "completed"] as const) {
3452+
for (const visibility of ["showAll", "zen"] as const) {
3453+
const rows = rowsFor(completedSecond, runStatus, visibility, phase);
3454+
expect(rows.some((row) => row.id === "old-reply-entry")).toBe(retained);
3455+
if (phase !== undefined && runStatus === "completed") {
3456+
const nativeRows = rowsFor(completedSecond, runStatus, visibility, phase, true);
3457+
expect(nativeRows.some((row) => row.id === "old-reply-entry")).toBe(retained);
3458+
expect(nativeRows.some((row) => row.id === "status-entry")).toBe(false);
3459+
}
3460+
if (runStatus === "completed") {
3461+
expect(rows.some((row) => row.id === "status-entry")).toBe(false);
3462+
}
3463+
}
3464+
}
34373465
},
34383466
);
3439-
3440-
it("folds a reply the steer cut off as partial output", () => {
3441-
const rows = rowsFor(11, "running");
3442-
expect(rows.find((row) => row.kind === "attempt-fold")).toMatchObject({
3443-
attemptId: supersededAttempt.id,
3444-
label: "Superseded attempt",
3445-
});
3446-
expect(rows.map((row) => row.id)).not.toContain("old-reply-entry");
3447-
});
34483467
});
34493468

34503469
it("hides the interruption request while keeping intervening work and the result", () => {

0 commit comments

Comments
 (0)