Files
plugins/packages/agy-cli/test/re2-compile.go
T
yetoneandklarkxy 738fd501d7 agy-cli 0.1.14: tool schema patterns are turned into RE2 or dropped, so an ECMA-262 pattern no longer aborts the turn (klarkxy #946)
Tool input schemas' `pattern` and `patternProperties` keys are translated
to RE2 (re2-pattern.mjs) before they reach agy; a pattern RE2 can't hold
is dropped with a hint, and `additionalProperties: false` is lifted when
every patternProperties key was dropped. Closes #944.

Reviewed head 99093088 (on c27ba5f7), landed onto main a0b1e491 with the
version moved to 0.1.14.
Run at the reviewed head: WSL2 bun test packages/agy-cli 9 of 9,
check.mjs ok; Windows 8 pass, 1 fail, the same smoke red as main (#973).
A fuzz of 1,033 kept patterns compiled every one with Go's regexp
(go1.26.8), against 222 failures before the `[:` fix. Broken by hand:
the `[:` reject, the sanitizePatternFields call, the propertyNames /
contains / if / then / else / unevaluated* recursion and the
additionalProperties lift each turn re2-pattern-unit.mjs red.
Run at this commit (WSL2 Ubuntu 24.04, Bun 1.4.2, temp HOME): bun test
packages/agy-cli 9 of 9 pass; bun scripts/check.mjs agy-cli ok.
Not tried: macOS (mote refused ssh), a real agy.

Closes #946

Co-authored-by: klarkxy <klarkxy@users.noreply.github.com>
2026-11-11 13:34:04 +08:00

73 lines
1.6 KiB
Go

// Helper for re2-pattern-unit.mjs: compile each stdin JSON line's "pattern"
// with Go's regexp (RE2) and print whether it succeeded.
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"regexp"
)
type req struct {
Pattern string `json:"pattern"`
}
type res struct {
Pattern string `json:"pattern"`
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
Match *bool `json:"match,omitempty"`
Input string `json:"input,omitempty"`
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
// Patterns can be long; raise the limit.
scanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)
out := json.NewEncoder(os.Stdout)
for scanner.Scan() {
line := scanner.Bytes()
if len(line) == 0 {
continue
}
var r req
var extra struct {
Pattern string `json:"pattern"`
Input string `json:"input"`
}
if err := json.Unmarshal(line, &extra); err != nil {
fmt.Fprintf(os.Stderr, "bad json: %v\n", err)
os.Exit(1)
}
r.Pattern = extra.Pattern
re, err := regexp.Compile(r.Pattern)
row := res{Pattern: r.Pattern, OK: err == nil}
if err != nil {
row.Error = err.Error()
} else if extra.Input != "" || (err == nil && jsonHasInput(line)) {
matched := re.MatchString(extra.Input)
row.Match = &matched
row.Input = extra.Input
}
if err := out.Encode(row); err != nil {
fmt.Fprintf(os.Stderr, "encode: %v\n", err)
os.Exit(1)
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "stdin: %v\n", err)
os.Exit(1)
}
}
func jsonHasInput(line []byte) bool {
var m map[string]any
if json.Unmarshal(line, &m) != nil {
return false
}
_, ok := m["input"]
return ok
}