fix: avoid history reclamation during structure-only upgrades
Ordinary database schema upgrades previously ran VACUUM on free pages and showed misleading history-cleanup progress. Separate structure upgrades from required legacy conversions, retaining reclamation intent only for retries within the same startup. Preserve released data without new schema versions or persistent migration markers; Agent and normal writes remain unchanged. Add real SQLite and Worker regressions, bilingual operation-specific startup copy, and migration documentation. Synchronize native Electron drag input with actual resize state without weakening layout assertions. Validation: 47 focused migration tests passed; typecheck, lint and diff checks passed. Final full suite: 5137 passed, 74 skipped, 2 unrelated Electron failures. Both failing Electron tests passed an isolated rerun; full CI remains unverified. Release note: 修复普通数据库结构升级再次整理历史和回收空间的问题;仅在需要转换旧数据时执行相关处理,启动页准确显示当前升级操作。
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import type { EventEmitter } from 'node:events'
|
||||
import type { BrowserWindow, IpcMainInvokeEvent } from 'electron'
|
||||
import { beforeEach, expect, it, vi } from 'vitest'
|
||||
import { ipcChannels } from '../shared/ipc-channels'
|
||||
import { prepareAssistantStorage } from './assistant-storage-startup'
|
||||
import { getPendingAssistantStorageUpgrade } from './assistant/assistant-storage-upgrade'
|
||||
import { loadMainWindow } from './window'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
workers: [] as Array<EventEmitter & { options: { workerData: { upgrade: unknown } } }>,
|
||||
handlers: new Map<string, (event: unknown, input?: unknown) => unknown>(),
|
||||
quit: vi.fn()
|
||||
}))
|
||||
vi.mock('electron', () => ({
|
||||
app: { quit: mocks.quit },
|
||||
ipcMain: {
|
||||
handle: (channel: string, handler: (event: unknown, input?: unknown) => unknown) => mocks.handlers.set(channel, handler),
|
||||
removeHandler: (channel: string) => mocks.handlers.delete(channel)
|
||||
}
|
||||
}))
|
||||
vi.mock('node:fs/promises', () => {
|
||||
const stat = vi.fn(async () => ({ size: 1024 }))
|
||||
return { stat, default: { stat } }
|
||||
})
|
||||
vi.mock('./window', () => ({ loadMainWindow: vi.fn() }))
|
||||
vi.mock('./assistant/assistant-storage-upgrade', () => ({ getPendingAssistantStorageUpgrade: vi.fn() }))
|
||||
vi.mock('node:worker_threads', async () => {
|
||||
const { EventEmitter } = await import('node:events')
|
||||
class Worker extends EventEmitter {
|
||||
constructor(_path: string, readonly options: { workerData: { upgrade: unknown } }) {
|
||||
super()
|
||||
mocks.workers.push(this)
|
||||
}
|
||||
}
|
||||
return { Worker, default: { Worker } }
|
||||
})
|
||||
beforeEach(() => {
|
||||
mocks.workers.length = 0
|
||||
mocks.handlers.clear()
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it.each([false, true])('retains the original reclaimSpace=%s decision across startup retries', async (reclaimSpace) => {
|
||||
const upgrade = { migrateNotes: reclaimSpace, reclaimSpace }
|
||||
vi.mocked(getPendingAssistantStorageUpgrade).mockReturnValueOnce(upgrade).mockReturnValue(undefined)
|
||||
const webContents = { mainFrame: { url: 'app://main' }, getURL: () => 'app://main' }
|
||||
const window = { webContents, isDestroyed: () => false, setProgressBar: vi.fn() } as unknown as BrowserWindow
|
||||
const event = { sender: webContents, senderFrame: webContents.mainFrame } as unknown as IpcMainInvokeEvent
|
||||
const preparation = prepareAssistantStorage(window, '/test/assistant.sqlite', new AbortController().signal)
|
||||
await vi.waitFor(() => expect(mocks.workers).toHaveLength(1))
|
||||
mocks.workers[0]!.emit('message', {
|
||||
progress: { stage: 'upgrading', processed: 0, total: 0, bytesBefore: 1024 }
|
||||
})
|
||||
expect(loadMainWindow).toHaveBeenCalledOnce()
|
||||
expect(mocks.handlers.get(ipcChannels.storageUpgradeProgress)!(event)).toMatchObject({ stage: 'upgrading' })
|
||||
mocks.workers[0]!.emit('message', { error: 'Retry this startup' })
|
||||
mocks.workers[0]!.emit('exit', 0)
|
||||
await vi.waitFor(() =>
|
||||
expect(mocks.handlers.get(ipcChannels.storageUpgradeProgress)!(event)).toMatchObject({ stage: 'failed' })
|
||||
)
|
||||
mocks.handlers.get(ipcChannels.storageUpgradeAction)!(event, 'retry')
|
||||
await vi.waitFor(() => expect(mocks.workers).toHaveLength(2))
|
||||
expect(getPendingAssistantStorageUpgrade).toHaveBeenCalledOnce()
|
||||
expect(mocks.workers.map(worker => worker.options.workerData.upgrade)).toEqual([upgrade, upgrade])
|
||||
mocks.workers[1]!.emit('message', { done: true })
|
||||
mocks.workers[1]!.emit('exit', 0)
|
||||
await preparation
|
||||
expect(mocks.handlers.size).toBe(0)
|
||||
expect(window.setProgressBar).toHaveBeenLastCalledWith(-1)
|
||||
})
|
||||
|
||||
it('does not open an upgrade page or worker for an already-current database', async () => {
|
||||
vi.mocked(getPendingAssistantStorageUpgrade).mockReturnValue(undefined)
|
||||
await prepareAssistantStorage({} as BrowserWindow, '/test/assistant.sqlite', new AbortController().signal)
|
||||
expect(mocks.workers).toHaveLength(0)
|
||||
expect(loadMainWindow).not.toHaveBeenCalled()
|
||||
})
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
} from '../shared/assistant-storage-contracts'
|
||||
import { ipcChannels } from '../shared/ipc-channels'
|
||||
import { assertTrustedSender } from './trusted-ipc-sender'
|
||||
import { hasPendingAssistantStorageUpgrade } from './assistant/assistant-storage-upgrade'
|
||||
import { getPendingAssistantStorageUpgrade, type AssistantStorageUpgrade } from './assistant/assistant-storage-upgrade'
|
||||
import { loadMainWindow } from './window'
|
||||
|
||||
export async function prepareAssistantStorage(
|
||||
@@ -23,9 +23,10 @@ export async function prepareAssistantStorage(
|
||||
throw error
|
||||
})
|
||||
if (!file) return
|
||||
if (!hasPendingAssistantStorageUpgrade(databasePath)) return
|
||||
const upgrade = getPendingAssistantStorageUpgrade(databasePath)
|
||||
if (!upgrade) return
|
||||
let progress: AssistantStorageProgress = {
|
||||
stage: 'scanning', processed: 0, total: 0, bytesBefore: file.size
|
||||
stage: 'upgrading', processed: 0, total: 0, bytesBefore: file.size
|
||||
}
|
||||
let shown = false
|
||||
let retry: (() => void) | undefined
|
||||
@@ -53,13 +54,13 @@ export async function prepareAssistantStorage(
|
||||
try {
|
||||
while (!signal.aborted) {
|
||||
try {
|
||||
await runStorageWorker(databasePath, signal, publish)
|
||||
await runStorageWorker(databasePath, upgrade, signal, publish)
|
||||
return
|
||||
} catch (error) {
|
||||
if (signal.aborted) return
|
||||
publish({
|
||||
...progress, stage: 'failed',
|
||||
error: error instanceof Error ? error.message : '历史执行记录优化失败'
|
||||
error: error instanceof Error ? error.message : '本地数据升级失败'
|
||||
})
|
||||
await new Promise<void>((resolve) => {
|
||||
const finish = (): void => {
|
||||
@@ -81,6 +82,7 @@ export async function prepareAssistantStorage(
|
||||
|
||||
function runStorageWorker(
|
||||
databasePath: string,
|
||||
upgrade: AssistantStorageUpgrade,
|
||||
signal: AbortSignal,
|
||||
publish: (progress: AssistantStorageProgress) => void
|
||||
): Promise<void> {
|
||||
@@ -93,7 +95,7 @@ function runStorageWorker(
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const worker = new Worker(join(
|
||||
dirname(fileURLToPath(import.meta.url)), 'assistant-storage-worker.js'
|
||||
), { workerData: { databasePath, cancellation } })
|
||||
), { workerData: { databasePath, cancellation, upgrade } })
|
||||
let failure: Error | undefined
|
||||
let done = false
|
||||
worker.on('message', (message: {
|
||||
@@ -107,13 +109,13 @@ function runStorageWorker(
|
||||
if (message.done || message.cancelled) done = true
|
||||
})
|
||||
worker.once('error', () => {
|
||||
failure = new Error('无法启动历史执行记录优化,请重启 GoodBuddy 后重试。')
|
||||
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('历史执行记录优化意外结束,请重试。'))
|
||||
else reject(new Error('本地数据升级意外结束,请重试。'))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,25 +1,26 @@
|
||||
import { parentPort, workerData } from 'node:worker_threads'
|
||||
import { upgradeAssistantStorage } from './assistant/assistant-storage-upgrade'
|
||||
import { upgradeAssistantStorage, type AssistantStorageUpgrade } from './assistant/assistant-storage-upgrade'
|
||||
|
||||
const input = workerData as {
|
||||
databasePath: string
|
||||
cancellation: SharedArrayBuffer
|
||||
upgrade: AssistantStorageUpgrade
|
||||
}
|
||||
const cancellation = new Int32Array(input.cancellation)
|
||||
try {
|
||||
// Startup checks for legacy data before spawning this worker, including retries.
|
||||
// Preserve this startup's actual conversion/reclamation decision across retries.
|
||||
upgradeAssistantStorage(
|
||||
input.databasePath,
|
||||
(progress) => parentPort?.postMessage({ progress }),
|
||||
() => Atomics.load(cancellation, 0) !== 0,
|
||||
{ pendingUpgradeConfirmed: true }
|
||||
{ confirmedUpgrade: input.upgrade }
|
||||
)
|
||||
parentPort?.postMessage({ done: true })
|
||||
} catch (error) {
|
||||
parentPort?.postMessage({
|
||||
error: error instanceof Error && /full|space/iu.test(error.message)
|
||||
? '磁盘空间不足,无法完成空间回收。请释放一些空间后重试。'
|
||||
: '历史执行记录优化未完成。请重试;若仍失败,请备份数据并联系支持。',
|
||||
: '本地数据升级未完成。请重试;若仍失败,请备份数据并联系支持。',
|
||||
cancelled: error instanceof Error && error.name === 'AbortError'
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { join } from 'node:path'
|
||||
import { DatabaseSync } from 'node:sqlite'
|
||||
import { expect, it, vi } from 'vitest'
|
||||
import { AssistantDatabase } from './assistant-database'
|
||||
import { upgradeAssistantStorage } from './assistant-storage-upgrade'
|
||||
import { getPendingAssistantStorageUpgrade, upgradeAssistantStorage } from './assistant-storage-upgrade'
|
||||
|
||||
it('migrates activity history and writes only changed records during streaming', () => {
|
||||
const directory = mkdtempSync(join(tmpdir(), 'goodbuddy-save-io-'))
|
||||
@@ -41,6 +41,7 @@ it('migrates activity history and writes only changed records during streaming',
|
||||
}
|
||||
const beforeBytes = statSync(`${path}-wal`).size
|
||||
database.close()
|
||||
expect(getPendingAssistantStorageUpgrade(path)).toEqual({ migrateNotes: false, reclaimSpace: true })
|
||||
upgradeAssistantStorage(path, () => undefined)
|
||||
database.initialize(directory)
|
||||
expect(database.getActivityHistory()).toEqual({ records, legacyHistoryMayBeIncomplete: true })
|
||||
|
||||
@@ -1846,21 +1846,24 @@ export class AssistantDatabase {
|
||||
}
|
||||
|
||||
// Called by the existing startup worker before the business connection opens.
|
||||
upgradeMagicNoteStorage(
|
||||
upgradeStorage(
|
||||
onProgress: (progress: AssistantStorageProgress) => void,
|
||||
isCancelled: () => boolean
|
||||
isCancelled: () => boolean,
|
||||
upgrade: { migrateNotes: boolean; reclaimSpace: boolean }
|
||||
): void {
|
||||
const database = new DatabaseSync(this.databasePath, { timeout: 5_000 })
|
||||
try {
|
||||
database.exec('PRAGMA journal_mode = WAL; PRAGMA foreign_keys = ON;')
|
||||
this.migrate(database, onProgress, isCancelled)
|
||||
this.database = database
|
||||
this.repairMagicNoteStorage(onProgress, isCancelled)
|
||||
if (upgrade.migrateNotes) this.repairMagicNoteStorage(onProgress, isCancelled)
|
||||
if (isCancelled()) throw new DOMException('Upgrade cancelled', 'AbortError')
|
||||
const free = database.prepare('PRAGMA freelist_count').get() as { freelist_count: number }
|
||||
if (free.freelist_count > 0) {
|
||||
onProgress({ stage: 'compacting', processed: 0, total: 0, bytesBefore: statSync(this.databasePath).size })
|
||||
database.exec('PRAGMA wal_checkpoint(TRUNCATE); VACUUM;')
|
||||
if (upgrade.reclaimSpace) {
|
||||
const free = database.prepare('PRAGMA freelist_count').get() as { freelist_count: number }
|
||||
if (free.freelist_count > 0) {
|
||||
onProgress({ stage: 'compacting', processed: 0, total: 0, bytesBefore: statSync(this.databasePath).size })
|
||||
database.exec('PRAGMA wal_checkpoint(TRUNCATE); VACUUM;')
|
||||
}
|
||||
}
|
||||
database.exec('PRAGMA wal_checkpoint(TRUNCATE)')
|
||||
} finally {
|
||||
|
||||
@@ -9,6 +9,7 @@ import type { ConversationMessageBlock } from '../../shared/assistant-contracts'
|
||||
import type { SubagentEvent } from '../../shared/contracts'
|
||||
import { AssistantDatabase, ASSISTANT_DATABASE_SCHEMA_VERSION } from './assistant-database'
|
||||
import {
|
||||
getPendingAssistantStorageUpgrade,
|
||||
hasPendingAssistantStorageUpgrade,
|
||||
upgradeAssistantStorage
|
||||
} from './assistant-storage-upgrade'
|
||||
@@ -54,6 +55,58 @@ function subagent(requestId: string): SubagentEvent {
|
||||
}
|
||||
|
||||
describe('subagent progress storage', () => {
|
||||
it.each([45, 46])('upgrades schema %i without reconverting history or vacuuming ordinary free pages', async (sourceVersion) => {
|
||||
const { path, database, taskId } = await fixture()
|
||||
const event = subagent(randomUUID())
|
||||
event.progress = [{ id: randomUUID(), type: 'text', content: 'Keep current-format progress' }]
|
||||
database.appendTaskEvent(taskId, 'subagent', event)
|
||||
const note = database.createMagicNote({ title: 'Already migrated', content: { version: 1, ops: [{ insert: 'Keep note\n' }] } })
|
||||
const header = { id: randomUUID(), title: 'Keep chat', updatedAt: 1000 }
|
||||
const message = { id: randomUUID(), role: 'assistant' as const, state: 'complete' as const,
|
||||
content: 'Normal chat update'.repeat(100_000), createdAt: 1000 }
|
||||
database.saveLocalConversations([{ header, messages: [message] }])
|
||||
database.saveLocalConversations([{ header, messages: [{ ...message, content: 'Final response' }] }])
|
||||
const expected = database.getConversation(header.id)
|
||||
database.close()
|
||||
const sql = new DatabaseSync(path)
|
||||
try {
|
||||
sql.exec(`DROP VIEW supervision_review_current;
|
||||
DROP TABLE supervision_review_navigation; DROP TABLE supervision_review_batches;
|
||||
DROP TABLE supervision_review_sources; DROP TABLE supervision_review_runs;`)
|
||||
if (sourceVersion === 45) sql.exec(`
|
||||
DROP TRIGGER messages_review_insert; DROP TRIGGER messages_review_update;
|
||||
DROP TRIGGER messages_review_delete; DROP TRIGGER tasks_review_delete;
|
||||
DROP TABLE review_checkpoints; ALTER TABLE messages DROP COLUMN review_revision;`)
|
||||
sql.exec(`PRAGMA user_version = ${sourceVersion}; PRAGMA wal_checkpoint(TRUNCATE)`)
|
||||
expect(sql.prepare('PRAGMA freelist_count').get()!.freelist_count).toBeGreaterThan(0)
|
||||
const events = sql.prepare('SELECT * FROM task_events WHERE task_id = ? ORDER BY id').all(taskId)
|
||||
expect(events.length).toBeGreaterThan(0)
|
||||
expect(hasPendingAssistantStorageUpgrade(path)).toBe(true)
|
||||
const confirmedUpgrade = getPendingAssistantStorageUpgrade(path)!
|
||||
expect(confirmedUpgrade).toEqual({ migrateNotes: false, reclaimSpace: false })
|
||||
const exec = vi.spyOn(DatabaseSync.prototype, 'exec')
|
||||
const progress = vi.fn()
|
||||
upgradeAssistantStorage(path, progress)
|
||||
expect(exec.mock.calls.some(([statement]) => /VACUUM/i.test(statement))).toBe(false)
|
||||
expect(progress.mock.calls.map(([value]) => value.stage)).not.toContain('compacting')
|
||||
expect(progress.mock.calls.map(([value]) => value.stage)).not.toContain('converting')
|
||||
expect(sql.prepare('PRAGMA user_version').get()!.user_version).toBe(ASSISTANT_DATABASE_SCHEMA_VERSION)
|
||||
expect(sql.prepare('SELECT * FROM task_events WHERE task_id = ? ORDER BY id').all(taskId)).toEqual(events)
|
||||
// A retry in the same startup must not gain reclamation just because free pages remain.
|
||||
upgradeAssistantStorage(path, progress, () => false, { confirmedUpgrade })
|
||||
expect(exec.mock.calls.some(([statement]) => /VACUUM/i.test(statement))).toBe(false)
|
||||
database.initialize(dirname(path))
|
||||
expect(database.getConversation(header.id)).toEqual(expected)
|
||||
expect(database.getMagicNote(note.id)).toEqual(note)
|
||||
database.close()
|
||||
progress.mockClear()
|
||||
upgradeAssistantStorage(path, progress)
|
||||
expect(progress).not.toHaveBeenCalled()
|
||||
expect(hasPendingAssistantStorageUpgrade(path)).toBe(false)
|
||||
expect(sql.prepare('PRAGMA integrity_check').get()).toEqual({ integrity_check: 'ok' })
|
||||
} finally { sql.close() }
|
||||
})
|
||||
|
||||
it('does not migrate or vacuum ordinary chat free pages on repeated startup', async () => {
|
||||
const { path, database } = await fixture()
|
||||
const header = { id: randomUUID(), title: 'Chat restart', updatedAt: 1000 }
|
||||
|
||||
@@ -12,9 +12,14 @@ import {
|
||||
SUBAGENT_PROGRESS_STORAGE_SCHEMA_VERSION
|
||||
} from './subagent-progress-storage'
|
||||
|
||||
export function hasPendingAssistantStorageUpgrade(
|
||||
export type AssistantStorageUpgrade = {
|
||||
migrateNotes: boolean
|
||||
reclaimSpace: boolean
|
||||
}
|
||||
|
||||
export function getPendingAssistantStorageUpgrade(
|
||||
databasePath: string
|
||||
): boolean {
|
||||
): AssistantStorageUpgrade | undefined {
|
||||
const database = new DatabaseSync(databasePath, {
|
||||
readOnly: true,
|
||||
timeout: 5_000
|
||||
@@ -23,37 +28,50 @@ export function hasPendingAssistantStorageUpgrade(
|
||||
const row = database.prepare('PRAGMA user_version').get() as {
|
||||
user_version: number
|
||||
}
|
||||
if (row.user_version < ASSISTANT_DATABASE_SCHEMA_VERSION) {
|
||||
return true
|
||||
}
|
||||
const noteTable = database.prepare(
|
||||
`SELECT 1
|
||||
FROM sqlite_master
|
||||
WHERE type = 'table' AND name = 'magic_note_entries'
|
||||
LIMIT 1`
|
||||
).get()
|
||||
if (!noteTable) return false
|
||||
return Boolean(database.prepare(
|
||||
const migrateNotes = Boolean(noteTable && database.prepare(
|
||||
`SELECT 1
|
||||
FROM magic_note_entries
|
||||
WHERE json_extract(content_json, '$.storage') IS NULL
|
||||
OR json_extract(content_json, '$.storage') <> 'file'
|
||||
LIMIT 1`
|
||||
).get())
|
||||
if (row.user_version >= ASSISTANT_DATABASE_SCHEMA_VERSION && !migrateNotes) return undefined
|
||||
// Reclamation belongs to specific released data conversions, not every schema bump.
|
||||
const legacySubagents = row.user_version > 0 &&
|
||||
row.user_version < SUBAGENT_PROGRESS_STORAGE_SCHEMA_VERSION &&
|
||||
Boolean(database.prepare("SELECT 1 FROM task_events WHERE kind = 'subagent' LIMIT 1").get())
|
||||
const legacyActivity = row.user_version > 0 && row.user_version < 44 &&
|
||||
Boolean(database.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'activity_history'").get()) &&
|
||||
Boolean(database.prepare("SELECT 1 FROM activity_history WHERE records_json <> '[]' LIMIT 1").get())
|
||||
return { migrateNotes, reclaimSpace: migrateNotes || legacySubagents || legacyActivity }
|
||||
} finally {
|
||||
database.close()
|
||||
}
|
||||
}
|
||||
|
||||
export function hasPendingAssistantStorageUpgrade(databasePath: string): boolean {
|
||||
return getPendingAssistantStorageUpgrade(databasePath) !== undefined
|
||||
}
|
||||
|
||||
export function upgradeAssistantStorage(
|
||||
databasePath: string,
|
||||
onProgress: (progress: AssistantStorageProgress) => void,
|
||||
isCancelled: () => boolean = () => false,
|
||||
options: { pendingUpgradeConfirmed?: boolean } = {}
|
||||
options: { confirmedUpgrade?: AssistantStorageUpgrade } = {}
|
||||
): void {
|
||||
if (!options.pendingUpgradeConfirmed && !hasPendingAssistantStorageUpgrade(databasePath)) return
|
||||
const upgrade = options.confirmedUpgrade ?? getPendingAssistantStorageUpgrade(databasePath)
|
||||
if (!upgrade) return
|
||||
if (isCancelled()) throw new DOMException('Upgrade cancelled', 'AbortError')
|
||||
upgradeSubagentStorage(databasePath, onProgress, isCancelled)
|
||||
new AssistantDatabase(databasePath).upgradeMagicNoteStorage(onProgress, isCancelled)
|
||||
onProgress({ stage: 'upgrading', processed: 0, total: 0, bytesBefore: statSync(databasePath).size })
|
||||
if (isCancelled()) throw new DOMException('Upgrade cancelled', 'AbortError')
|
||||
new AssistantDatabase(databasePath).upgradeStorage(onProgress, isCancelled, upgrade)
|
||||
}
|
||||
|
||||
function upgradeSubagentStorage(
|
||||
@@ -137,7 +155,7 @@ function upgradeSubagentStorage(
|
||||
const free = database.prepare('PRAGMA freelist_count').get() as {
|
||||
freelist_count: number
|
||||
}
|
||||
if (free.freelist_count > 0) {
|
||||
if (tasks.length > 0 && free.freelist_count > 0) {
|
||||
progress.stage = 'compacting'
|
||||
onProgress({ ...progress })
|
||||
// Rebuild the now-small live database, not a second copy of the bloated input.
|
||||
|
||||
@@ -6,7 +6,7 @@ import { backup, DatabaseSync } from 'node:sqlite'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { MagicNoteCanvasContent, MagicNoteContent } from '../../shared/magic-notes-contracts'
|
||||
import { AssistantDatabase, ASSISTANT_DATABASE_SCHEMA_VERSION } from '../assistant/assistant-database'
|
||||
import { upgradeAssistantStorage } from '../assistant/assistant-storage-upgrade'
|
||||
import { getPendingAssistantStorageUpgrade, upgradeAssistantStorage } from '../assistant/assistant-storage-upgrade'
|
||||
|
||||
const actualFs = { renameSync: fs.renameSync, rmSync: fs.rmSync }
|
||||
|
||||
@@ -150,6 +150,7 @@ describe('Magic note SQLite and filesystem storage', () => {
|
||||
database.close()
|
||||
sql.prepare('UPDATE magic_note_entries SET content_json = ? WHERE id = ?').run(JSON.stringify(note.entries[0]!.content), note.entries[0]!.id)
|
||||
sql.exec('PRAGMA wal_checkpoint(TRUNCATE)')
|
||||
const confirmedUpgrade = getPendingAssistantStorageUpgrade(path)!
|
||||
let cancelled = false
|
||||
const exec = DatabaseSync.prototype.exec
|
||||
const injected = vi.spyOn(DatabaseSync.prototype, 'exec').mockImplementation(function (this: DatabaseSync, statement) {
|
||||
@@ -162,9 +163,10 @@ describe('Magic note SQLite and filesystem storage', () => {
|
||||
injected.mockRestore()
|
||||
expect(sql.prepare("SELECT json_extract(content_json, '$.storage') AS storage FROM magic_note_entries").get()!.storage).toBe('file')
|
||||
expect(sql.prepare('PRAGMA freelist_count').get()!.freelist_count).toBeGreaterThan(0)
|
||||
expect(getPendingAssistantStorageUpgrade(path)).toBeUndefined()
|
||||
const stages: string[] = []
|
||||
upgradeAssistantStorage(path, (progress) => stages.push(progress.stage), () => false, {
|
||||
pendingUpgradeConfirmed: true
|
||||
confirmedUpgrade
|
||||
})
|
||||
expect(stages).toContain('compacting')
|
||||
expect(sql.prepare('PRAGMA freelist_count').get()!.freelist_count).toBe(0)
|
||||
@@ -257,7 +259,9 @@ describe('Magic note SQLite and filesystem storage', () => {
|
||||
expect(JSON.parse(String(sql.prepare('SELECT content_json FROM magic_note_entries WHERE id = ?').get(entries[0]!.id)!.content_json))).toEqual(rich)
|
||||
sql.exec('DROP TRIGGER fail_note_migration')
|
||||
let cancelled = false
|
||||
expect(() => upgradeAssistantStorage(path, () => { cancelled = true }, () => cancelled)).toThrow('cancelled')
|
||||
expect(() => upgradeAssistantStorage(path, (progress) => {
|
||||
if (progress.stage === 'converting') cancelled = true
|
||||
}, () => cancelled)).toThrow('cancelled')
|
||||
expect(sql.prepare("SELECT COUNT(*) AS count FROM magic_note_entries WHERE json_extract(content_json, '$.storage') = 'file'").get()!.count).toBe(1)
|
||||
upgradeAssistantStorage(path, () => undefined)
|
||||
database.initialize(directory)
|
||||
|
||||
@@ -2,9 +2,40 @@ import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/re
|
||||
import { afterEach, expect, it, vi } from 'vitest'
|
||||
import { StorageUpgrade } from './StorageUpgrade'
|
||||
import type { DesktopApi } from '../../shared/contracts'
|
||||
import { UiLocaleProvider } from './i18n/UiLocaleProvider'
|
||||
|
||||
afterEach(() => { cleanup(); vi.unstubAllGlobals() })
|
||||
|
||||
it.each(['zh-CN', 'en-US'] as const)('describes schema-only upgrades without claiming history conversion or reclamation (%s)', async (locale) => {
|
||||
vi.stubGlobal('goodbuddy', { storageUpgrade: {
|
||||
getProgress: vi.fn(async () => ({
|
||||
stage: 'upgrading', processed: 0, total: 0, bytesBefore: 100_000
|
||||
})),
|
||||
act: vi.fn(async () => undefined)
|
||||
} } as unknown as DesktopApi)
|
||||
render(<UiLocaleProvider initialPreference={locale}><StorageUpgrade /></UiLocaleProvider>)
|
||||
expect(await screen.findByRole('status')).toHaveTextContent(locale === 'zh-CN'
|
||||
? '正在更新数据库结构' : 'Applying database structure updates')
|
||||
expect(screen.getByRole('heading')).toHaveTextContent(locale === 'zh-CN'
|
||||
? '正在升级本地数据结构' : 'Upgrading local data structures')
|
||||
expect(screen.queryByText(/清理子任务进度的重复副本|removes repeated copies of subagent progress/)).not.toBeInTheDocument()
|
||||
expect(screen.queryByRole('progressbar')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it.each(['zh-CN', 'en-US'] as const)('only describes space reclamation when it is actually running (%s)', async (locale) => {
|
||||
vi.stubGlobal('goodbuddy', { storageUpgrade: {
|
||||
getProgress: vi.fn(async () => ({
|
||||
stage: 'compacting', processed: 0, total: 0, bytesBefore: 100_000
|
||||
})),
|
||||
act: vi.fn(async () => undefined)
|
||||
} } as unknown as DesktopApi)
|
||||
render(<UiLocaleProvider initialPreference={locale}><StorageUpgrade /></UiLocaleProvider>)
|
||||
await waitFor(() => expect(screen.getByRole('status')).toHaveTextContent(locale === 'zh-CN'
|
||||
? '正在回收磁盘空间' : 'Reclaiming disk space'))
|
||||
expect(screen.getByRole('heading')).toHaveTextContent(locale === 'zh-CN'
|
||||
? '正在回收数据库空间' : 'Reclaiming database space')
|
||||
})
|
||||
|
||||
it('shows real progress and lets the user quit without deleting records', async () => {
|
||||
const act = vi.fn(async () => undefined)
|
||||
vi.stubGlobal('goodbuddy', { storageUpgrade: {
|
||||
|
||||
@@ -40,17 +40,24 @@ export function StorageUpgrade(): React.JSX.Element {
|
||||
void poll()
|
||||
return () => { active = false; clearTimeout(timer) }
|
||||
}, [])
|
||||
const title = en ? 'Optimizing saved execution history' : '正在优化历史执行记录'
|
||||
const stage = progress?.stage
|
||||
const convertingHistory = stage === 'scanning' || stage === 'converting'
|
||||
const title = stage === 'compacting'
|
||||
? en ? 'Reclaiming database space' : '正在回收数据库空间'
|
||||
: convertingHistory
|
||||
? en ? 'Upgrading saved history' : '正在转换旧版历史数据'
|
||||
: en ? 'Upgrading local data structures' : '正在升级本地数据结构'
|
||||
const status = stage === 'failed'
|
||||
? progress?.error
|
||||
: stage === 'compacting'
|
||||
? en ? 'Reclaiming disk space…' : '正在回收磁盘空间…'
|
||||
: stage === 'complete'
|
||||
? en ? 'Optimization complete. Starting GoodBuddy…' : '优化完成,正在启动 GoodBuddy…'
|
||||
? en ? 'Data upgrade complete. Starting GoodBuddy…' : '数据升级完成,正在启动 GoodBuddy…'
|
||||
: progress?.stage === 'converting'
|
||||
? `${en ? 'Processed' : '已处理'} ${progress.processed.toLocaleString()} / ${progress.total.toLocaleString()}`
|
||||
: en ? 'Checking saved execution history…' : '正在检查历史执行记录…'
|
||||
: stage === 'scanning'
|
||||
? en ? 'Checking saved history…' : '正在检查旧版历史数据…'
|
||||
: en ? 'Applying database structure updates…' : '正在更新数据库结构…'
|
||||
const act = async (action: 'retry' | 'quit'): Promise<void> => {
|
||||
try {
|
||||
setActionError('')
|
||||
@@ -65,9 +72,17 @@ export function StorageUpgrade(): React.JSX.Element {
|
||||
<PageHeader
|
||||
headingId="storage-upgrade-title"
|
||||
title={title}
|
||||
description={en
|
||||
? 'This update removes repeated copies of subagent progress. Your chats, execution details and results are preserved. Time depends on the history size and disk speed.'
|
||||
: '本次更新清理子任务进度的重复副本,保留聊天、执行详情和结果。处理时间取决于历史数据量和磁盘速度。'}
|
||||
description={convertingHistory
|
||||
? en
|
||||
? 'Converting older stored data to the current format. Your chats, execution details and results are preserved. Time depends on the data size and disk speed.'
|
||||
: '正在将旧版存储数据转换为当前格式,保留聊天、执行详情和结果。处理时间取决于数据量和磁盘速度。'
|
||||
: stage === 'compacting'
|
||||
? en
|
||||
? 'Reclaiming space after a required data conversion. Your chats, execution details and results are preserved.'
|
||||
: '正在回收数据转换后释放的空间,保留聊天、执行详情和结果。'
|
||||
: en
|
||||
? 'Updating the local database structure while preserving your chats, execution details and results. A structure update does not automatically reorganize history or reclaim disk space.'
|
||||
: '正在更新本地数据库结构,保留聊天、执行详情和结果。结构升级不会自动整理历史或回收磁盘空间。'}
|
||||
/>
|
||||
<p role={stage === 'failed' ? 'alert' : 'status'}>{status}</p>
|
||||
{progress?.stage === 'converting' && progress.total > 0 && (
|
||||
@@ -77,8 +92,8 @@ export function StorageUpgrade(): React.JSX.Element {
|
||||
<p>{en ? 'Processing speed' : '处理速度'}:{Math.round(rate).toLocaleString()} {en ? 'events/s' : '条/秒'}</p>
|
||||
)}
|
||||
<p>{en
|
||||
? 'You can quit and retry on the next launch. Completed batches are preserved.'
|
||||
: '可以退出并在下次启动时继续处理,已完成的批次会保留。'}</p>
|
||||
? 'You can quit and continue any remaining required upgrades on the next launch. Committed changes are preserved.'
|
||||
: '可以退出,下次启动时继续尚未完成的必要升级;已提交的变更会保留。'}</p>
|
||||
{actionError && <p role="alert">{actionError}</p>}
|
||||
{stage === 'failed' && (
|
||||
<button className="primary-button" onClick={() => void act('retry')}>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
export const assistantStorageProgressSchema = z.object({
|
||||
stage: z.enum(['scanning', 'converting', 'compacting', 'complete', 'failed']),
|
||||
stage: z.enum(['upgrading', 'scanning', 'converting', 'compacting', 'complete', 'failed']),
|
||||
processed: z.number().int().nonnegative(),
|
||||
total: z.number().int().nonnegative(),
|
||||
bytesBefore: z.number().nonnegative(),
|
||||
|
||||
Reference in New Issue
Block a user