Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/funky-jars-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@stackables/bridge-compiler": minor
"@stackables/bridge-graphql": minor
"@stackables/bridge-parser": minor
"@stackables/bridge-core": minor
"@stackables/bridge": minor
---

New internal wire structure with recursive expressions
68 changes: 31 additions & 37 deletions packages/bridge-compiler/src/bridge-asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import {
SELF_MODULE,
type Bridge,
type NodeRef,
type Wire,
} from "@stackables/bridge-core";

const isPull = (w: Wire): boolean => w.sources[0]?.expr.type === "ref";
const wRef = (w: Wire): NodeRef => (w.sources[0].expr as { ref: NodeRef }).ref;

export class BridgeCompilerIncompatibleError extends Error {
constructor(
public readonly operation: string,
Expand Down Expand Up @@ -56,20 +60,22 @@ export function assertBridgeCompilerCompatible(
): void {
const op = `${bridge.type}.${bridge.field}`;

const wires: Wire[] = bridge.wires;

// Pipe-handle trunk keys — block-scoped aliases inside array maps
// reference these; the compiler handles them correctly.
const pipeTrunkKeys = new Set((bridge.pipeHandles ?? []).map((ph) => ph.key));

for (const w of bridge.wires) {
for (const w of wires) {
// User-level alias (Shadow) wires: compiler has TDZ ordering bugs.
// Block-scoped aliases inside array maps wire FROM a pipe-handle tool
// instance (key is in pipeTrunkKeys) and are handled correctly.
if (w.to.module === "__local" && w.to.type === "Shadow") {
if (!("from" in w)) continue;
if (!isPull(w)) continue;
const fromKey =
w.from.instance != null
? `${w.from.module}:${w.from.type}:${w.from.field}:${w.from.instance}`
: `${w.from.module}:${w.from.type}:${w.from.field}`;
wRef(w).instance != null
? `${wRef(w).module}:${wRef(w).type}:${wRef(w).field}:${wRef(w).instance}`
: `${wRef(w).module}:${wRef(w).type}:${wRef(w).field}`;
if (!pipeTrunkKeys.has(fromKey)) {
throw new BridgeCompilerIncompatibleError(
op,
Expand All @@ -79,16 +85,12 @@ export function assertBridgeCompilerCompatible(
continue;
}

if (!("from" in w)) continue;
if (!isPull(w)) continue;

// Catch fallback on pipe wires (expression results) — the catch must
// propagate to the upstream tool, not the internal operator; codegen
// does not handle this yet.
if (
"pipe" in w &&
w.pipe &&
("catchFallback" in w || "catchFallbackRef" in w || "catchControl" in w)
) {
if (w.pipe && w.catch) {
throw new BridgeCompilerIncompatibleError(
op,
"Catch fallback on expression (pipe) wires is not yet supported by the compiler.",
Expand All @@ -97,13 +99,11 @@ export function assertBridgeCompilerCompatible(

// Catch fallback that references a pipe handle — the compiler eagerly
// calls all tools in the catch branch even when the main wire succeeds.
if ("catchFallbackRef" in w && w.catchFallbackRef) {
const ref = w.catchFallbackRef as NodeRef;
if (w.catch && "ref" in w.catch) {
const ref = w.catch.ref;
if (ref.instance != null) {
const refKey = `${ref.module}:${ref.type}:${ref.field}:${ref.instance}`;
if (
bridge.pipeHandles?.some((ph) => ph.key === refKey)
) {
if (bridge.pipeHandles?.some((ph) => ph.key === refKey)) {
throw new BridgeCompilerIncompatibleError(
op,
"Catch fallback referencing a pipe expression is not yet supported by the compiler.",
Expand All @@ -115,16 +115,12 @@ export function assertBridgeCompilerCompatible(
// Catch fallback on wires whose source tool has tool-backed input
// dependencies — the compiler only catch-guards the direct source
// tool, not its transitive dependency chain.
if (
("catchFallback" in w || "catchFallbackRef" in w || "catchControl" in w) &&
"from" in w &&
isToolRef(w.from, bridge)
) {
const sourceTrunk = `${w.from.module}:${w.from.type}:${w.from.field}`;
for (const iw of bridge.wires) {
if (!("from" in iw)) continue;
if (w.catch && isToolRef(wRef(w), bridge)) {
const sourceTrunk = `${wRef(w).module}:${wRef(w).type}:${wRef(w).field}`;
for (const iw of wires) {
if (!isPull(iw)) continue;
const iwDest = `${iw.to.module}:${iw.to.type}:${iw.to.field}`;
if (iwDest === sourceTrunk && isToolRef(iw.from, bridge)) {
if (iwDest === sourceTrunk && isToolRef(wRef(iw), bridge)) {
throw new BridgeCompilerIncompatibleError(
op,
"Catch fallback on wires with tool chain dependencies is not yet supported by the compiler.",
Expand All @@ -136,30 +132,28 @@ export function assertBridgeCompilerCompatible(
// Fallback chains (|| / ??) with tool-backed refs — compiler eagerly
// calls all tools via Promise.all, so short-circuit semantics are lost
// and tool side effects fire unconditionally.
if (w.fallbacks) {
for (const fb of w.fallbacks) {
if (fb.ref && isToolRef(fb.ref, bridge)) {
throw new BridgeCompilerIncompatibleError(
op,
"Fallback chains (|| / ??) with tool-backed sources are not yet supported by the compiler.",
);
}
for (const src of w.sources.slice(1)) {
if (src.expr.type === "ref" && isToolRef(src.expr.ref, bridge)) {
throw new BridgeCompilerIncompatibleError(
op,
"Fallback chains (|| / ??) with tool-backed sources are not yet supported by the compiler.",
);
}
}
}

// Same-cost overdefinition sourced only from tools can diverge from runtime
// tracing/error behavior in current AOT codegen; compile must downgrade.
const toolOnlyOverdefs = new Map<string, number>();
for (const w of bridge.wires) {
for (const w of wires) {
if (
w.to.module !== SELF_MODULE ||
w.to.type !== bridge.type ||
w.to.field !== bridge.field
) {
continue;
}
if (!("from" in w) || !isToolRef(w.from, bridge)) {
if (!isPull(w) || !isToolRef(wRef(w), bridge)) {
continue;
}

Expand Down Expand Up @@ -196,8 +190,8 @@ export function assertBridgeCompilerCompatible(
);
}

for (const w of bridge.wires) {
if (!("from" in w) || w.to.path.length === 0) continue;
for (const w of wires) {
if (!isPull(w) || w.to.path.length === 0) continue;
// Build the full key for this wire target
const fullKey =
w.to.instance != null
Expand Down
Loading
Loading