Skip to content

fix: resolve the closed pr head so a clean closed worktree can prune - #31124

Merged
bolichen97 merged 2 commits into
mainfrom
fix/closed-pr-prune-head-30399
Nov 12, 2026
Merged

bolichen97 merged 2 commits into
mainfrom
fix/closed-pr-prune-head-30399

Conversation

@JLargent42

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A clean worktree whose PR was closed without merging is offered under Prune merged as a closed candidate instead of sitting in Kept as closed_unverified on every scan.

In Dev Fleet, a clean worktree whose branch has a CLOSED (not merged) pull request is always kept with "PR closed but verification unavailable — retry", even when gh answers CLOSED with the matching head OID. The manual closed-PR prune path can never be used.

Why it matters

The closed-PR prune path (manual, with the unmerged-commits warning) is unreachable, so every declined or superseded worktree has to be removed by hand, and the Kept list shows a misleading "retry" that never succeeds.

Not a goal

  • Changing what the reaper auto-prunes (it still filters to merged).
  • Changing any removal-path check; every removal caller keeps the MERGED-only lookup.
  • Relaxing any refusal: dirty trees, reused heads carrying an OPEN PR, saturated PR batches and diverged heads stay refused.

What changed (motivation → approach → change)

  • Symptom: _prunable returns closed_unverified for every clean closed-PR worktree.
  • Root cause: the closed branch binds its verdict to the PR head through fleet_state._fetch_pr_head_oid, which only ever returns the head of a MERGED PR. For a CLOSED PR it always returns None, so the fail-closed guard always fires.
  • Change: _fetch_pr_head_oid takes a keyword-only state (MERGED by default, or CLOSED; anything else is refused before gh runs). It keeps the same fresh gh pr list --state all query, the saturation sentinel, and the refusal when any OPEN PR is on the head; only the state the returned OID must come from changes. The closed branch of _prunable passes state="CLOSED", then still runs the head-containment check, so a branch reused after its PR closed stays closed_new_commits.

Backwards compatibility

Compatible: the new parameter is keyword-only with a default that keeps the existing MERGED-only behavior for every current caller.

Removes nothing: the change only lets a previously unreachable closed verdict be reached; no refusal, key, or caller is removed.

Tests

  • test/test_dev_fleet_server_coverage_prune.py::test_prunable_closed_clean_resolves_head_through_real_lookup: with only gh stubbed, a clean closed-PR worktree whose head matches is a closed candidate.
  • test/test_dev_fleet_server_coverage_prune.py::test_prunable_closed_with_open_pr_on_head_stays_unverified: a reused head with an OPEN PR next to the CLOSED one stays closed_unverified.
  • test/test_dev_fleet_server_more_coverage.py::test_fetch_pr_head_oid_closed_gate_returns_closed_head: state="CLOSED" returns the CLOSED head; the default still returns None for it.
  • test/test_dev_fleet_server_more_coverage.py::test_fetch_pr_head_oid_closed_gate_keeps_refusals: OPEN-on-head, saturated batch, MERGED-only head and an OPEN state are all refused, the last without calling gh.

Proof: PROVEN test/test_dev_fleet_server_coverage_prune.py::test_prunable_closed_clean_resolves_head_through_real_lookup (head 9e2eacfd4bba)

python3 scripts/local-gate.py green locally (backend related: 1308 passed; frontend related: 466 files / 13395 tests passed). Lint, mypy, comment-history and brand gates pass.

Manual verification

N/A — unit coverage sufficient: the defect sits entirely in how the gh pr list JSON is read, which the new tests drive with real-shaped payloads.

Related Issues

Closes #30399

Sibling sweep

Swept: git grep -n "_fetch_pr_head_oid" -- src/
Sibling: src/kiro_crew/apps/builtins/dev_fleet/worktree_ops.py -- changed here
Sibling: src/kiro_crew/apps/builtins/dev_fleet/worktree_ops_removal.py -- not affected -- every call there gates a MERGED verdict and keeps the default
Sibling: src/kiro_crew/apps/builtins/dev_fleet/fleet_state.py -- changed here

