Skip to content

feat: add output cache system with TTL and diff detection#554

Open
bz00qa wants to merge 2 commits intortk-ai:developfrom
bz00qa:feat/cache-system
Open

feat: add output cache system with TTL and diff detection#554
bz00qa wants to merge 2 commits intortk-ai:developfrom
bz00qa:feat/cache-system

Conversation

@bz00qa
Copy link
Copy Markdown

@bz00qa bz00qa commented Mar 13, 2026

Summary

  • cache.rs: File-based output cache with deterministic keys (command + cwd hash), configurable TTL, volatile command detection, and line-level diff between cached/current output
  • rtk cache-clear: CLI command to reset the cache
  • Tracking integration: New cache_hit column in SQLite — all gain/analytics queries exclude cache hits to keep savings stats accurate
  • Config: [cache] section in config.toml with enabled (default: true) and ttl_minutes (default: 5), overridable via RTK_CACHE and RTK_CACHE_TTL env vars

How it works

When proxy -f (from #553) or future commands use the cache:

  1. Compute deterministic key from command + working directory
  2. Check if cached output exists and is within TTL
  3. If cache hit: diff against current output, show only changes
  4. If cache miss: store filtered output for next invocation

Volatile commands (git status, ls, find, etc.) are never cached.

Tests

11 new tests covering key generation, volatile detection, diff computation, and truncation.

Test plan

  • cargo fmt --all passes
  • cargo clippy --all-targets passes (no new warnings)
  • cargo test — 898 passed, 11 new cache tests all green
  • Manual: rtk cache-clear clears cache directory
  • Manual: verify [cache] section appears in rtk config

🤖 Generated with Claude Code

New cache module for caching filtered command output:
- File-based cache in ~/.local/share/rtk/cache/ with deterministic
  keys (command + cwd hash)
- Configurable TTL (default 5min) via config.toml or RTK_CACHE_TTL
  env var
- Enable/disable via config.toml or RTK_CACHE env var
- Volatile command detection (git status, ls, etc. are never cached)
- Line-level diff between cached and current output with truncation
- `rtk cache-clear` command to reset cache

Tracking integration:
- New cache_hit column in SQLite tracking database
- record_cache_hit() and track_cache_hit() methods
- All gain/analytics queries exclude cache hits (AND cache_hit = 0)
  to keep savings stats accurate
- get_cache_stats() for cache performance reporting

Config:
- CacheConfig struct in config.toml ([cache] section)
- enabled (default: true), ttl_minutes (default: 5)

11 tests for cache module, all existing tests pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: bz00qa <bz00qa.hub@gmail.com>
@bz00qa bz00qa force-pushed the feat/cache-system branch from 373d384 to 7cf47d0 Compare March 13, 2026 16:36
@bz00qa bz00qa changed the base branch from master to develop March 13, 2026 16:37
Signed-off-by: bZ00qa <167500396+bz00qa@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@pszymkowiak
Copy link
Copy Markdown
Collaborator

This PR has merge conflicts with develop. Please rebase:

git fetch upstream develop
git rebase upstream/develop
git push --force-with-lease

Labeling awaiting-changes until conflicts are resolved.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@aeppling
Copy link
Copy Markdown
Contributor

Hey

We are cleaning up the codebase and improving the project structure for better onboarding. As part of this effort, PR #826 reorganizes src/ from a flat layout into subfolders.

No logic changes — only file moves and import path updates.

What you need to do

Rebase your branch on develop when receiving this comment:

git fetch origin && git rebase origin/develop

Git detects renames automatically. If you get import conflicts, update the paths:

use crate::git;        // now: use crate::cmds::git::git;
use crate::tracking;   // now: use crate::core::tracking;
use crate::config;     // now: use crate::core::config;
use crate::init;       // now: use crate::hooks::init;
use crate::gain;       // now: use crate::analytics::gain;

Need help rebasing? Tag @aeppling

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants