Skip to content

feat(audit-engine): grouped stream producer, one line per distinct finding (repo#1883) - #1912

Merged
nc9 merged 2 commits into
mainfrom
boat/grouped-stream-findings-1883
Nov 16, 2026
Merged

nc9 merged 2 commits into
mainfrom
boat/grouped-stream-findings-1883

Conversation

@nc9

@nc9 nc9 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Part of squirrelscan/repo#1883 (epic squirrelscan/repo#1880). This PR does not close it: the API side is a private follow-up (see below).

Summary

Adds a grouped chunked-publish producer next to buildStreamFindings, in packages/audit-engine/src/stream-findings.ts. It is exported from both . and ./smart-audits. buildStreamFindings and buildSkippedPassCounts are unchanged, so the current wire contract and its private consumers keep working until the private switch-over.

  • buildGroupedStreamFindings(ruleResults, now, { maxLineChars? }) emits one line per distinct (ruleId, checkName, locator, fingerprint). Each line carries the complete affected-page list as pages: [normalizedUrl, payloadIndex][] (sorted), plus a deduped payloads table, because payloads differ per page (emission index, page-level aggregate, evidence). It filters exactly as buildStreamFindings does: unfold, skip site-scope and carried/unrendered checks, flatten per (rule, url), last write wins per primary key. The group key uses the literal status/message/value/expected, so a fingerprint collision cannot merge rows. Groups are built incrementally, so the per-page row array is never built.
  • A group whose line would exceed GROUPED_STREAM_MAX_LINE_CHARS (128 KiB, half the API's 256 KiB per-line ceiling) is split into part/parts lines. Each part carries only the payloads it references. No page is dropped, and a single oversized page gets its own part.
  • expandGroupedStreamFinding(line) is the inverse. It returns the exact per-page rows buildStreamFindings would have produced, so the API can rebuild the page_findings rows server-side with one shared function.
  • summarizeGroupedStream(lines) returns { lines, rows }. rows is the page_findings row count to expect. The finalize gate should compare count(*) against rows, not lines, so a dropped line or part fails closed.

Acceptance criteria (squirrelscan/repo#1883)

  • One line per distinct (ruleId, checkName, locator, fingerprint) with a complete page list: tests one line per distinct (rule, check, locator, fingerprint) when nothing splits and expand(grouped) equals buildStreamFindings on a mixed fixture.

  • [~] Under 3,000 lines for drscholls. Shown only on a synthetic fixture sized to the prod measurement (401 pages, 28,733 rows), which gives 2,479 lines (test drscholls-shaped: 401 pages, 28,733 rows collapse to 2,479 lines). A real drscholls run needs the private switch-over.

  • [~] The API builds the same page_findings rows: expandGroupedStreamFinding round-trips to exactly the buildStreamFindings rows. It is tested on a mixed fixture, on the drscholls-shaped fixture, and with every group forced to split. The API ingest that calls it is private.

  • [~] Finalize gate coherent and fails closed. Test rows equals distinct PKs, and dropping any line leaves rows short. The API gate change itself is private.

  • A group that goes over budget is split across lines with no page dropped: tests a wide group splits into parts within budget, losing no page, each part carries only the payloads it references, re-indexed, and a single page over budget still gets its own part.

  • [~] Peak RSS is lower on the container side. Run bun scripts/bench-grouped-stream.ts --mode per-page|grouped in packages/audit-engine; each mode runs in its own process, and the figures are the second of two runs:

    producer lines NDJSON bytes peak RSS heap delta time
    per-page 28,733 16.6 MB 122.8 MB 26.9 MB 887 ms
    grouped 2,479 2.1 MB 80.1 MB 12.0 MB 130 ms

    Finalize RSS is on the private API and is not measured here.

  • Health score delta 0: test grouped-materialized rows score identically to per-page rows, which runs runCloudSmartAudits complete-store reconstruct and calculateHealthScoreFromTallies. buildSkippedPassCounts is unchanged (test buildSkippedPassCounts is unchanged by the new producer).

Gates

  • bun test tests/grouped-stream-findings-1883.test.ts: 13 pass, 0 fail.
  • bun test tests/complete-store-parity.test.ts tests/item-finding-identity.test.ts tests/fingerprint-parity-golden.test.ts: 59 pass, 0 fail.
  • bun run typecheck (packages/audit-engine): clean.
  • oxlint on the changed files: clean. Packages have no format script, so oxfmt was not run.

Private follow-up

The worker-agent needs to stream buildGroupedStreamFindings. The API chunk ingest needs to accept grouped lines and build them back with expandGroupedStreamFinding. The finalize gate needs to compare against rows. Then the gitlink gets bumped. That is tracked as a private follow-up.

Agent skill

No change needed: this is internal publish plumbing, with no CLI or user-facing output change. No docs change either, for the same reason.

@nc9
nc9 force-pushed the boat/grouped-stream-findings-1883 branch from 0e99c06 to 60e6a4c Compare November 16, 2026 04:50
@nc9
nc9 marked this pull request as ready for review November 16, 2026 06:02
nc9 added 2 commits November 16, 2026 17:12
…nding with its complete page list (repo#1883)

Signed-off-by: Nik Cubrilovic <[email protected]>
…tream MemStore repo#1883

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the boat/grouped-stream-findings-1883 branch from 60e6a4c to ecf1b59 Compare November 16, 2026 06:12
@nc9
nc9 merged commit 4240eb8 into main Nov 16, 2026
16 checks passed
@nc9
nc9 deleted the boat/grouped-stream-findings-1883 branch November 16, 2026 06:27
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