mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-10-30 17:27:09 +00:00
* refactor(skills): fold six domain skills into hyperframes and media-use guides
hyperframes-core, -animation, -keyframes, -creative and -studio move to
skills/hyperframes/{core,animation,keyframes,creative,studio}/, and
hyperframes-audio moves to skills/media-use/mix/. Each moved SKILL.md becomes
a frontmatter-free GUIDE.md so installers that scan recursively for SKILL.md
no longer see them as skills; the published list goes from 21 to 15.
Relative links, code paths and skill-name mentions are rewritten to the new
locations, the catalog surfaces, marketplace core-skills list, fallback core
set, module-copy targets and skills-manifest.json follow, and
`hyperframes skills update` now removes the retired skills (RETIRED_SKILLS)
when they are still installed.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* feat(skills): slim the core entries and give media-use a HeyGen and a free path
The four entries every run opens are rewritten to what an agent cannot infer:
hyperframes 21.8 KB to 7.7 KB, hyperframes-cli 24.8 KB to 6.9 KB,
hyperframes-registry 9.9 KB to 4.3 KB, media-use 9.7 KB to 4.9 KB. Detail moved
whole into references (routing close calls and creator edits, CLI pin upgrades,
catalog search tiers, CLI conventions and feedback reports).
- hyperframes: state table, routing, the render contract, a terminal work loop
(check, snapshot, preview before render), the desktop-app hand-off, and a
table of the folded guides.
- media-use: one table of HeyGen versus free local tools per need; HeyGen
sign-in, credits and the CLI's video-agent, avatar, clone, lipsync, translate
and clipping commands; every free path works with no account.
- scripts/check-core-skills.mjs holds entries and descriptions to budget and
checks every taught `npx hyperframes` command and path; runs in lint.
- Core entries are exempt from the formatter: its table padding cost ~1.5 KB
per entry on every run.
- Content tests follow the text to its new home.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(skills): plugin runs skip skills update; media-use explains HeyGen sign-in and TTS setup
The smoke eval showed a presenter request never reaching media-use and the
free voiceover failing on a missing kokoro-onnx; plugin runs also ran the
skills update the plugin rules forbid.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* chore(skills): drop a committed Python bytecode file and ignore bytecode
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* feat(skills): align the entry with the Studio skills and keep the workflows' measured facts
The entry drops routing and the intent interview and carries the contract, the
editable-project rules and the work loop. Studio's format, runtimes, motion,
look and captions references come over for terminal use, frame presets move to
assets/, media-use gains Studio's mixing and local-command references, and
film.md and overlays.md keep what the workflow skills measured.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* feat(skills)!: retire the workflow skills and folded guides; publish seven knowledge skills
The skill pack now publishes exactly seven skills - hyperframes,
hyperframes-cli, hyperframes-registry, media-use, figma, slideshow and
remotion-to-hyperframes - carrying knowledge, not process. The eight
workflow skills, the folded domain guides and the routing/interview
references are deleted; their measured facts already live in the
hyperframes references (format, runtimes, motion, look, captions, film,
overlays) and media-use's mixing reference.
- bake-basemap.mjs and locate.mjs move to skills/hyperframes/scripts/
(bake-basemap gains --help); extract-audio-data.py moves to
media-use/audio/scripts/ for the caption motion reference.
- skills update: the eight workflows join RETIRED_SKILLS so existing
installs prune them (by lock attribution only, since their names are
generic); naming a retired skill reports where its knowledge went and
succeeds instead of failing as unknown.
- Capture prompts point at the hyperframes skill's references/film.md;
the live-map warning names the new bake-basemap path.
- Content tests that pinned knowledge are retargeted to the kept
references; tests of deleted workflows, scripts and process go.
- Catalog surfaces, templates and docs describe the seven skills.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(skills): resolve the local.md captions link and allow capture's output files in citations
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(skills): regenerate the skills manifest from tracked files only
The committed media-use hash included an ignored __pycache__ bytecode file present
in the local checkout, so CI's clean checkout hashed differently.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(docs): restore generated catalog pages and docs navigation to main
Catalog PRs commit item sources only; CI regenerates the pages from the edited
registry sources. docs/docs.json is catalog navigation, so its Workflows group
rename is left for a non-catalog PR.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* build(cli): raise the CLI size budget for the skill references it bundles
The CLI bundles skills/hyperframes, which now carries the folded domain guides and
frame presets (155 KB to 1.08 MB in git). Budgets are the CI-measured size plus 5%:
packed 7526634, unpacked 22593213.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* test: keep repo-sweep guards meaningful after the workflow skills left
The last shell ffprobe caller and most root index.html examples lived in retired
skills. The shell detector is now pinned on a literal command line, and the
structure-rule sweep needs more than 5 roots (11 remain) rather than 30.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* feat(skills): slim figma, slideshow and remotion to the Studio shape; restore the storyboard board
slideshow 33.5 KB to 4.9, figma 18.2 to 6.2, remotion-to-hyperframes 11.0 to
4.9, each keeping the facts only it holds (the figma motion helpers, verify gate,
storyboard decoding and usage beacons; present and hand-off; the Remotion lint,
SSIM check and API map). The board page format from the storyboard recipe comes
back as hyperframes/references/storyboard.md: the desktop app's Storyboard tab
reads boards/storyboard.html by its frame, title and time classes. The entry and
the init template now say to snapshot the moments an edit changed before
replying, which the terminal eval found skipped in 22 of 38 runs.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(skills): manifest without local bytecode; Studio's edit prompt names kept files
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(skills): render when the person asks for the file or for no stops, not only after approval
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(skills): keep the voiceover carve in media-use; presets stage their own fonts
Review follow-ups on #5651:
- carve.mjs and its tests move to skills/media-use/audio/scripts; the audio
docs, mixing.md and two Studio comments point there.
- code-editorial's FRAME.md says to copy its bundled WOFF2s and add the
@font-face blocks, instead of promising the retired build-frame.mjs.
- media-use states the host rule before the HeyGen sign-in steps.
- storyboard-format marks transition_in as a note the agent builds itself.
- Two stale citations (creator-editing-recipes.md, sub-compositions.md).
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* chore(skills): regenerate the manifest without local bytecode
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(skills): the CLI render step follows step 4's rule, not approval alone
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(ci): run the core-skills check from lint again after the main merge
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* feat(skills): films that tell a story start from three storylines; maps use real imagery
Opus 5.5 arm D read motion.md and applied it, but its launch film was a
feature list (pain, three feature cards, proof, logo): film.md left story
to the model. And the map case never opened film.md, so it drew Natural
Earth outlines instead of baking satellite tiles.
- references/story.md: the generic half of Studio's moment-film story and
craft (one thing, three storylines with world and device, the hook,
beats, worlds, copy, brand, tells of a generated film). The person picks
a storyline, or the agent takes its recommendation when asked for the
file or for no stops.
- The entry's work loop names the storyline step for films over ~15 s; the
references table routes stories to story.md and maps to film.md.
- film.md points at story.md and gives maps and real places a section.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(skills): story.md follows the entry: no voiceover unless asked
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(skills): bake-basemap starts from a globe and stops waiting out its idle timeout
The Opus 5.5 map run spent 13 of its 38 minutes on parallel bakes that
stalled and 15 more recording 300 frames, after rewriting the script because
it could only start on a flat map.
- PROJECTION=globe with a seeded starfield and atmosphere, and FROM/PAN to
turn the globe to CENTER before the dive.
- Each frame polls for loaded tiles instead of waiting for an "idle" event
that had often already fired: 30 frames went from 198-219 s to 10-13 s
locally with byte-identical frames.
- Progress prints seconds a frame and minutes left; film.md shows the globe
call and says to run one bake at a time.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(skills): render in the foreground; wait on a detached render's log, never pgrep -f
Both Opus 5.5 explainer runs started a detached render, then waited with
`until ! pgrep -f "hyperframes render"`, which matches its own loop: each
render took 88-117 s and the wait sat for the tool's full 10 minutes.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* feat(skills): route each kind of film; storylines carry look and sound; add review and sound
The entry routes by request (launch, explainer, music, data, vertical, 3D,
character) to a self-contained file under references/types/, and a film
over ~15 s now gets music and sounds on its visible actions unless the
person asked for silence. Each storyline names its look and its sound, so
picking one picks both; STORYBOARD.md frames carry `reads` and `sound`.
review.md is the whole-film pass: the check warnings, the review sheets,
seven questions to look for, P0/P1/P2 notes and REVIEW.md. motion.md gains
eases by role, six title entrances, a reading-time rule and nine measured
transitions; look.md a design plan before the first scene, with presets
kept for decks and named picks. story.md adds the second default that
generated design falls into, with what to do instead.
The blank template and format.md no longer teach the fade-and-slide-up
entrance on a dark canvas.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* feat(cli): check warns on unread lines, dead stretches, small type, empty frame 0 and silence
A coding agent cannot watch its film, so check now measures what a viewer
notices over time. A new `craft` section holds warnings only (exit 0
unless --strict), sampled on the dense 8 fps grid the overlap pass
already seeks, skipped for --range checks:
- line_unread: a reading-size line (>= 30 px tall at 1080) on screen
under 0.4 s for 1-2 words, else under 3/4 of 0.5 s + 0.3 s a word;
the film's last line needs 2 s. Short lines are re-measured between
samples to ~0.01 s before judging. Captions-track and
data-layout-ignore text are exempt.
- dead_stretch: more than 3 s where the keepsMoving liveness signature
does not change, outside the final 3 s and any data-hold element;
quiet when sweep_static fires or a whole-composition keepsMoving
assertion owns the limit.
- text_below_floor: text under 2.6% of the short side (28 px on 1080)
held 0.5 s, one finding per file listing every element.
- first_frame_empty: frame 0 paints only the background.
- film_silent: over 15 s with no unmuted audio or audible video.
Each message names the fix and the skill reference to read. keepsMoving
now also leaves the film's last 3 s (a still end card) out of its
static runs, so the two agree.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* feat(cli): snapshot --review captures a beat sheet, seam strips and a phone sheet
snapshot --review picks the frames that show a film over time instead of
evenly spaced ones, in the same single browser session:
- review-beats.jpg: one settled frame per scene at its start + 0.3 s,
plus one per timed data-beat="name" element, labelled time and id.
Scenes are the composition's top-level timed clips and sub-composition
hosts, read like `timeline` reads rows (captions and audio excluded)
and timed by the runtime's start resolver over the live page.
- review-seam-NN.jpg: frames at -0.2, -0.1, 0, +0.1, +0.2 s around
every cut (each scene start, and any scene end no scene starts at).
- review-phone.jpg: the beat frames at 360 px wide.
--composition scopes the review to one sub-composition; with fewer than
two scenes the beats spread evenly and a note says so. snapshot also
gains --json: one envelope with every frame's path and time and, under
--review, the scenes, beats and seams behind the sheets; progress moves
to stderr.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(cli): craft warnings stay out of --strict; a hold gets a real next action, not a caret
Craft findings (unread lines, dead stretches, small type, an empty first
frame, silence) are judgment calls a film may break on purpose, so
--strict keeps gating defects only. The check reference no longer
suggests a blinking caret to keep a still frame moving.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(cli): review spreads beats when every scene starts together; map credits skip the type floor
A map film whose basemap and overlay both start at 0 s got one beat
frame; the even spread now keys on distinct scene starts. film.md tells
agents to mark the imagery credit data-layout-ignore and back it, since
the new type floor flagged it on the first real run.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(cli): review frames each scene at its middle too, not only 0.3 s in
On the first real explainer, 0.3 s into a scene showed a bare colour
field before its title entered. Each scene longer than about a second
and a half now adds a frame at its middle, so the sheet shows what the
scene builds.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* feat(skills): a film is built from a numeric SPEC.md, with defaults banned by name and a look test
Studied in full: flxrnc's Motion Lab (vague vs directed prompt), 0xMovez's
course, EianLu's riso skill, agentchengfeng's style samples and the two
Opus 5.5 guides. Their common point: adjectives are where the default look
lives, so the agent now writes SPEC.md before any scene (GOAL, OUTPUT,
TIME, LOOK, MOTION, SHOTS, BANNED, QA), with a number for every adjective.
spec.md bans the defaults by name, unless the brand or reference uses
them (a dark void with small things floating, glow and particles, the
cream-serif-mono-label look, fade-and-slide-up, corner brackets), and
grows that list after the first render. Each storyline gets a 3 s test of
its hero shot before the pick; a look is written as the rules of one
medium. Composition numbers: the hero fills 40% of the frame, one hero
move per shot, the shot's information in within 2.5 s then a held poster.
motion.md adds springs by damping ratio, shorter exits, overlap, stagger
and the beat grid; review.md reviews as a harsh director and grows BANNED;
vertical.md takes the riso skill's measured safe band; explainer.md the
shapes of information.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(skills): story before look; every object reads as itself; posters only for info cards
The first spec-driven C02 swapped the dark default for a flat Swiss
poster, and the story went with it: two black bars nobody read as mirrors,
slogans instead of cause and effect, a storyline picked for its picture.
The spec now serves the story: a storyline is picked for the story a
stranger would follow, the look test asks first whether a stranger
understands what they see, and SPEC.md gains an OBJECTS line giving each
thing the material and details that make it read without a label. The
2.5 s held-poster rule is limited to information cards; in a story film
every shot causes the next. Explainers start from a person or situation
the viewer knows. Review names every object as a stranger would and
treats one that needs explaining as a P0.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(skills): storylines are three films in words, each with a device and a form
The three storylines are pitched as text and the person picks from them;
nothing is built for a storyline before the pick, so the 3 s hero-shot
look test is gone. Ideas pair a device with a form, five or more first,
keeping the three that differ most, and the one stop states the rest in
an assumption line that names every paid item beside its free option.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* ci: list main's new embedded-captions hf-cli test among the retired workflow files
The merge with main removed it with the rest of the retired skill, and the
deletion guard rejects any removal it has not been told about.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(core): keep the audio preset comment within core's size budget
The comment's new citation to media-use's mixing reference made the published
package 14 bytes larger than its budget, which sits at main's exact size.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(cli): craft keeps its own 8 fps grid; comments back within the checks
Review on #5651 found that the merge moved craft onto the overlap pass's
times, which main caps at 180, so a brief line in a long film fell between
samples and line_unread missed it. Craft now builds its own grid at 8 fps up to
600 samples, as before the merge, and a 60 s film with a line shown for 0.2 s
pins it.
The Comments check passes again: REVIEW.md and STORYLINES.md join the files a
skill writes into a project, the comment lines this branch added come back
under each file's share at the merge-base, and the audio preset comment drops
a word the check reads as history.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1041 lines
39 KiB
JavaScript
1041 lines
39 KiB
JavaScript
#!/usr/bin/env node
|
|
// fallow-ignore-file complexity
|
|
// Grades the comments of the files a PR changed; the rules and their reasons are in CONTRIBUTING.md.
|
|
// Citations (path, path:line, backticked camelCase symbol, "pinned by <file>") must resolve.
|
|
// Blocks the diff touched may not run past 40 lines, narrate history, or be commented-out code.
|
|
// Only lines the diff added can fail; the same finding elsewhere in the file is printed, not failed.
|
|
import { execFileSync } from "node:child_process";
|
|
import { readFileSync } from "node:fs";
|
|
import path from "node:path";
|
|
import ts from "typescript";
|
|
|
|
const SCANNABLE = new Set([".ts", ".tsx", ".mjs", ".js", ".css", ".py", ".md"]);
|
|
// The lookahead keeps `ts` from matching inside ".tsx" and `js` inside ".json".
|
|
const PATH_EXT = "(?:ts|tsx|js|jsx|mjs|cjs|css|scss|py|md|json|ya?ml|sql|sh|html|txt)(?![\\w])";
|
|
const PATHISH = new RegExp(`^[\\w@./-]*[\\w-]+\\.${PATH_EXT}$`);
|
|
const BARE_PATH_LINE = new RegExp(`\\b([\\w@./-]*[\\w-]+\\.${PATH_EXT}):(\\d+)\\b`, "g");
|
|
const TEST_CLAIM = new RegExp(
|
|
`\\b(pinned by|asserted in|covered by|see)\\s+\`?([\\w@./-]*[\\w-]+\\.${PATH_EXT})\`?`,
|
|
"gi",
|
|
);
|
|
|
|
// A name cited to say it is absent ("no `danger.js`") must not be resolved. Bound to one sentence,
|
|
// reaching back far enough to cover a wrapped three-item list.
|
|
const ASSERTS_ABSENCE =
|
|
/\b(no|not|never|without|neither|nor|nothing|absent|missing)\b[^.;]{0,120}$/i;
|
|
|
|
// A sentence that names another repo or an installed package cites a file this checkout cannot see.
|
|
const NAMES_ANOTHER_REPO = /\bheygen-com\/(?!hyperframes\b)[\w.-]+/i;
|
|
const NAMES_A_DEPENDENCY = /@[\w.-]+\/[\w.-]+|\b[\w.-]+@\d+\.\d+|\b[\w.-]+\/dist\//;
|
|
|
|
// The block rules read code only: a markdown plan describing what the code once did is a record.
|
|
const CODE_EXT = new Set([".ts", ".tsx", ".mjs", ".js", ".py"]);
|
|
const MAX_BLOCK_LINES = 40;
|
|
|
|
// Bounded forms only: bare "was" also matches live conditions ("if the response was truncated").
|
|
const HISTORY_NARRATION =
|
|
/\b(used to|previously|formerly|load[- ]bearing|before this (?:change|commit|PR|fix|refactor|rewrite|version|landed)|(?:before|after|in|per) #\d+|PRs? #\d+|was (?:removed|renamed|replaced|moved|split|deleted|introduced|reverted))\b/i;
|
|
|
|
// Quoted words are someone else's, not this code's history. The single-quote form needs word
|
|
// boundaries and one line, or an apostrophe in "isn't" opens a span that blanks real findings.
|
|
const QUOTED_SPAN = /"[^"\n]*"|(?<![A-Za-z0-9])'[^'\n]*'(?![A-Za-z0-9])/g;
|
|
|
|
// Exempts a block from the length rule, only with a stated reason.
|
|
const LENGTH_MARKER = /comment-length:\s*(\S.*?)\s*$/;
|
|
|
|
// Pragmas and TODOs parse as code and are not code (the ruff ERA001 false-positive classes).
|
|
const PRAGMA =
|
|
/^\s*(?:@ts-|eslint-|prettier-|biome-|oxlint-|istanbul |c8 |v8 |type:|noqa|pylint:|mypy:|TODO|FIXME|NOTE|XXX|HACK|ponytail:|comment-length:)/i;
|
|
|
|
// A bare identifier or string literal parses clean and means nothing; these bind or have an effect.
|
|
const REAL_STATEMENT = new Set([
|
|
ts.SyntaxKind.VariableStatement,
|
|
ts.SyntaxKind.FunctionDeclaration,
|
|
ts.SyntaxKind.ClassDeclaration,
|
|
ts.SyntaxKind.IfStatement,
|
|
ts.SyntaxKind.ForStatement,
|
|
ts.SyntaxKind.ForOfStatement,
|
|
ts.SyntaxKind.ForInStatement,
|
|
ts.SyntaxKind.WhileStatement,
|
|
ts.SyntaxKind.ReturnStatement,
|
|
ts.SyntaxKind.ImportDeclaration,
|
|
ts.SyntaxKind.ExportDeclaration,
|
|
ts.SyntaxKind.ExportAssignment,
|
|
ts.SyntaxKind.SwitchStatement,
|
|
ts.SyntaxKind.TryStatement,
|
|
ts.SyntaxKind.ThrowStatement,
|
|
ts.SyntaxKind.InterfaceDeclaration,
|
|
ts.SyntaxKind.TypeAliasDeclaration,
|
|
]);
|
|
|
|
// Platform names git grep cannot find in this repo. Listed, not read from node_modules, so the
|
|
// verdict does not depend on an install. Taken at react and react-dom 19.2.4.
|
|
const REACT_EXPORTS = [
|
|
"cacheSignal",
|
|
"captureOwnerStack",
|
|
"cloneElement",
|
|
"createContext",
|
|
"createElement",
|
|
"createRef",
|
|
"forwardRef",
|
|
"isValidElement",
|
|
"startTransition",
|
|
"useActionState",
|
|
"useCallback",
|
|
"useContext",
|
|
"useDebugValue",
|
|
"useDeferredValue",
|
|
"useEffect",
|
|
"useEffectEvent",
|
|
"useId",
|
|
"useImperativeHandle",
|
|
"useInsertionEffect",
|
|
"useLayoutEffect",
|
|
"useMemo",
|
|
"useOptimistic",
|
|
"useReducer",
|
|
"useRef",
|
|
"useState",
|
|
"useSyncExternalStore",
|
|
"useTransition",
|
|
];
|
|
const REACT_DOM_EXPORTS = [
|
|
"createPortal",
|
|
"createRoot",
|
|
"flushSync",
|
|
"hydrateRoot",
|
|
"prefetchDNS",
|
|
"preinitModule",
|
|
"preloadModule",
|
|
"requestFormReset",
|
|
"useFormState",
|
|
"useFormStatus",
|
|
];
|
|
|
|
// Grow this when the gate flags a real name that lives outside the repo.
|
|
const ALLOW = new Set([
|
|
"canParse",
|
|
"showModal",
|
|
"requestIdleCallback",
|
|
"webkitAnimationEnd",
|
|
"targetOrigin",
|
|
...REACT_EXPORTS,
|
|
...REACT_DOM_EXPORTS,
|
|
]);
|
|
|
|
// Files a skill workflow or a CLI command (`capture`, `transcribe`) writes into the user's project at run time; skill docs name them bare.
|
|
const PROJECT_FILES = new Set([
|
|
"frame.md",
|
|
"STORYBOARD.md",
|
|
"SCRIPT.md",
|
|
"audio_meta.json",
|
|
"BRIEF.md",
|
|
"boards/storyboard.html",
|
|
"design.md",
|
|
"_role.md",
|
|
"cinematic.json",
|
|
"safe-zones.json",
|
|
"BLOCKED.md",
|
|
"REVIEW.md",
|
|
"STORYLINES.md",
|
|
"assets/transcript.json",
|
|
"asset-descriptions.md",
|
|
"extracted/page.html",
|
|
"extracted/visible-text.txt",
|
|
"extracted/tokens.json",
|
|
"extracted/design-styles.json",
|
|
]);
|
|
|
|
// A URL, an absolute path, a `<sha>:<path>`, build output or a project file: none resolve in the tree.
|
|
function namesSomethingElse(cited) {
|
|
if (PROJECT_FILES.has(cited)) return true;
|
|
if (cited.startsWith("/") || cited.startsWith("~")) return true;
|
|
if (cited.includes(":")) return true;
|
|
return cited.split("/")[0] === "dist";
|
|
}
|
|
|
|
/** Of several files that could satisfy one citation, the one sharing the most leading path
|
|
* segments with the file doing the citing. */
|
|
function nearestTo(from, candidates) {
|
|
if (candidates.length === 0) return null;
|
|
const home = from.split("/");
|
|
const shared = (file) => {
|
|
const parts = file.split("/");
|
|
let i = 0;
|
|
while (i < home.length && i < parts.length && home[i] === parts[i]) i++;
|
|
return i;
|
|
};
|
|
return candidates.reduce((best, file) => (shared(file) > shared(best) ? file : best));
|
|
}
|
|
|
|
/** Blank out string literals so a `//` inside one is not read as a comment, and so a template
|
|
* literal's backticks are not read as a citation. Length is preserved to keep offsets honest. */
|
|
function stripStrings(line) {
|
|
return line.replace(/(["'`])(?:\\.|(?!\1)[^\\])*\1/g, (m) => " ".repeat(m.length));
|
|
}
|
|
|
|
// Index just past the literal opened at `start`, or -1 when it does not close on this line.
|
|
function literalEnd(line, start, regex) {
|
|
const close = regex ? "/" : line[start];
|
|
let inClass = false;
|
|
for (let i = start + 1; i < line.length; i++) {
|
|
const ch = line[i];
|
|
if (ch === "\\") i++;
|
|
else if (regex && ch === "[") inClass = true;
|
|
else if (regex && ch === "]") inClass = false;
|
|
else if (ch === close && !inClass) return i + 1;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
/** stripStrings for C-like code, plus regex literals, so `/["'`]/` opens no string or template.
|
|
* A `/` after a value, `<`, `++` or `--` is division or a tag (`a / b`, `</p>`, `i++ / 2`).
|
|
* Stops at the first comment. */
|
|
function stripLiterals(line) {
|
|
let out = "";
|
|
for (let i = 0; i < line.length; ) {
|
|
const ch = line[i];
|
|
const comment = ch === "/" && (line[i + 1] === "/" || line[i + 1] === "*");
|
|
if (comment) return out + line.slice(i);
|
|
// ponytail: `return /re/` reads as division; add a keyword check if that ever hides a comment.
|
|
const regex = ch === "/" && !/(?:[\w$)\]<]|\+\+|--)$/.test(out.trimEnd());
|
|
let end = regex || `"'\``.includes(ch) ? literalEnd(line, i, regex) : -1;
|
|
// A regex never closes on a comment opener: this `/` is division, e.g. continued from the line above.
|
|
if (regex && (line[end] === "/" || line[end] === "*")) end = -1;
|
|
if (end === -1) {
|
|
out += ch;
|
|
i++;
|
|
} else {
|
|
out += " ".repeat(end - i);
|
|
i = end;
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function scanCLike(lines) {
|
|
const out = [];
|
|
let inBlock = false;
|
|
let inTemplate = false;
|
|
for (let i = 0; i < lines.length; i++) {
|
|
let rest = lines[i];
|
|
let text = "";
|
|
while (rest.length > 0) {
|
|
if (inBlock) {
|
|
const end = rest.indexOf("*/");
|
|
if (end === -1) {
|
|
text += rest;
|
|
break;
|
|
}
|
|
text += rest.slice(0, end);
|
|
inBlock = false;
|
|
rest = rest.slice(end + 2);
|
|
continue;
|
|
}
|
|
if (inTemplate) {
|
|
const end = rest.indexOf("`");
|
|
if (end === -1) break;
|
|
inTemplate = false;
|
|
rest = rest.slice(end + 1);
|
|
continue;
|
|
}
|
|
// Complete literals are blanked, so a backtick left over opens a multi-line template.
|
|
const code = stripLiterals(rest);
|
|
const marks = [
|
|
[code.indexOf("//"), "line"],
|
|
[code.indexOf("/*"), "block"],
|
|
[code.indexOf("`"), "template"],
|
|
]
|
|
.filter(([at]) => at !== -1)
|
|
.sort((a, b) => a[0] - b[0]);
|
|
if (marks.length === 0) break;
|
|
const [at, kind] = marks[0];
|
|
if (kind === "template") {
|
|
inTemplate = true;
|
|
rest = rest.slice(at + 1);
|
|
continue;
|
|
}
|
|
if (kind === "block") {
|
|
inBlock = true;
|
|
rest = rest.slice(at + 2);
|
|
continue;
|
|
}
|
|
if (rest[at - 1] === ":") {
|
|
rest = rest.slice(at + 2); // a URL, not a comment
|
|
continue;
|
|
}
|
|
text += rest.slice(at + 2);
|
|
break;
|
|
}
|
|
const cleaned = text.replace(/^\s*\*+ ?/, "");
|
|
if (cleaned.trim()) out.push({ line: i + 1, text: cleaned });
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function scanPython(lines) {
|
|
const out = [];
|
|
let doc = null;
|
|
for (let i = 0; i < lines.length; i++) {
|
|
const raw = lines[i];
|
|
if (doc !== null) {
|
|
const end = raw.indexOf(doc);
|
|
out.push({ line: i + 1, text: end === -1 ? raw : raw.slice(0, end) });
|
|
if (end !== -1) doc = null;
|
|
continue;
|
|
}
|
|
const opener = raw.match(/("""|''')/);
|
|
if (opener) {
|
|
const rest = raw.slice(opener.index + 3);
|
|
const end = rest.indexOf(opener[1]);
|
|
out.push({ line: i + 1, text: end === -1 ? rest : rest.slice(0, end) });
|
|
if (end === -1) doc = opener[1];
|
|
continue;
|
|
}
|
|
const hash = stripStrings(raw).indexOf("#");
|
|
if (hash !== -1) out.push({ line: i + 1, text: raw.slice(hash + 1) });
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/** Markdown is prose all the way down, so every line outside a fence counts as a comment. */
|
|
function scanMarkdown(lines) {
|
|
const out = [];
|
|
let fence = null;
|
|
for (let i = 0; i < lines.length; i++) {
|
|
const raw = lines[i];
|
|
const marker = raw.match(/^\s*(```+|~~~+)/);
|
|
if (fence !== null) {
|
|
if (marker && raw.trim().startsWith(fence)) fence = null;
|
|
continue;
|
|
}
|
|
if (marker) {
|
|
fence = marker[1];
|
|
continue;
|
|
}
|
|
if (raw.trim()) out.push({ line: i + 1, text: raw });
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// A comment is a paragraph: the repo a name belongs to is often named a line or two above it.
|
|
function groupComments(comments) {
|
|
const blocks = [];
|
|
for (const comment of comments) {
|
|
const previous = blocks[blocks.length - 1];
|
|
if (previous && comment.line === previous[previous.length - 1].line + 1) previous.push(comment);
|
|
else blocks.push([comment]);
|
|
}
|
|
return blocks;
|
|
}
|
|
|
|
// Code blocks merge across blank continuation lines, so a symbol cited deep in a docblock
|
|
// excludes the whole docblock when its own occurrences are counted.
|
|
function withBlockContext(comments, lines, ext) {
|
|
const blocks = CODE_EXT.has(ext)
|
|
? mergeAcrossBlankLines(groupComments(comments), lines)
|
|
: groupComments(comments);
|
|
return blocks.flatMap((block) => {
|
|
const context = block.map((comment) => comment.text).join(" ");
|
|
const blockStart = block[0].line;
|
|
const blockEnd = block.spanEnd ?? block[block.length - 1].line;
|
|
// `prefix` is what the paragraph said before this line, so a negation governs a wrapped name.
|
|
return block.map((comment, index) => ({
|
|
...comment,
|
|
context,
|
|
prefix: block
|
|
.slice(0, index)
|
|
.map((earlier) => earlier.text)
|
|
.join(" "),
|
|
blockStart,
|
|
blockEnd,
|
|
}));
|
|
});
|
|
}
|
|
|
|
// A blank " *" line inside a docblock is still inside it; the text scan drops it, so length
|
|
// measures the physical span by stepping over these.
|
|
const BLANK_CONTINUATION = /^\s*(?:\/\/+|\*|\/\*\*?)?\s*$/;
|
|
|
|
function mergeAcrossBlankLines(blocks, lines) {
|
|
const merged = [];
|
|
for (const block of blocks) {
|
|
const previous = merged[merged.length - 1];
|
|
const gapStart = previous ? previous[previous.length - 1].line : 0;
|
|
const gap = previous ? lines.slice(gapStart, block[0].line - 1) : null;
|
|
if (previous && gap.length > 0 && gap.every((line) => BLANK_CONTINUATION.test(line))) {
|
|
previous.push(...block);
|
|
previous.spanEnd = block[block.length - 1].line;
|
|
} else {
|
|
const copy = [...block];
|
|
copy.spanEnd = block[block.length - 1].line;
|
|
merged.push(copy);
|
|
}
|
|
}
|
|
return merged;
|
|
}
|
|
|
|
const blocksOf = (lines, ext) => mergeAcrossBlankLines(groupComments(scanFor(lines, ext)), lines);
|
|
|
|
function scanFor(lines, ext) {
|
|
if (ext === ".md") return scanMarkdown(lines);
|
|
if (ext === ".py") return scanPython(lines);
|
|
return scanCLike(lines);
|
|
}
|
|
|
|
// Asks the TypeScript parser, not a word list: prose puts two identifiers side by side, which is
|
|
// a syntax error, so clean diagnostics plus one binding or effectful statement means code.
|
|
function looksLikeCommentedOutCode(block) {
|
|
const texts = block.map((comment) => comment.text.trim()).filter(Boolean);
|
|
if (texts.length === 0 || texts.some((text) => PRAGMA.test(text))) return false;
|
|
const source = texts.join("\n");
|
|
if (source.length < 6) return false;
|
|
// Parenthesised prose parses as a call; copied-out code keeps its formatter's terminator.
|
|
if (!/[;}]$/.test(source.trim())) return false;
|
|
const parsed = ts.createSourceFile(
|
|
"comment.tsx",
|
|
source,
|
|
ts.ScriptTarget.Latest,
|
|
false,
|
|
ts.ScriptKind.TSX,
|
|
);
|
|
if (parsed.parseDiagnostics.length > 0) return false;
|
|
return parsed.statements.some((node) => {
|
|
if (REAL_STATEMENT.has(node.kind)) return true;
|
|
if (!ts.isExpressionStatement(node)) return false;
|
|
const expression = node.expression;
|
|
return (
|
|
ts.isCallExpression(expression) ||
|
|
ts.isAwaitExpression(expression) ||
|
|
(ts.isBinaryExpression(expression) &&
|
|
expression.operatorToken.kind === ts.SyntaxKind.EqualsToken)
|
|
);
|
|
});
|
|
}
|
|
|
|
// Opener through closer line: a docblock's bare `/**` and `*/` carry no text but a reader
|
|
// still scrolls past them.
|
|
function physicalExtent(block, lines) {
|
|
let start = block[0].line;
|
|
let end = block.spanEnd ?? block[block.length - 1].line;
|
|
while (start > 1 && /^\s*\/\*+\s*$/.test(lines[start - 2])) start--;
|
|
while (end < lines.length && /^\s*\*+\/\s*$/.test(lines[end])) end++;
|
|
return { start, end };
|
|
}
|
|
|
|
// Every comment block in one source. `textLines` counts lines carrying prose, which a volume
|
|
// measure needs: a span grows when deleting code merges two blocks.
|
|
export function commentBlocks(source, ext) {
|
|
const lines = source.split("\n");
|
|
const blocks = blocksOf(lines, ext);
|
|
return blocks.map((block) => ({
|
|
...physicalExtent(block, lines),
|
|
textLines: block.length,
|
|
marked: LENGTH_MARKER.test(block[0].text.trim()),
|
|
}));
|
|
}
|
|
|
|
// Length, narration and commented-out code need no citation to be wrong. `touches(file, start,
|
|
// end)` limits them to blocks holding a line the diff added.
|
|
export function checkBlockRules(files, repo, touches = () => true) {
|
|
const failures = [];
|
|
for (const file of files) {
|
|
const ext = path.extname(file);
|
|
if (!CODE_EXT.has(ext) || !repo.has(file)) continue;
|
|
const lines = repo.source(file).split("\n");
|
|
for (const block of blocksOf(lines, ext)) {
|
|
const extent = physicalExtent(block, lines);
|
|
if (!touches(file, extent.start, extent.end)) continue;
|
|
const where = `${file}:${block[0].line}`;
|
|
const first = block[0].text.trim();
|
|
const joined = block.map((comment) => comment.text).join(" ");
|
|
|
|
const span = extent.end - extent.start + 1;
|
|
if (span > MAX_BLOCK_LINES && !LENGTH_MARKER.test(first)) {
|
|
failures.push({
|
|
where,
|
|
cite: `${span}-line comment block`,
|
|
why: `over ${MAX_BLOCK_LINES} lines. Cut it to the why and the invariant, or mark the first line "comment-length: <reason>" if it genuinely must stay (a licence, a diagram, a protocol table).`,
|
|
});
|
|
}
|
|
|
|
const history = joined.replace(QUOTED_SPAN, '""').match(HISTORY_NARRATION);
|
|
if (history) {
|
|
failures.push({
|
|
where,
|
|
cite: joined.trim().slice(0, 100),
|
|
why: `narrates history ("${history[1]}"). Delete the sentence; git owns what this used to be. If it also states a live invariant, keep only that clause.`,
|
|
});
|
|
}
|
|
|
|
if (looksLikeCommentedOutCode(block)) {
|
|
failures.push({
|
|
where,
|
|
cite: joined.trim().slice(0, 100),
|
|
why: "parses as code, so it is commented-out code. Delete it; git owns it.",
|
|
});
|
|
}
|
|
}
|
|
}
|
|
return failures;
|
|
}
|
|
|
|
// `#` opens a comment only in Python; in TypeScript it opens a private field.
|
|
const COMMENT_LINE = /^\s*(?:\/\/|\/\*|\*)/;
|
|
const HASH_COMMENT_LINE = /^\s*#/;
|
|
const isCommentLine = (line, ext) =>
|
|
COMMENT_LINE.test(line) || (ext === ".py" && HASH_COMMENT_LINE.test(line));
|
|
|
|
// A TODO names who owns it or the issue that tracks it. Only a marker opening a comment line
|
|
// counts; a sentence that mentions a TODO is prose.
|
|
const TODO_MARK = /^\s*(?:TODO|FIXME|XXX)\s*[:(]/;
|
|
// An owner is a person or an area (`jrs`, `player-perf`, `core follow-up`), or an issue.
|
|
const OWNER = "(?:@?[A-Za-z][\\w .-]*|#\\d+)";
|
|
const TODO_OWNED = new RegExp(`\\b(?:TODO|FIXME|XXX)\\s*\\(${OWNER}(?:,\\s*${OWNER})*\\)`);
|
|
// Owner or issue go in the parentheses, `TODO(name):` or `TODO(#1234):`: a bare `#123456` could be a
|
|
// colour. Otherwise the TODO's own line links the issue.
|
|
const COPIED = /\b(?:copied|adapted|ported|borrowed) from\b/i;
|
|
const URL_IN_TEXT = /\bhttps?:\/\/[^\s<>"'`)\]]*/g;
|
|
const TRAILING_PUNCTUATION = /[.,;:!?]+$/;
|
|
// The host is parsed, not matched: `evil.example/github.com/...` is no issue link.
|
|
const ISSUE_PATH = /^\/[\w.-]+\/[\w.-]+\/issues\/\d+\/?$/;
|
|
const linksIssue = (text) =>
|
|
[...text.matchAll(URL_IN_TEXT)].some(([raw]) => {
|
|
const url = URL.parse(raw.replace(TRAILING_PUNCTUATION, ""));
|
|
return url?.hostname === "github.com" && ISSUE_PATH.test(url.pathname);
|
|
});
|
|
// Loopback is left alone: comments describe dev servers and CORS origins by it, not as links.
|
|
const LOOPBACK = /^https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?:[:/]|$)/i;
|
|
const PRIVATE_HOST =
|
|
/^(?:10(?:\.\d+){3}|192\.168(?:\.\d+){2}|172\.(?:1[6-9]|2\d|3[01])(?:\.\d+){2}|169\.254(?:\.\d+){2})$|\.(?:local|internal|lan|corp|intranet)$/;
|
|
const SIGNED_QUERY =
|
|
/[?&](?:X-Amz-Signature|X-Amz-Credential|X-Goog-Signature|Signature|sig|token|access_token|Key-Pair-Id)=/i;
|
|
// prettier-ignore
|
|
const STOP_WORDS = new Set([
|
|
"a", "an", "the", "this", "that", "these", "those", "to", "of", "for", "in", "on", "at", "by", "with",
|
|
"and", "or", "is", "are", "be", "it", "its", "we", "our", "if", "then", "else", "as", "from",
|
|
]);
|
|
const words = (text) =>
|
|
text
|
|
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
.toLowerCase()
|
|
.match(/[a-z][a-z0-9]*/g) ?? [];
|
|
|
|
// Why a URL in a comment cannot be followed by a reader, or null when it can. Placeholders
|
|
// (`https://${host}`, `https://<host>`) are templates, not links.
|
|
function urlProblem(raw) {
|
|
const cited = raw.replace(TRAILING_PUNCTUATION, "");
|
|
if (/^https?:\/\/$/.test(cited) || /[{}$*…]/.test(cited) || LOOPBACK.test(cited)) return null;
|
|
let url;
|
|
try {
|
|
url = new URL(cited);
|
|
} catch {
|
|
return "is not a well-formed URL";
|
|
}
|
|
if (PRIVATE_HOST.test(url.hostname.toLowerCase()))
|
|
return `points at a private host (${url.hostname})`;
|
|
if (SIGNED_QUERY.test(url.search)) return "carries a signature or token in its query";
|
|
return null;
|
|
}
|
|
|
|
const RESTATE_SHARE = 0.8;
|
|
const DIVIDER = /[─━═]{2,}|-{4,}|={4,}/;
|
|
|
|
// A standalone comment of one or two lines whose words are almost all names on the next code line.
|
|
// Section dividers and JSDoc are left alone: they label and document, they do not narrate.
|
|
function restatesCode(block, lines, extent, ext) {
|
|
if (block.length > 2 || !isCommentLine(lines[block[0].line - 1], ext)) return false;
|
|
if (/^\s*\/\*\*/.test(lines[extent.start - 1]) || block.some(({ text }) => DIVIDER.test(text))) {
|
|
return false;
|
|
}
|
|
const next = lines.slice(extent.end).find((line) => line.trim());
|
|
if (!next || isCommentLine(next, ext) || /^\s*}/.test(next)) return false;
|
|
const said = words(block.map((comment) => comment.text).join(" ")).filter(
|
|
(w) => !STOP_WORDS.has(w),
|
|
);
|
|
if (said.length < 2) return false;
|
|
const code = new Set(words(stripStrings(next)));
|
|
return said.filter((w) => code.has(w)).length / said.length >= RESTATE_SHARE;
|
|
}
|
|
|
|
// Findings for the checkable best-practice rules, each with the line range it rests on. Pure, so
|
|
// the same function grades a PR and measures the whole tree.
|
|
export function practiceFindings(source, ext) {
|
|
if (!CODE_EXT.has(ext)) return [];
|
|
const lines = source.split("\n");
|
|
const found = [];
|
|
for (const block of blocksOf(lines, ext)) {
|
|
const extent = physicalExtent(block, lines);
|
|
const joined = block.map((comment) => comment.text).join(" ");
|
|
const linked = /\bhttps?:\/\//.test(joined);
|
|
for (const { line, text } of block) {
|
|
const at = { from: line, to: line, cite: text.trim().slice(0, 100) };
|
|
if (TODO_MARK.test(text) && !TODO_OWNED.test(text) && !linksIssue(text)) {
|
|
found.push({
|
|
...at,
|
|
rule: "todo",
|
|
why: 'a TODO names its owner, area or issue, "TODO(name):" or "TODO(#1234):", or links the issue.',
|
|
});
|
|
}
|
|
if (COPIED.test(text) && !linked) {
|
|
found.push({
|
|
...at,
|
|
rule: "source",
|
|
warn: true,
|
|
why: "copied or adapted code links the original source.",
|
|
});
|
|
}
|
|
for (const [raw] of text.matchAll(URL_IN_TEXT)) {
|
|
const problem = urlProblem(raw);
|
|
if (problem)
|
|
found.push({
|
|
...at,
|
|
rule: "url",
|
|
why: `${raw} ${problem}; link what every reader can open.`,
|
|
});
|
|
}
|
|
}
|
|
if (restatesCode(block, lines, extent, ext)) {
|
|
found.push({
|
|
from: extent.start,
|
|
to: extent.end,
|
|
cite: joined.trim().slice(0, 100),
|
|
rule: "restates",
|
|
warn: true,
|
|
why: "restates the next line of code. Say why, or delete it and let the names speak.",
|
|
});
|
|
}
|
|
}
|
|
return found;
|
|
}
|
|
|
|
function extractComments(source, ext) {
|
|
const lines = source.split("\n");
|
|
return withBlockContext(scanFor(lines, ext), lines, ext);
|
|
}
|
|
|
|
// camelCase only: a case shift separates a name from an English word, and PascalCase or
|
|
// snake_case names are mostly types and fields from outside the repo.
|
|
function looksLikeSymbol(token) {
|
|
if (!/^[a-z][A-Za-z0-9$]*$/.test(token)) return false;
|
|
if (token.length < 4 || ALLOW.has(token)) return false;
|
|
return /[a-z][A-Z]/.test(token);
|
|
}
|
|
|
|
// Everything one comment line claims about the repo. `context` is the whole paragraph; a negation
|
|
// is read only from `prefix`, what the paragraph said before the citation.
|
|
function findCitations(text, context = text, prefix = "") {
|
|
const clean = text.replace(/\b[a-z][\w+.-]*:\/\/\S+/gi, " ");
|
|
if (NAMES_ANOTHER_REPO.test(context) || NAMES_A_DEPENDENCY.test(context)) {
|
|
return { paths: [], symbols: [], claims: [] };
|
|
}
|
|
const paths = [];
|
|
const symbols = [];
|
|
let outsideTicks = clean;
|
|
for (const match of clean.matchAll(/`([^`\n]+)`/g)) {
|
|
const span = match[1].trim();
|
|
const before = prefix + clean.slice(0, match.index);
|
|
outsideTicks = outsideTicks.replace(match[0], " ".repeat(match[0].length));
|
|
if (ASSERTS_ABSENCE.test(before)) continue;
|
|
const withLine = span.match(new RegExp(`^(.+\\.${PATH_EXT}):(\\d+)$`));
|
|
if (withLine) {
|
|
paths.push({ path: withLine[1], line: Number(withLine[2]), quoted: span });
|
|
continue;
|
|
}
|
|
if (PATHISH.test(span)) {
|
|
paths.push({ path: span, line: null, quoted: span });
|
|
continue;
|
|
}
|
|
if (looksLikeSymbol(span)) symbols.push(span);
|
|
}
|
|
for (const match of outsideTicks.matchAll(BARE_PATH_LINE)) {
|
|
if (ASSERTS_ABSENCE.test(prefix + clean.slice(0, match.index))) continue;
|
|
paths.push({ path: match[1], line: Number(match[2]), quoted: match[0] });
|
|
}
|
|
const claims = [...clean.matchAll(TEST_CLAIM)].map((m) => ({ phrase: m[1], path: m[2] }));
|
|
return { paths, symbols, claims };
|
|
}
|
|
|
|
/** The repo as the checker needs to see it: which paths exist, how long they are, and where a
|
|
* word occurs. Backed by git, so it sees what is tracked and nothing else. */
|
|
export function openRepo(root) {
|
|
const tracked = execFileSync("git", ["ls-files"], {
|
|
cwd: root,
|
|
encoding: "utf8",
|
|
maxBuffer: 256e6,
|
|
})
|
|
.split("\n")
|
|
.filter(Boolean);
|
|
const files = new Set(tracked);
|
|
const byBasename = new Map();
|
|
for (const file of tracked) {
|
|
const base = path.basename(file);
|
|
if (!byBasename.has(base)) byBasename.set(base, []);
|
|
byBasename.get(base).push(file);
|
|
}
|
|
const contents = new Map();
|
|
|
|
const read = (file) => {
|
|
if (!contents.has(file)) contents.set(file, readFileSync(path.join(root, file), "utf8"));
|
|
return contents.get(file);
|
|
};
|
|
|
|
return {
|
|
has: (file) => files.has(file),
|
|
source: read,
|
|
// Tried from the repo root, the citing file, its package, then as a path suffix, because
|
|
// people abbreviate a long path to the part that identifies it.
|
|
resolve(cited, from) {
|
|
const candidates = [
|
|
path.normalize(cited),
|
|
path.normalize(path.join(path.dirname(from), cited)),
|
|
];
|
|
for (let dir = path.dirname(from); dir !== "." && dir !== "/"; dir = path.dirname(dir)) {
|
|
if (files.has(path.join(dir, "package.json"))) {
|
|
candidates.push(path.normalize(path.join(dir, cited)));
|
|
break;
|
|
}
|
|
}
|
|
for (const candidate of candidates) if (files.has(candidate)) return candidate;
|
|
const suffix = `/${path.normalize(cited)}`;
|
|
const loose = cited.includes("/")
|
|
? tracked.filter((file) => file.endsWith(suffix))
|
|
: (byBasename.get(cited) ?? []);
|
|
// Several packages share a basename, so the citing file's own package wins.
|
|
return nearestTo(from, loose);
|
|
},
|
|
// A `packages/<name>/...` path whose package this checkout does not contain.
|
|
namesAbsentPackage(cited) {
|
|
const parts = path.normalize(cited).split("/");
|
|
if (parts[0] !== "packages" || parts.length < 3) return false;
|
|
return !tracked.some((file) => file.startsWith(`packages/${parts[1]}/`));
|
|
},
|
|
lineCount(file) {
|
|
return read(file).split("\n").length;
|
|
},
|
|
mentions(file, token) {
|
|
return read(file).includes(token);
|
|
},
|
|
// Where each token occurs as a whole word, as "path:line". One git grep per batch of 400,
|
|
// since a spawn per token is minutes on a large diff.
|
|
occurrencesOf(tokens) {
|
|
const found = new Map(tokens.map((token) => [token, []]));
|
|
for (let i = 0; i < tokens.length; i += 400) {
|
|
const batch = tokens.slice(i, i + 400);
|
|
let output = "";
|
|
try {
|
|
output = execFileSync("git", ["grep", "-nwF", ...batch.flatMap((t) => ["-e", t])], {
|
|
cwd: root,
|
|
encoding: "utf8",
|
|
maxBuffer: 512e6,
|
|
});
|
|
} catch (error) {
|
|
// git grep exits 1 for "no matches at all". Any other status is a broken repo.
|
|
if (error.status !== 1) throw error;
|
|
}
|
|
for (const hit of output.split("\n")) {
|
|
if (!hit) continue;
|
|
const at = hit.split(":").slice(0, 2).join(":");
|
|
const body = hit.slice(at.length + 1);
|
|
for (const token of batch) if (body.includes(token)) found.get(token).push(at);
|
|
}
|
|
}
|
|
return found;
|
|
},
|
|
};
|
|
}
|
|
|
|
/** Every claim the comments in these files make about the rest of the repo, before any of it is
|
|
* resolved. Kept separate from resolution so the resolver can batch its one git grep. */
|
|
function collectCitations(files, repo, inScope) {
|
|
const cited = [];
|
|
// The one place that decides whether a named file was opened; the OK line reports this count.
|
|
let examined = 0;
|
|
for (const file of files) {
|
|
if (!SCANNABLE.has(path.extname(file))) continue;
|
|
if (!repo.has(file)) continue; // deleted by this diff, or untracked
|
|
examined++;
|
|
for (const comment of extractComments(repo.source(file), path.extname(file))) {
|
|
const where = `${file}:${comment.line}`;
|
|
const touched = inScope(file, comment.line);
|
|
const { paths, symbols, claims } = findCitations(
|
|
comment.text,
|
|
comment.context,
|
|
comment.prefix,
|
|
);
|
|
for (const cite of paths) cited.push({ kind: "path", where, file, touched, ...cite });
|
|
for (const symbol of symbols) {
|
|
cited.push({
|
|
kind: "symbol",
|
|
where,
|
|
file,
|
|
touched,
|
|
symbol,
|
|
blockStart: comment.blockStart,
|
|
blockEnd: comment.blockEnd,
|
|
});
|
|
}
|
|
for (const claim of claims)
|
|
cited.push({ kind: "claim", where, file, touched, ...claim, symbols });
|
|
}
|
|
}
|
|
return { cited, examined };
|
|
}
|
|
|
|
// `scope` is `{ kind: "all" }` for a sweep, or `{ kind: "lines", added, removed }` for a diff,
|
|
// where `removed` holds the names the diff's deleted code used. Only touched lines fail.
|
|
export function checkComments(root, files, scope = { kind: "all" }) {
|
|
const repo = openRepo(root);
|
|
const inScope = (file, line) =>
|
|
scope.kind === "all" || (scope.added.get(file)?.has(line) ?? false);
|
|
const touches = (file, start, end) => {
|
|
const edited = scope.edited?.get(file);
|
|
for (let line = start; line <= end; line++) {
|
|
if (inScope(file, line) || edited?.has(line)) return true;
|
|
}
|
|
return false;
|
|
};
|
|
const { cited, examined } = collectCitations(files, repo, inScope);
|
|
const warnings = [];
|
|
const blockFailures = checkBlockRules(files, repo, touches);
|
|
// Line rules grade the lines the diff added; the restating rule grades the block it rests on.
|
|
for (const file of files.filter((name) => repo.has(name))) {
|
|
for (const finding of practiceFindings(repo.source(file), path.extname(file))) {
|
|
const graded =
|
|
finding.rule === "restates"
|
|
? touches(file, finding.from, finding.to)
|
|
: inScope(file, finding.from);
|
|
if (!graded) continue;
|
|
const entry = { where: `${file}:${finding.from}`, cite: finding.cite, why: finding.why };
|
|
(finding.warn ? warnings : blockFailures).push(entry);
|
|
}
|
|
}
|
|
const symbols = [...new Set(cited.filter((c) => c.kind === "symbol").map((c) => c.symbol))];
|
|
const occurrences = repo.occurrencesOf(symbols);
|
|
const missingPaths = new Set();
|
|
const failures = [];
|
|
const report = (cite, why) =>
|
|
(cite.touched ? failures : warnings).push({
|
|
where: cite.where,
|
|
cite: cite.quoted ?? cite.display,
|
|
why,
|
|
});
|
|
|
|
for (const cite of cited) {
|
|
if (cite.kind === "path") {
|
|
if (namesSomethingElse(cite.path) || repo.namesAbsentPackage(cite.path)) continue;
|
|
const resolved = repo.resolve(cite.path, cite.file);
|
|
if (resolved === null) {
|
|
missingPaths.add(`${cite.where} ${cite.path}`);
|
|
report(cite, "no such file in the repo");
|
|
continue;
|
|
}
|
|
const length = repo.lineCount(resolved);
|
|
if (cite.line !== null && cite.line > length)
|
|
report(cite, `${resolved} has only ${length} lines`);
|
|
continue;
|
|
}
|
|
|
|
if (cite.kind === "symbol") {
|
|
// A hit inside the citing block proves nothing, so the whole block is excluded, not only
|
|
// the citing line: a renamed symbol can linger two lines up in the same docblock.
|
|
const elsewhere = occurrences.get(cite.symbol).filter((hit) => {
|
|
const match = hit.match(/^(.*):(\d+)$/);
|
|
if (!match) return true;
|
|
const [, hitFile, hitLine] = match;
|
|
return !(
|
|
hitFile === cite.file &&
|
|
Number(hitLine) >= cite.blockStart &&
|
|
Number(hitLine) <= cite.blockEnd
|
|
);
|
|
});
|
|
// A comment explaining a deletion may name what the diff's removed code used. Removed
|
|
// code, not removed lines: an edited comment would otherwise waive its own stale name.
|
|
if (elsewhere.length === 0 && !scope.removed?.has(cite.symbol)) {
|
|
report(
|
|
{ ...cite, display: `\`${cite.symbol}\`` },
|
|
"resolves nowhere in the repo (add it to ALLOW in this script if it names something outside it)",
|
|
);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (namesSomethingElse(cite.path) || repo.namesAbsentPackage(cite.path)) continue;
|
|
const shown = { ...cite, display: `${cite.phrase} ${cite.path}` };
|
|
const resolved = repo.resolve(cite.path, cite.file);
|
|
if (resolved === null) {
|
|
// The path pass owns "no such file"; report it here only if it did not already see this one.
|
|
if (!missingPaths.has(`${cite.where} ${cite.path}`))
|
|
report(shown, "that file does not exist");
|
|
continue;
|
|
}
|
|
if (
|
|
cite.symbols.length > 0 &&
|
|
!cite.symbols.some((symbol) => repo.mentions(resolved, symbol))
|
|
) {
|
|
report(
|
|
shown,
|
|
`${resolved} never mentions ${cite.symbols.map((sym) => `\`${sym}\``).join(" or ")}`,
|
|
);
|
|
}
|
|
}
|
|
return { failures: [...failures, ...blockFailures], warnings, examined };
|
|
}
|
|
|
|
function mergeBase(root, ref) {
|
|
return execFileSync("git", ["merge-base", ref, "HEAD"], { cwd: root, encoding: "utf8" }).trim();
|
|
}
|
|
|
|
function changedFiles(root, base) {
|
|
return execFileSync("git", ["diff", "--name-only", `${base}...HEAD`], {
|
|
cwd: root,
|
|
encoding: "utf8",
|
|
maxBuffer: 64e6,
|
|
})
|
|
.split("\n")
|
|
.filter(Boolean);
|
|
}
|
|
|
|
// The code on one removed line, comment stripped. It errs toward dropping names: a lost name
|
|
// fails a citation loudly, a kept one would waive it silently.
|
|
function codeOnly(line) {
|
|
const closed = line.replace(/\/\*[\s\S]*?\*\//g, " ");
|
|
const opensBlock = closed.indexOf("/*");
|
|
const body = opensBlock === -1 ? closed : closed.slice(0, opensBlock);
|
|
if (/^\s*\*/.test(body)) return "";
|
|
const opensLine = body.indexOf("//");
|
|
return opensLine === -1 ? body : body.slice(0, opensLine);
|
|
}
|
|
|
|
const IDENTIFIER = /[A-Za-z_$][\w$]*/g;
|
|
|
|
// One `git diff` answers three things: the lines each file gained, the lines a deleted comment line
|
|
// sat between (so the block it left still counts as edited), and the names removed code used.
|
|
// No pathspec: limiting it to the new paths stops git pairing a rename with its old path.
|
|
function diffScope(root, base) {
|
|
const added = new Map();
|
|
const edited = new Map();
|
|
const removed = new Set();
|
|
const diff = execFileSync("git", ["diff", "--unified=0", "-M", `${base}...HEAD`], {
|
|
cwd: root,
|
|
encoding: "utf8",
|
|
maxBuffer: 256e6,
|
|
});
|
|
let lines = null;
|
|
let near = null;
|
|
let gapAt = null;
|
|
let hashComments = false;
|
|
for (const line of diff.split("\n")) {
|
|
if (line.startsWith("+++ ")) {
|
|
lines = near = null;
|
|
if (line.startsWith("+++ b/")) {
|
|
hashComments = line.endsWith(".py");
|
|
added.set(line.slice(6), (lines = new Set()));
|
|
edited.set(line.slice(6), (near = new Set()));
|
|
}
|
|
continue;
|
|
}
|
|
const hunk = line.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))?/);
|
|
if (hunk) {
|
|
const start = Number(hunk[1]);
|
|
const count = Number(hunk[2] ?? 1);
|
|
for (let i = 0; i < count; i++) lines?.add(start + i);
|
|
gapAt = count === 0 ? start : null;
|
|
continue;
|
|
}
|
|
// Skips the "--- a/path" header, and with it a removed line whose code starts with a dash.
|
|
if (line.startsWith("-") && !line.startsWith("---")) {
|
|
const body = line.slice(1);
|
|
const isComment = COMMENT_LINE.test(body) || (hashComments && HASH_COMMENT_LINE.test(body));
|
|
if (near && gapAt !== null && isComment) near.add(gapAt).add(gapAt + 1);
|
|
for (const name of codeOnly(body).match(IDENTIFIER) ?? []) removed.add(name);
|
|
}
|
|
}
|
|
return { added, edited, removed };
|
|
}
|
|
|
|
// A file whose citations are dead by construction. Every run grades it first, resolved against
|
|
// this script's repo, so a checker that stopped resolving cannot print OK.
|
|
export const PROBE = "scripts/gate-probes/broken-citation.md";
|
|
const PROBE_ROOT = path.resolve(import.meta.dirname, "..");
|
|
|
|
// Path and symbol citations are settled by separate resolvers; each must report on the probe.
|
|
const PROBE_MUST_REPORT = [
|
|
["a path that does not resolve", /no such file|does not exist/],
|
|
["a symbol that resolves nowhere", /resolves nowhere/],
|
|
];
|
|
|
|
function proveGateCanFail() {
|
|
let failures = [];
|
|
try {
|
|
failures = checkComments(PROBE_ROOT, [PROBE], { kind: "all" }).failures;
|
|
} catch (error) {
|
|
console.error(`comments: the self-check could not run: ${error.message}`);
|
|
process.exit(2);
|
|
}
|
|
const silent = PROBE_MUST_REPORT.filter(
|
|
([, saying]) => !failures.some((f) => saying.test(f.why)),
|
|
);
|
|
if (silent.length === 0) return;
|
|
console.error(`comments: SELF-CHECK FAILED. ${PROBE} carries a citation of each kind below and`);
|
|
console.error(` this checker did not report ${silent.length} of them, so a clean verdict on`);
|
|
console.error(` anything else would mean nothing. Silent:`);
|
|
for (const [kind] of silent) console.error(` ${kind}`);
|
|
console.error(` Repair the checker, or the probe file if it was edited.`);
|
|
process.exit(2);
|
|
}
|
|
|
|
if (process.argv[1] && import.meta.url === `file://${process.argv[1]}`) {
|
|
const root = process.cwd();
|
|
proveGateCanFail();
|
|
const explicit = process.argv.slice(2);
|
|
// Named files are a sweep of every line and need no git ref; no arguments grades the diff
|
|
// against the fork point with the base branch.
|
|
const base =
|
|
explicit.length > 0 ? null : mergeBase(root, process.env.COMMENT_CHECK_BASE ?? "origin/main");
|
|
// The probe is graded by the self-check only, or every PR that edits it would go red.
|
|
const files =
|
|
base === null ? explicit : changedFiles(root, base).filter((file) => file !== PROBE);
|
|
const scope = base === null ? { kind: "all" } : { kind: "lines", ...diffScope(root, base) };
|
|
const { failures, warnings, examined } = checkComments(root, files, scope);
|
|
|
|
// A run that opened no file must not print OK. A sweep that examined nothing is a mistaken
|
|
// invocation (a git ref passed as a path); a diff with no scannable file is ordinary.
|
|
if (examined === 0) {
|
|
if (explicit.length > 0) {
|
|
console.error(`comments: examined 0 of the ${explicit.length} argument(s) given.`);
|
|
console.error(` This script takes FILE PATHS, not a git ref, and only these extensions:`);
|
|
console.error(` ${[...SCANNABLE].join(" ")}. Run it with no arguments to grade the diff.`);
|
|
process.exit(2);
|
|
}
|
|
console.log(`comments: nothing to check (${files.length} changed file(s), none scannable)`);
|
|
process.exit(0);
|
|
}
|
|
|
|
for (const warning of warnings) {
|
|
console.log(`comments: ${warning.where} ${warning.cite}\n ${warning.why}`);
|
|
}
|
|
if (warnings.length > 0) {
|
|
console.log(
|
|
`\n ${warnings.length} warning(s). They do not fail the build; fixing one while you are in`,
|
|
);
|
|
console.log(` the file is still free.\n`);
|
|
}
|
|
if (failures.length === 0) {
|
|
console.log(`comments: OK (${examined} file(s) examined of ${files.length} in scope)`);
|
|
process.exit(0);
|
|
}
|
|
console.error(
|
|
`comments FAILED. ${failures.length} problem(s) in the comments of the files this diff changed.\n`,
|
|
);
|
|
for (const failure of failures) {
|
|
console.error(` - ${failure.where} ${failure.cite}`);
|
|
console.error(` ${failure.why}`);
|
|
}
|
|
console.error(
|
|
`\n Fix the citation or delete the comment. A comment nobody can trust is worse than`,
|
|
);
|
|
console.error(` no comment: it reads as evidence and sends the next reader to a dead end.`);
|
|
process.exit(1);
|
|
}
|