Give a behavior more ways in and out, and hear it as an event (#294)
game-maker / gate (push) Successful in 2m48s
game-maker / suite (push) Failing after 13m34s

A behavior run by another one had a single flow port at its Start
and at its End, so it could not branch for its caller. And it could
not be used as an event: a call needs a pulse at its flow input,
and the events of a called graph never fire. An input like
"Confirm" (Space or Enter) had to be built again in every graph.

- A declared port of a Start or an End node can be of the kind
  Flow. It sits above the values and shares their names, exec stays
  reserved. Only these two nodes declare one, a struct does not.
- Run behavior shows a flow port of the Start as a further flow
  input. A call arriving through it leaves the Start through the
  output of that name and not through exec.
- Run behavior shows a flow port of the End as a further flow
  output. The call goes on through the one the End was reached by
  last, and then through exec. exec fires always, as before.
- On behavior is a new event node without a flow input. The
  behavior it names runs at the node for as long as the behavior
  around it runs, with all of its own events and one set of
  variables per node.
- Its outputs are those of the End. It fires once the inner pulse
  is through, by the output the End was reached by last, exec only
  for End.exec. A pulse that never reaches the End is not seen.
- The value ports of the Start are its inputs, read anew for each
  inner pulse. The Start itself never fires there.
- It nests to any depth. Under a Run behavior it stays silent. A
  behavior hearing itself, however far round, is not started and
  the node says "A behavior cannot listen to itself".
- The palette offers every other behavior with an End once more as
  "On <name>". A node naming one without End says so.
- An On node removed while the game runs ends its behavior with On
  disable and On destroy. The sounds of the entity end only with
  the behavior placed on it.
- Renaming and deleting a declared port follows the edges at On
  behavior nodes as it does at Run behavior nodes.
- The flow outputs of the Start of a behavior placed on an object
  never fire, like its exec.
- The engine prompt of the benchmark names flow ports and On
  behavior.

Tests:
- Gate on melektaus-atx right before this commit: 215 files, 3319
  tests (3278 + 41), exit 0.
- e2e 19 on melektaus-atx: 39 checks (+4), all pass. e2e
  1-6,17-24,26,36,51: 628 checks, all pass.
- 19 unit tests in flow-ports.test.ts, 19 in on-behavior.test.ts
  on a real world, the rest in the node catalog.
- Four new e2e checks in module 19: a flow port declared by mouse
  at the End, On Confirm in the graph, the call leaving through the
  further output while playing, Enter and Space on a real keyboard.
- 19 unit counter-probes all fail their tests. One did not at first
  (a removed node kept its instance), a test was added. Two browser
  counter-probes fail their checks.
- One screenshot looked at: On Confirm with exec and pressed, the
  second flow pin at the calling node.
- The e2e sum of 1680 is counted (1676 + 4), the full suite and
  the smokes did not run.
- Not seen in the browser: flow inputs at the Start, nesting, the
  ring, values through On behavior, the palette entry. No test
  holds that the sounds of the entity go on when a heard behavior
  ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-02 19:33:34 +02:00
1 parent b5339b96c2
commit 46c620ae3b
26 files changed
+1211 -42

No files matched your search

+54 -13
View File
@@ -2,7 +2,7 @@ import { blockProblem, findMode, MATCH_INPUT, matchOf, searchOutcome, stopsAtHit
import type { Graph, GraphNode } from './Graph';
import { type GraphCache, type GraphIndex, searchingIndex } from './graphIndex';
import { type GraphLookup, NO_LOOKUP } from './lookup';
import { BEHAVIOR_END, callEntries, terminalNode } from './nested';
import { BEHAVIOR_END, BEHAVIOR_START, callEntries } from './nested';
import { fieldValue, splitField, withSplitValues } from './portSplits';
import type { PropertyBag, PropertyValue } from '../model/Properties';
import type { NodeContext } from './NodeContext';
@@ -41,6 +41,14 @@ interface Frame {
index: GraphIndex;
outputCache: Map<string, Record<string, Value>>;
dataCache: Map<string, Record<string, Value>>;
/** The flow input this graph's end node was reached by last, once it was reached at all (#294). */
ended?: string;
}
/** How a graph was left: through which flow input of its end node, and with what. */
export interface Ending {
port: string;
values: Record<string, Value>;
}
/**
@@ -83,6 +91,11 @@ export class GraphInterpreter {
private callSites: string[] = [];
private steps = 0;
trace: TraceListener | null = null;
/**
* What the start node hands out while this graph runs on its own events and not as a call: the
* inputs of the `On behavior` node listening to it (#294). Set by whoever sends the pulses.
*/
given: Record<string, Value> | null = null;
constructor(
readonly graph: Graph,
@@ -101,6 +114,14 @@ export class GraphInterpreter {
return { graph, index: this.indexOf(graph), outputCache: new Map(), dataCache: new Map() };
}
/** A frame of the graph itself, its start node holding what was given from outside. */
private ownFrame(): Frame {
const frame = this.emptyFrame(this.graph);
const start = this.given ? frame.index.ofType(BEHAVIOR_START)[0] : undefined;
if (start && this.given) frame.outputCache.set(start.id, { ...this.given });
return frame;
}
eventNodes(type: string): readonly GraphNode[] {
return this.indexOf(this.graph).ofType(type);
}
@@ -110,7 +131,7 @@ export class GraphInterpreter {
* through – a state event names one.
*/
pulse(node: GraphNode, ctx: NodeContext, outputs: Record<string, Value> = {}, port = 'exec'): void {
this.frame = this.emptyFrame(this.graph);
this.frame = this.ownFrame();
this.callStack = [];
this.callSites = [];
this.steps = 0;
@@ -125,12 +146,30 @@ export class GraphInterpreter {
* the node at all (#160). Null for an input the node does not have.
*/
eventInput(node: GraphNode, key: string, ctx: NodeContext): Value {
this.frame = this.emptyFrame(this.graph);
this.frame = this.ownFrame();
this.steps = 0;
const port = this.def(node)?.inputs.find((p) => p.key === key);
return port ? this.inputValue(node, port, ctx) : null;
}
/** Every data input of an event node, read ahead of a pulse like `eventInput`. */
eventInputs(node: GraphNode, ctx: NodeContext): Record<string, Value> {
this.frame = this.ownFrame();
this.steps = 0;
return this.collectInputs(node, ctx);
}
/** How the last pulse left this graph, or null when it never reached the end node. */
get ending(): Ending | null {
return this.endingOf(this.frame);
}
private endingOf(frame: Frame): Ending | null {
if (frame.ended === undefined) return null;
const end = frame.index.ofType(BEHAVIOR_END)[0];
return { port: frame.ended, values: (end && frame.outputCache.get(end.id)) ?? {} };
}
/**
* The `On state entered` / `On state exited` nodes watching one machine. A node whose machine
* input carries a running value rather than a pick has no state outputs at all, so it is never
@@ -161,30 +200,33 @@ export class GraphInterpreter {
if (!def?.run) return;
const inputs = this.collectInputs(node, ctx);
const result = def.run(ctx, inputs, node.params, def, entry, this.lookup);
const returned = result.call ? this.call(result.call, node.id, ctx, depth) : {};
const produced = { ...result.outputs, ...returned };
const returned = result.call ? this.call(result.call, node.id, ctx, depth) : null;
const produced = { ...result.outputs, ...returned?.values };
if (node.type === BEHAVIOR_END) this.frame.ended = entry;
if (!def.read) this.frame.outputCache.set(node.id, produced);
this.trace?.onNode(node.id);
if (this.trace?.onValues) this.record(node.id, def.read ? this.readAll(node, def, ctx) : produced);
if (result.repeat) this.loop(node, result.repeat, ctx, depth);
if (result.find) this.search(node, result.find, ctx, depth);
const next = result.next === undefined ? [] : Array.isArray(result.next) ? result.next : [result.next];
for (const port of next) this.follow(node, port, ctx, depth);
// The way out the called graph took comes first, so `exec` reads as "and then" (#294).
const ways = returned && returned.port !== 'exec' ? [returned.port, ...next] : next;
for (const port of ways) this.follow(node, port, ctx, depth);
}
/**
* Runs another behavior from its start node (or, lacking one, its `On start` nodes) to its end
* node and returns what the end node was handed. The nested graph gets its own frame and a fresh
* node and returns how it was left; null when it never got there. The nested graph gets its own frame and a fresh
* copy of its variable defaults, so it is a call and not an inlining: nothing of it survives the
* return but the values it hands back – and the variables it keeps, which wait for the next call
* from the same site.
*/
private call(call: CallResult, site: string, ctx: NodeContext, depth: number): Record<string, Value> {
private call(call: CallResult, site: string, ctx: NodeContext, depth: number): Ending | null {
const program = this.lookup.behavior(call.behaviorId);
if (!program) return {};
if (!program) return null;
if (this.callStack.includes(call.behaviorId)) throw new Error(`Behavior "${program.name}" runs itself`);
const entries = callEntries(program.graph);
if (entries.length === 0) return {};
if (entries.length === 0) return null;
const sites = [...this.callSites, site].join('/');
const kept = program.kept ?? [];
const variables = resumedVariables(program.defaults, this.keptCalls.get(sites), kept);
@@ -212,10 +254,9 @@ export class GraphInterpreter {
try {
for (const entry of entries) {
this.trace?.onNode(entry.id);
this.follow(entry, 'exec', inner, depth + 1);
this.follow(entry, call.entry, inner, depth + 1);
}
const end = terminalNode(program.graph, BEHAVIOR_END);
return (end && this.frame.outputCache.get(end.id)) ?? {};
return this.endingOf(this.frame);
} finally {
this.callStack.pop();
this.callSites.pop();