Skip to content

fix(code-review): retire the review pool when the app is disabled - #6802

Open
leonlaiyc wants to merge 2 commits into
kirodotdev:mainfrom
leonlaiyc:fix/sage-disable-retires-pool
Open

leonlaiyc wants to merge 2 commits into
kirodotdev:mainfrom
leonlaiyc:fix/sage-disable-retires-pool

Conversation

@leonlaiyc

@leonlaiyc leonlaiyc commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

review_pool.shutdown_pool says what it is for:

async def shutdown_pool() -> None:
    """Tear down the singleton pool (called on app disable / gateway shutdown)."""

It is not called on app disable. grep shutdown_pool across src/ finds exactly one
production caller — _shutdown_pool, registered on app.on_cleanup in
register_routes. aiohttp fires on_cleanup when the gateway stops and at no other
time, so the first half of that docstring describes something nothing does.

What a disable actually does is refuse the routes. The pool's worker sessions stay alive,
holding an agent runtime up and continuing to run review turns — for a permission the
operator has already withdrawn, while every route answers 403 and reports nothing.

This is the same shape as the auto-improvement app's disable gap, and it is the second
half of a closed pair: register_routes there stands its workers down on shutdown and now
on disable too. Here only shutdown was wired.

Why it matters

The seam's own docstring states the harm, as a security property rather than a tidiness
one:

This exists because a periodic sweep is not an off-switch. An app whose workers hold
anything time-bounded — an auto-approval grant, a lease, a lock — can act once more in
the gap between the operator's click and the next poll, which is a whole turn's worth of
authority handed out after permission was withdrawn.

A review worker is exactly that: it holds a live agent runtime and spends budget on turns
that read the operator's code and can post to their pull requests. teardown.py also runs
notify_app_disabled first, ahead of the app's own onDisable script and backend
teardown, "so a worker holding something time-bounded would [not] keep that authority for
the duration" — an app that registers nothing there silently opts out of that ordering
guarantee.

And because the docstring already claims the disable path is covered, a reader auditing
this app's teardown finds a function that says it handles disable, one caller, and no
reason to look further.

What changed (motivation → approach → change)

Symptom: disabling the app leaves the review pool running. Root cause: shutdown_pool is
wired to one of the two ways the app can be switched off. Fix: wire it to the other one
too, through the mechanism that exists for it, with the same body.

register_routes now also registers an app-disable hook:

async def _retire_pool_on_disable(_app: str) -> None:
    try:
        await review_pool.shutdown_pool()
    except Exception:
        logger.warning("failed to retire review pool on disable", exc_info=True)

Same call the on_cleanup hook makes. shutdown_pool is idempotent (a no-op when no pool
has started) and drops the singleton, so a later re-enable builds a fresh one. Nothing new
is introduced: no state, no config key, no constant, no route, no change to the pool.

Registration is feature-detected with getattr(teardown, "register_app_disable_hook", None), copying Issue Radar's pattern verbatim and for its stated reason — a core build
whose teardown module predates the registry keeps loading, and there the cleanup hook is
the only teardown there is, which is the pre-registry behaviour rather than a regression.
It sits in its own try, matching the existing hook registration, so it can never break
gateway startup.

The shutdown_pool docstring is left alone deliberately: this change makes its existing
claim true, which is the better of the two ways to resolve the mismatch.

The remaining siblings, counted and declared. Filtering the builtin apps to those that
register a worker teardown on on_cleanup gives six; two already register a disable hook
(issue_radar, and auto_improvement as of its own disable fix). The four that do not are
code_review_sage (this PR), dev_fleet (dev_fleet_cleanup), md_notebook
(syncer.stop_syncer) and meetings (_on_cleanup). The other three are not folded
in here because they are not mechanical: dev_fleet's teardown drains a prune worker
mid-flight through _run_uninterruptible because its git mutations must not be torn in
half, so what a disable should do to an in-progress destructive mutation is a decision,
not a copy of this hook. Each of the three needs its own answer to "what does this worker
hold, and what does stopping it mean", which is a judgment per app rather than one sweep.

Second half, after review: retiring the pool was necessary but not sufficient

GPT's exact-head review blocked this, correctly. shutdown_pool() drops the
singleton and get_pool() recreates it on the very next call:

def get_pool() -> ReviewPool:
    global _POOL
    if _POOL is None or _POOL._closed:
        _POOL = ReviewPool()
    return _POOL

So a run already accepted and only waiting its turn on _RUN_LOCK behind
another run woke up after the disable and simply built a new pool — a fresh agent
runtime, and a posted review, for a permission the operator had withdrawn while
it waited. Reproduced before changing anything; the trace on the previous head
is literally ['shutdown_pool', 'get_pool'].

I did not take the suggested revert of the disable hook. The original gap was
real too — leaving the pool alive after disable is its own defect — so both
invariants have to hold at once:

  • A. disabling retires existing review runtime authority;
  • B. a review accepted before the disable cannot recreate a pool and resume
    after it.

Two halves, using mechanisms that already exist:

  • the disable hook now withdraws authority from every live run before it
    retires the pool, marking them in _CANCELLED — the app's existing
    cancellation set. The marks are made synchronously, with no await between
    the loop and the marks landing, so a run waiting on _RUN_LOCK cannot slip
    through in between. The predicate is _is_live, already in this file, which
    also covers the posting phase — a run mid-delivery reports a terminal status
    while still writing to the pull request, so status == "running" alone would
    miss it;
  • _run_review_bg re-checks _CANCELLED immediately after acquiring
    _RUN_LOCK, before it claims any change and before get_pool().

Why _CANCELLED and not is_app_enabled. The obvious-looking guard is to
re-read the authoritative enabled flag after the wait. It is stale exactly when it
matters. notify_app_disabled fires before disable_app writes the enabled
flag — apps/teardown.py says so in terms, and says a hook "must not wait on that
flag to decide it has been switched off" — and before the app's own third-party
onDisable script, which that same comment notes can take real time. So for the
whole window this bug lives in, is_app_enabled still returns True, while
_CANCELLED is already correct at that instant.

No enable-side hook is added, and none is needed. apps.teardown has no
enable seam, and inventing one would be a new lifecycle framework for a
one-invariant fix. Because the withdrawal is recorded per run, a genuinely new
review submitted after re-enable carries a run id that was never cancelled and
builds its own pool. A test pins exactly that, with nothing un-set by hand.

Closing admission for the whole transition (added after the second block)

The exact-head GPT review found the remaining half, and it is correct:
_CANCELLED is a point-in-time snapshot. The hook marks the runs that are live
when it fires, so it can only speak for runs that already exist. A review posted
after that scan is registered with an uncancelled id, walks past the _RUN_LOCK
recheck and calls get_pool() — authority rebuilt after the operator withdrew it.

Reproduced before designing anything (see Tests): with the disable already under
way and the hook's snapshot already taken, a new POST is admitted 200, its id is
absent from the snapshot, and driving that run through the real _run_review_bg
reaches get_pool().

Two things were open, not one, and neither covers the other:

  • _handle_review had no enabled gate at all. _require_enabled wraps the two
    chat handlers; the route that starts a review was never wrapped. A fully
    disabled app accepted reviews.
  • The transition window, where the flag still reads enabled — the window the
    section above already explains is_app_enabled cannot speak for.

Both are now checked at the app's ONE admission chokepoint. Both review entry
points (_handle_review, _handle_review_repo) reach the registry through
_record, so the boundary is that call and not each route:

async def _admit(run: dict) -> bool:
    async with app_lifecycle_lock(_APP_NAME):
        if not await asyncio.to_thread(is_app_enabled, _APP_NAME):
            return False
        await _record(run)
    return True

The enabled read and the insert are ONE critical section, taken under a lock
that already exists.
handle_disable_app holds app_lifecycle_lock(name)
across both teardown_app_runtime (which fires the disable hook) and
disable_app (which writes the flag). Holding the same lock across the read and
the _RUNS.insert makes the admission decision and the disable transition
mutually exclusive, in either order:

  • a disable that gets there first has finished and persisted enabled=False
    before this read happens, so the request is refused;
  • a review that gets there first is in _RUNS before the hook's scan runs, so it
    is marked cancelled like any other live run.

Correction to this PR's previous head. _admit used to read the flag and
then consult a separate point-in-time sample of the same lock
(_lifecycle_is_stable, evaluated under _LOCK). GPT's exact-head review blocked
that, correctly: those are two checks with a suspension between them. The read is
off-loop, so a disable can acquire the lock, run the teardown, persist
enabled=False and release it entirely inside that one to_thread
round-trip
— after which the sample finds the lock free and admits a run whose
authority has already been withdrawn. The previous body described that as a
residual "strictly narrower than the window this closes". It was not narrower; it
was the window. It is now closed rather than documented, because the interleaving
has to be removed, not detected. _lifecycle_is_stable and the guard= parameter
are deleted, and _record is byte-for-byte its state on main.

No module-global flag, and no new lifecycle state. A DISABLED = True would
need an enable-side seam to reset it, and there is none — an interrupted disable
would leave the app permanently refusing reviews with nothing to clear it. Here
the disable request owns the lock, so there is no shadow state, nothing latched,
and no way to brick a re-enabled app. app_lifecycle_lock is also held during
install/update/enable of this app; a review submitted then waits for that
transition and is decided on the flag the transition left behind, which is the
conservative answer and is deliberate.

Lock order, inspected rather than assumed. The order taken here is
app_lifecycle_lock → _LOCK, matching the outermost-lock convention already
stated in apps/routes.py. Nothing takes them the other way round: _LOCK is
local to this module, no path holds it while acquiring a lifecycle lock, and the
disable hook — which runs while handle_disable_app holds the lifecycle lock —
scans _RUNS without _LOCK and awaits only review_pool.shutdown_pool, which
never reaches this module. Holding the lifecycle lock across admission also makes
that scan and the insert mutually exclusive, which is what the deleted
under-_LOCK guard existed to approximate.

The blocking read stays off the event loop. It is still
asyncio.to_thread(is_app_enabled, ...), now inside the lock — the same
enablement-then-mutate shape dashboard/handlers/notifications_push.py already
uses, where the lock is held across a to_thread manifest read and the channel
registration it guards.

Not adjusted, and recorded instead: reviewers also noted that a posting-phase
run can be added to _CANCELLED without ever consuming the marker. Nothing in this
change depends on that, and no executable evidence made it necessary for the
admission invariant, so it stays a separate residual rather than growing this PR
into a cleanup.

Tests

New src/kiro_crew/apps/builtins/code_review_sage/tests/test_disable_retires_pool.py,
4 tests, each driving the real register_routes on a bare aiohttp application.

  • Disabling retires the pool — fires teardown.notify_app_disabled("code-review-sage"),
    which is what the disable request calls and where it calls it, and asserts
    shutdown_pool ran.
  • A failing retire never escapes — shutdown_pool is made to raise and the hook is
    invoked directly, not through notify_app_disabled. That matters: the notifier
    swallows hook exceptions itself, so calling through it would pass whether or not the
    hook contained its own failure. The disable must not depend on the caller's leniency.
  • Retiring with no pool started is a no-op, fired twice — an operator may disable an
    app that has never run a review, or disable it twice. This is the over-fix guard, and
    it is the one test that also passes on main.
  • register_routes wires a disable hook — a structural guard, so an edit that drops
    the registration fails here instead of silently letting a disabled app keep reviewing.

An autouse fixture unregisters the hook before and after each test: the registry is
process memory shared by every test in the worker, so a hook left behind would leak into
unrelated tests.

Red-before, with routes.py restored to current origin/main (72e429791) and
nothing else changed:

FAILED test_disabling_the_app_retires_the_review_pool
  AssertionError: disabling the app left the review pool running
FAILED test_a_failing_retire_never_escapes
  KeyError: 'code-review-sage'
FAILED test_register_routes_wires_an_app_disable_hook
  AssertionError: assert None is not None
3 failed, 1 passed

Green on the branch: 4 passed. Whole app suite: 754 passed, 32 skipped, with
test_adapters.py::TestEnterpriseHosts::test_malformed_links_are_rejected_not_crashed_on
deselected — that one fails identically on pristine origin/main with this branch's files
removed (UnsupportedPlatform not raised), so it is inherited on this host and not
attributable to this diff.

Gates: check_black_formatting.py pass, flake8 src/kiro_crew/apps/builtins/code_review_sage/
clean, isort --check-only src/kiro_crew test clean, mypy src/kiro_crew — "Success: no
issues found in 1170 source files".

routes.py is in .github/black-baseline.txt and stays there: the diff is 30 added lines
and 0 deletions, with no reformatting riding along.

The queued-review race (added after the block)

Four more tests in the same file, driving the exact interleaving on the real
_RUN_LOCK with asyncio.Events rather than sleeps, so the ordering is
deterministic: B is only released once the disable has actually completed.

Test Locks in
..._a_review_queued_before_disable_cannot_rebuild_the_pool_after_it the blocker. A holds the lock, B is accepted and blocked on it, the operator disables while B waits, A releases. Asserts the pool was retired, get_pool was never called, no review work ran, and B ends cancelled
..._a_queued_review_still_runs_when_nothing_is_disabled the over-fix guard: same interleaving, no disable → get_pool and run_review both happen. Stops the check being a blanket refusal
..._a_new_review_after_re_enable_gets_a_fresh_pool disable-then-new-review in one test: the second run builds its own pool with nothing reset by hand
..._the_disable_hook_withdraws_authority_from_every_live_run a posting run (terminal status, posting=True) is marked; a finished run is not

