Skip to content

feat: LLM-guided discovery mode for AI auto-playlist - #96

Open
ErfanBagheri404 wants to merge 5 commits into
mainfrom
probe/ai-mix-llm
Open

ErfanBagheri404 wants to merge 5 commits into
mainfrom
probe/ai-mix-llm

Conversation

@ErfanBagheri404

@ErfanBagheri404 ErfanBagheri404 commented Oct 10, 2026 •

Copy link
Copy Markdown
Owner

Extends #89 / #49: the auto-playlist only resampled tracks the user already knew (liked + history pool). This adds a discovery mode that can generate genuinely new tracks, with zero API keys.

How it works

  1. `modules/aiMixGenerator.ts` asks the free keyless Pollinations.ai endpoint (OpenAI-compatible, anonymous) for 3-6 search queries, seeded with the user's top artists/tracks from `listeningStats`.
  2. Queries feed the existing `searchMixed` -> deterministic scorer -> pinned `@ai_mix` playlist (unchanged).
  3. Fallback chain on failure: keyless model alias -> canonical model id -> on-device `generateAiMixPlaylist`. Null only when there is no listening history at all.

The LLM only ever writes search strings; track selection stays deterministic on-device. No credentials, no Authorization header, anonymous lane only.

Verified

  • `node tests/ai-mix-regression.cjs` — 27/27 (incl. keyless-source contract + 402 chain-rescue)
  • `node tests/ai-mix-discovery-e2e.cjs` — live LLM run, 6 queries -> 30 tracks
  • `npm run typecheck` clean
  • Release build installed on emulator: sparkles tap -> `[aiMix] discovery: 6 queries -> 105 candidates -> 30 tracks` -> "AI Mix / 30 songs" pinned in Library

Summary by Sourcery

Enable keyless LLM-guided discovery for AI auto-playlists while retaining deterministic track selection and robust on-device fallbacks.

New Features:

  • Add an LLM-guided discovery mode that generates new music search queries from the user's listening history and builds a deterministic AI Mix from the results.

Bug Fixes:

  • Fall back to the existing on-device AI Mix when the discovery service, query resolution, or candidate pool cannot produce a usable mix.

Enhancements:

  • Centralize AI Mix persistence while preserving its stable pinned playlist and existing metadata.
  • Keep discovery requests anonymous and limit the LLM to producing search queries rather than selecting tracks.

Tests:

  • Extend regression coverage for discovery generation, strict query parsing, fallback behavior, anonymous endpoint usage, model-chain recovery, and no-history handling.
  • Add a live end-to-end check for the keyless discovery flow with an offline-deterministic search stage.

#49)

- modules/aiMixPlaylist.ts: seed = most recent play, pool = liked + recent + replay top tracks deduped, scoring via buildSmartQueue + loadPlayCounts
- stable AI_MIX_ID so regenerating replaces the mix; user playlists untouched
- null when history is too thin — nothing stored, the alert says so
- FullPlayerModal: sparkle button in the track row, lazy import, Alert for both outcomes
- locale: player.aiMixTitle/aiMixReady/aiMixEmpty in en+fa
- regression: tests/ai-mix-regression.cjs 12/12, sabotage-verified
Two real defects in the AI Mix generator:

- candidatePool took a third list built from the play-count summary, but
  those ids all come from liked/recent, so the dedupe dropped every one
  of them. The resolution loop is deleted; the counts are still passed
  to buildSmartQueue for scoring.

- Regenerate read loadPlaylists() twice and routed the create path
  through addPlaylist, which re-reads the list internally. A playlist
  the user made between the first and second read was silently lost.
  Read once, write once with savePlaylists.

Tests: 3 new regressions covering read/write counts, survival of an
externally added playlist, and the absence of the top-tracks list.
15/15 pass; 4 sabotage mutations all caught.
…evice fallback

- aiMixGenerator: Pollinations text API (anonymous, no key) turns listening
  history into search queries; app search resolves them; deterministic
  scoring picks tracks. openai -> mistral fallback, then on-device mix.
- aiMixPlaylist: shared persistAiMix, exported pickSeed
- FullPlayerModal button now calls generateAiMixWithDiscovery
- 12 new regression checks (26/26), e2e harness for the live endpoint
Pollinations retired the 'mistral' alias (HTTP 404) and now serves a single
anonymous model, 'openai-fast', behind the 'openai' alias. Walk the alias
first, then the canonical id, so a retired alias does not cost a whole run.
Probed live: mistral 404, openai 200.
@sourcery-ai

sourcery-ai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This PR adds a lazy, localized discovery action to the player that asks an anonymous Pollinations model for bounded search queries based on listening history, resolves those queries through the existing search API, and deterministically scores and persists new tracks in the stable @ai_mix playlist; failures fall back to the existing on-device generator, with extensive contract, regression, and live E2E coverage.

Sequence diagram for AI discovery mix generation

sequenceDiagram
    actor User
    participant Player as FullPlayerModal
    participant Generator as aiMixGenerator
    participant Stats as listeningStats
    participant LLM as Pollinations
    participant Search as searchAPI
    participant Storage as StorageService

    User->>Player: handleAiMix()
    Player->>Generator: generateAiMixWithDiscovery(language)
    Generator->>Stats: loadReplaySummary(alltime)
    Generator->>Storage: loadLikedSongs() and loadPreviouslyPlayedSongs()
    Generator->>Generator: pickSeed(liked, recent)
    Generator->>LLM: fetchSearchQueries(prompt)
    LLM-->>Generator: JSON search queries
    Generator->>Search: resolveQueries(queries)
    Search-->>Generator: discovery candidates
    Generator->>Generator: buildSmartQueue(seed, candidates, playCounts)
    Generator->>Storage: persistAiMix(discovered, language)
    Storage-->>Generator: Playlist(@ai_mix)
    Generator-->>Player: Playlist or null
    Player-->>User: Alert.alert(AI Mix ready or empty)
Loading

Flow diagram for AI discovery fallback chain

flowchart TD
    Start[User taps AI Mix] --> Seed[Load listening history and pickSeed]
    Seed --> HasSeed{Seed available?}
    HasSeed -- No --> Empty[Return null]
    HasSeed -- Yes --> Alias[fetchSearchQueries with openai alias]
    Alias --> Queries{Queries returned?}
    Queries -- No --> Canonical[fetchSearchQueries with openai-fast]
    Canonical --> Queries2{Queries returned?}
    Queries2 -- No --> Local[generateAiMixPlaylist]
    Queries -- Yes --> Resolve[resolveQueries via searchAPI]
    Queries2 -- Yes --> Resolve
    Resolve --> Enough{At least 8 candidates?}
    Enough -- No --> Local
    Enough -- Yes --> Score[buildSmartQueue]
    Score --> Scored{At least 8 tracks scored?}
    Scored -- No --> Local
    Scored -- Yes --> Persist[persistAiMix as @ai_mix]
    Local --> Result[Return playlist or null]
    Persist --> Result
    Empty --> Result
Loading

File-Level Changes

Change Details Files
Adds an LLM-guided discovery pipeline that converts listening history into search queries while retaining deterministic, local track selection.
  • Loads ranked artist and track names from listening statistics and builds English or Persian prompts.
  • Calls the anonymous Pollinations OpenAI-compatible endpoint with no authorization credentials, parses query-only JSON output, and retries an alternate model after failures.
  • Resolves queries through the existing YouTube Music search surface, deduplicates candidates, applies existing smart-queue scoring, and falls back to the on-device generator when any discovery stage is unavailable or too small.
  • Returns null without network activity when there is no usable listening seed.
modules/aiMixGenerator.ts
Introduces shared persistence and on-device generation for the stable pinned AI Mix playlist.
  • Centralizes playlist replacement under the stable @ai_mix id while preserving other playlists and the original creation timestamp.
  • Uses recent playback first, liked tracks second, and play counts as deterministic scoring inputs.
  • Supports localized playlist names/descriptions and derives cover art from the first selected track.
  • Keeps the original on-device generation path as the discovery fallback.
modules/aiMixPlaylist.ts
Adds a player control for generating the discovery mix with lazy loading and localized feedback.
  • Adds a sparkles/hourglass button with duplicate-press protection and in-flight visual state.
  • Lazy-imports the generator so player startup does not load history and storage dependencies.
  • Displays localized success or no-history alerts and resets generation state in a finally block.
components/FullPlayerModal.tsx
locales/en.json
locales/fa.json
Expands regression coverage for playlist generation, discovery behavior, endpoint contracts, and UI integration.
  • Adds stubbed tests for deterministic generation, playlist replacement, seed selection, scoring inputs, persistence safety, parsing, fallback stages, search failures, and no-history behavior.
  • Verifies the anonymous endpoint contract, model-chain rescue after HTTP 402, query-only prompting, and new-track discovery.
  • Adds a live network E2E script that accepts either successful discovery or clean fallback and checks playlist invariants.
  • Normalizes line endings in the existing local playback regression fixture.
tests/ai-mix-regression.cjs
tests/ai-mix-discovery-e2e.cjs
tests/local-playback-regression.cjs

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 54 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7800c1bd-ac78-447c-bb3c-f21589cb3b6c
Commits

Reviewing files that changed from the base of the PR and between e42110b and 3c40f75.

Files selected for processing: 5

  • components/FullPlayerModal.tsx
  • modules/aiMixGenerator.ts
  • modules/aiMixPlaylist.ts
  • tests/ai-mix-discovery-e2e.cjs
  • tests/ai-mix-regression.cjs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 14 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="modules/aiMixPlaylist.ts" line_range="23" />
<code_context>
+  // Read once, write once. addPlaylist re-reads the list internally, so
+  // going through it here would race another writer's change; and reading
+  // twice (once for `existing`, once for `rest`) lets the two disagree.
+  const stored = await StorageService.loadPlaylists();
+  const existing = stored.find((p) => p.id === AI_MIX_ID);
+  const playlist: Playlist = {
</code_context>
<issue_to_address>
**Existing playlists can be erased**

When `StorageService.loadPlaylists()` encounters a read or parse error, `persistAiMix` treats the returned empty array as a successful read and saves only the AI Mix, overwriting the user's other playlists.

Distinguish a failed playlist read from a valid empty list and do not save a replacement after a failed read.
</issue_to_address>

### Comment 2
<location path="modules/aiMixPlaylist.ts" line_range="23-39" />
<code_context>
+  const stored = await StorageService.loadPlaylists();
</code_context>
<issue_to_address>
**Concurrent playlist changes disappear**

When another playlist writer or AI Mix generation saves between `persistAiMix` loading and saving the list, `persistAiMix` saves its earlier snapshot over the concurrent change, so the user's playlist edit or newer mix disappears.

Serialize playlist read-modify-write operations or use an atomic update that preserves concurrent changes.
</issue_to_address>

### Comment 3
<location path="modules/aiMixGenerator.ts" line_range="201" />
<code_context>
+      continue;
+    }
+    for (const t of results) {
+      if (!t?.id || !t.title || seen.has(String(t.id))) continue;
+      seen.add(String(t.id));
+      found.push(t);
</code_context>
<issue_to_address>
**Known tracks enter discovery mix**

When a search query returns a track the user has liked or previously played, `resolveQueries` passes the track to `buildSmartQueue`, which excludes only the seed and can include the already-known track in the discovery mix.

Exclude liked and previously played track IDs from discovery candidates before scoring them.

Also at `modules/aiMixGenerator.ts:258`, `modules/aiMixGenerator.ts:268-273`.
</issue_to_address>

### Comment 4
<location path="modules/aiMixGenerator.ts" line_range="268-273" />
<code_context>
+
+  // Seed scoring gives the discovery pool taste; new tracks have no play
+  // history, so playCounts only discriminates tracks the user already has.
+  const discovered = buildSmartQueue({
+    seed,
+    library: candidates,
+    size: 30,
+    playCounts,
+  });
+  if (discovered.length < MIN_DISCOVERY_TRACKS) {
+    console.log("[aiMix] scoring kept too little, falling back to on-device mix");
</code_context>
<issue_to_address>
**Unrelated tracks enter discovery mix**

When discovery queries return enough candidates with no meaningful artist or title overlap with the seed, `buildSmartQueue` gives every candidate a positive liked-track baseline, so unrelated search results remain eligible and can be included instead of triggering fallback.

Require meaningful similarity to the seed before accepting discovery candidates, and fall back when too few qualify.
</issue_to_address>

### Comment 5
<location path="modules/aiMixGenerator.ts" line_range="195" />
<code_context>
+  for (const query of queries.slice(0, MAX_QUERIES)) {
+    let results: Track[] = [];
+    try {
+      results = await search(query);
+    } catch {
+      // one dead query source must not sink the batch
</code_context>
<issue_to_address>
**A hanging search stalls generation**

When a search request remains pending instead of resolving or rejecting, `resolveQueries` awaits each search without a deadline, so it never reaches the remaining queries or on-device fallback and the generating indicator stays active indefinitely.

Give search requests a deadline so a stalled request can be skipped and generation can continue.
</issue_to_address>

### Comment 6
<location path="components/FullPlayerModal.tsx" line_range="1683-1693" />
<code_context>
+      const { generateAiMixWithDiscovery } = await import(
+        "../modules/aiMixGenerator"
+      );
+      const mix = await generateAiMixWithDiscovery(
+        language === "fa" ? "fa" : "en",
+      );
+      // null = not enough history to curate from. The icon stops spinning and
+      // the user is still looking at the player, so the alert carries the
+      // outcome — the same pattern the credits sheet already uses here.
+      Alert.alert(
+        t("player.aiMixTitle"),
+        t(mix ? "player.aiMixReady" : "player.aiMixEmpty"),
+      );
+    } finally {
+      setIsGeneratingAiMix(false);
+    }
</code_context>
<issue_to_address>
**Failed generation gives no outcome**

When playlist persistence or another uncaught generator operation rejects, `handleAiMix` runs only its `finally` block, clears the spinner, and lets the rejection escape without showing an alert, so the user receives no outcome.

Catch generation and persistence errors in `handleAiMix` and show an alert describing the failure.

Also at `modules/aiMixGenerator.ts:282`.
</issue_to_address>

### Comment 7
<location path="modules/aiMixPlaylist.ts" line_range="39" />
<code_context>
+  // Replace in place, keep every other playlist, and put the mix first so
+  // it stays where the user expects it.
+  const rest = stored.filter((p) => p.id !== AI_MIX_ID);
+  await StorageService.savePlaylists([playlist, ...rest]);
+  return playlist;
+}
</code_context>
<issue_to_address>
**Saved mix may not refresh the library**

When the metadata-cache update fails after the playlist write succeeds, `savePlaylists` rejects after storing the mix but before updating the metadata cache and emitting the library event, so the library may not refresh and the player shows no result alert.

Keep the metadata cache and library event consistent when a playlist write succeeds, even if the cache update initially fails.

Also at `components/FullPlayerModal.tsx:1693-1695`.
</issue_to_address>

### Comment 8
<location path="modules/aiMixGenerator.ts" line_range="240-241" />
<code_context>
+  const [{ artists, tracks: trackNames }, liked, recent, playCounts] =
+    await Promise.all([
+      loadHistoryNames(),
+      StorageService.loadLikedSongs(),
+      StorageService.loadPreviouslyPlayedSongs(),
+      loadPlayCounts(),
+    ]);
</code_context>
<issue_to_address>
**History read failures look like no history**

When reading liked songs or previously played songs fails while the user has history, `StorageService` returns empty arrays, so `pickSeed` returns `null` and generation reports an empty-history outcome instead of a read failure.

Propagate history read errors or distinguish them from valid empty lists before deciding there is no seed.

Also at `modules/aiMixGenerator.ts:248`.
</issue_to_address>

### Comment 9
<location path="modules/aiMixGenerator.ts" line_range="210-220" />
<code_context>
+}
+
+/** Default resolver: YouTube Music via the existing search surface. */
+async function defaultSearch(query: string): Promise<Track[]> {
+  const result = await searchAPI.searchWithYouTubeMusic(query, "all", 1, SEARCH_LIMIT);
+  const items = result?.items || [];
+  return items.map((item: any) => ({
+    id: String(item.id),
+    title: item.title || "",
+    artist: item.author || item.artist || "",
+    thumbnail: item.img || item.thumbnailUrl || item.thumbnail,
+    url: item.href,
+    source: "youtubemusic",
+  })) as Track[];
+}
+
</code_context>
<issue_to_address>
**Discovery searches only one catalog**

When a discovery track is available from another supported search catalog but not YouTube Music, `defaultSearch` calls only `searchWithYouTubeMusic`, so tracks from the other mixed-search providers never enter the candidate pool.

Use the app's mixed search providers to resolve discovery queries.
</issue_to_address>

### Comment 10
<location path="modules/aiMixPlaylist.ts" line_range="105-113" />
<code_context>
+  }
+
+  const pool = candidatePool(liked, recent);
+  const tracks = buildSmartQueue({
+    seed,
+    library: pool,
+    size: MIX_SIZE,
+    playCounts,
+  });
+  if (!tracks.length) {
+    return null;
+  }
+
+  return persistAiMix(tracks, language);
</code_context>
<issue_to_address>
**Small history pool reports empty mix**

When the user has some liked or recently played tracks but fewer than five distinct tracks, and discovery cannot produce a usable mix, `buildSmartQueue` returns no tracks for the small pool, so the on-device path and discovery fallback return `null` and the player reports an empty-history outcome despite existing history.

Handle a nonempty but undersized history pool without treating it as no listening history.

Also at `modules/aiMixGenerator.ts:255`, `modules/aiMixGenerator.ts:275-277`.
</issue_to_address>

### Comment 11
<location path="modules/aiMixGenerator.ts" line_range="74-75" />
<code_context>
+      : "These are what I listen to most:";
+  return [
+    head,
+    `Top artists: ${artists.slice(0, 10).join(", ")}`,
+    `Top tracks: ${tracks.slice(0, 15).join("; ")}`,
+    buildHistoryPrompt(language),
+  ].join("\n");
</code_context>
<issue_to_address>
**Listening history sent to third party**

When the user taps the AI Mix button with listening history available, `loadHistoryNames` puts ranked artist and track names into the prompt, and `fetchFrom` posts it to Pollinations. The player tells the user only that an AI Mix is being generated, so they are not informed that personal listening preferences leave the device and are sent to an external service.

Disclose the third-party transfer and obtain informed opt-in before sending history names, or keep the history-based generation on-device.

Also at `modules/aiMixGenerator.ts:128`.
</issue_to_address>

### Comment 12
<location path="tests/ai-mix-discovery-e2e.cjs" line_range="6-8" />
<code_context>
+ *
+ * NOT a hermetic regression test — it hits text.pollinations.ai over the
+ * network and asserts the real response parses into real search queries.
+ * Exits 0 if the discovery path works OR if it cleanly falls back (both
+ * are valid product outcomes); logs which happened. Fails only if the
+ * call throws uncaught or a track slips through from the wrong pool.
+ */
+const assert = require('node:assert/strict');
</code_context>
<issue_to_address>
**Live discovery test accepts fallback**

When the generator always falls back, or the live endpoint returns no usable queries, `main` in `ai-mix-discovery-e2e.cjs` accepts any nonempty mix at line 93 and labels fallback a pass at line 114; the real queries are only logged at lines 100–103, so this test passes without verifying that the live response parsed or discovery ran.

Assert that live queries were produced and require a discovery result when the endpoint responds; report endpoint unavailability as skipped rather than as a passing discovery check.
</issue_to_address>

### Comment 13
<location path="tests/ai-mix-regression.cjs" line_range="358-364" />
<code_context>
+
+  await check('discovery: LLM queries resolve to a pinned mix of NEW tracks', async () => {
+    resetDiscovery();
+    const mix = await GEN.generateAiMixWithDiscovery('en', { fetchImpl: llmFetch, search: discoverySearch });
+    assert.ok(mix, 'expected a mix');
+    assert.equal(mix.id, MIX.AI_MIX_ID);
+    assert.equal(llmCalls, 1, 'the LLM must be called exactly once');
+    // Every track came from discovery, none from the user's library.
+    assert.ok(mix.tracks.length >= 8, 'expected a real discovery pool');
+    assert.ok(mix.tracks.every((t) => t.id.startsWith('d')), 'mix must be new tracks only');
+    assert.equal(playlists.filter((p) => p.id === MIX.AI_MIX_ID).length, 1);
+  });
</code_context>
<issue_to_address>
**Mock search ignores generated queries**

When the generator calls the injected search with a constant or empty query instead of the parsed LLM output, in `discovery: LLM queries resolve to a pinned mix of NEW tracks`, `discoverySearch` at line 183 returns the same results for every input, so the assertions at lines 358–364 pass even if the generator ignores the LLM queries and searches with a constant or empty string.

Capture the query arguments in `discoverySearch` and assert they match the queries parsed from the mocked LLM response.
</issue_to_address>

### Comment 14
<location path="tests/ai-mix-regression.cjs" line_range="394-400" />
<code_context>
+    assert.equal(long[0].length, 80);
+  });
+
+  await check('discovery: a failed LLM call falls back to the on-device mix', async () => {
+    resetDiscovery();
+    llmStatus = 500;
+    const mix = await GEN.generateAiMixWithDiscovery('en', { fetchImpl: llmFetch, search: discoverySearch });
+    assert.ok(mix, 'fallback must still produce a mix');
+    // On-device mix draws from the user's library, not discovery ids.
+    assert.ok(mix.tracks.every((t) => !t.id.startsWith('d')), 'fallback must be the on-device mix');
+  });
+
</code_context>
<issue_to_address>
**Fallback tests accept empty mixes**

When the fallback returns a playlist whose track list is empty, when a fallback returns a non-null playlist with `tracks: []`, the failed-LLM, unparseable-response, and thin-discovery checks pass because `.every(...)` is true for an empty array; the throwing-search check only asserts `mix` is truthy. These tests pass without a usable fallback mix.

Assert that each fallback mix contains at least one track before checking that its tracks came from the on-device pool.

Also at `tests/ai-mix-regression.cjs:403-408`, `tests/ai-mix-regression.cjs:411-416`, `tests/ai-mix-regression.cjs:419-425`.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 14 findings to address first, and the feature sends users' top artist and track names to an anonymous third-party LLM service and persists its resulting playlist locally. Reverting can remove the feature and playlist, but it cannot retract listening-history data already disclosed to the external service, and an incorrect generated mix may remain until it is replaced or deleted.

Blocking findings: modules/aiMixPlaylist.ts:23, modules/aiMixPlaylist.ts:39, modules/aiMixGenerator.ts:201, modules/aiMixGenerator.ts:273, modules/aiMixGenerator.ts:195, and 9 more


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread modules/aiMixPlaylist.ts
// Read once, write once. addPlaylist re-reads the list internally, so
// going through it here would race another writer's change; and reading
// twice (once for `existing`, once for `rest`) lets the two disagree.
const stored = await StorageService.loadPlaylists();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Critical · Existing playlists can be erased

When StorageService.loadPlaylists() encounters a read or parse error, persistAiMix treats the returned empty array as a successful read and saves only the AI Mix, overwriting the user's other playlists.

Distinguish a failed playlist read from a valid empty list and do not save a replacement after a failed read.

Prompt for AI agents
In `modules/aiMixPlaylist.ts` at line 23:

**Existing playlists can be erased**

When `StorageService.loadPlaylists()` encounters a read or parse error, `persistAiMix` treats the returned empty array as a successful read and saves only the AI Mix, overwriting the user's other playlists.

Distinguish a failed playlist read from a valid empty list and do not save a replacement after a failed read.