Pattern harvest

Not generalizable: one lookup helper was gated to a single PR state and reused by a caller whose verdict rests on a different state.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license.

@JLargent42
JLargent42 requested review from a team and KiroCrewCaptain as code owners November 6, 2026 06:03
@JLargent42
JLargent42 requested a review from Zedmor November 6, 2026 06:03
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 75b01c8fdde5d644876b602e108d979a3d04a8a3 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 75b01c8fdde5d644876b602e108d979a3d04a8a3 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: the fix targets the real cause (a lookup that only accepted MERGED) by letting the caller choose the state, every refusal stays in place, and the removal callers keep the MERGED-only default.

Notes

  • The batched-preview pin wt-c: closed_unverified → closed checked that the batched result matches the per-head result. It recorded the bug, not a decision, so changing it is correct.

[DESIGN-REVIEWED] 75b01c8

Description read

[DESCRIPTION-READ] 11b7057db674ad3a886004093b35cffcf6a290d007ea8e80b7612f37c27c1bb6

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 75b01c8fdde5d644876b602e108d979a3d04a8a3 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

This is a mechanism-level fix for #30399. The head lookup only matched MERGED, so the closed branch could never resolve an OID. The rewritten batch pin "wt-c": "closed_unverified" was recording that bug, not a decision.

What this change ships

Inventory (5 items) — 5 justified

Intent: let an operator prune a clean worktree whose PR was closed without merging, from the existing manual Prune-merged path — FIX.

  1. A clean worktree whose closed-PR head matches now shows as a closed prune candidate instead of sitting in Kept as "verification unavailable — retry" — justified
  2. A reused head that also carries an OPEN PR is still kept as closed_unverified — justified
  3. The head-OID lookup takes a keyword-only state (MERGED by default, CLOSED for the closed-PR prune check); all 4 other calls (grep "_fetch_pr_head_oid(" in src/) keep MERGED — justified
  4. Asking the lookup for any state other than MERGED or CLOSED returns nothing before gh runs — justified
  5. The batched-preview test now expects closed for the clean closed head, matching the per-head lookup — justified

[FIRST-PRINCIPLES-REVIEWED] 75b01c8

Description read

[DESCRIPTION-READ] 12fa99213bbfb0c9714d8bcd7a24b4f46f822982d8d2fc695cbb412a24c1e1ba

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 75b01c8fdde5d644876b602e108d979a3d04a8a3 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 75b01c8

Verdict parsed from the review's SHA-scoped output markers for commit 75b01c8fdde5d644876b602e108d979a3d04a8a3.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 75b01c8fdde5d644876b602e108d979a3d04a8a3 false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 75b01c8fdde5d644876b602e108d979a3d04a8a3 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 75b01c8

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 75b01c8fdde5d644876b602e108d979a3d04a8a3 false-positive: <reason, 20 to 500 characters>

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: checking Automated validation is still running labels Nov 6, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor

HOLD. Reviewed 57bf7ac.