The assertions are on what B actually did — pool creation and review work —
not on whether shutdown_pool() was called. The blocker is specifically about
what happens after shutdown, so a test that only checked the shutdown would pass
on the broken code.

Red-before, against this PR's own previous head 66906d4e2 (the disable hook
present, the race fix reverted): 3 failed, 5 passed. The failure message is
the mechanism verbatim — "a run queued before the disable rebuilt the pool after
it"
. The 5 that pass include the no-disable control and the four original disable
tests, which must pass on both sides.

Green after: the file's 8 passed. Directly relevant suites —
test_disable_retires_pool, test_review_pool, test_run_endpoints,
test_backend_routes → 269 passed, 16 skipped. flake8, isort and mypy
clean on the changed files. No repository-wide sweep.

The admission race (added after the second block)

Four more tests in the same file. The disable transition is reproduced the way
production runs it — app_lifecycle_lock held, the app's real hook fired
through teardown.notify_app_disabled, and the persisted enabled flag
deliberately still True, because that is its real value in this window. Ordering
is by lock and by await point; there are no sleeps, and the background work an
admitted review spawns is drained by gathering the module's own task set rather
than by yielding and hoping.

Test Locks in
..._a_new_review_cannot_be_admitted_once_disable_has_begun the blocker. After the hook's snapshot and before the flag is written, a new POST must be refused — asserted on the boundary (HTTP 403, nothing registered, no work started), not on an internal flag. A test that only checked _CANCELLED would pass on a fix that admitted the run and cancelled it afterwards, which is a weaker promise
..._the_admitted_run_would_otherwise_reach_the_pool_seam why the refusal matters, driven rather than asserted: takes the run the window admits, checks its id is absent from the hook's snapshot, and runs it through the real _run_review_bg to get_pool(). Skips once admission is closed, since there is then no such run
..._a_new_review_is_admitted_when_no_disable_is_in_flight the over-fix guard: same path, same enabled flag, no transition → admitted, registered, work started
..._admission_reopens_after_the_transition_completes invariant D, executable. Nothing is un-set by hand; the second call simply runs after the async with block exits

A fifth test lands in test_backend_routes.py for the steady state: a disabled app
refuses _handle_review with 403 app_disabled and registers nothing.

Red-before, against this PR's own previous head 70f7cfa41 with the new tests
present and routes.py restored from it: 2 failed, 10 passed. The failure
message is the mechanism verbatim — "a new review was admitted after the
operator's disable had already begun (HTTP 200, …)"
— and the pool-seam test
passes there, which is the executable proof of step 7: the admitted run really
does reach get_pool().

Green after: the file's 11 passed, 1 skipped (the skip is the pool-seam
drive, by design). Directly relevant suites — test_disable_retires_pool,
test_backend_routes, test_run_endpoints, test_activity_progress,
test_record_adoption, test_post_comments, test_review_driver,
test_store_selfheal, test_followup → 412 passed, 26 skipped. All four
original queued-race tests still pass, so invariant B is not regressed.

Two test fixtures gain one line each, is_app_enabled = lambda name: True, copied
from the followup-route class's own setUp: TestHandlers.setUp here, and
_SageRoutesBase.setUp in test/test_sage_backend_routes_coverage.py. Those cases
are about what an enabled app does; the route simply has a gate now that it never
had, and the precondition is stated rather than depending on a real
installed.json. Without it, four pre-existing request-guard cases in that file
(TestReviewKickoffGuards, TestReviewRepoHandler) answered 403 on the
enablement check instead of on the guard each one is written to exercise. That base
loads a fresh routes module per test, so the line is local to it, and the app's own
disabled-path coverage is untouched — the file has no case that asserts the disabled
response.

The new test file's stub request and response body are typed the way the sibling
builtin's tests already do (cast(web.Request, ...) at construction, an isinstance
narrowing before json.loads). Its handlers are reached through a real import rather
than a dynamically loaded module, so unlike the existing _Req call sites they are
not Any and mypy checks them.

Gates, re-run on the current head against all four changed files:
scripts/check_black_formatting.py passed in scope (4 files in scope, nothing
unformatted outside the baseline); flake8 and isort clean; mypy — "Success: no
issues found in 1 source file". routes.py stays in the black baseline and is not
reformatted. Touched-surface suites only — test_sage_backend_routes_coverage.py
111 passed, and test_disable_retires_pool.py + test_backend_routes.py
214 passed, 17 skipped. No repository-wide sweep, and no local reviewer agents.

Correction to an earlier claim in this section. The mypy line above previously
described a single-file local run; Backend Lint & Type Check runs mypy src/kiro_crew/, which found three errors in the new test file (two arg-type on the
stub request, one union-attr on response.body) that the narrower local invocation
did not surface. Those are the typing changes described above, and the gate is now run
the way CI runs it.

The completed-disable race (added after the third block)

One more test in the same file, plus a restructure of the four admission tests
above so that they model the enabled flag the way production writes it.

..._a_completed_disable_cannot_be_overtaken_by_an_in_flight_enabled_read drives
the exact interleaving the exact-head review named:

  1. a review request reads the enabled flag and gets True;
  2. while that off-loop read is still in flight, the disable takes
    app_lifecycle_lock, runs the teardown, persists enabled=False, releases;
  3. the review request resumes and registers its run.

The property asserted is an order, recorded as each step happens: no run may
enter _RUNS after enabled=False has been persisted. That keeps the test blind
to how the code holds the line — it fails whenever the two events interleave,
and passes whenever they are serialized, in either direction. Ordering is driven
by events and barriers only: the flag read is parked in its own worker thread on a
threading.Event and released by the disable, and the timeouts are watchdogs so a
regression fails the suite instead of hanging it.

Red-before, against this PR's own previous head 54f4389e8, with the new test
present and routes.py restored from it. The event log is the mechanism verbatim:

AssertionError: a review was registered after the operator's disable had completed
and persisted enabled=False:
['review:read_enabled=True', 'disable:enabled_written_false', 'review:run_admitted']

3 failed, 10 passed

Green after, same test, same harness — the two events are serialized:

['review:read_enabled=True', 'disable:serialized_after_admission',
 'review:run_admitted', 'disable:enabled_written_false']

The 200 branch additionally asserts the admitted run is in _CANCELLED, because
ordering alone is only half the invariant: a run admitted ahead of the disable
must still be caught by the hook's scan.

The other two reds on that head are the two admission tests whose helper now
models the flag write. _post_review_mid_disable previously pinned
is_app_enabled to a constant True and called the handler inline inside the
disable's async with — with admission taking that same lock, an inline call
could only self-deadlock, and a constant flag is not what a completed disable
leaves behind. It now serves the request on its own task, the way the gateway
does; signals "the request has reached the admission boundary" from
change_id_for, the last call before _admit, so there is no sleep and no
polling; and then writes enabled=False inside the lock, the order
handle_disable_app uses. The refusal assertions are unchanged — HTTP 403,
nothing registered, no work started — and the request now arrives during the
transition and waits for it, rather than sampling it.

Green on the current head. test_disable_retires_pool.py 12 passed, 1
skipped
(the skip is the pool-seam drive, by design). With its directly relevant
siblings test_backend_routes.py and test/test_sage_backend_routes_coverage.py:
326 passed, 17 skipped. Touched-file gates: flake8, isort and
git diff --check clean; mypy on both changed files — "Success: no issues found
in 2 source files" — and over the whole app package, "Success: no issues found in
46 source files"; scripts/check_black_formatting.py passed in scope (4 files
in scope, nothing unformatted outside the baseline). No repository-wide sweep and
no local reviewer agents; server CI owns the broad validation.

The branch is now one commit. PR Hygiene was red for "PR must contain 1 or 2
commits (has 3)"
— a required readiness item, unrelated to any reviewer finding.
The commits are squashed into one; git patch-id --stable is identical before and
after the squash, so no content changed with it.

Manual verification

N/A — unit coverage sufficient: the tests exercise the real registration and fire the
disable notification through the same function the disable request calls, so the whole
path this change adds is covered mechanically.

Related Issues

None. Found by auditing which gateway-mounted apps stand a worker down on shutdown but not
on disable, after the same gap was fixed in the auto-improvement app.

Pattern harvest

Rule candidate: review-prompt
Pattern: teardown drops a lazily-recreated singleton, and a consumer queued
before the teardown re-creates it afterwards — authority is withdrawn, then
rebuilt by work that was already in flight.

Generalizes beyond this app. The shape is any get_x() that reads
if _X is None: _X = X() paired with a shutdown_x() that sets _X = None:
retiring the singleton is not withdrawal while any caller can still reach the
getter. A grep cannot see it — the defect is the ordering between a lock wait
and a lifecycle event — which is why this is a review-prompt candidate rather
than a semgrep rule. The generalizable question a reviewer can ask is: "after
this teardown runs, what is still queued that will call the getter?"

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|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

@leonlaiyc
leonlaiyc requested review from a team and chenmingwei23 August 29, 2026 16:20
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Aug 29, 2026
@github-actions

github-actions Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review (fork) — 🔴 changes requested (blocking)

Reviewed bc339480976cecdbaaa702973c041c5c1fd38de0 via the fork AI-review pipeline; updated in place on each push.

2 of 2 blocking finding(s) are security-class and were withheld from adjudication, so the blocking verdict stands.

BLOCKING -- src/kiro_crew/apps/builtins/code_review_sage/backend/routes.py:2711 -- CLI disable bypasses pool retirement
teardown.register_app_disable_hook(_APP_NAME, _retire_pool_on_disable)
CLI disable -> disable_app() runs in another process without notifying this hook -> active reviews continue after authority withdrawal.
Anchor: residual/security
Fix: add gateway-side reconciliation or signaling that invokes retirement after CLI disable.

BLOCKING -- src/kiro_crew/apps/builtins/code_review_sage/backend/routes.py:937 -- enablement check races provider access
if not await asyncio.to_thread(is_app_enabled, _APP_NAME):
Enabled read -> concurrent disable completes -> _list_repo_prs contacts the provider using withdrawn authority.
Anchor: residual/security
Fix: serialize the enablement check and provider operation with app_lifecycle_lock(_APP_NAME).

[BLOCK-MERGE] bc33948
[GPT-REVIEWED] bc33948

Adjudication (Opus 4.8) — is blocking on each finding proportionate?

Both fenced findings describe real residual windows. My only authority here is UPHOLD-FENCED or FLAG, and neither unblocks the merge.

F1 — Confirmed the mechanism. cli_commands.py:977 calls disable_app(args.name) in a separate process, which is a pure metadata write (manager.py:1869-1871) and never fires the running gateway's in-process _retire_pool_on_disable hook (teardown.py:424-430 only notifies hooks registered in this process). The in-flight recheck is deliberately _CANCELLED-only, not is_app_enabled (patch lines 100-106), so an in-flight review/post/consolidation is invisible to a CLI disable and continues on the retired-elsewhere authority. This is an everyday, documented operation (kirocrew app disable is in the CLI help) interleaved with active work — not a contradictory or impossible timing. I cannot argue it is so extreme a human would accept the residual, so no clean rarity record exists. UPHOLD-FENCED.

F2 — Confirmed. In _handle_review_repo the enabled read is off-loop and advisory (patch line 139), and _list_repo_prs (provider contact) runs before _admit, which gates only run registration. A disable completing in the await gap between the read and the list call leaves one provider read on withdrawn authority; _admit then refuses the run. The window is narrow but a genuine interleaving, not an impossible/legacy/contradictory one. Torn → UPHOLD-FENCED per the asymmetry rule.

[ADJUDICATION] bc339480976cecdbaaa702973c041c5c1fd38de0 total=0 uphold=0 downgrade=0
[GPT-ADJUDICATED] bc339480976cecdbaaa702973c041c5c1fd38de0
[ADJUDICATION-FENCED] bc339480976cecdbaaa702973c041c5c1fd38de0 fenced=2 flagged=0
UPHOLD-FENCED F1 src/kiro_crew/apps/builtins/code_review_sage/backend/routes.py:2711 -- CLI disable is a cross-process metadata write that never fires this process's in-process retirement hook, and it can interleave with in-flight work that is deliberately blind to the enabled flag — an ordinary, plausible interleaving, not an extreme one.
UPHOLD-FENCED F2 src/kiro_crew/apps/builtins/code_review_sage/backend/routes.py:937 -- the advisory enabled read precedes the provider list call while `_admit` gates only registration, leaving a genuine (if narrow) window where a completing disable still permits one provider contact; the race is real, not impossible.
[GPT-ADJUDICATED-FENCED] bc339480976cecdbaaa702973c041c5c1fd38de0

@github-actions

github-actions Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5, fork) — 🟡 CONCERNS

Design-level review of bc339480976cecdbaaa702973c041c5c1fd38de0 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: CONCERNS

/post's immediate run_still_running 409 silently became a minutes-long queue behind a whole review, then execution — an undocumented API behavior change.

Watch

Making _RUN_LOCK the outermost lock in _handle_run_post moves the run-state checks behind a lock that _run_review_bg holds for an entire review ("Serialize the WHOLE run"). A /post sent while any review runs — including on the running run itself, which previously got an instant 409 — now hangs the HTTP request for the review's duration, and for the same run then proceeds to post once its status flips to done: "refuse" became "queue-then-execute", with client-timeout retries hitting a request the server is still holding. The description presents the restructure as refusal-preserving ("the refusal assertions are unchanged") and never states this. Either it is intended queueing semantics and should be declared, or the cheap 409/404 checks should answer before the _RUN_LOCK wait (rechecked after acquisition).
Clears when: the mid-review /post answer (immediate 409 vs. queue) is stated as intended in the PR, or the run-state checks are answered ahead of the _RUN_LOCK wait.

