Cycles spec
Cycle Detection and Prevention Spec¶
Why¶
Prevent infinite loops in workflows while allowing controlled iteration. Need to handle:
- Multiple independent cycles
- Nested cycles (loops within loops)
- Overlapping cycles (cycles sharing edges)
- Diamond patterns (non-cyclic, should not trigger)
Design: Database Schema¶
flow_cycle - Detected cycles at flow creation
CREATE TABLE skemata.flow_cycle (
id VARCHAR(128) PRIMARY KEY, -- hash of ordered edge IDs
flow_id VARCHAR(128) NOT NULL REFERENCES flow(id) ON DELETE CASCADE,
edges VARCHAR(128)[] NOT NULL, -- ordered array of edge IDs in cycle
cycle_start_edge VARCHAR(128) NOT NULL, -- for human readability
default_limit INTEGER NOT NULL DEFAULT 10,
org_id org_id_type NOT NULL REFERENCES org_skemata_config(org_id) ON DELETE CASCADE,
created_at TIMESTAMP NOT NULL DEFAULT now()
);
flow_instance_cycle_tracker - Runtime cycle counters per flow instance
CREATE TABLE skemata.flow_instance_cycle_tracker (
id VARCHAR(128) PRIMARY KEY DEFAULT id_gen('fct'),
flow_instance_id VARCHAR(128) NOT NULL REFERENCES flow_instance(id) ON DELETE CASCADE,
cycle_id VARCHAR(128) NOT NULL REFERENCES flow_cycle(id) ON DELETE CASCADE,
counter INTEGER NOT NULL DEFAULT 0,
limit INTEGER NOT NULL, -- copied from flow_cycle or user override
last_incremented_at TIMESTAMP, -- timestamp of last edge instance that completed cycle
org_id org_id_type NOT NULL REFERENCES org_skemata_config(org_id) ON DELETE CASCADE,
UNIQUE(flow_instance_id, cycle_id)
);
Creation Time Algorithm¶
On flow create/update:
- Run DFS to detect all cycles (back-edge detection).
- For each cycle found:
a. Normalize: Sort edges by lexicographically smallest edge ID as start.
b. Generate ID:
hash(ordered_edge_ids). c. Insert intoflow_cycletable. - Handle duplicates: Cycles starting at different points are the same cycle after normalization.
Runtime Algorithm¶
On edge_instance creation (ei):
- Get all cycles where
ei.edge_id IN cycle.edges. - For EACH cycle:
a. Get tracker or create if first iteration: -
flow_instance_cycle_trackerfor this cycle +flow_instance.
b. Get candidate edge_instances: - WHERE flow_instance_id = ei.flow_instance_id - AND edge_id IN cycle.edges - AND created_at >= COALESCE(tracker.last_incremented_at, flow_instance.created_at) - AND created_at <= ei.created_at - ORDER BY created_at
c. Extract sequence of edge_ids from edge_instances.
d. Check if sequence matches cycle: - Build hash of sequence. - Compare with cycle.id (hash of cycle.edges). - OR check if sequence matches any rotation of cycle.edges.
e. If match: - INCREMENT tracker.counter. - SET tracker.last_incremented_at = ei.created_at. - IF tracker.counter >= tracker.limit: - RAISE EXCEPTION 'Cycle limit exceeded for cycle {cycle.id}'
Key Design Decisions¶
Diamond Graphs (Non-Cycles)¶
Not a cycle - no repeated edges. Hash won't match any cycle pattern. ✅ Handled correctly.
Cycles Sharing Edges¶
- Edge BC participates in both cycles.
- Each cycle has separate tracker with separate counter.
- Edge instances can contribute to multiple cycle completions.
- ✅ Independent tracking per cycle.
Overlapping Cycle Detection¶
A→B→C→D→B could be detected as:
A→B→C→D→B(start at A)B→C→D→B(start at B)