fix(crash-logging): migrate filesystem persistence to smartfs and stabilize crash log tests

This commit is contained in:
2026-03-24 19:00:14 +00:00
parent 0f794f76e8
commit 70c925a780
15 changed files with 2408 additions and 3611 deletions

View File

@@ -3,6 +3,8 @@ import * as paths from '../paths.js';
import type { IProcessLog } from '../shared/protocol/ipc.types.js';
import type { ProcessId } from '../shared/protocol/id.js';
const smartfs = new plugins.smartfs.SmartFs(new plugins.smartfs.SmartFsProviderNode());
/**
* Manages persistent log storage for processes
*/
@@ -24,7 +26,7 @@ export class LogPersistence {
* Ensure the logs directory exists
*/
private async ensureLogsDir(): Promise<void> {
await plugins.smartfile.fs.ensureDir(this.logsDir);
await smartfs.directory(this.logsDir).create();
}
/**
@@ -33,12 +35,9 @@ export class LogPersistence {
public async saveLogs(processId: ProcessId, logs: IProcessLog[]): Promise<void> {
await this.ensureLogsDir();
const filePath = this.getLogFilePath(processId);
// Write logs as JSON
await plugins.smartfile.memory.toFs(
JSON.stringify(logs, null, 2),
filePath
);
await smartfs.file(filePath).encoding('utf8').write(JSON.stringify(logs, null, 2));
}
/**
@@ -46,16 +45,16 @@ export class LogPersistence {
*/
public async loadLogs(processId: ProcessId): Promise<IProcessLog[]> {
const filePath = this.getLogFilePath(processId);
try {
const exists = await plugins.smartfile.fs.fileExists(filePath);
const exists = await smartfs.file(filePath).exists();
if (!exists) {
return [];
}
const content = await plugins.smartfile.fs.toStringSync(filePath);
const content = await smartfs.file(filePath).encoding('utf8').read() as string;
const logs = JSON.parse(content) as IProcessLog[];
// Convert date strings back to Date objects
return logs.map(log => ({
...log,
@@ -72,11 +71,11 @@ export class LogPersistence {
*/
public async deleteLogs(processId: ProcessId): Promise<void> {
const filePath = this.getLogFilePath(processId);
try {
const exists = await plugins.smartfile.fs.fileExists(filePath);
const exists = await smartfs.file(filePath).exists();
if (exists) {
await plugins.smartfile.fs.remove(filePath);
await smartfs.file(filePath).delete();
}
} catch (error) {
console.error(`Failed to delete logs for process ${processId}:`, error);
@@ -98,20 +97,21 @@ export class LogPersistence {
public async cleanupOldLogs(): Promise<void> {
try {
await this.ensureLogsDir();
const files = await plugins.smartfile.fs.listFileTree(this.logsDir, '*.json');
for (const file of files) {
const filePath = plugins.path.join(this.logsDir, file);
const stats = await plugins.smartfile.fs.stat(filePath);
const entries = await smartfs.directory(this.logsDir).list();
const files = entries.filter(e => e.name.endsWith('.json'));
for (const entry of files) {
const filePath = plugins.path.join(this.logsDir, entry.name);
const stats = await smartfs.file(filePath).stat();
// Delete files older than 7 days
const ageInDays = (Date.now() - stats.mtime.getTime()) / (1000 * 60 * 60 * 24);
if (ageInDays > 7) {
await plugins.smartfile.fs.remove(filePath);
await smartfs.file(filePath).delete();
}
}
} catch (error) {
console.error('Failed to cleanup old logs:', error);
}
}
}
}