fix(web): keep codex restart responses continuous (#9560)

Co-authored-by: maria-rcks <[email protected]>
This commit is contained in:
mariaandmaria-rcks authored and GitHub committed 2026-09-03 23:00:04 -04:00
1 parent fee2e0ff81
commit 65f1839ae8
2 files changed
+159 -18

No files matched your search

@@ -1326,6 +1326,111 @@ describe("deriveMessagesTimelineRows", () => {
]);
});
it("keeps a promptless restart in one active visual response", () => {
const rows = deriveMessagesTimelineRows({
timelineEntries: [
{
id: "user-entry",
kind: "message",
createdAt: "2026-01-01T00:00:00Z",
message: {
id: "user-1" as never,
role: "user",
text: "keep going",
turnId: null,
createdAt: "2026-01-01T00:00:00Z",
updatedAt: "2026-01-01T00:00:00Z",
streaming: false,
},
},
{
id: "old-work-entry",
kind: "work",
createdAt: "2026-01-01T00:00:05Z",
entry: {
id: "old-work",
createdAt: "2026-01-01T00:00:05Z",
turnId: "turn-before-restart" as never,
label: "Searched files",
command: "rg restart",
tone: "tool" as const,
toolLifecycleStatus: "completed" as const,
},
},
{
id: "old-stale-work-entry",
kind: "work",
createdAt: "2026-01-01T00:00:06Z",
entry: {
id: "old-stale-work",
createdAt: "2026-01-01T00:00:06Z",
turnId: "turn-before-restart" as never,
label: "Running stale command",
command: "rg stale",
tone: "tool" as const,
toolLifecycleStatus: "inProgress" as const,
},
},
{
id: "old-commentary-entry",
kind: "message",
createdAt: "2026-01-01T00:00:08Z",
message: {
id: "old-commentary" as never,
role: "assistant",
text: "the server restarted, continuing here.",
turnId: "turn-before-restart" as never,
createdAt: "2026-01-01T00:00:08Z",
updatedAt: "2026-01-01T00:00:08Z",
streaming: false,
},
},
{
id: "new-work-entry",
kind: "work",
createdAt: "2026-01-01T00:01:05Z",
entry: {
id: "new-work",
createdAt: "2026-01-01T00:01:05Z",
turnId: "turn-after-restart" as never,
label: "Running tests",
command: "vp test run",
tone: "tool" as const,
toolLifecycleStatus: "inProgress" as const,
},
},
],
latestTurn: {
turnId: "turn-after-restart" as never,
state: "running",
startedAt: "2026-01-01T00:01:00Z",
completedAt: null,
},
isWorking: true,
activeTurnStartedAt: "2026-01-01T00:01:00Z",
turnDiffSummaryByAssistantMessageId: new Map(),
revertTurnCountByUserMessageId: new Map(),
});
expect(rows.some((row) => row.kind === "turn-fold")).toBe(false);
expect(rows.filter((row) => row.id === "working-indicator-row")).toHaveLength(1);
expect(rows.findIndex((row) => row.id === "working-indicator-row")).toBeLessThan(
rows.findIndex((row) => row.id === "old-work-entry"),
);
expect(rows.find((row) => row.id === "working-indicator-row")).toMatchObject({
createdAt: "2026-01-01T00:00:00Z",
});
expect(rows.find((row) => row.id === "old-commentary-entry")).toMatchObject({
showAssistantMeta: false,
showAssistantCopyButton: false,
assistantCopyStreaming: true,
});
expect(rows.filter((row) => row.kind === "work-live" && row.active)).toEqual([
expect.objectContaining({ entry: expect.objectContaining({ id: "new-work" }) }),
]);
expect(rows.some((row) => row.kind === "thinking")).toBe(false);
});
it("keeps an actually running tool in the shared activity row", () => {
const rows = deriveMessagesTimelineRows({
timelineEntries: [
@@ -512,6 +512,37 @@ function timelineEntryTurnId(entry: TimelineEntry): TurnId | null {
return entry.kind === "work" ? (entry.entry.turnId ?? null) : null;
}
/**
* A promptless provider restart replaces the native turn without adding a
* user message. Keep every provider turn since the latest user message in one
* visual response until the replacement turn settles. A steer has its own
* user message, so it naturally starts a new visual response.
*/
function deriveActiveVisualResponseTurnIds(input: {
timelineEntries: ReadonlyArray<TimelineEntry>;
unsettledTurnId: TurnId | null;
isWorking: boolean;
}): ReadonlySet<TurnId> {
const turnIds = new Set<TurnId>();
if (input.unsettledTurnId === null) {
return turnIds;
}
turnIds.add(input.unsettledTurnId);
if (!input.isWorking) {
return turnIds;
}
const latestUserMessageIndex = lastUserMessageIndex(input.timelineEntries);
for (let index = latestUserMessageIndex + 1; index < input.timelineEntries.length; index += 1) {
const turnId = timelineEntryTurnId(input.timelineEntries[index]!);
if (turnId !== null) {
turnIds.add(turnId);
}
}
return turnIds;
}
function workEntryIsActiveTurnActivity(entry: WorkLogEntry): boolean {
return (
entry.toolLifecycleStatus === "inProgress" ||
@@ -529,7 +560,7 @@ function deriveTurnFolds(input: {
timelineEntries: ReadonlyArray<TimelineEntry>;
terminalAssistantMessageIds: ReadonlySet<string>;
latestTurn: TimelineLatestTurn | null;
unsettledTurnId: TurnId | null;
unfoldedTurnIds: ReadonlySet<TurnId>;
}): ReadonlyMap<string, TurnFold> {
interface TurnGroup {
entries: Array<TimelineEntry>;
@@ -587,7 +618,7 @@ function deriveTurnFolds(input: {
const foldsByAnchorEntryId = new Map<string, TurnFold>();
for (const [turnId, group] of groupsByTurnId) {
if (turnId === input.unsettledTurnId) {
if (input.unfoldedTurnIds.has(turnId)) {
continue;
}
if (group.hasStreamingMessage) {
@@ -769,11 +800,16 @@ export function deriveMessagesTimelineRows(input: {
input.latestTurn ?? null,
input.runningTurnId ?? null,
);
const activeVisualResponseTurnIds = deriveActiveVisualResponseTurnIds({
timelineEntries: input.timelineEntries,
unsettledTurnId,
isWorking: input.isWorking,
});
const foldsByAnchorEntryId = deriveTurnFolds({
timelineEntries: input.timelineEntries,
terminalAssistantMessageIds,
latestTurn: input.latestTurn ?? null,
unsettledTurnId,
unfoldedTurnIds: activeVisualResponseTurnIds,
});
const collapsedEntryIds = new Set<string>();
for (const fold of foldsByAnchorEntryId.values()) {
@@ -787,15 +823,7 @@ export function deriveMessagesTimelineRows(input: {
let activeTurnHeaderIndex = input.timelineEntries.length;
if (input.isWorking) {
const latestUserMessageIndex = lastUserMessageIndex(input.timelineEntries);
const firstOwnedAfterUser =
unsettledTurnId === null
? -1
: input.timelineEntries.findIndex(
(entry, index) =>
index > latestUserMessageIndex && timelineEntryTurnId(entry) === unsettledTurnId,
);
activeTurnHeaderIndex =
firstOwnedAfterUser >= 0 ? firstOwnedAfterUser : latestUserMessageIndex + 1;
activeTurnHeaderIndex = latestUserMessageIndex + 1;
}
const entryBelongsToActiveTurn = (entry: TimelineEntry, index: number) =>
input.isWorking &&
@@ -862,10 +890,17 @@ export function deriveMessagesTimelineRows(input: {
activeWorkRow !== null || latestToolFailed ? activeToolEntries.map((entry) => entry.id) : [],
);
const appendWorkingRow = () => {
const latestUserMessage = input.timelineEntries[lastUserMessageIndex(input.timelineEntries)];
const visualResponseStartedAt =
activeVisualResponseTurnIds.size > 1 &&
latestUserMessage?.kind === "message" &&
latestUserMessage.message.role === "user"
? latestUserMessage.message.createdAt
: input.activeTurnStartedAt;
nextRows.push({
kind: "working",
id: "working-indicator-row",
createdAt: input.activeTurnStartedAt,
createdAt: visualResponseStartedAt,
});
};
let hasActivityRow = false;
@@ -1081,10 +1116,11 @@ export function deriveMessagesTimelineRows(input: {
continue;
}
const assistantTurnStillInProgress =
const assistantResponseStillInProgress =
timelineEntry.message.role === "assistant" &&
unsettledTurnId !== null &&
timelineEntry.message.turnId === unsettledTurnId;
timelineEntry.message.turnId !== null &&
timelineEntry.message.turnId !== undefined &&
activeVisualResponseTurnIds.has(timelineEntry.message.turnId);
const durationStart =
durationStartByMessageId.get(timelineEntry.message.id) ?? timelineEntry.message.createdAt;
@@ -1095,7 +1131,7 @@ export function deriveMessagesTimelineRows(input: {
const showAssistantMeta =
timelineEntry.message.role === "assistant" &&
terminalAssistantMessageIds.has(timelineEntry.message.id) &&
!assistantTurnStillInProgress;
!assistantResponseStillInProgress;
nextRows.push({
kind: "message",
@@ -1105,7 +1141,7 @@ export function deriveMessagesTimelineRows(input: {
durationStart,
showAssistantMeta,
showAssistantCopyButton: showAssistantMeta,
assistantCopyStreaming: timelineEntry.message.streaming || assistantTurnStillInProgress,
assistantCopyStreaming: timelineEntry.message.streaming || assistantResponseStillInProgress,
assistantTurnDiffSummary:
timelineEntry.message.role === "assistant"
? input.turnDiffSummaryByAssistantMessageId.get(timelineEntry.message.id)