Skip to content

Support shadowed and memoized loop-scoped tool aliases in the AOT compiler#109

Merged
aarne merged 5 commits intoscopes_slow_modefrom
copilot/reduce-bridge-compiler-errors
Mar 7, 2026
Merged

Support shadowed and memoized loop-scoped tool aliases in the AOT compiler#109
aarne merged 5 commits intoscopes_slow_modefrom
copilot/reduce-bridge-compiler-errors

Conversation

Copy link
Contributor

Copilot AI commented Mar 7, 2026

The compiler still rejected some bridges that the runtime already handled, including nested array scopes that redeclare the same tool alias and memoized tool handles. This change removes those incompatibilities so those bridges compile directly instead of throwing BridgeCompilerIncompatibleError and falling back.

  • Compiler compatibility

    • Remove the AOT incompatibility gate for shadowed loop-scoped tool handles.
    • Remove the AOT incompatibility gate for memoized tool handles.
  • Codegen fix

    • Assign distinct tool instances to repeated handle bindings for the same tool signature.
    • Support both:
      • repeated bindings backed by distinct wire instances
      • repeated bindings that are shadowed but unused in the wire graph, by allocating synthetic non-colliding instances
    • Emit request-scoped memoization helpers in compiled output so AOT execution preserves runtime memoization semantics.
    • Reuse cached results by tool trunk key plus a stable input fingerprint across normal, lazy, ToolDef-backed, and element-scoped tool calls.
  • Regression coverage

    • Update loop-scoped tool tests to assert shadowed nested tool aliases compile and execute without interpreter fallback.
    • Add an edge-case test for repeated unused shadowed bindings to ensure instance assignment remains collision-free.
    • Update memoized loop-scoped tool tests to assert compiled execution no longer falls back.
    • Add compiler-level tests to verify generated memoization helpers are emitted and that compiled memoized handles reuse cached results, including undefined input cases.
  • Release note

    • Add/update changesets documenting the new compiler support.
  • Performance validation

    • Revalidate the existing compiler-vs-runtime performance test.
    • Run the benchmark suite to confirm compiled execution remains faster than interpreted execution for the measured cases.

Example of the now-supported shadowed pattern:

bridge Query.processCatalog {
  with context as ctx
  with output as o

  o <- ctx.catalog[] as cat {
    with std.httpCall as http
    http.value <- cat.val
    .outer <- http.data

    .children <- cat.children[] as child {
      with std.httpCall as http
      http.value <- child.val
      .inner <- http.data
    }
  }
}

Example of the now-supported memoized pattern:

bridge Query.processCatalog {
  with context as ctx
  with output as o

  o <- ctx.catalog[] as cat {
    with std.httpCall as fetchItem memoize

    fetchItem.value <- cat.id
    .item <- fetchItem.data
  }
}

Before this change, the compiler rejected these shapes because shadowed aliases and memoized handles were treated as unsupported. With this update, the compiler treats shadowed bindings as distinct instances and preserves request-scoped memoization behavior in compiled execution.

[!WARNING]


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@changeset-bot
Copy link

changeset-bot bot commented Mar 7, 2026

🦋 Changeset detected

Latest commit: b2f52c3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@stackables/bridge Patch
@stackables/bridge-core Patch
@stackables/bridge-compiler Patch
@stackables/bridge-parser Patch
@stackables/bridge-graphql Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Co-authored-by: aarne <82001+aarne@users.noreply.github.com>
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 7, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bridge b2f52c3 Commit Preview URL

Branch Preview URL
Mar 07 2026, 12:32 PM

Co-authored-by: aarne <82001+aarne@users.noreply.github.com>
Copilot AI changed the title [WIP] Reduce unsupported bridge compiler errors Support shadowed loop-scoped tool aliases in the AOT compiler Mar 7, 2026
Copilot finished work on behalf of aarne March 7, 2026 11:33
@aarne
Copy link
Contributor

aarne commented Mar 7, 2026

@copilot

implement ALL compiler features ... including memoization ... ensure compiled version is actually faster than the interpreted version

@aarne aarne marked this pull request as ready for review March 7, 2026 12:13
@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2026

🐰 Bencher Report

Branchcopilot/reduce-bridge-compiler-errors
Testbedubuntu-latest

⚠️ WARNING: No Threshold found!

Without a Threshold, no Alerts will ever be generated.

Click here to create a new Threshold
For more information, see the Threshold documentation.
To only post results if a Threshold exists, set the --ci-only-thresholds flag.

