Files
filip131311 b232114422 docs: audit every comment in src against the code it describes (#931)
Audits every comment in `src/` and `scripts/` against the code it
describes.

**462 files, 57 commits, net −8,194 lines.** Comment text only — the
whole branch is code-identical to `main`.

## Method

One subagent per file, strictly sequential. Scope was `src/` +
`scripts/` (459 files); three more files were added at the end because
they carried dead references of the same kind — two test headers citing
design docs that do not exist, and `publish-npm.yml` citing a retired
workflow. Each agent verified every comment — line, block, JSDoc, file
header, trailing — against the surrounding code and the rest of the
repo, following identifiers, paths, tool ids, config keys, env vars and
issue links to see whether they still exist and still behave as
described. Rules:

- **A false or misleading comment is deleted, not reworded.** If a claim
could not be confirmed by reading the source, it went. That is why the
deletion count is so much larger than the rewrite count.
- Survivors are cut to the shortest form carrying something the code
does not already say. Restatement, preamble, hedging, changelog prose
and ASCII banners are gone; the non-obvious *why* stays.
- Preserved byte-identical: license headers, pragmas and directives
(`@ts-*`, `eslint-disable`, shebangs, `/// <reference>`), JSDoc tag
tokens, everything inside a string or template literal, and the sole
comment inside an otherwise empty block (ESLint `no-empty` counts a
comment-bearing block as non-empty).

## Verification

Every file passed two independent gates before being recorded as done:

1. `comments-only` — the required check.
2. A second comment-stripping comparator with a proper mode stack,
written for this pass because `comments-only`'s flat scanner desyncs on
nested template literals and quote-bearing regex literals and then
reports comment lines as code changes. Two files hit that false FAIL
(`utils/android-profiler/pipeline/index.ts`,
`scripts/extract-tools.mjs`); in both the "changed code" it printed was
literally `//` lines, and the second checker confirmed the code was
byte-identical.

After the last file, all 462 changed files were re-checked against
`main` with the same comparator, rather than trusting any agent's
self-report. **459 code-identical; 2 are non-code (`.svg`, `.md`); 1
intentional.**

The intentional one is `packages/argent/scripts/bundle-tools.cjs`: the
changed template literal *is* the comment header of the file it
generates, `packages/native-devtools-android/src/bundled-meta.ts`.
Fixing only the generated file would have been reverted by the next
build, so the generator changed too — and it has been verified to
reproduce the committed generated file byte-for-byte.

## Representative false claims removed

Not wording nits — statements a reader would have acted on:

- **Reversed directions.** `proxyStart`'s JSDoc had the tunnel backwards
(it is a reverse tunnel: the host binds first and the simulator dials
in). A `paste()` doc had the pasteboard copy direction reversed.
- **Contradicted by the code below it.** A timeout budget multiplied by
three where the probes run concurrently — the same comment said so six
lines later. A "warn once" that warns on every call. A "binary search"
that is a linear scan.
- **Named things that do not exist.** A `vega-fast-cli` binary, a
`finish-recording.ts`, a `publish-next.yml` workflow, two
`profiler-react19-*.md` design docs, a `DebuggerTarget.ts`, a commit
hash git does not know, two tool ids, an `ensureEnv` cycle.
- **Wrong by construction.** "Welford accumulators" across four files
where the code keeps naive `n`/`sum`/`sumSq`; `sum`/`sumSq` documented
over `actualDuration` when reduce sums `selfDuration`; a strict-mode
halving written `n/2` where the code ceils; field docs listing enum
values the producers never emit.
- **Guarantees the code does not make.** A validation matrix claiming to
cover "EVERY tool" that skips flagless ones; a Pareto cutoff that
`slice(0, 20)` makes inert; an idempotence claim where the real rule is
at-or-ahead; a capability note describing a clean 400 the shape-based
device resolver can never produce.
- **Unverifiable assertions** about prebuilt binaries, external CLIs and
the cloud SDK — deleted rather than kept as folklore, since nothing in
the repo can confirm them.
- **Stale numbers**: invented Android tool versions, hard-coded tool
counts and description lengths that had drifted.

## Review

A Fable agent reviewed both halves adversarially for over-deletion,
misread code, `no-empty` hazards and byte-identity violations.
Second-half verdict: **SHIP**, with two one-line restores, both applied
in the final commit — the `npm view ""` rationale behind a blank-token
guard, and the note that `argent-mcp` keeps a copy of
`SECRET_PLACEHOLDER_MARKER` it cannot import.

## Code issues surfaced but deliberately not fixed

This pass changes comments only. Eight genuine findings are logged for a
follow-up:

1. `telemetry/src/consent.ts` — a non-ENOENT read error returns null and
falls through to the default-on path, so file errors *can* silently flip
telemetry on.
2. `chromium-server/navigation.ts` — `navigate()` is reachable from
`POST /api/navigate` with only a `typeof === "string"` check; open-url's
schema is a bare `z.string()`, so the "already validated by zod" premise
never held.
3. `http.ts` — `constantTimeEqual` returns early on a length mismatch,
so the auth token's length is observable.
4. `describe/index.ts:~114` — the ios-remote branch passes `{ isTvOs:
false }` unconditionally, so a remote tvOS simulator takes the iOS
ax-service path, though `isRemoteTvOsSimulator` exists and shake/paste
do use it.
5. `devices/boot-device.ts` — `-crash-report-mode never` is passed
unconditionally *and* appended again by the feature-detecting path, so
every emulator spawn passes it twice.
6. `react-profiler/pipeline/04-rank.ts` — `PARETO_THRESHOLD_PCT` is
dead: `slice(0, 20)` always wins.
7. `reaped-sessions.ts` — a user-facing hint string tells the agent that
`react-profiler-start { force: true }` disposes the debugger and
profiler session; it does not. Left byte-identical because it is a
string literal, not a comment.
8. `utils/simctl-backend.ts` — `localSimctl` is exported with no
importers anywhere.

## Docs

No documentation change is needed: this pass touches only source
comments, and no user-facing capability, tool, CLI flag, config key or
flow-file behaviour changed.

---------

Co-authored-by: filip131311 <f.kaminski2000@gmail.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-24 12:16:20 +02:00

275 lines
9.7 KiB
JavaScript

// @ts-check
/**
* Generates one Open Graph image per page at build time and points the
* `og:image` / `twitter:image` tags of every built HTML file at it.
*
* The card is `static/img/og-background.png` with a frosted glass panel, the
* white Argent logo centred on the panel and the page title underneath.
*
* Background and panel are the same on every card, so they are rendered once
* into a bitmap and reused; only the logo and the title are per page.
*/
const fs = require("node:fs/promises");
const path = require("node:path");
/** Standard Open Graph card size. */
const WIDTH = 1200;
const HEIGHT = 630;
/** Frosted glass panel, centred on the canvas. */
const PANEL = {
width: 920,
height: 400,
radius: 32,
/** `#C3D3E033`, drawn over the blurred background. */
tint: "#C3D3E0",
tintOpacity: 0x33 / 0xff,
/** `backdrop-filter: blur(60px)`; an SVG blur takes half that as its standard deviation. */
blurStdDeviation: 30,
};
const PANEL_X = (WIDTH - PANEL.width) / 2;
const PANEL_Y = (HEIGHT - PANEL.height) / 2;
/** Logo and title inside the panel. */
const LOGO_WIDTH = 280;
const LOGO_ASPECT = 36.75 / 194; // viewBox of static/img/logo.svg
const LOGO_GAP = 40;
const TITLE_FONT_SIZE = 56;
const TITLE_LINE_HEIGHT = 1.25;
const TITLE_COLOR = "#FFFFFF";
const CONTENT_PADDING = 64;
const ASSETS_DIR = path.join(__dirname, "..", "..", "scripts", "og-assets");
const STATIC_IMG_DIR = path.join(__dirname, "..", "..", "static", "img");
/** Card output directory, relative to the build output. */
const OUT_SUBDIR = path.join("img", "og");
/**
* @param {string} baseUrl
* @param {string} routePath
* @returns {string}
*/
function fileNameForRoute(baseUrl, routePath) {
const relative = routePath.startsWith(baseUrl) ? routePath.slice(baseUrl.length) : routePath;
const slug = relative.replace(/^\/+|\/+$/g, "").replace(/[^a-zA-Z0-9]+/g, "-");
return `${slug === "" ? "index" : slug.toLowerCase()}.png`;
}
/**
* Reads the page title out of a built HTML file: Docusaurus renders
* `<title>Page | Argent</title>`, and `<title>Argent</title>` on the home page.
*
* `null` means a card with the logo alone: the home page, whose title is the
* site title the logo already shows, and the search page, whose title the
* search theme renders as `[object Object]`.
*
* @param {string} html
* @param {string} siteTitle
* @returns {string | null}
*/
function titleFromHtml(html, siteTitle) {
const match = /<title[^>]*>([\s\S]*?)<\/title>/i.exec(html);
if (!match) {
return null;
}
const decoded = match[1]
.replace(/&amp;/g, "&")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, '"')
.replace(/&#(\d+);/g, (_, code) => String.fromCodePoint(Number(code)))
.trim();
const withoutSuffix = decoded.replace(new RegExp(`\\s*\\|\\s*${siteTitle}\\s*$`), "").trim();
if (withoutSuffix === "" || withoutSuffix === siteTitle || withoutSuffix === "[object Object]") {
return null;
}
return withoutSuffix;
}
/**
* Points the social image tags of a built page at `imageUrl` and declares the
* card size.
*
* @param {string} html
* @param {string} imageUrl
* @returns {string}
*/
function rewriteImageTags(html, imageUrl) {
const escaped = imageUrl.replace(/&/g, "&amp;").replace(/"/g, "&quot;");
/**
* @param {string} source
* @param {RegExp} tagPattern
* @returns {string}
*/
const setContent = (source, tagPattern) =>
source.replace(tagPattern, (tag) => tag.replace(/content="[^"]*"/i, `content="${escaped}"`));
let next = setContent(html, /<meta\b[^>]*\bproperty="og:image"[^>]*>/gi);
next = setContent(next, /<meta\b[^>]*\bname="twitter:image"[^>]*>/gi);
if (!/property="og:image:width"/i.test(next)) {
next = next.replace(
/<meta\b[^>]*\bproperty="og:image"[^>]*>/i,
(tag) =>
`${tag}<meta property="og:image:width" content="${WIDTH}"/>` +
`<meta property="og:image:height" content="${HEIGHT}"/>`
);
}
return next;
}
/**
* The background and frosted glass panel, identical on every card.
*
* @param {(svg: string) => Buffer} renderSvg
* @returns {Promise<string>} a `data:` URI holding the rendered PNG
*/
async function renderPlate(renderSvg) {
const background = await fs.readFile(path.join(STATIC_IMG_DIR, "og-background.png"));
const backgroundUri = `data:image/png;base64,${background.toString("base64")}`;
const rect = `x="${PANEL_X}" y="${PANEL_Y}" width="${PANEL.width}" height="${PANEL.height}" rx="${PANEL.radius}" ry="${PANEL.radius}"`;
const svg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${WIDTH}" height="${HEIGHT}" viewBox="0 0 ${WIDTH} ${HEIGHT}">
<defs>
<clipPath id="panel"><rect ${rect}/></clipPath>
<filter id="frost" x="-25%" y="-25%" width="150%" height="150%" color-interpolation-filters="sRGB">
<feGaussianBlur stdDeviation="${PANEL.blurStdDeviation}"/>
</filter>
</defs>
<image x="0" y="0" width="${WIDTH}" height="${HEIGHT}" xlink:href="${backgroundUri}"/>
<g clip-path="url(#panel)">
<image x="0" y="0" width="${WIDTH}" height="${HEIGHT}" filter="url(#frost)" xlink:href="${backgroundUri}"/>
<rect ${rect} fill="${PANEL.tint}" fill-opacity="${PANEL.tintOpacity}"/>
</g>
</svg>`;
return `data:image/png;base64,${renderSvg(svg).toString("base64")}`;
}
/**
* The Argent logo recoloured white, as a `data:` URI satori can draw.
*
* @returns {Promise<string>}
*/
async function loadWhiteLogo() {
const source = await fs.readFile(path.join(STATIC_IMG_DIR, "logo.svg"), "utf8");
const white = source.replace(/#0D0F26/gi, TITLE_COLOR);
return `data:image/svg+xml;base64,${Buffer.from(white).toString("base64")}`;
}
/** @type {import('@docusaurus/types').PluginModule} */
module.exports = function ogImagePlugin() {
return {
name: "argent/og-image",
async postBuild({ outDir, routesPaths, siteConfig }) {
const { default: satori } = await import("satori");
const { Resvg } = require("@resvg/resvg-js");
/** @param {string} svg */
const renderSvg = (svg) =>
new Resvg(svg, {
fitTo: { mode: "width", value: WIDTH },
// Satori embeds every glyph as a path, so no font lookup is needed.
font: { loadSystemFonts: false },
})
.render()
.asPng();
const font = await fs.readFile(path.join(ASSETS_DIR, "DMSans-Medium.ttf"));
const [plateUri, logoUri] = await Promise.all([renderPlate(renderSvg), loadWhiteLogo()]);
const outputDir = path.join(outDir, OUT_SUBDIR);
await fs.mkdir(outputDir, { recursive: true });
const baseUrl = siteConfig.baseUrl;
const imageBase = `${siteConfig.url.replace(/\/+$/, "")}${baseUrl}${OUT_SUBDIR.split(path.sep).join("/")}/`;
let generated = 0;
for (const routePath of routesPaths) {
// Most routes are directories holding an index.html; `/404.html` is
// a file.
const relative = path.relative(baseUrl, `/${routePath.replace(/^\/+/, "")}`);
const htmlPath = routePath.endsWith(".html")
? path.join(outDir, relative)
: path.join(outDir, relative, "index.html");
let html;
try {
html = await fs.readFile(htmlPath, "utf8");
} catch {
continue;
}
const title = titleFromHtml(html, siteConfig.title);
// Satori takes the React element shape, which is a plain object, so
// the card is described as an object literal rather than JSX.
const element = /** @type {import("react").ReactNode} */ (
/** @type {unknown} */ ({
type: "div",
props: {
style: {
width: PANEL.width,
height: PANEL.height,
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
padding: CONTENT_PADDING,
},
children: [
{
type: "img",
props: {
src: logoUri,
width: LOGO_WIDTH,
height: Math.round(LOGO_WIDTH * LOGO_ASPECT),
},
},
title === null
? null
: {
type: "div",
props: {
style: {
marginTop: LOGO_GAP,
display: "flex",
fontFamily: "DM Sans",
fontWeight: 500,
fontSize: TITLE_FONT_SIZE,
lineHeight: TITLE_LINE_HEIGHT,
color: TITLE_COLOR,
textAlign: "center",
},
children: title,
},
},
],
},
})
);
const layer = await satori(element, {
width: PANEL.width,
height: PANEL.height,
fonts: [{ name: "DM Sans", data: font, weight: 500, style: "normal" }],
});
const card = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${WIDTH}" height="${HEIGHT}" viewBox="0 0 ${WIDTH} ${HEIGHT}">
<image x="0" y="0" width="${WIDTH}" height="${HEIGHT}" xlink:href="${plateUri}"/>
<g transform="translate(${PANEL_X} ${PANEL_Y})">${layer.replace(/^<svg[^>]*>|<\/svg>$/g, "")}</g>
</svg>`;
const fileName = fileNameForRoute(baseUrl, routePath);
await fs.writeFile(path.join(outputDir, fileName), renderSvg(card));
await fs.writeFile(htmlPath, rewriteImageTags(html, `${imageBase}${fileName}`));
generated += 1;
}
console.log(`[og-image] generated ${generated} Open Graph images in ${OUT_SUBDIR}`);
},
};
};