Repository navigation
[CTX-1814] test(terminal): cover the new-window authorization denial branch - #3141
Conversation
…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`.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Xuepoo
left a comment
There was a problem hiding this comment.
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 testsatipc_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 testsbegins atrender.rs:1207; the single deleted line is the importuse crate::ctl::{… EXIT_COMPAT …}atrender.rs:1211, replaced atrender.rs:1213to addEXIT_PERMto 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-1736callsbuild_dispatcher(false).dispatch(&context, &request);build_dispatcher(ipc_serve.rs:352) registerscrate::ctl::METHOD_NEW_WINDOW→handle_new_windowatipc_serve.rs:373.- The gate exercised is the production one at
ipc_serve.rs:317, fed bycontext.current_authorization()atipc_serve.rs:307. - The context is built with the public
ServeContext::with_connection_grant(ipc_serve.rs:1720) from a realControlAuthority::open_connectiongrant (ipc_serve.rs:1711-1717); that constructor setsauthority_required: true(bitty-ipc/crates/bitty-ipc/src/devtools/serve.rs:598), andcurrent_authorizationthen 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 atipc_serve.rs:1725-1728. ScopeSet::mcp_default()really lacksview.manage: it inserts onlyterminal.inspect,view.inspect,config.inspect,plugin.inspect(bitty-ipc/crates/bitty-ipc-api/src/scope.rs:293-300), whilecli_default()insertsview.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, aftercargo clean -p bitty-terminalin 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)
ipc_serve.rs:1744-1747assertsclaim()isNoneafterdispatchreturns, 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 thecategory/codeassertions, which is exactly what went red. Hardening (optional): run the dispatch on a thread and assertclaim()staysNonefor a short poll window while it is in flight.- Both handler tests are
#[cfg(unix)]because they need aUnixStreamsocketpair for the peer proof.handle_new_windowitself is not Unix-gated (it is kept alive for the Windows-D warningscross-check,ipc_serve.rs:299-301), so the denial branch still has no Windows-build coverage. Reasonable, but the gap is real. render.rs:1289compares againstEXIT_PERMrather than the literal7. The value is defined atdoctor.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.render.rs:1260-1265restates the handler's("scope", "ScopeDenied")pair and message as fixture literals. Drift between the handler and this fixture is caught by theipc_serveassertion, 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
What
Adds the missing regression coverage for the
bitty ctl window newauthorization denial branch (
bitty#1765, slice 5).The review of #3140 (mutation M4) found that forcing the
view.managegate inhandle_new_window(crates/bitty-terminal/src/ipc_serve.rs) tofalseleftthe entire suite green — nothing exercised the denial, so the gate could be
deleted without a red test.
Three tests, default build, no feature gates:
new_window_denies_a_connection_without_view_manage_before_the_lanecrates/bitty-terminal/src/ipc_serve.rsScopeSet::mcp_default()(view.inspect, noview.manage) is refused withscope/ScopeDenied, the message namesview.manage, and the request never reaches the lane (new_window::claim()stays empty)new_window_reaches_the_lane_when_the_grant_carries_view_managecrates/bitty-terminal/src/ipc_serve.rsScopeSet::cli_default()grant reaches the lane and the app-side claim answersAdmitted→{"requested":true,"windows":2}window_new_reports_a_scope_denial_as_permission_deniedcrates/bitty-terminal/src/ctl/render.rsDenied/ exit 7 forCtlRequest::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 theServeContextis built withServeContext::with_connection_grantfrom a realbitty_ipc::ctl::ControlAuthoritygrant whose session is bound to an attestedsocketpairpeer — the same shape a served connection gets. No test-only scopeshortcut, 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 inipc_serve.rstofalsenew_window_denies_...goes RED:left: "transport", right: "scope"—the request falls through to the lane, which nothing services, and the
handler answers
Unavailableafter the 5 s deadline;new_window_reaches_the_lane_...stays green (the fixture is not whatfails);
Gates (all green,
CARGO_TARGET_DIR=.targets/ctx-1814):cargo fmt --all -- --checkcargo clippy -p bitty-terminal --all-targets --offline -- -D warningscargo test -p bitty-terminal --offline <new test names>— 3/3cargo test -p bitty-terminal --offline --bin bitty ipc_serve::— 21 passedcargo test -p bitty-terminal --offline --bin bitty ctl::— 101 passedcargo test -p bitty-terminal --offline --bin bitty— 1584 passed (full unit suite)cargo +1.85 check -p bitty-terminal --tests --offlineRUSTDOCFLAGS="-D warnings" cargo doc -p bitty-terminal --no-deps --offlinejust ci-localwas not run (act cannot reach github.com from this host).Related #3140
Related #1765
Summary by CodeRabbit