Phase 2: Architecture & maintainability improvements#4
Merged
Conversation
Replaces sessions, ptys, cmds, doneChans, frameDetectors, and responders maps with a single handles map[string]*sessionHandle. Eliminates the risk of map synchronization drift across handlers.
Eliminates the intermediate Session struct that duplicated SessionMeta fields. All session state now lives directly in sessionHandle, removing one layer of indirection and a source of state drift.
Deduplicates the two near-identical settle polling loops in handleSnapshot into a shared helper function.
Extracts the duplicated "send input, wait for output, return new content" pattern from cmd/exec.go and mcp/tools.go into a single Client.Exec method. Callers now use daemon.ExecOptions instead of manually orchestrating Read/Send/ForOutput. Also refactors MCP ToolRegistry to use registration-based pattern: adding a tool is now a single register() call instead of coordinated edits to List() schema, Call() switch, and handler method.
Client sends ProtocolVersion=1 with every request. Daemon rejects mismatched versions with a clear error message telling the user to restart the daemon. Version 0 (omitted) is accepted for backward compatibility during rollout.
Daemon previously ran with stdout/stderr discarded, making crash debugging impossible. Now supports --log-file to capture logs and redirect stderr. Added panic recovery in handleConn and captureOutput goroutines to log stack traces instead of crashing silently. Also fixes unparam lint: waitForSettle return value was unused.
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.
Enhancement
The daemon server manages per-session state across 6 parallel maps that must stay in sync, duplicates session metadata between
SessionandSessionMetastructs, and requires 3 coordinated edits to add a new MCP tool. This PR reduces the internal bug surface area and makes the codebase easier to extend.Solution
All per-session state now lives in a single
sessionHandlestruct stored in one map, eliminating the class of bugs where one map is updated but another is forgotten. The intermediateSessionstruct is removed entirely, with its fields promoted intosessionHandle. AClient.Execmethod encapsulates the send+wait+read pattern, and the MCP tool registry uses a registration-based pattern where each tool is a singleregister()call.Changes
Daemon server (
internal/daemon/server.go):sessions,ptys,cmds,doneChans,frameDetectors,respondersmaps into singlehandles map[string]*sessionHandleSessionstruct, promote fields (name,pid,command,state,createdAt,stoppedAt) directly intosessionHandlewaitForSettlehelper fromhandleSnapshot, replacing two near-identical settle polling loopsClient (
internal/daemon/client.go):Client.Execmethod withExecOptions/ExecResulttypes, consolidating the send+wait+read sequenceProtocolVersion=1) on every requestProtocol (
internal/daemon/constants.go,server.go):MCP tools (
internal/mcp/tools.go):List()schema +Call()switch withregister()patternList()andCall()are now generic iterators over registered entriesCLI (
cmd/exec.go):runExecdelegates toclient.Exec()instead of manual orchestration