diff --git a/packages/cli/src/api/transcribe.ts b/packages/cli/src/api/transcribe.ts index e6d3402e1c..ddd8b3cd67 100644 --- a/packages/cli/src/api/transcribe.ts +++ b/packages/cli/src/api/transcribe.ts @@ -1,4 +1,5 @@ import { existsSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs"; +import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { basename, dirname, extname, join, resolve } from "node:path"; import { transcribeWithHeygen } from "../heygen/transcribe.js"; import { normalizeErrorMessage } from "../utils/errorMessage.js"; @@ -279,7 +280,7 @@ function importTranscript(inputPath: string, dir: string): TranscribeResult { const { words, format } = readTranscript(inputPath); const transcriptPath = join(dir, TRANSCRIPT_FILE); - writeFileSync(transcriptPath, JSON.stringify(words, null, 2)); + replaceFileAtomically(transcriptPath, JSON.stringify(words, null, 2)); patchCaptionHtml(dir, words); return { kind: "imported", format, wordCount: words.length, transcriptPath }; } @@ -387,7 +388,7 @@ function finishTranscript( ): TranscribeResult { const { words } = loadTranscript(result.transcriptPath); - writeFileSync(result.transcriptPath, JSON.stringify(words, null, 2)); + replaceFileAtomically(result.transcriptPath, JSON.stringify(words, null, 2)); patchCaptionHtml(dir, words); if (sidecar) { try { diff --git a/packages/cli/src/commands/batchRender.ts b/packages/cli/src/commands/batchRender.ts index d432bc3dd6..d238010bf3 100644 --- a/packages/cli/src/commands/batchRender.ts +++ b/packages/cli/src/commands/batchRender.ts @@ -1,5 +1,5 @@ import { failCommand } from "../utils/commandResult.js"; -import { writeFileSync } from "node:fs"; +import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { makeOutputFolder } from "../utils/outputFolder.js"; import { readJsonFileText } from "../utils/jsonFlag.js"; import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; @@ -345,7 +345,7 @@ function summarizeManifest(manifest: BatchManifest): void { function writeManifest(manifest: BatchManifest): void { summarizeManifest(manifest); makeOutputFolder(dirname(manifest.manifestPath)); - writeFileSync(manifest.manifestPath, JSON.stringify(manifest, null, 2) + "\n", "utf8"); + replaceFileAtomically(manifest.manifestPath, JSON.stringify(manifest, null, 2) + "\n"); } async function renderBatchRow( diff --git a/packages/cli/src/commands/grade-compare.ts b/packages/cli/src/commands/grade-compare.ts index 78bf33ab09..ddf06b014e 100644 --- a/packages/cli/src/commands/grade-compare.ts +++ b/packages/cli/src/commands/grade-compare.ts @@ -6,6 +6,7 @@ import { rmSync, writeFileSync, } from "node:fs"; +import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { makeOutputFolder } from "../utils/outputFolder.js"; import { readJsonFileText } from "../utils/jsonFlag.js"; import { tmpdir } from "node:os"; @@ -674,7 +675,7 @@ export default defineCommand({ parsed.timeoutMs, ); makeOutputFolder(dirname(parsed.outPath)); - copyFileSync(tempSheet, parsed.outPath); + replaceFileAtomically(parsed.outPath, readFileSync(tempSheet)); trackCompareSheet({ command: "grade-compare", cells: prepared.cells.length, diff --git a/packages/cli/src/commands/lambda/state.ts b/packages/cli/src/commands/lambda/state.ts index 916d537ec6..2242ae1fcd 100644 --- a/packages/cli/src/commands/lambda/state.ts +++ b/packages/cli/src/commands/lambda/state.ts @@ -11,8 +11,9 @@ import { failCommand } from "../../utils/commandResult.js"; * can symlink the directory. */ -import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync } from "node:fs"; import { dirname, join } from "node:path"; +import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; export interface StackOutputs { /** Project-name prefix passed to `deploy`. Used as CloudFormation stack-name suffix. */ @@ -52,7 +53,7 @@ export function stateFilePath( export function writeStackOutputs(outputs: StackOutputs, cwd: string = process.cwd()): string { const path = stateFilePath(outputs.stackName, cwd); mkdirSync(dirname(path), { recursive: true }); - writeFileSync(path, JSON.stringify(outputs, null, 2) + "\n"); + replaceFileAtomically(path, JSON.stringify(outputs, null, 2) + "\n"); return path; } diff --git a/packages/cli/src/commands/measure/events.ts b/packages/cli/src/commands/measure/events.ts index 3b082182ab..b8e9a71a22 100644 --- a/packages/cli/src/commands/measure/events.ts +++ b/packages/cli/src/commands/measure/events.ts @@ -1,4 +1,4 @@ -import { writeFileSync } from "node:fs"; +import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { join } from "node:path"; import { defineCommand } from "../../utils/defineCommand.js"; import type { Example } from "../_examples.js"; @@ -155,7 +155,7 @@ export default defineCommand({ const edges = [start, ...cuts, end]; const windows = lookWindows(found.events, info.fps, sound?.onsets.map((o) => o.t) ?? []); const file = join(dir, `${name}-map.json`); - writeFileSync( + replaceFileAtomically( file, JSON.stringify({ ...head, events: found.events, sound, windows }, null, 1), ); diff --git a/packages/cli/src/measure/diff.test.ts b/packages/cli/src/measure/diff.test.ts index f67de29826..38e0bd65c0 100644 --- a/packages/cli/src/measure/diff.test.ts +++ b/packages/cli/src/measure/diff.test.ts @@ -1,4 +1,11 @@ -import { existsSync, mkdirSync, writeFileSync } from "node:fs"; +import { + existsSync, + lstatSync, + mkdirSync, + readFileSync, + symlinkSync, + writeFileSync, +} from "node:fs"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; import { @@ -265,6 +272,18 @@ describe.skipIf(noTools)("diffVideos", () => { expect(r.worst).toEqual([]); }); + it("writes the side-by-side over a link at sbs.mp4, never through it", async () => { + const out = join(dir, "sbs"); + mkdirSync(out, { recursive: true }); + const victim = join(dir, "sbs-victim.txt"); + writeFileSync(victim, "keep me"); + symlinkSync(victim, join(out, "sbs.mp4")); + const r = await diffVideos({ reference: ref, yours: ref, outDir: out, sbs: true }); + expect(readFileSync(victim, "utf8")).toBe("keep me"); + expect(r.sbs).toBe(join(out, "sbs.mp4")); + expect(lstatSync(join(out, "sbs.mp4")).isFile()).toBe(true); + }); + it("finds a wrong element by its cell when the whole frame still scores high", async () => { // An 80x60 box over a 320x180 frame for one second: about 0.94 whole-frame SSIM. const yours = clip("box.mp4", [ diff --git a/packages/cli/src/measure/diff.ts b/packages/cli/src/measure/diff.ts index d18cf4b5cb..854d54e946 100644 --- a/packages/cli/src/measure/diff.ts +++ b/packages/cli/src/measure/diff.ts @@ -11,11 +11,11 @@ import { readFileSync, rmSync, statSync, - writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; import { basename, dirname, join, resolve } from "node:path"; import { fpsToNumber, parseFps } from "@hyperframes/core"; +import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { readCompositionFps } from "../utils/compositionFps.js"; import { type Cut, classifiedCuts, confirmsCut, frameChanges, signatures } from "./cuts.js"; import { @@ -29,6 +29,7 @@ import { compareEnergy, energyCurve, type EnergyComparison } from "./energy.js"; import { compareLook, look, sameLook, type LookComparison } from "./look.js"; import { decode, + ffmpegToFile, Fix, frameAt, pearson, @@ -383,7 +384,7 @@ function ssimSeries(ref: Clip, yours: Clip, logPath: string): Series { undefined, work, ).toString(); - writeFileSync(logPath, text); + replaceFileAtomically(logPath, text); const frame = all(text); const perCell = Array.from({ length: GRID * GRID }, (_, i) => all(readFileSync(join(work, `cell${i}.log`), "utf8")), @@ -1214,13 +1215,15 @@ function sideBySide( offset = 0, ): string { const skip = (seconds: number) => (seconds > 0 ? ["-ss", String(seconds)] : []); - runFfmpeg([ - ...["-y", ...skip(-offset), "-i", reference, ...skip(offset), "-i", yours, "-filter_complex"], - `[0:v]setsar=1[a];[1:v]scale=${ref.width}:${ref.height},setsar=1[b];[a][b]hstack=inputs=2,pad=ceil(iw/2)*2:ceil(ih/2)*2[v]`, - ...["-map", "[v]", "-map", "0:a?", "-c:v", "libx264", "-crf", "20", "-pix_fmt", "yuv420p"], - ...["-c:a", "aac", "-shortest", out], - ]); - return out; + return ffmpegToFile( + [ + ...["-y", ...skip(-offset), "-i", reference, ...skip(offset), "-i", yours, "-filter_complex"], + `[0:v]setsar=1[a];[1:v]scale=${ref.width}:${ref.height},setsar=1[b];[a][b]hstack=inputs=2,pad=ceil(iw/2)*2:ceil(ih/2)*2[v]`, + ...["-map", "[v]", "-map", "0:a?", "-c:v", "libx264", "-crf", "20", "-pix_fmt", "yuv420p"], + ...["-c:a", "aac", "-shortest"], + ], + out, + ); } function choosePick(opts: DiffOptions, ref: VideoInfo): Pick { @@ -1330,7 +1333,7 @@ export async function diffVideos(options: DiffOptions): Promise { warnings.push(`yours ends early: ${series.frame.length} of ${ks.length} frames compared`); const worstCell = series.cells.map((cells) => Math.min(...cells)); // Every compared frame, to find the stretches that fail. - writeFileSync( + replaceFileAtomically( join(opts.outDir, "frames.tsv"), [ "t\tframe\tssim\tworst_cell\tcell", diff --git a/packages/cli/src/measure/media.ts b/packages/cli/src/measure/media.ts index 35a4d47e87..1171c12f90 100644 --- a/packages/cli/src/measure/media.ts +++ b/packages/cli/src/measure/media.ts @@ -1,9 +1,12 @@ // Decode with ffmpeg into raw arrays, so measuring and comparing need nothing but node and ffmpeg. // Ported from the Studio's viral-clone media.mjs and tool.mjs. import { execFileSync } from "node:child_process"; -import { closeSync, existsSync, mkdirSync, openSync, readSync } from "node:fs"; -import { dirname, join, resolve } from "node:path"; +import { closeSync, existsSync, mkdirSync, openSync, readSync, rmSync } from "node:fs"; +import { basename, dirname, join, resolve } from "node:path"; +import { createStagingDir } from "@hyperframes/core/atomic-file"; +import { renameWithRetry } from "@hyperframes/core/rename-retry"; import { findFFmpeg, findFFprobe, getFFmpegInstallHint } from "../browser/ffmpeg.js"; +import { recordFolder } from "../utils/projectRecords.js"; const MAX_BUFFER = 1 << 30; /** ffmpeg flags on every run. Inline before a spread, the ffprobe `--` contract test reads them as a probe wrapper. */ @@ -53,6 +56,18 @@ export function runFfmpeg(args: string[], input?: Buffer, cwd?: string): Buffer }); } +export function ffmpegToFile(args: string[], out: string, input?: Buffer): string { + const work = createStagingDir(dirname(out), ".ffmpeg-"); + try { + const made = join(work, basename(out)); + runFfmpeg([...args, made], input); + renameWithRetry(made, out); + return out; + } finally { + rmSync(work, { recursive: true, force: true }); + } +} + export interface VideoInfo { path: string; width: number; @@ -361,11 +376,11 @@ export function meanAbsDiff(a: Uint8Array, b: Uint8Array): number { /** Writes raw RGB (or grey) bytes as an image; the extension picks the format. */ export function writeImage(path: string, px: Uint8Array, w: number, h: number, gray = false) { const header = Buffer.from(`${gray ? "P5" : "P6"}\n${w} ${h}\n255\n`); - runFfmpeg( - ["-y", "-f", gray ? "pgm_pipe" : "ppm_pipe", "-i", "-", "-frames:v", "1", path], + return ffmpegToFile( + ["-y", "-f", gray ? "pgm_pipe" : "ppm_pipe", "-i", "-", "-frames:v", "1"], + path, Buffer.concat([header, Buffer.from(px.buffer, px.byteOffset, px.byteLength)]), ); - return path; } export const round = (n: number, d = 3) => Math.round(n * 10 ** d) / 10 ** d; @@ -420,10 +435,20 @@ export function parseRoi(text: unknown, width?: number, height?: number): Roi { return roi; } -/** The folder a tool writes its pictures to, made on demand: --out, or .hyperframes/ in the current folder. */ +/** The folder a tool writes its pictures to, made on demand: --out, or .hyperframes/ in the current folder. + * The default is used only when each folder on the way is a real one: a cloned project can link any of them out. */ export function outDir(out: unknown, tool: string): string { - const dir = typeof out === "string" && out ? resolve(out) : resolve(join(".hyperframes", tool)); - mkdirSync(dir, { recursive: true }); + if (typeof out === "string" && out) { + const dir = resolve(out); + mkdirSync(dir, { recursive: true }); + return dir; + } + const dir = recordFolder(resolve("."), tool); + if (!dir) + throw new Fix( + `${join(".hyperframes", tool)} is a link or not a folder here, so nothing is written into it`, + "pass --out ", + ); return dir; } diff --git a/packages/cli/src/utils/historyOwner.ts b/packages/cli/src/utils/historyOwner.ts index d74fdc5a86..3b560fec00 100644 --- a/packages/cli/src/utils/historyOwner.ts +++ b/packages/cli/src/utils/historyOwner.ts @@ -1,14 +1,6 @@ import { randomUUID } from "node:crypto"; -import { - accessSync, - constants, - mkdirSync, - readFileSync, - renameSync, - rmSync, - writeFileSync, -} from "node:fs"; -import { dirname, join, resolve } from "node:path"; +import { accessSync, constants } from "node:fs"; +import { join, resolve } from "node:path"; import { DEFAULT_HISTORY_ROOT, HistoryBusyError, @@ -24,6 +16,7 @@ import { } from "@hyperframes/studio-server/history"; import { resolveProject } from "./project.js"; +import { readRecord, removeRecord, writeRecord } from "./projectRecords.js"; import { findPreviewServerForProject, studioApiUrl } from "./studioSelectionClient.js"; export type { UndoMode }; @@ -77,7 +70,9 @@ export const historyDeps = { /** A refusal is the caller's to fix: commands print its message, never a stack. */ export class Refusal extends Error {} -const turnFile = (dir: string) => join(dir, ".hyperframes", "history-turn.json"); +const TURN_FILE = "history-turn.json"; +const LAST_TURNS_FILE = "history-turns.json"; +const MAX_RECORD_BYTES = 1024 * 1024; const WHO_KINDS: readonly unknown[] = ["person", "agent", "outside"] satisfies HistoryWho["kind"][]; const isId = (value: unknown): value is string => typeof value === "string" && isHistoryId(value); @@ -110,24 +105,26 @@ function parseTurn(value: unknown): Turn | null { /** The open turn, or null; a marker with no last write time cannot say when the turn ended, so it has. */ function readTurn(dir: string): Turn | null { try { - return parseTurn(JSON.parse(readFileSync(turnFile(dir), "utf-8"))); + return parseTurn(JSON.parse(readRecord(dir, TURN_FILE, MAX_RECORD_BYTES))); } catch { return null; } } +function writeRecordOrRefuse(dir: string, name: string, data: string): void { + if (!writeRecord(dir, name, data)) + throw new Refusal(`Could not write ${join(dir, ".hyperframes", name)}.`); +} + export function writeTurn(dir: string, turn: Turn | null): void { - if (!turn) return rmSync(turnFile(dir), { force: true }); - mkdirSync(dirname(turnFile(dir)), { recursive: true }); - writeFileSync(turnFile(dir), JSON.stringify(turn)); + if (!turn) return void removeRecord(dir, TURN_FILE); + writeRecordOrRefuse(dir, TURN_FILE, JSON.stringify(turn)); } /** Each agent's last ended turn, as the entries it filed; kept beside the marker, outside the history. */ -const lastTurnsFile = (dir: string) => join(dir, ".hyperframes", "history-turns.json"); - function readLastTurns(dir: string): Record { try { - const turns: unknown = JSON.parse(readFileSync(lastTurnsFile(dir), "utf-8")); + const turns: unknown = JSON.parse(readRecord(dir, LAST_TURNS_FILE, MAX_RECORD_BYTES)); if (!turns || typeof turns !== "object") return {}; return Object.fromEntries(Object.entries(turns).filter(([, parts]) => isIds(parts))); } catch { @@ -159,10 +156,11 @@ export async function endTurn( const own = entry ? [entry.id] : []; const parts = [...new Set([...turn.parts, ...cut.map((part) => part.id), ...own])]; // Recorded before the marker goes, so a crash between the two never leaves the older turn as the last one. - const draft = `${lastTurnsFile(dir)}.${process.pid}.tmp`; - mkdirSync(dirname(draft), { recursive: true }); - writeFileSync(draft, JSON.stringify({ ...readLastTurns(dir), [turn.who.name]: parts })); - renameSync(draft, lastTurnsFile(dir)); + writeRecordOrRefuse( + dir, + LAST_TURNS_FILE, + JSON.stringify({ ...readLastTurns(dir), [turn.who.name]: parts }), + ); writeTurn(dir, null); return { entry, parts }; } diff --git a/packages/cli/src/utils/linkedWrites.test.ts b/packages/cli/src/utils/linkedWrites.test.ts new file mode 100644 index 0000000000..9d25cab4fd --- /dev/null +++ b/packages/cli/src/utils/linkedWrites.test.ts @@ -0,0 +1,148 @@ +// @vitest-environment node +import { + lstatSync, + mkdirSync, + mkdtempSync, + readFileSync, + readdirSync, + rmSync, + symlinkSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { writeStackOutputs } from "../commands/lambda/state.js"; +import { outDir, writeImage } from "../measure/media.js"; +import { writeParakeetTranscript } from "../whisper/parakeet.js"; +import { writeTurn, type Turn } from "./historyOwner.js"; +import { recordProjectRegistryItems, seedProjectAuthoringSkill } from "./projectConfig.js"; + +const roots: string[] = []; +afterEach(() => { + vi.restoreAllMocks(); + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); + +/** A project folder and, beside it, an outside folder holding a file the project must never change. */ +function setup() { + const root = mkdtempSync(join(tmpdir(), "hf-record-links-")); + roots.push(root); + const project = join(root, "project"); + const outside = join(root, "outside"); + mkdirSync(project); + mkdirSync(outside); + const victim = join(outside, "victim.txt"); + writeFileSync(victim, "keep me"); + return { project, outside, victim }; +} + +const records = (project: string) => join(project, ".hyperframes"); +const isLink = (path: string) => lstatSync(path).isSymbolicLink(); + +function linkRecordsFolderOut(project: string, outside: string): void { + symlinkSync(outside, records(project)); +} + +describe("history turn markers", () => { + const turn = (id: string): Turn => ({ + via: "preview", + id, + who: { kind: "agent", name: "claude" }, + label: "Retitle", + startedAt: 1, + lastWriteAt: Date.now(), + parts: [], + }); + + it("replace a linked marker instead of writing through it", () => { + const { project, victim } = setup(); + mkdirSync(records(project)); + const marker = join(records(project), "history-turn.json"); + symlinkSync(victim, marker); + + writeTurn(project, turn("a1b2")); + + expect(readFileSync(victim, "utf8")).toBe("keep me"); + expect(isLink(marker)).toBe(false); + }); + + it("refuse a linked .hyperframes folder", () => { + const { project, outside } = setup(); + linkRecordsFolderOut(project, outside); + expect(() => writeTurn(project, turn("a1b2"))).toThrow(/Could not write/); + expect(readdirSync(outside)).toEqual(["victim.txt"]); + }); +}); + +describe("hyperframes.json patches", () => { + it("leave a linked config and its target alone", () => { + const { project, outside } = setup(); + const shared = join(outside, "shared.json"); + writeFileSync(shared, '{ "registry": "x" }\n'); + const config = join(project, "hyperframes.json"); + symlinkSync(shared, config); + + seedProjectAuthoringSkill(project, "general-video"); + recordProjectRegistryItems(project, [{ name: "a", type: "hyperframes:block", target: "b" }]); + + expect(readFileSync(shared, "utf8")).toBe('{ "registry": "x" }\n'); + expect(isLink(config)).toBe(true); + }); + + it("patch a plain config in place", () => { + const { project } = setup(); + const config = join(project, "hyperframes.json"); + writeFileSync(config, '{\n "registry": "x"\n}\n'); + seedProjectAuthoringSkill(project, "general-video"); + expect(readFileSync(config, "utf8")).toBe( + '{\n "registry": "x",\n "authoringSkill": "general-video"\n}\n', + ); + }); +}); + +describe("fixed-name outputs", () => { + it("replace a linked transcript.json and lambda stack file instead of writing through them", () => { + const { project, victim } = setup(); + const transcript = join(project, "transcript.json"); + symlinkSync(victim, transcript); + writeParakeetTranscript(project, [{ text: "hi", start: 0, end: 1 }]); + expect(readFileSync(victim, "utf8")).toBe("keep me"); + expect(isLink(transcript)).toBe(false); + + mkdirSync(records(project)); + const state = join(records(project), "lambda-stack-demo.json"); + symlinkSync(victim, state); + writeStackOutputs({ stackName: "demo", region: "us-east-1" } as never, project); + expect(readFileSync(victim, "utf8")).toBe("keep me"); + expect(isLink(state)).toBe(false); + }); + + it("refuse a default measure folder reached through a link, but not a chosen --out", () => { + const { project, outside } = setup(); + vi.spyOn(process, "cwd").mockReturnValue(project); + expect(outDir(undefined, "measure/events")).toBe(join(records(project), "measure", "events")); + + rmSync(records(project), { recursive: true }); + linkRecordsFolderOut(project, outside); + expect(() => outDir(undefined, "measure/events")).toThrow(/link or not a folder/); + expect(readdirSync(outside)).toEqual(["victim.txt"]); + + expect(outDir(join(project, "mine"), "measure/events")).toBe(join(project, "mine")); + }); + + it("replace a link at an ffmpeg output name in a real default folder", () => { + const { project, victim } = setup(); + mkdirSync(join(records(project), "measure", "events"), { recursive: true }); + const clip = join(records(project), "measure", "events", "clip.png"); + symlinkSync("../../../../outside/victim.txt", clip); + expect(readFileSync(clip, "utf8")).toBe("keep me"); + vi.spyOn(process, "cwd").mockReturnValue(project); + + writeImage(join(outDir(undefined, "measure/events"), "clip.png"), new Uint8Array(48), 4, 4); + + expect(readFileSync(victim, "utf8")).toBe("keep me"); + expect(isLink(clip)).toBe(false); + expect(readFileSync(clip).subarray(1, 4).toString()).toBe("PNG"); + }); +}); diff --git a/packages/cli/src/utils/projectConfig.ts b/packages/cli/src/utils/projectConfig.ts index 07f0c0ba95..04198efc16 100644 --- a/packages/cli/src/utils/projectConfig.ts +++ b/packages/cli/src/utils/projectConfig.ts @@ -7,7 +7,8 @@ * point at custom registries or reshape their project layout. */ -import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; +import { lstatSync, mkdirSync, readFileSync } from "node:fs"; +import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { join, resolve } from "node:path"; import { DEFAULT_REGISTRY_URL } from "../registry/index.js"; import { normalizeSkillSlug } from "../telemetry/skill.js"; @@ -200,8 +201,7 @@ export function writeProjectConfig( projectDir: string, config: ProjectConfig = DEFAULT_PROJECT_CONFIG, ): void { - const path = projectConfigPath(projectDir); - writeFileSync(path, JSON.stringify(config, null, 2) + "\n", "utf-8"); + replaceFileAtomically(projectConfigPath(projectDir), JSON.stringify(config, null, 2) + "\n"); } /** Create `hyperframes.json` without replacing an existing file or following a symlink. */ @@ -244,10 +244,13 @@ function isJsonObject(value: unknown): value is Record { /** Write a patched config back with the file's own indentation and byte order mark. */ function rewriteConfig(path: string, text: string, parsed: Record): void { const indent = /\n([ \t]+)"/.exec(text)?.[1] ?? " "; - writeFileSync( + const stats = lstatSync(path); + if (!stats.isFile()) + throw new Error(`${path} is a link or not a plain file, so it is left alone`); + replaceFileAtomically( path, (text.startsWith("\uFEFF") ? "\uFEFF" : "") + JSON.stringify(parsed, null, indent) + "\n", - "utf-8", + stats.mode & 0o777, ); } diff --git a/packages/cli/src/utils/projectRecords.ts b/packages/cli/src/utils/projectRecords.ts index eee1255423..a7563f417a 100644 --- a/packages/cli/src/utils/projectRecords.ts +++ b/packages/cli/src/utils/projectRecords.ts @@ -108,6 +108,10 @@ export function writeRecord(projectDir: string, name: string, data: string): boo return dir !== null && replaceFile(join(dir, recordFile(name)), data, 0o600); } +export function recordFolder(projectDir: string, folder: string): string | null { + return recordsDir(projectDir, `${folder.replaceAll("\\", "/")}/`, true); +} + /** Deletes a record that is a plain file; false when there is none to delete. */ export function removeRecord(projectDir: string, name: string): boolean { const dir = recordsDir(projectDir, name); diff --git a/packages/cli/src/whisper/parakeet.ts b/packages/cli/src/whisper/parakeet.ts index 66e43569a4..5895ecf0ac 100644 --- a/packages/cli/src/whisper/parakeet.ts +++ b/packages/cli/src/whisper/parakeet.ts @@ -14,8 +14,9 @@ */ import { execFileSync } from "node:child_process"; +import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { findTrustedExecutable } from "@hyperframes/parsers/ff-binaries"; -import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; import { homedir, tmpdir } from "node:os"; import { basename, extname, join } from "node:path"; import { stoppableExecFile } from "../utils/stoppableExecFile.js"; @@ -325,7 +326,7 @@ export async function transcribeWithParakeet( export function writeParakeetTranscript(dir: string, words: Word[]): RunnerResult { mkdirSync(dir, { recursive: true }); const transcriptPath = join(dir, TRANSCRIPT_FILE); - writeFileSync(transcriptPath, JSON.stringify(words, null, 2)); + replaceFileAtomically(transcriptPath, JSON.stringify(words, null, 2)); const durationSeconds = words.length > 0 ? words[words.length - 1]!.end : 0; return { model: PARAKEET_MODEL_LABEL,