Skip to content

Remembered-secret search costs O(values x text) on every redaction call #182

Backlog

Description

@TMHSDigital

Summary

Every redaction call copies all remembered token-like values into a new map and runs one substring search over the whole text per value. The number of values is set by the scanned tree, so a tree with many secret-named values in infrastructure files makes every tool call slow.

Where

  • internal/redact/redact.go:532-547 (rememberedMatches)
  • internal/redact/redact.go:348 (called from every redaction-mode scan)
  • internal/redact/redact.go:391 (maxRemembered = 1 << 20)
  • internal/seed/common.go:82, internal/seed/k8s.go:217, internal/seed/terraform.go:137, internal/seed/docker.go:154 (Remember callers)

Details

One call costs O(V) allocations plus O(V x len(text)) scanning, where V is the number of remembered values. The infrastructure checks call redact.Remember for every secret-named value in every Dockerfile, Kubernetes YAML and Terraform file, with no per-file or per-run cap below the global bound. A value of 12 or more token characters with a letter and a digit is global, so it is searched for in all text. This runs for every file the tools read or search for the first time, twice per tool result, once per new model message and once per lead.

Impact

Unmeasured; estimates from the code. With a few hundred values the cost is negligible. With about 10,000 token-like values (a monorepo with many generated manifests or variable files, or fixtures full of dummy credentials) each call makes 10,000 passes over its text: seconds per 1 MB file, so the first whole-target search of a run takes minutes. Near the bound, a scan effectively stops making progress, and tool calls are outside the model-call deadline.

Suggested fix

Match all remembered values in one pass: build an Aho-Corasick automaton (or a first-bytes index plus map lookup, since token-like values use a fixed character set) for the global set, rebuild it only when the set changes (a generation counter), and reuse it across calls. Iterate the global set under the read lock without copying it per call. Independently, cap token-like global values per file and per run well below the current bound and note in the run when the cap was hit.

Acceptance criteria

  • One pass over the text per call, whatever the number of values
  • No per-call copy of the global set
  • A per-file and per-run cap with a run note when it is hit
  • A benchmark with 10,000 values shows the improvement

Getting started

  • Relevant files: internal/redact/redact.go, internal/seed/common.go
  • How to run tests: make test-short (Unix) or .\make.ps1 test-short (Windows PowerShell); make lint before you call it done
  • Related docs: CONTRIBUTING.md, docs/ARCHITECTURE.md

Filed by repo-audit/full-audit at 50e92ec. Severity: medium. Effort: M. audit-key: performance|internal/redact/redact.go|rememberedMatches

Activity

  1. added
    help wanted
    performance
    severity: medium
    on Dec 3, 2026
  2. TMHSDigital commented on Dec 3, 2026

    @TMHSDigital
    OwnerAuthor

    The search itself is fixed in the commit named below; the cap on values is not, so this issue stays open for that part.

    Done:

    • The token-like remembered values are found in one pass over the text through an index (Aho-Corasick, internal/redact/multi.go, standard library only), once there are 64 of them. Below that the pass per value stays, now without copying the set on every call.
    • The index is rebuilt when the values added since the last build reach an eighth of its size (at least 32). Until then the new ones are searched one by one, so no value is ever left unsearched. A value that was forgotten after the build is dropped when it matches.
    • Values that belong to one file only are searched as before.

    Measured with BenchmarkRememberedMatches (committed) against the replaced search (a scratch benchmark, not committed), on one megabyte of text:

    token-like values before after
    10 11 ms 12 ms
    1,000 1,220 ms 3.8 ms
    10,000 9,857 ms 41 ms

    Checked for equal results: TestIndexedSearchFindsWhatAPassPerValueFinds compares every match (offsets and hash) with the replaced search on generated values that are prefixes and substrings of one another and overlap themselves, with and without markers, with and without a file key, in five states: few values (no index), with an index, with values added after the build, after a rebuild, and after the oldest file was forgotten. internal/redact, internal/seed, internal/agent/... and internal/engine pass, and golangci-lint is clean.

    Not done, and why: capping the token-like values per file and per run, with a run note when the cap is hit. A cap means some values a check found are no longer hidden everywhere, which is a decision about redaction, not about speed. With the search no longer the bottleneck the remaining cost of a very large number of values is memory for the index and the build time, bounded by the existing limit of 2^20 values.

  3. TMHSDigital commented on Dec 3, 2026

    @TMHSDigital
    OwnerAuthor

    The commit is cc150f5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedperformanceseverity: medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions