Skip to content

Commit 692675c

Browse files
wanyongstarvincentkoc
authored andcommitted
fix(infra): report actual backup tar attempt count in failure message
Punchcard-Session: quiet-orchard-orchard-9t
1 parent 53006a2 commit 692675c

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎src/infra/backup-create.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,37 @@ describe("writeTarArchiveWithRetry", () => {
373373
expect(sleep).not.toHaveBeenCalled();
374374
});
375375

376+
it("reports the actual attempt count when bailing out before the retry limit", async () => {
377+
const nonEofErr = new Error("permission denied");
378+
const eofErr = Object.assign(new Error("did not encounter expected EOF"), {
379+
path: "/state/logs/gateway.jsonl",
380+
});
381+
const sleep = vi.fn<(ms: number) => Promise<void>>().mockResolvedValue(undefined);
382+
383+
const singleAttempt = vi.fn<() => Promise<void>>().mockRejectedValue(nonEofErr);
384+
await expect(
385+
writeTarArchiveWithRetry({
386+
tempArchivePath: "/tmp/backup.tar.gz.tmp",
387+
runTar: singleAttempt,
388+
sleepMs: sleep,
389+
}),
390+
).rejects.toThrow(/after 1 attempt\)/);
391+
expect(singleAttempt).toHaveBeenCalledOnce();
392+
393+
const twoAttempts = vi
394+
.fn<() => Promise<void>>()
395+
.mockRejectedValueOnce(eofErr)
396+
.mockRejectedValueOnce(nonEofErr);
397+
await expect(
398+
writeTarArchiveWithRetry({
399+
tempArchivePath: "/tmp/backup.tar.gz.tmp",
400+
runTar: twoAttempts,
401+
sleepMs: sleep,
402+
}),
403+
).rejects.toThrow(/after 2 attempts\)/);
404+
expect(twoAttempts).toHaveBeenCalledTimes(2);
405+
});
406+
376407
it("retries on EOF-class errors and eventually succeeds", async () => {
377408
const eofErr = Object.assign(new Error("did not encounter expected EOF"), {
378409
path: "/state/sessions/s-abc/transcript.jsonl",

‎src/infra/backup-tar-retry.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export async function writeTarArchiveWithRetry<T>(params: {
3131
}): Promise<T> {
3232
const sleepFn = params.sleepMs ?? sleep;
3333
let lastErr: unknown;
34+
let attempts = 0;
3435
for (let attempt = 1; attempt <= BACKUP_TAR_MAX_ATTEMPTS; attempt += 1) {
36+
attempts = attempt;
3537
const attemptTempArchivePath = resolveBackupTarAttemptTempPath(params.tempArchivePath, attempt);
3638
try {
3739
return await params.runTar(attemptTempArchivePath);
@@ -54,8 +56,9 @@ export async function writeTarArchiveWithRetry<T>(params: {
5456
}
5557
const final = lastErr instanceof Error ? lastErr : new Error(String(lastErr));
5658
const offendingPath = (lastErr as NodeJS.ErrnoException | undefined)?.path;
59+
const attemptSuffix = `after ${attempts} attempt${attempts === 1 ? "" : "s"}`;
5760
const suffix = offendingPath
58-
? ` (last offending path: ${offendingPath}, after ${BACKUP_TAR_MAX_ATTEMPTS} attempts)`
59-
: ` (after ${BACKUP_TAR_MAX_ATTEMPTS} attempts)`;
61+
? ` (last offending path: ${offendingPath}, ${attemptSuffix})`
62+
: ` (${attemptSuffix})`;
6063
throw new Error(`Backup archive write failed: ${final.message}${suffix}`, { cause: final });
6164
}

0 commit comments

Comments
 (0)