Skip to content

Clear /llm schema cache on completion refresh - #2498

Open
leninworld wants to merge 1 commit into
dbcli:mainfrom
leninworld:fix-llm-schema-cache-refresh-issue-2497
Open

leninworld wants to merge 1 commit into
dbcli:mainfrom
leninworld:fix-llm-schema-cache-refresh-issue-2497

Conversation

@leninworld

Copy link
Copy Markdown
Contributor

This PR fixes issue #2497.

Description

/llm caches the schema and sample rows it puts in its prompt (SCHEMA_DATA_CACHE, SAMPLE_DATA_CACHE), keyed by database name, and nothing cleared them. After the first /llm question, a table created, altered or dropped later in the session never reached the prompt, and rehash (\#) could not pick up schema changes made from another session.

This change adds clear_llm_cache() to special_commands/llm.py (with a no-op stand-in when MYCLI_LLM_OFF is set) and calls it at the start of refresh_completions(). That method already runs after DDL, use, connect/\r and rehash (\#), so the next /llm question reads the schema and sample rows again. Within an unchanged session the cache still saves the schema queries.

Tests:

  • test_special_llm.py::test_clear_llm_cache_refreshes_schema_and_sample_data: a table added after the first call stays missing until clear_llm_cache(), then both the schema and the sample data include it and the queries run again.
  • test_client_query.py::test_refresh_completions_clears_llm_cache: refresh_completions() empties both caches.
  • test_special_commands_init.py: the export and the MYCLI_LLM_OFF stand-in.

The two new tests fail on main and pass with the change. Locally (Python 3.11, [all,dev] extras): pytest mycli_test/pytests 3893 passed, 88 skipped, 8 xfailed; ruff check, ruff format and mypy --install-types . clean. The behave tests need a MySQL server and were not run.

Not covered, since they follow when mycli refreshes completions: data changes (INSERT/DELETE) keep the cached sample rows, and DDL run through source file.sql, or in a multi-statement input that fails after the DDL, doesn't trigger a refresh.

Fixes #2497

Checklist

  • I added this contribution to the changelog.md file.
  • I added my name to the mycli/AUTHORS.txt file (or it's already there).
  • To lint and format the code, I ran
    uv run ruff check && uv run ruff format && uv run mypy --install-types .

This branch has not been deployed

No deployments
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.

/llm keeps the schema from its first call for the rest of the session

1 participant