69 lines
2.7 KiB
Python
69 lines
2.7 KiB
Python
import json
|
|
from unittest.mock import patch
|
|
|
|
from apps.admin_console.database.connection import db_session
|
|
from apps.admin_console.database.repositories.session_repository import SessionRepository
|
|
|
|
|
|
def _insert_running_session(db_path, session_id, pid):
|
|
with db_session(db_path) as conn:
|
|
conn.execute(
|
|
"INSERT INTO sessions "
|
|
"(session_id, initial_goal, start_time, status, device_info, pid) "
|
|
"VALUES (?, ?, ?, ?, ?, ?)",
|
|
(session_id, session_id, 1.0, "running", "{}", pid),
|
|
)
|
|
conn.commit()
|
|
|
|
|
|
def test_startup_cleanup_keeps_live_cross_process_session(tmp_path):
|
|
db_path = tmp_path / "sessions.db"
|
|
repository = SessionRepository(db_path)
|
|
_insert_running_session(db_path, "live", 111)
|
|
_insert_running_session(db_path, "dead", 222)
|
|
|
|
with patch.object(repository, "process_is_alive", side_effect=lambda pid: pid == 111):
|
|
assert repository.cleanup_orphans_on_startup() == 1
|
|
|
|
assert repository.get_session_status("live") == "running"
|
|
assert repository.get_session_status("dead") == "interrupted"
|
|
|
|
|
|
def test_session_list_harvest_rechecks_worker_liveness(tmp_path):
|
|
db_path = tmp_path / "sessions.db"
|
|
repository = SessionRepository(db_path)
|
|
_insert_running_session(db_path, "external-live", 333)
|
|
|
|
with patch.object(repository, "process_is_alive", return_value=True):
|
|
assert repository.harvest_orphaned_sessions(["external-live"]) == 0
|
|
|
|
assert repository.get_session_status("external-live") == "running"
|
|
|
|
|
|
def test_recorded_model_metadata_is_per_session_and_prefers_operator(tmp_path):
|
|
db_path = tmp_path / "sessions.db"
|
|
repository = SessionRepository(db_path)
|
|
for session_id in ("one", "two", "no-record"):
|
|
_insert_running_session(db_path, session_id, 1)
|
|
with db_session(db_path) as conn:
|
|
for trace_id, session_id, timestamp, payload in (
|
|
("invalid", "one", 0, "{"),
|
|
("planner", "one", 1, json.dumps({"role": "planner", "model": "other-model"})),
|
|
(
|
|
"operator",
|
|
"one",
|
|
2,
|
|
json.dumps({"role": "operator", "model": "gpt-6.1-terra", "provider": "openai"}),
|
|
),
|
|
("flash", "two", 1, json.dumps({"role": "FlashRunner", "model": "gpt-6-luna"})),
|
|
):
|
|
conn.execute(
|
|
"INSERT INTO traces(trace_id,session_id,type,timestamp,payload) VALUES (?,?,?,?,?)",
|
|
(trace_id, session_id, "llm_diagnostic", timestamp, payload),
|
|
)
|
|
conn.commit()
|
|
assert repository.get_recorded_model_info_map(["one", "two", "no-record"]) == {
|
|
"one": {"id": "gpt-6.1-terra", "provider": "openai"},
|
|
"two": {"id": "gpt-6-luna", "provider": ""},
|
|
}
|