Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ The Architecture Map below describes pine and the shared packages.
| `navigation.go` | Screen routing via back stack, propagateSize (sets all 8 screen models), initScreen, updateScreen dispatch |
| `render.go` | View composition: header + error banner + screen + hints + player footer + chapter overlay modal + confirmation overlay |
| `bookmarks.go` | Bookmark CRUD, chapter next/prev, seek logic (in-track vs cross-track), fetchBookmarksCmd/fetchEpisodesCmd/fetchBookDetailCmd |
| `metadata.go` | Metadata editor save flow: server PATCH, cache eviction, detail refresh |
| `metadata.go` | Metadata editor save flow: server PATCH, cache eviction, search snapshot prewarm, detail refresh |
| `messages.go` | Screen enum, PlaySessionMsg, PlaySessionData, QueueEntry, all root-level message types |
| `keymap.go` | Global keybindings (quit, back, help, chapter overlay, queue, sleep, command palette). Key names for the player model. |
| `help.go` | Help overlay groups for pine |
| `sessions.go` | The `R` key and the palette entries for listening sessions: the target of the detail screen, the item that plays, or all items. The jump from a session: a seek, or a fetch of the item and `playItemAt`. |
| `queue.go` | Queue entry enqueue/dequeue with dedup, clone helpers |
| `mpris_state.go` | MPRIS bridge message conversion between player state and D-Bus adapter |
| `mpris_state.go` | `MprisState`: the playback state that the MPRIS server reads through `mpris.ModelAccessor` |

### Player (internal/player/, shared)

| File | Responsibility |
|------|---------------|
| `mpv.go` | Mpv struct: Launch (creates a libmpv handle and loads the file), GetPosition/GetDuration/GetPaused/GetIdle, Seek, SetSpeed/SetVolume, Quit. `ErrClosed` and `ErrPropertyUnavailable`. Writes the warnings and errors of mpv to the app log, without URL queries. Runtime directory for the media cache. |
| `mpv.go` | Mpv struct: Launch (creates a libmpv handle and loads the file), GetPosition/GetDuration/GetPaused/GetIdle, Seek, SetSpeed/SetVolume, Quit. `ErrClosed` and `ErrPropertyUnavailable`. Writes the warnings and errors of mpv to the app log, without the query, the user, or the password of a URL. Runtime directory for the media cache. |
| `libmpv.go`, `libmpv_unix.go`, `libmpv_windows.go` | Binding to libmpv through purego, with no cgo. The library loads on the first Launch. Library search order for each system. |
| `commands.go` | tea.Cmd factories: TickCmd (500ms poll), TickAfterCmd (poll after an interval), LaunchCmd, SetPauseCmd, SetSpeedCmd, SetVolumeCmd, QuitCmd. A command for the pause state, the speed, or the volume returns CommandErrMsg if the player does not accept the value. WatchEvents returns PlayerEndMsg at the end of a playback. |
| `model.go` | Player sub-model: Position/Duration/Speed/Volume state, key handling (play/pause, seek, speed, volume) |
Expand All @@ -89,42 +89,42 @@ The Architecture Map below describes pine and the shared packages.
| `serieslist` | model.go | Paginated series browser (50/page), infinite scroll. |
| `series` | model.go | Books within a series, sorted by sequence number. Highlights current item, prefixes finished books with ✓. |
| `sessions` | model.go | Listening sessions of a book, a podcast episode, or all items (50 newest). Each row shows the end position, the device, and the time of the last sync. Enter sends `PlayFromMsg`. |
| `search` | cache.go | No longer a standalone screen. Fuzzy-search cache used by the command palette and shared with the library view. Builds full library snapshots (all items fetched, normalized, tokenized) with background pre-warm. |
| `search` | cache.go | No longer a standalone screen. Fuzzy-search cache used by the command palette and shared with the library view. Builds full library snapshots (all items fetched, normalized, tokenized) with background pre-warm. `Search` answers only from a ready snapshot in memory or in SQLite. On a miss, it returns no results and builds the snapshot in the background, so the palette does not wait for the network in `Update`. When the palette opens and the snapshot of its library is not ready, `openGlobalPalette` returns `PrebuildCmd`. On `CacheReadyMsg` for that library, `Palette.Refresh` applies the query again and shows the results. An invalidation removes the builds in flight, so a build that started before a change stores nothing. |

### Cache (internal/cache/)

