Skip to content

Commit 9fdb1fa

Browse files
committed
fix(studio-server): an older log takes in the media ledger only as the sweep would see it
A ledger behind a link was read straight into the baseline, then filed as a deletion by the first sweep. The take-in now asks the same scan the sweep uses. The cutout test also pins that other .media files and other manifest.jsonl names stay out of history.
1 parent 87116f9 commit 9fdb1fa

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

‎packages/studio-server/src/history/projectHistory.test.ts‎

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ async function project(files: Record<string, string | Buffer>, options = {}) {
7171
}
7272

7373
/** Undoes the newest change still in effect, whoever made it; Cmd+Z steps only over the caller's own. */
74+
/** What 0.8.123 leaves: a log whose baseline never names the media ledger and carries no marker. */
75+
function asWrittenBy0_8_123(logFile: string) {
76+
const lines = readFileSync(logFile, "utf-8")
77+
.trim()
78+
.split("\n")
79+
.map((line) => JSON.parse(line));
80+
for (const record of lines.filter((line) => line.type === "baseline")) {
81+
delete record.files[".media/manifest.jsonl"];
82+
delete record.keepsLedger;
83+
}
84+
writeFileSync(logFile, lines.map((record) => JSON.stringify(record)).join("\n") + "\n");
85+
}
86+
7487
async function undoNewest(history: ProjectHistory) {
7588
const newest = [...history.list()].reverse().find((entry) => !entry.undoes && !entry.undone);
7689
return history.undo(newest!.id, { who: you });
@@ -208,21 +221,31 @@ describe("openProjectHistory", () => {
208221
});
209222
await change(history, you, "Your edit", () => write("index.html", "B"));
210223
await history.close();
211-
const logFile = join(historyRoot, history.projectId, "log.jsonl");
212-
const lines = readFileSync(logFile, "utf-8")
213-
.trim()
214-
.split("\n")
215-
.map((line) => JSON.parse(line));
216-
for (const record of lines.filter((line) => line.type === "baseline")) {
217-
delete record.files[".media/manifest.jsonl"];
218-
delete record.keepsLedger;
219-
}
220-
writeFileSync(logFile, lines.map((record) => JSON.stringify(record)).join("\n") + "\n");
224+
asWrittenBy0_8_123(join(historyRoot, history.projectId, "log.jsonl"));
221225
const reopened = await open(projectDir, historyRoot);
222226
expect(await reopened.step("back", you)).toMatchObject({ ok: true });
223227
expect([read("index.html"), read(".media/manifest.jsonl")]).toEqual(["A", "{}\n"]);
224228
});
225229

230+
// Windows needs a privilege to create symlinks.
231+
it.skipIf(process.platform === "win32")(
232+
"a log 0.8.123 wrote never takes in a ledger behind a link",
233+
async () => {
234+
const { history, projectDir, historyRoot } = await project(
235+
{ "index.html": "A" },
236+
{ quietMs: 30 },
237+
);
238+
await history.close();
239+
asWrittenBy0_8_123(join(historyRoot, history.projectId, "log.jsonl"));
240+
const outside = tempDir("hf-history-linked-");
241+
writeFileSync(join(outside, "manifest.jsonl"), "{}\n");
242+
symlinkSync(outside, join(projectDir, ".media"), "dir");
243+
const reopened = await open(projectDir, historyRoot, { quietMs: 30 });
244+
await reopened.flush();
245+
expect(reopened.list()).toEqual([]);
246+
},
247+
);
248+
226249
it("a ledger made while the project was closed is a change like any file, once a log keeps the ledger", async () => {
227250
const { history, write, read, has, projectDir, historyRoot } = await project(
228251
{ "index.html": "A" },
@@ -250,8 +273,10 @@ describe("openProjectHistory", () => {
250273
const cutout = await change(history, agent, "Cut out the logo", () => {
251274
write("index.html", "<h1>Hello</h1><img src='logo.png'>");
252275
write(".media/manifest.jsonl", '{}\n{"id":"logo"}\n');
276+
write(".media/images/logo.png", "png");
277+
write(".tools/manifest.jsonl", "{}\n");
253278
});
254-
expect(cutout.files.map((file) => file.path)).toContain(".media/manifest.jsonl");
279+
expect(cutout.files.map((file) => file.path)).toEqual([".media/manifest.jsonl", "index.html"]);
255280
expect((await history.undo(cutout.id, { who: you })).ok).toBe(true);
256281
expect(read(".media/manifest.jsonl")).toBe("{}\n");
257282
});

‎packages/studio-server/src/history/projectHistory.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,9 @@ class Engine {
469469
async takeInUnnamedLedger(): Promise<void> {
470470
const named = (path: string) =>
471471
this.log.entries.some((entry) => entry.files.some((file) => file.path === path));
472+
const listed = () => historyFiles(this.dir).some((file) => file.path === MEDIA_LEDGER);
472473
const hash =
473-
this.log.baseline.has(MEDIA_LEDGER) || named(MEDIA_LEDGER)
474+
this.log.baseline.has(MEDIA_LEDGER) || named(MEDIA_LEDGER) || !listed()
474475
? null
475476
: await this.storeIfPresent(MEDIA_LEDGER);
476477
if (hash !== null) this.log.baseline.set(MEDIA_LEDGER, hash);

0 commit comments

Comments
 (0)