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

+113
View File
@@ -16,6 +16,7 @@ export async function graph(t) {
drag,
mouse,
locate,
mutate,
typeText,
press,
holdKey,
@@ -928,5 +929,117 @@ export async function graph(t) {
await click(reopen.x, reopen.y);
check('expanding leaves no trace in the file', !('collapsed' in (await savedMove())));
// 6z. Ports carrying one value (#207): the output takes the type both inputs agree on, the
// inputs stay open for everything, and an edge the new type breaks is drawn as broken.
await mutate(`
const p = __gm.state.project;
p.assets.push({
id: 'picky',
type: 'behavior',
name: 'Picky',
props: { enabled: true, runInEditor: false },
variables: [],
graph: {
nodes: [
{ id: 'p-a', type: 'Number', x: 0, y: 0, params: { value: 1 } },
{ id: 'p-b', type: 'Number', x: 0, y: 150, params: { value: 2 } },
{ id: 'p-text', type: 'String', x: 0, y: 300, params: { value: 'hi' } },
{ id: 'p-pick', type: 'FirstNotEmpty', x: 300, y: 60, params: {} },
{ id: 'p-sub', type: 'Substring', x: 600, y: 60, params: {} },
],
edges: [],
},
});
__gm.state.select({ kind: 'asset', assetId: 'picky' });
`);
await showTab('graph');
const pickTypes = async () => json(`__gm.graph.portTypes('p-pick')`);
check(
'a group with nothing plugged in stays any',
(await pickTypes()).outputs.value === 'any',
JSON.stringify(await pickTypes()),
);
const wire = async (fromNode, fromPort, toNode, toPort) => {
const from = await json(
`__gm.graph.portAt(${JSON.stringify(fromNode)}, ${JSON.stringify(fromPort)}, true)`,
);
const to = await json(`__gm.graph.portAt(${JSON.stringify(toNode)}, ${JSON.stringify(toPort)}, false)`);
await drag(from.x, from.y, to.x, to.y, 16);
};
// Laid while the output is still any, so a number later makes it a text input fed by a number.
await wire('p-pick', 'value', 'p-sub', 'text');
const toSub = async () => (await asset('picky')).graph.edges.find((e) => e.to.node === 'p-sub');
const outEdge = await toSub();
const edgeColor = async () => json(`__gm.graph.edgeColor(${JSON.stringify(outEdge?.id ?? '')})`);
const danger = await json(`__gm.themeColor('danger')`);
check(
'an any output connects to a text input and is drawn as a working link',
outEdge !== undefined && (await edgeColor()) !== danger,
JSON.stringify({ edge: Boolean(outEdge), color: await edgeColor(), danger }),
);
await wire('p-a', 'value', 'p-pick', 'a');
check(
'one input alone says nothing: the other could still become anything',
(await pickTypes()).outputs.value === 'any',
JSON.stringify(await pickTypes()),
);
await wire('p-b', 'value', 'p-pick', 'b');
const typed = await pickTypes();
check(
'with both inputs on a number the output is a number, the inputs stay any',
typed.outputs.value === 'number' && typed.inputs.a === 'any' && typed.inputs.b === 'any',
JSON.stringify(typed),
);
check(
'both input pins are painted as the number that flows in, though their type is any',
typed.paint.a === 'number' && typed.paint.b === 'number',
JSON.stringify(typed),
);
check(
'the edge the new type breaks stays and is drawn in the danger colour',
(await toSub()) !== undefined && (await edgeColor()) === danger,
JSON.stringify({ color: await edgeColor(), danger }),
);
await shot('19-shared-port-types');
// An input that keeps its `any` is one that can be re-plugged without unplugging first.
await wire('p-text', 'value', 'p-pick', 'a');
const mixed = await pickTypes();
check(
'a connected input takes another type, and the group opens up again',
mixed.outputs.value === 'any' && (await edgeColor()) !== danger,
JSON.stringify({ types: mixed, color: await edgeColor() }),
);
check(
'inputs fed by different types are painted apart, which is why the output says nothing',
mixed.paint.a === 'string' && mixed.paint.b === 'number' && mixed.inputs.a === 'any',
JSON.stringify(mixed),
);
// Read off the canvas: the colour is on the pin, not only in the definition.
const pinPixel = async (port) => {
const at = await json(`__gm.graph.portAt('p-pick', ${JSON.stringify(port)}, false)`);
return json(`__gm.pixelAt(${Math.round(at.x)}, ${Math.round(at.y)})`);
};
const pinColors = { a: await pinPixel('a'), b: await pinPixel('b') };
const wanted = {
a: await json(`__gm.themeColor('portString')`),
b: await json(`__gm.themeColor('portNumber')`),
};
check(
'the two pins are drawn in those two colours',
pinColors.a === wanted.a && pinColors.b === wanted.b,
JSON.stringify({ pinColors, wanted }),
);
await shot('20-input-pin-colours');
await mutate(`
const p = __gm.state.project;
p.assets = p.assets.filter((a) => a.id !== 'picky');
__gm.state.select({ kind: 'asset', assetId: 'mover' });
`);
await showTab('scene');
}