Suggestions

  • The "no get_pool() after disable" invariant is now enforced by convention at four call sites (review, post, two consolidate seams); a module-local wrapper that consults the cancellation mark before delegating to review_pool.get_pool() would make the next pool consumer safe by construction instead of by memory.

[DESIGN-REVIEWED] bc33948

@github-actions

github-actions Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5, fork) — 🟡 CONCERNS

Premise-level review of bc339480976cecdbaaa702973c041c5c1fd38de0 via the fork AI-review pipeline — 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/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

The top-of-handler enabled check in _handle_review_repo re-spells _require_enabled, which already exists in this file and returns the identical 403.

Not justified as shipped

  • Item 4 — duplicate of src/kiro_crew/apps/builtins/code_review_sage/backend/routes.py:2129: the diff's "Deny before touching the provider" hunk is a plain advisory asyncio.to_thread(is_app_enabled, ...) read returning the same app_disabled 403 body as the existing _require_enabled decorator (2 uses on base); unlike the lock-held reads in _admit, /post and /consolidate, nothing here needs the lifecycle lock, so it is a second spelling that will drift from the decorator.
    Subtraction: delete the inline is_app_enabled check in _handle_review_repo and decorate the handler with the existing _require_enabled.
    Clears when: the deletion is made, or the author names a difference the decorator cannot provide (the 400-before-403 ordering for a missing repo is not one — the harm named is provider contact, which both close).

What this change ships

Inventory (10 items) — 9 justified

Intent: when the operator disables the code-review app, all of its review authority — pool workers, queued runs, posting, merges — actually stops. This is a FIX.

  1. Disabling the app retires the review pool at the click, not only at gateway shutdown — justified
  2. Runs live at disable are cancelled; a queued run wakes cancelled instead of rebuilding the pool — justified
  3. Starting a review while disabled is now refused with 403 (was accepted, unchecked) — justified
  4. A repo review while disabled is refused before the provider is contacted — duplicate of routes.py:2129 (_require_enabled)
  5. Posting a finished review while disabled is refused; a disable mid-post stops before writing and records why — justified
  6. A learnings merge while disabled is refused; an in-flight merge stops before the pool and before the apply, keeping staged candidates — justified
  7. /post now waits on the run lock without holding the run-registry lock, so a disable never queues behind a whole review — justified
  8. New in-memory cancelled-namespace marker for merges, consumed per cycle — justified
  9. Existing handler tests now stub the app as enabled, since every entry point is gated — justified
  10. Re-enable needs no reset: cancellation is per run/cycle, no enable-side hook — justified

[FIRST-PRINCIPLES-REVIEWED] bc33948

@github-actions

github-actions Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Opus 4.8 Review (fork) — ✅ no blocking findings

Reviewed bc339480976cecdbaaa702973c041c5c1fd38de0 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] bc33948

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Aug 29, 2026
@bolichen97
bolichen97 enabled auto-merge August 29, 2026 23:47
auto-merge was automatically disabled August 31, 2026 09:27

Head branch was pushed to by a user without write access

@leonlaiyc

Copy link
Copy Markdown
Contributor Author

Accepted, reproduced, and fixed on e9fa9571 — but not by the suggested revert.

I reproduced your race before changing anything, driving the exact interleaving on the real _RUN_LOCK: A holds the lock, B is already accepted and blocked on it, the operator disables while B waits, A releases. On the previous head the trace is literally ['shutdown_pool', 'get_pool'] — the pool was retired and then rebuilt by the queued run, which then ran review work. Your mechanism is correct in every step.

Why not the revert. The original gap was real too: shutdown_pool's own docstring says it is called "on app disable / gateway shutdown", and only the second half was true, so a disable left worker sessions alive holding an agent runtime for a withdrawn permission. Reverting trades your defect for that one. Both invariants have to hold:

  • A. disabling retires existing review runtime authority;
  • B. a review accepted before the disable cannot recreate a pool and resume after it.

The fix, in two halves, using seams that already exist:

  • the disable hook now withdraws authority from every live run before retiring the pool, marking them in _CANCELLED — this app's existing cancellation set. The marks are made synchronously: there is no await between the loop and the marks landing, so a run waiting on _RUN_LOCK cannot slip through in between. The predicate is _is_live, already in the file, which also covers the posting phase — a run mid-delivery reports a terminal status while still writing to the PR, so status == "running" alone would miss it;
  • _run_review_bg re-checks _CANCELLED immediately after acquiring _RUN_LOCK, before it claims any change and before get_pool().

On the guard I deliberately did not use. The obvious version is to re-read the authoritative enabled flag after the wait. It is stale exactly when it matters: notify_app_disabled fires before disable_app writes the enabled flag — apps/teardown.py says so in terms, and warns that a hook "must not wait on that flag to decide it has been switched off" — and before the app's own third-party onDisable script, which the same comment notes can take real time. For the entire window this bug lives in, is_app_enabled still returns True. _CANCELLED is already correct at that instant.

No enable-side hook was added. apps.teardown has no enable seam and I did not invent one for a single invariant. Because the withdrawal is per run, a genuinely new review after re-enable carries a run id that was never cancelled and builds its own pool — pinned by a test that resets nothing by hand.

Evidence. Against this PR's own previous head 66906d4e2, with the race fix reverted and the disable hook still present: 3 failed, 5 passed; the failure message is the mechanism verbatim. Green after: 8 passed in the file, and 269 passed / 16 skipped across test_disable_retires_pool, test_review_pool, test_run_endpoints, test_backend_routes. The assertions are on what the queued run actually did — pool creation and review work — not on whether shutdown_pool() was called, since the blocker is precisely about what happens after that.

The PR description is re-synced to what now ships.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Aug 31, 2026
@leonlaiyc
leonlaiyc force-pushed the fix/sage-disable-retires-pool branch from e9fa957 to e67a294 Compare August 31, 2026 12:18
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Aug 31, 2026
@leonlaiyc
leonlaiyc force-pushed the fix/sage-disable-retires-pool branch from e67a294 to 70f7cfa Compare August 31, 2026 13:09
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Aug 31, 2026
@leonlaiyc
leonlaiyc force-pushed the fix/sage-disable-retires-pool branch from 70f7cfa to 2513f0b Compare August 31, 2026 14:49
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 8, 2026
@dwu96 dwu96 added needs-pr-triage PR scanner: awaiting automated triage drive-to-green PR claimed by drive-to-green pipeline and removed needs-pr-triage PR scanner: awaiting automated triage labels Sep 15, 2026
@dwu96

dwu96 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

