Skip to content

test(daemon): extract shared DaemonAPIHandler mock scaffolding - #3434

Merged
Sinity merged 3 commits into
masterfrom
feature/test/mock-scaffolding-extract
Jul 31, 2026
Merged

Sinity merged 3 commits into
masterfrom
feature/test/mock-scaffolding-extract

Conversation

@Sinity

@Sinity Sinity commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Summary

  • Extracts the hand-duplicated _MockServer/_MockHeaders/_make_handler trio from three daemon test files into a shared tests/infra/daemon_http_harness.py (polylogue-myhg).
  • Closes three CodeRabbit findings from PR fix(insights): prefer typed PR/session evidence over regex correlation #3425's review that were triaged and filed as polylogue-2vor rather than blocking that merge (all three are in polylogue/insights/session_commit.py, with regression tests).

Problem

polylogue-myhg: CodeRabbit flagged (PR #2559) that _MockServer/_MockHeaders/_make_handler were duplicated near-identically across test_daemon_http_security.py, test_daemon_events_endpoint.py, and test_provider_usage_endpoint.py. Drift already happened once: the host= parameter was added to two of the three copies during a fast-follow, not all three. A fresh grep tonight confirmed the duplication was still live.

polylogue-2vor: three P2 findings in session_commit.py were left unaddressed at PR #3425's merge time:

  1. typed_refs_from_session_refs() coerced a session_refs row with a valid url/repo but no ref_number to "PR #0" (observed for Codex Cloud's chatgpt_codex_sidecar._pull_request_ref(), which stores external_pull_request_id in url and leaves repo/number unset). Since typed refs are authoritative over the regex fallback, this could suppress a correctly-parsed regex result with a bogus PR #0.
  2. Disagreement detection compared PR/issue identity by bare number only, so acme/product#42 and other/repo#42 compared equal — a real cross-repo disagreement was never surfaced.
  3. Foreign-trailer classification flagged a disagreement whenever a commit carried any Claude-Session trailer, even when the current session has no bridge_session_ids of its own — i.e. even with no typed identity to compare against.

Solution

Mock harness extraction (tests/infra/daemon_http_harness.py):

  • MockDaemonServer, MockHeaders, make_daemon_handler, capture_json_response, capture_responses — these mock only the HTTP transport boundary (the listening socket/ThreadingHTTPServer a real DaemonAPIHandler normally sits on, and the parsed header block a socket read would produce). do_GET/do_POST dispatch, _check_auth, cross-origin checks, route handlers, and JSON/SSE serialization all still run as the real production DaemonAPIHandler built via __new__ — no boundary that was previously mocked is hollowed out.
  • The three named test files now import the shared harness instead of defining their own copies. test_daemon_http_security.py keeps a thin file-local _make_handler wrapper because that file's whole point is exercising the auth-required path, so its default server needs a configured token ("secret") unlike the harness's own open-by-default MockDaemonServer.
  • While touching test_daemon_events_endpoint.py, three ad-hoc type()-built _Srv server stand-ins were also converted to MockDaemonServer — same duplication pattern CodeRabbit flagged in the original trio.
  • Fixed two cross-file importers (test_web_auth.py, test_route_contracts.py) that imported _MockServer/_capture_responses from test_daemon_http_security.py rather than defining their own copies — a case a narrow per-file devtools test run doesn't catch but whole-repo mypy --strict does.

session_commit.py fixes (regression test per finding, see tests/unit/insights/test_session_commit.py):

  1. typed_refs_from_session_refs() now tries to recover a real number by parsing a genuine github.com PR/issue URL out of url when number is absent; if that also fails, the row is skipped rather than defaulting to 0.
  2. New _refs_match() compares the full (owner, repo, number) identity whenever both refs are repo-qualified, falling back to number-only equality when either side lacks repo identity (e.g. a bare #42 regex mention).
  3. foreign_trailer now additionally requires own_trailer_tokens to be non-empty.

Verification

  • ruff check + mypy (strict, whole repo: 2397 source files) clean.
  • devtools test tests/unit/daemon/test_daemon_http_security.py tests/unit/daemon/test_daemon_events_endpoint.py tests/unit/daemon/test_provider_usage_endpoint.py tests/unit/daemon/test_web_auth.py tests/unit/daemon/test_route_contracts.py → 709 passed.
  • devtools test tests/unit/insights/test_session_commit.py → 46 passed; also ran tests/unit/cli/test_correlate_view.py tests/unit/storage/test_archive_tiers_write.py (other consumers of session_commit.py) → 80 passed.
  • devtools verify --quick → clean end to end.

Ref polylogue-myhg
Ref polylogue-2vor

Sinity and others added 3 commits July 31, 2026 10:06
Problem: the _MockServer/_MockHeaders/_make_handler trio was
hand-duplicated near-identically across test_daemon_http_security.py,
test_daemon_events_endpoint.py, and test_provider_usage_endpoint.py
(CodeRabbit finding on PR #2559, filed as polylogue-myhg). Drift already
happened once: the host= parameter was added to two of the three copies
during a fast-follow, not all three.

Solution: new tests/infra/daemon_http_harness.py holds MockDaemonServer,
MockHeaders, make_daemon_handler, capture_json_response, and
capture_responses. These mock only the HTTP transport boundary (the
listening socket/ThreadingHTTPServer a real DaemonAPIHandler normally
sits on, and the parsed header block a socket read would produce) —
do_GET/do_POST dispatch, _check_auth, cross-origin checks, route
handlers, and JSON/SSE serialization all still run as the real
production DaemonAPIHandler built via __new__. The three test files now
import the shared harness instead of defining their own copies; each
keeps a thin file-local _make_handler wrapper only where it needs a
different default (http_security.py defaults its server to a configured
auth token, since that file's whole point is exercising the
auth-required path). While touching test_daemon_events_endpoint.py,
three ad-hoc type()-built _Srv server stand-ins were also converted to
MockDaemonServer for the same reason CodeRabbit flagged the original
trio — same duplication pattern, same drift risk.

No boundary that was previously mocked is now hollowed out: the
production DaemonAPIHandler class is still constructed and its real
methods still execute against these fakes in every migrated test.

Verification: ruff check + mypy (strict) clean on all four touched
files; devtools test tests/unit/daemon/test_daemon_http_security.py
tests/unit/daemon/test_daemon_events_endpoint.py
tests/unit/daemon/test_provider_usage_endpoint.py -> 584 passed.

Ref polylogue-myhg

Co-Authored-By: Claude <[email protected]>
Problem: three CodeRabbit findings from PR #3425's review were triaged
and filed as polylogue-2vor rather than blocking that merge:

1. typed_refs_from_session_refs() coerced a session_refs row with a
   valid url/repo but no ref_number to "PR #0" (observed for Codex
   Cloud's chatgpt_codex_sidecar._pull_request_ref(), which stores
   external_pull_request_id in url and leaves repo/number unset).
   Because typed refs are authoritative over the regex fallback, this
   could suppress a correctly-parsed regex result with a bogus PR #0.
2. Disagreement detection in _resolve_refs() compared PR/issue identity
   by bare number only, so acme/product#42 and other/repo#42 compared
   equal -- a real cross-repo disagreement was never surfaced.
3. Foreign-trailer classification in detect_session_commits() set
   foreign_trailer=True whenever a commit carried any Claude-Session
   trailer, even when the current session has no bridge_session_ids of
   its own (own_trailer_tokens empty) -- i.e. even when there is no
   typed identity to compare against, producing a disagreement out of
   thin air.

Solution:
1. typed_refs_from_session_refs() now tries to recover a real number
   by parsing a genuine github.com PR/issue URL out of `url` when
   `number` is absent; if that also fails, the row is skipped rather
   than defaulting to 0.
2. New _refs_match() compares the full (owner, repo, number) identity
   whenever both refs are repo-qualified, falling back to number-only
   equality when either side lacks repo identity (e.g. a bare "#42"
   regex mention, where number is the only available signal).
3. foreign_trailer now additionally requires own_trailer_tokens to be
   non-empty.

Each fix has a regression test in tests/unit/insights/test_session_commit.py
naming the exact bead/finding it covers.

Verification: ruff check + mypy (strict) clean; devtools test
tests/unit/insights/test_session_commit.py -> 46 passed; also ran
tests/unit/cli/test_correlate_view.py tests/unit/storage/test_archive_tiers_write.py
(other consumers of this module) -> 80 passed; devtools render all --check clean.

Ref polylogue-2vor

Co-Authored-By: Claude <[email protected]>
Problem: devtools verify --quick (mypy, whole-repo) caught what the
narrower per-file devtools test runs in the prior commit could not:
tests/unit/daemon/test_web_auth.py and test_route_contracts.py import
_MockServer/_capture_responses FROM test_daemon_http_security.py rather
than defining their own copies, so removing those names broke both
files (attr-defined errors under mypy --strict; would also have been an
ImportError at collection time).

Solution: both files now import MockDaemonServer/capture_responses
directly from tests/infra/daemon_http_harness.py (still importing
_make_handler from test_daemon_http_security.py, since that file's
thin wrapper deliberately defaults the mock server's auth token to
"secret" -- test_web_auth.py's whole point is exercising the
web-credential/cookie auth path, which only activates once a token is
configured; _check_auth() returns True unconditionally when no token
is set, so an open-by-default server would silently skip the very
paths these tests assert on).

Verification: ruff check + mypy (strict, whole repo: 2397 source
files) clean; devtools test tests/unit/daemon/test_web_auth.py
tests/unit/daemon/test_route_contracts.py
tests/unit/daemon/test_daemon_http_security.py
tests/unit/daemon/test_daemon_events_endpoint.py
tests/unit/daemon/test_provider_usage_endpoint.py -> 709 passed;
devtools verify --quick clean end to end.

Ref polylogue-myhg

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 22 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 220f9c3e-bc03-4f6c-8cad-6e5b1213a7da

📥 Commits

Reviewing files that changed from the base of the PR and between 3245eb3 and acdb805.

📒 Files selected for processing (8)
  • polylogue/insights/session_commit.py
  • tests/infra/daemon_http_harness.py
  • tests/unit/daemon/test_daemon_events_endpoint.py
  • tests/unit/daemon/test_daemon_http_security.py
  • tests/unit/daemon/test_provider_usage_endpoint.py
  • tests/unit/daemon/test_route_contracts.py
  • tests/unit/daemon/test_web_auth.py
  • tests/unit/insights/test_session_commit.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Sinity
Sinity merged commit 9b56c49 into master Jul 31, 2026
3 checks passed
@Sinity
Sinity deleted the feature/test/mock-scaffolding-extract branch July 31, 2026 08:26

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: acdb805448

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread polylogue/insights/session_commit.py Resolved
Sinity added a commit that referenced this pull request Jul 31, 2026
Both closed after PR #3434 merged: shared DaemonAPIHandler mock
scaffolding extraction (tests/infra/daemon_http_harness.py) and the
three session_commit.py typed-evidence gaps from PR #3425's
CodeRabbit review.

Co-Authored-By: Claude <[email protected]>
@Sinity

Sinity commented Sep 30, 2026

Copy link
Copy Markdown
Owner Author

Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.

Only the rows listed above are dispositioned. Held findings remain open.

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.

1 participant