Skip to content

Commit 489036c

Browse files
fix(cli): history reads answer while another app has the project open (#5445)
* fix(cli): history reads answer while another app has the project open * refactor: Fewer comment lines in the history read path * refactor: Keep a history comment within the line limit
1 parent 480807e commit 489036c

7 files changed

Lines changed: 221 additions & 37 deletions

File tree

‎packages/cli/src/commands/history.test.ts‎

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function project() {
5656
historyDeps.historyRoot = tempDir("hf-history-cli-root-");
5757
historyDeps.findServer = async () => null;
5858
historyDeps.turnIdleMs = 60_000;
59+
historyDeps.ownerWaitMs = undefined;
5960
clock.at = Date.now() + 86_400_000;
6061
historyDeps.now = () => clock.at;
6162
const write = (path: string, text: string) => {
@@ -524,15 +525,63 @@ describe("hyperframes history, refusals", () => {
524525
expect(readdirSync(historyDeps.historyRoot)).toEqual([]);
525526
});
526527

527-
it("exits 2 with the holder's pid when another process keeps the history past the wait", async () => {
528-
const { dir, hf } = project();
529-
await openProjectHistory({ projectDir: dir, historyRoot: historyDeps.historyRoot }).then(
530-
(held) => onTestFinished(() => held.close()),
531-
);
532-
const busy = await hf();
533-
expect([busy.code, busy.err]).toEqual([
528+
it("reads the history while another app keeps it open, and refuses to change it there", async () => {
529+
const { dir, read, hf, turn } = project();
530+
await hf();
531+
const first = await turn("claude", "Retitle", "index.html", "A2");
532+
await turn("claude", "Again", "index.html", "A3");
533+
await hf("begin", "--who", "codex", "--label", "Open turn");
534+
const marker = () => readFileSync(join(dir, ".hyperframes", "history-turn.json"), "utf-8");
535+
const before = marker();
536+
const held = await openProjectHistory({
537+
projectDir: dir,
538+
historyRoot: historyDeps.historyRoot,
539+
});
540+
onTestFinished(() => held.close());
541+
historyDeps.ownerWaitMs = 0;
542+
543+
const listed = await hf("--json", "--limit", "2");
544+
expect(listed.code).toBe(0);
545+
const labels = JSON.parse(listed.out).entries.map((entry: { label: string }) => entry.label);
546+
expect(labels).toEqual(["Again", "Retitle"]);
547+
expect((await hf("show", first.id)).out).toContain("M index.html");
548+
const stdout = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
549+
await hf("peek", first.id, "index.html");
550+
expect(String(stdout.mock.calls[0]?.[0])).toBe("A2");
551+
stdout.mockRestore();
552+
expect(marker()).toBe(before);
553+
554+
const refused = await hf("undo", first.id);
555+
expect([refused.code, refused.err]).toEqual([
534556
2,
535-
`This project's history is open in another process (pid ${process.pid}).`,
557+
`This project is open in another app (pid ${process.pid}), which keeps its history: undo, restore or pin there.`,
536558
]);
537-
}, 15_000);
559+
expect(read("index.html")).toBe("A3");
560+
});
561+
562+
it("still refuses a history id it cannot read, instead of reading the history as empty", async () => {
563+
const { dir, hf } = project();
564+
mkdirSync(join(dir, ".hyperframes"), { recursive: true });
565+
writeFileSync(join(dir, ".hyperframes", "history-id"), "garbage\n");
566+
const refused = await hf();
567+
expect(refused.code).toBe(2);
568+
expect(refused.err).toContain("holds no history id this version can read");
569+
});
570+
571+
it("peek refuses cleanly when another app pruned the point's file after its log was read", async () => {
572+
const { dir, hf, turn } = project();
573+
await hf();
574+
const first = await turn("claude", "Retitle", "index.html", "A2");
575+
const held = await openProjectHistory({
576+
projectDir: dir,
577+
historyRoot: historyDeps.historyRoot,
578+
});
579+
onTestFinished(() => held.close());
580+
historyDeps.ownerWaitMs = 0;
581+
const hash = held.peek(first.id)!["index.html"]!;
582+
rmSync(join(historyDeps.historyRoot, held.projectId, "blobs", hash.slice(0, 2), hash));
583+
584+
const refused = await hf("peek", first.id, "index.html");
585+
expect([refused.code, refused.err]).toEqual([2, "That point is no longer kept"]);
586+
});
538587
});

‎packages/cli/src/commands/history.ts‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ import {
2121
historyDeps,
2222
lastTurnParts,
2323
withOwner as withHistoryOwner,
24+
withReader as withHistoryReader,
2425
writeTurn,
2526
type Owner,
27+
type Reader,
2628
type Turn,
2729
type UndoMode,
2830
} from "../utils/historyOwner.js";
@@ -54,6 +56,16 @@ const withOwner = <T>(
5456
return task(owner, turn, projectDir);
5557
});
5658

59+
const withReader = <T>(
60+
action: string,
61+
dir: string | undefined,
62+
task: (reader: Reader) => Promise<T>,
63+
) =>
64+
withHistoryReader(dir, (reader) => {
65+
trackHistoryAction({ action, via: reader.via });
66+
return task(reader);
67+
});
68+
5769
/** An agent names itself with --who; without it the caller is the person, even during an agent's turn. */
5870
const whoOf = (name: string | undefined): HistoryWho => (name ? { kind: "agent", name } : YOU);
5971

@@ -138,13 +150,13 @@ function print(json: boolean, data: object, text: string): void {
138150
console.log(json ? JSON.stringify(withMeta(data), null, 2) : text);
139151
}
140152

141-
async function textDiff(owner: Owner, entry: HistoryEntry): Promise<string> {
153+
async function textDiff(reader: Reader, entry: HistoryEntry): Promise<string> {
142154
const work = mkdtempSync(join(tmpdir(), "hf-history-diff-"));
143155
try {
144156
const out: string[] = [];
145157
for (const file of entry.files) {
146158
const sides = await Promise.all(
147-
[file.before, file.after].map((hash) => (hash ? owner.blob(hash) : Buffer.alloc(0))),
159+
[file.before, file.after].map((hash) => (hash ? reader.blob(hash) : Buffer.alloc(0))),
148160
);
149161
if (sides.some((bytes) => bytes.subarray(0, 8000).includes(0))) {
150162
out.push(`Binary ${file.path} changed`);
@@ -283,7 +295,10 @@ function guarded<A>(run: (args: A) => Promise<void>) {
283295
error instanceof HistoryIdError;
284296
if (!refused) throw error;
285297
setCommandExitCode(2);
286-
const { message } = error as Error;
298+
const message =
299+
error instanceof HistoryBusyError
300+
? `This project is open in another app (pid ${error.pid}), which keeps its history: undo, restore or pin there.`
301+
: (error as Error).message;
287302
if ((args as { json?: boolean }).json) print(true, { ok: false, error: message }, message);
288303
else console.error(message);
289304
}
@@ -316,8 +331,8 @@ const listEntries = async (args: {
316331
json: boolean;
317332
}) => {
318333
if (args._?.[0]) return;
319-
await withOwner("list", args.dir, async (owner) => {
320-
const all = await owner.list();
334+
await withReader("list", args.dir, async (reader) => {
335+
const all = await reader.list();
321336
const picked = args.since ? since(all, args.since, whoOf(args.who)) : all;
322337
const limit = Number(args.limit ?? 20);
323338
if (!Number.isInteger(limit) || limit < 1)
@@ -348,13 +363,13 @@ export default defineCommand({
348363
"One entry's files, before and after",
349364
{ ref: { type: "positional", required: true }, diff: { type: "boolean", default: false } },
350365
(args) =>
351-
withOwner("show", args.dir, async (owner) => {
352-
const entry = entryOf(await owner.list(), args.ref);
366+
withReader("show", args.dir, async (reader) => {
367+
const entry = entryOf(await reader.list(), args.ref);
353368
const changes = entry.files.map((file) => ({
354369
path: file.path,
355370
change: changeOf(file),
356371
}));
357-
const diff = args.diff ? await textDiff(owner, entry) : undefined;
372+
const diff = args.diff ? await textDiff(reader, entry) : undefined;
358373
const marks = changes.map((file) => ` ${file.change[0]!.toUpperCase()} ${file.path}`);
359374
print(
360375
args.json,
@@ -402,13 +417,13 @@ export default defineCommand({
402417
path: { type: "positional", required: false },
403418
},
404419
(args) =>
405-
withOwner("peek", args.dir, async (owner) => {
406-
const files = await owner.peek(pointOf(await owner.list(), args.ref));
420+
withReader("peek", args.dir, async (reader) => {
421+
const files = await reader.peek(pointOf(await reader.list(), args.ref));
407422
if (!files) throw new Refusal("That point is no longer kept");
408423
if (!args.path) return print(args.json, { files }, Object.keys(files).join("\n"));
409424
const hash = files[args.path];
410425
if (!hash) throw new Refusal(`${args.path} did not exist then`);
411-
process.stdout.write(await owner.blob(hash));
426+
process.stdout.write(await reader.blob(hash));
412427
}),
413428
),
414429
pin: () =>

‎packages/cli/src/utils/historyOwner.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node
33
import { dirname, join } from "node:path";
44
import {
55
DEFAULT_HISTORY_ROOT,
6+
HistoryBusyError,
67
MAX_WINDOW_IDLE_MS,
78
openProjectHistory,
9+
readProjectHistory,
810
type HistoryEntry,
911
type HistoryListItem,
1012
type HistoryResult,
@@ -43,12 +45,15 @@ export interface Owner {
4345
close(): Promise<void>;
4446
}
4547

48+
export type Reader = Pick<Owner, "via" | "list" | "peek" | "blob">;
49+
4650
/** Swapped by tests. */
4751
export const historyDeps = {
4852
historyRoot: DEFAULT_HISTORY_ROOT,
4953
findServer: (projectDir: string) => findPreviewServerForProject(projectDir),
5054
/** A turn with no write for this long has ended, through a preview or not. */
5155
turnIdleMs: MAX_WINDOW_IDLE_MS,
56+
ownerWaitMs: undefined as number | undefined,
5257
now: () => Date.now(),
5358
};
5459

@@ -158,6 +163,7 @@ async function directOwner(projectDir: string, turn: Turn | null): Promise<Owner
158163
historyRoot: historyDeps.historyRoot,
159164
now: () => historyDeps.now(),
160165
pruneGoneProjectsBudgetMs: 1000,
166+
ownerWaitMs: historyDeps.ownerWaitMs,
161167
// A turn begun through a preview that has since stopped is still the agent's, until its idle limit.
162168
...(turn && {
163169
closedWindow: {
@@ -226,3 +232,29 @@ export async function withOwner<T>(
226232
}
227233
}
228234
}
235+
236+
export async function withReader<T>(
237+
dir: string | undefined,
238+
task: (reader: Reader) => Promise<T>,
239+
): Promise<T> {
240+
try {
241+
return await withOwner(dir, (owner) => task(owner));
242+
} catch (error) {
243+
if (!(error instanceof HistoryBusyError)) throw error;
244+
}
245+
// Busy is only thrown on opening, so the task has not run; an open turn's marker is left as it is.
246+
const view = readProjectHistory({
247+
projectDir: resolveProject(dir).dir,
248+
historyRoot: historyDeps.historyRoot,
249+
});
250+
return task({
251+
via: "direct",
252+
list: async () => view.list(),
253+
peek: async (point) => view.peek(point),
254+
// The owner may have pruned a blob since its log was read.
255+
blob: (hash) =>
256+
view.readBlob(hash).catch((error: NodeJS.ErrnoException) => {
257+
throw error.code === "ENOENT" ? new Refusal("That point is no longer kept") : error;
258+
}),
259+
});
260+
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ export interface BlobStore {
1919

2020
const BLOB_HASH = /^[0-9a-f]{64}$/;
2121

22+
function blobPath(dir: string, hash: string): string {
23+
// Checked where the path is joined, so no caller can read or write outside the store.
24+
if (!BLOB_HASH.test(hash)) throw new Error("That is not a history blob.");
25+
return join(dir, hash.slice(0, 2), hash);
26+
}
27+
28+
export const readBlob = (dir: string, hash: string) => readFile(blobPath(dir, hash));
29+
2230
async function hashFile(path: string): Promise<string> {
2331
const hash = createHash("sha256");
2432
for await (const chunk of createReadStream(path)) hash.update(chunk);
@@ -44,11 +52,7 @@ export async function openBlobStore(dir: string): Promise<BlobStore> {
4452
for (const shard of await readdir(dir))
4553
for (const hash of await readdir(join(dir, shard)).catch(() => []))
4654
sizes.set(hash, (await stat(join(dir, shard, hash))).size);
47-
const pathOf = (hash: string) => {
48-
// Checked where the path is joined, so no caller can read or write outside the store.
49-
if (!BLOB_HASH.test(hash)) throw new Error("That is not a history blob.");
50-
return join(dir, hash.slice(0, 2), hash);
51-
};
55+
const pathOf = (hash: string) => blobPath(dir, hash);
5256
let total = [...sizes.values()].reduce((sum, size) => sum + size, 0);
5357

5458
return {
@@ -71,7 +75,7 @@ export async function openBlobStore(dir: string): Promise<BlobStore> {
7175
return hash;
7276
},
7377
has: (hash) => sizes.has(hash),
74-
read: async (hash) => readFile(pathOf(hash)),
78+
read: async (hash) => readBlob(dir, hash),
7579
writeTo: async (hash, absPath, beforeReplace) =>
7680
cloneOrCopy(pathOf(hash), absPath, beforeReplace),
7781
bytes: () => total,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
export {
22
DEFAULT_HISTORY_ROOT,
33
openProjectHistory,
4+
readProjectHistory,
45
MAX_WINDOW_IDLE_MS,
56
UNDO_MODES,
67
type UndoMode,
78
type ClosedWindow,
89
type ProjectHistory,
910
type ProjectHistoryOptions,
11+
type ProjectHistoryView,
1012
type HistoryListItem,
1113
type HistoryResult,
1214
type HistoryWindow,

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @vitest-environment node
22
// fallow-ignore-file code-duplication
33
import {
4+
appendFileSync,
45
cpSync,
56
existsSync,
67
mkdirSync,
@@ -21,7 +22,12 @@ import { afterEach, describe, expect, it, vi } from "vitest";
2122
import { fileContentVersion, hashOfVersion, recordFileWriteReceipt } from "../helpers/fileVersion";
2223
import { HistoryBusyError } from "./ownerLock";
2324
import { HistoryIdError } from "./historyId";
24-
import { HistoryClosedError, openProjectHistory, type ProjectHistory } from "./projectHistory";
25+
import {
26+
HistoryClosedError,
27+
openProjectHistory,
28+
readProjectHistory,
29+
type ProjectHistory,
30+
} from "./projectHistory";
2531
import { START, type HistoryWho } from "./historyLog";
2632

2733
// A test can hold a media copy, and run work after its blob is stored but before history records it.
@@ -1234,6 +1240,43 @@ describe("openProjectHistory", () => {
12341240
expect((await waiting).projectId).toBe(history.projectId);
12351241
});
12361242

1243+
it("reads a history another process owns without its lock, past a half-appended last line", async () => {
1244+
const { history, write, projectDir, historyRoot } = await project({ "index.html": "v1" });
1245+
const second = await change(history, you, "Second", () => write("index.html", "v2"));
1246+
const home = join(historyRoot, history.projectId);
1247+
appendFileSync(join(home, "log.jsonl"), '{"type":"entry","entry":{"id":"torn"');
1248+
const files = () =>
1249+
readdirSync(home).map((name) =>
1250+
statSync(join(home, name)).isFile() ? [name, inside(home, name)] : [name],
1251+
);
1252+
const before = files();
1253+
1254+
const view = readProjectHistory({ projectDir, historyRoot });
1255+
expect(view.list()).toEqual(history.list());
1256+
expect(view.list().map((entry) => entry.label)).toEqual(["Second"]);
1257+
expect(view.peek(START)).toEqual(history.peek(START));
1258+
expect((await view.readBlob(view.peek(second.id)!["index.html"]!)).toString()).toBe("v2");
1259+
expect(files()).toEqual(before);
1260+
});
1261+
1262+
it("reads a copied folder as having no history, and leaves hidden paths out as the owner does", async () => {
1263+
const { history, write, projectDir, historyRoot } = await project({ "index.html": "v1" });
1264+
await change(history, you, "Second", () => write("index.html", "v2"));
1265+
const copy = tempDir("hf-history-copy-");
1266+
cpSync(projectDir, copy, { recursive: true });
1267+
expect(readProjectHistory({ projectDir: copy, historyRoot }).list()).toEqual([]);
1268+
1269+
const [entry] = history.list();
1270+
const hidden = { path: ".hidden/x", before: null, after: entry!.files[0]!.after };
1271+
const older = { ...entry!, id: "older", files: [...entry!.files, hidden] };
1272+
const logFile = join(historyRoot, history.projectId, "log.jsonl");
1273+
appendFileSync(logFile, `${JSON.stringify({ type: "entry", entry: older })}\n`);
1274+
const paths = readProjectHistory({ projectDir, historyRoot })
1275+
.list()
1276+
.map((item) => item.files.map((file) => file.path));
1277+
expect(paths).toEqual([["index.html"], ["index.html"]]);
1278+
});
1279+
12371280
it("files what changed while closed to a window begun on an earlier open, under its id", async () => {
12381281
const { history, write, projectDir, historyRoot } = await project({ "index.html": "v1" });
12391282
await history.close();

0 commit comments

Comments
 (0)