Skip to content

fix(test): stop leaving stub modules registered in sys.modules - #40129

Merged
Zedmor merged 1 commit into
mainfrom
fix/slack-handler-import-cycle
Nov 20, 2026
Merged

Zedmor merged 1 commit into
mainfrom
fix/slack-handler-import-cycle

Conversation

@Zedmor

@Zedmor Zedmor commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Five test files no longer leave a stand-in module registered in sys.modules, so each passes when run by itself and none breaks the tests that run after it in the same process.

Four files fail on main when run alone, every test erroring in setup:

ImportError: cannot import name '_PHASE_EMOJIS' from 'kiro_crew.slack.handler' (unknown location)

test_cron_origin_injection.py, test_send_message_targeted.py and test_send_message_session_link.py each begin with if "kiro_crew.slack.handler" not in sys.modules: and then register a two-function types.ModuleType under that name, never removing it. The autouse fixture _clean_emojis (test/conftest_slack.py) imports _PHASE_EMOJIS from kiro_crew.slack.handler before every test, finds the stub, and fails. test_send_message_per_part.py imports its helpers from test_send_message_targeted.py and fails the same way.

Two more files, test_enable_deps_resolution.py and test_lifecycle_hooks.py, register a MagicMock as kiro_crew.dashboard.server the same way. Their own tests pass; every later test in the process that imports the dashboard gets the mock.

Why it matters

Main is green only by order: an xdist worker has imported the real modules while collecting earlier files, so the not in sys.modules branch never runs. Anyone running one of these files alone gets a wall of errors that has nothing to do with their change, and any regrouping of files into processes (a leaf-scope run, a smaller shard) can turn it red in CI. It is the same class as #40124: a test's stand-in outliving the test.

Not a goal

  • Changing kiro_crew.slack.handler, the dashboard, or the _clean_emojis fixture: the product code and the fixture are correct; the stubs are the defect.
  • A general guard against test modules writing to sys.modules.
  • test_pip_deps_consistency.py and test_publish_personal_drive_registration.py, which also assign into sys.modules: the second restores the original, and neither showed a failure in the check below.

What changed (motivation → approach → change)

Symptom: ImportError in setup when a file runs alone; a mock dashboard for later tests. Cause: a stub registered under a real module's name at import or inside a test, with no restore. The stubs' stated reason was "importable even when heavy transitive deps aren't installed" and "avoid circular import"; neither holds in this suite: the autouse fixture already imports the real handler for every test, and both dashboard files pass with the real module. Change: the five stub registrations are deleted, with the imports they needed (sys, types) and the # noqa: E402 marks that only existed because code sat above the imports. One test that looked the stub up in sys.modules to patch is_tracked_channel now patches the real module, inside the same with.

Backwards compatibility

Compatible: test files only; nothing that runs in the product changes.

Removes nothing: five stub registrations are removed from test modules; no test, assertion or fixture is removed.

Tests

Proof: not provable -- test-only change

Each command is python -m pytest -n0 --no-cov -q -p no:cacheprovider <file>; the last two rows add test/test_power.py::test_should_prevent_sleep_no_sessions, which imports the dashboard.

file before, main d465efc after
test/test_cron_origin_injection.py 6 errors 6 passed
test/test_send_message_per_part.py 18 errors 18 passed
test/test_send_message_session_link.py 36 errors 36 passed
test/test_send_message_targeted.py 87 errors 87 passed
test/test_enable_deps_resolution.py + the dashboard test 1 failed, 15 passed 16 passed
test/test_lifecycle_hooks.py + the dashboard test 1 failed, 38 passed 39 passed

All six files together with test/test_flake_pattern_ratchet.py under -n 4: 275 passed. Black, isort, flake8, comment-history, brand and file-line-cap gates pass. test/test_lifecycle_hooks.py is on the black baseline and is edited without reformatting.

Manual verification

N/A — the table above is the whole behaviour, and each row is a pytest command.

Real-machine verification

Machine: own development host (EC2, AMD EPYC 9R14), Amazon Linux 2023, x86_64, 32 vCPU / 123 GiB
Before (main d465efc): python -m pytest -n0 --no-cov -q -p no:cacheprovider test/test_send_message_targeted.py -> 87 errors, ImportError: cannot import name '_PHASE_EMOJIS' from 'kiro_crew.slack.handler' (unknown location)
After (head 73dff1f): the same command -> 87 passed
Cleanup: own computer

Related Issues

Related: #39702

Sibling sweep

