Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Notable changes to house-rules. Versions follow [semantic versioning](https://se

## Unreleased

- `hr-code-review`'s panel recommendations catalog is generated: `scripts/refresh-recommendations.mjs` builds `panel-recommendations.json` from a source spec, a snapshot of the public Bug Hunt board (`--fetch` rewrites it) and the maintainer's own results (`--from-file` replaces them from a file of structured facts: a model and harness the spec lists, fixed choices and bounded numbers, with a deny file for private names; they live only in the catalog's own entries), and `--check` fails offline when the catalog is not what those inputs generate. The one-time refresh moves the Opus, Sonnet and Muse figures to the board's current rows and adds GPT-6.2 Astra and Gemini 4 Neon entries; entries gain a `kind`.
- `hr-code-review`'s panel recommendations catalog is generated: `scripts/refresh-recommendations.mjs` builds `panel-recommendations.json` from a source spec, a snapshot of the public Bug Hunt board (`--fetch` rewrites it) and the maintainer's own results (`--from-file` replaces them from a file of structured facts: a model and harness the spec lists, fixed choices and bounded numbers, with a deny file for private names; they live only in the catalog's own entries), and `--check` fails offline when the catalog is not what those inputs generate or the references folder holds a file the spec does not list or any symlink. The refresh no longer deletes inside a leftover transaction folder beyond the files it wrote, restores a file through a staged copy and keeps the before images when a restore fails, compares a stale lock's whole record before removing it, removes an interrupted run's before images, leaves the snapshot alone on an import, refuses malformed board rows and a repeated key in a fetched board, and states the board's planted total once. The one-time refresh moves the Opus, Sonnet and Muse figures to the board's current rows and adds GPT-6.2 Astra and Gemini 4 Neon entries; entries gain a `kind`.
- `hr-better-colors`' focus-ring table row says it gives the blue step and that other hues are measured (Radix yellow `9` is 1.26:1 on white); the paragraph under the table is split in three.
- `hr-better-colors` puts the focus ring and input boundary on Radix step `9`, not `8`: Radix blue `8` (`#5eb1ef`) measures 2.33:1 on white, `9` measures 3.26:1 (Tailwind `500` stays 3.68:1). The previous entry's wording gave step `8`.
- Five review points on the vendored jakubkrehel text (from the private reviewer on the pin adoption) are fixed in wording: `hr-better-accessibility` limits `novalidate` to forms whose submit handler renders the errors (a form that renders each error from the field's `invalid` event needs none) and carries the typed-name destructive-confirmation exception into `forms.md`, `semantics-and-aria.md` and the checklist; `hr-better-colors` stops giving the focus ring to the decorative `300` border step (`--blue-300` measures 1.80:1 on white) and gives the focus ring and input boundary their own table row on the first step that measures 3:1, and its class-based theme recipe resolves the theme in one function (a saved choice wins, System reads the current query value) run at startup, on a choice change and on the system `change` event; `hr-better-typography` says `65ch` and the `max-w-md` / `max-w-lg` mapping assume a proportional font (in single-width monospace text the range is `60ch`–`75ch` and `max-w-md` holds about 46 characters; full-width glyphs take two cells, so a mixed-width line needs a rendered count), with a separate cheat-sheet row for monospace.
Expand Down
10 changes: 8 additions & 2 deletions skills/hr-code-review/references/panel-recommendations.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ The import also needs a deny file of private names. The script refuses that file
the results file when either sits inside the skill folder, because the installer copies
that folder. Any published string that holds a denied name, in the spec, snapshot or
catalog, fails the run. The spec's `distributed` list names every file of this folder,
and `--check` fails on any other file, so a leftover private input cannot ship.
and `--check` fails on any other file of any name, and on any symlink, so a leftover
private input cannot ship. Only the script's own lock and work files at the top of
the folder are left out.

A plain run of the script regenerates the catalog and writes the snapshot in its one
canonical layout, which is what `--check` compares.
canonical layout, which is what `--check` compares; an import leaves the snapshot's
bytes alone. The spec's board task writes the planted total as `{plantedTotal}`, so the
task text always names the total the snapshot holds. A board row with a missing name or
an unreadable measurement, or a repeated key anywhere in the board, refuses the refresh
before any file changes, whether the board is fetched or saved.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"version": 1,
"board": {
"url": "https://bughunt.productcompass.pm/data/benchmark.json",
"task": "Find planted bugs: 105 across two repositories, count fixed",
"task": "Find planted bugs: {plantedTotal} across two repositories, count fixed",
"noisePoints": 1.5,
"timeDefinition": "wall clock summed over both repositories; the board says its machines were shared with other work for some rows, so times compare loosely at best",
"costDefinition": "list-rate API price estimated from usage counts, summed over both repositories; not an invoice or subscription billing"
Expand Down
144 changes: 110 additions & 34 deletions skills/hr-code-review/scripts/refresh-files.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
// File handling for refresh-recommendations.mjs: a JSON reader that refuses a repeated key, the one byte layout of
// the files it writes, an all-or-nothing multi-file write and the lock that serializes refreshes.
import { createHash } from "node:crypto";
import {
existsSync,
linkSync,
lstatSync,
mkdirSync,
mkdtempSync,
readdirSync,
readFileSync,
renameSync,
rmdirSync,
rmSync,
statSync,
unlinkSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { dirname, join, resolve } from "node:path";

/** The one byte layout every JSON file here is written in, so --check can compare bytes. */
export const serialize = (/** @type {unknown} */ value) => `${JSON.stringify(value, null, 2)}\n`;
Expand All @@ -21,41 +25,78 @@ export const serialize = (/** @type {unknown} */ value) => `${JSON.stringify(val
// that is the whole of what a later run reclaims.
const TRANSACTION_MARKER = ".transaction";
const TRANSACTION_TEXT = "refresh-recommendations transaction\n";
/** @param {string} dir the references folder */
/**
* Reclaim the transactions an interrupted run left in the references folder. Nothing is entered or removed
* recursively: a work folder is the tool's only when it is a real folder holding the marker file and plain
* `new-<n>` files, each checked without following links; those files are unlinked one by one and the folder is
* removed with a plain rmdir, which refuses a folder that still holds anything.
* @param {string} dir the references folder
*/
function reclaimAbandoned(dir) {
for (const name of readdirSync(dir)) {
const path = join(dir, name);
if (!/^\.refresh-[A-Za-z0-9]{6}$/.test(name) || !statSync(path).isDirectory()) {
if (!/^\.refresh-[A-Za-z0-9]{6}$/.test(name) || !lstatSync(path).isDirectory()) {
continue;
}
const inside = readdirSync(path);
const inside = readdirSync(path, { withFileTypes: true });
const marker = inside.find((e) => e.name === TRANSACTION_MARKER);
if (
inside.includes(TRANSACTION_MARKER) &&
readFileSync(join(path, TRANSACTION_MARKER), "utf8") === TRANSACTION_TEXT &&
inside.every((n) => n === TRANSACTION_MARKER || /^new-\d+$/.test(n))
marker?.isFile() !== true ||
readFileSync(join(path, TRANSACTION_MARKER), "utf8") !== TRANSACTION_TEXT ||
!inside.every(
(e) => e.isFile() && (e.name === TRANSACTION_MARKER || /^new-\d+$/.test(e.name)),
)
) {
rmSync(path, { recursive: true, force: true });
continue;
}
try {
for (const e of inside.filter((e) => e.name !== TRANSACTION_MARKER)) {
unlinkSync(join(path, e.name));
}
unlinkSync(join(path, TRANSACTION_MARKER));
rmdirSync(path);
} catch {
// Something arrived while cleaning up, or a file is held: leave what is left for the next run.
}
}
}

/**
* Where the before images of a write to this folder go: one private directory per folder, outside the skill
* folder, so no copy of the skill carries them. The folder's lock is held by whoever writes, so a directory
* already there is what a run that was killed left behind, and the next write removes it.
* @param {string} dir the references folder
*/
export const backupDirFor = (dir) =>
join(
tmpdir(),
`refresh-recommendations-${createHash("sha256").update(resolve(dir)).digest("hex").slice(0, 16)}`,
);

/**
* Write several files of one folder so that a failure leaves none of them changed. The new contents are staged
* in a new directory beside the targets (same filesystem, so each rename is atomic); the before images, which
* may hold data a replacement removes, go to a private directory outside the skill folder, so no copy of the
* skill ever carries them. The renames then run back to back, and a failed rename puts the earlier files back.
* Readers consume only the catalog, which goes last: an interruption leaves the old catalog complete, and
* may hold data a replacement removes, go to a private directory outside the skill folder (`backupDirFor`). The
* renames then run back to back, and a failed rename puts the earlier files back, each by staging a copy of its
* before image beside the target and renaming that over it, so a failure while restoring never leaves a
* truncated file. When a file cannot be restored the before images are kept and their folder is named in the
* error. Readers consume only the catalog, which goes last: an interruption leaves the old catalog complete, and
* --check reports the sources that are ahead of it until a plain run regenerates it.
* @param {Array<[string, unknown]>} outputs
* @param {{ rename?: (from: string, to: string) => void }} [io] a test seam for the rename
*/
export function writeAll(outputs) {
export function writeAll(outputs, io = {}) {
const rename = io.rename ?? renameSync;
const dir = dirname(outputs[0]?.[0] ?? ".");
if (outputs.some(([path]) => dirname(path) !== dir)) {
throw new Error("the outputs must be in one folder");
}
const stage = mkdtempSync(join(dir, ".refresh-"));
writeFileSync(join(stage, TRANSACTION_MARKER), TRANSACTION_TEXT, { flag: "wx" });
const backups = mkdtempSync(join(tmpdir(), "refresh-recommendations-"));
const backups = backupDirFor(dir);
rmSync(backups, { recursive: true, force: true });
mkdirSync(backups, { mode: 0o700 });
let keepBackups = /** @type {boolean} */ (false);
try {
const staged = outputs.map(([path, value], i) => {
const file = join(stage, `new-${i}`);
Expand All @@ -68,26 +109,46 @@ export function writeAll(outputs) {
}
return { path, file, back };
});
/** @type {typeof staged} */
const done = [];
try {
for (const item of staged) {
renameSync(item.file, item.path);
rename(item.file, item.path);
done.push(item);
}
} catch (error) {
for (const item of done) {
if (item.back === undefined) {
rmSync(item.path, { force: true });
} else {
// The before image lives on another filesystem, so it is copied back, not renamed.
writeFileSync(item.path, readFileSync(item.back));
/** @type {string[]} */
const unrestored = [];
done.forEach((item, i) => {
try {
if (item.back === undefined) {
rmSync(item.path, { force: true });
} else {
// The before image lives on another filesystem: copy it beside the target, then rename it over.
const copy = join(stage, `new-${outputs.length + i}`);
writeFileSync(copy, readFileSync(item.back), { flag: "wx" });
rename(copy, item.path);
}
} catch (restoreError) {
unrestored.push(`${item.path} (${/** @type {Error} */ (restoreError).message})`);
}
});
if (unrestored.length > 0) {
keepBackups = true;
const kept = `${backups}-kept-${Date.now()}`;
renameSync(backups, kept);
throw new Error(
`${/** @type {Error} */ (error).message}; could not put back ${unrestored.join(", ")}; the before images are kept in ${kept}`,
{ cause: error },
);
}
throw error;
}
} finally {
rmSync(stage, { recursive: true, force: true });
rmSync(backups, { recursive: true, force: true });
if (!keepBackups) {
rmSync(backups, { recursive: true, force: true });
}
}
}

Expand Down Expand Up @@ -126,11 +187,16 @@ export async function withLock(dir, fn, hooks = {}) {
throw error;
}
};
/** @param {string} path the owner's pid, "gone" when the file vanished, "unreadable" otherwise */
/**
* @param {string} path the owner's pid and the whole record (pid and token), "gone" when the file vanished,
* "unreadable" otherwise
* @returns {{ pid: number, record: string } | "gone" | "unreadable"}
*/
const ownerOf = (path) => {
try {
const owner = Number(readFileSync(path, "utf8").trim().split(/\s+/)[0]);
return Number.isInteger(owner) && owner > 0 ? owner : "unreadable";
const record = readFileSync(path, "utf8");
const pid = Number(record.trim().split(/\s+/)[0]);
return Number.isInteger(pid) && pid > 0 ? { pid, record } : "unreadable";
} catch (error) {
if (/** @type {any} */ (error).code === "ENOENT") {
return "gone";
Expand Down Expand Up @@ -158,21 +224,31 @@ export async function withLock(dir, fn, hooks = {}) {
if (owner === "gone" || owner === "unreadable") {
throw busy("its owner cannot be read");
}
let alive = true;
try {
process.kill(owner, 0);
} catch (error) {
alive = /** @type {any} */ (error).code === "EPERM";
}
if (alive || attempt >= 5) {
throw busy(`process ${owner}`);
const isAlive = (/** @type {number} */ pid) => {
try {
process.kill(pid, 0);
return true;
} catch (error) {
return /** @type {any} */ (error).code === "EPERM";
}
};
if (isAlive(owner.pid) || attempt >= 5) {
throw busy(`process ${owner.pid}`);
}
await hooks.afterRead?.();
if (!claim(takeover)) {
throw busy(`a stale lock is being recovered; remove ${takeover} if no refresh is running`);
}
try {
if (ownerOf(lock) === owner) {
// Under the recovery lock the lock must still be the record that was observed, whole (a pid alone can be
// reused by a later holder), and its owner still gone.
const now = ownerOf(lock);
if (
now !== "gone" &&
now !== "unreadable" &&
now.record === owner.record &&
!isAlive(now.pid)
) {
rmSync(lock, { force: true });
}
} finally {
Expand Down
Loading