feat(tsmd): idle-quit daemon when locked and silent for 30 minutes#13
Merged
Conversation
Replaces the staleness-detection logic in the npm shim (#12) with a self-contained lifecycle on the daemon. The daemon shuts itself down when its vault is locked AND no client has sent an RPC for the idle window (default 30 minutes). The next `tsm` command spawns a fresh daemon from whatever binary is on disk, so an upgrade is picked up automatically once the user has been idle long enough β no shim heuristics, no postinstall scripts, no path comparisons. The shim-based approach had a real correctness gap: bun's `bun install -g` overwrites the binary in place, so the running tsmd's argv[0] matched the post-upgrade path and the staleness check left it alone. Idle-quit covers the in-place case, the cross-prefix case, and any future install method without a wrapper-side opinion. Tradeoff: between an upgrade and the idle window, clients keep talking to the old code. `tsm daemon stop` is the escape hatch when that matters. Implementation: - New IdleTracker actor (tracks lastActivityAt, isIdle predicate). - JSONRPCHandler bumps the tracker on every request. - Daemon owns the tracker, exposes shouldIdleQuit(), and the existing 15 s tick now also evaluates the idle predicate and posts .tsmdShutdown when it fires. - Daemon.init grew injectable vault/idleTracker/idleQuitSeconds/ tickInterval parameters so tests can drive Daemon.run() to completion in milliseconds. Tests: IdleTrackerTests (6), DaemonIdleQuitTests (5, including a real Daemon.run() round-trip that confirms the timer β notification β shutdown chain), plus two JSONRPCHandler bump tests. Existing 110 tests still pass; total 123. Reverts the shim-side staleness logic from #12 β shim.js is back to resolving the platform package and exec'ing tsm, nothing else.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
idleQuitSeconds(default 1800). The nexttsmcommand spawns a fresh daemon from whatever binary is on disk, so upgrades land automatically once the user has been idle long enough.bun install -g: bun overwrites the binary in place, so the running daemon'sargv[0]matches the post-upgrade path and the path-mismatch heuristic skips it. Idle-quit covers in-place upgrades, cross-prefix upgrades, and any future install method without a wrapper-side opinion.tsm daemon stopis the escape hatch when that matters.Implementation
IdleTrackeractor intsmd/β trackslastActivityAt, exposesbump()andisIdle(idleSeconds:now:).JSONRPCHandlerbumps the tracker on every request (errors and unknown methods included β they're still client activity).Daemonowns the tracker, exposesshouldIdleQuit(), and the existing 15 s tick now evaluates the predicate and posts.tsmdShutdownwhen it fires.Daemon.initgrew injectablevault/idleTracker/idleQuitSeconds/tickIntervalparameters with defaults that preserve production behavior, so tests can driveDaemon.run()to completion in milliseconds.Test plan
swift testβ 123 pass / 0 fail (added 13 tests: 6IdleTrackerTests, 5DaemonIdleQuitTestsincluding a realDaemon.run()round-trip that verifies the full timer β notification β shutdown β socket-unlinked chain, 2 handler bump tests)go test ./...β all packages passtsm daemon stopstill works as the immediate escape hatch