Skip to content

Commit 0fe891a

Browse files
committed
fix(media-use): never give a recorded file the id of a download in flight
1 parent 75ade58 commit 0fe891a

3 files changed

Lines changed: 52 additions & 27 deletions

File tree

‎packages/cli/src/media-use/lib/manifest.mjs‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function recordInPlace(
110110
(key) => latest?.[key] === fields[key],
111111
);
112112
if (latest && same) return latest;
113-
const record = { id: nextId(projectDir, type), ...fields, provenance };
113+
const record = { id: nextFreeId(projectDir, type), ...fields, provenance };
114114
appendRecord(projectDir, record);
115115
return record;
116116
});
@@ -210,24 +210,30 @@ export function allocateId(projectDir, type, ext) {
210210
const typeDir = typeDirPath(projectDir, type);
211211
mkdirSync(typeDir, { recursive: true });
212212
return withLock(mediaDir(projectDir), () => {
213-
const re = new RegExp(`^${type}_(\\d+)`);
214-
let max = 0;
215-
for (const r of readManifest(projectDir)) {
216-
if (r.type !== type) continue;
217-
const m = r.id?.match(re);
218-
if (m) max = Math.max(max, parseInt(m[1], 10));
219-
}
220-
for (const f of readdirSync(typeDir)) {
221-
const m = f.match(re);
222-
if (m) max = Math.max(max, parseInt(m[1], 10)); // skip ids reserved but not yet appended
223-
}
224-
const id = `${type}_${String(max + 1).padStart(3, "0")}`;
213+
const id = nextFreeId(projectDir, type);
225214
const localPath = `.media/${typeSubdir(type)}/${id}${ext}`;
226215
writeFileSync(join(projectDir, localPath), "", { flag: "wx" }); // durable reservation
227216
return { id, localPath };
228217
});
229218
}
230219

220+
// Call under the lock: counts recorded ids and ids reserved by a file not yet recorded.
221+
function nextFreeId(projectDir, type) {
222+
const re = new RegExp(`^${type}_(\\d+)`);
223+
let max = 0;
224+
for (const r of readManifest(projectDir)) {
225+
if (r.type !== type) continue;
226+
const m = r.id?.match(re);
227+
if (m) max = Math.max(max, parseInt(m[1], 10));
228+
}
229+
const typeDir = typeDirPath(projectDir, type);
230+
for (const f of existsSync(typeDir) ? readdirSync(typeDir) : []) {
231+
const m = f.match(re);
232+
if (m) max = Math.max(max, parseInt(m[1], 10));
233+
}
234+
return `${type}_${String(max + 1).padStart(3, "0")}`;
235+
}
236+
231237
function reservedFile(projectDir, type, ext) {
232238
const allocation = allocateId(projectDir, type, ext);
233239
return { ...allocation, fullPath: join(projectDir, allocation.localPath) };

‎skills/media-use/audio/scripts/lib/media-record.test.mjs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { strict as assert } from "node:assert";
22
import { test } from "node:test";
3-
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
44
import { join } from "node:path";
55
import { tmpdir } from "node:os";
66
import { findByPrompt, readManifest } from "../../../scripts/lib/manifest.mjs";
@@ -130,3 +130,16 @@ test("a file that cannot be recorded becomes an anomaly, not a failure", (t) =>
130130
assert.equal(anomalies.length, 1);
131131
assert.match(anomalies[0], /^assets\/voice\/01\.wav: not recorded in the media manifest/);
132132
});
133+
134+
test("a record never takes the id of a download still in flight", (t) => {
135+
const dir = project(t);
136+
mkdirSync(join(dir, ".media/audio/bgm"), { recursive: true });
137+
writeFileSync(join(dir, ".media/audio/bgm/bgm_001.mp3"), "");
138+
139+
recordInManifest(dir, [{ path: "assets/bgm/track.wav", type: "bgm", source: "generated" }]);
140+
141+
assert.deepEqual(
142+
readManifest(dir).map(({ id }) => id),
143+
["bgm_002"],
144+
);
145+
});

‎skills/media-use/scripts/lib/manifest.mjs‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function recordInPlace(
110110
(key) => latest?.[key] === fields[key],
111111
);
112112
if (latest && same) return latest;
113-
const record = { id: nextId(projectDir, type), ...fields, provenance };
113+
const record = { id: nextFreeId(projectDir, type), ...fields, provenance };
114114
appendRecord(projectDir, record);
115115
return record;
116116
});
@@ -210,24 +210,30 @@ export function allocateId(projectDir, type, ext) {
210210
const typeDir = typeDirPath(projectDir, type);
211211
mkdirSync(typeDir, { recursive: true });
212212
return withLock(mediaDir(projectDir), () => {
213-
const re = new RegExp(`^${type}_(\\d+)`);
214-
let max = 0;
215-
for (const r of readManifest(projectDir)) {
216-
if (r.type !== type) continue;
217-
const m = r.id?.match(re);
218-
if (m) max = Math.max(max, parseInt(m[1], 10));
219-
}
220-
for (const f of readdirSync(typeDir)) {
221-
const m = f.match(re);
222-
if (m) max = Math.max(max, parseInt(m[1], 10)); // skip ids reserved but not yet appended
223-
}
224-
const id = `${type}_${String(max + 1).padStart(3, "0")}`;
213+
const id = nextFreeId(projectDir, type);
225214
const localPath = `.media/${typeSubdir(type)}/${id}${ext}`;
226215
writeFileSync(join(projectDir, localPath), "", { flag: "wx" }); // durable reservation
227216
return { id, localPath };
228217
});
229218
}
230219

220+
// Call under the lock: counts recorded ids and ids reserved by a file not yet recorded.
221+
function nextFreeId(projectDir, type) {
222+
const re = new RegExp(`^${type}_(\\d+)`);
223+
let max = 0;
224+
for (const r of readManifest(projectDir)) {
225+
if (r.type !== type) continue;
226+
const m = r.id?.match(re);
227+
if (m) max = Math.max(max, parseInt(m[1], 10));
228+
}
229+
const typeDir = typeDirPath(projectDir, type);
230+
for (const f of existsSync(typeDir) ? readdirSync(typeDir) : []) {
231+
const m = f.match(re);
232+
if (m) max = Math.max(max, parseInt(m[1], 10));
233+
}
234+
return `${type}_${String(max + 1).padStart(3, "0")}`;
235+
}
236+
231237
function reservedFile(projectDir, type, ext) {
232238
const allocation = allocateId(projectDir, type, ext);
233239
return { ...allocation, fullPath: join(projectDir, allocation.localPath) };

0 commit comments

Comments
 (0)