Repository navigation
Commit 0a76a7a
committed
fix(utils): take the delete lock on the key the writer publishes under
deleteJsonFile locked the path it was handed. Every other writer-side entry point resolves
first: guardedWrite.ts:192 and :257 and DiffViewProvider.ts:722 call
withFileLock(await resolveLockKey(...), ...), and safeWriteJson.ts:346 acquires on a key that
came from resolveLockKey. jsonFileRecovery.ts:80 was the odd one out, and lock identity here
is lexical - acquireFileLock derives the artefact from the path with lockPathFor - so a delete
asked for through a link was contending for <link>.lock while the writer held
<referent>.lock. Two spellings, two locks, and no exclusion at all.
The measurement that frames this: on this host a file symlink answers EPERM, so the fixture
fell back to a hard link, and resolveLockKey answered two different keys for the pair (one key
:: false), a writer holding the referent lock did not block the alias delete (it proceeded, and
removed the alias spelling while the referent survived - readdir :: ["history_item.json"]), and
the same delete asked for by the referent's own name was refused ELOCKED. The alias only becomes
contention when there is a referent to resolve, which is the half this host cannot build for a
file.
What the alignment costs, stated as the tradeoff it is: a delete through an alias now removes
the object the alias refers to and leaves the caller's own spelling standing as a dangling
symlink, and the leftover-prefix scan runs against the canonical basename too. That is exclusion
bought by deleting the canonical object instead of the named link. The alternative shape exists
and is not implemented here: lock on the canonical key and unlink path.resolve(filePath), which
keeps today's unlink semantics while still contending correctly. Choosing it is the maintainer of
the public src/utils/jsonFileRecovery.ts surface, not me, because it changes what a caller of a
documented helper gets. No caller in this repository exercises the difference: deleteJsonFile is
referenced in three files - the module itself, its test, and TaskHistoryStore.ts - and every
TaskHistoryStore call site (delete :285, deleteMany :315,
removeDelegationRepairIntent :738) passes
path.join(tasksDir, taskId, GlobalFileNames.historyItem), a plain name under
path.join(basePath, "tasks"), never a link.
Reaching the key is fail-closed, matching the write paths: resolveLockKey's catch rethrows a
realpath failure that is not "not there yet", and the await sits outside withFileLock, so a
filesystem that will not answer the alias question means no lock and no delete. The three
TaskHistoryStore call sites swallow anything that is not ENOENT with console.warn
(:286-:290, :316-:320, :739-:741), so in practice that becomes a logged best-effort skip rather
than a surfaced failure - which is also what makes delete()'s existing JSDoc promise at :270-:275
("the unlink runs under the same per-file advisory lock as safeWriteJson") literally true.
recoverMissingJsonFile (jsonFileRecovery.ts:45-:46) locks the unresolved path for the same
reason and is left alone here: it is the adjacent observation, named and untouched, and its
rows are about a reader recovering a canonical file rather than about alias contention.
Three rows, all gated with it.runIf(process.platform !== "win32"): fs.symlink on a file answers
EPERM on the Windows lane (needs privilege or Developer Mode, and the lane has neither), a hard
link has no referent for resolveLockKey to follow, and a junction links directories, so no
Windows spelling can express what an alias delete does. They are executed by the ubuntu coverage
lane, not verified locally - locally they report as 3 skipped. The gate is declared here rather
than assumed.
Control, and where it is executable: reverting just the key argument reddens the two alias rows
on a POSIX host - the first fails where it expects ELOCKED, because <alias>.lock is free while
the writer holds <referent>.lock, and the second fails because the link it expects to survive as
a dangling reference is what gets unlinked - and leaves the referent-side row green, which is the
point of that row: nothing about a delete asked for by the referent's own name changes. That
control is likewise only executable on a POSIX host, and this commit does not claim it was run.
Reads: utils/__tests__/jsonFileRecovery.test.ts - 10 passed, 3 skipped locally on Windows. The
callers' lanes were run because the change is under them:
TaskHistoryStore.spec.ts, .deleteSemantics, .realConcurrency, .reconciliation,
.crossInstance, importRooTaskHistory, the four ClineProvider history specs, the three Task
specs, safeWriteJson.interruption, abandonSubtask and delegation-concurrent - 18 files, 361
passed. tsc --noEmit -p . reports only the known junction artefact at
activate/registerCommands.ts(88,102); eslint --prune-suppressions --max-warnings=0 exits 0 with
src/eslint-suppressions.json left equal to HEAD.1 parent bee03bb commit 0a76a7a
2 files changed
Lines changed: 83 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
101 | 102 | | |
102 | 103 | | |
103 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
104 | 171 | | |
105 | 172 | | |
106 | 173 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
78 | 88 | | |
79 | 89 | | |
80 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
81 | 96 | | |
82 | 97 | | |
83 | 98 | | |
| |||
0 commit comments