From dc4dfc24106f5f32476376c4e2cbf9babb4cd64e Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:20:36 -0800 Subject: [PATCH] refactor(ai-vault): keep the scanner client under the line limit Moves out-of-memory hold and rerun into its own class and the child-answer handling next to the call send. --- .../session-scanner-service-client.ts | 97 ++++--------------- .../session-scanner-service-oom-recovery.ts | 76 +++++++++++++++ ...ession-scanner-service-request-deadline.ts | 30 +++++- 3 files changed, 123 insertions(+), 80 deletions(-) create mode 100644 src/main/ai-vault/scan-service/session-scanner-service-oom-recovery.ts diff --git a/src/main/ai-vault/scan-service/session-scanner-service-client.ts b/src/main/ai-vault/scan-service/session-scanner-service-client.ts index cd2b8a35488..92bfff79e24 100644 --- a/src/main/ai-vault/scan-service/session-scanner-service-client.ts +++ b/src/main/ai-vault/scan-service/session-scanner-service-client.ts @@ -10,7 +10,6 @@ import { aiVaultServiceErrorText, attachAiVaultServiceChild, cancelAiVaultServiceCall, - clearAiVaultServiceCall, createAiVaultServicePendingCall, createAiVaultServiceReadyWaiter, rejectAiVaultServiceCall, @@ -22,7 +21,7 @@ import { type AiVaultServiceReadyWaiter } from './session-scanner-service-client-state' import { - noteAiVaultServiceScanProgress, + answerAiVaultServiceCall, sendAiVaultServiceCall, type AiVaultServiceScanListener } from './session-scanner-service-request-deadline' @@ -32,11 +31,10 @@ import { isAiVaultServiceChildMessage, type AiVaultSessionSearchInit, type AiVaultServiceRequest, - type AiVaultServiceRequestBody, - type AiVaultServiceResultValue + type AiVaultServiceRequestBody } from './session-scanner-service-protocol' import { forwardWslDistroNotesToChild } from './session-scanner-service-wsl-notes' -import { recordAiVaultServiceExit } from './session-scanner-service-exit' +import { AiVaultServiceOutOfMemoryRecovery } from './session-scanner-service-oom-recovery' export class AiVaultScannerServiceClient { private child: ChildProcess | null = null @@ -49,10 +47,7 @@ export class AiVaultScannerServiceClient { private readonly restartPolicy = new AiVaultServiceRestartPolicy() private readonly sessionSearch = new AiVaultServiceSessionSearchHold() private disposed = false - private ranOutOfMemory = false - private readonly awaitingExit = new Set() - // Set from a fault until the dead child says whether it ran out of heap; the next spawn needs that. - private exitVerdict: PromiseWithResolvers | null = null + private readonly outOfMemory = new AiVaultServiceOutOfMemoryRecovery() private readonly stopForwardingWslNotes = forwardWslDistroNotesToChild(() => this.child) @@ -127,20 +122,18 @@ export class AiVaultScannerServiceClient { this.restartPolicy.dispose() this.idleRetirement.clear() const error = new Error('AI Vault service client was disposed.') - for (const call of [...this.active.values(), ...this.queue, ...this.awaitingExit]) { + for (const call of [...this.active.values(), ...this.queue]) { rejectAiVaultServiceCall(call, error) } this.active.clear() this.queue.length = 0 - this.awaitingExit.clear() - this.exitVerdict?.resolve() - this.exitVerdict = null + this.outOfMemory.rejectAll(error) this.invalidations.rejectAll(error) this.retireChild() } private pump(): void { - if (this.restartPolicy.restartScheduled || this.exitVerdict) { + if (this.restartPolicy.restartScheduled || this.outOfMemory.pendingVerdict) { return } for (const lane of ['cache', 'interactive'] as const) { @@ -195,8 +188,9 @@ export class AiVaultScannerServiceClient { } private ensureChild(): Promise { - if (this.exitVerdict) { - return this.exitVerdict.promise.then(() => + const verdict = this.outOfMemory.pendingVerdict + if (verdict) { + return verdict.then(() => this.disposed ? Promise.reject(new Error('AI Vault service client was disposed.')) : this.ensureChild() @@ -214,7 +208,7 @@ export class AiVaultScannerServiceClient { } let child: ChildProcess try { - child = this.options.processFactory({ afterOutOfMemory: this.ranOutOfMemory }) + child = this.options.processFactory({ afterOutOfMemory: this.outOfMemory.afterOutOfMemory }) } catch (error) { this.restartPolicy.recordFault(() => this.pump()) return Promise.reject(error instanceof Error ? error : new Error(String(error))) @@ -258,23 +252,10 @@ export class AiVaultScannerServiceClient { return } const call = [...this.active.values()].find((entry) => entry.request.id === message.id) - if (!call) { - return + if (call && answerAiVaultServiceCall(call, message)) { + this.active.delete(call.lane) + this.pump() } - if (message.type === 'progress') { - noteAiVaultServiceScanProgress(call, message.work) - return - } - this.active.delete(call.lane) - clearAiVaultServiceCall(call) - if (!call.cancelled) { - if (message.type === 'error') { - call.reject(new Error(message.message)) - } else { - call.resolve((message as { value: AiVaultServiceResultValue['value'] }).value) - } - } - this.pump() } private cancel(call: AiVaultServicePendingCall): void { @@ -305,55 +286,15 @@ export class AiVaultScannerServiceClient { this.restartPolicy.recordFault(() => this.pump()) const active = [...this.active.values()] this.active.clear() - // A scan that exhausted the heap would exhaust it again; hold it until the exit says why. - // Retired children were alive at the fault; one that runs out of heap while retiring is not rerun. - const held = - this.ranOutOfMemory || retire - ? [] - : active.filter((call) => call.sent && call.request.operation === 'scan') - for (const call of held) { - this.awaitingExit.add(call) - } - const verdict = retire ? null : Promise.withResolvers() - this.exitVerdict = verdict ?? this.exitVerdict - recordAiVaultServiceExit(child, error, (outOfMemory) => { - this.ranOutOfMemory ||= outOfMemory - if (verdict) { - if (this.exitVerdict === verdict) { - this.exitVerdict = null - } - verdict.resolve() - } - this.settleHeldCalls(held, error, outOfMemory) - }) - for (const call of active) { - if (!held.includes(call)) { - this.retryStartOrReject(call, error) - } + const unheld = this.outOfMemory.onFault(child, error, active, retire, this.queue, () => + this.pump() + ) + for (const call of unheld) { + this.retryStartOrReject(call, error) } this.invalidations.rejectAll(error) } - private settleHeldCalls( - held: AiVaultServicePendingCall[], - error: Error, - outOfMemory: boolean - ): void { - for (const call of held.toReversed()) { - if (!this.awaitingExit.delete(call)) { - continue - } - if (outOfMemory && !call.cancelled) { - // The replacement child never saw this request; cancelling it there must not await an ack. - call.sent = false - this.queue.unshift(call) - } else { - rejectAiVaultServiceCall(call, error) - } - } - this.pump() - } - private scheduleIdleIfNeeded(): void { this.idleRetirement.schedule( this.sessionSearch.holdsChild || diff --git a/src/main/ai-vault/scan-service/session-scanner-service-oom-recovery.ts b/src/main/ai-vault/scan-service/session-scanner-service-oom-recovery.ts new file mode 100644 index 00000000000..97a1a57d2cf --- /dev/null +++ b/src/main/ai-vault/scan-service/session-scanner-service-oom-recovery.ts @@ -0,0 +1,76 @@ +import type { ChildProcess } from 'node:child_process' +import { + rejectAiVaultServiceCall, + type AiVaultServicePendingCall +} from './session-scanner-service-client-state' +import { recordAiVaultServiceExit } from './session-scanner-service-exit' + +/** Holds a faulted child's scans until its exit says whether it ran out of heap, then reruns them once. */ +export class AiVaultServiceOutOfMemoryRecovery { + private ranOutOfMemory = false + private readonly awaitingExit = new Set() + // Set from a fault until the dead child says whether it ran out of heap; the next spawn needs that. + private exitVerdict: PromiseWithResolvers | null = null + + get afterOutOfMemory(): boolean { + return this.ranOutOfMemory + } + + get pendingVerdict(): Promise | null { + return this.exitVerdict?.promise ?? null + } + + /** Returns the faulted calls that were not held, for the caller to retry or reject now. */ + onFault( + child: ChildProcess, + error: Error, + active: AiVaultServicePendingCall[], + retire: boolean, + queue: AiVaultServicePendingCall[], + onSettled: () => void + ): AiVaultServicePendingCall[] { + // A scan that exhausted the heap would exhaust it again; hold it until the exit says why. + // Retired children were alive at the fault; one that runs out of heap while retiring is not rerun. + const held = + this.ranOutOfMemory || retire + ? [] + : active.filter((call) => call.sent && call.request.operation === 'scan') + for (const call of held) { + this.awaitingExit.add(call) + } + const verdict = retire ? null : Promise.withResolvers() + this.exitVerdict = verdict ?? this.exitVerdict + recordAiVaultServiceExit(child, error, (outOfMemory) => { + this.ranOutOfMemory ||= outOfMemory + if (verdict) { + if (this.exitVerdict === verdict) { + this.exitVerdict = null + } + verdict.resolve() + } + for (const call of held.toReversed()) { + if (!this.awaitingExit.delete(call)) { + continue + } + if (outOfMemory && !call.cancelled) { + // The replacement child never saw this request; cancelling it there must not await an ack. + call.sent = false + queue.unshift(call) + } else { + rejectAiVaultServiceCall(call, error) + } + } + onSettled() + }) + return active.filter((call) => !held.includes(call)) + } + + rejectAll(error: Error): void { + for (const call of this.awaitingExit) { + rejectAiVaultServiceCall(call, error) + } + this.awaitingExit.clear() + this.exitVerdict?.resolve() + this.exitVerdict = null + } +} diff --git a/src/main/ai-vault/scan-service/session-scanner-service-request-deadline.ts b/src/main/ai-vault/scan-service/session-scanner-service-request-deadline.ts index 82f6d4e8c62..6b7ac16b742 100644 --- a/src/main/ai-vault/scan-service/session-scanner-service-request-deadline.ts +++ b/src/main/ai-vault/scan-service/session-scanner-service-request-deadline.ts @@ -1,5 +1,11 @@ -import type { AiVaultServicePendingCall } from './session-scanner-service-client-state' -import type { AiVaultServiceRequest } from './session-scanner-service-protocol' +import { + clearAiVaultServiceCall, + type AiVaultServicePendingCall +} from './session-scanner-service-client-state' +import type { + AiVaultServiceChildMessage, + AiVaultServiceRequest +} from './session-scanner-service-protocol' import type { AiVaultScanWork } from '../../../shared/ai-vault/scanning/ai-vault-scan-progress' export const AI_VAULT_SERVICE_INTERACTIVE_TIMEOUT_MS = 15_000 @@ -51,3 +57,23 @@ export function noteAiVaultServiceScanProgress( } call.scanListener?.work(work) } + +/** Returns whether the child's answer finished the call. */ +export function answerAiVaultServiceCall( + call: AiVaultServicePendingCall, + message: Extract +): boolean { + if (message.type === 'progress') { + noteAiVaultServiceScanProgress(call, message.work) + return false + } + clearAiVaultServiceCall(call) + if (!call.cancelled) { + if (message.type === 'error') { + call.reject(new Error(message.message)) + } else { + call.resolve(message.value) + } + } + return true +}