diff --git a/src/sovereign_cortex/next_action.py b/src/sovereign_cortex/next_action.py index 1d2fe09..5e3a403 100644 --- a/src/sovereign_cortex/next_action.py +++ b/src/sovereign_cortex/next_action.py @@ -8,12 +8,19 @@ def recommend_next_action(tasks: Iterable[MarkdownDocument], *, workspace_name: str, today: date) -> str: + task_list = list(tasks) + by_id = {str(task.metadata.get("id", "")): task for task in task_list if task.metadata.get("id")} candidates = [] - for task in tasks: + blocked_count = 0 + + for task in task_list: metadata = task.metadata status = str(metadata.get("status", "unspecified")).lower() if status in DONE_STATUSES: continue + if not dependencies_ready(task, by_id): + blocked_count += 1 + continue raw_due = metadata.get("due") due = None @@ -27,6 +34,8 @@ def recommend_next_action(tasks: Iterable[MarkdownDocument], *, workspace_name: candidates.append((due is None, due or date.max, title.lower(), task)) if not candidates: + if blocked_count: + return f"Next action for {workspace_name}:\n- No unblocked active tasks found." return f"Next action for {workspace_name}:\n- No active tasks found." _, due, _, task = sorted(candidates)[0] @@ -35,11 +44,11 @@ def recommend_next_action(tasks: Iterable[MarkdownDocument], *, workspace_name: raw_due = metadata.get("due") if raw_due: if due < today: - reason = f"active overdue task due {raw_due}" + reason = f"unblocked overdue task due {raw_due}" else: - reason = f"active task with nearest due date {raw_due}" + reason = f"unblocked task with nearest due date {raw_due}" else: - reason = "active unscheduled task selected by title order" + reason = "unblocked unscheduled task selected by title order" return "\n".join( [ @@ -49,3 +58,17 @@ def recommend_next_action(tasks: Iterable[MarkdownDocument], *, workspace_name: f"- Reason: {reason}.", ] ) + + +def dependencies_ready(task: MarkdownDocument, by_id: dict[str, MarkdownDocument]) -> bool: + dependencies = task.metadata.get("dependencies") or [] + if not isinstance(dependencies, list): + dependencies = [dependencies] + for dependency in dependencies: + dependency_task = by_id.get(str(dependency)) + if dependency_task is None: + return False + status = str(dependency_task.metadata.get("status", "unspecified")).lower() + if status not in DONE_STATUSES: + return False + return True diff --git a/tests/test_next_action.py b/tests/test_next_action.py index 0b9f7ec..e34eaa5 100644 --- a/tests/test_next_action.py +++ b/tests/test_next_action.py @@ -1,5 +1,9 @@ +from datetime import date + from sovereign_cortex import cli from sovereign_cortex.events import EventType +from sovereign_cortex.next_action import recommend_next_action +from sovereign_cortex.vault import MarkdownDocument class DummyRecordStore: @@ -21,6 +25,23 @@ def handle_command(self, text, *, auto_approve=False, today=None): raise AssertionError("Next action report should be handled before update planning") +def _task(task_id: str, title: str, status: str, dependencies=None, due=None): + metadata = { + "id": task_id, + "title": title, + "status": status, + "dependencies": dependencies or [], + } + if due: + metadata["due"] = due + return MarkdownDocument( + relative_path=f"tasks/{task_id}.md", + metadata=metadata, + body=f"# {title}\n", + raw="", + ) + + def test_cli_next_action_report_is_read_only(monkeypatch, capsys): DummyRecordStore.calls = [] monkeypatch.setattr(cli, "LocalOrchestrator", FailingOrchestrator) @@ -33,7 +54,7 @@ def test_cli_next_action_report_is_read_only(monkeypatch, capsys): assert "Next action for demo-project:" in captured.out assert "Task: Launch Website" in captured.out assert "Path: tasks/launch-website.md" in captured.out - assert "active overdue task due 2026-06-05" in captured.out + assert "unblocked overdue task due 2026-06-05" in captured.out assert "Approval ID" not in captured.out assert len(DummyRecordStore.calls) == 1 assert [event.event_type for event in DummyRecordStore.calls[0]] == [ @@ -42,6 +63,34 @@ def test_cli_next_action_report_is_read_only(monkeypatch, capsys): ] +def test_next_action_skips_tasks_with_active_dependencies(): + report = recommend_next_action( + [ + _task("waiting", "Waiting Task", "planned", dependencies=["support"], due="2026-06-01"), + _task("support", "Support Task", "todo", due="2026-06-10"), + _task("ready", "Ready Task", "planned", due="2026-06-03"), + ], + workspace_name="demo-project", + today=date(2026, 6, 6), + ) + + assert "Task: Ready Task" in report + assert "Path: tasks/ready.md" in report + assert "unblocked" in report + + +def test_next_action_reports_when_all_active_tasks_are_not_ready(): + report = recommend_next_action( + [ + _task("waiting", "Waiting Task", "planned", dependencies=["support"], due="2026-06-01"), + ], + workspace_name="demo-project", + today=date(2026, 6, 6), + ) + + assert "No unblocked active tasks found." in report + + def test_cli_next_action_detection_does_not_intercept_update_commands(): result = cli.build_next_action_result( cli.Path(__file__).resolve().parents[1],