Skip to content

refactor(bridge-core): extract gate helpers from resolveWiresAsync#85

Merged
aarne merged 2 commits intomainfrom
copilot/refactor-resolve-wires-async
Mar 5, 2026
Merged

refactor(bridge-core): extract gate helpers from resolveWiresAsync#85
aarne merged 2 commits intomainfrom
copilot/refactor-resolve-wires-async

Conversation

Copy link
Contributor

Copilot AI commented Mar 4, 2026

resolveWiresAsync handled all four resolution layers (Execution, Falsy Gate, Nullish Gate, Catch Gate) inline in a single try/catch, making each layer hard to reason about and impossible to unit test in isolation.

Changes

  • WireWithGates typeExclude<Wire, { value: string }> scopes the gate helpers to non-constant wires only
  • applyFalsyGate — extracts Layer 2a (||): walks falsyFallbackRefs, then falsyControl/falsyFallback
  • applyNullishGate — extracts Layer 2b (??): applies nullishControl/nullishFallbackRef/nullishFallback
  • applyCatchGate — extracts Layer 3: recovers via catchControl/catchFallbackRef/catchFallback, returns undefined when no handler
  • All three helpers are exported for direct unit testing
  • resolveWiresAsync reduced to a clean pipeline:
let value = await evaluateWireSource(ctx, w, pullChain);
value = await applyFalsyGate(ctx, w, value, pullChain);
value = await applyNullishGate(ctx, w, value, pullChain);
if (value != null) return value;
  • 23 new unit tests in packages/bridge-core/test/resolve-wires-gates.test.ts covering each gate helper in isolation via a lightweight mock TreeContext, including pullChain forwarding verification
Original prompt

This section details on the original issue you should resolve

<issue_title>Reduce complexity in resolveWiresAsync</issue_title>
<issue_description>The resolveWiresAsync function handles multiple layers of logic (Execution, Falsy Gate, Nullish Gate, Overdefinition, Catch) in a single massive try/catch loop. This makes the cognitive load very high and testing individual fallback layers difficult.

Extract the fallback gates into a pure reducer or a pipeline pattern. By isolating the evaluation of Layer 2a (Falsy) and Layer 2b (Nullish) into helper functions, you can unit test the gate logic independently of the wire execution.

Something like this would already be a good start

async function resolveWiresAsync(
  ctx: TreeContext,
  wires: Wire[],
  pullChain?: Set<string>,
): Promise<unknown> {
  let lastError: unknown;

  for (const w of wires) {
    // 1. Pre-flight checks
    if (ctx.signal?.aborted) throw new BridgeAbortError();
    if ("value" in w) return coerceConstant(w.value);

    try {
      // 2. The Resolution Pipeline
      let value = await evaluateExecutionLayer(ctx, w, pullChain);
      
      value = await applyFalsyGate(ctx, w, value, pullChain);
      value = await applyNullishGate(ctx, w, value, pullChain);

      // 3. Overdefinition Boundary
      if (value != null) return value; 

    } catch (err: unknown) {
      // 4. Catch Gate Pipeline
      if (isFatalError(err)) throw err;
      
      const recoveredValue = await applyCatchGate(ctx, w, err, pullChain);
      if (recoveredValue != null) return recoveredValue;
      
      lastError = err;
    }
  }

  if (lastError) throw lastError;
  return undefined;
}
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@changeset-bot
Copy link

changeset-bot bot commented Mar 4, 2026

⚠️ No Changeset found

Latest commit: 93c5f03

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Co-authored-by: aarne <82001+aarne@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor resolveWiresAsync to reduce complexity refactor(bridge-core): extract gate helpers from resolveWiresAsync Mar 4, 2026
Copilot finished work on behalf of aarne March 4, 2026 21:27
@cloudflare-workers-and-pages
Copy link

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 93c5f03 Commit Preview URL

Branch Preview URL
Mar 04 2026, 09:28 PM

@aarne aarne marked this pull request as ready for review March 4, 2026 21:32
@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

🐰 Bencher Report

Branchcopilot/refactor-resolve-wires-async
Testbedubuntu-latest
Click to view all benchmark results
BenchmarkLatencyBenchmark Result
nanoseconds (ns)
(Result Δ%)
Upper Boundary
nanoseconds (ns)
(Limit %)
compiled: absolute baseline (passthrough, no tools)📈 view plot
🚷 view threshold
0.00 ns
(+1.41%)Baseline: 0.00 ns
0.00 ns
(97.53%)
compiled: array + tool-per-element 10📈 view plot
🚷 view threshold
0.01 ns
(-1.04%)Baseline: 0.01 ns
0.01 ns
(95.55%)
compiled: array + tool-per-element 100📈 view plot
🚷 view threshold
0.03 ns
(-1.75%)Baseline: 0.03 ns
0.04 ns
(91.77%)
compiled: chained 3-tool fan-out📈 view plot
🚷 view threshold
0.00 ns
(+1.35%)Baseline: 0.00 ns
0.00 ns
(96.12%)
compiled: flat array 10 items📈 view plot
🚷 view threshold
0.01 ns
(+0.31%)Baseline: 0.01 ns
0.01 ns
(96.04%)
compiled: flat array 100 items📈 view plot
🚷 view threshold
0.01 ns
(+0.08%)Baseline: 0.01 ns
0.01 ns
(94.73%)
compiled: flat array 1000 items📈 view plot
🚷 view threshold
0.07 ns
(-2.01%)Baseline: 0.07 ns
0.07 ns
(92.20%)
compiled: nested array 10x10📈 view plot
🚷 view threshold
0.02 ns
(+1.26%)Baseline: 0.02 ns
0.02 ns
(97.79%)
compiled: nested array 20x10📈 view plot
🚷 view threshold
0.03 ns
(+1.93%)Baseline: 0.03 ns
0.03 ns
(98.84%)
compiled: nested array 5x5📈 view plot
🚷 view threshold
0.01 ns
(+0.66%)Baseline: 0.01 ns
0.01 ns
(97.91%)
compiled: short-circuit (overdefinition bypass)📈 view plot
🚷 view threshold
0.00 ns
(+1.10%)Baseline: 0.00 ns
0.00 ns
(96.84%)
compiled: simple chain (1 tool)📈 view plot
🚷 view threshold
0.00 ns
(+1.02%)Baseline: 0.00 ns
0.00 ns
(95.75%)
exec: absolute baseline (passthrough, no tools)📈 view plot
🚷 view threshold
0.00 ns
(+1.22%)Baseline: 0.00 ns
0.00 ns
(81.97%)
exec: array + tool-per-element 10📈 view plot
🚷 view threshold
0.08 ns
(+1.97%)Baseline: 0.07 ns
0.10 ns
(74.23%)
exec: array + tool-per-element 100📈 view plot
🚷 view threshold
0.68 ns
(+0.22%)Baseline: 0.67 ns
0.95 ns
(70.72%)
exec: chained 3-tool fan-out📈 view plot
🚷 view threshold
0.01 ns
(+11.65%)Baseline: 0.01 ns
0.02 ns
(81.37%)
exec: flat array 10 items📈 view plot
🚷 view threshold
0.01 ns
(-13.57%)Baseline: 0.02 ns
0.04 ns
(36.19%)
exec: flat array 100 items📈 view plot
🚷 view threshold
0.07 ns
(-29.03%)Baseline: 0.10 ns
0.35 ns
(20.92%)
exec: flat array 1000 items📈 view plot
🚷 view threshold
0.67 ns
(-36.95%)Baseline: 1.06 ns
4.26 ns
(15.62%)
exec: nested array 10x10📈 view plot
🚷 view threshold
0.12 ns
(-21.05%)Baseline: 0.15 ns
0.42 ns
(28.48%)
exec: nested array 20x10📈 view plot
🚷 view threshold
0.23 ns
(-22.51%)Baseline: 0.30 ns
0.86 ns
(26.98%)
exec: nested array 5x5📈 view plot
🚷 view threshold
0.05 ns
(-12.53%)Baseline: 0.05 ns
0.12 ns
(40.29%)
exec: short-circuit (overdefinition bypass)📈 view plot
🚷 view threshold
0.00 ns
(+4.85%)Baseline: 0.00 ns
0.00 ns
(96.94%)
exec: simple chain (1 tool)📈 view plot
🚷 view threshold
0.01 ns
(+8.97%)Baseline: 0.01 ns
0.01 ns
(83.56%)
parse: large bridge (20 handles x 5 wires)📈 view plot
🚷 view threshold
1.16 ns
(+20.32%)Baseline: 0.97 ns
1.32 ns
(88.24%)
parse: simple bridge📈 view plot
🚷 view threshold
0.04 ns
(+22.77%)Baseline: 0.03 ns
0.04 ns
(87.16%)
🐰 View full continuous benchmarking report in Bencher

