Skip to content
Closed
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
10 changes: 5 additions & 5 deletions packages/bridge-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"build"
],
"scripts": {
"disabled___build": "tsc -p tsconfig.build.json",
"disabled___lint:types": "tsc -p tsconfig.json",
"disabled___test": "node --experimental-transform-types --test test/*.test.ts",
"disabled___fuzz": "node --experimental-transform-types --test test/*.fuzz.ts",
"disabled___prepack": "pnpm build"
"build": "tsc -p tsconfig.build.json",
"lint:types": "tsc -p tsconfig.json",
"test": "node --experimental-transform-types --test test/*.test.ts",
"fuzz": "node --experimental-transform-types --test test/*.fuzz.ts",
"prepack": "pnpm build"
},
"dependencies": {
"@stackables/bridge-core": "workspace:*",
Expand Down
229 changes: 96 additions & 133 deletions packages/bridge-compiler/src/bridge-asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import {
SELF_MODULE,
type Bridge,
type NodeRef,
type Wire,
type Statement,
type Expression,
} 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 @@ -54,117 +52,115 @@ function isToolRef(ref: NodeRef, bridge: Bridge): boolean {
return true;
}

export function assertBridgeCompilerCompatible(
bridge: Bridge,
requestedFields?: string[],
): 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 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 (!isPull(w)) continue;
const fromKey =
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,
"Alias (shadow) wires are not yet supported by the compiler.",
);
}
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 (w.pipe && w.catch) {
throw new BridgeCompilerIncompatibleError(
op,
"Catch fallback on expression (pipe) wires is not yet supported by the compiler.",
function isToolBackedExpr(expr: Expression, bridge: Bridge): boolean {
switch (expr.type) {
case "ref":
return isToolRef(expr.ref, bridge);
case "pipe":
return true;
case "literal":
case "control":
return false;
case "ternary":
return (
isToolBackedExpr(expr.cond, bridge) ||
isToolBackedExpr(expr.then, bridge) ||
isToolBackedExpr(expr.else, bridge)
);
}
case "and":
case "or":
return (
isToolBackedExpr(expr.left, bridge) ||
isToolBackedExpr(expr.right, bridge)
);
case "binary":
return (
isToolBackedExpr(expr.left, bridge) ||
isToolBackedExpr(expr.right, bridge)
);
case "unary":
return isToolBackedExpr(expr.operand, bridge);
case "concat":
return expr.parts.some((p) => isToolBackedExpr(p, bridge));
case "array":
return isToolBackedExpr(expr.source, bridge);
}
}

interface OutputWireInfo {
targetPath: string;
primaryIsToolRef: boolean;
}

// Catch fallback that references a pipe handle — the compiler eagerly
// calls all tools in the catch branch even when the main wire succeeds.
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)) {
throw new BridgeCompilerIncompatibleError(
op,
"Catch fallback referencing a pipe expression is not yet supported by the compiler.",
);
function collectOutputWires(
body: Statement[],
bridge: Bridge,
): OutputWireInfo[] {
const selfKey = `${SELF_MODULE}:${bridge.type}:${bridge.field}`;
const results: OutputWireInfo[] = [];

function walk(stmts: Statement[], pathPrefix: string[]) {
for (const s of stmts) {
if (s.kind === "wire") {
const tk = `${s.target.module}:${s.target.type}:${s.target.field}`;
if (tk === selfKey && s.target.instance == null) {
const fullPath = [...pathPrefix, ...s.target.path];
const primary = s.sources[0]!;
results.push({
targetPath: fullPath.join("."),
primaryIsToolRef:
primary.expr.type === "ref" &&
isToolRef(primary.expr.ref, bridge),
});
}
}
if (s.kind === "scope") {
walk(s.body, [...pathPrefix, ...s.target.path]);
}
}
}
walk(body, []);
return results;
}

// 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 (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(wRef(iw), bridge)) {
throw new BridgeCompilerIncompatibleError(
op,
"Catch fallback on wires with tool chain dependencies is not yet supported by the compiler.",
);
export function assertBridgeCompilerCompatible(
bridge: Bridge,
requestedFields?: string[],
): void {
if (!bridge.body) return;

const op = `${bridge.type}.${bridge.field}`;

// Check fallback chains with tool-backed refs
function checkFallbackChains(stmts: Statement[]) {
for (const s of stmts) {
if (s.kind === "wire" || s.kind === "alias" || s.kind === "spread") {
for (const src of s.sources.slice(1)) {
if (isToolBackedExpr(src.expr, bridge)) {
throw new BridgeCompilerIncompatibleError(
op,
"Fallback chains (|| / ??) with tool-backed sources are not yet supported by the compiler.",
);
}
}
}
}

// 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.
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.",
);
if (s.kind === "scope") {
checkFallbackChains(s.body);
}
}
}
checkFallbackChains(bridge.body);

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

const outputPath = w.to.path.join(".");
if (!matchesRequestedField(outputPath, requestedFields)) {
continue;
}

for (const w of outputWires) {
if (!w.primaryIsToolRef) continue;
if (!matchesRequestedField(w.targetPath, requestedFields)) continue;
toolOnlyOverdefs.set(
outputPath,
(toolOnlyOverdefs.get(outputPath) ?? 0) + 1,
w.targetPath,
(toolOnlyOverdefs.get(w.targetPath) ?? 0) + 1,
);
}

Expand All @@ -176,37 +172,4 @@ export function assertBridgeCompilerCompatible(
);
}
}

// Pipe handles with extra bridge wires to the same tool — the compiler
// treats pipe forks as independent tool calls, so bridge wires that set
// fields on the main tool trunk are not merged into the fork's input.
if (bridge.pipeHandles && bridge.pipeHandles.length > 0) {
const pipeHandleKeys = new Set<string>();
const pipedToolNames = new Set<string>();
for (const ph of bridge.pipeHandles) {
pipeHandleKeys.add(ph.key);
pipedToolNames.add(
`${ph.baseTrunk.module}:${ph.baseTrunk.type}:${ph.baseTrunk.field}`,
);
}

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
? `${w.to.module}:${w.to.type}:${w.to.field}:${w.to.instance}`
: `${w.to.module}:${w.to.type}:${w.to.field}`;
// Skip wires that target the pipe handle itself (fork input)
if (pipeHandleKeys.has(fullKey)) continue;
// Check if this wire targets a tool that also has pipe calls
const toolName = `${w.to.module}:${w.to.type}:${w.to.field}`;
if (pipedToolNames.has(toolName)) {
throw new BridgeCompilerIncompatibleError(
op,
"Bridge wires that set fields on a tool with pipe calls are not yet supported by the compiler.",
);
}
}
}
}
Loading
Loading