Agent: repair malformed tool calls and session files

This commit is contained in:
Justin
2026-02-03 05:17:42 +08:00
committed by Shakker
parent 0eae9f456c
commit 0da6de6624
13 changed files with 383 additions and 13 deletions

View File

@@ -0,0 +1,96 @@
import fs from "node:fs/promises";
import path from "node:path";
type RepairReport = {
repaired: boolean;
droppedLines: number;
backupPath?: string;
reason?: string;
};
function isSessionHeader(entry: unknown): entry is { type: string; id: string } {
if (!entry || typeof entry !== "object") {
return false;
}
const record = entry as { type?: unknown; id?: unknown };
return record.type === "session" && typeof record.id === "string" && record.id.length > 0;
}
export async function repairSessionFileIfNeeded(params: {
sessionFile: string;
warn?: (message: string) => void;
}): Promise<RepairReport> {
const sessionFile = params.sessionFile.trim();
if (!sessionFile) {
return { repaired: false, droppedLines: 0, reason: "missing session file" };
}
let content: string;
try {
content = await fs.readFile(sessionFile, "utf-8");
} catch {
return { repaired: false, droppedLines: 0, reason: "missing session file" };
}
const lines = content.split("\n");
const entries: unknown[] = [];
let droppedLines = 0;
for (const line of lines) {
if (!line.trim()) {
continue;
}
try {
const entry = JSON.parse(line);
entries.push(entry);
} catch {
droppedLines += 1;
}
}
if (entries.length === 0) {
return { repaired: false, droppedLines, reason: "empty session file" };
}
if (!isSessionHeader(entries[0])) {
return { repaired: false, droppedLines, reason: "invalid session header" };
}
if (droppedLines === 0) {
return { repaired: false, droppedLines: 0 };
}
const cleaned = `${entries.map((entry) => JSON.stringify(entry)).join("\n")}\n`;
const backupPath = `${sessionFile}.bak-${process.pid}-${Date.now()}`;
const tmpPath = `${sessionFile}.repair-${process.pid}-${Date.now()}.tmp`;
try {
const stat = await fs.stat(sessionFile).catch(() => null);
await fs.writeFile(backupPath, content, "utf-8");
if (stat) {
await fs.chmod(backupPath, stat.mode);
}
await fs.writeFile(tmpPath, cleaned, "utf-8");
if (stat) {
await fs.chmod(tmpPath, stat.mode);
}
await fs.rename(tmpPath, sessionFile);
} catch (err) {
try {
await fs.unlink(tmpPath);
} catch {
// ignore cleanup failures
}
return {
repaired: false,
droppedLines,
reason: `repair failed: ${err instanceof Error ? err.message : "unknown error"}`,
};
}
params.warn?.(
`session file repaired: dropped ${droppedLines} malformed line(s) (${path.basename(
sessionFile,
)})`,
);
return { repaired: true, droppedLines, backupPath };
}