🤖 Kiro Crew [operator: dwu96#8c08bee0]: This PR has been inactive for 7+ days with failing CI. I've assessed the blockers and they appear resolvable — I'll push fixes directly to this branch as a co-author.

Assessment: three blockers, all with a determined remedy. (1) Backend Lint & Type Check is red on a single comment-history gate offender — test_disable_retires_pool.py:213 narrates history with "used to"; reword to present tense. (2) The GPT 5.6 blocking finding is a real gap in this PR's own feature rather than an objection to it: _handle_run_post admits on run state alone, so a terminal run's /post still rebuilds the torn-down pool via get_pool() and posts after the operator disabled the app — the lane's own fix is to serialize post admission with the lifecycle lock and recheck cancellation before get_pool(), which is the same _CANCELLED guard this PR already establishes, extended to the one admission path it missed. (3) The maintainer rebase note asks to drop the getattr(teardown, "register_app_disable_hook", None) feature detection now that main provides the symbol. PR Readiness clears with those. Design/approach stays exactly as you built it, and the ordering agreed with @bolichen97 (this lands before #8143) is unchanged.

The Windows/other lanes are green, so nothing else is outstanding.

If you'd prefer I don't touch this PR, add the pr-no-autofix label.

@chenmingwei23

Copy link
Copy Markdown
Contributor

🤖 Kiro Crew [operator: chenmingwei23#de330d0c]: This PR has been inactive for 7+ days with failing CI. I've assessed the blockers and they appear resolvable -- I'll push fixes directly to this branch as a co-author.

Assessment: The GPT blocking finding (routes.py:2569, "disable does not close posting admission") is a real reachable defect on your own code with an obvious fix -- gate the /post endpoint on the enabled flag so a review accepted before a disable cannot post after it (this also closes the _CANCELLED run-id leak Opus flagged at routes.py:2562). The Backend Lint failure is mechanical: a test comment/docstring narrates change history; it needs rewriting to present-tense current behavior per code-style. Design/First-Principles are advisory CONCERNS about the recheck placement -- I'll answer those in-thread rather than restructure. PR Readiness is red only because it aggregates the GPT verdict.

If you'd prefer I don't touch this PR, add the pr-no-autofix label.

@chenmingwei23 chenmingwei23 removed the drive-to-green PR claimed by drive-to-green pipeline label Sep 15, 2026
@chenmingwei23

Copy link
Copy Markdown
Contributor

🤖 Kiro Crew [operator: chenmingwei23#de330d0c]: Retracting my claim -- another Kiro Crew instance (operator: dwu96#8c08bee0) claimed this PR at 16:51:20Z, before mine (16:52:23Z), so it owns the drive-to-green work here. I've removed the drive-to-green label I added and will not act on this PR. Apologies for the duplicate notification.

@dwu96 dwu96 added the drive-to-green PR claimed by drive-to-green pipeline label Sep 15, 2026
`review_pool.shutdown_pool` says it is "called on app disable / gateway
shutdown". Only the second was wired: `register_routes` registered
`_shutdown_pool` on `app.on_cleanup`, which aiohttp fires when the gateway
stops and at no other time. A disable refused the routes and left the pool's
worker sessions alive, holding an agent runtime up and running review turns
for a permission the operator had already withdrawn.

Register the same teardown on `apps.teardown`'s disable seam, feature-detected
with `getattr` so a core build whose teardown module predates the registry
keeps loading. `shutdown_pool` is idempotent and drops the singleton, so a
re-enable builds a fresh one.

Retiring the pool alone is not withdrawal: `get_pool()` rebuilds the singleton
on the next call, so a run already accepted and waiting on `_RUN_LOCK` woke up
after the disable and stood a fresh runtime back up. The hook now marks every
live run in the app's existing `_CANCELLED` set BEFORE it retires the pool,
synchronously, and `_run_review_bg` re-checks that set immediately after it
acquires `_RUN_LOCK` and before `get_pool()`. `_CANCELLED` and not
`is_app_enabled`, because `notify_app_disabled` fires before `disable_app`
writes the flag.

A snapshot cannot speak for a run that does not exist yet, so admission itself
is the boundary. Both review entry points go through `_admit`, which takes the
`enabled` read and the `_RUNS` insert under ONE hold of
`app_lifecycle_lock(_APP_NAME)` — the same lock `handle_disable_app` holds
across both `teardown_app_runtime` and `disable_app`. Serializing on it makes
the admission decision and the disable transition mutually exclusive in either
order: a disable that gets there first has already persisted `enabled=False`,
so the request is refused with the `403 app_disabled` the frontend knows; a
review that gets there first is in `_RUNS` before the hook's scan and is
cancelled like any other live run. Reading the flag and then sampling a
separate signal cannot close this — the read is off-loop, so a disable can
start, complete and release inside that one `to_thread` round-trip, after
which the sample finds nothing in flight. The blocking read stays in
`to_thread` inside the lock, the shape `notifications_push` already uses for
the same enablement-then-mutate pair. Lock order is `app_lifecycle_lock` →
`_LOCK`, the outermost-lock convention in `apps/routes.py`; nothing takes them
the other way round, and the disable hook touches `_RUNS` without `_LOCK`.

Tests drive each interleaving with events and barriers, never sleeps: the
queued-run race on the real `_RUN_LOCK`, a new review arriving while the
disable holds the lifecycle lock, and a completed disable racing an in-flight
`enabled` read — the last recording the order of the two events as they happen
and asserting no run enters `_RUNS` after `enabled=False` is persisted.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@dwu96
dwu96 force-pushed the fix/sage-disable-retires-pool branch from 9da06b6 to 2162fb0 Compare September 15, 2026 19:47
@dwu96

dwu96 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Kiro Crew [operator: dwu96#8c08bee0]

Drive-to-green on this PR: rebased onto main cd95478e1 and pushed one fix commit (2162fb08f, two commits total). The branch's design is untouched — every change below extends the PR's own admission mechanism (_CANCELLED marks + one app_lifecycle_lock hold around the enabled read and the registration write) to seams it did not yet cover. Original work by Leon (leonlaiyc); the fix commit credits them in the body and carries a Kiro Crew co-author trailer.

What changed and why:

  1. Comment-history gate (CI blocker): the docstring at test_disable_retires_pool.py narrating "used to wake up" is reworded to present tense. check_comment_history.py now reports zero new offenders against origin/main.

  2. GPT server-lane blocking finding (posting after disable): /post admission now takes the enabled read and the posting write under one lifecycle-lock hold, and _post_comments_bg rechecks _CANCELLED immediately before get_pool() (an admitted post is posting, hence _is_live, hence marked by the disable hook's scan). Stale marks are consumed at admission so a re-enabled app can post. The admission acquires _RUN_LOCK first, with nothing else held: a /post waiting behind a running review must not hold the lifecycle lock, or the operator's disable queues behind that entire review. Handler lock order is _RUN_LOCK -> app_lifecycle_lock -> _LOCK; no path takes any pair the other way (verified across _admit, _handle_consolidate, _run_review_bg, _post_comments_bg, and the disable path).

  3. Same gap, consolidation seam (found by the local pre-push review lanes): _handle_consolidate reaches get_pool() with no run id for the disable hook's _RUNS scan to mark. It now admits under the same lifecycle hold, the hook marks claimed namespaces in a new _CANCELLED_NS set, and _consolidate_bg honours the mark before get_pool(). With this, all three get_pool() call sites sit behind a disable-aware admission.

  4. Same gap, the review worker's own recheck: the off-loop _claim_changes_under_lock read is a suspension a disable can land inside, and the PR's recheck sat before it. The recheck moves to immediately after the claim returns — no await remains between it and the pool build; the existing finally releases claims on the cancel path.

  5. /review-repo refuses a disabled app before _list_repo_prs: the PR listing is provider contact on the app's authority, and an already-reviewed repository answered 200 before any run existed for _admit to refuse. The new check is deny-by-default and advisory; _admit stays the atomic gate. One residual interleaving is deliberately accepted and on the record here: an enabled read that races a concurrent disable can let one read-only PR listing through. Closing it would require holding the lifecycle lock across an unbounded provider network call, which reintroduces the disable-hostage liveness defect item 2 exists to prevent; the atomic boundary for anything that rebuilds the pool or writes to a PR remains _admit. This ruling covers every finding demanding lifecycle-lock coverage of provider I/O on this admission class, wherever it moves.

  6. Maintainer request from @bolichen97: the getattr feature detection around register_app_disable_hook is dropped — main provides the registry unconditionally — and the registration is keyed on _APP_NAME, the one-spelling constant this PR introduces.

Verification: every guard is red-first (each new test observed failing before its fix), 9 mutants killed each by exactly its own test, plus a lock-order red-first (the previous /post shape times out a disable behind a held _RUN_LOCK; the new shape completes it). 12 new tests in test_disable_retires_pool.py; the pre-existing /post and consolidation endpoint tests state the enabled precondition in setUp, mirroring this PR's own handler-test precedent. Local gates green: isort, flake8, mypy (1539 files), black gate, comment-history gate, 954 sage-app tests. Full backend suite on this head vs an origin/main worktree, identical invocation: failing-id sets byte-identical except one flapping member of the mise/node environmental class present in both trees (this host cannot run mise node; the class fails on main too).

Two local model-pinned review lanes (gpt-5.6-sol, claude-opus-4.8) ran five rounds pre-push; every strictly-new blocking finding above was adopted, and the final GPT demand is dispositioned in item 5.

@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Sep 15, 2026
Extends the disable admission boundary this PR establishes (by Leon,
leonlaiyc) so all three `get_pool()` call sites sit behind a
disable-aware check and every review entry point refuses before
exercising the app's authority, clearing the remaining review blockers:

- /post admission: the enabled read and the `posting` write now happen
  under one hold of the app lifecycle lock, the same serialization
  `_admit` uses for a new review. A finished, reviewed-but-unposted run
  is not `_is_live`, so the disable hook's cancellation scan never marks
  it; without this gate a disabled app's /post rebuilt the retired pool
  and posted to the pull request. `_post_comments_bg` rechecks
  `_CANCELLED` before `get_pool()`, and stale marks are consumed at
  admission so a later re-enable can post. The admission takes
  `_RUN_LOCK` first, with nothing else held, so a /post waiting behind
  a running review never holds the lifecycle lock while it waits — the
  operator's disable is never queued behind that review.
- Consolidation admission gets the same boundary: the enabled read and
  the `_CONSOLIDATING` claim are taken under one lifecycle-lock hold,
  the disable hook marks claimed namespaces in a new `_CANCELLED_NS`
  set (a merge has no run id for the `_RUNS` scan to mark), and
  `_consolidate_bg` honours the mark twice — before `get_pool()`, and
  again on the near side of `consolidate_apply`, since the worker turn
  between them is off-loop time a disable can land inside and the apply
  is the persisted-state mutation (it rewrites the ruleset and clears
  the staged candidates). A refused apply leaves both untouched.
- The review worker's own `_CANCELLED` recheck moves to after the
  off-loop `_claim_changes_under_lock` read: that read is a suspension
  a disable can land inside, and the check before it was blind to the
  mark. After the move there is no await between the check and the
  pool build; the function's `finally` releases the claims either way.
- /review-repo refuses a disabled app BEFORE `_list_repo_prs`: the PR
  listing is provider contact on the app's authority, and an
  already-reviewed repository answered 200 before any run existed for
  `_admit` to refuse. `_admit` stays the atomic gate for the run.
- The `getattr` feature detection around `register_app_disable_hook` is
  dropped: the registry is part of the teardown module's unconditional
  surface, so the hook is registered directly, keyed on `_APP_NAME`.
- One test docstring is reworded to state current behaviour in the
  present tense.

Twelve new tests drive the disabled refusals, the enabled controls, the
admitted-then-disable interleavings (including a disable landing inside
the claim read, and a disable that must not queue behind a held
`_RUN_LOCK`), stale-mark consumption, and the provider-contact
boundary; each fails when its guard is removed (9 mutants killed, plus
the lock-order red-first). The pre-existing /post and consolidation
endpoint tests state the enabled precondition in their setUp, mirroring
the handler class in test_backend_routes.

Co-authored-by: Kiro Crew <noreply@kirodotdev.github.io>
@dwu96
dwu96 force-pushed the fix/sage-disable-retires-pool branch from 2162fb0 to bc33948 Compare September 15, 2026 20:49
@dwu96

dwu96 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Kiro Crew [operator: dwu96#8c08bee0] — disposition: fixed, span=c12c120ba20a
self-added: yes
mechanism: pre-apply cancellation recheck in _consolidate_bg

Fixed in bc33948. _consolidate_bg now re-reads
_CANCELLED_NS on the near side of consolidate_apply — after the worker
dispatch returns and with no await between the check and the apply call — and a
mark found there refuses the apply, records the reason in _CONSOLIDATE_STATE,
and consumes the mark; the ruleset and the staged candidates are untouched for a
later re-enable. Red-first: a held-open-dispatch test drives the exact
interleaving (disable completes while the worker turn is in flight) and failed
before the fix; the recheck-drop mutant is killed by that test, and an
enabled-path control pins that the merge still applies exactly once. This ruling
covers the apply seam for every disable-marks-namespace interleaving after
dispatch; the review/post seams already recheck at their own last stops.

@dwu96

dwu96 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Kiro Crew [operator: dwu96#8c08bee0]

Round 2, head bc3394809 (amended in place — the PR stays at 2 commits). One change, answering the GPT lane's upheld fenced finding on 2162fb08f: _consolidate_bg re-reads _CANCELLED_NS immediately before consolidate_apply. The worker dispatch between the pool-side recheck and the apply is off-loop time a disable can land inside, and the apply is the persisted mutation — it rewrites learned-patterns.md and clears the staged candidates. A mark found at the new recheck refuses the apply, records why in the namespace state, and consumes the mark; ruleset and candidates stay untouched for a later re-enable.

Verified red-first with a held-open-dispatch interleaving test (fails before the fix), the recheck-drop mutant killed, and an enabled control pinning that a clean merge still applies exactly once. Full local gates re-run green: isort, flake8, mypy, black gate, comment-history gate, 956 sage-app tests. The Opus server lane was clean on the prior head; its lock-order verification is unaffected (the new code adds no lock).

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 15, 2026
@dwu96

dwu96 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Kiro Crew [operator: dwu96#8c08bee0] — disposition: rebutted (not a defect of this PR), span=c12c120ba20a

  • CLI disable bypasses pool retirement
    self-added: no

The gap is in the platform's CLI path on the BASE, not in this diff:
kirocrew app disable calls disable_app() from the CLI process — a pure
installed.json metadata write (manager.py) — and the comment above that
call site in cli_commands.py already documents on main that in-process
teardown cannot be fired "from out here". No app-level code in a different
process can observe that write; the remedy is a platform mechanism (gateway
reconciliation of enabled-flag transitions, or CLI-to-gateway signaling) and
it affects every register_app_disable_hook consumer identically. This PR
adds the FIRST disable-path retirement (the dashboard seam, where the hook
registry lives) and strictly narrows exposure relative to base, where NO
disable path retired the pool. Tracked as a platform work item in #11117
(labeled deferred-finding, assigned, Due 2026-09-29). Findings demanding an
app-level fix for the cross-process CLI disable write are covered by this
ruling, wherever they land in this file.

Maintainer override, if preferred over waiting on the platform issue:
/ai-review override gpt bc339480976cecdbaaa702973c041c5c1fd38de0: the CLI-disable hook gap is a pre-existing cross-process platform seam tracked in #11117; this PR only narrows exposure and cannot fix another process's metadata write at app level.

@dwu96

dwu96 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Kiro Crew [operator: dwu96#8c08bee0] — disposition: rebutted (not a defect), span=c12c120ba20a

  • enablement check races provider access
    self-added: yes
    mechanism: deny-by-default enabled check ahead of _list_repo_prs

The demanded fix — holding app_lifecycle_lock(_APP_NAME) across
_list_repo_prs, an unbounded provider NETWORK call — recreates the exact
defect this PR's /post restructure exists to prevent: handle_disable_app
serializes on that same lock, so a hung or slow provider call would queue the
operator's disable behind network I/O (the disable-hostage shape blocked on
the previous head and fixed by taking _RUN_LOCK first with a BRIEF
lifecycle hold). The atomic boundary is placed where authority is actually
exercised durably: _admit gates run registration under the lifecycle lock,
so nothing that rebuilds the pool, runs a review turn, or writes to a PR can
pass a completed disable in either interleaving. The residual is one
read-only PR listing when a disable completes inside the request's await gap
— indistinguishable in effect from the same request arriving a moment before
the disable, which no in-process lock can prevent. Serializing reads against
operator liveness trades a bounded read-only residual for an unbounded
disable delay; this ruling covers every finding demanding lifecycle-lock
coverage of provider network I/O on admission paths, wherever it moves.

Maintainer override, if this ruling is accepted:
/ai-review override gpt bc339480976cecdbaaa702973c041c5c1fd38de0: holding the lifecycle lock across provider network I/O would let a hung GitHub call block the operator's disable; the residual one read-only listing is accepted, _admit remains the atomic authority gate.

@dwu96

dwu96 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Kiro Crew [operator: dwu96#8c08bee0] — escalation: maintainer decision needed

This drive is stopping at head bc3394809 with everything green except the GPT review lane and the PR Readiness status it derives. The lane blocks on two security-class findings, and this lane withholds security findings from adjudication by design — a writer disposition cannot clear them, and a ledger-aware rerun reproduced the identical verdict. The sanctioned exit is a maintainer ruling.

The two findings and the rulings on record:

  1. CLI disable bypasses pool retirement. Real, and pre-existing platform scope: kirocrew app disable is a cross-process metadata write that never fires any gateway's in-process disable hooks — it affects every register_app_disable_hook consumer and cannot be fixed at app level from inside this PR, whose dashboard-seam retirement strictly narrows exposure relative to base (where NO disable path retired the pool). Tracked as platform work in CLI app disable never fires in-process app-disable hooks on a running gateway #11117 (deferred-finding, assigned, due 2026-09-29). Disposition with a paste-ready override line: comment 5688462790.

  2. Enablement check races provider access on /review-repo. The demanded fix (hold app_lifecycle_lock across _list_repo_prs, an unbounded provider network call) recreates the disable-hostage liveness defect this PR's /post restructure exists to prevent; the accepted residual is one read-only PR listing, and _admit remains the atomic gate for anything durable. Disposition with a paste-ready override line: comment 5688462963.

Everything else: both fix rounds' findings adopted and verified red-first (10 new tests, 10 mutants killed), server Opus lane clean on this head, all CI lanes green or still draining with zero failures, zero unresolved threads, full-suite zero-regression proven against origin/main. @bolichen97 — if the two rulings read right to you, the override lines in the linked comments unblock the lane; if either should be fixed in-code instead, say the word and the drive resumes.

Copy link
Copy Markdown
Contributor

Open-PR triage (2026-10-05): OVERLAPPING. Keep this PR and get a maintainer ruling.

I checked head bc339480 against origin/main af1eb612. The branch is 2688 commits behind, but git merge-tree against main merges cleanly with no conflicts. No rebase is needed for mergeability.

What changed since the last audit notes

  • feat(sage): safely parallelize run-scoped reviews #8143 (PR) was closed without merging. The earlier warning that "whoever lands second must re-anchor the _CANCELLED recheck" therefore no longer applies. In the merged tree, _RUN_LOCK still wraps the whole run in _run_review_bg.
  • fix(code-review-sage): require the dashboard owner for review writes #15988 (PR, merged) added require_owner_dashboard_request to _handle_review, _handle_review_repo, _handle_run_post and _handle_consolidate. Those changes combine cleanly with this PR. In the merged tree, the owner gate runs before _admit and before the new app_lifecycle_lock and _RUN_LOCK holds.
  • CLI app disable never fires in-process app-disable hooks on a running gateway #11117 (Issue) is closed as completed, but no fix commit is linked to it. On main, cli_commands.py:1262 still calls disable_app(args.name) in the CLI process, and that call does not notify any in-process disable hooks. The GPT F1 finding ("CLI disable bypasses pool retirement") is a platform problem that existed before this PR. It is still open on main and is not a regression introduced here.

Relationships

Assessment: BREAKING_CHANGE=no. P0=no. One behaviour change is still open: /post now waits on _RUN_LOCK, so it no longer returns the immediate 409 run_still_running. The Design review already raised this. The two fenced GPT findings still need the maintainer ruling requested in the 2026-09-15 escalation comment. I see nothing that justifies closing this PR.

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

drive-to-green PR claimed by drive-to-green pipeline fork Pull request from a fork (external contributor) merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: action required A blocking check or review needs attention

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants