Skip to content

[CTX-1814] test(terminal): cover the new-window authorization denial branch - #3141

Merged
Xuepoo merged 1 commit into
mainfrom
ctx-1814/window-denial-test
Nov 13, 2026
Merged

Xuepoo merged 1 commit into
mainfrom
ctx-1814/window-denial-test

Conversation

@Xuepoo

@Xuepoo Xuepoo commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds the missing regression coverage for the bitty ctl window new
authorization denial branch (bitty#1765, slice 5).

The review of #3140 (mutation M4) found that forcing the view.manage gate in
handle_new_window (crates/bitty-terminal/src/ipc_serve.rs) to false left
the entire suite green — nothing exercised the denial, so the gate could be
deleted without a red test.

Three tests, default build, no feature gates:

test file proves
new_window_denies_a_connection_without_view_manage_before_the_lane crates/bitty-terminal/src/ipc_serve.rs a grant shaped like ScopeSet::mcp_default() (view.inspect, no view.manage) is refused with scope / ScopeDenied, the message names view.manage, and the request never reaches the lane (new_window::claim() stays empty)
new_window_reaches_the_lane_when_the_grant_carries_view_manage crates/bitty-terminal/src/ipc_serve.rs positive control: the same request through the same table with a ScopeSet::cli_default() grant reaches the lane and the app-side claim answers Admitted → {"requested":true,"windows":2}
window_new_reports_a_scope_denial_as_permission_denied crates/bitty-terminal/src/ctl/render.rs the handler's exact pair renders as Denied / exit 7 for CtlRequest::NewWindow (never transport or conflict)

Why

The denial branch is the only thing standing between a read-only connection
(MCP/agent default) and a window-creating side effect. A green suite that
cannot fail when the gate is removed is not coverage.

How

The handler tests call the production build_dispatcher(false) table, and the
ServeContext is built with ServeContext::with_connection_grant from a real
bitty_ipc::ctl::ControlAuthority grant whose session is bound to an attested
socketpair peer — the same shape a served connection gets. No test-only scope
shortcut, no reimplementation of the check: the assertion is on the error the
handler returns.

Verified

Mutation proof in a scratch copy of the worktree with its own CARGO_TARGET_DIR
(never the lane checkout): forcing the
!scopes.contains(bitty_ipc::Scope::ViewManage) condition in
ipc_serve.rs to false

  • new_window_denies_... goes RED: left: "transport", right: "scope" —
    the request falls through to the lane, which nothing services, and the
    handler answers Unavailable after the 5 s deadline;
  • new_window_reaches_the_lane_... stays green (the fixture is not what
    fails);
  • restoring the condition turns both green.

Gates (all green, CARGO_TARGET_DIR=.targets/ctx-1814):

  • cargo fmt --all -- --check
  • cargo clippy -p bitty-terminal --all-targets --offline -- -D warnings
  • cargo test -p bitty-terminal --offline <new test names> — 3/3
  • cargo test -p bitty-terminal --offline --bin bitty ipc_serve:: — 21 passed
  • cargo test -p bitty-terminal --offline --bin bitty ctl:: — 101 passed
  • cargo test -p bitty-terminal --offline --bin bitty — 1584 passed (full unit suite)
  • cargo +1.85 check -p bitty-terminal --tests --offline
  • RUSTDOCFLAGS="-D warnings" cargo doc -p bitty-terminal --no-deps --offline

just ci-local was not run (act cannot reach github.com from this host).

Related #3140
Related #1765

Summary by CodeRabbit

  • Tests
    • Added coverage for new-window permission checks, confirming that requests without the required access are denied and do not create a window.
    • Verified that permitted requests can proceed and that permission denials are distinguished from unavailable-instance and conflict outcomes.

…branch

The review of #3140 (mutation M4) showed that forcing the `view.manage` gate
in `handle_new_window` false left the whole suite green: nothing exercised the
denial branch, so the gate could be deleted unnoticed.

- ipc_serve: `new_window_denies_a_connection_without_view_manage_before_the_lane`
  drives the production `build_dispatcher(false)` table with a real
  `ControlAuthority`-issued grant shaped like `ScopeSet::mcp_default()`
  (`view.inspect`, no `view.manage`) bound to an attested peer, and asserts the
  handler answers `scope`/`ScopeDenied`, names the missing scope, and never
  offers the lane a request (`new_window::claim()` stays empty).
- ipc_serve: `new_window_reaches_the_lane_when_the_grant_carries_view_manage` is
  the positive control: the same request and table with a
  `ScopeSet::cli_default()` grant reaches the lane, and the app-side claim
  answers `Admitted` -> `{"requested":true,"windows":2}`.
- ctl::render: `window_new_reports_a_scope_denial_as_permission_denied` pins
  that the handler's exact pair renders as `Denied` / exit 7 for
  `CtlRequest::NewWindow`, so a denial never reads as a transport failure or a
  conflict.

Mutation proof (scratch copy of this worktree, own CARGO_TARGET_DIR): forcing
the `!scopes.contains(Scope::ViewManage)` condition in ipc_serve.rs to `false`
turns the denial test RED (`transport`/`Unavailable` after the 5 s lane
deadline, 5.00s run) while the positive control stays green; restoring the
condition turns both green.

Gates: fmt --all --check, clippy --all-targets -D warnings, the new tests plus
`ipc_serve::` (21) and `ctl::` (101) module tests, MSRV `+1.85 check --tests`,
and `RUSTDOCFLAGS=-D warnings cargo doc`.
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The pull request adds tests for bitty.debug/newWindow authorization with mcp_default and cli_default grants. It also adds a test that checks how window new renders a scope-denied outcome.

Changes: Window Scope Denial

Layer / File(s) Summary
New-window denial and outcome rendering
crates/bitty-terminal/src/ipc_serve.rs, crates/bitty-terminal/src/ctl/render.rs
Unix-only tests check that mcp_default is denied without placing a request on the new-window lane, and that cli_default reaches the lane and returns an admission for two windows. A rendering test checks that a scope-denied outcome is classified as Denied and returns EXIT_PERM.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other

Merge Risk: Minimal · up to 9cf16

This change adds tests for the new-window permission denial path and its exit-code rendering. It has no production behavior change, and no merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the conventional-commit type and scope and states the main change: adding a test that covers the new-window authorization denial branch. The raw summary and objectives confirm the chan…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the conventional-commit type and scope and states the main change: adding a test that covers the new-window authorization denial branch. The raw summary and objectives confirm the changeset adds regression tests for the scope denial path in window new and ipc_serve, so the title matches the primary change. It omits the mutation-testing detail, which is acceptable.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI (base), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: aebd5408-ed03-4ac8-aa76-21412e75c9b2
Commits

Reviewing files that changed from the base of the PR and between eb26e87 and 9cf163a.

Files selected for processing: 2

  • crates/bitty-terminal/src/ctl/render.rs
  • crates/bitty-terminal/src/ipc_serve.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@Xuepoo Xuepoo left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Review — #3141 test(terminal): cover the new-window authorization denial branch

Reviewed at 9cf163a99597e8f3998306ab1f58765b61952978 against origin/main.
Read-only on the lane worktree; all mutation work was done on a private copy
(~/.hermes/cache/scratch/ctx-1814-review/bitty, its own CARGO_TARGET_DIR).
Lane tree left clean (git status --porcelain empty, HEAD unchanged).

1. Scope — PASS

git diff origin/main..HEAD touches exactly two files, +180 / -1:

  • crates/bitty-terminal/src/ipc_serve.rs — one hunk, @@ -1677,6 +1677,140 @@ mod tests {. The module gate is #[cfg(test)] mod tests at ipc_serve.rs:1118, so the whole hunk is inside the test module; the file ends at 1944, still inside it.
  • crates/bitty-terminal/src/ctl/render.rs — hunks @@ -1208,7 +1208,7 @@ and @@ -1246,6 +1246,51 @@ mod tests {. mod tests begins at render.rs:1207; the single deleted line is the import use crate::ctl::{… EXIT_COMPAT …} at render.rs:1211, replaced at render.rs:1213 to add EXIT_PERM to the same test-module import.

No production logic, no #[allow], no #[ignore], no #[cfg(feature = …)] and no should_panic anywhere in the added lines. The only attributes added are #[cfg(unix)] #[test] (ipc_serve.rs:1696-1697, 1755-1756); Unix is the default build on this crate's CI (cargo nextest run --workspace in .github/workflows/ci.yml:351), so nothing is feature-hidden — I ran all three tests in the default build (see §6).

2. Real boundary — PASS

The denial test drives the production table and the production handler, not a re-implementation of the check:

  • ipc_serve.rs:1734-1736 calls build_dispatcher(false).dispatch(&context, &request); build_dispatcher (ipc_serve.rs:352) registers crate::ctl::METHOD_NEW_WINDOW → handle_new_window at ipc_serve.rs:373.
  • The gate exercised is the production one at ipc_serve.rs:317, fed by context.current_authorization() at ipc_serve.rs:307.
  • The context is built with the public ServeContext::with_connection_grant (ipc_serve.rs:1720) from a real ControlAuthority::open_connection grant (ipc_serve.rs:1711-1717); that constructor sets authority_required: true (bitty-ipc/crates/bitty-ipc/src/devtools/serve.rs:598), and current_authorization then reads the live grant (serve.rs:740-748 → ConnectionGrant::snapshot, crates/bitty-ipc-devtools/src/ctl.rs:697-699), not the construction-time copy. The test asserts that invariant at ipc_serve.rs:1725-1728.
  • ScopeSet::mcp_default() really lacks view.manage: it inserts only terminal.inspect, view.inspect, config.inspect, plugin.inspect (bitty-ipc/crates/bitty-ipc-api/src/scope.rs:293-300), while cli_default() inserts view.manage (scope.rs:271-279). Both preconditions are asserted in-test (ipc_serve.rs:1705-1708, 1764-1767).

3. Assertions — PASS

Denial test (ipc_serve.rs:1738-1747): category "scope", code "ScopeDenied", message contains view.manage, and new_window::claim() is None. All four read production state — HandlerError returned by the dispatcher, and the real process-global lane (ctl/new_window.rs:301-315), not the test's own fixture.

Positive control (ipc_serve.rs:1788-1811): the same table and the same request shape, differing only in the granted scope set; it claims through the production lane (ipc_serve.rs:1795), answers NewWindowReply::Admitted { windows: 2 } (1804), and asserts the handler's own success payload {"requested":true,"windows":2} (1810). That payload is produced by handle_new_window (ipc_serve.rs:330-332), so the assertion is about handler output, not about the fixture.

Lane isolation is sound: every other in-process user of the lane also holds test_lane_lock() (ctl/new_window.rs:374, 391, 414, 429; src/tests.rs:19863, 20105), so the claim().is_none() check cannot be perturbed by a concurrent test. See the residual in the next section for what that check does and does not prove.

4. Discrimination (my own mutation, independent) — PASS

Private copy of the worktree, own target dir, gate forced false at ipc_serve.rs:317 (if false && !scopes.contains(bitty_ipc::Scope::ViewManage)):

test ipc_serve::tests::new_window_denies_a_connection_without_view_manage_before_the_lane ... FAILED
test ipc_serve::tests::new_window_reaches_the_lane_when_the_grant_carries_view_manage ... ok

---- ipc_serve::tests::new_window_denies_a_connection_without_view_manage_before_the_lane stdout ----
thread '…' panicked at crates/bitty-terminal/src/ipc_serve.rs:1738:9:
assertion `left == right` failed: HandlerError { category: "transport", code: "Unavailable",
message: "the running instance did not service the new-window request before the deadline; nothing was created" }
  left: "transport"
 right: "scope"

test result: FAILED. 1 passed; 1 failed; … finished in 5.01s

Restoring the condition (grep -c "if false &&" → 0, line 317 back to the original) returns both to green:

test ipc_serve::tests::new_window_denies_a_connection_without_view_manage_before_the_lane ... ok
test ipc_serve::tests::new_window_reaches_the_lane_when_the_grant_carries_view_manage ... ok
test result: ok. 2 passed; 0 failed; …

The denial test fails on exactly the pre-fix behaviour the #3140 review demonstrated (M4), and the positive control is not what fails.

5. Non-tautology — PASS

render.rs:1256 builds a CtlIpcOutcome carrying the handler's ("scope", "ScopeDenied") pair, then asserts two production functions:

  • super::class_for_server_error (render.rs:91-113) → "Denied" (render.rs:1276-1280).
  • render_outcome (render.rs:490-542) → EXIT_PERM (render.rs:1281-1291).

That is not a restatement of a constant: render_outcome has a CtlRequest::NewWindow-specific branch (render.rs:516-529) that returns EXIT_COMPAT for NotFound/UnknownMethod, so the test pins that a new-window scope denial takes the permission exit and does not get swallowed by the skew branch. It would go red if class_for_server_error or that branch were changed to classify this pair differently.

6. Gates (run by me) — PASS

Lane worktree, CARGO_TARGET_DIR=/mnt/data/Workspace/Projects/bitty-terminal/.targets/ctx-1814-review (a private reflink copy of .targets/ctx-1814; no writes to the lane tree, no shared cache):

  • cargo test -p bitty-terminal --offline --bin bitty -- new_window_denies_a_connection_without_view_manage_before_the_lane new_window_reaches_the_lane_when_the_grant_carries_view_manage window_new_reports_a_scope_denial_as_permission_denied → 3 passed; 0 failed; 0 ignored (1581 filtered out), default build.
  • cargo clippy -p bitty-terminal --all-targets --offline -- -D warnings → clean, after cargo clean -p bitty-terminal in my own target dir so the run was not a cache hit.

Not run: the full workspace suite (CI covers it) and just ci-local (act cannot reach github.com from this host).

Residuals (non-blocking)

  1. ipc_serve.rs:1744-1747 asserts claim() is None after dispatch returns, so it cannot distinguish "never offered to the lane" from "offered and reaped when the request timed out" — under the mutation the request does reach the lane and the slot is cleared before the assertion. The test name's "before the lane" is therefore carried by the category/code assertions, which is exactly what went red. Hardening (optional): run the dispatch on a thread and assert claim() stays None for a short poll window while it is in flight.
  2. Both handler tests are #[cfg(unix)] because they need a UnixStream socketpair for the peer proof. handle_new_window itself is not Unix-gated (it is kept alive for the Windows -D warnings cross-check, ipc_serve.rs:299-301), so the denial branch still has no Windows-build coverage. Reasonable, but the gap is real.
  3. render.rs:1289 compares against EXIT_PERM rather than the literal 7. The value is defined at doctor.rs:82 (pub const EXIT_PERM: i32 = 7;) and the "exit 7" contract is separately pinned (ctl/tests.rs:699, cli.rs:2495), so nothing is uncovered codebase-wide — only this test would survive a change to the constant.
  4. render.rs:1260-1265 restates the handler's ("scope", "ScopeDenied") pair and message as fixture literals. Drift between the handler and this fixture is caught by the ipc_serve assertion, but the two tests are not coupled by construction; sharing one constant (or deriving the fixture from the handler's failure) would make the pair authoritative in one place.

Verified independently, and the requested discipline holds: the new tests fail on the exact code the PR claims they cover, and pass on the real fix.

VERDICT: APPROVE

@Xuepoo
Xuepoo merged commit f39f220 into main Nov 13, 2026
21 checks passed
@Xuepoo
Xuepoo deleted the ctx-1814/window-denial-test branch November 13, 2026 18:18
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