feat(storage): move desktop SQLite ownership into a storage utility process

Main no longer opens desktop business SQLite. A single utility-process
storage owner holds the Assistant, Knowledge, attachment, document-result,
remote-binding and native model-ledger databases and runs whole domain
transactions; Main and Runtime callers use explicit async domain operations.

- Remove the Main readWithFallback path and the supervision worker's
  separate business write connection; manifests are scanned read-only and
  committed by the sole writer.
- Remote events are ACKed only after commit; process loss reports
  unconfirmed writes instead of replaying them.
- Use native utilityProcess messaging with one in-flight bound (32) and
  ordered waiting instead of capacity rejection and retries.
- Start the storage process before the window so startup overlaps.
- Keep short tool output in memory and store full output once in owned
  storage; paging, branching, restart and deletion reuse that copy.
- Put required runtime launch files under goodbuddy-prefixed owned temp
  directories and reclaim confirmed stale ones; stop writing duplicate
  parsed.md.
- Add lint guards for SQLite ownership and architecture, concurrency
  acceptance fixtures, and maintenance/architecture documentation.

Validation: typecheck and lint pass. Alternating baseline (edf019aa) vs
candidate runs show no regression on single-session, C01, C02, C04 and a
5-minute C06; Main p99 drops from ~21 ms to ~16.7 ms under concurrency.
Fresh portable copy (1.75M events) upgrade content unchanged; real-model
output paging passes with 4 external requests. Full npm test runs had
1-9 different timing-sensitive failures per run that pass when rerun.
Not covered: C03 review publication, C07 source body, app-level C05.
This commit is contained in:
mesalogo
2026-10-06 15:40:18 +08:00
parent bc677a3718
commit d80d42b032
229 changed files with 13632 additions and 3800 deletions
+37 -6
View File
@@ -19,7 +19,8 @@ import {
PagedOutputStore,
type PagedOutputPage,
type PagedOutputReference,
type PagedOutputWriter
type PagedOutputWriter,
type PagedOutputStoreOptions
} from './paged-output-store'
const DEFAULT_TIMEOUT_MS = 120_000
@@ -100,12 +101,18 @@ export interface DirectModelProcessService {
}
type ProcessOutput = {
pause?(): unknown
resume?(): unknown
on(event: 'data', listener: (chunk: Buffer | string) => void): unknown
once(event: 'error', listener: (error: Error) => void): unknown
removeListener?(
event: 'error',
listener: (error: Error) => void
): unknown
removeListener?(
event: 'data',
listener: (chunk: Buffer | string) => void
): unknown
}
export type DirectModelProcessChild = WaitableProcessTreeChild & {
@@ -128,6 +135,7 @@ export type DirectModelProcessSpawn = (
) => DirectModelProcessChild
export type LocalDirectModelProcessServiceOptions = {
outputStore?: PagedOutputStoreOptions
outputPrefix?: string
platform?: NodeJS.Platform
environment?: NodeJS.ProcessEnv
@@ -348,7 +356,7 @@ export class LocalDirectModelProcessService
constructor(
private readonly options: LocalDirectModelProcessServiceOptions = {}
) {
this.outputs = new PagedOutputStore(options.outputPrefix ?? 'process')
this.outputs = new PagedOutputStore(options.outputPrefix ?? 'process', options.outputStore)
this.shell = resolveProcessShell(options)
}
@@ -515,9 +523,6 @@ export class LocalDirectModelProcessService
stdout: PagedOutputWriter,
stderr: PagedOutputWriter
): Promise<ProcessExecuteResult> {
child.stdout?.on('data', (chunk) => stdout.append(chunk))
child.stderr?.on('data', (chunk) => stderr.append(chunk))
return new Promise((resolveResult, rejectResult) => {
let settled = false
let cleanupStarted = false
@@ -525,12 +530,16 @@ export class LocalDirectModelProcessService
let terminationReason: 'timeout' | 'cancelled' | undefined
let exitCode: number | null = null
let exitSignal: string | undefined
const outputListeners: Array<{ stream: ProcessOutput; listener: (chunk: Buffer | string) => void }> = []
const removeListeners = (): void => {
clearTimeout(timeout)
cancellationSignal.removeEventListener('abort', cancel)
child.stdout?.removeListener?.('error', failFromOutput)
child.stderr?.removeListener?.('error', failFromOutput)
for (const { stream, listener } of outputListeners) {
stream.removeListener?.('data', listener)
}
}
const result = async (): Promise<ProcessExecuteResult> => {
const stdoutResult = await stdout.finish()
@@ -618,6 +627,23 @@ export class LocalDirectModelProcessService
failure = error
startCleanup()
}
const capture = (stream: ProcessOutput | null | undefined, writer: PagedOutputWriter): void => {
if (!stream) return
const listener = (chunk: Buffer | string): void => {
if (settled) return
stream.pause?.()
try {
void writer.append(chunk).then(
() => { if (!settled) stream.resume?.() },
(error: Error) => { failFromOutput(error) }
)
} catch (error) {
failFromOutput(error instanceof Error ? error : new Error('Unable to capture tool output'))
}
}
outputListeners.push({ stream, listener })
stream.on('data', listener)
}
const timeout = timeoutMs === undefined ? undefined : setTimeout(() => {
if (settled || terminationReason === 'cancelled') {
return
@@ -627,6 +653,8 @@ export class LocalDirectModelProcessService
}, timeoutMs)
timeout?.unref?.()
capture(child.stdout, stdout)
capture(child.stderr, stderr)
child.stdout?.once('error', failFromOutput)
child.stderr?.once('error', failFromOutput)
child.once('error', (error: unknown) => {
@@ -674,7 +702,10 @@ export class LocalDirectModelProcessService
}
dispose(): Promise<void> {
this.disposePromise ??= this.disposeOnce()
this.disposePromise ??= this.disposeOnce().catch((error: unknown) => {
this.disposePromise = undefined
throw error
})
return this.disposePromise
}