Compare commits
3 Commits
feat/analy
...
feat/react
| Author | SHA1 | Date | |
|---|---|---|---|
| d63a301cea | |||
| 3b52998f20 | |||
| 5cfc8882bd |
@@ -10,5 +10,4 @@ export {
|
||||
validateSchema,
|
||||
validateGraph,
|
||||
validate,
|
||||
} from "../graph/validation.js";
|
||||
export { validatePreconditions, validateTemplate } from "./workflow.js";
|
||||
} from "../graph/validation.js";
|
||||
@@ -1,197 +1 @@
|
||||
import type { TSchema } from "@alkdev/typebox";
|
||||
import { KindGuard } from "@alkdev/typebox";
|
||||
import type { UNode } from "@alkdev/ujsx";
|
||||
import { createHostRoot } from "@alkdev/ujsx";
|
||||
import { hasCycle } from "graphology-dag";
|
||||
import { DirectedGraph } from "graphology";
|
||||
import type { FlowGraph } from "../graph/construction.js";
|
||||
import type { OperationNodeAttrs } from "../schema/node.js";
|
||||
import type { OperationEdgeAttrs } from "../schema/edge.js";
|
||||
import type { ValidationError, AnyValidationError } from "../error/index.js";
|
||||
import { GraphologyHostConfig } from "../host/graphology.js";
|
||||
import { reachableFrom } from "../graph/queries.js";
|
||||
|
||||
function getRequiredTopLevelFields(schema: unknown): Set<string> {
|
||||
const fields = new Set<string>();
|
||||
if (schema === null || schema === undefined || typeof schema !== "object") return fields;
|
||||
const s = schema as TSchema;
|
||||
if (!KindGuard.IsObject(s)) return fields;
|
||||
const props = s.properties as Record<string, TSchema> | undefined;
|
||||
const required = s.required as string[] | undefined;
|
||||
if (props && required) {
|
||||
for (const key of required) {
|
||||
fields.add(key);
|
||||
}
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
function getProvidedFields(schema: unknown): Set<string> {
|
||||
const fields = new Set<string>();
|
||||
if (schema === null || schema === undefined || typeof schema !== "object") return fields;
|
||||
const s = schema as TSchema;
|
||||
if (!KindGuard.IsObject(s)) return fields;
|
||||
const props = s.properties as Record<string, TSchema> | undefined;
|
||||
if (props) {
|
||||
for (const key of Object.keys(props)) {
|
||||
fields.add(key);
|
||||
}
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
export function validatePreconditions(
|
||||
graph: FlowGraph<typeof import("../schema/node.js").OperationNodeAttrs, typeof import("../schema/edge.js").OperationEdgeAttrs>,
|
||||
): ValidationError[] {
|
||||
const errors: ValidationError[] = [];
|
||||
const nodeKeys = graph.nodes();
|
||||
|
||||
for (const nodeKey of nodeKeys) {
|
||||
const attrs = graph.getNodeAttributes(nodeKey) as unknown as OperationNodeAttrs;
|
||||
const inputSchema = attrs.inputSchema;
|
||||
const requiredFields = getRequiredTopLevelFields(inputSchema);
|
||||
|
||||
if (requiredFields.size === 0) continue;
|
||||
|
||||
const predecessors = graph.predecessors(nodeKey);
|
||||
if (predecessors.length === 0) {
|
||||
for (const field of requiredFields) {
|
||||
errors.push({
|
||||
type: "schema",
|
||||
nodeKey,
|
||||
field,
|
||||
message: `Required input field "${field}" has no predecessor providing it`,
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const providedFields = new Set<string>();
|
||||
for (const predKey of predecessors) {
|
||||
const predAttrs = graph.getNodeAttributes(predKey) as unknown as OperationNodeAttrs;
|
||||
const predProvided = getProvidedFields(predAttrs.outputSchema);
|
||||
for (const field of predProvided) {
|
||||
providedFields.add(field);
|
||||
}
|
||||
}
|
||||
|
||||
for (const field of requiredFields) {
|
||||
if (!providedFields.has(field)) {
|
||||
errors.push({
|
||||
type: "schema",
|
||||
nodeKey,
|
||||
field,
|
||||
message: `Required input field "${field}" is not provided by any predecessor`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
function collectOperationNodeKeys(dag: DirectedGraph): string[] {
|
||||
const names: string[] = [];
|
||||
dag.forEachNode((key) => {
|
||||
if (!key.startsWith("__")) {
|
||||
names.push(key);
|
||||
}
|
||||
});
|
||||
return names;
|
||||
}
|
||||
|
||||
export function validateTemplate(
|
||||
template: UNode,
|
||||
operationGraph: FlowGraph<typeof import("../schema/node.js").OperationNodeAttrs, typeof import("../schema/edge.js").OperationEdgeAttrs>,
|
||||
): AnyValidationError[] {
|
||||
const errors: AnyValidationError[] = [];
|
||||
|
||||
let renderedDag: DirectedGraph;
|
||||
try {
|
||||
const root = createHostRoot(GraphologyHostConfig, null);
|
||||
root.render(template);
|
||||
renderedDag = root.ctx.graph as DirectedGraph;
|
||||
} catch {
|
||||
renderedDag = new DirectedGraph();
|
||||
}
|
||||
|
||||
const templateNodeKeys = collectOperationNodeKeys(renderedDag);
|
||||
const graphNodeKeys = new Set(operationGraph.nodes());
|
||||
|
||||
for (const opKey of templateNodeKeys) {
|
||||
if (!graphNodeKeys.has(opKey)) {
|
||||
errors.push({
|
||||
type: "graph",
|
||||
category: "orphan-node",
|
||||
details: { operation: opKey, message: `Operation "${opKey}" not found in operation graph` },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (hasCycle(renderedDag)) {
|
||||
errors.push({
|
||||
type: "graph",
|
||||
category: "cycle",
|
||||
details: { message: "Rendered template DAG contains a cycle" },
|
||||
});
|
||||
}
|
||||
|
||||
for (const opKey of templateNodeKeys) {
|
||||
if (!graphNodeKeys.has(opKey)) continue;
|
||||
const outEdges = renderedDag.outEdges(opKey) ?? [];
|
||||
for (const edge of outEdges) {
|
||||
const target = renderedDag.target(edge);
|
||||
if (target.startsWith("__")) continue;
|
||||
if (!graphNodeKeys.has(target)) continue;
|
||||
if (operationGraph.hasEdge(opKey, target)) {
|
||||
const edgeAttrs = operationGraph.getEdgeAttributes(opKey, target) as unknown as OperationEdgeAttrs;
|
||||
if (!edgeAttrs.compatible) {
|
||||
errors.push({
|
||||
type: "type-compat",
|
||||
sourceKey: opKey,
|
||||
targetKey: target,
|
||||
compatible: false,
|
||||
mismatches: edgeAttrs.mismatches ?? [],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (templateNodeKeys.length > 1) {
|
||||
const roots: string[] = [];
|
||||
for (const key of templateNodeKeys) {
|
||||
const inDegree = renderedDag.inDegree(key);
|
||||
if (inDegree === 0) {
|
||||
roots.push(key);
|
||||
}
|
||||
}
|
||||
|
||||
if (roots.length > 0) {
|
||||
const reachable = reachableFrom(renderedDag, roots);
|
||||
for (const nodeKey of templateNodeKeys) {
|
||||
if (!reachable.has(nodeKey)) {
|
||||
errors.push({
|
||||
type: "graph",
|
||||
category: "orphan-node",
|
||||
details: { nodeKey, message: `Operation "${nodeKey}" is not reachable from start` },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const nodeKey of templateNodeKeys) {
|
||||
const inDegree = renderedDag.inDegree(nodeKey);
|
||||
const outDegree = renderedDag.outDegree(nodeKey);
|
||||
if (inDegree === 0 && outDegree === 0 && templateNodeKeys.length > 1) {
|
||||
errors.push({
|
||||
type: "graph",
|
||||
category: "orphan-node",
|
||||
details: { nodeKey, message: `Operation "${nodeKey}" has no edges (orphan node)` },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
export {};
|
||||
@@ -1,13 +1,15 @@
|
||||
import { DirectedGraph } from "graphology";
|
||||
import type { TSchema, Static } from "@alkdev/typebox";
|
||||
import { Value } from "@alkdev/typebox/value";
|
||||
import { willCreateCycle, topologicalSort, hasCycle } from "graphology-dag";
|
||||
import {
|
||||
DuplicateNodeError,
|
||||
DuplicateEdgeError,
|
||||
NodeNotFoundError,
|
||||
CycleError,
|
||||
InvalidInputError,
|
||||
} from "../error/index.js";
|
||||
import type { CallStatus, AnyValidationError } from "../error/index.js";
|
||||
import type { CallStatus, AnyValidationError, ValidationError } from "../error/index.js";
|
||||
import {
|
||||
findCycles,
|
||||
reachableFrom as reachableFromFn,
|
||||
@@ -16,8 +18,10 @@ import { validate as _validate } from "./validation.js";
|
||||
import {
|
||||
OperationNodeAttrs as OperationNodeAttrsSchema,
|
||||
OperationEdgeAttrs as OperationEdgeAttrsSchema,
|
||||
OperationGraphSerialized,
|
||||
CallGraphSerialized,
|
||||
} from "../schema/index.js";
|
||||
import type { OperationNodeAttrs } from "../schema/index.js";
|
||||
import type { OperationNodeAttrs, FlowGraphSerialized } from "../schema/index.js";
|
||||
import { typeCompat, type TypeCompatResult } from "../analysis/type-compat.js";
|
||||
|
||||
export interface FlowGraphOptions {
|
||||
@@ -367,10 +371,64 @@ export class FlowGraph<
|
||||
throw new Error("not implemented");
|
||||
}
|
||||
|
||||
export(): FlowGraphSerialized {
|
||||
return this._graph.export() as unknown as FlowGraphSerialized;
|
||||
}
|
||||
|
||||
toJSON(): FlowGraphSerialized {
|
||||
return this.export();
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return JSON.stringify(this.export());
|
||||
}
|
||||
|
||||
static fromJSON(
|
||||
_data: unknown,
|
||||
data: FlowGraphSerialized,
|
||||
): FlowGraph<TSchema, TSchema> {
|
||||
throw new Error("not implemented");
|
||||
const opCheck = Value.Check(OperationGraphSerialized, data);
|
||||
const callCheck = Value.Check(CallGraphSerialized, data);
|
||||
if (!opCheck && !callCheck) {
|
||||
const errors: ValidationError[] = [];
|
||||
const opIter = Value.Errors(OperationGraphSerialized, data as Record<string, unknown>);
|
||||
for (const err of opIter) {
|
||||
errors.push({
|
||||
type: "schema",
|
||||
nodeKey: "",
|
||||
field: err.path.replace(/^\//, "") || err.path,
|
||||
message: err.message,
|
||||
value: err.value,
|
||||
});
|
||||
}
|
||||
if (errors.length === 0) {
|
||||
const callIter = Value.Errors(CallGraphSerialized, data as Record<string, unknown>);
|
||||
for (const err of callIter) {
|
||||
errors.push({
|
||||
type: "schema",
|
||||
nodeKey: "",
|
||||
field: err.path.replace(/^\//, "") || err.path,
|
||||
message: err.message,
|
||||
value: err.value,
|
||||
});
|
||||
}
|
||||
}
|
||||
throw new InvalidInputError(errors);
|
||||
}
|
||||
|
||||
const fg = new FlowGraph<TSchema, TSchema>();
|
||||
for (const node of data.nodes) {
|
||||
fg._graph.addNode(node.key, node.attributes as Attrs);
|
||||
}
|
||||
for (const edge of data.edges) {
|
||||
fg._graph.addEdgeWithKey(edge.key, edge.source, edge.target, edge.attributes as Attrs);
|
||||
}
|
||||
|
||||
if (hasCycle(fg._graph)) {
|
||||
const cycles = findCycles(fg._graph);
|
||||
throw new CycleError(cycles);
|
||||
}
|
||||
|
||||
return fg;
|
||||
}
|
||||
|
||||
private _findPath(from: string, to: string): string[] {
|
||||
|
||||
@@ -88,6 +88,7 @@ export class WorkflowReactiveRoot implements EventLogProjection {
|
||||
blockedByFailure: Map<string, ReadonlySignal<boolean>>;
|
||||
resultMap: Map<string, ReadonlySignal<CallResult | undefined>>;
|
||||
nodeKeyToRequestId: Map<string, string>;
|
||||
requestIdToNodeKey: Map<string, string>;
|
||||
|
||||
private graph: DirectedGraph;
|
||||
private effectDisposers: (() => void)[];
|
||||
@@ -106,10 +107,16 @@ export class WorkflowReactiveRoot implements EventLogProjection {
|
||||
this.effectDisposers = [];
|
||||
this.eventLog = [];
|
||||
this.nodeKeyToRequestId = new Map();
|
||||
this.requestIdToNodeKey = new Map();
|
||||
this._failurePolicy = options?.failurePolicy ?? "continue-running";
|
||||
this.initializeSignals();
|
||||
}
|
||||
|
||||
setRequestId(nodeKey: string, requestId: string): void {
|
||||
this.nodeKeyToRequestId.set(nodeKey, requestId);
|
||||
this.requestIdToNodeKey.set(requestId, nodeKey);
|
||||
}
|
||||
|
||||
private initializeSignals(): void {
|
||||
for (const node of this.graph.nodes()) {
|
||||
const predecessors: string[] = this.graph.inNeighbors(node) ?? [];
|
||||
@@ -213,15 +220,29 @@ export class WorkflowReactiveRoot implements EventLogProjection {
|
||||
|
||||
if (!("requestId" in event)) return;
|
||||
|
||||
const nodeId = this.findNodeByRequestId(event.requestId);
|
||||
let nodeId = this.requestIdToNodeKey.get(event.requestId);
|
||||
|
||||
if (nodeId === undefined) {
|
||||
for (const [nId, rid] of this.nodeKeyToRequestId) {
|
||||
if (rid === event.requestId) {
|
||||
nodeId = nId;
|
||||
this.requestIdToNodeKey.set(event.requestId, nId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (nodeId === undefined) return;
|
||||
|
||||
const statusSignal = this.statusMap.get(nodeId);
|
||||
if (!statusSignal) return;
|
||||
const currentRequestId = this.nodeKeyToRequestId.get(nodeId);
|
||||
if (currentRequestId === event.requestId) {
|
||||
const statusSignal = this.statusMap.get(nodeId);
|
||||
if (!statusSignal) return;
|
||||
|
||||
const derived = EVENT_TO_STATUS[event.type];
|
||||
if (derived !== undefined) {
|
||||
statusSignal.value = derived;
|
||||
const derived = EVENT_TO_STATUS[event.type];
|
||||
if (derived !== undefined) {
|
||||
statusSignal.value = derived;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,12 +275,17 @@ export class WorkflowReactiveRoot implements EventLogProjection {
|
||||
}
|
||||
|
||||
getEvents(nodeId: string): CallEventMapValue[] {
|
||||
const requestId = this.nodeKeyToRequestId.get(nodeId);
|
||||
if (!requestId) return [];
|
||||
const requestIds = new Set<string>();
|
||||
for (const [rid, nId] of this.requestIdToNodeKey) {
|
||||
if (nId === nodeId) {
|
||||
requestIds.add(rid);
|
||||
}
|
||||
}
|
||||
if (requestIds.size === 0) return [];
|
||||
|
||||
const events: CallEventMapValue[] = [];
|
||||
for (const e of this.eventLog) {
|
||||
if ("requestId" in e && e.requestId === requestId) {
|
||||
if ("requestId" in e && requestIds.has(e.requestId)) {
|
||||
events.push(e);
|
||||
}
|
||||
}
|
||||
@@ -325,13 +351,7 @@ export class WorkflowReactiveRoot implements EventLogProjection {
|
||||
this.blockedByFailure.clear();
|
||||
this.resultMap.clear();
|
||||
this.nodeKeyToRequestId.clear();
|
||||
this.requestIdToNodeKey.clear();
|
||||
this.eventLog = [];
|
||||
}
|
||||
|
||||
private findNodeByRequestId(requestId: string): string | undefined {
|
||||
for (const [nodeId, rid] of this.nodeKeyToRequestId) {
|
||||
if (rid === requestId) return nodeId;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
id: graph/construction-json
|
||||
name: Implement fromJSON and export/toJSON serialization for FlowGraph
|
||||
status: pending
|
||||
status: completed
|
||||
depends_on:
|
||||
- graph/flowgraph-class
|
||||
- schema/graph-schemas
|
||||
|
||||
@@ -1,251 +1,7 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { Type } from "@alkdev/typebox";
|
||||
import { h, createHostRoot } from "@alkdev/ujsx";
|
||||
import { Operation, Sequential, Parallel, Conditional } from "../../src/component/index.js";
|
||||
import { validatePreconditions, validateTemplate } from "../../src/analysis/workflow.js";
|
||||
import { FlowGraph } from "../../src/graph/construction.js";
|
||||
import type { OperationNodeAttrs, OperationEdgeAttrs } from "../../src/schema/index.js";
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
type OpGraph = FlowGraph<typeof import("../../src/schema/node.js").OperationNodeAttrs, typeof import("../../src/schema/edge.js").OperationEdgeAttrs>;
|
||||
|
||||
function createOperationGraph(
|
||||
specs: Array<{
|
||||
name: string;
|
||||
namespace?: string;
|
||||
inputSchema?: Record<string, unknown>;
|
||||
outputSchema?: Record<string, unknown>;
|
||||
}>,
|
||||
): OpGraph {
|
||||
const graph = new FlowGraph() as OpGraph;
|
||||
for (const spec of specs) {
|
||||
const ns = spec.namespace ?? "test";
|
||||
const key = `${ns}.${spec.name}`;
|
||||
graph.addNode(key, {
|
||||
name: spec.name,
|
||||
namespace: ns,
|
||||
version: "1.0.0",
|
||||
type: "query",
|
||||
inputSchema: spec.inputSchema ?? Type.Object({}),
|
||||
outputSchema: spec.outputSchema ?? Type.Object({}),
|
||||
} as OperationNodeAttrs);
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
|
||||
function createOperationGraphWithEdges(
|
||||
specs: Array<{
|
||||
name: string;
|
||||
namespace?: string;
|
||||
inputSchema?: Record<string, unknown>;
|
||||
outputSchema?: Record<string, unknown>;
|
||||
}>,
|
||||
edges?: Array<{ source: string; target: string; compatible: boolean; mismatches?: Array<{ path: string; expected: string; actual: string }> }>,
|
||||
): OpGraph {
|
||||
const graph = createOperationGraph(specs);
|
||||
if (edges) {
|
||||
for (const edge of edges) {
|
||||
graph.addTypedEdge(edge.source, edge.target, {
|
||||
compatible: edge.compatible,
|
||||
mismatches: edge.mismatches,
|
||||
});
|
||||
}
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
|
||||
describe("validatePreconditions", () => {
|
||||
it("returns empty for valid graph with no required fields", () => {
|
||||
const graph = createOperationGraph([
|
||||
{ name: "a", outputSchema: Type.Object({ x: Type.Number() }) },
|
||||
{ name: "b", inputSchema: Type.Object({}) },
|
||||
]);
|
||||
graph.addEdge("test.a", "test.b");
|
||||
const errors = validatePreconditions(graph);
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns empty when all required input fields are provided by predecessors", () => {
|
||||
const graph = createOperationGraph([
|
||||
{ name: "a", outputSchema: Type.Object({ x: Type.Number(), y: Type.String() }) },
|
||||
{ name: "b", inputSchema: Type.Object({ x: Type.Number() }) },
|
||||
]);
|
||||
graph.addEdge("test.a", "test.b");
|
||||
const errors = validatePreconditions(graph);
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns errors when required input field is not provided by any predecessor", () => {
|
||||
const graph = createOperationGraph([
|
||||
{ name: "a", outputSchema: Type.Object({ x: Type.Number() }) },
|
||||
{ name: "b", inputSchema: Type.Object({ x: Type.Number(), y: Type.String() }) },
|
||||
]);
|
||||
graph.addEdge("test.a", "test.b");
|
||||
const errors = validatePreconditions(graph);
|
||||
expect(errors.length).toBeGreaterThan(0);
|
||||
const fieldNames = errors.map((e) => e.field);
|
||||
expect(fieldNames).toContain("y");
|
||||
});
|
||||
|
||||
it("returns errors when node with required fields has no predecessors", () => {
|
||||
const graph = createOperationGraph([
|
||||
{ name: "a", inputSchema: Type.Object({ x: Type.Number() }), outputSchema: Type.Object({}) },
|
||||
]);
|
||||
const errors = validatePreconditions(graph);
|
||||
expect(errors.length).toBeGreaterThan(0);
|
||||
expect(errors[0]!.message).toContain("no predecessor");
|
||||
});
|
||||
|
||||
it("collects provided fields from multiple predecessors", () => {
|
||||
const graph = createOperationGraph([
|
||||
{ name: "a", outputSchema: Type.Object({ x: Type.Number() }) },
|
||||
{ name: "c", outputSchema: Type.Object({ y: Type.String() }) },
|
||||
{ name: "b", inputSchema: Type.Object({ x: Type.Number(), y: Type.String() }) },
|
||||
]);
|
||||
graph.addEdge("test.a", "test.b");
|
||||
graph.addEdge("test.c", "test.b");
|
||||
const errors = validatePreconditions(graph);
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns empty for graph with no nodes", () => {
|
||||
const graph = new FlowGraph() as OpGraph;
|
||||
const errors = validatePreconditions(graph);
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("validateTemplate", () => {
|
||||
it("returns empty for valid template with all operations in graph", () => {
|
||||
const graph = createOperationGraphWithEdges([
|
||||
{ name: "a", outputSchema: Type.Object({ x: Type.Number() }) },
|
||||
{ name: "b", inputSchema: Type.Object({ x: Type.Number() }) },
|
||||
], [
|
||||
{ source: "test.a", target: "test.b", compatible: true },
|
||||
]);
|
||||
const template = h(Sequential, {},
|
||||
h(Operation, { name: "test.a" }),
|
||||
h(Operation, { name: "test.b" }),
|
||||
);
|
||||
const errors = validateTemplate(template, graph);
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns error when operation name is not in operation graph", () => {
|
||||
const graph = createOperationGraph([
|
||||
{ name: "a" },
|
||||
]);
|
||||
const template = h(Sequential, {},
|
||||
h(Operation, { name: "test.a" }),
|
||||
h(Operation, { name: "test.missing" }),
|
||||
);
|
||||
const errors = validateTemplate(template, graph);
|
||||
expect(errors.length).toBeGreaterThan(0);
|
||||
const missingErrors = errors.filter(
|
||||
(e) => e.type === "graph" && (e as { type: string; category: string; details: unknown }).category === "orphan-node"
|
||||
&& JSON.stringify((e as { details: unknown }).details).includes("missing"),
|
||||
);
|
||||
expect(missingErrors.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("returns error for type incompatibility between sequential operations", () => {
|
||||
const graph = createOperationGraphWithEdges([
|
||||
{ name: "a", outputSchema: Type.Object({ x: Type.String() }) },
|
||||
{ name: "b", inputSchema: Type.Object({ x: Type.Number() }) },
|
||||
], [
|
||||
{ source: "test.a", target: "test.b", compatible: false, mismatches: [{ path: "/x", expected: "number", actual: "string" }] },
|
||||
]);
|
||||
const template = h(Sequential, {},
|
||||
h(Operation, { name: "test.a" }),
|
||||
h(Operation, { name: "test.b" }),
|
||||
);
|
||||
const errors = validateTemplate(template, graph);
|
||||
const typeErrors = errors.filter((e) => e.type === "type-compat");
|
||||
expect(typeErrors.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("returns empty for single-operation template", () => {
|
||||
const graph = createOperationGraph([
|
||||
{ name: "a" },
|
||||
]);
|
||||
const template = h(Operation, { name: "test.a" });
|
||||
const errors = validateTemplate(template, graph);
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
it("detects unreachable nodes", () => {
|
||||
const graph = createOperationGraphWithEdges([
|
||||
{ name: "a" },
|
||||
{ name: "b" },
|
||||
{ name: "c" },
|
||||
]);
|
||||
const template = h(Sequential, {},
|
||||
h(Operation, { name: "test.a" }),
|
||||
h(Operation, { name: "test.b" }),
|
||||
);
|
||||
const errors = validateTemplate(template, graph);
|
||||
const reachableErrors = errors.filter(
|
||||
(e) => e.type === "graph" && JSON.stringify((e as { details: unknown }).details).includes("not reachable"),
|
||||
);
|
||||
expect(reachableErrors.length).toBe(0);
|
||||
});
|
||||
|
||||
it("returns empty for valid parallel template", () => {
|
||||
const graph = createOperationGraphWithEdges([
|
||||
{ name: "a" },
|
||||
{ name: "b" },
|
||||
{ name: "c" },
|
||||
]);
|
||||
const template = h(Sequential, {},
|
||||
h(Operation, { name: "test.a" }),
|
||||
h(Parallel, {},
|
||||
h(Operation, { name: "test.b" }),
|
||||
h(Operation, { name: "test.c" }),
|
||||
),
|
||||
);
|
||||
const errors = validateTemplate(template, graph);
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
it("handles template with conditional", () => {
|
||||
const graph = createOperationGraphWithEdges([
|
||||
{ name: "a", outputSchema: Type.Object({ result: Type.Boolean() }) },
|
||||
{ name: "b" },
|
||||
{ name: "c" },
|
||||
]);
|
||||
const template = h(Sequential, {},
|
||||
h(Operation, { name: "test.a" }),
|
||||
h(Conditional, { test: "test.a" },
|
||||
h(Operation, { name: "test.b" }),
|
||||
),
|
||||
h(Operation, { name: "test.c" }),
|
||||
);
|
||||
const errors = validateTemplate(template, graph);
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
it("template validation is advisory - never throws", () => {
|
||||
const graph = new FlowGraph() as OpGraph;
|
||||
const template = h(Sequential, {},
|
||||
h(Operation, { name: "nonexistent" }),
|
||||
);
|
||||
const errors = validateTemplate(template, graph);
|
||||
expect(Array.isArray(errors)).toBe(true);
|
||||
});
|
||||
|
||||
it("detects orphan node with no edges in multi-node template", () => {
|
||||
const graph = createOperationGraphWithEdges([
|
||||
{ name: "a" },
|
||||
{ name: "b" },
|
||||
]);
|
||||
const template = h(Parallel, {},
|
||||
h(Operation, { name: "test.a" }),
|
||||
h(Operation, { name: "test.b" }),
|
||||
);
|
||||
const errors = validateTemplate(template, graph);
|
||||
const orphanErrors = errors.filter(
|
||||
(e) => e.type === "graph" && (e as { type: string; category: string }).category === "orphan-node"
|
||||
&& JSON.stringify((e as { details: unknown }).details).includes("no edges"),
|
||||
);
|
||||
expect(orphanErrors.length).toBeGreaterThan(0);
|
||||
describe('analysis workflow', () => {
|
||||
it('placeholder', () => {
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -303,10 +303,6 @@ describe("FlowGraph static stubs", () => {
|
||||
it("fromCallEvents throws not implemented", () => {
|
||||
expect(() => FlowGraph.fromCallEvents([])).toThrow("not implemented");
|
||||
});
|
||||
|
||||
it("fromJSON throws not implemented", () => {
|
||||
expect(() => FlowGraph.fromJSON({})).toThrow("not implemented");
|
||||
});
|
||||
});
|
||||
|
||||
describe("FlowGraph.addOperation", () => {
|
||||
|
||||
232
test/graph/serialization.test.ts
Normal file
232
test/graph/serialization.test.ts
Normal file
@@ -0,0 +1,232 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { Type } from "@alkdev/typebox";
|
||||
import { FlowGraph } from "../../src/graph/construction.js";
|
||||
import type { OperationSpec } from "../../src/graph/construction.js";
|
||||
import { InvalidInputError, CycleError } from "../../src/error/index.js";
|
||||
|
||||
describe("FlowGraph.export", () => {
|
||||
it("returns graphology native JSON format for empty graph", () => {
|
||||
const fg = new FlowGraph();
|
||||
const data = fg.export();
|
||||
expect(data.options).toEqual({ type: "directed", multi: false, allowSelfLoops: false });
|
||||
expect(data.attributes).toEqual({});
|
||||
expect(data.nodes).toEqual([]);
|
||||
expect(data.edges).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns graphology native JSON format for operation graph", () => {
|
||||
const specs: OperationSpec[] = [
|
||||
{ name: "extract", namespace: "task", version: "1.0.0", type: "query", inputSchema: Type.Object({ raw: Type.String() }), outputSchema: Type.Object({ text: Type.String() }) },
|
||||
{ name: "classify", namespace: "task", version: "1.0.0", type: "query", inputSchema: Type.Object({ text: Type.String() }), outputSchema: Type.Object({ label: Type.String() }) },
|
||||
];
|
||||
const graph = FlowGraph.fromSpecs(specs);
|
||||
const data = graph.export();
|
||||
expect(data.options.type).toBe("directed");
|
||||
expect(data.nodes.length).toBe(2);
|
||||
expect(data.edges.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("FlowGraph.toJSON", () => {
|
||||
it("is an alias for export", () => {
|
||||
const fg = new FlowGraph();
|
||||
fg.addNode("a", { name: "a" } as never);
|
||||
const exported = fg.export();
|
||||
const jsoned = fg.toJSON();
|
||||
expect(jsoned).toEqual(exported);
|
||||
});
|
||||
});
|
||||
|
||||
describe("FlowGraph.toString", () => {
|
||||
it("returns JSON.stringify of export()", () => {
|
||||
const fg = new FlowGraph();
|
||||
fg.addNode("a", { name: "a" } as never);
|
||||
expect(fg.toString()).toBe(JSON.stringify(fg.export()));
|
||||
});
|
||||
|
||||
it("round-trips through JSON.parse", () => {
|
||||
const fg = new FlowGraph();
|
||||
fg.addNode("a", { name: "a" } as never);
|
||||
const parsed = JSON.parse(fg.toString());
|
||||
expect(parsed.options).toEqual({ type: "directed", multi: false, allowSelfLoops: false });
|
||||
});
|
||||
});
|
||||
|
||||
describe("FlowGraph.fromJSON", () => {
|
||||
it("round-trips fromSpecs -> export -> fromJSON", () => {
|
||||
const specs: OperationSpec[] = [
|
||||
{ name: "extract", namespace: "task", version: "1.0.0", type: "query", inputSchema: Type.Object({ raw: Type.String() }), outputSchema: Type.Object({ text: Type.String() }) },
|
||||
{ name: "classify", namespace: "task", version: "1.0.0", type: "query", inputSchema: Type.Object({ text: Type.String() }), outputSchema: Type.Object({ label: Type.String() }) },
|
||||
];
|
||||
const original = FlowGraph.fromSpecs(specs);
|
||||
const data = original.export();
|
||||
const restored = FlowGraph.fromJSON(data);
|
||||
expect(restored.order).toBe(original.order);
|
||||
expect(restored.size).toBe(original.size);
|
||||
for (const node of original.nodes()) {
|
||||
expect(restored.hasNode(node)).toBe(true);
|
||||
const origAttrs = original.getNodeAttributes(node as never) as Record<string, unknown>;
|
||||
const restAttrs = restored.getNodeAttributes(node as never) as Record<string, unknown>;
|
||||
expect(restAttrs.name).toBe(origAttrs.name);
|
||||
expect(restAttrs.namespace).toBe(origAttrs.namespace);
|
||||
}
|
||||
for (const edge of original.edges()) {
|
||||
const source = edge.split("->")[0]!;
|
||||
const target = edge.split("->")[1]!;
|
||||
expect(restored.hasEdge(source, target)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("round-trips empty graph", () => {
|
||||
const fg = new FlowGraph();
|
||||
const data = fg.export();
|
||||
const restored = FlowGraph.fromJSON(data);
|
||||
expect(restored.order).toBe(0);
|
||||
expect(restored.size).toBe(0);
|
||||
expect(restored.export()).toEqual(data);
|
||||
});
|
||||
|
||||
it("throws InvalidInputError on invalid input", () => {
|
||||
expect(() => FlowGraph.fromJSON({})).toThrow(InvalidInputError);
|
||||
});
|
||||
|
||||
it("InvalidInputError contains errors array", () => {
|
||||
try {
|
||||
FlowGraph.fromJSON({});
|
||||
expect.unreachable("should throw");
|
||||
} catch (e) {
|
||||
expect(e).toBeInstanceOf(InvalidInputError);
|
||||
const err = e as InvalidInputError;
|
||||
expect(err.errors.length).toBeGreaterThan(0);
|
||||
}
|
||||
});
|
||||
|
||||
it("throws InvalidInputError on missing nodes", () => {
|
||||
const bad = {
|
||||
options: { type: "directed", multi: false, allowSelfLoops: false },
|
||||
attributes: {},
|
||||
edges: [],
|
||||
};
|
||||
expect(() => FlowGraph.fromJSON(bad as never)).toThrow(InvalidInputError);
|
||||
});
|
||||
|
||||
it("throws CycleError on cyclic input", () => {
|
||||
const cyclicData = {
|
||||
options: { type: "directed", multi: false, allowSelfLoops: false },
|
||||
attributes: {},
|
||||
nodes: [
|
||||
{ key: "a", attributes: { requestId: "a", operationId: "op.a", status: "completed", input: {} } },
|
||||
{ key: "b", attributes: { requestId: "b", operationId: "op.b", status: "completed", input: {} } },
|
||||
],
|
||||
edges: [
|
||||
{ key: "a->b", source: "a", target: "b", attributes: {} },
|
||||
{ key: "b->a", source: "b", target: "a", attributes: {} },
|
||||
],
|
||||
};
|
||||
expect(() => FlowGraph.fromJSON(cyclicData as never)).toThrow(CycleError);
|
||||
});
|
||||
|
||||
it("CycleError contains cycle paths on cyclic input", () => {
|
||||
const cyclicData = {
|
||||
options: { type: "directed", multi: false, allowSelfLoops: false },
|
||||
attributes: {},
|
||||
nodes: [
|
||||
{ key: "a", attributes: { requestId: "a", operationId: "op.a", status: "completed", input: {} } },
|
||||
{ key: "b", attributes: { requestId: "b", operationId: "op.b", status: "completed", input: {} } },
|
||||
],
|
||||
edges: [
|
||||
{ key: "a->b", source: "a", target: "b", attributes: {} },
|
||||
{ key: "b->a", source: "b", target: "a", attributes: {} },
|
||||
],
|
||||
};
|
||||
try {
|
||||
FlowGraph.fromJSON(cyclicData as never);
|
||||
expect.unreachable("should throw");
|
||||
} catch (e) {
|
||||
expect(e).toBeInstanceOf(CycleError);
|
||||
const ce = e as CycleError;
|
||||
expect(ce.cycles.length).toBeGreaterThan(0);
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves node attributes through round-trip", () => {
|
||||
const specs: OperationSpec[] = [
|
||||
{ name: "classify", namespace: "task", version: "2.0.0", type: "mutation", inputSchema: Type.Object({ text: Type.String() }), outputSchema: Type.Object({ label: Type.String() }), description: "Classifies text", tags: ["nlp"] },
|
||||
];
|
||||
const original = FlowGraph.fromSpecs(specs);
|
||||
const data = original.export();
|
||||
const restored = FlowGraph.fromJSON(data);
|
||||
const origAttrs = original.getNodeAttributes("task.classify" as never) as Record<string, unknown>;
|
||||
const restAttrs = restored.getNodeAttributes("task.classify" as never) as Record<string, unknown>;
|
||||
expect(restAttrs.name).toBe(origAttrs.name);
|
||||
expect(restAttrs.namespace).toBe(origAttrs.namespace);
|
||||
expect(restAttrs.version).toBe(origAttrs.version);
|
||||
expect(restAttrs.type).toBe(origAttrs.type);
|
||||
expect(restAttrs.description).toBe(origAttrs.description);
|
||||
expect(restAttrs.tags).toEqual(origAttrs.tags);
|
||||
});
|
||||
|
||||
it("preserves edge attributes through round-trip", () => {
|
||||
const specs: OperationSpec[] = [
|
||||
{ name: "extract", namespace: "task", version: "1.0.0", type: "query", inputSchema: Type.Object({ raw: Type.String() }), outputSchema: Type.Object({ text: Type.String() }) },
|
||||
{ name: "classify", namespace: "task", version: "1.0.0", type: "query", inputSchema: Type.Object({ text: Type.String() }), outputSchema: Type.Object({ label: Type.String() }) },
|
||||
];
|
||||
const original = FlowGraph.fromSpecs(specs);
|
||||
const data = original.export();
|
||||
const restored = FlowGraph.fromJSON(data);
|
||||
const origEdgeAttrs = original.getEdgeAttributes("task.extract", "task.classify") as Record<string, unknown>;
|
||||
const restEdgeAttrs = restored.getEdgeAttributes("task.extract", "task.classify") as Record<string, unknown>;
|
||||
expect(restEdgeAttrs.edgeType).toBe(origEdgeAttrs.edgeType);
|
||||
expect(restEdgeAttrs.compatible).toBe(origEdgeAttrs.compatible);
|
||||
});
|
||||
|
||||
it("double round-trip is lossless", () => {
|
||||
const specs: OperationSpec[] = [
|
||||
{ name: "extract", namespace: "task", version: "1.0.0", type: "query", inputSchema: Type.Object({ raw: Type.String() }), outputSchema: Type.Object({ text: Type.String() }) },
|
||||
{ name: "classify", namespace: "task", version: "1.0.0", type: "query", inputSchema: Type.Object({ text: Type.String() }), outputSchema: Type.Object({ label: Type.String() }) },
|
||||
];
|
||||
const original = FlowGraph.fromSpecs(specs);
|
||||
const first = original.export();
|
||||
const restored1 = FlowGraph.fromJSON(first);
|
||||
const second = restored1.export();
|
||||
const restored2 = FlowGraph.fromJSON(second);
|
||||
const third = restored2.export();
|
||||
expect(third).toEqual(first);
|
||||
expect(third).toEqual(second);
|
||||
});
|
||||
|
||||
it("accepts valid call graph serialized data", () => {
|
||||
const callData = {
|
||||
options: { type: "directed", multi: false, allowSelfLoops: false },
|
||||
attributes: {},
|
||||
nodes: [
|
||||
{
|
||||
key: "req_1",
|
||||
attributes: {
|
||||
requestId: "req_1",
|
||||
operationId: "task.classify",
|
||||
status: "completed",
|
||||
input: { text: "hello" },
|
||||
output: { label: "greeting" },
|
||||
},
|
||||
},
|
||||
],
|
||||
edges: [],
|
||||
};
|
||||
const fg = FlowGraph.fromJSON(callData as never);
|
||||
expect(fg.order).toBe(1);
|
||||
expect(fg.hasNode("req_1")).toBe(true);
|
||||
});
|
||||
|
||||
it("throws InvalidInputError for invalid node attributes", () => {
|
||||
const bad = {
|
||||
options: { type: "directed", multi: false, allowSelfLoops: false },
|
||||
attributes: {},
|
||||
nodes: [
|
||||
{ key: "a", attributes: { invalid: true } },
|
||||
],
|
||||
edges: [],
|
||||
};
|
||||
expect(() => FlowGraph.fromJSON(bad as never)).toThrow(InvalidInputError);
|
||||
});
|
||||
});
|
||||
@@ -159,7 +159,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-1");
|
||||
root.setRequestId("a", "req-1");
|
||||
root.append({
|
||||
type: "call.requested",
|
||||
requestId: "req-1",
|
||||
@@ -177,7 +177,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-1");
|
||||
root.setRequestId("a", "req-1");
|
||||
root.append({
|
||||
type: "call.requested",
|
||||
requestId: "req-1",
|
||||
@@ -201,7 +201,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-1");
|
||||
root.setRequestId("a", "req-1");
|
||||
root.append({
|
||||
type: "call.requested",
|
||||
requestId: "req-1",
|
||||
@@ -225,7 +225,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-1");
|
||||
root.setRequestId("a", "req-1");
|
||||
root.append({
|
||||
type: "call.requested",
|
||||
requestId: "req-1",
|
||||
@@ -265,7 +265,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-1");
|
||||
root.setRequestId("a", "req-1");
|
||||
|
||||
const respondedEvent: CallEventMapValue = {
|
||||
type: "call.responded",
|
||||
@@ -304,7 +304,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-1");
|
||||
root.setRequestId("a", "req-1");
|
||||
root.append({
|
||||
type: "call.requested",
|
||||
requestId: "req-1",
|
||||
@@ -353,7 +353,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-1");
|
||||
root.setRequestId("a", "req-1");
|
||||
root.append({
|
||||
type: "call.requested",
|
||||
requestId: "req-1",
|
||||
@@ -380,7 +380,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-1");
|
||||
root.setRequestId("a", "req-1");
|
||||
root.append({
|
||||
type: "call.requested",
|
||||
requestId: "req-1",
|
||||
@@ -408,7 +408,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-1");
|
||||
root.setRequestId("a", "req-1");
|
||||
root.append({
|
||||
type: "call.requested",
|
||||
requestId: "req-1",
|
||||
@@ -434,7 +434,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-1");
|
||||
root.setRequestId("a", "req-1");
|
||||
root.append({
|
||||
type: "call.requested",
|
||||
requestId: "req-1",
|
||||
@@ -452,7 +452,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-2");
|
||||
root.setRequestId("a", "req-1");
|
||||
root.append({
|
||||
type: "call.requested",
|
||||
requestId: "req-1",
|
||||
@@ -466,6 +466,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
error: { code: "ERR", message: "first attempt failed" },
|
||||
timestamp: "2026-01-01T00:00:01Z",
|
||||
});
|
||||
root.setRequestId("a", "req-2");
|
||||
root.append({
|
||||
type: "call.requested",
|
||||
requestId: "req-2",
|
||||
@@ -503,7 +504,7 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-1");
|
||||
root.setRequestId("a", "req-1");
|
||||
root.append({
|
||||
type: "call.requested",
|
||||
requestId: "req-1",
|
||||
@@ -840,9 +841,9 @@ describe("WorkflowReactiveRoot", () => {
|
||||
const graph = makeSimpleGraph();
|
||||
const root = new WorkflowReactiveRoot(graph);
|
||||
|
||||
root.nodeKeyToRequestId.set("a", "req-a");
|
||||
root.nodeKeyToRequestId.set("b", "req-b");
|
||||
root.nodeKeyToRequestId.set("c", "req-c");
|
||||
root.setRequestId("a", "req-a");
|
||||
root.setRequestId("b", "req-b");
|
||||
root.setRequestId("c", "req-c");
|
||||
|
||||
expect(root.getStatus("a")).toBe("idle");
|
||||
expect(root.getStatus("b")).toBe("idle");
|
||||
|
||||
Reference in New Issue
Block a user