Repository navigation
Expand file tree
/
Copy pathparse-worker.ts
More file actions
272 lines (245 loc) · 12.4 KB
/
Copy pathparse-worker.ts
File metadata and controls
272 lines (245 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/* Parse worker entry point.
* Supports both worker_threads and child-process IPC so parsing can run
* in an isolated process with its own heap limit.
*/
import { parentPort } from 'worker_threads';
import * as os from 'os';
import * as path from 'path';
import { flushCacheWrites, stripSessionsForMemory } from './cache';
import { hideAccountName, redactHomePaths } from './display-path';
import { emitResultChunks, DEFAULT_SESSION_CHUNK_SIZE } from './parse-chunking';
import { createAckWindow, shouldSendProgressImmediately } from './parse-worker-stream';
import { parseAllLogsAsyncDetailed, type LoadProgress } from './parser';
import { getParseWarningCounts, getParseWarnings, setCuriosityDetector } from './parser-shared';
import { detectCuriosity } from './curiosity';
import { installRuntimeDebugHooks, runtimeDebug } from './runtime-debug';
import { createTelemetrySampler } from './worker-telemetry';
import { extractSourceImages, loadSourceSessionDetail, loadTimelineText, type SessionSourceRequest, type TimelineTextRequest } from './session-images';
interface ParseWorkerRequest {
logsDirs?: string[];
}
/** Number of sessions per streamed IPC chunk (issue #106, S1). */
const SESSION_CHUNK_SIZE = DEFAULT_SESSION_CHUNK_SIZE;
/** Max unacked chunks allowed in flight before the worker pauses emitting (issue #106). This
* bounds how many serialized chunks can sit in the child's native IPC write buffer, preventing
* the native (off-heap) OOM abort that a busy/slow parent triggered when the whole result was
* flushed at once. */
const CHUNK_ACK_WINDOW = 4;
interface ProgressMessage {
type: 'progress';
progress: LoadProgress;
}
const port = parentPort;
const canUseProcessChannel = typeof process.send === 'function';
if (!port && !canUseProcessChannel) throw new Error('parse-worker: no parent channel');
setCuriosityDetector(detectCuriosity);
installRuntimeDebugHooks('parse-worker');
runtimeDebug('parse-worker', port ? 'thread-started' : 'process-started');
function send(msg: unknown): void {
if (port) port.postMessage(msg);
else if (canUseProcessChannel) process.send?.(msg);
}
function parseWorkerRequest(msg: unknown): ParseWorkerRequest {
if (typeof msg !== 'object' || msg === null) return {};
const candidate = msg as { logsDirs?: unknown };
return {
logsDirs: Array.isArray(candidate.logsDirs)
? candidate.logsDirs.filter((dir): dir is string => typeof dir === 'string')
: undefined,
};
}
/** Parent -> worker chunk acknowledgement (issue #106). Routed away from the request handler so
* it never re-triggers a parse. */
interface AckMessage { type: 'ack'; seq: number }
function isAckMessage(msg: unknown): msg is AckMessage {
return (
typeof msg === 'object' && msg !== null &&
(msg as { type?: unknown }).type === 'ack' &&
typeof (msg as { seq?: unknown }).seq === 'number'
);
}
/** Set by the streaming step to receive chunk acks from the parent. */
let onAck: ((seq: number) => void) | null = null;
const isStringArray = (value: unknown): value is string[] => Array.isArray(value) && value.every(item => typeof item === 'string');
const isFiniteNumber = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value);
/** A Timeline text request's scope, kept turns and scrub labels. */
function isTimelineTextFields(candidate: Partial<TimelineTextRequest>): boolean {
const { scope, labels } = candidate;
return typeof scope === 'object' && scope !== null && isFiniteNumber(scope.from) && isFiniteNumber(scope.to)
&& Array.isArray(scope.turns) && scope.turns.every(t => typeof t === 'object' && t !== null && isFiniteNumber(t.t)
&& (t.id === undefined || typeof t.id === 'string'))
&& (candidate.kept === undefined || isStringArray(candidate.kept))
&& typeof labels === 'object' && labels !== null && isStringArray(labels.names)
&& (labels.home === undefined || typeof labels.home === 'string') && (labels.handles === undefined || isStringArray(labels.handles));
}
function isSessionSourceRequest(value: object): value is SessionSourceRequest {
const candidate = value as Partial<SessionSourceRequest>;
const source = candidate.source;
return (candidate.type === 'session-detail'
|| (candidate.type === 'timeline-text' && isTimelineTextFields(candidate))
|| (candidate.type === 'session-images' && typeof candidate.requestId === 'string'
&& (candidate.userEventId === undefined || typeof candidate.userEventId === 'string')
&& (candidate.requestIndex === undefined || (Number.isSafeInteger(candidate.requestIndex) && candidate.requestIndex >= 0))))
&& typeof candidate.sessionId === 'string'
&& typeof source === 'object' && source !== null
&& typeof source.filePath === 'string' && typeof source.workspaceId === 'string'
&& typeof source.workspaceName === 'string' && typeof source.harness === 'string'
&& (source.workspaceRootPath === undefined || typeof source.workspaceRootPath === 'string')
&& ['vscode-session-file', 'cli-events', 'claude-jsonl', 'codex-jsonl', 'opencode-storage', 'opencode-db', 'xcode-db'].includes(source.kind)
&& (source.additionalFilePaths === undefined || (Array.isArray(source.additionalFilePaths)
&& source.additionalFilePaths.every(file => typeof file === 'string')))
&& (source.droppedUserEventIds === undefined || (Array.isArray(source.droppedUserEventIds)
&& source.droppedUserEventIds.every(id => typeof id === 'string')));
}
function onMessage(handler: (msg: ParseWorkerRequest) => void | Promise<void>): void {
const dispatch = (raw: unknown): void => {
if (isAckMessage(raw)) {
onAck?.(raw.seq);
return;
}
if (typeof raw === 'object' && raw !== null && 'type' in raw
&& (raw.type === 'session-images' || raw.type === 'session-detail' || raw.type === 'timeline-text')) {
try {
if (!isSessionSourceRequest(raw)) throw new Error('Invalid session source request');
if (raw.type === 'session-images') {
send({ type: 'session-images', images: extractSourceImages(raw.source, raw.sessionId, raw.requestId, raw) });
} else if (raw.type === 'timeline-text') {
send({ type: 'timeline-text', text: loadTimelineText(raw) });
} else {
send({ type: 'session-detail', session: loadSourceSessionDetail(raw.source, raw.sessionId) });
}
} catch (error) {
send({ type: 'error', message: error instanceof Error ? error.message : String(error) });
}
return;
}
void handler(parseWorkerRequest(raw));
};
if (port) {
port.on('message', dispatch);
return;
}
process.on('message', dispatch);
}
/** Build a live telemetry snapshot of this worker for the loading UI (issue #106). The CPU-delta
* state lives inside the sampler; see worker-telemetry.ts for the (unit-tested) math. */
const sampleTelemetry = createTelemetrySampler({ warningCounts: () => getParseWarningCounts() });
onMessage(async (msg) => {
let lastProgress: LoadProgress | null = null;
// Periodically refresh telemetry even if the parse loop goes quiet during a single large
// workspace, so the loading screen's resource gauges keep ticking (issue #106).
const memTimer = setInterval(() => {
if (lastProgress) {
// Resend the last phase/pct with fresh telemetry only; drop one-shot grid fields so the
// refresh never replays workspace-plan / workspace-done side effects in the UI.
const { workspacePlan: _wp, workspaceDone: _wd, ...rest } = lastProgress;
void _wp; void _wd;
send({ type: 'progress', progress: { ...rest, telemetry: sampleTelemetry() } });
}
}, 500);
memTimer.unref?.();
try {
const logsDirs = Array.isArray(msg.logsDirs) ? msg.logsDirs : [];
const home = os.homedir();
runtimeDebug('parse-worker', 'message-start', `logsDirs=${logsDirs.length}`);
// Throttle verbose intra-workspace progress messages, but always send
// phase changes, workspace grid plans, and workspace completion updates.
let lastSendTime = 0;
let lastPhase = -1;
let pending: ProgressMessage | null = null;
const flushPending = () => {
if (pending) {
send(pending);
pending = null;
lastSendTime = Date.now();
}
};
const { result, dirMetas } = await parseAllLogsAsyncDetailed(logsDirs, (progress) => {
// Progress is shown on screen: never send the home folder (it usually holds the user
// name). Parser items already carry display paths; this also covers free-text details.
if (progress.detail) progress.detail = hideAccountName(redactHomePaths(progress.detail, home), path.basename(home));
progress.telemetry = sampleTelemetry();
lastProgress = progress;
const progressMessage: ProgressMessage = { type: 'progress', progress };
const now = Date.now();
// Always send immediately for phase changes, workspace grid updates, or >= 100%.
if (shouldSendProgressImmediately(progress, lastPhase)) {
flushPending();
send(progressMessage);
lastPhase = progress.phase;
lastSendTime = now;
return;
}
if (now - lastSendTime >= 200) {
send(progressMessage);
lastSendTime = now;
pending = null;
} else {
pending = progressMessage;
}
});
// Flush any final pending progress before sending result.
flushPending();
// Keep full text only in the disk cache written by parseAllLogsAsyncDetailed.
// The parent process receives the memory-efficient representation only.
// (VS Code / CLI sessions are already stripped eagerly during parse; this also strips
// external-harness sessions collected after the main loop.)
stripSessionsForMemory(result.sessions);
runtimeDebug('parse-worker', 'message-result', `workspaces=${result.workspaces.size} sessions=${result.sessions.length}`);
// Stream the result to the parent in per-session-batch chunks (issue #106, S1). See
// parse-chunking.ts for the emit/assemble contract; orphan edit/source entries are
// returned in `done` so nothing is dropped.
//
// Backpressure (issue #106): the parent acks each chunk; the worker keeps at most
// CHUNK_ACK_WINDOW chunks unacked so serialized payloads cannot accumulate in the child's
// native IPC write buffer (the native OOM that V8 heap limits could not prevent).
let nextSeq = 0;
const ackWindow = createAckWindow(CHUNK_ACK_WINDOW);
onAck = (seq) => ackWindow.onAck(seq);
const done = await emitResultChunks(
result,
async (chunk) => {
const seq = nextSeq++;
send({ type: 'chunk', seq, payload: chunk });
// Backpressure applies only to the child-process IPC channel, where unflushed chunks
// accumulate in a native write buffer. The worker_threads path posts into the parent's
// heap instead and never acks, so waiting there would deadlock (issue #106).
if (port) return;
// Pause while the window is full; each ack from the parent re-checks the condition.
await ackWindow.waitForSlot(seq);
},
SESSION_CHUNK_SIZE,
);
onAck = null;
// The parent terminates this worker as soon as `done` arrives, which would also kill the
// cache write that parseAllLogsAsyncDetailed started in its own thread (it overlaps the
// streaming above). Let it land first, or every launch cold-parses again. The wait is
// bounded so a stalled disk cannot hold the parse result hostage.
const flushStarted = Date.now();
const cacheWriteSettled = await flushCacheWrites();
runtimeDebug('parse-worker', 'cache-write-wait', `waitMs=${Date.now() - flushStarted} settled=${cacheWriteSettled}`);
send({
type: 'done',
payload: {
workspaces: done.workspaces,
orphanEditLoc: done.orphanEditLoc,
orphanSources: done.orphanSources,
dirMetas,
parseWarnings: getParseWarnings(),
parseWarningCounts: getParseWarningCounts(),
},
});
} catch (e) {
runtimeDebug('parse-worker', 'message-error', e);
send({
type: 'error',
message: e instanceof Error ? e.message : String(e),
});
} finally {
clearInterval(memTimer);
}
});