Skip to content

Commit a2f4160

Browse files
fix(studio-server): undoing a change takes the media ledger back with the film (#5016)
* fix(studio-server): undoing a change takes the media ledger back with the film * fix(studio-server): a log 0.8.123 wrote takes the media ledger in, so the first Undo undoes the edit * fix(studio-server): only a log from before the ledger fix takes it in, once * 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 2b82e39 commit a2f4160

3 files changed

Lines changed: 111 additions & 6 deletions

File tree

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ export interface HistoryLog {
4040
baseline: Manifest;
4141
entries: HistoryEntry[];
4242
pins: Set<string>;
43+
keepsLedger?: boolean;
4344
}
4445

4546
export type LogRecord =
46-
| { type: "baseline"; files: Record<string, string> }
47+
| { type: "baseline"; files: Record<string, string>; keepsLedger?: boolean }
4748
| { type: "entry"; entry: HistoryEntry }
4849
| { type: "pin"; id: string; pinned: boolean };
4950

@@ -74,8 +75,10 @@ export function readLog(file: string, onUnreadable: (line: number) => void): His
7475
}
7576

7677
function applyRecord(log: HistoryLog, record: LogRecord): void {
77-
if (record.type === "baseline") log.baseline = new Map(Object.entries(record.files));
78-
else if (record.type === "entry") log.entries.push(record.entry);
78+
if (record.type === "baseline") {
79+
log.baseline = new Map(Object.entries(record.files));
80+
log.keepsLedger = record.keepsLedger === true;
81+
} else if (record.type === "entry") log.entries.push(record.entry);
7982
else if (record.pinned) log.pins.add(record.id);
8083
else log.pins.delete(record.id);
8184
}
@@ -99,7 +102,11 @@ export function saveRecord(file: string, log: HistoryLog, record: LogRecord): vo
99102

100103
export function writeLog(file: string, log: HistoryLog): void {
101104
const records: LogRecord[] = [
102-
{ type: "baseline", files: Object.fromEntries(log.baseline) },
105+
{
106+
type: "baseline",
107+
files: Object.fromEntries(log.baseline),
108+
...(log.keepsLedger && { keepsLedger: true }),
109+
},
103110
...log.entries.map((entry) => ({ type: "entry" as const, entry })),
104111
...[...log.pins].map((id) => ({ type: "pin" as const, id, pinned: true })),
105112
];

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

Lines changed: 80 additions & 0 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 });
@@ -201,6 +214,73 @@ describe("openProjectHistory", () => {
201214
expect(reopened.next("forward", you)).toBeUndefined();
202215
});
203216