@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

🐰 Bencher Report

Branchcopilot/refactor-resolve-wires-async
Testbedubuntu-latest

🚨 10 Alerts

BenchmarkMeasure
Units
ViewBenchmark Result
(Result Δ%)
Upper Boundary
(Limit %)
compiled: absolute baseline (passthrough, no tools)Latency
nanoseconds (ns)
📈 plot
🚷 threshold
🚨 alert (🔔)
0.00 ns
(+7.69%)Baseline: 0.00 ns
0.00 ns
(103.57%)

compiled: array + tool-per-element 10Latency
nanoseconds (ns)
📈 plot
🚷 threshold
🚨 alert (🔔)
0.01 ns
(+5.03%)Baseline: 0.01 ns
0.01 ns
(101.42%)

compiled: chained 3-tool fan-outLatency
nanoseconds (ns)
📈 plot
🚷 threshold
🚨 alert (🔔)
0.00 ns
(+6.52%)Baseline: 0.00 ns
0.00 ns
(101.02%)

compiled: flat array 10 itemsLatency
nanoseconds (ns)
📈 plot
🚷 threshold
🚨 alert (🔔)
0.01 ns
(+7.48%)Baseline: 0.01 ns
0.01 ns
(102.90%)

compiled: nested array 10x10Latency
nanoseconds (ns)
📈 plot
🚷 threshold
🚨 alert (🔔)
0.02 ns
(+8.82%)Baseline: 0.02 ns
0.02 ns
(105.09%)

compiled: nested array 20x10Latency
nanoseconds (ns)
📈 plot
🚷 threshold
🚨 alert (🔔)
0.04 ns
(+8.80%)Baseline: 0.03 ns
0.03 ns
(105.50%)

compiled: nested array 5x5Latency
nanoseconds (ns)
📈 plot
🚷 threshold
🚨 alert (🔔)
0.01 ns
(+6.76%)Baseline: 0.01 ns
0.01 ns
(103.85%)

compiled: short-circuit (overdefinition bypass)Latency
nanoseconds (ns)
📈 plot
🚷 threshold
🚨 alert (🔔)
0.00 ns
(+6.55%)Baseline: 0.00 ns
0.00 ns
(102.07%)

compiled: simple chain (1 tool)Latency
nanoseconds (ns)
📈 plot
🚷 threshold
🚨 alert (🔔)
0.00 ns
(+5.69%)Baseline: 0.00 ns
0.00 ns
(100.18%)

exec: short-circuit (overdefinition bypass)Latency
nanoseconds (ns)
📈 plot
🚷 threshold
🚨 alert (🔔)
0.00 ns
(+14.45%)Baseline: 0.00 ns
0.00 ns
(105.82%)

Click to view all benchmark results
BenchmarkLatencyBenchmark Result
nanoseconds (ns)
(Result Δ%)
Upper Boundary
nanoseconds (ns)
(Limit %)
compiled: absolute baseline (passthrough, no tools)📈 view plot
🚷 view threshold
🚨 view alert (🔔)
0.00 ns
(+7.69%)Baseline: 0.00 ns
0.00 ns
(103.57%)

compiled: array + tool-per-element 10📈 view plot
🚷 view threshold
🚨 view alert (🔔)
0.01 ns
(+5.03%)Baseline: 0.01 ns
0.01 ns
(101.42%)

compiled: array + tool-per-element 100📈 view plot
🚷 view threshold
0.03 ns
(+3.51%)Baseline: 0.03 ns
0.04 ns
(96.68%)
compiled: chained 3-tool fan-out📈 view plot
🚷 view threshold
🚨 view alert (🔔)
0.00 ns
(+6.52%)Baseline: 0.00 ns
0.00 ns
(101.02%)

compiled: flat array 10 items📈 view plot
🚷 view threshold
🚨 view alert (🔔)
0.01 ns
(+7.48%)Baseline: 0.01 ns
0.01 ns
(102.90%)

