Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
959edbc
fix(studio-server): a file the drive never answers no longer holds a …
miguel-heygen Nov 18, 2026
29ee475
fix(studio-server): keep history's comment share at or below its base
miguel-heygen Nov 18, 2026
e718969
fix(studio-server): a first copy that lands past its deadline is kept…
miguel-heygen Nov 19, 2026
4892c12
fix(studio-server): a late first copy is adopted and kept while anoth…
miguel-heygen Nov 19, 2026
68a04bb
fix(studio-server): a waiting late first copy counts as baseline and …
miguel-heygen Nov 19, 2026
4b65fe7
fix(studio-server): every reread of an owed file takes a copy slot an…
miguel-heygen Nov 19, 2026
065757f
fix(studio-server): history pins a copy as it is filed and keeps a re…
miguel-heygen Nov 19, 2026
59d9710
fix(studio-server): a history pin is taken and released by its owner,…
miguel-heygen Nov 19, 2026
9e2378b
Merge remote-tracking branch 'origin/main' into fix/history-copy-dead…
miguel-heygen Nov 21, 2026
27631d4
fix(studio-server): an agent step waits at most a deadline for a file…
miguel-heygen Nov 21, 2026
397d3bf
fix(studio-server): undo refuses a file whose first copy landed after…
miguel-heygen Nov 21, 2026
031671c
chore(studio-server): give the packed size budget its usual 5% headroom
miguel-heygen Nov 21, 2026
33eafbf
fix(studio-server): an agent step that goes ahead of a first copy own…
miguel-heygen Nov 21, 2026
6ff4d4e
fix(studio-server): undo of a step that went ahead of a pending first…
miguel-heygen Nov 21, 2026
dfe957d
Merge remote-tracking branch 'origin/main' into fix/history-copy-dead…
miguel-heygen Nov 21, 2026
a30cf5b
fix(studio-server): an undo after a step that rewrote a pending file …
miguel-heygen Nov 21, 2026
a2032ba
fix(studio-server): a step ahead of a pending file refuses undo after…
miguel-heygen Nov 21, 2026
b910bd6
fix(studio-server): a step that went ahead of a pending file never un…
miguel-heygen Nov 21, 2026
021580d
fix(studio-server): a recovered step keeps its guard through a second…
miguel-heygen Nov 21, 2026
2e293fc
fix(studio-server): a start keeps receipts heard before its read; reo…
miguel-heygen Nov 21, 2026
59ee378
fix(studio-server): a first copy's start follows only receipts heard …
miguel-heygen Nov 21, 2026
5a0b3a6
fix(studio-server): bytes kept for a pending first copy count as star…
miguel-heygen Nov 21, 2026
46e78b1
Merge remote-tracking branch 'origin/main' into fix/history-copy-dead…
miguel-heygen Nov 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/studio-server/src/history/blobStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,47 @@ describe("the history store's count of what it holds", () => {
});
});

describe("a pinned copy", () => {
it("survives every prune from the moment it is filed until it is unpinned", async () => {
const root = mkdtempSync(join(tmpdir(), "hf-blob-pin-"));
try {
const small = join(root, "small.txt");
const large = join(root, "large.txt");
writeFileSync(small, "pinned bytes");
writeFileSync(large, "y".repeat(200 * 1024));
const store = await openBlobStore(join(root, "history", "blobs"));
const hashes = [await store.put(small, true), await store.put(large, true)];
await store.prune(new Set());
for (const hash of hashes) expect(store.has(hash!)).toBe(true);
for (const hash of hashes) store.unpin(hash!);
await store.prune(new Set());
for (const hash of hashes) expect(store.has(hash!)).toBe(false);
} finally {
rmSync(root, { recursive: true, force: true });
}
});

it("pins a copy whose bytes the store already holds, and keeps it until each pin is let go", async () => {
const root = mkdtempSync(join(tmpdir(), "hf-blob-pin-"));
try {
const file = join(root, "same.txt");
writeFileSync(file, "same bytes");
const store = await openBlobStore(join(root, "history", "blobs"));
const hash = (await store.put(file))!;
await store.put(file, true);
await store.put(file, true);
store.unpin(hash);
await store.prune(new Set());
expect(store.has(hash)).toBe(true);
store.unpin(hash);
await store.prune(new Set());
expect(store.has(hash)).toBe(false);
} finally {
rmSync(root, { recursive: true, force: true });
}
});
});