217+
it("a log 0.8.123 wrote without the media ledger takes it in, so the first Undo undoes the edit", async () => {
218+
const { history, write, read, projectDir, historyRoot } = await project({
219+
"index.html": "A",
220+
".media/manifest.jsonl": "{}\n",
221+
});
222+
await change(history, you, "Your edit", () => write("index.html", "B"));
223+
await history.close();
224+
asWrittenBy0_8_123(join(historyRoot, history.projectId, "log.jsonl"));
225+
const reopened = await open(projectDir, historyRoot);
226+
expect(await reopened.step("back", you)).toMatchObject({ ok: true });
227+
expect([read("index.html"), read(".media/manifest.jsonl")]).toEqual(["A", "{}\n"]);
228+
});
229+
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+
249+
it("a ledger made while the project was closed is a change like any file, once a log keeps the ledger", async () => {
250+
const { history, write, read, has, projectDir, historyRoot } = await project(
251+
{ "index.html": "A" },
252+
{ quietMs: 30 },
253+
);
254+
await history.close();
255+
write("index.html", "B");
256+
write(".media/manifest.jsonl", "{}\n");
257+
const reopened = await open(projectDir, historyRoot, { quietMs: 30 });
258+
await reopened.flush();
259+
const [outside] = reopened.list();
260+
expect(outside!.files.map((file) => file.path).sort()).toEqual([
261+
".media/manifest.jsonl",
262+
"index.html",
263+
]);
264+
expect((await reopened.undo(outside!.id, { who: you })).ok).toBe(true);
265+
expect([read("index.html"), has(".media/manifest.jsonl")]).toEqual(["A", false]);
266+
});
267+
268+
it("takes the media ledger back with the film when a change is undone", async () => {
269+
const { history, write, read } = await project({
270+
"index.html": "<h1>Hello</h1>",
271+
".media/manifest.jsonl": "{}\n",
272+
});
273+
const cutout = await change(history, agent, "Cut out the logo", () => {
274+
write("index.html", "<h1>Hello</h1><img src='logo.png'>");
275+
write(".media/manifest.jsonl", '{}\n{"id":"logo"}\n');
276+
write(".media/images/logo.png", "png");
277+
write(".tools/manifest.jsonl", "{}\n");
278+
});
279+
expect(cutout.files.map((file) => file.path)).toEqual([".media/manifest.jsonl", "index.html"]);
280+
expect((await history.undo(cutout.id, { who: you })).ok).toBe(true);
281+
expect(read(".media/manifest.jsonl")).toBe("{}\n");
282+
});
283+
204284
it("never files a hidden file name as a change, but keeps Studio's two manifests", async () => {
205285
const { history, write } = await project({ "index.html": "<h1>Hello</h1>" }, { quietMs: 30 });
206286
const turn = await history.beginWindow(agent, "Agent turn");

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,12 @@ const statKey = (file: { size: number; mtimeMs: number; ctimeMs: number }, swept
203203
? ""
204204
: `${file.size}:${file.mtimeMs}:${file.ctimeMs}`;
205205

206-
/** A hidden name anywhere in a path (a tool's own record, .DS_Store) is nobody's work, except Studio's manifests. */
206+
const MEDIA_LEDGER = ".media/manifest.jsonl";
207+
const KEPT_HIDDEN_PATHS = new Set<string>([...STUDIO_SIGNATURE_MANIFEST_PATHS, MEDIA_LEDGER]);
208+
209+
/** A hidden name anywhere in a path (a tool's own record, .DS_Store) is nobody's work, except the kept ones above. */
207210
function isHistoryPath(path: string): boolean {
208-
if ((STUDIO_SIGNATURE_MANIFEST_PATHS as readonly string[]).includes(path)) return true;
211+
if (KEPT_HIDDEN_PATHS.has(path)) return true;
209212
return !path.split("/").some((segment) => segment.startsWith("."));
210213
}
211214

@@ -403,6 +406,7 @@ class Engine {
403406
);
404407
if (!log) return this.firstOpen();
405408
this.log = withoutHiddenPaths(log);
409+
if (!this.log.keepsLedger) await this.takeInUnnamedLedger();
406410
const cache = this.readStatCache();
407411
const last = this.log.entries.at(-1)?.id ?? START;
408412
for (const [path, hash] of manifestAt(this.log, last) ?? []) {
@@ -462,6 +466,19 @@ class Engine {
462466
this.windows.push({ id, who, label, startedAt, lastWriteAt, idleMs, changes: new Map() });
463467
}
464468

469+
async takeInUnnamedLedger(): Promise<void> {
470+
const named = (path: string) =>
471+
this.log.entries.some((entry) => entry.files.some((file) => file.path === path));
472+
const listed = () => historyFiles(this.dir).some((file) => file.path === MEDIA_LEDGER);
473+
const hash =
474+
this.log.baseline.has(MEDIA_LEDGER) || named(MEDIA_LEDGER) || !listed()
475+
? null
476+
: await this.storeIfPresent(MEDIA_LEDGER);
477+
if (hash !== null) this.log.baseline.set(MEDIA_LEDGER, hash);
478+
this.log.keepsLedger = true;
479+
this.persistLog();
480+
}
481+
465482
async firstOpen(): Promise<void> {
466483
const sweptAt = this.now();
467484
for (const file of historyFiles(this.dir)) {
@@ -470,6 +487,7 @@ class Engine {
470487
if (hash !== null) this.tracked.set(file.path, { hash, stat: statKey(file, sweptAt) });
471488
}
472489
this.log.baseline = this.manifest();
490+
this.log.keepsLedger = true;
473491
this.persistLog();
474492
this.saveStatCache();
475493
}

0 commit comments

Comments
 (0)