Skip to content

refactor(shared): Migrate shared caching implementation to cache4k - #744

Merged
haiphucnguyen merged 3 commits into
mainfrom
feature/improvements
Oct 2, 2026
Merged

haiphucnguyen merged 3 commits into
mainfrom
feature/improvements

Conversation

@haiphucnguyen

Copy link
Copy Markdown
Collaborator

Summary

  • Replaces the Caffeine cache library with Cache4k.
  • Improves multiplatform compatibility for shared caching logic.
  • Updates cache access methods (e.g., getIfPresent -> get/getOrPut) to align with the new API.

Related Issue(s)

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 💥 Breaking change
  • 📝 Documentation
  • ♻️ Refactor (no functional change)
  • 🧪 Tests
  • 🔧 Chore / tooling / CI

What Changed & Why

How to Test

./gradlew test

Screenshots / Recordings (if applicable)

Checklist

  • I have read the CONTRIBUTING guide
  • My branch is up to date with main
  • I ran ./gradlew spotlessApply and ./gradlew detekt locally
  • I ran ./gradlew test and all tests pass
  • I added/updated tests for my changes (if applicable)
  • I added/updated documentation (if applicable)
  • My commits are signed off (DCO) — git commit -s
  • This PR is focused on a single feature/fix

Additional Notes

- Replaces the dependency on Caffeine with cache4k across the application.
- Updates caching logic in `ChatSessionService` and `McpInstanceService` to utilize the cache4k API.
- Ensures build system and dependency management reflect the change in caching library.
Copilot AI balanced review requested due to automatic review settings October 2, 2026 04:30

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Concurrent invalidation can be undone by an in-flight loader, and memory-removal behavior lacks regression coverage.

Review effort: Balanced
Findings: 1 High severity · 1 Medium severity

Open (2)
What changed in this PR

Migrates shared caching from Caffeine to Cache4k to improve multiplatform compatibility.

Changes:

  • Updates cache construction, lookups, loading, and removal listeners.
  • Replaces the cache dependency and removes a Caffeine-specific comment reference.
  • Updates Spotless and configures Kotlin formatting targets.
File Description
shared/​src/​main/​kotlin/​io/​askimo/​core/​mcp/​McpInstanceService.kt Migrates MCP caches and event handling.
shared/​src/​main/​kotlin/​io/​askimo/​core/​chat/​service/​ChatSessionService.kt Migrates session and memory caches; adds synchronous loading.
shared/​build.gradle.kts Replaces the cache dependency.
gradle/​libs.versions.toml Adds Cache4k and updates Spotless.
desktop-shared/​src/​main/​kotlin/​io/​askimo/​ui/​chat/​ChatViewModel.kt Removes Caffeine wording from a comment only.
build.gradle.kts Sets Kotlin formatting targets and excludes build directories.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Test cleanup can race with pending summarization writes and leaves summarizer executors running.

Review effort: Balanced
Findings: 1 High severity · 2 Medium severity

Open (3)

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

Cache synchronization can block unrelated sessions during remote initialization and lacks concurrency regression coverage.

Review effort: Balanced
Findings: None

Resolved since last review (3)
Previously missed (2)

In code that hasn't changed since last review

Medium severity Avoid holding a cache-wide lock during slow client loading

shared/​src/​main/​kotlin/​io/​askimo/​core/​cache/​SynchronizedCache.kt:31

This cache-wide monitor is held throughout loader(), blocking cache misses and invalidations for unrelated sessions. A context load in ChatSessionService calls createRetrieverForContainer() → RagUtils.getEmbeddingStore() → getDimensionForModel() → embeddingModel.embed(). With a remote embedding provider, one slow request therefore delays another session's client creation or deleteSession() until it finishes. Coordinate loading and invalidation per key, with separate coordination for invalidateAll(), so unrelated sessions can proceed without allowing an invalidated load to publish a stale entry.

Medium severity Add regression tests for stale cache publication and request deduplication

shared/​src/​main/​kotlin/​io/​askimo/​core/​chat/​service/​ChatSessionService.kt:269

Add deterministic concurrency regression tests for this context-loading path. The new eviction tests exercise memory removal, not the stale-publication guarantee that motivated SynchronizedCache. Block a loader with a latch, start setWebSearchForSession() on another thread, then release the loader and wait for invalidation to complete. Verify that the next getOrCreateClientForSession() rebuilds the client. Also cover model-change invalidateAll() and verify that simultaneous same-session requests create only one client. These tests should protect the ordering guarantee when the synchronization implementation changes.

@haiphucnguyen
haiphucnguyen merged commit ab11958 into main Oct 2, 2026
10 of 11 checks passed
@haiphucnguyen
haiphucnguyen deleted the feature/improvements branch October 2, 2026 05:26
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.

2 participants