Skip to content

fix(antigravity): preserve local schema refs - #488

Open
testikun wants to merge 7 commits into
openpi-dev:mainfrom
testikun:codex/issue-465-ref-safety
Open

testikun wants to merge 7 commits into
openpi-dev:mainfrom
testikun:codex/issue-465-ref-safety

Conversation

@testikun

@testikun testikun commented Sep 8, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Fixes #465. Antigravity dropped local schema-reference definitions and then stripped references, leaving result-contract properties empty. The original resolver also interpreted literal $ref keys inside default, examples, const and enum data as schema references and rejected otherwise legal requests before dispatch.

Value

Direct Subagent and Workflow result-contract constraints remain visible in Antigravity declarations. Literal instance data remains compatible with existing conversion, and unsafe schema expansion fails before a request is sent.

Approach

Expand own-property same-document JSON Pointers before the existing CCA sanitizer. Detection, measurement and expansion share schema/map/data position rules: annotation payloads are opaque to reference interpretation but are still charged to output budgets when expansion occurs. Measure depth, nodes and serialized bytes before cloning referenced subtrees; preserve the no-reference fast path, sibling conjunctions and boolean targets. Local Pi validation remains authoritative.

Validation

  • Integrated original contributor head 682a7801 with fixed main f98d6a47, retaining history.
  • bun install --frozen-lockfile: passed, pinned Pi SDK 1.2.0.
  • Ten production buildRequestBody regressions failed before the literal-data repair, then passed.
  • node --test --experimental-strip-types tests/extensions/ai-providers/antigravity.test.ts: 83 passed, including schema/data positions, escaped own pointers, depth/node/byte limits, sparse slots, generated conjunction output shape and no-reference compatibility. Two synthetic-output budget regressions failed before repair; both independent output probes now reject expansion before cloning.
  • Production request-construction smoke used the shared Direct/Workflow structured tool and ordinary tools on Gemini/Claude routes. Captured requests retain enum/required/allOf/false targets and sanitizer spill, remove structural refs/defs, and use parameters; ablation reproduces an empty referenced property and amplification is rejected before materialization.
  • bun run check: passed (format, lint, types).
  • bun run test: passed; Node 2473 passed / 9 skipped / 0 failed, UI 1338 passed across 88 files.
  • Live CCA acceptance of allOf and boolean schemas remains unverified. No authorized no-cost live route was established; no provider calls, tool execution or user credential inspection/modification was used. Local request construction is not provider acceptance.

Two independent final source reviews passed on b11ec04f7cf9ab41b220799483afc9a8c655db23. Fresh final-head CI and independent last-push review remain merge gates. Both reviews retain the precise remote CCA acceptance gap above.

Final-head remote CI passed all 18 checks: 38700817196. Independent GitHub approval still gates merge.

Impact

  • User/model-visible behavior: referenced tool/result constraints are retained; literal annotation data is no longer misinterpreted as schema references.
  • Runtime: bounded provider request conversion; unsafe references fail closed before dispatch.
  • Local validation, provider/auth ownership, tool authority, persisted config/data: unchanged.
  • Compatibility/risk: schemas without schema references keep the existing path. Live CCA allOf/boolean support remains an explicit verification gap. No dependency, provider stack or configuration choice is added.

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Sep 8, 2026
@JS-banana

JS-banana commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

hi,Preserving structured output contracts in CCA tool declarations is a valuable fix.

Sharing two observations regarding traversal overhead and node/byte accounting in expandLocalSchemaRefs:

1. Repeated subtree serialization in visit

In visit:

bytes += Buffer.byteLength(JSON.stringify(value) ?? "");
  • Because JSON.stringify(value) is executed at every node during the recursive walk, every subtree is serialized repeatedly down to the leaf nodes. For a schema with depth $D$ and node count $N$, this produces $O(N \times D)$ serialization overhead.
  • bytes accumulates the sum of every serialized intermediate subtree rather than the byte length of the schema itself (for instance, a schema of ~4 KB can accumulate >20 KB in bytes).
  • This traversal runs in convertTools for all registered tools on each turn, regardless of whether a schema contains $ref.

