A port can take its type from what is plugged into it (#207)
game-maker / gate (push) Canceled after 1m8s
game-maker / suite (push) Canceled after 1m8s

Until now every port of "First not empty" was grey and typeless, so the
editor could not tell you what actually travels through the node. The
output is now typed as soon as both inputs agree, and every any-input is
painted in the colour of its own edge. Nothing changes at runtime.

- A NodeDef can declare which of its ports carry the same value
  (`sharedType: { inputs, outputs }`). The rule itself lives as a pure
  function `agreedType` in `src/core/graph/portTypes.ts`.
- Only the output inherits. It becomes the shared type when every input
  of the group is connected and they all carry the same type, otherwise
  it stays `any`.
- The inputs stay `any` on purpose. `canConnect` checks against the port
  type and a busy input is replaced by the new edge, so a typed `a`
  could never be re-plugged with a text without disconnecting first.
- Chains do not propagate. When looking up what feeds an input, a node
  of the same kind reports the `any` it is registered with, so two of
  them in a row stay untyped and a data cycle cannot ask itself in a
  circle.
- An edge that the new type makes invalid stays where it is and turns
  red. `EdgeLayer` already paints a mismatching edge with `danger`,
  nothing is disconnected on its own.
- New `PortDef.paintType`: colour only, never truth. It is set in
  `drawnDef` and read in `NodeView.redraw`, so `canConnect`, the
  interpreter and the compiler keep seeing `any`. Every connected
  any-input is painted, not just members of a `sharedType` group; ghost
  ports keep their hollow red look.
- The pin now matches the edge that ends in it, because both take the
  colour from the resolved output type of the neighbour. Mixed sources
  therefore show two colours, which explains why the output stays grey.
- New hooks `__gm.graph.portTypes(id)`, `__gm.graph.edgeColor(id)` and
  `__gm.themeColor(name)`. `portTypes` reports type and paint colour
  separately so the type checks do not silently become colour checks.

Findings:

- `resolveDef` only appends derived ports to the registered ones, so a
  fixed port cannot be retyped that way -- it would appear twice. The
  shared type is therefore resolved before the ports are built.
- Answer 5 of the issue (inline field follows the type) has no case left
  under variant A: an unconnected input stays `any` and keeps its text
  field.
- `Item at`, `Join` and `Equals` do not profit: `Item at` has no
  any-input, the other two have fixed outputs. The next candidates are
  the shader arithmetic nodes, which were left alone.

Tests:

- ./check.sh -> 143 files, 2127 tests, exit 0 (was 142 / 2113)
- GM_STEPS=1-6 fresh -> ALL PASSED, 245 checks (was 242), 9 of them new
- 14 new unit tests in tests/core/shared-port-type.test.ts
- 9 new e2e checks in block 6z, one of them a pixel check straight on
  the pin via pixelAt + themeColor
- Counter-probes all bite: filtering unconnected inputs out of
  `agreedType` fails 3 unit tests, ignoring `inherit` fails 1, killing
  the derivation in the bundle fails exactly the 2 expected e2e checks,
  and never setting `paintType` fails 4 unit tests plus the 3 pin
  checks (#bdbdbd instead of #e6a8ff / #6fcf97)
- Screenshot read: `a` pink, `b` green, output `value` grey, edges in
  the same colours
- Check total 1302 (1299 + 3), 1293 was measured by CI at 1a3babf

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5 committed 2026-09-22 21:15:21 +02:00
1 parent 1a3babf9c5
commit d7a2521ca7
14 files changed
+499 -19

No files matched your search

+5 -3
View File
@@ -52,7 +52,7 @@ function setup() {
const real = resolveDef(node, asset.graph, graphLookup(f.project))!;
return {
real,
drawn: drawnDef(node, asset.graph, real),
drawn: drawnDef(node, asset.graph, real, graphLookup(f.project)),
problem: ghostProblem(node, asset.graph, real),
};
};
@@ -120,10 +120,12 @@ describe('an edge on a port that is gone', () => {
edit.connect(number, 'value', call, 'in1');
const node = () => caller.graph.nodes.find((n) => n.id === call.id)!;
const real = () => resolveDef(node(), caller.graph, graphLookup(f.project))!;
expect(drawnDef(node(), caller.graph, real()).inputs.filter((p) => p.ghost)).toHaveLength(0);
expect(
drawnDef(node(), caller.graph, real(), graphLookup(f.project)).inputs.filter((p) => p.ghost),
).toHaveLength(0);
f.stack.execute(new RemoveNodeCommand(callee.id, start.id));
expect(
drawnDef(node(), caller.graph, real())
drawnDef(node(), caller.graph, real(), graphLookup(f.project))
.inputs.filter((p) => p.ghost)
.map((p) => p.key),
).toEqual(['in1']);
+205
View File
@@ -0,0 +1,205 @@
/**
* Ports carrying one value (`NodeDef.sharedType`, #207): the outputs take the type the inputs agree
* on, the inputs stay `any`. Only the editor sees this – the interpreter passes `Value` either way.
*/
import { describe, expect, it } from 'vitest';
import {
agreedType,
canConnect,
drawnDef,
type GraphNode,
type PortDef,
portsCompatible,
portTypeOf,
resolveDef,
} from '../../src/core/graph';
import { graphLookup } from '../../src/core/model/graphLookup';
import type { BehaviorAsset } from '../../src/core/model/Asset';
import { graphEditor, makeBehavior, makeProject, type ProjectFixture } from '../helpers';
function fixture() {
const project = makeProject();
const behavior = makeBehavior(project, 'B');
return { ...project, behavior, edit: graphEditor(project, behavior) };
}
const typeOf = (
behavior: BehaviorAsset,
node: GraphNode,
key: string,
fixture: ProjectFixture,
side: 'inputs' | 'outputs' = 'outputs',
) => resolveDef(node, behavior.graph, graphLookup(fixture.project))![side].find((p) => p.key === key)!.type;
describe('the type a group of ports agrees on', () => {
it('is any unless every source is known and the same', () => {
expect(agreedType(['number', 'number'])).toBe('number');
expect(agreedType(['string'])).toBe('string');
expect(agreedType(['number', 'string'])).toBe('any');
expect(agreedType(['number', undefined])).toBe('any');
expect(agreedType([undefined, undefined])).toBe('any');
expect(agreedType(['any', 'any'])).toBe('any');
expect(agreedType(['exec', 'exec'])).toBe('any');
expect(agreedType([])).toBe('any');
});
});
describe('First not empty, whose a, b and value carry the same value', () => {
it('hands out any while nothing is connected', () => {
const f = fixture();
const node = f.edit.addNode('FirstNotEmpty');
expect(typeOf(f.behavior, node, 'value', f)).toBe('any');
});
it('takes the type once both inputs carry it', () => {
const f = fixture();
const node = f.edit.addNode('FirstNotEmpty');
f.edit.connect(f.edit.addNode('Number'), 'value', node, 'a');
// One input says nothing yet: the other could still become anything.
expect(typeOf(f.behavior, node, 'value', f)).toBe('any');
f.edit.connect(f.edit.addNode('Number'), 'value', node, 'b');
expect(typeOf(f.behavior, node, 'value', f)).toBe('number');
});
it('falls back to any when the two inputs disagree', () => {
const f = fixture();
const node = f.edit.addNode('FirstNotEmpty');
f.edit.connect(f.edit.addNode('Number'), 'value', node, 'a');
f.edit.connect(f.edit.addNode('String'), 'value', node, 'b');
expect(typeOf(f.behavior, node, 'value', f)).toBe('any');
});
it('is any again as soon as one of the edges is undone', () => {
const f = fixture();
const node = f.edit.addNode('FirstNotEmpty');
f.edit.connect(f.edit.addNode('Self'), 'object', node, 'a');
f.edit.connect(f.edit.addNode('Self'), 'object', node, 'b');
expect(typeOf(f.behavior, node, 'value', f)).toBe('object');
f.stack.undo();
expect(typeOf(f.behavior, node, 'value', f)).toBe('any');
});
it('keeps its inputs any, so a connected one can be swapped for another type', () => {
const f = fixture();
const node = f.edit.addNode('FirstNotEmpty');
f.edit.wire([
[f.edit.addNode('Number'), 'value', node, 'a'],
[f.edit.addNode('Number'), 'value', node, 'b'],
]);
expect(typeOf(f.behavior, node, 'a', f, 'inputs')).toBe('any');
const text = f.edit.addNode('String');
const may = canConnect(
f.behavior.graph,
{ nodeId: text.id, port: 'value', isOutput: true },
{ nodeId: node.id, port: 'a', isOutput: false },
graphLookup(f.project),
);
expect(may).toBe(true);
});
it('does not pass the type on through a second one of its kind', () => {
const f = fixture();
const first = f.edit.addNode('FirstNotEmpty');
const second = f.edit.addNode('FirstNotEmpty');
f.edit.wire([
[f.edit.addNode('Number'), 'value', first, 'a'],
[f.edit.addNode('Number'), 'value', first, 'b'],
[first, 'value', second, 'a'],
[first, 'value', second, 'b'],
]);
expect(typeOf(f.behavior, first, 'value', f)).toBe('number');
// A chain would have to resolve its neighbour to answer, and two of them would ask in circles.
expect(typeOf(f.behavior, second, 'value', f)).toBe('any');
});
it('leaves an edge that the new type makes invalid in place, for the canvas to mark', () => {
const f = fixture();
const node = f.edit.addNode('FirstNotEmpty');
const join = f.edit.addNode('Substring');
f.edit.connect(node, 'value', join, 'text');
f.edit.wire([
[f.edit.addNode('Number'), 'value', node, 'a'],
[f.edit.addNode('Number'), 'value', node, 'b'],
]);
expect(f.behavior.graph.edges.filter((e) => e.to.node === join.id)).toHaveLength(1);
const lookup = graphLookup(f.project);
const out = portTypeOf(f.behavior.graph, { nodeId: node.id, port: 'value', isOutput: true }, lookup)!;
const inp = portTypeOf(f.behavior.graph, { nodeId: join.id, port: 'text', isOutput: false }, lookup)!;
expect(portsCompatible(out, inp)).toBe(false);
});
});
/** As the canvas draws it: `drawnDef` is the only place a pin colour is decided. */
const inputPin = (behavior: BehaviorAsset, node: GraphNode, key: string, f: ProjectFixture): PortDef => {
const lookup = graphLookup(f.project);
const real = resolveDef(node, behavior.graph, lookup)!;
return drawnDef(node, behavior.graph, real, lookup).inputs.find((p) => p.key === key)!;
};
describe('an any input painted as what flows into it', () => {
it('stays grey while nothing is connected', () => {
const f = fixture();
const node = f.edit.addNode('FirstNotEmpty');
expect(inputPin(f.behavior, node, 'a', f).paintType).toBeUndefined();
});
it('takes the colour of its own edge while its type stays any', () => {
const f = fixture();
const node = f.edit.addNode('FirstNotEmpty');
f.edit.connect(f.edit.addNode('Number'), 'value', node, 'a');
const pin = inputPin(f.behavior, node, 'a', f);
expect(pin.paintType).toBe('number');
// The type is what canConnect reads: painted or not, the port still takes everything.
expect(pin.type).toBe('any');
});
it('paints each input on its own, so a disagreeing pair shows two colours', () => {
const f = fixture();
const node = f.edit.addNode('FirstNotEmpty');
f.edit.wire([
[f.edit.addNode('Number'), 'value', node, 'a'],
[f.edit.addNode('String'), 'value', node, 'b'],
]);
expect(inputPin(f.behavior, node, 'a', f).paintType).toBe('number');
expect(inputPin(f.behavior, node, 'b', f).paintType).toBe('string');
// Exactly the case in which the output says nothing – the pins say why.
expect(typeOf(f.behavior, node, 'value', f)).toBe('any');
});
it('is grey again once the edge is undone', () => {
const f = fixture();
const node = f.edit.addNode('FirstNotEmpty');
f.edit.connect(f.edit.addNode('Self'), 'object', node, 'a');
expect(inputPin(f.behavior, node, 'a', f).paintType).toBe('object');
f.stack.undo();
expect(inputPin(f.behavior, node, 'a', f).paintType).toBeUndefined();
});
it('follows the colour the edge itself has, even where the output inherited it', () => {
const f = fixture();
const first = f.edit.addNode('FirstNotEmpty');
const second = f.edit.addNode('FirstNotEmpty');
f.edit.wire([
[f.edit.addNode('Number'), 'value', first, 'a'],
[f.edit.addNode('Number'), 'value', first, 'b'],
[first, 'value', second, 'a'],
]);
// The edge is drawn in number, so the pin it ends on is too – the type is not passed on.
expect(inputPin(f.behavior, second, 'a', f).paintType).toBe('number');
expect(typeOf(f.behavior, second, 'value', f)).toBe('any');
});
it('leaves an input with a type of its own alone', () => {
const f = fixture();
const sub = f.edit.addNode('Substring');
f.edit.connect(f.edit.addNode('String'), 'value', sub, 'text');
const pin = inputPin(f.behavior, sub, 'text', f);
expect(pin.type).toBe('string');
expect(pin.paintType).toBeUndefined();
});
});