Skip to content

fix(kanban): recover missed goal finalizers - #63

Merged
sahilm-ti merged 2 commits into
mainfrom
hermes-agent/t_e4fd0a46-fix-kanban-separate-goal-continuation-fr
Jul 21, 2026
Merged

sahilm-ti merged 2 commits into
mainfrom
hermes-agent/t_e4fd0a46-fix-kanban-separate-goal-continuation-fr

Conversation

@sahilm-ai

@sahilm-ai sahilm-ai commented Jul 21, 2026 •

Copy link
Copy Markdown
Collaborator

Diagnosis

Previously, a goal-mode worker that produced completion prose but missed the lifecycle tool was nudged once and then force-blocked by the outer goal loop. That could race a valid kanban_complete/kanban_review, mark clean work as blocked, and permit scratch cleanup before recovery.

Change

  • Goal continuation now distinguishes worker-owned terminal outcomes from a missed finalizer. done prose followed by one missed finalizer nudge returns a bounded finalization_recovery contract; the outer loop never calls block_fn for that case.
  • The CLI persists recovery atomically: it CAS-checks the active run, keeps workspace/artifact state, closes that run as finalization_recovery, records the recovery contract in the event/comment history, and returns the task to ready for deterministic re-dispatch.
  • Goal-mode review/human-review handoffs stop the loop cleanly. Completion, block, review, and human-review tools reconcile duplicate/lost-CAS terminal attempts as no-op successes when any valid terminal lifecycle state already won.
  • Completion-judge rejection now tells the worker to continue, includes the exact judge reason, and instructs it to retry completion with evidence rather than generically blocking.

Tests

  • scripts/run_tests.sh tests/hermes_cli/test_kanban_goal_mode.py tests/tools/test_kanban_tools.py tests/hermes_cli/test_kanban_completion_audit.py tests/hermes_cli/test_kanban_core_functionality.py
    • 357 passed
  • ruff check on changed Python files passed.
  • Static added-line security scan passed (no hardcoded secret, shell execution, eval/exec, pickle, or SQL-format matches).

Compatibility / migration

No schema migration: recovery uses the existing ready task state, task-run history, comments, and event log. Existing real dependency and needs_input block behavior is unchanged.

Environment-only check limitations

npm run check is currently blocked by missing workspace dependencies/types (for example @tauri-apps/api/core, @assistant-ui/react, @nous-research/ui, and plist). Full-file ty check reports 208 existing diagnostics in unrelated legacy areas; no new diagnostics were reported in the modified recovery code.

Summary by CodeRabbit

  • New Features

    • Added recovery handling when goal-mode work is judged complete but lacks a terminal lifecycle action.
    • Added clearer guidance for retrying completion with evidence or requesting external input.
    • Added safe no-op handling for repeated or concurrent terminal actions.
    • Worker handoffs to review now stop cleanly without additional turns.
  • Documentation

    • Clarified goal-mode retry, budget, blocking, and recovery behavior.

@coderabbitai

coderabbitai Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The goal loop now emits finalization recovery outcomes, the CLI persists recovery contracts and resets eligible tasks, and terminal Kanban tools reconcile repeated or racing lifecycle calls as no-op successes. Tests and documentation cover recovery, handoffs, terminal races, and updated goal-mode behavior.

Changes

Goal finalization recovery

Layer / File(s) Summary
Terminal transition reconciliation
tools/kanban_tools.py, tests/tools/test_kanban_tools.py
Terminal lifecycle handlers re-check already-terminal tasks after failed transitions and return structured no-op responses, with regression coverage for completion, blocking, and review handoffs.
Goal-loop recovery outcomes
hermes_cli/goals.py, tests/hermes_cli/test_kanban_goal_mode.py, website/docs/user-guide/features/kanban.md
Goal mode handles review handoffs, emits recovery contracts when completion lacks a terminal lifecycle call, updates rejection guidance, and documents the revised behavior.
Recovery persistence and orchestration
hermes_cli/kanban_db.py, cli.py, tests/hermes_cli/test_kanban_goal_mode.py
Finalization recovery is CAS-guarded by task and run state, resets tasks to ready, closes abandoned runs, records events and comments, and is invoked by the CLI with guarded cleanup.

Estimated code review effort: 4 (Complex) | ~45 minutes

Suggested reviewers: teknium1, kshitijk4poor, plcunha

Sequence Diagram(s)

