behavior-contracts 0.11.11 → 0.11.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/behavior.d.ts +26 -0
- package/dist/behavior.d.ts.map +1 -1
- package/dist/behavior.js +33 -5
- package/dist/behavior.js.map +1 -1
- package/dist/generator/emit-typed-native-go.d.ts.map +1 -1
- package/dist/generator/emit-typed-native-go.js +111 -23
- package/dist/generator/emit-typed-native-go.js.map +1 -1
- package/dist/generator/emit-typed-native-rust.d.ts.map +1 -1
- package/dist/generator/emit-typed-native-rust.js +110 -25
- package/dist/generator/emit-typed-native-rust.js.map +1 -1
- package/dist/type-gate.d.ts.map +1 -1
- package/dist/type-gate.js +59 -19
- package/dist/type-gate.js.map +1 -1
- package/package.json +1 -1
|
@@ -180,7 +180,7 @@ function resolveLeafSymbol(component, leaf, specSymbol) {
|
|
|
180
180
|
* `WireRow.entries` is a public field (the shared crate exports it), so cross-crate construction is direct.
|
|
181
181
|
*/
|
|
182
182
|
function rustPayloadRow(entries) {
|
|
183
|
-
return `WireRow { entries:
|
|
183
|
+
return `WireRow { entries: ${entries} }`;
|
|
184
184
|
}
|
|
185
185
|
/**
|
|
186
186
|
* rustPayloadBatch — the BATCHED/fanout payload: ONE WireRow `{items:[<per-element ports rows>…]}` over an
|
|
@@ -684,8 +684,9 @@ function nativeResolveHead(comp, plan, typedNodes, isPrior, opts) {
|
|
|
684
684
|
/** compilePortNode — lower a port expression to its native ports-struct field via the runtime-free
|
|
685
685
|
* native-expr compiler: the ONE port lowering. The port's field TYPE is the compiled `ref`, its
|
|
686
686
|
* initializer the compiled `expr` — derived from a SINGLE lowering, so they cannot disagree. A port that
|
|
687
|
-
* cannot lower FAILS CLOSED; a FALLIBLE lowering
|
|
688
|
-
*
|
|
687
|
+
* cannot lower FAILS CLOSED; a FALLIBLE lowering hoists statements whose early exit belongs to the
|
|
688
|
+
* function-or-block they land in: the runner (`?`) or the payload-entry block (`opts.portsLabel`, #238).
|
|
689
|
+
* Reuses the same nativeResolveHead cond/guard/port share, with `ownArrays` (a port owns). */
|
|
689
690
|
function compilePortNode(node, comp, plan, typedNodes, isPrior, where, expected, opts) {
|
|
690
691
|
// #173: the port boundary is declared-type-driven. `expected` is the referenced leaf's declared port
|
|
691
692
|
// type (node.portSchemas[port] → inputPortTypeRef). It is MANDATORY (the guard requires portSchemas on
|
|
@@ -700,7 +701,7 @@ function compilePortNode(node, comp, plan, typedNodes, isPrior, where, expected,
|
|
|
700
701
|
// #170: opts.narrowed carries the gate/guard-proven-non-null ref paths for THIS node's port scope, so an
|
|
701
702
|
// opt-in-required operand it proves non-null narrows instead of failing closed (the ONE narrowing site).
|
|
702
703
|
// #173: lower to the DECLARED port type via compilePortTo → compileTo (no element-kind inference).
|
|
703
|
-
c = new NativeExprCompiler(makeRustExprBackend(resolveHead, plan), opts?.narrowed).compilePortTo(node, expected, resolveDecl);
|
|
704
|
+
c = new NativeExprCompiler(makeRustExprBackend(resolveHead, plan, opts?.portsLabel), opts?.narrowed).compilePortTo(node, expected, resolveDecl);
|
|
704
705
|
}
|
|
705
706
|
catch (e) {
|
|
706
707
|
if (!(e instanceof GeneratorFailure))
|
|
@@ -751,8 +752,13 @@ function rustStripTrailingClone(expr) {
|
|
|
751
752
|
}
|
|
752
753
|
/** makeRustExprBackend — renders native-expr for Rust. `resolveHead` maps a ref head to an OWNED typed
|
|
753
754
|
* base expr (a prior-node cell clone / an input field / a $as element). Sets rustExprUsed.helpers when
|
|
754
|
-
* a fallible helper is emitted.
|
|
755
|
-
|
|
755
|
+
* a fallible helper is emitted.
|
|
756
|
+
*
|
|
757
|
+
* `breakLabel` names the FUNCTION-OR-BLOCK the hoisted statements land in, which is what decides how a
|
|
758
|
+
* fallible helper exits: the runner body is `Result<_, BehaviorError>`, so the default is `?`; the
|
|
759
|
+
* payload-entry block (#238, see rustPortEntries) is a labeled block yielding a Result, so its hoists
|
|
760
|
+
* `break '<label> Err(e)` — `?` there would return from the RUNNER and lose the block's deferral. */
|
|
761
|
+
function makeRustExprBackend(resolveHead, plan, breakLabel) {
|
|
756
762
|
const used = rustExprUsed;
|
|
757
763
|
return {
|
|
758
764
|
lang: "rust",
|
|
@@ -773,7 +779,9 @@ function makeRustExprBackend(resolveHead, plan) {
|
|
|
773
779
|
resolveHead,
|
|
774
780
|
hoistFallible: (temp, _ty, call) => {
|
|
775
781
|
used.helpers = true;
|
|
776
|
-
return
|
|
782
|
+
return breakLabel === undefined
|
|
783
|
+
? [`let ${temp} = ${call}?;`]
|
|
784
|
+
: [`let ${temp} = match ${call} { Ok(v) => v, Err(e) => break ${breakLabel} Err(e) };`];
|
|
777
785
|
},
|
|
778
786
|
helperCall: (helper, args) => {
|
|
779
787
|
used.helpers = true;
|
|
@@ -1471,14 +1479,49 @@ function inputPortRustType(schema, plan, where = "input port") {
|
|
|
1471
1479
|
* `buildWirePlan(dir:"in")`) — the SAME renderer every box crossing uses (a `value` / `Vec<WireValue>` port
|
|
1472
1480
|
* is already the opaque form, boxed as identity `value` / list-of-`value`, so there is NO double-box). The
|
|
1473
1481
|
* entry key is the RAW PORT NAME — the WireRow field-name↔port-name identity is the load-bearing transport
|
|
1474
|
-
* contract (a dynamic-port consumer reads `PK`/`SK`/`GSI1PK` by that name). A FALLIBLE port expr
|
|
1482
|
+
* contract (a dynamic-port consumer reads `PK`/`SK`/`GSI1PK` by that name). A FALLIBLE port expr (checked
|
|
1483
|
+
* arithmetic / `len` / a widen) lowers to STATEMENTS beside the entry, laid out by {@link rustPortEntries}.
|
|
1475
1484
|
*/
|
|
1476
1485
|
function emitPortWireField(pn, expr, comp, plan, typedNodes, isPrior, expected, opts) {
|
|
1477
1486
|
const c = compilePortNode(expr, comp, plan, typedNodes, isPrior, `port '${pn}'`, expected, opts);
|
|
1478
|
-
if (c.stmts.length > 0)
|
|
1479
|
-
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `rust typed-native (runtime-free): port '${pn}' ${JSON.stringify(expr)} lowers to a FALLIBLE expression (INT_OVERFLOW / NAN_OR_INF / MOD_ZERO / PRECISION_LOSS) — statements with an early error-return, which a transport payload literal cannot host. Use a non-fallible port expression.`);
|
|
1480
1487
|
const boxed = renderRustWireBox(buildWirePlan(c.ref, { from: "top" }, { model: "port", field: pn, plan }, "in"), c.expr);
|
|
1481
|
-
return `(Cow::Borrowed(${rustStrLit(pn)}), ${boxed})
|
|
1488
|
+
return { stmts: c.stmts, entry: `(Cow::Borrowed(${rustStrLit(pn)}), ${boxed})` };
|
|
1489
|
+
}
|
|
1490
|
+
/** rustPortsLabel — the payload-entry block's label for a node, the ONE name its hoists break to. */
|
|
1491
|
+
function rustPortsLabel(nodeId) {
|
|
1492
|
+
return `'ports_${sanitize(nodeId)}`;
|
|
1493
|
+
}
|
|
1494
|
+
/**
|
|
1495
|
+
* rustPortEntries — lay out ONE transport payload's port entries at `il`.
|
|
1496
|
+
*
|
|
1497
|
+
* A port expression can be FALLIBLE (checked arithmetic, `len`, an int→float widen): it lowers to
|
|
1498
|
+
* STATEMENTS carrying an early exit, which no expression literal can host. So the entry list is not always
|
|
1499
|
+
* a literal — when ANY port of the payload hoists, all of them are built by a LABELED BLOCK yielding
|
|
1500
|
+
* `Result<Vec<…>, BehaviorError>`, whose statements run in PORT ORDER (the order run_behavior's evalPorts
|
|
1501
|
+
* evaluates them, so the failure that surfaces is the same one).
|
|
1502
|
+
*
|
|
1503
|
+
* The block — not `?` — is what lets the caller decide WHERE the failure surfaces: the sequential / map /
|
|
1504
|
+
* fanout arms take it right here with `?` (the port is evaluated at that point in program order, as in the
|
|
1505
|
+
* interpreter); the parallel-stage arm keeps the Result and surfaces it in the ascending COMMIT, so a
|
|
1506
|
+
* lower-index member's failure still wins (run_behavior settles a stage in INDEX order — plan.ts).
|
|
1507
|
+
*
|
|
1508
|
+
* An infallible payload keeps the literal it always had, so nothing changes for the shapes already covered.
|
|
1509
|
+
*/
|
|
1510
|
+
function rustPortEntries(il, nodeId, resVar, ports) {
|
|
1511
|
+
const literal = `vec![${ports.map((p) => p.entry).join(", ")}]`;
|
|
1512
|
+
if (!ports.some((p) => p.stmts.length > 0))
|
|
1513
|
+
return { lines: [], entries: literal };
|
|
1514
|
+
const label = rustPortsLabel(nodeId);
|
|
1515
|
+
return {
|
|
1516
|
+
lines: [
|
|
1517
|
+
`${il}let ${resVar}: Result<Vec<(Cow<'static, str>, WireValue)>, BehaviorError> = ${label}: {`,
|
|
1518
|
+
...ports.flatMap((p) => p.stmts).map((st) => `${il} ${st}`),
|
|
1519
|
+
`${il} Ok(${literal})`,
|
|
1520
|
+
`${il}};`,
|
|
1521
|
+
],
|
|
1522
|
+
entries: `${resVar}?`,
|
|
1523
|
+
res: resVar,
|
|
1524
|
+
};
|
|
1482
1525
|
}
|
|
1483
1526
|
// ── the combined runner (STRUCT-returning; INLINE sequential; native ports in; concrete row out) ──
|
|
1484
1527
|
function rustScalar(s) {
|
|
@@ -1675,7 +1718,7 @@ function emitNativeRawRunner(comp, plan, ap, leaf, declared) {
|
|
|
1675
1718
|
}
|
|
1676
1719
|
}
|
|
1677
1720
|
const narrowed = nodeNarrowedPaths(comp, node);
|
|
1678
|
-
const
|
|
1721
|
+
const pe = rustPortEntries(indent, node.id, `pf_${s}`, portNames.map((pn) => emitPortWireField(pn, node.ports[pn], comp, plan, typedNodes, (h) => priorNodeAt(h, k), inputPortTypeRef(node.portSchemas?.[pn], plan), { narrowed, moveHeads, portsLabel: rustPortsLabel(node.id) })));
|
|
1679
1722
|
// bc#97: derived per-node await — this point-read's future is awaited HERE (its result-consumption
|
|
1680
1723
|
// point) iff THIS node's terminal handler is async. A sync handler in the same async runner is called
|
|
1681
1724
|
// directly (no `.await`). The runner being `async fn` does not force every call site to await.
|
|
@@ -1684,7 +1727,10 @@ function emitNativeRawRunner(comp, plan, ap, leaf, declared) {
|
|
|
1684
1727
|
// ports). `bound` is NOT passed — it is BC's internal produced/skip gate, already enforced by the
|
|
1685
1728
|
// surrounding `if bound_<s>.is_some()` / `if produced_<parent>` guards; the leaf never receives it.
|
|
1686
1729
|
const call = `${resolveLeafSymbol(node.component, leaf, node.leafSymbol)}(payload_${s})${awaitSfx}`;
|
|
1687
|
-
|
|
1730
|
+
// a FALLIBLE port (#238) builds the entries in a labeled block first; `?` on its Result surfaces the
|
|
1731
|
+
// failure HERE, where the interpreter evaluates this node's ports.
|
|
1732
|
+
lines.push(...pe.lines);
|
|
1733
|
+
lines.push(`${indent}let payload_${s} = ${rustPayloadRow(pe.entries)};`);
|
|
1688
1734
|
// uniform seam: every covered node returns a single result WIRE; the inline de-box (renderResultDeBox)
|
|
1689
1735
|
// classifies it against the STATICALLY declared result type and produces the structured Error Value on a
|
|
1690
1736
|
// mismatch (byte-equal codes to run_behavior's outType check, so ≡ holds). The wire's concrete type is
|
|
@@ -1799,6 +1845,16 @@ function emitParallelStageArm(comp, stage, atPos, concurrency, metas, typedNodes
|
|
|
1799
1845
|
// parent struct field (typed access — no serialize) into a stage-scoped Option; a None bound =>
|
|
1800
1846
|
// UNPRODUCED/skip (ports_<s> stays None), exactly run_behavior's preflightOp bindField skip, before
|
|
1801
1847
|
// any dispatch. The bound is captured (cloned) by the member's scoped-thread worker.
|
|
1848
|
+
// Every member's ports are lowered ONCE, here, and read by both the preflight build and the commit —
|
|
1849
|
+
// lowering twice would compile the same port twice and hand the two copies different expression temps.
|
|
1850
|
+
const memberPorts = new Map();
|
|
1851
|
+
for (const i of refMembers) {
|
|
1852
|
+
const node = comp.body[i];
|
|
1853
|
+
const narrowed = nodeNarrowedPaths(comp, node);
|
|
1854
|
+
memberPorts.set(i, Object.keys(node.ports).map((pn) => emitPortWireField(pn, node.ports[pn], comp, plan, typedNodes, (h) => priorNodeAt(h, atPos), inputPortTypeRef(node.portSchemas?.[pn], plan), { narrowed, portsLabel: rustPortsLabel(node.id) })));
|
|
1855
|
+
}
|
|
1856
|
+
/** #238: does this member's payload carry a FALLIBLE port (settled in preflight, surfaced at commit)? */
|
|
1857
|
+
const portsFallible = (i) => memberPorts.get(i).some((p) => p.stmts.length > 0);
|
|
1802
1858
|
for (const i of refMembers) {
|
|
1803
1859
|
const node = comp.body[i];
|
|
1804
1860
|
const s = sanitize(node.id);
|
|
@@ -1813,8 +1869,18 @@ function emitParallelStageArm(comp, stage, atPos, concurrency, metas, typedNodes
|
|
|
1813
1869
|
// bound is now purely the preflight skip-gate condition (the leaf transport no longer receives it), so
|
|
1814
1870
|
// it is a plain local inside the produced block — NOT a stage-scoped mut (nothing captures it) and no
|
|
1815
1871
|
// dead None init. A control-gate parent needs no bound at all (produced_<gate> already carries the skip).
|
|
1816
|
-
|
|
1872
|
+
// #238: a FALLIBLE port settles its failure into a member-scoped local, and the ascending COMMIT
|
|
1873
|
+
// surfaces it. Taking it with `?` in preflight instead would let a HIGHER-index member's port failure
|
|
1874
|
+
// beat a lower-index member's Component failure — run_behavior settles a stage in INDEX order
|
|
1875
|
+
// (plan.ts runPlan / runPlanAsync commit), so the lowest index wins no matter how each member failed.
|
|
1876
|
+
// Its payload is filled in the same match, so the slot starts empty exactly as a gated member's does.
|
|
1877
|
+
const fallible = portsFallible(i);
|
|
1878
|
+
if (fallible)
|
|
1879
|
+
lines.push(` let mut perr_${s}: Option<BehaviorError> = None;`);
|
|
1880
|
+
if (gated || fallible) {
|
|
1817
1881
|
lines.push(` let payload_${s}: std::sync::Mutex<Option<WireRow>> = std::sync::Mutex::new(None);`);
|
|
1882
|
+
}
|
|
1883
|
+
if (gated) {
|
|
1818
1884
|
lines.push(`${ind}if produced_${sanitize(node.parent)}.get() {`);
|
|
1819
1885
|
closers.unshift(`${ind}}`);
|
|
1820
1886
|
ind += " ";
|
|
@@ -1833,13 +1899,19 @@ function emitParallelStageArm(comp, stage, atPos, concurrency, metas, typedNodes
|
|
|
1833
1899
|
ind += " ";
|
|
1834
1900
|
}
|
|
1835
1901
|
}
|
|
1836
|
-
const
|
|
1837
|
-
|
|
1838
|
-
if (
|
|
1839
|
-
lines.push(`${ind}
|
|
1902
|
+
const pe = rustPortEntries(ind, node.id, `pf_${s}`, memberPorts.get(i));
|
|
1903
|
+
lines.push(...pe.lines);
|
|
1904
|
+
if (pe.res !== undefined) {
|
|
1905
|
+
lines.push(`${ind}match ${pe.res} {`);
|
|
1906
|
+
lines.push(`${ind} Ok(entries) => { *payload_${s}.lock().unwrap() = Some(WireRow { entries }); }`);
|
|
1907
|
+
lines.push(`${ind} Err(e) => { perr_${s} = Some(e); }`);
|
|
1908
|
+
lines.push(`${ind}}`);
|
|
1909
|
+
}
|
|
1910
|
+
else if (gated) {
|
|
1911
|
+
lines.push(`${ind}*payload_${s}.lock().unwrap() = Some(${rustPayloadRow(pe.entries)});`);
|
|
1840
1912
|
}
|
|
1841
1913
|
else {
|
|
1842
|
-
lines.push(`${ind}let payload_${s}: std::sync::Mutex<Option<WireRow>> = std::sync::Mutex::new(Some(${rustPayloadRow(
|
|
1914
|
+
lines.push(`${ind}let payload_${s}: std::sync::Mutex<Option<WireRow>> = std::sync::Mutex::new(Some(${rustPayloadRow(pe.entries)}));`);
|
|
1843
1915
|
}
|
|
1844
1916
|
for (const c of closers)
|
|
1845
1917
|
lines.push(c);
|
|
@@ -1973,6 +2045,11 @@ function emitParallelStageArm(comp, stage, atPos, concurrency, metas, typedNodes
|
|
|
1973
2045
|
// bind first: an `if let` scrutinee keeps its temporaries — including the MutexGuard — alive for the
|
|
1974
2046
|
// whole body, which outlives the borrow the body needs. A joined-futures slot is a plain Option (one
|
|
1975
2047
|
// task on one thread), so it takes without a lock.
|
|
2048
|
+
// #238: this member's FALLIBLE port failed → it surfaces HERE, at this member's index, ahead of any
|
|
2049
|
+
// higher-index member and behind any lower-index one (≡ run_behavior's index-ordered commit). A port
|
|
2050
|
+
// ExprFailure is never policy-interpreted (plan.ts: an exec throw propagates), so it returns as-is.
|
|
2051
|
+
if (portsFallible(i))
|
|
2052
|
+
lines.push(` if let Some(e) = perr_${s} { return Err(e); }`);
|
|
1976
2053
|
const takeSlot = stageIsAsync ? `slot_${s}.take()` : `slot_${s}.lock().unwrap().take()`;
|
|
1977
2054
|
lines.push(` let taken_${s} = ${takeSlot};`);
|
|
1978
2055
|
lines.push(` if let Some(out_${s}) = taken_${s} {`);
|
|
@@ -2078,16 +2155,22 @@ isAsync = false, policy = "fail") {
|
|
|
2078
2155
|
? "None"
|
|
2079
2156
|
: `${renderTypeRef(elemDeBoxRef)}::default()`;
|
|
2080
2157
|
// the per-element boxed port entries (#183) — the SAME box SSoT as a point node, over the $as element.
|
|
2081
|
-
const
|
|
2158
|
+
const portEntries = (il) => {
|
|
2082
2159
|
const asBinding = { name: asName, ref: overElemRef, plan };
|
|
2083
2160
|
// #170: a guarded map narrows its `when(ne($e.key,null))`-proven element key non-null in kept elements.
|
|
2084
2161
|
const narrowed = nodeNarrowedPaths(comp, node);
|
|
2085
|
-
return portNames.map((pn) => emitPortWireField(pn, m.ports[pn], comp, plan, typedNodes, priorHere, inputPortTypeRef(m.portSchemas?.[pn], plan), { asBinding, asBase: `oel_${s}`, narrowed }));
|
|
2162
|
+
return rustPortEntries(il, node.id, `pf_${s}`, portNames.map((pn) => emitPortWireField(pn, m.ports[pn], comp, plan, typedNodes, priorHere, inputPortTypeRef(m.portSchemas?.[pn], plan), { asBinding, asBase: `oel_${s}`, narrowed, portsLabel: rustPortsLabel(node.id) })));
|
|
2086
2163
|
};
|
|
2087
2164
|
// BATCHED element: `ep_<s>` is a WireRow VALUE (a `WireValue::Row`) collected into the items WireList of
|
|
2088
2165
|
// the batch payload. NON-batched element: `ep_<s>` is the per-element transport payload WireRow.
|
|
2089
|
-
|
|
2090
|
-
|
|
2166
|
+
// A FALLIBLE element port (#238) builds its entries in a labeled block inside the loop, so the failure
|
|
2167
|
+
// surfaces at the element the interpreter would have failed on.
|
|
2168
|
+
const buildElem = (il, wrap) => {
|
|
2169
|
+
const pe = portEntries(il);
|
|
2170
|
+
return [...pe.lines, `${il}let ep_${s} = ${wrap(pe.entries)};`];
|
|
2171
|
+
};
|
|
2172
|
+
const buildBatchElem = (il) => buildElem(il, (e) => `WireValue::Row(WireRow { entries: ${e} })`);
|
|
2173
|
+
const buildPayloadElem = (il) => buildElem(il, (e) => rustPayloadRow(e));
|
|
2091
2174
|
// #108 guard: compile `when` to a native bool over an element-scoped resolveHead ($as element value,
|
|
2092
2175
|
// prior node, input port). mkGuard returns the compiled {stmts, expr} (or null when unguarded); the
|
|
2093
2176
|
// keep-with-null batched path consumes it directly to branch instead of skipping.
|
|
@@ -2320,8 +2403,10 @@ function emitFanoutArm(comp, node, atPos, typedNodes, plan, priorNodeAt, leaf, i
|
|
|
2320
2403
|
// per-id boxed port WireFields — `ep_<s>` is a WireRow VALUE collected into the batch payload's items list.
|
|
2321
2404
|
const buildPorts = (il) => {
|
|
2322
2405
|
const asBinding = { name: asName, ref: overElemRef, plan };
|
|
2323
|
-
|
|
2324
|
-
|
|
2406
|
+
// a FALLIBLE per-id port (#238) builds in a labeled block inside the id loop — the failure surfaces at
|
|
2407
|
+
// the id it happened on.
|
|
2408
|
+
const pe = rustPortEntries(il, node.id, `pf_${s}`, portNames.map((pn) => emitPortWireField(pn, f.ports[pn], comp, plan, typedNodes, priorHere, inputPortTypeRef(f.portSchemas?.[pn], plan), { asBinding, asBase: `oel_${s}`, portsLabel: rustPortsLabel(node.id) })));
|
|
2409
|
+
return [...pe.lines, `${il}let ep_${s} = WireValue::Row(WireRow { entries: ${pe.entries} });`];
|
|
2325
2410
|
};
|
|
2326
2411
|
const lines = [];
|
|
2327
2412
|
const closers = [];
|