Comment thread modules/aiMixPlaylist.ts
Comment on lines +23 to +39
const stored = await StorageService.loadPlaylists();
const existing = stored.find((p) => p.id === AI_MIX_ID);
const playlist: Playlist = {
id: AI_MIX_ID,
name: aiMixName(language),
description: language === "fa" ? "از تاریخچه پخش شما" : "Built from your listening history",
tracks,
createdAt: existing?.createdAt ?? now,
updatedAt: now,
// The top track's art doubles as the cover: a real image beats a
// placeholder glyph, and it changes as the mix does.
thumbnail: tracks[0]?.thumbnail,
};
// Replace in place, keep every other playlist, and put the mix first so
// it stays where the user expects it.
const rest = stored.filter((p) => p.id !== AI_MIX_ID);
await StorageService.savePlaylists([playlist, ...rest]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Concurrent playlist changes disappear

When another playlist writer or AI Mix generation saves between persistAiMix loading and saving the list, persistAiMix saves its earlier snapshot over the concurrent change, so the user's playlist edit or newer mix disappears.

Serialize playlist read-modify-write operations or use an atomic update that preserves concurrent changes.

Prompt for AI agents
In `modules/aiMixPlaylist.ts` at lines 23-39:

**Concurrent playlist changes disappear**

When another playlist writer or AI Mix generation saves between `persistAiMix` loading and saving the list, `persistAiMix` saves its earlier snapshot over the concurrent change, so the user's playlist edit or newer mix disappears.

Serialize playlist read-modify-write operations or use an atomic update that preserves concurrent changes.

Comment thread modules/aiMixGenerator.ts
continue;
}
for (const t of results) {
if (!t?.id || !t.title || seen.has(String(t.id))) continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Known tracks enter discovery mix

When a search query returns a track the user has liked or previously played, resolveQueries passes the track to buildSmartQueue, which excludes only the seed and can include the already-known track in the discovery mix.

Exclude liked and previously played track IDs from discovery candidates before scoring them.

Also at modules/aiMixGenerator.ts:258, modules/aiMixGenerator.ts:268-273.

Prompt for AI agents
In `modules/aiMixGenerator.ts` at line 201:

**Known tracks enter discovery mix**

When a search query returns a track the user has liked or previously played, `resolveQueries` passes the track to `buildSmartQueue`, which excludes only the seed and can include the already-known track in the discovery mix.

Exclude liked and previously played track IDs from discovery candidates before scoring them.

Also at `modules/aiMixGenerator.ts:258`, `modules/aiMixGenerator.ts:268-273`.

Comment thread modules/aiMixGenerator.ts
Comment on lines +268 to +273
const discovered = buildSmartQueue({
seed,
library: candidates,
size: 30,
playCounts,
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Unrelated tracks enter discovery mix

When discovery queries return enough candidates with no meaningful artist or title overlap with the seed, buildSmartQueue gives every candidate a positive liked-track baseline, so unrelated search results remain eligible and can be included instead of triggering fallback.

Require meaningful similarity to the seed before accepting discovery candidates, and fall back when too few qualify.

Prompt for AI agents
In `modules/aiMixGenerator.ts` at lines 268-273:

**Unrelated tracks enter discovery mix**

When discovery queries return enough candidates with no meaningful artist or title overlap with the seed, `buildSmartQueue` gives every candidate a positive liked-track baseline, so unrelated search results remain eligible and can be included instead of triggering fallback.

Require meaningful similarity to the seed before accepting discovery candidates, and fall back when too few qualify.

Comment thread modules/aiMixGenerator.ts
for (const query of queries.slice(0, MAX_QUERIES)) {
let results: Track[] = [];
try {
results = await search(query);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · A hanging search stalls generation

When a search request remains pending instead of resolving or rejecting, resolveQueries awaits each search without a deadline, so it never reaches the remaining queries or on-device fallback and the generating indicator stays active indefinitely.

Give search requests a deadline so a stalled request can be skipped and generation can continue.

Prompt for AI agents
In `modules/aiMixGenerator.ts` at line 195:

**A hanging search stalls generation**

When a search request remains pending instead of resolving or rejecting, `resolveQueries` awaits each search without a deadline, so it never reaches the remaining queries or on-device fallback and the generating indicator stays active indefinitely.

Give search requests a deadline so a stalled request can be skipped and generation can continue.

Comment thread modules/aiMixPlaylist.ts
Comment on lines +105 to +113
const tracks = buildSmartQueue({
seed,
library: pool,
size: MIX_SIZE,
playCounts,
});
if (!tracks.length) {
return null;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Small history pool reports empty mix

When the user has some liked or recently played tracks but fewer than five distinct tracks, and discovery cannot produce a usable mix, buildSmartQueue returns no tracks for the small pool, so the on-device path and discovery fallback return null and the player reports an empty-history outcome despite existing history.

Handle a nonempty but undersized history pool without treating it as no listening history.

Also at modules/aiMixGenerator.ts:255, modules/aiMixGenerator.ts:275-277.

Prompt for AI agents
In `modules/aiMixPlaylist.ts` at lines 105-113:

**Small history pool reports empty mix**

When the user has some liked or recently played tracks but fewer than five distinct tracks, and discovery cannot produce a usable mix, `buildSmartQueue` returns no tracks for the small pool, so the on-device path and discovery fallback return `null` and the player reports an empty-history outcome despite existing history.

Handle a nonempty but undersized history pool without treating it as no listening history.

Also at `modules/aiMixGenerator.ts:255`, `modules/aiMixGenerator.ts:275-277`.

Comment thread modules/aiMixGenerator.ts
Comment on lines +74 to +75
`Top artists: ${artists.slice(0, 10).join(", ")}`,
`Top tracks: ${tracks.slice(0, 15).join("; ")}`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Listening history sent to third party

When the user taps the AI Mix button with listening history available, loadHistoryNames puts ranked artist and track names into the prompt, and fetchFrom posts it to Pollinations. The player tells the user only that an AI Mix is being generated, so they are not informed that personal listening preferences leave the device and are sent to an external service.

Disclose the third-party transfer and obtain informed opt-in before sending history names, or keep the history-based generation on-device.

Also at modules/aiMixGenerator.ts:128.

Prompt for AI agents
In `modules/aiMixGenerator.ts` at lines 74-75:

**Listening history sent to third party**

When the user taps the AI Mix button with listening history available, `loadHistoryNames` puts ranked artist and track names into the prompt, and `fetchFrom` posts it to Pollinations. The player tells the user only that an AI Mix is being generated, so they are not informed that personal listening preferences leave the device and are sent to an external service.

Disclose the third-party transfer and obtain informed opt-in before sending history names, or keep the history-based generation on-device.

Also at `modules/aiMixGenerator.ts:128`.

Comment on lines +6 to +8
* Exits 0 if the discovery path works OR if it cleanly falls back (both
* are valid product outcomes); logs which happened. Fails only if the
* call throws uncaught or a track slips through from the wrong pool.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Live discovery test accepts fallback

When the generator always falls back, or the live endpoint returns no usable queries, main in ai-mix-discovery-e2e.cjs accepts any nonempty mix at line 93 and labels fallback a pass at line 114; the real queries are only logged at lines 100–103, so this test passes without verifying that the live response parsed or discovery ran.

Assert that live queries were produced and require a discovery result when the endpoint responds; report endpoint unavailability as skipped rather than as a passing discovery check.

Prompt for AI agents
In `tests/ai-mix-discovery-e2e.cjs` at lines 6-8:

**Live discovery test accepts fallback**

When the generator always falls back, or the live endpoint returns no usable queries, `main` in `ai-mix-discovery-e2e.cjs` accepts any nonempty mix at line 93 and labels fallback a pass at line 114; the real queries are only logged at lines 100–103, so this test passes without verifying that the live response parsed or discovery ran.

Assert that live queries were produced and require a discovery result when the endpoint responds; report endpoint unavailability as skipped rather than as a passing discovery check.

Comment on lines +358 to +364
const mix = await GEN.generateAiMixWithDiscovery('en', { fetchImpl: llmFetch, search: discoverySearch });
assert.ok(mix, 'expected a mix');
assert.equal(mix.id, MIX.AI_MIX_ID);
assert.equal(llmCalls, 1, 'the LLM must be called exactly once');
// Every track came from discovery, none from the user's library.
assert.ok(mix.tracks.length >= 8, 'expected a real discovery pool');
assert.ok(mix.tracks.every((t) => t.id.startsWith('d')), 'mix must be new tracks only');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Mock search ignores generated queries

When the generator calls the injected search with a constant or empty query instead of the parsed LLM output, in discovery: LLM queries resolve to a pinned mix of NEW tracks, discoverySearch at line 183 returns the same results for every input, so the assertions at lines 358–364 pass even if the generator ignores the LLM queries and searches with a constant or empty string.

Capture the query arguments in discoverySearch and assert they match the queries parsed from the mocked LLM response.

Prompt for AI agents
In `tests/ai-mix-regression.cjs` at lines 358-364:

**Mock search ignores generated queries**

When the generator calls the injected search with a constant or empty query instead of the parsed LLM output, in `discovery: LLM queries resolve to a pinned mix of NEW tracks`, `discoverySearch` at line 183 returns the same results for every input, so the assertions at lines 358–364 pass even if the generator ignores the LLM queries and searches with a constant or empty string.

Capture the query arguments in `discoverySearch` and assert they match the queries parsed from the mocked LLM response.

Comment on lines +394 to +400
await check('discovery: a failed LLM call falls back to the on-device mix', async () => {
resetDiscovery();
llmStatus = 500;
const mix = await GEN.generateAiMixWithDiscovery('en', { fetchImpl: llmFetch, search: discoverySearch });
assert.ok(mix, 'fallback must still produce a mix');
// On-device mix draws from the user's library, not discovery ids.
assert.ok(mix.tracks.every((t) => !t.id.startsWith('d')), 'fallback must be the on-device mix');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Fallback tests accept empty mixes

When the fallback returns a playlist whose track list is empty, when a fallback returns a non-null playlist with tracks: [], the failed-LLM, unparseable-response, and thin-discovery checks pass because .every(...) is true for an empty array; the throwing-search check only asserts mix is truthy. These tests pass without a usable fallback mix.

Assert that each fallback mix contains at least one track before checking that its tracks came from the on-device pool.

Also at tests/ai-mix-regression.cjs:403-408, tests/ai-mix-regression.cjs:411-416, tests/ai-mix-regression.cjs:419-425.

Prompt for AI agents
In `tests/ai-mix-regression.cjs` at lines 394-400:

**Fallback tests accept empty mixes**

When the fallback returns a playlist whose track list is empty, when a fallback returns a non-null playlist with `tracks: []`, the failed-LLM, unparseable-response, and thin-discovery checks pass because `.every(...)` is true for an empty array; the throwing-search check only asserts `mix` is truthy. These tests pass without a usable fallback mix.

Assert that each fallback mix contains at least one track before checking that its tracks came from the on-device pool.

Also at `tests/ai-mix-regression.cjs:403-408`, `tests/ai-mix-regression.cjs:411-416`, `tests/ai-mix-regression.cjs:419-425`.

@llamapreview llamapreview Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LlamaPReview — No blocking issues found

Safe to merge: every discovery stage is wrapped with a fallback to the shipped on-device mix, the mix persists through one stable write path, and the LLM only ever produces search queries.

2 non-blocking findings retained — highest: Sparkle tap now sends top artist/track names to a third-party endpoint, undisclosed.

Change flow

The whole discovery pipeline is a fallback chain: one persistence step under the stable @ai_mix id, the LLM fenced behind a strict parse and the app's own search, and every failure converging on the unchanged on-device generator.

sequenceDiagram
participant U as User
participant P as FullPlayerModal
participant G as aiMixGenerator
participant L as Pollinations LLM
participant S as YT Music Search
participant D as Local Storage
U->>P: tap sparkle
P->>G: generateAiMixWithDiscovery(language)
G->>D: loadHistory + liked + recent + counts
G->>G: pickSeed -> none -> null (alert)
note over G: PR change — discovery-first: LLM writes queries, search resolves, scoring stays deterministic
alt keyless LLM returns queries
G->>L: POST keyless prompt (no auth)
L-->>G: JSON array of <=6 queries
G->>S: resolveQueries (per-query catch, dedupe)
S-->>G: candidates
G->>G: buildSmartQueue(seed, candidates, 30)
opt queries empty or <8 candidates or <8 scored
G->>G: fall back to on-device mix
end
G->>D: persistAiMix: replace @ai_mix, keep rest
D-->>P: library-updated event
P-->>U: mix ready alert
else LLM unparseable or no candidates
G->>G: on-device mix fallback
G->>D: persistAiMix
P-->>U: mix ready alert
end
note over G,D: Impact — user sees a pinned replayable AI Mix, or the unchanged on-device mix
Loading
Review details and evidence
Priority File Finding Evidence
P2 modules/aiMixGenerator.ts Sparkle tap now sends top artist/track names to a third-party endpoint, undisclosed confirmed
P2 modules/aiMixGenerator.ts Discovery ordering carries little signal, so 'curated' is weaker than on-device confirmed

Finding details

P2 · Sparkle tap now sends top artist/track names to a third-party endpoint, undisclosed

modules/aiMixGenerator.ts

Every sparkle tap posts the user's top artist names and track titles to a third-party anonymous endpoint, and there is no user-facing disclosure; the tradeoff is documented only in the module header and PR body. The request is evidenced as body-only and credential-free, so the consequence is preference data leaving the device on a user-initiated action — not an exploitable path — and no pre-merge change is required.

Owner action: Optional: surface a one-line notice or setting for the discovery call.

Verification boundary: confirmed; scope: bounded reviewed context.

P2 · Discovery ordering carries little signal, so 'curated' is weaker than on-device

modules/aiMixGenerator.ts

In the discovery branch the scoring weights have little to bite on: the prompt explicitly tells the model not to reuse the seed's artist/track names, the liked-baseline weight is added to every candidate, and play counts mostly refer to ids that are not in the discovery pool — so ordering is largely title-token overlap plus search order for ties. Selection is still deterministic and size-capped, so the consequence is a weaker curated claim than the on-device path, not a broken playlist.

Owner action: Optional: accept as-is, or give discovery its own signal later.

Verification boundary: confirmed; scope: bounded reviewed context.

Material unknowns

  • Whether StorageService.loadPlaylists() throws or silently returns [] on a failed read. If it swallows errors and returns [], a failed read would make a successful write persist a mix-only playlist set locally, which would become a P1 data-consistency finding and move the posture to request changes (distinguish failed reads from an empty library, or serialize/guard the write). If it throws, the current behavior is simply a failed generation. Deep did not raise this as pre-merge because the premise is unobserved.
    • Check: Check loadPlaylists failure semantics in the storage layer.
  • Whether tests/ai-mix-regression.cjs and tests/ai-mix-discovery-e2e.cjs are wired into a repository runner. If not wired, the 27-check and live-run claims remain manual-only and the regression protection is not enforced. A coverage gap, not a defect claim.
    • Check: Confirm the two suites are invoked from package.json or CI.

LlamaPReview checks

  • Reviewed changed regions in modules/aiMixGenerator.ts.
  • Read bounded PR-head context from modules/aiMixGenerator.ts.
  • Read bounded PR-head context from modules/aiPlaylistService.ts.
  • Read bounded PR-head context from utils/storage.ts.
  • Inspected matching PR-head repository snippets in utils/storage.ts, components/screens/LibraryScreen.tsx, lib/cloud-library-sync.ts and 2 more path(s).
  • Reviewed changed regions in modules/aiMixPlaylist.ts.

Still coding alone? Open CarbonChat, a chat room right inside Codex.

@github-actions github-actions Bot added the config label Nov 5, 2026
@ErfanBagheri404
ErfanBagheri404 changed the base branch from feat/ai-mix to main November 5, 2026 21:02
@ErfanBagheri404

Copy link
Copy Markdown
Owner Author

Scope decision

(a) Local-only reviews. No backend, no accounts, no sync — matches the offline-first rule this app is built on. The note lives on the rating row in Room, is shown on the dish detail next to the stars, and is searchable.

(b) would need an anonymous vote endpoint and (c) accounts + moderation; neither is justified for a personal recipe app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant