Initial commit: add .gitignore and README
This commit is contained in:
19
fusionagi/planning/__init__.py
Normal file
19
fusionagi/planning/__init__.py
Normal file
@@ -0,0 +1,19 @@
|
||||
"""Planning engine: plan graph, dependency resolution, checkpoints."""
|
||||
|
||||
from fusionagi.planning.graph import (
|
||||
topological_order,
|
||||
next_step,
|
||||
get_step,
|
||||
ready_steps,
|
||||
)
|
||||
from fusionagi.planning.strategies import linear_order, dependency_order, get_strategy
|
||||
|
||||
__all__ = [
|
||||
"topological_order",
|
||||
"next_step",
|
||||
"get_step",
|
||||
"ready_steps",
|
||||
"linear_order",
|
||||
"dependency_order",
|
||||
"get_strategy",
|
||||
]
|
||||
68
fusionagi/planning/graph.py
Normal file
68
fusionagi/planning/graph.py
Normal file
@@ -0,0 +1,68 @@
|
||||
"""Plan graph: topological sort, next step, checkpoints."""
|
||||
|
||||
from fusionagi.schemas.plan import Plan, PlanStep
|
||||
|
||||
|
||||
def topological_order(plan: Plan) -> list[str]:
|
||||
"""
|
||||
Return step ids in dependency order (topological sort).
|
||||
Steps with cycles or missing dependencies are still appended at the end so the result is a full list of step ids.
|
||||
"""
|
||||
steps_by_id = {s.id: s for s in plan.steps}
|
||||
in_degree = {
|
||||
s.id: len([d for d in s.dependencies if d in steps_by_id])
|
||||
for s in plan.steps
|
||||
}
|
||||
order: list[str] = []
|
||||
remaining = set(plan.step_ids())
|
||||
while remaining:
|
||||
ready = [sid for sid in remaining if in_degree[sid] == 0]
|
||||
if not ready:
|
||||
break # cycle or missing dep
|
||||
for sid in ready:
|
||||
order.append(sid)
|
||||
remaining.discard(sid)
|
||||
for s in plan.steps:
|
||||
if sid in s.dependencies:
|
||||
in_degree[s.id] = max(0, in_degree[s.id] - 1)
|
||||
for sid in plan.step_ids():
|
||||
if sid not in order:
|
||||
order.append(sid)
|
||||
return order
|
||||
|
||||
|
||||
def next_step(plan: Plan, completed_step_ids: set[str]) -> str | None:
|
||||
"""Return the next step id that has all dependencies satisfied, or None."""
|
||||
order = topological_order(plan)
|
||||
for sid in order:
|
||||
if sid in completed_step_ids:
|
||||
continue
|
||||
step = next((s for s in plan.steps if s.id == sid), None)
|
||||
if step and set(step.dependencies).issubset(completed_step_ids):
|
||||
return sid
|
||||
return None
|
||||
|
||||
|
||||
def ready_steps(plan: Plan, completed_step_ids: set[str]) -> list[str]:
|
||||
"""
|
||||
Return all step ids that have dependencies satisfied and can run in parallel.
|
||||
|
||||
For multi-agent acceleration: steps with no mutual dependencies can be
|
||||
dispatched to different agents concurrently.
|
||||
|
||||
Returns:
|
||||
List of step ids ready for parallel execution.
|
||||
"""
|
||||
ready: list[str] = []
|
||||
steps_by_id = {s.id: s for s in plan.steps}
|
||||
for sid, step in steps_by_id.items():
|
||||
if sid in completed_step_ids:
|
||||
continue
|
||||
if set(step.dependencies).issubset(completed_step_ids):
|
||||
ready.append(sid)
|
||||
return ready
|
||||
|
||||
|
||||
def get_step(plan: Plan, step_id: str) -> PlanStep | None:
|
||||
"""Return the step by id or None."""
|
||||
return next((s for s in plan.steps if s.id == step_id), None)
|
||||
23
fusionagi/planning/strategies.py
Normal file
23
fusionagi/planning/strategies.py
Normal file
@@ -0,0 +1,23 @@
|
||||
"""Planning strategies: linear vs dependency-based order."""
|
||||
|
||||
from typing import Callable
|
||||
|
||||
from fusionagi.schemas.plan import Plan
|
||||
from fusionagi.planning.graph import topological_order
|
||||
|
||||
|
||||
def linear_order(plan: Plan) -> list[str]:
|
||||
"""Return step ids in declaration order (ignore dependencies)."""
|
||||
return plan.step_ids()
|
||||
|
||||
|
||||
def dependency_order(plan: Plan) -> list[str]:
|
||||
"""Return step ids in dependency order (topological sort)."""
|
||||
return topological_order(plan)
|
||||
|
||||
|
||||
def get_strategy(name: str) -> Callable[[Plan], list[str]]:
|
||||
"""Return strategy function by name: linear or dependency."""
|
||||
if name == "linear":
|
||||
return linear_order
|
||||
return dependency_order
|
||||
Reference in New Issue
Block a user