"""Execute a thinking flow (Def 4.2): a DAG over thinking operators run in topological order. Generate seeds the pool; Divergent branches into a candidate pool; Convergent collapses the pool by selecting the best; the other operators transform the current best idea. e(y | G; x_t, x_r) returns the final idea. """ from __future__ import annotations from . import operators as ops def execute_flow(seq, topic, related, model=None, seed=None, divergent_n=3): """seq: list of refinement operator names (Generate is always prepended). Returns (final_idea, trace) where trace is the list of (op, idea) steps.""" trace = [] s = (seed or 0) current = ops.op_generate(topic, related, model=model, seed=s) pool = [current] trace.append(("Generate", current)) for i, name in enumerate(seq): s += 1 if name == "Divergent": alts = ops.op_divergent_expand(topic, related, current, n=divergent_n, model=model, seed=s) pool = pool + alts current = alts[0] elif name == "Convergent": current = ops.op_convergent_select(topic, related, pool, model=model, seed=s) pool = [current] else: fn = ops.OPERATORS.get(name) if fn is None: continue current = fn(topic, related, current, model=model, seed=s) pool[-1] = current trace.append((name, current)) return current, trace def idea_to_text(idea): if not idea: return "" return (f"Title: {idea.get('title','')}\n" f"Motivation/Problem: {idea.get('problem','')}\n" f"Method: {idea.get('method','')}\n" f"Evaluation: {idea.get('evaluation','')}") def motivation_text(idea): return f"Title: {idea.get('title','')}\nMotivation: {idea.get('problem','')}" if idea else ""