Skip to content

Commit 2b95854

Browse files
fix(opencode): close audit-v16 wave M-C anchor-form ref retention
1 parent 7b7e70a commit 2b95854

2 files changed

Lines changed: 122 additions & 2 deletions

File tree

‎packages/opencode/src/mcp/catalog.ts‎

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,49 @@ function resolvePointer(root: unknown, ref: string) {
539539
return true
540540
}
541541

542+
// A plain-name `$ref` (`#name`) resolves against the anchors declared in its own schema
543+
// resource, so an anchor only satisfies a ref sharing its nearest `$id` scope; ajv rejects
544+
// a ref that reaches an anchor in another resource. Collect the anchors that survive in the
545+
// emitted document, keyed by resource. Only positions ajv registers anchors from are
546+
// walked: it ignores an `$anchor` on the top-level schema itself and inside `prefixItems`,
547+
// while an `$anchor` under `enum`/`const`/a custom keyword is instance data (NEW-V14-07).
548+
function collectAnchors(root: unknown) {
549+
const byResource = new Map<string, Set<string>>()
550+
const walk = (value: unknown, kind: SchemaKind, resource: string, atRoot: boolean) => {
551+
if (Array.isArray(value)) {
552+
value.forEach((item) => walk(item, kind, resource, false))
553+
return
554+
}
555+
if (!isPlainObject(value)) return
556+
const nodeResource = kind === "schema" && typeof value.$id === "string" ? value.$id : resource
557+
for (const [key, item] of Object.entries(value)) {
558+
if (!atRoot && kind === "schema" && (key === "$anchor" || key === "$dynamicAnchor")) {
559+
if (typeof item === "string") {
560+
const names = byResource.get(nodeResource)
561+
if (names) names.add(item)
562+
else byResource.set(nodeResource, new Set([item]))
563+
}
564+
continue
565+
}
566+
const childKind: SchemaKind =
567+
kind === "map"
568+
? "schema"
569+
: kind === "instance"
570+
? "instance"
571+
: key === "enum" || key === "const" || key === "default" || key === "examples"
572+
? "instance"
573+
: SCHEMA_MAP_KEYWORDS.has(key)
574+
? "map"
575+
: SCHEMA_VALUE_KEYWORDS.has(key) || SCHEMA_ARRAY_KEYWORDS.has(key)
576+
? "schema"
577+
: "instance"
578+
walk(item, childKind, nodeResource, false)
579+
}
580+
}
581+
walk(root, "schema", "", true)
582+
return byResource
583+
}
584+
542585
// A `$defs` entry can be pruned (depth, node budget, or shared identity) while a `$ref`
543586
// to it survives, leaving an unresolvable local reference that makes providers reject the
544587
// whole manifest. Drop such refs, leaving the containing schema unconstrained. Only
@@ -549,12 +592,15 @@ function dropDanglingRefs(
549592
root: unknown = value,
550593
kind: SchemaKind = "schema",
551594
defs: Map<string, Set<string>> = topLevelDefs(root),
595+
anchors: Map<string, Set<string>> = collectAnchors(root),
596+
resource = "",
552597
) {
553598
if (Array.isArray(value)) {
554-
value.forEach((item) => dropDanglingRefs(item, root, kind, defs))
599+
value.forEach((item) => dropDanglingRefs(item, root, kind, defs, anchors, resource))
555600
return
556601
}
557602
if (!isPlainObject(value)) return
603+
const nodeResource = kind === "schema" && typeof value.$id === "string" ? value.$id : resource
558604
for (const [key, item] of Object.entries(value)) {
559605
if (kind === "schema" && key === "$ref") {
560606
// Only local `#` pointers are resolvable in the emitted document. `urn:`/relative/
@@ -569,6 +615,10 @@ function dropDanglingRefs(
569615
if (!defs.get(def[1])?.has(def[2])) delete value[key]
570616
continue
571617
}
618+
// `#name` is a plain-name anchor reference. Keep it only when an anchor with that
619+
// name survives in the same `$id` resource; a ref to an anchor that was stripped or
620+
// never existed stays dangling and is pruned like any other unresolvable pointer.
621+
if (item.length > 1 && item[1] !== "/" && anchors.get(nodeResource)?.has(item.slice(1))) continue
572622
if (!resolvePointer(root, item)) delete value[key]
573623
continue
574624
}
@@ -582,7 +632,7 @@ function dropDanglingRefs(
582632
: SCHEMA_MAP_KEYWORDS.has(key)
583633
? "map"
584634
: "schema"
585-
dropDanglingRefs(item, root, childKind, defs)
635+
dropDanglingRefs(item, root, childKind, defs, anchors, nodeResource)
586636
}
587637
}
588638

‎packages/opencode/test/mcp/catalog.test.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,4 +660,74 @@ describe("McpCatalog.convertTool bounds untrusted server input", () => {
660660
expect(props.p.$anchor).toBeUndefined()
661661
expect(() => new Ajv2020({ strict: false }).compile(schema)).not.toThrow()
662662
})
663+
664+
test("retains anchor-form $refs that resolve and prunes the ones that do not", () => {
665+
const ajv = new Ajv2020({ strict: false })
666+
667+
const retained = emitted({
668+
type: "object",
669+
$defs: { Kind: { $anchor: "Kind", type: "string", enum: ["a", "b"] } },
670+
properties: { kind: { $ref: "#Kind" } },
671+
})
672+
expect((retained.properties as Record<string, Record<string, unknown>>).kind).toEqual({ $ref: "#Kind" })
673+
expect(() => ajv.compile(retained)).not.toThrow()
674+
675+
const dynamicTarget = emitted({
676+
type: "object",
677+
$defs: { Kind: { $dynamicAnchor: "Kind", type: "string" } },
678+
properties: { kind: { $ref: "#Kind" } },
679+
})
680+
expect((dynamicTarget.properties as Record<string, Record<string, unknown>>).kind).toEqual({ $ref: "#Kind" })
681+
expect(() => ajv.compile(dynamicTarget)).not.toThrow()
682+
683+
const inProperty = emitted({
684+
type: "object",
685+
properties: { def: { $anchor: "A", type: "string" }, use: { $ref: "#A" } },
686+
})
687+
expect((inProperty.properties as Record<string, Record<string, unknown>>).use).toEqual({ $ref: "#A" })
688+
expect(() => ajv.compile(inProperty)).not.toThrow()
689+
690+
const pruned = emitted({
691+
type: "object",
692+
$anchor: "RootA",
693+
properties: {
694+
missing: { $ref: "#Missing" },
695+
ghost: { $ref: "#Ghost" },
696+
prefixed: { $ref: "#Pre" },
697+
rooted: { $ref: "#RootA" },
698+
},
699+
enum: [{ $anchor: "Ghost", type: "string" }],
700+
prefixItems: [{ $anchor: "Pre", type: "string" }],
701+
})
702+
const props = pruned.properties as Record<string, Record<string, unknown>>
703+
expect(props.missing.$ref).toBeUndefined()
704+
expect(props.ghost.$ref).toBeUndefined()
705+
expect(props.prefixed.$ref).toBeUndefined()
706+
expect(props.rooted.$ref).toBeUndefined()
707+
expect(() => ajv.compile(pruned)).not.toThrow()
708+
})
709+
710+
test("scopes anchor-form refs to their $id resource", () => {
711+
const schema = emitted({
712+
type: "object",
713+
$id: "https://example.com/root",
714+
properties: {
715+
cross: { $id: "https://example.com/inner", $anchor: "Outer", type: "string" },
716+
scoped: {
717+
$id: "https://example.com/other",
718+
type: "object",
719+
properties: { def: { $anchor: "Scoped", type: "string" }, use: { $ref: "#Scoped" } },
720+
},
721+
refOther: { $ref: "#Scoped" },
722+
refCross: { $ref: "#Outer" },
723+
},
724+
})
725+
const props = schema.properties as Record<string, Record<string, unknown>>
726+
727+
expect(props.refOther.$ref).toBeUndefined()
728+
expect(props.refCross.$ref).toBeUndefined()
729+
const scoped = props.scoped as Record<string, unknown>
730+
expect((scoped.properties as Record<string, Record<string, unknown>>).use.$ref).toBe("#Scoped")
731+
expect(() => new Ajv2020({ strict: false }).compile(schema)).not.toThrow()
732+
})
663733
})

0 commit comments

Comments
 (0)