The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui_gateway): fall back to session id when session_key is NULL in truncation persist
jdgg7776da30f72a286
diff --git a/tests/test_tui_gateway_server.py b/tests/test_tui_gateway_server.pyindex c3d957eb277..b6d17ae4219 100644--- a/tests/test_tui_gateway_server.py+++ b/tests/test_tui_gateway_server.py@@ -5053,6 +5053,56 @@ def test_prompt_submit_truncates_by_message_id(monkeypatch):server._sessions.pop("msg-id-trunc-sid", None)+def test_prompt_submit_truncation_falls_back_to_sid_when_session_key_null(monkeypatch):+ """#81904: a NULL session_key must not FK-fail the truncation persist.++ CLI-origin sessions resumed in the Desktop have no session_key; the+ truncation path used to call replace_messages(None, ...), whose reinsert+ violated the messages.session_id FK ("FOREIGN KEY constraint failed" →+ "Restore failed"). The persist must key off the session id instead —+ for CLI-origin rows the durable sessions.id IS the requested sid.+ """+ replaced = []++ class _FakeDB:+ def replace_messages(self, key, messages, active_only=False, archive_dropped=False):+ replaced.append((key, list(messages)))++ history = [+ {"role": "user", "content": "first"},+ {"role": "assistant", "content": "reply 1"},+ {"role": "user", "content": "second"},+ {"role": "assistant", "content": "reply 2"},+ ]+ server._sessions["null-key-trunc-sid"] = _session(+ history=list(history), session_key=None+ )+ monkeypatch.setattr(server, "_get_db", lambda: _FakeDB())+ monkeypatch.setattr(server, "_start_agent_build", lambda *a, **k: None)+ monkeypatch.setattr(server, "_start_inflight_turn", lambda *a, **k: None)++ try:+ resp = server.handle_request(+ {+ "id": "1",+ "method": "prompt.submit",+ "params": {+ "session_id": "null-key-trunc-sid",+ "text": "new turn",+ "truncate_before_user_ordinal": 1,+ "confirm_truncate": True,+ },+ }+ )+ assert resp.get("result") is not None+ assert len(replaced) == 1+ # Keyed by the session id, never None (the FK-violating value).+ assert replaced[0][0] == "null-key-trunc-sid"+ assert replaced[0][1] == history[:2]+ finally:+ server._sessions.pop("null-key-trunc-sid", None)++def test_prompt_submit_refuses_ordinal_and_message_id_mismatch(monkeypatch):"""#82756: A mismatch between truncate_before_user_ordinal and truncate_before_message_id must return 4030."""history = [diff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.pyindex aaf796c8a02..fd2feabd452 100644--- a/tui_gateway/methods_prompt.py+++ b/tui_gateway/methods_prompt.py@@ -582,8 +582,12 @@ def _(rid, params: dict) -> dict:# #82756). Soft-archiving keeps them on disk (active=0) and# in the FTS index, so a mis-aimed cut is recoverable# instead of terminal. The live transcript is unchanged.+ # Fall back to session id when session_key is NULL — CLI-origin+ # sessions created before the session_key default fix have no+ # key, and replace_messages(None) triggers an FK violation.+ truncation_key = session.get("session_key") or siddb.replace_messages(- session["session_key"],+ truncation_key,truncated,active_only=True,archive_dropped=True,