Click to view all benchmark results
BenchmarkLatencynanoseconds (ns)
compiled: absolute baseline (passthrough, no tools)📈 view plot
⚠️ NO THRESHOLD
0.00 ns
compiled: array + SYNC tool-per-element 10📈 view plot
⚠️ NO THRESHOLD
0.01 ns
compiled: array + SYNC tool-per-element 100📈 view plot
⚠️ NO THRESHOLD
0.02 ns
compiled: array + tool-per-element 10📈 view plot
⚠️ NO THRESHOLD
0.01 ns
compiled: array + tool-per-element 100📈 view plot
⚠️ NO THRESHOLD
0.03 ns
compiled: chained 3-tool fan-out📈 view plot
⚠️ NO THRESHOLD
0.00 ns
compiled: flat array 10 items📈 view plot
⚠️ NO THRESHOLD
0.01 ns
compiled: flat array 100 items📈 view plot
⚠️ NO THRESHOLD
0.01 ns
compiled: flat array 1000 items📈 view plot
⚠️ NO THRESHOLD
0.07 ns
compiled: nested array 10x10📈 view plot
⚠️ NO THRESHOLD
0.02 ns
compiled: nested array 20x10📈 view plot
⚠️ NO THRESHOLD
0.03 ns
compiled: nested array 5x5📈 view plot
⚠️ NO THRESHOLD
0.01 ns
compiled: short-circuit (overdefinition bypass)📈 view plot
⚠️ NO THRESHOLD
0.00 ns
compiled: simple chain (1 tool)📈 view plot
⚠️ NO THRESHOLD
0.00 ns
exec: absolute baseline (passthrough, no tools)📈 view plot
⚠️ NO THRESHOLD
0.00 ns
exec: array + SYNC tool-per-element 10📈 view plot
⚠️ NO THRESHOLD
0.06 ns
exec: array + SYNC tool-per-element 100📈 view plot
⚠️ NO THRESHOLD
0.57 ns
exec: array + tool-per-element 10📈 view plot
⚠️ NO THRESHOLD
0.08 ns
exec: array + tool-per-element 100📈 view plot
⚠️ NO THRESHOLD
0.70 ns
exec: chained 3-tool fan-out📈 view plot
⚠️ NO THRESHOLD
0.02 ns
exec: flat array 10 items📈 view plot
⚠️ NO THRESHOLD
0.01 ns
exec: flat array 100 items📈 view plot
⚠️ NO THRESHOLD
0.07 ns
exec: flat array 1000 items📈 view plot
⚠️ NO THRESHOLD
0.66 ns
exec: nested array 10x10📈 view plot
⚠️ NO THRESHOLD
0.12 ns
exec: nested array 20x10📈 view plot
⚠️ NO THRESHOLD
0.24 ns
exec: nested array 5x5📈 view plot
⚠️ NO THRESHOLD
0.05 ns
exec: short-circuit (overdefinition bypass)📈 view plot
⚠️ NO THRESHOLD
0.00 ns
exec: simple chain (1 tool)📈 view plot
⚠️ NO THRESHOLD
0.01 ns
parse: large bridge (20 handles x 5 wires)📈 view plot
⚠️ NO THRESHOLD
0.83 ns
parse: simple bridge📈 view plot
⚠️ NO THRESHOLD
0.03 ns
🐰 View full continuous benchmarking report in Bencher

Copilot AI and others added 2 commits March 7, 2026 12:23
Co-authored-by: aarne <82001+aarne@users.noreply.github.com>
Co-authored-by: aarne <82001+aarne@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 7, 2026

@copilot

implement ALL compiler features ... including memoization ... ensure compiled version is actually faster than the interpreted version

Implemented in b2f52c3. Memoized tool handles now compile in AOT as well, including loop-scoped cases, and the old compiler fallback test was converted to direct compiler support. I also added compiler-level memoization coverage, aligned the stable memoization key logic with runtime semantics, and revalidated performance: npx pnpm build, npx pnpm lint, and npx pnpm test all pass, the existing compiler speed test still passes, and the benchmark run showed compiled throughput ahead of runtime across the measured cases (min 1.1×, avg 6.2×).

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.example.com
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node node --experimental-transform-types --conditions source packages/bridge/bench/compiler.bench.ts (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title Support shadowed loop-scoped tool aliases in the AOT compiler Support shadowed and memoized loop-scoped tool aliases in the AOT compiler Mar 7, 2026
Copilot finished work on behalf of aarne March 7, 2026 12:32
@aarne aarne merged commit 3f7be45 into scopes_slow_mode Mar 7, 2026
5 of 8 checks passed
@aarne aarne deleted the copilot/reduce-bridge-compiler-errors branch March 7, 2026 12:36
aarne added a commit that referenced this pull request Mar 7, 2026
* Fix strict nested scope resolution for array mappings

* Add compiler compatibility fallback for nested loop-scoped tools

* Add memoized tool handles with compiler fallback

* Dnd user docs

* fuzzer

* Support shadowed and memoized loop-scoped tool aliases in the AOT compiler (#109)

* Initial plan

* fix: support shadowed loop-scoped tools in AOT compiler

Co-authored-by: aarne <82001+aarne@users.noreply.github.com>

* test: cover shadowed loop-scoped compiler support edge cases

Co-authored-by: aarne <82001+aarne@users.noreply.github.com>

* feat: compile memoized tool handles in AOT

Co-authored-by: aarne <82001+aarne@users.noreply.github.com>

* test: cover compiled memoization edge cases

Co-authored-by: aarne <82001+aarne@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: aarne <82001+aarne@users.noreply.github.com>

* HAndle define correclty

* compiler parity

---------

Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: aarne <82001+aarne@users.noreply.github.com>
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