Swept: grep -rnE 'sys\.modules\["kiro_crew\.[a-z_.]*"\] = |sys\.modules\.setdefault\("kiro_crew' test/*.py (assignments not made through monkeypatch), then each hit's file run alone and followed by the dashboard test
Sibling: test/test_cron_origin_injection.py -- changed here
Sibling: test/test_send_message_targeted.py -- changed here
Sibling: test/test_send_message_session_link.py -- changed here
Sibling: test/test_enable_deps_resolution.py -- changed here
Sibling: test/test_lifecycle_hooks.py -- changed here
Sibling: test/test_send_message_per_part.py -- not affected -- registers nothing itself; it failed through its import of test_send_message_targeted.py and passes now
Sibling: test/test_publish_personal_drive_registration.py -- not affected -- the assignment at line 76 puts the original module back; with the dashboard test after it: 19 passed
Sibling: test/test_pip_deps_consistency.py -- not affected -- run with the dashboard test after it: 11 passed

The search covers test/*.py only, not the app suites under src/kiro_crew/apps/builtins.

Pattern harvest

Rule candidate: semgrep
Pattern: a test module assigns into sys.modules under a real kiro_crew.* name outside monkeypatch.setitem, so the stand-in is never removed

Checklist

  • A Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits
  • 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

Contributed under the project's Apache-2.0 license.

AI-assisted, opened by Claude Code

Five test files put a stand-in module into sys.modules when the real
one was not yet imported, and never took it out.

Three of them registered a two-function stub as
kiro_crew.slack.handler. An autouse fixture imports _PHASE_EMOJIS from
that module before every test, so with the stub in place every test of
the file fails in setup with ImportError. test_send_message_per_part.py
imports one of the three and fails the same way. Main is green because
an xdist worker has imported the real handler while collecting an
earlier file, so the stub is never installed; each of the four files
run alone fails.

Two more registered a MagicMock as kiro_crew.dashboard.server. Their
own tests pass, and every later test in the process that imports the
dashboard gets the mock.

The stubs are removed. The real modules import in this suite: the
autouse fixture already needs the handler for every test, and both
dashboard files pass without the mock.

Related: #39702
@Zedmor
Zedmor requested review from a team and KiroCrewCaptain as code owners November 20, 2026 20:23
@Zedmor
Zedmor requested a review from chenmingwei23 November 20, 2026 20:23
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ⏭️ skipped

Revision 73dff1f124b103c60ce27bb09be54c69baf3c555 ships no reviewable capability (docs, tests or generated files only), so there is nothing to inventory. Does not block merge.

@github-actions

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

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

@github-actions

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

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

Design-Verdict: PASS

Nothing blocks: this is a test-only change that deletes stub modules nothing ever removed, so the tests now import the real modules and no longer depend on which files ran first in the process.

Notes

[DESIGN-REVIEWED] 73dff1f

Description read

[DESCRIPTION-READ] 133acf7ea4ed52076f7a3336e8284807413050d79313d132a3ef351d653b1bbe

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

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 73dff1f124b103c60ce27bb09be54c69baf3c555 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 73dff1f

Verdict parsed from the review's SHA-scoped output markers for commit 73dff1f124b103c60ce27bb09be54c69baf3c555.

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

@github-actions

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 73dff1f124b103c60ce27bb09be54c69baf3c555 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 73dff1f

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

@Zedmor
Zedmor enabled auto-merge (squash) November 20, 2026 20:52
@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Nov 20, 2026
@KiroCrewCaptain

KiroCrewCaptain commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: Approve

Why: the decider estimates a 5% chance that this change introduces a defect, within the 12% limit for approving on its own, and every check passes.

Test-only change. It deletes stand-in modules that five test files registered in sys.modules and never removed: three for kiro_crew.slack.handler, two MagicMocks for kiro_crew.dashboard.server. Every test that depends on is_tracked_channel or is_allowed_user already patches the real kiro_crew.slack.handler attribute, and the product code imports those functions at call time, so the patches take effect. With no patch, the real functions return False for test inputs (empty tracked-channel set, owner-only check), which matches the stand-ins. The edited files have no leftover uses of sys or types, and the local imports that remain are still used. No tests or assertions are removed, and every CI check is green, including Windows, the backend shards and new-test stability.

Tier: 1, every tier gate passed.

An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit 73dff1f124 · 75 changed lines. If this recommendation is wrong, reply here: replies are how the approver is evaluated.

@KiroCrewCaptain KiroCrewCaptain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved automatically by the code change approver, not by a person. The decider estimates a 5% chance that this change introduces a defect, within the 12% limit for approving on its own, and the change has at most 400 changed lines.

Test-only change. It deletes stand-in modules that five test files registered in sys.modules and never removed: three for kiro_crew.slack.handler, two MagicMocks for kiro_crew.dashboard.server. Every test that depends on is_tracked_channel or is_allowed_user already patches the real kiro_crew.slack.handler attribute, and the product code imports those functions at call time, so the patches take effect. With no patch, the real functions return False for test inputs (empty tracked-channel set, owner-only check), which matches the stand-ins. The edited files have no leftover uses of sys or types, and the local imports that remain are still used. No tests or assertions are removed, and every CI check is green, including Windows, the backend shards and new-test stability.

Tier: 1, every tier gate passed.
Criteria: no merge conflict, no owner block, no change request, the default branch, no security-sensitive path, no design document to agree on, the readiness status, the review lanes, the P0 history, the linked issues' tier, the review lessons, no concern label, the file count, a safe change category, not its own change, the decider, the security checklist, the change matches its title, no competing change.

Decider v3c (217fe607). To overrule it, request changes or dismiss this review.

@Zedmor
Zedmor merged commit 9c7ee2b into main Nov 20, 2026
101 checks passed
@Zedmor
Zedmor deleted the fix/slack-handler-import-cycle branch November 20, 2026 21:52
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants