I need to understand why triage mode gets re-entered. Find and read the following code paths in the desloppify codebase:
The stage flow commands file (where triage stages get injected, around line 67)
The stale policy file (where is_triage_stale() is defined, around line 148)
The guardrails file (where resolve gets blocked, around line 107)
The resolve command file (where triage freshness is checked, around line 159)
Any triage handler files
Search for these patterns: "stage_flow", "stale_policy", "guardrails", "is_triage_stale", "triage_fresh", "re-enter", "TRIAGE MODE", "not triaged"
Read the full contents of all relevant files. I need to understand the exact flow that causes triage to be re-entered when it's still in-progress (not because new issues appeared). Thoroughness: very thorough.
Assistant
I'll search for and read the triage-related code paths you've specified. Let me start with comprehensive searches to locate these files and understand the flow.
Assistant
Now let me search for the resolve command specifically:
Assistant
Perfect! Now let me read the key files. I'll start with the main files you mentioned:
Assistant
Now let me look at triage-related handler files and the plan.py facade:
Assistant
Now let me read the stale_dimensions.py file which is imported by plan.py and contains the re-exported functions:
Assistant
Perfect! Now let me search for where sync_triage_needed is called to understand the full flow:
Assistant
Let me read the reconcile.py file and the plan_reconcile.py command to see where sync_triage_needed gets called:
Assistant
Excellent! Now let me get a clearer understanding by looking at the exact stale policy logic one more time with focus on lines around 128-154:
Assistant
Perfect! Now let me look at the test file to understand the actual behavior:
Assistant
Perfect! Now I have all the context I need. Let me create a comprehensive summary document of the triage re-entry flow:
Summary: Why Triage Mode Gets Re-entered
Based on my thorough analysis of the codebase, I've identified the exact flow that causes triage mode to be re-entered when stages are already in-progress (not because new issues appeared). Here's the complete picture:
Key Files Read
/Users/user_c042661f/Documents/desloppify/desloppify/engine/_plan/stale_dimensions.py — Central sync logic, lines 348-406
/Users/user_c042661f/Documents/desloppify/desloppify/app/commands/scan/plan_reconcile.py — Post-scan plan sync, lines 225-240
/Users/user_c042661f/Documents/desloppify/desloppify/engine/plan.py — Public plan facade
/Users/user_c042661f/Documents/desloppify/desloppify/tests/plan/test_stale_policy.py — Tests clarifying the behavior
The Bug: Lines 148-152 in stale_policy.py
python
confirmed = set(meta.get("triage_stages", {}).keys())
if confirmed:
order = set(plan.get("queue_order", []))
if order & set(triage_ids):
return True
return False
This is the problematic logic in is_triage_stale(). When called with the triage_ids parameter (which is the set of all 6 triage stage IDs), it does the following:
Check if any triage stages are confirmed (confirmed = set(meta.get("triage_stages", {}).keys()))
This set contains stage names like "observe", "reflect", "organize", "enrich", "sense-check" — these are keys in the dict, not IDs with the "triage::" prefix
If yes, check if ANY triage ID is in the queue (if order & set(triage_ids))
This checks whether any of {"triage::observe", "triage::reflect", "triage::organize", ...} are present in queue_order
If both conditions are true → return True (triage is stale)
The Problem
The logic says: "If triage work is confirmed AND triage stage IDs are in the queue, then triage is stale (needs re-running)"
But this is backwards! The issue is:
When triage stages are already in-progress (e.g., just completed observe and working on reflect), the stage IDs ARE in the queue
The confirmed set will contain completed stage names
This causes is_triage_stale() to return True EVEN WHEN NO NEW ISSUES APPEARED
Where This Gets Called
In /Users/user_c042661f/Documents/desloppify/desloppify/app/commands/scan/plan_reconcile.py (line 225-240):
python
def _sync_triage_and_log(
plan: dict[str, object],
state: state_mod.StateModel,
) -> bool:
triage_sync = sync_triage_needed(plan, state)
if not triage_sync.changes:
return False
if triage_sync.injected:
print(...)
append_log_entry(plan, "sync_triage", ...)
return True
This calls sync_triage_needed() which internally calls is_triage_stale().
In /Users/user_c042661f/Documents/desloppify/desloppify/engine/_plan/stale_dimensions.py (lines 348-406):
python
def sync_triage_needed(plan, state):
# ... lines 375-391 ...
if already_present:
# Stages present — check if the reason for injection still applies.
if last_hash and not confirmed:
new_since_triage = _new_review_ids_since_triage(state, meta)
if not new_since_triage:
_prune_all_triage_stages(order)
result.pruned = True
return result
if current_hash and current_hash != last_hash:
new_since_triage = _new_review_ids_since_triage(state, meta)
if new_since_triage:
result.injected = _inject_pending_triage_stages(order, plan, confirmed)
And in /Users/user_c042661f/Documents/desloppify/desloppify/app/commands/resolve/cmd.py (line 159-164):
python
if args.status == "fixed":
require_triage_current_or_exit(
state=state,
bypass=bool(getattr(args, "force_resolve", False)),
attest=getattr(args, "attest", "") or "",
)
This blocks resolve --status fixed unless triage is "current" by calling triage_guardrail_status() which calls is_triage_stale().
Auto-Injection on Stage Commands
When you run any stage command like desloppify plan triage --stage observe --report "...":
In /Users/user_c042661f/Documents/desloppify/desloppify/app/commands/plan/triage/stage_flow_commands.py (lines 64-72):
python
if not has_triage_in_queue(plan):
inject_triage_stages(plan)
meta = plan.setdefault("epic_triage_meta", {})
meta.setdefault("triage_stages", {})
resolved_services.save_plan(plan)
print(colorize(" Planning mode auto-started (6 stages queued).", "cyan"))
This auto-injects the triage stage IDs ("triage::observe", "triage::reflect", etc.) into queue_order if they're not already there.
The Re-entry Scenario
What causes re-entry while stages are in-progress:
User starts triage: desloppify plan triage --start
Calls _cmd_triage_start() which calls inject_triage_stages(plan) → adds all 6 stage IDs to queue_order
Sets meta["triage_stages"] = {}
User records observe stage: desloppify plan triage --stage observe --report "..."
This version calls with triage_ids=TRIAGE_IDS per stale_dimensions.py line 547
In stale_policy.py lines 148-152: triggers the problematic logic
confirmed = {"observe"} (truthy)
order & set(triage_ids) → True (triage IDs are in queue)
Returns True → "triage is stale"
Blocks the resolve command until triage is completed
Test Evidence
In /Users/user_c042661f/Documents/desloppify/desloppify/tests/plan/test_stale_policy.py lines 490-501:
python
def test_stale_when_triage_stages_confirmed_and_triage_ids_in_queue(self):
"""Stale when confirmed stages exist and triage IDs are in the queue."""
plan = {
"epic_triage_meta": {
"triaged_ids": ["r1"],
"triage_stages": {"observe": {"report": "analysis"}},
},
"queue_order": ["triage::observe", "triage::reflect"],
}
state = _state_with_review_issues("r1")
triage_ids = {"triage::observe", "triage::reflect"}
assert is_triage_stale(plan, state, triage_ids=triage_ids) is True
This test explicitly documents the behavior: when triage_stages contains confirmed stages AND triage IDs are in the queue, is_triage_stale() returns True even though no new issues exist.
Root Cause Summary
The second condition in is_triage_stale() (lines 148-152) is semantically broken. It was apparently intended to handle a case where you manually re-started triage with --start, but it unconditionally marks triage as stale whenever:
ANY triage stage has been confirmed, AND
ANY triage stage IDs are in the queue_order
This catches the normal in-progress triage workflow (where both conditions are always true until completion), not just the edge case of needing to re-triage.
File Paths Reference
Stale policy (staleness detection):/Users/user_c042661f/Documents/desloppify/desloppify/engine/_plan/stale_policy.py
Stale dimensions (sync orchestration):/Users/user_c042661f/Documents/desloppify/desloppify/engine/_plan/stale_dimensions.py