Skip to content

Commit ac6009e

Browse files
steipeteharjothkharaZengWen-DT
authored
fix(tui): keep shared sessions live and bound stream state (#114443)
Fix cross-client terminal history synchronization without letting stale snapshots erase in-flight responses. Bound orphaned stream assembly, preserve session and agent ownership, and cover real Gateway delivery, persistence races, reconnects, and stress bursts. Closes #38829. Credits prior issue investigation in #96252 and bounded-stream work in #109492. Co-authored-by: Harjoth Khara <[email protected]> Co-authored-by: Wynne668 <[email protected]>
1 parent 4029070 commit ac6009e

8 files changed

Lines changed: 604 additions & 28 deletions

‎docs/web/tui.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ Tips:
219219

220220
- On connect, the TUI loads the latest history (default 200 messages).
221221
- Streaming responses update in place until finalized.
222+
- Messages sent to the same session from another client appear automatically.
222223
- The TUI also listens to agent tool events for richer tool cards.
223224

224225
## Connection details

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

Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
BtwEvent,
99
ChatEvent,
1010
SessionChangedEvent,
11+
SessionMessageEvent,
1112
TuiHistoryLoadResult,
1213
TuiStateAccess,
1314
} from "./tui-types.js";
@@ -1933,6 +1934,342 @@ describe("tui-event-handlers: handleAgentEvent", () => {
19331934
expect(loadHistory).toHaveBeenCalledTimes(1);
19341935
});
19351936