2. Double traversal of target and MAX_SCHEMA_REF_NODES (512)

In $ref expansion:

const target = visit(pointer(ref), depth + 1, ref) as Record<string, unknown>;
active.delete(ref);
const siblings = Object.fromEntries(
  Object.entries(object).filter(([key]) => key !== "$ref"),
);
return visit({ ...target, ...siblings }, depth + 1, path);
  • target is fully traversed and counted by visit(pointer(ref)).
  • Passing { ...target, ...siblings } into return visit(...) traverses the entire target subtree a second time, incrementing nodes and bytes again for every field.
  • Along with the initial traversal of $defs from the root, the effective node count for referenced types is multiplied. In practice, a schema with ~50–60 fields and 1–2 $ref usages can exceed MAX_SCHEMA_REF_NODES (512) due to the repeated counting and fail closed before sending the request.

@testikun

testikun commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator Author

已推送修复提交 9322d4f(同步最新 main,快进更新源分支)。针对 schema $ref 展开评审意见:移除每个节点的重复 JSON 序列化;展开引用目标后不再二次遍历;保留最终展开 schema 的 256 KiB 上限,并用 60 字段/双引用和超大 schema 回归锁定节点与字节边界。验证:bun run check 通过;完整测试 Node 1469 passed / 1 skipped,Vitest 130 passed;Antigravity 41/41 passed。

@tt-a1i tt-a1i left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at exact head 9322d4f.

Standards

[P1] The 256 KiB bound is checked only after the full expanded graph has been materialized and JSON-stringified. Many references to one large subtree can amplify a small input dramatically before rejection, allowing excessive allocation or OOM during request preparation. Account for emitted expansion bytes before cloning/materialization.

[P2] The resolver applies depth 16 / node 512 limits to every schema, including schemas with no $ref, while the canonical structured-output boundary accepts depth 24 / 10,000 nodes. This regresses previously valid Antigravity tools. Fast-path no-ref schemas or align the limits with the authoritative boundary.

Spec

[P1] $ref siblings shallow-overwrite referenced keywords. A target containing properties / required combined with sibling constraints loses the referenced constraints instead of preserving conjunction.

[P1] A valid boolean-schema target false is object-spread into {}, reversing “reject everything” into unconstrained acceptance.

[P2] JSON Pointer traversal uses inherited property lookup, so #/$defs/toString can resolve Object.prototype.toString instead of failing closed. Use own-property traversal only.

Please add regressions for sibling collisions, boolean targets, inherited/unresolved pointers, and ordinary no-ref compatibility before merge.

@testikun
testikun force-pushed the codex/issue-465-ref-safety branch from 9322d4f to 682a780 Compare September 17, 2026 02:59
@testikun

Copy link
Copy Markdown
Collaborator Author

已更新并重放到当前 main(682a780)。评审项均已处理:无 schema 走原样快速路径;含引用的展开使用 depth 24/node 10000 边界,并在构造引用子树前预算最终 emitted JSON bytes; siblings 通过 allOf 保留合取语义,boolean target 不再被 object spread 改写,JSON Pointer 仅接受自有属性。新增 sibling collision、boolean schema、inherited pointer 和大型无引用 schema 回归。验证:antigravity 44/44 通过。当前没有未解决问题。

@tt-a1i tt-a1i left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审查提交:682a7801bd2d2948a758886ed87e04a6509a6ec1。

合并前需修复已复现的检查失败。

验证范围

Full resolver and regression diff reviewed: byte measurement precedes clone; no-ref fast path, conjunction-preserving allOf, false target, and own-property lookup address previous five findings. 44 Antigravity tests pass at 682a780. Biome formatting fails in both changed TS files.

限制与后续

Provider acceptance of expanded allOf/boolean declarations remains unverified; no live provider call. No new confirmed runtime defect from this pass.

已核对的 CI 失败

Run 35176490697:三个 Node job 被 google-conversion.ts / antigravity.test.ts 格式检查阻断。Web E2E 在 unknown prompt admission 夹具 route.fetch 的 response.json 处报 Response has been disposed;本轮未独立重跑确认原因。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(antigravity): 工具 Schema 转换静默删除 $ref 结果约束

3 participants