Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

29.8 KB
Raw Blame
package asymptoteobserve

import (
	"iter"
	"math/bits"
	"regexp"
	"regexp/syntax"
	"slices"
	"strings"
	"sync"
	"unicode/utf8"
)

// This file is how redaction finds the matches of its rules (privacy.go). The rules, their order,
// their patterns and their replacements are unchanged; what changed is how a rule's matches are
// found, without changing which:
//
//   - The gate used to lower-case the text and run a substring search per needle set. On a few
//     kilobytes of tool output or source code that search was nearly all of redaction's cost. One
//     pass of an Aho-Corasick automaton over the text, folding ASCII case as it reads, now finds
//     every occurrence of every needle of every rule (needleScanner).
//   - A rule's pattern used to run over the whole text once the gate let it through. Go's regexp
//     has no DFA, so that cost tens of nanoseconds a byte for the case-insensitive label rules. A
//     match always holds one of the rule's needles, at a place the rule's start names, so the
//     pattern now runs anchored at those places only, which finds exactly the matches the whole
//     pattern does (see redactionRule.matches).
//   - Redaction stays linear in the text whatever the text holds. A pattern anchored at one place
//     can read on to the end of its line or run, so many places on one long line could cost a
//     quadratic amount; past a budget a rule runs its whole pattern once instead, from a start
//     where it finds the same matches (redactSlot.safeStart). TestRedactionWorkIsLinear counts the
//     work on texts built for the worst case.
//
// Text holding a character Go's case folding equates with an ASCII letter (U+017F folds to s,
// U+212A to k) can match a case-insensitive pattern where no needle occurs, so on such text the
// case-insensitive rules run their whole patterns, as they always did. redactString(value, false)
// runs every rule's whole pattern on any text, and TestRedactGateNeverChangesAResult and
// FuzzRedactGate hold the two to the same result.
//
// A shape rule's matches also take in the secrets written right after them (redact_adjacent.go),
// the same way whichever way the matches are found.

// redactStart says where, around an occurrence of one of a rule's needles that passes the needle
// set's precededBy and followedBy (and those of the sets it is within), a match of the rule's
// pattern can begin.
type redactStart uint8

const (
	// startNeedle: the rule's offset bytes before the needle, at a word boundary when the pattern
	// opens with \b.
	startNeedle redactStart = iota
	// startLabel: at a word boundary in the run of [A-Za-z0-9_.-] that ends at the needle, where
	// redactLabel's lazy `[A-Za-z0-9_.-]*?` begins. The pattern opens with \b.
	startLabel
	// startFlag: at the whitespace before the flag that ends at the needle, or at the start of the
	// text where the flag does: redactFlag's `(?:^|\s)`.
	startFlag
	// startListedFlag: at the quote before the flag that ends at the needle.
	startListedFlag
	// startScheme: at a word boundary in the run of [A-Za-z0-9_+.-] before the needle, where a
	// URL's `\b[a-z0-9_+.-]*?[a-z][a-z0-9+.-]*` scheme and the word it ends begin. The pattern
	// opens with \b.
	startScheme
	// startParam: at the `?` or `&` right before the run of [A-Za-z0-9_.-] that ends at the
	// needle, where a URL query parameter opens, and where startLabel begins when there is none
	// there or the pattern does not match from it. The pattern is two alternatives: the parameter
	// with its `?` or `&`, and a label that opens with \b.
	startParam
	// startLine: anywhere on the line of the needle or the blank lines before it. The pattern runs
	// whole from the start of that line, where its (?m)^ and \b see what they see in the whole
	// text.
	startLine
)

// redactSlot is what one rule compiles, on first use.
type redactSlot struct {
	once     sync.Once
	anchored *regexp.Regexp
	boundary bool
	// The assertions the whole pattern makes, which decide where it can be run on a suffix of the
	// text with the same result (safeStart): \b or \B, (?m)^, and ^ or \A.
	wordAssert, lineStart, textStart bool
	// index is the rule's index in redactionRules, or -1.
	index int
}

// boundaryPrefixes are the ways a pattern opens with \b.
var boundaryPrefixes = []string{`(?i)(\b`, `(?i)\b`, `(\b`, `\b`}

// prepare compiles the rule's pattern anchored at the start of the text, without the \b it opens
// with, if any: the anchored pattern cannot see the byte before its start, so the boundary is
// checked against the whole text instead.
func (s *redactSlot) prepare(rule redactionRule) {
	s.once.Do(func() {
		expr := rule.expr
		for _, prefix := range boundaryPrefixes {
			if strings.HasPrefix(expr, prefix) {
				s.boundary = true
				expr = prefix[:len(prefix)-2] + expr[len(prefix):]
				break
			}
		}
		if (rule.start == startLabel || rule.start == startScheme) && !s.boundary {
			panic("asymptoteobserve: a label or scheme redaction rule must open with \\b: " + rule.expr)
		}
		s.anchored = regexp.MustCompile(`^` + expr)
		parsed, err := syntax.Parse(rule.expr, syntax.Perl)
		if err != nil {
			panic(err)
		}
		var walk func(re *syntax.Regexp)
		walk = func(re *syntax.Regexp) {
			switch re.Op {
			case syntax.OpWordBoundary, syntax.OpNoWordBoundary:
				s.wordAssert = true
			case syntax.OpBeginLine:
				s.lineStart = true
			case syntax.OpBeginText:
				s.textStart = true
			}
			for _, sub := range re.Sub {
				walk(sub)
			}
		}
		walk(parsed)
	})
}

// safeStart returns the latest position at or before q where the rule's whole pattern can run on
// the rest of the text and find what it finds there in the whole text: a position whose assertions
// read the same with the text before it cut off. It is the start of the text, or a position after
// an ASCII byte (so a character boundary however the text before it decodes) that is not a word
// character when the pattern tests word boundaries, and a newline when it tests line starts; a
// pattern that tests the start of the text can only run from it.
func (s *redactSlot) safeStart(t string, q int) int {
	if s.textStart {
		return 0
	}
	from := q
	for ; from > 0; from-- {
		c := t[from-1]
		if c < utf8.RuneSelf && (!s.wordAssert || !isWordByte(c)) && (!s.lineStart || c == '\n') {
			break
		}
	}
	if redactWork != nil {
		redactWork(q - from)
	}
	return from
}

// Anchored matching tries one place per occurrence of a rule's needles, and a pattern anchored at
// one place can read on to the end of the text before it fails: a curl rule reads its whole line.
// A text with many occurrences could make that quadratic. So before trying any, a rule adds up the
// text it would hand the pattern, from each place to the end, and when that passes
// redactAnchoredPerByte times the text's length plus redactAnchoredBase, it runs its whole pattern
// once instead, from a safe start before the first place. Either way the matches are the same, and
// the regexp engine reads at most that budget plus the text once: linear in the text, whatever it
// holds. TestRedactionWorkIsLinear holds this.
var (
	redactAnchoredPerByte = 8
	redactAnchoredBase    = 16 << 10
	// redactWork, when set (by tests), is told the length of every text redaction hands a pattern
	// and of every run it scans back over to find where a match starts.
	redactWork func(n int)
)

// redactEngine is built the first time anything is redacted: building it at package init would
// cost every hook run its allocations whether it redacts or not (TestPackageInitStaysCheap).
type redactEngine struct {
	scanner  needleScanner
	setMasks [idSetCap]idSet // redactNeedles id -> the needles of the set, as scanner needle ids
	rules    []redactSlot
	assigned redactSlot
	// adjacent is what redact_adjacent.go knows of the rules, built the first time it is needed.
	adjacentOnce sync.Once
	adjacent     *redactAdjacency
}

var redactEngineFor = sync.OnceValue(func() *redactEngine {
	e := &redactEngine{rules: make([]redactSlot, len(redactionRules))}
	for i := range e.rules {
		e.rules[i].index = i
	}
	e.assigned.index = -1
	ids := map[redactNeedle]int{}
	var needles []redactNeedle
	var register func(set *redactNeedles)
	register = func(set *redactNeedles) {
		for ; set != nil; set = set.within {
			for _, text := range set.needles {
				n := redactNeedle{text: text, folded: set.folded}
				id, ok := ids[n]
				if !ok {
					id = len(needles)
					ids[n] = id
					needles = append(needles, n)
				}
				e.setMasks[set.id].add(id)
			}
		}
	}
	for _, rule := range redactionRules {
		register(rule.needles)
	}
	register(redactAssignedRule().needles)
	// Not needles of any rule: they mark text a case-insensitive pattern can match without one.
	for _, text := range []string{"\u017f", "\u212a"} {
		e.scanner.unfoldable.add(len(needles))
		needles = append(needles, redactNeedle{text: text})
	}
	if len(needles) > idSetCap {
		panic("asymptoteobserve: more redaction needles than an idSet holds; lengthen idSet")
	}
	e.scanner.build(needles)
	return e
})

// compiledPatterns holds every redaction pattern compiled so far, by expression.
var compiledPatterns sync.Map

// compiledPattern compiles expr the first time it is asked for.
func compiledPattern(expr string) *regexp.Regexp {
	if re, ok := compiledPatterns.Load(expr); ok {
		return re.(*regexp.Regexp)
	}
	re, _ := compiledPatterns.LoadOrStore(expr, regexp.MustCompile(expr))
	return re.(*regexp.Regexp)
}

// redactGate answers whether text holds any of a set of needles: an occurrence of one that
// passes the set's precededBy and followedBy, in a text that holds the set it is within. The
// occurrences are found once per version of the text.
type redactGate struct {
	value   string
	enabled bool
	engine  *redactEngine
	scanned bool
	hits    []redactHit
	found   idSet // scanner needle ids
	// known and present are sets of redactNeedles ids: which sets were looked for in this version
	// of the text, and which were found.
	known, present idSet
	// hidden is set when a match may hide another from this pass: a value a rule keeps, or the
	// capture takes, that is itself a label and its separator (keptLabel), inside a match that
	// neither looks into; a copy a token ran on over, as replacing a copy may (copyHidesMatch); or a
	// replaced AWS secret's `/`, where a URL's password stopped.
	hidden bool
	// copies are the assigned values whose copies are removed after every rule has run, and what
	// the chains of this pass found of them in the text (copyIndex).
	copies *copyIndex
}

func (g *redactGate) set(value string) {
	if value != g.value {
		g.value, g.scanned, g.known, g.present = value, false, idSet{}, idSet{}
		if g.copies != nil {
			g.copies.lo, g.copies.at = 0, g.copies.at[:0]
		}
	}
}

func (g *redactGate) e() *redactEngine {
	if g.engine == nil {
		g.engine = redactEngineFor()
	}
	return g.engine
}

func (g *redactGate) scan() {
	if !g.scanned {
		g.hits = g.e().scanner.scan(g.value, g.hits[:0], &g.found)
		g.scanned = true
	}
}

// unfoldable reports whether the text holds U+017F or U+212A, which a case-insensitive pattern
// equates with s and k: such a pattern can match "ſecret", which holds no needle "secret".
func (g *redactGate) unfoldable() bool {
	g.scan()
	return g.found.meets(&g.e().scanner.unfoldable)
}

// holdsNothing reports whether the text holds no needle at all, and so no set: no rule can match
// it.
func (g *redactGate) holdsNothing() bool {
	g.scan()
	return g.enabled && g.found.empty()
}

func (g *redactGate) holds(set *redactNeedles) bool {
	if !g.enabled {
		return true
	}
	if set.within != nil && !g.holds(set.within) {
		return false
	}
	if g.known.has(set.id) {
		return g.present.has(set.id)
	}
	g.known.add(set.id)
	if set.folded && g.unfoldable() {
		g.present.add(set.id)
		return true
	}
	g.scan()
	mask := &g.e().setMasks[set.id]
	if !g.found.meets(mask) {
		return false
	}
	for _, h := range g.hits {
		if mask.has(h.id) && set.passes(g.e(), g.value, h, false) {
			g.present.add(set.id)
			return true
		}
	}
	return false
}

// passes reports whether the occurrence h passes the set's followedBy and precededBy and, with
// within, those of the sets it is within that hold its needle too. For a rule's own set with
// within, that is a condition every match holding the occurrence meets: a narrower set's needle
// ends the same label or flag as the broader set's does.
//
// followedBy is asked first. It reads only the separator after the needle, and it passes only a
// needle that ends its run of letters, digits and dashes, so precededBy, which reads that run back
// from the needle, reads each run once however many needles it holds.
func (set *redactNeedles) passes(engine *redactEngine, t string, h redactHit, within bool) bool {
	masks := &engine.setMasks
	for s := set; s != nil; s = s.within {
		if masks[s.id].has(h.id) && (s.followedBy != nil && !s.followedBy(t[h.end:]) || s.precededBy != nil && !s.precededBy(t[:h.start])) {
			return false
		}
		if !within {
			break
		}
	}
	return true
}

// matches yields what FindAllStringSubmatchIndex(text, -1) of the rule's pattern returns, in
// order and with the same indexes, each with a pattern ExpandString can expand it with.
//
// FindAll returns, after the end of the previous match, the match that starts leftmost, the one the
// pattern prefers among those starting there. Every match holds an occurrence of one of the rule's
// needles that passes its set's conditions, and begins at a place the rule's start names around
// it. The pattern anchored at such a place (with an opening \b checked against the whole text,
// which the anchored pattern cannot see before its start) finds exactly the match the whole
// pattern prefers among those starting there. So trying the places in order and taking the first
// that matches is what FindAll does, without running the pattern over the text between them.
//
// Where a match can begin anywhere in a run before the needle (startLabel, startScheme), only the
// leftmost place in the run is tried. Whatever a match from a later place in the run takes, a
// match from the earlier one can take too, its lazy or starred run of the same characters taking
// the ones between; so when the earlier place does not match, no place in the run does. A
// startParam rule tries the `?` or `&` before the run first, then that place.
//
// When the places would hand the pattern more text than the budget allows (redactAnchoredPerByte),
// the whole pattern runs once from a safe start before the first of them instead. No match starts
// before the first place, and from a safe start the pattern finds what it finds in the whole text.
//
// A shape rule's match is followed by the secrets written right after it (redactChain.run), each
// yielded with the rule it is a match of, and the next match is looked for from where they end,
// in both ways of finding it. Text with none gets exactly FindAll's matches.
func (rule redactionRule) matches(text *redactGate, slot *redactSlot) iter.Seq2[[]int, redactUse] {
	return func(yield func([]int, redactUse) bool) {
		t := text.value
		var chain *redactChain
		if rule.shape != shapeNone {
			text.scan()
			slot.prepare(rule)
			chain = newRedactChain(text.e(), t, text.hits)
			chain.copies = text.copies
			defer func() {
				text.hidden = text.hidden || chain.hid || chain.slashed && strings.Contains(t, "://")
			}()
		}
		whole := func(from int) {
			re := rule.pattern()
			use := redactUse{re, &rule}
			if chain == nil {
				redactWorked(len(t) - from)
				for _, m := range re.FindAllStringSubmatchIndex(t[from:], -1) {
					for i := range m {
						if m[i] >= 0 {
							m[i] += from
						}
					}
					if !yield(m, use) {
						return
					}
				}
				return
			}
			// FindAll's loop, from where each chain ends. The pattern run on t[pos:] takes pos for
			// the start of a word; where t does not, the search goes on from the next byte. The
			// first match from aheadFrom on (nil: none) stays the first from any later pos up to
			// its start, so a rescued match before it does not make the search read on to it again.
			si := chain.adj.byRule[slot.index]
			var ahead []int
			aheadFrom := -1
			for pos := from; pos < len(t); {
				if aheadFrom < 0 || ahead != nil && ahead[0] < pos {
					ahead, aheadFrom = chain.search(re, pos), pos
					if ahead != nil && ahead[0] == pos && pos > from && slot.boundary && !wordBoundary(t, pos) {
						ahead = chain.search(re, pos+1)
					}
				}
				m, limit := ahead, len(t)
				if m != nil {
					limit = m[0]
				}
				if rescued := chain.rescueBefore(si, pos, limit); rescued != nil {
					m = rescued
				}
				if m == nil {
					return
				}
				end, ok := chain.run(si, m, use, yield)
				if !ok {
					return
				}
				pos = end
			}
		}
		if !text.enabled || rule.needles.folded && text.unfoldable() {
			whole(0)
			return
		}
		text.scan()
		engine := text.e()
		mask := &engine.setMasks[rule.needles.id]
		if !text.found.meets(mask) {
			return
		}
		if rule.start == startLine {
			if from, ok := lineStart(engine, t, text.hits, mask, rule.needles); ok {
				whole(from)
			}
			return
		}
		slot.prepare(rule)
		// What the anchored attempts would hand the pattern, at most: each place, to the end.
		first, handed, last := -1, 0, -1
		budget := redactAnchoredPerByte*len(t) + redactAnchoredBase
		for _, h := range text.hits {
			if !mask.has(h.id) || !rule.needles.passes(engine, t, h, true) {
				continue
			}
			q := rule.startFor(t, h, 0, slot.boundary)
			if q < 0 || q == last {
				continue
			}
			if first < 0 || q < first {
				first = q
			}
			last = q
			places := 1
			if rule.opensParam(t, q) {
				places = 2 // the label after it, where the parameter does not match
			}
			if handed += places * (len(t) - q); handed > budget {
				break
			}
		}
		if first < 0 {
			return
		}
		if handed > budget {
			whole(slot.safeStart(t, first))
			return
		}
		anchoredAt := func(q int) []int {
			if redactWork != nil {
				redactWork(len(t) - q)
			}
			m := slot.anchored.FindStringSubmatchIndex(t[q:])
			for i := range m {
				if m[i] >= 0 {
					m[i] += q
				}
			}
			return m
		}
		pos, failed := 0, -1
		for _, h := range text.hits {
			if !mask.has(h.id) || h.start < pos || !rule.needles.passes(engine, t, h, true) {
				continue
			}
			q := rule.startFor(t, h, pos, slot.boundary)
			if q < 0 || q == failed {
				continue
			}
			m := anchoredAt(q)
			if m == nil && rule.opensParam(t, q) {
				// No parameter opens at the `?` or `&`: the label after it may still match.
				if q = rule.startFor(t, h, q+1, slot.boundary); q < 0 {
					continue
				}
				m = anchoredAt(q)
			}
			if m == nil && chain != nil {
				m = chain.rescue(chain.adj.byRule[slot.index], q)
			}
			if m == nil {
				failed = q
				continue
			}
			use := redactUse{slot.anchored, &rule}
			if chain != nil {
				end, ok := chain.run(chain.adj.byRule[slot.index], m, use, yield)
				if !ok {
					return
				}
				pos = end
				continue
			}
			pos = m[1]
			if !yield(m, use) {
				return
			}
		}
	}
}

// redactUse is what matches yields with a match: the pattern to expand it with and the rule it is
// a match of, which for a secret written right after a shape rule's match is another shape rule.
type redactUse struct {
	pattern *regexp.Regexp
	rule    *redactionRule
}

// startFor returns the leftmost place at or after pos where a match holding h can begin, or -1.
func (rule redactionRule) startFor(t string, h redactHit, pos int, boundary bool) int {
	switch rule.start {
	case startNeedle:
		q := h.start - rule.offset
		if q < pos || boundary && !wordBoundary(t, q) {
			return -1
		}
		return q
	case startLabel, startScheme, startParam:
		class, last := &labelClass, h.start
		if rule.start == startScheme {
			class, last = &schemeClass, h.start-1
		}
		run := h.start
		for run > 0 && class[t[run-1]] {
			run--
		}
		if redactWork != nil {
			redactWork(h.start - run)
		}
		if rule.start == startParam && run > pos && (t[run-1] == '?' || t[run-1] == '&') {
			return run - 1
		}
		for q := max(run, pos); q <= last; q++ {
			if wordBoundary(t, q) {
				return q
			}
		}
		return -1
	case startFlag, startListedFlag:
		// The needle set's precededBy has checked that what comes before the flag is whitespace,
		// the start of the text, or (listed) a quote.
		q, _ := flagStart(t[:h.start])
		if q > 0 {
			q--
		}
		if q < pos {
			return -1
		}
		return q
	}
	panic("asymptoteobserve: no start for a redaction rule")
}

// opensParam reports whether q, a place startFor returned, is the `?` or `&` a startParam rule's
// query parameter opens with, rather than its label's place: a label holds neither.
func (rule redactionRule) opensParam(t string, q int) bool {
	return rule.start == startParam && (t[q] == '?' || t[q] == '&')
}

// lineStart returns the start of a line at or before every place a startLine match holding one of
// the needles can begin: before the needle, an optional `set-`, a quote, whitespace across lines,
// and a few bytes (`=`, `--header`) more.
func lineStart(engine *redactEngine, t string, hits []redactHit, mask *idSet, set *redactNeedles) (int, bool) {
	from, ok := len(t), false
	for _, h := range hits {
		if !mask.has(h.id) || !set.passes(engine, t, h, true) {
			continue
		}
		b := h.start
		if b >= 4 && strings.EqualFold(t[b-4:b], "set-") {
			b -= 4
		}
		if b > 0 && strings.IndexByte(redactQuotes, t[b-1]) >= 0 {
			b--
		}
		for b > 0 && isRegexpSpace(t[b-1]) {
			b--
		}
		from, ok = min(from, max(0, b-10)), true
	}
	for from > 0 && t[from-1] != '\n' {
		from--
	}
	return from, ok
}

func isRegexpSpace(c byte) bool {
	return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'
}

// isWordByte is \b's notion of a word character; a byte of a multi-byte character, or an invalid
// one, decodes to a rune that is not one.
func isWordByte(c byte) bool {
	return 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' || c == '_'
}

// wordBoundary reports whether \b holds at i < len(t).
func wordBoundary(t string, i int) bool {
	return (i > 0 && isWordByte(t[i-1])) != isWordByte(t[i])
}

func isASCIILetter(c byte) bool {
	return 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z'
}

// labelClass and schemeClass are the runs a startLabel and a startScheme match begin in, with
// capitals: the patterns are case-insensitive.
var labelClass, schemeClass = func() (label, scheme [256]bool) {
	for c := range 256 {
		b := byte(c)
		alnum := isASCIILetter(b) || '0' <= b && b <= '9'
		label[c] = alnum || b == '_' || b == '.' || b == '-'
		scheme[c] = alnum || b == '_' || b == '+' || b == '.' || b == '-'
	}
	return label, scheme
}()

// ---- id sets ----

// idSet is a set of small ids: the scanner's needle ids, or redactNeedles ids. What the gate asks
// of one (does the text hold one of this set's needles, is this hit's needle one of them) is a few
// ANDs, with nothing allocated. Its methods take pointers: the sets sit in arrays and structs, and
// Go copies an array of more than one word for a value receiver and passes it in memory.
//
// It holds idSetCap ids. To hold more, lengthen the array, to a power of two: idBit masks an id's
// word index with len-1, which the compiler can prove in range, so has and add carry no bounds
// check. The engine panics at build time before an id reaches idSetCap.
type idSet [2]uint64

// idSetCap is how many ids an idSet holds.
const idSetCap = len(idSet{}) * 64

// An idSet's length must be a power of two (idBit); this constant overflows, and the package
// fails to compile, when it is not.
const _ = -uint(len(idSet{}) & (len(idSet{}) - 1))

// idBit returns the index of id's word in an idSet and its bit in that word.
func idBit(id int) (int, uint64) {
	return int(uint(id)>>6) & (len(idSet{}) - 1), 1 << (uint(id) & 63)
}

func (s *idSet) add(id int) {
	w, bit := idBit(id)
	s[w] |= bit
}

func (s *idSet) has(id int) bool {
	w, bit := idBit(id)
	return s[w]&bit != 0
}

func (s *idSet) empty() bool { return *s == idSet{} }

// meets reports whether s and o hold an id in common.
func (s *idSet) meets(o *idSet) bool {
	var common uint64
	for i := range s {
		common |= s[i] & o[i]
	}
	return common != 0
}

// union adds every id of o to s.
func (s *idSet) union(o *idSet) {
	for i := range s {
		s[i] |= o[i]
	}
}

// ---- the needle scanner ----

// redactNeedle is one literal some rule's pattern cannot match without; folded needles are lower
// case and found whatever the case of the text's ASCII letters.
type redactNeedle struct {
	text   string
	folded bool
}

// redactHit is one occurrence of a needle.
type redactHit struct {
	start, end int
	id         int
}

// needleScanner is an Aho-Corasick automaton over every needle, reading the text with its ASCII
// letters lower-cased: one pass finds every occurrence of every needle, overlapping ones included.
//
// A state is the offset of its row in delta, so a step is one add and one load. The states where
// a needle ends are numbered last, from firstOut, so a step that finds none costs one comparison.
type needleScanner struct {
	needles    []redactNeedle
	class      [256]uint8 // byte -> input class; 0 for bytes no needle holds
	delta      []uint32   // state + class -> next state
	out        []idSet    // (state-firstOut)/nc -> the needles ending there
	nc         int        // number of classes, the length of a row
	firstOut   int
	longest    int   // the longest needle's length
	unfoldable idSet // the needles that are U+017F and U+212A
}

func asciiLower(c byte) byte {
	if 'A' <= c && c <= 'Z' {
		return c + 'a' - 'A'
	}
	return c
}

func (sc *needleScanner) build(needles []redactNeedle) {
	sc.needles = needles
	var classOf [256]uint8
	n := 1
	for _, nd := range needles {
		sc.longest = max(sc.longest, len(nd.text))
		for i := range len(nd.text) {
			if c := asciiLower(nd.text[i]); classOf[c] == 0 {
				classOf[c] = uint8(n)
				n++
			}
		}
	}
	sc.nc = n
	for b := range 256 {
		sc.class[b] = classOf[asciiLower(byte(b))]
	}
	// The trie.
	next := [][]int{make([]int, n)}
	out := []idSet{{}}
	for id, nd := range needles {
		s := 0
		for i := range len(nd.text) {
			c := sc.class[nd.text[i]]
			if next[s][c] == 0 {
				next = append(next, make([]int, n))
				out = append(out, idSet{})
				next[s][c] = len(next) - 1
			}
			s = next[s][c]
		}
		out[s].add(id)
	}
	// Failure links, breadth first, folded into a complete transition table. Class 0 never
	// continues a needle, so it always returns to the root.
	fail := make([]int, len(next))
	delta := make([]int, len(next)*n)
	var queue []int
	for c := 1; c < n; c++ {
		if t := next[0][c]; t != 0 {
			queue = append(queue, t)
		}
		delta[c] = next[0][c]
	}
	for len(queue) > 0 {
		s := queue[0]
		queue = queue[1:]
		out[s].union(&out[fail[s]])
		for c := 1; c < n; c++ {
			if t := next[s][c]; t != 0 {
				fail[t] = delta[fail[s]*n+c]
				delta[s*n+c] = t
				queue = append(queue, t)
			} else {
				delta[s*n+c] = delta[fail[s]*n+c]
			}
		}
	}
	// Renumber: the root first, then the states no needle ends in, then those some do.
	order := make([]int, 0, len(next))
	for s := range next {
		if out[s].empty() {
			order = append(order, s)
		}
	}
	sc.firstOut = len(order) * n
	for s := range next {
		if !out[s].empty() {
			order = append(order, s)
			sc.out = append(sc.out, out[s])
		}
	}
	row := make([]int, len(next))
	for i, s := range order {
		row[s] = i * n
	}
	sc.delta = make([]uint32, len(next)*n)
	for s := range next {
		for c := range n {
			sc.delta[row[s]+c] = uint32(row[delta[s*n+c]])
		}
	}
}

// scanLanes is how many places of a long text are read at once. One reading is a chain of
// dependent loads; independent ones overlap.
const scanLanes = 4

// scan appends every needle occurrence in t to hits, ordered by start, returns them, and sets
// found to the needles that occur. found is written in place rather than returned: Go passes an
// array of more than one word in memory, so returning one cost a copy on every text.
func (sc *needleScanner) scan(t string, hits []redactHit, found *idSet) []redactHit {
	*found = idSet{}
	delta, class, first := sc.delta, &sc.class, uint32(sc.firstOut)
	s, from := uint32(0), 0
	if seg := len(t) / scanLanes; seg >= 64 {
		// Lane k reports the occurrences that end in (k*seg, (k+1)*seg]. It starts reading early
		// enough for the longest needle to end at its first byte, and lane 0 reads that much past
		// its end; what ends outside its part, a lane does not report.
		overlap := sc.longest - 1
		t0 := t[:seg+overlap]
		t1 := t[seg-overlap : 2*seg]
		t2 := t[2*seg-overlap : 3*seg]
		t3 := t[3*seg-overlap : 4*seg]
		var s0, s1, s2, s3 uint32
		for j := 0; j < len(t0) && j < len(t1) && j < len(t2) && j < len(t3); j++ {
			s0 = delta[s0+uint32(class[t0[j]])]
			s1 = delta[s1+uint32(class[t1[j]])]
			s2 = delta[s2+uint32(class[t2[j]])]
			s3 = delta[s3+uint32(class[t3[j]])]
			if s0 >= first || s1 >= first || s2 >= first || s3 >= first {
				for k, st := range [scanLanes]uint32{s0, s1, s2, s3} {
					if end := k*seg - overlap*min(k, 1) + j + 1; st >= first && end > k*seg && end <= (k+1)*seg {
						hits = sc.record(t, st, end, hits, found)
					}
				}
			}
		}
		// The last lane goes on to the end.
		s, from = s3, scanLanes*seg
	}
	for i := from; i < len(t); i++ {
		if s = delta[s+uint32(class[t[i]])]; s >= first {
			hits = sc.record(t, s, i+1, hits, found)
		}
	}
	// The automaton finds occurrences by where they end, lane by lane; the rules want them by
	// where they start.
	slices.SortStableFunc(hits, func(a, b redactHit) int { return a.start - b.start })
	return hits
}

// record appends the occurrences ending at end, where the automaton is in state s. A needle that
// is not folded must match the text byte for byte.
func (sc *needleScanner) record(t string, s uint32, end int, hits []redactHit, found *idSet) []redactHit {
	// The words are read here, and each word's needles found gathered in a register and stored
	// once, rather than through an iterator and idSet.add: a store per occurrence into found made
	// the scan of 8 KiB of tool output about 8% slower.
	out := &sc.out[(int(s)-sc.firstOut)/sc.nc]
	for w, m := range out {
		var got uint64
		for ; m != 0; m &= m - 1 {
			bit := bits.TrailingZeros64(m)
			id := w<<6 | bit
			nd := &sc.needles[id]
			start := end - len(nd.text)
			if !nd.folded && t[start:end] != nd.text {
				continue
			}
			got |= 1 << bit
			hits = append(hits, redactHit{start: start, end: end, id: id})
		}
		found[w] |= got
	}
	return hits
}