compiled: flat array 100 items📈 view plot
🚷 view threshold
0.01 ns
(+4.45%)Baseline: 0.01 ns
0.01 ns
(98.86%)
compiled: flat array 1000 items📈 view plot
🚷 view threshold
0.07 ns
(+0.63%)Baseline: 0.07 ns
0.07 ns
(94.68%)
compiled: nested array 10x10📈 view plot
🚷 view threshold
🚨 view alert (🔔)
0.02 ns
(+8.82%)Baseline: 0.02 ns
0.02 ns
(105.09%)

compiled: nested array 20x10📈 view plot
🚷 view threshold
🚨 view alert (🔔)
0.04 ns
(+8.80%)Baseline: 0.03 ns
0.03 ns
(105.50%)

compiled: nested array 5x5📈 view plot
🚷 view threshold
🚨 view alert (🔔)
0.01 ns
(+6.76%)Baseline: 0.01 ns
0.01 ns
(103.85%)

compiled: short-circuit (overdefinition bypass)📈 view plot
🚷 view threshold
🚨 view alert (🔔)
0.00 ns
(+6.55%)Baseline: 0.00 ns
0.00 ns
(102.07%)

compiled: simple chain (1 tool)📈 view plot
🚷 view threshold
🚨 view alert (🔔)
0.00 ns
(+5.69%)Baseline: 0.00 ns
0.00 ns
(100.18%)

exec: absolute baseline (passthrough, no tools)📈 view plot
🚷 view threshold
0.00 ns
(+11.89%)Baseline: 0.00 ns
0.00 ns
(90.62%)
exec: array + tool-per-element 10📈 view plot
🚷 view threshold
0.08 ns
(+1.64%)Baseline: 0.07 ns
0.10 ns
(73.99%)
exec: array + tool-per-element 100📈 view plot
🚷 view threshold
0.68 ns
(+1.38%)Baseline: 0.67 ns
0.95 ns
(71.54%)
exec: chained 3-tool fan-out📈 view plot
🚷 view threshold
0.02 ns
(+16.05%)Baseline: 0.01 ns
0.02 ns
(84.58%)
exec: flat array 10 items📈 view plot
🚷 view threshold
0.01 ns
(-8.03%)Baseline: 0.02 ns
0.04 ns
(38.51%)
exec: flat array 100 items📈 view plot
🚷 view threshold
0.07 ns
(-27.79%)Baseline: 0.10 ns
0.35 ns
(21.29%)
exec: flat array 1000 items📈 view plot
🚷 view threshold
0.73 ns
(-30.89%)Baseline: 1.06 ns
4.26 ns
(17.13%)
exec: nested array 10x10📈 view plot
🚷 view threshold
0.13 ns
(-17.36%)Baseline: 0.15 ns
0.42 ns
(29.81%)
exec: nested array 20x10📈 view plot
🚷 view threshold
0.25 ns
(-17.26%)Baseline: 0.30 ns
0.86 ns
(28.80%)
exec: nested array 5x5📈 view plot
🚷 view threshold
0.05 ns
(-6.86%)Baseline: 0.05 ns
0.12 ns
(42.90%)
exec: short-circuit (overdefinition bypass)📈 view plot
🚷 view threshold
🚨 view alert (🔔)
0.00 ns
(+14.45%)Baseline: 0.00 ns
0.00 ns
(105.82%)

exec: simple chain (1 tool)📈 view plot
🚷 view threshold
0.01 ns
(+14.06%)Baseline: 0.01 ns
0.01 ns
(87.46%)
parse: large bridge (20 handles x 5 wires)📈 view plot
🚷 view threshold
1.03 ns
(+6.95%)Baseline: 0.97 ns
1.32 ns
(78.44%)
parse: simple bridge📈 view plot
🚷 view threshold
0.03 ns
(-10.59%)Baseline: 0.03 ns
0.04 ns
(63.48%)
🐰 View full continuous benchmarking report in Bencher

@aarne aarne merged commit 84b327e into main Mar 5, 2026
11 of 12 checks passed
@aarne aarne deleted the copilot/refactor-resolve-wires-async branch March 5, 2026 06:25
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.

Reduce complexity in resolveWiresAsync

2 participants