Skip to content

test(subagent): keep the claimed write's bound out of the ending race - #28247

Closed
leonlaiyc wants to merge 1 commit into
kirodotdev:mainfrom
leonlaiyc:claude/subagent-claimed-write-bound-flake
Closed

leonlaiyc wants to merge 1 commit into
kirodotdev:mainfrom
leonlaiyc:claude/subagent-claimed-write-bound-flake

Conversation

@leonlaiyc

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Stop test_whatever_lands_after_the_claim_the_run_is_completed_once from failing at random on a loaded CI runner.

The test failed on an unrelated PR (#24788, Backend Tests (3.12, 6)). Its first check after the claimed result write is entered is assert info._ending_claimed and not info.done, and that check failed. The captured log shows why:

WARNING  kiro_crew.subagent:run_records.py:198 result complete write for 944c7fa21076fa56 did not finish in 5s — leaving it to finish detached

Why it matters

When this test fails at random, a backend shard goes red on PRs that never touch subagents. Each red shard costs a maintainer a re-run and an attribution comment. The failing line runs before any ending is applied, so all seven parametrized cases have the same exposure.

Not a goal

  • Changing _STATE_DRAIN_TIMEOUT or the bounded-drain behaviour in production. The bound is correct; this test just was not meant to exercise it.
  • Reworking _GatedResultWrite or the other tests that use it.

What changed (motivation → approach → change)

  • Symptom: the run is already done when the test checks that the claimed write is still pending.
  • Root cause: _GatedResultWrite holds the write, but the write's drain has its own wall-clock bound (_STATE_DRAIN_TIMEOUT, 5 s), which starts at the drain's first await. If the pool thread picks up the write late, or the loop is starved, the gate signals entered only after the bound has expired. By then the run has left the write detached and finished. test_a_claimed_result_write_that_hangs_is_bounded in the same file already guards against this race with _HeldResultDrain (test: hold the claimed result write's drain until the test looks #19149).
  • Change: this test now sets subagent._STATE_DRAIN_TIMEOUT to 60 s, which is past the gate's own 15 s hold. The gate alone now decides when the write lands, and the bound can no longer win a race the test never meant to run. I chose this over _HeldResultDrain because parking the drain before its real body would deliver the cancel-shaped endings to the park, not to the real drain those cases test. Test-only; one file.

Backwards compatibility

Compatible: test-only change, no production code touched.

Removes nothing: only a test's local timing bound is raised.

Tests

Proof: not provable -- test-only change

I reproduced it locally by injecting a 6 s sleep into _GatedResultWrite before it signals entered. That sleep was not committed. It stands in for a late pool pickup:

result
main + 6 s delay 7 failed, all with assert (True and not True) at the same line as CI
this branch + 6 s delay 7 passed
this branch, no delay, whole file 76 passed

Also run locally: black, isort, flake8, check_comment_history.py, check_brand_name.py, test_flake_pattern_ratchet.py.

Manual verification

N/A — test-only change; the delay-injection run above covers the race directly.

Related Issues

Closes #28239

Sibling sweep

Swept: git grep -n "_GatedResultWrite(" -- test/
Sibling: test/test_subagent_stop_reason_consistency.py:610 -- not affected -- already parks the drain with _HeldResultDrain and shrinks the bound on purpose
Sibling: test/test_subagent_stop_reason_consistency.py:653 -- not affected -- asserts info.done, which is true whether or not the bound expired
Sibling: test/test_subagent_stop_reason_consistency.py:1250 -- not affected -- asserts info.done, and its outcome is the same if the bound expired first

Pattern harvest

Rule candidate: review-prompt
Pattern: a test gates an operation that production code also bounds by wall-clock, then asserts it is still pending, without lifting or parking that bound

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

🤖 Generated with Claude Code

test_whatever_lands_after_the_claim_the_run_is_completed_once holds the
claimed result write with a gate, then asserts the run is not done yet.
The write's drain has its own 5 s wall-clock bound. On a loaded runner
that bound expires before the gate reports entered, so the run is
already done when the test looks.

Raise the bound past the gate's 15 s hold in this test, so only the
gate decides when the write lands.

Closes kirodotdev#28239

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leonlaiyc
leonlaiyc requested review from a team and KiroCrewCaptain as code owners November 2, 2026 15:14
@github-actions github-actions Bot added the fork Pull request from a fork (external contributor) label Nov 2, 2026
@github-actions github-actions Bot added the readiness: action required A blocking check or review needs attention label Nov 2, 2026
@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Nov 6, 2026
@leonlaiyc

Copy link
Copy Markdown
Contributor Author

Closing as superseded: #31174 landed the same fix on main, and rebasing this PR leaves no change beyond what is already there.

@leonlaiyc leonlaiyc closed this Nov 13, 2026
@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor) merge conflict Branch has merge conflicts with its base — author must resolve before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky: test_whatever_lands_after_the_claim_the_run_is_completed_once — the claimed write's 5 s bound expires before the test looks

1 participant