Files
magpie/internal/capre/capre_test.go
T
yetone dae104bfac capre: a length check, not an unrolled repeat, holds nine patterns
Go's regexp unrolls a bounded repeat into that many copies of its
class: [A-Za-z0-9.-]{0,254} (the session terminal's bundle id) was
89kB of program, kept from start to exit, and Codex's item-id refusal
`.{0,1000}?` 88kB. Measured one by one over magpie's 540 package-level
patterns (1.5MB in all), the nine largest bounded ones were ~440kB.

capre.MustCompile(expr, max) is the pattern with * or +, matching only
a string of max characters at most (rune count, so the (?i) Kelvin
sign and invalid bytes count as the regexp counted them). The nine:
settings' bundle id, library's name and agent id (now name@wsl:distro
cut in two), Anthropic tool names, the trace page's name and find,
access's MCP server, OpenClaw's profile and Codex's provider id. The
item-id refusal is a function: the tail within 1000 characters of a
head.

Run on mote (go1.26.8):
- go test ./internal/capre ./internal/access ./internal/settings
  ./internal/library ./internal/sessions ./internal/agent ./internal/gui
  . : ok; ./internal/gateway -run 'ItemID|Undeclared|ToolName|Refus': ok
- TestCappedMatchesBounded holds each old pattern against its new one
  over 14 units x 19 lengths (0..300, at each bound +-1); with the
  length check taken out it is red (1586 mismatches).
  TestItemIDRefusalAsTheRegexp holds the function against the old
  regexp at 998..1002 characters of x, é, 日, \xff, \n; with 1001 it is
  red. TestCappedIsSmall: 1304 bytes held, 49680 bounded.
- go vet native, GOOS=linux and GOOS=windows over the touched packages
  (gui natively: its Linux build needs cgo); gofmt -l clean.
- Real build, GODEBUG=inittrace=1: magpie's own inits allocate
  11.93MB -> 10.61MB.
- Idle on the real-account copy, 180s heap: regexp programs (sampled)
  4.54MB -> 3.01MB.

Not tried: the full ./... suite; the gateway package beyond the run
above.
2026-11-09 21:47:24 +08:00

63 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package capre
import (
"regexp"
"runtime"
"strings"
"testing"
)
// The bounded patterns magpie had, each beside what replaced it.
var pairs = []struct {
bounded, open string
max int
}{
{`^[A-Za-z0-9][A-Za-z0-9.-]{0,254}$`, `^[A-Za-z0-9][A-Za-z0-9.-]*$`, 255},
{`^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$`, `^[A-Za-z0-9][A-Za-z0-9_-]*$`, 64},
{`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`, `^[A-Za-z0-9][A-Za-z0-9._-]*$`, 64},
{`^[a-zA-Z0-9_-]{1,128}$`, `^[a-zA-Z0-9_-]+$`, 128},
{`^[A-Za-z0-9._@:/ -]{1,80}$`, `^[A-Za-z0-9._@:/ -]+$`, 80},
{`^[^\x00-\x1f\x7f]{0,100}$`, `^[^\x00-\x1f\x7f]*$`, 100},
{`(?i)^[a-z0-9][a-z0-9_-]{0,63}$`, `(?i)^[a-z0-9][a-z0-9_-]*$`, 64},
{`^[0-9A-Za-z_.-]{1,64}$`, `^[0-9A-Za-z_.-]{1,}$`, 64},
}
func TestCappedMatchesBounded(t *testing.T) {
var in []string
for _, unit := range []string{"a", "Z", "9", "-", ".", "_", " ", "é", "K", "ſ", "\x00", "\xff", "@", "日"} {
for _, n := range []int{0, 1, 2, 63, 64, 65, 79, 80, 81, 99, 100, 101, 127, 128, 129, 254, 255, 256, 300} {
in = append(in, strings.Repeat(unit, n), "a"+strings.Repeat(unit, n), strings.Repeat(unit, n)+"a")
}
}
for _, p := range pairs {
want, got := regexp.MustCompile(p.bounded), MustCompile(p.open, p.max)
for _, s := range in {
if w, g := want.MatchString(s), got.MatchString(s); w != g {
t.Errorf("%s on %d bytes of %q: %v, want %v", p.open, len(s), s[:min(len(s), 4)], g, w)
}
}
}
}
// TestCappedIsSmall: the reason for the package. A program of the
// bounded class is tens of kB; held to a length it is a fraction of one.
func TestCappedIsSmall(t *testing.T) {
heap := func(f func() any) int64 {
var a, b runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&a)
v := f()
runtime.GC()
runtime.ReadMemStats(&b)
runtime.KeepAlive(v)
return max(0, int64(b.HeapAlloc)-int64(a.HeapAlloc))
}
p := pairs[0]
bounded := heap(func() any { return regexp.MustCompile(p.bounded) })
capped := heap(func() any { return MustCompile(p.open, p.max) })
if capped*10 > bounded {
t.Fatalf("held to a length: %d bytes, bounded %d", capped, bounded)
}
t.Logf("held to a length: %d bytes, bounded %d", capped, bounded)
}