Archive milestone artifacts: - milestones/v1.0-ROADMAP.md (full roadmap archive) - milestones/v1.0-REQUIREMENTS.md (26/26 requirements complete) - milestones/v1.0-phases/ (8 phase directories with plans, summaries, verifications) Updated: - PROJECT.md: full evolution review, all consolidation requirements validated - ROADMAP.md: collapsed to milestone summary with archive link - STATE.md: reset for next milestone - MILESTONES.md: created with stats and accomplishments - RETROSPECTIVE.md: created with lessons learned Deleted: - REQUIREMENTS.md (archived, fresh for next milestone) 8 phases, 17 plans, 34 tasks, 84 tests added, 6 days
13 KiB
phase, plan, type, wave, depends_on, files_modified, autonomous, requirements, must_haves
| phase | plan | type | wave | depends_on | files_modified | autonomous | requirements | must_haves | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 07-backend-performance | 01 | execute | 1 |
|
true |
|
|
Purpose: Single-track queue mutations (add, remove) currently rewrite the entire queue_tracks table (DELETE ALL + batch INSERT). This is O(n) where n is the queue length. For a 500-track queue, adding one track rewrites 501 rows. These operations should use incremental INSERT/DELETE with position shifts, making them O(1) for the actual mutation plus O(k) for position shifts (where k is the number of tracks after the mutation point). SetQueue Phase 2 currently re-resolves ALL file paths even though Phase 1 already resolved up to 50 of them — passing the Phase 1 results as an exclusion set eliminates redundant database work.
Output: Modified persistence.go with incremental persist helpers, modified queue.go with updated mutation methods and Phase 2 dedup.
<execution_context> @/home/caleb/.config/opencode/get-shit-done/workflows/execute-plan.md @/home/caleb/.config/opencode/get-shit-done/templates/summary.md </execution_context>
@.planning/PROJECT.md @.planning/ROADMAP.md @.planning/STATE.md @backend/queue/persistence.go @backend/queue/queue.go @backend/queue/emit.go @backend/database/sql/queries/queue.sql @backend/database/sql/sqlcgen/queue.sql.goFrom backend/queue/queue.go:
type Track struct {
ID int64 `json:"id"`
AudioFileID int64 `json:"audioFileId"`
FilePath string `json:"filePath"`
Position int64 `json:"position"`
Title string `json:"title"`
Artist string `json:"artist"`
}
type trackMeta struct {
AudioFileID int64
FilePath string
Title string
Artist string
}
func (m trackMeta) toTrack(position int64) Track
// commitMutation persists the current queue state after a mutation.
// When reindex is true, track positions are renumbered first.
// The caller must hold q.mu.
func (q *Queue) commitMutation(reindex bool)
// reindexPositions updates the Position field of all tracks to match slice index.
func (q *Queue) reindexPositions()
From backend/database/sql/sqlcgen/queue.sql.go (existing sqlc queries available):
func (q *Queries) InsertQueueTrack(ctx context.Context, arg InsertQueueTrackParams) (QueueTrack, error)
func (q *Queries) RemoveQueueTrackByPosition(ctx context.Context, position int64) error
func (q *Queries) ShiftQueuePositionsDown(ctx context.Context, position int64) error // position = position - 1 WHERE position > ?
func (q *Queries) ShiftQueuePositionsUp(ctx context.Context, position int64) error // position = position + 1 WHERE position >= ?
func (q *Queries) ClearQueueTracks(ctx context.Context) error
-
persistAddTrack(track Track)— Inserts a single track at positiontrack.PositionusingInsertQueueTrack. No position shifting needed because AddTrack always appends to the end. -
persistAddTracks(tracks []Track)— Inserts multiple tracks at consecutive positions at the end of the queue. Use the sameInsertQueueTrackin a loop (these are appends, so no position shifting needed). Wrap in a transaction for atomicity (useq.db.BeginTx(),q.db.Queries.WithTx(tx)). -
persistInsertTracks(tracks []Track, insertPos int)— For insert-at-position operations. In a transaction: (a) CallShiftQueuePositionsUpwithinsertPosto make room — but noteShiftQueuePositionsUpshifts by 1, so for N tracks, we need to shift by N. Since the sqlc query only shifts by 1, use a hand-crafted UPDATE:UPDATE queue_tracks SET position = position + ? WHERE position >= ?with args (len(tracks), insertPos). Add a// SAFETY:comment explaining why. (b) Insert each track usingInsertQueueTrackwith positionsinsertPos,insertPos+1, ...,insertPos+N-1. -
persistRemoveTrack(position int)— In a transaction: (a) CallRemoveQueueTrackByPosition(position). (b) CallShiftQueuePositionsDown(position)to close the gap. -
persistRemoveTracks(positions []int)— For multi-track removal. Since multiple position shifts interact, use the fullpersistTracks()rewrite for simplicity (the bulk path is acceptable for multi-remove — the user decision specified bulk operations keep the full rewrite). Just callpersistTracks()directly.
In queue.go, update these methods to use incremental persistence instead of commitMutation:
-
AddTrack— Replaceq.commitMutation(false)with:q.persistAddTrack(track)thenq.persistState(). No reindex needed (appending at end, position is already correct). -
AddTracks— Replaceq.commitMutation(false)with:q.persistAddTracks(newTracks)thenq.persistState(). No reindex needed. -
InsertNext— Replaceq.commitMutation(true)with: callq.reindexPositions()first, thenq.persistInsertTracks([]Track{track}, insertPos)thenq.persistState(). The reindex ensures in-memory positions are correct for subsequent operations. -
InsertNextTracks— Replaceq.commitMutation(true)with: callq.reindexPositions()first, thenq.persistInsertTracks(newTracks, insertPos)thenq.persistState(). -
InsertTracksAt— Replaceq.commitMutation(true)with: callq.reindexPositions()first, thenq.persistInsertTracks(newTracks, index)thenq.persistState(). -
RemoveTrack— Replaceq.commitMutation(true)with: callq.persistRemoveTrack(position)thenq.reindexPositions()thenq.persistState(). -
RemoveTracks— Replaceq.commitMutation(true)with: callq.reindexPositions()thenq.persistTracks()(full rewrite, per user decision for bulk ops) thenq.persistState().
Keep commitMutation for: Clear, MoveQueueTracks, resolveRemainingTracks — bulk operations that still do full rewrites per user decision.
For the hand-crafted SQL in persistInsertTracks: Use tx.ExecContext(q.db.Ctx, "UPDATE queue_tracks SET position = position + ? WHERE position >= ?", count, insertPos) with a // SAFETY: Multi-row position shift by variable N unsupported by sqlc (shift queries only shift by 1). Bind variables match args; no string interpolation. comment.
Important: Shuffle order regeneration was handled by commitMutation. For all the methods that previously called commitMutation with reindex=true, generateShuffleOrder() was also called if shuffleMode was active. Continue this behavior: after the incremental persist, check q.shuffleMode and call q.generateShuffleOrder() if true. For methods that called commitMutation(false) (AddTrack, AddTracks), shuffle order regeneration was also done if active — preserve this.
Verification approach: Existing persistence roundtrip tests in persistence_test.go exercise SaveState/RestoreState which uses persistTracks (full rewrite). The incremental paths are verified by: (1) the existing queue_test.go tests that call AddTrack/RemoveTrack/InsertNext etc. with a real DB, and (2) adding a focused test.
cd backend && go build ./... && go test ./queue/... -race -count=1
- AddTrack/AddTracks use persistAddTrack/persistAddTracks (no full table rewrite)
- RemoveTrack uses persistRemoveTrack (single DELETE + position shift, no full table rewrite)
- InsertNext/InsertNextTracks/InsertTracksAt use persistInsertTracks (position shift + INSERT, no full table rewrite)
- RemoveTracks uses full persistTracks rewrite (acceptable for bulk operations)
- MoveQueueTracks, Clear, SetQueue still use commitMutation/persistTracks (unchanged bulk behavior)
- All existing tests pass with -race
-
Change
resolveRemainingTrackssignature to accept the Phase 1 result map:func (q *Queue) resolveRemainingTracks( gen int64, filePaths []string, playingPath string, phase1Meta map[string]trackMeta, // NEW: already-resolved from Phase 1 ) -
Inside
resolveRemainingTracks, build the exclusion set fromphase1Metakeys. FilterfilePathsto get only the paths NOT inphase1Metabefore callinglookupTrackMetaBatch:// Exclude paths already resolved in Phase 1. var unresolvedPaths []string for _, fp := range filePaths { if _, alreadyResolved := phase1Meta[fp]; !alreadyResolved { unresolvedPaths = append(unresolvedPaths, fp) } } // Only look up paths that Phase 1 didn't cover. remainingMeta := q.lookupTrackMetaBatch(unresolvedPaths) // Merge Phase 1 results into the lookup. for k, v := range phase1Meta { remainingMeta[k] = v } -
The rest of the method (building tracks from
allMeta, findingplayingPath, callingcommitMutation) usesremainingMetainstead ofallMeta. Rename the variable for clarity. -
Update the call site in
SetQueue: PassbatchMeta(the Phase 1 result) toresolveRemainingTracks:go q.resolveRemainingTracks(gen, filePaths, playingPath, batchMeta)
Keep initialBatchSize at 50 — no changes to the Phase 1 window size (per user decision).
Result: For a 1000-track SetQueue where Phase 1 resolves 50, Phase 2 now queries only 950 paths instead of all 1000. The 50 already-resolved paths are merged from the Phase 1 map. cd backend && go build ./... && go test ./queue/... -race -count=1 - resolveRemainingTracks accepts phase1Meta parameter - Phase 2 filters out already-resolved paths before calling lookupTrackMetaBatch - Phase 1 results are merged into Phase 2 results - SetQueue call site passes batchMeta to resolveRemainingTracks - initialBatchSize remains at 50 - All existing tests pass with -race
```bash # All queue tests pass with race detector cd backend && go test ./queue/... -race -count=1 -vBuild succeeds
cd backend && go build ./...
Lint passes
make lint
</verification>
<success_criteria>
- Single-track add/remove uses incremental INSERT/DELETE (not full table rewrite)
- Insert-at-position uses position shift + INSERT (not full table rewrite)
- SetQueue Phase 2 only queries unreolved paths (not all paths)
- All existing queue tests pass with -race
- No linting errors
</success_criteria>
<output>
After completion, create `.planning/phases/07-backend-performance/07-01-SUMMARY.md`
</output>