@@ -1401,6 +1401,42 @@ describe("tui command handlers", () => {
14011401 expect ( loadHistory ) . toHaveBeenCalledTimes ( 1 ) ;
14021402 } ) ;
14031403
1404+ it . each ( [ "error" , "timeout" , "ok" ] ) (
1405+ "ignores a terminal %s ACK after its history reload switches sessions" ,
1406+ async ( status ) => {
1407+ const history = createDeferred ( ) ;
1408+ const harness = createHarness ( {
1409+ currentAgentId : "research" ,
1410+ currentSessionKey : "agent:research:private" ,
1411+ currentSessionId : "private-session" ,
1412+ sendChat : vi . fn ( async ( { runId } : { runId : string } ) => ( { runId, status } ) ) ,
1413+ loadHistory : vi . fn ( ( ) => history . promise ) as LoadHistoryMock ,
1414+ } ) ;
1415+ const pending = harness . handleCommand ( "private provider request" ) ;
1416+ await vi . waitFor ( ( ) => expect ( harness . loadHistory ) . toHaveBeenCalledTimes ( 1 ) ) ;
1417+ harness . addSystem . mockClear ( ) ;
1418+ harness . setActivityStatus . mockClear ( ) ;
1419+ harness . requestRender . mockClear ( ) ;
1420+ harness . state . currentAgentId = "ops" ;
1421+ harness . state . currentSessionKey = "agent:ops:public" ;
1422+ harness . state . currentSessionId = "public-session" ;
1423+ harness . state . activeChatRunId = "public-run" ;
1424+ harness . state . pendingSubmit = {
1425+ phase : "accepted" ,
1426+ runId : "public-run" ,
1427+ draftText : null ,
1428+ } ;
1429+
1430+ history . resolve ( ) ;
1431+ await pending ;
1432+
1433+ expect ( harness . addSystem ) . not . toHaveBeenCalled ( ) ;
1434+ expect ( harness . setActivityStatus ) . not . toHaveBeenCalled ( ) ;
1435+ expect ( harness . state . activeChatRunId ) . toBe ( "public-run" ) ;
1436+ expect ( harness . state . pendingSubmit ?. runId ) . toBe ( "public-run" ) ;
1437+ } ,
1438+ ) ;
1439+
14041440 it ( "removes the accepted canonical pending turn after a re-keyed terminal failure" , async ( ) => {
14051441 const sendChat = vi . fn ( ) . mockResolvedValue ( {
14061442 runId : "accepted-failed-run" ,
@@ -2212,6 +2248,86 @@ describe("tui command handlers", () => {
22122248 expect ( harness . addSystem ) . not . toHaveBeenCalled ( ) ;
22132249 } ) ;
22142250
2251+ it . each ( [ "/model openai/gpt-5.6-luna" , "/usage reset" ] ) (
2252+ "does not apply a stale %s result after a nested patch handoff" ,
2253+ async ( command ) => {
2254+ const appliedAgents : string [ ] = [ ] ;
2255+ const displayedAgents : string [ ] = [ ] ;
2256+ const patchSession = vi . fn ( ( ) => {
2257+ queueMicrotask ( ( ) => {
2258+ queueMicrotask ( ( ) => {
2259+ harness . state . currentAgentId = "ops" ;
2260+ harness . state . currentSessionKey = "agent:ops:public" ;
2261+ harness . state . currentSessionId = "public-session" ;
2262+ harness . state . sessionGeneration += 1 ;
2263+ harness . state . sessionInfo = {
2264+ responseUsage : "tokens" ,
2265+ effectiveResponseUsage : "tokens" ,
2266+ } ;
2267+ } ) ;
2268+ } ) ;
2269+ return Promise . resolve ( {
2270+ ok : true as const ,
2271+ path : "/sessions/patch" ,
2272+ key : "agent:research:private" ,
2273+ entry : { model : "private-sensitive-model" } ,
2274+ } ) ;
2275+ } ) ;
2276+ const harness = createHarness ( {
2277+ currentAgentId : "research" ,
2278+ currentSessionKey : "agent:research:private" ,
2279+ currentSessionId : "private-session" ,
2280+ sessionGeneration : 2 ,
2281+ sessionInfo : { responseUsage : "tokens" , effectiveResponseUsage : "tokens" } ,
2282+ patchSession,
2283+ applySessionInfoFromPatch : vi . fn ( ( ) => appliedAgents . push ( harness . state . currentAgentId ) ) ,
2284+ } ) ;
2285+ harness . addSystem . mockImplementation ( ( ) =>
2286+ displayedAgents . push ( harness . state . currentAgentId ) ,
2287+ ) ;
2288+
2289+ await harness . handleCommand ( command ) ;
2290+
2291+ expect ( harness . state . currentAgentId ) . toBe ( "ops" ) ;
2292+ expect ( harness . state . sessionInfo . responseUsage ) . toBe ( "tokens" ) ;
2293+ expect ( appliedAgents ) . toEqual ( [ "research" ] ) ;
2294+ expect ( displayedAgents ) . toEqual ( [ "research" ] ) ;
2295+ } ,
2296+ ) ;
2297+
2298+ it . each ( [
2299+ { command : "/model openai/gpt-5.6-luna" , hook : "refresh" } ,
2300+ { command : "/think high" , hook : "refresh" } ,
2301+ { command : "/verbose full" , hook : "history" } ,
2302+ { command : "/usage reset" , hook : "refresh" } ,
2303+ ] ) (
2304+ "hides a stale $command failure after its post-patch $hook rejects" ,
2305+ async ( { command, hook } ) => {
2306+ const followup = createDeferred ( ) ;
2307+ const harness = createHarness ( {
2308+ currentAgentId : "research" ,
2309+ currentSessionKey : "agent:research:private" ,
2310+ currentSessionId : "private-session" ,
2311+ ...( hook === "history"
2312+ ? { loadHistory : vi . fn ( ( ) => followup . promise ) as LoadHistoryMock }
2313+ : { refreshSessionInfo : vi . fn ( ( ) => followup . promise ) } ) ,
2314+ } ) ;
2315+ const pending = harness . handleCommand ( command ) ;
2316+ const followupCall = hook === "history" ? harness . loadHistory : harness . refreshSessionInfo ;
2317+ await vi . waitFor ( ( ) => expect ( followupCall ) . toHaveBeenCalledTimes ( 1 ) ) ;
2318+ harness . addSystem . mockClear ( ) ;
2319+ harness . state . currentAgentId = "ops" ;
2320+ harness . state . currentSessionKey = "agent:ops:public" ;
2321+ harness . state . currentSessionId = "public-session" ;
2322+
2323+ followup . reject ( new Error ( "private provider account rejected research tenant" ) ) ;
2324+ await pending ;
2325+
2326+ expect ( harness . addSystem ) . not . toHaveBeenCalled ( ) ;
2327+ expect ( harness . state . currentAgentId ) . toBe ( "ops" ) ;
2328+ } ,
2329+ ) ;
2330+
22152331 it . each ( [ "/model openai/gpt-5.6-luna" , "/usage reset" ] ) (
22162332 "ignores a stale global-agent %s result" ,
22172333 async ( command ) => {
0 commit comments