Skip to content

Commit 433f22f

Browse files
committed
refactor(cloud-agent): determine session type from transport resolution
Add onResolved callback to CloudAgentSession that fires after resolveSession completes in resolveAndConnect. The session manager now derives activeSessionType from the authoritative ResolvedSession (cloudAgentSessionId + isLive) instead of inferring it indirectly from FetchedSessionData or onFirstActivity heuristics.
1 parent c770842 commit 433f22f

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

src/lib/cloud-agent-sdk/session-manager.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,6 @@ function createSessionManager(config: SessionManagerConfig): SessionManager {
444444
}
445445
if (kiloSessionId !== activeSessionId) return;
446446
store.set(fetchedSessionDataAtom, data);
447-
if (data.cloudAgentSessionId) activeSessionType = 'cloud-agent';
448447

449448
// Populate session metadata and swap in the new storage eagerly.
450449
// The storage starts empty; snapshot replay (inside session.connect)
@@ -505,6 +504,10 @@ function createSessionManager(config: SessionManagerConfig): SessionManager {
505504
const ap = store.get(activePermissionAtom);
506505
if (ap?.requestId === requestId) store.set(activePermissionAtom, null);
507506
},
507+
onResolved: resolved => {
508+
if (resolved.cloudAgentSessionId) activeSessionType = 'cloud-agent';
509+
else if (resolved.isLive) activeSessionType = 'cli';
510+
},
508511
onBranchChanged: branch => config.onBranchChanged?.(branch),
509512
onError: message => store.set(errorAtom, message),
510513
onEvent: event => {
@@ -537,8 +540,7 @@ function createSessionManager(config: SessionManagerConfig): SessionManager {
537540
// Fallback: clear loading when events flow even if no root
538541
// session.created was replayed (e.g. CLI snapshot failure).
539542
store.set(isLoadingAtom, false);
540-
if (!data.cloudAgentSessionId && session.canSend) {
541-
activeSessionType = 'cli';
543+
if (activeSessionType === 'cli') {
542544
config.onRemoteSessionOpened?.({ kiloSessionId });
543545
}
544546
},

src/lib/cloud-agent-sdk/session.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type CloudAgentSessionConfig = {
4242
) => void;
4343
onPermissionResolved?: (requestId: string) => void;
4444
onBranchChanged?: (branch: string) => void;
45+
onResolved?: (resolved: ResolvedSession) => void;
4546
onSessionCreated?: (info: SessionInfo) => void;
4647
onSessionUpdated?: (info: SessionInfo) => void;
4748
onEvent?: (event: NormalizedEvent) => void;
@@ -225,6 +226,7 @@ function createCloudAgentSession(config: CloudAgentSessionConfig): CloudAgentSes
225226
if (expectedGeneration !== connectGeneration) return;
226227

227228
console.log('[cli-debug] resolveAndConnect: resolved=%o', resolved);
229+
config.onResolved?.(resolved);
228230

229231
let factory: TransportFactory;
230232
try {

0 commit comments

Comments
 (0)