Skip to content

fix(agent): resolve shared local $defs once in Express catalog instructions - #4492

Merged
gspencergoog merged 1 commit into
a2ui-project:mainfrom
gspencergoog:fix-something/20261201-express-prompt-diamond-dag
Dec 3, 2026
Merged

gspencergoog merged 1 commit into
a2ui-project:mainfrom
gspencergoog:fix-something/20261201-express-prompt-diamond-dag

Conversation

@gspencergoog

@gspencergoog gspencergoog commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Architecture & Contract Brief

  • Root cause: resolveObjectProperties, resolveStructuredProperty, and isProtocolWireEnvelope in the TypeScript Express prompt generator removed each schema from their visited set on the way back up, so a local $defs definition was resolved once per path that reached it.
  • Contract impact: none for acyclic schemas. No public API, schema, or prompt text changes.
  • Verified by: a unit test that counts dereferenceLocalSchema calls at two chain depths. It failed before the fix and passes after.
  • Judgment call: a node inside a reference cycle now gets one cached result, so mutually recursive allOf definitions can resolve differently depending on which one is reached first.
  • Scope: ExpressPromptGenerator.generateCatalogInstructions in @a2ui/agent only.

Summary

ExpressPromptGenerator.generateCatalogInstructions took time exponential in the depth of a chain of local $defs when each definition referenced the next one through more than one oneOf, anyOf, or allOf branch. This PR caches each schema node's result for one call, which makes the work linear. The traversals were added in #4184.

This fixes a downstream issue in Google code.

Reviewer Focus

  • SchemaTraversalCache and the in-progress marker in prompt-generator.ts. An entry is written when a traversal enters a node and overwritten when it leaves. Entries are never removed, which is what removes the repeated work and also what changes results inside reference cycles.
  • The cache key is the schema node before its local $ref is followed. dereferenceLocalSchema returns a new merged object for every $ref node, so two $ref nodes that name one definition stay separate keys and the keys beside each $ref still apply to that node alone.
  • hasWireEnvelopeProperties and directObjectProperties are the existing rules moved into helpers, in the same order.

Changes

  • prompt-generator.ts: replace the path-scoped visited sets of the three traversals with a SchemaTraversalCache that generateComponentSignatures creates once per call. isProtocolWireEnvelope now takes the node before dereferencing and dereferences it itself.
  • prompt-generator.test.ts: add a linear-work test for oneOf, anyOf, and allOf chains, and a test that a definition shared by two branches keeps its properties on the second visit.
  • CHANGELOG.md: add an Unreleased entry.

Impact & Risks

  • Generating catalog instructions for an allOf chain took 0.8 s at depth 12, 4.3 s at depth 14, and 21.6 s at depth 16. After the change, depth 30 takes about 1 ms for each of the three keywords.
  • Output is unchanged for acyclic schemas. The golden-file tests and the Express prompt conformance suite pass unchanged.
  • For schemas with mutually recursive allOf definitions, the listed map keys and the wire-envelope classification of a definition inside the cycle can differ from before, and depend on which definition the traversal enters first. Self-referencing definitions resolve as before.

Testing

  • yarn vitest run tests/unit tests/conformance in typescript/agents/a2ui_agent: 53 files and 2038 tests pass.
  • Before the fix, the new linear-work cases failed with 8217 calls against a limit of 423 (oneOf and anyOf) and 106523 calls against a limit of 2733 (allOf).
  • eslint on the two changed TypeScript files, prettier --check on the three changed files, and ./scripts/fix_licenses.py --check pass.
  • scripts/e2e_test.sh was not run.

Pre-launch Checklist

One time:

For this PR:

  • I have updated the relevant CHANGELOG.md file.
  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • If my branch is on a fork, I have verified that scripts/e2e_test.sh passes.

@gspencergoog
gspencergoog force-pushed the fix-something/20261201-express-prompt-diamond-dag branch from f35b07b to 7a542c6 Compare December 1, 2026 19:21

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a caching mechanism (SchemaTraversalCache) to ExpressPromptGenerator to optimize the resolution of local #/$defs/... schema definitions. Previously, resolving deeply nested or shared definitions through multiple branches (such as oneOf, anyOf, or allOf) resulted in exponential time complexity. By caching traversal results, these definitions are now resolved in linear time. The changes also include comprehensive unit tests to verify the performance improvement and correctness of the schema resolution. There are no review comments to address, and the implementation looks solid.

@gspencergoog
gspencergoog requested a review from ditman December 1, 2026 21:11
@gspencergoog
gspencergoog marked this pull request as ready for review December 1, 2026 21:11

@ditman ditman 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.

🤖 LGTM, just needs a quick rebase/update since #4270 landed and enforces @return instead of @returns in ESLint (left two quick suggestions inline).

…ctions

`ExpressPromptGenerator` listed a property's map keys with three
traversals that removed each schema from their visited set on the way
back up, so a local definition reached through several `oneOf`, `anyOf`,
or `allOf` branches was resolved once per path. A chain of definitions
that each reference the next one twice took time exponential in its
length: 21.6 s for an `allOf` chain of depth 16.

Cache each schema node's result for one `generateComponentSignatures`
call instead. A node reached again while it is being resolved still
yields no result, which is what stops reference cycles. Output for
acyclic schemas is unchanged.
@gspencergoog
gspencergoog force-pushed the fix-something/20261201-express-prompt-diamond-dag branch from 7a542c6 to f43daae Compare December 3, 2026 17:24
@gspencergoog
gspencergoog enabled auto-merge (squash) December 3, 2026 17:26
@gspencergoog
gspencergoog merged commit 785f6c1 into a2ui-project:main Dec 3, 2026
57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants