Skip to content

Commit 75673e2

Browse files
committed
fix(tui): keep PTY progress and lifecycle truthful
1 parent 4e47c4c commit 75673e2

4 files changed

Lines changed: 48 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
@@ -924,6 +924,7 @@ describe("TUI PTY real backends", () => {
924924
const responseOffset = fixture.run.visibleOutput().lastIndexOf("LOCAL_PTY_RESPONSE");
925925
await waitForOutputAfter(fixture.run, "| idle", responseOffset);
926926
await createFreshSession(fixture.run, "new session: agent:main:tui-");
927+
const secondResponseStart = fixture.run.visibleOutput().length;
927928
await fixture.run.write("send after local new\r");
928929
await waitFor({
929930
timeoutMs: LOCAL_OUTPUT_TIMEOUT_MS,
@@ -934,6 +935,9 @@ describe("TUI PTY real backends", () => {
934935
expect(JSON.stringify(fixture.mockModel.requests()[1]?.body)).toContain(
935936
"send after local new",
936937
);
938+
await waitForOutputAfter(fixture.run, "LOCAL_PTY_RESPONSE", secondResponseStart);
939+
const secondResponseOffset = fixture.run.visibleOutput().lastIndexOf("LOCAL_PTY_RESPONSE");
940+
await waitForOutputAfter(fixture.run, "| idle", secondResponseOffset);
937941

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

‎test/vitest/vitest.tui-pty.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function createTuiPtyVitestConfig(env?: Record<string, string | undefined>) {
4343
exclude,
4444
fileParallelism: false,
4545
maxWorkers: 1,
46+
reporters: ["verbose"],
4647
setupFiles: [
4748
...new Set(
4849
[...(baseTest.setupFiles ?? []), "test/setup-openclaw-runtime.ts"].map(

0 commit comments

Comments
 (0)