Skip to content

feat: track per-user tool access times with in-memory cache and periodic DB flush#94

Merged
aptend merged 3 commits intomainfrom
feat/tool-active
Mar 23, 2026
Merged

feat: track per-user tool access times with in-memory cache and periodic DB flush#94
aptend merged 3 commits intomainfrom
feat/tool-active

Conversation

@aptend
Copy link
Copy Markdown
Collaborator

@aptend aptend commented Mar 23, 2026

What type of PR is this?

  • feat (new feature)
  • fix (bug fix)
  • docs (documentation)
  • style (formatting, no code change)
  • refactor (code change that neither fixes a bug nor adds a feature)
  • perf (performance improvement)
  • test (adding or updating tests)
  • chore (maintenance, tooling)
  • build / ci (build or CI changes)

Which issue(s) this PR fixes

N/A — new feature

What this PR does / why we need it

Adds per-user tool usage tracking: an in-memory map records the last access time for each (user_id, tool) pair, flushed to DB every 10 minutes. A query API returns results directly from memory with zero DB overhead.

Changes

memoria-api/src/auth.rs

  • New ToolUsageBatcher struct: in-memory HashMap<(user_id, tool), (DateTime, dirty)>
    • mark_used() — records access with current timestamp
    • get_user_tool_usage(user_id) — returns all tool timestamps from memory
    • rebuild_from_db() — restores cache from mem_tool_usage table on startup
    • flush() — batch upserts dirty entries to DB; only clears dirty flags after all writes succeed (retry-safe)
  • spawn_tool_usage_flusher() — background task, 10-minute interval
  • AuthUser extractor reads X-Tool-Name header, filters empty values, calls mark_used on both auth paths

memoria-api/src/state.rs

  • AppState gains tool_usage_batcher: Arc<ToolUsageBatcher>
  • init_auth_pool() calls rebuild_from_db() then starts the flusher

memoria-api/src/lib.rs + routes/memory.rs

  • GET /v1/tool-usage — returns current user tool access times from memory

memoria-storage/src/store.rs

  • migrate() creates mem_tool_usage table with PRIMARY KEY (user_id, tool_name)

Testing

  • 3 unit tests (auth::tests): mark/query, overwrite updates time, empty tool filtered
  • 1 e2e test (test_tool_usage_tracking): empty state, recording, empty header ignored, multiple tools, user isolation
  • INSERT ON DUPLICATE KEY UPDATE verified against MatrixOne directly

@aptend aptend merged commit bd18be6 into main Mar 23, 2026
4 checks passed
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.

1 participant