| File | Responsibility |
|------|---------------|
| `client.go` | Cached wrapper around `abs.Client`: serves cache hits instantly, falls back to API, and triggers background rebuilds. Each cached method calls `cached(c, key, ttl, fetch)`, which returns the data of the server. A cache read or write error goes to the log and does not fail the request. |
| `keys.go` | The names of all cache keys. Code outside the package uses `ItemKey`, `PersonalizedKey`, `SeriesContentsKey`, `BookmarksKey`, and `SearchSnapshotKey`, not key strings. |
| `client.go` | Cached wrapper around `abs.Client`: serves cache hits instantly. On a miss, it fetches from the API in the command of the caller and stores the result. Each cached method calls `cached(c, key, ttl, fetch)`, which returns the data of the server. A cache read or write error goes to the log and does not fail the request. A fetch that started before an invalidation does not store its value (`generation`, under `mu`). `invalidate` calls `OnInvalidate` also when the store is nil. |
| `keys.go` | The names of all cache keys. Code outside the package uses `ItemKey`, `PersonalizedKey`, `SeriesContentsKey`, `BookmarksKey`, and `SearchSnapshotKey`, not key strings. A key has no account, so a login (`LoginSuccessMsg`) clears the persistent cache with `ClearAll`. |
| `store.go` | SQLite-backed cache persistence and `ClearAll`. Each key in the database has the prefix `keyVersion` (`v1-`). Raise it when the type of a cached value changes. |

### MPRIS (internal/mpris/, shared)

| File | Responsibility |
|------|---------------|
| `adapter.go` | D-Bus MPRIS adapter exposing playback status, metadata, and seek controls. |
| `bridge.go` | Bubbletea message bridge between the MPRIS adapter and root model. |
| `adapter.go` | `RootAdapter` (`CanQuit` and `CanRaise` are false), the playback status and the track metadata from the app state, `trackPath`, and `PlayerActions`. |
| `bridge.go` | Bubbletea message bridge between the MPRIS server and root model. A rate of 0 pauses. A rate is limited to `MinimumRate`..`MaximumRate`, and a volume to 0..1.5. `SetPosition` ignores a position outside the track. With skip seek, `CanGoNext` and `CanGoPrevious` are `HasActiveItem` and `CanSeek`. |
| `accessor.go` | Read-only MPRIS property accessor. |
| `server.go` | MPRIS server on `godbus/dbus`: the root and player interfaces, the D-Bus properties interface, introspection, and the change signals. |
| `server.go` | MPRIS server on `godbus/dbus`: the root and player interfaces, the D-Bus properties interface, introspection, and the change signals. `NewServer(name, root, accessor, actions)` reads the player properties from `ModelAccessor` and calls `PlayerActions`. It rejects a Rate or a Volume value that is not finite. `CanPlay` and `CanPause` are `HasActiveItem`, and `StatusChanged` and `PlaybackChanged` send them. `StatusChanged` and `PlaybackChanged` also send `CanGoNext` and `CanGoPrevious`. |

The server is written from the MPRIS 2.2 specification. The Go MPRIS server library that exists is GPL-3.0, and a GPL module makes the release binaries GPL. Do not add a module with a copyleft license.

### API (internal/abs/)

| File | Responsibility |
|------|---------------|
| `client.go` | HTTP client with 10s timeout, 50MB response limit, auth header, request logging |
| `client.go` | HTTP client with 10s timeout, 50MB response limit, auth header, request logging. `NewClient` removes the spaces and the trailing slashes of the server address. `HTTPStatusError` keeps at most 500 bytes of the response body. A log line or a request error shows `SessionTag` (a short hash) in place of a play session ID. |
| `types.go` | All ABS data types. Custom UnmarshalJSON for MediaMetadata.Series (handles both object and array). |
| `libraries.go` | GetLibraries, GetPersonalized, GetLibraryItems (paginated), GetLibraryItemsBatch (10 concurrent), GetLibrarySeries, GetSeriesContents, FilterAudioLibraries (sample-based audio detection) |
| `playback.go` | StartPlaySession, StartEpisodePlaySession, SyncSession, CloseSession, CloseSessionNoSync (sends no position) |
| `progress.go`, `me.go` | UpdateProgress, UpdatePosition (no `isFinished` key), GetMediaProgress (ABS gives no bookmarks with the progress), UpdateEpisodeProgress, GetMe (user + all media progress) |
| `progress.go`, `me.go` | UpdateProgress, UpdatePosition (no `isFinished` key), GetMediaProgress (ABS gives no bookmarks with the progress), GetMe (user + all media progress). UpdateProgress and UpdatePosition take an episode ID for a podcast episode. |
| `sessions.go` | GetListeningSessions (`GET /api/me/listening-sessions`), GetItemListeningSessions (`GET /api/me/item/listening-sessions/{id}[/{episodeId}]`) |
| `bookmarks.go` | CreateBookmark, UpdateBookmark, DeleteBookmark, GetBookmarks (`GET /api/me/bookmarks/{id}`, or `GET /api/me` for ABS before 2.36.0) |

