How the engine works
The rule engine lives in packages/gate/src/engine.mjs. It has two entry points:
analyzeContent(relPath, content, config)judges one file’s text and returns its violations.checkPaths(targets, cwd)walks files and folders for thesmugan checkcommand and callsanalyzeContenton each file.
The hooks call analyzeContent directly, on the text a file would have after an edit and on the text it had before. The engine reads no git history and has no idea what a turn is. The hook decides what counts as new (see Introduced vs. pre-existing).
Analyzing one file
Section titled “Analyzing one file”analyzeContent does the following, in order:
-
Context artifacts are skipped. A path that
isContextArtifactrecognizes returns no violations. -
The text is normalized. It removes a leading BOM and turns
\r\ninto\n. Without this step, the same violation read from an autocrlf checkout and from a git-HEAD baseline would carry two different messages, and the before/after comparison would stop cancelling old debt. -
Every applicable check runs. The check list is
CHECKSinchecks/index.mjs, 18 rules. A check runs only when:- its
appliesTo(path)accepts the path (each check filters by extension and folder), and checkEnabled(config, name, path)inconfig.mjsallows it.checks.<rule>: falseturns a rule off. The seven Supabase rules (rls,rls-tests,security-notes,anon-grants,view-invoker,search-path,rls-policy) default to on only where the file belongs to a Supabase project.ui-stringsdefaults to on only whenSMUGAN_UI_LANGis apttag.
Each check gets
{ path, content, lines, config, options }, whereoptionsis the object underchecks.<rule>in the config. It returns{ line, message }findings. The engine addsfile,ruleandseverity: 'error'to each one. - its
-
Suppressions are applied.
parseSuppressionsreadssmugan-ignorecomments from the normalized lines (rules below). A suppression whose reason is too short creates its ownsmugan-ignoreviolation, and that violation cannot be suppressed.
await-in-loop is not in CHECKS. The pre hook calls it separately, only in a critical turn. The end-of-turn detectors (contract drift, clones, residue, assertion weakening) live in their own modules and call parseSuppressions themselves where they honor it. See End-of-turn detectors.
Blanking comments and strings
Section titled “Blanking comments and strings”Most checks match regular expressions. To keep those from matching inside comments or string contents, the checks first run the text through strip.mjs:
stripCode(JS/TS) replaces comments, string contents, regex bodies and template-literal text with spaces. It keeps the length and every line break, so an offset or line number in the stripped copy is the same in the original. Code inside${…}is kept and scanned the same way. Other behaviors:- The
/that opens a regex is told apart from division by the previous significant character, plus a list of keywords (return,typeof,case, …). After a postfix operator (i++ / 2,price! / 100) or a JSX closing tag,/is never a regex. - An apostrophe glued to a word (JSX text such as
You're) does not open a string, unless the word is a keyword such asfromorimport.
- The
stripSqlblanks--comments,/* … */comments and'…'string contents (with''escapes). It keeps dollar-quoted bodies, because those hold DDL that the SQL rules need to read.
Because the copy keeps offsets, a check can find a construct in the stripped text and then read the original at the same position. stale-mutation reads an .rpc('name') this way, rls-policy reads user_metadata inside a string, and test-weakening compares the two sides of an expect(…).
Two checks read the raw text on purpose:
secretsscans every raw line, comments included, so a key left in a comment is still flagged.- The
smugan-ignoreparser matches raw lines, which is why a suppression works in//,--,/* */and<!-- -->comments alike.
Shared SQL helpers are in sql-util.mjs:
| Helper | What it does |
|---|---|
QUALIFIED_NAME |
Accepts bare or double-quoted parts, so "public"."notes" from supabase db diff reads as table notes |
normalizeIdent |
Defaults the schema to public |
PLATFORM_SCHEMAS |
auth, storage, extensions, realtime, vault, pg_catalog |
userTables |
The non-temporary tables a file creates outside those schemas |
isMigrationPath |
True for a .sql file under a migration/ or migrations/ folder |
statementHeader |
A statement’s text up to its ;, without dollar-quoted bodies |
Suppressions
Section titled “Suppressions”The syntax, from SUPPRESS_RE and parseSuppressions:
smugan-ignore <rule>[,<rule>…]: <reason>smugan-ignore-file <rule>[,<rule>…]: <reason>- Rule list. Lowercase letters, digits,
-,*and commas, with no spaces.*matches every rule. If the list does not match that shape, or the colon is missing, the comment is not read as a suppression at all. - Reason. It is cut at a trailing
*/or-->and trimmed. It must be at least 6 characters. A shorter one suppresses nothing and is reported as asmugan-ignoreviolation:suppression without a real reason — use "smugan-ignore <rule>: <reason with 6+ chars>". - Scope.
smugan-ignorecovers its own line and the line after it. A blank line in between breaks the link.smugan-ignore-filecovers the whole file, wherever it stands. - One per line. Only the first suppression on a line is read.
- Old name.
smaug-ignore, the project’s former name, is accepted the same way.
Suppressions apply only to rule findings. A typecheck, test or ESLint failure is not a rule line, and the hook does not suggest smugan-ignore for one.
Walking a tree (smugan check)
Section titled “Walking a tree (smugan check)”checkPaths visits each target. A folder is walked recursively.
- Skipped folders. It never descends into
node_modules,.git,.next,dist,build,out,coverage,.vercel,.turbo,.expo,.claude,.venv,venv,site-packages,__pycache__or.terraform. - Ignored paths. The config’s
ignoreglobs and the fixedDEFAULT_IGNORElist also apply. That list includes lockfiles,*.min.*,*.gen.*,database.types.ts,.env,.env.local,.env.*.local, and avendorfolder that a package manager filled. - Which files are read. A file is read only when at least one check’s
appliesToaccepts it and it is at most 1 MiB (MAX_FILE_BYTES). - Config per file. Each file is judged with the config found by walking up from its own folder. That config’s folder is the root that the rule paths and ignore globs are relative to. A folder with no config,
.gitorpackage.jsonof its own inherits its parent’s config.
format.mjs prints the result, sorted by file, line and rule. There are two formats:
- Human text:
file:line [rule] message, plus a footer that tells you how to suppress. --json:{ ok, filesChecked, violations }.
smugan check exits with 1 when it found violations, 0 with --warn or a clean run, and 2 on a usage or internal error (bin/smugan.mjs). It lists every violation in the files it reads and does not compare against any baseline.
Introduced vs. pre-existing violations
Section titled “Introduced vs. pre-existing violations”The hooks block only what a change adds. The comparison is in hook.mjs:
- Identity.
countViolationsturns a list of violations into a count perrule|message. The line number is not part of the key, so a violation that only moved is not new. - Comparison.
freshAgainst(baseline, violations)spends each baseline count against the current violations. Whatever is left over is new: a second identical violation counts as new even when one was already there.
Before a write (pre).
- The hook builds the text the file would have after the edit. A
Writeprovides the whole content.EditandMultiEditreplaceold_stringwithnew_stringin the current file. - It analyzes the current file and the result, and blocks with exit code
2on the violations that are new. - If an
old_stringdoes not match, or the file is over 1 MiB, there is nothing to judge and the write passes. - A new file has no “before”, so every violation in it counts.
dead-importsis not judged inEditorMultiEdit, only inWrite. A change made in several steps legitimately passes through an unused import.
First touch of the turn. The first time a turn touches a file, recordBaseline stores the before counts in the turn state. For a code file it also stores:
- the names of the functions the file already has,
- the contract-drift shapes it already has,
- for a test file, its assertion profile and test counts.
The first touch wins: later edits in the same turn do not move the baseline.
End of turn (stop). The hook analyzes every touched file again and compares against one baseline, chosen in this order:
- The first-touch baseline, when there is one.
- For a file a shell command changed, its version from before the turn’s first commit to it.
- Otherwise, the committed version at git HEAD (
freshVsHead). HEAD baselines are cached per blob sha. - A file outside git gets the full check.
It reports at most 20 violations per file.
Warn mode. With "mode": "warn" in the config, both hooks report the same findings without blocking.