Files
orca/src/main/native-chat/wsl-transcript-fs-process-dispatch.ts
T
OrcaWinandOrcaWin 3286f3b259 [comment audit] Clean up IPC, host lifecycle and native-chat comments (#32966)
* [comment audit] Tighten watcher lifetime and file-write constraints [skip ci]

* [comment audit] Clarify IPC routing and notification comments [skip ci]

* [comment audit] Shorten watcher, preflight and PTY comments [skip ci]

* [comment audit] Clarify PTY delivery and restore ownership [skip ci]

* [comment audit] Tighten PTY environment and flow-control rationale [skip ci]

* [comment audit] Clarify IPC spawn and stable-pane guarantees [skip ci]

* [comment audit] Tighten PTY provider and filesystem ownership rationale [skip ci]

* [comment audit] Clarify clone ownership and remote runtime recovery [skip ci]

* [comment audit] Condense upload, settings and SSH lifecycle comments [skip ci]

* [comment audit] Shorten shutdown, preview and worktree watcher rationale [skip ci]

* [comment audit] Clarify worktree identity, remote ownership and copy limits [skip ci]

* [comment audit] Shorten worktree catalog, removal and WSL routing rationale [skip ci]

* [comment audit] Tighten Linear credential, pagination and export comments [skip ci]

* [comment audit] Condense managed-server activation, snapshot and launch rationale [skip ci]

* [comment audit] Trim runtime installation and managed-server lifecycle comments [skip ci]

* [comment audit] Simplify managed tunnel, memory and menu comments [skip ci]

* [comment audit] Condense integration, model catalog and attachment rationale [skip ci]

* [comment audit] Shorten journal persistence, replay and queue rationale [skip ci]

* [comment audit] Condense journal ordering and timeline lifecycle comments [skip ci]

* [comment audit] Trim timeline and subscriber delivery explanations [skip ci]

* [comment audit] Shorten Stop, child-exit and recovery contracts [skip ci]

* [comment audit] Simplify handover and host recovery comments [skip ci]

* [comment audit] Trim queue delivery and provider startup commentary [skip ci]

* [comment audit] Clarify restart and rewind safety comments [skip ci]

* [comment audit] Trim status, Stop and completion commentary [skip ci]

* [comment audit] Simplify transcript lookup and visual containment comments [skip ci]

* [comment audit] Trim transcript reader, watcher and WSL transport commentary [skip ci]

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-10-23 19:07:32 -07:00

88 lines
3.1 KiB
TypeScript

import { open, type FileHandle } from 'node:fs/promises'
import {
invalidTranscriptHandleError,
type WslTranscriptFsReusableProcessCall
} from './wsl-transcript-fs-process-protocol'
import { decodeWslTranscriptFsProcessValue } from './wsl-transcript-fs-process-decode'
import {
WslTranscriptFsProcessClient,
type WslTranscriptFsProcessHandle
} from './wsl-transcript-fs-process-client'
import { wslTranscriptFsHandleOwners } from './wsl-transcript-fs-process-handle-owner'
import { WslTranscriptFsProcessOperations } from './wsl-transcript-fs-process-operations'
import { forkWslTranscriptFsProcess } from './wsl-transcript-fs-process-spawn'
export type { WslTranscriptFsProcessHandle } from './wsl-transcript-fs-process-client'
// Require the worker global too: leaked VITEST=true must never enable in-process UNC I/O in real apps.
function inVitestWorker(): boolean {
return process.env.VITEST === 'true' && '__vitest_worker__' in globalThis
}
// Tests reuse child dispatch/decoding in-process; production always crosses the process boundary.
let inProcessOperations: WslTranscriptFsProcessOperations | null = null
function runInProcess<T>(request: WslTranscriptFsReusableProcessCall): Promise<T> {
inProcessOperations ??= new WslTranscriptFsProcessOperations()
return inProcessOperations
.execute({ ...request, id: 0 })
.then((value) => decodeWslTranscriptFsProcessValue(request.operation, value)) as Promise<T>
}
const clientsByLane = new Map<string, WslTranscriptFsProcessClient>()
function getLaneClient(laneKey: string): WslTranscriptFsProcessClient {
const existing = clientsByLane.get(laneKey)
if (existing) {
return existing
}
const client = new WslTranscriptFsProcessClient(forkWslTranscriptFsProcess)
clientsByLane.set(laneKey, client)
return client
}
export function runWslTranscriptFsProcess<T>(
request: WslTranscriptFsReusableProcessCall,
signal: AbortSignal,
laneKey: string
): Promise<T> {
if (inVitestWorker()) {
return runInProcess<T>(request)
}
return getLaneClient(laneKey).run<T>(request, signal)
}
export function openWslTranscriptFsProcess(
path: string,
signal: AbortSignal,
laneKey: string
): Promise<WslTranscriptFsProcessHandle | FileHandle> {
if (inVitestWorker()) {
// Test suites use real FileHandles and the native ownership path.
return open(path, 'r')
}
return getLaneClient(laneKey).open(path, signal)
}
export function readWslTranscriptFsProcess(
handle: WslTranscriptFsProcessHandle,
position: number,
length: number,
signal: AbortSignal
): Promise<Buffer> {
const owner = wslTranscriptFsHandleOwners.get(handle)
return owner
? owner.read(handle, position, length, signal)
: Promise.reject(invalidTranscriptHandleError())
}
export function closeWslTranscriptFsProcess(handle: WslTranscriptFsProcessHandle): Promise<void> {
return wslTranscriptFsHandleOwners.get(handle)?.close(handle) ?? Promise.resolve()
}
export function isWslTranscriptFsProcessHandle(
value: FileHandle | WslTranscriptFsProcessHandle
): value is WslTranscriptFsProcessHandle {
return 'wslTranscriptFsProcessHandle' in value
}