-
Notifications
You must be signed in to change notification settings - Fork 227
perf: Worker Thread session parsing with SubagentDisplayMeta lazy-loading #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,7 @@ import { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| type SessionsByIdsOptions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| type SessionsPaginationOptions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from '../types'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { sessionParserPool } from '../workers/SessionParserPool'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { coercePageLimit, validateProjectId, validateSessionId } from './guards'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -33,6 +34,9 @@ const logger = createLogger('IPC:sessions'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| // Service registry - set via initialize | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| let registry: ServiceContextRegistry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Sessions where native pipeline produced invalid chunks — permanently use JS fallback | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const nativeDisabledSessions = new Set<string>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Initializes session handlers with service registry. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -67,6 +71,9 @@ export function removeSessionHandlers(ipcMain: IpcMain): void { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| ipcMain.removeHandler('get-session-metrics'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ipcMain.removeHandler('get-waterfall-data'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Release accumulated per-session state | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| nativeDisabledSessions.clear(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info('Session handlers removed'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -219,6 +226,7 @@ async function handleGetSessionDetail( | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check cache first | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| let sessionDetail = dataCache.get(cacheKey); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| let usedNative = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!sessionDetail) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const fsType = projectScanner.getFileSystemProvider().type; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -231,32 +239,105 @@ async function handleGetSessionDetail( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Parse session messages | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const parsedSession = await sessionParser.parseSession(safeProjectId, safeSessionId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Resolve subagents | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const subagents = await subagentResolver.resolveSubagents( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Try native Rust pipeline (local filesystem only). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Rust handles: JSONL read -> classify -> chunk -> tool executions -> semantic steps. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Returns serde_json::Value with exact TS field names. JS only converts timestamps. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // JS still handles: subagent resolution (requires filesystem provider). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Use native Rust pipeline for local sessions WITHOUT subagents. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Sessions with subagents need ProcessLinker + sidechain context which | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // only the JS pipeline provides (Review finding #1). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const hasSubagentFiles = await projectScanner.hasSubagents( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| safeProjectId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| safeSessionId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| parsedSession.taskCalls, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| parsedSession.messages | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| safeSessionId | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| session.hasSubagents = subagents.length > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fsType === 'local' && !hasSubagentFiles && !nativeDisabledSessions.has(cacheKey)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { buildSessionChunksNative } = await import('../utils/nativeJsonl'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sessionPath = projectScanner.getSessionPath(safeProjectId, safeSessionId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const nativeResult = buildSessionChunksNative(sessionPath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Validate ALL chunks — not just the first. If any chunk has wrong | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // shape, fall back to JS pipeline instead of sending bad data to renderer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isValidNative = | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| nativeResult && | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| nativeResult.chunks.length > 0 && | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| (nativeResult.chunks as Record<string, unknown>[]).every( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| (c) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| c != null && | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| typeof c.chunkType === 'string' && | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'rawMessages' in c && | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'startTime' in c && | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'metrics' in c | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+260
to
+270
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Chunk validation is incomplete — missing required BaseChunk fields. The validation checks 🔧 Suggested validation fix const isValidNative =
nativeResult &&
nativeResult.chunks.length > 0 &&
(nativeResult.chunks as Record<string, unknown>[]).every(
(c) =>
c != null &&
+ typeof c.id === 'string' &&
typeof c.chunkType === 'string' &&
'rawMessages' in c &&
'startTime' in c &&
+ 'endTime' in c &&
+ typeof c.durationMs === 'number' &&
'metrics' in c
);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isValidNative) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| sessionDetail = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| session, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| messages: [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| chunks: nativeResult.chunks as SessionDetail['chunks'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| processes: [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| metrics: nativeResult.metrics as SessionDetail['metrics'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| usedNative = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (nativeResult) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Native produced chunks but they failed validation — permanently | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // disable native for this session to avoid repeated failures. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.warn(`Native validation failed for ${cacheKey}, disabling native for this session`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| nativeDisabledSessions.add(cacheKey); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Native not available — fall through to JS | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Build session detail with chunks | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| sessionDetail = chunkBuilder.buildSessionDetail(session, parsedSession.messages, subagents); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // JS fallback pipeline — dispatch to Worker Thread to avoid blocking main process | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!usedNative) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| sessionDetail = await sessionParserPool.parse({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| projectsDir: projectScanner.getProjectsDir(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| sessionPath: projectScanner.getSessionPath(safeProjectId, safeSessionId), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| projectId: safeProjectId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| sessionId: safeSessionId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| fsType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| session, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (workerError) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Worker failed (timeout, crash, etc.) — fall back to inline blocking parse | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.warn('Worker parse failed, falling back to inline:', workerError); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const parsedSession = await sessionParser.parseSession(safeProjectId, safeSessionId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const subagents = await subagentResolver.resolveSubagents( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| safeProjectId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| safeSessionId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| parsedSession.taskCalls, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| parsedSession.messages | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| session.hasSubagents = subagents.length > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| sessionDetail = chunkBuilder.buildSessionDetail( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| session, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| parsedSession.messages, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| subagents | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Cache the result | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataCache.set(cacheKey, sessionDetail); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Cache JS pipeline results only — native results skip cache so any | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // rendering failures on the next request will fall back to JS pipeline. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (sessionDetail && !usedNative) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataCache.set(cacheKey, sessionDetail); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!sessionDetail) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Strip raw messages before IPC transfer — the renderer never uses them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Only chunks (with semantic steps) and process summaries cross the boundary. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // This cuts IPC serialization + renderer heap by ~50-60%. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...sessionDetail, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| messages: [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| processes: sessionDetail.processes.map((p) => ({ ...p, messages: [] })), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Only report native pipeline when Rust actually handled full chunking. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _nativePipeline: usedNative ? Date.now() : false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.error(`Error in get-session-detail for ${projectId}/${sessionId}:`, error); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.