1937+
describe("session.message history reload", () => {
1938+
it("reloads the current session when another client appends a message", () => {
1939+
const { state, loadHistory, handleSessionMessageEvent } = createHandlersHarness({
1940+
state: {
1941+
activeChatRunId: null,
1942+
currentSessionId: "session-before",
1943+
sessionInfo: { verboseLevel: "on", updatedAt: 100 },
1944+
},
1945+
});
1946+
1947+
handleSessionMessageEvent({
1948+
sessionKey: state.currentSessionKey,
1949+
sessionId: "session-after",
1950+
updatedAt: 200,
1951+
} satisfies SessionMessageEvent);
1952+
1953+
expect(state.currentSessionId).toBe("session-after");
1954+
expect(state.sessionInfo.updatedAt).toBe(200);
1955+
expect(loadHistory).toHaveBeenCalledTimes(1);
1956+
});
1957+
1958+
it("accepts the canonical session's unscoped alias", () => {
1959+
const { loadHistory, handleSessionMessageEvent } = createHandlersHarness({
1960+
state: { activeChatRunId: null, currentSessionKey: "agent:main:main" },
1961+
});
1962+
1963+
handleSessionMessageEvent({ sessionKey: "main" } satisfies SessionMessageEvent);
1964+
1965+
expect(loadHistory).toHaveBeenCalledTimes(1);
1966+
});
1967+
1968+
it("ignores an unscoped alias owned by a different agent", () => {
1969+
const { state, loadHistory, handleSessionMessageEvent } = createHandlersHarness({
1970+
state: {
1971+
activeChatRunId: null,
1972+
currentAgentId: "work",
1973+
currentSessionId: "session-work",
1974+
currentSessionKey: "agent:work:main",
1975+
sessionInfo: { verboseLevel: "on", updatedAt: 100 },
1976+
},
1977+
});
1978+
1979+
handleSessionMessageEvent({
1980+
sessionKey: "main",
1981+
agentId: "main",
1982+
sessionId: "session-default-agent",
1983+
updatedAt: 200,
1984+
} satisfies SessionMessageEvent);
1985+
1986+
expect(state.currentSessionId).toBe("session-work");
1987+
expect(state.sessionInfo.updatedAt).toBe(100);
1988+
expect(loadHistory).not.toHaveBeenCalled();
1989+
});
1990+
1991+
it("does not assign an unqualified default-agent alias to another agent", () => {
1992+
const { state, loadHistory, handleSessionMessageEvent } = createHandlersHarness({
1993+
state: {
1994+
activeChatRunId: null,
1995+
currentAgentId: "work",
1996+
currentSessionId: "session-work",
1997+
currentSessionKey: "agent:work:main",
1998+
},
1999+
});
2000+
2001+
handleSessionMessageEvent({
2002+
sessionKey: "main",
2003+
sessionId: "session-default-agent",
2004+
} satisfies SessionMessageEvent);
2005+
2006+
expect(state.currentSessionId).toBe("session-work");
2007+
expect(loadHistory).not.toHaveBeenCalled();
2008+
});
2009+
2010+
it("ignores messages for another session without changing selected metadata", () => {
2011+
const { state, loadHistory, tui, handleSessionMessageEvent } = createHandlersHarness({
2012+
state: {
2013+
activeChatRunId: null,
2014+
currentAgentId: "work",
2015+
currentSessionId: "session-before",
2016+
currentSessionKey: "agent:work:main",
2017+
sessionInfo: { verboseLevel: "on", updatedAt: 100 },
2018+
},
2019+
});
2020+
2021+
handleSessionMessageEvent({
2022+
sessionKey: "agent:work:other",
2023+
agentId: "work",
2024+
sessionId: "other-session",
2025+
updatedAt: 200,
2026+
} satisfies SessionMessageEvent);
2027+
2028+
expect(state.currentSessionId).toBe("session-before");
2029+
expect(state.sessionInfo.updatedAt).toBe(100);
2030+
expect(loadHistory).not.toHaveBeenCalled();
2031+
expect(tui.requestRender).not.toHaveBeenCalled();
2032+
});
2033+
2034+
it("does not let an older transcript snapshot replace newer session metadata", () => {
2035+
const { state, loadHistory, handleSessionMessageEvent } = createHandlersHarness({
2036+
state: {
2037+
activeChatRunId: null,
2038+
currentSessionId: "session-current",
2039+
sessionInfo: { verboseLevel: "on", updatedAt: 200 },
2040+
},
2041+
});
2042+
2043+
handleSessionMessageEvent({
2044+
sessionKey: state.currentSessionKey,
2045+
sessionId: "session-stale",
2046+
updatedAt: 100,
2047+
} satisfies SessionMessageEvent);
2048+
2049+
expect(state.currentSessionId).toBe("session-current");
2050+
expect(state.sessionInfo.updatedAt).toBe(200);
2051+
expect(loadHistory).toHaveBeenCalledTimes(1);
2052+
});
2053+
2054+
it("reloads a global session only for its selected agent", () => {
2055+
const { loadHistory, handleSessionMessageEvent } = createHandlersHarness({
2056+
state: {
2057+
agentDefaultId: "main",
2058+
activeChatRunId: null,
2059+
currentAgentId: "work",
2060+
currentSessionKey: "global",
2061+
sessionScope: "global",
2062+
},
2063+
});
2064+
2065+
handleSessionMessageEvent({
2066+
sessionKey: "global",
2067+
agentId: "main",
2068+
} satisfies SessionMessageEvent);
2069+
expect(loadHistory).not.toHaveBeenCalled();
2070+
2071+
handleSessionMessageEvent({
2072+
sessionKey: "global",
2073+
agentId: "work",
2074+
} satisfies SessionMessageEvent);
2075+
expect(loadHistory).toHaveBeenCalledTimes(1);
2076+
});
2077+
2078+
it("coalesces a burst of transcript updates into one follow-up reload", async () => {
2079+
let resolveFirstHistory: ((result: TuiHistoryLoadResult) => void) | undefined;
2080+
const { state, loadHistory, handleSessionMessageEvent } = createHandlersHarness({
2081+
state: { activeChatRunId: null },
2082+
});
2083+
loadHistory.mockImplementationOnce(
2084+
() =>
2085+
new Promise<TuiHistoryLoadResult>((resolve) => {
2086+
resolveFirstHistory = resolve;
2087+
}),
2088+
);
2089+
2090+
for (let index = 0; index < 250; index += 1) {
2091+
handleSessionMessageEvent({
2092+
sessionKey: state.currentSessionKey,
2093+
updatedAt: index,
2094+
} satisfies SessionMessageEvent);
2095+
}
2096+
2097+
expect(loadHistory).toHaveBeenCalledTimes(1);
2098+
expect(state.sessionInfo.updatedAt).toBe(249);
2099+
2100+
resolveFirstHistory?.({ loaded: true, inFlightRunId: null });
2101+
await vi.waitFor(() => expect(loadHistory).toHaveBeenCalledTimes(2));
2102+
});
2103+
2104+
it("waits for terminal persistence before refreshing a displayed local final", () => {
2105+
const {
2106+
state,
2107+
chatLog,
2108+
loadHistory,
2109+
handleChatEvent,
2110+
handleSessionsChangedEvent,
2111+
handleSessionMessageEvent,
2112+
} = createHandlersHarness({ state: { activeChatRunId: "run-active" } });
2113+
2114+
handleSessionMessageEvent({
2115+
sessionKey: state.currentSessionKey,
2116+
} satisfies SessionMessageEvent);
2117+
handleChatEvent({
2118+
runId: "run-active",
2119+
sessionKey: state.currentSessionKey,
2120+
state: "final",
2121+
message: { content: [{ type: "text", text: "keep this visible" }] },
2122+
});
2123+
2124+
expect(chatLog.finalizeAssistant).toHaveBeenCalledWith("keep this visible", "run-active");
2125+
expect(loadHistory).not.toHaveBeenCalled();
2126+
2127+
handleSessionMessageEvent({
2128+
sessionKey: state.currentSessionKey,
2129+
updatedAt: 200,
2130+
} satisfies SessionMessageEvent);
2131+
expect(loadHistory).not.toHaveBeenCalled();
2132+
2133+
handleSessionsChangedEvent({
2134+
sessionKey: state.currentSessionKey,
2135+
runId: "run-active",
2136+
phase: "end",
2137+
} satisfies SessionChangedEvent);
2138+
2139+
expect(loadHistory).toHaveBeenCalledTimes(1);
2140+
});
2141+
2142+
it("refreshes after a local final when terminal persistence arrives first", () => {
2143+
const {
2144+
state,
2145+
chatLog,
2146+
loadHistory,
2147+
handleChatEvent,
2148+
handleSessionsChangedEvent,
2149+
handleSessionMessageEvent,
2150+
} = createHandlersHarness({ state: { activeChatRunId: "run-active" } });
2151+
2152+
handleSessionMessageEvent({
2153+
sessionKey: state.currentSessionKey,
2154+
} satisfies SessionMessageEvent);
2155+
handleSessionsChangedEvent({
2156+
sessionKey: state.currentSessionKey,
2157+
runId: "run-active",
2158+
phase: "end",
2159+
} satisfies SessionChangedEvent);
2160+
expect(loadHistory).not.toHaveBeenCalled();
2161+
2162+
handleChatEvent({
2163+
runId: "run-active",
2164+
sessionKey: state.currentSessionKey,
2165+
state: "final",
2166+
message: { content: [{ type: "text", text: "keep this visible" }] },
2167+
});
2168+
2169+
expect(chatLog.finalizeAssistant).toHaveBeenCalledWith("keep this visible", "run-active");
2170+
expect(loadHistory).toHaveBeenCalledTimes(1);
2171+
});
2172+
2173+
it("defers the first external update after a visible final until persistence", () => {
2174+
const {
2175+
state,
2176+
chatLog,
2177+
loadHistory,
2178+
handleChatEvent,
2179+
handleSessionsChangedEvent,
2180+
handleSessionMessageEvent,
2181+
} = createHandlersHarness({ state: { activeChatRunId: "run-active" } });
2182+
2183+
handleChatEvent({
2184+
runId: "run-active",
2185+
sessionKey: state.currentSessionKey,
2186+
state: "final",
2187+
message: { content: [{ type: "text", text: "keep this visible" }] },
2188+
});
2189+
2190+
expect(chatLog.finalizeAssistant).toHaveBeenCalledWith("keep this visible", "run-active");
2191+
expect(loadHistory).not.toHaveBeenCalled();
2192+
2193+
handleSessionMessageEvent({
2194+
sessionKey: state.currentSessionKey,
2195+
} satisfies SessionMessageEvent);
2196+
2197+
expect(loadHistory).not.toHaveBeenCalled();
2198+
2199+
handleSessionsChangedEvent({
2200+
sessionKey: state.currentSessionKey,
2201+
runId: "run-active",
2202+
phase: "end",
2203+
} satisfies SessionChangedEvent);
2204+
2205+
expect(loadHistory).toHaveBeenCalledTimes(1);
2206+
});
2207+
2208+
it("does not reload until an optimistic submit is resolved", () => {
2209+
const { state, loadHistory, handleSessionMessageEvent, flushPendingHistoryRefreshIfIdle } =
2210+
createHandlersHarness({
2211+
state: { activeChatRunId: null, pendingSubmit: sendingSubmit("run-pending") },
2212+
});
2213+
2214+
handleSessionMessageEvent({
2215+
sessionKey: state.currentSessionKey,
2216+
} satisfies SessionMessageEvent);
2217+
expect(loadHistory).not.toHaveBeenCalled();
2218+
2219+
state.pendingSubmit = null;
2220+
flushPendingHistoryRefreshIfIdle();
2221+
2222+
expect(loadHistory).toHaveBeenCalledTimes(1);
2223+
});
2224+
2225+
it("waits for persistence when a refresh arrives before an optimistic run is accepted", () => {
2226+
const {
2227+
state,
2228+
chatLog,
2229+
loadHistory,
2230+
handleAgentEvent,
2231+
handleChatEvent,
2232+
handleSessionsChangedEvent,
2233+
handleSessionMessageEvent,
2234+
} = createHandlersHarness({
2235+
state: { activeChatRunId: null, pendingSubmit: sendingSubmit("run-pending") },
2236+
});
2237+
2238+
handleSessionMessageEvent({
2239+
sessionKey: state.currentSessionKey,
2240+
} satisfies SessionMessageEvent);
2241+
expect(loadHistory).not.toHaveBeenCalled();
2242+
2243+
state.pendingSubmit = acceptedSubmit("run-pending");
2244+
handleAgentEvent({
2245+
runId: "run-pending",
2246+
sessionKey: state.currentSessionKey,
2247+
stream: "lifecycle",
2248+
data: { phase: "start" },
2249+
} satisfies AgentEvent);
2250+
handleChatEvent({
2251+
runId: "run-pending",
2252+
sessionKey: state.currentSessionKey,
2253+
state: "final",
2254+
message: { content: [{ type: "text", text: "keep accepted output visible" }] },
2255+
});
2256+
2257+
expect(chatLog.finalizeAssistant).toHaveBeenCalledWith(
2258+
"keep accepted output visible",
2259+
"run-pending",
2260+
);
2261+
expect(loadHistory).not.toHaveBeenCalled();
2262+
2263+
handleSessionsChangedEvent({
2264+
sessionKey: state.currentSessionKey,
2265+
runId: "run-pending",
2266+
phase: "end",
2267+
} satisfies SessionChangedEvent);
2268+
2269+
expect(loadHistory).toHaveBeenCalledTimes(1);
2270+
});
2271+
});
2272+
19362273
describe("sessions.changed history reload", () => {
19372274
const startRun = (
19382275
state: TuiStateAccess,

0 commit comments

Comments
 (0)