Skip to content

feat(cli): squirrel entities command with filters, export formats and --diff (#2092) - #336

Merged
nc9 merged 7 commits into
mainfrom
feat/2092-entities-command
Sep 13, 2026
Merged

nc9 merged 7 commits into
mainfrom
feat/2092-entities-command

Conversation

@nc9

@nc9 nc9 commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Implements squirrel entities (squirrelscan/repo#2092, epic #2061 sections 5 and 9) on top of the entity map that #333 made every audit build.

The command

Shaped like squirrel report: same flag names, same project-scoped store access, same --input escape hatch.

Invocation What it does
squirrel entities Summary of the latest audit's map
squirrel entities <@id|key|name> One entity in full: properties, declaring pages, references, conflicts
squirrel entities --list Stored audits with their entity counts and stable-@id share
squirrel entities --diff What changed between two audits

--type, --page and --problem produce a filtered map, not a filtered view, so -f graphml --type Organization hands a graph tool the filtered graph and the summary counts describe what survived rather than the whole site. --problem accepts no-id, conflict, dangling, single-page and split-identity.

Exit code is 0 on every path that produced an answer, including a site that declares no JSON-LD at all. That is a finding, not a failure. Only a usage mistake or an unreadable store exits 1.

Export formats

csv, dot, graphml and mermaid join the existing json, jsonld, html and markdown, all in packages/report beside the renderers they sit with.

Every string in a map comes from an audited page, so each renderer escapes for its own parser rather than sharing one escape: RFC 4180 quoting plus a leading-quote guard against spreadsheet formula injection for csv, backslash escaping for dot, XML 1.0 character-range filtering (control bytes, U+FFFE/U+FFFF and unpaired surrogates are dropped, not escaped, because &#x1; is itself invalid) for graphml, and label sanitisation with quoted edge labels for mermaid. Mermaid stops rendering usefully past a few hundred nodes, so it draws the 150 most-declared and says so in a comment.

--diff

New diffEntityMaps in the engine, exported from the @squirrelscan/audit-engine/entity-map subpath.

The property it exists to protect: two audits of a site rarely cover the same pages, and a naive set difference reports every entity on a page that simply was not visited as removed, which reads as "the site deleted its structured data". So an entity is only removed when every page that declared it was crawled again, and everything else lands in not crawled again.

Proving that is the subtle part. A node's pages is capped at 50 in the document with morePages counting the rest, so deciding on the sample calls a 101-page entity removed the moment the first 50 are recrawled. Coverage is therefore taken from the uncapped per-page declares index, and only when that index is itself complete (a publish projection clips it, which the crawl's own page count detects). Where coverage cannot be proven, the answer is not-crawled: conservative, says less, never the reverse.

The same rule governs fixes. A conflict or dangling reference counts as resolved only when the entity survived and all of its declaring pages were visited again, because a conflict needs two declarations to exist and seeing one of them tells you nothing.

Identity matching across an @id change pairs one-to-one: a group of same-named candidates is only matched when exactly one unmatched candidate exists on each side, so an ambiguous group falls through to added/removed rather than inventing a rename and a deletion at once. Signatures are JSON.stringify([sortedTypes, name]), never a delimiter join, since @type is site-controlled.

Review

Two codex exec passes over the non-test diff. The first found 16 correctness issues, the second confirmed 12 of those fixes and found the remaining 4 gaps plus one new defect. All are now fixed, with tests for the ones that were behavioural.

The load-bearing ones:

  • Both coverage defects above, and one-to-one identity matching.
  • getCrawlPageUrls used request URLs and every status where the collector uses final URLs and 2xx only, so a redirect became a page no entity row could name.
  • backfillDerivedFields repaired present-but-invalid values into a document that then validated. Absent means an older build did not write the field; present-and-invalid means the file is broken.
  • A store that could not be read exited 0 with its error on stdout, exactly like "no audit stored yet". Those call for opposite next steps.
  • --diff looked for the same-site predecessor inside the 50 most recent audits, but that window is global and ordered by time, so a busy site buried a quiet site's previous audit.
  • -o wrote whatever fmt produced, and fmt decides colour from whether stdout is a TTY. Run from a terminal, the entity and list views wrote escape bytes into the file.

Verification

  • tsgo --noEmit clean in core-contracts, report, crawler, audit-engine and cli
  • 117 tests pass across the entity-map suites: 25 diff, 16 export, 18 filter, 6 loader, plus the existing builder, streaming, slim, html and markdown suites
  • oxfmt and oxlint clean on apps/cli
  • make validate clean in docs/ (394 pages, no broken links)
  • End to end twice over. Against the real before/after snapshots of docs.squirrelscan.com, committed as fixtures: every format, every filter, and each error path checked for its exit code. Then against a live project store, which is where the --list column overflow and the empty-older-side case surfaced.

Docs

New /cli/entities reference page, added to the CLI nav and the command table.

Closes squirrelscan/repo#2092

https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP

@nc9

nc9 commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up filed as squirrelscan/repo#2105: three fields a map reassembled from the project store cannot recover exactly (a page's declaration count and references, an edge's pages), plus the fact that the store does not record whether an audit wrote a map at all. None is load-bearing here, and this PR documents each as an approximation rather than emitting a plausible number. Fixing them needs an additive migration to tables #2091 had only just shipped, which seemed worth separating from this change.

https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP

nc9 added 7 commits September 13, 2026 13:27
… --diff (#2092)

Queries the entity map every audit already builds (#2091), shaped like
`squirrel report`: a summary with no arguments, `--list` over stored audits,
and a lookup by `@id`, key or name.

`--type`, `--page` and `--problem` produce a filtered MAP rather than a
filtered view, so `-f graphml --type Organization` hands a graph tool the
filtered graph and the summary counts describe what survived.

Adds csv, dot, graphml and mermaid renderers beside the existing json, jsonld,
html and markdown. Each escapes for its own parser: RFC 4180 quoting plus a
formula-injection guard for csv, XML character-range filtering for graphml,
and label sanitisation with quoted edge labels for mermaid, which is capped at
150 nodes with a comment saying so.

`--diff` compares two audits via a new `diffEntityMaps` in the engine. The
property it exists to protect is that a smaller crawl never reads as a site
that deleted its structured data: an entity is only `removed` when every page
that declared it was crawled again, proven from the uncapped per-page index
rather than the node's capped sample, and anything unproven is reported as not
crawled. Conflicts and dangling references are only called resolved on the
same terms. Identity matching across an `@id` change is one-to-one and refuses
to pair an ambiguous group.

Exits 0 on every path that produced an answer, including a site that declares
no JSON-LD. That is a finding, not a failure.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
…to read (#2092)

Two defects from a second review pass.

`--page` chose between the per-page index and the node's own list. Each is
incomplete in a different way — the index is absent from a clipped export, the
node's list is truncated past 50 pages — so picking one drops entities that are
genuinely on the page. Take the union; neither source can lose anything the
other has.

`loadEntityMap` reported an unreadable project store only when NO project
produced a map. Returning project A's map while project B was unreadable is an
answer drawn from part of the data, so carry the failures back and say so.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
A store that could not be READ exited 0 and printed its error to stdout, the
same way "no audit stored yet" does. Those call for opposite next steps, so a
script could take a broken database for an empty one. Only the not-found case
is an answer now; a read failure exits 1 on stderr.

A skipped empty crawl was remembered per project, but auto-selection picks the
newest map across every project, so the audit it passed over routinely belongs
to another site and the note never appeared. Pool the skips and report the
newest one that is actually newer than what is being shown.

`--diff` looked for the same-site predecessor inside the 50 most recent audits.
That window is global and ordered by time, so 50 audits of a busy site bury a
quiet site's previous audit past the end and the command claims it has only one.
`listEntityMaps` now takes a `baseUrl` that narrows BEFORE the limit.

`-o` wrote whatever `fmt` produced, and `fmt` decides on colour from whether
stdout is a TTY. Right question for stdout, wrong one for a file: run from a
terminal, the entity and list views wrote escape bytes into it. Strip them in
`emit`, which is the one place that knows the destination is a file.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
A listing reads the entity rows of every crawl in every project. With two
crawl ids given, nothing in it was used.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
…own count (#2092)

The index is authoritative for coverage only when it accounts for every page
the node claims, `morePages` included. The two disagree only in a document
someone assembled by hand, and there the conservative answer is the right one.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
…n empty audit (#2092)

`toLocaleString()` runs to 21 characters on a 12-hour clock, so the 22-wide
Date column ran straight into Pages.

An audit with no entities is skipped as the older side of a default diff on
purpose: the store cannot tell "this site declared nothing" from "this audit
predates the entity map", and reading the second as the first turns every
entity the site has today into a fresh addition. The message now names the
escape hatch for when the user knows which it was.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
…rm (#2092)

`BASH_BLOCK_END` names the case arm that ends each command's bash completion
block, because those blocks contain nested `;;` of their own. Inserting
`entities)` between `audit)` and `crawl)` made the audit block over-read into
it, and the test failed on `--list`, a flag audit never offered.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the feat/2092-entities-command branch from 5d9383a to 04494d8 Compare September 13, 2026 03:28
@nc9
nc9 merged commit 72c878a into main Sep 13, 2026
14 checks passed
@nc9
nc9 deleted the feat/2092-entities-command branch September 13, 2026 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant