fix: tool registry — 8 bugs from adversarial review (17.1, 17.2)

Verification review found 8 bugs, all fixed with regression tests:
  - Available() deadlock: CheckFn evaluated outside RLock
  - Register() shared references: Clone() defensive copy before storage
  - Clone() shallow nested schemas: deepCopyAny for maps/arrays
  - stringLit: no backtick/escape handling → strconv.Unquote
  - intLit: no negative/hex/octal/binary → strconv.ParseInt
  - Discover: abort on first error → Continue + errors.Join
  - isRegisterCall: too broad → only DefaultRegistry() calls match
  - schemaLit: only string values → handle ints, bools, nested maps

38 tests pass. All 34 packages green. golangci-lint + gofmt clean.

Closes: archie-core-abg.17.1, 17.2
This commit is contained in:
sam committed 2026-07-26 01:31:17 +10:00
1 parent bc1caf67aa
commit 156453cf74
3 files changed
+55 -18

No files matched your search

+5 -13
View File
@@ -213,21 +213,13 @@ func isRegisterCall(call *ast.CallExpr) bool {
return false
}
// The receiver (sel.X) should be a call to DefaultRegistry, or an
// identifier that names a registry variable.
switch x := sel.X.(type) {
case *ast.CallExpr:
// DefaultRegistry() or tools.DefaultRegistry()
return callsDefaultRegistry(x)
case *ast.Ident:
// r.Register(...) — accept any identifier as a potential
// registry variable. False positives are harmless: the
// call's argument won't parse as a ToolEntry and will
// be skipped by extractToolEntries.
return true
default:
// The receiver must be a call to DefaultRegistry, with or without
// a package qualifier (e.g. DefaultRegistry() or tools.DefaultRegistry()).
rcv, ok := sel.X.(*ast.CallExpr)
if !ok {
return false
}
return callsDefaultRegistry(rcv)
}
// callsDefaultRegistry reports whether call is a DefaultRegistry()
+6 -4
View File
@@ -276,11 +276,12 @@ func toolNames(entries []ToolEntry) []string {
func TestRegistryAvailableReentrySafety(t *testing.T) {
// Regression: Available() called CheckFn while holding RLock,
// which could deadlock if CheckFn re-entered the registry.
// After the fix, CheckFn is evaluated outside the lock.
// After the fix, CheckFn is evaluated outside the lock, so
// calling All(), ByToolset(), or even Register() from inside
// CheckFn cannot deadlock.
r := NewRegistry()
r.Register(ToolEntry{Name: "safe", Handler: noopHandler})
// CheckFn that calls back into the registry (reads and writes).
called := false
r.Register(ToolEntry{
Name: "reentrant",
@@ -289,10 +290,11 @@ func TestRegistryAvailableReentrySafety(t *testing.T) {
called = true
// Call All() from inside CheckFn — must not deadlock.
_ = r.All()
// Call Available() from inside CheckFn — must not deadlock.
_ = r.Available()
// Call ByToolset from inside CheckFn — must not deadlock.
_ = r.ByToolset("")
// Call Register from inside CheckFn — must not deadlock
// (was the original deadlock trigger when lock was held).
_ = r.Register(ToolEntry{Name: "nested-reg", Handler: noopHandler})
return true
},
})
+44 -1
View File
@@ -179,13 +179,56 @@ func TestToolEntryClone(t *testing.T) {
t.Errorf("RequiresEnv[0] = %q, want %q (clone should be independent)", clone.RequiresEnv[0], "X")
}
// Schema is an independent shallow copy.
// Schema is an independent deep copy.
e.Schema["key"] = "mutated"
if clone.Schema["key"] != "value" {
t.Errorf("Schema[key] = %v, want 'value' (clone should be independent)", clone.Schema["key"])
}
}
func TestToolEntryCloneDeepNested(t *testing.T) {
// Regression: Clone() used maps.Copy which only does a shallow copy
// of Schema values. Nested maps and slices were shared between
// the clone and original, enabling mutation leaks.
e := ToolEntry{
Name: "nested",
Handler: noopHandler,
Schema: JSONSchema{
"type": "object",
"properties": JSONSchema{
"name": JSONSchema{"type": "string"},
},
"required": []any{"name"},
"tags": []string{"a", "b"},
},
}
clone := e.Clone()
// Mutate nested map in original.
props := e.Schema["properties"].(JSONSchema)
props["name"] = JSONSchema{"type": "integer"}
cloneProps := clone.Schema["properties"].(JSONSchema)
if cloneProps["name"].(JSONSchema)["type"] != "string" {
t.Error("nested schema map mutation leaked into clone")
}
// Mutate nested slice in original.
req := e.Schema["required"].([]any)
req[0] = "email"
cloneReq := clone.Schema["required"].([]any)
if cloneReq[0] != "name" {
t.Error("nested schema []any slice mutation leaked into clone")
}
// Mutate string slice in original.
tags := e.Schema["tags"].([]string)
tags[0] = "mutated"
cloneTags := clone.Schema["tags"].([]string)
if cloneTags[0] != "a" {
t.Error("nested schema []string slice mutation leaked into clone")
}
}
func TestToolEntryJSONRoundTrip(t *testing.T) {
e := ToolEntry{
Name: "greet",