Skip to content

fix: stop returning learnings past their ttl_days, and say why learnings need a workspace - #46

Merged
tofutim merged 1 commit into
srclight:developfrom
vishalhabib99:fix/learnings-ttl-expiry
Oct 14, 2026
Merged

tofutim merged 1 commit into
srclight:developfrom
vishalhabib99:fix/learnings-ttl-expiry

Conversation

@vishalhabib99

Copy link
Copy Markdown
Contributor

What's wrong

1. ttl_days learnings never expire. record_learning documents ttl_days as "Auto-expire after N days", and it sets expires_at, but nothing reads it. An expired learning keeps coming back:

  1. record_learning {"kind":"blocker","content":"staging deploy frozen until the zebra migration lands","ttl_days":1}
  2. Let the day pass (or set the row's expires_at to yesterday).
  3. relevant_learnings {"query":"zebra"} still returns the blocker.

2. In single-repo mode the learnings tools don't say why they fail. record_learning, relevant_learnings, learning_stats and conversation_summary answer only Error executing tool learning_stats (etc.). The reason, "Learnings require workspace mode", only shows up in the server log.

Cause

  1. LearningsDB.expire_stale() (src/srclight/learnings.py:358) is only called from tests/test_learnings.py. search_fts, _search_like and search_embeddings have no expires_at condition.
  2. _get_learnings_db() (src/srclight/server.py:316) raises RuntimeError. Under mcp 2.x, MCPServer forwards the text of a ToolError but replaces any other exception with the bare "Error executing tool ".

Fix

  1. The three searches skip rows where expires_at has passed: AND (expires_at IS NULL OR expires_at > ?) with _now_iso(). Both sides use the same %Y-%m-%dT%H:%M:%S.%fZ format, so the text comparison is safe. Nothing is deleted. expire_stale() is left as it is.
  2. _get_learnings_db() raises ToolError("Learnings require workspace mode: start the server with 'srclight serve --workspace NAME'"). The srclight tool CLI path already catches exceptions from mcp.call_tool, so it now prints the same reason.

Tests

Added to tests/test_learnings.py:

  • test_search_skips_expired_learnings: records a ttl_days=1 learning and a permanent one, expires the first, and checks that the FTS, LIKE-fallback and embedding searches return only the permanent one.
  • test_learnings_tools_explain_workspace_mode: calls learning_stats through mcp.call_tool with no workspace and expects a ToolError that mentions "workspace mode".

Both fail on develop (assert [1, 2] == [2], and Actual message: 'Error executing tool learning_stats') and pass with the fix.

Full suite on macOS / Python 3.12: 875 passed, 17 skipped, 9 failed. The same 9 tests fail on develop without this change (test_hook_still_runs_on_linux, test_index_dart, and 7 test_read_only_uri_survives_metacharacters cases; they look macOS-specific). ruff check reports the same results before and after.

…ngs need a workspace

record_learning documents ttl_days as "Auto-expire after N days", but
nothing ever removed or filtered an expired learning: expire_stale() is
only called from a test, and none of the three searches checked
expires_at. relevant_learnings kept returning a 1-day blocker after it
expired. The searches now skip rows whose expires_at has passed.

In single-repo mode the four learnings tools failed with only "Error
executing tool <name>": MCPServer forwards the text of a ToolError but
masks a RuntimeError. _get_learnings_db now raises ToolError, so the
client sees that learnings need 'srclight serve --workspace NAME'.
@tofutim

tofutim commented Oct 14, 2026

Copy link
Copy Markdown
Member

🔍 Conductor reviewer dispatched (pid 2072382)

@tofutim tofutim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

The fix is correct: expires_at > ? filters are added in the same position/style as the existing include_superseded clause in all three searches, _now_iso() matches the %Y-%m-%dT%H:%M:%S.%fZ format used when expires_at is written (so the lexicographic comparison is valid), and SQL param order matches placeholder order. The RuntimeError→ToolError swap correctly surfaces the "workspace mode" reason to the client. Full test suite passes (900 passed, 1 skipped) including the two new tests.

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

@tofutim
tofutim merged commit 8fa3826 into srclight:develop Oct 14, 2026
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