Skip to content

Commit 339b827

Browse files
authored
fix(tui): keep PTY progress and lifecycle truthful (#117402)
1 parent 56ffa2b commit 339b827

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

‎src/tui/tui-event-handlers.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ describe("tui-event-handlers: handleAgentEvent", () => {
940940
it("shows finishing context for a known run after assistant final", () => {
941941
const { state, tui, setActivityStatus, handleChatEvent, handleAgentEvent } =
942942
createHandlersHarness({
943+
localMode: true,
943944
state: { activeChatRunId: null },
944945
});
945946

@@ -974,6 +975,43 @@ describe("tui-event-handlers: handleAgentEvent", () => {
974975
expect(tui.requestRender).toHaveBeenCalledWith(true);
975976
});
976977

978+
it("keeps a local run finishing until its authoritative chat final", () => {
979+
const { state, tui, setActivityStatus, handleChatEvent, handleAgentEvent } =
980+
createHandlersHarness({
981+
localMode: true,
982+
state: {
983+
activeChatRunId: null,
984+
pendingSubmit: acceptedSubmit("run-local"),
985+
},
986+
});
987+
988+
handleAgentEvent({
989+
runId: "run-local",
990+
stream: "lifecycle",
991+
data: { phase: "finishing" },
992+
});
993+
setActivityStatus.mockClear();
994+
tui.requestRender.mockClear();
995+
996+
handleAgentEvent({
997+
runId: "run-local",
998+
stream: "lifecycle",
999+
data: { phase: "end" },
1000+
});
1001+
1002+
expect(setActivityStatus).not.toHaveBeenCalledWith("idle");
1003+
expect(tui.requestRender).toHaveBeenCalledWith(true);
1004+
1005+
handleChatEvent({
1006+
runId: "run-local",
1007+
sessionKey: state.currentSessionKey,
1008+
state: "final",
1009+
message: { content: [{ type: "text", text: "done" }] },
1010+
});
1011+
1012+
expect(setActivityStatus).toHaveBeenCalledWith("idle");
1013+
});
1014+
9771015
it("force-renders when terminal lifecycle end clears an active status", () => {
9781016
const { tui, handleAgentEvent } = createHandlersHarness({
9791017
state: { activeChatRunId: "run-9" },

‎src/tui/tui-event-handlers.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -672,14 +672,15 @@ export function createEventHandlers(context: EventHandlerContext) {
672672
clearStreamingWatchdog();
673673
setActivityStatus("finishing context");
674674
}
675-
let forceRender = false;
675+
let forceRender = phase === "end";
676676
if (phase === "end") {
677677
postFinalizingRuns.delete(evt.runId);
678678
if (!canUpdateActivityStatus) {
679679
return;
680680
}
681-
setActivityStatus("idle");
682-
forceRender = true;
681+
if (!localMode || !isLocalRunId?.(evt.runId) || finalizedRuns.has(evt.runId)) {
682+
setActivityStatus("idle"); // Local chat.final proves post-turn maintenance finished.
683+
}
683684
}
684685
if (phase === "error") {
685686
postFinalizingRuns.delete(evt.runId);
@@ -695,10 +696,8 @@ export function createEventHandlers(context: EventHandlerContext) {
695696
? evt.data.errorMessage
696697
: "unknown";
697698
scheduleTerminalLifecycleError(evt.runId, errorMessage);
698-
setActivityStatus("error");
699-
} else {
700-
setActivityStatus("error");
701699
}
700+
setActivityStatus("error");
702701
forceRender = true;
703702
}
704703
tui.requestRender(forceRender);

‎src/tui/tui-pty-local.e2e.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,7 @@ describe("TUI PTY real backends", () => {
934934
const responseOffset = fixture.run.visibleOutput().lastIndexOf("LOCAL_PTY_RESPONSE");
935935
await waitForOutputAfter(fixture.run, "| idle", responseOffset);
936936
await createFreshSession(fixture.run, "new session: agent:main:tui-");
937+
const secondResponseStart = fixture.run.visibleOutput().length;
937938
await fixture.run.write("send after local new\r");
938939
await waitFor({
939940
timeoutMs: LOCAL_OUTPUT_TIMEOUT_MS,
@@ -944,6 +945,9 @@ describe("TUI PTY real backends", () => {
944945
expect(JSON.stringify(fixture.mockModel.requests()[1]?.body)).toContain(
945946
"send after local new",
946947
);
948+
await waitForOutputAfter(fixture.run, "LOCAL_PTY_RESPONSE", secondResponseStart);
949+
const secondResponseOffset = fixture.run.visibleOutput().lastIndexOf("LOCAL_PTY_RESPONSE");
950+
await waitForOutputAfter(fixture.run, "| idle", secondResponseOffset);
947951

948952
await fixture.run.write("/exit\r", { delay: false });
949953
const exit = await fixture.run.waitForExit();

0 commit comments

Comments
 (0)