What would clear it:

  1. Resolve the merge conflict. Main has merged perf: batch the dev fleet prune preview's pr lookups #31165 (e4ab9d2), which rewrote _fetch_pr_head_oid and _prunable's call sites to read batched PR rows. After the rebase, the state="CLOSED" gate must also apply when rows= is passed. Otherwise the preview's closed branch still gets None and the fix does nothing.
  2. A human owner signs off on the deletion change. This PR makes a clean closed-PR worktree a reachable closed prune candidate, which the manual prune then removes past the "PR not merged" refusal. It also widens _fetch_pr_head_oid, the gate destructive callers rely on, to return a CLOSED head (fleet_state.py#L184-L278, worktree_ops.py#L226-L234). That is reserved for a human review.

The code as written looks sound. Every other caller keeps the MERGED default, an OPEN PR on the head is still refused, and the head-containment check still runs. All AI lanes pass on this head. The rebase needs a new review.

— crew-pr-reviewer (r148)

@JLargent42

Copy link
Copy Markdown
Contributor Author

The KiroCrew conductor (largjaco's v2 drive-to-green session) now owns this PR under the 24h orphan rule; the desk session that opened it has been offline since 11-06. I'll rebase onto main, fix CI and answer review here.

@JLargent42
JLargent42 force-pushed the fix/closed-pr-prune-head-30399 branch from 57bf7ac to 75b01c8 Compare November 12, 2026 19:38
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention readiness: passed Eligible automated validation passed for the current revision and removed readiness: passed Eligible automated validation passed for the current revision merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Nov 12, 2026

@bolichen97 bolichen97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving. I read the whole diff, tests included.

  • The root cause is right. The closed-PR branch of _prunable bound its verdict through _fetch_pr_head_oid, which only ever returned a MERGED head, so a clean closed worktree always read closed_unverified and the manual closed-PR prune could never be reached.
  • The fix is narrow. state is keyword-only with MERGED as the default, so every removal caller is unchanged. Anything outside {"MERGED", "CLOSED"} is refused before gh runs, so OPEN can never be passed.
  • Every guard is kept: the same fresh gh pr list --state all query with state and OID from one row, the saturation sentinel, the refusal when any OPEN PR sits on the head, and the head-containment check after it, so a branch reused after its PR closed stays closed_new_commits. The tests pin the OPEN-next-to-CLOSED refusal, the saturated batch, a MERGED-only head under the CLOSED gate, and OPEN as state.
  • The reaper still auto-prunes only merged. A closed candidate is offered only to the manual path, with its unmerged-commits warning.
  • No regex.

@bolichen97
bolichen97 merged commit 78531fd into main Nov 12, 2026
154 of 158 checks passed
@bolichen97
bolichen97 deleted the fix/closed-pr-prune-head-30399 branch November 12, 2026 23:05
KiroCrewCaptain added a commit to KiroCrewCaptain/kirocrew-app-dev-fleet that referenced this pull request Nov 14, 2026
Kiro Crew's built-in Dev Fleet moved on after this tree was cut from
main at 6d07df11. An adopted host runs this tree, so each of those
changes is carried here first, up to main at 46f62dea:

- pod_down waits for the pod's whole stop, and the tool waits 120s
  (kirodotdev/KiroCrew#33272)
- pod_up names a worktree hidden in a session's scratch, and its
  description says where to create one (kirodotdev/KiroCrew#33254)
- the how-to sentence is one message per language
  (kirodotdev/KiroCrew#33327), rendered by i18nNodes, the bundle's
  stand-in for the dashboard's <Trans components>
- Create worktree from latest stable, with Refresh, in a labelled More
  actions menu (kirodotdev/KiroCrew#33391), on the page's own MenuBtn
- pod_up's test-double env override (kirodotdev/KiroCrew#35199)
- provision checks the build input (kirodotdev/KiroCrew#22686), a clean
  closed worktree can prune (kirodotdev/KiroCrew#31124), the secret
  comes from the spawn environment (kirodotdev/KiroCrew#34762), every
  bad-body 400 carries a code (kirodotdev/KiroCrew#32503), and the
  demo skill's Windows tool lookup (kirodotdev/KiroCrew#33556, #33779)

app.json now carries the wheel copy's agentTools block byte for byte,
which an adopted record needs for its tools to stay listed
(kirodotdev/KiroCrew#33141). The suite runs against Kiro Crew main at
46f62dea, the fixtures match that wheel copy, and PORTING.md records
each port.

Co-authored-by: Bolin Chen <bolichen@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

readiness: passed Eligible automated validation passed for the current revision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A clean closed-PR worktree is never offered for pruning ("verification unavailable — retry")

3 participants