Repository navigation
refactor: layer into domain, adapters and transports; fix token refresh race - #2
Merged
Merged
Conversation
…sh race - internal/account and internal/playback hold the domain: typed values (Profile, InviteCode, URI, DeviceID, Kind), the rules (invites, state + PKCE login, device selection, actionable errors) and the ports they need. - internal/spotify (Web API, OAuth) and internal/filestore (JSON files) implement the ports; wire and file formats are unexported there. - internal/mcpserver and internal/web depend only on small interfaces they define; web no longer touches oauth2 or storage. - internal/config parses env into typed values; main only wires. Race fix: the token refresh now runs inside Tokens.UpdateConnection, which reads, refreshes and writes under one lock (mutex + flock on the data dir). A login during a refresh waits and wins instead of being overwritten by the old account's token, and the invite command's process can no longer lose an invite written concurrently. A 401 invalidates the token and retries once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Profile is now an exported interface (ID() string) backed by an unexported *profile built by ParseProfile, so more profile information can be added later without changing callers. Profiles are compared by ID; filestore rejects nil profiles, re-validates IDs and parses invite profiles from disk; CreateInvite rejects a nil profile. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Profile is now the profile as loaded: ID() plus Account(), the connected SpotifyAccount (renamed from User). A new Profiles loader (exported interface, unexported implementation) builds it from the Tokens port, so it is a fresh snapshot. The key used by config, invites, Tokens and the token source is the new ProfileID type. Status, FinishLogin and Disconnect return the Profile; Identity.User is now Identity.Account. The on-disk format is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ProfileID, InviteCode and InviteHash were named string types that any package could create by conversion, skipping validation. They are now sealed interfaces built only by ParseProfileID, ParseInviteCode and ParseInviteHash (or by the domain itself). Invite is an interface; adapters rebuild stored invites through RestoreInvite, which checks every part. InviteCode exposes the secret only via Secret() and prints as redacted. The on-disk format is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Apply one rule across the repo: anything that carries a rule or a secret is an exported interface with an unexported implementation, created only through a constructor that validates. - account: Token (prints redacted), Connection, SpotifyAccount, Status, Login and Callback are interfaces with New* constructors; Service, Inviter and TokenSource are interfaces. Tokens.UpdateConnection takes fn(Connection) (Connection, error) instead of mutating a struct. - playback: Kind, URI and DeviceID are sealed value interfaces (Parse*, Kind* functions instead of mutable globals); Device, Item (functional options) and State are interfaces; Service is an interface. A nil DeviceID means the active device. - spotify: NewOAuth and NewPlayer return the ports they implement; API is a sealed interface; Scopes() and DefaultEndpoint() are functions, so the scope list can't be widened from outside. - filestore: Open returns a Store interface; stored connections are rebuilt through the account constructors. - config: Config is an interface built only by Load; it prints without the client secret. Kept on purpose: sentinel errors and error types (errors.Is/As), the functional option types, OAuthConfig and the MCP tool input structs. The on-disk format is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
subpathdev
commented
Nov 23, 2026
subpathdev
commented
Nov 23, 2026
Replace the functional options (Option, apply, WithClock) with an explicit Clock interface that NewService and NewTokenSource require; main wires SystemClock(), tests a fake. The domain no longer calls time.Now itself. Remove Inviter: CreateInvite is a use case of account.Service. The invite command builds the same Service; it never calls Spotify, so it still works without credentials. invite.go now holds only the invite types; ProfileID moves to profileid.go. README layout updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
subpathdev
commented
Nov 23, 2026
…terface Address the review on PR #2: - Value types (ProfileID, InviteCode, InviteHash, Kind, URI, DeviceID) are opaque structs with unexported fields, the netip.Addr pattern, instead of sealed interfaces with empty marker methods. Other packages can only make the zero value, which every function rejects (IsZero). The zero DeviceID means the active device. - InviteCode.Hash() replaces the free function hashInvite. - NewToken requires both the access and the refresh token. The OAuth adapter keeps the old refresh token when Spotify sends none, so the token source no longer patches it in. Token's String() (redacted) is part of the interface. - filestore: the cross-process lock is an unexported locker interface with one implementation per platform (flock, noLock). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 23, 2026
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.
Follow-up to #1, which was merged and released as v0.1.0 before this commit landed on its branch.
What changes
Layering
accountandplaybackhold the typed values and define ports (interfaces) for what they need.spotify(Web API + OAuth) andfilestore(tokens and invites on the PVC). Their wire and file types are unexported.mcpserverandwebdepend on small interfaces they define themselves.cmd/spotify-mcponly wires things together. Theinvitecommand uses its ownaccount.Inviterinstead of a half-built service.Token refresh race
Tokens.UpdateConnection, so read, refresh and write happen under one lock. A login that finishes during a refresh is no longer overwritten by the refreshed old token (TestLoginDuringRefreshWins; it fails if the lock does not cover the refresh).filestorelocks with a mutex plusflockonDATA_DIR/.lock, so the separateinviteprocess (kubectl exec) is covered too.Checks
go test -race ./...passes, and golangci-lint reports 0 issues.initializework, and the data survives recreating the container.🤖 Generated with Claude Code