From 156453cf7469fb8f665e8d0411f6648aea08b68a Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 26 Jul 2026 01:31:17 +1000 Subject: [PATCH] =?UTF-8?q?fix:=20tool=20registry=20=E2=80=94=208=20bugs?= =?UTF-8?q?=20from=20adversarial=20review=20(17.1,=2017.2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- internal/tools/registry.go | 18 ++++--------- internal/tools/registry_test.go | 10 ++++--- internal/tools/toolentry_test.go | 45 +++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 18 deletions(-) diff --git a/internal/tools/registry.go b/internal/tools/registry.go index 9bce0577f..4e28bcd14 100644 --- a/internal/tools/registry.go +++ b/internal/tools/registry.go @@ -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() diff --git a/internal/tools/registry_test.go b/internal/tools/registry_test.go index 2834928bc..f22b865d8 100644 --- a/internal/tools/registry_test.go +++ b/internal/tools/registry_test.go @@ -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 }, }) diff --git a/internal/tools/toolentry_test.go b/internal/tools/toolentry_test.go index b9cdf3d68..1f7c6c1ff 100644 --- a/internal/tools/toolentry_test.go +++ b/internal/tools/toolentry_test.go @@ -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",