Files
goodbuddy/src/main/assistant-storage-startup.ts
T
mesalogo 49377206cc
GoodBuddy Agents / Validate Agent source (push) Canceled after 0s
GoodBuddy Agents / Build darwin arm64 Agent (push) Canceled after 0s
GoodBuddy Agents / Build linux arm64 Agent (push) Canceled after 0s
GoodBuddy Agents / Build linux x64 Agent (push) Canceled after 0s
Cross-platform packages / Validate source (push) Canceled after 0s
Cross-platform packages / Authorize desktop packaging (push) Canceled after 0s
Cross-platform packages / linux arm64 (push) Canceled after 0s
Cross-platform packages / macos arm64 (push) Canceled after 0s
Cross-platform packages / windows arm64 (push) Canceled after 0s
Cross-platform packages / linux x64 (push) Canceled after 0s
Cross-platform packages / macos x64 (push) Canceled after 0s
Cross-platform packages / windows x64 (push) Canceled after 0s
Cross-platform packages / Publish GitHub and OSS release (push) Canceled after 0s
fix: stop subagent history from exhausting disk space
Repeated full subagent progress snapshots could grow the desktop database to
many gigabytes during ordinary long-running tasks. Local and managed SSH
writes now retain lossless progress changes while preserving conversation
snapshots, event identities, remote replay deduplication and transaction rollback.

Upgrade existing history in bounded worker-thread batches, show progress and
retry controls, and reclaim freed disk space after conversion. Schema 34 cannot
be opened by older clients; a pre-upgrade backup is required for client rollback.
Document reusable database migration guidance and the verified large-database,
portable, local Runtime and Linux Host evidence.

Prepare Desktop 0.13.1 with the approved bilingual notes and aligned feature
lists. Agent remains 0.11.24. Final candidate validation and production builds
will run on main CI, as requested, without additional local release testing.

Release note: 修复长任务和子任务造成的数据库异常膨胀;升级时自动整理旧记录并回收空间,保留聊天、执行详情和结果,并支持退出后继续处理。
2026-09-13 20:50:33 +08:00

118 lines
4.0 KiB
TypeScript

import { app, ipcMain, type BrowserWindow } from 'electron'
import { stat } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { Worker } from 'node:worker_threads'
import {
assistantStorageActionSchema,
assistantStorageProgressSchema,
type AssistantStorageProgress
} from '../shared/assistant-storage-contracts'
import { ipcChannels } from '../shared/ipc-channels'
import { assertTrustedSender } from './trusted-ipc-sender'
import { loadMainWindow } from './window'
export async function prepareAssistantStorage(
window: BrowserWindow,
databasePath: string,
signal: AbortSignal
): Promise<void> {
const file = await stat(databasePath).catch((error: NodeJS.ErrnoException) => {
if (error.code === 'ENOENT') return undefined
throw error
})
if (!file) return
let progress: AssistantStorageProgress = {
stage: 'scanning', processed: 0, total: 0, bytesBefore: file.size
}
let shown = false
let retry: (() => void) | undefined
ipcMain.handle(ipcChannels.storageUpgradeProgress, (event) => {
assertTrustedSender(event, window)
return progress
})
ipcMain.handle(ipcChannels.storageUpgradeAction, (event, input: unknown) => {
assertTrustedSender(event, window)
const action = assistantStorageActionSchema.parse(input)
if (action === 'quit') app.quit()
else if (progress.stage === 'failed') retry?.()
})
const publish = (next: AssistantStorageProgress): void => {
progress = assistantStorageProgressSchema.parse(next)
if (window.isDestroyed()) return
if (!shown) {
shown = true
loadMainWindow(window, true)
}
window.setProgressBar(next.stage === 'failed' ? -1 :
next.total > 0 && next.stage === 'converting'
? next.processed / next.total : 2)
}
try {
while (!signal.aborted) {
try {
await runStorageWorker(databasePath, signal, publish)
return
} catch (error) {
if (signal.aborted) return
publish({
...progress, stage: 'failed',
error: error instanceof Error ? error.message : '历史执行记录优化失败'
})
await new Promise<void>((resolve) => {
const finish = (): void => {
signal.removeEventListener('abort', finish)
retry = undefined
resolve()
}
retry = finish
signal.addEventListener('abort', finish, { once: true })
})
}
}
} finally {
ipcMain.removeHandler(ipcChannels.storageUpgradeProgress)
ipcMain.removeHandler(ipcChannels.storageUpgradeAction)
if (!window.isDestroyed()) window.setProgressBar(-1)
}
}
function runStorageWorker(
databasePath: string,
signal: AbortSignal,
publish: (progress: AssistantStorageProgress) => void
): Promise<void> {
const cancellation = new SharedArrayBuffer(4)
const cancel = (): void => {
Atomics.store(new Int32Array(cancellation), 0, 1)
}
if (signal.aborted) return Promise.resolve()
signal.addEventListener('abort', cancel, { once: true })
return new Promise<void>((resolve, reject) => {
const worker = new Worker(join(
dirname(fileURLToPath(import.meta.url)), 'assistant-storage-worker.js'
), { workerData: { databasePath, cancellation } })
let failure: Error | undefined
let done = false
worker.on('message', (message: {
progress?: AssistantStorageProgress
error?: string
cancelled?: boolean
done?: boolean
}) => {
if (message.progress) publish(message.progress)
if (message.error && !message.cancelled) failure = new Error(message.error)
if (message.done || message.cancelled) done = true
})
worker.once('error', () => {
failure = new Error('无法启动历史执行记录优化,请重启 GoodBuddy 后重试。')
})
worker.once('exit', (code) => {
signal.removeEventListener('abort', cancel)
if (failure) reject(failure)
else if (done && code === 0) resolve()
else reject(new Error('历史执行记录优化意外结束,请重试。'))
})
})
}