Skip to content

refactor: layer into domain, adapters and transports; fix token refresh race - #2

Merged
subpathdev merged 7 commits into
mainfrom
refactor/layering
Nov 23, 2026
Merged

subpathdev merged 7 commits into
mainfrom
refactor/layering

Conversation

@subpathdev

Copy link
Copy Markdown
Owner

Follow-up to #1, which was merged and released as v0.1.0 before this commit landed on its branch.

What changes

Layering

  • Domain packages account and playback hold the typed values and define ports (interfaces) for what they need.
  • Adapters implement those ports: spotify (Web API + OAuth) and filestore (tokens and invites on the PVC). Their wire and file types are unexported.
  • Transports mcpserver and web depend on small interfaces they define themselves.
  • cmd/spotify-mcp only wires things together. The invite command uses its own account.Inviter instead of a half-built service.

Token refresh race

  • The refresh now runs inside 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).
  • filestore locks with a mutex plus flock on DATA_DIR/.lock, so the separate invite process (kubectl exec) is covered too.
  • A 401 from Spotify invalidates the token (only if it is still the stored one) and retries once.

Checks

  • go test -race ./... passes, and golangci-lint reports 0 issues.
  • Built and ran the image with Podman, hardened as in the chart (read-only root, all caps dropped, non-root). Invite, connect page, both healthz endpoints and MCP initialize work, and the data survives recreating the container.
  • Not yet tested: a real Spotify login and the refresh against Spotify. Both need the deployment.

🤖 Generated with Claude Code

subpathdev and others added 5 commits November 23, 2026 10:45
…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>
Comment thread internal/account/ids.go Resolved
Comment thread internal/account/invite.go Resolved
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>
Comment thread internal/account/entities.go Resolved
Comment thread internal/account/entities.go Resolved
Comment thread internal/account/invite.go Resolved
Comment thread internal/account/invite.go Resolved
Comment thread internal/account/invite.go Resolved
Comment thread internal/account/invite.go Resolved
Comment thread internal/account/profileid.go Resolved
Comment thread internal/filestore/lock_other.go Resolved
…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>
@subpathdev
subpathdev merged commit c3da5a1 into main Nov 23, 2026
5 of 6 checks passed
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