### Data (internal/db/)

SQLite store (WAL mode) for accounts and listening sessions. `busy_timeout` is 5000 ms, so a write waits while a different connection writes. Single "last" session row for crash recovery.
SQLite store (WAL mode) for accounts and listening sessions. `busy_timeout` is 5000 ms, so a write waits while a different connection writes. Single "last" session row for crash recovery. It keeps only the item ID and the episode ID. The restore gets the position from the server. The accounts table keeps only the account of the last login, because `SaveAccount` deletes the other accounts.

### Config (internal/config/)

Expand All @@ -141,7 +141,7 @@ Styles from theme config. Format helpers (timestamps, durations). `RunProgram` r
- All async work in `tea.Cmd` closures captures needed values by copy, never model references
- `tea.Batch` for concurrent async operations
- Skeleton loading with generation-gated reveal delays (home: 500ms, library: 150ms)
- Cache-first data loading through `internal/cache.Client`: home and library views hit the persistent cache first; misses trigger a background rebuild
- Cache-first data loading through `internal/cache.Client`: home and library views hit the persistent cache first. A miss fetches from the server in the command of the caller and stores the result.
- `q` quits, and `esc`/left goes back (never quits). `h`/`l` seek only, arrows navigate only. These keys have no global function while a text input or a dialog gets the keys (`capturesKeys`).
- A quit (`q` or ctrl+c) returns only `tea.Quit` and keeps the session in the model. `Cleanup` sends the last position, and `main` waits for it. Bubble Tea does not wait for a command at a quit, so a quit must not send the position in a command.
- The `main` of pine and the `main` of spruce run the program with `ui.RunProgram`, which quits the program at SIGHUP, the close of the terminal, so that `Cleanup` runs. Bubble Tea quits the program at SIGTERM.
Expand All @@ -153,6 +153,9 @@ Styles from theme config. Format helpers (timestamps, durations). `RunProgram` r
- pine finds the end of a track through `errors.Is(err, player.ErrClosed)` on a position tick. spruce uses `PlayerEndMsg`, and a tick with `ErrClosed` can arrive before it. Do not match the text of a player error.
- Enter on a bookmark (`handleSeekToBookmark`) seeks the player only if the book or the episode of the bookmark plays. If not, it stops the playback and starts that book or that episode at the time of the bookmark.
- A bookmark of ABS has no episode ID. `handleAddBookmark` puts the episode title and `episodeTitleSeparator` before the title of a podcast bookmark, and `bookmarkEpisode` finds the episode from that title. The bookmarks on a server have that format, so do not change it.
- The copies of the root model share the MPRIS bridge through `mprisLink`, because `main` calls `SetProgram` after `tea.NewProgram` copies the model. Reach the bridge through `mprisBridge()`.
- pine writes the MPRIS position to `MprisState` on each position tick. It sends the MPRIS signal Seeked only for a seek (`seekToBookGlobalPosition`), for a playback that starts at a position after 0, and for a restart of the same item (`restartPlaybackAt`) at each position, because the track ID does not change. It does not send Seeked as a periodic position report.
- `isUnauthorized` finds a 401 with `abs.IsHTTPStatus(err, http.StatusUnauthorized)`. A test that needs a 401 uses `&abs.HTTPStatusError{StatusCode: http.StatusUnauthorized}`, not an error text.

## Testing

Expand All @@ -163,13 +166,14 @@ Styles from theme config. Format helpers (timestamps, durations). `RunProgram` r
- `internal/app/model_test.go`, `playback_test.go`, `bookmarks_test.go`, `render_test.go` for app-level tests
- `internal/ui/program_unix_test.go` starts the test program again as a child process, sends SIGHUP to it, and checks that it gets to the place where a `main` calls `Cleanup`. The child runs its program with `ui.RunProgram`, as each `main` does. It does not run on Windows.
- `internal/cache/*_test.go` for cache store and helper behavior
- `internal/mpris/mpris_test.go` for the D-Bus adapter and bridge
- `internal/mpris/mpris_test.go` for `RootAdapter` and the server properties, and `bridge_test.go` for the bridge
- `internal/mpris/server_linux_test.go` starts the MPRIS server on a private session bus and checks it as an MPRIS client. It skips if `dbus-daemon` is absent. With `PINE_REQUIRE_DBUS=1` it fails instead. CI and `scripts/release.sh` set that variable.
- `internal/app/mpris_linux_test.go` starts the MPRIS server of the pine model on a private session bus and checks the Seeked signals. It skips if `dbus-daemon` is absent. With `PINE_REQUIRE_DBUS=1` it fails instead.
- `internal/screens/metadataedit/model_test.go` for the metadata editor
- Screen models have individual `*_test.go` files
- `internal/player/libmpv_test.go`, `mpv_test.go`, and `group_test.go` play audio through the real libmpv with `ao=null`. They skip if libmpv is absent. With `PINE_REQUIRE_LIBMPV=1` they fail instead. `scripts/release.sh` sets that variable.
- `.github/workflows/ci.yml` runs `go vet` and the tests on Ubuntu 22.04, 24.04, and 26.04, on macOS (Apple Silicon and Intel), and on Windows. It runs `golangci-lint` v2.14.0 on Ubuntu 24.04, and the job fails on each issue. A test that asserts a Unix file mode must not make that assertion on Windows.

