mirror of
https://github.com/escalante29/WealthySmart.git
synced 2026-07-17 11:08:47 +02:00
Assistant: stop stale clients from resurrecting cleared threads
Root cause of the "chunks" clear bug: conversation history is server-owned, but MAF seeds a thread from whatever the client sends when no snapshot exists. Any stale tab (open across a clear or from an older session) still holds the old conversation in memory, and its next question re-persisted the whole thing — clears appeared to peel off one layer at a time. The agent middleware now drops client-sent history for threads with no stored snapshot, keeping only the last user turn (fresh conversations and empty-messages hydration requests pass through untouched). Verified live: simulated a cleared-thread stale tab, asked from it, and only the new turn was persisted. Also: clearThread surfaces DELETE failures as a toast instead of silently reloading, and conftest sets a dummy OPENAI_API_KEY so tests can import app.main. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -129,3 +129,39 @@ def test_clear_thread_endpoint(monkeypatch, engine, session):
|
||||
|
||||
assert clear_thread(session=session, _user="testadmin") == {"cleared": True}
|
||||
assert asyncio.run(store.get(scope="default", thread_id="main")) is None
|
||||
|
||||
|
||||
def test_drop_stale_client_history(session, monkeypatch, engine):
|
||||
from app.main import _drop_stale_client_history
|
||||
|
||||
history = [
|
||||
{"role": "user", "content": "vieja pregunta"},
|
||||
{"role": "assistant", "content": "vieja respuesta"},
|
||||
{"role": "tool", "content": "x", "toolCallId": "t1"},
|
||||
{"role": "user", "content": "nueva pregunta"},
|
||||
]
|
||||
|
||||
# No stored snapshot for the thread → stale client: keep last user turn.
|
||||
assert _drop_stale_client_history(session, "main", list(history)) == [
|
||||
{"role": "user", "content": "nueva pregunta"}
|
||||
]
|
||||
|
||||
# Fresh conversation (no assistant/tool yet) passes through.
|
||||
fresh = [{"role": "user", "content": "hola"}]
|
||||
assert _drop_stale_client_history(session, "main", list(fresh)) == fresh
|
||||
|
||||
# Empty messages (hydration trigger) pass through.
|
||||
assert _drop_stale_client_history(session, "main", []) == []
|
||||
|
||||
# With a stored snapshot the full history is legitimate.
|
||||
store = _store(monkeypatch, engine)
|
||||
asyncio.run(
|
||||
store.save(
|
||||
scope="default",
|
||||
thread_id="main",
|
||||
snapshot=AGUIThreadSnapshot(
|
||||
messages=[{"id": "u1", "role": "user", "content": "hola"}]
|
||||
),
|
||||
)
|
||||
)
|
||||
assert _drop_stale_client_history(session, "main", list(history)) == history
|
||||
|
||||
Reference in New Issue
Block a user