Files
hyperframes/packages/cli/src/utils/projectLock.ts
T
WaterrrForeverandClaude Opus 5.5 4202462ca5 fix(cli): print project file names in check's stderr notes as visible escapes (#8485)
* fix(cli): print project file names in check's stderr notes as visible escapes (SECOPS-2752)

The text_over_face note printed the unread video's reason raw, and that
reason carries the project's file name and the decoder's own error text,
so a video named with ESC, BEL or CR could drive the terminal during
`hyperframes check`. The same check output path had four more lines
printing project-derived text raw: the project-lock waiting notice (the
holder is read from the project's lock file), the "images did not load"
warning (image addresses read back from the page), the font-localization
warning (the injector's error text), and the saved report's Revision line
(read from the project's history).

Each now goes through the existing @hyperframes/core terminal-text helper:
terminalLines where the text is an error message whose line breaks are
kept, terminalText for one-line notices. Plain names print as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): keep check's escape fix within the comment ratchet and testable on Windows (SECOPS-2752)

Drop the three comment lines the escape fix added to source files, which
raised their comment share. The text_over_face hostile-name test now feeds
the name through the candidate and an injected reader, so it runs on every
platform; the real-reader case, which needs a file named with control
characters, runs everywhere but Windows, whose file names cannot hold them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 16:56:27 +00:00

136 lines
4.3 KiB
TypeScript

import { readFileSync, unlinkSync } from "node:fs";
import { terminalText } from "@hyperframes/core/terminal-text";
import { createFileAtomically } from "@hyperframes/core/atomic-file";
import { join } from "node:path";
import { setTimeout as sleep } from "node:timers/promises";
import { recordFolder } from "./projectRecords.js";
import { readToolContext, type ToolContext } from "./toolContext.js";
export class ProjectLockTimeout extends Error {}
export interface Holder {
pid: number;
command: string;
startedAt: number;
}
const POLL_MS = 250;
export const projectLockPath = (projectDir: string) =>
join(projectDir, ".hyperframes", "project.lock");
export function readHolder<T extends Holder = Holder>(path: string): T | null {
try {
return JSON.parse(readFileSync(path, "utf8")) as T;
} catch {
return null;
}
}
export function alive(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch (error) {
return (error as NodeJS.ErrnoException).code === "EPERM";
}
}
const sameHolder = (a: Holder | null, b: Holder) =>
a?.pid === b.pid && a.startedAt === b.startedAt && a.command === b.command;
function breakStaleHolder(path: string, current: Holder, holder: Holder): boolean {
const breakPath = `${path}.break`;
if (!createNew(breakPath, holder)) return false;
try {
if (sameHolder(readHolder(path), current)) {
try {
unlinkSync(path);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
}
}
return tryAcquire(path, holder);
} finally {
if (sameHolder(readHolder(breakPath), holder)) unlinkSync(breakPath);
}
}
function createNew(path: string, holder: Holder): boolean {
try {
createFileAtomically(path, JSON.stringify(holder));
return true;
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "EEXIST") return false;
throw error;
}
}
function tryAcquire(path: string, holder: Holder): boolean {
if (createNew(path, holder)) return true;
const current = readHolder(path);
if (current && !alive(current.pid)) return breakStaleHolder(path, current, holder);
return false;
}
const FIRST_NOTICE_MS = 5_000;
const NOTICE_EVERY_MS = 30_000;
function sayWaiting(current: Holder | null, waitMs: number): void {
if (!current) return;
const running = Math.round((Date.now() - current.startedAt) / 1000);
console.error(
terminalText(
`[hyperframes] waiting for ${current.command} (pid ${current.pid}, running ${running}s) to finish with this project; waiting up to ${Math.round(waitMs / 1000)}s`,
),
);
}
export async function withProjectLock<T>(
projectDir: string,
command: string,
run: () => Promise<T>,
context: ToolContext = readToolContext(),
): Promise<T> {
if (!context.projectLock) return run();
const path = projectLockPath(projectDir);
if (!recordFolder(projectDir, ""))
throw new Error(
`${join(projectDir, ".hyperframes")} is a link or not a folder; remove it and run again`,
);
const holder: Holder = { pid: process.pid, command, startedAt: Date.now() };
const deadline = holder.startedAt + context.projectLock.waitMs;
let nextNoticeAt = holder.startedAt + FIRST_NOTICE_MS;
while (!tryAcquire(path, holder)) {
if (Date.now() >= nextNoticeAt) {
nextNoticeAt = Date.now() + NOTICE_EVERY_MS;
sayWaiting(readHolder(path), context.projectLock.waitMs);
}
const remaining = deadline - Date.now();
if (remaining <= 0) {
const current = readHolder(path);
const breakPath = `${path}.break`;
const breaker = readHolder(breakPath);
if ((!current || !alive(current.pid)) && breaker && !alive(breaker.pid)) {
throw new ProjectLockTimeout(
`hyperframes left ${breakPath} (pid ${breaker.pid}); delete it to continue`,
);
}
throw new ProjectLockTimeout(
`hyperframes ${current?.command ?? "another command"} (pid ${current?.pid ?? "unknown"}) ` +
`is still using this project after ${Math.round(context.projectLock.waitMs / 1000)}s. Run this command again when it finishes.`,
);
}
await sleep(Math.min(POLL_MS, remaining));
}
try {
return await run();
} finally {
release(path, holder);
}
}
export function release(path: string, holder: Holder): void {
if (sameHolder(readHolder(path), holder)) unlinkSync(path);
}