## Logging

Logs to `~/.config/pine/pine.log` (slog-based, auto-rotated at 5MB). Token/credentials are NOT logged. spruce logs to `spruce.log` in its config directory.
Logs to `~/.config/pine/pine.log` (slog-based). At start, `Init` renames `<name>.log` to `<name>.log.old` when the file is over 5 MB, and only while it holds the lock of the file. An instance that cannot lock the log writes to `<name>-<pid>.log`. Token/credentials are NOT logged. A play session ID gives the audio of the session with no login, so pine logs only `abs.SessionTag(id)` in the field `session`, never the full ID. spruce logs to `spruce.log` in its config directory.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ error = "#e67e80"
muted = "#859289"
selected = "#475258"
border = "#4f585e"
warning = "#dbbc7f"
info = "#7fbbb3"
```

`player.speed` must be from 0.1 to 100, and `player.seek_seconds` must be more than 0. For a different value, pine stops at startup with an error that names the key.

Pine creates a stable playback identity on first run:

```toml
Expand All @@ -156,6 +157,10 @@ display-only and can be changed without changing `device_id`.
Audiobookshelf displays this as `Pine <device_name>` (without Pine's default
`(Pine)` suffix), for example `Pine my-host`.

Pine writes a generated `device_name` and `device_id` to `config.toml`. A
read-only `config.toml`, for example a home-manager symlink, must set the two
keys, or Pine cannot start.

Pine sends its Go module build version as `clientVersion` to Audiobookshelf.
Local development builds report `dev`.

Expand Down
18 changes: 13 additions & 5 deletions docs/spruce/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ and the file uses mode `0600`; local file permissions are the security boundary.
See [Auth token storage](../design/spruce/auth-token-storage.md) for the decision and
operational details.

Logs are written to the same config directory as `spruce.log` and rotated after
5 MB.
Logs are written to the same config directory as `spruce.log` and rotated at
start when the file is over 5 MB.

## Common keybindings

Expand All @@ -94,11 +94,12 @@ Keys can be context-specific. Press `?` in Spruce for the complete active help.
| `space` | Play or pause |
| `h` / `l` | Seek backward or forward |
| `n` / `>` | Next track |
| `N` / `p` / `<` | Previous track |
| `N` / `p` / `<` | Previous track in the first 3 seconds of a track. Later, or at the first track, play the track again from the start. |
| `a` / `A` | Add selected track or collection to the queue |
| `S` | Shuffle selected collection or cycle the sleep timer |
| `S` | On the library and playlists screens: add the selected album or playlist to the queue in shuffled order |
| `S` | On the queue screen during playback: cycle the sleep timer. The command palette also sets it. |
| `s` | Shuffle the current queue |
| `r` / `R` | Repeat current track or queue |
| `r` / `R` | Repeat a track or the queue. On the queue screen, `r` repeats the selected track. On the other screens, it repeats the track that plays. |
| `b` | Toggle subliminal mode from the queue |
| `+` / `-` | Increase or decrease playback speed |
| `]` / `[` | Increase or decrease volume |
Expand All @@ -109,6 +110,8 @@ Keys can be context-specific. Press `?` in Spruce for the complete active help.
| `ctrl+p` | Open the command palette |
| `?` | Toggle help |

With queue repeat, the next track key at the last track plays the first track.

## Repeat cache and subliminal mode

When track or queue repeat is active, Spruce downloads repeated media into a
Expand Down Expand Up @@ -151,6 +154,11 @@ sleep_timer = "S"
back = "esc"
```

On the library and playlists screens, `S` adds the selection to the queue in
shuffled order. With `sleep_timer = "S"`, the sleep timer key does not work on
these two screens. To use the sleep timer key on each screen, set a different
key.

The generated `[theme]` section contains the editable Everforest color values.
See [Theme configuration](../design/spruce/theme-configuration.md) for the supported
scope.
Expand Down
Loading
Loading