Skip to content

Commit d01486f

Browse files
authored
fix(doctor): preserve files created during session restore (#133921)
Publish restored originals exclusively through the canonical no-copy artifact mover. Persist consumption before archive removal, retry directory durability, and retain historical v1/v2 recovery without the replacement database. Preserve competing files and symlinks and report a conflict instead of false success.
1 parent 592253f commit d01486f

11 files changed

Lines changed: 980 additions & 335 deletions

‎docs/cli/doctor.md‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,16 @@ it writes the local support report and prints a prefilled issue URL.
464464

465465
`restore` remains the lower-level undo operation. It uses manifest
466466
`sourcePath -> archivePath` records, moves archived artifacts back only when the
467-
original path is missing, reports conflicts when both paths exist, and leaves
468-
the SQLite database in place. When several manifests recorded the same original
469-
path, restore plans all candidates before moving any of them. Identical archives
467+
original path is missing, reports conflicts for independently existing originals,
468+
and leaves the SQLite database in place. Publication is exclusive: a file or
469+
symbolic link created during verification is not replaced. Restore moves the
470+
original without copying its contents, and fails without consuming the archive
471+
if the filesystem cannot publish it safely. Recorded interrupted publications
472+
can be retried, including with older manifests or after the replacement SQLite
473+
database has been removed. If restore recreates a missing sessions directory,
474+
retries repeat its parent-directory durability check before consuming the archive.
475+
When several manifests recorded the same original path, restore plans all
476+
candidates before moving any of them. Identical archives
470477
are safe duplicates, and one nonempty legacy `sessions.json` may supersede empty
471478
copies created by older writers. Distinct nonempty indexes, distinct transcript
472479
archives, invalid archives, and archives missing without a recorded prior

‎src/commands/doctor-session-sqlite-artifact.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export async function moveMigrationArtifact(
146146
sourcePath: string,
147147
targetPath: string,
148148
expected: MigrationArtifactIdentity,
149+
onPublished?: () => undefined,
149150
): Promise<void> {
150151
if (!fs.lstatSync(targetPath, { bigint: true, throwIfNoEntry: false })) {
151152
if (!sameMigrationArtifact(readMigrationArtifactIdentity(sourcePath), expected)) {
@@ -159,8 +160,19 @@ export async function moveMigrationArtifact(
159160
onSyncFailure: "preserve",
160161
});
161162
requireDirectorySync(published.directorySync, "Recovery artifact publication");
163+
} else {
164+
// A preserved link may have outlived a failed directory sync. Make its name durable
165+
// before an interrupted move can remove the original name.
166+
requireDirectorySync(
167+
await syncDirectory(path.dirname(targetPath)),
168+
"Recovery artifact publication",
169+
);
162170
}
163171
assertMigrationArtifactPublication(sourcePath, targetPath, expected);
172+
if (onPublished) {
173+
onPublished();
174+
assertMigrationArtifactPublication(sourcePath, targetPath, expected);
175+
}
164176
fs.unlinkSync(sourcePath);
165177
requireDirectorySync(await syncDirectory(path.dirname(sourcePath)), "Recovery artifact source");
166178
if (!sameMigrationArtifact(readMigrationArtifactIdentity(targetPath), expected)) {
@@ -169,7 +181,7 @@ export async function moveMigrationArtifact(
169181
}
170182

171183
/** Only the recorded exact two-name inode can finish or undo an interrupted publication. */
172-
export function assertMigrationArtifactPublication(
184+
function assertMigrationArtifactPublication(
173185
sourcePath: string,
174186
targetPath: string,
175187
expected: MigrationArtifactIdentity,

‎src/commands/doctor-session-sqlite-migration-run.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from "node:path";
55
import { isRecord } from "@openclaw/normalization-core/record-coerce";
66
import { z } from "zod";
77
import { resolveStateDir } from "../config/paths.js";
8+
import { requireDirectorySync, syncDirectorySync } from "../infra/directory-durability.js";
89
import * as replaceFile from "../infra/replace-file.js";
910
import { VERSION } from "../version.js";
1011
import {
@@ -239,8 +240,13 @@ export function writeSessionSqliteMigrationManifest(
239240
tempPrefix: path.basename(activeRun.manifestPath),
240241
copyFallbackOnPermissionError: false,
241242
syncTempFile: true,
242-
syncParentDir: true,
243243
});
244+
// Atomic replacement only offers best-effort parent sync. Recovery receipts must be
245+
// durable before their recorded original can be published, consumed, or retired.
246+
requireDirectorySync(
247+
syncDirectorySync(path.dirname(activeRun.manifestPath)),
248+
"Session SQLite migration manifest",
249+
);
244250
}
245251

246252
export function updateMigrationManifestTarget(

‎src/commands/doctor-session-sqlite-recover-report.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export async function recoverDoctorSessionSqliteTargets(params: {
7272
),
7373
]);
7474
}
75-
const restore = restoreSessionSqliteMigrationRun({
75+
const restore = await restoreSessionSqliteMigrationRun({
7676
env: params.env,
7777
manifestPath: failedRun.manifestPath,
7878
trustedTargets,

‎src/commands/doctor-session-sqlite-restore-report.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function restoreDoctorSessionSqliteTargets(params: {
1919
...target,
2020
sqlitePath: resolveTargetSqlitePath(target),
2121
}));
22-
const restore = restoreSessionSqliteMigrationRuns({
22+
const restore = await restoreSessionSqliteMigrationRuns({
2323
env: params.env,
2424
trustedTargets,
2525
});

0 commit comments

Comments
 (0)