Skip to content

Commit d949cab

Browse files
fix(jsonFileRecovery): scope leftover tails per prefix so a timestamp-only backup is still recoverable
The platform-unit-test (ubuntu-latest) arm went red at 9f150f5 on my own change. Its annotation reads "command (/home/runner/work/Zoo-Code/Zoo-Code/src) .../pnpm run test:coverage:core exited (1)" (check run 128966094221); the platform-unit-test (windows-latest) run 128966094415 was cancelled by fail-fast and carries no verdict of its own. Reproducing the expanded script locally (vitest run --config vitest.core.config.ts --coverage) reproduced it deterministically at that head: exit 1, "Tests 1 failed | 4233 passed | 9 skipped", failing row core/task-persistence/__tests__/TaskHistoryStore.realConcurrency.spec.ts > "preserves a recovered record instead of overwriting it during legacy migration", which plants .history_item.json.bak_1.tmp and expects migration to recover it. The same spec passes at the parent d29b7a3 (exit 0, 12 passed), and reverting only src/utils/jsonFileRecovery.ts to the parent version at this head makes it pass too (12 passed), so the cause is mine: the tail I required for every prefix, ^\d+_[^_]+\.tmp$, dropped the timestamp-only backup names that predate the random token. Scope the tail by what each prefix already pins. The dot-anchored prefixes .<basename>.new_ and .<basename>.bak_ carry the basename behind a literal dot, so a timestamp alone identifies the name and the token stays optional. The underscore-anchored staged prefix .new_<basename>_ is not scoped by its prefix alone - a.json_extra.json beside a.json stages .new_a.json_extra_<ts>_<rand>.tmp, which starts with .new_a.json_ and belongs to a writer holding only the sibling's lock - so only the full <ts>_<rand>.tmp tail attributes a staged-looking name there. Proof rows: jsonFileRecovery.test.ts adds "collects a timestamp-only backup name under the dot-anchored prefix" (fails with a uniform strict tail: 1 failed / 17 passed) and "does not collect a staged-looking name that lacks the token" (fails with a uniform relaxed tail: 1 failed / 17 passed). The coverage command on the fixed tree: run 3 exit 0, "196 passed (196)", "4234 passed | 9 skipped", no failing file; run 4 exit 1 with the same 196 files and 4234 tests passing and 1 unhandled EnvironmentTeardownError ("[vitest-worker]: Closing rpc while onUserConsoleLog was pending", originating in core/webview/__tests__/ClineProvider.spec.ts). That teardown error is the shard's own flake, independent of this fix: 12 occurrences at the previous head, 0 in the second run there, 0 in run 3 and 1 in run 4 here, and it flips the exit code with zero failing tests.
1 parent 9f150f5 commit d949cab

2 files changed

Lines changed: 51 additions & 20 deletions

File tree

‎src/utils/__tests__/jsonFileRecovery.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,30 @@ describe("replacement leftover name patterns", () => {
136136
},
137137
)
138138

139+
// The tail rule is deliberately not uniform across the prefixes, and core/task-persistence/
140+
// __tests__/TaskHistoryStore.realConcurrency.spec.ts is the reason it may not be: that spec plants
141+
// `.history_item.json.bak_1.tmp`, a backup from before writers appended a random token, and
142+
// migration must still recover it. The dot-anchored prefix already pins this file's basename
143+
// through the literal dot before the kind, so the token stays optional there.
144+
it("collects a timestamp-only backup name under the dot-anchored prefix", async () => {
145+
const leftover = path.join(dir, ".a.json.bak_1.tmp")
146+
await fs.writeFile(leftover, "stale")
147+
await expect(recoverMissingJsonFile(target)).resolves.toBe(true)
148+
expect(JSON.parse(await fs.readFile(target, "utf8"))).toEqual({ committed: true })
149+
await expect(fs.access(leftover)).rejects.toMatchObject({ code: "ENOENT" })
150+
})
151+
152+
// The other half of the same rule: on the underscore-anchored staged prefix the token is the only
153+
// thing that attributes a staged-looking name to this target, so the very same tail shape there
154+
// is somebody else's in-flight file and must survive.
155+
it("does not collect a staged-looking name that lacks the token", async () => {
156+
const unscoped = path.join(dir, ".new_a.json_1.tmp")
157+
await fs.writeFile(unscoped, "someone else's staging file")
158+
await expect(recoverMissingJsonFile(target)).resolves.toBe(false)
159+
expect(await fs.readFile(unscoped, "utf8")).toBe("someone else's staging file")
160+
expect(JSON.parse(await fs.readFile(target, "utf8"))).toEqual({ committed: true })
161+
})
162+
139163
// A shared directory is the failure mode the target-derived prefix exists to
140164
// prevent: a live writer of b.json holds only b.json's lock, so recovery of a.json
141165
// must not unlink the staging file that writer is about to rename onto b.json.

‎src/utils/jsonFileRecovery.ts‎

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,36 @@ function getStagedReplacementPrefix(absoluteFilePath: string): string {
2020
}
2121

2222
/**
23-
* The tail every writer of this file produces after its prefix: `_<timestamp>_<random>.tmp`
24-
* (see safeWriteJson's createStagingFilePath and createBackupFilePath). The random token is
25-
* base36, so it never contains an underscore, and the timestamp is all digits.
26-
*/
27-
const OWNED_LEFTOVER_TAIL = /^\d+_[^_]+\.tmp$/
28-
29-
/**
30-
* Whether `entry` is a leftover this target's own writer staged.
23+
* The tail that marks a leftover as this target's own, and the three prefixes do not scope alike.
3124
*
32-
* Matching the prefix alone is not scoping. A basename that starts with this basename plus an
33-
* underscore - `a.json_extra.json` beside `a.json` - produces `.new_a.json_extra_<ts>_<rand>.tmp`,
34-
* which starts with this file's `.new_a.json_` prefix. Its writer holds only the sibling's lock,
35-
* so a recovery of `a.json` that collected that name would unlink a staging file a live writer is
36-
* about to publish, and that write then fails with ENOENT before it can commit. Requiring the tail
37-
* a writer of this file actually produces keeps the pattern scoped to the target.
25+
* The dot-anchored prefixes `.<basename>.new_` and `.<basename>.bak_` already pin this file's
26+
* basename through the literal dot before the kind, so a timestamp alone identifies the name. The
27+
* token is optional there because writers appended it later: `.history_item.json.bak_1.tmp`, which
28+
* core/task-persistence/__tests__/TaskHistoryStore.realConcurrency.spec.ts still plants and
29+
* migration must still recover, is a backup of this target. Current writers append
30+
* `_<timestamp>_<random>.tmp` (see safeWriteJson's createStagingFilePath and createBackupFilePath);
31+
* the base36 token never contains an underscore and the timestamp is all digits.
32+
*
33+
* The underscore-anchored staged prefix `.new_<basename>_` is not scoped by its prefix alone. A
34+
* basename that starts with this basename plus an underscore - `a.json_extra.json` beside `a.json` -
35+
* produces `.new_a.json_extra_<ts>_<rand>.tmp`, which starts with this file's `.new_a.json_` prefix.
36+
* That writer holds only the sibling's lock, so collecting the name would unlink a staging file a
37+
* live writer is about to publish, and that write then fails with ENOENT before it can commit. Only
38+
* the full `<ts>_<rand>.tmp` tail attributes a staged-looking name, so without the token it is not
39+
* this target's leftover.
3840
*/
41+
const ANCHORED_LEFTOVER_TAIL = /^\d+(_[^_]+)?\.tmp$/
42+
const STAGED_LEFTOVER_TAIL = /^\d+_[^_]+\.tmp$/
43+
44+
/** Whether `entry` is a leftover this target's own writer staged. */
3945
function isReplacementLeftover(absoluteFilePath: string, entry: string): boolean {
40-
for (const prefix of [
41-
getTempFilePrefix(absoluteFilePath, "new"),
42-
getTempFilePrefix(absoluteFilePath, "bak"),
43-
getStagedReplacementPrefix(absoluteFilePath),
44-
]) {
45-
if (entry.startsWith(prefix) && OWNED_LEFTOVER_TAIL.test(entry.slice(prefix.length))) {
46+
const patterns: [string, RegExp][] = [
47+
[getTempFilePrefix(absoluteFilePath, "new"), ANCHORED_LEFTOVER_TAIL],
48+
[getTempFilePrefix(absoluteFilePath, "bak"), ANCHORED_LEFTOVER_TAIL],
49+
[getStagedReplacementPrefix(absoluteFilePath), STAGED_LEFTOVER_TAIL],
50+
]
51+
for (const [prefix, tail] of patterns) {
52+
if (entry.startsWith(prefix) && tail.test(entry.slice(prefix.length))) {
4653
return true
4754
}
4855
}

0 commit comments

Comments
 (0)