sequenceDiagram
  participant Worker
  participant GoalLoop
  participant Judge
  participant CLI
  participant KanbanDB
  Worker->>GoalLoop: execute goal-mode turns
  GoalLoop->>Judge: evaluate task output
  Judge-->>GoalLoop: done verdict without finalizer
  GoalLoop-->>CLI: finalization_recovery contract
  CLI->>KanbanDB: record recovery with expected run id
  KanbanDB-->>CLI: reset task, close run, record event
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 85.71% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: recovering goal-mode runs when the finalizer tool was missed.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch hermes-agent/t_e4fd0a46-fix-kanban-separate-goal-continuation-fr

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sahilm-ti
sahilm-ti merged this pull request into main Jul 21, 2026
35 of 36 checks passed
@sahilm-ti
sahilm-ti deleted the hermes-agent/t_e4fd0a46-fix-kanban-separate-goal-continuation-fr branch July 21, 2026 17:27

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cli.py`:
- Around line 15600-15612: Redact the worker/LLM-influenced recovery_contract
before passing it to _kb.record_finalization_recovery, using the same
redact_sensitive_text(..., force=True) pattern as _handle_complete and
_handle_block in tools/kanban_tools.py. Preserve the existing recovery_reason
handling and pass the redacted contract to both persisted task comments and
event payloads through record_finalization_recovery.

In `@tools/kanban_tools.py`:
- Around line 298-336: Remove the non-terminal "todo" and "triage" values from
_TERMINAL_RECONCILIATION_STATUSES, leaving only statuses that represent a
successful terminal outcome for the guarded lifecycle calls. Keep
_terminal_transition_noop and its four call sites unchanged so reopened or
rework states return the existing error path instead of a false noop success.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6a82943d-6543-4634-bc53-7c613996613a

📥 Commits

Reviewing files that changed from the base of the PR and between 598c58c and 0d4318b.

📒 Files selected for processing (7)
  • cli.py
  • hermes_cli/goals.py
  • hermes_cli/kanban_db.py
  • tests/hermes_cli/test_kanban_goal_mode.py
  • tests/tools/test_kanban_tools.py
  • tools/kanban_tools.py
  • website/docs/user-guide/features/kanban.md

Comment thread cli.py
Comment on lines +15600 to +15612
recovery_contract = str(loop_result.get("recovery_contract") or "").strip()
recovery_reason = str(loop_result.get("reason") or "").strip()
run_id_raw = (_os.environ.get("HERMES_KANBAN_RUN_ID") or "").strip()
expected_run_id = int(run_id_raw) if run_id_raw.isdigit() else None
c = _kb.connect()
try:
saved = _kb.record_finalization_recovery(
c,
task_id,
reason=recovery_reason,
contract=recovery_contract,
expected_run_id=expected_run_id,
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

Recovery contract text isn't redacted before being persisted.

recovery_contract embeds the goal judge's freeform reason (via KANBAN_GOAL_FINALIZATION_RECOVERY_TEMPLATE, which can echo content derived from the worker's response) and flows straight into record_finalization_recovery, which writes it verbatim into task_comments and the finalization_recovery_requested event payload. Every sibling lifecycle path (_handle_complete, _handle_block in tools/kanban_tools.py) redacts comparable user/LLM-influenced text with redact_sensitive_text(..., force=True) before persistence; this path skips that step.

🛡️ Proposed fix
     recovery_contract = str(loop_result.get("recovery_contract") or "").strip()
     recovery_reason = str(loop_result.get("reason") or "").strip()
+    if recovery_contract:
+        from agent.redact import redact_sensitive_text
+        recovery_contract = redact_sensitive_text(recovery_contract, force=True)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
recovery_contract = str(loop_result.get("recovery_contract") or "").strip()
recovery_reason = str(loop_result.get("reason") or "").strip()
run_id_raw = (_os.environ.get("HERMES_KANBAN_RUN_ID") or "").strip()
expected_run_id = int(run_id_raw) if run_id_raw.isdigit() else None
c = _kb.connect()
try:
saved = _kb.record_finalization_recovery(
c,
task_id,
reason=recovery_reason,
contract=recovery_contract,
expected_run_id=expected_run_id,
)
recovery_contract = str(loop_result.get("recovery_contract") or "").strip()
recovery_reason = str(loop_result.get("reason") or "").strip()
if recovery_contract:
from agent.redact import redact_sensitive_text
recovery_contract = redact_sensitive_text(recovery_contract, force=True)
run_id_raw = (_os.environ.get("HERMES_KANBAN_RUN_ID") or "").strip()
expected_run_id = int(run_id_raw) if run_id_raw.isdigit() else None
c = _kb.connect()
try:
saved = _kb.record_finalization_recovery(
c,
task_id,
reason=recovery_reason,
contract=recovery_contract,
expected_run_id=expected_run_id,
)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cli.py` around lines 15600 - 15612, Redact the worker/LLM-influenced
recovery_contract before passing it to _kb.record_finalization_recovery, using
the same redact_sensitive_text(..., force=True) pattern as _handle_complete and
_handle_block in tools/kanban_tools.py. Preserve the existing recovery_reason
handling and pass the redacted contract to both persisted task comments and
event payloads through record_finalization_recovery.

Comment thread tools/kanban_tools.py
Comment on lines +298 to +336
_TERMINAL_RECONCILIATION_STATUSES = (
"done",
"blocked",
"todo",
"triage",
"review",
"human_review",
)


def _terminal_transition_noop(
kb,
conn,
task_id: str,
*,
terminal_statuses: tuple[str, ...],
action: str,
extra: Optional[dict] = None,
) -> Optional[str]:
"""Reconcile a lost CAS race when the desired terminal state already won."""
task = kb.get_task(conn, task_id)
if task is None or task.status not in terminal_statuses:
return None
run = kb.latest_run(conn, task_id)
payload = {
"task_id": task_id,
"run_id": run.id if run else None,
"status": task.status,
"noop": True,
}
if isinstance(extra, dict):
payload.update(extra)
logger.info(
"%s: reconciled as no-op for task %s already in %s",
action,
task_id,
task.status,
)
return _ok(**payload)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

_TERMINAL_RECONCILIATION_STATUSES includes non-terminal statuses, causing false-positive noop successes.

The docstring says this reconciles "a lost CAS race when the desired terminal state already won," but "todo" and "triage" are not a "won" outcome for any of the four guarded calls (kanban_complete, kanban_block, kanban_review, kanban_human_review) — they represent the task being sent back for rework. If a task is externally reset to todo/triage (e.g. reopened) while a stale finalize call races in, this helper will report a false success (ok=True, noop=True, status="todo") instead of an error, and the worker/orchestrator will believe its lifecycle call succeeded while the task actually needs redoing — silently dropped work with no error signal. This affects all four call sites at Lines 724-732, 801-810, 1523-1531, and 1570-1578.

🐛 Proposed fix — drop non-terminal statuses from the shared tuple
 _TERMINAL_RECONCILIATION_STATUSES = (
     "done",
     "blocked",
-    "todo",
-    "triage",
     "review",
     "human_review",
 )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
_TERMINAL_RECONCILIATION_STATUSES = (
"done",
"blocked",
"todo",
"triage",
"review",
"human_review",
)
def _terminal_transition_noop(
kb,
conn,
task_id: str,
*,
terminal_statuses: tuple[str, ...],
action: str,
extra: Optional[dict] = None,
) -> Optional[str]:
"""Reconcile a lost CAS race when the desired terminal state already won."""
task = kb.get_task(conn, task_id)
if task is None or task.status not in terminal_statuses:
return None
run = kb.latest_run(conn, task_id)
payload = {
"task_id": task_id,
"run_id": run.id if run else None,
"status": task.status,
"noop": True,
}
if isinstance(extra, dict):
payload.update(extra)
logger.info(
"%s: reconciled as no-op for task %s already in %s",
action,
task_id,
task.status,
)
return _ok(**payload)
_TERMINAL_RECONCILIATION_STATUSES = (
"done",
"blocked",
"review",
"human_review",
)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tools/kanban_tools.py` around lines 298 - 336, Remove the non-terminal "todo"
and "triage" values from _TERMINAL_RECONCILIATION_STATUSES, leaving only
statuses that represent a successful terminal outcome for the guarded lifecycle
calls. Keep _terminal_transition_noop and its four call sites unchanged so
reopened or rework states return the existing error path instead of a false noop
success.

sahilm-ti pushed a commit that referenced this pull request Jul 23, 2026
* fix(kanban): recover missed goal finalizers

* test(kanban): cover duplicate blocker recovery

---------

Co-authored-by: Sahil (AI) <266772320+sahilm-ai@users.noreply.github.com>
sahilm-ti pushed a commit that referenced this pull request Jul 28, 2026
* fix(kanban): recover missed goal finalizers

* test(kanban): cover duplicate blocker recovery

---------

Co-authored-by: Sahil (AI) <266772320+sahilm-ai@users.noreply.github.com>
sahilm-ti pushed a commit that referenced this pull request Aug 24, 2026
* fix(kanban): recover missed goal finalizers

* test(kanban): cover duplicate blocker recovery

---------

Co-authored-by: Sahil (AI) <266772320+sahilm-ai@users.noreply.github.com>
sahilm-ti pushed a commit that referenced this pull request Sep 2, 2026
* fix(kanban): recover missed goal finalizers

* test(kanban): cover duplicate blocker recovery

---------

Co-authored-by: Sahil (AI) <266772320+sahilm-ai@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants