Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lineage checks in UI #2494

Merged
merged 8 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/common/nodes/checkNodeValidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Input, InputData, InputId, NodeSchema } from '../common-types';
import { FunctionInstance } from '../types/function';
import { generateAssignmentErrorTrace, printErrorTrace, simpleError } from '../types/mismatch';
import { withoutNull } from '../types/util';
import { assertNever } from '../util';
import { VALID, Validity, invalid } from '../Validity';
import { testInputCondition } from './condition';
import { ChainLineage, Lineage } from './lineage';
import { getRequireConditions } from './required';

const formatMissingInputs = (missingInputs: Input[]) => {
Expand All @@ -15,12 +17,16 @@ export interface CheckNodeValidityOptions {
inputData: InputData;
connectedInputs: ReadonlySet<InputId>;
functionInstance: FunctionInstance | undefined;
chainLineage: ChainLineage | undefined;
nodeId: string;
}
export const checkNodeValidity = ({
schema,
inputData,
connectedInputs,
functionInstance,
chainLineage,
nodeId,
}: CheckNodeValidityOptions): Validity => {
const isOptional = (input: Input): boolean => {
if (input.kind !== 'generic' || !input.optional) {
Expand Down Expand Up @@ -60,6 +66,7 @@ export const checkNodeValidity = ({
return invalid(formatMissingInputs(missingInputs));
}

// Type check
if (functionInstance) {
for (const { inputId, assignedType, inputType } of functionInstance.inputErrors) {
const input = schema.inputs.find((i) => i.id === inputId)!;
Expand Down Expand Up @@ -89,6 +96,29 @@ export const checkNodeValidity = ({
}
}

// Lineage check
if (chainLineage) {
for (const input of schema.inputs) {
const sourceLineage = chainLineage.getConnectedOutputLineage({
nodeId,
inputId: input.id,
});
if (sourceLineage !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
const lineageValid = checkAssignedLineage(
sourceLineage,
nodeId,
input.id,
schema,
chainLineage
);
if (!lineageValid.isValid) {
return lineageValid;
}
}
}
}

return VALID;
};

Expand Down Expand Up @@ -118,3 +148,62 @@ export const checkRequiredInputs = (schema: NodeSchema, inputData: InputData): V
reason: formatMissingInputs(missingInputs),
};
};

export const checkAssignedLineage = (
sourceLineage: Lineage | null,
nodeId: string,
inputId: InputId,
schema: NodeSchema,
chainLineage: ChainLineage
): Validity => {
const input = schema.inputs.find((i) => i.id === inputId)!;

const differentLineage = () =>
invalid('Cannot connect node to 2 different sequence of different origin.');
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
const nonIteratorInput = () => invalid(`Input ${input.label} cannot be connect to a sequence.`);
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved

switch (schema.kind) {
case 'regularNode': {
// regular is auto-iterated, so it has to be treated separately
if (sourceLineage) {
const targetLineage = chainLineage.getInputLineage(nodeId, {
exclude: new Set([inputId]),
});
if (targetLineage && !targetLineage.equals(sourceLineage)) {
return differentLineage();
}
} else {
// it's always valid connect a node with no lineage
}
break;
}
case 'newIterator': {
if (sourceLineage) {
return nonIteratorInput();
}
break;
}
case 'collector': {
const isIterated = schema.iteratorInputs[0].inputs.includes(inputId);
if (isIterated) {
if (!sourceLineage) {
return invalid(`Input ${input.label} expects a sequence.`);
}

const targetLineage = chainLineage.getInputLineage(nodeId, {
exclude: new Set([inputId]),
});
if (targetLineage && !targetLineage.equals(sourceLineage)) {
return differentLineage();
}
} else if (sourceLineage) {
return nonIteratorInput();
}
break;
}
default:
return assertNever(schema.kind);
}

return VALID;
};
138 changes: 88 additions & 50 deletions src/common/nodes/lineage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Edge, Node } from 'reactflow';
import { EdgeData, NodeData, NodeSchema } from '../common-types';
import { EdgeData, InputId, NodeData, NodeSchema } from '../common-types';
import { SchemaMap } from '../SchemaMap';
import {
EMPTY_ARRAY,
EMPTY_SET,
ParsedSourceHandle,
ParsedTargetHandle,
assertNever,
groupBy,
parseSourceHandle,
parseTargetHandle,
stringifyTargetHandle,
} from '../util';

Expand Down Expand Up @@ -57,82 +59,107 @@ export class ChainLineage {

static readonly EMPTY: ChainLineage = new ChainLineage(SchemaMap.EMPTY, [], []);

getEdgeByTarget(handle: ParsedTargetHandle): Edge<EdgeData> | undefined {
private getEdgeByTarget(handle: ParsedTargetHandle): Edge<EdgeData> | undefined {
return this.byTargetHandle.get(stringifyTargetHandle(handle));
}

/**
* Returns the single lineage (if any) of all iterated inputs of the given node.
*
* Note: regular nodes are auto-iterated, so their lineage is that of the first iterated input (if any).
*
* Note: the input lineage of collector nodes is `null` if there are no connected iterated inputs (invalid chain).
*/
getInputLineage(nodeId: string): Lineage | null {
const schema = this.nodeSchemata.get(nodeId);
if (!schema) return null;

private getInputLineageImpl(
nodeId: string,
schema: NodeSchema,
exclude: ReadonlySet<InputId>
): Lineage | null {
switch (schema.kind) {
case 'newIterator': {
// iterator source nodes do not support iterated inputs
return null;
}
case 'regularNode': {
// regular nodes are auto-iterated, so their lineage is that of the first iterated input
let lineage = this.nodeLineageCache.get(nodeId);
if (lineage === undefined) {
lineage = null;

const edges = this.byTargetNode.get(nodeId) ?? EMPTY_ARRAY;
for (const edge of edges) {
const inputLineage = this.getOutputLineage(
parseSourceHandle(edge.sourceHandle!)
);
if (inputLineage !== null) {
lineage = inputLineage;
break;
}
let lineage: Lineage | null = null;

const edges = this.byTargetNode.get(nodeId) ?? EMPTY_ARRAY;
for (const edge of edges) {
if (
exclude.size > 0 &&
exclude.has(parseTargetHandle(edge.targetHandle!).inputId)
) {
// eslint-disable-next-line no-continue
continue;
}

this.nodeLineageCache.set(nodeId, lineage);
const inputLineage = this.getOutputLineage(
parseSourceHandle(edge.sourceHandle!)
);
if (inputLineage !== null) {
lineage = inputLineage;
break;
}
}

return lineage;
}
case 'collector': {
// collectors already return non-iterator outputs
let lineage = this.nodeLineageCache.get(nodeId);
if (lineage === undefined) {
lineage = null;

if (schema.iteratorInputs.length !== 1) {
throw new Error(
`Collector nodes should have exactly 1 iterator input info (${schema.schemaId})`
);
}
const info = schema.iteratorInputs[0];
let lineage: Lineage | null = null;

for (const inputId of info.inputs) {
const edge = this.getEdgeByTarget({ nodeId, inputId });
// eslint-disable-next-line no-continue
if (!edge) continue;

const handle = parseSourceHandle(edge.sourceHandle!);
const inputLineage = this.getOutputLineage(handle);
if (inputLineage !== null) {
lineage = inputLineage;
break;
}
}
if (schema.iteratorInputs.length !== 1) {
throw new Error(
`Collector nodes should have exactly 1 iterator input info (${schema.schemaId})`
);
}
const info = schema.iteratorInputs[0];

for (const inputId of info.inputs) {
// eslint-disable-next-line no-continue
if (exclude.has(inputId)) continue;

this.nodeLineageCache.set(nodeId, lineage);
const edge = this.getEdgeByTarget({ nodeId, inputId });
// eslint-disable-next-line no-continue
if (!edge) continue;

const handle = parseSourceHandle(edge.sourceHandle!);
const inputLineage = this.getOutputLineage(handle);
if (inputLineage !== null) {
lineage = inputLineage;
break;
}
}

return lineage;
}
default:
return assertNever(schema.kind);
}
}

/**
* Returns the single lineage (if any) of all iterated inputs of the given node.
*
* Note: regular nodes are auto-iterated, so their lineage is that of the first iterated input (if any).
*
* Note: the input lineage of collector nodes is `null` if there are no connected iterated inputs (invalid chain).
*/
getInputLineage(
nodeId: string,
{ exclude }: { exclude?: ReadonlySet<InputId> } = {}
): Lineage | null {
const schema = this.nodeSchemata.get(nodeId);
if (!schema) return null;

const useCache = exclude === undefined || exclude.size === 0;

if (!useCache) {
return this.getInputLineageImpl(nodeId, schema, exclude);
}

let lineage = this.nodeLineageCache.get(nodeId);
if (lineage === undefined) {
lineage = this.getInputLineageImpl(nodeId, schema, EMPTY_SET);
this.nodeLineageCache.set(nodeId, lineage);
}
return lineage;
}

/**
* Returns the lineage of the given specific output.
*/
Expand Down Expand Up @@ -164,4 +191,15 @@ export class ChainLineage {
return assertNever(schema.kind);
}
}

/**
* Returns `getOutputLineage` for the source handle connected to the given target handle.
* If no such connection exists, returns `undefined`.
*
*/
getConnectedOutputLineage(target: ParsedTargetHandle): Lineage | null | undefined {
const edge = this.getEdgeByTarget(target);
if (!edge) return undefined;
return this.getOutputLineage(parseSourceHandle(edge.sourceHandle!));
}
}
4 changes: 4 additions & 0 deletions src/main/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { log } from '../../common/log';
import { checkNodeValidity } from '../../common/nodes/checkNodeValidity';
import { getConnectedInputs } from '../../common/nodes/connectedInputs';
import { getEffectivelyDisabledNodes } from '../../common/nodes/disabled';
import { ChainLineage } from '../../common/nodes/lineage';
import { parseFunctionDefinitions } from '../../common/nodes/parseFunctionDefinitions';
import { getNodesWithSideEffects } from '../../common/nodes/sideEffect';
import { toBackendJson } from '../../common/nodes/toBackendJson';
Expand Down Expand Up @@ -154,6 +155,7 @@ const ensureStaticCorrectness = (

const byId = new Map(nodes.map((n) => [n.id, n]));
const typeState = TypeState.create(byId, edges, new Map(), functionDefinitions);
const chainLineage = new ChainLineage(schemata, nodes, edges);

const invalidNodes = nodes.flatMap((node) => {
const functionInstance = typeState.functions.get(node.data.id);
Expand All @@ -164,6 +166,8 @@ const ensureStaticCorrectness = (
connectedInputs: getConnectedInputs(node.id, edges),
schema,
functionInstance,
chainLineage,
nodeId: node.id,
});
if (validity.isValid) return [];

Expand Down
2 changes: 2 additions & 0 deletions src/renderer/components/NodeDocumentation/NodeExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ export const NodeExample = memo(({ accentColor, selectedSchema }: NodeExamplePro
connectedInputs: requiredGenericInputs,
inputData,
functionInstance: typeInfo.instance,
chainLineage: undefined,
nodeId,
});

const { iteratedInputs, iteratedOutputs } = useMemo(() => {
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/contexts/ExecutionContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ const useRegisterNodeEvents = (
export const ExecutionProvider = memo(({ children }: React.PropsWithChildren<{}>) => {
const {
typeStateRef,
chainLineageRef,
outputDataActions,
getInputHash,
setManualOutputType,
Expand Down Expand Up @@ -384,6 +385,8 @@ export const ExecutionProvider = memo(({ children }: React.PropsWithChildren<{}>
connectedInputs: getConnectedInputs(node.id, edges),
schema,
functionInstance,
chainLineage: chainLineageRef.current,
nodeId: node.id,
})
);
if (validity.isValid) return [];
Expand Down Expand Up @@ -448,6 +451,7 @@ export const ExecutionProvider = memo(({ children }: React.PropsWithChildren<{}>
schemata,
sendAlert,
typeStateRef,
chainLineageRef,
features,
featureStates,
backend,
Expand Down
Loading