fix(cli): add lists files from a received config's registry as received (#7820)

* fix(cli): add lists files from a received config's registry as received

When a project's hyperframes.json is listed in .hyperframes/received-files.json
and names a registry other than the public one, add now lists every file it is
about to write in that record before writing any of them. This covers every
caller of addToProject: add by name, by tag, several names, and Studio.

The decision reads the config the install uses, so a byte-order mark or another
spelling of the file on disk is handled the same way. A person's own config, the
public registry and a record that cannot be read are left as they are.

* fix(cli): list received files as they are written, under a shared lock

add now lists each file in the received-files record just before it writes
it, using the write-time decision, so a file kept because the person edited it
during the download is never listed. When an install fails, it takes back the
entries it listed but never wrote.

Changes to the record take .hyperframes/received-files.json.lock, created
exclusively. A writer retries for about 2 s, takes over a lock older than
about 10 s, and always releases it. A writer that cannot take the lock in time
fails with a clear error instead of writing past it.

* fix(cli): an unreadable config counts as received; a failed install keeps what is on disk

add now counts an install as received when hyperframes.json exists but cannot be
read or parsed, since that config is not proven to be the person's own. When an
install fails, it takes back only the entries it listed whose files are not on
disk; files that landed stay listed.

* fix(cli): the received-files lock resolves the project folder as the installer does

The record's lock and write resolved the project folder with fs.realpathSync,
while the containment check resolves targets with the native realpath. The two
can spell the same folder differently: a Windows short name or a folder typed
in another letter case. The target then looked outside the project and the
change was refused. The record now resolves the folder with the installer's
realProjectRoot, so the root and target compare in one spelling.

The failed-install tests now fail the write by stubbing it, which works on every
platform, instead of a read-only folder, which Windows ignores.
This commit is contained in:
Michael Wang
2026-11-15 09:10:08 +00:00
committed by GitHub
parent c4d60c347f
commit 6a7b4df7c8
8 changed files with 451 additions and 11 deletions
@@ -1,8 +1,23 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import {
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
symlinkSync,
utimesSync,
writeFileSync,
} from "node:fs";
import { spawn } from "node:child_process";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { isEnvOrGitPath, isPrivateCompositionFile, receivedFiles } from "./privateProjectFile.js";
import { dirname, join } from "node:path";
import {
isEnvOrGitPath,
isPrivateCompositionFile,
receivedFiles,
recordReceivedFiles,
} from "./privateProjectFile.js";
const zeroFileIds = vi.hoisted(() => ({ on: false }));
@@ -82,3 +97,62 @@ describe("receivedFiles", () => {
expect(received("mine.html")).toBe(false);
});
});
describe("recordReceivedFiles", () => {
const record = (root: string) => join(root, ".hyperframes", "received-files.json");
const lock = (root: string) => `${record(root)}.lock`;
const listed = (root: string) => JSON.parse(readFileSync(record(root), "utf-8")).files;
function project(): string {
dir = mkdtempSync(join(tmpdir(), "hf-received-lock-"));
mkdirSync(join(dir, ".hyperframes"));
writeFileSync(record(dir), JSON.stringify({ files: ["hyperframes.json"] }));
return dir;
}
it("waits for another writer's lock, so both writers keep their entries", async () => {
const root = project();
writeFileSync(lock(root), "");
// Another writer: it read the record, holds the lock, writes its entry, then lets go.
const other = spawn(process.execPath, [
"-e",
`const fs = require("node:fs");
const files = JSON.parse(fs.readFileSync(process.argv[1], "utf8")).files;
process.stdout.write("read");
setTimeout(() => {
fs.writeFileSync(process.argv[1], JSON.stringify({ files: [...files, "theirs.html"] }));
fs.rmSync(process.argv[2]);
}, 300);`,
record(root),
lock(root),
]);
await new Promise((resolve) => other.stdout.once("data", resolve));
recordReceivedFiles(root, ["mine.html"]);
await new Promise((resolve) => other.once("exit", resolve));
expect(listed(root)).toEqual(["hyperframes.json", "mine.html", "theirs.html"]);
});
it("takes a project folder however its path is spelled", () => {
const root = join(project(), "Project");
mkdirSync(root);
const respelled = join(dirname(root), "project");
const spelled = existsSync(respelled) ? respelled : root;
expect(recordReceivedFiles(spelled, ["mine.html"])).toEqual(["mine.html"]);
expect(listed(root)).toEqual(["mine.html"]);
});
it("takes over a lock left behind for more than ten seconds", () => {
const root = project();
writeFileSync(lock(root), "");
const old = new Date(Date.now() - 60_000);
utimesSync(lock(root), old, old);
expect(recordReceivedFiles(root, ["mine.html"])).toEqual(["mine.html"]);
expect(listed(root)).toEqual(["hyperframes.json", "mine.html"]);
});
it("fails rather than write past a lock that stays held", () => {
const root = project();
writeFileSync(lock(root), "");
expect(() => recordReceivedFiles(root, ["mine.html"])).toThrow(/Another program is changing/);
expect(listed(root)).toEqual(["hyperframes.json"]);
});
});