coordinator.graph¶
coordinator.graph ¶
Task graph for multi-agent coordination.
In-memory directed acyclic graph where nodes are tasks and edges are dependencies. The graph drives wave-based execution: tasks become ready only when all their predecessors are complete. Multiple tasks in the same wave run in parallel.
SoftCapReached ¶
Bases: Exception
Raised by :meth:TaskGraph.extend_graph when the soft extension cap is hit.
The soft cap is an escalation gate, NOT a hard wall: the caller must either
escalate to the user (a higher cap / a fresh graph) or consciously override
by re-calling with force=True and a reason (recorded and surfaced).
A separate, larger hard ceiling still raises ValueError to stop runaway
loops even when forced. Carries the counts so the caller can build a
structured response without re-reading graph internals.
Source code in coordinator/graph.py
TaskGraph ¶
In-memory task graph backed by a plain dict.
Source code in coordinator/graph.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 | |
create_graph ¶
create_graph(tasks: list[dict], goal: str = '', agent_models: dict[str, str] | None = None, agent_passes: dict[str, int] | None = None, agent_schemas: dict[str, str] | None = None, schema_resolver: Callable[[str], object | None] | None = None, schema_enabled: bool = True) -> list[str]
Create a task graph from a list of task definitions.
Each task dict has
- alias: str -- local reference name (e.g. "eng", "rev")
- agent: str -- agent type (e.g. "engineer", "reviewer")
- description: str -- what the agent should do
- depends_on: list[str] -- aliases this task depends on (empty = root)
- model: str (optional) -- model override for this specific task
- passes: int (optional) -- self-refinement pass override for this task
- schema: str (optional) -- extraction-schema name for this task
- writes: list[str] -- declared write scope for path-scoped
coordination. The coordinator protocol requires this to be filled
explicitly for implementer tasks: pass disjoint paths to run
implementers in parallel, an empty list for dynamic mode (the
agent leases paths at write time via manage_paths action acquire/release),
or
["."]for a deliberate whole-workspace lease. Omitting it falls back to a whole-workspace lease as a safety backstop. Seelease_keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_models
|
dict[str, str] | None
|
Default model per agent type (from agents.yaml).
Task-level |
None
|
agent_passes
|
dict[str, int] | None
|
Default self-refinement pass count per agent type
(from agents.yaml). Task-level |
None
|
agent_schemas
|
dict[str, str] | None
|
Default extraction-schema name per agent type. Task
level |
None
|
schema_resolver
|
Callable[[str], object | None] | None
|
Callable |
None
|
schema_enabled
|
bool
|
Whether the grounded-extraction capability is enabled for this project. When False, a task carrying a schema raises (spec §8.3). |
True
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of generated task IDs. |
Source code in coordinator/graph.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |
create_pipeline ¶
create_pipeline(steps: list[tuple[str, str] | tuple[str, str, str | None]], goal: str = '', agent_models: dict[str, str] | None = None, agent_passes: dict[str, int] | None = None) -> list[str]
Create a linear pipeline (convenience wrapper around create_graph).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
steps
|
list[tuple[str, str] | tuple[str, str, str | None]]
|
Ordered list of (agent, description) or (agent, description, model) tuples. |
required |
agent_models
|
dict[str, str] | None
|
Default model per agent type (from agents.yaml). |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of task IDs in execution order. |
Source code in coordinator/graph.py
extend_graph ¶
extend_graph(tasks: list[dict], after_task_ids: list[str], context: str = '', agent_models: dict[str, str] | None = None, agent_passes: dict[str, int] | None = None, agent_schemas: dict[str, str] | None = None, schema_resolver: Callable[[str], object | None] | None = None, schema_enabled: bool = True, force: bool = False, reason: str = '') -> list[str]
Append new tasks to the graph after one or more existing tasks.
Used when tasks fail and the coordinator decides to add corrective steps. The graph only grows forward.
Extension cap is tiered (soft cap + hard ceiling):
* below the soft cap -> proceed normally;
* at/above the soft cap and not force -> raise
:class:SoftCapReached (an escalation gate, not a wall);
* forced and below the hard ceiling -> proceed and count it as a
FORCED extension (recorded so the override is visible);
* at/above the hard ceiling -> raise ValueError (runaway wall),
even when forced.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tasks
|
list[dict]
|
List of task dicts (same format as create_graph). |
required |
after_task_ids
|
list[str]
|
Task IDs that the first new task depends on. Typically the failed tasks from a wave. |
required |
context
|
str
|
Failure context to inject into the first new task's description. |
''
|
agent_models
|
dict[str, str] | None
|
Default model per agent type (from agents.yaml). |
None
|
force
|
bool
|
Override the soft cap (still bounded by the hard ceiling). |
False
|
reason
|
str
|
Why the override is justified (logged; the caller records it on the run for dashboard visibility). |
''
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of new task IDs. |
Raises:
| Type | Description |
|---|---|
SoftCapReached
|
Soft cap hit and not forced. |
ValueError
|
Hard ceiling reached, or extension validation failed. |
Source code in coordinator/graph.py
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 | |
restore_tasks ¶
restore_tasks(entries: list[dict], extensions_used: int = 0, forced_extensions: int = 0) -> list[str]
Rehydrate persisted tasks into the graph (for resuming a run).
Used when adopting a run from the state file after the coordinator process that owned it died. Tasks keep their original IDs, aliases, dependencies, and terminal statuses. Tasks that were RUNNING when the owning process died are reset to PENDING so they can be re-run (their results were lost with the process).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entries
|
list[dict]
|
Persisted task dicts with keys task_id, alias, agent, description, depends_on (task IDs), status, and optionally model, result, parsed_result, started_at, completed_at. |
required |
extensions_used
|
int
|
The extension count from the persisted run so a
resumed run cannot exceed |
0
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of restored task IDs. |
Raises:
| Type | Description |
|---|---|
ValueError
|
A task ID already exists in the graph, or a dependency does not resolve. |
Source code in coordinator/graph.py
resolve_alias ¶
get_ready_tasks ¶
Return tasks that are pending and have all dependencies satisfied.
A dependency is satisfied if it is in a terminal state (DONE, FAILED, or CANCELLED) -- extensions chain after failed tasks, and a finalized run's cancelled tasks never block (though finalize cancels dependents too, so this is mostly belt-and-suspenders).
Source code in coordinator/graph.py
claim_task ¶
Mark a task as running.
Raises:
| Type | Description |
|---|---|
KeyError
|
Task does not exist. |
ValueError
|
Task is not in PENDING state. |
Source code in coordinator/graph.py
submit_result ¶
Record the result of a task and mark it done or failed.
Source code in coordinator/graph.py
finalize ¶
Mark the graph complete by cancelling any non-terminal tasks.
Converts every PENDING/RUNNING task to CANCELLED (stamping
completed_at) so the run reads as terminally complete. Used when a
stranded run is "finalized" instead of resumed or trashed -- its
already-finished work is preserved while the unrun tail is closed out.
Returns:
| Type | Description |
|---|---|
list[str]
|
The IDs of the tasks that were cancelled (was pending/running). |
Source code in coordinator/graph.py
record_pass ¶
Record completion of one self-refinement pass for a running task.
Increments passes_done and appends an optional one-line summary
to pass_notes (for the dashboard tooltip). The task must be RUNNING
-- passes happen between :meth:claim_task and :meth:submit_result.
passes_done is allowed to reach passes_total; if it would exceed
it, passes_total is bumped up so the displayed fraction stays sane.
Raises:
| Type | Description |
|---|---|
KeyError
|
Task does not exist. |
ValueError
|
Task is not RUNNING. |
Source code in coordinator/graph.py
get_task_context ¶
Return outputs from all predecessor tasks.
Each entry is a dict with keys: task_id, agent, description, result, status. Includes predecessors that are DONE or FAILED (so fix agents see failures).
Source code in coordinator/graph.py
get_status ¶
Return current state of all tasks.
Source code in coordinator/graph.py
get_report ¶
Generate a summary report of the graph execution.
Source code in coordinator/graph.py
to_state ¶
Full-fidelity serialization for process-boundary persistence.
Unlike :meth:export_trace (telemetry/debugging), this round-trips with
:meth:from_state exactly — it preserves created_at and the live
RUNNING status (no reset), so a CLI that persists between command
invocations can claim a task in one call and submit it in the next.
Source code in coordinator/graph.py
from_state
classmethod
¶
Reconstruct a graph from :meth:to_state output, preserving status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
A dict produced by :meth: |
required |
agent_roles
|
dict[str, str] | None
|
Role overrides to attach to the rebuilt graph (e.g. project-local roles). Roles are not serialized because they are resolved from configuration, not persisted run state. |
None
|
Source code in coordinator/graph.py
export_trace ¶
Serialize the full graph state for telemetry/debugging.
Source code in coordinator/graph.py
normalize_write_path ¶
Normalize a declared write path into a comparable lock key.
Returns "" (the whole-workspace sentinel) for an empty path, .,
/, or a path whose first segment is a glob. A glob is otherwise reduced
to its longest non-magic leading directory prefix (notes/A/*.md ->
notes/A), so a lease conservatively covers the glob's containing dir.
Source code in coordinator/graph.py
keys_overlap ¶
Two normalized keys overlap if one contains the other (or either is the
whole-workspace sentinel "").
Source code in coordinator/graph.py
lease_keys ¶
Resolve a task's declared writes to claim-time lock keys.
None -> [""] (whole-workspace lease; conservative default that
reproduces the legacy one-implementer-at-a-time behavior). [] -> []
(dynamic mode: no claim-time lease). A concrete list is normalized to unique
keys; any element that normalizes to the whole-workspace sentinel collapses
the whole set to [""].
Source code in coordinator/graph.py
write_sets_conflict ¶
Whether two declared write scopes would contend at claim time.
A side holding nothing (dynamic mode, []) never conflicts at claim.
Source code in coordinator/graph.py
validate_graph ¶
validate_graph(tasks: list[dict], existing_task_ids: set[str] | None = None, agent_roles: dict[str, str] | None = None) -> list[str]
Validate a DAG definition. Returns list of violations (empty = valid).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tasks
|
list[dict]
|
List of task dicts with keys: alias, agent, depends_on. |
required |
existing_task_ids
|
set[str] | None
|
Task IDs already in the graph (for extension validation). |
None
|
agent_roles
|
dict[str, str] | None
|
Extra agent->role mappings (e.g. project-local agents), merged over the built-in AGENT_ROLES. Roles: "implementer", "planner", "checker", "meta". Unknown agents default to "checker". |
None
|
Rules
- Acyclic: no cycles in the dependency graph.
- At least one implementer or planner: a graph must have at least one agent that does real work (writes code or designs a plan). A planner-only graph (no implementer) is valid.
- Implementer ancestry: every checker task must have at least one implementer in its transitive dependency chain. Meta and planner agents are exempt.
- Planner ancestry: planner tasks must NOT have an implementer in their transitive dependency chain (planners run before engineers, not after them).
- No orphan references: every depends_on alias must resolve to a task in the graph or in existing_task_ids.
Source code in coordinator/graph.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |