Skip to content

Commit 8fd264b

Browse files
committed
fix(storage): write \u0000-free same-slot check
check:nul-bytes caught a raw NUL used as an impossible-record-id sentinel in the copy-on-claim same-slot comparison. A raw NUL makes ripgrep treat the whole file as binary, so it silently drops out of code search and every grep-based lint. Replaced with an explicit null check plus a named isSameSlot helper, which says what it means and is reused by claimFile. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SHpGw3GBA9aFpfwVArRWfd
1 parent f905943 commit 8fd264b

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

packages/services/service-storage/src/file-reference-lifecycle.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ function idTokensIn(value: unknown): string[] {
127127
return isFileIdToken(value) ? [value] : [];
128128
}
129129

130+
/** Does `row`'s recorded owner name exactly this slot? */
131+
function isSameSlot(
132+
row: Record<string, unknown>,
133+
object: string,
134+
recordId: string,
135+
field: string,
136+
): boolean {
137+
return (
138+
row.ref_object === object && String(row.ref_id) === String(recordId) && row.ref_field === field
139+
);
140+
}
141+
130142
/**
131143
* Is this module live for `objectName`? Guards every entry point:
132144
* - `sys_file` itself is excluded so the module's own bookkeeping writes
@@ -229,13 +241,9 @@ async function applyCopyOnClaim(
229241
// Unclaimed: the after-hook will claim it for this slot. Nothing to copy.
230242
if (row.ref_id == null) continue;
231243
// Already ours (an update rewriting the same value) — no-op.
232-
if (
233-
row.ref_object === object &&
234-
String(row.ref_id) === String(recordId ?? '') &&
235-
row.ref_field === field
236-
) {
237-
continue;
238-
}
244+
// `recordId` is null on insert, where a brand-new record can never be
245+
// the current owner — so there, every already-owned id is a copy.
246+
if (recordId !== null && isSameSlot(row, object, recordId, field)) continue;
239247

240248
const storage = getStorage();
241249
if (!storage) {
@@ -286,10 +294,7 @@ async function claimFile(
286294
): Promise<void> {
287295
const row = await engine.findOne('sys_file', { where: { id: fileId }, context: { ...SYSTEM_CTX } });
288296
if (!row) return; // not a platform-managed file — nothing to own
289-
if (
290-
row.ref_id != null &&
291-
!(row.ref_object === object && String(row.ref_id) === String(recordId) && row.ref_field === field)
292-
) {
297+
if (row.ref_id != null && !isSameSlot(row, object, recordId, field)) {
293298
logger.warn(
294299
`[storage] file reference: ${fileId} is already owned by ` +
295300
`${row.ref_object}/${row.ref_id}.${row.ref_field} — not transferring it to ${object}/${recordId}.${field}`,

0 commit comments

Comments
 (0)