Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 0 additions & 218 deletions relatorio-issue-69678-sqlite-fd-leaks.md

This file was deleted.

53 changes: 53 additions & 0 deletions tests/test_tui_gateway_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8818,6 +8818,59 @@ def start(self):
server._sessions.pop("sid", None)


def test_prompt_submit_snapshots_history_after_pending_model_switch(monkeypatch):
marker = {"role": "user", "content": "[model switched]"}
seen = {}

class _Agent:
def run_conversation(self, prompt, conversation_history=None, **_kwargs):
seen["history"] = conversation_history
return {
"final_response": "reply",
"messages": [
*(conversation_history or []),
{"role": "user", "content": prompt},
{"role": "assistant", "content": "reply"},
],
}

class _ImmediateThread:
def __init__(self, target=None, **_kwargs):
self._target = target

def start(self):
self._target()

def _apply_pending(_sid, session):
with session["history_lock"]:
session["history"].append(marker)
session["history_version"] += 1

server._sessions["sid"] = _session(agent=_Agent())
server._sessions["sid"]["pending_model_switch"] = {"raw": "new-model"}
emits = []
try:
monkeypatch.setattr(server.threading, "Thread", _ImmediateThread)
monkeypatch.setattr(server, "_apply_pending_model_switch", _apply_pending)
monkeypatch.setattr(server, "_sync_agent_model_with_config", lambda *_a: None)
monkeypatch.setattr(server, "_get_usage", lambda _a: {})
monkeypatch.setattr(server, "render_message", lambda *_a: "")
monkeypatch.setattr(server, "_emit", lambda *a: emits.append(a))

server.handle_request(
{"id": "1", "method": "prompt.submit", "params": {"session_id": "sid", "text": "hi"}}
)

assert seen["history"] == [marker]
assert server._sessions["sid"]["history"][-1] == {
"role": "assistant", "content": "reply"
}
complete = [a for a in emits if a[0] == "message.complete"]
assert "warning" not in complete[0][2]
finally:
server._sessions.pop("sid", None)


def test_prompt_submit_can_truncate_before_user_ordinal(monkeypatch):
"""Desktop user-message edits should restart the turn from the edited user."""

Expand Down
7 changes: 5 additions & 2 deletions tui_gateway/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9203,8 +9203,6 @@ def _run_prompt_submit(
):
session["running"] = False
return
history = list(session["history"])
history_version = int(session.get("history_version", 0))
if image_paths is None:
images = list(session.get("attached_images", []))
session["attached_images"] = []
Expand Down Expand Up @@ -9284,6 +9282,11 @@ def run():
# config sync so an explicit pick wins over a config.yaml change.
_apply_pending_model_switch(sid, session)
_sync_agent_model_with_config(sid, session)
# Snapshot after turn-start model sync. A deferred switch mutates
# history and its version; that mutation belongs to this turn.
with session["history_lock"]:
history = list(session["history"])
history_version = int(session.get("history_version", 0))
cwd = _session_cwd(session)
_register_session_cwd(session)
cols = session.get("cols", 80)
Expand Down
Loading