gateway: plainSchema resolves a $ref into properties (#4561)

A $ref that points inside the schema ("#/properties/func/properties/
triggers/items", as dsh's tool parameters carry one) is a local JSON
pointer like a "#/$defs/x" one, but plainSchema read a ref only by its
$defs or definitions name: the pointer's last segment named nothing
there, and the field it stood for went out as a bare {type: object} on
the Antigravity path, the tool losing that field's shape.

Any local pointer is resolved now, where it names; a self-reference —
a ref to the site or one of its containers — stops at the target's
type alone, as the plugin's googleSchema has it.

A test with the error's path fails on the old code.

Reviewed and applied from #4561 (ggbdpq, branch head ab39ddcbc; the
patch's SHA-256 matches the issue). A review fix follows in the next
commit: selfRef matched a pointer as a string prefix.

Broken by hand: plainSchema back at main with the test kept, and
TestPlainSchemaResolvesPointerIntoProperties fails ("again" goes out as a
bare {"type":"object"}).

Run (on mote, through mote-run):
- Full `go test -tags nogui -count=1 -timeout 45m ./...` with this stack
  on c30d3b7b6: 82 packages ok. internal/gui was red only on
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514, SearchAll from
  b09349d17) and TestColdTurnsTaken (#4510). Both are filed, and neither
  touches this stack.
- Rebased onto 29d0ab39f, which changed internal/gateway since then:
  gofmt -l, go vet (darwin, GOOS=linux -tags nogui, GOOS=windows) and
  go test -count=1 ./internal/gateway ./internal/usage ./internal/provider
  ./internal/sessions all ok at the pushed head. go build ./... ok for
  darwin, linux (nogui) and windows.
- go test -race of the gateway tests this stack adds or touches, on
  the stack at fb29cd30f: ok.
This commit is contained in:
ggbdpq
2026-10-31 23:35:27 +08:00
committed by yetone
parent 29d0ab39fb
commit abaf2c4e8f
2 changed files with 146 additions and 14 deletions
+97 -14
View File
@@ -819,17 +819,89 @@ func plainSchema(raw json.RawMessage) json.RawMessage {
}
}
}
// localRef is the schema a local $ref names in root: a JSON pointer
// ("#", "#/properties/func/properties/triggers/items", as dsh's tool
// parameters carry one, #4561) or, when it is none, a $defs or
// definitions name; ok false where it names nothing here
localRef := func(ref string) (any, bool) {
if rest, ok := strings.CutPrefix(ref, "#/"); ok {
cur := any(root)
for _, tok := range strings.Split(rest, "/") {
tok = strings.ReplaceAll(strings.ReplaceAll(tok, "~1", "/"), "~0", "~")
switch m := cur.(type) {
case map[string]any:
cur = m[tok]
case []any:
n, err := strconv.Atoi(tok)
if err != nil || n < 0 || n >= len(m) {
return nil, false
}
cur = m[n]
default:
return nil, false
}
if cur == nil {
return nil, false
}
}
return cur, true
}
if ref == "#" {
return root, true
}
d, ok := defs[ref[strings.LastIndex(ref, "/")+1:]]
return d, ok
}
// resolve is a member as its local $ref names it, nil when it is no
// schema
resolve := func(v any) map[string]any {
m, _ := v.(map[string]any)
if ref, ok := m["$ref"].(string); ok {
m, _ = defs[ref[strings.LastIndex(ref, "/")+1:]].(map[string]any)
if d, ok := localRef(ref); ok {
m, _ = d.(map[string]any)
} else {
m = nil
}
}
return m
}
var walk func(v any, depth int) any
walk = func(v any, depth int) any {
// selfRef is whether a local $ref names the site it sits at or one
// of its containers: in a tree, a reference to an ancestor is a
// self-reference
selfRef := func(ref, path string) bool {
if strings.HasPrefix(ref, "#") {
return strings.HasPrefix(path, ref[1:])
}
return false
}
// refPath is the path a $ref's target is walked at: its own pointer
// where the ref is a JSON pointer, the site's where it is none (a
// $defs name, whose place the pointer doesn't say)
refPath := func(ref, path string) string {
if strings.HasPrefix(ref, "#") {
return ref[1:]
}
return path
}
// expanding is the $ref chain being walked, a self-reference caught
// before it recurses
expanding := map[string]bool{}
// schemaTypeOf is the type a schema's own shape says, what a
// self-reference stops at: the target's type alone
schemaTypeOf := func(m map[string]any) string {
if t, ok := m["type"].(string); ok {
return t
}
if m["properties"] != nil {
return "object"
}
if m["items"] != nil {
return "array"
}
return "object"
}
var walk func(v any, depth int, path string) any
walk = func(v any, depth int, path string) any {
switch x := v.(type) {
case bool:
// a boolean schema, which JSON Schema has wherever a schema
@@ -846,7 +918,7 @@ func plainSchema(raw json.RawMessage) json.RawMessage {
case []any:
out := make([]any, len(x))
for i, e := range x {
out[i] = walk(e, depth)
out[i] = walk(e, depth, path)
}
return out
case map[string]any:
@@ -854,9 +926,19 @@ func plainSchema(raw json.RawMessage) json.RawMessage {
return map[string]any{"type": "object"}
}
if ref, ok := x["$ref"].(string); ok {
name := ref[strings.LastIndex(ref, "/")+1:]
if d, ok := defs[name]; ok {
return walk(d, depth+1)
if d, found := localRef(ref); found {
if expanding[ref] || selfRef(ref, path) {
// a self-reference stops at the target's
// type alone
if m, ok := d.(map[string]any); ok {
return map[string]any{"type": schemaTypeOf(m)}
}
return map[string]any{"type": "object"}
}
expanding[ref] = true
out := walk(d, depth+1, refPath(ref, path))
delete(expanding, ref)
return out
}
return map[string]any{"type": "object"}
}
@@ -875,7 +957,7 @@ func plainSchema(raw json.RawMessage) json.RawMessage {
// alone, a member that only requires ({"required":["a"]})
// has no properties to keep its names for
required = append(required, requiredNames(resolve(e)["required"])...)
if m, ok := walk(e, depth+1).(map[string]any); ok {
if m, ok := walk(e, depth+1, path+"/allOf").(map[string]any); ok {
for k, v := range m {
if k == "required" {
continue
@@ -898,7 +980,7 @@ func plainSchema(raw json.RawMessage) json.RawMessage {
if len(required) > 0 {
merged["required"] = distinctNames(required)
}
return walk(merged, depth+1)
return walk(merged, depth+1, path)
}
if (x["anyOf"] != nil || x["oneOf"] != nil) && ownShape(x) {
// a schema with its own type or properties beside an anyOf
@@ -908,7 +990,7 @@ func plainSchema(raw json.RawMessage) json.RawMessage {
// for the whole, sent it as {"type":"string"}; Claude on
// Antigravity answered 400 "input_schema.type: Input should
// be 'object'" (#4046, imuxin)
return walk(foldUnion(x, resolve), depth+1)
return walk(foldUnion(x, resolve), depth+1, path)
}
// anyOf / oneOf with nothing of its own beside it: the first
// member that isn't null, nullable
@@ -926,7 +1008,7 @@ func plainSchema(raw json.RawMessage) json.RawMessage {
}
out := map[string]any{}
if pick != nil {
if m, ok := walk(pick, depth+1).(map[string]any); ok {
if m, ok := walk(pick, depth+1, path+"/"+k).(map[string]any); ok {
out = m
}
}
@@ -982,14 +1064,15 @@ func plainSchema(raw json.RawMessage) json.RawMessage {
// a property whose schema is false is one the
// call may not have: it is left out, and so is
// its name in required
if w := walk(pv, depth+1); w != nil {
at := strings.ReplaceAll(strings.ReplaceAll(pk, "~", "~0"), "/", "~1")
if w := walk(pv, depth+1, path+"/properties/"+at); w != nil {
props[pk] = w
}
}
}
out[k] = props
case "items":
if w := walk(v, depth+1); w != nil {
if w := walk(v, depth+1, path+"/items"); w != nil {
out[k] = w
} else if x["maxItems"] == nil {
// items false: an array with nothing in it
@@ -1074,7 +1157,7 @@ func plainSchema(raw json.RawMessage) json.RawMessage {
}
return v
}
b, err := json.Marshal(walk(root, 0))
b, err := json.Marshal(walk(root, 0, ""))
if err != nil {
return raw
}
+49
View File
@@ -6,6 +6,55 @@ import (
"testing"
)
// A $ref that points inside properties ("#/properties/…"), as dsh's tool
// parameters carry one, is a local JSON pointer like any other: resolved
// where it names, not written out as a bare object (#4561), and a
// self-reference stops at the target's type alone.
func TestPlainSchemaResolvesPointerIntoProperties(t *testing.T) {
in := `{"type":"object",
"properties":{
"func":{"type":"object","properties":{
"name":{"type":"string"},
"triggers":{"type":"array","items":{
"type":"object",
"properties":{
"kind":{"type":"string","enum":["command","prompt"]},
"self":{"$ref":"#/properties/func/properties/triggers/items"}
},
"required":["kind"]
}}
}},
"again":{"$ref":"#/properties/func/properties/triggers/items"}
}}`
want := `{"type":"object",
"properties":{
"func":{"type":"object","properties":{
"name":{"type":"string"},
"triggers":{"type":"array","items":{
"type":"object",
"properties":{
"kind":{"type":"string","enum":["command","prompt"]},
"self":{"type":"object"}
},
"required":["kind"]
}}
}},
"again":{"type":"object","properties":{
"kind":{"type":"string","enum":["command","prompt"]},
"self":{"type":"object"}
},"required":["kind"]}
}}`
var got, exp any
if err := json.Unmarshal(plainSchema(json.RawMessage(in)), &got); err != nil {
t.Fatal(err)
}
json.Unmarshal([]byte(want), &exp)
if !reflect.DeepEqual(got, exp) {
b, _ := json.Marshal(got)
t.Errorf("got %s", b)
}
}
// Antigravity refuses a function declaration over any keyword outside its
// schema subset (#187: exclusiveMinimum from zcode's and dsh's tools).
func TestPlainSchemaKeepsOnlyWhatAntigravityTakes(t *testing.T) {