Skip to content

refactor(daemon): split session-state handlers by device-state domain question - #3450

Open
thymikee wants to merge 2 commits into
mainfrom
crap/3424-session-state-split
Open

thymikee wants to merge 2 commits into
mainfrom
crap/3424-session-state-split

Conversation

@thymikee

@thymikee thymikee commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

Summary

Splits the two src/daemon/handlers/session-state.ts functions whose cyclomatic complexity alone kept CRAP ≥ 30 at 92–96% coverage (G2 of #3424, part of #3414):

  • handleSessionStateCommands: 34 → 4 — now a dispatcher over dedicated boot / shutdown handlers.
  • handleAppStateCommand: 30 → 5 — appstate separates target admission (session-appstate.ts), the Apple session response (session-appstate-apple-session.ts), and the device-runtime query (session-appstate-device-runtime.ts).

All resulting functions score below 30 (check:coverage-crap --all and fallow health at max-crap 1 show no production finding ≥ 30 in the new modules; highest is handleShutdownCommand at CC 15). New modules are registered in the R81 daemon-layer manifest; the path-keyed session-state.ts health-baseline entry moves in the final chore(gates) commit (no bulk regeneration).

Behavior is shown unchanged: existing tests were kept green with moves only (renames + one source-slice test retargeted to session-shutdown.ts, one inventory-mocked boot test adapted to the boot file's harness), no assertion edits. Production gross diff is 975 lines (6 files).

Part of #3424.

Validation

  • Tested SHA: ab79e9b8f
  • pnpm check:affected --run: all runnable checks passed (under the machine lock).
  • pnpm check:coverage-crap --all: "no function(s) at or above 30" (partial map including the six handler modules).
  • pnpm check:layering, pnpm check:fallow, pnpm typecheck: green.

View guided diff Turn on auto-fix

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 5.17 MB 5.17 MB +568 B
Package (unpacked) 5.17 MB 5.17 MB +568 B
Package (download) 1.55 MB 1.55 MB +156 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 30.9 ms 29.1 ms -1.7 ms
CLI --help 83.5 ms 84.2 ms +0.7 ms

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 16 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread src/daemon/handlers/__tests__/session-state.test.ts Outdated
… question

handleSessionStateCommands (CC 34) and handleAppStateCommand (CC 30) kept
CRAP at or above 30 from complexity alone. The command group now routes
through a small dispatcher: boot, shutdown, and appstate each answer their
own domain question in a dedicated module, and appstate separates target
admission, the Apple session response, and the device-runtime query.
No behavior change; tests move one-to-one with their subjects.
…ed health baseline entry

The five new handler modules join the daemon-sessions layer in the R81
manifest, and the fallow health baseline drops the session-state.ts
complexity_critical entry its two split functions no longer trigger.
@thymikee

Copy link
Copy Markdown
Member Author

The split of the session-state handlers by domain question looks correct at ab79e9b, and all 19 checks pass. I did not run the test suite, check:coverage-crap or fallow myself, so the CRAP<30 claim rests on your report and CI. No conflicts.

Not blocking, and you can take or leave these: (1) the full diff is +1214/-1023 (2237 gross), mostly test moves, while docs/agents/pull-requests.md and #3424 set a 1,000-line gross budget and the rename-only exemption does not apply, so either move the tests first in a refactor(move) PR (session-boot-shutdown.test.ts to session-boot.test.ts and session-shutdown.test.ts) or state the overage against the gross number in the PR body; (2) the routing test at https://github.com/callstack/agent-device/blob/ab79e9b/src/daemon/handlers/__tests__/session-state.test.ts#L26 only asserts INVALID_ARGS, which all three commands return, so swapping boot and shutdown routing would still pass, and asserting the command name in the message would fix that; (3) the Android serial allowlist sort is now spelled in session-boot.ts:119 and session-appstate-device-runtime.ts:15, and also in session-doctor.ts:276 and session-lifecycle/internal/inventory.ts:373, so one shared helper beside resolveAndroidSerialAllowlist in @agent-device/kernel/device-isolation could serve all four sites.

Could session.ts's existing command table point boot, shutdown and appstate straight at handleBootCommand, handleShutdownCommand and handleAppStateCommand, so session-state.ts (https://github.com/callstack/agent-device/blob/ab79e9b/src/daemon/handlers/session-state.ts#L12) and its routing test can go, since the table is compile-checked and the new switch has a return null branch it cannot reach? Could session-appstate-device-runtime.ts also fold back into session-appstate.ts, since that is its only caller?

The cubic-dev-ai test-assertion thread at #3450 (comment) is fixed at this head, so you can resolve it.

Before merge, please answer the gross-diff budget (2237 vs 1,000) and whether session.ts should own the dispatch. Also, #3424 is labeled on-hold pending coordination with the src/daemon to packages/daemon extraction, and I did not check whether that move collides with these new files, so please confirm that coordination before landing new handler files.

This branch has not been deployed

No deployments
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