I need to understand a reported bug about inconsistent queue behavior in the desloppify codebase. Specifically:
plan show reports queue length from plan["queue_order"]
plan queue/next build a dynamic work queue that can inject synthetic subjective items
When all objective items are skipped, subjective items appear but plan show says queue is empty
Score stays frozen because queue_remaining > 0 (from subjective items)
workflow::run-scan never appears because queue isn't truly empty
I need to find and read the relevant code sections. Search very thoroughly for:
The plan show command implementation — look in desloppify/app/commands/ for plan-related files, specifically where it reads plan["queue_order"] or reports queue length
The work queue building logic — look in desloppify/engine/_work_queue/ for core.py, synthetic.py, subjective_policy.py
Where workflow::run-scan is injected
Where score/status freezing happens based on queue_remaining
The lifecycle/endgame logic that surfaces subjective items when no objective items remain
Read each relevant file thoroughly. I want to understand:
Is plan show truly using a different queue definition than next/plan queue?
Is the subjective injection after skipping all objectives intentional or a bug?
Is the score freezing behavior correct?
Should run-scan appear before subjective items?
Be very thorough - read full files where needed.
Assistant
I'll search thoroughly for all the relevant code. Let me start with multiple parallel searches.
Assistant
Now let me read the remaining relevant files in parallel.
Assistant
Now let me look at the score freezing / queue_remaining logic and the next command.
Assistant
Now let me check the scan preflight gating and the _clear_plan_start_scores_if_queue_empty more carefully, plus how breakdown.actionable relates to breakdown.queue_total.
Assistant
Now let me check the stale_dimensions sync to understand what IDs are injected into plan["queue_order"] for subjective items, and how the _clear_plan_start_scores_if_queue_empty actually interacts with the subjective items being in queue_order.
Assistant
Now I have a thorough understanding of the codebase. Let me also quickly confirm the key relationship between plan show's count and what build_work_queue returns for subjective items:
Assistant
User
I need to read specific files to plan fixes for queue inconsistency bugs. Read these files thoroughly:
desloppify/engine/_work_queue/core.py — full file, focus on build_work_queue, _apply_lifecycle_filter, _empty_queue_fallback, _has_objective_items, separate_skipped
desloppify/engine/_work_queue/synthetic.py — how subjective items are built
I'll read all the files in parallel to get a comprehensive view.
Assistant
The subjective_policy.py file wasn't found at that path. Let me locate it and read the remaining files.
Assistant
Now let me read the plan and status command files.
Assistant
Now let me do targeted searches for any existing "subjective-only" concept and review a few more key functions referenced:
Assistant
Good. I found an important test reference. Let me look at it and also check the scan preflight use of actionable:
Assistant
Let me read the scan preflight file and check the _print_score_section logic in status more carefully, plus the scan preflight queue guard:
Assistant
I now have a complete picture. Here is a thorough analysis of every component involved in the reported bug.
Analysis: Queue Inconsistency Bug
1. What plan show reports vs. what next/plan queue show
plan show (cmd.py:84) reads directly from the persisted JSON:
python
ordered = len(plan.get("queue_order", []))
This is a raw count of IDs in queue_order. It does NOT run build_work_queue. It does NOT apply lifecycle filtering, subjective injection logic, or the _empty_queue_fallback. What it prints is literally "how many IDs live in the plan JSON's queue_order list."
next / plan queue both call build_work_queue(state, options=QueueBuildOptions(...)) which runs a full pipeline (core.py:94–145):
Builds issue items from state
Builds subjective dimension items dynamically (not from queue_order)
Builds workflow items by checking queue_order for triage::*/workflow::* IDs
Filters by impact floor
Separates plan-skipped items
Applies _apply_lifecycle_filter (the endgame gate)
Sorts
If queue is empty after all that, injects workflow::run-scan (the _empty_queue_fallback)
The critical distinction: subjective_dimension items are generated dynamically in _gather_subjective_items → build_subjective_items. They are NOT read from queue_order. Their IDs (subjective::<slug>) may or may not appear in queue_order, but the dynamic build doesn't require it — the items are synthesized from state["dimension_scores"] whenever score is below threshold.
2. The "all objective items skipped" scenario — what actually happens
When all objective issues are skipped (they're in plan["skipped"]), here is what build_work_queue sees:
Step 1: build_issue_items returns all issues. separate_skipped (step 3) splits them out. After _plan_postsort, skipped items are re-appended only if include_skipped=True — otherwise the effective non-skipped list is empty for objective items.
Step 1 also: _gather_subjective_items builds subjective items dynamically. _is_endgame_only is True for these when initial_review=False. But _has_objective_items (core.py:273) checks only non-skipped items in the items list at the point of _apply_lifecycle_filter — and the skipped items have already been separated before lifecycle filtering runs (step 3 happens before step 4).
So when all objective items are in plan["skipped"], _has_objective_items(items) returns False because the skipped items were removed in step 3. This means _apply_lifecycle_filter returns all items, including endgame-only subjective items. Subjective items surface.
This is intentional behavior — the intent is that once you've skipped everything, you're in "endgame" mode and subjective work becomes visible. Whether skipping everything should be treated as "objective work complete" vs. "objective work still pending but deferred" is the philosophical question, but the code deliberately treats skipped-out items as if they don't exist for lifecycle purposes.
3. Is the subjective injection after skipping all objectives a bug?
From the code's perspective: it's intentional but potentially surprising. The lifecycle filter in _apply_lifecycle_filter (core.py:290) only considers items still in the active items list at filter time. Skipped items are removed in separate_skipped before the lifecycle filter runs. So the filter cannot tell the difference between "all objectives resolved" and "all objectives skipped."
The SubjectiveVisibility.should_inject_to_plan logic in subjective_policy.py has a distinction: it checks has_objective_backlog, which is computed from open issues excluding plan-skipped IDs. So at scan time (sync_stale_dimensions during reconcile_plan_post_scan), stale/under-target subjective IDs will be injected into queue_order when all objectives are skipped, because skipped IDs are excluded from objective_count (subjective_policy.py:103–119).
This means after a scan that observes all objectives skipped, plan["queue_order"] will contain subjective::* IDs. That in turn means plan show will now report a non-zero queue length — so the claim in the bug report that "plan show says queue is empty" requires clarification. plan show would say queue is N (however many subjective IDs were injected), but they're subjective::* IDs. The disconnect the user sees is likely that plan show reports N items but those items don't seem like "real work."
4. Score frozen / queue_remaining > 0
The score-freezing path in queue_progress.py:print_execution_or_reveal (line 290–336):
python
remaining = breakdown.queue_total if breakdown else 0
# Frozen: objective work remains — show frozen plan-start score and return
if remaining > 0 and frozen_strict is not None:
objective_remaining = remaining - breakdown.subjective - breakdown.workflow
if objective_remaining > 0:
# show frozen score
return
Critical: the score is only frozen when objective_remaining > 0. When all objectives are skipped and only subjective items remain, breakdown.subjective > 0 but objective_remaining == 0, so the code falls through to the live score display path + _print_objective_drained_banner. Score is NOT frozen in the all-skipped scenario.
However, in status/cmd.py:_print_score_section (line 196–227), it uses queue_remaining = breakdown.queue_total (not breakdown.actionable). If subjective items are in the queue, queue_remaining > 0 and it calls print_frozen_score_with_queue_context. This is the bug: status uses a simpler check (queue_total > 0) while next/post-scan uses objective_remaining > 0. The status command doesn't distinguish between objective and subjective remaining items.
5. workflow::run-scan never appearing
workflow::run-scan is only injected by _empty_queue_fallback (core.py:302–317):
python
def _empty_queue_fallback(plan: dict | None) -> list[WorkQueueItem]:
"""Return a 'run scan' nudge when an active plan cycle has cleared."""
if not plan:
return []
plan_scores = plan.get("plan_start_scores", {})
if plan_scores.get("strict") is None:
return []
return [{"id": "workflow::run-scan", ...}]
This only fires when items is empty after the full pipeline including lifecycle filter. If subjective items survived the lifecycle filter (because no objective items exist), items is non-empty, and _empty_queue_fallback is never called. So workflow::run-scan never appears when subjective items are present.
This is a real gap: once the user skips all objective items and subjective items surface, there is no workflow::run-scan nudge to prompt them to re-scan. The subjective items have a primary_command of desloppify review --prepare ..., which is the right next action, but after completing that the user still needs to scan — and nothing prompts that.
6. _clear_plan_start_scores_if_queue_empty and the cycle-clearing logic
In plan_reconcile.py:_clear_plan_start_scores_if_queue_empty (line 124–145):
breakdown.actionable (queue_progress.py:42–49) is max(0, queue_total - workflow). It subtracts workflow items (like workflow::run-scan) but does NOT subtract subjective items. So if subjective items are in the queue (counted in queue_total and not in workflow), breakdown.actionable > 0 and plan_start_scores are NOT cleared.
This means the plan cycle remains "active" (with a frozen start score in plan_start_scores) indefinitely as long as subjective items remain in queue_order. The user cannot complete the cycle and get a fresh scan start without either:
Resolving the subjective items (completing the reviews)
Force-rescanning with attestation
Manually clearing plan_start_scores
Summary of Confirmed Bugs and Design Gaps
Bug 1 — plan show vs. dynamic queue counts are different quantities
plan show reads len(plan["queue_order"]). The dynamic queue (next/plan queue) reads build_work_queue which includes dynamically-synthesized subjective items NOT necessarily in queue_order. These counts will diverge: you can have subjective items visible in next that are not in queue_order, and vice versa (items in queue_order that are skipped won't appear in next).
Bug 2 — Score frozen in status but not in next when only subjective items remain
status/cmd.py:_print_score_section line 204 gates on queue_remaining = breakdown.queue_total (any items including subjective). queue_progress.py:print_execution_or_reveal gates on objective_remaining > 0. These two code paths give different answers: status shows the score as frozen; next/post-scan shows it as live. This is an inconsistency.
Bug 3 — workflow::run-scan never surfaces when subjective items are present
_empty_queue_fallback only fires when items is empty. Subjective items after all objectives are skipped prevent it from firing. The user never sees the "run scan" prompt even though a scan is the appropriate next step after completing subjective reviews.
Bug 4 — Plan cycle never clears while subjective items in queue_order
breakdown.actionable only excludes workflow_* kinds, not subjective_dimension. So _clear_plan_start_scores_if_queue_empty never fires while any subjective items are in queue_order, even if those items only require reviews (not a scan to clear). The frozen plan-start score persists through the entire subjective review phase, which may span many days.
The root cause of all four: there are now three categories of items — objective issues, subjective dimension assessments, and workflow navigation steps — but most of the lifecycle/freezing/gating logic only has a binary model (work present vs. queue empty). The breakdown.actionable property partially addresses this by excluding workflow items, but does not exclude subjective items. The lifecycle filter in build_work_queue does filter endgame-only subjective items, but only during queue building, not during the score-freezing or plan-cycle-clearing checks.
_clear_plan_start_scores_if_queue_empty
For each file, note:
The exact line numbers of the relevant logic
What data structures are used (plan dict keys, breakdown fields)
Where the binary "work present vs empty" assumption lives
Also search for any existing concept of "subjective-only remaining" or similar that might already partially address this.
I now have a thorough picture of all seven files. Here is the complete analysis:
File-by-file analysis
1. engine/_work_queue/core.py — build_work_queue and lifecycle filter
Key line numbers and logic:
Lines 90-145 — build_work_queue pipeline. Step 4 (line 124) is the lifecycle filter:
python
items = _apply_lifecycle_filter(items)
Step 6 (lines 131-133) is the empty-queue fallback:
python
if not items:
items += _empty_queue_fallback(plan)
Lines 273-279 — _has_objective_items: iterates the already-gathered items list and returns True if any item has kind == "issue" and detector not in NON_OBJECTIVE_DETECTORS. This is the binary decision point. It sees items that have already been gathered and passed the impact floor but have NOT yet been lifecycle-filtered. This is important: the check is on the mid-pipeline list, before lifecycle filtering removes endgame subjective items.
Lines 282-299 — _is_endgame_only and _apply_lifecycle_filter: endgame items are kind == "subjective_dimension" where initial_review is falsy. The lifecycle filter passes all items when _has_objective_items is False, i.e., when no objective issues are present.
Lines 302-317 — _empty_queue_fallback: fires only when items is empty after lifecycle filter AND sort. Requires plan_start_scores.strict to be non-None. The "work present vs empty" binary assumption here is: not items — a hard zero-items check. It does not distinguish "empty because subjective-only was filtered" from "truly empty."
Data structures:WorkQueueItem TypedDict with keys kind, detector, id, is_subjective, initial_review, estimated_impact, confidence, summary, file, etc.
2. engine/_work_queue/synthetic.py — subjective item construction
Key line numbers:
Lines 166-259 — build_subjective_items: iterates all_subjective_entries, skips entries where strict_val >= threshold. For each entry below threshold, emits a WorkQueueItem with:
kind: "subjective_dimension"
initial_review = True when entry.get("placeholder") or name is in unassessed_dims or (strict_val <= 0 and failing == 0) (line 226-229)
initial_review = False (absent) for stale/under-target items
Lines 232-258 — The is_stale/is_unassessed/under-target distinction lives here. Only initial_review=True items survive lifecycle filter when objective work remains.
Lines 34-42 — should_inject_to_plan: unscored_ids inject unconditionally; stale_ids and under_target_ids inject only when not has_objective_backlog. This is the plan reconciliation gate, separate from the queue lifecycle filter.
Lines 44-50 — should_evict_from_plan: evicts stale/under-target IDs when objective backlog is present.
Lines 110-119 — compute_subjective_visibility: counts objective issues excluding NON_OBJECTIVE_DETECTORS, suppressed, evidence-only, and out-of-scope issues. Returns has_objective_backlog = objective_count > 0 — this is also a binary decision. No notion of "subjective-only remaining" in the plan.
Key distinction:SubjectiveVisibility operates on raw state issues (for plan injection decisions), while _has_objective_items in core.py operates on the already-built items list (for display/lifecycle decisions). They should agree but use different code paths.
4. app/commands/plan/cmd.py — plan show
Key line numbers:
Lines 81-132 — _cmd_plan_show: reads plan.get("queue_order", []) directly at line 84 and prints len(...) as the count. This is a raw queue_order length, not the live queue count. It does not call build_work_queue or plan_aware_queue_breakdown. There is no score-freezing logic here.
Lines 41-73 — cmd_plan_output (plan with no subcommand): calls planning_mod.generate_plan_md(state) — no direct queue breakdown or score freezing.
Score freezing in plan cmd: There is none directly. Score freezing display is centralized in queue_progress.py and called from status/cmd.py and scan output, not from plan/cmd.py.
Data structure:plan.get("queue_order", []) — a list of issue IDs. plan.get("skipped", {}) — dict keyed by issue ID.
5. app/commands/status/cmd.py — _print_score_section and score freezing
Key line numbers:
Lines 192-227 — _print_score_section: the binary assumption lives at line 204:
python
if plan_start_strict is not None and queue_remaining > 0:
print_frozen_score_with_queue_context(...)
else:
# show live scores
queue_remaining = breakdown.queue_total (line 200). This is a raw total — it counts subjective items. If subjective items remain but objective work is done, queue_remaining > 0 is True, so it shows the frozen score even though objective work is complete. The "phase transition" display logic exists in print_execution_or_reveal in queue_progress.py (used by the scan command path) but is NOT used here in _print_score_section.
Lines 104-107 — _plan_active determination: plan is considered active only when it has queue_order or clusters. A plan that has been started but has all its objective items done and only subjective items queued is still "active."
Line 130 — _objective_backlog = _ctx.policy.objective_count — used for dimension table and subjective followup display. This is the corrected value, but it is NOT piped into the score freeze decision.
Critically: actionable does NOT subtract subjective items. So when only subjective items remain, actionable = queue_total - workflow > 0. This is the core of the inconsistency.
Lines 52-132 — plan_aware_queue_breakdown: calls build_work_queue(count=None) then counts subjective (line 85-88) and workflow (line 89-91) separately. The subjective count uses is_subjective_queue_item() helper.
Lines 290-336 — print_execution_or_reveal: has the correct three-mode logic:
Frozen (objective > 0): line 319, objective_remaining = remaining - breakdown.subjective - breakdown.workflow; if objective_remaining > 0: show frozen
Phase transition (objective = 0 but non-objective remains): lines 334-335
Live (no remaining): show live
This logic correctly computes objective_remaining on line 318. However, this function is only called from the scan command path (show_score_with_plan_context), not from _print_score_section in status/cmd.py.
Lines 242-263 — print_frozen_score_with_queue_context: always shows frozen score when called. No objective-vs-subjective distinction inside this function.
The divergence:print_execution_or_reveal (used after scan) correctly gates frozen display on objective_remaining > 0. But _print_score_section in status gates on queue_remaining > 0 (the raw total), causing frozen score to show even when only subjective work remains.
Uses breakdown.actionable = queue_total - workflow. This does NOT treat "subjective-only remaining" as empty. If subjective items remain, actionable > 0, queue_empty = False, and plan-start scores are NOT cleared.
So the cycle does not complete (scores stay frozen) until subjective items are also resolved — which the user may never do if they're all endgame-gated items that were only displayed because the queue was empty.
Lines 273-286 — _sync_plan_start_scores_and_log calls seeding, then clearing. The clearing check runs on every scan post-reconcile, but only after the seed check passes.
Lines 161 — cycle_just_completed = not plan.get("plan_start_scores") — this flag is set to True immediately after _clear_plan_start_scores_if_queue_empty clears the scores. Used in _sync_stale_and_log and _sync_auto_clusters_and_log to allow injection of stale subjective items.
Where the binary "work present vs empty" assumption lives
There are four distinct locations with this assumption, and they do not all agree:
Location
Expression
What it counts
Bug?
core.py:297_has_objective_items
any(kind=="issue" and detector not in NON_OBJECTIVE_DETECTORS)
queue_remaining > 0 where queue_remaining = breakdown.queue_total
Raw total
Worst — also counts workflow items
The print_execution_or_reveal function (scan path) has the correct logic at lines 318-319 (objective_remaining = remaining - breakdown.subjective - breakdown.workflow), but this knowledge is not shared back into actionable, _print_score_section, or _clear_plan_start_scores_if_queue_empty.
Existing "subjective-only remaining" concepts
Yes — partially addressed in two places:
print_execution_or_reveal (lines 318-319, queue_progress.py): the scan post-fix display correctly computes objective_remaining and gates frozen display on objective_remaining > 0. But this is local to that function and not exported as a property or helper.
_print_objective_drained_banner (lines 266-287, queue_progress.py): the banner function explicitly handles the "objective drained, subjective/workflow remains" phase transition for the scan path.
Test test_injection_when_only_subjective_in_queue (tests/plan/test_stale_dimensions.py:391): there is a test that verifies stale dims are injected when only subjective IDs remain — so the plan reconciliation injection side has been thought about, but the score-clearing side has not.
Test test_reveal_shows_live_scores_when_only_subjective_remains (tests/commands/test_queue_progress.py:369): there is a test for print_execution_or_reveal covering the phase-transition case, confirming that function works. But there is no equivalent test for _print_score_section.
Summary of the inconsistency bugs
Bug 1 — _print_score_section in status/cmd.py (line 204): Gates frozen score on breakdown.queue_total > 0 instead of objective_remaining > 0. When only subjective items remain, it still shows the frozen score. Fix: compute objective_remaining = breakdown.queue_total - breakdown.subjective - breakdown.workflow (mirroring the scan path logic at queue_progress.py:318) and gate on that.
Bug 2 — QueueBreakdown.actionable (line 49, queue_progress.py): Defined as queue_total - workflow, so it still counts subjective items as actionable work. The docstring says "use this when gating on is there work left?" — but that guidance is wrong for score-clearing purposes. The _clear_plan_start_scores_if_queue_empty function relies on this. Fix: either add a second property objective_actionable = queue_total - workflow - subjective, or make actionable subtract subjective, and update _clear_plan_start_scores_if_queue_empty to match the intended semantics. (Note: the scan preflight also uses breakdown.actionable at preflight.py:64 — that guard probably SHOULD block on subjective remaining, so actionable behavior there might be correct. Two different consumers with different needs.)
Bug 3 — _clear_plan_start_scores_if_queue_empty (line 137, plan_reconcile.py): Relies on breakdown.actionable == 0. Because actionable includes subjective, the cycle never completes while subjective items remain in the queue, even if all objective work is done. This keeps plan_start_scores populated indefinitely, blocking the post-scan score reveal.
The core tension: The scan preflight (preflight.py:64) legitimately wants to block on actionable including subjective items — you should finish your subjective work before rescanning. But score-clearing and score-display need to distinguish "objective done, subjective pending" from "everything done." These need separate properties or explicit arithmetic at the call sites.