11import type { ContentBlock } from "@agentclientprotocol/sdk" ;
22import { container } from "../../di/container.js" ;
33import { MAIN_TOKENS } from "../../di/tokens.js" ;
4+ import { logger } from "../../lib/logger.js" ;
45import {
56 AgentServiceEvent ,
67 cancelPermissionInput ,
@@ -22,8 +23,13 @@ import {
2223 tokenUpdateInput ,
2324} from "../../services/agent/schemas.js" ;
2425import type { AgentService } from "../../services/agent/service.js" ;
26+ import type { ProcessTrackingService } from "../../services/process-tracking/service.js" ;
27+ import type { ShellService } from "../../services/shell/service.js" ;
28+ import type { SleepService } from "../../services/sleep/service.js" ;
2529import { publicProcedure , router } from "../trpc.js" ;
2630
31+ const log = logger . scope ( "agent-router" ) ;
32+
2733const getService = ( ) => container . get < AgentService > ( MAIN_TOKENS . AgentService ) ;
2834
2935export const agentRouter = router ( {
@@ -140,6 +146,30 @@ export const agentRouter = router({
140146 getService ( ) . markAllSessionsForRecreation ( ) ,
141147 ) ,
142148
149+ resetAll : publicProcedure . mutation ( async ( ) => {
150+ log . info ( "Resetting all sessions (logout/project switch)" ) ;
151+
152+ // Clean up all agent sessions (flushes logs, stops agents, releases sleep blockers)
153+ const agentService = getService ( ) ;
154+ await agentService . cleanupAll ( ) ;
155+
156+ // Destroy all shell PTY sessions
157+ const shellService = container . get < ShellService > ( MAIN_TOKENS . ShellService ) ;
158+ shellService . destroyAll ( ) ;
159+
160+ // Kill any remaining tracked processes (belt and suspenders)
161+ const processTracking = container . get < ProcessTrackingService > (
162+ MAIN_TOKENS . ProcessTrackingService ,
163+ ) ;
164+ processTracking . killAll ( ) ;
165+
166+ // Release any lingering sleep blockers
167+ const sleepService = container . get < SleepService > ( MAIN_TOKENS . SleepService ) ;
168+ sleepService . cleanup ( ) ;
169+
170+ log . info ( "All sessions reset successfully" ) ;
171+ } ) ,
172+
143173 getGatewayModels : publicProcedure
144174 . input ( getGatewayModelsInput )
145175 . output ( getGatewayModelsOutput )
0 commit comments