describe("a file written to while the history reads it", () => {
let root: string;
beforeEach(() => {
Expand Down
43 changes: 32 additions & 11 deletions packages/studio-server/src/history/blobStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import { assertLandedIn, assertStillWithin, folderWithin } from "./containedPath
/** File contents stored once by sha256, text and binary alike. */
export interface BlobStore {
/** Copies the file in (cloned where it can) and returns its hash; null while it keeps changing. */
put(absPath: string): Promise<string | null>;
put(absPath: string, pin?: boolean): Promise<string | null>;
unpin(hash: string): void;
pinned(): Iterable<string>;
reference(absPath: string): Promise<string>;
has(hash: string): boolean;
read(hash: string): Promise<Buffer>;
Expand Down Expand Up @@ -197,10 +199,17 @@ export async function openBlobStore(dir: string): Promise<BlobStore> {
for (const hash of asideSizes.keys()) holds.set(hash, new Set(["*"]));
const pathOf = (hash: string) => blobPath(sizes.has(hash) ? dir : asideDir, hash);
let total = [...sizes.values()].reduce((sum, size) => sum + size, 0);
const pins = new Map<string, number>();
const forgetting = new Map<string, Promise<void>>();
const pin = (hash: string) => pins.set(hash, (pins.get(hash) ?? 0) + 1);
const forget = async (hash: string) => {
await rm(blobPath(dir, hash), { force: true });
total -= sizes.get(hash) ?? 0;
sizes.delete(hash);
const gone = (async () => {
await rm(blobPath(dir, hash), { force: true });
total -= sizes.get(hash) ?? 0;
sizes.delete(hash);
})();
forgetting.set(hash, gone);
await gone.finally(() => forgetting.delete(hash));
};
const forgetAside = async (hash: string) => {
await rm(blobPath(asideDir, hash), { force: true });
Expand All @@ -219,9 +228,10 @@ export async function openBlobStore(dir: string): Promise<BlobStore> {
await write();
});
// The bytes hashed are the bytes filed, so a write racing the read can never file bytes under the wrong hash.
const putBytes = async (bytes: Buffer, mode: number) => {
const putBytes = async (bytes: Buffer, mode: number, pinned: boolean) => {
const hash = sha256(bytes);
if (sizes.has(hash)) return hash;
await forgetting.get(hash);
if (sizes.has(hash)) return pinned ? (pin(hash), hash) : hash;
const temp = join(dir, `incoming-${randomUUID()}`);
try {
await withFolderMadeIfMissing(dir, () => writeFile(temp, bytes, { mode }));
Expand All @@ -234,12 +244,13 @@ export async function openBlobStore(dir: string): Promise<BlobStore> {
throw error;
}
file(hash, bytes.length);
if (pinned) pin(hash);
return hash;
};

const putIfUnchanged = async (absPath: string): Promise<string | null> => {
const putIfUnchanged = async (absPath: string, pinned: boolean): Promise<string | null> => {
const small = await readIfSmall(absPath);
if (small) return small.moved ? null : putBytes(small.bytes, small.mode);
if (small) return small.moved ? null : putBytes(small.bytes, small.mode, pinned);
// Hash the copy, not the source, so a write racing the copy can never file bytes under the wrong hash.
const temp = join(dir, `incoming-${randomUUID()}`);
// Recreated if removed while open, so a missing folder is never mistaken for a deleted project file.
Expand All @@ -256,21 +267,24 @@ export async function openBlobStore(dir: string): Promise<BlobStore> {
return null;
}
const hash = await hashFile(temp);
await forgetting.get(hash);
if (sizes.has(hash)) {
if (pinned) pin(hash);
await rm(temp, { force: true });
return hash;
}
await mkdir(dirname(blobPath(dir, hash)), { recursive: true });
const size = (await stat(temp)).size;
await renameWithRetryAsync(temp, blobPath(dir, hash));
file(hash, size);
if (pinned) pin(hash);
return hash;
};

return {
async put(absPath) {
async put(absPath, pinned = false) {
for (let read = 0; read < READS_BEFORE_LEAVING_IT; read += 1) {
const hash = await putIfUnchanged(absPath);
const hash = await putIfUnchanged(absPath, pinned);
if (hash !== null) return hash;
}
return null;
Expand Down Expand Up @@ -317,8 +331,15 @@ export async function openBlobStore(dir: string): Promise<BlobStore> {
async drop(hashes) {
for (const hash of hashes) if (sizes.has(hash)) await forget(hash);
},
unpin(hash) {
const left = (pins.get(hash) ?? 0) - 1;
if (left > 0) pins.set(hash, left);
else pins.delete(hash);
},
pinned: () => pins.keys(),
async prune(keep) {
for (const hash of [...sizes.keys()]) if (!keep.has(hash)) await forget(hash);
for (const hash of [...sizes.keys()])
if (!keep.has(hash) && !pins.has(hash)) await forget(hash);
for (const hash of [...asideSizes.keys()]) if (!keep.has(hash)) await forgetAside(hash);
},
};
Expand Down
Loading
Loading