Skip to content

perf(evidence): compact canonicalization and invariant hardening - #27

Open
spaxe-dev wants to merge 4 commits into
mainfrom
feat/hardening-evidence-fast-path
Open

spaxe-dev wants to merge 4 commits into
mainfrom
feat/hardening-evidence-fast-path

Conversation

@spaxe-dev

@spaxe-dev spaxe-dev commented Dec 3, 2026 •

Copy link
Copy Markdown
Owner

Summary

This PR makes evidence canonicalization and the operations built on it
substantially faster, adds a deterministic invariant/property suite for the
pure policy layers, routes config domain normalization through the shared URL
policy, and removes compiler-proven dead code.

It is applied on top of current origin/main (c9681b2). It does not
include PR #26; it was independently merged with PR #26 in a separate worktree
and re-tested (see Reconciliation).

What changed

  • src/shared/evidence.ts: replace the per-character canonical mapping with
    compact segments and a lazy array view. Printable ASCII runs are lowercased
    in bulk; non-ASCII characters keep the exact NFKC/NFD/fold behavior and a
    bounded cache avoids re-normalizing repeated characters.
  • src/shared/search.ts: use the compact mapping accessors for case-insensitive
    literal search offsets.
  • src/shared/url-policy.ts / src/config.ts: one shared domain normalizer.
    Normalization is idempotent, strips mixed leading/trailing dot and whitespace
    forms, and fails closed on malformed wildcard placement instead of turning
    ".*.a" into the broader bare host "a".
  • Dead-code cleanup: remove unused exports, locals, parameters and imports that
    the compiler and an import scan proved unused. No workflow, recovery, policy
    evaluation or browser-control behavior is changed.
  • New test/unit/invariants.test.ts: 36 deterministic invariant/property tests.
  • New docs/invariants.md: invariant catalog, fix-history coverage,
    counterexamples, benchmark methodology and limitations.
  • New scripts/bench-evidence.ts + npm run bench:evidence: reproducible
    warmup/median/p95 benchmark.

Correctness and parity

  • The new canonicalizer was compared with the pre-change implementation on a
    generated mixed Unicode corpus: 20,157/20,157 exact matches, including
    text, starts, ends and folds.
  • Fixed parity cases cover composed/decomposed accents, combining marks,
    smart punctuation, full-width characters, circled numbers, emoji, invisible
    characters, leading/trailing whitespace, tabs/newlines and collapsed runs.
  • Existing evidence tests, the 36 new invariant tests, and all current unit
    tests pass.
  • Fuzzy matching semantics were not changed to improve the benchmark.

Performance

Method: Windows 11, Node v22.15.1 (win32/x64), AMD Ryzen 7 4800H (16 logical
CPUs), 23.4 GiB RAM. npm run bench:evidence: 3 warmup runs, 15 measured
iterations (5 MB uses 5), median and p95. The same harness was run in a detached
origin/main worktree for the baseline.

ASCII page = repeated 70-character ASCII sentence. Mixed page = repeated
sentence with accented Latin, curly quotes, an em dash and an apostrophe.

Operation Before (median) After (median) Speedup
Canonicalize 1 MB ASCII 575 ms 2.4 ms ~245x
Canonicalize 5 MB ASCII 2,932 ms 11.6 ms ~253x
Literal search 1 MB ASCII 594 ms 4.6 ms ~129x
Literal search 5 MB ASCII 2,662 ms 14.8 ms ~180x
Normalized evidence 1 MB ASCII 555 ms 2.7 ms ~209x
Normalized evidence 5 MB ASCII 2,587 ms 13.6 ms ~190x
Exact evidence 1 MB ASCII 0.10 ms 0.08 ms unchanged
Fuzzy evidence 1 MB ASCII 612 ms 70.6 ms ~8.7x
Canonicalize 1 MB mixed 529 ms 162 ms ~3.3x
Literal search 1 MB mixed 520 ms 152 ms ~3.4x
Normalized evidence 1 MB mixed 522 ms 152 ms ~3.4x
Fuzzy evidence 1 MB mixed 599 ms 223 ms ~2.7x

1 MB p95: canonicalization 688 → 6.0 ms; literal 669 → 6.6 ms; normalized
594 → 4.8 ms; fuzzy 622 → 94 ms. 5 MB p95: 3,513 → 13.2 ms; 3,046 → 16.7 ms;
2,791 → 14.8 ms; 2,721 → 107 ms.

Limitations: the 100x+ result is for predominantly ASCII pages. Mixed-script
pages are ~3.4x because per-character segments dominate. Fuzzy matching is
still dominated by bounded Levenshtein distance; it was not forced to 100x
because that would alter matching semantics.

Invariants and counterexamples

Exhaustive over finite domains: every policy mode × capability, every
permanent deny/restriction, every invalid-policy capability, every mutating
tool mapping, every site-scoped URL requirement, every short URL/domain string
over a stated alphabet, and every boolean-type config rejection case.

Seeded property tests: canonical mapping bounds/order/folds, canonicalization
idempotence, literal-search span validity, ambiguity/context behavior,
audit redaction and outcome derivation, snapshot caps, target normalization,
and config fail-closed parsing.

Counterexamples found and fixed:

  1. normalizeDomainEntry was not idempotent (".*.a" → "*.a" → "a").
  2. config.ts had a second domain parser that left trailing dots.
  3. A first idempotent fix broadened malformed ".*.a" into the bare host
    "a"; it now fails closed instead.
  4. The first fast canonicalizer kept leading whitespace and used a collapsed
    run's end offset instead of its first character's offset; both were caught
    by the parity corpus and fixed.

Reconciliation with main and PR #26

  • Applied cleanly onto origin/main (c9681b2); only CHANGELOG.md conflicted.
  • Merged origin/phase7/workflow-execution-engine-20261203 (PR feat(workflows): bounded workflow execution engine with safe resume #26) into a
    separate worktree, resolving only CHANGELOG.md and the startPageServer
    helper conflict. No workflow execution, recovery, cancellation or checkpoint
    code was modified.
  • Combined stack: npm run typecheck, npm run build, and npm test pass:
    758 tests, 743 pass, 15 skip, 0 fail.
  • Feature branch alone: 705 tests, 690 pass, 15 skip, 0 fail.
  • If PR feat(workflows): bounded workflow execution engine with safe resume #26 merges first, expect only trivial conflicts in CHANGELOG.md and
    test/integration/helpers.ts.

Dead code

Removed 98 lines: 9 unused exports and 10 unused locals/parameters/imports.
tsc --noUnusedLocals --noUnusedParameters is clean for the changed source.
The two files with no relative importer (src/bridge/entry.ts,
src/onboard/skill-offer-cli.ts) are build entry points and were retained.

Risks

Not doing

  • No merge, no force push, no direct push to main.
  • No unrelated features or semantic changes to fuzzy matching.
  • No live browser behavior changes.

Replace the per-character canonical mapping with compact segments and a lazy array view. Printable ASCII runs are lowercased in bulk, common non-ASCII characters are cached, and the existing offset/fold contract is preserved for callers and tests.

Also route config domain normalization through the shared URL policy, fail closed on malformed wildcard placement, remove compiler-proven dead code, and add deterministic property/invariant tests plus a reproducible benchmark.
Use median/p95 results from the committed benchmark harness on the stated hardware, and scope tsconfig to the new benchmark file instead of every script.
@spaxe-dev
spaxe-dev requested a balanced review from Copilot December 4, 2026 02:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

2 participants