behavior-contracts 0.9.0 → 0.9.1

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.
@@ -1 +1 @@
1
- {"version":3,"file":"emit-typed-native-go.d.ts","sourceRoot":"","sources":["../../src/generator/emit-typed-native-go.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,OAAO,KAAK,EAAe,aAAa,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAiClF;;;;+GAI+G;AAC/G,wBAAgB,wBAAwB,CAAC,iBAAiB,EAAE,MAAM,GAAG,MAAM,CAE1E;AA07FD;;;;;;;;oFAQoF;AACpF,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,OAAO,GAAG,MAAM,CAiC9E;AAED;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAAC,EAAE,EAAE,OAAO,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,oBAAoB,GAAG,MAAM,CAuW7G;AAED;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,EAAE,aAOlC,CAAC"}
1
+ {"version":3,"file":"emit-typed-native-go.d.ts","sourceRoot":"","sources":["../../src/generator/emit-typed-native-go.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,OAAO,KAAK,EAAe,aAAa,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAiClF;;;;+GAI+G;AAC/G,wBAAgB,wBAAwB,CAAC,iBAAiB,EAAE,MAAM,GAAG,MAAM,CAE1E;AAo/FD;;;;;;;;oFAQoF;AACpF,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,OAAO,GAAG,MAAM,CAiC9E;AAED;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAAC,EAAE,EAAE,OAAO,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,oBAAoB,GAAG,MAAM,CAuW7G;AAED;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,EAAE,aAOlC,CAAC"}
@@ -2,7 +2,7 @@ import { GeneratorFailure } from "./core.js";
2
2
  import { isControlGate, nodeWirePassthrough } from "../behavior.js";
3
3
  import { goStraightlineInternals } from "./emit-shared-go.js";
4
4
  import { goTypedInternals } from "./emit-shared-go-typed.js";
5
- import { buildTypePlan, deriveTypeRef, findDecl, inputPortIsOptional, inputPortTypeRef } from "./typed.js";
5
+ import { buildTypePlan, deriveTypeRef, findDecl, guardedMapIntoIsOpt, inputPortIsOptional, inputPortTypeRef } from "./typed.js";
6
6
  import { concurrencyPlan, execPlan, analyzeSequentialOps, classifyExpr, buildComponentPlan, nodeNarrowedPaths, } from "./straightline.js";
7
7
  import { NativeExprCompiler } from "./native-expr.js";
8
8
  import { buildDeBoxPlan, buildWirePlan, notationOfRef } from "./emit-shared-debox.js";
@@ -815,10 +815,16 @@ function coveredMapNode(n, bodyIds, comp) {
815
815
  return false;
816
816
  const m = n.map;
817
817
  // #86 pt2: batched map...into. #93 shape 2/3: (non-)batched map...into / connection-collect. #108:
818
- // a guarded map (`when`) IS covered when it has NO `into` (guard+into is heterogeneous — skipped
819
- // elements lack the into field, cannot de-box into one []T; keep it fail-closed, matching the typed
820
- // straightline reject). A guarded map over a native-typed element collection drops skipped elements.
821
- if (m.when !== undefined && m.into !== undefined)
818
+ // a guarded map (`when`) IS covered when it has NO `into`. #177: a guarded map WITH `into` is covered
819
+ // ONLY when the into field is OPT — the augmented list is then homogeneous []ElemWithOptInto (kept
820
+ // element → present into, guard-skipped element → into=nil), which is EXACTLY run_behavior's applyInto
821
+ // pass-through (behavior.ts:846 — a skipped element is pushed unaugmented) followed by output
822
+ // conformance materializing the absent opt-into field as null (behavior.ts:597 — keep-with-null). A
823
+ // REQUIRED into field stays fail-closed: run_behavior FAILS MISSING_PROP on a skipped element there,
824
+ // so rejecting is byte-equal. Opt-ness is read from the into field's declared type (the map node's
825
+ // element outType); an element type that does not resolve to a named struct with the into field →
826
+ // fail-closed (the resolvers below would reject it anyway).
827
+ if (m.when !== undefined && m.into !== undefined && !guardedMapIntoIsOpt(n, comp))
822
828
  return false;
823
829
  if (m.policy !== undefined && m.policy !== "fail")
824
830
  return false;
@@ -839,12 +845,18 @@ function coveredMapNode(n, bodyIds, comp) {
839
845
  return false;
840
846
  const head = overE.path[0];
841
847
  if (!bodyIds.has(head)) {
842
- // input-array over: covered only when the input port declares elemType (#108).
848
+ // input-array over: covered only when the input port declares elemType (#108) AND is REQUIRED.
849
+ // #177: an OPT input-array (`{opt:{arr}}`, required:false) is NOT natively coverable — the over would
850
+ // resolve to `in.<Field>` of native `*[]E` and `range` over a pointer-to-slice does not compile;
851
+ // run_behavior itself FAILS MAP_OVER_NOT_ARRAY on a null opt collection, so fail-close here is
852
+ // byte-equal. (A faithful nil→empty iteration is an owner-level semantics decision, not an emitter fix.)
843
853
  if (overE.path.length !== 1)
844
854
  return false;
845
855
  const schema = comp.inputPorts?.[head];
846
856
  if (schema === undefined || schema.elemType === undefined)
847
857
  return false;
858
+ if (inputPortIsOptional(schema))
859
+ return false;
848
860
  }
849
861
  const ports = m.ports;
850
862
  const plan = planForComp(comp);
@@ -878,11 +890,17 @@ function coveredFanoutNode(n, bodyIds, comp) {
878
890
  return false;
879
891
  const head = overE.path[0];
880
892
  if (!bodyIds.has(head)) {
893
+ // input-array over: covered only when the port declares elemType AND is REQUIRED. #177: an OPT
894
+ // input-array (`{opt:{arr}}`, required:false) resolves to `in.<Field>` of native `*[]E` — `len`/
895
+ // `range` over a pointer-to-slice does not compile, and run_behavior FAILS FANOUT_OVER_NOT_ARRAY on
896
+ // a null opt collection — so fail-close here is byte-equal (the faithful nil→empty is owner-level).
881
897
  if (overE.path.length !== 1)
882
898
  return false;
883
899
  const schema = comp.inputPorts?.[head];
884
900
  if (schema === undefined || schema.elemType === undefined)
885
901
  return false;
902
+ if (inputPortIsOptional(schema))
903
+ return false;
886
904
  }
887
905
  const ports = f.ports;
888
906
  const plan = planForComp(comp);
@@ -1908,6 +1926,10 @@ function emitMapArm(comp, node, atPos, typedNodes, plan, priorNodeAt, zeroOut, f
1908
1926
  const batched = m.batched === true;
1909
1927
  const hasInto = m.into !== undefined;
1910
1928
  const hasGuard = m.when !== undefined;
1929
+ // #177 keep-with-null: a guarded map WITH `into` keeps guard-skipped elements with the (opt) into
1930
+ // field set to nil (coveredMapNode only admits this when the into field is opt). This differs from a
1931
+ // guarded NO-into map, which DROPs skipped elements. Byte-equal to run_behavior's applyInto keep-with-null.
1932
+ const keepWithNull = hasGuard && hasInto;
1911
1933
  const s = sanitize(node.id);
1912
1934
  // #154 cross-package: a BATCHED map's element + batch ports structs live in the shared package (they
1913
1935
  // cross the transport boundary as a whole value), so reference them selector-qualified here. A
@@ -1958,7 +1980,9 @@ function emitMapArm(comp, node, atPos, typedNodes, plan, priorNodeAt, zeroOut, f
1958
1980
  }
1959
1981
  // over collection (typed field access to the parent node's arr — no serialize).
1960
1982
  lines.push(`${indent}over_${s} := ${overExpr}`);
1961
- lines.push(`${indent}${typedLocal(node.id)} = make(${elemGo}, 0, len(over_${s}))`);
1983
+ // #177 batched keep-with-null assembles the result BY INDEX (over-order, kept slots filled post-dispatch),
1984
+ // so it needs a full-length backing slice; every other shape appends in kept order.
1985
+ lines.push(`${indent}${typedLocal(node.id)} = make(${elemGo}, ${batched && keepWithNull ? `len(over_${s})` : `0, len(over_${s})`})`);
1962
1986
  // each element is de-boxed strictly INLINE: the handler returns the per-element WIRE and the arm decodes
1963
1987
  // it against the element ref (the `into` field's type for map...into, else the element type). A de-box
1964
1988
  // mismatch fails closed regardless of elementPolicy (matching the interpreter's whole-node outType
@@ -1968,7 +1992,7 @@ function emitMapArm(comp, node, atPos, typedNodes, plan, priorNodeAt, zeroOut, f
1968
1992
  const renderElemDeBox = (target, src, il) => renderResultDeBox(elemRowRef, target, src, node.id, plan, il, returnZeroFail(zeroOut), nodeWirePassthrough(node));
1969
1993
  // emit the per-element guard keep-gate (#108). `il` = indent, `elemVar` = the over element loop var.
1970
1994
  // On skip: batched → `continue` (element excluded from items + keptOver); non-batched → `continue`.
1971
- const emitGuard = (il, elemVar) => {
1995
+ const emitGuard = (il, elemVar, onSkip = ["continue"]) => {
1972
1996
  const g = mkGuard(elemVar);
1973
1997
  if (g === null)
1974
1998
  return [];
@@ -1976,11 +2000,46 @@ function emitMapArm(comp, node, atPos, typedNodes, plan, priorNodeAt, zeroOut, f
1976
2000
  for (const st of g.stmts)
1977
2001
  out.push(`${il}${st}`);
1978
2002
  out.push(`${il}if !(${g.expr}) {`);
1979
- out.push(`${il}\tcontinue`);
2003
+ for (const a of onSkip)
2004
+ out.push(`${il}\t${a}`);
1980
2005
  out.push(`${il}}`);
1981
2006
  return out;
1982
2007
  };
1983
- if (batched) {
2008
+ if (batched && keepWithNull) {
2009
+ // #177 BATCHED keep-with-null (guarded map...into, opt into): first pass records, per over element in
2010
+ // ORDER, whether it is kept (guard true → build its ports, remember its over-index) or skipped (guard
2011
+ // false → materialize the augmented element in place with into=nil). ONE batched dispatch over the kept
2012
+ // ports, then the aligned rows are de-boxed and zipped back onto their over-index slots. Byte-equal to
2013
+ // run_behavior: batched keptIdx-aligned collect + applyInto (skipped element unaugmented → opt-into null).
2014
+ const g = mkGuard(elemLocal(s));
2015
+ lines.push(`${indent}items_${s} := make([]${structName}, 0, len(over_${s}))`);
2016
+ lines.push(`${indent}keptIdx_${s} := make([]int, 0, len(over_${s}))`);
2017
+ lines.push(`${indent}for oi_${s} := range over_${s} {`);
2018
+ lines.push(`${indent}\t${elemLocal(s)} := over_${s}[oi_${s}]`);
2019
+ for (const st of g.stmts)
2020
+ lines.push(`${indent}\t${st}`);
2021
+ lines.push(`${indent}\tif ${g.expr} {`);
2022
+ for (const l of buildPorts(indent + "\t\t", elemLocal(s)))
2023
+ lines.push(l);
2024
+ lines.push(`${indent}\t\titems_${s} = append(items_${s}, ep_${s})`);
2025
+ lines.push(`${indent}\t\tkeptIdx_${s} = append(keptIdx_${s}, oi_${s})`);
2026
+ lines.push(`${indent}\t} else {`);
2027
+ lines.push(`${indent}\t\t${typedLocal(node.id)}[oi_${s}] = ${elemMat}(${elemLocal(s)}, nil)`);
2028
+ lines.push(`${indent}\t}`);
2029
+ lines.push(`${indent}}`);
2030
+ lines.push(`${indent}if len(items_${s}) > 0 {`);
2031
+ lines.push(`${indent}\tbp_${s} := ${batchStruct}{Items: items_${s}}`);
2032
+ lines.push(`${indent}\tmo_${s}, mo_${s}Err := ${leafSym}(bp_${s})`);
2033
+ lines.push(`${indent}\tif mo_${s}Err != nil {`, `${indent}\t\treturn ${zeroOut}, opFailed(${JSON.stringify(node.id)}, "fail", mo_${s}Err)`, `${indent}\t}`);
2034
+ lines.push(`${indent}\tif len(mo_${s}) != len(items_${s}) {`, `${indent}\t\treturn ${zeroOut}, ${goErr("MAP_BATCH_RESULT_MISMATCH", `fmt.Sprintf("map '${node.id}': batched handler must return a list aligned to items (want %d)", len(items_${s}))`)}`, `${indent}\t}`);
2035
+ lines.push(`${indent}\tfor mk_${s} := range items_${s} {`);
2036
+ lines.push(`${indent}\t\tvar dec_${s} ${elemRowGo}`);
2037
+ lines.push(renderElemDeBox(`dec_${s}`, `mo_${s}[mk_${s}]`, `${indent}\t\t`));
2038
+ lines.push(`${indent}\t\t${typedLocal(node.id)}[keptIdx_${s}[mk_${s}]] = ${elemMat}(over_${s}[keptIdx_${s}[mk_${s}]], dec_${s})`);
2039
+ lines.push(`${indent}\t}`);
2040
+ lines.push(`${indent}}`);
2041
+ }
2042
+ else if (batched) {
1984
2043
  // BATCHED (#86 pt2 into / #93 shape 3 no-into / #108 guarded no-into): build kept-element ports,
1985
2044
  // dispatch ONCE. The concrete Node_* returns the aligned per-element WIRES ([]WireValue) or an error.
1986
2045
  // With a guard, `items` (and, for into, `keptOver`) hold only kept elements — matching run_behavior's
@@ -2031,7 +2090,12 @@ function emitMapArm(comp, node, atPos, typedNodes, plan, priorNodeAt, zeroOut, f
2031
2090
  // (no dispatch, excluded from the result). The concrete Node_* returns ONE element WIRE (WireValue).
2032
2091
  lines.push(`${indent}for mk_${s} := range over_${s} {`);
2033
2092
  lines.push(`${indent}\t${elemLocal(s)} := over_${s}[mk_${s}]`);
2034
- for (const l of emitGuard(indent + "\t", elemLocal(s)))
2093
+ // #177 keep-with-null: a guard-skipped element is KEPT with into=nil (elemMat(over,nil)) instead of
2094
+ // dropped — byte-equal to run_behavior's applyInto pass-through + opt-into null.
2095
+ const nonBatchedSkip = keepWithNull
2096
+ ? [`${typedLocal(node.id)} = append(${typedLocal(node.id)}, ${elemMat}(${elemLocal(s)}, nil))`, "continue"]
2097
+ : ["continue"];
2098
+ for (const l of emitGuard(indent + "\t", elemLocal(s), nonBatchedSkip))
2035
2099
  lines.push(l);
2036
2100
  for (const l of buildPorts(indent + "\t", elemLocal(s)))
2037
2101
  lines.push(l);